annotate Objects/tupleobject.c @ 64209:dfd0229748ef 2.7

Merged revisions 84239 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/py3k ........ r84239 | antoine.pitrou | 2010-08-21 21:09:32 +0200 (sam., 21 août 2010) | 4 lines Issue #9617: Signals received during a low-level write operation aren't ignored by the buffered IO layer anymore. ........
author Antoine Pitrou <solipsis@pitrou.net>
date Sat, 21 Aug 2010 19:17:25 +0000
parents fd25030223b0
children b36cb4602e21
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
1
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 /* Tuple object implementation */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
4 #include "Python.h"
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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 */
45191
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
7 #ifndef PyTuple_MAXSAVESIZE
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
8 #define PyTuple_MAXSAVESIZE 20 /* Largest tuple to save on free list */
12439
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
61265
7be8129ee0fd Remove an unneeded variable assignment.
Brett Cannon <bcannon@gmail.com>
parents: 59205
diff changeset
10 #ifndef PyTuple_MAXFREELIST
45191
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
11 #define PyTuple_MAXFREELIST 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
45191
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
14 #if PyTuple_MAXSAVESIZE > 0
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
15 /* Entries 1 up to PyTuple_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 */
45191
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
18 static PyTupleObject *free_list[PyTuple_MAXSAVESIZE];
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
19 static int numfree[PyTuple_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
50627
f0e0c3536e22 Issue #4850: Change COUNT_ALLOCS variables to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 49431
diff changeset
22 Py_ssize_t fast_tuple_allocs;
f0e0c3536e22 Issue #4850: Change COUNT_ALLOCS variables to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 49431
diff changeset
23 Py_ssize_t tuple_zero_allocs;
1352
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
52288
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
26 /* Debug statistic to count GC tracking of tuples.
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
27 Please note that tuples are only untracked when considered by the GC, and
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
28 many of them will be dead before. Therefore, a tracking rate close to 100%
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
29 does not necessarily prove that the heuristic is inefficient.
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
30 */
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
31 #ifdef SHOW_TRACK_COUNT
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
32 static Py_ssize_t count_untracked = 0;
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
33 static Py_ssize_t count_tracked = 0;
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
34
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
35 static void
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
36 show_track(void)
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
37 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
38 fprintf(stderr, "Tuples created: %" PY_FORMAT_SIZE_T "d\n",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
39 count_tracked + count_untracked);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
40 fprintf(stderr, "Tuples tracked by the GC: %" PY_FORMAT_SIZE_T
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
41 "d\n", count_tracked);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
42 fprintf(stderr, "%.2f%% tuple tracking rate\n\n",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
43 (100.0*count_tracked/(count_untracked+count_tracked)));
52288
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
44 }
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
45 #endif
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
46
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
47
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
48 PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
49 PyTuple_New(register Py_ssize_t size)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
51 register PyTupleObject *op;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
52 Py_ssize_t i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
53 if (size < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
54 PyErr_BadInternalCall();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
55 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
56 }
45191
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
57 #if PyTuple_MAXSAVESIZE > 0
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
58 if (size == 0 && free_list[0]) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
59 op = free_list[0];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
60 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
61 #ifdef COUNT_ALLOCS
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
62 tuple_zero_allocs++;
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
63 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
64 return (PyObject *) op;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
65 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
66 if (size < PyTuple_MAXSAVESIZE && (op = free_list[size]) != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
67 free_list[size] = (PyTupleObject *) op->ob_item[0];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
68 numfree[size]--;
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
69 #ifdef COUNT_ALLOCS
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
70 fast_tuple_allocs++;
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
71 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
72 /* Inline PyObject_InitVar */
9632
ac5345814ba0 When tracing references, reset the type and size of tuples allocated
Guido van Rossum <guido@python.org>
parents: 9484
diff changeset
73 #ifdef Py_TRACE_REFS
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
74 Py_SIZE(op) = size;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
75 Py_TYPE(op) = &PyTuple_Type;
9632
ac5345814ba0 When tracing references, reset the type and size of tuples allocated
Guido van Rossum <guido@python.org>
parents: 9484
diff changeset
76 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
77 _Py_NewReference((PyObject *)op);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
78 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
79 else
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
80 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
81 {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
82 Py_ssize_t nbytes = size * sizeof(PyObject *);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
83 /* Check for overflow */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
84 if (nbytes / sizeof(PyObject *) != (size_t)size ||
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
85 (nbytes > PY_SSIZE_T_MAX - sizeof(PyTupleObject) - sizeof(PyObject *)))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
86 {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
87 return PyErr_NoMemory();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
88 }
48114
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 47682
diff changeset
89
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
90 op = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
91 if (op == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
92 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
93 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
94 for (i=0; i < size; i++)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
95 op->ob_item[i] = NULL;
45191
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
96 #if PyTuple_MAXSAVESIZE > 0
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
97 if (size == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
98 free_list[0] = op;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
99 ++numfree[0];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
100 Py_INCREF(op); /* extra INCREF so that this is never freed */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
101 }
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
102 #endif
52292
b3791309e690 The tracking statistics were actually too pessimistic
Antoine Pitrou <solipsis@pitrou.net>
parents: 52288
diff changeset
103 #ifdef SHOW_TRACK_COUNT
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
104 count_tracked++;
52292
b3791309e690 The tracking statistics were actually too pessimistic
Antoine Pitrou <solipsis@pitrou.net>
parents: 52288
diff changeset
105 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
106 _PyObject_GC_TRACK(op);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
107 return (PyObject *) op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
109
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
110 Py_ssize_t
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
111 PyTuple_Size(register PyObject *op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
113 if (!PyTuple_Check(op)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
114 PyErr_BadInternalCall();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
115 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
116 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
117 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
118 return Py_SIZE(op);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
120
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
121 PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
122 PyTuple_GetItem(register PyObject *op, register Py_ssize_t i)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
124 if (!PyTuple_Check(op)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
125 PyErr_BadInternalCall();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
126 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
127 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
128 if (i < 0 || i >= Py_SIZE(op)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
129 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
130 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
131 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
132 return ((PyTupleObject *)op) -> ob_item[i];
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
134
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
136 PyTuple_SetItem(register PyObject *op, register Py_ssize_t i, PyObject *newitem)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
138 register PyObject *olditem;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
139 register PyObject **p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
140 if (!PyTuple_Check(op) || op->ob_refcnt != 1) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
141 Py_XDECREF(newitem);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
142 PyErr_BadInternalCall();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
143 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
144 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
145 if (i < 0 || i >= Py_SIZE(op)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
146 Py_XDECREF(newitem);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
147 PyErr_SetString(PyExc_IndexError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
148 "tuple assignment index out of range");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
149 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
150 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
151 p = ((PyTupleObject *)op) -> ob_item + i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
152 olditem = *p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
153 *p = newitem;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
154 Py_XDECREF(olditem);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
155 return 0;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
157
52288
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
158 void
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
159 _PyTuple_MaybeUntrack(PyObject *op)
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
160 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
161 PyTupleObject *t;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
162 Py_ssize_t i, n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
163
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
164 if (!PyTuple_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
165 return;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
166 t = (PyTupleObject *) op;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
167 n = Py_SIZE(t);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
168 for (i = 0; i < n; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
169 PyObject *elt = PyTuple_GET_ITEM(t, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
170 /* Tuple with NULL elements aren't
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
171 fully constructed, don't untrack
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
172 them yet. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
173 if (!elt ||
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
174 _PyObject_GC_MAY_BE_TRACKED(elt))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
175 return;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
176 }
52288
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
177 #ifdef SHOW_TRACK_COUNT
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
178 count_tracked--;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
179 count_untracked++;
52288
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
180 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
181 _PyObject_GC_UNTRACK(op);
52288
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
182 }
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
183
30682
473f4e2959a8 Extended tuple's C API to include a new function, PyTuple_Pack() that is
Raymond Hettinger <python@rcn.com>
parents: 29008
diff changeset
184 PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
185 PyTuple_Pack(Py_ssize_t n, ...)
30682
473f4e2959a8 Extended tuple's C API to include a new function, PyTuple_Pack() that is
Raymond Hettinger <python@rcn.com>
parents: 29008
diff changeset
186 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
187 Py_ssize_t i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
188 PyObject *o;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
189 PyObject *result;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
190 PyObject **items;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
191 va_list vargs;
30682
473f4e2959a8 Extended tuple's C API to include a new function, PyTuple_Pack() that is
Raymond Hettinger <python@rcn.com>
parents: 29008
diff changeset
192
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
193 va_start(vargs, n);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
194 result = PyTuple_New(n);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
195 if (result == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
196 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
197 items = ((PyTupleObject *)result)->ob_item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
198 for (i = 0; i < n; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
199 o = va_arg(vargs, PyObject *);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
200 Py_INCREF(o);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
201 items[i] = o;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
202 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
203 va_end(vargs);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
204 return result;
30682
473f4e2959a8 Extended tuple's C API to include a new function, PyTuple_Pack() that is
Raymond Hettinger <python@rcn.com>
parents: 29008
diff changeset
205 }
473f4e2959a8 Extended tuple's C API to include a new function, PyTuple_Pack() that is
Raymond Hettinger <python@rcn.com>
parents: 29008
diff changeset
206
473f4e2959a8 Extended tuple's C API to include a new function, PyTuple_Pack() that is
Raymond Hettinger <python@rcn.com>
parents: 29008
diff changeset
207
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 /* Methods */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
209
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 static void
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
211 tupledealloc(register PyTupleObject *op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
213 register Py_ssize_t i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
214 register Py_ssize_t len = Py_SIZE(op);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
215 PyObject_GC_UnTrack(op);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
216 Py_TRASHCAN_SAFE_BEGIN(op)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
217 if (len > 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
218 i = len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
219 while (--i >= 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
220 Py_XDECREF(op->ob_item[i]);
45191
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
221 #if PyTuple_MAXSAVESIZE > 0
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
222 if (len < PyTuple_MAXSAVESIZE &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
223 numfree[len] < PyTuple_MAXFREELIST &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
224 Py_TYPE(op) == &PyTuple_Type)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
225 {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
226 op->ob_item[0] = (PyObject *) free_list[len];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
227 numfree[len]++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
228 free_list[len] = op;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
229 goto done; /* return */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
230 }
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
231 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
232 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
233 Py_TYPE(op)->tp_free((PyObject *)op);
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
234 done:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
235 Py_TRASHCAN_SAFE_END(op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
237
288
9b43a983124c printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 258
diff changeset
238 static int
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
239 tupleprint(PyTupleObject *op, FILE *fp, int flags)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
241 Py_ssize_t i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
242 Py_BEGIN_ALLOW_THREADS
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
243 fprintf(fp, "(");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
244 Py_END_ALLOW_THREADS
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
245 for (i = 0; i < Py_SIZE(op); i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
246 if (i > 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
247 Py_BEGIN_ALLOW_THREADS
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
248 fprintf(fp, ", ");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
249 Py_END_ALLOW_THREADS
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
250 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
251 if (PyObject_Print(op->ob_item[i], fp, 0) != 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
252 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
253 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
254 i = Py_SIZE(op);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
255 Py_BEGIN_ALLOW_THREADS
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
256 if (i == 1)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
257 fprintf(fp, ",");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
258 fprintf(fp, ")");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
259 Py_END_ALLOW_THREADS
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
260 return 0;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
261 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
262
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
263 static PyObject *
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
264 tuplerepr(PyTupleObject *v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
266 Py_ssize_t i, n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
267 PyObject *s, *temp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
268 PyObject *pieces, *result = NULL;
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
269
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
270 n = Py_SIZE(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
271 if (n == 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
272 return PyString_FromString("()");
43452
106a71c51477 Fix error introduced by r58288; if a tuple is length 0 return its repr and
Brett Cannon <bcannon@gmail.com>
parents: 43451
diff changeset
273
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
274 /* While not mutable, it is still possible to end up with a cycle in a
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
275 tuple through an object that stores itself within a tuple (and thus
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
276 infinitely asks for the repr of itself). This should only be
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
277 possible within a type. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
278 i = Py_ReprEnter((PyObject *)v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
279 if (i != 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
280 return i > 0 ? PyString_FromString("(...)") : NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
281 }
43451
381ed41003e7 tuple.__repr__ did not consider a reference loop as it is not possible from
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
282
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
283 pieces = PyTuple_New(n);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
284 if (pieces == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
285 return NULL;
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
286
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
287 /* Do repr() on each element. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
288 for (i = 0; i < n; ++i) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
289 if (Py_EnterRecursiveCall(" while getting the repr of a tuple"))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
290 goto Done;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
291 s = PyObject_Repr(v->ob_item[i]);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
292 Py_LeaveRecursiveCall();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
293 if (s == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
294 goto Done;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
295 PyTuple_SET_ITEM(pieces, i, s);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
296 }
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
297
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
298 /* Add "()" decorations to the first and last items. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
299 assert(n > 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
300 s = PyString_FromString("(");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
301 if (s == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
302 goto Done;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
303 temp = PyTuple_GET_ITEM(pieces, 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
304 PyString_ConcatAndDel(&s, temp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
305 PyTuple_SET_ITEM(pieces, 0, s);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
306 if (s == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
307 goto Done;
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
308
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
309 s = PyString_FromString(n == 1 ? ",)" : ")");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
310 if (s == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
311 goto Done;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
312 temp = PyTuple_GET_ITEM(pieces, n-1);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
313 PyString_ConcatAndDel(&temp, s);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
314 PyTuple_SET_ITEM(pieces, n-1, temp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
315 if (temp == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
316 goto Done;
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
317
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
318 /* Paste them all together with ", " between. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
319 s = PyString_FromString(", ");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
320 if (s == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
321 goto Done;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
322 result = _PyString_Join(s, pieces);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
323 Py_DECREF(s);
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
324
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
325 Done:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
326 Py_DECREF(pieces);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
327 Py_ReprLeave((PyObject *)v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
328 return result;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
329 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
330
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
331 /* The addend 82520, was selected from the range(0, 1000000) for
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
332 generating the greatest number of prime multipliers for tuples
32242
0bdb49a5a8fa Replaced arbitrary addend in tuple_hash with one that is known to generate
Raymond Hettinger <python@rcn.com>
parents: 32171
diff changeset
333 upto length eight:
0bdb49a5a8fa Replaced arbitrary addend in tuple_hash with one that is known to generate
Raymond Hettinger <python@rcn.com>
parents: 32171
diff changeset
334
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
335 1082527, 1165049, 1082531, 1165057, 1247581, 1330103, 1082533,
32242
0bdb49a5a8fa Replaced arbitrary addend in tuple_hash with one that is known to generate
Raymond Hettinger <python@rcn.com>
parents: 32171
diff changeset
336 1330111, 1412633, 1165069, 1247599, 1495177, 1577699
0bdb49a5a8fa Replaced arbitrary addend in tuple_hash with one that is known to generate
Raymond Hettinger <python@rcn.com>
parents: 32171
diff changeset
337 */
0bdb49a5a8fa Replaced arbitrary addend in tuple_hash with one that is known to generate
Raymond Hettinger <python@rcn.com>
parents: 32171
diff changeset
338
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
339 static long
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
340 tuplehash(PyTupleObject *v)
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
341 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
342 register long x, y;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
343 register Py_ssize_t len = Py_SIZE(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
344 register PyObject **p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
345 long mult = 1000003L;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
346 x = 0x345678L;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
347 p = v->ob_item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
348 while (--len >= 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
349 y = PyObject_Hash(*p++);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
350 if (y == -1)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
351 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
352 x = (x ^ y) * mult;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
353 /* the cast might truncate len; that doesn't change hash stability */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
354 mult += (long)(82520L + len + len);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
355 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
356 x += 97531L;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
357 if (x == -1)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
358 x = -2;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
359 return x;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
360 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
361
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
362 static Py_ssize_t
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
363 tuplelength(PyTupleObject *a)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
364 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
365 return Py_SIZE(a);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
366 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
367
12516
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12439
diff changeset
368 static int
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
369 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
370 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
371 Py_ssize_t i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
372 int cmp;
12516
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12439
diff changeset
373
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
374 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
375 cmp = PyObject_RichCompareBool(el, PyTuple_GET_ITEM(a, i),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
376 Py_EQ);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
377 return cmp;
12516
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12439
diff changeset
378 }
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12439
diff changeset
379
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
380 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
381 tupleitem(register PyTupleObject *a, register Py_ssize_t i)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
382 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
383 if (i < 0 || i >= Py_SIZE(a)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
384 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
385 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
386 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
387 Py_INCREF(a->ob_item[i]);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
388 return a->ob_item[i];
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
389 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
390
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
391 static PyObject *
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
392 tupleslice(register PyTupleObject *a, register Py_ssize_t ilow,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
393 register Py_ssize_t ihigh)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
394 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
395 register PyTupleObject *np;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
396 PyObject **src, **dest;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
397 register Py_ssize_t i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
398 Py_ssize_t len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
399 if (ilow < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
400 ilow = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
401 if (ihigh > Py_SIZE(a))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
402 ihigh = Py_SIZE(a);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
403 if (ihigh < ilow)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
404 ihigh = ilow;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
405 if (ilow == 0 && ihigh == Py_SIZE(a) && PyTuple_CheckExact(a)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
406 Py_INCREF(a);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
407 return (PyObject *)a;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
408 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
409 len = ihigh - ilow;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
410 np = (PyTupleObject *)PyTuple_New(len);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
411 if (np == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
412 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
413 src = a->ob_item + ilow;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
414 dest = np->ob_item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
415 for (i = 0; i < len; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
416 PyObject *v = src[i];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
417 Py_INCREF(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
418 dest[i] = v;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
419 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
420 return (PyObject *)np;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
421 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
422
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
423 PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
424 PyTuple_GetSlice(PyObject *op, Py_ssize_t i, Py_ssize_t j)
599
a347933a6d92 New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents: 288
diff changeset
425 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
426 if (op == NULL || !PyTuple_Check(op)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
427 PyErr_BadInternalCall();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
428 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
429 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
430 return tupleslice((PyTupleObject *)op, i, j);
599
a347933a6d92 New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents: 288
diff changeset
431 }
a347933a6d92 New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents: 288
diff changeset
432
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
433 static PyObject *
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
434 tupleconcat(register PyTupleObject *a, register PyObject *bb)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
435 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
436 register Py_ssize_t size;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
437 register Py_ssize_t i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
438 PyObject **src, **dest;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
439 PyTupleObject *np;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
440 if (!PyTuple_Check(bb)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
441 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
442 "can only concatenate tuple (not \"%.200s\") to tuple",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
443 Py_TYPE(bb)->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
444 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
445 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
446 #define b ((PyTupleObject *)bb)
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
447 size = Py_SIZE(a) + Py_SIZE(b);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
448 if (size < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
449 return PyErr_NoMemory();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
450 np = (PyTupleObject *) PyTuple_New(size);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
451 if (np == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
452 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
453 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
454 src = a->ob_item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
455 dest = np->ob_item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
456 for (i = 0; i < Py_SIZE(a); i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
457 PyObject *v = src[i];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
458 Py_INCREF(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
459 dest[i] = v;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
460 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
461 src = b->ob_item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
462 dest = np->ob_item + Py_SIZE(a);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
463 for (i = 0; i < Py_SIZE(b); i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
464 PyObject *v = src[i];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
465 Py_INCREF(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
466 dest[i] = v;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
467 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
468 return (PyObject *)np;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
469 #undef b
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
470 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
471
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
472 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
473 tuplerepeat(PyTupleObject *a, Py_ssize_t n)
258
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
474 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
475 Py_ssize_t i, j;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
476 Py_ssize_t size;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
477 PyTupleObject *np;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
478 PyObject **p, **items;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
479 if (n < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
480 n = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
481 if (Py_SIZE(a) == 0 || n == 1) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
482 if (PyTuple_CheckExact(a)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
483 /* Since tuples are immutable, we can return a shared
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
484 copy in this case */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
485 Py_INCREF(a);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
486 return (PyObject *)a;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
487 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
488 if (Py_SIZE(a) == 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
489 return PyTuple_New(0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
490 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
491 size = Py_SIZE(a) * n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
492 if (size/Py_SIZE(a) != n)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
493 return PyErr_NoMemory();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
494 np = (PyTupleObject *) PyTuple_New(size);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
495 if (np == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
496 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
497 p = np->ob_item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
498 items = a->ob_item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
499 for (i = 0; i < n; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
500 for (j = 0; j < Py_SIZE(a); j++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
501 *p = items[j];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
502 Py_INCREF(*p);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
503 p++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
504 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
505 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
506 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
507 }
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
508
45207
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 45191
diff changeset
509 static PyObject *
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 45191
diff changeset
510 tupleindex(PyTupleObject *self, PyObject *args)
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 45191
diff changeset
511 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
512 Py_ssize_t i, start=0, stop=Py_SIZE(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
513 PyObject *v;
45207
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 45191
diff changeset
514
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
515 if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
516 _PyEval_SliceIndex, &start,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
517 _PyEval_SliceIndex, &stop))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
518 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
519 if (start < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
520 start += Py_SIZE(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
521 if (start < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
522 start = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
523 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
524 if (stop < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
525 stop += Py_SIZE(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
526 if (stop < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
527 stop = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
528 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
529 for (i = start; i < stop && i < Py_SIZE(self); i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
530 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
531 if (cmp > 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
532 return PyInt_FromSsize_t(i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
533 else if (cmp < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
534 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
535 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
536 PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
537 return NULL;
45207
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 45191
diff changeset
538 }
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 45191
diff changeset
539
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 45191
diff changeset
540 static PyObject *
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 45191
diff changeset
541 tuplecount(PyTupleObject *self, PyObject *v)
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 45191
diff changeset
542 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
543 Py_ssize_t count = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
544 Py_ssize_t i;
45207
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 45191
diff changeset
545
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
546 for (i = 0; i < Py_SIZE(self); i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
547 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
548 if (cmp > 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
549 count++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
550 else if (cmp < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
551 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
552 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
553 return PyInt_FromSsize_t(count);
45207
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 45191
diff changeset
554 }
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 45191
diff changeset
555
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12929
diff changeset
556 static int
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12929
diff changeset
557 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
558 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
559 Py_ssize_t i;
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12929
diff changeset
560
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
561 for (i = Py_SIZE(o); --i >= 0; )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
562 Py_VISIT(o->ob_item[i]);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
563 return 0;
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12929
diff changeset
564 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12929
diff changeset
565
15973
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
566 static PyObject *
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
567 tuplerichcompare(PyObject *v, PyObject *w, int op)
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
568 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
569 PyTupleObject *vt, *wt;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
570 Py_ssize_t i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
571 Py_ssize_t vlen, wlen;
15973
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
572
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
573 if (!PyTuple_Check(v) || !PyTuple_Check(w)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
574 Py_INCREF(Py_NotImplemented);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
575 return Py_NotImplemented;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
576 }
15973
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
577
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
578 vt = (PyTupleObject *)v;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
579 wt = (PyTupleObject *)w;
15973
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
580
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
581 vlen = Py_SIZE(vt);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
582 wlen = Py_SIZE(wt);
15973
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
583
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
584 /* Note: the corresponding code for lists has an "early out" test
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
585 * here when op is EQ or NE and the lengths differ. That pays there,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
586 * but Tim was unable to find any real code where EQ/NE tuple
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
587 * compares don't have the same length, so testing for it here would
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
588 * have cost without benefit.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
589 */
17841
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
590
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
591 /* Search for the first index where items are different.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
592 * Note that because tuples are immutable, it's safe to reuse
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
593 * vlen and wlen across the comparison calls.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
594 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
595 for (i = 0; i < vlen && i < wlen; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
596 int k = PyObject_RichCompareBool(vt->ob_item[i],
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
597 wt->ob_item[i], Py_EQ);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
598 if (k < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
599 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
600 if (!k)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
601 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
602 }
15973
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
603
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
604 if (i >= vlen || i >= wlen) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
605 /* No more items to compare -- compare sizes */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
606 int cmp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
607 PyObject *res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
608 switch (op) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
609 case Py_LT: cmp = vlen < wlen; break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
610 case Py_LE: cmp = vlen <= wlen; break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
611 case Py_EQ: cmp = vlen == wlen; break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
612 case Py_NE: cmp = vlen != wlen; break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
613 case Py_GT: cmp = vlen > wlen; break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
614 case Py_GE: cmp = vlen >= wlen; break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
615 default: return NULL; /* cannot happen */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
616 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
617 if (cmp)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
618 res = Py_True;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
619 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
620 res = Py_False;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
621 Py_INCREF(res);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
622 return res;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
623 }
15973
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
624
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
625 /* We have an item that differs -- shortcuts for EQ/NE */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
626 if (op == Py_EQ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
627 Py_INCREF(Py_False);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
628 return Py_False;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
629 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
630 if (op == Py_NE) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
631 Py_INCREF(Py_True);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
632 return Py_True;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
633 }
15973
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
634
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
635 /* Compare the final item again using the proper operator */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
636 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
15973
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
637 }
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
638
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24184
diff changeset
639 static PyObject *
19592
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
640 tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
641
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
642 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
643 tuple_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
644 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
645 PyObject *arg = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
646 static char *kwlist[] = {"sequence", 0};
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
647
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
648 if (type != &PyTuple_Type)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
649 return tuple_subtype_new(type, args, kwds);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
650 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:tuple", kwlist, &arg))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
651 return NULL;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
652
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
653 if (arg == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
654 return PyTuple_New(0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
655 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
656 return PySequence_Tuple(arg);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
657 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
658
19592
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
659 static PyObject *
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
660 tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
661 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
662 PyObject *tmp, *newobj, *item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
663 Py_ssize_t i, n;
19592
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
664
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
665 assert(PyType_IsSubtype(type, &PyTuple_Type));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
666 tmp = tuple_new(&PyTuple_Type, args, kwds);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
667 if (tmp == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
668 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
669 assert(PyTuple_Check(tmp));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
670 newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
671 if (newobj == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
672 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
673 for (i = 0; i < n; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
674 item = PyTuple_GET_ITEM(tmp, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
675 Py_INCREF(item);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
676 PyTuple_SET_ITEM(newobj, i, item);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
677 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
678 Py_DECREF(tmp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
679 return newobj;
19592
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
680 }
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
681
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
682 PyDoc_STRVAR(tuple_doc,
59205
1c6fc70f49d1 #8030: more docstring fix for builtin types.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 57665
diff changeset
683 "tuple() -> empty tuple\n\
1c6fc70f49d1 #8030: more docstring fix for builtin types.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 57665
diff changeset
684 tuple(iterable) -> tuple initialized from iterable's items\n\
1c6fc70f49d1 #8030: more docstring fix for builtin types.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 57665
diff changeset
685 \n\
1c6fc70f49d1 #8030: more docstring fix for builtin types.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 57665
diff changeset
686 If the argument is a tuple, the return value is the same object.");
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
687
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
688 static PySequenceMethods tuple_as_sequence = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
689 (lenfunc)tuplelength, /* sq_length */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
690 (binaryfunc)tupleconcat, /* sq_concat */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
691 (ssizeargfunc)tuplerepeat, /* sq_repeat */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
692 (ssizeargfunc)tupleitem, /* sq_item */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
693 (ssizessizeargfunc)tupleslice, /* sq_slice */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
694 0, /* sq_ass_item */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
695 0, /* sq_ass_slice */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
696 (objobjproc)tuplecontains, /* sq_contains */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
697 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
698
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
699 static PyObject*
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
700 tuplesubscript(PyTupleObject* self, PyObject* item)
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
701 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
702 if (PyIndex_Check(item)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
703 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
704 if (i == -1 && PyErr_Occurred())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
705 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
706 if (i < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
707 i += PyTuple_GET_SIZE(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
708 return tupleitem(self, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
709 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
710 else if (PySlice_Check(item)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
711 Py_ssize_t start, stop, step, slicelength, cur, i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
712 PyObject* result;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
713 PyObject* it;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
714 PyObject **src, **dest;
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
715
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
716 if (PySlice_GetIndicesEx((PySliceObject*)item,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
717 PyTuple_GET_SIZE(self),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
718 &start, &stop, &step, &slicelength) < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
719 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
720 }
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
721
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
722 if (slicelength <= 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
723 return PyTuple_New(0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
724 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
725 else if (start == 0 && step == 1 &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
726 slicelength == PyTuple_GET_SIZE(self) &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
727 PyTuple_CheckExact(self)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
728 Py_INCREF(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
729 return (PyObject *)self;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
730 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
731 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
732 result = PyTuple_New(slicelength);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
733 if (!result) return NULL;
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
734
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
735 src = self->ob_item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
736 dest = ((PyTupleObject *)result)->ob_item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
737 for (cur = start, i = 0; i < slicelength;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
738 cur += step, i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
739 it = src[cur];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
740 Py_INCREF(it);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
741 dest[i] = it;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
742 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
743
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
744 return result;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
745 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
746 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
747 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
748 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
749 "tuple indices must be integers, not %.200s",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
750 Py_TYPE(item)->tp_name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
751 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
752 }
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
753 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
754
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 25740
diff changeset
755 static PyObject *
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 25740
diff changeset
756 tuple_getnewargs(PyTupleObject *v)
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 25740
diff changeset
757 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
758 return Py_BuildValue("(N)", tupleslice(v, 0, Py_SIZE(v)));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
759
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 25740
diff changeset
760 }
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 25740
diff changeset
761
47682
2dfaa0683be8 Fixed: sys.getsizeof does not take the actual length of the tuples into account.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47527
diff changeset
762 static PyObject *
2dfaa0683be8 Fixed: sys.getsizeof does not take the actual length of the tuples into account.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47527
diff changeset
763 tuple_sizeof(PyTupleObject *self)
2dfaa0683be8 Fixed: sys.getsizeof does not take the actual length of the tuples into account.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47527
diff changeset
764 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
765 Py_ssize_t res;
47682
2dfaa0683be8 Fixed: sys.getsizeof does not take the actual length of the tuples into account.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47527
diff changeset
766
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
767 res = PyTuple_Type.tp_basicsize + Py_SIZE(self) * sizeof(PyObject *);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
768 return PyInt_FromSsize_t(res);
47682
2dfaa0683be8 Fixed: sys.getsizeof does not take the actual length of the tuples into account.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47527
diff changeset
769 }
2dfaa0683be8 Fixed: sys.getsizeof does not take the actual length of the tuples into account.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47527
diff changeset
770
45207
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 45191
diff changeset
771 PyDoc_STRVAR(index_doc,
49431
2e794757bdc8 Merged revisions 66766-66767,66771-66772,66774,66776,66783-66787,66790,66793,66797 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48114
diff changeset
772 "T.index(value, [start, [stop]]) -> integer -- return first index of value.\n"
2e794757bdc8 Merged revisions 66766-66767,66771-66772,66774,66776,66783-66787,66790,66793,66797 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48114
diff changeset
773 "Raises ValueError if the value is not present."
2e794757bdc8 Merged revisions 66766-66767,66771-66772,66774,66776,66783-66787,66790,66793,66797 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48114
diff changeset
774 );
45207
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 45191
diff changeset
775 PyDoc_STRVAR(count_doc,
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 45191
diff changeset
776 "T.count(value) -> integer -- return number of occurrences of value");
47682
2dfaa0683be8 Fixed: sys.getsizeof does not take the actual length of the tuples into account.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47527
diff changeset
777 PyDoc_STRVAR(sizeof_doc,
2dfaa0683be8 Fixed: sys.getsizeof does not take the actual length of the tuples into account.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47527
diff changeset
778 "T.__sizeof__() -- size of T in memory, in bytes");
45207
3f124975e099 Issue 2025: Add tuple.count() and tuple.index() to follow the ABC in collections.Sequence.
Raymond Hettinger <python@rcn.com>
parents: 45191
diff changeset
779
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 25740
diff changeset
780 static PyMethodDef tuple_methods[] = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
781 {"__getnewargs__", (PyCFunction)tuple_getnewargs, METH_NOARGS},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
782 {"__sizeof__", (PyCFunction)tuple_sizeof, METH_NOARGS, sizeof_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
783 {"index", (PyCFunction)tupleindex, METH_VARARGS, index_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
784 {"count", (PyCFunction)tuplecount, METH_O, count_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
785 {NULL, NULL} /* sentinel */
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 25740
diff changeset
786 };
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 25740
diff changeset
787
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
788 static PyMappingMethods tuple_as_mapping = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
789 (lenfunc)tuplelength,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
790 (binaryfunc)tuplesubscript,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
791 0
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
792 };
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
793
24683
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
794 static PyObject *tuple_iter(PyObject *seq);
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
795
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
796 PyTypeObject PyTuple_Type = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
797 PyVarObject_HEAD_INIT(&PyType_Type, 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
798 "tuple",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
799 sizeof(PyTupleObject) - sizeof(PyObject *),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
800 sizeof(PyObject *),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
801 (destructor)tupledealloc, /* tp_dealloc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
802 (printfunc)tupleprint, /* tp_print */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
803 0, /* tp_getattr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
804 0, /* tp_setattr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
805 0, /* tp_compare */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
806 (reprfunc)tuplerepr, /* tp_repr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
807 0, /* tp_as_number */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
808 &tuple_as_sequence, /* tp_as_sequence */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
809 &tuple_as_mapping, /* tp_as_mapping */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
810 (hashfunc)tuplehash, /* tp_hash */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
811 0, /* tp_call */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
812 0, /* tp_str */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
813 PyObject_GenericGetAttr, /* tp_getattro */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
814 0, /* tp_setattro */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
815 0, /* tp_as_buffer */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
816 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
817 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS, /* tp_flags */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
818 tuple_doc, /* tp_doc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
819 (traverseproc)tupletraverse, /* tp_traverse */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
820 0, /* tp_clear */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
821 tuplerichcompare, /* tp_richcompare */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
822 0, /* tp_weaklistoffset */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
823 tuple_iter, /* tp_iter */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
824 0, /* tp_iternext */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
825 tuple_methods, /* tp_methods */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
826 0, /* tp_members */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
827 0, /* tp_getset */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
828 0, /* tp_base */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
829 0, /* tp_dict */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
830 0, /* tp_descr_get */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
831 0, /* tp_descr_set */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
832 0, /* tp_dictoffset */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
833 0, /* tp_init */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
834 0, /* tp_alloc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
835 tuple_new, /* tp_new */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
836 PyObject_GC_Del, /* tp_free */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
837 };
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
838
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
839 /* 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
840 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
841 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
842 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
843 efficiently. In any case, don't use this if the tuple may already be
17976
d579a5d8b3fe Cruft cleanup: Removed the unused last_is_sticky argument from the internal
Tim Peters <tim.peters@gmail.com>
parents: 17975
diff changeset
844 known to some other part of the code. */
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
845
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
846 int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
847 _PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
848 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
849 register PyTupleObject *v;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
850 register PyTupleObject *sv;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
851 Py_ssize_t i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
852 Py_ssize_t oldsize;
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
853
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
854 v = (PyTupleObject *) *pv;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
855 if (v == NULL || Py_TYPE(v) != &PyTuple_Type ||
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
856 (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
857 *pv = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
858 Py_XDECREF(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
859 PyErr_BadInternalCall();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
860 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
861 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
862 oldsize = Py_SIZE(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
863 if (oldsize == newsize)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
864 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
865
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
866 if (oldsize == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
867 /* Empty tuples are often shared, so we should never
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
868 resize them in-place even if we do own the only
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
869 (current) reference */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
870 Py_DECREF(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
871 *pv = PyTuple_New(newsize);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
872 return *pv == NULL ? -1 : 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
873 }
17975
527d0ffdf099 _PyTuple_Resize: take into account the empty tuple. There can be only one.
Thomas Wouters <thomas@python.org>
parents: 17841
diff changeset
874
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
875 /* XXX UNREF/NEWREF interface should be more symmetrical */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
876 _Py_DEC_REFTOTAL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
877 if (_PyObject_GC_IS_TRACKED(v))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
878 _PyObject_GC_UNTRACK(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
879 _Py_ForgetReference((PyObject *) v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
880 /* DECREF items deleted by shrinkage */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
881 for (i = newsize; i < oldsize; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
882 Py_XDECREF(v->ob_item[i]);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
883 v->ob_item[i] = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
884 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
885 sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
886 if (sv == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
887 *pv = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
888 PyObject_GC_Del(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
889 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
890 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
891 _Py_NewReference((PyObject *) sv);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
892 /* Zero out items added by growing */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
893 if (newsize > oldsize)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
894 memset(&sv->ob_item[oldsize], 0,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
895 sizeof(*sv->ob_item) * (newsize - oldsize));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
896 *pv = (PyObject *) sv;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
897 _PyObject_GC_TRACK(sv);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
898 return 0;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
899 }
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
900
45341
65ae68c7aca3 Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents: 45210
diff changeset
901 int
65ae68c7aca3 Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents: 45210
diff changeset
902 PyTuple_ClearFreeList(void)
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
903 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
904 int freelist_size = 0;
45191
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
905 #if PyTuple_MAXSAVESIZE > 0
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
906 int i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
907 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
908 PyTupleObject *p, *q;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
909 p = free_list[i];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
910 freelist_size += numfree[i];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
911 free_list[i] = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
912 numfree[i] = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
913 while (p) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
914 q = p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
915 p = (PyTupleObject *)(p->ob_item[0]);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
916 PyObject_GC_Del(q);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
917 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
918 }
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
919 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
920 return freelist_size;
45341
65ae68c7aca3 Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents: 45210
diff changeset
921 }
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
922
45341
65ae68c7aca3 Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents: 45210
diff changeset
923 void
65ae68c7aca3 Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents: 45210
diff changeset
924 PyTuple_Fini(void)
65ae68c7aca3 Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents: 45210
diff changeset
925 {
65ae68c7aca3 Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents: 45210
diff changeset
926 #if PyTuple_MAXSAVESIZE > 0
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
927 /* empty tuples are used all over the place and applications may
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
928 * rely on the fact that an empty tuple is a singleton. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
929 Py_XDECREF(free_list[0]);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
930 free_list[0] = NULL;
45341
65ae68c7aca3 Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents: 45210
diff changeset
931
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
932 (void)PyTuple_ClearFreeList();
45341
65ae68c7aca3 Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents: 45210
diff changeset
933 #endif
52288
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
934 #ifdef SHOW_TRACK_COUNT
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
935 show_track();
52288
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
936 #endif
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
937 }
24683
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
938
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
939 /*********************** Tuple Iterator **************************/
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
940
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
941 typedef struct {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
942 PyObject_HEAD
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
943 long it_index;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
944 PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
24683
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
945 } tupleiterobject;
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
946
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
947 static void
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
948 tupleiter_dealloc(tupleiterobject *it)
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
949 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
950 _PyObject_GC_UNTRACK(it);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
951 Py_XDECREF(it->it_seq);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
952 PyObject_GC_Del(it);
24683
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
953 }
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
954
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
955 static int
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
956 tupleiter_traverse(tupleiterobject *it, visitproc visit, void *arg)
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
957 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
958 Py_VISIT(it->it_seq);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
959 return 0;
24683
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
960 }
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
961
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
962 static PyObject *
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
963 tupleiter_next(tupleiterobject *it)
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
964 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
965 PyTupleObject *seq;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
966 PyObject *item;
24683
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
967
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
968 assert(it != NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
969 seq = it->it_seq;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
970 if (seq == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
971 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
972 assert(PyTuple_Check(seq));
24683
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
973
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
974 if (it->it_index < PyTuple_GET_SIZE(seq)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
975 item = PyTuple_GET_ITEM(seq, it->it_index);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
976 ++it->it_index;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
977 Py_INCREF(item);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
978 return item;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
979 }
24683
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
980
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
981 Py_DECREF(seq);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
982 it->it_seq = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
983 return NULL;
24683
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
984 }
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
985
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 32318
diff changeset
986 static PyObject *
31686
823279ae43eb Make iterators length transparent where possible.
Raymond Hettinger <python@rcn.com>
parents: 31663
diff changeset
987 tupleiter_len(tupleiterobject *it)
823279ae43eb Make iterators length transparent where possible.
Raymond Hettinger <python@rcn.com>
parents: 31663
diff changeset
988 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
989 Py_ssize_t len = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
990 if (it->it_seq)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
991 len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
992 return PyInt_FromSsize_t(len);
31686
823279ae43eb Make iterators length transparent where possible.
Raymond Hettinger <python@rcn.com>
parents: 31663
diff changeset
993 }
823279ae43eb Make iterators length transparent where possible.
Raymond Hettinger <python@rcn.com>
parents: 31663
diff changeset
994
36484
20f75d5e79e9 Renamed _length_cue() to __length_hint__(). See:
Armin Rigo <arigo@tunes.org>
parents: 35999
diff changeset
995 PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 32318
diff changeset
996
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 32318
diff changeset
997 static PyMethodDef tupleiter_methods[] = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
998 {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
999 {NULL, NULL} /* sentinel */
31686
823279ae43eb Make iterators length transparent where possible.
Raymond Hettinger <python@rcn.com>
parents: 31663
diff changeset
1000 };
823279ae43eb Make iterators length transparent where possible.
Raymond Hettinger <python@rcn.com>
parents: 31663
diff changeset
1001
24683
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
1002 PyTypeObject PyTupleIter_Type = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1003 PyVarObject_HEAD_INIT(&PyType_Type, 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1004 "tupleiterator", /* tp_name */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1005 sizeof(tupleiterobject), /* tp_basicsize */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1006 0, /* tp_itemsize */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1007 /* methods */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1008 (destructor)tupleiter_dealloc, /* tp_dealloc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1009 0, /* tp_print */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1010 0, /* tp_getattr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1011 0, /* tp_setattr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1012 0, /* tp_compare */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1013 0, /* tp_repr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1014 0, /* tp_as_number */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1015 0, /* tp_as_sequence */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1016 0, /* tp_as_mapping */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1017 0, /* tp_hash */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1018 0, /* tp_call */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1019 0, /* tp_str */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1020 PyObject_GenericGetAttr, /* tp_getattro */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1021 0, /* tp_setattro */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1022 0, /* tp_as_buffer */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1023 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1024 0, /* tp_doc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1025 (traverseproc)tupleiter_traverse, /* tp_traverse */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1026 0, /* tp_clear */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1027 0, /* tp_richcompare */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1028 0, /* tp_weaklistoffset */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1029 PyObject_SelfIter, /* tp_iter */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1030 (iternextfunc)tupleiter_next, /* tp_iternext */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1031 tupleiter_methods, /* tp_methods */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1032 0,
24683
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
1033 };
37602
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37596
diff changeset
1034
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37596
diff changeset
1035 static PyObject *
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37596
diff changeset
1036 tuple_iter(PyObject *seq)
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37596
diff changeset
1037 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1038 tupleiterobject *it;
37602
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37596
diff changeset
1039
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1040 if (!PyTuple_Check(seq)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1041 PyErr_BadInternalCall();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1042 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1043 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1044 it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1045 if (it == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1046 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1047 it->it_index = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1048 Py_INCREF(seq);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1049 it->it_seq = (PyTupleObject *)seq;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1050 _PyObject_GC_TRACK(it);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61265
diff changeset
1051 return (PyObject *)it;
37602
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37596
diff changeset
1052 }