annotate Objects/rangeobject.c @ 66846:2cf7bb2bbfb8

Remove reference to stuff which is already obsolete in 2.x.
author Antoine Pitrou <solipsis@pitrou.net>
date Sun, 12 Dec 2010 18:12:40 +0000
parents 62b61abd02b8
children fef521f45af3
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
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
5 /* Support objects whose length is > PY_SSIZE_T_MAX.
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
6
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
7 This could be sped up for small PyLongs if they fit in an Py_ssize_t.
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
8 This only matters on Win64. Though we could use PY_LONG_LONG which
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
9 would presumably help perf.
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
10 */
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
11
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 typedef struct {
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
13 PyObject_HEAD
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
14 PyObject *start;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
15 PyObject *stop;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
16 PyObject *step;
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
17 PyObject *length;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 } rangeobject;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
20 /* Helper function for validating step. Always returns a new reference or
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
21 NULL on error.
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
22 */
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
23 static PyObject *
66335
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
24 validate_step(PyObject *step)
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
25 {
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
26 /* No step specified, use a step of 1. */
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
27 if (!step)
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 44061
diff changeset
28 return PyLong_FromLong(1);
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
29
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
30 step = PyNumber_Index(step);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
31 if (step) {
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
32 Py_ssize_t istep = PyNumber_AsSsize_t(step, NULL);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
33 if (istep == -1 && PyErr_Occurred()) {
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
34 /* Ignore OverflowError, we know the value isn't 0. */
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
35 PyErr_Clear();
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
36 }
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
37 else if (istep == 0) {
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
38 PyErr_SetString(PyExc_ValueError,
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
39 "range() arg 3 must not be zero");
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
40 Py_CLEAR(step);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
41 }
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
42 }
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
43
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
44 return step;
23695
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
45 }
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
46
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
47 static PyObject *
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
48 compute_range_length(PyObject *start, PyObject *stop, PyObject *step);
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
49
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
50 static rangeobject *
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
51 make_range_object(PyTypeObject *type, PyObject *start,
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
52 PyObject *stop, PyObject *step)
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
53 {
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
54 rangeobject *obj = NULL;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
55 PyObject *length;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
56 length = compute_range_length(start, stop, step);
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
57 if (length == NULL) {
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
58 return NULL;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
59 }
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
60 obj = PyObject_New(rangeobject, type);
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
61 if (obj == NULL) {
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
62 Py_DECREF(length);
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
63 return NULL;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
64 }
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
65 obj->start = start;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
66 obj->stop = stop;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
67 obj->step = step;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
68 obj->length = length;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
69 return obj;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
70 }
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
71
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
72 /* XXX(nnorwitz): should we error check if the user passes any empty ranges?
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
73 range(-10)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
74 range(0, -5)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
75 range(0, 5, -1)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
76 */
23695
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
77 static PyObject *
66335
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
78 range_new(PyTypeObject *type, PyObject *args, PyObject *kw)
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
79 {
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
80 rangeobject *obj;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
81 PyObject *start = NULL, *stop = NULL, *step = NULL;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
82
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
83 if (!_PyArg_NoKeywords("range()", kw))
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
84 return NULL;
23695
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
85
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
86 if (PyTuple_Size(args) <= 1) {
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
87 if (!PyArg_UnpackTuple(args, "range", 1, 1, &stop))
54726
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
88 return NULL;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
89 stop = PyNumber_Index(stop);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
90 if (!stop)
54726
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
91 return NULL;
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 44061
diff changeset
92 start = PyLong_FromLong(0);
54726
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
93 if (!start) {
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
94 Py_DECREF(stop);
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
95 return NULL;
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
96 }
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 44061
diff changeset
97 step = PyLong_FromLong(1);
54726
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
98 if (!step) {
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
99 Py_DECREF(stop);
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
100 Py_DECREF(start);
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
101 return NULL;
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
102 }
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
103 }
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
104 else {
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
105 if (!PyArg_UnpackTuple(args, "range", 2, 3,
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
106 &start, &stop, &step))
54726
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
107 return NULL;
35449
1fdfd0130e28 Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents: 34087
diff changeset
108
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
109 /* Convert borrowed refs to owned refs */
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
110 start = PyNumber_Index(start);
54726
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
111 if (!start)
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
112 return NULL;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
113 stop = PyNumber_Index(stop);
54726
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
114 if (!stop) {
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
115 Py_DECREF(start);
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
116 return NULL;
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
117 }
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
118 step = validate_step(step); /* Caution, this can clear exceptions */
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
119 if (!step) {
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
120 Py_DECREF(start);
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
121 Py_DECREF(stop);
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
122 return NULL;
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
123 }
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
124 }
33047
c81ed079576f Bug 1003935: xrange overflows
Tim Peters <tim.peters@gmail.com>
parents: 33046
diff changeset
125
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
126 obj = make_range_object(type, start, stop, step);
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
127 if (obj != NULL)
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
128 return (PyObject *) obj;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
129
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
130 /* Failed to create object, release attributes */
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
131 Py_XDECREF(start);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
132 Py_XDECREF(stop);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
133 Py_XDECREF(step);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
134 return NULL;
23695
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
135 }
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
136
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23702
diff changeset
137 PyDoc_STRVAR(range_doc,
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
138 "range([start,] stop[, step]) -> range object\n\
23695
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
139 \n\
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
140 Returns a virtual sequence of numbers from start to stop by step.");
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
141
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
142 static void
66335
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
143 range_dealloc(rangeobject *r)
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
144 {
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
145 Py_DECREF(r->start);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
146 Py_DECREF(r->stop);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
147 Py_DECREF(r->step);
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
148 Py_DECREF(r->length);
43867
f444e259341a Found another memory leak in longrangeiter. And redo the previous correction
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43861
diff changeset
149 PyObject_Del(r);
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
150 }
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
151
55589
546aa134d773 Merged revisions 73656,73658,73663,73666 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54847
diff changeset
152 /* Return number of items in range (lo, hi, step) as a PyLong object,
546aa134d773 Merged revisions 73656,73658,73663,73666 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54847
diff changeset
153 * when arguments are PyLong objects. Arguments MUST return 1 with
546aa134d773 Merged revisions 73656,73658,73663,73666 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54847
diff changeset
154 * PyLong_Check(). Return NULL when there is an error.
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
155 */
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
156 static PyObject*
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
157 compute_range_length(PyObject *start, PyObject *stop, PyObject *step)
66335
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
158 {
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
159 /* -------------------------------------------------------------
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
160 Algorithm is equal to that of get_len_of_range(), but it operates
54846
79a630da8f00 fix comment
Benjamin Peterson <benjamin@python.org>
parents: 54845
diff changeset
161 on PyObjects (which are assumed to be PyLong objects).
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
162 ---------------------------------------------------------------*/
51244
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47536
diff changeset
163 int cmp_result;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
164 PyObject *lo, *hi;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
165 PyObject *diff = NULL;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
166 PyObject *one = NULL;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
167 PyObject *tmp1 = NULL, *tmp2 = NULL, *result;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
168 /* holds sub-expression evaluations */
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
169
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
170 PyObject *zero = PyLong_FromLong(0);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
171 if (zero == NULL)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
172 return NULL;
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
173 cmp_result = PyObject_RichCompareBool(step, zero, Py_GT);
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
174 Py_DECREF(zero);
51244
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47536
diff changeset
175 if (cmp_result == -1)
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
176 return NULL;
23695
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
177
51244
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47536
diff changeset
178 if (cmp_result == 1) {
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
179 lo = start;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
180 hi = stop;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
181 Py_INCREF(step);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
182 } else {
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
183 lo = stop;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
184 hi = start;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
185 step = PyNumber_Negative(step);
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
186 if (!step)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
187 return NULL;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
188 }
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
189
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
190 /* if (lo >= hi), return length of 0. */
51244
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47536
diff changeset
191 if (PyObject_RichCompareBool(lo, hi, Py_GE) == 1) {
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
192 Py_XDECREF(step);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
193 return PyLong_FromLong(0);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
194 }
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
195
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
196 if ((one = PyLong_FromLong(1L)) == NULL)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
197 goto Fail;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
198
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
199 if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
200 goto Fail;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
201
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
202 if ((diff = PyNumber_Subtract(tmp1, one)) == NULL)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
203 goto Fail;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
204
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
205 if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
206 goto Fail;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
207
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
208 if ((result = PyNumber_Add(tmp2, one)) == NULL)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
209 goto Fail;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
210
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
211 Py_DECREF(tmp2);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
212 Py_DECREF(diff);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
213 Py_DECREF(step);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
214 Py_DECREF(tmp1);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
215 Py_DECREF(one);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
216 return result;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
217
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
218 Fail:
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
219 Py_XDECREF(tmp2);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
220 Py_XDECREF(diff);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
221 Py_XDECREF(step);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
222 Py_XDECREF(tmp1);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
223 Py_XDECREF(one);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
224 return NULL;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
226
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
227 static Py_ssize_t
66335
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
228 range_length(rangeobject *r)
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
229 {
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
230 return PyLong_AsSsize_t(r->length);
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
231 }
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
232
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
233 /* range(...)[x] is necessary for: seq[:] = range(...) */
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
234 static PyObject *
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
235 compute_range_item(rangeobject *r, Py_ssize_t i)
66335
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
236 {
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
237 PyObject *rem, *incr, *result;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
238
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
239 /* XXX(nnorwitz): optimize for short ints. */
45249
aebd9c4d19ed Merge r60673
Raymond Hettinger <python@rcn.com>
parents: 44366
diff changeset
240 rem = PyLong_FromSsize_t(i);
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
241 if (!rem)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
242 return NULL;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
243 incr = PyNumber_Multiply(rem, r->step);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
244 Py_DECREF(rem);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
245 if (!incr)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
246 return NULL;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
247 result = PyNumber_Add(r->start, incr);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
248 Py_DECREF(incr);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
249 return result;
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
250 }
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
251
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
252 static PyObject *
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
253 range_item(rangeobject *r, Py_ssize_t i)
66335
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
254 {
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
255 /* XXX(nnorwitz): should we support range[x] where x > PY_SSIZE_T_MAX? */
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
256 Py_ssize_t len = range_length(r);
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
257
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
258 if (i < 0)
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
259 i += len;
13848
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
260
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
261 if (i < 0 || i >= len) {
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
262 /* Also handles case where len < 0 due to (e.g) OverflowError */
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
263 if (!PyErr_Occurred())
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
264 PyErr_SetString(PyExc_IndexError,
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
265 "range object index out of range");
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
266 return NULL;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
267 }
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
268 return compute_range_item(r, i);
47536
33492c241dab Issue 2582: Fix pickling of range objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46564
diff changeset
269 }
33492c241dab Issue 2582: Fix pickling of range objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46564
diff changeset
270
64725
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
271 /* Assumes (PyLong_CheckExact(ob) || PyBool_Check(ob)) */
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
272 static int
66335
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
273 range_contains_long(rangeobject *r, PyObject *ob)
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
274 {
64725
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
275 int cmp1, cmp2, cmp3;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
276 PyObject *tmp1 = NULL;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
277 PyObject *tmp2 = NULL;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
278 PyObject *zero = NULL;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
279 int result = -1;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
280
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
281 zero = PyLong_FromLong(0);
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
282 if (zero == NULL) /* MemoryError in int(0) */
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
283 goto end;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
284
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
285 /* Check if the value can possibly be in the range. */
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
286
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
287 cmp1 = PyObject_RichCompareBool(r->step, zero, Py_GT);
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
288 if (cmp1 == -1)
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
289 goto end;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
290 if (cmp1 == 1) { /* positive steps: start <= ob < stop */
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
291 cmp2 = PyObject_RichCompareBool(r->start, ob, Py_LE);
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
292 cmp3 = PyObject_RichCompareBool(ob, r->stop, Py_LT);
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
293 }
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
294 else { /* negative steps: stop < ob <= start */
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
295 cmp2 = PyObject_RichCompareBool(ob, r->start, Py_LE);
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
296 cmp3 = PyObject_RichCompareBool(r->stop, ob, Py_LT);
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
297 }
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
298
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
299 if (cmp2 == -1 || cmp3 == -1) /* TypeError */
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
300 goto end;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
301 if (cmp2 == 0 || cmp3 == 0) { /* ob outside of range */
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
302 result = 0;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
303 goto end;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
304 }
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
305
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
306 /* Check that the stride does not invalidate ob's membership. */
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
307 tmp1 = PyNumber_Subtract(ob, r->start);
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
308 if (tmp1 == NULL)
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
309 goto end;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
310 tmp2 = PyNumber_Remainder(tmp1, r->step);
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
311 if (tmp2 == NULL)
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
312 goto end;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
313 /* result = (int(ob) - start % step) == 0 */
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
314 result = PyObject_RichCompareBool(tmp2, zero, Py_EQ);
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
315 end:
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
316 Py_XDECREF(tmp1);
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
317 Py_XDECREF(tmp2);
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
318 Py_XDECREF(zero);
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
319 return result;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
320 }
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
321
56192
d599a3f2e72d Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
322 static int
66335
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
323 range_contains(rangeobject *r, PyObject *ob)
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
324 {
64725
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
325 if (PyLong_CheckExact(ob) || PyBool_Check(ob))
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
326 return range_contains_long(r, ob);
56192
d599a3f2e72d Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
327
d599a3f2e72d Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
328 return (int)_PySequence_IterSearch((PyObject*)r, ob,
d599a3f2e72d Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
329 PY_ITERSEARCH_CONTAINS);
d599a3f2e72d Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
330 }
d599a3f2e72d Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
331
64725
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
332 static PyObject *
66335
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
333 range_count(rangeobject *r, PyObject *ob)
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
334 {
64725
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
335 if (PyLong_CheckExact(ob) || PyBool_Check(ob)) {
66330
add7e9721f80 Add error handling in range_count.
Georg Brandl <georg@python.org>
parents: 66329
diff changeset
336 int result = range_contains_long(r, ob);
add7e9721f80 Add error handling in range_count.
Georg Brandl <georg@python.org>
parents: 66329
diff changeset
337 if (result == -1)
add7e9721f80 Add error handling in range_count.
Georg Brandl <georg@python.org>
parents: 66329
diff changeset
338 return NULL;
add7e9721f80 Add error handling in range_count.
Georg Brandl <georg@python.org>
parents: 66329
diff changeset
339 else if (result)
66329
238caaf6852b count() should return integers #10474
Benjamin Peterson <benjamin@python.org>
parents: 64725
diff changeset
340 return PyLong_FromLong(1);
64725
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
341 else
66329
238caaf6852b count() should return integers #10474
Benjamin Peterson <benjamin@python.org>
parents: 64725
diff changeset
342 return PyLong_FromLong(0);
64725
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
343 } else {
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
344 Py_ssize_t count;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
345 count = _PySequence_IterSearch((PyObject*)r, ob, PY_ITERSEARCH_COUNT);
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
346 if (count == -1)
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
347 return NULL;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
348 return PyLong_FromSsize_t(count);
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
349 }
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
350 }
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
351
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
352 static PyObject *
66335
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
353 range_index(rangeobject *r, PyObject *ob)
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
354 {
64725
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
355 int contains;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
356
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
357 if (!PyLong_CheckExact(ob) && !PyBool_Check(ob)) {
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
358 Py_ssize_t index;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
359 index = _PySequence_IterSearch((PyObject*)r, ob, PY_ITERSEARCH_INDEX);
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
360 if (index == -1)
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
361 return NULL;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
362 return PyLong_FromSsize_t(index);
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
363 }
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
364
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
365 contains = range_contains_long(r, ob);
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
366 if (contains == -1)
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
367 return NULL;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
368
66331
e01237831ad2 code style and simplification
Benjamin Peterson <benjamin@python.org>
parents: 66330
diff changeset
369 if (contains) {
e01237831ad2 code style and simplification
Benjamin Peterson <benjamin@python.org>
parents: 66330
diff changeset
370 PyObject *idx, *tmp = PyNumber_Subtract(ob, r->start);
e01237831ad2 code style and simplification
Benjamin Peterson <benjamin@python.org>
parents: 66330
diff changeset
371 if (tmp == NULL)
e01237831ad2 code style and simplification
Benjamin Peterson <benjamin@python.org>
parents: 66330
diff changeset
372 return NULL;
e01237831ad2 code style and simplification
Benjamin Peterson <benjamin@python.org>
parents: 66330
diff changeset
373 /* idx = (ob - r.start) // r.step */
e01237831ad2 code style and simplification
Benjamin Peterson <benjamin@python.org>
parents: 66330
diff changeset
374 idx = PyNumber_FloorDivide(tmp, r->step);
e01237831ad2 code style and simplification
Benjamin Peterson <benjamin@python.org>
parents: 66330
diff changeset
375 Py_DECREF(tmp);
e01237831ad2 code style and simplification
Benjamin Peterson <benjamin@python.org>
parents: 66330
diff changeset
376 return idx;
e01237831ad2 code style and simplification
Benjamin Peterson <benjamin@python.org>
parents: 66330
diff changeset
377 }
64725
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
378
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
379 /* object is not in the range */
66331
e01237831ad2 code style and simplification
Benjamin Peterson <benjamin@python.org>
parents: 66330
diff changeset
380 PyErr_Format(PyExc_ValueError, "%R is not in range", ob);
64725
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
381 return NULL;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
382 }
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
383
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
384 static PySequenceMethods range_as_sequence = {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
385 (lenfunc)range_length, /* sq_length */
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
386 0, /* sq_concat */
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
387 0, /* sq_repeat */
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
388 (ssizeargfunc)range_item, /* sq_item */
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
389 0, /* sq_slice */
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
390 0, /* sq_ass_item */
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
391 0, /* sq_ass_slice */
56192
d599a3f2e72d Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
392 (objobjproc)range_contains, /* sq_contains */
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
393 };
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
394
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
395 static PyObject *
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
396 range_repr(rangeobject *r)
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
397 {
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
398 Py_ssize_t istep;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
399
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
400 /* Check for special case values for printing. We don't always
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
401 need the step value. We don't care about errors
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
402 (it means overflow), so clear the errors. */
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
403 istep = PyNumber_AsSsize_t(r->step, NULL);
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
404 if (istep != 1 || (istep == -1 && PyErr_Occurred())) {
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
405 PyErr_Clear();
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
406 }
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
407
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
408 if (istep == 1)
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
409 return PyUnicode_FromFormat("range(%R, %R)", r->start, r->stop);
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
410 else
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
411 return PyUnicode_FromFormat("range(%R, %R, %R)",
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
412 r->start, r->stop, r->step);
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
413 }
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
414
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
415 /* Pickling support */
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
416 static PyObject *
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
417 range_reduce(rangeobject *r, PyObject *args)
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
418 {
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
419 return Py_BuildValue("(O(OOO))", Py_TYPE(r),
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
420 r->start, r->stop, r->step);
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
421 }
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
422
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
423 static PyObject *
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
424 range_subscript(rangeobject* self, PyObject* item)
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
425 {
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
426 if (PyIndex_Check(item)) {
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
427 Py_ssize_t i;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
428 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
429 if (i == -1 && PyErr_Occurred())
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
430 return NULL;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
431 return range_item(self, i);
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
432 }
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
433 if (PySlice_Check(item)) {
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
434 Py_ssize_t start, stop, step, len, rlen;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
435 rangeobject *result;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
436 PyObject *substart = NULL, *substep = NULL, *substop = NULL;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
437
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
438 rlen = range_length(self);
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
439 if (rlen < 0) {
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
440 return NULL;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
441 }
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
442
66664
62b61abd02b8 Merge branches/pep-0384.
Martin v. Löwis <martin@v.loewis.de>
parents: 66641
diff changeset
443 if (PySlice_GetIndicesEx(item, rlen,
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
444 &start, &stop, &step, &len) < 0) {
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
445 return NULL;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
446 }
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
447 if (step == 1) {
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
448 substep = self->step;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
449 Py_INCREF(substep);
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
450 } else {
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
451 /* NB: slice step != Py_None here */
66664
62b61abd02b8 Merge branches/pep-0384.
Martin v. Löwis <martin@v.loewis.de>
parents: 66641
diff changeset
452 substep = PyNumber_Multiply(self->step, ((PySliceObject*)item)->step);
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
453 if (substep == NULL)
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
454 goto fail;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
455 }
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
456 substart = compute_range_item(self, start);
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
457 if (substart == NULL)
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
458 goto fail;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
459 if (len <= 0) {
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
460 substop = substart;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
461 Py_INCREF(substop);
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
462 }
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
463 else {
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
464 substop = compute_range_item(self, stop);
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
465 if (substop == NULL)
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
466 goto fail;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
467 }
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
468 result = make_range_object(Py_TYPE(self), substart, substop, substep);
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
469 if (result != NULL)
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
470 return (PyObject *) result;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
471 fail:
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
472 Py_XDECREF(substart);
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
473 Py_XDECREF(substep);
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
474 Py_XDECREF(substop);
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
475 return NULL;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
476 }
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
477 PyErr_Format(PyExc_TypeError,
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
478 "range indices must be integers or slices, not %.200s",
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
479 item->ob_type->tp_name);
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
480 return NULL;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
481 }
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
482
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
483
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
484 static PyMappingMethods range_as_mapping = {
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
485 (lenfunc)range_length, /* mp_length */
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
486 (binaryfunc)range_subscript, /* mp_subscript */
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
487 (objobjargproc)0, /* mp_ass_subscript */
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
488 };
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
489
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24260
diff changeset
490 static PyObject * range_iter(PyObject *seq);
30865
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
491 static PyObject * range_reverse(PyObject *seq);
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
492
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
493 PyDoc_STRVAR(reverse_doc,
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
494 "Returns a reverse iterator.");
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
495
64725
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
496 PyDoc_STRVAR(count_doc,
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
497 "rangeobject.count(value) -> integer -- return number of occurrences of value");
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
498
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
499 PyDoc_STRVAR(index_doc,
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
500 "rangeobject.index(value, [start, [stop]]) -> integer -- return index of value.\n"
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
501 "Raises ValueError if the value is not present.");
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
502
30865
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
503 static PyMethodDef range_methods[] = {
64725
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
504 {"__reversed__", (PyCFunction)range_reverse, METH_NOARGS, reverse_doc},
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
505 {"__reduce__", (PyCFunction)range_reduce, METH_VARARGS},
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
506 {"count", (PyCFunction)range_count, METH_O, count_doc},
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
507 {"index", (PyCFunction)range_index, METH_O, index_doc},
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
508 {NULL, NULL} /* sentinel */
30865
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
509 };
23693
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
510
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
511 PyTypeObject PyRange_Type = {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
512 PyVarObject_HEAD_INIT(&PyType_Type, 0)
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
513 "range", /* Name of this type */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
514 sizeof(rangeobject), /* Basic object size */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
515 0, /* Item size for varobject */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
516 (destructor)range_dealloc, /* tp_dealloc */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
517 0, /* tp_print */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
518 0, /* tp_getattr */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
519 0, /* tp_setattr */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
520 0, /* tp_reserved */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
521 (reprfunc)range_repr, /* tp_repr */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
522 0, /* tp_as_number */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
523 &range_as_sequence, /* tp_as_sequence */
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
524 &range_as_mapping, /* tp_as_mapping */
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
525 0, /* tp_hash */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
526 0, /* tp_call */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
527 0, /* tp_str */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
528 PyObject_GenericGetAttr, /* tp_getattro */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
529 0, /* tp_setattro */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
530 0, /* tp_as_buffer */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
531 Py_TPFLAGS_DEFAULT, /* tp_flags */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
532 range_doc, /* tp_doc */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
533 0, /* tp_traverse */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
534 0, /* tp_clear */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
535 0, /* tp_richcompare */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
536 0, /* tp_weaklistoffset */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
537 range_iter, /* tp_iter */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
538 0, /* tp_iternext */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
539 range_methods, /* tp_methods */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
540 0, /* tp_members */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
541 0, /* tp_getset */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
542 0, /* tp_base */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
543 0, /* tp_dict */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
544 0, /* tp_descr_get */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
545 0, /* tp_descr_set */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
546 0, /* tp_dictoffset */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
547 0, /* tp_init */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
548 0, /* tp_alloc */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
549 range_new, /* tp_new */
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
550 };
23693
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
551
41803
3d3bf27409e6 Rename test_xrange.py to test_range.py and fix the
Walter Dörwald <walter@livinglogic.de>
parents: 41800
diff changeset
552 /*********************** range Iterator **************************/
23693
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
553
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
554 /* There are 2 types of iterators, one for C longs, the other for
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
555 Python longs (ie, PyObjects). This should make iteration fast
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
556 in the normal case, but possible for any numeric value.
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
557 */
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
558
23693
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
559 typedef struct {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
560 PyObject_HEAD
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
561 long index;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
562 long start;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
563 long step;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
564 long len;
23693
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
565 } rangeiterobject;
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
566
30865
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
567 static PyObject *
66335
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
568 rangeiter_next(rangeiterobject *r)
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
569 {
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
570 if (r->index < r->len)
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
571 /* cast to unsigned to avoid possible signed overflow
57267
3ebf907db028 Fix another case of potential signed overflow.
Mark Dickinson <dickinsm@gmail.com>
parents: 57262
diff changeset
572 in intermediate calculations. */
3ebf907db028 Fix another case of potential signed overflow.
Mark Dickinson <dickinsm@gmail.com>
parents: 57262
diff changeset
573 return PyLong_FromLong((long)(r->start +
3ebf907db028 Fix another case of potential signed overflow.
Mark Dickinson <dickinsm@gmail.com>
parents: 57262
diff changeset
574 (unsigned long)(r->index++) * r->step));
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
575 return NULL;
23693
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
576 }
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
577
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
578 static PyObject *
66335
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
579 rangeiter_len(rangeiterobject *r)
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
580 {
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 44061
diff changeset
581 return PyLong_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
582 }
04eb2ebb46e2 Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents: 30865
diff changeset
583
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
584 PyDoc_STRVAR(length_hint_doc,
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
585 "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
586
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
587 static PyMethodDef rangeiter_methods[] = {
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
588 {"__length_hint__", (PyCFunction)rangeiter_len, METH_NOARGS,
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
589 length_hint_doc},
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
590 {NULL, NULL} /* sentinel */
31635
04eb2ebb46e2 Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents: 30865
diff changeset
591 };
04eb2ebb46e2 Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents: 30865
diff changeset
592
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
593 static PyObject *rangeiter_new(PyTypeObject *, PyObject *args, PyObject *kw);
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
594
44061
0cc552ab76bd Added all PyTypeObjects to the appropriate header files.
Christian Heimes <christian@cheimes.de>
parents: 44043
diff changeset
595 PyTypeObject PyRangeIter_Type = {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
596 PyVarObject_HEAD_INIT(&PyType_Type, 0)
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
597 "range_iterator", /* tp_name */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
598 sizeof(rangeiterobject), /* tp_basicsize */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
599 0, /* tp_itemsize */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
600 /* methods */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
601 (destructor)PyObject_Del, /* tp_dealloc */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
602 0, /* tp_print */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
603 0, /* tp_getattr */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
604 0, /* tp_setattr */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
605 0, /* tp_reserved */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
606 0, /* tp_repr */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
607 0, /* tp_as_number */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
608 0, /* tp_as_sequence */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
609 0, /* tp_as_mapping */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
610 0, /* tp_hash */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
611 0, /* tp_call */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
612 0, /* tp_str */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
613 PyObject_GenericGetAttr, /* tp_getattro */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
614 0, /* tp_setattro */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
615 0, /* tp_as_buffer */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
616 Py_TPFLAGS_DEFAULT, /* tp_flags */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
617 0, /* tp_doc */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
618 0, /* tp_traverse */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
619 0, /* tp_clear */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
620 0, /* tp_richcompare */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
621 0, /* tp_weaklistoffset */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
622 PyObject_SelfIter, /* tp_iter */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
623 (iternextfunc)rangeiter_next, /* tp_iternext */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
624 rangeiter_methods, /* tp_methods */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
625 0, /* tp_members */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
626 0, /* tp_getset */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
627 0, /* tp_base */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
628 0, /* tp_dict */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
629 0, /* tp_descr_get */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
630 0, /* tp_descr_set */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
631 0, /* tp_dictoffset */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
632 0, /* tp_init */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
633 0, /* tp_alloc */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
634 rangeiter_new, /* tp_new */
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
635 };
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
636
57261
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
637 /* Return number of items in range (lo, hi, step). step != 0
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
638 * required. The result always fits in an unsigned long.
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
639 */
57261
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
640 static unsigned long
66335
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
641 get_len_of_range(long lo, long hi, long step)
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
642 {
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
643 /* -------------------------------------------------------------
57261
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
644 If step > 0 and lo >= hi, or step < 0 and lo <= hi, the range is empty.
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
645 Else for step > 0, if n values are in the range, the last one is
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
646 lo + (n-1)*step, which must be <= hi-1. Rearranging,
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
647 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
648 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
649 the RHS is non-negative and so truncation is the same as the
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
650 floor. Letting M be the largest positive long, the worst case
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
651 for the RHS numerator is hi=M, lo=-M-1, and then
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
652 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
57261
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
653 precision to compute the RHS exactly. The analysis for step < 0
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
654 is similar.
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
655 ---------------------------------------------------------------*/
57261
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
656 assert(step != 0);
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
657 if (step > 0 && lo < hi)
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
658 return 1UL + (hi - 1UL - lo) / step;
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
659 else if (step < 0 && lo > hi)
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
660 return 1UL + (lo - 1UL - hi) / (0UL - step);
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
661 else
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
662 return 0UL;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
663 }
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
664
57261
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
665 /* Initialize a rangeiter object. If the length of the rangeiter object
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
666 is not representable as a C long, OverflowError is raised. */
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
667
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
668 static PyObject *
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
669 fast_range_iter(long start, long stop, long step)
66335
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
670 {
44061
0cc552ab76bd Added all PyTypeObjects to the appropriate header files.
Christian Heimes <christian@cheimes.de>
parents: 44043
diff changeset
671 rangeiterobject *it = PyObject_New(rangeiterobject, &PyRangeIter_Type);
57261
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
672 unsigned long ulen;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
673 if (it == NULL)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
674 return NULL;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
675 it->start = start;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
676 it->step = step;
57261
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
677 ulen = get_len_of_range(start, stop, step);
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
678 if (ulen > (unsigned long)LONG_MAX) {
57286
eb235eb45d32 fix one visible and several possible refleaks in rangeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 57267
diff changeset
679 Py_DECREF(it);
57261
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
680 PyErr_SetString(PyExc_OverflowError,
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
681 "range too large to represent as a range_iterator");
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
682 return NULL;
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
683 }
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
684 it->len = (long)ulen;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
685 it->index = 0;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
686 return (PyObject *)it;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
687 }
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
688
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
689 static PyObject *
66335
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
690 rangeiter_new(PyTypeObject *type, PyObject *args, PyObject *kw)
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
691 {
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
692 long start, stop, step;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
693
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
694 if (!_PyArg_NoKeywords("rangeiter()", kw))
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
695 return NULL;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
696
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
697 if (!PyArg_ParseTuple(args, "lll;rangeiter() requires 3 int arguments",
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
698 &start, &stop, &step))
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
699 return NULL;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
700
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
701 return fast_range_iter(start, stop, step);
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
702 }
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
703
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
704 typedef struct {
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
705 PyObject_HEAD
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
706 PyObject *index;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
707 PyObject *start;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
708 PyObject *step;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
709 PyObject *len;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
710 } longrangeiterobject;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
711
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
712 static PyObject *
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
713 longrangeiter_len(longrangeiterobject *r, PyObject *no_args)
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
714 {
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
715 return PyNumber_Subtract(r->len, r->index);
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
716 }
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
717
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
718 static PyMethodDef longrangeiter_methods[] = {
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
719 {"__length_hint__", (PyCFunction)longrangeiter_len, METH_NOARGS,
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
720 length_hint_doc},
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
721 {NULL, NULL} /* sentinel */
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
722 };
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
723
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
724 static void
66335
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
725 longrangeiter_dealloc(longrangeiterobject *r)
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
726 {
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
727 Py_XDECREF(r->index);
41644
9cc4c3f0b580 Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
728 Py_XDECREF(r->start);
9cc4c3f0b580 Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
729 Py_XDECREF(r->step);
9cc4c3f0b580 Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
730 Py_XDECREF(r->len);
43867
f444e259341a Found another memory leak in longrangeiter. And redo the previous correction
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43861
diff changeset
731 PyObject_Del(r);
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
732 }
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
733
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
734 static PyObject *
66335
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
735 longrangeiter_next(longrangeiterobject *r)
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
736 {
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
737 PyObject *one, *product, *new_index, *result;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
738 if (PyObject_RichCompareBool(r->index, r->len, Py_LT) != 1)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
739 return NULL;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
740
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
741 one = PyLong_FromLong(1);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
742 if (!one)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
743 return NULL;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
744
57286
eb235eb45d32 fix one visible and several possible refleaks in rangeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 57267
diff changeset
745 new_index = PyNumber_Add(r->index, one);
eb235eb45d32 fix one visible and several possible refleaks in rangeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 57267
diff changeset
746 Py_DECREF(one);
eb235eb45d32 fix one visible and several possible refleaks in rangeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 57267
diff changeset
747 if (!new_index)
eb235eb45d32 fix one visible and several possible refleaks in rangeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 57267
diff changeset
748 return NULL;
eb235eb45d32 fix one visible and several possible refleaks in rangeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 57267
diff changeset
749
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
750 product = PyNumber_Multiply(r->index, r->step);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
751 if (!product) {
57286
eb235eb45d32 fix one visible and several possible refleaks in rangeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 57267
diff changeset
752 Py_DECREF(new_index);
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
753 return NULL;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
754 }
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
755
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
756 result = PyNumber_Add(r->start, product);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
757 Py_DECREF(product);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
758 if (result) {
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
759 Py_DECREF(r->index);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
760 r->index = new_index;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
761 }
57286
eb235eb45d32 fix one visible and several possible refleaks in rangeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 57267
diff changeset
762 else {
eb235eb45d32 fix one visible and several possible refleaks in rangeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 57267
diff changeset
763 Py_DECREF(new_index);
eb235eb45d32 fix one visible and several possible refleaks in rangeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 57267
diff changeset
764 }
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
765
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
766 return result;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
767 }
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
768
44061
0cc552ab76bd Added all PyTypeObjects to the appropriate header files.
Christian Heimes <christian@cheimes.de>
parents: 44043
diff changeset
769 PyTypeObject PyLongRangeIter_Type = {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
770 PyVarObject_HEAD_INIT(&PyType_Type, 0)
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
771 "longrange_iterator", /* tp_name */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
772 sizeof(longrangeiterobject), /* tp_basicsize */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
773 0, /* tp_itemsize */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
774 /* methods */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
775 (destructor)longrangeiter_dealloc, /* tp_dealloc */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
776 0, /* tp_print */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
777 0, /* tp_getattr */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
778 0, /* tp_setattr */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
779 0, /* tp_reserved */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
780 0, /* tp_repr */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
781 0, /* tp_as_number */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
782 0, /* tp_as_sequence */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
783 0, /* tp_as_mapping */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
784 0, /* tp_hash */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
785 0, /* tp_call */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
786 0, /* tp_str */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
787 PyObject_GenericGetAttr, /* tp_getattro */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
788 0, /* tp_setattro */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
789 0, /* tp_as_buffer */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
790 Py_TPFLAGS_DEFAULT, /* tp_flags */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
791 0, /* tp_doc */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
792 0, /* tp_traverse */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
793 0, /* tp_clear */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
794 0, /* tp_richcompare */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
795 0, /* tp_weaklistoffset */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
796 PyObject_SelfIter, /* tp_iter */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
797 (iternextfunc)longrangeiter_next, /* tp_iternext */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
798 longrangeiter_methods, /* tp_methods */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
799 0,
23693
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
800 };
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36501
diff changeset
801
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36501
diff changeset
802 static PyObject *
66335
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
803 range_iter(PyObject *seq)
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
804 {
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
805 rangeobject *r = (rangeobject *)seq;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
806 longrangeiterobject *it;
44366
83ced4177eac Drop _PyLong_FitsInLong. Fixes #1666.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
807 long lstart, lstop, lstep;
57261
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
808 PyObject *int_it;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
809
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
810 assert(PyRange_Check(seq));
44366
83ced4177eac Drop _PyLong_FitsInLong. Fixes #1666.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
811
57261
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
812 /* If all three fields and the length convert to long, use the int
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
813 * version */
44366
83ced4177eac Drop _PyLong_FitsInLong. Fixes #1666.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
814 lstart = PyLong_AsLong(r->start);
57261
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
815 if (lstart == -1 && PyErr_Occurred()) {
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
816 PyErr_Clear();
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
817 goto long_range;
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
818 }
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
819 lstop = PyLong_AsLong(r->stop);
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
820 if (lstop == -1 && PyErr_Occurred()) {
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
821 PyErr_Clear();
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
822 goto long_range;
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
823 }
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
824 lstep = PyLong_AsLong(r->step);
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
825 if (lstep == -1 && PyErr_Occurred()) {
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
826 PyErr_Clear();
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
827 goto long_range;
44366
83ced4177eac Drop _PyLong_FitsInLong. Fixes #1666.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
828 }
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
829 int_it = fast_range_iter(lstart, lstop, lstep);
57261
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
830 if (int_it == NULL && PyErr_ExceptionMatches(PyExc_OverflowError)) {
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
831 PyErr_Clear();
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
832 goto long_range;
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
833 }
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
834 return (PyObject *)int_it;
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
835
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
836 long_range:
44061
0cc552ab76bd Added all PyTypeObjects to the appropriate header files.
Christian Heimes <christian@cheimes.de>
parents: 44043
diff changeset
837 it = PyObject_New(longrangeiterobject, &PyLongRangeIter_Type);
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
838 if (it == NULL)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
839 return NULL;
41644
9cc4c3f0b580 Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
840
9cc4c3f0b580 Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
841 /* Do all initialization here, so we can DECREF on failure. */
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
842 it->start = r->start;
41644
9cc4c3f0b580 Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
843 it->step = r->step;
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
844 it->len = r->length;
41644
9cc4c3f0b580 Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
845 Py_INCREF(it->start);
9cc4c3f0b580 Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
846 Py_INCREF(it->step);
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
847 Py_INCREF(it->len);
41644
9cc4c3f0b580 Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
848
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
849 it->index = PyLong_FromLong(0);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
850 if (!it->index)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
851 goto create_failure;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
852
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
853 return (PyObject *)it;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
854
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
855 create_failure:
41644
9cc4c3f0b580 Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
856 Py_DECREF(it);
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
857 return NULL;
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36501
diff changeset
858 }
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36501
diff changeset
859
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36501
diff changeset
860 static PyObject *
66335
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
861 range_reverse(PyObject *seq)
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
862 {
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
863 rangeobject *range = (rangeobject*) seq;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
864 longrangeiterobject *it;
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
865 PyObject *one, *sum, *diff, *product;
57261
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
866 long lstart, lstop, lstep, new_start, new_stop;
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
867 unsigned long ulen;
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36501
diff changeset
868
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
869 assert(PyRange_Check(seq));
44366
83ced4177eac Drop _PyLong_FitsInLong. Fixes #1666.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
870
57261
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
871 /* reversed(range(start, stop, step)) can be expressed as
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
872 range(start+(n-1)*step, start-step, -step), where n is the number of
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
873 integers in the range.
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
874
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
875 If each of start, stop, step, -step, start-step, and the length
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
876 of the iterator is representable as a C long, use the int
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
877 version. This excludes some cases where the reversed range is
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
878 representable as a range_iterator, but it's good enough for
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
879 common cases and it makes the checks simple. */
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
880
44366
83ced4177eac Drop _PyLong_FitsInLong. Fixes #1666.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
881 lstart = PyLong_AsLong(range->start);
57261
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
882 if (lstart == -1 && PyErr_Occurred()) {
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
883 PyErr_Clear();
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
884 goto long_range;
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
885 }
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
886 lstop = PyLong_AsLong(range->stop);
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
887 if (lstop == -1 && PyErr_Occurred()) {
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
888 PyErr_Clear();
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
889 goto long_range;
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
890 }
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
891 lstep = PyLong_AsLong(range->step);
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
892 if (lstep == -1 && PyErr_Occurred()) {
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
893 PyErr_Clear();
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
894 goto long_range;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
895 }
57261
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
896 /* check for possible overflow of -lstep */
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
897 if (lstep == LONG_MIN)
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
898 goto long_range;
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
899
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
900 /* check for overflow of lstart - lstep:
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
901
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
902 for lstep > 0, need only check whether lstart - lstep < LONG_MIN.
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
903 for lstep < 0, need only check whether lstart - lstep > LONG_MAX
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
904
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
905 Rearrange these inequalities as:
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
906
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
907 lstart - LONG_MIN < lstep (lstep > 0)
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
908 LONG_MAX - lstart < -lstep (lstep < 0)
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
909
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
910 and compute both sides as unsigned longs, to avoid the
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
911 possibility of undefined behaviour due to signed overflow. */
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
912
57261
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
913 if (lstep > 0) {
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
914 if ((unsigned long)lstart - LONG_MIN < (unsigned long)lstep)
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
915 goto long_range;
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
916 }
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
917 else {
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
918 if (LONG_MAX - (unsigned long)lstart < 0UL - lstep)
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
919 goto long_range;
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
920 }
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
921
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
922 ulen = get_len_of_range(lstart, lstop, lstep);
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
923 if (ulen > (unsigned long)LONG_MAX)
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
924 goto long_range;
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
925
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
926 new_stop = lstart - lstep;
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
927 new_start = (long)(new_stop + ulen * lstep);
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
928 return fast_range_iter(new_start, new_stop, -lstep);
57261
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
929
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
930 long_range:
44061
0cc552ab76bd Added all PyTypeObjects to the appropriate header files.
Christian Heimes <christian@cheimes.de>
parents: 44043
diff changeset
931 it = PyObject_New(longrangeiterobject, &PyLongRangeIter_Type);
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
932 if (it == NULL)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
933 return NULL;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
934
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
935 /* start + (len - 1) * step */
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
936 it->len = range->length;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
937 Py_INCREF(it->len);
57286
eb235eb45d32 fix one visible and several possible refleaks in rangeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 57267
diff changeset
938
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
939 one = PyLong_FromLong(1);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
940 if (!one)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
941 goto create_failure;
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36501
diff changeset
942
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
943 diff = PyNumber_Subtract(it->len, one);
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
944 Py_DECREF(one);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
945 if (!diff)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
946 goto create_failure;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
947
57261
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
948 product = PyNumber_Multiply(diff, range->step);
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
949 Py_DECREF(diff);
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
950 if (!product)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
951 goto create_failure;
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36501
diff changeset
952
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
953 sum = PyNumber_Add(range->start, product);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
954 Py_DECREF(product);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
955 it->start = sum;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
956 if (!it->start)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
957 goto create_failure;
57261
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
958
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
959 it->step = PyNumber_Negative(range->step);
57286
eb235eb45d32 fix one visible and several possible refleaks in rangeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 57267
diff changeset
960 if (!it->step)
57261
370d218a01bc Issue #7298: Fix a variety of problems leading to wrong results with
Mark Dickinson <dickinsm@gmail.com>
parents: 56210
diff changeset
961 goto create_failure;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
962
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
963 it->index = PyLong_FromLong(0);
57286
eb235eb45d32 fix one visible and several possible refleaks in rangeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 57267
diff changeset
964 if (!it->index)
eb235eb45d32 fix one visible and several possible refleaks in rangeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 57267
diff changeset
965 goto create_failure;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
966
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
967 return (PyObject *)it;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
968
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
969 create_failure:
57286
eb235eb45d32 fix one visible and several possible refleaks in rangeobject.c
Benjamin Peterson <benjamin@python.org>
parents: 57267
diff changeset
970 Py_DECREF(it);
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
971 return NULL;
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36501
diff changeset
972 }