annotate Objects/rangeobject.c @ 15842:f6cd36498a50 legacy-trunk

Document extensions to .pth files.
author Martin v. Löwis <martin@v.loewis.de>
date Sat, 13 Jan 2001 09:54:41 +0000
parents 74ee661581b6
children 7cbe45d79ab5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
1
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 /* Range object implementation */
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
4 #include "Python.h"
15390
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
5 #include "structmember.h"
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
6 #include <string.h>
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 typedef struct {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
9 PyObject_HEAD
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 long start;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 long step;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 long len;
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
13 int reps;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 } rangeobject;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
15
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
17 PyObject *
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
18 PyRange_New(long start, long len, long step, int reps)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
20 rangeobject *obj = PyObject_NEW(rangeobject, &PyRange_Type);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
21
15562
74ee661581b6 Test for NULL returned from PyObject_NEW().
Guido van Rossum <guido@python.org>
parents: 15390
diff changeset
22 if (obj == NULL)
74ee661581b6 Test for NULL returned from PyObject_NEW().
Guido van Rossum <guido@python.org>
parents: 15390
diff changeset
23 return NULL;
74ee661581b6 Test for NULL returned from PyObject_NEW().
Guido van Rossum <guido@python.org>
parents: 15390
diff changeset
24
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 obj->start = start;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 obj->len = len;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 obj->step = step;
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
28 obj->reps = reps;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
30 return (PyObject *) obj;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 static void
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
34 range_dealloc(rangeobject *r)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 {
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 9828
diff changeset
36 PyObject_DEL(r);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
39 static PyObject *
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
40 range_item(rangeobject *r, int i)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 {
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
42 if (i < 0 || i >= r->len * r->reps) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
43 PyErr_SetString(PyExc_IndexError,
9828
80150f9c8c41 Use 'xrange', not 'range', in error messages. Reported by Nathan Sullivan.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
44 "xrange object index out of range");
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 return NULL;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
47
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
48 return PyInt_FromLong(r->start + (i % r->len) * r->step);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
50
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 static int
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
52 range_length(rangeobject *r)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 {
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
54 return r->len * r->reps;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
55 }
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
56
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
57 static PyObject *
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
58 range_repr(rangeobject *r)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 {
13848
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
60 /* buffers must be big enough to hold 3 longs + an int +
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
61 * a bit of "(xrange(...) * ...)" text.
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
62 */
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
63 char buf1[250];
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
64 char buf2[250];
13836
b368aa8208f3 Remove the tp_print handler.
Fred Drake <fdrake@acm.org>
parents: 13458
diff changeset
65
13848
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
66 if (r->start == 0 && r->step == 1)
13847
a55e4883f886 snprintf() is not portable, so continue to use sprintf() until a portable
Fred Drake <fdrake@acm.org>
parents: 13836
diff changeset
67 sprintf(buf1, "xrange(%ld)", r->start + r->len * r->step);
13848
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
68
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
69 else if (r->step == 1)
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
70 sprintf(buf1, "xrange(%ld, %ld)",
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
71 r->start,
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
72 r->start + r->len * r->step);
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
73
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
74 else
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
75 sprintf(buf1, "xrange(%ld, %ld, %ld)",
13847
a55e4883f886 snprintf() is not portable, so continue to use sprintf() until a portable
Fred Drake <fdrake@acm.org>
parents: 13836
diff changeset
76 r->start,
a55e4883f886 snprintf() is not portable, so continue to use sprintf() until a portable
Fred Drake <fdrake@acm.org>
parents: 13836
diff changeset
77 r->start + r->len * r->step,
a55e4883f886 snprintf() is not portable, so continue to use sprintf() until a portable
Fred Drake <fdrake@acm.org>
parents: 13836
diff changeset
78 r->step);
13848
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
79
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
80 if (r->reps != 1)
13847
a55e4883f886 snprintf() is not portable, so continue to use sprintf() until a portable
Fred Drake <fdrake@acm.org>
parents: 13836
diff changeset
81 sprintf(buf2, "(%s * %d)", buf1, r->reps);
13848
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
82
13836
b368aa8208f3 Remove the tp_print handler.
Fred Drake <fdrake@acm.org>
parents: 13458
diff changeset
83 return PyString_FromString(r->reps == 1 ? buf1 : buf2);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
85
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
86 static PyObject *
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
87 range_concat(rangeobject *r, PyObject *obj)
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
88 {
9828
80150f9c8c41 Use 'xrange', not 'range', in error messages. Reported by Nathan Sullivan.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
89 PyErr_SetString(PyExc_TypeError, "cannot concatenate xrange objects");
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
90 return NULL;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
91 }
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
92
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
93 static PyObject *
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
94 range_repeat(rangeobject *r, int n)
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
95 {
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
96 if (n < 0)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
97 return (PyObject *) PyRange_New(0, 0, 1, 1);
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
98
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
99 else if (n == 1) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
100 Py_INCREF(r);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
101 return (PyObject *) r;
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
102 }
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
103
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
104 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
105 return (PyObject *) PyRange_New(
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
106 r->start,
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
107 r->len,
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
108 r->step,
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
109 r->reps * n);
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
110 }
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
111
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 static int
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
113 range_compare(rangeobject *r1, rangeobject *r2)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 {
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 if (r1->start != r2->start)
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 return r1->start - r2->start;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
117
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 else if (r1->step != r2->step)
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 return r1->step - r2->step;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
120
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 else if (r1->len != r2->len)
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 return r1->len - r2->len;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
123
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
124 else
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
125 return r1->reps - r2->reps;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
127
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
128 static PyObject *
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
129 range_slice(rangeobject *r, int low, int high)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 {
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
131 if (r->reps != 1) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
132 PyErr_SetString(PyExc_TypeError,
9828
80150f9c8c41 Use 'xrange', not 'range', in error messages. Reported by Nathan Sullivan.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
133 "cannot slice a replicated xrange");
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
134 return NULL;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
135 }
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
136 if (low < 0)
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
137 low = 0;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
138 else if (low > r->len)
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
139 low = r->len;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
140 if (high < 0)
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
141 high = 0;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
142 if (high < low)
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
143 high = low;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
144 else if (high > r->len)
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
145 high = r->len;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
146
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
147 if (low == 0 && high == r->len) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
148 Py_INCREF(r);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
149 return (PyObject *) r;
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
150 }
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
151
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
152 return (PyObject *) PyRange_New(
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
153 low * r->step + r->start,
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
154 high - low,
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
155 r->step,
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
156 1);
1387
f992ac2a38d5 * rangeobject.{c,h}, bltinmodule.c: removed non-essential ops from range
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
157 }
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
158
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
159 static PyObject *
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
160 range_tolist(rangeobject *self, PyObject *args)
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
161 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
162 PyObject *thelist;
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
163 int j;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
164 int len = self->len * self->reps;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
165
13836
b368aa8208f3 Remove the tp_print handler.
Fred Drake <fdrake@acm.org>
parents: 13458
diff changeset
166 if (! PyArg_ParseTuple(args, ":tolist"))
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
167 return NULL;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
168
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
169 if ((thelist = PyList_New(len)) == NULL)
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
170 return NULL;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
171
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
172 for (j = 0; j < len; ++j)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
173 if ((PyList_SetItem(thelist, j, (PyObject *) PyInt_FromLong(
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
174 self->start + (j % self->len) * self->step))) < 0)
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
175 return NULL;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
176
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
177 return thelist;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
178 }
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
179
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
180 static PyObject *
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
181 range_getattr(rangeobject *r, char *name)
1387
f992ac2a38d5 * rangeobject.{c,h}, bltinmodule.c: removed non-essential ops from range
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
182 {
15390
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
183 PyObject *result;
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
184
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
185 static PyMethodDef range_methods[] = {
13836
b368aa8208f3 Remove the tp_print handler.
Fred Drake <fdrake@acm.org>
parents: 13458
diff changeset
186 {"tolist", (PyCFunction)range_tolist, METH_VARARGS,
b368aa8208f3 Remove the tp_print handler.
Fred Drake <fdrake@acm.org>
parents: 13458
diff changeset
187 "tolist() -> list\n"
b368aa8208f3 Remove the tp_print handler.
Fred Drake <fdrake@acm.org>
parents: 13458
diff changeset
188 "Return a list object with the same values."},
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
189 {NULL, NULL}
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
190 };
15390
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
191 static struct memberlist range_members[] = {
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
192 {"step", T_LONG, offsetof(rangeobject, step), RO},
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
193 {"start", T_LONG, offsetof(rangeobject, start), RO},
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
194 {"stop", T_LONG, 0, RO},
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
195 {NULL, 0, 0, 0}
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
196 };
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
197
15390
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
198 result = Py_FindMethod(range_methods, (PyObject *) r, name);
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
199 if (result == NULL) {
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
200 PyErr_Clear();
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
201 if (strcmp("stop", name) == 0)
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
202 result = PyInt_FromLong(r->start + (r->len * r->step));
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
203 else
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
204 result = PyMember_Get((char *)r, range_members, name);
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
205 }
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
206 return result;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
208
12925
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
209 static int
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
210 range_contains(rangeobject *r, PyObject *obj)
12925
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
211 {
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
212 long num = PyInt_AsLong(obj);
13848
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
213
12925
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
214 if (num < 0 && PyErr_Occurred())
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
215 return -1;
13848
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
216
15390
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
217 if (r->step > 0) {
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
218 if ((num < r->start) || ((num - r->start) % r->step))
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
219 return 0;
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
220 if (num >= (r->start + (r->len * r->step)))
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
221 return 0;
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
222 }
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
223 else {
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
224 if ((num > r->start) || ((num - r->start) % r->step))
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
225 return 0;
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
226 if (num <= (r->start + (r->len * r->step)))
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
227 return 0;
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
228 }
12925
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
229 return 1;
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
230 }
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
231
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
232 static PySequenceMethods range_as_sequence = {
15390
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
233 (inquiry)range_length, /*sq_length*/
1908
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
234 (binaryfunc)range_concat, /*sq_concat*/
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
235 (intargfunc)range_repeat, /*sq_repeat*/
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
236 (intargfunc)range_item, /*sq_item*/
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
237 (intintargfunc)range_slice, /*sq_slice*/
15390
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
238 0, /*sq_ass_item*/
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
239 0, /*sq_ass_slice*/
12925
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
240 (objobjproc)range_contains, /*sq_contains*/
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 };
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
242
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
243 PyTypeObject PyRange_Type = {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
244 PyObject_HEAD_INIT(&PyType_Type)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 0, /* Number of items for varobject */
1387
f992ac2a38d5 * rangeobject.{c,h}, bltinmodule.c: removed non-essential ops from range
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
246 "xrange", /* Name of this type */
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 sizeof(rangeobject), /* Basic object size */
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 0, /* Item size for varobject */
1908
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
249 (destructor)range_dealloc, /*tp_dealloc*/
13836
b368aa8208f3 Remove the tp_print handler.
Fred Drake <fdrake@acm.org>
parents: 13458
diff changeset
250 0, /*tp_print*/
1908
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
251 (getattrfunc)range_getattr, /*tp_getattr*/
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 0, /*tp_setattr*/
1908
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
253 (cmpfunc)range_compare, /*tp_compare*/
13836
b368aa8208f3 Remove the tp_print handler.
Fred Drake <fdrake@acm.org>
parents: 13458
diff changeset
254 (reprfunc)range_repr, /*tp_repr*/
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
255 0, /*tp_as_number*/
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 &range_as_sequence, /*tp_as_sequence*/
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
257 0, /*tp_as_mapping*/
12925
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
258 0, /*tp_hash*/
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
259 0, /*tp_call*/
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
260 0, /*tp_str*/
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
261 0, /*tp_getattro*/
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
262 0, /*tp_setattro*/
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
263 0, /*tp_as_buffer*/
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
264 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 };