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