annotate Objects/rangeobject.c @ 73798:6cde416ef03b

Credit Per Øyvind Karlsen for the initial implementation of the lzma module (issue #6715).
author Nadeem Vawda <nadeem.vawda@gmail.com>
date Thu, 01 Dec 2011 01:18:27 +0200
parents 4643be424293
children 4ff234337e24
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"
73378
4643be424293 add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents: 73072
diff changeset
4 #include "structmember.h"
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
5
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
6 /* 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
7
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
8 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
9 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
10 would presumably help perf.
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
11 */
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
12
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 typedef struct {
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
14 PyObject_HEAD
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
15 PyObject *start;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
16 PyObject *stop;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
17 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
18 PyObject *length;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 } rangeobject;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
20
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
21 /* Helper function for validating step. Always returns a new reference or
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
22 NULL on error.
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
23 */
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
24 static PyObject *
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
25 validate_step(PyObject *step)
23695
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
26 {
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
27 /* 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
28 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
29 return PyLong_FromLong(1);
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
30
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
31 step = PyNumber_Index(step);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
32 if (step) {
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
33 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
34 if (istep == -1 && PyErr_Occurred()) {
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
35 /* 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
36 PyErr_Clear();
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
37 }
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
38 else if (istep == 0) {
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
39 PyErr_SetString(PyExc_ValueError,
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
40 "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
41 Py_CLEAR(step);
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
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
45 return step;
23695
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
46 }
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
47
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
48 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
49 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
50
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 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
52 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
53 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
54 {
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 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
56 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
57 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
58 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
59 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
60 }
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 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
62 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
63 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
64 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
65 }
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->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
67 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
68 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
69 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
70 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
71 }
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
72
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
73 /* 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
74 range(-10)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
75 range(0, -5)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
76 range(0, 5, -1)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
77 */
23695
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
78 static PyObject *
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
79 range_new(PyTypeObject *type, PyObject *args, PyObject *kw)
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
80 {
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
81 rangeobject *obj;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
82 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
83
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
84 if (!_PyArg_NoKeywords("range()", kw))
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
85 return NULL;
23695
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
86
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
87 if (PyTuple_Size(args) <= 1) {
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
88 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
89 return NULL;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
90 stop = PyNumber_Index(stop);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
91 if (!stop)
54726
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
92 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
93 start = PyLong_FromLong(0);
54726
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
94 if (!start) {
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
95 Py_DECREF(stop);
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
96 return NULL;
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
97 }
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
98 step = PyLong_FromLong(1);
54726
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
99 if (!step) {
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
100 Py_DECREF(stop);
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
101 Py_DECREF(start);
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
102 return NULL;
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
103 }
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
104 }
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
105 else {
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
106 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
107 &start, &stop, &step))
54726
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
108 return NULL;
35449
1fdfd0130e28 Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents: 34087
diff changeset
109
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
110 /* Convert borrowed refs to owned refs */
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
111 start = PyNumber_Index(start);
54726
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
112 if (!start)
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
113 return NULL;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
114 stop = PyNumber_Index(stop);
54726
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
115 if (!stop) {
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
116 Py_DECREF(start);
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
117 return NULL;
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
118 }
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
119 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
120 if (!step) {
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
121 Py_DECREF(start);
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
122 Py_DECREF(stop);
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
123 return NULL;
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
124 }
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
125 }
33047
c81ed079576f Bug 1003935: xrange overflows
Tim Peters <tim.peters@gmail.com>
parents: 33046
diff changeset
126
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
127 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
128 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
129 return (PyObject *) obj;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
130
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
131 /* 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
132 Py_XDECREF(start);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
133 Py_XDECREF(stop);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
134 Py_XDECREF(step);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
135 return NULL;
23695
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
136 }
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
137
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23702
diff changeset
138 PyDoc_STRVAR(range_doc,
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
139 "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
140 \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
141 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
142
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
143 static void
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
144 range_dealloc(rangeobject *r)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
145 {
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
146 Py_DECREF(r->start);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
147 Py_DECREF(r->stop);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
148 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
149 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
150 PyObject_Del(r);
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
151 }
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
152
55589
546aa134d773 Merged revisions 73656,73658,73663,73666 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54847
diff changeset
153 /* 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
154 * 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
155 * 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
156 */
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
157 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
158 compute_range_length(PyObject *start, PyObject *stop, PyObject *step)
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 /* -------------------------------------------------------------
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
161 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
162 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
163 ---------------------------------------------------------------*/
51244
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47536
diff changeset
164 int cmp_result;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
165 PyObject *lo, *hi;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
166 PyObject *diff = NULL;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
167 PyObject *one = NULL;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
168 PyObject *tmp1 = NULL, *tmp2 = NULL, *result;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
169 /* holds sub-expression evaluations */
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
170
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
171 PyObject *zero = PyLong_FromLong(0);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
172 if (zero == NULL)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
173 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
174 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
175 Py_DECREF(zero);
51244
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47536
diff changeset
176 if (cmp_result == -1)
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
177 return NULL;
23695
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
178
51244
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47536
diff changeset
179 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
180 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
181 hi = stop;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
182 Py_INCREF(step);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
183 } 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
184 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
185 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
186 step = PyNumber_Negative(step);
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
187 if (!step)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
188 return NULL;
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
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
191 /* if (lo >= hi), return length of 0. */
51244
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47536
diff changeset
192 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
193 Py_XDECREF(step);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
194 return PyLong_FromLong(0);
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
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
197 if ((one = PyLong_FromLong(1L)) == NULL)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
198 goto Fail;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
199
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
200 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
201 goto Fail;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
202
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
203 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
204 goto Fail;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
205
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
206 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
207 goto Fail;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
208
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
209 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
210 goto Fail;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
211
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
212 Py_DECREF(tmp2);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
213 Py_DECREF(diff);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
214 Py_DECREF(step);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
215 Py_DECREF(tmp1);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
216 Py_DECREF(one);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
217 return result;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
218
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
219 Fail:
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
220 Py_XDECREF(tmp2);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
221 Py_XDECREF(diff);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
222 Py_XDECREF(step);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
223 Py_XDECREF(tmp1);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
224 Py_XDECREF(one);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
225 return NULL;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
227
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
228 static Py_ssize_t
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
229 range_length(rangeobject *r)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 {
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
231 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
232 }
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
233
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
234 static PyObject *
67543
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
235 compute_item(rangeobject *r, PyObject *i)
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
236 {
67543
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
237 PyObject *incr, *result;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
238 /* PyLong equivalent to:
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
239 * return r->start + (i * r->step)
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
240 */
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
241 incr = PyNumber_Multiply(i, r->step);
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
242 if (!incr)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
243 return NULL;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
244 result = PyNumber_Add(r->start, incr);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
245 Py_DECREF(incr);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
246 return result;
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
247 }
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
248
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
249 static PyObject *
67543
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
250 compute_range_item(rangeobject *r, PyObject *arg)
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
251 {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
252 int cmp_result;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
253 PyObject *i, *result;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
254
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
255 PyObject *zero = PyLong_FromLong(0);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
256 if (zero == NULL)
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
257 return NULL;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
258
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
259 /* PyLong equivalent to:
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
260 * if (arg < 0) {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
261 * i = r->length + arg
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
262 * } else {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
263 * i = arg
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
264 * }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
265 */
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
266 cmp_result = PyObject_RichCompareBool(arg, zero, Py_LT);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
267 if (cmp_result == -1) {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
268 Py_DECREF(zero);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
269 return NULL;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
270 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
271 if (cmp_result == 1) {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
272 i = PyNumber_Add(r->length, arg);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
273 if (!i) {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
274 Py_DECREF(zero);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
275 return NULL;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
276 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
277 } else {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
278 i = arg;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
279 Py_INCREF(i);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
280 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
281
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
282 /* PyLong equivalent to:
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
283 * if (i < 0 || i >= r->length) {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
284 * <report index out of bounds>
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
285 * }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
286 */
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
287 cmp_result = PyObject_RichCompareBool(i, zero, Py_LT);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
288 Py_DECREF(zero);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
289 if (cmp_result == 0) {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
290 cmp_result = PyObject_RichCompareBool(i, r->length, Py_GE);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
291 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
292 if (cmp_result == -1) {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
293 Py_DECREF(i);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
294 return NULL;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
295 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
296 if (cmp_result == 1) {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
297 Py_DECREF(i);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
298 PyErr_SetString(PyExc_IndexError,
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
299 "range object index out of range");
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
300 return NULL;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
301 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
302
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
303 result = compute_item(r, i);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
304 Py_DECREF(i);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
305 return result;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
306 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
307
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
308 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
309 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
310 {
67575
10d67db67942 plug reference leak
Benjamin Peterson <benjamin@python.org>
parents: 67543
diff changeset
311 PyObject *res, *arg = PyLong_FromLong(i);
67543
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
312 if (!arg) {
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
313 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
314 }
67575
10d67db67942 plug reference leak
Benjamin Peterson <benjamin@python.org>
parents: 67543
diff changeset
315 res = compute_range_item(r, arg);
10d67db67942 plug reference leak
Benjamin Peterson <benjamin@python.org>
parents: 67543
diff changeset
316 Py_DECREF(arg);
10d67db67942 plug reference leak
Benjamin Peterson <benjamin@python.org>
parents: 67543
diff changeset
317 return res;
67543
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
318 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
319
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
320 /* Additional helpers, since the standard slice helpers
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
321 * all clip to PY_SSIZE_T_MAX
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
322 */
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
323
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
324 /* Replace _PyEval_SliceIndex */
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
325 static PyObject *
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
326 compute_slice_element(PyObject *obj)
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
327 {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
328 PyObject *result = NULL;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
329 if (obj != NULL) {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
330 if (PyIndex_Check(obj)) {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
331 result = PyNumber_Index(obj);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
332 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
333 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
334 if (result == NULL) {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
335 PyErr_SetString(PyExc_TypeError,
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
336 "slice indices must be integers or "
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
337 "None or have an __index__ method");
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
338 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
339 return result;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
340 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
341
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
342 /* Replace PySlice_GetIndicesEx
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
343 * Result indicates whether or not the slice is empty
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
344 * (-1 = error, 0 = empty slice, 1 = slice contains elements)
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
345 */
67642
faec1b5a96db rangeobject.c (compute_slice_indices): Make function static.
Matthias Klose <doko@ubuntu.com>
parents: 67575
diff changeset
346 static int
67543
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
347 compute_slice_indices(rangeobject *r, PySliceObject *slice,
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
348 PyObject **start, PyObject **stop, PyObject **step)
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
349 {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
350 int cmp_result, has_elements;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
351 Py_ssize_t clamped_step = 0;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
352 PyObject *zero = NULL, *one = NULL, *neg_one = NULL, *candidate = NULL;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
353 PyObject *tmp_start = NULL, *tmp_stop = NULL, *tmp_step = NULL;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
354 zero = PyLong_FromLong(0);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
355 if (zero == NULL) goto Fail;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
356 one = PyLong_FromLong(1);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
357 if (one == NULL) goto Fail;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
358 neg_one = PyLong_FromLong(-1);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
359 if (neg_one == NULL) goto Fail;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
360
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
361 /* Calculate step value */
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
362 if (slice->step == Py_None) {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
363 clamped_step = 1;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
364 tmp_step = one;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
365 Py_INCREF(tmp_step);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
366 } else {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
367 if (!_PyEval_SliceIndex(slice->step, &clamped_step)) goto Fail;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
368 if (clamped_step == 0) {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
369 PyErr_SetString(PyExc_ValueError,
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
370 "slice step cannot be zero");
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
371 goto Fail;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
372 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
373 tmp_step = compute_slice_element(slice->step);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
374 if (tmp_step == NULL) goto Fail;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
375 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
376
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
377 /* Calculate start value */
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
378 if (slice->start == Py_None) {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
379 if (clamped_step < 0) {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
380 tmp_start = PyNumber_Subtract(r->length, one);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
381 if (tmp_start == NULL) goto Fail;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
382 } else {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
383 tmp_start = zero;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
384 Py_INCREF(tmp_start);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
385 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
386 } else {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
387 candidate = compute_slice_element(slice->start);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
388 if (candidate == NULL) goto Fail;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
389 cmp_result = PyObject_RichCompareBool(candidate, zero, Py_LT);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
390 if (cmp_result == -1) goto Fail;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
391 if (cmp_result) {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
392 /* candidate < 0 */
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
393 tmp_start = PyNumber_Add(r->length, candidate);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
394 if (tmp_start == NULL) goto Fail;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
395 Py_CLEAR(candidate);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
396 } else {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
397 /* candidate >= 0 */
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
398 tmp_start = candidate;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
399 candidate = NULL;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
400 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
401 cmp_result = PyObject_RichCompareBool(tmp_start, zero, Py_LT);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
402 if (cmp_result == -1) goto Fail;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
403 if (cmp_result) {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
404 /* tmp_start < 0 */
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
405 Py_CLEAR(tmp_start);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
406 if (clamped_step < 0) {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
407 tmp_start = neg_one;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
408 } else {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
409 tmp_start = zero;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
410 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
411 Py_INCREF(tmp_start);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
412 } else {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
413 /* tmp_start >= 0 */
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
414 cmp_result = PyObject_RichCompareBool(tmp_start, r->length, Py_GE);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
415 if (cmp_result == -1) goto Fail;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
416 if (cmp_result) {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
417 /* tmp_start >= r->length */
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
418 Py_CLEAR(tmp_start);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
419 if (clamped_step < 0) {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
420 tmp_start = PyNumber_Subtract(r->length, one);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
421 if (tmp_start == NULL) goto Fail;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
422 } else {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
423 tmp_start = r->length;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
424 Py_INCREF(tmp_start);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
425 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
426 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
427 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
428 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
429
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
430 /* Calculate stop value */
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
431 if (slice->stop == Py_None) {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
432 if (clamped_step < 0) {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
433 tmp_stop = neg_one;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
434 } else {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
435 tmp_stop = r->length;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
436 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
437 Py_INCREF(tmp_stop);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
438 } else {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
439 candidate = compute_slice_element(slice->stop);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
440 if (candidate == NULL) goto Fail;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
441 cmp_result = PyObject_RichCompareBool(candidate, zero, Py_LT);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
442 if (cmp_result == -1) goto Fail;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
443 if (cmp_result) {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
444 /* candidate < 0 */
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
445 tmp_stop = PyNumber_Add(r->length, candidate);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
446 if (tmp_stop == NULL) goto Fail;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
447 Py_CLEAR(candidate);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
448 } else {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
449 /* candidate >= 0 */
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
450 tmp_stop = candidate;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
451 candidate = NULL;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
452 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
453 cmp_result = PyObject_RichCompareBool(tmp_stop, zero, Py_LT);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
454 if (cmp_result == -1) goto Fail;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
455 if (cmp_result) {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
456 /* tmp_stop < 0 */
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
457 Py_CLEAR(tmp_stop);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
458 if (clamped_step < 0) {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
459 tmp_stop = neg_one;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
460 } else {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
461 tmp_stop = zero;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
462 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
463 Py_INCREF(tmp_stop);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
464 } else {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
465 /* tmp_stop >= 0 */
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
466 cmp_result = PyObject_RichCompareBool(tmp_stop, r->length, Py_GE);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
467 if (cmp_result == -1) goto Fail;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
468 if (cmp_result) {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
469 /* tmp_stop >= r->length */
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
470 Py_CLEAR(tmp_stop);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
471 if (clamped_step < 0) {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
472 tmp_stop = PyNumber_Subtract(r->length, one);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
473 if (tmp_stop == NULL) goto Fail;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
474 } else {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
475 tmp_stop = r->length;
69359
8025fb83dece #11845: Fix typo in rangeobject.c that caused a crash in compute_slice_indices. Patch by Daniel Urban.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68257
diff changeset
476 Py_INCREF(tmp_stop);
67543
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
477 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
478 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
479 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
480 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
481
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
482 /* Check if the slice is empty or not */
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
483 if (clamped_step < 0) {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
484 has_elements = PyObject_RichCompareBool(tmp_start, tmp_stop, Py_GT);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
485 } else {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
486 has_elements = PyObject_RichCompareBool(tmp_start, tmp_stop, Py_LT);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
487 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
488 if (has_elements == -1) goto Fail;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
489
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
490 *start = tmp_start;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
491 *stop = tmp_stop;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
492 *step = tmp_step;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
493 Py_DECREF(neg_one);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
494 Py_DECREF(one);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
495 Py_DECREF(zero);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
496 return has_elements;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
497
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
498 Fail:
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
499 Py_XDECREF(tmp_start);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
500 Py_XDECREF(tmp_stop);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
501 Py_XDECREF(tmp_step);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
502 Py_XDECREF(candidate);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
503 Py_XDECREF(neg_one);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
504 Py_XDECREF(one);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
505 Py_XDECREF(zero);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
506 return -1;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
507 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
508
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
509 static PyObject *
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
510 compute_slice(rangeobject *r, PyObject *_slice)
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
511 {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
512 PySliceObject *slice = (PySliceObject *) _slice;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
513 rangeobject *result;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
514 PyObject *start = NULL, *stop = NULL, *step = NULL;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
515 PyObject *substart = NULL, *substop = NULL, *substep = NULL;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
516 int has_elements;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
517
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
518 has_elements = compute_slice_indices(r, slice, &start, &stop, &step);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
519 if (has_elements == -1) return NULL;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
520
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
521 substep = PyNumber_Multiply(r->step, step);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
522 if (substep == NULL) goto fail;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
523 Py_CLEAR(step);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
524
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
525 substart = compute_item(r, start);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
526 if (substart == NULL) goto fail;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
527 Py_CLEAR(start);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
528
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
529 if (has_elements) {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
530 substop = compute_item(r, stop);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
531 if (substop == NULL) goto fail;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
532 } else {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
533 substop = substart;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
534 Py_INCREF(substop);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
535 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
536 Py_CLEAR(stop);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
537
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
538 result = make_range_object(Py_TYPE(r), substart, substop, substep);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
539 if (result != NULL) {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
540 return (PyObject *) result;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
541 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
542 fail:
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
543 Py_XDECREF(start);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
544 Py_XDECREF(stop);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
545 Py_XDECREF(step);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
546 Py_XDECREF(substart);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
547 Py_XDECREF(substop);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
548 Py_XDECREF(substep);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
549 return NULL;
47536
33492c241dab Issue 2582: Fix pickling of range objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46564
diff changeset
550 }
33492c241dab Issue 2582: Fix pickling of range objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46564
diff changeset
551
64725
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
552 /* 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
553 static int
66335
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
554 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
555 {
64725
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
556 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
557 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
558 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
559 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
560 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
561
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
562 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
563 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
564 goto end;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
565
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
566 /* 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
567
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
568 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
569 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
570 goto end;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
571 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
572 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
573 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
574 }
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
575 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
576 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
577 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
578 }
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
579
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
580 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
581 goto end;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
582 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
583 result = 0;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
584 goto end;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
585 }
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
586
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
587 /* 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
588 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
589 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
590 goto end;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
591 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
592 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
593 goto end;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
594 /* 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
595 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
596 end:
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
597 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
598 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
599 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
600 return result;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
601 }
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
602
56192
d599a3f2e72d Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
603 static int
66335
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
604 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
605 {
64725
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
606 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
607 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
608
d599a3f2e72d Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
609 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
610 PY_ITERSEARCH_CONTAINS);
d599a3f2e72d Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
611 }
d599a3f2e72d Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
612
73072
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
613 /* Compare two range objects. Return 1 for equal, 0 for not equal
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
614 and -1 on error. The algorithm is roughly the C equivalent of
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
615
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
616 if r0 is r1:
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
617 return True
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
618 if len(r0) != len(r1):
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
619 return False
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
620 if not len(r0):
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
621 return True
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
622 if r0.start != r1.start:
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
623 return False
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
624 if len(r0) == 1:
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
625 return True
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
626 return r0.step == r1.step
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
627 */
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
628 static int
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
629 range_equals(rangeobject *r0, rangeobject *r1)
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
630 {
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
631 int cmp_result;
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
632 PyObject *one;
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
633
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
634 if (r0 == r1)
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
635 return 1;
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
636 cmp_result = PyObject_RichCompareBool(r0->length, r1->length, Py_EQ);
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
637 /* Return False or error to the caller. */
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
638 if (cmp_result != 1)
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
639 return cmp_result;
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
640 cmp_result = PyObject_Not(r0->length);
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
641 /* Return True or error to the caller. */
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
642 if (cmp_result != 0)
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
643 return cmp_result;
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
644 cmp_result = PyObject_RichCompareBool(r0->start, r1->start, Py_EQ);
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
645 /* Return False or error to the caller. */
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
646 if (cmp_result != 1)
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
647 return cmp_result;
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
648 one = PyLong_FromLong(1);
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
649 if (!one)
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
650 return -1;
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
651 cmp_result = PyObject_RichCompareBool(r0->length, one, Py_EQ);
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
652 Py_DECREF(one);
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
653 /* Return True or error to the caller. */
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
654 if (cmp_result != 0)
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
655 return cmp_result;
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
656 return PyObject_RichCompareBool(r0->step, r1->step, Py_EQ);
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
657 }
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
658
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
659 static PyObject *
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
660 range_richcompare(PyObject *self, PyObject *other, int op)
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
661 {
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
662 int result;
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
663
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
664 if (!PyRange_Check(other))
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
665 Py_RETURN_NOTIMPLEMENTED;
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
666 switch (op) {
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
667 case Py_NE:
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
668 case Py_EQ:
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
669 result = range_equals((rangeobject*)self, (rangeobject*)other);
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
670 if (result == -1)
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
671 return NULL;
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
672 if (op == Py_NE)
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
673 result = !result;
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
674 if (result)
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
675 Py_RETURN_TRUE;
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
676 else
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
677 Py_RETURN_FALSE;
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
678 case Py_LE:
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
679 case Py_GE:
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
680 case Py_LT:
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
681 case Py_GT:
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
682 Py_RETURN_NOTIMPLEMENTED;
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
683 default:
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
684 PyErr_BadArgument();
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
685 return NULL;
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
686 }
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
687 }
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
688
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
689 /* Hash function for range objects. Rough C equivalent of
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
690
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
691 if not len(r):
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
692 return hash((len(r), None, None))
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
693 if len(r) == 1:
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
694 return hash((len(r), r.start, None))
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
695 return hash((len(r), r.start, r.step))
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
696 */
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
697 static Py_hash_t
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
698 range_hash(rangeobject *r)
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
699 {
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
700 PyObject *t;
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
701 Py_hash_t result = -1;
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
702 int cmp_result;
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
703
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
704 t = PyTuple_New(3);
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
705 if (!t)
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
706 return -1;
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
707 Py_INCREF(r->length);
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
708 PyTuple_SET_ITEM(t, 0, r->length);
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
709 cmp_result = PyObject_Not(r->length);
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
710 if (cmp_result == -1)
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
711 goto end;
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
712 if (cmp_result == 1) {
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
713 Py_INCREF(Py_None);
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
714 Py_INCREF(Py_None);
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
715 PyTuple_SET_ITEM(t, 1, Py_None);
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
716 PyTuple_SET_ITEM(t, 2, Py_None);
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
717 }
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
718 else {
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
719 PyObject *one;
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
720 Py_INCREF(r->start);
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
721 PyTuple_SET_ITEM(t, 1, r->start);
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
722 one = PyLong_FromLong(1);
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
723 if (!one)
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
724 goto end;
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
725 cmp_result = PyObject_RichCompareBool(r->length, one, Py_EQ);
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
726 Py_DECREF(one);
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
727 if (cmp_result == -1)
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
728 goto end;
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
729 if (cmp_result == 1) {
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
730 Py_INCREF(Py_None);
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
731 PyTuple_SET_ITEM(t, 2, Py_None);
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
732 }
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
733 else {
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
734 Py_INCREF(r->step);
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
735 PyTuple_SET_ITEM(t, 2, r->step);
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
736 }
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
737 }
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
738 result = PyObject_Hash(t);
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
739 end:
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
740 Py_DECREF(t);
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
741 return result;
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
742 }
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
743
64725
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
744 static PyObject *
66335
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
745 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
746 {
64725
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
747 if (PyLong_CheckExact(ob) || PyBool_Check(ob)) {
66330
add7e9721f80 Add error handling in range_count.
Georg Brandl <georg@python.org>
parents: 66329
diff changeset
748 int result = range_contains_long(r, ob);
add7e9721f80 Add error handling in range_count.
Georg Brandl <georg@python.org>
parents: 66329
diff changeset
749 if (result == -1)
add7e9721f80 Add error handling in range_count.
Georg Brandl <georg@python.org>
parents: 66329
diff changeset
750 return NULL;
add7e9721f80 Add error handling in range_count.
Georg Brandl <georg@python.org>
parents: 66329
diff changeset
751 else if (result)
66329
238caaf6852b count() should return integers #10474
Benjamin Peterson <benjamin@python.org>
parents: 64725
diff changeset
752 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
753 else
66329
238caaf6852b count() should return integers #10474
Benjamin Peterson <benjamin@python.org>
parents: 64725
diff changeset
754 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
755 } else {
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
756 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
757 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
758 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
759 return NULL;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
760 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
761 }
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
762 }
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
763
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
764 static PyObject *
66335
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
765 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
766 {
64725
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
767 int contains;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
768
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
769 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
770 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
771 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
772 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
773 return NULL;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
774 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
775 }
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
776
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
777 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
778 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
779 return NULL;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
780
66331
e01237831ad2 code style and simplification
Benjamin Peterson <benjamin@python.org>
parents: 66330
diff changeset
781 if (contains) {
e01237831ad2 code style and simplification
Benjamin Peterson <benjamin@python.org>
parents: 66330
diff changeset
782 PyObject *idx, *tmp = PyNumber_Subtract(ob, r->start);
e01237831ad2 code style and simplification
Benjamin Peterson <benjamin@python.org>
parents: 66330
diff changeset
783 if (tmp == NULL)
e01237831ad2 code style and simplification
Benjamin Peterson <benjamin@python.org>
parents: 66330
diff changeset
784 return NULL;
e01237831ad2 code style and simplification
Benjamin Peterson <benjamin@python.org>
parents: 66330
diff changeset
785 /* idx = (ob - r.start) // r.step */
e01237831ad2 code style and simplification
Benjamin Peterson <benjamin@python.org>
parents: 66330
diff changeset
786 idx = PyNumber_FloorDivide(tmp, r->step);
e01237831ad2 code style and simplification
Benjamin Peterson <benjamin@python.org>
parents: 66330
diff changeset
787 Py_DECREF(tmp);
e01237831ad2 code style and simplification
Benjamin Peterson <benjamin@python.org>
parents: 66330
diff changeset
788 return idx;
e01237831ad2 code style and simplification
Benjamin Peterson <benjamin@python.org>
parents: 66330
diff changeset
789 }
64725
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
790
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
791 /* object is not in the range */
66331
e01237831ad2 code style and simplification
Benjamin Peterson <benjamin@python.org>
parents: 66330
diff changeset
792 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
793 return NULL;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
794 }
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
795
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
796 static PySequenceMethods range_as_sequence = {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
797 (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
798 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
799 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
800 (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
801 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
802 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
803 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
804 (objobjproc)range_contains, /* sq_contains */
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
805 };
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
806
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
807 static PyObject *
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
808 range_repr(rangeobject *r)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
809 {
41800
993f8a9b8ab3 repr(range(10)) now returns 'range(0, 10)' for clarity.
Walter Dörwald <walter@livinglogic.de>
parents: 41785
diff changeset
810 Py_ssize_t istep;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
811
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
812 /* Check for special case values for printing. We don't always
41800
993f8a9b8ab3 repr(range(10)) now returns 'range(0, 10)' for clarity.
Walter Dörwald <walter@livinglogic.de>
parents: 41785
diff changeset
813 need the step value. We don't care about errors
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
814 (it means overflow), so clear the errors. */
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
815 istep = PyNumber_AsSsize_t(r->step, NULL);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
816 if (istep != 1 || (istep == -1 && PyErr_Occurred())) {
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
817 PyErr_Clear();
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
818 }
13848
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
819
41800
993f8a9b8ab3 repr(range(10)) now returns 'range(0, 10)' for clarity.
Walter Dörwald <walter@livinglogic.de>
parents: 41785
diff changeset
820 if (istep == 1)
41785
d1c38af065bf Change range_repr() to use %R for the start/stop/step attributes.
Walter Dörwald <walter@livinglogic.de>
parents: 41769
diff changeset
821 return PyUnicode_FromFormat("range(%R, %R)", r->start, r->stop);
d1c38af065bf Change range_repr() to use %R for the start/stop/step attributes.
Walter Dörwald <walter@livinglogic.de>
parents: 41769
diff changeset
822 else
d1c38af065bf Change range_repr() to use %R for the start/stop/step attributes.
Walter Dörwald <walter@livinglogic.de>
parents: 41769
diff changeset
823 return PyUnicode_FromFormat("range(%R, %R, %R)",
d1c38af065bf Change range_repr() to use %R for the start/stop/step attributes.
Walter Dörwald <walter@livinglogic.de>
parents: 41769
diff changeset
824 r->start, r->stop, r->step);
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
825 }
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
826
47536
33492c241dab Issue 2582: Fix pickling of range objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46564
diff changeset
827 /* Pickling support */
33492c241dab Issue 2582: Fix pickling of range objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46564
diff changeset
828 static PyObject *
33492c241dab Issue 2582: Fix pickling of range objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46564
diff changeset
829 range_reduce(rangeobject *r, PyObject *args)
33492c241dab Issue 2582: Fix pickling of range objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46564
diff changeset
830 {
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
831 return Py_BuildValue("(O(OOO))", Py_TYPE(r),
47536
33492c241dab Issue 2582: Fix pickling of range objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46564
diff changeset
832 r->start, r->stop, r->step);
33492c241dab Issue 2582: Fix pickling of range objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46564
diff changeset
833 }
33492c241dab Issue 2582: Fix pickling of range objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46564
diff changeset
834
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
835 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
836 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
837 {
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
838 if (PyIndex_Check(item)) {
67543
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
839 PyObject *i, *result;
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
840 i = PyNumber_Index(item);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
841 if (!i)
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
842 return NULL;
67543
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
843 result = compute_range_item(self, i);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
844 Py_DECREF(i);
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
845 return result;
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
846 }
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 if (PySlice_Check(item)) {
67543
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents: 66664
diff changeset
848 return compute_slice(self, item);
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
849 }
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
850 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
851 "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
852 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
853 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
854 }
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
855
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
856
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
857 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
858 (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
859 (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
860 (objobjargproc)0, /* mp_ass_subscript */
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
861 };
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
862
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24260
diff changeset
863 static PyObject * range_iter(PyObject *seq);
30865
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
864 static PyObject * range_reverse(PyObject *seq);
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
865
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
866 PyDoc_STRVAR(reverse_doc,
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
867 "Returns a reverse iterator.");
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
868
64725
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
869 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
870 "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
871
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
872 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
873 "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
874 "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
875
30865
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
876 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
877 {"__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
878 {"__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
879 {"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
880 {"index", (PyCFunction)range_index, METH_O, index_doc},
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
881 {NULL, NULL} /* sentinel */
30865
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
882 };
23693
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
883
73378
4643be424293 add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents: 73072
diff changeset
884 static PyMemberDef range_members[] = {
4643be424293 add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents: 73072
diff changeset
885 {"start", T_OBJECT_EX, offsetof(rangeobject, start), READONLY},
4643be424293 add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents: 73072
diff changeset
886 {"stop", T_OBJECT_EX, offsetof(rangeobject, stop), READONLY},
4643be424293 add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents: 73072
diff changeset
887 {"step", T_OBJECT_EX, offsetof(rangeobject, step), READONLY},
4643be424293 add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents: 73072
diff changeset
888 {0}
4643be424293 add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents: 73072
diff changeset
889 };
4643be424293 add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents: 73072
diff changeset
890
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
891 PyTypeObject PyRange_Type = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
892 PyVarObject_HEAD_INIT(&PyType_Type, 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
893 "range", /* Name of this type */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
894 sizeof(rangeobject), /* Basic object size */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
895 0, /* Item size for varobject */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
896 (destructor)range_dealloc, /* tp_dealloc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
897 0, /* tp_print */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
898 0, /* tp_getattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
899 0, /* tp_setattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
900 0, /* tp_reserved */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
901 (reprfunc)range_repr, /* tp_repr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
902 0, /* tp_as_number */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
903 &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
904 &range_as_mapping, /* tp_as_mapping */
73072
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
905 (hashfunc)range_hash, /* tp_hash */
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
906 0, /* tp_call */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
907 0, /* tp_str */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
908 PyObject_GenericGetAttr, /* tp_getattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
909 0, /* tp_setattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
910 0, /* tp_as_buffer */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
911 Py_TPFLAGS_DEFAULT, /* tp_flags */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
912 range_doc, /* tp_doc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
913 0, /* tp_traverse */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
914 0, /* tp_clear */
73072
479a7dd1ea6a Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents: 69359
diff changeset
915 range_richcompare, /* tp_richcompare */
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
916 0, /* tp_weaklistoffset */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
917 range_iter, /* tp_iter */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
918 0, /* tp_iternext */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
919 range_methods, /* tp_methods */
73378
4643be424293 add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents: 73072
diff changeset
920 range_members, /* tp_members */
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
921 0, /* tp_getset */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
922 0, /* tp_base */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
923 0, /* tp_dict */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
924 0, /* tp_descr_get */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
925 0, /* tp_descr_set */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
926 0, /* tp_dictoffset */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
927 0, /* tp_init */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
928 0, /* tp_alloc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
929 range_new, /* tp_new */
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
930 };
23693
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
931
41803
3d3bf27409e6 Rename test_xrange.py to test_range.py and fix the
Walter Dörwald <walter@livinglogic.de>
parents: 41800
diff changeset
932 /*********************** range Iterator **************************/
23693
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
933
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
934 /* 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
935 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
936 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
937 */
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
938
23693
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
939 typedef struct {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
940 PyObject_HEAD
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
941 long index;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
942 long start;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
943 long step;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
944 long len;
23693
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
945 } rangeiterobject;
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
946
30865
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
947 static PyObject *
23693
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
948 rangeiter_next(rangeiterobject *r)
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
949 {
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
950 if (r->index < r->len)
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
951 /* cast to unsigned to avoid possible signed overflow
57268
9505afe8823f Merged revisions 76298 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 57263
diff changeset
952 in intermediate calculations. */
9505afe8823f Merged revisions 76298 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 57263
diff changeset
953 return PyLong_FromLong((long)(r->start +
9505afe8823f Merged revisions 76298 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 57263
diff changeset
954 (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
955 return NULL;
23693
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
956 }
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
957
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
958 static PyObject *
31635
04eb2ebb46e2 Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents: 30865
diff changeset
959 rangeiter_len(rangeiterobject *r)
04eb2ebb46e2 Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents: 30865
diff changeset
960 {
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
961 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
962 }
04eb2ebb46e2 Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents: 30865
diff changeset
963
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
964 PyDoc_STRVAR(length_hint_doc,
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
965 "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
966
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
967 static PyMethodDef rangeiter_methods[] = {
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
968 {"__length_hint__", (PyCFunction)rangeiter_len, METH_NOARGS,
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
969 length_hint_doc},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
970 {NULL, NULL} /* sentinel */
31635
04eb2ebb46e2 Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents: 30865
diff changeset
971 };
04eb2ebb46e2 Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents: 30865
diff changeset
972
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
973 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
974
44061
0cc552ab76bd Added all PyTypeObjects to the appropriate header files.
Christian Heimes <christian@cheimes.de>
parents: 44043
diff changeset
975 PyTypeObject PyRangeIter_Type = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
976 PyVarObject_HEAD_INIT(&PyType_Type, 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
977 "range_iterator", /* tp_name */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
978 sizeof(rangeiterobject), /* tp_basicsize */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
979 0, /* tp_itemsize */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
980 /* methods */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
981 (destructor)PyObject_Del, /* tp_dealloc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
982 0, /* tp_print */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
983 0, /* tp_getattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
984 0, /* tp_setattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
985 0, /* tp_reserved */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
986 0, /* tp_repr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
987 0, /* tp_as_number */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
988 0, /* tp_as_sequence */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
989 0, /* tp_as_mapping */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
990 0, /* tp_hash */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
991 0, /* tp_call */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
992 0, /* tp_str */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
993 PyObject_GenericGetAttr, /* tp_getattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
994 0, /* tp_setattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
995 0, /* tp_as_buffer */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
996 Py_TPFLAGS_DEFAULT, /* tp_flags */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
997 0, /* tp_doc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
998 0, /* tp_traverse */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
999 0, /* tp_clear */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1000 0, /* tp_richcompare */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1001 0, /* tp_weaklistoffset */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1002 PyObject_SelfIter, /* tp_iter */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1003 (iternextfunc)rangeiter_next, /* tp_iternext */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1004 rangeiter_methods, /* tp_methods */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1005 0, /* tp_members */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1006 0, /* tp_getset */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1007 0, /* tp_base */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1008 0, /* tp_dict */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1009 0, /* tp_descr_get */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1010 0, /* tp_descr_set */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1011 0, /* tp_dictoffset */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1012 0, /* tp_init */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1013 0, /* tp_alloc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1014 rangeiter_new, /* tp_new */
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1015 };
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1016
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1017 /* Return number of items in range (lo, hi, step). step != 0
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1018 * 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
1019 */
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1020 static unsigned long
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1021 get_len_of_range(long lo, long hi, long step)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1022 {
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1023 /* -------------------------------------------------------------
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1024 If step > 0 and lo >= hi, or step < 0 and lo <= hi, the range is empty.
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1025 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
1026 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
1027 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
1028 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
1029 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
1030 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
1031 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
1032 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1033 precision to compute the RHS exactly. The analysis for step < 0
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1034 is similar.
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1035 ---------------------------------------------------------------*/
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1036 assert(step != 0);
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1037 if (step > 0 && lo < hi)
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1038 return 1UL + (hi - 1UL - lo) / step;
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1039 else if (step < 0 && lo > hi)
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1040 return 1UL + (lo - 1UL - hi) / (0UL - step);
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1041 else
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1042 return 0UL;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1043 }
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1044
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1045 /* Initialize a rangeiter object. If the length of the rangeiter object
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1046 is not representable as a C long, OverflowError is raised. */
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1047
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1048 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
1049 fast_range_iter(long start, long stop, long step)
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1050 {
44061
0cc552ab76bd Added all PyTypeObjects to the appropriate header files.
Christian Heimes <christian@cheimes.de>
parents: 44043
diff changeset
1051 rangeiterobject *it = PyObject_New(rangeiterobject, &PyRangeIter_Type);
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1052 unsigned long ulen;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1053 if (it == NULL)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1054 return NULL;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1055 it->start = start;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1056 it->step = step;
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1057 ulen = get_len_of_range(start, stop, step);
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1058 if (ulen > (unsigned long)LONG_MAX) {
57287
f1112c2700b1 Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57268
diff changeset
1059 Py_DECREF(it);
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1060 PyErr_SetString(PyExc_OverflowError,
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1061 "range too large to represent as a range_iterator");
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1062 return NULL;
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1063 }
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1064 it->len = (long)ulen;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1065 it->index = 0;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1066 return (PyObject *)it;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1067 }
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1068
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1069 static PyObject *
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1070 rangeiter_new(PyTypeObject *type, PyObject *args, PyObject *kw)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1071 {
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1072 long start, stop, step;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1073
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1074 if (!_PyArg_NoKeywords("rangeiter()", kw))
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1075 return NULL;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1076
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1077 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
1078 &start, &stop, &step))
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1079 return NULL;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1080
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
1081 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
1082 }
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
1083
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
1084 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
1085 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
1086 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
1087 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
1088 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
1089 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
1090 } 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
1091
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
1092 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
1093 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
1094 {
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
1095 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
1096 }
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1097
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1098 static PyMethodDef longrangeiter_methods[] = {
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1099 {"__length_hint__", (PyCFunction)longrangeiter_len, METH_NOARGS,
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1100 length_hint_doc},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1101 {NULL, NULL} /* sentinel */
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1102 };
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1103
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1104 static void
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1105 longrangeiter_dealloc(longrangeiterobject *r)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1106 {
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1107 Py_XDECREF(r->index);
41644
9cc4c3f0b580 Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
1108 Py_XDECREF(r->start);
9cc4c3f0b580 Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
1109 Py_XDECREF(r->step);
9cc4c3f0b580 Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
1110 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
1111 PyObject_Del(r);
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1112 }
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1113
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1114 static PyObject *
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1115 longrangeiter_next(longrangeiterobject *r)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1116 {
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1117 PyObject *one, *product, *new_index, *result;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1118 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
1119 return NULL;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1120
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1121 one = PyLong_FromLong(1);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1122 if (!one)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1123 return NULL;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1124
57287
f1112c2700b1 Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57268
diff changeset
1125 new_index = PyNumber_Add(r->index, one);
f1112c2700b1 Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57268
diff changeset
1126 Py_DECREF(one);
f1112c2700b1 Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57268
diff changeset
1127 if (!new_index)
f1112c2700b1 Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57268
diff changeset
1128 return NULL;
f1112c2700b1 Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57268
diff changeset
1129
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1130 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
1131 if (!product) {
57287
f1112c2700b1 Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57268
diff changeset
1132 Py_DECREF(new_index);
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1133 return NULL;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1134 }
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1135
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1136 result = PyNumber_Add(r->start, product);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1137 Py_DECREF(product);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1138 if (result) {
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1139 Py_DECREF(r->index);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1140 r->index = new_index;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1141 }
57287
f1112c2700b1 Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57268
diff changeset
1142 else {
f1112c2700b1 Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57268
diff changeset
1143 Py_DECREF(new_index);
f1112c2700b1 Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57268
diff changeset
1144 }
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1145
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1146 return result;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1147 }
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1148
44061
0cc552ab76bd Added all PyTypeObjects to the appropriate header files.
Christian Heimes <christian@cheimes.de>
parents: 44043
diff changeset
1149 PyTypeObject PyLongRangeIter_Type = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1150 PyVarObject_HEAD_INIT(&PyType_Type, 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1151 "longrange_iterator", /* tp_name */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1152 sizeof(longrangeiterobject), /* tp_basicsize */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1153 0, /* tp_itemsize */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1154 /* methods */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1155 (destructor)longrangeiter_dealloc, /* tp_dealloc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1156 0, /* tp_print */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1157 0, /* tp_getattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1158 0, /* tp_setattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1159 0, /* tp_reserved */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1160 0, /* tp_repr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1161 0, /* tp_as_number */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1162 0, /* tp_as_sequence */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1163 0, /* tp_as_mapping */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1164 0, /* tp_hash */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1165 0, /* tp_call */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1166 0, /* tp_str */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1167 PyObject_GenericGetAttr, /* tp_getattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1168 0, /* tp_setattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1169 0, /* tp_as_buffer */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1170 Py_TPFLAGS_DEFAULT, /* tp_flags */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1171 0, /* tp_doc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1172 0, /* tp_traverse */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1173 0, /* tp_clear */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1174 0, /* tp_richcompare */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1175 0, /* tp_weaklistoffset */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1176 PyObject_SelfIter, /* tp_iter */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1177 (iternextfunc)longrangeiter_next, /* tp_iternext */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1178 longrangeiter_methods, /* tp_methods */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1179 0,
23693
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
1180 };
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
1181
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36501
diff changeset
1182 static PyObject *
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36501
diff changeset
1183 range_iter(PyObject *seq)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36501
diff changeset
1184 {
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1185 rangeobject *r = (rangeobject *)seq;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1186 longrangeiterobject *it;
44366
83ced4177eac Drop _PyLong_FitsInLong. Fixes #1666.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
1187 long lstart, lstop, lstep;
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1188 PyObject *int_it;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1189
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1190 assert(PyRange_Check(seq));
44366
83ced4177eac Drop _PyLong_FitsInLong. Fixes #1666.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
1191
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1192 /* If all three fields and the length convert to long, use the int
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1193 * version */
44366
83ced4177eac Drop _PyLong_FitsInLong. Fixes #1666.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
1194 lstart = PyLong_AsLong(r->start);
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1195 if (lstart == -1 && PyErr_Occurred()) {
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1196 PyErr_Clear();
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1197 goto long_range;
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1198 }
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1199 lstop = PyLong_AsLong(r->stop);
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1200 if (lstop == -1 && PyErr_Occurred()) {
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1201 PyErr_Clear();
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1202 goto long_range;
44366
83ced4177eac Drop _PyLong_FitsInLong. Fixes #1666.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
1203 }
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1204 lstep = PyLong_AsLong(r->step);
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1205 if (lstep == -1 && PyErr_Occurred()) {
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1206 PyErr_Clear();
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1207 goto long_range;
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1208 }
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
1209 int_it = fast_range_iter(lstart, lstop, lstep);
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1210 if (int_it == NULL && PyErr_ExceptionMatches(PyExc_OverflowError)) {
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1211 PyErr_Clear();
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1212 goto long_range;
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1213 }
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1214 return (PyObject *)int_it;
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
1215
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1216 long_range:
44061
0cc552ab76bd Added all PyTypeObjects to the appropriate header files.
Christian Heimes <christian@cheimes.de>
parents: 44043
diff changeset
1217 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
1218 if (it == NULL)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1219 return NULL;
41644
9cc4c3f0b580 Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
1220
9cc4c3f0b580 Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
1221 /* 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
1222 it->start = r->start;
41644
9cc4c3f0b580 Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
1223 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
1224 it->len = r->length;
41644
9cc4c3f0b580 Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
1225 Py_INCREF(it->start);
9cc4c3f0b580 Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
1226 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
1227 Py_INCREF(it->len);
41644
9cc4c3f0b580 Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
1228
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1229 it->index = PyLong_FromLong(0);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1230 if (!it->index)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1231 goto create_failure;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1232
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1233 return (PyObject *)it;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1234
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1235 create_failure:
41644
9cc4c3f0b580 Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
1236 Py_DECREF(it);
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1237 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
1238 }
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36501
diff changeset
1239
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36501
diff changeset
1240 static PyObject *
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36501
diff changeset
1241 range_reverse(PyObject *seq)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36501
diff changeset
1242 {
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1243 rangeobject *range = (rangeobject*) seq;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1244 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
1245 PyObject *one, *sum, *diff, *product;
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1246 long lstart, lstop, lstep, new_start, new_stop;
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1247 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
1248
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1249 assert(PyRange_Check(seq));
44366
83ced4177eac Drop _PyLong_FitsInLong. Fixes #1666.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
1250
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1251 /* reversed(range(start, stop, step)) can be expressed as
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1252 range(start+(n-1)*step, start-step, -step), where n is the number of
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1253 integers in the range.
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1254
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1255 If each of start, stop, step, -step, start-step, and the length
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1256 of the iterator is representable as a C long, use the int
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1257 version. This excludes some cases where the reversed range is
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1258 representable as a range_iterator, but it's good enough for
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1259 common cases and it makes the checks simple. */
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1260
44366
83ced4177eac Drop _PyLong_FitsInLong. Fixes #1666.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
1261 lstart = PyLong_AsLong(range->start);
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1262 if (lstart == -1 && PyErr_Occurred()) {
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1263 PyErr_Clear();
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1264 goto long_range;
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1265 }
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1266 lstop = PyLong_AsLong(range->stop);
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1267 if (lstop == -1 && PyErr_Occurred()) {
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1268 PyErr_Clear();
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1269 goto long_range;
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1270 }
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1271 lstep = PyLong_AsLong(range->step);
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1272 if (lstep == -1 && PyErr_Occurred()) {
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1273 PyErr_Clear();
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1274 goto long_range;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1275 }
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1276 /* check for possible overflow of -lstep */
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1277 if (lstep == LONG_MIN)
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1278 goto long_range;
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1279
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1280 /* check for overflow of lstart - lstep:
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1281
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1282 for lstep > 0, need only check whether lstart - lstep < LONG_MIN.
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1283 for lstep < 0, need only check whether lstart - lstep > LONG_MAX
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1284
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1285 Rearrange these inequalities as:
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1286
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1287 lstart - LONG_MIN < lstep (lstep > 0)
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1288 LONG_MAX - lstart < -lstep (lstep < 0)
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1289
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1290 and compute both sides as unsigned longs, to avoid the
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1291 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
1292
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1293 if (lstep > 0) {
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1294 if ((unsigned long)lstart - LONG_MIN < (unsigned long)lstep)
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1295 goto long_range;
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1296 }
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1297 else {
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1298 if (LONG_MAX - (unsigned long)lstart < 0UL - lstep)
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1299 goto long_range;
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1300 }
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1301
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1302 ulen = get_len_of_range(lstart, lstop, lstep);
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1303 if (ulen > (unsigned long)LONG_MAX)
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1304 goto long_range;
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1305
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1306 new_stop = lstart - lstep;
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1307 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
1308 return fast_range_iter(new_start, new_stop, -lstep);
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1309
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1310 long_range:
44061
0cc552ab76bd Added all PyTypeObjects to the appropriate header files.
Christian Heimes <christian@cheimes.de>
parents: 44043
diff changeset
1311 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
1312 if (it == NULL)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1313 return NULL;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1314
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1315 /* 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
1316 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
1317 Py_INCREF(it->len);
57287
f1112c2700b1 Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57268
diff changeset
1318
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1319 one = PyLong_FromLong(1);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1320 if (!one)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1321 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
1322
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
1323 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
1324 Py_DECREF(one);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1325 if (!diff)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1326 goto create_failure;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1327
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1328 product = PyNumber_Multiply(diff, range->step);
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1329 Py_DECREF(diff);
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1330 if (!product)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1331 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
1332
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1333 sum = PyNumber_Add(range->start, product);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1334 Py_DECREF(product);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1335 it->start = sum;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1336 if (!it->start)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1337 goto create_failure;
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1338
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1339 it->step = PyNumber_Negative(range->step);
57287
f1112c2700b1 Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57268
diff changeset
1340 if (!it->step)
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1341 goto create_failure;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1342
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1343 it->index = PyLong_FromLong(0);
57287
f1112c2700b1 Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57268
diff changeset
1344 if (!it->index)
f1112c2700b1 Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57268
diff changeset
1345 goto create_failure;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1346
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1347 return (PyObject *)it;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1348
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1349 create_failure:
57287
f1112c2700b1 Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57268
diff changeset
1350 Py_DECREF(it);
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1351 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
1352 }