Mercurial > cpython-lru_cache-weakref
annotate Objects/sliceobject.c @ 48345:cb871f736be0
merge error: test.test_support is now test.support
| author | Amaury Forgeot d'Arc <amauryfa@gmail.com> |
|---|---|
| date | Thu, 19 Jun 2008 22:03:50 +0000 |
| parents | 86a2679dca46 |
| children | e22deaeb6c1b |
| rev | line source |
|---|---|
| 3837 | 1 /* |
| 2 Written by Jim Hugunin and Chris Chase. | |
| 3 | |
|
4403
3b3f7c3f6da2
Ellipses -> Ellipsis rename (the dictionary really says that it should
Guido van Rossum <guido@python.org>
parents:
3861
diff
changeset
|
4 This includes both the singular ellipsis object and slice objects. |
| 3837 | 5 |
| 6 Guido, feel free to do whatever you want in the way of copyrights | |
| 7 for this file. | |
| 8 */ | |
| 9 | |
| 10 /* | |
|
4403
3b3f7c3f6da2
Ellipses -> Ellipsis rename (the dictionary really says that it should
Guido van Rossum <guido@python.org>
parents:
3861
diff
changeset
|
11 Py_Ellipsis encodes the '...' rubber index token. It is similar to |
| 3837 | 12 the Py_NoneStruct in that there is no way to create other objects of |
| 13 this type and there is exactly one in existence. | |
| 14 */ | |
| 15 | |
| 16 #include "Python.h" | |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
17 #include "structmember.h" |
| 3837 | 18 |
| 19 static PyObject * | |
|
13458
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
20 ellipsis_repr(PyObject *op) |
| 3837 | 21 { |
|
41769
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41554
diff
changeset
|
22 return PyUnicode_FromString("Ellipsis"); |
| 3837 | 23 } |
| 24 | |
|
4403
3b3f7c3f6da2
Ellipses -> Ellipsis rename (the dictionary really says that it should
Guido van Rossum <guido@python.org>
parents:
3861
diff
changeset
|
25 static PyTypeObject PyEllipsis_Type = { |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
41782
diff
changeset
|
26 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
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
|
27 "ellipsis", /* tp_name */ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
28 0, /* tp_basicsize */ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
29 0, /* tp_itemsize */ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
30 0, /*never called*/ /* tp_dealloc */ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
31 0, /* tp_print */ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
32 0, /* tp_getattr */ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
33 0, /* tp_setattr */ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
34 0, /* tp_compare */ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
35 ellipsis_repr, /* tp_repr */ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
36 0, /* tp_as_number */ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
37 0, /* tp_as_sequence */ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
38 0, /* tp_as_mapping */ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
39 0, /* tp_hash */ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
40 0, /* tp_call */ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
41 0, /* tp_str */ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
42 PyObject_GenericGetAttr, /* tp_getattro */ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
43 0, /* tp_setattro */ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
44 0, /* tp_as_buffer */ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
45 Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 3837 | 46 }; |
| 47 | |
|
4403
3b3f7c3f6da2
Ellipses -> Ellipsis rename (the dictionary really says that it should
Guido van Rossum <guido@python.org>
parents:
3861
diff
changeset
|
48 PyObject _Py_EllipsisObject = { |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
41782
diff
changeset
|
49 _PyObject_EXTRA_INIT |
|
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
41782
diff
changeset
|
50 1, &PyEllipsis_Type |
| 3837 | 51 }; |
| 52 | |
| 53 | |
| 54 /* Slice object implementation | |
| 55 | |
| 56 start, stop, and step are python objects with None indicating no | |
| 57 index is present. | |
| 58 */ | |
| 59 | |
| 60 PyObject * | |
|
13458
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
61 PySlice_New(PyObject *start, PyObject *stop, PyObject *step) |
| 3837 | 62 { |
|
22978
363c7fc174ac
Remove PyMalloc_New and PyMalloc_Del.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22446
diff
changeset
|
63 PySliceObject *obj = PyObject_New(PySliceObject, &PySlice_Type); |
| 3837 | 64 |
|
15563
35e8a29e5488
Test for NULL returned from PyObject_NEW().
Guido van Rossum <guido@python.org>
parents:
13458
diff
changeset
|
65 if (obj == NULL) |
|
35e8a29e5488
Test for NULL returned from PyObject_NEW().
Guido van Rossum <guido@python.org>
parents:
13458
diff
changeset
|
66 return NULL; |
|
35e8a29e5488
Test for NULL returned from PyObject_NEW().
Guido van Rossum <guido@python.org>
parents:
13458
diff
changeset
|
67 |
| 3837 | 68 if (step == NULL) step = Py_None; |
| 69 Py_INCREF(step); | |
| 70 if (start == NULL) start = Py_None; | |
| 71 Py_INCREF(start); | |
| 72 if (stop == NULL) stop = Py_None; | |
| 73 Py_INCREF(stop); | |
| 74 | |
| 75 obj->step = step; | |
| 76 obj->start = start; | |
| 77 obj->stop = stop; | |
| 78 | |
| 79 return (PyObject *) obj; | |
| 80 } | |
| 81 | |
|
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
|
82 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
|
83 _PySlice_FromIndices(Py_ssize_t istart, Py_ssize_t istop) |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
84 { |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
85 PyObject *start, *end, *slice; |
|
44099
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
43651
diff
changeset
|
86 start = PyLong_FromSsize_t(istart); |
|
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
|
87 if (!start) |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
88 return NULL; |
|
44099
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
43651
diff
changeset
|
89 end = PyLong_FromSsize_t(istop); |
|
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
|
90 if (!end) { |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
91 Py_DECREF(start); |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
92 return NULL; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
93 } |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
94 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
95 slice = PySlice_New(start, end, NULL); |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
96 Py_DECREF(start); |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
97 Py_DECREF(end); |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
98 return slice; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
99 } |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
100 |
| 3837 | 101 int |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35450
diff
changeset
|
102 PySlice_GetIndices(PySliceObject *r, Py_ssize_t length, |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35450
diff
changeset
|
103 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step) |
| 3837 | 104 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35450
diff
changeset
|
105 /* XXX support long ints */ |
| 3837 | 106 if (r->step == Py_None) { |
| 107 *step = 1; | |
| 108 } else { | |
|
43124
727539865f04
Remove checking redundantly for checks of PyInt and PyLong.
Neal Norwitz <nnorwitz@gmail.com>
parents:
42955
diff
changeset
|
109 if (!PyLong_Check(r->step)) return -1; |
|
44099
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
43651
diff
changeset
|
110 *step = PyLong_AsSsize_t(r->step); |
| 3837 | 111 } |
| 112 if (r->start == Py_None) { | |
| 113 *start = *step < 0 ? length-1 : 0; | |
| 114 } else { | |
|
44099
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
43651
diff
changeset
|
115 if (!PyLong_Check(r->start)) return -1; |
|
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
43651
diff
changeset
|
116 *start = PyLong_AsSsize_t(r->start); |
| 3837 | 117 if (*start < 0) *start += length; |
| 118 } | |
| 119 if (r->stop == Py_None) { | |
| 120 *stop = *step < 0 ? -1 : length; | |
| 121 } else { | |
|
44099
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
43651
diff
changeset
|
122 if (!PyLong_Check(r->stop)) return -1; |
|
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
43651
diff
changeset
|
123 *stop = PyLong_AsSsize_t(r->stop); |
| 3837 | 124 if (*stop < 0) *stop += length; |
| 125 } | |
| 126 if (*stop > length) return -1; | |
| 127 if (*start >= length) return -1; | |
| 128 if (*step == 0) return -1; | |
| 129 return 0; | |
| 130 } | |
| 131 | |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
132 int |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35450
diff
changeset
|
133 PySlice_GetIndicesEx(PySliceObject *r, Py_ssize_t length, |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35450
diff
changeset
|
134 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength) |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
135 { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
136 /* this is harder to get right than you might think */ |
|
26035
6ed2e23aae45
Some days, I think my comment of
Michael W. Hudson <mwh@python.net>
parents:
25228
diff
changeset
|
137 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35450
diff
changeset
|
138 Py_ssize_t defstart, defstop; |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
139 |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
140 if (r->step == Py_None) { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
141 *step = 1; |
|
26035
6ed2e23aae45
Some days, I think my comment of
Michael W. Hudson <mwh@python.net>
parents:
25228
diff
changeset
|
142 } |
|
6ed2e23aae45
Some days, I think my comment of
Michael W. Hudson <mwh@python.net>
parents:
25228
diff
changeset
|
143 else { |
|
26079
cab167613e43
Handle really big steps in extended slices.
Michael W. Hudson <mwh@python.net>
parents:
26035
diff
changeset
|
144 if (!_PyEval_SliceIndex(r->step, step)) return -1; |
|
cab167613e43
Handle really big steps in extended slices.
Michael W. Hudson <mwh@python.net>
parents:
26035
diff
changeset
|
145 if (*step == 0) { |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
146 PyErr_SetString(PyExc_ValueError, |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
147 "slice step cannot be zero"); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
148 return -1; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
149 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
150 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
151 |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
152 defstart = *step < 0 ? length-1 : 0; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
153 defstop = *step < 0 ? -1 : length; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
154 |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
155 if (r->start == Py_None) { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
156 *start = defstart; |
|
26035
6ed2e23aae45
Some days, I think my comment of
Michael W. Hudson <mwh@python.net>
parents:
25228
diff
changeset
|
157 } |
|
6ed2e23aae45
Some days, I think my comment of
Michael W. Hudson <mwh@python.net>
parents:
25228
diff
changeset
|
158 else { |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
159 if (!_PyEval_SliceIndex(r->start, start)) return -1; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
160 if (*start < 0) *start += length; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
161 if (*start < 0) *start = (*step < 0) ? -1 : 0; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
162 if (*start >= length) |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
163 *start = (*step < 0) ? length - 1 : length; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
164 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
165 |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
166 if (r->stop == Py_None) { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
167 *stop = defstop; |
|
26035
6ed2e23aae45
Some days, I think my comment of
Michael W. Hudson <mwh@python.net>
parents:
25228
diff
changeset
|
168 } |
|
6ed2e23aae45
Some days, I think my comment of
Michael W. Hudson <mwh@python.net>
parents:
25228
diff
changeset
|
169 else { |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
170 if (!_PyEval_SliceIndex(r->stop, stop)) return -1; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
171 if (*stop < 0) *stop += length; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
172 if (*stop < 0) *stop = -1; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
173 if (*stop > length) *stop = length; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
174 } |
|
26035
6ed2e23aae45
Some days, I think my comment of
Michael W. Hudson <mwh@python.net>
parents:
25228
diff
changeset
|
175 |
|
6ed2e23aae45
Some days, I think my comment of
Michael W. Hudson <mwh@python.net>
parents:
25228
diff
changeset
|
176 if ((*step < 0 && *stop >= *start) |
|
6ed2e23aae45
Some days, I think my comment of
Michael W. Hudson <mwh@python.net>
parents:
25228
diff
changeset
|
177 || (*step > 0 && *start >= *stop)) { |
|
23780
c7cda63a007e
Fix for problem reported by Neal Norwitz. Tighten up calculation of
Michael W. Hudson <mwh@python.net>
parents:
23778
diff
changeset
|
178 *slicelength = 0; |
|
c7cda63a007e
Fix for problem reported by Neal Norwitz. Tighten up calculation of
Michael W. Hudson <mwh@python.net>
parents:
23778
diff
changeset
|
179 } |
|
c7cda63a007e
Fix for problem reported by Neal Norwitz. Tighten up calculation of
Michael W. Hudson <mwh@python.net>
parents:
23778
diff
changeset
|
180 else if (*step < 0) { |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
181 *slicelength = (*stop-*start+1)/(*step)+1; |
|
26035
6ed2e23aae45
Some days, I think my comment of
Michael W. Hudson <mwh@python.net>
parents:
25228
diff
changeset
|
182 } |
|
6ed2e23aae45
Some days, I think my comment of
Michael W. Hudson <mwh@python.net>
parents:
25228
diff
changeset
|
183 else { |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
184 *slicelength = (*stop-*start-1)/(*step)+1; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
185 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
186 |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
187 return 0; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
188 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
189 |
|
23868
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
190 static PyObject * |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
191 slice_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
192 { |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
193 PyObject *start, *stop, *step; |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
194 |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
195 start = stop = step = NULL; |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
196 |
|
35450
97c97194e64b
Disallow keyword arguments for type constructors that don't use them
Georg Brandl <georg@python.org>
parents:
30332
diff
changeset
|
197 if (!_PyArg_NoKeywords("slice()", kw)) |
|
97c97194e64b
Disallow keyword arguments for type constructors that don't use them
Georg Brandl <georg@python.org>
parents:
30332
diff
changeset
|
198 return NULL; |
|
97c97194e64b
Disallow keyword arguments for type constructors that don't use them
Georg Brandl <georg@python.org>
parents:
30332
diff
changeset
|
199 |
|
26818
e094a03a80ff
SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents:
26079
diff
changeset
|
200 if (!PyArg_UnpackTuple(args, "slice", 1, 3, &start, &stop, &step)) |
|
23868
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
201 return NULL; |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
202 |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
203 /* This swapping of stop and start is to maintain similarity with |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
204 range(). */ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
205 if (stop == NULL) { |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
206 stop = start; |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
207 start = NULL; |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
208 } |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
209 return PySlice_New(start, stop, step); |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
210 } |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
211 |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
212 PyDoc_STRVAR(slice_doc, |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
213 "slice([start,] stop[, step])\n\ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
214 \n\ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
215 Create a slice object. This is used for extended slicing (e.g. a[0:10:2])."); |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
216 |
| 3837 | 217 static void |
|
13458
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
218 slice_dealloc(PySliceObject *r) |
| 3837 | 219 { |
| 220 Py_DECREF(r->step); | |
| 221 Py_DECREF(r->start); | |
| 222 Py_DECREF(r->stop); | |
|
22978
363c7fc174ac
Remove PyMalloc_New and PyMalloc_Del.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22446
diff
changeset
|
223 PyObject_Del(r); |
| 3837 | 224 } |
| 225 | |
| 226 static PyObject * | |
|
13458
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
227 slice_repr(PySliceObject *r) |
| 3837 | 228 { |
|
41782
bee8d01ef42c
Add a format specifier %R to PyUnicode_FromFormat(), which embeds
Walter Dörwald <walter@livinglogic.de>
parents:
41769
diff
changeset
|
229 return PyUnicode_FromFormat("slice(%R, %R, %R)", r->start, r->stop, r->step); |
| 3837 | 230 } |
| 231 | |
|
20012
3faabf54b8b8
Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
232 static PyMemberDef slice_members[] = { |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
233 {"start", T_OBJECT, offsetof(PySliceObject, start), READONLY}, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
234 {"stop", T_OBJECT, offsetof(PySliceObject, stop), READONLY}, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
235 {"step", T_OBJECT, offsetof(PySliceObject, step), READONLY}, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
236 {0} |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
237 }; |
| 3837 | 238 |
|
24319
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
239 static PyObject* |
|
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
240 slice_indices(PySliceObject* self, PyObject* len) |
|
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
241 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35450
diff
changeset
|
242 Py_ssize_t ilen, start, stop, step, slicelength; |
|
24319
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
243 |
|
39610
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
244 ilen = PyNumber_AsSsize_t(len, PyExc_OverflowError); |
|
24319
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
245 |
|
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
246 if (ilen == -1 && PyErr_Occurred()) { |
|
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
247 return NULL; |
|
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
248 } |
|
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
249 |
|
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
250 if (PySlice_GetIndicesEx(self, ilen, &start, &stop, |
|
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
251 &step, &slicelength) < 0) { |
|
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
252 return NULL; |
|
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
253 } |
|
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
254 |
|
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
|
255 return Py_BuildValue("(nnn)", start, stop, step); |
|
24319
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
256 } |
|
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
257 |
|
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
258 PyDoc_STRVAR(slice_indices_doc, |
|
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
259 "S.indices(len) -> (start, stop, stride)\n\ |
|
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
260 \n\ |
|
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
261 Assuming a sequence of length len, calculate the start and stop\n\ |
|
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
262 indices, and the stride length of the extended slice described by\n\ |
|
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
263 S. Out of bounds indices are clipped in a manner consistent with the\n\ |
|
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
264 handling of normal slices."); |
|
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
265 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
266 static PyObject * |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
267 slice_reduce(PySliceObject* self) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
268 { |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
269 return Py_BuildValue("O(OOO)", Py_TYPE(self), self->start, self->stop, self->step); |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
270 } |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
271 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
272 PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
273 |
|
24319
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
274 static PyMethodDef slice_methods[] = { |
|
24321
31227353cbbc
Silly typo. Not sure how that got in.
Michael W. Hudson <mwh@python.net>
parents:
24319
diff
changeset
|
275 {"indices", (PyCFunction)slice_indices, |
|
24319
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
276 METH_O, slice_indices_doc}, |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
277 {"__reduce__", (PyCFunction)slice_reduce, |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
278 METH_NOARGS, reduce_doc}, |
|
24319
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
279 {NULL, NULL} |
|
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
280 }; |
|
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
281 |
|
39644
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
282 static PyObject * |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
283 slice_richcompare(PyObject *v, PyObject *w, int op) |
|
17080
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
284 { |
|
39644
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
285 PyObject *t1; |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
286 PyObject *t2; |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
287 PyObject *res; |
|
17080
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
288 |
|
42955
30511068d3d4
Fix buglet in sliceobjects, they were not returning Py_NotImplemented when
Thomas Wouters <thomas@python.org>
parents:
42334
diff
changeset
|
289 if (!PySlice_Check(v) || !PySlice_Check(w)) { |
|
30511068d3d4
Fix buglet in sliceobjects, they were not returning Py_NotImplemented when
Thomas Wouters <thomas@python.org>
parents:
42334
diff
changeset
|
290 Py_INCREF(Py_NotImplemented); |
|
30511068d3d4
Fix buglet in sliceobjects, they were not returning Py_NotImplemented when
Thomas Wouters <thomas@python.org>
parents:
42334
diff
changeset
|
291 return Py_NotImplemented; |
|
30511068d3d4
Fix buglet in sliceobjects, they were not returning Py_NotImplemented when
Thomas Wouters <thomas@python.org>
parents:
42334
diff
changeset
|
292 } |
|
30511068d3d4
Fix buglet in sliceobjects, they were not returning Py_NotImplemented when
Thomas Wouters <thomas@python.org>
parents:
42334
diff
changeset
|
293 |
|
39644
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
294 if (v == w) { |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
295 /* XXX Do we really need this shortcut? |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
296 There's a unit test for it, but is that fair? */ |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
297 switch (op) { |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
298 case Py_EQ: |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
299 case Py_LE: |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
300 case Py_GE: |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
301 res = Py_True; |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
302 break; |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
303 default: |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
304 res = Py_False; |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
305 break; |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
306 } |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
307 Py_INCREF(res); |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
308 return res; |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
309 } |
|
17080
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
310 |
|
39644
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
311 t1 = PyTuple_New(3); |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
312 t2 = PyTuple_New(3); |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
313 if (t1 == NULL || t2 == NULL) |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
314 return NULL; |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
315 |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
316 PyTuple_SET_ITEM(t1, 0, ((PySliceObject *)v)->start); |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
317 PyTuple_SET_ITEM(t1, 1, ((PySliceObject *)v)->stop); |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
318 PyTuple_SET_ITEM(t1, 2, ((PySliceObject *)v)->step); |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
319 PyTuple_SET_ITEM(t2, 0, ((PySliceObject *)w)->start); |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
320 PyTuple_SET_ITEM(t2, 1, ((PySliceObject *)w)->stop); |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
321 PyTuple_SET_ITEM(t2, 2, ((PySliceObject *)w)->step); |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
322 |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
323 res = PyObject_RichCompare(t1, t2, op); |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
324 |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
325 PyTuple_SET_ITEM(t1, 0, NULL); |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
326 PyTuple_SET_ITEM(t1, 1, NULL); |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
327 PyTuple_SET_ITEM(t1, 2, NULL); |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
328 PyTuple_SET_ITEM(t2, 0, NULL); |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
329 PyTuple_SET_ITEM(t2, 1, NULL); |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
330 PyTuple_SET_ITEM(t2, 2, NULL); |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
331 |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
332 Py_DECREF(t1); |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
333 Py_DECREF(t2); |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
334 |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
335 return res; |
|
17080
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
336 } |
| 3837 | 337 |
|
30332
6b84802c9524
SF bug #800796: Difference between hash() and __hash__()
Raymond Hettinger <python@rcn.com>
parents:
26818
diff
changeset
|
338 static long |
|
6b84802c9524
SF bug #800796: Difference between hash() and __hash__()
Raymond Hettinger <python@rcn.com>
parents:
26818
diff
changeset
|
339 slice_hash(PySliceObject *v) |
|
6b84802c9524
SF bug #800796: Difference between hash() and __hash__()
Raymond Hettinger <python@rcn.com>
parents:
26818
diff
changeset
|
340 { |
|
6b84802c9524
SF bug #800796: Difference between hash() and __hash__()
Raymond Hettinger <python@rcn.com>
parents:
26818
diff
changeset
|
341 PyErr_SetString(PyExc_TypeError, "unhashable type"); |
|
6b84802c9524
SF bug #800796: Difference between hash() and __hash__()
Raymond Hettinger <python@rcn.com>
parents:
26818
diff
changeset
|
342 return -1L; |
|
6b84802c9524
SF bug #800796: Difference between hash() and __hash__()
Raymond Hettinger <python@rcn.com>
parents:
26818
diff
changeset
|
343 } |
|
6b84802c9524
SF bug #800796: Difference between hash() and __hash__()
Raymond Hettinger <python@rcn.com>
parents:
26818
diff
changeset
|
344 |
| 3837 | 345 PyTypeObject PySlice_Type = { |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
41782
diff
changeset
|
346 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3837 | 347 "slice", /* Name of this type */ |
| 348 sizeof(PySliceObject), /* Basic object size */ | |
| 349 0, /* Item size for varobject */ | |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
350 (destructor)slice_dealloc, /* tp_dealloc */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
351 0, /* tp_print */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
352 0, /* tp_getattr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
353 0, /* tp_setattr */ |
|
39644
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
354 0, /* tp_compare */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
355 (reprfunc)slice_repr, /* tp_repr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
356 0, /* tp_as_number */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
357 0, /* tp_as_sequence */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
358 0, /* tp_as_mapping */ |
|
30332
6b84802c9524
SF bug #800796: Difference between hash() and __hash__()
Raymond Hettinger <python@rcn.com>
parents:
26818
diff
changeset
|
359 (hashfunc)slice_hash, /* tp_hash */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
360 0, /* tp_call */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
361 0, /* tp_str */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
362 PyObject_GenericGetAttr, /* tp_getattro */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
363 0, /* tp_setattro */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
364 0, /* tp_as_buffer */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
365 Py_TPFLAGS_DEFAULT, /* tp_flags */ |
|
23868
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
366 slice_doc, /* tp_doc */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
367 0, /* tp_traverse */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
368 0, /* tp_clear */ |
|
39644
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
369 slice_richcompare, /* tp_richcompare */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
370 0, /* tp_weaklistoffset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
371 0, /* tp_iter */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
372 0, /* tp_iternext */ |
|
24319
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
373 slice_methods, /* tp_methods */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
374 slice_members, /* tp_members */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
375 0, /* tp_getset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
376 0, /* tp_base */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
377 0, /* tp_dict */ |
|
23868
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
378 0, /* tp_descr_get */ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
379 0, /* tp_descr_set */ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
380 0, /* tp_dictoffset */ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
381 0, /* tp_init */ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
382 0, /* tp_alloc */ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
383 slice_new, /* tp_new */ |
| 3837 | 384 }; |
