annotate Objects/rangeobject.c @ 19283:b865cd5c1925 legacy-trunk

Added comments before recently added/assigned slots in the type object, so the backward compatibility issues will be easier to understand. I only added comments indicating additions and assignments back to Python 2.0.
author Fred Drake <fdrake@acm.org>
date Wed, 15 Aug 2001 18:32:33 +0000
parents 09df3254b49d
children b9701538f827
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
1
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 /* Range object implementation */
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
4 #include "Python.h"
15390
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
5 #include "structmember.h"
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
6 #include <string.h>
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
8 #define WARN(msg) if (PyErr_Warn(PyExc_DeprecationWarning, msg) < 0) \
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
9 return NULL;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
10
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 typedef struct {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
12 PyObject_HEAD
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 long start;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 long step;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 long len;
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
16 int reps;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
17 long totlen;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 } rangeobject;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
20 static int
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
21 long_mul(long i, long j, long *kk)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
22 {
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
23 PyObject *a;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
24 PyObject *b;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
25 PyObject *c;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
26
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
27 if ((a = PyInt_FromLong(i)) == NULL)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
28 return 0;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
29
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
30 if ((b = PyInt_FromLong(j)) == NULL)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
31 return 0;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
32
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
33 c = PyNumber_Multiply(a, b);
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
34
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
35 Py_DECREF(a);
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
36 Py_DECREF(b);
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
37
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
38 if (c == NULL)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
39 return 0;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
40
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
41 *kk = PyInt_AS_LONG(c);
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
42 Py_DECREF(c);
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
43
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
44 if (*kk > INT_MAX) {
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
45 PyErr_SetString(PyExc_OverflowError,
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
46 "integer multiplication");
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
47 return 0;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
48 }
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
49 else
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
50 return 1;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
51 }
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
52
18391
aadd99830531 Rip out the fancy behaviors of xrange that nobody uses: repeat, slice,
Guido van Rossum <guido@python.org>
parents: 15890
diff changeset
53 PyObject *
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
54 PyRange_New(long start, long len, long step, int reps)
15890
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
55 {
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
56 long totlen = -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
57 rangeobject *obj = PyObject_NEW(rangeobject, &PyRange_Type);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
58
15562
74ee661581b6 Test for NULL returned from PyObject_NEW().
Guido van Rossum <guido@python.org>
parents: 15390
diff changeset
59 if (obj == NULL)
74ee661581b6 Test for NULL returned from PyObject_NEW().
Guido van Rossum <guido@python.org>
parents: 15390
diff changeset
60 return NULL;
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
61
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
62 if (reps != 1)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
63 WARN("PyRange_New's 'repetitions' argument is deprecated");
15562
74ee661581b6 Test for NULL returned from PyObject_NEW().
Guido van Rossum <guido@python.org>
parents: 15390
diff changeset
64
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
65 if (len == 0 || reps <= 0) {
15890
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
66 start = 0;
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
67 len = 0;
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
68 step = 1;
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
69 reps = 1;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
70 totlen = 0;
15890
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
71 }
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
72 else {
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
73 long last = start + (len - 1) * step;
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
74 if ((step > 0) ?
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
75 (last > (PyInt_GetMax() - step)) :
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
76 (last < (-1 - PyInt_GetMax() - step))) {
15890
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
77 PyErr_SetString(PyExc_OverflowError,
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
78 "integer addition");
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
79 return NULL;
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
80 }
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
81 if (! long_mul(len, (long) reps, &totlen)) {
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
82 if(!PyErr_ExceptionMatches(PyExc_OverflowError))
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
83 return NULL;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
84 PyErr_Clear();
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
85 totlen = -1;
15890
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
86 }
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
87 }
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
88
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 obj->start = start;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 obj->len = len;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 obj->step = step;
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
92 obj->reps = reps;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
93 obj->totlen = totlen;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
94
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
95 return (PyObject *) obj;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
97
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 static void
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
99 range_dealloc(rangeobject *r)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 {
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 9828
diff changeset
101 PyObject_DEL(r);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
103
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
104 static PyObject *
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
105 range_item(rangeobject *r, int i)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 {
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
107 if (i < 0 || i >= r->totlen)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
108 if (r->totlen!=-1) {
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
109 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
110 "xrange object index out of range");
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
111 return NULL;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
112 }
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
113
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
114 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
115 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
116
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 static int
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
118 range_length(rangeobject *r)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 {
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
120 if (r->totlen == -1)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
121 PyErr_SetString(PyExc_OverflowError,
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
122 "xrange object has too many items");
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
123 return r->totlen;
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
124 }
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
125
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
126 static PyObject *
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
127 range_repr(rangeobject *r)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 {
13848
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
129 /* 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
130 * 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
131 */
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
132 char buf1[250];
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
133 char buf2[250];
13836
b368aa8208f3 Remove the tp_print handler.
Fred Drake <fdrake@acm.org>
parents: 13458
diff changeset
134
13848
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
135 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
136 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
137
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
138 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
139 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
140 r->start,
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
141 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
142
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
143 else
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
144 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
145 r->start,
a55e4883f886 snprintf() is not portable, so continue to use sprintf() until a portable
Fred Drake <fdrake@acm.org>
parents: 13836
diff changeset
146 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
147 r->step);
13848
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
148
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
149 if (r->reps != 1)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
150 sprintf(buf2, "(%s * %d)", buf1, r->reps);
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
151
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
152 return PyString_FromString(r->reps == 1 ? buf1 : buf2);
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
153 }
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
154
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
155 static PyObject *
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
156 range_repeat(rangeobject *r, int n)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
157 {
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
158 long lreps = 0;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
159
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
160 WARN("xrange object multiplication is deprecated; "
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
161 "convert to list instead");
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
162
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
163 if (n <= 0)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
164 return (PyObject *) PyRange_New(0, 0, 1, 1);
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
165
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
166 else if (n == 1) {
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
167 Py_INCREF(r);
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
168 return (PyObject *) r;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
169 }
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
170
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
171 else if (! long_mul((long) r->reps, (long) n, &lreps))
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
172 return NULL;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
173
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
174 else
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
175 return (PyObject *) PyRange_New(
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
176 r->start,
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
177 r->len,
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
178 r->step,
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
179 (int) lreps);
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
180 }
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
181
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
182 static int
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
183 range_compare(rangeobject *r1, rangeobject *r2)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
184 {
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
185
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
186 if (PyErr_Warn(PyExc_DeprecationWarning,
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
187 "xrange object comparision is deprecated; "
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
188 "convert to list instead") < 0)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
189 return -1;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
190
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
191 if (r1->start != r2->start)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
192 return r1->start - r2->start;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
193
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
194 else if (r1->step != r2->step)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
195 return r1->step - r2->step;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
196
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
197 else if (r1->len != r2->len)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
198 return r1->len - r2->len;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
199
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
200 else
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
201 return r1->reps - r2->reps;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
202 }
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
203
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
204 static PyObject *
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
205 range_slice(rangeobject *r, int low, int high)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
206 {
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
207 WARN("xrange object slicing is deprecated; "
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
208 "convert to list instead");
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
209
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
210 if (r->reps != 1) {
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
211 PyErr_SetString(PyExc_TypeError,
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
212 "cannot slice a replicated xrange");
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
213 return NULL;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
214 }
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
215 if (low < 0)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
216 low = 0;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
217 else if (low > r->len)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
218 low = r->len;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
219 if (high < 0)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
220 high = 0;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
221 if (high < low)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
222 high = low;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
223 else if (high > r->len)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
224 high = r->len;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
225
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
226 if (low == 0 && high == r->len) {
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
227 Py_INCREF(r);
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
228 return (PyObject *) r;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
229 }
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
230
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
231 return (PyObject *) PyRange_New(
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
232 low * r->step + r->start,
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
233 high - low,
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
234 r->step,
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
235 1);
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
236 }
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
237
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
238 static PyObject *
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
239 range_tolist(rangeobject *self, PyObject *args)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
240 {
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
241 PyObject *thelist;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
242 int j;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
243
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
244 WARN("xrange.tolist() is deprecated; use list(xrange) instead");
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
245
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
246 if (! PyArg_ParseTuple(args, ":tolist"))
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
247 return NULL;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
248
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
249 if (self->totlen == -1)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
250 return PyErr_NoMemory();
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
251
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
252 if ((thelist = PyList_New(self->totlen)) == NULL)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
253 return NULL;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
254
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
255 for (j = 0; j < self->totlen; ++j)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
256 if ((PyList_SetItem(thelist, j, (PyObject *) PyInt_FromLong(
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
257 self->start + (j % self->len) * self->step))) < 0)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
258 return NULL;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
259
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
260 return thelist;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
261 }
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
262
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
263 static PyObject *
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
264 range_getattr(rangeobject *r, char *name)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
265 {
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
266 PyObject *result;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
267
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
268 static PyMethodDef range_methods[] = {
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
269 {"tolist", (PyCFunction)range_tolist, METH_VARARGS,
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
270 "tolist() -> list\n"
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
271 "Return a list object with the same values.\n"
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
272 "(This method is deprecated; use list() instead.)"},
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
273 {NULL, NULL}
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
274 };
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
275 static struct memberlist range_members[] = {
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
276 {"step", T_LONG, offsetof(rangeobject, step), RO},
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
277 {"start", T_LONG, offsetof(rangeobject, start), RO},
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
278 {"stop", T_LONG, 0, RO},
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
279 {NULL, 0, 0, 0}
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
280 };
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
281
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
282 result = Py_FindMethod(range_methods, (PyObject *) r, name);
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
283 if (result == NULL) {
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
284 PyErr_Clear();
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
285 if (strcmp("stop", name) == 0)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
286 result = PyInt_FromLong(r->start + (r->len * r->step));
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
287 else
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
288 result = PyMember_Get((char *)r, range_members, name);
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
289 if (result)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
290 WARN("xrange object's 'start', 'stop' and 'step' "
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
291 "attributes are deprecated");
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
292 }
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
293 return result;
12925
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
294 }
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
295
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
296 static PySequenceMethods range_as_sequence = {
15390
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
297 (inquiry)range_length, /*sq_length*/
18391
aadd99830531 Rip out the fancy behaviors of xrange that nobody uses: repeat, slice,
Guido van Rossum <guido@python.org>
parents: 15890
diff changeset
298 0, /*sq_concat*/
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
299 (intargfunc)range_repeat, /*sq_repeat*/
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
300 (intargfunc)range_item, /*sq_item*/
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
301 (intintargfunc)range_slice, /*sq_slice*/
15390
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
302 0, /*sq_ass_item*/
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
303 0, /*sq_ass_slice*/
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
304 0, /*sq_contains*/
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
305 };
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
306
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
307 PyTypeObject PyRange_Type = {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
308 PyObject_HEAD_INIT(&PyType_Type)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
309 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
310 "xrange", /* Name of this type */
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 sizeof(rangeobject), /* Basic object size */
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
312 0, /* Item size for varobject */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
313 (destructor)range_dealloc, /*tp_dealloc*/
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
314 0, /*tp_print*/
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
315 (getattrfunc)range_getattr, /*tp_getattr*/
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
316 0, /*tp_setattr*/
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
317 (cmpfunc)range_compare, /*tp_compare*/
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
318 (reprfunc)range_repr, /*tp_repr*/
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
319 0, /*tp_as_number*/
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
320 &range_as_sequence, /*tp_as_sequence*/
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
321 0, /*tp_as_mapping*/
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
322 0, /*tp_hash*/
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
323 0, /*tp_call*/
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
324 0, /*tp_str*/
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
325 PyObject_GenericGetAttr, /*tp_getattro*/
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
326 0, /*tp_setattro*/
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
327 0, /*tp_as_buffer*/
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
328 Py_TPFLAGS_DEFAULT, /*tp_flags*/
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
329 0, /* tp_doc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
330 0, /* tp_traverse */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
331 0, /* tp_clear */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
332 0, /* tp_richcompare */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
333 0, /* tp_weaklistoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
334 0, /* tp_iter */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
335 0, /* tp_iternext */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
336 0, /* tp_methods */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
337 0, /* tp_members */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
338 0, /* tp_getset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
339 0, /* tp_base */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
340 0, /* tp_dict */
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
341 };
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
342
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
343 #undef WARN