Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* Tuple object implementation */ |
| 3 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | 5ce78f8 | 2000-04-21 21:15:05 +0000 | [diff] [blame] | 6 | /* Speed optimization to avoid frequent malloc/free of small tuples */ |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 7 | #ifndef PyTuple_MAXSAVESIZE |
| 8 | #define PyTuple_MAXSAVESIZE 20 /* Largest tuple to save on free list */ |
Guido van Rossum | 5ce78f8 | 2000-04-21 21:15:05 +0000 | [diff] [blame] | 9 | #endif |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 10 | #ifndef PyTuple_MAXFREELIST |
| 11 | #define PyTuple_MAXFREELIST 2000 /* Maximum number of tuples of each size to save */ |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 12 | #endif |
| 13 | |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 14 | #if PyTuple_MAXSAVESIZE > 0 |
| 15 | /* Entries 1 up to PyTuple_MAXSAVESIZE are free lists, entry 0 is the empty |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 16 | tuple () of which at most one instance will be allocated. |
| 17 | */ |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 18 | static PyTupleObject *free_list[PyTuple_MAXSAVESIZE]; |
| 19 | static int numfree[PyTuple_MAXSAVESIZE]; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 20 | #endif |
| 21 | #ifdef COUNT_ALLOCS |
| 22 | int fast_tuple_allocs; |
| 23 | int tuple_zero_allocs; |
| 24 | #endif |
| 25 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 26 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 27 | PyTuple_New(register Py_ssize_t size) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 28 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 29 | register PyTupleObject *op; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 30 | Py_ssize_t i; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 31 | if (size < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 32 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 33 | return NULL; |
| 34 | } |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 35 | #if PyTuple_MAXSAVESIZE > 0 |
| 36 | if (size == 0 && free_list[0]) { |
| 37 | op = free_list[0]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 38 | Py_INCREF(op); |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 39 | #ifdef COUNT_ALLOCS |
| 40 | tuple_zero_allocs++; |
| 41 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 42 | return (PyObject *) op; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 43 | } |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 44 | if (size < PyTuple_MAXSAVESIZE && (op = free_list[size]) != NULL) { |
| 45 | free_list[size] = (PyTupleObject *) op->ob_item[0]; |
| 46 | numfree[size]--; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 47 | #ifdef COUNT_ALLOCS |
| 48 | fast_tuple_allocs++; |
| 49 | #endif |
Guido van Rossum | e3a8e7e | 2002-08-19 19:26:42 +0000 | [diff] [blame] | 50 | /* Inline PyObject_InitVar */ |
Guido van Rossum | 68055ce | 1998-12-11 14:56:38 +0000 | [diff] [blame] | 51 | #ifdef Py_TRACE_REFS |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 52 | Py_SIZE(op) = size; |
| 53 | Py_TYPE(op) = &PyTuple_Type; |
Guido van Rossum | 68055ce | 1998-12-11 14:56:38 +0000 | [diff] [blame] | 54 | #endif |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 55 | _Py_NewReference((PyObject *)op); |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 56 | } |
| 57 | else |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 58 | #endif |
| 59 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 60 | Py_ssize_t nbytes = size * sizeof(PyObject *); |
Guido van Rossum | 5bc51f2 | 1999-07-12 23:06:58 +0000 | [diff] [blame] | 61 | /* Check for overflow */ |
| 62 | if (nbytes / sizeof(PyObject *) != (size_t)size || |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 63 | (nbytes += sizeof(PyTupleObject) - sizeof(PyObject *)) |
Guido van Rossum | 5bc51f2 | 1999-07-12 23:06:58 +0000 | [diff] [blame] | 64 | <= 0) |
| 65 | { |
| 66 | return PyErr_NoMemory(); |
| 67 | } |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 68 | op = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size); |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 69 | if (op == NULL) |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 70 | return NULL; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 71 | } |
Armin Rigo | 6fce78e | 2004-03-21 22:29:05 +0000 | [diff] [blame] | 72 | for (i=0; i < size; i++) |
| 73 | op->ob_item[i] = NULL; |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 74 | #if PyTuple_MAXSAVESIZE > 0 |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 75 | if (size == 0) { |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 76 | free_list[0] = op; |
| 77 | ++numfree[0]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 78 | Py_INCREF(op); /* extra INCREF so that this is never freed */ |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 79 | } |
| 80 | #endif |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 81 | _PyObject_GC_TRACK(op); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 82 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 85 | Py_ssize_t |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 86 | PyTuple_Size(register PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 87 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 88 | if (!PyTuple_Check(op)) { |
| 89 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 90 | return -1; |
| 91 | } |
| 92 | else |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 93 | return Py_SIZE(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 96 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 97 | PyTuple_GetItem(register PyObject *op, register Py_ssize_t i) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 98 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 99 | if (!PyTuple_Check(op)) { |
| 100 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 101 | return NULL; |
| 102 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 103 | if (i < 0 || i >= Py_SIZE(op)) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 104 | PyErr_SetString(PyExc_IndexError, "tuple index out of range"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 105 | return NULL; |
| 106 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 107 | return ((PyTupleObject *)op) -> ob_item[i]; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 111 | PyTuple_SetItem(register PyObject *op, register Py_ssize_t i, PyObject *newitem) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 112 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 113 | register PyObject *olditem; |
| 114 | register PyObject **p; |
Guido van Rossum | 787bdd3 | 1997-08-17 16:25:45 +0000 | [diff] [blame] | 115 | if (!PyTuple_Check(op) || op->ob_refcnt != 1) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 116 | Py_XDECREF(newitem); |
| 117 | PyErr_BadInternalCall(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 118 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 119 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 120 | if (i < 0 || i >= Py_SIZE(op)) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 121 | Py_XDECREF(newitem); |
| 122 | PyErr_SetString(PyExc_IndexError, |
| 123 | "tuple assignment index out of range"); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 124 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 125 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 126 | p = ((PyTupleObject *)op) -> ob_item + i; |
Guido van Rossum | 5fe6058 | 1995-03-09 12:12:50 +0000 | [diff] [blame] | 127 | olditem = *p; |
| 128 | *p = newitem; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 129 | Py_XDECREF(olditem); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 130 | return 0; |
| 131 | } |
| 132 | |
Raymond Hettinger | cb2da43 | 2003-10-12 18:24:34 +0000 | [diff] [blame] | 133 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 134 | PyTuple_Pack(Py_ssize_t n, ...) |
Raymond Hettinger | cb2da43 | 2003-10-12 18:24:34 +0000 | [diff] [blame] | 135 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 136 | Py_ssize_t i; |
Raymond Hettinger | cb2da43 | 2003-10-12 18:24:34 +0000 | [diff] [blame] | 137 | PyObject *o; |
| 138 | PyObject *result; |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 139 | PyObject **items; |
Raymond Hettinger | cb2da43 | 2003-10-12 18:24:34 +0000 | [diff] [blame] | 140 | va_list vargs; |
| 141 | |
| 142 | va_start(vargs, n); |
| 143 | result = PyTuple_New(n); |
| 144 | if (result == NULL) |
| 145 | return NULL; |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 146 | items = ((PyTupleObject *)result)->ob_item; |
Raymond Hettinger | cb2da43 | 2003-10-12 18:24:34 +0000 | [diff] [blame] | 147 | for (i = 0; i < n; i++) { |
| 148 | o = va_arg(vargs, PyObject *); |
| 149 | Py_INCREF(o); |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 150 | items[i] = o; |
Raymond Hettinger | cb2da43 | 2003-10-12 18:24:34 +0000 | [diff] [blame] | 151 | } |
| 152 | va_end(vargs); |
| 153 | return result; |
| 154 | } |
| 155 | |
| 156 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 157 | /* Methods */ |
| 158 | |
| 159 | static void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 160 | tupledealloc(register PyTupleObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 161 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 162 | register Py_ssize_t i; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 163 | register Py_ssize_t len = Py_SIZE(op); |
Guido van Rossum | ff413af | 2002-03-28 20:34:59 +0000 | [diff] [blame] | 164 | PyObject_GC_UnTrack(op); |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 165 | Py_TRASHCAN_SAFE_BEGIN(op) |
Guido van Rossum | 5ce78f8 | 2000-04-21 21:15:05 +0000 | [diff] [blame] | 166 | if (len > 0) { |
| 167 | i = len; |
Armin Rigo | 6fce78e | 2004-03-21 22:29:05 +0000 | [diff] [blame] | 168 | while (--i >= 0) |
| 169 | Py_XDECREF(op->ob_item[i]); |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 170 | #if PyTuple_MAXSAVESIZE > 0 |
| 171 | if (len < PyTuple_MAXSAVESIZE && |
| 172 | numfree[len] < PyTuple_MAXFREELIST && |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 173 | Py_TYPE(op) == &PyTuple_Type) |
Guido van Rossum | 4b8c0f6 | 2001-08-30 18:31:30 +0000 | [diff] [blame] | 174 | { |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 175 | op->ob_item[0] = (PyObject *) free_list[len]; |
| 176 | numfree[len]++; |
| 177 | free_list[len] = op; |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 178 | goto done; /* return */ |
Guido van Rossum | 1bb2687 | 1998-06-26 15:53:50 +0000 | [diff] [blame] | 179 | } |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 180 | #endif |
Guido van Rossum | 1bb2687 | 1998-06-26 15:53:50 +0000 | [diff] [blame] | 181 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 182 | Py_TYPE(op)->tp_free((PyObject *)op); |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 183 | done: |
| 184 | Py_TRASHCAN_SAFE_END(op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Guido van Rossum | 49e8514 | 1991-06-07 22:59:30 +0000 | [diff] [blame] | 187 | static int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 188 | tupleprint(PyTupleObject *op, FILE *fp, int flags) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 189 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 190 | Py_ssize_t i; |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 191 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 192 | fprintf(fp, "("); |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 193 | Py_END_ALLOW_THREADS |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 194 | for (i = 0; i < Py_SIZE(op); i++) { |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 195 | if (i > 0) { |
| 196 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 197 | fprintf(fp, ", "); |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 198 | Py_END_ALLOW_THREADS |
| 199 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 200 | if (PyObject_Print(op->ob_item[i], fp, 0) != 0) |
Guido van Rossum | 49e8514 | 1991-06-07 22:59:30 +0000 | [diff] [blame] | 201 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 202 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 203 | i = Py_SIZE(op); |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 204 | Py_BEGIN_ALLOW_THREADS |
| 205 | if (i == 1) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 206 | fprintf(fp, ","); |
| 207 | fprintf(fp, ")"); |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 208 | Py_END_ALLOW_THREADS |
Guido van Rossum | 49e8514 | 1991-06-07 22:59:30 +0000 | [diff] [blame] | 209 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 212 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 213 | tuplerepr(PyTupleObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 214 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 215 | Py_ssize_t i, n; |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 216 | PyObject *s, *temp; |
| 217 | PyObject *pieces, *result = NULL; |
| 218 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 219 | n = Py_SIZE(v); |
Brett Cannon | 31ba848 | 2007-09-30 20:37:19 +0000 | [diff] [blame] | 220 | if (n == 0) |
| 221 | return PyString_FromString("()"); |
| 222 | |
Brett Cannon | 0b14f24 | 2007-09-30 19:45:10 +0000 | [diff] [blame] | 223 | /* While not mutable, it is still possible to end up with a cycle in a |
| 224 | tuple through an object that stores itself within a tuple (and thus |
| 225 | infinitely asks for the repr of itself). This should only be |
| 226 | possible within a type. */ |
| 227 | i = Py_ReprEnter((PyObject *)v); |
| 228 | if (i != 0) { |
| 229 | return i > 0 ? PyString_FromString("(...)") : NULL; |
| 230 | } |
| 231 | |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 232 | pieces = PyTuple_New(n); |
| 233 | if (pieces == NULL) |
| 234 | return NULL; |
| 235 | |
| 236 | /* Do repr() on each element. */ |
| 237 | for (i = 0; i < n; ++i) { |
Brett Cannon | 0b14f24 | 2007-09-30 19:45:10 +0000 | [diff] [blame] | 238 | if (Py_EnterRecursiveCall(" while getting the repr of a tuple")) |
| 239 | goto Done; |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 240 | s = PyObject_Repr(v->ob_item[i]); |
Brett Cannon | 0b14f24 | 2007-09-30 19:45:10 +0000 | [diff] [blame] | 241 | Py_LeaveRecursiveCall(); |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 242 | if (s == NULL) |
| 243 | goto Done; |
| 244 | PyTuple_SET_ITEM(pieces, i, s); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 245 | } |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 246 | |
| 247 | /* Add "()" decorations to the first and last items. */ |
| 248 | assert(n > 0); |
| 249 | s = PyString_FromString("("); |
| 250 | if (s == NULL) |
| 251 | goto Done; |
| 252 | temp = PyTuple_GET_ITEM(pieces, 0); |
| 253 | PyString_ConcatAndDel(&s, temp); |
| 254 | PyTuple_SET_ITEM(pieces, 0, s); |
| 255 | if (s == NULL) |
| 256 | goto Done; |
| 257 | |
| 258 | s = PyString_FromString(n == 1 ? ",)" : ")"); |
| 259 | if (s == NULL) |
| 260 | goto Done; |
| 261 | temp = PyTuple_GET_ITEM(pieces, n-1); |
| 262 | PyString_ConcatAndDel(&temp, s); |
| 263 | PyTuple_SET_ITEM(pieces, n-1, temp); |
| 264 | if (temp == NULL) |
| 265 | goto Done; |
| 266 | |
| 267 | /* Paste them all together with ", " between. */ |
| 268 | s = PyString_FromString(", "); |
| 269 | if (s == NULL) |
| 270 | goto Done; |
| 271 | result = _PyString_Join(s, pieces); |
| 272 | Py_DECREF(s); |
| 273 | |
| 274 | Done: |
| 275 | Py_DECREF(pieces); |
Brett Cannon | 0b14f24 | 2007-09-30 19:45:10 +0000 | [diff] [blame] | 276 | Py_ReprLeave((PyObject *)v); |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 277 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Raymond Hettinger | 4ec44e8 | 2004-06-04 06:35:20 +0000 | [diff] [blame] | 280 | /* The addend 82520, was selected from the range(0, 1000000) for |
| 281 | generating the greatest number of prime multipliers for tuples |
| 282 | upto length eight: |
| 283 | |
| 284 | 1082527, 1165049, 1082531, 1165057, 1247581, 1330103, 1082533, |
| 285 | 1330111, 1412633, 1165069, 1247599, 1495177, 1577699 |
| 286 | */ |
| 287 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 288 | static long |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 289 | tuplehash(PyTupleObject *v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 290 | { |
| 291 | register long x, y; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 292 | register Py_ssize_t len = Py_SIZE(v); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 293 | register PyObject **p; |
Raymond Hettinger | 41bd022 | 2004-06-01 06:36:24 +0000 | [diff] [blame] | 294 | long mult = 1000003L; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 295 | x = 0x345678L; |
| 296 | p = v->ob_item; |
| 297 | while (--len >= 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 298 | y = PyObject_Hash(*p++); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 299 | if (y == -1) |
| 300 | return -1; |
Raymond Hettinger | 41bd022 | 2004-06-01 06:36:24 +0000 | [diff] [blame] | 301 | x = (x ^ y) * mult; |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 302 | /* the cast might truncate len; that doesn't change hash stability */ |
| 303 | mult += (long)(82520L + len + len); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 304 | } |
Raymond Hettinger | 57c2d93 | 2004-06-10 18:42:15 +0000 | [diff] [blame] | 305 | x += 97531L; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 306 | if (x == -1) |
| 307 | x = -2; |
| 308 | return x; |
| 309 | } |
| 310 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 311 | static Py_ssize_t |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 312 | tuplelength(PyTupleObject *a) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 313 | { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 314 | return Py_SIZE(a); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Jeremy Hylton | 37b1a26 | 2000-04-27 21:41:03 +0000 | [diff] [blame] | 317 | static int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 318 | tuplecontains(PyTupleObject *a, PyObject *el) |
Jeremy Hylton | 37b1a26 | 2000-04-27 21:41:03 +0000 | [diff] [blame] | 319 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 320 | Py_ssize_t i; |
| 321 | int cmp; |
Jeremy Hylton | 37b1a26 | 2000-04-27 21:41:03 +0000 | [diff] [blame] | 322 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 323 | for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i) |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 324 | cmp = PyObject_RichCompareBool(el, PyTuple_GET_ITEM(a, i), |
Raymond Hettinger | bd9adab | 2002-09-05 20:18:08 +0000 | [diff] [blame] | 325 | Py_EQ); |
Neal Norwitz | bb9c5f5 | 2002-09-05 21:32:55 +0000 | [diff] [blame] | 326 | return cmp; |
Jeremy Hylton | 37b1a26 | 2000-04-27 21:41:03 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 329 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 330 | tupleitem(register PyTupleObject *a, register Py_ssize_t i) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 331 | { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 332 | if (i < 0 || i >= Py_SIZE(a)) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 333 | PyErr_SetString(PyExc_IndexError, "tuple index out of range"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 334 | return NULL; |
| 335 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 336 | Py_INCREF(a->ob_item[i]); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 337 | return a->ob_item[i]; |
| 338 | } |
| 339 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 340 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 341 | tupleslice(register PyTupleObject *a, register Py_ssize_t ilow, |
| 342 | register Py_ssize_t ihigh) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 343 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 344 | register PyTupleObject *np; |
Raymond Hettinger | b7d05db | 2004-03-08 07:25:05 +0000 | [diff] [blame] | 345 | PyObject **src, **dest; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 346 | register Py_ssize_t i; |
| 347 | Py_ssize_t len; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 348 | if (ilow < 0) |
| 349 | ilow = 0; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 350 | if (ihigh > Py_SIZE(a)) |
| 351 | ihigh = Py_SIZE(a); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 352 | if (ihigh < ilow) |
| 353 | ihigh = ilow; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 354 | if (ilow == 0 && ihigh == Py_SIZE(a) && PyTuple_CheckExact(a)) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 355 | Py_INCREF(a); |
| 356 | return (PyObject *)a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 357 | } |
Raymond Hettinger | b7d05db | 2004-03-08 07:25:05 +0000 | [diff] [blame] | 358 | len = ihigh - ilow; |
| 359 | np = (PyTupleObject *)PyTuple_New(len); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 360 | if (np == NULL) |
| 361 | return NULL; |
Raymond Hettinger | b7d05db | 2004-03-08 07:25:05 +0000 | [diff] [blame] | 362 | src = a->ob_item + ilow; |
| 363 | dest = np->ob_item; |
| 364 | for (i = 0; i < len; i++) { |
| 365 | PyObject *v = src[i]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 366 | Py_INCREF(v); |
Raymond Hettinger | b7d05db | 2004-03-08 07:25:05 +0000 | [diff] [blame] | 367 | dest[i] = v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 368 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 369 | return (PyObject *)np; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 372 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 373 | PyTuple_GetSlice(PyObject *op, Py_ssize_t i, Py_ssize_t j) |
Guido van Rossum | 7c36ad7 | 1992-01-14 18:45:33 +0000 | [diff] [blame] | 374 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 375 | if (op == NULL || !PyTuple_Check(op)) { |
| 376 | PyErr_BadInternalCall(); |
Guido van Rossum | 7c36ad7 | 1992-01-14 18:45:33 +0000 | [diff] [blame] | 377 | return NULL; |
| 378 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 379 | return tupleslice((PyTupleObject *)op, i, j); |
Guido van Rossum | 7c36ad7 | 1992-01-14 18:45:33 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 382 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 383 | tupleconcat(register PyTupleObject *a, register PyObject *bb) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 384 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 385 | register Py_ssize_t size; |
| 386 | register Py_ssize_t i; |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 387 | PyObject **src, **dest; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 388 | PyTupleObject *np; |
| 389 | if (!PyTuple_Check(bb)) { |
Fred Drake | b6a9ada | 2000-06-01 03:12:13 +0000 | [diff] [blame] | 390 | PyErr_Format(PyExc_TypeError, |
Marc-André Lemburg | 29dc381 | 2000-06-16 17:05:57 +0000 | [diff] [blame] | 391 | "can only concatenate tuple (not \"%.200s\") to tuple", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 392 | Py_TYPE(bb)->tp_name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 393 | return NULL; |
| 394 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 395 | #define b ((PyTupleObject *)bb) |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 396 | size = Py_SIZE(a) + Py_SIZE(b); |
Guido van Rossum | a5c0e6d | 2002-10-11 21:05:56 +0000 | [diff] [blame] | 397 | if (size < 0) |
| 398 | return PyErr_NoMemory(); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 399 | np = (PyTupleObject *) PyTuple_New(size); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 400 | if (np == NULL) { |
Guido van Rossum | 49e8514 | 1991-06-07 22:59:30 +0000 | [diff] [blame] | 401 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 402 | } |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 403 | src = a->ob_item; |
| 404 | dest = np->ob_item; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 405 | for (i = 0; i < Py_SIZE(a); i++) { |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 406 | PyObject *v = src[i]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 407 | Py_INCREF(v); |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 408 | dest[i] = v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 409 | } |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 410 | src = b->ob_item; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 411 | dest = np->ob_item + Py_SIZE(a); |
| 412 | for (i = 0; i < Py_SIZE(b); i++) { |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 413 | PyObject *v = src[i]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 414 | Py_INCREF(v); |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 415 | dest[i] = v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 416 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 417 | return (PyObject *)np; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 418 | #undef b |
| 419 | } |
| 420 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 421 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 422 | tuplerepeat(PyTupleObject *a, Py_ssize_t n) |
Guido van Rossum | b8393da | 1991-06-04 19:35:24 +0000 | [diff] [blame] | 423 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 424 | Py_ssize_t i, j; |
| 425 | Py_ssize_t size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 426 | PyTupleObject *np; |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 427 | PyObject **p, **items; |
Guido van Rossum | b8393da | 1991-06-04 19:35:24 +0000 | [diff] [blame] | 428 | if (n < 0) |
| 429 | n = 0; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 430 | if (Py_SIZE(a) == 0 || n == 1) { |
Tim Peters | 7b07a41 | 2001-09-11 19:48:03 +0000 | [diff] [blame] | 431 | if (PyTuple_CheckExact(a)) { |
| 432 | /* Since tuples are immutable, we can return a shared |
| 433 | copy in this case */ |
| 434 | Py_INCREF(a); |
| 435 | return (PyObject *)a; |
| 436 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 437 | if (Py_SIZE(a) == 0) |
Tim Peters | 7b07a41 | 2001-09-11 19:48:03 +0000 | [diff] [blame] | 438 | return PyTuple_New(0); |
Guido van Rossum | b8393da | 1991-06-04 19:35:24 +0000 | [diff] [blame] | 439 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 440 | size = Py_SIZE(a) * n; |
| 441 | if (size/Py_SIZE(a) != n) |
Guido van Rossum | 5bc51f2 | 1999-07-12 23:06:58 +0000 | [diff] [blame] | 442 | return PyErr_NoMemory(); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 443 | np = (PyTupleObject *) PyTuple_New(size); |
Guido van Rossum | b8393da | 1991-06-04 19:35:24 +0000 | [diff] [blame] | 444 | if (np == NULL) |
| 445 | return NULL; |
| 446 | p = np->ob_item; |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 447 | items = a->ob_item; |
Guido van Rossum | b8393da | 1991-06-04 19:35:24 +0000 | [diff] [blame] | 448 | for (i = 0; i < n; i++) { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 449 | for (j = 0; j < Py_SIZE(a); j++) { |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 450 | *p = items[j]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 451 | Py_INCREF(*p); |
Guido van Rossum | b8393da | 1991-06-04 19:35:24 +0000 | [diff] [blame] | 452 | p++; |
| 453 | } |
| 454 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 455 | return (PyObject *) np; |
Guido van Rossum | b8393da | 1991-06-04 19:35:24 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Raymond Hettinger | 5b07ebc | 2008-02-07 00:54:20 +0000 | [diff] [blame] | 458 | static PyObject * |
| 459 | tupleindex(PyTupleObject *self, PyObject *args) |
| 460 | { |
| 461 | Py_ssize_t i, start=0, stop=Py_SIZE(self); |
| 462 | PyObject *v; |
| 463 | |
| 464 | if (!PyArg_ParseTuple(args, "O|O&O&:index", &v, |
| 465 | _PyEval_SliceIndex, &start, |
| 466 | _PyEval_SliceIndex, &stop)) |
| 467 | return NULL; |
| 468 | if (start < 0) { |
| 469 | start += Py_SIZE(self); |
| 470 | if (start < 0) |
| 471 | start = 0; |
| 472 | } |
| 473 | if (stop < 0) { |
| 474 | stop += Py_SIZE(self); |
| 475 | if (stop < 0) |
| 476 | stop = 0; |
| 477 | } |
| 478 | for (i = start; i < stop && i < Py_SIZE(self); i++) { |
| 479 | int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ); |
| 480 | if (cmp > 0) |
Raymond Hettinger | 55285ef | 2008-02-07 02:12:52 +0000 | [diff] [blame] | 481 | return PyInt_FromSsize_t(i); |
Raymond Hettinger | 5b07ebc | 2008-02-07 00:54:20 +0000 | [diff] [blame] | 482 | else if (cmp < 0) |
| 483 | return NULL; |
| 484 | } |
| 485 | PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in list"); |
| 486 | return NULL; |
| 487 | } |
| 488 | |
| 489 | static PyObject * |
| 490 | tuplecount(PyTupleObject *self, PyObject *v) |
| 491 | { |
| 492 | Py_ssize_t count = 0; |
| 493 | Py_ssize_t i; |
| 494 | |
| 495 | for (i = 0; i < Py_SIZE(self); i++) { |
| 496 | int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ); |
| 497 | if (cmp > 0) |
| 498 | count++; |
| 499 | else if (cmp < 0) |
| 500 | return NULL; |
| 501 | } |
Raymond Hettinger | 55285ef | 2008-02-07 02:12:52 +0000 | [diff] [blame] | 502 | return PyInt_FromSsize_t(count); |
Raymond Hettinger | 5b07ebc | 2008-02-07 00:54:20 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 505 | static int |
| 506 | tupletraverse(PyTupleObject *o, visitproc visit, void *arg) |
| 507 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 508 | Py_ssize_t i; |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 509 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 510 | for (i = Py_SIZE(o); --i >= 0; ) |
Thomas Wouters | c6e5506 | 2006-04-15 21:47:09 +0000 | [diff] [blame] | 511 | Py_VISIT(o->ob_item[i]); |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 512 | return 0; |
| 513 | } |
| 514 | |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 515 | static PyObject * |
| 516 | tuplerichcompare(PyObject *v, PyObject *w, int op) |
| 517 | { |
| 518 | PyTupleObject *vt, *wt; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 519 | Py_ssize_t i; |
| 520 | Py_ssize_t vlen, wlen; |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 521 | |
| 522 | if (!PyTuple_Check(v) || !PyTuple_Check(w)) { |
| 523 | Py_INCREF(Py_NotImplemented); |
| 524 | return Py_NotImplemented; |
| 525 | } |
| 526 | |
| 527 | vt = (PyTupleObject *)v; |
| 528 | wt = (PyTupleObject *)w; |
| 529 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 530 | vlen = Py_SIZE(vt); |
| 531 | wlen = Py_SIZE(wt); |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 532 | |
Tim Peters | d7ed3bf | 2001-05-15 20:12:59 +0000 | [diff] [blame] | 533 | /* Note: the corresponding code for lists has an "early out" test |
| 534 | * here when op is EQ or NE and the lengths differ. That pays there, |
| 535 | * but Tim was unable to find any real code where EQ/NE tuple |
| 536 | * compares don't have the same length, so testing for it here would |
| 537 | * have cost without benefit. |
| 538 | */ |
| 539 | |
| 540 | /* Search for the first index where items are different. |
| 541 | * Note that because tuples are immutable, it's safe to reuse |
| 542 | * vlen and wlen across the comparison calls. |
| 543 | */ |
| 544 | for (i = 0; i < vlen && i < wlen; i++) { |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 545 | int k = PyObject_RichCompareBool(vt->ob_item[i], |
| 546 | wt->ob_item[i], Py_EQ); |
| 547 | if (k < 0) |
| 548 | return NULL; |
| 549 | if (!k) |
| 550 | break; |
| 551 | } |
| 552 | |
Tim Peters | d7ed3bf | 2001-05-15 20:12:59 +0000 | [diff] [blame] | 553 | if (i >= vlen || i >= wlen) { |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 554 | /* No more items to compare -- compare sizes */ |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 555 | int cmp; |
| 556 | PyObject *res; |
| 557 | switch (op) { |
Tim Peters | d7ed3bf | 2001-05-15 20:12:59 +0000 | [diff] [blame] | 558 | case Py_LT: cmp = vlen < wlen; break; |
| 559 | case Py_LE: cmp = vlen <= wlen; break; |
| 560 | case Py_EQ: cmp = vlen == wlen; break; |
| 561 | case Py_NE: cmp = vlen != wlen; break; |
| 562 | case Py_GT: cmp = vlen > wlen; break; |
| 563 | case Py_GE: cmp = vlen >= wlen; break; |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 564 | default: return NULL; /* cannot happen */ |
| 565 | } |
| 566 | if (cmp) |
| 567 | res = Py_True; |
| 568 | else |
| 569 | res = Py_False; |
| 570 | Py_INCREF(res); |
| 571 | return res; |
| 572 | } |
| 573 | |
| 574 | /* We have an item that differs -- shortcuts for EQ/NE */ |
| 575 | if (op == Py_EQ) { |
| 576 | Py_INCREF(Py_False); |
| 577 | return Py_False; |
| 578 | } |
| 579 | if (op == Py_NE) { |
| 580 | Py_INCREF(Py_True); |
| 581 | return Py_True; |
| 582 | } |
| 583 | |
| 584 | /* Compare the final item again using the proper operator */ |
| 585 | return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op); |
| 586 | } |
| 587 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 588 | static PyObject * |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 589 | tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 590 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 591 | static PyObject * |
| 592 | tuple_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 593 | { |
| 594 | PyObject *arg = NULL; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 595 | static char *kwlist[] = {"sequence", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 596 | |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 597 | if (type != &PyTuple_Type) |
| 598 | return tuple_subtype_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 599 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:tuple", kwlist, &arg)) |
| 600 | return NULL; |
| 601 | |
| 602 | if (arg == NULL) |
| 603 | return PyTuple_New(0); |
| 604 | else |
| 605 | return PySequence_Tuple(arg); |
| 606 | } |
| 607 | |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 608 | static PyObject * |
| 609 | tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 610 | { |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 611 | PyObject *tmp, *newobj, *item; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 612 | Py_ssize_t i, n; |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 613 | |
| 614 | assert(PyType_IsSubtype(type, &PyTuple_Type)); |
| 615 | tmp = tuple_new(&PyTuple_Type, args, kwds); |
| 616 | if (tmp == NULL) |
| 617 | return NULL; |
| 618 | assert(PyTuple_Check(tmp)); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 619 | newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp)); |
| 620 | if (newobj == NULL) |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 621 | return NULL; |
| 622 | for (i = 0; i < n; i++) { |
| 623 | item = PyTuple_GET_ITEM(tmp, i); |
| 624 | Py_INCREF(item); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 625 | PyTuple_SET_ITEM(newobj, i, item); |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 626 | } |
| 627 | Py_DECREF(tmp); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 628 | return newobj; |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 631 | PyDoc_STRVAR(tuple_doc, |
Tim Peters | 1b8ca0d | 2001-09-02 06:42:25 +0000 | [diff] [blame] | 632 | "tuple() -> an empty tuple\n" |
| 633 | "tuple(sequence) -> tuple initialized from sequence's items\n" |
| 634 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 635 | "If the argument is a tuple, the return value is the same object."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 636 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 637 | static PySequenceMethods tuple_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 638 | (lenfunc)tuplelength, /* sq_length */ |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 639 | (binaryfunc)tupleconcat, /* sq_concat */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 640 | (ssizeargfunc)tuplerepeat, /* sq_repeat */ |
| 641 | (ssizeargfunc)tupleitem, /* sq_item */ |
| 642 | (ssizessizeargfunc)tupleslice, /* sq_slice */ |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 643 | 0, /* sq_ass_item */ |
| 644 | 0, /* sq_ass_slice */ |
| 645 | (objobjproc)tuplecontains, /* sq_contains */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 646 | }; |
| 647 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 648 | static PyObject* |
| 649 | tuplesubscript(PyTupleObject* self, PyObject* item) |
| 650 | { |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 651 | if (PyIndex_Check(item)) { |
| 652 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 653 | if (i == -1 && PyErr_Occurred()) |
| 654 | return NULL; |
| 655 | if (i < 0) |
| 656 | i += PyTuple_GET_SIZE(self); |
| 657 | return tupleitem(self, i); |
| 658 | } |
| 659 | else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 660 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 661 | PyObject* result; |
| 662 | PyObject* it; |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 663 | PyObject **src, **dest; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 664 | |
| 665 | if (PySlice_GetIndicesEx((PySliceObject*)item, |
| 666 | PyTuple_GET_SIZE(self), |
| 667 | &start, &stop, &step, &slicelength) < 0) { |
| 668 | return NULL; |
| 669 | } |
| 670 | |
| 671 | if (slicelength <= 0) { |
| 672 | return PyTuple_New(0); |
| 673 | } |
Thomas Wouters | 3ccec68 | 2007-08-28 15:28:19 +0000 | [diff] [blame] | 674 | else if (start == 0 && step == 1 && |
| 675 | slicelength == PyTuple_GET_SIZE(self) && |
| 676 | PyTuple_CheckExact(self)) { |
| 677 | Py_INCREF(self); |
| 678 | return (PyObject *)self; |
| 679 | } |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 680 | else { |
| 681 | result = PyTuple_New(slicelength); |
Georg Brandl | 5c170fd | 2006-03-17 19:03:25 +0000 | [diff] [blame] | 682 | if (!result) return NULL; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 683 | |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 684 | src = self->ob_item; |
| 685 | dest = ((PyTupleObject *)result)->ob_item; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 686 | for (cur = start, i = 0; i < slicelength; |
| 687 | cur += step, i++) { |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 688 | it = src[cur]; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 689 | Py_INCREF(it); |
Raymond Hettinger | a6366fe | 2004-03-09 13:05:22 +0000 | [diff] [blame] | 690 | dest[i] = it; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | return result; |
| 694 | } |
| 695 | } |
| 696 | else { |
Georg Brandl | 283a135 | 2006-11-19 08:48:30 +0000 | [diff] [blame] | 697 | PyErr_Format(PyExc_TypeError, |
| 698 | "tuple indices must be integers, not %.200s", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 699 | Py_TYPE(item)->tp_name); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 700 | return NULL; |
| 701 | } |
| 702 | } |
| 703 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 704 | static PyObject * |
| 705 | tuple_getnewargs(PyTupleObject *v) |
| 706 | { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 707 | return Py_BuildValue("(N)", tupleslice(v, 0, Py_SIZE(v))); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 708 | |
| 709 | } |
| 710 | |
Raymond Hettinger | 5b07ebc | 2008-02-07 00:54:20 +0000 | [diff] [blame] | 711 | PyDoc_STRVAR(index_doc, |
| 712 | "T.index(value, [start, [stop]]) -> integer -- return first index of value"); |
| 713 | PyDoc_STRVAR(count_doc, |
| 714 | "T.count(value) -> integer -- return number of occurrences of value"); |
| 715 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 716 | static PyMethodDef tuple_methods[] = { |
| 717 | {"__getnewargs__", (PyCFunction)tuple_getnewargs, METH_NOARGS}, |
Raymond Hettinger | 5b07ebc | 2008-02-07 00:54:20 +0000 | [diff] [blame] | 718 | {"index", (PyCFunction)tupleindex, METH_VARARGS, index_doc}, |
| 719 | {"count", (PyCFunction)tuplecount, METH_O, count_doc}, |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 720 | {NULL, NULL} /* sentinel */ |
| 721 | }; |
| 722 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 723 | static PyMappingMethods tuple_as_mapping = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 724 | (lenfunc)tuplelength, |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 725 | (binaryfunc)tuplesubscript, |
| 726 | 0 |
| 727 | }; |
| 728 | |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 729 | static PyObject *tuple_iter(PyObject *seq); |
| 730 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 731 | PyTypeObject PyTuple_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 732 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 733 | "tuple", |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 734 | sizeof(PyTupleObject) - sizeof(PyObject *), |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 735 | sizeof(PyObject *), |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 736 | (destructor)tupledealloc, /* tp_dealloc */ |
| 737 | (printfunc)tupleprint, /* tp_print */ |
| 738 | 0, /* tp_getattr */ |
| 739 | 0, /* tp_setattr */ |
| 740 | 0, /* tp_compare */ |
| 741 | (reprfunc)tuplerepr, /* tp_repr */ |
| 742 | 0, /* tp_as_number */ |
| 743 | &tuple_as_sequence, /* tp_as_sequence */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 744 | &tuple_as_mapping, /* tp_as_mapping */ |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 745 | (hashfunc)tuplehash, /* tp_hash */ |
| 746 | 0, /* tp_call */ |
| 747 | 0, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 748 | PyObject_GenericGetAttr, /* tp_getattro */ |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 749 | 0, /* tp_setattro */ |
| 750 | 0, /* tp_as_buffer */ |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 751 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
Neal Norwitz | ee3a1b5 | 2007-02-25 19:44:48 +0000 | [diff] [blame] | 752 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 753 | tuple_doc, /* tp_doc */ |
Guido van Rossum | f77bc62 | 2001-01-18 00:00:53 +0000 | [diff] [blame] | 754 | (traverseproc)tupletraverse, /* tp_traverse */ |
| 755 | 0, /* tp_clear */ |
| 756 | tuplerichcompare, /* tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 757 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 758 | tuple_iter, /* tp_iter */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 759 | 0, /* tp_iternext */ |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 760 | tuple_methods, /* tp_methods */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 761 | 0, /* tp_members */ |
| 762 | 0, /* tp_getset */ |
| 763 | 0, /* tp_base */ |
| 764 | 0, /* tp_dict */ |
| 765 | 0, /* tp_descr_get */ |
| 766 | 0, /* tp_descr_set */ |
| 767 | 0, /* tp_dictoffset */ |
| 768 | 0, /* tp_init */ |
| 769 | 0, /* tp_alloc */ |
| 770 | tuple_new, /* tp_new */ |
Neil Schemenauer | 626d774 | 2002-04-12 03:05:52 +0000 | [diff] [blame] | 771 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 772 | }; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 773 | |
| 774 | /* The following function breaks the notion that tuples are immutable: |
| 775 | it changes the size of a tuple. We get away with this only if there |
| 776 | is only one module referencing the object. You can also think of it |
Neil Schemenauer | 08b53e6 | 2000-10-05 19:36:49 +0000 | [diff] [blame] | 777 | as creating a new tuple object and destroying the old one, only more |
| 778 | efficiently. In any case, don't use this if the tuple may already be |
Tim Peters | 4324aa3 | 2001-05-28 22:30:08 +0000 | [diff] [blame] | 779 | known to some other part of the code. */ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 780 | |
| 781 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 782 | _PyTuple_Resize(PyObject **pv, Py_ssize_t newsize) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 783 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 784 | register PyTupleObject *v; |
| 785 | register PyTupleObject *sv; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 786 | Py_ssize_t i; |
| 787 | Py_ssize_t oldsize; |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 788 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 789 | v = (PyTupleObject *) *pv; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 790 | if (v == NULL || Py_TYPE(v) != &PyTuple_Type || |
| 791 | (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) { |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 792 | *pv = 0; |
Neil Schemenauer | 08b53e6 | 2000-10-05 19:36:49 +0000 | [diff] [blame] | 793 | Py_XDECREF(v); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 794 | PyErr_BadInternalCall(); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 795 | return -1; |
| 796 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 797 | oldsize = Py_SIZE(v); |
Guido van Rossum | f70590f | 2001-12-07 20:00:04 +0000 | [diff] [blame] | 798 | if (oldsize == newsize) |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 799 | return 0; |
Neil Schemenauer | 08b53e6 | 2000-10-05 19:36:49 +0000 | [diff] [blame] | 800 | |
Guido van Rossum | f70590f | 2001-12-07 20:00:04 +0000 | [diff] [blame] | 801 | if (oldsize == 0) { |
Thomas Wouters | 6a92237 | 2001-05-28 13:11:02 +0000 | [diff] [blame] | 802 | /* Empty tuples are often shared, so we should never |
| 803 | resize them in-place even if we do own the only |
| 804 | (current) reference */ |
| 805 | Py_DECREF(v); |
| 806 | *pv = PyTuple_New(newsize); |
Thomas Wouters | 0dcea59 | 2001-05-29 07:58:45 +0000 | [diff] [blame] | 807 | return *pv == NULL ? -1 : 0; |
Thomas Wouters | 6a92237 | 2001-05-28 13:11:02 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 810 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 811 | _Py_DEC_REFTOTAL; |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 812 | _PyObject_GC_UNTRACK(v); |
Neil Schemenauer | 08b53e6 | 2000-10-05 19:36:49 +0000 | [diff] [blame] | 813 | _Py_ForgetReference((PyObject *) v); |
Guido van Rossum | f70590f | 2001-12-07 20:00:04 +0000 | [diff] [blame] | 814 | /* DECREF items deleted by shrinkage */ |
| 815 | for (i = newsize; i < oldsize; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 816 | Py_XDECREF(v->ob_item[i]); |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 817 | v->ob_item[i] = NULL; |
| 818 | } |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 819 | sv = PyObject_GC_Resize(PyTupleObject, v, newsize); |
Neil Schemenauer | 08b53e6 | 2000-10-05 19:36:49 +0000 | [diff] [blame] | 820 | if (sv == NULL) { |
| 821 | *pv = NULL; |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 822 | PyObject_GC_Del(v); |
Neil Schemenauer | 08b53e6 | 2000-10-05 19:36:49 +0000 | [diff] [blame] | 823 | return -1; |
Guido van Rossum | 5ce78f8 | 2000-04-21 21:15:05 +0000 | [diff] [blame] | 824 | } |
Neil Schemenauer | 08b53e6 | 2000-10-05 19:36:49 +0000 | [diff] [blame] | 825 | _Py_NewReference((PyObject *) sv); |
Guido van Rossum | f70590f | 2001-12-07 20:00:04 +0000 | [diff] [blame] | 826 | /* Zero out items added by growing */ |
Neal Norwitz | 35fc760 | 2002-06-13 21:11:11 +0000 | [diff] [blame] | 827 | if (newsize > oldsize) |
Jeremy Hylton | 8b47dff | 2002-06-20 23:13:17 +0000 | [diff] [blame] | 828 | memset(&sv->ob_item[oldsize], 0, |
| 829 | sizeof(*sv->ob_item) * (newsize - oldsize)); |
Neil Schemenauer | 08b53e6 | 2000-10-05 19:36:49 +0000 | [diff] [blame] | 830 | *pv = (PyObject *) sv; |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 831 | _PyObject_GC_TRACK(sv); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 832 | return 0; |
| 833 | } |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 834 | |
Christian Heimes | 3b718a7 | 2008-02-14 12:47:33 +0000 | [diff] [blame] | 835 | int |
| 836 | PyTuple_ClearFreeList(void) |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 837 | { |
Christian Heimes | 3b718a7 | 2008-02-14 12:47:33 +0000 | [diff] [blame] | 838 | int freelist_size = 0; |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 839 | #if PyTuple_MAXSAVESIZE > 0 |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 840 | int i; |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 841 | for (i = 1; i < PyTuple_MAXSAVESIZE; i++) { |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 842 | PyTupleObject *p, *q; |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 843 | p = free_list[i]; |
Christian Heimes | 3b718a7 | 2008-02-14 12:47:33 +0000 | [diff] [blame] | 844 | freelist_size += numfree[i]; |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 845 | free_list[i] = NULL; |
Christian Heimes | 3b718a7 | 2008-02-14 12:47:33 +0000 | [diff] [blame] | 846 | numfree[i] = 0; |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 847 | while (p) { |
| 848 | q = p; |
| 849 | p = (PyTupleObject *)(p->ob_item[0]); |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 850 | PyObject_GC_Del(q); |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 851 | } |
| 852 | } |
| 853 | #endif |
Christian Heimes | 3b718a7 | 2008-02-14 12:47:33 +0000 | [diff] [blame] | 854 | return freelist_size; |
| 855 | } |
| 856 | |
| 857 | void |
| 858 | PyTuple_Fini(void) |
| 859 | { |
| 860 | #if PyTuple_MAXSAVESIZE > 0 |
| 861 | /* empty tuples are used all over the place and applications may |
| 862 | * rely on the fact that an empty tuple is a singleton. */ |
| 863 | Py_XDECREF(free_list[0]); |
| 864 | free_list[0] = NULL; |
| 865 | |
| 866 | (void)PyTuple_ClearFreeList(); |
| 867 | #endif |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 868 | } |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 869 | |
| 870 | /*********************** Tuple Iterator **************************/ |
| 871 | |
| 872 | typedef struct { |
| 873 | PyObject_HEAD |
| 874 | long it_index; |
| 875 | PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */ |
| 876 | } tupleiterobject; |
| 877 | |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 878 | static void |
| 879 | tupleiter_dealloc(tupleiterobject *it) |
| 880 | { |
| 881 | _PyObject_GC_UNTRACK(it); |
| 882 | Py_XDECREF(it->it_seq); |
| 883 | PyObject_GC_Del(it); |
| 884 | } |
| 885 | |
| 886 | static int |
| 887 | tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg) |
| 888 | { |
Thomas Wouters | c6e5506 | 2006-04-15 21:47:09 +0000 | [diff] [blame] | 889 | Py_VISIT(it->it_seq); |
| 890 | return 0; |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 893 | static PyObject * |
| 894 | tupleiter_next(tupleiterobject *it) |
| 895 | { |
| 896 | PyTupleObject *seq; |
| 897 | PyObject *item; |
| 898 | |
| 899 | assert(it != NULL); |
| 900 | seq = it->it_seq; |
| 901 | if (seq == NULL) |
| 902 | return NULL; |
| 903 | assert(PyTuple_Check(seq)); |
| 904 | |
| 905 | if (it->it_index < PyTuple_GET_SIZE(seq)) { |
| 906 | item = PyTuple_GET_ITEM(seq, it->it_index); |
| 907 | ++it->it_index; |
| 908 | Py_INCREF(item); |
| 909 | return item; |
| 910 | } |
| 911 | |
| 912 | Py_DECREF(seq); |
| 913 | it->it_seq = NULL; |
| 914 | return NULL; |
| 915 | } |
| 916 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 917 | static PyObject * |
Raymond Hettinger | 435bf58 | 2004-03-18 22:43:10 +0000 | [diff] [blame] | 918 | tupleiter_len(tupleiterobject *it) |
| 919 | { |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 920 | Py_ssize_t len = 0; |
Raymond Hettinger | 435bf58 | 2004-03-18 22:43:10 +0000 | [diff] [blame] | 921 | if (it->it_seq) |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 922 | len = PyTuple_GET_SIZE(it->it_seq) - it->it_index; |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 923 | return PyInt_FromSsize_t(len); |
Raymond Hettinger | 435bf58 | 2004-03-18 22:43:10 +0000 | [diff] [blame] | 924 | } |
| 925 | |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 926 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 927 | |
| 928 | static PyMethodDef tupleiter_methods[] = { |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 929 | {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc}, |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 930 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 435bf58 | 2004-03-18 22:43:10 +0000 | [diff] [blame] | 931 | }; |
| 932 | |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 933 | PyTypeObject PyTupleIter_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 934 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 935 | "tupleiterator", /* tp_name */ |
| 936 | sizeof(tupleiterobject), /* tp_basicsize */ |
| 937 | 0, /* tp_itemsize */ |
| 938 | /* methods */ |
| 939 | (destructor)tupleiter_dealloc, /* tp_dealloc */ |
| 940 | 0, /* tp_print */ |
| 941 | 0, /* tp_getattr */ |
| 942 | 0, /* tp_setattr */ |
| 943 | 0, /* tp_compare */ |
| 944 | 0, /* tp_repr */ |
| 945 | 0, /* tp_as_number */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 946 | 0, /* tp_as_sequence */ |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 947 | 0, /* tp_as_mapping */ |
| 948 | 0, /* tp_hash */ |
| 949 | 0, /* tp_call */ |
| 950 | 0, /* tp_str */ |
| 951 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 952 | 0, /* tp_setattro */ |
| 953 | 0, /* tp_as_buffer */ |
| 954 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 955 | 0, /* tp_doc */ |
| 956 | (traverseproc)tupleiter_traverse, /* tp_traverse */ |
| 957 | 0, /* tp_clear */ |
| 958 | 0, /* tp_richcompare */ |
| 959 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 960 | PyObject_SelfIter, /* tp_iter */ |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 961 | (iternextfunc)tupleiter_next, /* tp_iternext */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 962 | tupleiter_methods, /* tp_methods */ |
| 963 | 0, |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 964 | }; |
Martin v. Löwis | 72d2067 | 2006-04-11 09:04:12 +0000 | [diff] [blame] | 965 | |
| 966 | static PyObject * |
| 967 | tuple_iter(PyObject *seq) |
| 968 | { |
| 969 | tupleiterobject *it; |
| 970 | |
| 971 | if (!PyTuple_Check(seq)) { |
| 972 | PyErr_BadInternalCall(); |
| 973 | return NULL; |
| 974 | } |
| 975 | it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type); |
| 976 | if (it == NULL) |
| 977 | return NULL; |
| 978 | it->it_index = 0; |
| 979 | Py_INCREF(seq); |
| 980 | it->it_seq = (PyTupleObject *)seq; |
| 981 | _PyObject_GC_TRACK(it); |
| 982 | return (PyObject *)it; |
| 983 | } |