Mercurial > cpython-lru_cache-weakref
annotate Objects/rangeobject.c @ 15164:8930ec3bc00a legacy-trunk
Added additional test cases for pulldom modifications.
| author | Lars Gustäbel <lars@gustaebel.de> |
|---|---|
| date | Wed, 11 Oct 2000 22:36:00 +0000 |
| parents | 6f6f965c5e2d |
| children | 12fee7fb019b 87a5bae1b944 |
| 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" |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 typedef struct { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
7 PyObject_HEAD |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8 long start; |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
9 long step; |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 long len; |
|
1444
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
11 int reps; |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 } rangeobject; |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
15 PyObject * |
|
13458
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
16 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
|
17 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
18 rangeobject *obj = PyObject_NEW(rangeobject, &PyRange_Type); |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 obj->start = start; |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
21 obj->len = len; |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 obj->step = step; |
|
1444
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
23 obj->reps = reps; |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
25 return (PyObject *) obj; |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 } |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
27 |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 static void |
|
13458
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
29 range_dealloc(rangeobject *r) |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
30 { |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
9828
diff
changeset
|
31 PyObject_DEL(r); |
|
1369
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 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
34 static PyObject * |
|
13458
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
35 range_item(rangeobject *r, int i) |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
36 { |
|
1444
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
37 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
|
38 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
|
39 "xrange object index out of range"); |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
40 return NULL; |
|
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 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
|
44 } |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
45 |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
46 static int |
|
13458
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
47 range_length(rangeobject *r) |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
48 { |
|
1444
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
49 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
|
50 } |
|
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
51 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
52 static PyObject * |
|
13458
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
53 range_repr(rangeobject *r) |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
54 { |
|
13848
dd042f5a8fa2
Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents:
13847
diff
changeset
|
55 /* 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
|
56 * 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
|
57 */ |
|
dd042f5a8fa2
Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents:
13847
diff
changeset
|
58 char buf1[250]; |
|
dd042f5a8fa2
Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents:
13847
diff
changeset
|
59 char buf2[250]; |
| 13836 | 60 |
|
13848
dd042f5a8fa2
Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents:
13847
diff
changeset
|
61 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
|
62 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
|
63 |
|
dd042f5a8fa2
Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents:
13847
diff
changeset
|
64 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
|
65 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
|
66 r->start, |
|
dd042f5a8fa2
Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents:
13847
diff
changeset
|
67 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
|
68 |
|
dd042f5a8fa2
Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents:
13847
diff
changeset
|
69 else |
|
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, %ld)", |
|
13847
a55e4883f886
snprintf() is not portable, so continue to use sprintf() until a portable
Fred Drake <fdrake@acm.org>
parents:
13836
diff
changeset
|
71 r->start, |
|
a55e4883f886
snprintf() is not portable, so continue to use sprintf() until a portable
Fred Drake <fdrake@acm.org>
parents:
13836
diff
changeset
|
72 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
|
73 r->step); |
|
13848
dd042f5a8fa2
Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents:
13847
diff
changeset
|
74 |
|
dd042f5a8fa2
Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents:
13847
diff
changeset
|
75 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
|
76 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
|
77 |
| 13836 | 78 return PyString_FromString(r->reps == 1 ? buf1 : buf2); |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
79 } |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
81 static PyObject * |
|
13458
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
82 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
|
83 { |
|
9828
80150f9c8c41
Use 'xrange', not 'range', in error messages. Reported by Nathan Sullivan.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
84 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
|
85 return NULL; |
|
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
86 } |
|
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
87 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
88 static PyObject * |
|
13458
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
89 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
|
90 { |
|
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
91 if (n < 0) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
92 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
|
93 |
|
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
94 else if (n == 1) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
95 Py_INCREF(r); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
96 return (PyObject *) r; |
|
1444
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
97 } |
|
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 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
100 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
|
101 r->start, |
|
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
102 r->len, |
|
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
103 r->step, |
|
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
104 r->reps * n); |
|
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
105 } |
|
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
106 |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
107 static int |
|
13458
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
108 range_compare(rangeobject *r1, rangeobject *r2) |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
109 { |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
110 if (r1->start != r2->start) |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
111 return r1->start - r2->start; |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
112 |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
113 else if (r1->step != r2->step) |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
114 return r1->step - r2->step; |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
115 |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 else if (r1->len != r2->len) |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 return r1->len - r2->len; |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
118 |
|
1444
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
119 else |
|
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
120 return r1->reps - r2->reps; |
|
1369
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 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
123 static PyObject * |
|
13458
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
124 range_slice(rangeobject *r, int low, int high) |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
125 { |
|
1444
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
126 if (r->reps != 1) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
127 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
|
128 "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
|
129 return NULL; |
|
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
130 } |
|
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
131 if (low < 0) |
|
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
132 low = 0; |
|
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
133 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
|
134 low = r->len; |
|
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
135 if (high < 0) |
|
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
136 high = 0; |
|
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
137 if (high < low) |
|
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
138 high = low; |
|
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
139 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
|
140 high = r->len; |
|
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
141 |
|
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
142 if (low == 0 && high == r->len) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
143 Py_INCREF(r); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
144 return (PyObject *) r; |
|
1444
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
145 } |
|
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
146 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
147 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
|
148 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
|
149 high - low, |
|
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
150 r->step, |
|
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
151 1); |
|
1387
f992ac2a38d5
* rangeobject.{c,h}, bltinmodule.c: removed non-essential ops from range
Guido van Rossum <guido@python.org>
parents:
1369
diff
changeset
|
152 } |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
153 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
154 static PyObject * |
|
13458
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
155 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
|
156 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
157 PyObject *thelist; |
|
1444
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
158 int j; |
|
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
159 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
|
160 |
| 13836 | 161 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
|
162 return NULL; |
|
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
163 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
164 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
|
165 return NULL; |
|
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
166 |
|
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
167 for (j = 0; j < len; ++j) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
168 if ((PyList_SetItem(thelist, j, (PyObject *) PyInt_FromLong( |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
169 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
|
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 return thelist; |
|
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
173 } |
|
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
174 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
175 static PyObject * |
|
13458
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
176 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
|
177 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
178 static PyMethodDef range_methods[] = { |
| 13836 | 179 {"tolist", (PyCFunction)range_tolist, METH_VARARGS, |
| 180 "tolist() -> list\n" | |
| 181 "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
|
182 {NULL, NULL} |
|
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
183 }; |
|
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
184 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
185 return Py_FindMethod(range_methods, (PyObject *) r, name); |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
186 } |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
187 |
|
12925
128e7b994505
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
188 static int |
|
13458
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
189 range_contains(rangeobject *r, PyObject *obj) |
|
12925
128e7b994505
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
190 { |
|
128e7b994505
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
191 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
|
192 |
|
12925
128e7b994505
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
193 if (num < 0 && PyErr_Occurred()) |
|
128e7b994505
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
194 return -1; |
|
13848
dd042f5a8fa2
Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents:
13847
diff
changeset
|
195 |
|
12925
128e7b994505
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
196 if (num < r->start || (num - r->start) % r->step) |
|
128e7b994505
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
197 return 0; |
|
128e7b994505
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
198 if (num > (r->start + (r->len * r->step))) |
|
128e7b994505
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
199 return 0; |
|
128e7b994505
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
200 return 1; |
|
128e7b994505
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
201 } |
|
128e7b994505
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
202 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
203 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
|
204 (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
|
205 (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
|
206 (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
|
207 (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
|
208 (intintargfunc)range_slice, /*sq_slice*/ |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
209 0, /*sq_ass_item*/ |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
210 0, /*sq_ass_slice*/ |
|
12925
128e7b994505
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
211 (objobjproc)range_contains, /*sq_contains*/ |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
212 }; |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
213 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
214 PyTypeObject PyRange_Type = { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
215 PyObject_HEAD_INIT(&PyType_Type) |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
216 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
|
217 "xrange", /* Name of this type */ |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
218 sizeof(rangeobject), /* Basic object size */ |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
219 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
|
220 (destructor)range_dealloc, /*tp_dealloc*/ |
| 13836 | 221 0, /*tp_print*/ |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1444
diff
changeset
|
222 (getattrfunc)range_getattr, /*tp_getattr*/ |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
223 0, /*tp_setattr*/ |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1444
diff
changeset
|
224 (cmpfunc)range_compare, /*tp_compare*/ |
| 13836 | 225 (reprfunc)range_repr, /*tp_repr*/ |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
226 0, /*tp_as_number*/ |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
227 &range_as_sequence, /*tp_as_sequence*/ |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
228 0, /*tp_as_mapping*/ |
|
12925
128e7b994505
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
229 0, /*tp_hash*/ |
|
128e7b994505
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
230 0, /*tp_call*/ |
|
128e7b994505
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
231 0, /*tp_str*/ |
|
128e7b994505
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
232 0, /*tp_getattro*/ |
|
128e7b994505
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
233 0, /*tp_setattro*/ |
|
128e7b994505
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
234 0, /*tp_as_buffer*/ |
|
128e7b994505
Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
235 Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
236 }; |
