Mercurial > cpython-lru_cache-weakref
annotate Objects/sliceobject.c @ 39587:bbef2ecdee33 2.4
Patch #1540329: _hotshot.c fix backports.
| author | Georg Brandl <georg@python.org> |
|---|---|
| date | Sat, 19 Aug 2006 07:48:06 +0000 |
| parents | 97c97194e64b |
| children | 7fdc639bc5b4 |
| 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 { |
|
4403
3b3f7c3f6da2
Ellipses -> Ellipsis rename (the dictionary really says that it should
Guido van Rossum <guido@python.org>
parents:
3861
diff
changeset
|
22 return PyString_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 = { |
| 3837 | 26 PyObject_HEAD_INIT(&PyType_Type) |
|
20758
a4bf3c5cc45a
Add values to tp_getattro and tp_flags so that dir(Ellipsis) will
Guido van Rossum <guido@python.org>
parents:
20012
diff
changeset
|
27 0, /* ob_size */ |
|
a4bf3c5cc45a
Add values to tp_getattro and tp_flags so that dir(Ellipsis) will
Guido van Rossum <guido@python.org>
parents:
20012
diff
changeset
|
28 "ellipsis", /* tp_name */ |
|
a4bf3c5cc45a
Add values to tp_getattro and tp_flags so that dir(Ellipsis) will
Guido van Rossum <guido@python.org>
parents:
20012
diff
changeset
|
29 0, /* tp_basicsize */ |
|
a4bf3c5cc45a
Add values to tp_getattro and tp_flags so that dir(Ellipsis) will
Guido van Rossum <guido@python.org>
parents:
20012
diff
changeset
|
30 0, /* tp_itemsize */ |
|
a4bf3c5cc45a
Add values to tp_getattro and tp_flags so that dir(Ellipsis) will
Guido van Rossum <guido@python.org>
parents:
20012
diff
changeset
|
31 0, /*never called*/ /* tp_dealloc */ |
|
a4bf3c5cc45a
Add values to tp_getattro and tp_flags so that dir(Ellipsis) will
Guido van Rossum <guido@python.org>
parents:
20012
diff
changeset
|
32 0, /* tp_print */ |
|
a4bf3c5cc45a
Add values to tp_getattro and tp_flags so that dir(Ellipsis) will
Guido van Rossum <guido@python.org>
parents:
20012
diff
changeset
|
33 0, /* tp_getattr */ |
|
a4bf3c5cc45a
Add values to tp_getattro and tp_flags so that dir(Ellipsis) will
Guido van Rossum <guido@python.org>
parents:
20012
diff
changeset
|
34 0, /* tp_setattr */ |
|
a4bf3c5cc45a
Add values to tp_getattro and tp_flags so that dir(Ellipsis) will
Guido van Rossum <guido@python.org>
parents:
20012
diff
changeset
|
35 0, /* tp_compare */ |
|
a4bf3c5cc45a
Add values to tp_getattro and tp_flags so that dir(Ellipsis) will
Guido van Rossum <guido@python.org>
parents:
20012
diff
changeset
|
36 (reprfunc)ellipsis_repr, /* tp_repr */ |
|
a4bf3c5cc45a
Add values to tp_getattro and tp_flags so that dir(Ellipsis) will
Guido van Rossum <guido@python.org>
parents:
20012
diff
changeset
|
37 0, /* tp_as_number */ |
|
a4bf3c5cc45a
Add values to tp_getattro and tp_flags so that dir(Ellipsis) will
Guido van Rossum <guido@python.org>
parents:
20012
diff
changeset
|
38 0, /* tp_as_sequence */ |
|
a4bf3c5cc45a
Add values to tp_getattro and tp_flags so that dir(Ellipsis) will
Guido van Rossum <guido@python.org>
parents:
20012
diff
changeset
|
39 0, /* tp_as_mapping */ |
|
a4bf3c5cc45a
Add values to tp_getattro and tp_flags so that dir(Ellipsis) will
Guido van Rossum <guido@python.org>
parents:
20012
diff
changeset
|
40 0, /* tp_hash */ |
|
a4bf3c5cc45a
Add values to tp_getattro and tp_flags so that dir(Ellipsis) will
Guido van Rossum <guido@python.org>
parents:
20012
diff
changeset
|
41 0, /* tp_call */ |
|
a4bf3c5cc45a
Add values to tp_getattro and tp_flags so that dir(Ellipsis) will
Guido van Rossum <guido@python.org>
parents:
20012
diff
changeset
|
42 0, /* tp_str */ |
|
a4bf3c5cc45a
Add values to tp_getattro and tp_flags so that dir(Ellipsis) will
Guido van Rossum <guido@python.org>
parents:
20012
diff
changeset
|
43 PyObject_GenericGetAttr, /* tp_getattro */ |
|
a4bf3c5cc45a
Add values to tp_getattro and tp_flags so that dir(Ellipsis) will
Guido van Rossum <guido@python.org>
parents:
20012
diff
changeset
|
44 0, /* tp_setattro */ |
|
a4bf3c5cc45a
Add values to tp_getattro and tp_flags so that dir(Ellipsis) will
Guido van Rossum <guido@python.org>
parents:
20012
diff
changeset
|
45 0, /* tp_as_buffer */ |
|
a4bf3c5cc45a
Add values to tp_getattro and tp_flags so that dir(Ellipsis) will
Guido van Rossum <guido@python.org>
parents:
20012
diff
changeset
|
46 Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 3837 | 47 }; |
| 48 | |
|
4403
3b3f7c3f6da2
Ellipses -> Ellipsis rename (the dictionary really says that it should
Guido van Rossum <guido@python.org>
parents:
3861
diff
changeset
|
49 PyObject _Py_EllipsisObject = { |
|
3b3f7c3f6da2
Ellipses -> Ellipsis rename (the dictionary really says that it should
Guido van Rossum <guido@python.org>
parents:
3861
diff
changeset
|
50 PyObject_HEAD_INIT(&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 | |
| 82 int | |
|
13458
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
83 PySlice_GetIndices(PySliceObject *r, int length, |
|
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
84 int *start, int *stop, int *step) |
| 3837 | 85 { |
| 86 if (r->step == Py_None) { | |
| 87 *step = 1; | |
| 88 } else { | |
| 89 if (!PyInt_Check(r->step)) return -1; | |
| 90 *step = PyInt_AsLong(r->step); | |
| 91 } | |
| 92 if (r->start == Py_None) { | |
| 93 *start = *step < 0 ? length-1 : 0; | |
| 94 } else { | |
| 95 if (!PyInt_Check(r->start)) return -1; | |
| 96 *start = PyInt_AsLong(r->start); | |
| 97 if (*start < 0) *start += length; | |
| 98 } | |
| 99 if (r->stop == Py_None) { | |
| 100 *stop = *step < 0 ? -1 : length; | |
| 101 } else { | |
| 102 if (!PyInt_Check(r->stop)) return -1; | |
| 103 *stop = PyInt_AsLong(r->stop); | |
| 104 if (*stop < 0) *stop += length; | |
| 105 } | |
| 106 if (*stop > length) return -1; | |
| 107 if (*start >= length) return -1; | |
| 108 if (*step == 0) return -1; | |
| 109 return 0; | |
| 110 } | |
| 111 | |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
112 int |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
113 PySlice_GetIndicesEx(PySliceObject *r, int length, |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
114 int *start, int *stop, int *step, int *slicelength) |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
115 { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
116 /* 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
|
117 |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
118 int defstart, defstop; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
119 |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
120 if (r->step == Py_None) { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
121 *step = 1; |
|
26035
6ed2e23aae45
Some days, I think my comment of
Michael W. Hudson <mwh@python.net>
parents:
25228
diff
changeset
|
122 } |
|
6ed2e23aae45
Some days, I think my comment of
Michael W. Hudson <mwh@python.net>
parents:
25228
diff
changeset
|
123 else { |
|
26079
cab167613e43
Handle really big steps in extended slices.
Michael W. Hudson <mwh@python.net>
parents:
26035
diff
changeset
|
124 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
|
125 if (*step == 0) { |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
126 PyErr_SetString(PyExc_ValueError, |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
127 "slice step cannot be zero"); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
128 return -1; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
129 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
130 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
131 |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
132 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
|
133 defstop = *step < 0 ? -1 : length; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
134 |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
135 if (r->start == Py_None) { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
136 *start = defstart; |
|
26035
6ed2e23aae45
Some days, I think my comment of
Michael W. Hudson <mwh@python.net>
parents:
25228
diff
changeset
|
137 } |
|
6ed2e23aae45
Some days, I think my comment of
Michael W. Hudson <mwh@python.net>
parents:
25228
diff
changeset
|
138 else { |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
139 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
|
140 if (*start < 0) *start += length; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
141 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
|
142 if (*start >= length) |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
143 *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
|
144 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
145 |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
146 if (r->stop == Py_None) { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
147 *stop = defstop; |
|
26035
6ed2e23aae45
Some days, I think my comment of
Michael W. Hudson <mwh@python.net>
parents:
25228
diff
changeset
|
148 } |
|
6ed2e23aae45
Some days, I think my comment of
Michael W. Hudson <mwh@python.net>
parents:
25228
diff
changeset
|
149 else { |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
150 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
|
151 if (*stop < 0) *stop += length; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
152 if (*stop < 0) *stop = -1; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
153 if (*stop > length) *stop = length; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
154 } |
|
26035
6ed2e23aae45
Some days, I think my comment of
Michael W. Hudson <mwh@python.net>
parents:
25228
diff
changeset
|
155 |
|
6ed2e23aae45
Some days, I think my comment of
Michael W. Hudson <mwh@python.net>
parents:
25228
diff
changeset
|
156 if ((*step < 0 && *stop >= *start) |
|
6ed2e23aae45
Some days, I think my comment of
Michael W. Hudson <mwh@python.net>
parents:
25228
diff
changeset
|
157 || (*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
|
158 *slicelength = 0; |
|
c7cda63a007e
Fix for problem reported by Neal Norwitz. Tighten up calculation of
Michael W. Hudson <mwh@python.net>
parents:
23778
diff
changeset
|
159 } |
|
c7cda63a007e
Fix for problem reported by Neal Norwitz. Tighten up calculation of
Michael W. Hudson <mwh@python.net>
parents:
23778
diff
changeset
|
160 else if (*step < 0) { |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
161 *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
|
162 } |
|
6ed2e23aae45
Some days, I think my comment of
Michael W. Hudson <mwh@python.net>
parents:
25228
diff
changeset
|
163 else { |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
164 *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
|
165 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
166 |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
167 return 0; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
168 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22978
diff
changeset
|
169 |
|
23868
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
170 static PyObject * |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
171 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
|
172 { |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
173 PyObject *start, *stop, *step; |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
174 |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
175 start = stop = step = NULL; |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
176 |
|
35450
97c97194e64b
Disallow keyword arguments for type constructors that don't use them
Georg Brandl <georg@python.org>
parents:
30332
diff
changeset
|
177 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
|
178 return NULL; |
|
97c97194e64b
Disallow keyword arguments for type constructors that don't use them
Georg Brandl <georg@python.org>
parents:
30332
diff
changeset
|
179 |
|
26818
e094a03a80ff
SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents:
26079
diff
changeset
|
180 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
|
181 return NULL; |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
182 |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
183 /* 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
|
184 range(). */ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
185 if (stop == NULL) { |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
186 stop = start; |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
187 start = NULL; |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
188 } |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
189 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
|
190 } |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
191 |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
192 PyDoc_STRVAR(slice_doc, |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
193 "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
|
194 \n\ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
195 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
|
196 |
| 3837 | 197 static void |
|
13458
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
198 slice_dealloc(PySliceObject *r) |
| 3837 | 199 { |
| 200 Py_DECREF(r->step); | |
| 201 Py_DECREF(r->start); | |
| 202 Py_DECREF(r->stop); | |
|
22978
363c7fc174ac
Remove PyMalloc_New and PyMalloc_Del.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22446
diff
changeset
|
203 PyObject_Del(r); |
| 3837 | 204 } |
| 205 | |
| 206 static PyObject * | |
|
13458
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
207 slice_repr(PySliceObject *r) |
| 3837 | 208 { |
| 209 PyObject *s, *comma; | |
| 210 | |
| 211 s = PyString_FromString("slice("); | |
| 212 comma = PyString_FromString(", "); | |
| 213 PyString_ConcatAndDel(&s, PyObject_Repr(r->start)); | |
| 214 PyString_Concat(&s, comma); | |
| 215 PyString_ConcatAndDel(&s, PyObject_Repr(r->stop)); | |
| 216 PyString_Concat(&s, comma); | |
| 217 PyString_ConcatAndDel(&s, PyObject_Repr(r->step)); | |
| 218 PyString_ConcatAndDel(&s, PyString_FromString(")")); | |
| 219 Py_DECREF(comma); | |
| 220 return s; | |
| 221 } | |
| 222 | |
|
20012
3faabf54b8b8
Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
223 static PyMemberDef slice_members[] = { |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
224 {"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
|
225 {"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
|
226 {"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
|
227 {0} |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
228 }; |
| 3837 | 229 |
|
24319
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
230 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
|
231 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
|
232 { |
|
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
233 int ilen, start, stop, step, slicelength; |
|
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
234 |
|
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
235 ilen = PyInt_AsLong(len); |
|
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
236 |
|
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
237 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
|
238 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
|
239 } |
|
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
240 |
|
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
241 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
|
242 &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
|
243 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
|
244 } |
|
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
245 |
|
25228
1a7895f97b30
Fix for platforms where int != long.
Michael W. Hudson <mwh@python.net>
parents:
24321
diff
changeset
|
246 return Py_BuildValue("(iii)", 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
|
247 } |
|
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 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
|
250 "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
|
251 \n\ |
|
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
252 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
|
253 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
|
254 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
|
255 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
|
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 static PyMethodDef slice_methods[] = { |
|
24321
31227353cbbc
Silly typo. Not sure how that got in.
Michael W. Hudson <mwh@python.net>
parents:
24319
diff
changeset
|
258 {"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
|
259 METH_O, 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
|
260 {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
|
261 }; |
|
c43466f7dae8
A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents:
23868
diff
changeset
|
262 |
|
17080
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
263 static int |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
264 slice_compare(PySliceObject *v, PySliceObject *w) |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
265 { |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
266 int result = 0; |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
267 |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
268 if (v == w) |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
269 return 0; |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
270 |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
271 if (PyObject_Cmp(v->start, w->start, &result) < 0) |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
272 return -2; |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
273 if (result != 0) |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
274 return result; |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
275 if (PyObject_Cmp(v->stop, w->stop, &result) < 0) |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
276 return -2; |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
277 if (result != 0) |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
278 return result; |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
279 if (PyObject_Cmp(v->step, w->step, &result) < 0) |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
280 return -2; |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
281 return result; |
|
52cb0c5fdf23
SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents:
15563
diff
changeset
|
282 } |
| 3837 | 283 |
|
30332
6b84802c9524
SF bug #800796: Difference between hash() and __hash__()
Raymond Hettinger <python@rcn.com>
parents:
26818
diff
changeset
|
284 static long |
|
6b84802c9524
SF bug #800796: Difference between hash() and __hash__()
Raymond Hettinger <python@rcn.com>
parents:
26818
diff
changeset
|
285 slice_hash(PySliceObject *v) |
|
6b84802c9524
SF bug #800796: Difference between hash() and __hash__()
Raymond Hettinger <python@rcn.com>
parents:
26818
diff
changeset
|
286 { |
|
6b84802c9524
SF bug #800796: Difference between hash() and __hash__()
Raymond Hettinger <python@rcn.com>
parents:
26818
diff
changeset
|
287 PyErr_SetString(PyExc_TypeError, "unhashable type"); |
|
6b84802c9524
SF bug #800796: Difference between hash() and __hash__()
Raymond Hettinger <python@rcn.com>
parents:
26818
diff
changeset
|
288 return -1L; |
|
6b84802c9524
SF bug #800796: Difference between hash() and __hash__()
Raymond Hettinger <python@rcn.com>
parents:
26818
diff
changeset
|
289 } |
|
6b84802c9524
SF bug #800796: Difference between hash() and __hash__()
Raymond Hettinger <python@rcn.com>
parents:
26818
diff
changeset
|
290 |
| 3837 | 291 PyTypeObject PySlice_Type = { |
| 292 PyObject_HEAD_INIT(&PyType_Type) | |
| 293 0, /* Number of items for varobject */ | |
| 294 "slice", /* Name of this type */ | |
| 295 sizeof(PySliceObject), /* Basic object size */ | |
| 296 0, /* Item size for varobject */ | |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
297 (destructor)slice_dealloc, /* tp_dealloc */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
298 0, /* tp_print */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
299 0, /* tp_getattr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
300 0, /* tp_setattr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
301 (cmpfunc)slice_compare, /* tp_compare */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
302 (reprfunc)slice_repr, /* tp_repr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
303 0, /* tp_as_number */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
304 0, /* tp_as_sequence */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
305 0, /* tp_as_mapping */ |
|
30332
6b84802c9524
SF bug #800796: Difference between hash() and __hash__()
Raymond Hettinger <python@rcn.com>
parents:
26818
diff
changeset
|
306 (hashfunc)slice_hash, /* tp_hash */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
307 0, /* tp_call */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
308 0, /* tp_str */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
309 PyObject_GenericGetAttr, /* tp_getattro */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
310 0, /* tp_setattro */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
311 0, /* tp_as_buffer */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
312 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
|
313 slice_doc, /* tp_doc */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
314 0, /* tp_traverse */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
315 0, /* tp_clear */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
316 0, /* tp_richcompare */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
317 0, /* tp_weaklistoffset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
318 0, /* tp_iter */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
319 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
|
320 slice_methods, /* tp_methods */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
321 slice_members, /* tp_members */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
322 0, /* tp_getset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
323 0, /* tp_base */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17080
diff
changeset
|
324 0, /* tp_dict */ |
|
23868
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
325 0, /* tp_descr_get */ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
326 0, /* tp_descr_set */ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
327 0, /* tp_dictoffset */ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
328 0, /* tp_init */ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
329 0, /* tp_alloc */ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23780
diff
changeset
|
330 slice_new, /* tp_new */ |
| 3837 | 331 }; |
