annotate Objects/rangeobject.c @ 73323:dad02a080bbc

Improve byte-compilation in packaging to be independent of -O or -B. The code I fixed to comply with PEP 3147 still had one bug: When run under python -O, some paths for pyc files would be pyo, because I called imp.cache_from_source without explicit debug_override argument in some places, and under -O that would return .pyo (this is well explained in the imp docs). Now all code (util.byte_compile, build_py, install_lib) can create .pyo files according to options given by users, without interference from the calling Python’s own optimize mode. On a related topic, I also removed the code that prevented byte compilation under python -B. The rationale is that packaging gives control over the creation of pyc files to the user with its own explicit option, and the behavior should not be changed if the calling Python happens to run with -B for whatever reason. I will argue that this is a bug fix and ask to be allowed to backport this change to distutils. Finally, I moved one nugget of information about the --compile and --optimize options from the source into the doc. It clears up a misunderstanding that I (and maybe other people) had.
author Éric Araujo <merwok@netwok.org>
date Thu, 03 Nov 2011 05:08:28 +0100
parents 479a7dd1ea6a
children 4643be424293
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /* Range object implementation */
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
3 #include "Python.h"
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
4
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
5 /* Support objects whose length is > PY_SSIZE_T_MAX.
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
6
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
7 This could be sped up for small PyLongs if they fit in an Py_ssize_t.
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
8 This only matters on Win64. Though we could use PY_LONG_LONG which
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
9 would presumably help perf.
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
10 */
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
11
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 typedef struct {
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
13 PyObject_HEAD
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
14 PyObject *start;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
15 PyObject *stop;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
16 PyObject *step;
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
17 PyObject *length;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 } rangeobject;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
20 /* Helper function for validating step. Always returns a new reference or
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
21 NULL on error.
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
22 */
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
23 static PyObject *
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
24 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
25 {
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
26 /* No step specified, use a step of 1. */
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
27 if (!step)
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 44061
diff changeset
28 return PyLong_FromLong(1);
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
29
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
30 step = PyNumber_Index(step);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
31 if (step) {
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
32 Py_ssize_t istep = PyNumber_AsSsize_t(step, NULL);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
33 if (istep == -1 && PyErr_Occurred()) {
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
34 /* Ignore OverflowError, we know the value isn't 0. */
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
35 PyErr_Clear();
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
36 }
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
37 else if (istep == 0) {
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
38 PyErr_SetString(PyExc_ValueError,
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
39 "range() arg 3 must not be zero");
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
40 Py_CLEAR(step);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
41 }
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
42 }
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
43
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
44 return step;
23695
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
45 }
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
46
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
47 static PyObject *
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
48 compute_range_length(PyObject *start, PyObject *stop, PyObject *step);
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
49
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
50 static rangeobject *
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
51 make_range_object(PyTypeObject *type, PyObject *start,
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
52 PyObject *stop, PyObject *step)
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
53 {
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
54 rangeobject *obj = NULL;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
55 PyObject *length;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
56 length = compute_range_length(start, stop, step);
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
57 if (length == NULL) {
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
58 return NULL;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
59 }
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
60 obj = PyObject_New(rangeobject, type);
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
61 if (obj == NULL) {
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
62 Py_DECREF(length);
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
63 return NULL;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
64 }
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
65 obj->start = start;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
66 obj->stop = stop;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
67 obj->step = step;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
68 obj->length = length;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
69 return obj;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
70 }
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
71
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
72 /* XXX(nnorwitz): should we error check if the user passes any empty ranges?
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
73 range(-10)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
74 range(0, -5)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
75 range(0, 5, -1)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
76 */
23695
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
77 static PyObject *
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
78 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
79 {
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
80 rangeobject *obj;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
81 PyObject *start = NULL, *stop = NULL, *step = NULL;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
82
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
83 if (!_PyArg_NoKeywords("range()", kw))
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
84 return NULL;
23695
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
85
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
86 if (PyTuple_Size(args) <= 1) {
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
87 if (!PyArg_UnpackTuple(args, "range", 1, 1, &stop))
54726
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
88 return NULL;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
89 stop = PyNumber_Index(stop);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
90 if (!stop)
54726
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
91 return NULL;
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 44061
diff changeset
92 start = PyLong_FromLong(0);
54726
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
93 if (!start) {
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
94 Py_DECREF(stop);
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
95 return NULL;
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
96 }
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 44061
diff changeset
97 step = PyLong_FromLong(1);
54726
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
98 if (!step) {
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
99 Py_DECREF(stop);
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
100 Py_DECREF(start);
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
101 return NULL;
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
102 }
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
103 }
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
104 else {
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
105 if (!PyArg_UnpackTuple(args, "range", 2, 3,
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
106 &start, &stop, &step))
54726
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
107 return NULL;
35449
1fdfd0130e28 Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents: 34087
diff changeset
108
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
109 /* Convert borrowed refs to owned refs */
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
110 start = PyNumber_Index(start);
54726
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
111 if (!start)
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
112 return NULL;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
113 stop = PyNumber_Index(stop);
54726
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
114 if (!stop) {
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
115 Py_DECREF(start);
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
116 return NULL;
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
117 }
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
118 step = validate_step(step); /* Caution, this can clear exceptions */
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
119 if (!step) {
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
120 Py_DECREF(start);
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
121 Py_DECREF(stop);
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
122 return NULL;
75c4d3bc6cc8 Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
123 }
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
124 }
33047
c81ed079576f Bug 1003935: xrange overflows
Tim Peters <tim.peters@gmail.com>
parents: 33046
diff changeset
125
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
126 obj = make_range_object(type, start, stop, step);
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
127 if (obj != NULL)
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
128 return (PyObject *) obj;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
129
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
130 /* Failed to create object, release attributes */
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
131 Py_XDECREF(start);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
132 Py_XDECREF(stop);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
133 Py_XDECREF(step);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
134 return NULL;
23695
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
135 }
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
136
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23702
diff changeset
137 PyDoc_STRVAR(range_doc,
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
138 "range([start,] stop[, step]) -> range object\n\
23695
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
139 \n\
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
140 Returns a virtual sequence of numbers from start to stop by step.");
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
141
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
142 static void
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
143 range_dealloc(rangeobject *r)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
144 {
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
145 Py_DECREF(r->start);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
146 Py_DECREF(r->stop);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
147 Py_DECREF(r->step);
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
148 Py_DECREF(r->length);
43867
f444e259341a Found another memory leak in longrangeiter. And redo the previous correction
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43861
diff changeset
149 PyObject_Del(r);
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
150 }
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
151
55589
546aa134d773 Merged revisions 73656,73658,73663,73666 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54847
diff changeset
152 /* Return number of items in range (lo, hi, step) as a PyLong object,
546aa134d773 Merged revisions 73656,73658,73663,73666 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54847
diff changeset
153 * when arguments are PyLong objects. Arguments MUST return 1 with
546aa134d773 Merged revisions 73656,73658,73663,73666 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54847
diff changeset
154 * PyLong_Check(). Return NULL when there is an error.
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
155 */
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
156 static PyObject*
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
157 compute_range_length(PyObject *start, PyObject *stop, PyObject *step)
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
158 {
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
159 /* -------------------------------------------------------------
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
160 Algorithm is equal to that of get_len_of_range(), but it operates
54846
79a630da8f00 fix comment
Benjamin Peterson <benjamin@python.org>
parents: 54845
diff changeset
161 on PyObjects (which are assumed to be PyLong objects).
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
162 ---------------------------------------------------------------*/
51244
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47536
diff changeset
163 int cmp_result;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
164 PyObject *lo, *hi;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
165 PyObject *diff = NULL;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
166 PyObject *one = NULL;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
167 PyObject *tmp1 = NULL, *tmp2 = NULL, *result;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
168 /* holds sub-expression evaluations */
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
169
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
170 PyObject *zero = PyLong_FromLong(0);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
171 if (zero == NULL)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
172 return NULL;
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
173 cmp_result = PyObject_RichCompareBool(step, zero, Py_GT);
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
174 Py_DECREF(zero);
51244
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47536
diff changeset
175 if (cmp_result == -1)
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
176 return NULL;
23695
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23693
diff changeset
177
51244
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47536
diff changeset
178 if (cmp_result == 1) {
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
179 lo = start;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
180 hi = stop;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
181 Py_INCREF(step);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
182 } else {
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
183 lo = stop;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
184 hi = start;
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
185 step = PyNumber_Negative(step);
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
186 if (!step)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
187 return NULL;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
188 }
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
189
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
190 /* if (lo >= hi), return length of 0. */
51244
e9675d72e3e3 Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47536
diff changeset
191 if (PyObject_RichCompareBool(lo, hi, Py_GE) == 1) {
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
192 Py_XDECREF(step);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
193 return PyLong_FromLong(0);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
194 }
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
195
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
196 if ((one = PyLong_FromLong(1L)) == NULL)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
197 goto Fail;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
198
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
199 if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
200 goto Fail;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
201
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
202 if ((diff = PyNumber_Subtract(tmp1, one)) == NULL)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
203 goto Fail;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
204
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
205 if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
206 goto Fail;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
207
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
208 if ((result = PyNumber_Add(tmp2, one)) == NULL)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
209 goto Fail;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
210
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
211 Py_DECREF(tmp2);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
212 Py_DECREF(diff);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
213 Py_DECREF(step);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
214 Py_DECREF(tmp1);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
215 Py_DECREF(one);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
216 return result;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
217
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
218 Fail:
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
219 Py_XDECREF(tmp2);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
220 Py_XDECREF(diff);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
221 Py_XDECREF(step);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
222 Py_XDECREF(tmp1);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
223 Py_XDECREF(one);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
224 return NULL;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
226
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
227 static Py_ssize_t
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
228 range_length(rangeobject *r)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 {
66641
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
230 return PyLong_AsSsize_t(r->length);
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
231 }
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
232
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
233 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
234 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
235 {
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
236 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
237 /* 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
238 * 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
239 */
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
241 if (!incr)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
242 return NULL;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
243 result = PyNumber_Add(r->start, incr);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
244 Py_DECREF(incr);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
245 return result;
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
246 }
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
247
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
248 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
249 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
250 {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
252 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
253
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
255 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
256 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
257
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 /* 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
259 * 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
260 * 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
261 * } 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
262 * 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
263 * }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
266 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
267 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
268 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
269 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
271 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
272 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
273 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
274 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
275 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 } 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
277 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
278 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
279 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 /* 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
282 * 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
283 * <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
284 * }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
287 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
288 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
289 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
290 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
292 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
293 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
294 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
296 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
297 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
298 "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
299 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
300 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
303 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
304 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
305 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
308 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
309 {
67575
10d67db67942 plug reference leak
Benjamin Peterson <benjamin@python.org>
parents: 67543
diff changeset
310 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
311 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
312 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
313 }
67575
10d67db67942 plug reference leak
Benjamin Peterson <benjamin@python.org>
parents: 67543
diff changeset
314 res = compute_range_item(r, arg);
10d67db67942 plug reference leak
Benjamin Peterson <benjamin@python.org>
parents: 67543
diff changeset
315 Py_DECREF(arg);
10d67db67942 plug reference leak
Benjamin Peterson <benjamin@python.org>
parents: 67543
diff changeset
316 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
317 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 /* 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
320 * 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
321 */
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 /* 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
324 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
325 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
326 {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
328 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
329 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
330 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
331 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
334 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
335 "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
336 "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
337 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
339 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 /* 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
342 * 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
343 * (-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
344 */
67642
faec1b5a96db rangeobject.c (compute_slice_indices): Make function static.
Matthias Klose <doko@ubuntu.com>
parents: 67575
diff changeset
345 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
346 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
347 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
348 {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
350 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
351 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
352 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
353 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
354 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
355 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
356 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
357 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
358 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
359
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 /* 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
361 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
362 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
363 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
364 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
365 } 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
366 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
367 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
368 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
369 "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
370 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
371 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
373 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
374 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 /* 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
377 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
378 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
379 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
380 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
381 } 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
382 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
383 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
384 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 } 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
386 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
387 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
388 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
389 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
390 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
391 /* 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
392 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
393 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
394 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
395 } 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
396 /* 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
397 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
398 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
399 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
401 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
402 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
403 /* 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
404 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
405 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
406 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
407 } 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
408 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
409 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
411 } 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
412 /* 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
413 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
414 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
415 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
416 /* 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
417 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
418 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
419 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
420 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
421 } 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
422 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
423 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
424 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 /* 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
430 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
431 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
432 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
433 } 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
434 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
435 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
437 } 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
438 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
439 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
440 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
441 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
442 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
443 /* 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
444 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
445 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
446 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
447 } 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
448 /* 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
449 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
450 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
451 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
453 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
454 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
455 /* 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
456 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
457 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
458 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
459 } 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
460 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
461 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
463 } 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
464 /* 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
465 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
466 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
467 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
468 /* 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
469 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
470 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
471 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
472 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
473 } 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
474 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
475 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
476 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 /* 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
482 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
483 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
484 } 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
485 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
486 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
488
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 *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
490 *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
491 *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
492 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
493 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
494 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
495 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
496
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
498 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
499 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
500 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
501 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
502 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
503 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
504 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
505 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
506 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
509 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
510 {
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
512 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
513 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
514 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
515 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
516
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
518 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
519
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
521 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
522 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
523
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
525 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
526 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
527
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
529 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
530 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
531 } 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
532 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
533 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
534 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
536
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
538 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
539 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
540 }
fef521f45af3 Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
542 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
543 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
544 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
545 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
546 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
547 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
548 return NULL;
47536
33492c241dab Issue 2582: Fix pickling of range objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46564
diff changeset
549 }
33492c241dab Issue 2582: Fix pickling of range objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46564
diff changeset
550
64725
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
551 /* 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
552 static int
66335
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
553 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
554 {
64725
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
555 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
556 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
557 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
558 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
559 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
560
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
561 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
562 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
563 goto end;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
564
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
565 /* 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
566
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
567 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
568 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
569 goto end;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
570 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
571 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
572 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
573 }
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
574 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
575 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
576 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
577 }
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 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
580 goto end;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
581 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
582 result = 0;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
583 goto end;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
584 }
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 /* 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
587 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
588 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
589 goto end;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
590 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
591 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
592 goto end;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
593 /* 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
594 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
595 end:
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
596 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
597 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
598 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
599 return result;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
600 }
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
601
56192
d599a3f2e72d Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
602 static int
66335
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
603 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
604 {
64725
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
605 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
606 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
607
d599a3f2e72d Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
608 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
609 PY_ITERSEARCH_CONTAINS);
d599a3f2e72d Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
610 }
d599a3f2e72d Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
611
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
612 /* 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
613 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
614
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 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
616 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
617 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
618 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
619 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
620 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
621 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
622 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
623 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
624 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
625 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
626 */
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 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
628 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
629 {
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 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
631 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
632
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 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
634 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
635 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
636 /* 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
637 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
638 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
639 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
640 /* 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
641 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
642 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
643 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
644 /* 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
645 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
646 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
647 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
648 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
649 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
650 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
651 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
652 /* 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
653 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
654 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
655 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
656 }
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 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
659 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
660 {
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 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
662
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 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
664 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
665 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
666 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
667 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
668 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
669 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
670 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
671 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
672 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
673 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
674 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
675 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
676 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
677 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
678 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
679 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
680 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
681 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
682 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
683 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
684 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
685 }
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 /* 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
689
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 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
691 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
692 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
693 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
694 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
695 */
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 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
697 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
698 {
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 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
700 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
701 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
702
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 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
704 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
705 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
706 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
707 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
708 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
709 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
710 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
711 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
712 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
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 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
715 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
716 }
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 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
718 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
719 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
720 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
721 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
722 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
723 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
724 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
725 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
726 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
727 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
728 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
729 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
730 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
731 }
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 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
733 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
734 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
735 }
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 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
738 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
739 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
740 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
741 }
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
64725
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
743 static PyObject *
66335
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
744 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
745 {
64725
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
746 if (PyLong_CheckExact(ob) || PyBool_Check(ob)) {
66330
add7e9721f80 Add error handling in range_count.
Georg Brandl <georg@python.org>
parents: 66329
diff changeset
747 int result = range_contains_long(r, ob);
add7e9721f80 Add error handling in range_count.
Georg Brandl <georg@python.org>
parents: 66329
diff changeset
748 if (result == -1)
add7e9721f80 Add error handling in range_count.
Georg Brandl <georg@python.org>
parents: 66329
diff changeset
749 return NULL;
add7e9721f80 Add error handling in range_count.
Georg Brandl <georg@python.org>
parents: 66329
diff changeset
750 else if (result)
66329
238caaf6852b count() should return integers #10474
Benjamin Peterson <benjamin@python.org>
parents: 64725
diff changeset
751 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
752 else
66329
238caaf6852b count() should return integers #10474
Benjamin Peterson <benjamin@python.org>
parents: 64725
diff changeset
753 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
754 } else {
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
755 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
756 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
757 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
758 return NULL;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
759 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
760 }
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 static PyObject *
66335
acd962824819 pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents: 66331
diff changeset
764 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
765 {
64725
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
766 int contains;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
767
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
768 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
769 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
770 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
771 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
772 return NULL;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
773 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
774 }
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 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
777 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
778 return NULL;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
779
66331
e01237831ad2 code style and simplification
Benjamin Peterson <benjamin@python.org>
parents: 66330
diff changeset
780 if (contains) {
e01237831ad2 code style and simplification
Benjamin Peterson <benjamin@python.org>
parents: 66330
diff changeset
781 PyObject *idx, *tmp = PyNumber_Subtract(ob, r->start);
e01237831ad2 code style and simplification
Benjamin Peterson <benjamin@python.org>
parents: 66330
diff changeset
782 if (tmp == NULL)
e01237831ad2 code style and simplification
Benjamin Peterson <benjamin@python.org>
parents: 66330
diff changeset
783 return NULL;
e01237831ad2 code style and simplification
Benjamin Peterson <benjamin@python.org>
parents: 66330
diff changeset
784 /* idx = (ob - r.start) // r.step */
e01237831ad2 code style and simplification
Benjamin Peterson <benjamin@python.org>
parents: 66330
diff changeset
785 idx = PyNumber_FloorDivide(tmp, r->step);
e01237831ad2 code style and simplification
Benjamin Peterson <benjamin@python.org>
parents: 66330
diff changeset
786 Py_DECREF(tmp);
e01237831ad2 code style and simplification
Benjamin Peterson <benjamin@python.org>
parents: 66330
diff changeset
787 return idx;
e01237831ad2 code style and simplification
Benjamin Peterson <benjamin@python.org>
parents: 66330
diff changeset
788 }
64725
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
789
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
790 /* object is not in the range */
66331
e01237831ad2 code style and simplification
Benjamin Peterson <benjamin@python.org>
parents: 66330
diff changeset
791 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
792 return NULL;
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
793 }
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
794
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
795 static PySequenceMethods range_as_sequence = {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57286
diff changeset
796 (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
797 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
798 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
799 (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
800 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
801 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
802 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
803 (objobjproc)range_contains, /* sq_contains */
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
804 };
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
805
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
806 static PyObject *
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
807 range_repr(rangeobject *r)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
808 {
41800
993f8a9b8ab3 repr(range(10)) now returns 'range(0, 10)' for clarity.
Walter Dörwald <walter@livinglogic.de>
parents: 41785
diff changeset
809 Py_ssize_t istep;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
810
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
811 /* 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
812 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
813 (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
814 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
815 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
816 PyErr_Clear();
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
817 }
13848
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
818
41800
993f8a9b8ab3 repr(range(10)) now returns 'range(0, 10)' for clarity.
Walter Dörwald <walter@livinglogic.de>
parents: 41785
diff changeset
819 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
820 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
821 else
d1c38af065bf Change range_repr() to use %R for the start/stop/step attributes.
Walter Dörwald <walter@livinglogic.de>
parents: 41769
diff changeset
822 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
823 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
824 }
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
825
47536
33492c241dab Issue 2582: Fix pickling of range objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46564
diff changeset
826 /* Pickling support */
33492c241dab Issue 2582: Fix pickling of range objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46564
diff changeset
827 static PyObject *
33492c241dab Issue 2582: Fix pickling of range objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46564
diff changeset
828 range_reduce(rangeobject *r, PyObject *args)
33492c241dab Issue 2582: Fix pickling of range objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46564
diff changeset
829 {
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
830 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
831 r->start, r->stop, r->step);
33492c241dab Issue 2582: Fix pickling of range objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46564
diff changeset
832 }
33492c241dab Issue 2582: Fix pickling of range objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46564
diff changeset
833
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
834 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
835 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
836 {
78216263425d Issue 2690: Add support for 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 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
838 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
839 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
840 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
841 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
842 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
843 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
844 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
845 }
78216263425d Issue 2690: Add support for 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 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
847 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
848 }
78216263425d Issue 2690: Add support for 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 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
850 "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
851 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
852 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
853 }
78216263425d Issue 2690: Add support for 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 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
857 (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
858 (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
859 (objobjargproc)0, /* mp_ass_subscript */
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
860 };
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
861
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24260
diff changeset
862 static PyObject * range_iter(PyObject *seq);
30865
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
863 static PyObject * range_reverse(PyObject *seq);
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
864
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
865 PyDoc_STRVAR(reverse_doc,
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
866 "Returns a reverse iterator.");
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
867
64725
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
868 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
869 "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
870
e2dec9c0d13c Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
871 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
872 "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
873 "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
874
30865
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
875 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
876 {"__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
877 {"__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
878 {"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
879 {"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
880 {NULL, NULL} /* sentinel */
30865
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
881 };
23693
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
882
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
883 PyTypeObject PyRange_Type = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
884 PyVarObject_HEAD_INIT(&PyType_Type, 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
885 "range", /* Name of this type */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
886 sizeof(rangeobject), /* Basic object size */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
887 0, /* Item size for varobject */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
888 (destructor)range_dealloc, /* tp_dealloc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
889 0, /* tp_print */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
890 0, /* tp_getattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
891 0, /* tp_setattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
892 0, /* tp_reserved */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
893 (reprfunc)range_repr, /* tp_repr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
894 0, /* tp_as_number */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
895 &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
896 &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
897 (hashfunc)range_hash, /* tp_hash */
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
898 0, /* tp_call */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
899 0, /* tp_str */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
900 PyObject_GenericGetAttr, /* tp_getattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
901 0, /* tp_setattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
902 0, /* tp_as_buffer */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
903 Py_TPFLAGS_DEFAULT, /* tp_flags */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
904 range_doc, /* tp_doc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
905 0, /* tp_traverse */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
906 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
907 range_richcompare, /* tp_richcompare */
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
908 0, /* tp_weaklistoffset */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
909 range_iter, /* tp_iter */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
910 0, /* tp_iternext */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
911 range_methods, /* tp_methods */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
912 0, /* tp_members */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
913 0, /* tp_getset */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
914 0, /* tp_base */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
915 0, /* tp_dict */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
916 0, /* tp_descr_get */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
917 0, /* tp_descr_set */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
918 0, /* tp_dictoffset */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
919 0, /* tp_init */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
920 0, /* tp_alloc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
921 range_new, /* tp_new */
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
922 };
23693
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
923
41803
3d3bf27409e6 Rename test_xrange.py to test_range.py and fix the
Walter Dörwald <walter@livinglogic.de>
parents: 41800
diff changeset
924 /*********************** range Iterator **************************/
23693
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
925
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
926 /* 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
927 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
928 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
929 */
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
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 typedef struct {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
932 PyObject_HEAD
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
933 long index;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
934 long start;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
935 long step;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
936 long len;
23693
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
937 } rangeiterobject;
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
938
30865
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
939 static PyObject *
23693
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
940 rangeiter_next(rangeiterobject *r)
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
941 {
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
942 if (r->index < r->len)
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
943 /* 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
944 in intermediate calculations. */
9505afe8823f Merged revisions 76298 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 57263
diff changeset
945 return PyLong_FromLong((long)(r->start +
9505afe8823f Merged revisions 76298 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 57263
diff changeset
946 (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
947 return NULL;
23693
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
948 }
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
949
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
950 static PyObject *
31635
04eb2ebb46e2 Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents: 30865
diff changeset
951 rangeiter_len(rangeiterobject *r)
04eb2ebb46e2 Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents: 30865
diff changeset
952 {
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
953 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
954 }
04eb2ebb46e2 Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents: 30865
diff changeset
955
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
956 PyDoc_STRVAR(length_hint_doc,
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
957 "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
958
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
959 static PyMethodDef rangeiter_methods[] = {
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
960 {"__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
961 length_hint_doc},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
962 {NULL, NULL} /* sentinel */
31635
04eb2ebb46e2 Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents: 30865
diff changeset
963 };
04eb2ebb46e2 Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents: 30865
diff changeset
964
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
965 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
966
44061
0cc552ab76bd Added all PyTypeObjects to the appropriate header files.
Christian Heimes <christian@cheimes.de>
parents: 44043
diff changeset
967 PyTypeObject PyRangeIter_Type = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
968 PyVarObject_HEAD_INIT(&PyType_Type, 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
969 "range_iterator", /* tp_name */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
970 sizeof(rangeiterobject), /* tp_basicsize */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
971 0, /* tp_itemsize */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
972 /* methods */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
973 (destructor)PyObject_Del, /* tp_dealloc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
974 0, /* tp_print */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
975 0, /* tp_getattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
976 0, /* tp_setattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
977 0, /* tp_reserved */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
978 0, /* tp_repr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
979 0, /* tp_as_number */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
980 0, /* tp_as_sequence */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
981 0, /* tp_as_mapping */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
982 0, /* tp_hash */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
983 0, /* tp_call */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
984 0, /* tp_str */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
985 PyObject_GenericGetAttr, /* tp_getattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
986 0, /* tp_setattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
987 0, /* tp_as_buffer */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
988 Py_TPFLAGS_DEFAULT, /* tp_flags */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
989 0, /* tp_doc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
990 0, /* tp_traverse */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
991 0, /* tp_clear */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
992 0, /* tp_richcompare */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
993 0, /* tp_weaklistoffset */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
994 PyObject_SelfIter, /* tp_iter */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
995 (iternextfunc)rangeiter_next, /* tp_iternext */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
996 rangeiter_methods, /* tp_methods */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
997 0, /* tp_members */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
998 0, /* tp_getset */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
999 0, /* tp_base */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1000 0, /* tp_dict */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1001 0, /* tp_descr_get */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1002 0, /* tp_descr_set */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1003 0, /* tp_dictoffset */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1004 0, /* tp_init */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1005 0, /* tp_alloc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1006 rangeiter_new, /* tp_new */
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1007 };
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1008
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1009 /* 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
1010 * 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
1011 */
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1012 static unsigned long
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1013 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
1014 {
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1015 /* -------------------------------------------------------------
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1016 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
1017 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
1018 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
1019 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
1020 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
1021 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
1022 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
1023 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
1024 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
1025 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
1026 is similar.
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1027 ---------------------------------------------------------------*/
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1028 assert(step != 0);
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1029 if (step > 0 && lo < hi)
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1030 return 1UL + (hi - 1UL - lo) / step;
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1031 else if (step < 0 && lo > hi)
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1032 return 1UL + (lo - 1UL - hi) / (0UL - step);
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1033 else
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1034 return 0UL;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1035 }
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1036
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1037 /* 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
1038 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
1039
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1040 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
1041 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
1042 {
44061
0cc552ab76bd Added all PyTypeObjects to the appropriate header files.
Christian Heimes <christian@cheimes.de>
parents: 44043
diff changeset
1043 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
1044 unsigned long ulen;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1045 if (it == NULL)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1046 return NULL;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1047 it->start = start;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1048 it->step = step;
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1049 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
1050 if (ulen > (unsigned long)LONG_MAX) {
57287
f1112c2700b1 Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57268
diff changeset
1051 Py_DECREF(it);
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1052 PyErr_SetString(PyExc_OverflowError,
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1053 "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
1054 return NULL;
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1055 }
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1056 it->len = (long)ulen;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1057 it->index = 0;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1058 return (PyObject *)it;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1059 }
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1060
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1061 static PyObject *
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1062 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
1063 {
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1064 long start, stop, step;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1065
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1066 if (!_PyArg_NoKeywords("rangeiter()", kw))
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1067 return NULL;
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 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
1070 &start, &stop, &step))
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1071 return NULL;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1072
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
1073 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
1074 }
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
1075
78216263425d Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents: 66335
diff changeset
1076 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
1077 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
1078 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
1079 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
1080 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
1081 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
1082 } 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
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 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
1085 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
1086 {
78216263425d Issue 2690: Add support for 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 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
1088 }
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1089
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1090 static PyMethodDef longrangeiter_methods[] = {
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1091 {"__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
1092 length_hint_doc},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1093 {NULL, NULL} /* sentinel */
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1094 };
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1095
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1096 static void
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1097 longrangeiter_dealloc(longrangeiterobject *r)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1098 {
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1099 Py_XDECREF(r->index);
41644
9cc4c3f0b580 Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
1100 Py_XDECREF(r->start);
9cc4c3f0b580 Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
1101 Py_XDECREF(r->step);
9cc4c3f0b580 Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
1102 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
1103 PyObject_Del(r);
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1104 }
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1105
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1106 static PyObject *
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1107 longrangeiter_next(longrangeiterobject *r)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1108 {
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1109 PyObject *one, *product, *new_index, *result;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1110 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
1111 return NULL;
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 one = PyLong_FromLong(1);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1114 if (!one)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1115 return NULL;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1116
57287
f1112c2700b1 Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57268
diff changeset
1117 new_index = PyNumber_Add(r->index, one);
f1112c2700b1 Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57268
diff changeset
1118 Py_DECREF(one);
f1112c2700b1 Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57268
diff changeset
1119 if (!new_index)
f1112c2700b1 Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57268
diff changeset
1120 return NULL;
f1112c2700b1 Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57268
diff changeset
1121
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1122 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
1123 if (!product) {
57287
f1112c2700b1 Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57268
diff changeset
1124 Py_DECREF(new_index);
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1125 return NULL;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1126 }
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1127
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1128 result = PyNumber_Add(r->start, product);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1129 Py_DECREF(product);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1130 if (result) {
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1131 Py_DECREF(r->index);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1132 r->index = new_index;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1133 }
57287
f1112c2700b1 Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57268
diff changeset
1134 else {
f1112c2700b1 Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57268
diff changeset
1135 Py_DECREF(new_index);
f1112c2700b1 Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57268
diff changeset
1136 }
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1137
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1138 return result;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1139 }
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1140
44061
0cc552ab76bd Added all PyTypeObjects to the appropriate header files.
Christian Heimes <christian@cheimes.de>
parents: 44043
diff changeset
1141 PyTypeObject PyLongRangeIter_Type = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1142 PyVarObject_HEAD_INIT(&PyType_Type, 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1143 "longrange_iterator", /* tp_name */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1144 sizeof(longrangeiterobject), /* tp_basicsize */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1145 0, /* tp_itemsize */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1146 /* methods */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1147 (destructor)longrangeiter_dealloc, /* tp_dealloc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1148 0, /* tp_print */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1149 0, /* tp_getattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1150 0, /* tp_setattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1151 0, /* tp_reserved */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1152 0, /* tp_repr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1153 0, /* tp_as_number */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1154 0, /* tp_as_sequence */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1155 0, /* tp_as_mapping */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1156 0, /* tp_hash */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1157 0, /* tp_call */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1158 0, /* tp_str */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1159 PyObject_GenericGetAttr, /* tp_getattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1160 0, /* tp_setattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1161 0, /* tp_as_buffer */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1162 Py_TPFLAGS_DEFAULT, /* tp_flags */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1163 0, /* tp_doc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1164 0, /* tp_traverse */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1165 0, /* tp_clear */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1166 0, /* tp_richcompare */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1167 0, /* tp_weaklistoffset */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1168 PyObject_SelfIter, /* tp_iter */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1169 (iternextfunc)longrangeiter_next, /* tp_iternext */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1170 longrangeiter_methods, /* tp_methods */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57287
diff changeset
1171 0,
23693
8983158e9f11 SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents: 23664
diff changeset
1172 };
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
1173
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36501
diff changeset
1174 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
1175 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
1176 {
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1177 rangeobject *r = (rangeobject *)seq;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1178 longrangeiterobject *it;
44366
83ced4177eac Drop _PyLong_FitsInLong. Fixes #1666.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
1179 long lstart, lstop, lstep;
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1180 PyObject *int_it;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1181
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1182 assert(PyRange_Check(seq));
44366
83ced4177eac Drop _PyLong_FitsInLong. Fixes #1666.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
1183
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1184 /* 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
1185 * version */
44366
83ced4177eac Drop _PyLong_FitsInLong. Fixes #1666.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
1186 lstart = PyLong_AsLong(r->start);
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1187 if (lstart == -1 && PyErr_Occurred()) {
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1188 PyErr_Clear();
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1189 goto long_range;
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1190 }
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1191 lstop = PyLong_AsLong(r->stop);
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1192 if (lstop == -1 && PyErr_Occurred()) {
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1193 PyErr_Clear();
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1194 goto long_range;
44366
83ced4177eac Drop _PyLong_FitsInLong. Fixes #1666.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
1195 }
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1196 lstep = PyLong_AsLong(r->step);
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1197 if (lstep == -1 && PyErr_Occurred()) {
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1198 PyErr_Clear();
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1199 goto long_range;
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1200 }
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
1201 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
1202 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
1203 PyErr_Clear();
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1204 goto long_range;
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1205 }
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1206 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
1207
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1208 long_range:
44061
0cc552ab76bd Added all PyTypeObjects to the appropriate header files.
Christian Heimes <christian@cheimes.de>
parents: 44043
diff changeset
1209 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
1210 if (it == NULL)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1211 return NULL;
41644
9cc4c3f0b580 Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
1212
9cc4c3f0b580 Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
1213 /* 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
1214 it->start = r->start;
41644
9cc4c3f0b580 Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
1215 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
1216 it->len = r->length;
41644
9cc4c3f0b580 Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
1217 Py_INCREF(it->start);
9cc4c3f0b580 Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
1218 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
1219 Py_INCREF(it->len);
41644
9cc4c3f0b580 Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
1220
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1221 it->index = PyLong_FromLong(0);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1222 if (!it->index)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1223 goto create_failure;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1224
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1225 return (PyObject *)it;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1226
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1227 create_failure:
41644
9cc4c3f0b580 Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41642
diff changeset
1228 Py_DECREF(it);
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1229 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
1230 }
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36501
diff changeset
1231
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36501
diff changeset
1232 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
1233 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
1234 {
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1235 rangeobject *range = (rangeobject*) seq;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1236 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
1237 PyObject *one, *sum, *diff, *product;
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1238 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
1239 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
1240
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1241 assert(PyRange_Check(seq));
44366
83ced4177eac Drop _PyLong_FitsInLong. Fixes #1666.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
1242
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1243 /* 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
1244 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
1245 integers in the range.
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1246
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1247 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
1248 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
1249 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
1250 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
1251 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
1252
44366
83ced4177eac Drop _PyLong_FitsInLong. Fixes #1666.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
1253 lstart = PyLong_AsLong(range->start);
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1254 if (lstart == -1 && PyErr_Occurred()) {
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1255 PyErr_Clear();
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1256 goto long_range;
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1257 }
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1258 lstop = PyLong_AsLong(range->stop);
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1259 if (lstop == -1 && PyErr_Occurred()) {
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1260 PyErr_Clear();
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1261 goto long_range;
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1262 }
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1263 lstep = PyLong_AsLong(range->step);
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1264 if (lstep == -1 && PyErr_Occurred()) {
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1265 PyErr_Clear();
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1266 goto long_range;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1267 }
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1268 /* check for possible overflow of -lstep */
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1269 if (lstep == LONG_MIN)
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1270 goto long_range;
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1271
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1272 /* check for overflow of lstart - lstep:
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1273
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1274 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
1275 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
1276
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1277 Rearrange these inequalities as:
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1278
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1279 lstart - LONG_MIN < lstep (lstep > 0)
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1280 LONG_MAX - lstart < -lstep (lstep < 0)
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 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
1283 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
1284
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1285 if (lstep > 0) {
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1286 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
1287 goto long_range;
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1288 }
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1289 else {
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1290 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
1291 goto long_range;
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1292 }
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1293
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1294 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
1295 if (ulen > (unsigned long)LONG_MAX)
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1296 goto long_range;
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1297
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1298 new_stop = lstart - lstep;
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1299 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
1300 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
1301
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1302 long_range:
44061
0cc552ab76bd Added all PyTypeObjects to the appropriate header files.
Christian Heimes <christian@cheimes.de>
parents: 44043
diff changeset
1303 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
1304 if (it == NULL)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1305 return NULL;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1306
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1307 /* 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
1308 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
1309 Py_INCREF(it->len);
57287
f1112c2700b1 Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57268
diff changeset
1310
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1311 one = PyLong_FromLong(1);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1312 if (!one)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1313 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
1314
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
1315 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
1316 Py_DECREF(one);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1317 if (!diff)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1318 goto create_failure;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1319
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1320 product = PyNumber_Multiply(diff, range->step);
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1321 Py_DECREF(diff);
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1322 if (!product)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1323 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
1324
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1325 sum = PyNumber_Add(range->start, product);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1326 Py_DECREF(product);
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1327 it->start = sum;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1328 if (!it->start)
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1329 goto create_failure;
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1330
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1331 it->step = PyNumber_Negative(range->step);
57287
f1112c2700b1 Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57268
diff changeset
1332 if (!it->step)
57263
b2a390c5de5f Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 55797
diff changeset
1333 goto create_failure;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1334
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1335 it->index = PyLong_FromLong(0);
57287
f1112c2700b1 Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57268
diff changeset
1336 if (!it->index)
f1112c2700b1 Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57268
diff changeset
1337 goto create_failure;
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1338
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1339 return (PyObject *)it;
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1340
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1341 create_failure:
57287
f1112c2700b1 Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57268
diff changeset
1342 Py_DECREF(it);
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1343 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
1344 }