Mercurial > cpython-lru_cache-weakref
annotate Objects/tupleobject.c @ 30309:fed6501188da 2.3
Catch locale.error as well. Fixes #788378.
| author | Martin v. Löwis <martin@v.loewis.de> |
|---|---|
| date | Wed, 03 Sep 2003 05:15:20 +0000 |
| parents | c27a64b38762 |
| children | 473f4e2959a8 |
| rev | line source |
|---|---|
| 145 | 1 |
| 6 | 2 /* Tuple object implementation */ |
| 3 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
4 #include "Python.h" |
| 6 | 5 |
|
12439
456869eaa7d5
Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents:
11999
diff
changeset
|
6 /* Speed optimization to avoid frequent malloc/free of small tuples */ |
|
1352
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
7 #ifndef MAXSAVESIZE |
|
12439
456869eaa7d5
Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents:
11999
diff
changeset
|
8 #define MAXSAVESIZE 20 /* Largest tuple to save on free list */ |
|
456869eaa7d5
Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents:
11999
diff
changeset
|
9 #endif |
|
456869eaa7d5
Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents:
11999
diff
changeset
|
10 #ifndef MAXSAVEDTUPLES |
|
456869eaa7d5
Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents:
11999
diff
changeset
|
11 #define MAXSAVEDTUPLES 2000 /* Maximum number of tuples of each size to save */ |
|
1352
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
12 #endif |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
13 |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
14 #if MAXSAVESIZE > 0 |
|
12439
456869eaa7d5
Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents:
11999
diff
changeset
|
15 /* Entries 1 up to MAXSAVESIZE are free lists, entry 0 is the empty |
|
1352
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
16 tuple () of which at most one instance will be allocated. |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
17 */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
18 static PyTupleObject *free_tuples[MAXSAVESIZE]; |
|
12439
456869eaa7d5
Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents:
11999
diff
changeset
|
19 static int num_free_tuples[MAXSAVESIZE]; |
|
1352
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
20 #endif |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
21 #ifdef COUNT_ALLOCS |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
22 int fast_tuple_allocs; |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
23 int tuple_zero_allocs; |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
24 #endif |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
25 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
26 PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
27 PyTuple_New(register int size) |
| 6 | 28 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
29 register PyTupleObject *op; |
| 6 | 30 if (size < 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
31 PyErr_BadInternalCall(); |
| 6 | 32 return NULL; |
| 33 } | |
|
1352
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
34 #if MAXSAVESIZE > 0 |
|
1380
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
35 if (size == 0 && free_tuples[0]) { |
|
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
36 op = free_tuples[0]; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
37 Py_INCREF(op); |
|
1352
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
38 #ifdef COUNT_ALLOCS |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
39 tuple_zero_allocs++; |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
40 #endif |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
41 return (PyObject *) op; |
|
1352
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
42 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
43 if (0 < size && size < MAXSAVESIZE && |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
44 (op = free_tuples[size]) != NULL) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
45 { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
46 free_tuples[size] = (PyTupleObject *) op->ob_item[0]; |
|
12439
456869eaa7d5
Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents:
11999
diff
changeset
|
47 num_free_tuples[size]--; |
|
1352
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
48 #ifdef COUNT_ALLOCS |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
49 fast_tuple_allocs++; |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
50 #endif |
|
24898
a869d2de536f
Call me anal, but there was a particular phrase that was speading to
Guido van Rossum <guido@python.org>
parents:
24683
diff
changeset
|
51 /* Inline PyObject_InitVar */ |
|
9632
ac5345814ba0
When tracing references, reset the type and size of tuples allocated
Guido van Rossum <guido@python.org>
parents:
9484
diff
changeset
|
52 #ifdef Py_TRACE_REFS |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12516
diff
changeset
|
53 op->ob_size = size; |
|
9632
ac5345814ba0
When tracing references, reset the type and size of tuples allocated
Guido van Rossum <guido@python.org>
parents:
9484
diff
changeset
|
54 op->ob_type = &PyTuple_Type; |
|
ac5345814ba0
When tracing references, reset the type and size of tuples allocated
Guido van Rossum <guido@python.org>
parents:
9484
diff
changeset
|
55 #endif |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12516
diff
changeset
|
56 _Py_NewReference((PyObject *)op); |
|
5936
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
57 } |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
58 else |
|
1352
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
59 #endif |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
60 { |
|
11127
9a20038f0b99
Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents:
9632
diff
changeset
|
61 int nbytes = size * sizeof(PyObject *); |
|
9a20038f0b99
Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents:
9632
diff
changeset
|
62 /* Check for overflow */ |
|
9a20038f0b99
Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents:
9632
diff
changeset
|
63 if (nbytes / sizeof(PyObject *) != (size_t)size || |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
18933
diff
changeset
|
64 (nbytes += sizeof(PyTupleObject) - sizeof(PyObject *)) |
|
11127
9a20038f0b99
Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents:
9632
diff
changeset
|
65 <= 0) |
|
9a20038f0b99
Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents:
9632
diff
changeset
|
66 { |
|
9a20038f0b99
Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents:
9632
diff
changeset
|
67 return PyErr_NoMemory(); |
|
9a20038f0b99
Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents:
9632
diff
changeset
|
68 } |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
18933
diff
changeset
|
69 op = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size); |
|
1352
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
70 if (op == NULL) |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
18933
diff
changeset
|
71 return NULL; |
|
1352
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
72 } |
|
23834
8ae26d9be1b4
SF #561244 Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents:
23832
diff
changeset
|
73 memset(op->ob_item, 0, sizeof(*op->ob_item) * size); |
|
1352
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
74 #if MAXSAVESIZE > 0 |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
75 if (size == 0) { |
|
1380
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
76 free_tuples[0] = op; |
|
12439
456869eaa7d5
Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents:
11999
diff
changeset
|
77 ++num_free_tuples[0]; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
78 Py_INCREF(op); /* extra INCREF so that this is never freed */ |
|
1352
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
79 } |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
80 #endif |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
18933
diff
changeset
|
81 _PyObject_GC_TRACK(op); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
82 return (PyObject *) op; |
| 6 | 83 } |
| 84 | |
| 85 int | |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
86 PyTuple_Size(register PyObject *op) |
| 6 | 87 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
88 if (!PyTuple_Check(op)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
89 PyErr_BadInternalCall(); |
| 6 | 90 return -1; |
| 91 } | |
| 92 else | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
93 return ((PyTupleObject *)op)->ob_size; |
| 6 | 94 } |
| 95 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
96 PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
97 PyTuple_GetItem(register PyObject *op, register int i) |
| 6 | 98 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
99 if (!PyTuple_Check(op)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
100 PyErr_BadInternalCall(); |
| 6 | 101 return NULL; |
| 102 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
103 if (i < 0 || i >= ((PyTupleObject *)op) -> ob_size) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
104 PyErr_SetString(PyExc_IndexError, "tuple index out of range"); |
| 6 | 105 return NULL; |
| 106 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
107 return ((PyTupleObject *)op) -> ob_item[i]; |
| 6 | 108 } |
| 109 | |
| 110 int | |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
111 PyTuple_SetItem(register PyObject *op, register int i, PyObject *newitem) |
| 6 | 112 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
113 register PyObject *olditem; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
114 register PyObject **p; |
|
6083
f159044530b1
PyTuple_SetItem should require that the tuple's refcnt is one!
Guido van Rossum <guido@python.org>
parents:
5936
diff
changeset
|
115 if (!PyTuple_Check(op) || op->ob_refcnt != 1) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
116 Py_XDECREF(newitem); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
117 PyErr_BadInternalCall(); |
| 27 | 118 return -1; |
| 6 | 119 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
120 if (i < 0 || i >= ((PyTupleObject *)op) -> ob_size) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
121 Py_XDECREF(newitem); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
122 PyErr_SetString(PyExc_IndexError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
123 "tuple assignment index out of range"); |
| 27 | 124 return -1; |
| 6 | 125 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
126 p = ((PyTupleObject *)op) -> ob_item + i; |
|
2504
425835b88099
a few peephole optimizations
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
127 olditem = *p; |
|
425835b88099
a few peephole optimizations
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
128 *p = newitem; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
129 Py_XDECREF(olditem); |
| 6 | 130 return 0; |
| 131 } | |
| 132 | |
| 133 /* Methods */ | |
| 134 | |
| 135 static void | |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
136 tupledealloc(register PyTupleObject *op) |
| 6 | 137 { |
| 138 register int i; | |
|
12439
456869eaa7d5
Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents:
11999
diff
changeset
|
139 register int len = op->ob_size; |
| 22626 | 140 PyObject_GC_UnTrack(op); |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
141 Py_TRASHCAN_SAFE_BEGIN(op) |
|
12439
456869eaa7d5
Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents:
11999
diff
changeset
|
142 if (len > 0) { |
|
456869eaa7d5
Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents:
11999
diff
changeset
|
143 i = len; |
|
8559
92267838feb9
Slight rearrangement of some code to make it faster, by Vladimir
Guido van Rossum <guido@python.org>
parents:
6083
diff
changeset
|
144 while (--i >= 0) |
|
92267838feb9
Slight rearrangement of some code to make it faster, by Vladimir
Guido van Rossum <guido@python.org>
parents:
6083
diff
changeset
|
145 Py_XDECREF(op->ob_item[i]); |
|
1352
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
146 #if MAXSAVESIZE > 0 |
|
19612
c396afef1aa9
More stuff discovered while writing the simplest of testcases:
Guido van Rossum <guido@python.org>
parents:
19592
diff
changeset
|
147 if (len < MAXSAVESIZE && |
|
c396afef1aa9
More stuff discovered while writing the simplest of testcases:
Guido van Rossum <guido@python.org>
parents:
19592
diff
changeset
|
148 num_free_tuples[len] < MAXSAVEDTUPLES && |
|
c396afef1aa9
More stuff discovered while writing the simplest of testcases:
Guido van Rossum <guido@python.org>
parents:
19592
diff
changeset
|
149 op->ob_type == &PyTuple_Type) |
|
c396afef1aa9
More stuff discovered while writing the simplest of testcases:
Guido van Rossum <guido@python.org>
parents:
19592
diff
changeset
|
150 { |
|
12439
456869eaa7d5
Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents:
11999
diff
changeset
|
151 op->ob_item[0] = (PyObject *) free_tuples[len]; |
|
456869eaa7d5
Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents:
11999
diff
changeset
|
152 num_free_tuples[len]++; |
|
456869eaa7d5
Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents:
11999
diff
changeset
|
153 free_tuples[len] = op; |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
154 goto done; /* return */ |
|
8559
92267838feb9
Slight rearrangement of some code to make it faster, by Vladimir
Guido van Rossum <guido@python.org>
parents:
6083
diff
changeset
|
155 } |
|
1352
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
156 #endif |
|
8559
92267838feb9
Slight rearrangement of some code to make it faster, by Vladimir
Guido van Rossum <guido@python.org>
parents:
6083
diff
changeset
|
157 } |
|
20277
2983b54256c7
Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents:
19872
diff
changeset
|
158 op->ob_type->tp_free((PyObject *)op); |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
159 done: |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
160 Py_TRASHCAN_SAFE_END(op) |
| 6 | 161 } |
| 162 | |
|
288
9b43a983124c
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
258
diff
changeset
|
163 static int |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
164 tupleprint(PyTupleObject *op, FILE *fp, int flags) |
| 6 | 165 { |
| 166 int i; | |
| 167 fprintf(fp, "("); | |
|
288
9b43a983124c
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
258
diff
changeset
|
168 for (i = 0; i < op->ob_size; i++) { |
|
9b43a983124c
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
258
diff
changeset
|
169 if (i > 0) |
| 6 | 170 fprintf(fp, ", "); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
171 if (PyObject_Print(op->ob_item[i], fp, 0) != 0) |
|
288
9b43a983124c
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
258
diff
changeset
|
172 return -1; |
| 6 | 173 } |
| 174 if (op->ob_size == 1) | |
| 175 fprintf(fp, ","); | |
| 176 fprintf(fp, ")"); | |
|
288
9b43a983124c
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
258
diff
changeset
|
177 return 0; |
| 6 | 178 } |
| 179 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
180 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
181 tuplerepr(PyTupleObject *v) |
| 6 | 182 { |
|
18129
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
183 int i, n; |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
184 PyObject *s, *temp; |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
185 PyObject *pieces, *result = NULL; |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
186 |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
187 n = v->ob_size; |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
188 if (n == 0) |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
189 return PyString_FromString("()"); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
190 |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
191 pieces = PyTuple_New(n); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
192 if (pieces == NULL) |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
193 return NULL; |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
194 |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
195 /* Do repr() on each element. */ |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
196 for (i = 0; i < n; ++i) { |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
197 s = PyObject_Repr(v->ob_item[i]); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
198 if (s == NULL) |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
199 goto Done; |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
200 PyTuple_SET_ITEM(pieces, i, s); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
201 } |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
202 |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
203 /* Add "()" decorations to the first and last items. */ |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
204 assert(n > 0); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
205 s = PyString_FromString("("); |
|
18129
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
206 if (s == NULL) |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
207 goto Done; |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
208 temp = PyTuple_GET_ITEM(pieces, 0); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
209 PyString_ConcatAndDel(&s, temp); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
210 PyTuple_SET_ITEM(pieces, 0, s); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
211 if (s == NULL) |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
212 goto Done; |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
213 |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
214 s = PyString_FromString(n == 1 ? ",)" : ")"); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
215 if (s == NULL) |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
216 goto Done; |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
217 temp = PyTuple_GET_ITEM(pieces, n-1); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
218 PyString_ConcatAndDel(&temp, s); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
219 PyTuple_SET_ITEM(pieces, n-1, temp); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
220 if (temp == NULL) |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
221 goto Done; |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
222 |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
223 /* Paste them all together with ", " between. */ |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
224 s = PyString_FromString(", "); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
225 if (s == NULL) |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
226 goto Done; |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
227 result = _PyString_Join(s, pieces); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
228 Py_DECREF(s); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
229 |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
230 Done: |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
231 Py_DECREF(pieces); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17979
diff
changeset
|
232 return result; |
| 6 | 233 } |
| 234 | |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
235 static long |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
236 tuplehash(PyTupleObject *v) |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
237 { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
238 register long x, y; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
239 register int len = v->ob_size; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
240 register PyObject **p; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
241 x = 0x345678L; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
242 p = v->ob_item; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
243 while (--len >= 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
244 y = PyObject_Hash(*p++); |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
245 if (y == -1) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
246 return -1; |
|
4733
3c7812bfa1a4
Better tuple hash function.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
247 x = (1000003*x) ^ y; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
248 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
249 x ^= v->ob_size; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
250 if (x == -1) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
251 x = -2; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
252 return x; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
253 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
254 |
| 6 | 255 static int |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
256 tuplelength(PyTupleObject *a) |
| 6 | 257 { |
| 258 return a->ob_size; | |
| 259 } | |
| 260 | |
|
12516
e6e914d5b207
add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12439
diff
changeset
|
261 static int |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
262 tuplecontains(PyTupleObject *a, PyObject *el) |
|
12516
e6e914d5b207
add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12439
diff
changeset
|
263 { |
|
e6e914d5b207
add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12439
diff
changeset
|
264 int i, cmp; |
|
e6e914d5b207
add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12439
diff
changeset
|
265 |
|
25125
172845c09afe
Micro-optimization for list_contains. Factored double if test
Raymond Hettinger <python@rcn.com>
parents:
24898
diff
changeset
|
266 for (i = 0, cmp = 0 ; cmp == 0 && i < a->ob_size; ++i) |
|
15973
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
267 cmp = PyObject_RichCompareBool(el, PyTuple_GET_ITEM(a, i), |
|
25125
172845c09afe
Micro-optimization for list_contains. Factored double if test
Raymond Hettinger <python@rcn.com>
parents:
24898
diff
changeset
|
268 Py_EQ); |
|
25128
f1c69c32c42b
PyObject_RichCompareBool() already returns -1, 0, or 1, so return its value
Neal Norwitz <nnorwitz@gmail.com>
parents:
25125
diff
changeset
|
269 return cmp; |
|
12516
e6e914d5b207
add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12439
diff
changeset
|
270 } |
|
e6e914d5b207
add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12439
diff
changeset
|
271 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
272 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
273 tupleitem(register PyTupleObject *a, register int i) |
| 6 | 274 { |
| 275 if (i < 0 || i >= a->ob_size) { | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
276 PyErr_SetString(PyExc_IndexError, "tuple index out of range"); |
| 6 | 277 return NULL; |
| 278 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
279 Py_INCREF(a->ob_item[i]); |
| 6 | 280 return a->ob_item[i]; |
| 281 } | |
| 282 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
283 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
284 tupleslice(register PyTupleObject *a, register int ilow, register int ihigh) |
| 6 | 285 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
286 register PyTupleObject *np; |
| 6 | 287 register int i; |
| 288 if (ilow < 0) | |
| 289 ilow = 0; | |
| 290 if (ihigh > a->ob_size) | |
| 291 ihigh = a->ob_size; | |
| 292 if (ihigh < ilow) | |
| 293 ihigh = ilow; | |
|
19872
51363348dcfa
The endless 460020 bug.
Tim Peters <tim.peters@gmail.com>
parents:
19657
diff
changeset
|
294 if (ilow == 0 && ihigh == a->ob_size && PyTuple_CheckExact(a)) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
295 Py_INCREF(a); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
296 return (PyObject *)a; |
| 6 | 297 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
298 np = (PyTupleObject *)PyTuple_New(ihigh - ilow); |
| 6 | 299 if (np == NULL) |
| 300 return NULL; | |
| 301 for (i = ilow; i < ihigh; i++) { | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
302 PyObject *v = a->ob_item[i]; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
303 Py_INCREF(v); |
| 6 | 304 np->ob_item[i - ilow] = v; |
| 305 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
306 return (PyObject *)np; |
| 6 | 307 } |
| 308 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
309 PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
310 PyTuple_GetSlice(PyObject *op, int i, int j) |
|
599
a347933a6d92
New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents:
288
diff
changeset
|
311 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
312 if (op == NULL || !PyTuple_Check(op)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
313 PyErr_BadInternalCall(); |
|
599
a347933a6d92
New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents:
288
diff
changeset
|
314 return NULL; |
|
a347933a6d92
New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents:
288
diff
changeset
|
315 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
316 return tupleslice((PyTupleObject *)op, i, j); |
|
599
a347933a6d92
New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents:
288
diff
changeset
|
317 } |
|
a347933a6d92
New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents:
288
diff
changeset
|
318 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
319 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
320 tupleconcat(register PyTupleObject *a, register PyObject *bb) |
| 6 | 321 { |
| 322 register int size; | |
| 323 register int i; | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
324 PyTupleObject *np; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
325 if (!PyTuple_Check(bb)) { |
|
12816
4272b368bf69
Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
326 PyErr_Format(PyExc_TypeError, |
|
12929
2eef636ba1b6
Michael Hudson <mwh21@cam.ac.uk>:
Marc-André Lemburg <mal@egenix.com>
parents:
12925
diff
changeset
|
327 "can only concatenate tuple (not \"%.200s\") to tuple", |
|
12816
4272b368bf69
Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
328 bb->ob_type->tp_name); |
| 6 | 329 return NULL; |
| 330 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
331 #define b ((PyTupleObject *)bb) |
| 6 | 332 size = a->ob_size + b->ob_size; |
|
25740
70f9026a2b16
Add checks for size overflow on list*n, list+list, tuple+tuple.
Guido van Rossum <guido@python.org>
parents:
25128
diff
changeset
|
333 if (size < 0) |
|
70f9026a2b16
Add checks for size overflow on list*n, list+list, tuple+tuple.
Guido van Rossum <guido@python.org>
parents:
25128
diff
changeset
|
334 return PyErr_NoMemory(); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
335 np = (PyTupleObject *) PyTuple_New(size); |
| 6 | 336 if (np == NULL) { |
|
288
9b43a983124c
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
258
diff
changeset
|
337 return NULL; |
| 6 | 338 } |
| 339 for (i = 0; i < a->ob_size; i++) { | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
340 PyObject *v = a->ob_item[i]; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
341 Py_INCREF(v); |
| 6 | 342 np->ob_item[i] = v; |
| 343 } | |
| 344 for (i = 0; i < b->ob_size; i++) { | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
345 PyObject *v = b->ob_item[i]; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
346 Py_INCREF(v); |
| 6 | 347 np->ob_item[i + a->ob_size] = v; |
| 348 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
349 return (PyObject *)np; |
| 6 | 350 #undef b |
| 351 } | |
| 352 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
353 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
354 tuplerepeat(PyTupleObject *a, int n) |
|
258
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
355 { |
|
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
356 int i, j; |
|
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
357 int size; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
358 PyTupleObject *np; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
359 PyObject **p; |
|
258
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
360 if (n < 0) |
|
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
361 n = 0; |
|
11127
9a20038f0b99
Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents:
9632
diff
changeset
|
362 if (a->ob_size == 0 || n == 1) { |
|
19872
51363348dcfa
The endless 460020 bug.
Tim Peters <tim.peters@gmail.com>
parents:
19657
diff
changeset
|
363 if (PyTuple_CheckExact(a)) { |
|
51363348dcfa
The endless 460020 bug.
Tim Peters <tim.peters@gmail.com>
parents:
19657
diff
changeset
|
364 /* Since tuples are immutable, we can return a shared |
|
51363348dcfa
The endless 460020 bug.
Tim Peters <tim.peters@gmail.com>
parents:
19657
diff
changeset
|
365 copy in this case */ |
|
51363348dcfa
The endless 460020 bug.
Tim Peters <tim.peters@gmail.com>
parents:
19657
diff
changeset
|
366 Py_INCREF(a); |
|
51363348dcfa
The endless 460020 bug.
Tim Peters <tim.peters@gmail.com>
parents:
19657
diff
changeset
|
367 return (PyObject *)a; |
|
51363348dcfa
The endless 460020 bug.
Tim Peters <tim.peters@gmail.com>
parents:
19657
diff
changeset
|
368 } |
|
51363348dcfa
The endless 460020 bug.
Tim Peters <tim.peters@gmail.com>
parents:
19657
diff
changeset
|
369 if (a->ob_size == 0) |
|
51363348dcfa
The endless 460020 bug.
Tim Peters <tim.peters@gmail.com>
parents:
19657
diff
changeset
|
370 return PyTuple_New(0); |
|
258
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
371 } |
|
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
372 size = a->ob_size * n; |
|
11128
a4b7242a4b74
Mark Favas was quick to note that the last checkin divides by zero
Guido van Rossum <guido@python.org>
parents:
11127
diff
changeset
|
373 if (size/a->ob_size != n) |
|
11127
9a20038f0b99
Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents:
9632
diff
changeset
|
374 return PyErr_NoMemory(); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
375 np = (PyTupleObject *) PyTuple_New(size); |
|
258
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
376 if (np == NULL) |
|
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
377 return NULL; |
|
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
378 p = np->ob_item; |
|
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
379 for (i = 0; i < n; i++) { |
|
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
380 for (j = 0; j < a->ob_size; j++) { |
|
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
381 *p = a->ob_item[j]; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
382 Py_INCREF(*p); |
|
258
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
383 p++; |
|
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
384 } |
|
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
385 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
386 return (PyObject *) np; |
|
258
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
387 } |
|
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
388 |
|
12990
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12929
diff
changeset
|
389 static int |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12929
diff
changeset
|
390 tupletraverse(PyTupleObject *o, visitproc visit, void *arg) |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12929
diff
changeset
|
391 { |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12929
diff
changeset
|
392 int i, err; |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12929
diff
changeset
|
393 PyObject *x; |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12929
diff
changeset
|
394 |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12929
diff
changeset
|
395 for (i = o->ob_size; --i >= 0; ) { |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12929
diff
changeset
|
396 x = o->ob_item[i]; |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12929
diff
changeset
|
397 if (x != NULL) { |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12929
diff
changeset
|
398 err = visit(x, arg); |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12929
diff
changeset
|
399 if (err) |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12929
diff
changeset
|
400 return err; |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12929
diff
changeset
|
401 } |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12929
diff
changeset
|
402 } |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12929
diff
changeset
|
403 return 0; |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12929
diff
changeset
|
404 } |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12929
diff
changeset
|
405 |
|
15973
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
406 static PyObject * |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
407 tuplerichcompare(PyObject *v, PyObject *w, int op) |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
408 { |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
409 PyTupleObject *vt, *wt; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
410 int i; |
|
17841
2b5a9bff1b11
Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents:
15973
diff
changeset
|
411 int vlen, wlen; |
|
15973
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
412 |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
413 if (!PyTuple_Check(v) || !PyTuple_Check(w)) { |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
414 Py_INCREF(Py_NotImplemented); |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
415 return Py_NotImplemented; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
416 } |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
417 |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
418 vt = (PyTupleObject *)v; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
419 wt = (PyTupleObject *)w; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
420 |
|
17841
2b5a9bff1b11
Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents:
15973
diff
changeset
|
421 vlen = vt->ob_size; |
|
2b5a9bff1b11
Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents:
15973
diff
changeset
|
422 wlen = wt->ob_size; |
|
15973
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
423 |
|
17841
2b5a9bff1b11
Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents:
15973
diff
changeset
|
424 /* Note: the corresponding code for lists has an "early out" test |
|
2b5a9bff1b11
Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents:
15973
diff
changeset
|
425 * here when op is EQ or NE and the lengths differ. That pays there, |
|
2b5a9bff1b11
Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents:
15973
diff
changeset
|
426 * but Tim was unable to find any real code where EQ/NE tuple |
|
2b5a9bff1b11
Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents:
15973
diff
changeset
|
427 * compares don't have the same length, so testing for it here would |
|
2b5a9bff1b11
Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents:
15973
diff
changeset
|
428 * have cost without benefit. |
|
2b5a9bff1b11
Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents:
15973
diff
changeset
|
429 */ |
|
2b5a9bff1b11
Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents:
15973
diff
changeset
|
430 |
|
2b5a9bff1b11
Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents:
15973
diff
changeset
|
431 /* Search for the first index where items are different. |
|
2b5a9bff1b11
Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents:
15973
diff
changeset
|
432 * Note that because tuples are immutable, it's safe to reuse |
|
2b5a9bff1b11
Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents:
15973
diff
changeset
|
433 * vlen and wlen across the comparison calls. |
|
2b5a9bff1b11
Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents:
15973
diff
changeset
|
434 */ |
|
2b5a9bff1b11
Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents:
15973
diff
changeset
|
435 for (i = 0; i < vlen && i < wlen; i++) { |
|
15973
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
436 int k = PyObject_RichCompareBool(vt->ob_item[i], |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
437 wt->ob_item[i], Py_EQ); |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
438 if (k < 0) |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
439 return NULL; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
440 if (!k) |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
441 break; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
442 } |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
443 |
|
17841
2b5a9bff1b11
Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents:
15973
diff
changeset
|
444 if (i >= vlen || i >= wlen) { |
|
15973
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
445 /* No more items to compare -- compare sizes */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
446 int cmp; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
447 PyObject *res; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
448 switch (op) { |
|
17841
2b5a9bff1b11
Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents:
15973
diff
changeset
|
449 case Py_LT: cmp = vlen < wlen; break; |
|
2b5a9bff1b11
Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents:
15973
diff
changeset
|
450 case Py_LE: cmp = vlen <= wlen; break; |
|
2b5a9bff1b11
Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents:
15973
diff
changeset
|
451 case Py_EQ: cmp = vlen == wlen; break; |
|
2b5a9bff1b11
Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents:
15973
diff
changeset
|
452 case Py_NE: cmp = vlen != wlen; break; |
|
2b5a9bff1b11
Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents:
15973
diff
changeset
|
453 case Py_GT: cmp = vlen > wlen; break; |
|
2b5a9bff1b11
Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents:
15973
diff
changeset
|
454 case Py_GE: cmp = vlen >= wlen; break; |
|
15973
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
455 default: return NULL; /* cannot happen */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
456 } |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
457 if (cmp) |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
458 res = Py_True; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
459 else |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
460 res = Py_False; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
461 Py_INCREF(res); |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
462 return res; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
463 } |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
464 |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
465 /* We have an item that differs -- shortcuts for EQ/NE */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
466 if (op == Py_EQ) { |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
467 Py_INCREF(Py_False); |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
468 return Py_False; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
469 } |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
470 if (op == Py_NE) { |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
471 Py_INCREF(Py_True); |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
472 return Py_True; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
473 } |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
474 |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
475 /* Compare the final item again using the proper operator */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
476 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op); |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
477 } |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
478 |
|
24280
1598a1e473e2
staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24184
diff
changeset
|
479 static PyObject * |
|
19592
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
480 tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
481 |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
482 static PyObject * |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
483 tuple_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
484 { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
485 PyObject *arg = NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
486 static char *kwlist[] = {"sequence", 0}; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
487 |
|
19592
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
488 if (type != &PyTuple_Type) |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
489 return tuple_subtype_new(type, args, kwds); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
490 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:tuple", kwlist, &arg)) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
491 return NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
492 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
493 if (arg == NULL) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
494 return PyTuple_New(0); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
495 else |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
496 return PySequence_Tuple(arg); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
497 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
498 |
|
19592
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
499 static PyObject * |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
500 tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
501 { |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
502 PyObject *tmp, *new, *item; |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
503 int i, n; |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
504 |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
505 assert(PyType_IsSubtype(type, &PyTuple_Type)); |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
506 tmp = tuple_new(&PyTuple_Type, args, kwds); |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
507 if (tmp == NULL) |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
508 return NULL; |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
509 assert(PyTuple_Check(tmp)); |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
510 new = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp)); |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
511 if (new == NULL) |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
512 return NULL; |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
513 for (i = 0; i < n; i++) { |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
514 item = PyTuple_GET_ITEM(tmp, i); |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
515 Py_INCREF(item); |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
516 PyTuple_SET_ITEM(new, i, item); |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
517 } |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
518 Py_DECREF(tmp); |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
519 return new; |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
520 } |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
521 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
522 PyDoc_STRVAR(tuple_doc, |
|
19657
937965516b54
Rewrite the tuple() docstring to parallel the list() docstring.
Tim Peters <tim.peters@gmail.com>
parents:
19656
diff
changeset
|
523 "tuple() -> an empty tuple\n" |
|
937965516b54
Rewrite the tuple() docstring to parallel the list() docstring.
Tim Peters <tim.peters@gmail.com>
parents:
19656
diff
changeset
|
524 "tuple(sequence) -> tuple initialized from sequence's items\n" |
|
937965516b54
Rewrite the tuple() docstring to parallel the list() docstring.
Tim Peters <tim.peters@gmail.com>
parents:
19656
diff
changeset
|
525 "\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
526 "If the argument is a tuple, the return value is the same object."); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
527 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
528 static PySequenceMethods tuple_as_sequence = { |
|
15973
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
529 (inquiry)tuplelength, /* sq_length */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
530 (binaryfunc)tupleconcat, /* sq_concat */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
531 (intargfunc)tuplerepeat, /* sq_repeat */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
532 (intargfunc)tupleitem, /* sq_item */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
533 (intintargfunc)tupleslice, /* sq_slice */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
534 0, /* sq_ass_item */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
535 0, /* sq_ass_slice */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
536 (objobjproc)tuplecontains, /* sq_contains */ |
| 6 | 537 }; |
| 538 | |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
539 static PyObject* |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
540 tuplesubscript(PyTupleObject* self, PyObject* item) |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
541 { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
542 if (PyInt_Check(item)) { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
543 long i = PyInt_AS_LONG(item); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
544 if (i < 0) |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
545 i += PyTuple_GET_SIZE(self); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
546 return tupleitem(self, i); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
547 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
548 else if (PyLong_Check(item)) { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
549 long i = PyLong_AsLong(item); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
550 if (i == -1 && PyErr_Occurred()) |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
551 return NULL; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
552 if (i < 0) |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
553 i += PyTuple_GET_SIZE(self); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
554 return tupleitem(self, i); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
555 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
556 else if (PySlice_Check(item)) { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
557 int start, stop, step, slicelength, cur, i; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
558 PyObject* result; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
559 PyObject* it; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
560 |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
561 if (PySlice_GetIndicesEx((PySliceObject*)item, |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
562 PyTuple_GET_SIZE(self), |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
563 &start, &stop, &step, &slicelength) < 0) { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
564 return NULL; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
565 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
566 |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
567 if (slicelength <= 0) { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
568 return PyTuple_New(0); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
569 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
570 else { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
571 result = PyTuple_New(slicelength); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
572 |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
573 for (cur = start, i = 0; i < slicelength; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
574 cur += step, i++) { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
575 it = PyTuple_GET_ITEM(self, cur); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
576 Py_INCREF(it); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
577 PyTuple_SET_ITEM(result, i, it); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
578 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
579 |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
580 return result; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
581 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
582 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
583 else { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
584 PyErr_SetString(PyExc_TypeError, |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
585 "tuple indices must be integers"); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
586 return NULL; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
587 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
588 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
589 |
|
27521
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
25740
diff
changeset
|
590 static PyObject * |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
25740
diff
changeset
|
591 tuple_getnewargs(PyTupleObject *v) |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
25740
diff
changeset
|
592 { |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
25740
diff
changeset
|
593 return Py_BuildValue("(N)", tupleslice(v, 0, v->ob_size)); |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
25740
diff
changeset
|
594 |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
25740
diff
changeset
|
595 } |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
25740
diff
changeset
|
596 |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
25740
diff
changeset
|
597 static PyMethodDef tuple_methods[] = { |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
25740
diff
changeset
|
598 {"__getnewargs__", (PyCFunction)tuple_getnewargs, METH_NOARGS}, |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
25740
diff
changeset
|
599 {NULL, NULL} /* sentinel */ |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
25740
diff
changeset
|
600 }; |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
25740
diff
changeset
|
601 |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
602 static PyMappingMethods tuple_as_mapping = { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
603 (inquiry)tuplelength, |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
604 (binaryfunc)tuplesubscript, |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
605 0 |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
606 }; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
607 |
|
24683
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
608 static PyObject *tuple_iter(PyObject *seq); |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
609 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
610 PyTypeObject PyTuple_Type = { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
611 PyObject_HEAD_INIT(&PyType_Type) |
| 6 | 612 0, |
| 613 "tuple", | |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
18933
diff
changeset
|
614 sizeof(PyTupleObject) - sizeof(PyObject *), |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
615 sizeof(PyObject *), |
|
15973
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
616 (destructor)tupledealloc, /* tp_dealloc */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
617 (printfunc)tupleprint, /* tp_print */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
618 0, /* tp_getattr */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
619 0, /* tp_setattr */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
620 0, /* tp_compare */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
621 (reprfunc)tuplerepr, /* tp_repr */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
622 0, /* tp_as_number */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
623 &tuple_as_sequence, /* tp_as_sequence */ |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
22979
diff
changeset
|
624 &tuple_as_mapping, /* tp_as_mapping */ |
|
15973
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
625 (hashfunc)tuplehash, /* tp_hash */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
626 0, /* tp_call */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
627 0, /* tp_str */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
628 PyObject_GenericGetAttr, /* tp_getattro */ |
|
15973
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
629 0, /* tp_setattro */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
630 0, /* tp_as_buffer */ |
|
19592
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
631 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
632 Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
633 tuple_doc, /* tp_doc */ |
|
15973
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
634 (traverseproc)tupletraverse, /* tp_traverse */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
635 0, /* tp_clear */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
636 tuplerichcompare, /* tp_richcompare */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
637 0, /* tp_weaklistoffset */ |
|
24683
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
638 tuple_iter, /* tp_iter */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
639 0, /* tp_iternext */ |
|
27521
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
25740
diff
changeset
|
640 tuple_methods, /* tp_methods */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
641 0, /* tp_members */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
642 0, /* tp_getset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
643 0, /* tp_base */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
644 0, /* tp_dict */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
645 0, /* tp_descr_get */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
646 0, /* tp_descr_set */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
647 0, /* tp_dictoffset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
648 0, /* tp_init */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
649 0, /* tp_alloc */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
650 tuple_new, /* tp_new */ |
|
22979
9fe94c01eaf1
PyObject_GC_Del can now be used as a function designator.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22626
diff
changeset
|
651 PyObject_GC_Del, /* tp_free */ |
| 6 | 652 }; |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
653 |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
654 /* The following function breaks the notion that tuples are immutable: |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
655 it changes the size of a tuple. We get away with this only if there |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
656 is only one module referencing the object. You can also think of it |
|
15037
c7d46eae95d8
Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14584
diff
changeset
|
657 as creating a new tuple object and destroying the old one, only more |
|
c7d46eae95d8
Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14584
diff
changeset
|
658 efficiently. In any case, don't use this if the tuple may already be |
|
17976
d579a5d8b3fe
Cruft cleanup: Removed the unused last_is_sticky argument from the internal
Tim Peters <tim.peters@gmail.com>
parents:
17975
diff
changeset
|
659 known to some other part of the code. */ |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
660 |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
661 int |
|
17976
d579a5d8b3fe
Cruft cleanup: Removed the unused last_is_sticky argument from the internal
Tim Peters <tim.peters@gmail.com>
parents:
17975
diff
changeset
|
662 _PyTuple_Resize(PyObject **pv, int newsize) |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
663 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
664 register PyTupleObject *v; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
665 register PyTupleObject *sv; |
|
1380
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
666 int i; |
|
21293
b810c30b4aa1
_PyTuple_Resize(): this dumped core on tuple(globals()) for me. Turns
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
667 int oldsize; |
|
1380
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
668 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
669 v = (PyTupleObject *) *pv; |
|
19612
c396afef1aa9
More stuff discovered while writing the simplest of testcases:
Guido van Rossum <guido@python.org>
parents:
19592
diff
changeset
|
670 if (v == NULL || v->ob_type != &PyTuple_Type || |
|
17975
527d0ffdf099
_PyTuple_Resize: take into account the empty tuple. There can be only one.
Thomas Wouters <thomas@python.org>
parents:
17841
diff
changeset
|
671 (v->ob_size != 0 && v->ob_refcnt != 1)) { |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
672 *pv = 0; |
|
15037
c7d46eae95d8
Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14584
diff
changeset
|
673 Py_XDECREF(v); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
674 PyErr_BadInternalCall(); |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
675 return -1; |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
676 } |
|
21293
b810c30b4aa1
_PyTuple_Resize(): this dumped core on tuple(globals()) for me. Turns
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
677 oldsize = v->ob_size; |
|
b810c30b4aa1
_PyTuple_Resize(): this dumped core on tuple(globals()) for me. Turns
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
678 if (oldsize == newsize) |
|
1380
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
679 return 0; |
|
15037
c7d46eae95d8
Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14584
diff
changeset
|
680 |
|
21293
b810c30b4aa1
_PyTuple_Resize(): this dumped core on tuple(globals()) for me. Turns
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
681 if (oldsize == 0) { |
|
17975
527d0ffdf099
_PyTuple_Resize: take into account the empty tuple. There can be only one.
Thomas Wouters <thomas@python.org>
parents:
17841
diff
changeset
|
682 /* Empty tuples are often shared, so we should never |
|
527d0ffdf099
_PyTuple_Resize: take into account the empty tuple. There can be only one.
Thomas Wouters <thomas@python.org>
parents:
17841
diff
changeset
|
683 resize them in-place even if we do own the only |
|
527d0ffdf099
_PyTuple_Resize: take into account the empty tuple. There can be only one.
Thomas Wouters <thomas@python.org>
parents:
17841
diff
changeset
|
684 (current) reference */ |
|
527d0ffdf099
_PyTuple_Resize: take into account the empty tuple. There can be only one.
Thomas Wouters <thomas@python.org>
parents:
17841
diff
changeset
|
685 Py_DECREF(v); |
|
527d0ffdf099
_PyTuple_Resize: take into account the empty tuple. There can be only one.
Thomas Wouters <thomas@python.org>
parents:
17841
diff
changeset
|
686 *pv = PyTuple_New(newsize); |
|
17979
192d36869445
_PyTuple_Resize: guard against PyTuple_New() returning NULL, using Tim's
Thomas Wouters <thomas@python.org>
parents:
17976
diff
changeset
|
687 return *pv == NULL ? -1 : 0; |
|
17975
527d0ffdf099
_PyTuple_Resize: take into account the empty tuple. There can be only one.
Thomas Wouters <thomas@python.org>
parents:
17841
diff
changeset
|
688 } |
|
527d0ffdf099
_PyTuple_Resize: take into account the empty tuple. There can be only one.
Thomas Wouters <thomas@python.org>
parents:
17841
diff
changeset
|
689 |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
690 /* XXX UNREF/NEWREF interface should be more symmetrical */ |
|
24184
c4dcb80f48ea
object.h special-build macro minefield: renamed all the new lexical
Tim Peters <tim.peters@gmail.com>
parents:
23952
diff
changeset
|
691 _Py_DEC_REFTOTAL; |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
18933
diff
changeset
|
692 _PyObject_GC_UNTRACK(v); |
|
15037
c7d46eae95d8
Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14584
diff
changeset
|
693 _Py_ForgetReference((PyObject *) v); |
|
21293
b810c30b4aa1
_PyTuple_Resize(): this dumped core on tuple(globals()) for me. Turns
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
694 /* DECREF items deleted by shrinkage */ |
|
b810c30b4aa1
_PyTuple_Resize(): this dumped core on tuple(globals()) for me. Turns
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
695 for (i = newsize; i < oldsize; i++) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
696 Py_XDECREF(v->ob_item[i]); |
|
1380
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
697 v->ob_item[i] = NULL; |
|
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
698 } |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
18933
diff
changeset
|
699 sv = PyObject_GC_Resize(PyTupleObject, v, newsize); |
|
15037
c7d46eae95d8
Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14584
diff
changeset
|
700 if (sv == NULL) { |
|
c7d46eae95d8
Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14584
diff
changeset
|
701 *pv = NULL; |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
18933
diff
changeset
|
702 PyObject_GC_Del(v); |
|
15037
c7d46eae95d8
Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14584
diff
changeset
|
703 return -1; |
|
12439
456869eaa7d5
Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents:
11999
diff
changeset
|
704 } |
|
15037
c7d46eae95d8
Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14584
diff
changeset
|
705 _Py_NewReference((PyObject *) sv); |
|
21293
b810c30b4aa1
_PyTuple_Resize(): this dumped core on tuple(globals()) for me. Turns
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
706 /* Zero out items added by growing */ |
|
23834
8ae26d9be1b4
SF #561244 Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents:
23832
diff
changeset
|
707 if (newsize > oldsize) |
|
23952
3834cb0e7be6
Fix for SF bug 571885
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23834
diff
changeset
|
708 memset(&sv->ob_item[oldsize], 0, |
|
3834cb0e7be6
Fix for SF bug 571885
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23834
diff
changeset
|
709 sizeof(*sv->ob_item) * (newsize - oldsize)); |
|
15037
c7d46eae95d8
Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14584
diff
changeset
|
710 *pv = (PyObject *) sv; |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
18933
diff
changeset
|
711 _PyObject_GC_TRACK(sv); |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
712 return 0; |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
713 } |
|
5936
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
714 |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
715 void |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
716 PyTuple_Fini(void) |
|
5936
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
717 { |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
718 #if MAXSAVESIZE > 0 |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
719 int i; |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
720 |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
721 Py_XDECREF(free_tuples[0]); |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
722 free_tuples[0] = NULL; |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
723 |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
724 for (i = 1; i < MAXSAVESIZE; i++) { |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
725 PyTupleObject *p, *q; |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
726 p = free_tuples[i]; |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
727 free_tuples[i] = NULL; |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
728 while (p) { |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
729 q = p; |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
730 p = (PyTupleObject *)(p->ob_item[0]); |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
18933
diff
changeset
|
731 PyObject_GC_Del(q); |
|
5936
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
732 } |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
733 } |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
734 #endif |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
735 } |
|
24683
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
736 |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
737 /*********************** Tuple Iterator **************************/ |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
738 |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
739 typedef struct { |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
740 PyObject_HEAD |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
741 long it_index; |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
742 PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */ |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
743 } tupleiterobject; |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
744 |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
745 PyTypeObject PyTupleIter_Type; |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
746 |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
747 static PyObject * |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
748 tuple_iter(PyObject *seq) |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
749 { |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
750 tupleiterobject *it; |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
751 |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
752 if (!PyTuple_Check(seq)) { |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
753 PyErr_BadInternalCall(); |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
754 return NULL; |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
755 } |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
756 it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type); |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
757 if (it == NULL) |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
758 return NULL; |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
759 it->it_index = 0; |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
760 Py_INCREF(seq); |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
761 it->it_seq = (PyTupleObject *)seq; |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
762 _PyObject_GC_TRACK(it); |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
763 return (PyObject *)it; |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
764 } |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
765 |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
766 static void |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
767 tupleiter_dealloc(tupleiterobject *it) |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
768 { |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
769 _PyObject_GC_UNTRACK(it); |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
770 Py_XDECREF(it->it_seq); |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
771 PyObject_GC_Del(it); |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
772 } |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
773 |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
774 static int |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
775 tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg) |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
776 { |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
777 if (it->it_seq == NULL) |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
778 return 0; |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
779 return visit((PyObject *)it->it_seq, arg); |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
780 } |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
781 |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
782 static PyObject * |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
783 tupleiter_next(tupleiterobject *it) |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
784 { |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
785 PyTupleObject *seq; |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
786 PyObject *item; |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
787 |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
788 assert(it != NULL); |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
789 seq = it->it_seq; |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
790 if (seq == NULL) |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
791 return NULL; |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
792 assert(PyTuple_Check(seq)); |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
793 |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
794 if (it->it_index < PyTuple_GET_SIZE(seq)) { |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
795 item = PyTuple_GET_ITEM(seq, it->it_index); |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
796 ++it->it_index; |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
797 Py_INCREF(item); |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
798 return item; |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
799 } |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
800 |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
801 Py_DECREF(seq); |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
802 it->it_seq = NULL; |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
803 return NULL; |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
804 } |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
805 |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
806 PyTypeObject PyTupleIter_Type = { |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
807 PyObject_HEAD_INIT(&PyType_Type) |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
808 0, /* ob_size */ |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
809 "tupleiterator", /* tp_name */ |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
810 sizeof(tupleiterobject), /* tp_basicsize */ |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
811 0, /* tp_itemsize */ |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
812 /* methods */ |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
813 (destructor)tupleiter_dealloc, /* tp_dealloc */ |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
814 0, /* tp_print */ |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
815 0, /* tp_getattr */ |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
816 0, /* tp_setattr */ |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
817 0, /* tp_compare */ |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
818 0, /* tp_repr */ |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
819 0, /* tp_as_number */ |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
820 0, /* tp_as_sequence */ |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
821 0, /* tp_as_mapping */ |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
822 0, /* tp_hash */ |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
823 0, /* tp_call */ |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
824 0, /* tp_str */ |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
825 PyObject_GenericGetAttr, /* tp_getattro */ |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
826 0, /* tp_setattro */ |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
827 0, /* tp_as_buffer */ |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
828 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
829 0, /* tp_doc */ |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
830 (traverseproc)tupleiter_traverse, /* tp_traverse */ |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
831 0, /* tp_clear */ |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
832 0, /* tp_richcompare */ |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
833 0, /* tp_weaklistoffset */ |
|
28320
fd5ef7003469
Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents:
28308
diff
changeset
|
834 PyObject_SelfIter, /* tp_iter */ |
|
24683
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
835 (iternextfunc)tupleiter_next, /* tp_iternext */ |
|
567704bfa595
Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents:
24280
diff
changeset
|
836 }; |
