annotate Objects/rangeobject.c @ 17581:c1937faabb23 2.1

Added support for optional explanation parameter to the \versionadded macro. Refactored do_cmd_versionadded() and do_cmd_versionchanged() to do most of the work in a helper function, with the do_cmd_*() wrappers just supplying a portion of the replacement text.
author Fred Drake <fdrake@acm.org>
date Wed, 18 Apr 2001 17:29:14 +0000
parents 7cbe45d79ab5
children aadd99830531
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
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 typedef struct {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
9 PyObject_HEAD
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 long start;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 long step;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 long len;
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
13 int reps;
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
14 long totlen;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 } rangeobject;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
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
17 static int
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
18 long_mul(long i, long j, long *kk)
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
19 {
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
20 PyObject *a;
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
21 PyObject *b;
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
22 PyObject *c;
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
23
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
24 if ((a = PyInt_FromLong(i)) == NULL)
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
25 return 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
26
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
27 if ((b = PyInt_FromLong(j)) == NULL)
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
28 return 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
29
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
30 c = PyNumber_Multiply(a, b);
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
31
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
32 Py_DECREF(a);
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
33 Py_DECREF(b);
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
34
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
35 if (c == NULL)
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
36 return 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
37
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
38 *kk = PyInt_AS_LONG(c);
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
39 Py_DECREF(c);
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
40
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
41 if (*kk > INT_MAX) {
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
42 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
43 "integer multiplication");
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
44 return 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
45 }
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
46 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
47 return 1;
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
48 }
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
49
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
50 PyObject *
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
51 PyRange_New(long start, long len, long step, int reps)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 {
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
53 long totlen = -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
54 rangeobject *obj = PyObject_NEW(rangeobject, &PyRange_Type);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
55
15562
74ee661581b6 Test for NULL returned from PyObject_NEW().
Guido van Rossum <guido@python.org>
parents: 15390
diff changeset
56 if (obj == NULL)
74ee661581b6 Test for NULL returned from PyObject_NEW().
Guido van Rossum <guido@python.org>
parents: 15390
diff changeset
57 return NULL;
74ee661581b6 Test for NULL returned from PyObject_NEW().
Guido van Rossum <guido@python.org>
parents: 15390
diff changeset
58
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
59 if (len == 0 || reps <= 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
60 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
61 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
62 step = 1;
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
63 reps = 1;
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
64 totlen = 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
65 }
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 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
67 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
68 if ((step > 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
69 (last > (PyInt_GetMax() - 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
70 :(last < (-1 - PyInt_GetMax() - 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
71 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
72 "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
73 return NULL;
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 }
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
75 if (! long_mul(len, (long) reps, &totlen)) {
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
76 if(!PyErr_ExceptionMatches(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
77 return NULL;
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 PyErr_Clear();
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 totlen = -1;
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 }
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
81 }
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
82
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 obj->start = start;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 obj->len = len;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 obj->step = step;
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
86 obj->reps = 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
87 obj->totlen = totlen;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
88
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
89 return (PyObject *) obj;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
91
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 static void
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
93 range_dealloc(rangeobject *r)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 {
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 9828
diff changeset
95 PyObject_DEL(r);
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
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
98 static PyObject *
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
99 range_item(rangeobject *r, int i)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 {
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
101 if (i < 0 || i >= r->totlen)
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
102 if (r->totlen!=-1) {
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
103 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
104 "xrange object index out of range");
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
105 return NULL;
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
106 }
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
107
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
108 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
109 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
110
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 static int
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
112 range_length(rangeobject *r)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 {
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
114 if (r->totlen == -1)
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
115 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
116 "xrange object has too many items");
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
117 return r->totlen;
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
118 }
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
119
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
120 static PyObject *
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
121 range_repr(rangeobject *r)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 {
13848
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
123 /* 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
124 * 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
125 */
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
126 char buf1[250];
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
127 char buf2[250];
13836
b368aa8208f3 Remove the tp_print handler.
Fred Drake <fdrake@acm.org>
parents: 13458
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 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
130 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
131
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
132 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
133 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
134 r->start,
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
135 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
136
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
137 else
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
138 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
139 r->start,
a55e4883f886 snprintf() is not portable, so continue to use sprintf() until a portable
Fred Drake <fdrake@acm.org>
parents: 13836
diff changeset
140 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
141 r->step);
13848
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 if (r->reps != 1)
13847
a55e4883f886 snprintf() is not portable, so continue to use sprintf() until a portable
Fred Drake <fdrake@acm.org>
parents: 13836
diff changeset
144 sprintf(buf2, "(%s * %d)", buf1, r->reps);
13848
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
145
13836
b368aa8208f3 Remove the tp_print handler.
Fred Drake <fdrake@acm.org>
parents: 13458
diff changeset
146 return PyString_FromString(r->reps == 1 ? buf1 : buf2);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
148
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
149 static PyObject *
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
150 range_concat(rangeobject *r, PyObject *obj)
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
151 {
9828
80150f9c8c41 Use 'xrange', not 'range', in error messages. Reported by Nathan Sullivan.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
152 PyErr_SetString(PyExc_TypeError, "cannot concatenate xrange objects");
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
153 return NULL;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
154 }
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
155
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
156 static PyObject *
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
157 range_repeat(rangeobject *r, int n)
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
158 {
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
159 long lreps = 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
160
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
161 if (n <= 0)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
162 return (PyObject *) PyRange_New(0, 0, 1, 1);
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
163
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
164 else if (n == 1) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
165 Py_INCREF(r);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
166 return (PyObject *) r;
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
167 }
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
168
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
169 else if (! long_mul((long) r->reps, (long) n, &lreps))
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
170 return NULL;
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
171
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
172 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
173 return (PyObject *) PyRange_New(
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
174 r->start,
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
175 r->len,
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
176 r->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
177 (int) lreps);
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
178 }
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
179
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 static int
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
181 range_compare(rangeobject *r1, rangeobject *r2)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 {
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 if (r1->start != r2->start)
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 return r1->start - r2->start;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
185
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 else if (r1->step != r2->step)
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 return r1->step - r2->step;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
188
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 else if (r1->len != r2->len)
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 return r1->len - r2->len;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
191
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
192 else
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
193 return r1->reps - r2->reps;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
195
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
196 static PyObject *
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
197 range_slice(rangeobject *r, int low, int high)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 {
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
199 if (r->reps != 1) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
200 PyErr_SetString(PyExc_TypeError,
9828
80150f9c8c41 Use 'xrange', not 'range', in error messages. Reported by Nathan Sullivan.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
201 "cannot slice a replicated xrange");
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
202 return NULL;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
203 }
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
204 if (low < 0)
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
205 low = 0;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
206 else if (low > r->len)
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
207 low = r->len;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
208 if (high < 0)
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
209 high = 0;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
210 if (high < low)
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
211 high = low;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
212 else if (high > r->len)
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
213 high = r->len;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
214
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
215 if (low == 0 && high == r->len) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
216 Py_INCREF(r);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
217 return (PyObject *) r;
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
218 }
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
219
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
220 return (PyObject *) PyRange_New(
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
221 low * r->step + r->start,
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
222 high - low,
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
223 r->step,
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
224 1);
1387
f992ac2a38d5 * rangeobject.{c,h}, bltinmodule.c: removed non-essential ops from range
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
225 }
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
226
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
227 static PyObject *
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
228 range_tolist(rangeobject *self, PyObject *args)
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
229 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
230 PyObject *thelist;
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
231 int j;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
232
13836
b368aa8208f3 Remove the tp_print handler.
Fred Drake <fdrake@acm.org>
parents: 13458
diff changeset
233 if (! PyArg_ParseTuple(args, ":tolist"))
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
234 return NULL;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
235
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
236 if (self->totlen == -1)
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
237 return PyErr_NoMemory();
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
238
7cbe45d79ab5 SF patch #103158 by Greg Ball: Don't do unsafe arithmetic in xrange
Guido van Rossum <guido@python.org>
parents: 15562
diff changeset
239 if ((thelist = PyList_New(self->totlen)) == NULL)
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
240 return NULL;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
241
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
242 for (j = 0; j < self->totlen; ++j)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
243 if ((PyList_SetItem(thelist, j, (PyObject *) PyInt_FromLong(
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
244 self->start + (j % self->len) * self->step))) < 0)
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
245 return NULL;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
246
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
247 return thelist;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
248 }
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
249
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
250 static PyObject *
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
251 range_getattr(rangeobject *r, char *name)
1387
f992ac2a38d5 * rangeobject.{c,h}, bltinmodule.c: removed non-essential ops from range
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
252 {
15390
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
253 PyObject *result;
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
254
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
255 static PyMethodDef range_methods[] = {
13836
b368aa8208f3 Remove the tp_print handler.
Fred Drake <fdrake@acm.org>
parents: 13458
diff changeset
256 {"tolist", (PyCFunction)range_tolist, METH_VARARGS,
b368aa8208f3 Remove the tp_print handler.
Fred Drake <fdrake@acm.org>
parents: 13458
diff changeset
257 "tolist() -> list\n"
b368aa8208f3 Remove the tp_print handler.
Fred Drake <fdrake@acm.org>
parents: 13458
diff changeset
258 "Return a list object with the same values."},
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
259 {NULL, NULL}
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
260 };
15390
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
261 static struct memberlist range_members[] = {
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
262 {"step", T_LONG, offsetof(rangeobject, step), RO},
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
263 {"start", T_LONG, offsetof(rangeobject, start), RO},
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
264 {"stop", T_LONG, 0, RO},
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
265 {NULL, 0, 0, 0}
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
266 };
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
267
15390
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
268 result = Py_FindMethod(range_methods, (PyObject *) r, name);
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
269 if (result == NULL) {
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
270 PyErr_Clear();
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
271 if (strcmp("stop", name) == 0)
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
272 result = PyInt_FromLong(r->start + (r->len * r->step));
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
273 else
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
274 result = PyMember_Get((char *)r, range_members, name);
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
275 }
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
276 return result;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
277 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
278
12925
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
279 static int
13458
7908ec855665 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
280 range_contains(rangeobject *r, PyObject *obj)
12925
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
281 {
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
282 long num = PyInt_AsLong(obj);
13848
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
283
12925
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
284 if (num < 0 && PyErr_Occurred())
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
285 return -1;
13848
dd042f5a8fa2 Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 13847
diff changeset
286
15390
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
287 if (r->step > 0) {
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
288 if ((num < r->start) || ((num - r->start) % r->step))
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
289 return 0;
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
290 if (num >= (r->start + (r->len * r->step)))
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
291 return 0;
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
292 }
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
293 else {
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
294 if ((num > r->start) || ((num - r->start) % r->step))
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
295 return 0;
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
296 if (num <= (r->start + (r->len * r->step)))
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
297 return 0;
f62e3e7871be Fixed support for containment test when a negative step is used; this
Fred Drake <fdrake@acm.org>
parents: 15389
diff changeset
298 }
12925
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
299 return 1;
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
300 }
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
301
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
302 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
303 (inquiry)range_length, /*sq_length*/
1908
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
304 (binaryfunc)range_concat, /*sq_concat*/
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
305 (intargfunc)range_repeat, /*sq_repeat*/
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
306 (intargfunc)range_item, /*sq_item*/
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
307 (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
308 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
309 0, /*sq_ass_slice*/
12925
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
310 (objobjproc)range_contains, /*sq_contains*/
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 };
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
312
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
313 PyTypeObject PyRange_Type = {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
314 PyObject_HEAD_INIT(&PyType_Type)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 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
316 "xrange", /* Name of this type */
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 sizeof(rangeobject), /* Basic object size */
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
318 0, /* Item size for varobject */
1908
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
319 (destructor)range_dealloc, /*tp_dealloc*/
13836
b368aa8208f3 Remove the tp_print handler.
Fred Drake <fdrake@acm.org>
parents: 13458
diff changeset
320 0, /*tp_print*/
1908
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
321 (getattrfunc)range_getattr, /*tp_getattr*/
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
322 0, /*tp_setattr*/
1908
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
323 (cmpfunc)range_compare, /*tp_compare*/
13836
b368aa8208f3 Remove the tp_print handler.
Fred Drake <fdrake@acm.org>
parents: 13458
diff changeset
324 (reprfunc)range_repr, /*tp_repr*/
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
325 0, /*tp_as_number*/
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 &range_as_sequence, /*tp_as_sequence*/
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
327 0, /*tp_as_mapping*/
12925
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
328 0, /*tp_hash*/
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
329 0, /*tp_call*/
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
330 0, /*tp_str*/
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
331 0, /*tp_getattro*/
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
332 0, /*tp_setattro*/
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
333 0, /*tp_as_buffer*/
128e7b994505 Thomas Wouters <thomas@xs4all.net>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
334 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
335 };