annotate Objects/sliceobject.c @ 62449:53a152b400a7 legacy-trunk

must force gc here
author Benjamin Peterson <benjamin@python.org>
date Tue, 22 Jun 2010 20:26:20 +0000
parents fd25030223b0
children 3b484f53f91b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3837
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /*
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 Written by Jim Hugunin and Chris Chase.
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 Guido, feel free to do whatever you want in the way of copyrights
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 for this file.
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 */
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
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
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 the Py_NoneStruct in that there is no way to create other objects of
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 this type and there is exactly one in existence.
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 */
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
15
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
22 return PyString_FromString("Ellipsis");
3837
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 }
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
24
53273
42bf56e34825 Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47781
diff changeset
25 PyTypeObject PyEllipsis_Type = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
26 PyVarObject_HEAD_INIT(&PyType_Type, 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
27 "ellipsis", /* tp_name */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
28 0, /* tp_basicsize */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
29 0, /* tp_itemsize */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
30 0, /*never called*/ /* tp_dealloc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
31 0, /* tp_print */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
32 0, /* tp_getattr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
33 0, /* tp_setattr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
34 0, /* tp_compare */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
35 ellipsis_repr, /* tp_repr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
36 0, /* tp_as_number */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
37 0, /* tp_as_sequence */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
38 0, /* tp_as_mapping */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
39 0, /* tp_hash */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
40 0, /* tp_call */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
41 0, /* tp_str */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
42 PyObject_GenericGetAttr, /* tp_getattro */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
43 0, /* tp_setattro */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
44 0, /* tp_as_buffer */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
45 Py_TPFLAGS_DEFAULT, /* tp_flags */
3837
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 };
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
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 = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
49 _PyObject_EXTRA_INIT
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
50 1, &PyEllipsis_Type
3837
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 };
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
52
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
53
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 /* Slice object implementation
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
55
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 start, stop, and step are python objects with None indicating no
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 index is present.
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 */
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
59
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
63 PySliceObject *obj = PyObject_New(PySliceObject, &PySlice_Type);
3837
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
64
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
65 if (obj == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
66 return NULL;
15563
35e8a29e5488 Test for NULL returned from PyObject_NEW().
Guido van Rossum <guido@python.org>
parents: 13458
diff changeset
67
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
68 if (step == NULL) step = Py_None;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
69 Py_INCREF(step);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
70 if (start == NULL) start = Py_None;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
71 Py_INCREF(start);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
72 if (stop == NULL) stop = Py_None;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
73 Py_INCREF(stop);
3837
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
74
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
75 obj->step = step;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
76 obj->start = start;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
77 obj->stop = stop;
3837
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
78
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
79 return (PyObject *) obj;
3837
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 }
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
81
37165
45dd938b7feb Stop duplicating code and handle slice indices consistently and correctly
Neal Norwitz <nnorwitz@gmail.com>
parents: 36501
diff changeset
82 PyObject *
45dd938b7feb Stop duplicating code and handle slice indices consistently and correctly
Neal Norwitz <nnorwitz@gmail.com>
parents: 36501
diff changeset
83 _PySlice_FromIndices(Py_ssize_t istart, Py_ssize_t istop)
45dd938b7feb Stop duplicating code and handle slice indices consistently and correctly
Neal Norwitz <nnorwitz@gmail.com>
parents: 36501
diff changeset
84 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
85 PyObject *start, *end, *slice;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
86 start = PyInt_FromSsize_t(istart);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
87 if (!start)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
88 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
89 end = PyInt_FromSsize_t(istop);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
90 if (!end) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
91 Py_DECREF(start);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
92 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
93 }
37165
45dd938b7feb Stop duplicating code and handle slice indices consistently and correctly
Neal Norwitz <nnorwitz@gmail.com>
parents: 36501
diff changeset
94
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
95 slice = PySlice_New(start, end, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
96 Py_DECREF(start);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
97 Py_DECREF(end);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
98 return slice;
37165
45dd938b7feb Stop duplicating code and handle slice indices consistently and correctly
Neal Norwitz <nnorwitz@gmail.com>
parents: 36501
diff changeset
99 }
45dd938b7feb Stop duplicating code and handle slice indices consistently and correctly
Neal Norwitz <nnorwitz@gmail.com>
parents: 36501
diff changeset
100
3837
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
105 /* XXX support long ints */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
106 if (r->step == Py_None) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
107 *step = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
108 } else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
109 if (!PyInt_Check(r->step) && !PyLong_Check(r->step)) return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
110 *step = PyInt_AsSsize_t(r->step);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
111 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
112 if (r->start == Py_None) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
113 *start = *step < 0 ? length-1 : 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
114 } else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
115 if (!PyInt_Check(r->start) && !PyLong_Check(r->step)) return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
116 *start = PyInt_AsSsize_t(r->start);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
117 if (*start < 0) *start += length;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
118 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
119 if (r->stop == Py_None) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
120 *stop = *step < 0 ? -1 : length;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
121 } else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
122 if (!PyInt_Check(r->stop) && !PyLong_Check(r->step)) return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
123 *stop = PyInt_AsSsize_t(r->stop);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
124 if (*stop < 0) *stop += length;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
125 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
126 if (*stop > length) return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
127 if (*start >= length) return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
128 if (*step == 0) return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
129 return 0;
3837
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 }
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
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,
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
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 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
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
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
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
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
140 if (r->step == Py_None) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
141 *step = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
142 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
143 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
144 if (!_PyEval_SliceIndex(r->step, step)) return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
145 if (*step == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
146 PyErr_SetString(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
147 "slice step cannot be zero");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
148 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
149 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
150 }
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22978
diff changeset
151
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
152 defstart = *step < 0 ? length-1 : 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
153 defstop = *step < 0 ? -1 : length;
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22978
diff changeset
154
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
155 if (r->start == Py_None) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
156 *start = defstart;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
157 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
158 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
159 if (!_PyEval_SliceIndex(r->start, start)) return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
160 if (*start < 0) *start += length;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
161 if (*start < 0) *start = (*step < 0) ? -1 : 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
162 if (*start >= length)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
163 *start = (*step < 0) ? length - 1 : length;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
164 }
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22978
diff changeset
165
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
166 if (r->stop == Py_None) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
167 *stop = defstop;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
168 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
169 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
170 if (!_PyEval_SliceIndex(r->stop, stop)) return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
171 if (*stop < 0) *stop += length;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
172 if (*stop < 0) *stop = (*step < 0) ? -1 : 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
173 if (*stop >= length)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
174 *stop = (*step < 0) ? length - 1 : length;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
175 }
26035
6ed2e23aae45 Some days, I think my comment of
Michael W. Hudson <mwh@python.net>
parents: 25228
diff changeset
176
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
177 if ((*step < 0 && *stop >= *start)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
178 || (*step > 0 && *start >= *stop)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
179 *slicelength = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
180 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
181 else if (*step < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
182 *slicelength = (*stop-*start+1)/(*step)+1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
183 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
184 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
185 *slicelength = (*stop-*start-1)/(*step)+1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
186 }
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22978
diff changeset
187
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
188 return 0;
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22978
diff changeset
189 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22978
diff changeset
190
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23780
diff changeset
191 static PyObject *
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23780
diff changeset
192 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
193 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
194 PyObject *start, *stop, *step;
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23780
diff changeset
195
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
196 start = stop = step = NULL;
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23780
diff changeset
197
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
198 if (!_PyArg_NoKeywords("slice()", kw))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
199 return NULL;
35450
97c97194e64b Disallow keyword arguments for type constructors that don't use them
Georg Brandl <georg@python.org>
parents: 30332
diff changeset
200
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
201 if (!PyArg_UnpackTuple(args, "slice", 1, 3, &start, &stop, &step))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
202 return NULL;
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23780
diff changeset
203
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
204 /* This swapping of stop and start is to maintain similarity with
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
205 range(). */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
206 if (stop == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
207 stop = start;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
208 start = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
209 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
210 return PySlice_New(start, stop, step);
23868
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
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23780
diff changeset
213 PyDoc_STRVAR(slice_doc,
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23780
diff changeset
214 "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
215 \n\
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23780
diff changeset
216 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
217
3837
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 static void
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
219 slice_dealloc(PySliceObject *r)
3837
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
221 Py_DECREF(r->step);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
222 Py_DECREF(r->start);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
223 Py_DECREF(r->stop);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
224 PyObject_Del(r);
3837
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 }
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
226
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 static PyObject *
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
228 slice_repr(PySliceObject *r)
3837
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
230 PyObject *s, *comma;
3837
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
231
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
232 s = PyString_FromString("slice(");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
233 comma = PyString_FromString(", ");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
234 PyString_ConcatAndDel(&s, PyObject_Repr(r->start));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
235 PyString_Concat(&s, comma);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
236 PyString_ConcatAndDel(&s, PyObject_Repr(r->stop));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
237 PyString_Concat(&s, comma);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
238 PyString_ConcatAndDel(&s, PyObject_Repr(r->step));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
239 PyString_ConcatAndDel(&s, PyString_FromString(")"));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
240 Py_DECREF(comma);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
241 return s;
3837
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 }
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
243
20012
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
244 static PyMemberDef slice_members[] = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
245 {"start", T_OBJECT, offsetof(PySliceObject, start), READONLY},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
246 {"stop", T_OBJECT, offsetof(PySliceObject, stop), READONLY},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
247 {"step", T_OBJECT, offsetof(PySliceObject, step), READONLY},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
248 {0}
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17080
diff changeset
249 };
3837
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
250
24319
c43466f7dae8 A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents: 23868
diff changeset
251 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
252 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
253 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
254 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
255
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
256 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
257
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
258 if (ilen == -1 && PyErr_Occurred()) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
259 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
260 }
24319
c43466f7dae8 A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents: 23868
diff changeset
261
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
262 if (PySlice_GetIndicesEx(self, ilen, &start, &stop,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
263 &step, &slicelength) < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
264 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
265 }
24319
c43466f7dae8 A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents: 23868
diff changeset
266
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
267 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
268 }
c43466f7dae8 A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents: 23868
diff changeset
269
c43466f7dae8 A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents: 23868
diff changeset
270 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
271 "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
272 \n\
c43466f7dae8 A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents: 23868
diff changeset
273 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
274 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
275 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
276 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
277
41436
0dfa2a564076 SF 1191699: Make slices picklable
Raymond Hettinger <python@rcn.com>
parents: 39463
diff changeset
278 static PyObject *
0dfa2a564076 SF 1191699: Make slices picklable
Raymond Hettinger <python@rcn.com>
parents: 39463
diff changeset
279 slice_reduce(PySliceObject* self)
0dfa2a564076 SF 1191699: Make slices picklable
Raymond Hettinger <python@rcn.com>
parents: 39463
diff changeset
280 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
281 return Py_BuildValue("O(OOO)", Py_TYPE(self), self->start, self->stop, self->step);
41436
0dfa2a564076 SF 1191699: Make slices picklable
Raymond Hettinger <python@rcn.com>
parents: 39463
diff changeset
282 }
0dfa2a564076 SF 1191699: Make slices picklable
Raymond Hettinger <python@rcn.com>
parents: 39463
diff changeset
283
0dfa2a564076 SF 1191699: Make slices picklable
Raymond Hettinger <python@rcn.com>
parents: 39463
diff changeset
284 PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
0dfa2a564076 SF 1191699: Make slices picklable
Raymond Hettinger <python@rcn.com>
parents: 39463
diff changeset
285
24319
c43466f7dae8 A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents: 23868
diff changeset
286 static PyMethodDef slice_methods[] = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
287 {"indices", (PyCFunction)slice_indices,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
288 METH_O, slice_indices_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
289 {"__reduce__", (PyCFunction)slice_reduce,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
290 METH_NOARGS, reduce_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
291 {NULL, NULL}
24319
c43466f7dae8 A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents: 23868
diff changeset
292 };
c43466f7dae8 A few days ago, Guido said (in the thread "[Python-Dev] Python
Michael W. Hudson <mwh@python.net>
parents: 23868
diff changeset
293
17080
52cb0c5fdf23 SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents: 15563
diff changeset
294 static int
52cb0c5fdf23 SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents: 15563
diff changeset
295 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
296 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
297 int result = 0;
17080
52cb0c5fdf23 SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents: 15563
diff changeset
298
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
299 if (v == w)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
300 return 0;
17080
52cb0c5fdf23 SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents: 15563
diff changeset
301
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
302 if (PyObject_Cmp(v->start, w->start, &result) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
303 return -2;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
304 if (result != 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
305 return result;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
306 if (PyObject_Cmp(v->stop, w->stop, &result) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
307 return -2;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
308 if (result != 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
309 return result;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
310 if (PyObject_Cmp(v->step, w->step, &result) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
311 return -2;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
312 return result;
17080
52cb0c5fdf23 SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents: 15563
diff changeset
313 }
3837
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
314
30332
6b84802c9524 SF bug #800796: Difference between hash() and __hash__()
Raymond Hettinger <python@rcn.com>
parents: 26818
diff changeset
315 static long
6b84802c9524 SF bug #800796: Difference between hash() and __hash__()
Raymond Hettinger <python@rcn.com>
parents: 26818
diff changeset
316 slice_hash(PySliceObject *v)
6b84802c9524 SF bug #800796: Difference between hash() and __hash__()
Raymond Hettinger <python@rcn.com>
parents: 26818
diff changeset
317 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
318 PyErr_SetString(PyExc_TypeError, "unhashable type");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
319 return -1L;
30332
6b84802c9524 SF bug #800796: Difference between hash() and __hash__()
Raymond Hettinger <python@rcn.com>
parents: 26818
diff changeset
320 }
6b84802c9524 SF bug #800796: Difference between hash() and __hash__()
Raymond Hettinger <python@rcn.com>
parents: 26818
diff changeset
321
3837
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
322 PyTypeObject PySlice_Type = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
323 PyVarObject_HEAD_INIT(&PyType_Type, 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
324 "slice", /* Name of this type */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
325 sizeof(PySliceObject), /* Basic object size */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
326 0, /* Item size for varobject */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
327 (destructor)slice_dealloc, /* tp_dealloc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
328 0, /* tp_print */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
329 0, /* tp_getattr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
330 0, /* tp_setattr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
331 (cmpfunc)slice_compare, /* tp_compare */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
332 (reprfunc)slice_repr, /* tp_repr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
333 0, /* tp_as_number */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
334 0, /* tp_as_sequence */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
335 0, /* tp_as_mapping */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
336 (hashfunc)slice_hash, /* tp_hash */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
337 0, /* tp_call */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
338 0, /* tp_str */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
339 PyObject_GenericGetAttr, /* tp_getattro */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
340 0, /* tp_setattro */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
341 0, /* tp_as_buffer */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
342 Py_TPFLAGS_DEFAULT, /* tp_flags */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
343 slice_doc, /* tp_doc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
344 0, /* tp_traverse */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
345 0, /* tp_clear */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
346 0, /* tp_richcompare */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
347 0, /* tp_weaklistoffset */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
348 0, /* tp_iter */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
349 0, /* tp_iternext */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
350 slice_methods, /* tp_methods */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
351 slice_members, /* tp_members */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
352 0, /* tp_getset */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
353 0, /* tp_base */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
354 0, /* tp_dict */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
355 0, /* tp_descr_get */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
356 0, /* tp_descr_set */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
357 0, /* tp_dictoffset */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
358 0, /* tp_init */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
359 0, /* tp_alloc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 53273
diff changeset
360 slice_new, /* tp_new */
3837
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
361 };