annotate Objects/rangeobject.c @ 14150:302dfd53b3c8 legacy-trunk

Make sure Thomas Wouters has enough to do. ;-) (Some of this is in-progress; this also serves as a reminded to me to check this out once it is written.)
author Fred Drake <fdrake@acm.org>
date Thu, 24 Aug 2000 00:50:27 +0000
parents dd042f5a8fa2
children 6f6f965c5e2d
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 /***********************************************************
13255
916fb024ce94 Change copyright notice.
Guido van Rossum <guido@python.org>
parents: 12925
diff changeset
2 Copyright (c) 2000, BeOpen.com.
916fb024ce94 Change copyright notice.
Guido van Rossum <guido@python.org>
parents: 12925
diff changeset
3 Copyright (c) 1995-2000, Corporation for National Research Initiatives.
916fb024ce94 Change copyright notice.
Guido van Rossum <guido@python.org>
parents: 12925
diff changeset
4 Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
916fb024ce94 Change copyright notice.
Guido van Rossum <guido@python.org>
parents: 12925
diff changeset
5 All rights reserved.
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
13255
916fb024ce94 Change copyright notice.
Guido van Rossum <guido@python.org>
parents: 12925
diff changeset
7 See the file "Misc/COPYRIGHT" for information on usage and
916fb024ce94 Change copyright notice.
Guido van Rossum <guido@python.org>
parents: 12925
diff changeset
8 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 ******************************************************************/
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 /* Range object implementation */
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
13 #include "Python.h"
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
14
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 typedef struct {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
16 PyObject_HEAD
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 long start;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 long step;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 long len;
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
20 int reps;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 } rangeobject;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
22
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
23
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
24 PyObject *
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
25 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
26 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
27 rangeobject *obj = PyObject_NEW(rangeobject, &PyRange_Type);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
28
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 obj->start = start;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 obj->len = len;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 obj->step = step;
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
32 obj->reps = reps;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
34 return (PyObject *) obj;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 static void
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
38 range_dealloc(rangeobject *r)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 {
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 9828
diff changeset
40 PyObject_DEL(r);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
42
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
43 static PyObject *
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
44 range_item(rangeobject *r, int i)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 {
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
46 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
47 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
48 "xrange object index out of range");
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 return NULL;
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
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
52 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
53 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
54
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 static int
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
56 range_length(rangeobject *r)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 {
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
58 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
59 }
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
60
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
61 static PyObject *
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
62 range_repr(rangeobject *r)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 {
13848
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
64 /* 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
65 * 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
66 */
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
67 char buf1[250];
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
68 char buf2[250];
13836
b368aa8208f3 Remove the tp_print handler.
Fred Drake <fdrake@acm.org>
parents: 13458
diff changeset
69
13848
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
70 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
71 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
72
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
73 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
74 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
75 r->start,
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
76 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
77
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
78 else
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
79 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
80 r->start,
a55e4883f886 snprintf() is not portable, so continue to use sprintf() until a portable
Fred Drake <fdrake@acm.org>
parents: 13836
diff changeset
81 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
82 r->step);
13848
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
83
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
84 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
85 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
86
13836
b368aa8208f3 Remove the tp_print handler.
Fred Drake <fdrake@acm.org>
parents: 13458
diff changeset
87 return PyString_FromString(r->reps == 1 ? buf1 : buf2);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
89
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
90 static PyObject *
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
91 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
92 {
9828
80150f9c8c41 Use 'xrange', not 'range', in error messages. Reported by Nathan Sullivan.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
93 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
94 return NULL;
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
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
97 static PyObject *
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
98 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
99 {
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
100 if (n < 0)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
101 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
102
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
103 else if (n == 1) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
104 Py_INCREF(r);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
105 return (PyObject *) r;
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
106 }
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
107
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
108 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
109 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
110 r->start,
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
111 r->len,
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
112 r->step,
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
113 r->reps * n);
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
114 }
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
115
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 static int
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
117 range_compare(rangeobject *r1, rangeobject *r2)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 {
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 if (r1->start != r2->start)
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 return r1->start - r2->start;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
121
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 else if (r1->step != r2->step)
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 return r1->step - r2->step;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
124
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 else if (r1->len != r2->len)
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 return r1->len - r2->len;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
127
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
128 else
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
129 return r1->reps - r2->reps;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
131
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
132 static PyObject *
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
133 range_slice(rangeobject *r, int low, int high)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 {
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
135 if (r->reps != 1) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
136 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
137 "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
138 return NULL;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
139 }
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
140 if (low < 0)
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
141 low = 0;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
142 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
143 low = r->len;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
144 if (high < 0)
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
145 high = 0;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
146 if (high < low)
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
147 high = low;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
148 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
149 high = r->len;
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 if (low == 0 && high == r->len) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
152 Py_INCREF(r);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
153 return (PyObject *) r;
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
154 }
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
155
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
156 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
157 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
158 high - low,
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
159 r->step,
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
160 1);
1387
f992ac2a38d5 * rangeobject.{c,h}, bltinmodule.c: removed non-essential ops from range
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
161 }
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
162
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
163 static PyObject *
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
164 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
165 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
166 PyObject *thelist;
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
167 int j;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
168 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
169
13836
b368aa8208f3 Remove the tp_print handler.
Fred Drake <fdrake@acm.org>
parents: 13458
diff changeset
170 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
171 return NULL;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
172
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
173 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
174 return NULL;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
175
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
176 for (j = 0; j < len; ++j)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
177 if ((PyList_SetItem(thelist, j, (PyObject *) PyInt_FromLong(
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
178 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
179 return NULL;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
180
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
181 return thelist;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
182 }
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
183
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
184 static PyObject *
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
185 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
186 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
187 static PyMethodDef range_methods[] = {
13836
b368aa8208f3 Remove the tp_print handler.
Fred Drake <fdrake@acm.org>
parents: 13458
diff changeset
188 {"tolist", (PyCFunction)range_tolist, METH_VARARGS,
b368aa8208f3 Remove the tp_print handler.
Fred Drake <fdrake@acm.org>
parents: 13458
diff changeset
189 "tolist() -> list\n"
b368aa8208f3 Remove the tp_print handler.
Fred Drake <fdrake@acm.org>
parents: 13458
diff changeset
190 "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
191 {NULL, NULL}
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
192 };
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
193
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
194 return Py_FindMethod(range_methods, (PyObject *) r, name);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
196
12925
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
197 static int
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
198 range_contains(rangeobject *r, PyObject *obj)
12925
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
199 {
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
200 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
201
12925
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
202 if (num < 0 && PyErr_Occurred())
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
203 return -1;
13848
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
204
12925
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
205 if (num < r->start || (num - r->start) % r->step)
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
206 return 0;
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
207 if (num > (r->start + (r->len * r->step)))
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
208 return 0;
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
209 return 1;
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
210 }
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
211
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
212 static PySequenceMethods range_as_sequence = {
1908
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
213 (inquiry)range_length, /*sq_length*/
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
214 (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
215 (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
216 (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
217 (intintargfunc)range_slice, /*sq_slice*/
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 0, /*sq_ass_item*/
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 0, /*sq_ass_slice*/
12925
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
220 (objobjproc)range_contains, /*sq_contains*/
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 };
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
222
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
223 PyTypeObject PyRange_Type = {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
224 PyObject_HEAD_INIT(&PyType_Type)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 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
226 "xrange", /* Name of this type */
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 sizeof(rangeobject), /* Basic object size */
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 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
229 (destructor)range_dealloc, /*tp_dealloc*/
13836
b368aa8208f3 Remove the tp_print handler.
Fred Drake <fdrake@acm.org>
parents: 13458
diff changeset
230 0, /*tp_print*/
1908
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
231 (getattrfunc)range_getattr, /*tp_getattr*/
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 0, /*tp_setattr*/
1908
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
233 (cmpfunc)range_compare, /*tp_compare*/
13836
b368aa8208f3 Remove the tp_print handler.
Fred Drake <fdrake@acm.org>
parents: 13458
diff changeset
234 (reprfunc)range_repr, /*tp_repr*/
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 0, /*tp_as_number*/
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 &range_as_sequence, /*tp_as_sequence*/
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 0, /*tp_as_mapping*/
12925
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
238 0, /*tp_hash*/
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
239 0, /*tp_call*/
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
240 0, /*tp_str*/
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
241 0, /*tp_getattro*/
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
242 0, /*tp_setattro*/
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
243 0, /*tp_as_buffer*/
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
244 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 };