annotate Objects/rangeobject.c @ 22194:fc6dc2ea25cf legacy-trunk

Fix leak of NotImplemented in previous checkin to PyNumber_Add(). If result == Py_NotImplemented, always DECREF it before assigning a new value to result.
author Jeremy Hylton <jeremy@alum.mit.edu>
date Fri, 08 Mar 2002 21:28:54 +0000
parents 070d392042e3
children 1c9ce34d529d d5aad6c581c9
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
21189
a623f9cf9535 long_mul(): The PyNumber_Multiply() call can return a long if the
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
41 if (!PyInt_Check(c)) {
a623f9cf9535 long_mul(): The PyNumber_Multiply() call can return a long if the
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
42 Py_DECREF(c);
a623f9cf9535 long_mul(): The PyNumber_Multiply() call can return a long if the
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
43 goto overflow;
a623f9cf9535 long_mul(): The PyNumber_Multiply() call can return a long if the
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
44 }
a623f9cf9535 long_mul(): The PyNumber_Multiply() call can return a long if the
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
45
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
46 *kk = PyInt_AS_LONG(c);
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
47 Py_DECREF(c);
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 if (*kk > INT_MAX) {
21189
a623f9cf9535 long_mul(): The PyNumber_Multiply() call can return a long if the
Guido van Rossum <guido@python.org>
parents: 19476
diff changeset
50 overflow:
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
51 PyErr_SetString(PyExc_OverflowError,
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
52 "integer multiplication");
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
53 return 0;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
54 }
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
55 else
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
56 return 1;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
57 }
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
58
18391
aadd99830531 Rip out the fancy behaviors of xrange that nobody uses: repeat, slice,
Guido van Rossum <guido@python.org>
parents: 15890
diff changeset
59 PyObject *
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
60 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
61 {
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
62 long totlen = -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
63 rangeobject *obj = PyObject_NEW(rangeobject, &PyRange_Type);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
64
15562
74ee661581b6 Test for NULL returned from PyObject_NEW().
Guido van Rossum <guido@python.org>
parents: 15390
diff changeset
65 if (obj == NULL)
74ee661581b6 Test for NULL returned from PyObject_NEW().
Guido van Rossum <guido@python.org>
parents: 15390
diff changeset
66 return NULL;
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
67
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
68 if (reps != 1)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
69 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
70
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
71 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
72 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
73 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
74 step = 1;
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
75 reps = 1;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
76 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
77 }
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 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
79 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
80 if ((step > 0) ?
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
81 (last > (PyInt_GetMax() - step)) :
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
82 (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
83 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
84 "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
85 return NULL;
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
86 }
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
87 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
88 if(!PyErr_ExceptionMatches(PyExc_OverflowError))
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
89 return NULL;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
90 PyErr_Clear();
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
91 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
92 }
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
93 }
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
94
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 obj->start = start;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 obj->len = len;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 obj->step = step;
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
98 obj->reps = reps;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
99 obj->totlen = totlen;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
100
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
101 return (PyObject *) obj;
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
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 static void
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
105 range_dealloc(rangeobject *r)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 {
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 9828
diff changeset
107 PyObject_DEL(r);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
109
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
110 static PyObject *
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
111 range_item(rangeobject *r, int i)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 {
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
113 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
114 if (r->totlen!=-1) {
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
115 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
116 "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
117 return NULL;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
118 }
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
119
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
120 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
121 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
122
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 static int
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
124 range_length(rangeobject *r)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 {
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
126 if (r->totlen == -1)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
127 PyErr_SetString(PyExc_OverflowError,
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
128 "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
129 return r->totlen;
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
130 }
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
131
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
132 static PyObject *
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
133 range_repr(rangeobject *r)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 {
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
135 PyObject *rtn;
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
136
13848
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
137 if (r->start == 0 && r->step == 1)
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
138 rtn = PyString_FromFormat("xrange(%ld)",
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
139 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
140
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
141 else if (r->step == 1)
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
142 rtn = PyString_FromFormat("xrange(%ld, %ld)",
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
143 r->start,
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
144 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
145
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
146 else
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
147 rtn = PyString_FromFormat("xrange(%ld, %ld, %ld)",
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
148 r->start,
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
149 r->start + r->len * r->step,
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
150 r->step);
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
151 if (r->reps != 1) {
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
152 PyObject *extra = PyString_FromFormat(
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
153 "(%s * %d)",
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
154 PyString_AS_STRING(rtn), r->reps);
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
155 Py_DECREF(rtn);
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
156 rtn = extra;
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
157 }
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19301
diff changeset
158 return rtn;
18436
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
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
161 static PyObject *
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
162 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
163 {
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
164 long lreps = 0;
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 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
167 "convert to list instead");
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
168
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
169 if (n <= 0)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
170 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
171
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
172 else if (n == 1) {
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
173 Py_INCREF(r);
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
174 return (PyObject *) r;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
175 }
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
176
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
177 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
178 return NULL;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
179
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
180 else
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
181 return (PyObject *) PyRange_New(
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
182 r->start,
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
183 r->len,
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
184 r->step,
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
185 (int) lreps);
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
186 }
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
187
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
188 static int
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
189 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
190 {
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
191
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
192 if (PyErr_Warn(PyExc_DeprecationWarning,
21833
070d392042e3 Fix spelling mistakes. Bugfix candidates.
Neal Norwitz <nnorwitz@gmail.com>
parents: 21189
diff changeset
193 "xrange object comparison is deprecated; "
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
194 "convert to list instead") < 0)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
195 return -1;
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 if (r1->start != r2->start)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
198 return r1->start - r2->start;
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 if (r1->step != r2->step)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
201 return r1->step - r2->step;
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 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
204 return r1->len - r2->len;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
205
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
206 else
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
207 return r1->reps - r2->reps;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
208 }
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 static PyObject *
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
211 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
212 {
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
213 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
214 "convert to list instead");
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
215
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
216 if (r->reps != 1) {
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
217 PyErr_SetString(PyExc_TypeError,
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
218 "cannot slice a replicated xrange");
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
219 return NULL;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
220 }
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
221 if (low < 0)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
222 low = 0;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
223 else if (low > r->len)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
224 low = r->len;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
225 if (high < 0)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
226 high = 0;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
227 if (high < low)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
228 high = low;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
229 else if (high > r->len)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
230 high = r->len;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
231
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
232 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
233 Py_INCREF(r);
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
234 return (PyObject *) r;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
235 }
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 return (PyObject *) PyRange_New(
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
238 low * r->step + r->start,
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
239 high - low,
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
240 r->step,
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
241 1);
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
242 }
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 static PyObject *
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
245 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
246 {
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
247 PyObject *thelist;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
248 int j;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
249
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
250 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
251
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
252 if (self->totlen == -1)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
253 return PyErr_NoMemory();
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 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
256 return NULL;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
257
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
258 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
259 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
260 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
261 return NULL;
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 return thelist;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
264 }
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 static PyObject *
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
267 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
268 {
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
269 PyObject *result;
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
270
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
271 static PyMethodDef range_methods[] = {
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
272 {"tolist", (PyCFunction)range_tolist, METH_NOARGS,
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
273 "tolist() -> list\n"
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
274 "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
275 "(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
276 {NULL, NULL}
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
277 };
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
278 static struct memberlist range_members[] = {
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
279 {"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
280 {"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
281 {"stop", T_LONG, 0, RO},
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
282 {NULL, 0, 0, 0}
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
283 };
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
284
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
285 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
286 if (result == NULL) {
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
287 PyErr_Clear();
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
288 if (strcmp("stop", name) == 0)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
289 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
290 else
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
291 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
292 if (result)
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
293 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
294 "attributes are deprecated");
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
295 }
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
296 return result;
12925
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
297 }
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
298
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
299 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
300 (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
301 0, /*sq_concat*/
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
302 (intargfunc)range_repeat, /*sq_repeat*/
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
303 (intargfunc)range_item, /*sq_item*/
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
304 (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
305 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
306 0, /*sq_ass_slice*/
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
307 0, /*sq_contains*/
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 };
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
309
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
310 PyTypeObject PyRange_Type = {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
311 PyObject_HEAD_INIT(&PyType_Type)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
312 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
313 "xrange", /* Name of this type */
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
314 sizeof(rangeobject), /* Basic object size */
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 0, /* Item size for varobject */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
316 (destructor)range_dealloc, /*tp_dealloc*/
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
317 0, /*tp_print*/
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
318 (getattrfunc)range_getattr, /*tp_getattr*/
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
319 0, /*tp_setattr*/
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
320 (cmpfunc)range_compare, /*tp_compare*/
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
321 (reprfunc)range_repr, /*tp_repr*/
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
322 0, /*tp_as_number*/
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
323 &range_as_sequence, /*tp_as_sequence*/
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
324 0, /*tp_as_mapping*/
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
325 0, /*tp_hash*/
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
326 0, /*tp_call*/
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
327 0, /*tp_str*/
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
328 PyObject_GenericGetAttr, /*tp_getattro*/
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
329 0, /*tp_setattro*/
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
330 0, /*tp_as_buffer*/
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
331 Py_TPFLAGS_DEFAULT, /*tp_flags*/
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
332 0, /* tp_doc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
333 0, /* tp_traverse */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
334 0, /* tp_clear */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
335 0, /* tp_richcompare */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
336 0, /* tp_weaklistoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
337 0, /* tp_iter */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
338 0, /* tp_iternext */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
339 0, /* tp_methods */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
340 0, /* tp_members */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
341 0, /* tp_getset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
342 0, /* tp_base */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
343 0, /* tp_dict */
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 };
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
345
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18391
diff changeset
346 #undef WARN