Mercurial > cpython-lru_cache-weakref
annotate Objects/rangeobject.c @ 23364:bec157dbee2e legacy-trunk
Patch #551008: DL_IMPORT PyBool_FromLong.
| author | Martin v. Löwis <martin@v.loewis.de> |
|---|---|
| date | Thu, 02 May 2002 20:23:27 +0000 |
| parents | aee8f16f2774 |
| children | 956d038b1dd2 |
| 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" |
|
18436
9653272f036f
Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents:
18391
diff
changeset
|
5 |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 typedef struct { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
7 PyObject_HEAD |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8 long start; |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
9 long step; |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 long len; |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 } rangeobject; |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 |
|
18391
aadd99830531
Rip out the fancy behaviors of xrange that nobody uses: repeat, slice,
Guido van Rossum <guido@python.org>
parents:
15890
diff
changeset
|
13 PyObject * |
|
18436
9653272f036f
Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents:
18391
diff
changeset
|
14 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
|
15 { |
|
23361
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
16 rangeobject *obj; |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 |
|
23361
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
18 if (reps != 1) { |
|
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
19 PyErr_SetString(PyExc_ValueError, |
|
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
20 "PyRange_New's 'repetitions' argument must be 1"); |
|
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
21 return NULL; |
|
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
22 } |
|
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
23 |
|
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
24 obj = PyObject_New(rangeobject, &PyRange_Type); |
|
15562
74ee661581b6
Test for NULL returned from PyObject_NEW().
Guido van Rossum <guido@python.org>
parents:
15390
diff
changeset
|
25 if (obj == NULL) |
|
74ee661581b6
Test for NULL returned from PyObject_NEW().
Guido van Rossum <guido@python.org>
parents:
15390
diff
changeset
|
26 return NULL; |
|
74ee661581b6
Test for NULL returned from PyObject_NEW().
Guido van Rossum <guido@python.org>
parents:
15390
diff
changeset
|
27 |
|
23361
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
28 if (len == 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
|
29 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
|
30 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
|
31 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
|
32 } |
|
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 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
|
34 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
|
35 if ((step > 0) ? |
|
18436
9653272f036f
Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents:
18391
diff
changeset
|
36 (last > (PyInt_GetMax() - step)) : |
|
9653272f036f
Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents:
18391
diff
changeset
|
37 (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
|
38 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
|
39 "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
|
40 return NULL; |
|
18436
9653272f036f
Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents:
18391
diff
changeset
|
41 } |
|
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
|
42 } |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
43 obj->start = start; |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
44 obj->len = len; |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
45 obj->step = step; |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
46 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
47 return (PyObject *) obj; |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
48 } |
|
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 static PyObject * |
|
13458
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
51 range_item(rangeobject *r, int i) |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
52 { |
|
23361
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
53 if (i < 0 || i >= r->len) { |
|
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
54 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
|
55 "xrange object index out of range"); |
|
23361
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
56 return NULL; |
|
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
57 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
58 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
|
59 } |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
60 |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
61 static int |
|
13458
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
62 range_length(rangeobject *r) |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
63 { |
|
23361
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
64 return r->len; |
|
1444
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
65 } |
|
f2d04054c934
* rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents:
1387
diff
changeset
|
66 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
67 static PyObject * |
|
13458
7908ec855665
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
68 range_repr(rangeobject *r) |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 { |
|
19476
0e70df98424e
repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents:
19301
diff
changeset
|
70 PyObject *rtn; |
|
0e70df98424e
repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents:
19301
diff
changeset
|
71 |
|
13848
dd042f5a8fa2
Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents:
13847
diff
changeset
|
72 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
|
73 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
|
74 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
|
75 |
|
dd042f5a8fa2
Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents:
13847
diff
changeset
|
76 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
|
77 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
|
78 r->start, |
|
0e70df98424e
repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents:
19301
diff
changeset
|
79 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
|
80 |
|
dd042f5a8fa2
Boost buffer sizes in the absence of snprintf on Windows.
Tim Peters <tim.peters@gmail.com>
parents:
13847
diff
changeset
|
81 else |
|
19476
0e70df98424e
repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents:
19301
diff
changeset
|
82 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
|
83 r->start, |
|
0e70df98424e
repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents:
19301
diff
changeset
|
84 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
|
85 r->step); |
|
0e70df98424e
repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents:
19301
diff
changeset
|
86 return rtn; |
|
18436
9653272f036f
Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents:
18391
diff
changeset
|
87 } |
|
9653272f036f
Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents:
18391
diff
changeset
|
88 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
89 static PySequenceMethods range_as_sequence = { |
|
23361
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
90 (inquiry)range_length, /* sq_length */ |
|
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
91 0, /* sq_concat */ |
|
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
92 0, /* sq_repeat */ |
|
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
93 (intargfunc)range_item, /* sq_item */ |
|
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
94 0, /* sq_slice */ |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
95 }; |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
96 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
97 PyTypeObject PyRange_Type = { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4588
diff
changeset
|
98 PyObject_HEAD_INIT(&PyType_Type) |
|
23361
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
99 0, /* Number of items for varobject */ |
|
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
100 "xrange", /* Name of this type */ |
|
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
101 sizeof(rangeobject), /* Basic object size */ |
|
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
102 0, /* Item size for varobject */ |
|
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
103 (destructor)PyObject_Del, /* tp_dealloc */ |
|
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
104 0, /* tp_print */ |
|
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
105 0, /* tp_getattr */ |
|
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
106 0, /* tp_setattr */ |
|
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
107 0, /* tp_compare */ |
|
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
108 (reprfunc)range_repr, /* tp_repr */ |
|
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
109 0, /* tp_as_number */ |
|
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
110 &range_as_sequence, /* tp_as_sequence */ |
|
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
111 0, /* tp_as_mapping */ |
|
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
112 0, /* tp_hash */ |
|
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
113 0, /* tp_call */ |
|
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
114 0, /* tp_str */ |
|
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
115 0, /* tp_getattro */ |
|
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
116 0, /* tp_setattro */ |
|
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
117 0, /* tp_as_buffer */ |
|
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
118 Py_TPFLAGS_DEFAULT, /* tp_flags */ |
|
aee8f16f2774
Remove old deprecated features from the xrange object.
Fred Drake <fdrake@acm.org>
parents:
23348
diff
changeset
|
119 0, /* tp_doc */ |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
120 }; |
