Mercurial > cpython-lru_cache-weakref
annotate Objects/tupleobject.c @ 17581:c1937faabb23 2.1
Added support for optional explanation parameter to the \versionadded
macro.
Refactored do_cmd_versionadded() and do_cmd_versionchanged() to do most
of the work in a helper function, with the do_cmd_*() wrappers just supplying
a portion of the replacement text.
| author | Fred Drake <fdrake@acm.org> |
|---|---|
| date | Wed, 18 Apr 2001 17:29:14 +0000 |
| parents | 72139263f8a7 |
| children | b0b46d7734e0 2b5a9bff1b11 |
| 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 { |
| 29 register int i; | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
30 register PyTupleObject *op; |
| 6 | 31 if (size < 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
32 PyErr_BadInternalCall(); |
| 6 | 33 return NULL; |
| 34 } | |
|
1352
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
35 #if MAXSAVESIZE > 0 |
|
1380
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
36 if (size == 0 && free_tuples[0]) { |
|
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
37 op = free_tuples[0]; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
38 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
|
39 #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
|
40 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
|
41 #endif |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
42 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
|
43 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
44 if (0 < size && size < MAXSAVESIZE && |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
45 (op = free_tuples[size]) != NULL) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
46 { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
47 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
|
48 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
|
49 #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
|
50 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
|
51 #endif |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12516
diff
changeset
|
52 /* PyObject_InitVar is inlined */ |
|
9632
ac5345814ba0
When tracing references, reset the type and size of tuples allocated
Guido van Rossum <guido@python.org>
parents:
9484
diff
changeset
|
53 #ifdef Py_TRACE_REFS |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12516
diff
changeset
|
54 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
|
55 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
|
56 #endif |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12516
diff
changeset
|
57 _Py_NewReference((PyObject *)op); |
|
5936
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
58 } |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
59 else |
|
1352
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
60 #endif |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
61 { |
|
11127
9a20038f0b99
Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents:
9632
diff
changeset
|
62 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
|
63 /* 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
|
64 if (nbytes / sizeof(PyObject *) != (size_t)size || |
|
12993
8f79c5b69e0a
part 2 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12990
diff
changeset
|
65 (nbytes += sizeof(PyTupleObject) - sizeof(PyObject *) |
|
13183
54bffdddc0ef
final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12993
diff
changeset
|
66 + PyGC_HEAD_SIZE) |
|
11127
9a20038f0b99
Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents:
9632
diff
changeset
|
67 <= 0) |
|
9a20038f0b99
Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents:
9632
diff
changeset
|
68 { |
|
9a20038f0b99
Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents:
9632
diff
changeset
|
69 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
|
70 } |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12516
diff
changeset
|
71 /* PyObject_NewVar is inlined */ |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12516
diff
changeset
|
72 op = (PyTupleObject *) PyObject_MALLOC(nbytes); |
|
1352
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
73 if (op == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
74 return PyErr_NoMemory(); |
|
13183
54bffdddc0ef
final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12993
diff
changeset
|
75 op = (PyTupleObject *) PyObject_FROM_GC(op); |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12516
diff
changeset
|
76 PyObject_INIT_VAR(op, &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
|
77 } |
| 6 | 78 for (i = 0; i < size; i++) |
| 79 op->ob_item[i] = NULL; | |
|
1352
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
80 #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
|
81 if (size == 0) { |
|
1380
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
82 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
|
83 ++num_free_tuples[0]; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
84 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
|
85 } |
|
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
86 #endif |
|
13183
54bffdddc0ef
final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12993
diff
changeset
|
87 PyObject_GC_Init(op); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
88 return (PyObject *) op; |
| 6 | 89 } |
| 90 | |
| 91 int | |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
92 PyTuple_Size(register PyObject *op) |
| 6 | 93 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
94 if (!PyTuple_Check(op)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
95 PyErr_BadInternalCall(); |
| 6 | 96 return -1; |
| 97 } | |
| 98 else | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
99 return ((PyTupleObject *)op)->ob_size; |
| 6 | 100 } |
| 101 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
102 PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
103 PyTuple_GetItem(register PyObject *op, register int i) |
| 6 | 104 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
105 if (!PyTuple_Check(op)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
106 PyErr_BadInternalCall(); |
| 6 | 107 return NULL; |
| 108 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
109 if (i < 0 || i >= ((PyTupleObject *)op) -> ob_size) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
110 PyErr_SetString(PyExc_IndexError, "tuple index out of range"); |
| 6 | 111 return NULL; |
| 112 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
113 return ((PyTupleObject *)op) -> ob_item[i]; |
| 6 | 114 } |
| 115 | |
| 116 int | |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
117 PyTuple_SetItem(register PyObject *op, register int i, PyObject *newitem) |
| 6 | 118 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
119 register PyObject *olditem; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
120 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
|
121 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
|
122 Py_XDECREF(newitem); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
123 PyErr_BadInternalCall(); |
| 27 | 124 return -1; |
| 6 | 125 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
126 if (i < 0 || i >= ((PyTupleObject *)op) -> ob_size) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
127 Py_XDECREF(newitem); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
128 PyErr_SetString(PyExc_IndexError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
129 "tuple assignment index out of range"); |
| 27 | 130 return -1; |
| 6 | 131 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
132 p = ((PyTupleObject *)op) -> ob_item + i; |
|
2504
425835b88099
a few peephole optimizations
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
133 olditem = *p; |
|
425835b88099
a few peephole optimizations
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
134 *p = newitem; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
135 Py_XDECREF(olditem); |
| 6 | 136 return 0; |
| 137 } | |
| 138 | |
| 139 /* Methods */ | |
| 140 | |
| 141 static void | |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
142 tupledealloc(register PyTupleObject *op) |
| 6 | 143 { |
| 144 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
|
145 register int len = op->ob_size; |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
146 Py_TRASHCAN_SAFE_BEGIN(op) |
|
13183
54bffdddc0ef
final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12993
diff
changeset
|
147 PyObject_GC_Fini(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
|
148 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
|
149 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
|
150 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
|
151 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
|
152 #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
|
153 if (len < MAXSAVESIZE && num_free_tuples[len] < MAXSAVEDTUPLES) { |
|
456869eaa7d5
Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents:
11999
diff
changeset
|
154 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
|
155 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
|
156 free_tuples[len] = op; |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
157 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
|
158 } |
|
1352
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
159 #endif |
|
8559
92267838feb9
Slight rearrangement of some code to make it faster, by Vladimir
Guido van Rossum <guido@python.org>
parents:
6083
diff
changeset
|
160 } |
|
13265
e8bb0d536633
Neil Schemenauer: small fixes for GC
Guido van Rossum <guido@python.org>
parents:
13256
diff
changeset
|
161 op = (PyTupleObject *) PyObject_AS_GC(op); |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12516
diff
changeset
|
162 PyObject_DEL(op); |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
163 done: |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
164 Py_TRASHCAN_SAFE_END(op) |
| 6 | 165 } |
| 166 | |
|
288
9b43a983124c
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
258
diff
changeset
|
167 static int |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
168 tupleprint(PyTupleObject *op, FILE *fp, int flags) |
| 6 | 169 { |
| 170 int i; | |
| 171 fprintf(fp, "("); | |
|
288
9b43a983124c
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
258
diff
changeset
|
172 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
|
173 if (i > 0) |
| 6 | 174 fprintf(fp, ", "); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
175 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
|
176 return -1; |
| 6 | 177 } |
| 178 if (op->ob_size == 1) | |
| 179 fprintf(fp, ","); | |
| 180 fprintf(fp, ")"); | |
|
288
9b43a983124c
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
258
diff
changeset
|
181 return 0; |
| 6 | 182 } |
| 183 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
184 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
185 tuplerepr(PyTupleObject *v) |
| 6 | 186 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
187 PyObject *s, *comma; |
| 6 | 188 int i; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
189 s = PyString_FromString("("); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
190 comma = PyString_FromString(", "); |
| 6 | 191 for (i = 0; i < v->ob_size && s != NULL; i++) { |
| 192 if (i > 0) | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
193 PyString_Concat(&s, comma); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
194 PyString_ConcatAndDel(&s, PyObject_Repr(v->ob_item[i])); |
| 6 | 195 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
196 Py_DECREF(comma); |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
197 if (v->ob_size == 1) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
198 PyString_ConcatAndDel(&s, PyString_FromString(",")); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
199 PyString_ConcatAndDel(&s, PyString_FromString(")")); |
| 6 | 200 return s; |
| 201 } | |
| 202 | |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
203 static long |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
204 tuplehash(PyTupleObject *v) |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
205 { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
206 register long x, y; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
207 register int len = v->ob_size; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
208 register PyObject **p; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
209 x = 0x345678L; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
210 p = v->ob_item; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
211 while (--len >= 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
212 y = PyObject_Hash(*p++); |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
213 if (y == -1) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
214 return -1; |
|
4733
3c7812bfa1a4
Better tuple hash function.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
215 x = (1000003*x) ^ y; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
216 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
217 x ^= v->ob_size; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
218 if (x == -1) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
219 x = -2; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
220 return x; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
221 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
222 |
| 6 | 223 static int |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
224 tuplelength(PyTupleObject *a) |
| 6 | 225 { |
| 226 return a->ob_size; | |
| 227 } | |
| 228 | |
|
12516
e6e914d5b207
add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12439
diff
changeset
|
229 static int |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
230 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
|
231 { |
|
e6e914d5b207
add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12439
diff
changeset
|
232 int i, cmp; |
|
e6e914d5b207
add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12439
diff
changeset
|
233 |
|
e6e914d5b207
add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12439
diff
changeset
|
234 for (i = 0; i < a->ob_size; ++i) { |
|
15973
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
235 cmp = PyObject_RichCompareBool(el, PyTuple_GET_ITEM(a, i), |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
236 Py_EQ); |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
237 if (cmp > 0) |
|
12516
e6e914d5b207
add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12439
diff
changeset
|
238 return 1; |
|
15973
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
239 else if (cmp < 0) |
|
12516
e6e914d5b207
add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12439
diff
changeset
|
240 return -1; |
|
e6e914d5b207
add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12439
diff
changeset
|
241 } |
|
e6e914d5b207
add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12439
diff
changeset
|
242 return 0; |
|
e6e914d5b207
add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12439
diff
changeset
|
243 } |
|
e6e914d5b207
add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12439
diff
changeset
|
244 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
245 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
246 tupleitem(register PyTupleObject *a, register int i) |
| 6 | 247 { |
| 248 if (i < 0 || i >= a->ob_size) { | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
249 PyErr_SetString(PyExc_IndexError, "tuple index out of range"); |
| 6 | 250 return NULL; |
| 251 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
252 Py_INCREF(a->ob_item[i]); |
| 6 | 253 return a->ob_item[i]; |
| 254 } | |
| 255 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
256 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
257 tupleslice(register PyTupleObject *a, register int ilow, register int ihigh) |
| 6 | 258 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
259 register PyTupleObject *np; |
| 6 | 260 register int i; |
| 261 if (ilow < 0) | |
| 262 ilow = 0; | |
| 263 if (ihigh > a->ob_size) | |
| 264 ihigh = a->ob_size; | |
| 265 if (ihigh < ilow) | |
| 266 ihigh = ilow; | |
| 267 if (ilow == 0 && ihigh == a->ob_size) { | |
| 268 /* XXX can only do this if tuples are immutable! */ | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
269 Py_INCREF(a); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
270 return (PyObject *)a; |
| 6 | 271 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
272 np = (PyTupleObject *)PyTuple_New(ihigh - ilow); |
| 6 | 273 if (np == NULL) |
| 274 return NULL; | |
| 275 for (i = ilow; i < ihigh; i++) { | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
276 PyObject *v = a->ob_item[i]; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
277 Py_INCREF(v); |
| 6 | 278 np->ob_item[i - ilow] = v; |
| 279 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
280 return (PyObject *)np; |
| 6 | 281 } |
| 282 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
283 PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
284 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
|
285 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
286 if (op == NULL || !PyTuple_Check(op)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
287 PyErr_BadInternalCall(); |
|
599
a347933a6d92
New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents:
288
diff
changeset
|
288 return NULL; |
|
a347933a6d92
New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents:
288
diff
changeset
|
289 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
290 return tupleslice((PyTupleObject *)op, i, j); |
|
599
a347933a6d92
New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents:
288
diff
changeset
|
291 } |
|
a347933a6d92
New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents:
288
diff
changeset
|
292 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
293 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
294 tupleconcat(register PyTupleObject *a, register PyObject *bb) |
| 6 | 295 { |
| 296 register int size; | |
| 297 register int i; | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
298 PyTupleObject *np; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
299 if (!PyTuple_Check(bb)) { |
|
12816
4272b368bf69
Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
300 PyErr_Format(PyExc_TypeError, |
|
12929
2eef636ba1b6
Michael Hudson <mwh21@cam.ac.uk>:
Marc-André Lemburg <mal@egenix.com>
parents:
12925
diff
changeset
|
301 "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
|
302 bb->ob_type->tp_name); |
| 6 | 303 return NULL; |
| 304 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
305 #define b ((PyTupleObject *)bb) |
| 6 | 306 size = a->ob_size + b->ob_size; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
307 np = (PyTupleObject *) PyTuple_New(size); |
| 6 | 308 if (np == NULL) { |
|
288
9b43a983124c
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
258
diff
changeset
|
309 return NULL; |
| 6 | 310 } |
| 311 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
|
312 PyObject *v = a->ob_item[i]; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
313 Py_INCREF(v); |
| 6 | 314 np->ob_item[i] = v; |
| 315 } | |
| 316 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
|
317 PyObject *v = b->ob_item[i]; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
318 Py_INCREF(v); |
| 6 | 319 np->ob_item[i + a->ob_size] = v; |
| 320 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
321 return (PyObject *)np; |
| 6 | 322 #undef b |
| 323 } | |
| 324 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
325 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
326 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
|
327 { |
|
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
328 int i, j; |
|
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
329 int size; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
330 PyTupleObject *np; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
331 PyObject **p; |
|
258
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
332 if (n < 0) |
|
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
333 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
|
334 if (a->ob_size == 0 || n == 1) { |
|
258
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
335 /* Since tuples are immutable, we can return a shared |
|
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
336 copy in this case */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
337 Py_INCREF(a); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
338 return (PyObject *)a; |
|
258
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
339 } |
|
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
340 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
|
341 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
|
342 return PyErr_NoMemory(); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
343 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
|
344 if (np == NULL) |
|
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
345 return NULL; |
|
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
346 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
|
347 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
|
348 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
|
349 *p = a->ob_item[j]; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
350 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
|
351 p++; |
|
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
352 } |
|
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
353 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
354 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
|
355 } |
|
3b93bcea10f6
Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
356 |
|
12990
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12929
diff
changeset
|
357 static int |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12929
diff
changeset
|
358 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
|
359 { |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12929
diff
changeset
|
360 int i, err; |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12929
diff
changeset
|
361 PyObject *x; |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12929
diff
changeset
|
362 |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12929
diff
changeset
|
363 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
|
364 x = o->ob_item[i]; |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12929
diff
changeset
|
365 if (x != NULL) { |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12929
diff
changeset
|
366 err = visit(x, arg); |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12929
diff
changeset
|
367 if (err) |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12929
diff
changeset
|
368 return err; |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12929
diff
changeset
|
369 } |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12929
diff
changeset
|
370 } |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12929
diff
changeset
|
371 return 0; |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12929
diff
changeset
|
372 } |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12929
diff
changeset
|
373 |
|
15973
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
374 static PyObject * |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
375 tuplerichcompare(PyObject *v, PyObject *w, int op) |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
376 { |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
377 PyTupleObject *vt, *wt; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
378 int i; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
379 |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
380 if (!PyTuple_Check(v) || !PyTuple_Check(w)) { |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
381 Py_INCREF(Py_NotImplemented); |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
382 return Py_NotImplemented; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
383 } |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
384 |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
385 vt = (PyTupleObject *)v; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
386 wt = (PyTupleObject *)w; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
387 |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
388 if (vt->ob_size != wt->ob_size && (op == Py_EQ || op == Py_NE)) { |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
389 /* Shortcut: if the lengths differ, the tuples differ */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
390 PyObject *res; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
391 if (op == Py_EQ) |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
392 res = Py_False; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
393 else |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
394 res = Py_True; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
395 Py_INCREF(res); |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
396 return res; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
397 } |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
398 |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
399 /* Search for the first index where items are different */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
400 for (i = 0; i < vt->ob_size && i < wt->ob_size; i++) { |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
401 int k = PyObject_RichCompareBool(vt->ob_item[i], |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
402 wt->ob_item[i], Py_EQ); |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
403 if (k < 0) |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
404 return NULL; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
405 if (!k) |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
406 break; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
407 } |
|
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 if (i >= vt->ob_size || i >= wt->ob_size) { |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
410 /* No more items to compare -- compare sizes */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
411 int vs = vt->ob_size; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
412 int ws = wt->ob_size; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
413 int cmp; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
414 PyObject *res; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
415 switch (op) { |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
416 case Py_LT: cmp = vs < ws; break; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
417 case Py_LE: cmp = ws <= ws; break; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
418 case Py_EQ: cmp = vs == ws; break; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
419 case Py_NE: cmp = vs != ws; break; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
420 case Py_GT: cmp = vs > ws; break; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
421 case Py_GE: cmp = vs >= ws; break; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
422 default: return NULL; /* cannot happen */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
423 } |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
424 if (cmp) |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
425 res = Py_True; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
426 else |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
427 res = Py_False; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
428 Py_INCREF(res); |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
429 return res; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
430 } |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
431 |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
432 /* 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
|
433 if (op == Py_EQ) { |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
434 Py_INCREF(Py_False); |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
435 return Py_False; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
436 } |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
437 if (op == Py_NE) { |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
438 Py_INCREF(Py_True); |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
439 return Py_True; |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
440 } |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
441 |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
442 /* 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
|
443 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
|
444 } |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
445 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
446 static PySequenceMethods tuple_as_sequence = { |
|
15973
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
447 (inquiry)tuplelength, /* sq_length */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
448 (binaryfunc)tupleconcat, /* sq_concat */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
449 (intargfunc)tuplerepeat, /* sq_repeat */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
450 (intargfunc)tupleitem, /* sq_item */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
451 (intintargfunc)tupleslice, /* sq_slice */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
452 0, /* sq_ass_item */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
453 0, /* sq_ass_slice */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
454 (objobjproc)tuplecontains, /* sq_contains */ |
| 6 | 455 }; |
| 456 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
457 PyTypeObject PyTuple_Type = { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
458 PyObject_HEAD_INIT(&PyType_Type) |
| 6 | 459 0, |
| 460 "tuple", | |
|
13183
54bffdddc0ef
final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12993
diff
changeset
|
461 sizeof(PyTupleObject) - sizeof(PyObject *) + PyGC_HEAD_SIZE, |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
462 sizeof(PyObject *), |
|
15973
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
463 (destructor)tupledealloc, /* tp_dealloc */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
464 (printfunc)tupleprint, /* tp_print */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
465 0, /* tp_getattr */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
466 0, /* tp_setattr */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
467 0, /* tp_compare */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
468 (reprfunc)tuplerepr, /* tp_repr */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
469 0, /* tp_as_number */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
470 &tuple_as_sequence, /* tp_as_sequence */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
471 0, /* tp_as_mapping */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
472 (hashfunc)tuplehash, /* tp_hash */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
473 0, /* tp_call */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
474 0, /* tp_str */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
475 0, /* tp_getattro */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
476 0, /* tp_setattro */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
477 0, /* tp_as_buffer */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
478 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /* tp_flags */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
479 0, /* tp_doc */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
480 (traverseproc)tupletraverse, /* tp_traverse */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
481 0, /* tp_clear */ |
|
72139263f8a7
Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents:
15037
diff
changeset
|
482 tuplerichcompare, /* tp_richcompare */ |
| 6 | 483 }; |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
484 |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
485 /* 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
|
486 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
|
487 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
|
488 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
|
489 efficiently. In any case, don't use this if the tuple may already be |
|
c7d46eae95d8
Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14584
diff
changeset
|
490 known to some other part of the code. The last_is_sticky is not used |
|
c7d46eae95d8
Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14584
diff
changeset
|
491 and must always be false. */ |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
492 |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
493 int |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
494 _PyTuple_Resize(PyObject **pv, int newsize, int last_is_sticky) |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
495 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
496 register PyTupleObject *v; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
497 register PyTupleObject *sv; |
|
1380
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
498 int i; |
|
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
499 int sizediff; |
|
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
500 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
501 v = (PyTupleObject *) *pv; |
|
15037
c7d46eae95d8
Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14584
diff
changeset
|
502 if (v == NULL || !PyTuple_Check(v) || v->ob_refcnt != 1 || |
|
c7d46eae95d8
Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14584
diff
changeset
|
503 last_is_sticky) { |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
504 *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
|
505 Py_XDECREF(v); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
506 PyErr_BadInternalCall(); |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
507 return -1; |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
508 } |
|
2888
6abd14485884
better err checks in resizetuple
Guido van Rossum <guido@python.org>
parents:
2612
diff
changeset
|
509 sizediff = newsize - v->ob_size; |
|
1380
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
510 if (sizediff == 0) |
|
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
511 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
|
512 |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
513 /* XXX UNREF/NEWREF interface should be more symmetrical */ |
|
3586
36cc3ee54f28
new debugger symbol names
Guido van Rossum <guido@python.org>
parents:
2888
diff
changeset
|
514 #ifdef Py_REF_DEBUG |
|
2612
ca6b60d4ea90
fix dusty debugging macros
Guido van Rossum <guido@python.org>
parents:
2504
diff
changeset
|
515 --_Py_RefTotal; |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
516 #endif |
|
15037
c7d46eae95d8
Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14584
diff
changeset
|
517 _Py_ForgetReference((PyObject *) v); |
|
1380
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
518 for (i = newsize; i < v->ob_size; i++) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4733
diff
changeset
|
519 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
|
520 v->ob_item[i] = NULL; |
|
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
521 } |
|
15037
c7d46eae95d8
Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14584
diff
changeset
|
522 PyObject_GC_Fini(v); |
|
c7d46eae95d8
Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14584
diff
changeset
|
523 v = (PyTupleObject *) PyObject_AS_GC(v); |
|
c7d46eae95d8
Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14584
diff
changeset
|
524 sv = (PyTupleObject *) PyObject_REALLOC((char *)v, |
|
c7d46eae95d8
Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14584
diff
changeset
|
525 sizeof(PyTupleObject) |
|
c7d46eae95d8
Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14584
diff
changeset
|
526 + PyGC_HEAD_SIZE |
|
c7d46eae95d8
Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14584
diff
changeset
|
527 + newsize * sizeof(PyObject *)); |
|
c7d46eae95d8
Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14584
diff
changeset
|
528 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
|
529 *pv = NULL; |
|
c7d46eae95d8
Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14584
diff
changeset
|
530 PyObject_DEL(v); |
|
c7d46eae95d8
Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14584
diff
changeset
|
531 PyErr_NoMemory(); |
|
c7d46eae95d8
Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14584
diff
changeset
|
532 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
|
533 } |
|
15037
c7d46eae95d8
Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14584
diff
changeset
|
534 sv = (PyTupleObject *) PyObject_FROM_GC(sv); |
|
c7d46eae95d8
Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14584
diff
changeset
|
535 _Py_NewReference((PyObject *) sv); |
|
1380
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
536 for (i = sv->ob_size; i < newsize; i++) |
|
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1369
diff
changeset
|
537 sv->ob_item[i] = NULL; |
|
15037
c7d46eae95d8
Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14584
diff
changeset
|
538 sv->ob_size = newsize; |
|
c7d46eae95d8
Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
14584
diff
changeset
|
539 *pv = (PyObject *) sv; |
|
13183
54bffdddc0ef
final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12993
diff
changeset
|
540 PyObject_GC_Init(sv); |
|
1369
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
541 return 0; |
|
89e1e5d9ccbf
* compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
1352
diff
changeset
|
542 } |
|
5936
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
543 |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
544 void |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
545 PyTuple_Fini(void) |
|
5936
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
546 { |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
547 #if MAXSAVESIZE > 0 |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
548 int i; |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
549 |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
550 Py_XDECREF(free_tuples[0]); |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
551 free_tuples[0] = NULL; |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
552 |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
553 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
|
554 PyTupleObject *p, *q; |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
555 p = free_tuples[i]; |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
556 free_tuples[i] = NULL; |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
557 while (p) { |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
558 q = p; |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
559 p = (PyTupleObject *)(p->ob_item[0]); |
|
13265
e8bb0d536633
Neil Schemenauer: small fixes for GC
Guido van Rossum <guido@python.org>
parents:
13256
diff
changeset
|
560 q = (PyTupleObject *) PyObject_AS_GC(q); |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12516
diff
changeset
|
561 PyObject_DEL(q); |
|
5936
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
562 } |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
563 } |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
564 #endif |
|
80d24364b932
Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
565 } |
