annotate Objects/sliceobject.c @ 24042:63fbbed10714 legacy-trunk

Simplify HTTPSConnection constructor. See discussion in SF bug 458463.
author Jeremy Hylton <jeremy@alum.mit.edu>
date Fri, 28 Jun 2002 23:38:14 +0000
parents 8e6760bf1884
children c43466f7dae8
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
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
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 {
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
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
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
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 };
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
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 {
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
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 if (step == NULL) step = Py_None;
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 Py_INCREF(step);
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 if (start == NULL) start = Py_None;
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 Py_INCREF(start);
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 if (stop == NULL) stop = Py_None;
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 Py_INCREF(stop);
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
74
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 obj->step = step;
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 obj->start = start;
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 obj->stop = stop;
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
78
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 return (PyObject *) obj;
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
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 {
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 if (r->step == Py_None) {
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 *step = 1;
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 } else {
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 if (!PyInt_Check(r->step)) return -1;
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 *step = PyInt_AsLong(r->step);
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 }
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 if (r->start == Py_None) {
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 *start = *step < 0 ? length-1 : 0;
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 } else {
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 if (!PyInt_Check(r->start)) return -1;
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 *start = PyInt_AsLong(r->start);
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 if (*start < 0) *start += length;
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 }
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 if (r->stop == Py_None) {
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 *stop = *step < 0 ? -1 : length;
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 } else {
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 if (!PyInt_Check(r->stop)) return -1;
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 *stop = PyInt_AsLong(r->stop);
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 if (*stop < 0) *stop += length;
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 }
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 if (*stop > length) return -1;
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 if (*start >= length) return -1;
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 if (*step == 0) return -1;
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 return 0;
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 }
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
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 */
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22978
diff changeset
117 int defstart, defstop;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22978
diff changeset
118
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22978
diff changeset
119 if (r->step == Py_None) {
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22978
diff changeset
120 *step = 1;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22978
diff changeset
121 } else {
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22978
diff changeset
122 *step = PyInt_AsLong(r->step);
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22978
diff changeset
123 if (*step == -1 && PyErr_Occurred()) {
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22978
diff changeset
124 return -1;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22978
diff changeset
125 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22978
diff changeset
126 else if (*step == 0) {
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22978
diff changeset
127 PyErr_SetString(PyExc_ValueError,
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22978
diff changeset
128 "slice step cannot be zero");
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22978
diff changeset
129 return -1;
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
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22978
diff changeset
133 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
134 defstop = *step < 0 ? -1 : length;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22978
diff changeset
135
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22978
diff changeset
136 if (r->start == Py_None) {
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22978
diff changeset
137 *start = defstart;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22978
diff changeset
138 } else {
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;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22978
diff changeset
148 } else {
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22978
diff changeset
149 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
150 if (*stop < 0) *stop += length;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22978
diff changeset
151 if (*stop < 0) *stop = -1;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22978
diff changeset
152 if (*stop > length) *stop = length;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22978
diff changeset
153 }
23780
c7cda63a007e Fix for problem reported by Neal Norwitz. Tighten up calculation of
Michael W. Hudson <mwh@python.net>
parents: 23778
diff changeset
154
c7cda63a007e Fix for problem reported by Neal Norwitz. Tighten up calculation of
Michael W. Hudson <mwh@python.net>
parents: 23778
diff changeset
155 if ((*stop - *start)*(*step) <= 0) {
c7cda63a007e Fix for problem reported by Neal Norwitz. Tighten up calculation of
Michael W. Hudson <mwh@python.net>
parents: 23778
diff changeset
156 *slicelength = 0;
c7cda63a007e Fix for problem reported by Neal Norwitz. Tighten up calculation of
Michael W. Hudson <mwh@python.net>
parents: 23778
diff changeset
157 }
c7cda63a007e Fix for problem reported by Neal Norwitz. Tighten up calculation of
Michael W. Hudson <mwh@python.net>
parents: 23778
diff changeset
158 else if (*step < 0) {
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22978
diff changeset
159 *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
160 } else {
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;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22978
diff changeset
162 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22978
diff changeset
163
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22978
diff changeset
164 return 0;
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
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23780
diff changeset
167 static PyObject *
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23780
diff changeset
168 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
169 {
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23780
diff changeset
170 PyObject *start, *stop, *step;
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23780
diff changeset
171
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23780
diff changeset
172 start = stop = step = NULL;
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23780
diff changeset
173
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23780
diff changeset
174 if (!PyArg_ParseTuple(args, "O|OO:slice", &start, &stop, &step))
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23780
diff changeset
175 return NULL;
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23780
diff changeset
176
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23780
diff changeset
177 /* 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
178 range(). */
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23780
diff changeset
179 if (stop == NULL) {
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23780
diff changeset
180 stop = start;
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23780
diff changeset
181 start = 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 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
184 }
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23780
diff changeset
185
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23780
diff changeset
186 PyDoc_STRVAR(slice_doc,
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23780
diff changeset
187 "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
188 \n\
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23780
diff changeset
189 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
190
3837
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 static void
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
192 slice_dealloc(PySliceObject *r)
3837
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 {
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 Py_DECREF(r->step);
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 Py_DECREF(r->start);
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 Py_DECREF(r->stop);
22978
363c7fc174ac Remove PyMalloc_New and PyMalloc_Del.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22446
diff changeset
197 PyObject_Del(r);
3837
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 }
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
199
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 static PyObject *
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
201 slice_repr(PySliceObject *r)
3837
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 {
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 PyObject *s, *comma;
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
204
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 s = PyString_FromString("slice(");
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 comma = PyString_FromString(", ");
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 PyString_ConcatAndDel(&s, PyObject_Repr(r->start));
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 PyString_Concat(&s, comma);
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 PyString_ConcatAndDel(&s, PyObject_Repr(r->stop));
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 PyString_Concat(&s, comma);
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 PyString_ConcatAndDel(&s, PyObject_Repr(r->step));
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 PyString_ConcatAndDel(&s, PyString_FromString(")"));
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 Py_DECREF(comma);
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 return s;
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 }
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
216
20012
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
217 static PyMemberDef slice_members[] = {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17080
diff changeset
218 {"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
219 {"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
220 {"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
221 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17080
diff changeset
222 };
3837
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
223
17080
52cb0c5fdf23 SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents: 15563
diff changeset
224 static int
52cb0c5fdf23 SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents: 15563
diff changeset
225 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
226 {
52cb0c5fdf23 SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents: 15563
diff changeset
227 int result = 0;
52cb0c5fdf23 SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents: 15563
diff changeset
228
52cb0c5fdf23 SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents: 15563
diff changeset
229 if (v == w)
52cb0c5fdf23 SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents: 15563
diff changeset
230 return 0;
52cb0c5fdf23 SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents: 15563
diff changeset
231
52cb0c5fdf23 SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents: 15563
diff changeset
232 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
233 return -2;
52cb0c5fdf23 SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents: 15563
diff changeset
234 if (result != 0)
52cb0c5fdf23 SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents: 15563
diff changeset
235 return result;
52cb0c5fdf23 SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents: 15563
diff changeset
236 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
237 return -2;
52cb0c5fdf23 SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents: 15563
diff changeset
238 if (result != 0)
52cb0c5fdf23 SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents: 15563
diff changeset
239 return result;
52cb0c5fdf23 SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents: 15563
diff changeset
240 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
241 return -2;
52cb0c5fdf23 SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents: 15563
diff changeset
242 return result;
52cb0c5fdf23 SF patch #408326 by Robin Thomas: slice objects comparable, not
Guido van Rossum <guido@python.org>
parents: 15563
diff changeset
243 }
3837
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
244
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 PyTypeObject PySlice_Type = {
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 PyObject_HEAD_INIT(&PyType_Type)
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 0, /* Number of items for varobject */
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 "slice", /* Name of this type */
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 sizeof(PySliceObject), /* Basic object size */
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 0, /* Item size for varobject */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17080
diff changeset
251 (destructor)slice_dealloc, /* tp_dealloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17080
diff changeset
252 0, /* tp_print */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17080
diff changeset
253 0, /* tp_getattr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17080
diff changeset
254 0, /* tp_setattr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17080
diff changeset
255 (cmpfunc)slice_compare, /* tp_compare */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17080
diff changeset
256 (reprfunc)slice_repr, /* tp_repr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17080
diff changeset
257 0, /* tp_as_number */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17080
diff changeset
258 0, /* tp_as_sequence */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17080
diff changeset
259 0, /* tp_as_mapping */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17080
diff changeset
260 0, /* tp_hash */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17080
diff changeset
261 0, /* tp_call */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17080
diff changeset
262 0, /* tp_str */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17080
diff changeset
263 PyObject_GenericGetAttr, /* tp_getattro */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17080
diff changeset
264 0, /* tp_setattro */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17080
diff changeset
265 0, /* tp_as_buffer */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17080
diff changeset
266 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
267 slice_doc, /* tp_doc */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17080
diff changeset
268 0, /* tp_traverse */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17080
diff changeset
269 0, /* tp_clear */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17080
diff changeset
270 0, /* tp_richcompare */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17080
diff changeset
271 0, /* tp_weaklistoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17080
diff changeset
272 0, /* tp_iter */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17080
diff changeset
273 0, /* tp_iternext */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17080
diff changeset
274 0, /* tp_methods */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17080
diff changeset
275 slice_members, /* tp_members */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17080
diff changeset
276 0, /* tp_getset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17080
diff changeset
277 0, /* tp_base */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17080
diff changeset
278 0, /* tp_dict */
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23780
diff changeset
279 0, /* tp_descr_get */
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23780
diff changeset
280 0, /* tp_descr_set */
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23780
diff changeset
281 0, /* tp_dictoffset */
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23780
diff changeset
282 0, /* tp_init */
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23780
diff changeset
283 0, /* tp_alloc */
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23780
diff changeset
284 slice_new, /* tp_new */
3837
2b2efa48f28d Added sliceobject.c
Guido van Rossum <guido@python.org>
parents:
diff changeset
285 };