Mercurial > cpython-lru_cache-weakref
annotate Objects/rangeobject.c @ 84007:dbfbdf2b5c19 3.3
Fix #17967: For ftp urls CWD to target instead of hopping to each directory
towards target. This fixes a bug where target is accessible, but parent
directories are restricted.
| author | Senthil Kumaran <senthil@uthcode.com> |
|---|---|
| date | Sat, 01 Jun 2013 08:27:06 -0700 |
| parents | 2b656a2cf7ef |
| children | 4d62a62ba44d e9af9b1ca67e |
| rev | line source |
|---|---|
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 /* Range object implementation */ |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
3 #include "Python.h" |
|
73378
4643be424293
add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents:
73072
diff
changeset
|
4 #include "structmember.h" |
|
18436
9653272f036f
Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents:
18391
diff
changeset
|
5 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
6 /* Support objects whose length is > PY_SSIZE_T_MAX. |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
7 |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
8 This could be sped up for small PyLongs if they fit in an Py_ssize_t. |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
9 This only matters on Win64. Though we could use PY_LONG_LONG which |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
10 would presumably help perf. |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
11 */ |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
12 |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 typedef struct { |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
14 PyObject_HEAD |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
15 PyObject *start; |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
16 PyObject *stop; |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
17 PyObject *step; |
|
66641
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66335
diff
changeset
|
18 PyObject *length; |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 } rangeobject; |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
21 /* Helper function for validating step. Always returns a new reference or |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
22 NULL on error. |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
23 */ |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
24 static PyObject * |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
25 validate_step(PyObject *step) |
|
23695
6266f1bd1ab9
Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents:
23693
diff
changeset
|
26 { |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
27 /* No step specified, use a step of 1. */ |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
28 if (!step) |
|
44099
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
44061
diff
changeset
|
29 return PyLong_FromLong(1); |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
30 |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
31 step = PyNumber_Index(step); |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
32 if (step) { |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
33 Py_ssize_t istep = PyNumber_AsSsize_t(step, NULL); |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
34 if (istep == -1 && PyErr_Occurred()) { |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
35 /* Ignore OverflowError, we know the value isn't 0. */ |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
36 PyErr_Clear(); |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
37 } |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
38 else if (istep == 0) { |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
39 PyErr_SetString(PyExc_ValueError, |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
40 "range() arg 3 must not be zero"); |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
41 Py_CLEAR(step); |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
42 } |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
43 } |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
44 |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
45 return step; |
|
23695
6266f1bd1ab9
Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents:
23693
diff
changeset
|
46 } |
|
6266f1bd1ab9
Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents:
23693
diff
changeset
|
47 |
|
66641
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66335
diff
changeset
|
48 static PyObject * |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66335
diff
changeset
|
49 compute_range_length(PyObject *start, PyObject *stop, PyObject *step); |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66335
diff
changeset
|
50 |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66335
diff
changeset
|
51 static rangeobject * |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66335
diff
changeset
|
52 make_range_object(PyTypeObject *type, PyObject *start, |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66335
diff
changeset
|
53 PyObject *stop, PyObject *step) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66335
diff
changeset
|
54 { |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66335
diff
changeset
|
55 rangeobject *obj = NULL; |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66335
diff
changeset
|
56 PyObject *length; |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66335
diff
changeset
|
57 length = compute_range_length(start, stop, step); |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66335
diff
changeset
|
58 if (length == NULL) { |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66335
diff
changeset
|
59 return NULL; |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66335
diff
changeset
|
60 } |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66335
diff
changeset
|
61 obj = PyObject_New(rangeobject, type); |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66335
diff
changeset
|
62 if (obj == NULL) { |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66335
diff
changeset
|
63 Py_DECREF(length); |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66335
diff
changeset
|
64 return NULL; |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66335
diff
changeset
|
65 } |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66335
diff
changeset
|
66 obj->start = start; |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66335
diff
changeset
|
67 obj->stop = stop; |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66335
diff
changeset
|
68 obj->step = step; |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66335
diff
changeset
|
69 obj->length = length; |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66335
diff
changeset
|
70 return obj; |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66335
diff
changeset
|
71 } |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66335
diff
changeset
|
72 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
73 /* XXX(nnorwitz): should we error check if the user passes any empty ranges? |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
74 range(-10) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
75 range(0, -5) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
76 range(0, 5, -1) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
77 */ |
|
23695
6266f1bd1ab9
Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents:
23693
diff
changeset
|
78 static PyObject * |
|
6266f1bd1ab9
Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents:
23693
diff
changeset
|
79 range_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
|
6266f1bd1ab9
Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents:
23693
diff
changeset
|
80 { |
|
66641
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66335
diff
changeset
|
81 rangeobject *obj; |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
82 PyObject *start = NULL, *stop = NULL, *step = NULL; |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
83 |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
84 if (!_PyArg_NoKeywords("range()", kw)) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
85 return NULL; |
|
23695
6266f1bd1ab9
Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents:
23693
diff
changeset
|
86 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
87 if (PyTuple_Size(args) <= 1) { |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
88 if (!PyArg_UnpackTuple(args, "range", 1, 1, &stop)) |
|
54726
75c4d3bc6cc8
Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents:
51271
diff
changeset
|
89 return NULL; |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
90 stop = PyNumber_Index(stop); |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
91 if (!stop) |
|
54726
75c4d3bc6cc8
Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents:
51271
diff
changeset
|
92 return NULL; |
|
44099
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
44061
diff
changeset
|
93 start = PyLong_FromLong(0); |
|
54726
75c4d3bc6cc8
Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents:
51271
diff
changeset
|
94 if (!start) { |
|
75c4d3bc6cc8
Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents:
51271
diff
changeset
|
95 Py_DECREF(stop); |
|
75c4d3bc6cc8
Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents:
51271
diff
changeset
|
96 return NULL; |
|
75c4d3bc6cc8
Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents:
51271
diff
changeset
|
97 } |
|
44099
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
44061
diff
changeset
|
98 step = PyLong_FromLong(1); |
|
54726
75c4d3bc6cc8
Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents:
51271
diff
changeset
|
99 if (!step) { |
|
75c4d3bc6cc8
Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents:
51271
diff
changeset
|
100 Py_DECREF(stop); |
|
75c4d3bc6cc8
Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents:
51271
diff
changeset
|
101 Py_DECREF(start); |
|
75c4d3bc6cc8
Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents:
51271
diff
changeset
|
102 return NULL; |
|
75c4d3bc6cc8
Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents:
51271
diff
changeset
|
103 } |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
104 } |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
105 else { |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
106 if (!PyArg_UnpackTuple(args, "range", 2, 3, |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
107 &start, &stop, &step)) |
|
54726
75c4d3bc6cc8
Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents:
51271
diff
changeset
|
108 return NULL; |
|
35449
1fdfd0130e28
Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents:
34087
diff
changeset
|
109 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
110 /* Convert borrowed refs to owned refs */ |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
111 start = PyNumber_Index(start); |
|
54726
75c4d3bc6cc8
Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents:
51271
diff
changeset
|
112 if (!start) |
|
75c4d3bc6cc8
Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents:
51271
diff
changeset
|
113 return NULL; |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
114 stop = PyNumber_Index(stop); |
|
54726
75c4d3bc6cc8
Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents:
51271
diff
changeset
|
115 if (!stop) { |
|
75c4d3bc6cc8
Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents:
51271
diff
changeset
|
116 Py_DECREF(start); |
|
75c4d3bc6cc8
Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents:
51271
diff
changeset
|
117 return NULL; |
|
75c4d3bc6cc8
Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents:
51271
diff
changeset
|
118 } |
|
75c4d3bc6cc8
Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents:
51271
diff
changeset
|
119 step = validate_step(step); /* Caution, this can clear exceptions */ |
|
75c4d3bc6cc8
Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents:
51271
diff
changeset
|
120 if (!step) { |
|
75c4d3bc6cc8
Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents:
51271
diff
changeset
|
121 Py_DECREF(start); |
|
75c4d3bc6cc8
Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents:
51271
diff
changeset
|
122 Py_DECREF(stop); |
|
75c4d3bc6cc8
Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents:
51271
diff
changeset
|
123 return NULL; |
|
75c4d3bc6cc8
Backport 73392: Fix SystemError and ref counting issues.
Raymond Hettinger <python@rcn.com>
parents:
51271
diff
changeset
|
124 } |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
125 } |
|
33047
c81ed079576f
Bug 1003935: xrange overflows
Tim Peters <tim.peters@gmail.com>
parents:
33046
diff
changeset
|
126 |
|
66641
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66335
diff
changeset
|
127 obj = make_range_object(type, start, stop, step); |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66335
diff
changeset
|
128 if (obj != NULL) |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66335
diff
changeset
|
129 return (PyObject *) obj; |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
130 |
|
66641
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66335
diff
changeset
|
131 /* Failed to create object, release attributes */ |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
132 Py_XDECREF(start); |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
133 Py_XDECREF(stop); |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
134 Py_XDECREF(step); |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
135 return NULL; |
|
23695
6266f1bd1ab9
Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents:
23693
diff
changeset
|
136 } |
|
6266f1bd1ab9
Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents:
23693
diff
changeset
|
137 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23702
diff
changeset
|
138 PyDoc_STRVAR(range_doc, |
|
79587
e4598364ea29
Issue #14783: Improve int() docstring and also str(), range(), and slice().
Chris Jerdonek <chris.jerdonek@gmail.com>
parents:
76990
diff
changeset
|
139 "range(stop) -> range object\n\ |
|
e4598364ea29
Issue #14783: Improve int() docstring and also str(), range(), and slice().
Chris Jerdonek <chris.jerdonek@gmail.com>
parents:
76990
diff
changeset
|
140 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
|
141 \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
|
142 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
|
143 |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
144 static void |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
145 range_dealloc(rangeobject *r) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
146 { |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
147 Py_DECREF(r->start); |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
148 Py_DECREF(r->stop); |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
149 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
|
150 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
|
151 PyObject_Del(r); |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
152 } |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
153 |
|
55589
546aa134d773
Merged revisions 73656,73658,73663,73666 via svnmerge from
Georg Brandl <georg@python.org>
parents:
54847
diff
changeset
|
154 /* 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
|
155 * 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
|
156 * 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
|
157 */ |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
158 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
|
159 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
|
160 { |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
161 /* ------------------------------------------------------------- |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
162 Algorithm is equal to that of get_len_of_range(), but it operates |
| 54846 | 163 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
|
164 ---------------------------------------------------------------*/ |
|
51244
e9675d72e3e3
Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
47536
diff
changeset
|
165 int cmp_result; |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
166 PyObject *lo, *hi; |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
167 PyObject *diff = NULL; |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
168 PyObject *one = NULL; |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
169 PyObject *tmp1 = NULL, *tmp2 = NULL, *result; |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
170 /* holds sub-expression evaluations */ |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
171 |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
172 PyObject *zero = PyLong_FromLong(0); |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
173 if (zero == NULL) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
174 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
|
175 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
|
176 Py_DECREF(zero); |
|
51244
e9675d72e3e3
Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
47536
diff
changeset
|
177 if (cmp_result == -1) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
178 return NULL; |
|
23695
6266f1bd1ab9
Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents:
23693
diff
changeset
|
179 |
|
51244
e9675d72e3e3
Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
47536
diff
changeset
|
180 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
|
181 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
|
182 hi = stop; |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
183 Py_INCREF(step); |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
184 } 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
|
185 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
|
186 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
|
187 step = PyNumber_Negative(step); |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
188 if (!step) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
189 return NULL; |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
190 } |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
191 |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
192 /* if (lo >= hi), return length of 0. */ |
|
51244
e9675d72e3e3
Merged revisions 69181 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
47536
diff
changeset
|
193 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
|
194 Py_XDECREF(step); |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
195 return PyLong_FromLong(0); |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
196 } |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
197 |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
198 if ((one = PyLong_FromLong(1L)) == NULL) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
199 goto Fail; |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
200 |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
201 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
|
202 goto Fail; |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
203 |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
204 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
|
205 goto Fail; |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
206 |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
207 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
|
208 goto Fail; |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
209 |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
210 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
|
211 goto Fail; |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
212 |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
213 Py_DECREF(tmp2); |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
214 Py_DECREF(diff); |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
215 Py_DECREF(step); |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
216 Py_DECREF(tmp1); |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
217 Py_DECREF(one); |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
218 return result; |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
219 |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
220 Fail: |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
221 Py_XDECREF(tmp2); |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
222 Py_XDECREF(diff); |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
223 Py_XDECREF(step); |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
224 Py_XDECREF(tmp1); |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
225 Py_XDECREF(one); |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
226 return NULL; |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
227 } |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
228 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36484
diff
changeset
|
229 static Py_ssize_t |
|
13458
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
230 range_length(rangeobject *r) |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
231 { |
|
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
|
232 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
|
233 } |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
234 |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
235 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
|
236 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
|
237 { |
|
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
|
238 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
|
239 /* 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
|
240 * 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
|
241 */ |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66664
diff
changeset
|
242 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
|
243 if (!incr) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
244 return NULL; |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
245 result = PyNumber_Add(r->start, incr); |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
246 Py_DECREF(incr); |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
247 return result; |
|
1444
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
248 } |
|
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
249 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
250 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
|
251 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
|
252 { |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
|
254 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
|
255 |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
|
257 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
|
258 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
|
259 |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 /* 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
|
261 * 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
|
262 * 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
|
263 * } 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
|
264 * 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
|
265 * } |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66664
diff
changeset
|
266 */ |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
|
268 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
|
269 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
|
270 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
|
271 } |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 (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
|
273 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
|
274 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
|
275 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
|
276 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
|
277 } |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 } 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
|
279 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
|
280 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
|
281 } |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66664
diff
changeset
|
282 |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 /* 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
|
284 * 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
|
285 * <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
|
286 * } |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66664
diff
changeset
|
287 */ |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
|
289 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
|
290 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
|
291 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
|
292 } |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
|
294 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
|
295 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
|
296 } |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
|
298 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
|
299 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
|
300 "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
|
301 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
|
302 } |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66664
diff
changeset
|
303 |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
|
305 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
|
306 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
|
307 } |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66664
diff
changeset
|
308 |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66664
diff
changeset
|
309 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
|
310 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
|
311 { |
|
76990
888f5f3bfcb6
Issue #14829: Fix bisect and range() indexing with large indices (>= 2 ** 32) under 64-bit Windows.
Antoine Pitrou <solipsis@pitrou.net>
parents:
69359
diff
changeset
|
312 PyObject *res, *arg = PyLong_FromSsize_t(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
|
313 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
|
314 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
|
315 } |
|
67575
10d67db67942
plug reference leak
Benjamin Peterson <benjamin@python.org>
parents:
67543
diff
changeset
|
316 res = compute_range_item(r, arg); |
|
10d67db67942
plug reference leak
Benjamin Peterson <benjamin@python.org>
parents:
67543
diff
changeset
|
317 Py_DECREF(arg); |
|
10d67db67942
plug reference leak
Benjamin Peterson <benjamin@python.org>
parents:
67543
diff
changeset
|
318 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
|
319 } |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66664
diff
changeset
|
320 |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 /* 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
|
322 * 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
|
323 */ |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66664
diff
changeset
|
324 |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 /* 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
|
326 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
|
327 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
|
328 { |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
|
330 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
|
331 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
|
332 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
|
333 } |
|
80236
94d15358ad6e
Issue #16402: In range slicing, fix shadowing of exceptions from __index__ method.
Mark Dickinson <mdickinson@enthought.com>
parents:
79587
diff
changeset
|
334 else { |
|
94d15358ad6e
Issue #16402: In range slicing, fix shadowing of exceptions from __index__ method.
Mark Dickinson <mdickinson@enthought.com>
parents:
79587
diff
changeset
|
335 PyErr_SetString(PyExc_TypeError, |
|
94d15358ad6e
Issue #16402: In range slicing, fix shadowing of exceptions from __index__ method.
Mark Dickinson <mdickinson@enthought.com>
parents:
79587
diff
changeset
|
336 "slice indices must be integers or " |
|
94d15358ad6e
Issue #16402: In range slicing, fix shadowing of exceptions from __index__ method.
Mark Dickinson <mdickinson@enthought.com>
parents:
79587
diff
changeset
|
337 "None or have an __index__ method"); |
|
94d15358ad6e
Issue #16402: In range slicing, fix shadowing of exceptions from __index__ method.
Mark Dickinson <mdickinson@enthought.com>
parents:
79587
diff
changeset
|
338 } |
|
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
|
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 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
|
341 } |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66664
diff
changeset
|
342 |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 /* 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
|
344 * 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
|
345 * (-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
|
346 */ |
|
67642
faec1b5a96db
rangeobject.c (compute_slice_indices): Make function static.
Matthias Klose <doko@ubuntu.com>
parents:
67575
diff
changeset
|
347 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
|
348 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
|
349 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
|
350 { |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
|
352 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
|
353 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
|
354 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
|
355 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
|
356 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
|
357 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 (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 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
|
360 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
|
361 |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 /* 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
|
363 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
|
364 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
|
365 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
|
366 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
|
367 } 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
|
368 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
|
369 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
|
370 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
|
371 "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
|
372 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
|
373 } |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
|
375 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
|
376 } |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66664
diff
changeset
|
377 |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 /* 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
|
379 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
|
380 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
|
381 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
|
382 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
|
383 } 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
|
384 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
|
385 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
|
386 } |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 } 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
|
388 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
|
389 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
|
390 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
|
391 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
|
392 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
|
393 /* 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
|
394 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
|
395 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
|
396 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
|
397 } 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
|
398 /* 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
|
399 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
|
400 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
|
401 } |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
|
403 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
|
404 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
|
405 /* 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
|
406 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
|
407 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
|
408 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
|
409 } 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
|
410 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
|
411 } |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
|
413 } 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
|
414 /* 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
|
415 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
|
416 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
|
417 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
|
418 /* 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
|
419 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
|
420 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
|
421 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
|
422 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
|
423 } 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
|
424 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
|
425 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
|
426 } |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66664
diff
changeset
|
427 } |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66664
diff
changeset
|
428 } |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66664
diff
changeset
|
429 } |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66664
diff
changeset
|
430 |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 /* 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
|
432 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
|
433 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
|
434 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
|
435 } 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
|
436 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
|
437 } |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
|
439 } 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
|
440 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
|
441 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
|
442 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
|
443 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
|
444 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
|
445 /* 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
|
446 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
|
447 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
|
448 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
|
449 } 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
|
450 /* 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
|
451 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
|
452 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
|
453 } |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
|
455 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
|
456 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
|
457 /* 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
|
458 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
|
459 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
|
460 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
|
461 } 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
|
462 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
|
463 } |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
|
465 } 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
|
466 /* 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
|
467 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
|
468 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
|
469 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
|
470 /* 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
|
471 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
|
472 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
|
473 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
|
474 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
|
475 } 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
|
476 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
|
477 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
|
478 } |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66664
diff
changeset
|
479 } |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66664
diff
changeset
|
480 } |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66664
diff
changeset
|
481 } |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66664
diff
changeset
|
482 |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 /* 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
|
484 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
|
485 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
|
486 } 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
|
487 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
|
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 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
|
490 |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 *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
|
492 *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
|
493 *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
|
494 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
|
495 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
|
496 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
|
497 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
|
498 |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
|
500 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
|
501 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
|
502 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
|
503 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
|
504 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
|
505 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
|
506 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
|
507 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
|
508 } |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, since, as far as I know, we never said anywhere that this *didn't* work)
Nick Coghlan <ncoghlan@gmail.com>
parents:
66664
diff
changeset
|
509 |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
|
511 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
|
512 { |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
|
514 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
|
515 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
|
516 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
|
517 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
|
518 |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
|
520 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
|
521 |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
|
523 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
|
524 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
|
525 |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
|
527 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
|
528 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
|
529 |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 (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
|
531 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
|
532 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
|
533 } 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
|
534 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
|
535 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
|
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 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
|
538 |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
|
540 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
|
541 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
|
542 } |
|
fef521f45af3
Issue 10889: Support slicing and indexing of large ranges (no docs changes, 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 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
|
544 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
|
545 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
|
546 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
|
547 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
|
548 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
|
549 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
|
550 return NULL; |
|
47536
33492c241dab
Issue 2582: Fix pickling of range objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46564
diff
changeset
|
551 } |
|
33492c241dab
Issue 2582: Fix pickling of range objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46564
diff
changeset
|
552 |
|
64725
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
553 /* 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
|
554 static int |
|
66335
acd962824819
pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents:
66331
diff
changeset
|
555 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
|
556 { |
|
64725
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
557 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
|
558 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
|
559 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
|
560 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
|
561 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
|
562 |
|
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
563 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
|
564 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
|
565 goto end; |
|
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 /* 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
|
568 |
|
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
569 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
|
570 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
|
571 goto end; |
|
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
572 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
|
573 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
|
574 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
|
575 } |
|
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
576 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
|
577 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
|
578 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
|
579 } |
|
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
580 |
|
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 == -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
|
582 goto end; |
|
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
583 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
|
584 result = 0; |
|
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
585 goto end; |
|
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
586 } |
|
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
587 |
|
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
588 /* 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
|
589 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
|
590 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
|
591 goto end; |
|
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
592 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
|
593 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
|
594 goto end; |
|
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
595 /* 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
|
596 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
|
597 end: |
|
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(tmp1); |
|
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
599 Py_XDECREF(tmp2); |
|
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
600 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
|
601 return result; |
|
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
602 } |
|
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
603 |
|
56192
d599a3f2e72d
Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
604 static int |
|
66335
acd962824819
pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents:
66331
diff
changeset
|
605 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
|
606 { |
|
64725
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
607 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
|
608 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
|
609 |
|
d599a3f2e72d
Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
610 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
|
611 PY_ITERSEARCH_CONTAINS); |
|
d599a3f2e72d
Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
612 } |
|
d599a3f2e72d
Issue #1766304: Optimize membership testing for ranges: 'n in range(...)'
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
613 |
|
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
|
614 /* 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
|
615 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
|
616 |
|
479a7dd1ea6a
Issue #13201: equality for range objects 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 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
|
618 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
|
619 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
|
620 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
|
621 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
|
622 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
|
623 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
|
624 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
|
625 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
|
626 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
|
627 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
|
628 */ |
|
479a7dd1ea6a
Issue #13201: equality for range objects 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 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
|
630 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
|
631 { |
|
479a7dd1ea6a
Issue #13201: equality for range objects 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 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
|
633 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
|
634 |
|
479a7dd1ea6a
Issue #13201: equality for range objects 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 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
|
636 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
|
637 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
|
638 /* 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
|
639 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
|
640 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
|
641 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
|
642 /* 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
|
643 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
|
644 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
|
645 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
|
646 /* 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
|
647 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
|
648 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
|
649 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
|
650 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
|
651 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
|
652 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
|
653 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
|
654 /* 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
|
655 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
|
656 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
|
657 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
|
658 } |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
659 |
|
479a7dd1ea6a
Issue #13201: equality for range objects 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 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
|
661 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
|
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 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
|
664 |
|
479a7dd1ea6a
Issue #13201: equality for range objects 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 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
|
666 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
|
667 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
|
668 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
|
669 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
|
670 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
|
671 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
|
672 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
|
673 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
|
674 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
|
675 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
|
676 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
|
677 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
|
678 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
|
679 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
|
680 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
|
681 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
|
682 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
|
683 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
|
684 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
|
685 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
|
686 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
|
687 } |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
688 } |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
689 |
|
479a7dd1ea6a
Issue #13201: equality for range objects 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 /* 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
|
691 |
|
479a7dd1ea6a
Issue #13201: equality for range objects 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 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
|
693 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
|
694 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
|
695 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
|
696 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
|
697 */ |
|
479a7dd1ea6a
Issue #13201: equality for range objects 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 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
|
699 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
|
700 { |
|
479a7dd1ea6a
Issue #13201: equality for range objects 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 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
|
702 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
|
703 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
|
704 |
|
479a7dd1ea6a
Issue #13201: equality for range objects 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 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
|
706 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
|
707 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
|
708 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
|
709 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
|
710 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
|
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 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
|
713 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
|
714 Py_INCREF(Py_None); |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
715 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
|
716 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
|
717 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
|
718 } |
|
479a7dd1ea6a
Issue #13201: equality for range objects 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 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
|
720 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
|
721 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
|
722 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
|
723 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
|
724 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
|
725 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
|
726 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
|
727 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
|
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 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
|
730 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
|
731 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
|
732 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
|
733 } |
|
479a7dd1ea6a
Issue #13201: equality for range objects 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 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
|
735 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
|
736 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
|
737 } |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
738 } |
|
479a7dd1ea6a
Issue #13201: equality for range objects 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 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
|
740 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
|
741 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
|
742 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
|
743 } |
|
479a7dd1ea6a
Issue #13201: equality for range objects is now based on equality of the underlying sequences. Thanks Sven Marnach for the patch.
Mark Dickinson <mdickinson@enthought.com>
parents:
69359
diff
changeset
|
744 |
|
64725
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
745 static PyObject * |
|
66335
acd962824819
pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents:
66331
diff
changeset
|
746 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
|
747 { |
|
64725
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
748 if (PyLong_CheckExact(ob) || PyBool_Check(ob)) { |
|
66330
add7e9721f80
Add error handling in range_count.
Georg Brandl <georg@python.org>
parents:
66329
diff
changeset
|
749 int result = range_contains_long(r, ob); |
|
add7e9721f80
Add error handling in range_count.
Georg Brandl <georg@python.org>
parents:
66329
diff
changeset
|
750 if (result == -1) |
|
add7e9721f80
Add error handling in range_count.
Georg Brandl <georg@python.org>
parents:
66329
diff
changeset
|
751 return NULL; |
|
add7e9721f80
Add error handling in range_count.
Georg Brandl <georg@python.org>
parents:
66329
diff
changeset
|
752 else if (result) |
|
66329
238caaf6852b
count() should return integers #10474
Benjamin Peterson <benjamin@python.org>
parents:
64725
diff
changeset
|
753 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
|
754 else |
|
66329
238caaf6852b
count() should return integers #10474
Benjamin Peterson <benjamin@python.org>
parents:
64725
diff
changeset
|
755 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
|
756 } else { |
|
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
757 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
|
758 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
|
759 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
|
760 return NULL; |
|
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
761 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
|
762 } |
|
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
763 } |
|
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
764 |
|
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
765 static PyObject * |
|
66335
acd962824819
pep 7 actually wants the brace on a new line
Benjamin Peterson <benjamin@python.org>
parents:
66331
diff
changeset
|
766 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
|
767 { |
|
64725
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
768 int contains; |
|
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
769 |
|
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
770 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
|
771 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
|
772 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
|
773 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
|
774 return NULL; |
|
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
775 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
|
776 } |
|
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
777 |
|
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
778 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
|
779 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
|
780 return NULL; |
|
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
781 |
|
66331
e01237831ad2
code style and simplification
Benjamin Peterson <benjamin@python.org>
parents:
66330
diff
changeset
|
782 if (contains) { |
|
e01237831ad2
code style and simplification
Benjamin Peterson <benjamin@python.org>
parents:
66330
diff
changeset
|
783 PyObject *idx, *tmp = PyNumber_Subtract(ob, r->start); |
|
e01237831ad2
code style and simplification
Benjamin Peterson <benjamin@python.org>
parents:
66330
diff
changeset
|
784 if (tmp == NULL) |
|
e01237831ad2
code style and simplification
Benjamin Peterson <benjamin@python.org>
parents:
66330
diff
changeset
|
785 return NULL; |
|
e01237831ad2
code style and simplification
Benjamin Peterson <benjamin@python.org>
parents:
66330
diff
changeset
|
786 /* idx = (ob - r.start) // r.step */ |
|
e01237831ad2
code style and simplification
Benjamin Peterson <benjamin@python.org>
parents:
66330
diff
changeset
|
787 idx = PyNumber_FloorDivide(tmp, r->step); |
|
e01237831ad2
code style and simplification
Benjamin Peterson <benjamin@python.org>
parents:
66330
diff
changeset
|
788 Py_DECREF(tmp); |
|
e01237831ad2
code style and simplification
Benjamin Peterson <benjamin@python.org>
parents:
66330
diff
changeset
|
789 return idx; |
|
e01237831ad2
code style and simplification
Benjamin Peterson <benjamin@python.org>
parents:
66330
diff
changeset
|
790 } |
|
64725
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
791 |
|
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
792 /* object is not in the range */ |
|
66331
e01237831ad2
code style and simplification
Benjamin Peterson <benjamin@python.org>
parents:
66330
diff
changeset
|
793 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
|
794 return NULL; |
|
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
795 } |
|
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
796 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
797 static PySequenceMethods range_as_sequence = { |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57286
diff
changeset
|
798 (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
|
799 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
|
800 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
|
801 (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
|
802 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
|
803 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
|
804 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
|
805 (objobjproc)range_contains, /* sq_contains */ |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
806 }; |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
807 |
|
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
|
808 static PyObject * |
|
13458
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
809 range_repr(rangeobject *r) |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
810 { |
|
41800
993f8a9b8ab3
repr(range(10)) now returns 'range(0, 10)' for clarity.
Walter Dörwald <walter@livinglogic.de>
parents:
41785
diff
changeset
|
811 Py_ssize_t istep; |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
812 |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
813 /* 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
|
814 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
|
815 (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
|
816 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
|
817 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
|
818 PyErr_Clear(); |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
819 } |
|
13848
dd042f5a8fa2
Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents:
13847
diff
changeset
|
820 |
|
41800
993f8a9b8ab3
repr(range(10)) now returns 'range(0, 10)' for clarity.
Walter Dörwald <walter@livinglogic.de>
parents:
41785
diff
changeset
|
821 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
|
822 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
|
823 else |
|
d1c38af065bf
Change range_repr() to use %R for the start/stop/step attributes.
Walter Dörwald <walter@livinglogic.de>
parents:
41769
diff
changeset
|
824 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
|
825 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
|
826 } |
|
9653272f036f
Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents:
18391
diff
changeset
|
827 |
|
47536
33492c241dab
Issue 2582: Fix pickling of range objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46564
diff
changeset
|
828 /* Pickling support */ |
|
33492c241dab
Issue 2582: Fix pickling of range objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46564
diff
changeset
|
829 static PyObject * |
|
33492c241dab
Issue 2582: Fix pickling of range objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46564
diff
changeset
|
830 range_reduce(rangeobject *r, PyObject *args) |
|
33492c241dab
Issue 2582: Fix pickling of range objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46564
diff
changeset
|
831 { |
|
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
|
832 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
|
833 r->start, r->stop, r->step); |
|
33492c241dab
Issue 2582: Fix pickling of range objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46564
diff
changeset
|
834 } |
|
33492c241dab
Issue 2582: Fix pickling of range objects.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46564
diff
changeset
|
835 |
|
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
|
836 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
|
837 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
|
838 { |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66335
diff
changeset
|
839 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
|
840 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
|
841 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
|
842 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
|
843 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
|
844 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
|
845 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
|
846 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
|
847 } |
|
78216263425d
Issue 2690: Add support for 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 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
|
849 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
|
850 } |
|
78216263425d
Issue 2690: Add support for 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 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
|
852 "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
|
853 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
|
854 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
|
855 } |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66335
diff
changeset
|
856 |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66335
diff
changeset
|
857 |
|
78216263425d
Issue 2690: Add support for 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 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
|
859 (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
|
860 (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
|
861 (objobjargproc)0, /* mp_ass_subscript */ |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
862 }; |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
863 |
|
24280
1598a1e473e2
staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24260
diff
changeset
|
864 static PyObject * range_iter(PyObject *seq); |
|
30865
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
28320
diff
changeset
|
865 static PyObject * range_reverse(PyObject *seq); |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
28320
diff
changeset
|
866 |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
28320
diff
changeset
|
867 PyDoc_STRVAR(reverse_doc, |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
28320
diff
changeset
|
868 "Returns a reverse iterator."); |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
28320
diff
changeset
|
869 |
|
64725
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
870 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
|
871 "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
|
872 |
|
e2dec9c0d13c
Issue #9213: Add index and count methods to range objects, needed to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
61466
diff
changeset
|
873 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
|
874 "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
|
875 "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
|
876 |
|
30865
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
28320
diff
changeset
|
877 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
|
878 {"__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
|
879 {"__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
|
880 {"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
|
881 {"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
|
882 {NULL, NULL} /* sentinel */ |
|
30865
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
28320
diff
changeset
|
883 }; |
|
23693
8983158e9f11
SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents:
23664
diff
changeset
|
884 |
|
73378
4643be424293
add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents:
73072
diff
changeset
|
885 static PyMemberDef range_members[] = { |
|
4643be424293
add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents:
73072
diff
changeset
|
886 {"start", T_OBJECT_EX, offsetof(rangeobject, start), READONLY}, |
|
4643be424293
add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents:
73072
diff
changeset
|
887 {"stop", T_OBJECT_EX, offsetof(rangeobject, stop), READONLY}, |
|
4643be424293
add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents:
73072
diff
changeset
|
888 {"step", T_OBJECT_EX, offsetof(rangeobject, step), READONLY}, |
|
4643be424293
add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents:
73072
diff
changeset
|
889 {0} |
|
4643be424293
add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents:
73072
diff
changeset
|
890 }; |
|
4643be424293
add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents:
73072
diff
changeset
|
891 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
892 PyTypeObject PyRange_Type = { |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
893 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
894 "range", /* Name of this type */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
895 sizeof(rangeobject), /* Basic object size */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
896 0, /* Item size for varobject */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
897 (destructor)range_dealloc, /* tp_dealloc */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
898 0, /* tp_print */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
899 0, /* tp_getattr */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
900 0, /* tp_setattr */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
901 0, /* tp_reserved */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
902 (reprfunc)range_repr, /* tp_repr */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
903 0, /* tp_as_number */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
904 &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
|
905 &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
|
906 (hashfunc)range_hash, /* tp_hash */ |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
907 0, /* tp_call */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
908 0, /* tp_str */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
909 PyObject_GenericGetAttr, /* tp_getattro */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
910 0, /* tp_setattro */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
911 0, /* tp_as_buffer */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
912 Py_TPFLAGS_DEFAULT, /* tp_flags */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
913 range_doc, /* tp_doc */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
914 0, /* tp_traverse */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
915 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
|
916 range_richcompare, /* tp_richcompare */ |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
917 0, /* tp_weaklistoffset */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
918 range_iter, /* tp_iter */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
919 0, /* tp_iternext */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
920 range_methods, /* tp_methods */ |
|
73378
4643be424293
add introspection to range objects (closes #9896)
Benjamin Peterson <benjamin@python.org>
parents:
73072
diff
changeset
|
921 range_members, /* tp_members */ |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
922 0, /* tp_getset */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
923 0, /* tp_base */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
924 0, /* tp_dict */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
925 0, /* tp_descr_get */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
926 0, /* tp_descr_set */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
927 0, /* tp_dictoffset */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
928 0, /* tp_init */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
929 0, /* tp_alloc */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
930 range_new, /* tp_new */ |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
931 }; |
|
23693
8983158e9f11
SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents:
23664
diff
changeset
|
932 |
|
41803
3d3bf27409e6
Rename test_xrange.py to test_range.py and fix the
Walter Dörwald <walter@livinglogic.de>
parents:
41800
diff
changeset
|
933 /*********************** range Iterator **************************/ |
|
23693
8983158e9f11
SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents:
23664
diff
changeset
|
934 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
935 /* 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
|
936 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
|
937 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
|
938 */ |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
939 |
|
23693
8983158e9f11
SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents:
23664
diff
changeset
|
940 typedef struct { |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
941 PyObject_HEAD |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
942 long index; |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
943 long start; |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
944 long step; |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
945 long len; |
|
23693
8983158e9f11
SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents:
23664
diff
changeset
|
946 } rangeiterobject; |
|
8983158e9f11
SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents:
23664
diff
changeset
|
947 |
|
30865
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
28320
diff
changeset
|
948 static PyObject * |
|
23693
8983158e9f11
SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents:
23664
diff
changeset
|
949 rangeiter_next(rangeiterobject *r) |
|
8983158e9f11
SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents:
23664
diff
changeset
|
950 { |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
951 if (r->index < r->len) |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
952 /* 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
|
953 in intermediate calculations. */ |
|
9505afe8823f
Merged revisions 76298 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
57263
diff
changeset
|
954 return PyLong_FromLong((long)(r->start + |
|
9505afe8823f
Merged revisions 76298 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
57263
diff
changeset
|
955 (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
|
956 return NULL; |
|
23693
8983158e9f11
SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents:
23664
diff
changeset
|
957 } |
|
8983158e9f11
SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents:
23664
diff
changeset
|
958 |
|
35636
e22334b1da67
Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents:
35449
diff
changeset
|
959 static PyObject * |
|
31635
04eb2ebb46e2
Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents:
30865
diff
changeset
|
960 rangeiter_len(rangeiterobject *r) |
|
04eb2ebb46e2
Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents:
30865
diff
changeset
|
961 { |
|
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
|
962 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
|
963 } |
|
04eb2ebb46e2
Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents:
30865
diff
changeset
|
964 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
965 PyDoc_STRVAR(length_hint_doc, |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
966 "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
|
967 |
|
76091
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
968 static PyObject * |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
969 rangeiter_reduce(rangeiterobject *r) |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
970 { |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
971 PyObject *start=NULL, *stop=NULL, *step=NULL; |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
972 PyObject *range; |
|
79588
365da47a6dc1
Issue #14783: Merge changes from 3.2.
Chris Jerdonek <chris.jerdonek@gmail.com>
diff
changeset
|
973 |
|
76091
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
974 /* create a range object for pickling */ |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
975 start = PyLong_FromLong(r->start); |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
976 if (start == NULL) |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
977 goto err; |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
978 stop = PyLong_FromLong(r->start + r->len * r->step); |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
979 if (stop == NULL) |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
980 goto err; |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
981 step = PyLong_FromLong(r->step); |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
982 if (step == NULL) |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
983 goto err; |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
984 range = (PyObject*)make_range_object(&PyRange_Type, |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
985 start, stop, step); |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
986 if (range == NULL) |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
987 goto err; |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
988 /* return the result */ |
|
76105
fb907ee4fdfa
Rename _PyIter_GetBuiltin to _PyObject_GetBuiltin, and do not include it in the stable ABI.
Antoine Pitrou <solipsis@pitrou.net>
parents:
76091
diff
changeset
|
989 return Py_BuildValue("N(N)i", _PyObject_GetBuiltin("iter"), range, r->index); |
|
76091
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
990 err: |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
991 Py_XDECREF(start); |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
992 Py_XDECREF(stop); |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
993 Py_XDECREF(step); |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
994 return NULL; |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
995 } |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
996 |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
997 static PyObject * |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
998 rangeiter_setstate(rangeiterobject *r, PyObject *state) |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
999 { |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1000 long index = PyLong_AsLong(state); |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1001 if (index == -1 && PyErr_Occurred()) |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1002 return NULL; |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1003 if (index < 0 || index >= r->len) { |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1004 PyErr_SetString(PyExc_ValueError, "index out of range"); |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1005 return NULL; |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1006 } |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1007 r->index = index; |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1008 Py_RETURN_NONE; |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1009 } |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1010 |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1011 PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1012 PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1013 |
|
35636
e22334b1da67
Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents:
35449
diff
changeset
|
1014 static PyMethodDef rangeiter_methods[] = { |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1015 {"__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
|
1016 length_hint_doc}, |
|
76091
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1017 {"__reduce__", (PyCFunction)rangeiter_reduce, METH_NOARGS, |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1018 reduce_doc}, |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1019 {"__setstate__", (PyCFunction)rangeiter_setstate, METH_O, |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1020 setstate_doc}, |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1021 {NULL, NULL} /* sentinel */ |
|
31635
04eb2ebb46e2
Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents:
30865
diff
changeset
|
1022 }; |
|
04eb2ebb46e2
Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents:
30865
diff
changeset
|
1023 |
|
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
|
1024 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
|
1025 |
|
44061
0cc552ab76bd
Added all PyTypeObjects to the appropriate header files.
Christian Heimes <christian@cheimes.de>
parents:
44043
diff
changeset
|
1026 PyTypeObject PyRangeIter_Type = { |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1027 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1028 "range_iterator", /* tp_name */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1029 sizeof(rangeiterobject), /* tp_basicsize */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1030 0, /* tp_itemsize */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1031 /* methods */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1032 (destructor)PyObject_Del, /* tp_dealloc */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1033 0, /* tp_print */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1034 0, /* tp_getattr */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1035 0, /* tp_setattr */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1036 0, /* tp_reserved */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1037 0, /* tp_repr */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1038 0, /* tp_as_number */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1039 0, /* tp_as_sequence */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1040 0, /* tp_as_mapping */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1041 0, /* tp_hash */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1042 0, /* tp_call */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1043 0, /* tp_str */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1044 PyObject_GenericGetAttr, /* tp_getattro */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1045 0, /* tp_setattro */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1046 0, /* tp_as_buffer */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1047 Py_TPFLAGS_DEFAULT, /* tp_flags */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1048 0, /* tp_doc */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1049 0, /* tp_traverse */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1050 0, /* tp_clear */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1051 0, /* tp_richcompare */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1052 0, /* tp_weaklistoffset */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1053 PyObject_SelfIter, /* tp_iter */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1054 (iternextfunc)rangeiter_next, /* tp_iternext */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1055 rangeiter_methods, /* tp_methods */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1056 0, /* tp_members */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1057 0, /* tp_getset */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1058 0, /* tp_base */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1059 0, /* tp_dict */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1060 0, /* tp_descr_get */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1061 0, /* tp_descr_set */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1062 0, /* tp_dictoffset */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1063 0, /* tp_init */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1064 0, /* tp_alloc */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1065 rangeiter_new, /* tp_new */ |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1066 }; |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1067 |
|
57263
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1068 /* 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
|
1069 * 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
|
1070 */ |
|
57263
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1071 static unsigned long |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1072 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
|
1073 { |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1074 /* ------------------------------------------------------------- |
|
57263
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1075 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
|
1076 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
|
1077 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
|
1078 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
|
1079 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
|
1080 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
|
1081 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
|
1082 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
|
1083 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
|
1084 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
|
1085 is similar. |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1086 ---------------------------------------------------------------*/ |
|
57263
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1087 assert(step != 0); |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1088 if (step > 0 && lo < hi) |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1089 return 1UL + (hi - 1UL - lo) / step; |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1090 else if (step < 0 && lo > hi) |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1091 return 1UL + (lo - 1UL - hi) / (0UL - step); |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1092 else |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1093 return 0UL; |
|
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 |
|
57263
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1096 /* 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
|
1097 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
|
1098 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1099 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
|
1100 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
|
1101 { |
|
44061
0cc552ab76bd
Added all PyTypeObjects to the appropriate header files.
Christian Heimes <christian@cheimes.de>
parents:
44043
diff
changeset
|
1102 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
|
1103 unsigned long ulen; |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1104 if (it == NULL) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1105 return NULL; |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1106 it->start = start; |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1107 it->step = step; |
|
57263
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1108 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
|
1109 if (ulen > (unsigned long)LONG_MAX) { |
|
57287
f1112c2700b1
Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57268
diff
changeset
|
1110 Py_DECREF(it); |
|
57263
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1111 PyErr_SetString(PyExc_OverflowError, |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1112 "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
|
1113 return NULL; |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1114 } |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1115 it->len = (long)ulen; |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1116 it->index = 0; |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1117 return (PyObject *)it; |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1118 } |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1119 |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1120 static PyObject * |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1121 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
|
1122 { |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1123 long start, stop, step; |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1124 |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1125 if (!_PyArg_NoKeywords("rangeiter()", kw)) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1126 return NULL; |
|
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 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
|
1129 &start, &stop, &step)) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1130 return NULL; |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1131 |
|
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
|
1132 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
|
1133 } |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66335
diff
changeset
|
1134 |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66335
diff
changeset
|
1135 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
|
1136 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
|
1137 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
|
1138 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
|
1139 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
|
1140 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
|
1141 } 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
|
1142 |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66335
diff
changeset
|
1143 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
|
1144 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
|
1145 { |
|
78216263425d
Issue 2690: Add support for slicing and negative indices to range objects (includes precalculation and storage of the range length).
Nick Coghlan <ncoghlan@gmail.com>
parents:
66335
diff
changeset
|
1146 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
|
1147 } |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1148 |
|
76091
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1149 static PyObject * |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1150 longrangeiter_reduce(longrangeiterobject *r) |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1151 { |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1152 PyObject *product, *stop=NULL; |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1153 PyObject *range; |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1154 |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1155 /* create a range object for pickling. Must calculate the "stop" value */ |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1156 product = PyNumber_Multiply(r->len, r->step); |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1157 if (product == NULL) |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1158 return NULL; |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1159 stop = PyNumber_Add(r->start, product); |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1160 Py_DECREF(product); |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1161 if (stop == NULL) |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1162 return NULL; |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1163 Py_INCREF(r->start); |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1164 Py_INCREF(r->step); |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1165 range = (PyObject*)make_range_object(&PyRange_Type, |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1166 r->start, stop, r->step); |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1167 if (range == NULL) { |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1168 Py_DECREF(r->start); |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1169 Py_DECREF(stop); |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1170 Py_DECREF(r->step); |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1171 return NULL; |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1172 } |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1173 |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1174 /* return the result */ |
|
76105
fb907ee4fdfa
Rename _PyIter_GetBuiltin to _PyObject_GetBuiltin, and do not include it in the stable ABI.
Antoine Pitrou <solipsis@pitrou.net>
parents:
76091
diff
changeset
|
1175 return Py_BuildValue("N(N)O", _PyObject_GetBuiltin("iter"), range, r->index); |
|
76091
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1176 } |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1177 |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1178 static PyObject * |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1179 longrangeiter_setstate(longrangeiterobject *r, PyObject *state) |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1180 { |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1181 Py_CLEAR(r->index); |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1182 r->index = state; |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1183 Py_INCREF(r->index); |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1184 Py_RETURN_NONE; |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1185 } |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1186 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1187 static PyMethodDef longrangeiter_methods[] = { |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1188 {"__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
|
1189 length_hint_doc}, |
|
76091
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1190 {"__reduce__", (PyCFunction)longrangeiter_reduce, METH_NOARGS, |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1191 reduce_doc}, |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1192 {"__setstate__", (PyCFunction)longrangeiter_setstate, METH_O, |
|
4ff234337e24
Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
73378
diff
changeset
|
1193 setstate_doc}, |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1194 {NULL, NULL} /* sentinel */ |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1195 }; |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1196 |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1197 static void |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1198 longrangeiter_dealloc(longrangeiterobject *r) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1199 { |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1200 Py_XDECREF(r->index); |
|
41644
9cc4c3f0b580
Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41642
diff
changeset
|
1201 Py_XDECREF(r->start); |
|
9cc4c3f0b580
Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41642
diff
changeset
|
1202 Py_XDECREF(r->step); |
|
9cc4c3f0b580
Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41642
diff
changeset
|
1203 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
|
1204 PyObject_Del(r); |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1205 } |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1206 |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1207 static PyObject * |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1208 longrangeiter_next(longrangeiterobject *r) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1209 { |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1210 PyObject *one, *product, *new_index, *result; |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1211 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
|
1212 return NULL; |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1213 |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1214 one = PyLong_FromLong(1); |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1215 if (!one) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1216 return NULL; |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1217 |
|
57287
f1112c2700b1
Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57268
diff
changeset
|
1218 new_index = PyNumber_Add(r->index, one); |
|
f1112c2700b1
Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57268
diff
changeset
|
1219 Py_DECREF(one); |
|
f1112c2700b1
Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57268
diff
changeset
|
1220 if (!new_index) |
|
f1112c2700b1
Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57268
diff
changeset
|
1221 return NULL; |
|
f1112c2700b1
Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57268
diff
changeset
|
1222 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1223 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
|
1224 if (!product) { |
|
57287
f1112c2700b1
Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57268
diff
changeset
|
1225 Py_DECREF(new_index); |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1226 return NULL; |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1227 } |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1228 |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1229 result = PyNumber_Add(r->start, product); |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1230 Py_DECREF(product); |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1231 if (result) { |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1232 Py_DECREF(r->index); |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1233 r->index = new_index; |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1234 } |
|
57287
f1112c2700b1
Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57268
diff
changeset
|
1235 else { |
|
f1112c2700b1
Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57268
diff
changeset
|
1236 Py_DECREF(new_index); |
|
f1112c2700b1
Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57268
diff
changeset
|
1237 } |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1238 |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1239 return result; |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1240 } |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1241 |
|
44061
0cc552ab76bd
Added all PyTypeObjects to the appropriate header files.
Christian Heimes <christian@cheimes.de>
parents:
44043
diff
changeset
|
1242 PyTypeObject PyLongRangeIter_Type = { |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1243 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1244 "longrange_iterator", /* tp_name */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1245 sizeof(longrangeiterobject), /* tp_basicsize */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1246 0, /* tp_itemsize */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1247 /* methods */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1248 (destructor)longrangeiter_dealloc, /* tp_dealloc */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1249 0, /* tp_print */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1250 0, /* tp_getattr */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1251 0, /* tp_setattr */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1252 0, /* tp_reserved */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1253 0, /* tp_repr */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1254 0, /* tp_as_number */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1255 0, /* tp_as_sequence */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1256 0, /* tp_as_mapping */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1257 0, /* tp_hash */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1258 0, /* tp_call */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1259 0, /* tp_str */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1260 PyObject_GenericGetAttr, /* tp_getattro */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1261 0, /* tp_setattro */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1262 0, /* tp_as_buffer */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1263 Py_TPFLAGS_DEFAULT, /* tp_flags */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1264 0, /* tp_doc */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1265 0, /* tp_traverse */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1266 0, /* tp_clear */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1267 0, /* tp_richcompare */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1268 0, /* tp_weaklistoffset */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1269 PyObject_SelfIter, /* tp_iter */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1270 (iternextfunc)longrangeiter_next, /* tp_iternext */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1271 longrangeiter_methods, /* tp_methods */ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
57287
diff
changeset
|
1272 0, |
|
23693
8983158e9f11
SF 564601 adding rangeiterobject to make xrange() iterate like range().
Raymond Hettinger <python@rcn.com>
parents:
23664
diff
changeset
|
1273 }; |
|
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
|
1274 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
1275 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
|
1276 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
|
1277 { |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1278 rangeobject *r = (rangeobject *)seq; |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1279 longrangeiterobject *it; |
|
44366
83ced4177eac
Drop _PyLong_FitsInLong. Fixes #1666.
Martin v. Löwis <martin@v.loewis.de>
parents:
44099
diff
changeset
|
1280 long lstart, lstop, lstep; |
|
57263
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1281 PyObject *int_it; |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1282 |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1283 assert(PyRange_Check(seq)); |
|
44366
83ced4177eac
Drop _PyLong_FitsInLong. Fixes #1666.
Martin v. Löwis <martin@v.loewis.de>
parents:
44099
diff
changeset
|
1284 |
|
57263
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1285 /* 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
|
1286 * version */ |
|
44366
83ced4177eac
Drop _PyLong_FitsInLong. Fixes #1666.
Martin v. Löwis <martin@v.loewis.de>
parents:
44099
diff
changeset
|
1287 lstart = PyLong_AsLong(r->start); |
|
57263
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1288 if (lstart == -1 && PyErr_Occurred()) { |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1289 PyErr_Clear(); |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1290 goto long_range; |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1291 } |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1292 lstop = PyLong_AsLong(r->stop); |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1293 if (lstop == -1 && PyErr_Occurred()) { |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1294 PyErr_Clear(); |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1295 goto long_range; |
|
44366
83ced4177eac
Drop _PyLong_FitsInLong. Fixes #1666.
Martin v. Löwis <martin@v.loewis.de>
parents:
44099
diff
changeset
|
1296 } |
|
57263
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1297 lstep = PyLong_AsLong(r->step); |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1298 if (lstep == -1 && PyErr_Occurred()) { |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1299 PyErr_Clear(); |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1300 goto long_range; |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1301 } |
|
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
|
1302 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
|
1303 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
|
1304 PyErr_Clear(); |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1305 goto long_range; |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1306 } |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1307 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
|
1308 |
|
57263
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1309 long_range: |
|
44061
0cc552ab76bd
Added all PyTypeObjects to the appropriate header files.
Christian Heimes <christian@cheimes.de>
parents:
44043
diff
changeset
|
1310 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
|
1311 if (it == NULL) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1312 return NULL; |
|
41644
9cc4c3f0b580
Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41642
diff
changeset
|
1313 |
|
9cc4c3f0b580
Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41642
diff
changeset
|
1314 /* 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
|
1315 it->start = r->start; |
|
41644
9cc4c3f0b580
Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41642
diff
changeset
|
1316 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
|
1317 it->len = r->length; |
|
41644
9cc4c3f0b580
Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41642
diff
changeset
|
1318 Py_INCREF(it->start); |
|
9cc4c3f0b580
Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41642
diff
changeset
|
1319 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
|
1320 Py_INCREF(it->len); |
|
41644
9cc4c3f0b580
Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41642
diff
changeset
|
1321 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1322 it->index = PyLong_FromLong(0); |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1323 if (!it->index) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1324 goto create_failure; |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1325 |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1326 return (PyObject *)it; |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1327 |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1328 create_failure: |
|
41644
9cc4c3f0b580
Merged revisions 55180-55183 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41642
diff
changeset
|
1329 Py_DECREF(it); |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1330 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
|
1331 } |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
1332 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
1333 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
|
1334 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
|
1335 { |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1336 rangeobject *range = (rangeobject*) seq; |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1337 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
|
1338 PyObject *one, *sum, *diff, *product; |
|
57263
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1339 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
|
1340 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
|
1341 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1342 assert(PyRange_Check(seq)); |
|
44366
83ced4177eac
Drop _PyLong_FitsInLong. Fixes #1666.
Martin v. Löwis <martin@v.loewis.de>
parents:
44099
diff
changeset
|
1343 |
|
57263
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1344 /* 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
|
1345 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
|
1346 integers in the range. |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1347 |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1348 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
|
1349 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
|
1350 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
|
1351 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
|
1352 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
|
1353 |
|
44366
83ced4177eac
Drop _PyLong_FitsInLong. Fixes #1666.
Martin v. Löwis <martin@v.loewis.de>
parents:
44099
diff
changeset
|
1354 lstart = PyLong_AsLong(range->start); |
|
57263
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1355 if (lstart == -1 && PyErr_Occurred()) { |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1356 PyErr_Clear(); |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1357 goto long_range; |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1358 } |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1359 lstop = PyLong_AsLong(range->stop); |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1360 if (lstop == -1 && PyErr_Occurred()) { |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1361 PyErr_Clear(); |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1362 goto long_range; |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1363 } |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1364 lstep = PyLong_AsLong(range->step); |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1365 if (lstep == -1 && PyErr_Occurred()) { |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1366 PyErr_Clear(); |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1367 goto long_range; |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1368 } |
|
57263
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1369 /* check for possible overflow of -lstep */ |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1370 if (lstep == LONG_MIN) |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1371 goto long_range; |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1372 |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1373 /* check for overflow of lstart - lstep: |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1374 |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1375 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
|
1376 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
|
1377 |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1378 Rearrange these inequalities as: |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1379 |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1380 lstart - LONG_MIN < lstep (lstep > 0) |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1381 LONG_MAX - lstart < -lstep (lstep < 0) |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1382 |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1383 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
|
1384 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
|
1385 |
|
57263
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1386 if (lstep > 0) { |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1387 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
|
1388 goto long_range; |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1389 } |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1390 else { |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1391 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
|
1392 goto long_range; |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1393 } |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1394 |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1395 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
|
1396 if (ulen > (unsigned long)LONG_MAX) |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1397 goto long_range; |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1398 |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1399 new_stop = lstart - lstep; |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1400 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
|
1401 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
|
1402 |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1403 long_range: |
|
44061
0cc552ab76bd
Added all PyTypeObjects to the appropriate header files.
Christian Heimes <christian@cheimes.de>
parents:
44043
diff
changeset
|
1404 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
|
1405 if (it == NULL) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1406 return NULL; |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1407 |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1408 /* 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
|
1409 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
|
1410 Py_INCREF(it->len); |
|
57287
f1112c2700b1
Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57268
diff
changeset
|
1411 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1412 one = PyLong_FromLong(1); |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1413 if (!one) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1414 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
|
1415 |
|
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
|
1416 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
|
1417 Py_DECREF(one); |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1418 if (!diff) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1419 goto create_failure; |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1420 |
|
57263
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1421 product = PyNumber_Multiply(diff, range->step); |
|
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1422 Py_DECREF(diff); |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1423 if (!product) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1424 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
|
1425 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1426 sum = PyNumber_Add(range->start, product); |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1427 Py_DECREF(product); |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1428 it->start = sum; |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1429 if (!it->start) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1430 goto create_failure; |
|
57263
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1431 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1432 it->step = PyNumber_Negative(range->step); |
|
57287
f1112c2700b1
Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57268
diff
changeset
|
1433 if (!it->step) |
|
57263
b2a390c5de5f
Merged revisions 76292-76293 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
55797
diff
changeset
|
1434 goto create_failure; |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1435 |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1436 it->index = PyLong_FromLong(0); |
|
57287
f1112c2700b1
Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57268
diff
changeset
|
1437 if (!it->index) |
|
f1112c2700b1
Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57268
diff
changeset
|
1438 goto create_failure; |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1439 |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1440 return (PyObject *)it; |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1441 |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1442 create_failure: |
|
57287
f1112c2700b1
Merged revisions 76319 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57268
diff
changeset
|
1443 Py_DECREF(it); |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
1444 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
|
1445 } |
