annotate Objects/tupleobject.c @ 58765:2ba7677bfa95 legacy-trunk

testCopysign was defined twice in test_math; combine the definitions
author Mark Dickinson <dickinsm@gmail.com>
date Sat, 06 Feb 2010 23:11:25 +0000
parents 2ffd19a224d4
children 1c6fc70f49d1
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
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 44350
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
45191
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
10 #ifndef PyTuple_MAXFREELIST
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 {
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
38 fprintf(stderr, "Tuples created: %" PY_FORMAT_SIZE_T "d\n",
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
39 count_tracked + count_untracked);
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
40 fprintf(stderr, "Tuples tracked by the GC: %" PY_FORMAT_SIZE_T
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
41 "d\n", count_tracked);
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
42 fprintf(stderr, "%.2f%% tuple tracking rate\n\n",
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
43 (100.0*count_tracked/(count_untracked+count_tracked)));
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 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
51 register PyTupleObject *op;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
52 Py_ssize_t i;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 if (size < 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
54 PyErr_BadInternalCall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
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
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
58 if (size == 0 && free_list[0]) {
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
59 op = free_list[0];
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
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
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
62 tuple_zero_allocs++;
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
63 #endif
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
64 return (PyObject *) op;
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
65 }
45191
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
66 if (size < PyTuple_MAXSAVESIZE && (op = free_list[size]) != NULL) {
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
67 free_list[size] = (PyTupleObject *) op->ob_item[0];
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 44350
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
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
70 fast_tuple_allocs++;
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
71 #endif
24898
a869d2de536f Call me anal, but there was a particular phrase that was speading to
Guido van Rossum <guido@python.org>
parents: 24683
diff changeset
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
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43452
diff changeset
74 Py_SIZE(op) = size;
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43452
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
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12516
diff changeset
77 _Py_NewReference((PyObject *)op);
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
78 }
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
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
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
81 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
82 Py_ssize_t nbytes = size * sizeof(PyObject *);
11127
9a20038f0b99 Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents: 9632
diff changeset
83 /* Check for overflow */
9a20038f0b99 Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents: 9632
diff changeset
84 if (nbytes / sizeof(PyObject *) != (size_t)size ||
48114
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 47682
diff changeset
85 (nbytes > PY_SSIZE_T_MAX - sizeof(PyTupleObject) - sizeof(PyObject *)))
11127
9a20038f0b99 Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents: 9632
diff changeset
86 {
9a20038f0b99 Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents: 9632
diff changeset
87 return PyErr_NoMemory();
9a20038f0b99 Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents: 9632
diff changeset
88 }
48114
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 47682
diff changeset
89 nbytes += sizeof(PyTupleObject) - sizeof(PyObject *);
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 47682
diff changeset
90
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18933
diff changeset
91 op = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size);
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
92 if (op == NULL)
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18933
diff changeset
93 return NULL;
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
94 }
31759
be5d2a07bc26 Restored revision 2.87.
Armin Rigo <arigo@tunes.org>
parents: 31756
diff changeset
95 for (i=0; i < size; i++)
be5d2a07bc26 Restored revision 2.87.
Armin Rigo <arigo@tunes.org>
parents: 31756
diff changeset
96 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
97 #if PyTuple_MAXSAVESIZE > 0
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
98 if (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
99 free_list[0] = op;
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
100 ++numfree[0];
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
101 Py_INCREF(op); /* extra INCREF so that this is never freed */
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
102 }
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
103 #endif
52292
b3791309e690 The tracking statistics were actually too pessimistic
Antoine Pitrou <solipsis@pitrou.net>
parents: 52288
diff changeset
104 #ifdef SHOW_TRACK_COUNT
b3791309e690 The tracking statistics were actually too pessimistic
Antoine Pitrou <solipsis@pitrou.net>
parents: 52288
diff changeset
105 count_tracked++;
b3791309e690 The tracking statistics were actually too pessimistic
Antoine Pitrou <solipsis@pitrou.net>
parents: 52288
diff changeset
106 #endif
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18933
diff changeset
107 _PyObject_GC_TRACK(op);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
108 return (PyObject *) op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
110
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
111 Py_ssize_t
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
112 PyTuple_Size(register PyObject *op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
114 if (!PyTuple_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
115 PyErr_BadInternalCall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 return -1;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 else
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43452
diff changeset
119 return Py_SIZE(op);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
121
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
122 PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
123 PyTuple_GetItem(register PyObject *op, register Py_ssize_t i)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
125 if (!PyTuple_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
126 PyErr_BadInternalCall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 }
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43452
diff changeset
129 if (i < 0 || i >= Py_SIZE(op)) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
130 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
133 return ((PyTupleObject *)op) -> ob_item[i];
6
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
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
137 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
138 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
139 register PyObject *olditem;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
140 register PyObject **p;
6083
f159044530b1 PyTuple_SetItem should require that the tuple's refcnt is one!
Guido van Rossum <guido@python.org>
parents: 5936
diff changeset
141 if (!PyTuple_Check(op) || op->ob_refcnt != 1) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
142 Py_XDECREF(newitem);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
143 PyErr_BadInternalCall();
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
144 return -1;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 }
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43452
diff changeset
146 if (i < 0 || i >= Py_SIZE(op)) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
147 Py_XDECREF(newitem);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
148 PyErr_SetString(PyExc_IndexError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
149 "tuple assignment index out of range");
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
150 return -1;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
152 p = ((PyTupleObject *)op) -> ob_item + i;
2504
425835b88099 a few peephole optimizations
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
153 olditem = *p;
425835b88099 a few peephole optimizations
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
154 *p = newitem;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
155 Py_XDECREF(olditem);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 return 0;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
158
52288
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
159 void
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
160 _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
161 {
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
162 PyTupleObject *t;
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
163 Py_ssize_t i, n;
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
164
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
165 if (!PyTuple_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
166 return;
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
167 t = (PyTupleObject *) op;
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
168 n = Py_SIZE(t);
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
169 for (i = 0; i < n; i++) {
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
170 PyObject *elt = PyTuple_GET_ITEM(t, i);
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
171 /* Tuple with NULL elements aren't
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
172 fully constructed, don't untrack
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
173 them yet. */
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
174 if (!elt ||
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
175 _PyObject_GC_MAY_BE_TRACKED(elt))
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
176 return;
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
177 }
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
178 #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
179 count_tracked--;
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
180 count_untracked++;
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
181 #endif
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
182 _PyObject_GC_UNTRACK(op);
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
183 }
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
184
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
185 PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
186 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
187 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
188 Py_ssize_t i;
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
189 PyObject *o;
473f4e2959a8 Extended tuple's C API to include a new function, PyTuple_Pack() that is
Raymond Hettinger <python@rcn.com>
parents: 29008
diff changeset
190 PyObject *result;
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31623
diff changeset
191 PyObject **items;
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 va_list vargs;
473f4e2959a8 Extended tuple's C API to include a new function, PyTuple_Pack() that is
Raymond Hettinger <python@rcn.com>
parents: 29008
diff changeset
193
473f4e2959a8 Extended tuple's C API to include a new function, PyTuple_Pack() that is
Raymond Hettinger <python@rcn.com>
parents: 29008
diff changeset
194 va_start(vargs, n);
473f4e2959a8 Extended tuple's C API to include a new function, PyTuple_Pack() that is
Raymond Hettinger <python@rcn.com>
parents: 29008
diff changeset
195 result = PyTuple_New(n);
473f4e2959a8 Extended tuple's C API to include a new function, PyTuple_Pack() that is
Raymond Hettinger <python@rcn.com>
parents: 29008
diff changeset
196 if (result == NULL)
473f4e2959a8 Extended tuple's C API to include a new function, PyTuple_Pack() that is
Raymond Hettinger <python@rcn.com>
parents: 29008
diff changeset
197 return NULL;
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31623
diff changeset
198 items = ((PyTupleObject *)result)->ob_item;
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
199 for (i = 0; i < n; i++) {
473f4e2959a8 Extended tuple's C API to include a new function, PyTuple_Pack() that is
Raymond Hettinger <python@rcn.com>
parents: 29008
diff changeset
200 o = va_arg(vargs, PyObject *);
473f4e2959a8 Extended tuple's C API to include a new function, PyTuple_Pack() that is
Raymond Hettinger <python@rcn.com>
parents: 29008
diff changeset
201 Py_INCREF(o);
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31623
diff changeset
202 items[i] = o;
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
203 }
473f4e2959a8 Extended tuple's C API to include a new function, PyTuple_Pack() that is
Raymond Hettinger <python@rcn.com>
parents: 29008
diff changeset
204 va_end(vargs);
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 return result;
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
473f4e2959a8 Extended tuple's C API to include a new function, PyTuple_Pack() that is
Raymond Hettinger <python@rcn.com>
parents: 29008
diff changeset
208
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 /* Methods */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
210
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 static void
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
212 tupledealloc(register PyTupleObject *op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
214 register Py_ssize_t i;
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43452
diff changeset
215 register Py_ssize_t len = Py_SIZE(op);
22626
0446eeb787b7 Backport to 2.2.1:
Guido van Rossum <guido@python.org>
parents: 21293
diff changeset
216 PyObject_GC_UnTrack(op);
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
217 Py_TRASHCAN_SAFE_BEGIN(op)
12439
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
218 if (len > 0) {
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
219 i = len;
31759
be5d2a07bc26 Restored revision 2.87.
Armin Rigo <arigo@tunes.org>
parents: 31756
diff changeset
220 while (--i >= 0)
be5d2a07bc26 Restored revision 2.87.
Armin Rigo <arigo@tunes.org>
parents: 31756
diff changeset
221 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
222 #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
223 if (len < PyTuple_MAXSAVESIZE &&
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
224 numfree[len] < PyTuple_MAXFREELIST &&
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43452
diff changeset
225 Py_TYPE(op) == &PyTuple_Type)
19612
c396afef1aa9 More stuff discovered while writing the simplest of testcases:
Guido van Rossum <guido@python.org>
parents: 19592
diff changeset
226 {
45191
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
227 op->ob_item[0] = (PyObject *) free_list[len];
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
228 numfree[len]++;
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
229 free_list[len] = op;
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
230 goto done; /* return */
8559
92267838feb9 Slight rearrangement of some code to make it faster, by Vladimir
Guido van Rossum <guido@python.org>
parents: 6083
diff changeset
231 }
1352
4d1a15fc4748 intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
232 #endif
8559
92267838feb9 Slight rearrangement of some code to make it faster, by Vladimir
Guido van Rossum <guido@python.org>
parents: 6083
diff changeset
233 }
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43452
diff changeset
234 Py_TYPE(op)->tp_free((PyObject *)op);
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
235 done:
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
236 Py_TRASHCAN_SAFE_END(op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
238
288
9b43a983124c printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 258
diff changeset
239 static int
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
240 tupleprint(PyTupleObject *op, FILE *fp, int flags)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
242 Py_ssize_t i;
43378
7630248b593c Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents: 42951
diff changeset
243 Py_BEGIN_ALLOW_THREADS
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 fprintf(fp, "(");
43378
7630248b593c Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents: 42951
diff changeset
245 Py_END_ALLOW_THREADS
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43452
diff changeset
246 for (i = 0; i < Py_SIZE(op); i++) {
43378
7630248b593c Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents: 42951
diff changeset
247 if (i > 0) {
7630248b593c Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents: 42951
diff changeset
248 Py_BEGIN_ALLOW_THREADS
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 fprintf(fp, ", ");
43378
7630248b593c Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents: 42951
diff changeset
250 Py_END_ALLOW_THREADS
7630248b593c Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents: 42951
diff changeset
251 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
252 if (PyObject_Print(op->ob_item[i], fp, 0) != 0)
288
9b43a983124c printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 258
diff changeset
253 return -1;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 }
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43452
diff changeset
255 i = Py_SIZE(op);
43378
7630248b593c Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents: 42951
diff changeset
256 Py_BEGIN_ALLOW_THREADS
7630248b593c Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents: 42951
diff changeset
257 if (i == 1)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 fprintf(fp, ",");
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 fprintf(fp, ")");
43378
7630248b593c Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents: 42951
diff changeset
260 Py_END_ALLOW_THREADS
288
9b43a983124c printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 258
diff changeset
261 return 0;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
263
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
264 static PyObject *
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
265 tuplerepr(PyTupleObject *v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
267 Py_ssize_t i, n;
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
268 PyObject *s, *temp;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
269 PyObject *pieces, *result = NULL;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
270
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43452
diff changeset
271 n = Py_SIZE(v);
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
272 if (n == 0)
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
273 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
274
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
275 /* While not mutable, it is still possible to end up with a cycle in a
381ed41003e7 tuple.__repr__ did not consider a reference loop as it is not possible from
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
276 tuple through an object that stores itself within a tuple (and thus
381ed41003e7 tuple.__repr__ did not consider a reference loop as it is not possible from
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
277 infinitely asks for the repr of itself). This should only be
381ed41003e7 tuple.__repr__ did not consider a reference loop as it is not possible from
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
278 possible within a type. */
381ed41003e7 tuple.__repr__ did not consider a reference loop as it is not possible from
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
279 i = Py_ReprEnter((PyObject *)v);
381ed41003e7 tuple.__repr__ did not consider a reference loop as it is not possible from
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
280 if (i != 0) {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
281 return i > 0 ? PyString_FromString("(...)") : NULL;
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 }
381ed41003e7 tuple.__repr__ did not consider a reference loop as it is not possible from
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
283
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
284 pieces = PyTuple_New(n);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
285 if (pieces == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
286 return NULL;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
287
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
288 /* Do repr() on each element. */
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
289 for (i = 0; i < n; ++i) {
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
290 if (Py_EnterRecursiveCall(" while getting the repr of a tuple"))
381ed41003e7 tuple.__repr__ did not consider a reference loop as it is not possible from
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
291 goto Done;
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
292 s = PyObject_Repr(v->ob_item[i]);
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
293 Py_LeaveRecursiveCall();
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
294 if (s == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
295 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
296 PyTuple_SET_ITEM(pieces, i, s);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
297 }
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
298
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
299 /* Add "()" decorations to the first and last items. */
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
300 assert(n > 0);
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
301 s = PyString_FromString("(");
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
302 if (s == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
303 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
304 temp = PyTuple_GET_ITEM(pieces, 0);
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
305 PyString_ConcatAndDel(&s, temp);
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
306 PyTuple_SET_ITEM(pieces, 0, s);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
307 if (s == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
308 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
309
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
310 s = PyString_FromString(n == 1 ? ",)" : ")");
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
311 if (s == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
312 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
313 temp = PyTuple_GET_ITEM(pieces, n-1);
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
314 PyString_ConcatAndDel(&temp, s);
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
315 PyTuple_SET_ITEM(pieces, n-1, temp);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
316 if (temp == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
317 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
318
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
319 /* Paste them all together with ", " between. */
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
320 s = PyString_FromString(", ");
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
321 if (s == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
322 goto Done;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
323 result = _PyString_Join(s, pieces);
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
324 Py_DECREF(s);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
325
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
326 Done:
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
327 Py_DECREF(pieces);
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
328 Py_ReprLeave((PyObject *)v);
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17979
diff changeset
329 return result;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
330 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
331
32242
0bdb49a5a8fa Replaced arbitrary addend in tuple_hash with one that is known to generate
Raymond Hettinger <python@rcn.com>
parents: 32171
diff changeset
332 /* The addend 82520, was selected from the range(0, 1000000) for
0bdb49a5a8fa Replaced arbitrary addend in tuple_hash with one that is known to generate
Raymond Hettinger <python@rcn.com>
parents: 32171
diff changeset
333 generating the greatest number of prime multipliers for tuples
0bdb49a5a8fa Replaced arbitrary addend in tuple_hash with one that is known to generate
Raymond Hettinger <python@rcn.com>
parents: 32171
diff changeset
334 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
335
0bdb49a5a8fa Replaced arbitrary addend in tuple_hash with one that is known to generate
Raymond Hettinger <python@rcn.com>
parents: 32171
diff changeset
336 1082527, 1165049, 1082531, 1165057, 1247581, 1330103, 1082533,
0bdb49a5a8fa Replaced arbitrary addend in tuple_hash with one that is known to generate
Raymond Hettinger <python@rcn.com>
parents: 32171
diff changeset
337 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
338 */
0bdb49a5a8fa Replaced arbitrary addend in tuple_hash with one that is known to generate
Raymond Hettinger <python@rcn.com>
parents: 32171
diff changeset
339
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
340 static long
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
341 tuplehash(PyTupleObject *v)
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
342 {
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
343 register long x, y;
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43452
diff changeset
344 register Py_ssize_t len = Py_SIZE(v);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
345 register PyObject **p;
32171
97b908a9971a SF bug #942952: Weakness in tuple hash
Raymond Hettinger <python@rcn.com>
parents: 31759
diff changeset
346 long mult = 1000003L;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
347 x = 0x345678L;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
348 p = v->ob_item;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
349 while (--len >= 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
350 y = PyObject_Hash(*p++);
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
351 if (y == -1)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
352 return -1;
32171
97b908a9971a SF bug #942952: Weakness in tuple hash
Raymond Hettinger <python@rcn.com>
parents: 31759
diff changeset
353 x = (x ^ y) * mult;
36519
d6d415df0df0 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
354 /* the cast might truncate len; that doesn't change hash stability */
d6d415df0df0 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
355 mult += (long)(82520L + len + len);
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
356 }
32318
418a0c6bdc7d Add a final permutation step to the tuple hash function.
Raymond Hettinger <python@rcn.com>
parents: 32242
diff changeset
357 x += 97531L;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
358 if (x == -1)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
359 x = -2;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
360 return x;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
361 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
362
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
363 static Py_ssize_t
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
364 tuplelength(PyTupleObject *a)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
365 {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43452
diff changeset
366 return Py_SIZE(a);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
367 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
368
12516
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12439
diff changeset
369 static int
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
370 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
371 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
372 Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
373 int cmp;
12516
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12439
diff changeset
374
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43452
diff changeset
375 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
15973
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
376 cmp = PyObject_RichCompareBool(el, PyTuple_GET_ITEM(a, i),
25125
172845c09afe Micro-optimization for list_contains. Factored double if test
Raymond Hettinger <python@rcn.com>
parents: 24898
diff changeset
377 Py_EQ);
25128
f1c69c32c42b PyObject_RichCompareBool() already returns -1, 0, or 1, so return its value
Neal Norwitz <nnorwitz@gmail.com>
parents: 25125
diff changeset
378 return cmp;
12516
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12439
diff changeset
379 }
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12439
diff changeset
380
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
381 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
382 tupleitem(register PyTupleObject *a, register Py_ssize_t i)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
383 {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43452
diff changeset
384 if (i < 0 || i >= Py_SIZE(a)) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
385 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
386 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
387 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
388 Py_INCREF(a->ob_item[i]);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
389 return a->ob_item[i];
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
390 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
391
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
392 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
393 tupleslice(register PyTupleObject *a, register Py_ssize_t ilow,
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
394 register Py_ssize_t ihigh)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
395 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
396 register PyTupleObject *np;
31623
d34e7eaf4cd6 Optimize tuple_slice() and make further improvements to list_slice()
Raymond Hettinger <python@rcn.com>
parents: 30682
diff changeset
397 PyObject **src, **dest;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
398 register Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
399 Py_ssize_t len;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
400 if (ilow < 0)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
401 ilow = 0;
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43452
diff changeset
402 if (ihigh > Py_SIZE(a))
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43452
diff changeset
403 ihigh = Py_SIZE(a);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
404 if (ihigh < ilow)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
405 ihigh = ilow;
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43452
diff changeset
406 if (ilow == 0 && ihigh == Py_SIZE(a) && PyTuple_CheckExact(a)) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
407 Py_INCREF(a);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
408 return (PyObject *)a;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
409 }
31623
d34e7eaf4cd6 Optimize tuple_slice() and make further improvements to list_slice()
Raymond Hettinger <python@rcn.com>
parents: 30682
diff changeset
410 len = ihigh - ilow;
d34e7eaf4cd6 Optimize tuple_slice() and make further improvements to list_slice()
Raymond Hettinger <python@rcn.com>
parents: 30682
diff changeset
411 np = (PyTupleObject *)PyTuple_New(len);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
412 if (np == NULL)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
413 return NULL;
31623
d34e7eaf4cd6 Optimize tuple_slice() and make further improvements to list_slice()
Raymond Hettinger <python@rcn.com>
parents: 30682
diff changeset
414 src = a->ob_item + ilow;
d34e7eaf4cd6 Optimize tuple_slice() and make further improvements to list_slice()
Raymond Hettinger <python@rcn.com>
parents: 30682
diff changeset
415 dest = np->ob_item;
d34e7eaf4cd6 Optimize tuple_slice() and make further improvements to list_slice()
Raymond Hettinger <python@rcn.com>
parents: 30682
diff changeset
416 for (i = 0; i < len; i++) {
d34e7eaf4cd6 Optimize tuple_slice() and make further improvements to list_slice()
Raymond Hettinger <python@rcn.com>
parents: 30682
diff changeset
417 PyObject *v = src[i];
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
418 Py_INCREF(v);
31623
d34e7eaf4cd6 Optimize tuple_slice() and make further improvements to list_slice()
Raymond Hettinger <python@rcn.com>
parents: 30682
diff changeset
419 dest[i] = v;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
420 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
421 return (PyObject *)np;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
422 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
423
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
424 PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
425 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
426 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
427 if (op == NULL || !PyTuple_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
428 PyErr_BadInternalCall();
599
a347933a6d92 New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents: 288
diff changeset
429 return NULL;
a347933a6d92 New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents: 288
diff changeset
430 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
431 return tupleslice((PyTupleObject *)op, i, j);
599
a347933a6d92 New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents: 288
diff changeset
432 }
a347933a6d92 New function gettupleslice(v, i, j).
Guido van Rossum <guido@python.org>
parents: 288
diff changeset
433
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
434 static PyObject *
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
435 tupleconcat(register PyTupleObject *a, register PyObject *bb)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
436 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
437 register Py_ssize_t size;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
438 register Py_ssize_t i;
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31623
diff changeset
439 PyObject **src, **dest;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
440 PyTupleObject *np;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
441 if (!PyTuple_Check(bb)) {
12816
4272b368bf69 Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
442 PyErr_Format(PyExc_TypeError,
12929
2eef636ba1b6 Michael Hudson <mwh21@cam.ac.uk>:
Marc-André Lemburg <mal@egenix.com>
parents: 12925
diff changeset
443 "can only concatenate tuple (not \"%.200s\") to tuple",
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43452
diff changeset
444 Py_TYPE(bb)->tp_name);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
445 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
446 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
447 #define b ((PyTupleObject *)bb)
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43452
diff changeset
448 size = Py_SIZE(a) + Py_SIZE(b);
25740
70f9026a2b16 Add checks for size overflow on list*n, list+list, tuple+tuple.
Guido van Rossum <guido@python.org>
parents: 25128
diff changeset
449 if (size < 0)
70f9026a2b16 Add checks for size overflow on list*n, list+list, tuple+tuple.
Guido van Rossum <guido@python.org>
parents: 25128
diff changeset
450 return PyErr_NoMemory();
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
451 np = (PyTupleObject *) PyTuple_New(size);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
452 if (np == NULL) {
288
9b43a983124c printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 258
diff changeset
453 return NULL;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
454 }
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31623
diff changeset
455 src = a->ob_item;
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31623
diff changeset
456 dest = np->ob_item;
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43452
diff changeset
457 for (i = 0; i < Py_SIZE(a); i++) {
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31623
diff changeset
458 PyObject *v = src[i];
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
459 Py_INCREF(v);
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31623
diff changeset
460 dest[i] = v;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
461 }
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31623
diff changeset
462 src = b->ob_item;
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43452
diff changeset
463 dest = np->ob_item + Py_SIZE(a);
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43452
diff changeset
464 for (i = 0; i < Py_SIZE(b); i++) {
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31623
diff changeset
465 PyObject *v = src[i];
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
466 Py_INCREF(v);
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31623
diff changeset
467 dest[i] = v;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
468 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
469 return (PyObject *)np;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
470 #undef b
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
471 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
472
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
473 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
474 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
475 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
476 Py_ssize_t i, j;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
477 Py_ssize_t size;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
478 PyTupleObject *np;
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31623
diff changeset
479 PyObject **p, **items;
258
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
480 if (n < 0)
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
481 n = 0;
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43452
diff changeset
482 if (Py_SIZE(a) == 0 || n == 1) {
19872
51363348dcfa The endless 460020 bug.
Tim Peters <tim.peters@gmail.com>
parents: 19657
diff changeset
483 if (PyTuple_CheckExact(a)) {
51363348dcfa The endless 460020 bug.
Tim Peters <tim.peters@gmail.com>
parents: 19657
diff changeset
484 /* Since tuples are immutable, we can return a shared
51363348dcfa The endless 460020 bug.
Tim Peters <tim.peters@gmail.com>
parents: 19657
diff changeset
485 copy in this case */
51363348dcfa The endless 460020 bug.
Tim Peters <tim.peters@gmail.com>
parents: 19657
diff changeset
486 Py_INCREF(a);
51363348dcfa The endless 460020 bug.
Tim Peters <tim.peters@gmail.com>
parents: 19657
diff changeset
487 return (PyObject *)a;
51363348dcfa The endless 460020 bug.
Tim Peters <tim.peters@gmail.com>
parents: 19657
diff changeset
488 }
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43452
diff changeset
489 if (Py_SIZE(a) == 0)
19872
51363348dcfa The endless 460020 bug.
Tim Peters <tim.peters@gmail.com>
parents: 19657
diff changeset
490 return PyTuple_New(0);
258
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
491 }
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43452
diff changeset
492 size = Py_SIZE(a) * n;
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43452
diff changeset
493 if (size/Py_SIZE(a) != n)
11127
9a20038f0b99 Appropriate overflow checks so that things like sys.maxint*(1,) can't
Guido van Rossum <guido@python.org>
parents: 9632
diff changeset
494 return PyErr_NoMemory();
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
495 np = (PyTupleObject *) PyTuple_New(size);
258
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
496 if (np == NULL)
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
497 return NULL;
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
498 p = np->ob_item;
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31623
diff changeset
499 items = a->ob_item;
258
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
500 for (i = 0; i < n; i++) {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43452
diff changeset
501 for (j = 0; j < Py_SIZE(a); j++) {
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31623
diff changeset
502 *p = items[j];
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
503 Py_INCREF(*p);
258
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
504 p++;
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
505 }
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
506 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
507 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
508 }
3b93bcea10f6 Finally implement tuple*number. From now on all sequence types
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
509
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
510 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
511 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
512 {
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
513 Py_ssize_t i, start=0, stop=Py_SIZE(self);
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 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
515
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
516 if (!PyArg_ParseTuple(args, "O|O&O&:index", &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
517 _PyEval_SliceIndex, &start,
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
518 _PyEval_SliceIndex, &stop))
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
519 return NULL;
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
520 if (start < 0) {
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
521 start += Py_SIZE(self);
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
522 if (start < 0)
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
523 start = 0;
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
524 }
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
525 if (stop < 0) {
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
526 stop += Py_SIZE(self);
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
527 if (stop < 0)
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
528 stop = 0;
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
529 }
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
530 for (i = start; i < stop && i < Py_SIZE(self); i++) {
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
531 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
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
532 if (cmp > 0)
45210
0958cc46b33f Return ints instead of longs for tuple.count() and tuple.index().
Raymond Hettinger <python@rcn.com>
parents: 45207
diff changeset
533 return PyInt_FromSsize_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
534 else if (cmp < 0)
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
535 return NULL;
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
536 }
52173
442bdfe93b29 fix tuple.index() error message #5495
Benjamin Peterson <benjamin@python.org>
parents: 50627
diff changeset
537 PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple");
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 return NULL;
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
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 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
542 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
543 {
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
544 Py_ssize_t count = 0;
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 Py_ssize_t i;
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
546
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
547 for (i = 0; i < Py_SIZE(self); i++) {
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
548 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
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
549 if (cmp > 0)
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
550 count++;
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
551 else if (cmp < 0)
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
552 return NULL;
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
553 }
45210
0958cc46b33f Return ints instead of longs for tuple.count() and tuple.index().
Raymond Hettinger <python@rcn.com>
parents: 45207
diff changeset
554 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
555 }
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
556
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12929
diff changeset
557 static int
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12929
diff changeset
558 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
559 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
560 Py_ssize_t i;
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12929
diff changeset
561
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43452
diff changeset
562 for (i = Py_SIZE(o); --i >= 0; )
37756
7b2e436a4a21 Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents: 37602
diff changeset
563 Py_VISIT(o->ob_item[i]);
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12929
diff changeset
564 return 0;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12929
diff changeset
565 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12929
diff changeset
566
15973
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
567 static PyObject *
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
568 tuplerichcompare(PyObject *v, PyObject *w, int op)
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
569 {
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
570 PyTupleObject *vt, *wt;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
571 Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
572 Py_ssize_t vlen, wlen;
15973
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
573
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
574 if (!PyTuple_Check(v) || !PyTuple_Check(w)) {
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
575 Py_INCREF(Py_NotImplemented);
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
576 return Py_NotImplemented;
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
577 }
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
578
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
579 vt = (PyTupleObject *)v;
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
580 wt = (PyTupleObject *)w;
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
581
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43452
diff changeset
582 vlen = Py_SIZE(vt);
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43452
diff changeset
583 wlen = Py_SIZE(wt);
15973
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
584
17841
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
585 /* Note: the corresponding code for lists has an "early out" test
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
586 * here when op is EQ or NE and the lengths differ. That pays there,
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
587 * but Tim was unable to find any real code where EQ/NE tuple
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
588 * compares don't have the same length, so testing for it here would
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
589 * have cost without benefit.
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
590 */
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
591
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
592 /* Search for the first index where items are different.
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
593 * Note that because tuples are immutable, it's safe to reuse
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
594 * vlen and wlen across the comparison calls.
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
595 */
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
596 for (i = 0; i < vlen && i < wlen; i++) {
15973
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
597 int k = PyObject_RichCompareBool(vt->ob_item[i],
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
598 wt->ob_item[i], Py_EQ);
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
599 if (k < 0)
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
600 return NULL;
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
601 if (!k)
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
602 break;
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
603 }
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
604
17841
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
605 if (i >= vlen || i >= wlen) {
15973
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
606 /* No more items to compare -- compare sizes */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
607 int cmp;
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
608 PyObject *res;
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
609 switch (op) {
17841
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
610 case Py_LT: cmp = vlen < wlen; break;
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
611 case Py_LE: cmp = vlen <= wlen; break;
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
612 case Py_EQ: cmp = vlen == wlen; break;
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
613 case Py_NE: cmp = vlen != wlen; break;
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
614 case Py_GT: cmp = vlen > wlen; break;
2b5a9bff1b11 Speed tuple comparisons in two ways:
Tim Peters <tim.peters@gmail.com>
parents: 15973
diff changeset
615 case Py_GE: cmp = vlen >= wlen; break;
15973
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
616 default: return NULL; /* cannot happen */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
617 }
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
618 if (cmp)
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
619 res = Py_True;
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
620 else
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
621 res = Py_False;
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
622 Py_INCREF(res);
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
623 return res;
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
624 }
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
625
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
626 /* We have an item that differs -- shortcuts for EQ/NE */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
627 if (op == Py_EQ) {
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
628 Py_INCREF(Py_False);
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
629 return Py_False;
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
630 }
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
631 if (op == Py_NE) {
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
632 Py_INCREF(Py_True);
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
633 return Py_True;
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
634 }
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
635
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
636 /* Compare the final item again using the proper operator */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
637 return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
638 }
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
639
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24184
diff changeset
640 static PyObject *
19592
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
641 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
642
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
643 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
644 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
645 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
646 PyObject *arg = NULL;
36675
dba6494735d0 Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36519
diff changeset
647 static char *kwlist[] = {"sequence", 0};
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
648
19592
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
649 if (type != &PyTuple_Type)
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
650 return tuple_subtype_new(type, args, kwds);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
651 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:tuple", kwlist, &arg))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
652 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
653
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
654 if (arg == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
655 return PyTuple_New(0);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
656 else
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
657 return PySequence_Tuple(arg);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
658 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
659
19592
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
660 static PyObject *
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
661 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
662 {
37596
1b078493d538 More low-hanging fruit. Still need to re-arrange some code (or find a better
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37067
diff changeset
663 PyObject *tmp, *newobj, *item;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
664 Py_ssize_t i, n;
19592
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
665
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
666 assert(PyType_IsSubtype(type, &PyTuple_Type));
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
667 tmp = tuple_new(&PyTuple_Type, args, kwds);
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
668 if (tmp == NULL)
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
669 return NULL;
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
670 assert(PyTuple_Check(tmp));
37596
1b078493d538 More low-hanging fruit. Still need to re-arrange some code (or find a better
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37067
diff changeset
671 newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
1b078493d538 More low-hanging fruit. Still need to re-arrange some code (or find a better
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37067
diff changeset
672 if (newobj == NULL)
19592
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
673 return NULL;
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
674 for (i = 0; i < n; i++) {
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
675 item = PyTuple_GET_ITEM(tmp, i);
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
676 Py_INCREF(item);
37596
1b078493d538 More low-hanging fruit. Still need to re-arrange some code (or find a better
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37067
diff changeset
677 PyTuple_SET_ITEM(newobj, i, item);
19592
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
678 }
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
679 Py_DECREF(tmp);
37596
1b078493d538 More low-hanging fruit. Still need to re-arrange some code (or find a better
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37067
diff changeset
680 return newobj;
19592
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
681 }
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
682
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
683 PyDoc_STRVAR(tuple_doc,
19657
937965516b54 Rewrite the tuple() docstring to parallel the list() docstring.
Tim Peters <tim.peters@gmail.com>
parents: 19656
diff changeset
684 "tuple() -> an empty tuple\n"
937965516b54 Rewrite the tuple() docstring to parallel the list() docstring.
Tim Peters <tim.peters@gmail.com>
parents: 19656
diff changeset
685 "tuple(sequence) -> tuple initialized from sequence's items\n"
937965516b54 Rewrite the tuple() docstring to parallel the list() docstring.
Tim Peters <tim.peters@gmail.com>
parents: 19656
diff changeset
686 "\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
687 "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
688
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
689 static PySequenceMethods tuple_as_sequence = {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
690 (lenfunc)tuplelength, /* sq_length */
15973
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
691 (binaryfunc)tupleconcat, /* sq_concat */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
692 (ssizeargfunc)tuplerepeat, /* sq_repeat */
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
693 (ssizeargfunc)tupleitem, /* sq_item */
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
694 (ssizessizeargfunc)tupleslice, /* sq_slice */
15973
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
695 0, /* sq_ass_item */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
696 0, /* sq_ass_slice */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
697 (objobjproc)tuplecontains, /* sq_contains */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
698 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
699
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
700 static PyObject*
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
701 tuplesubscript(PyTupleObject* self, PyObject* item)
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
702 {
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37756
diff changeset
703 if (PyIndex_Check(item)) {
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37756
diff changeset
704 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
705 if (i == -1 && PyErr_Occurred())
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
706 return NULL;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
707 if (i < 0)
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
708 i += PyTuple_GET_SIZE(self);
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
709 return tupleitem(self, i);
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
710 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
711 else if (PySlice_Check(item)) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
712 Py_ssize_t start, stop, step, slicelength, cur, i;
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
713 PyObject* result;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
714 PyObject* it;
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31623
diff changeset
715 PyObject **src, **dest;
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
716
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
717 if (PySlice_GetIndicesEx((PySliceObject*)item,
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
718 PyTuple_GET_SIZE(self),
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
719 &start, &stop, &step, &slicelength) < 0) {
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
720 return NULL;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
721 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
722
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
723 if (slicelength <= 0) {
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
724 return PyTuple_New(0);
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
725 }
42951
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
726 else if (start == 0 && step == 1 &&
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
727 slicelength == PyTuple_GET_SIZE(self) &&
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
728 PyTuple_CheckExact(self)) {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
729 Py_INCREF(self);
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
730 return (PyObject *)self;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
731 }
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
732 else {
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
733 result = PyTuple_New(slicelength);
37067
1f4549865aad Fix some missing checks after PyTuple_New, PyList_New, PyDict_New
Georg Brandl <georg@python.org>
parents: 36882
diff changeset
734 if (!result) return NULL;
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
735
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31623
diff changeset
736 src = self->ob_item;
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31623
diff changeset
737 dest = ((PyTupleObject *)result)->ob_item;
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
738 for (cur = start, i = 0; i < slicelength;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
739 cur += step, i++) {
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31623
diff changeset
740 it = src[cur];
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
741 Py_INCREF(it);
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31623
diff changeset
742 dest[i] = it;
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
743 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
744
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
745 return result;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
746 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
747 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
748 else {
40357
b871e7bd8355 Patch [ 1586791 ] better error msgs for some TypeErrors
Georg Brandl <georg@python.org>
parents: 39463
diff changeset
749 PyErr_Format(PyExc_TypeError,
b871e7bd8355 Patch [ 1586791 ] better error msgs for some TypeErrors
Georg Brandl <georg@python.org>
parents: 39463
diff changeset
750 "tuple indices must be integers, not %.200s",
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43452
diff changeset
751 Py_TYPE(item)->tp_name);
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
752 return NULL;
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 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
755
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 25740
diff changeset
756 static PyObject *
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 25740
diff changeset
757 tuple_getnewargs(PyTupleObject *v)
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 25740
diff changeset
758 {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43452
diff changeset
759 return Py_BuildValue("(N)", tupleslice(v, 0, Py_SIZE(v)));
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 }
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 25740
diff changeset
762
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
763 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
764 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
765 {
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 Py_ssize_t res;
2dfaa0683be8 Fixed: sys.getsizeof does not take the actual length of the tuples into account.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47527
diff changeset
767
2dfaa0683be8 Fixed: sys.getsizeof does not take the actual length of the tuples into account.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47527
diff changeset
768 res = PyTuple_Type.tp_basicsize + Py_SIZE(self) * sizeof(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
769 return PyInt_FromSsize_t(res);
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 }
2dfaa0683be8 Fixed: sys.getsizeof does not take the actual length of the tuples into account.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47527
diff changeset
771
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
772 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
773 "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
774 "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
775 );
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
776 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
777 "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
778 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
779 "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
780
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 25740
diff changeset
781 static PyMethodDef tuple_methods[] = {
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 25740
diff changeset
782 {"__getnewargs__", (PyCFunction)tuple_getnewargs, METH_NOARGS},
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
783 {"__sizeof__", (PyCFunction)tuple_sizeof, METH_NOARGS, sizeof_doc},
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
784 {"index", (PyCFunction)tupleindex, METH_VARARGS, index_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
785 {"count", (PyCFunction)tuplecount, METH_O, count_doc},
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 25740
diff changeset
786 {NULL, NULL} /* sentinel */
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 25740
diff changeset
787 };
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 25740
diff changeset
788
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
789 static PyMappingMethods tuple_as_mapping = {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
790 (lenfunc)tuplelength,
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
791 (binaryfunc)tuplesubscript,
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
792 0
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
793 };
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
794
24683
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
795 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
796
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
797 PyTypeObject PyTuple_Type = {
42333
6620a510e9a9 PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents: 40908
diff changeset
798 PyVarObject_HEAD_INIT(&PyType_Type, 0)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
799 "tuple",
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18933
diff changeset
800 sizeof(PyTupleObject) - sizeof(PyObject *),
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
801 sizeof(PyObject *),
15973
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
802 (destructor)tupledealloc, /* tp_dealloc */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
803 (printfunc)tupleprint, /* tp_print */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
804 0, /* tp_getattr */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
805 0, /* tp_setattr */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
806 0, /* tp_compare */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
807 (reprfunc)tuplerepr, /* tp_repr */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
808 0, /* tp_as_number */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
809 &tuple_as_sequence, /* tp_as_sequence */
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 22979
diff changeset
810 &tuple_as_mapping, /* tp_as_mapping */
15973
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
811 (hashfunc)tuplehash, /* tp_hash */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
812 0, /* tp_call */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
813 0, /* tp_str */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
814 PyObject_GenericGetAttr, /* tp_getattro */
15973
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
815 0, /* tp_setattro */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
816 0, /* tp_as_buffer */
19592
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19581
diff changeset
817 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
40908
d01bd37bf5ce Variation of patch # 1624059 to speed up checking if an object is a subclass
Neal Norwitz <nnorwitz@gmail.com>
parents: 40357
diff changeset
818 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TUPLE_SUBCLASS, /* tp_flags */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
819 tuple_doc, /* tp_doc */
15973
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
820 (traverseproc)tupletraverse, /* tp_traverse */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
821 0, /* tp_clear */
72139263f8a7 Same treatment as listobject.c:
Guido van Rossum <guido@python.org>
parents: 15037
diff changeset
822 tuplerichcompare, /* tp_richcompare */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
823 0, /* tp_weaklistoffset */
24683
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
824 tuple_iter, /* tp_iter */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
825 0, /* tp_iternext */
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 25740
diff changeset
826 tuple_methods, /* tp_methods */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
827 0, /* tp_members */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
828 0, /* tp_getset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
829 0, /* tp_base */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
830 0, /* tp_dict */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
831 0, /* tp_descr_get */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
832 0, /* tp_descr_set */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
833 0, /* tp_dictoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
834 0, /* tp_init */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
835 0, /* tp_alloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
836 tuple_new, /* tp_new */
22979
9fe94c01eaf1 PyObject_GC_Del can now be used as a function designator.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22626
diff changeset
837 PyObject_GC_Del, /* tp_free */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
838 };
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
839
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
840 /* 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
841 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
842 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
843 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
844 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
845 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
846
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
847 int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
848 _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
849 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
850 register PyTupleObject *v;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
851 register PyTupleObject *sv;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
852 Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
853 Py_ssize_t oldsize;
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
854
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
855 v = (PyTupleObject *) *pv;
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43452
diff changeset
856 if (v == NULL || Py_TYPE(v) != &PyTuple_Type ||
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43452
diff changeset
857 (Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
858 *pv = 0;
15037
c7d46eae95d8 Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14584
diff changeset
859 Py_XDECREF(v);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
860 PyErr_BadInternalCall();
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
861 return -1;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
862 }
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43452
diff changeset
863 oldsize = Py_SIZE(v);
21293
b810c30b4aa1 _PyTuple_Resize(): this dumped core on tuple(globals()) for me. Turns
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
864 if (oldsize == newsize)
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
865 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
866
21293
b810c30b4aa1 _PyTuple_Resize(): this dumped core on tuple(globals()) for me. Turns
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
867 if (oldsize == 0) {
17975
527d0ffdf099 _PyTuple_Resize: take into account the empty tuple. There can be only one.
Thomas Wouters <thomas@python.org>
parents: 17841
diff changeset
868 /* Empty tuples are often shared, so we should never
527d0ffdf099 _PyTuple_Resize: take into account the empty tuple. There can be only one.
Thomas Wouters <thomas@python.org>
parents: 17841
diff changeset
869 resize them in-place even if we do own the only
527d0ffdf099 _PyTuple_Resize: take into account the empty tuple. There can be only one.
Thomas Wouters <thomas@python.org>
parents: 17841
diff changeset
870 (current) reference */
527d0ffdf099 _PyTuple_Resize: take into account the empty tuple. There can be only one.
Thomas Wouters <thomas@python.org>
parents: 17841
diff changeset
871 Py_DECREF(v);
527d0ffdf099 _PyTuple_Resize: take into account the empty tuple. There can be only one.
Thomas Wouters <thomas@python.org>
parents: 17841
diff changeset
872 *pv = PyTuple_New(newsize);
17979
192d36869445 _PyTuple_Resize: guard against PyTuple_New() returning NULL, using Tim's
Thomas Wouters <thomas@python.org>
parents: 17976
diff changeset
873 return *pv == NULL ? -1 : 0;
17975
527d0ffdf099 _PyTuple_Resize: take into account the empty tuple. There can be only one.
Thomas Wouters <thomas@python.org>
parents: 17841
diff changeset
874 }
527d0ffdf099 _PyTuple_Resize: take into account the empty tuple. There can be only one.
Thomas Wouters <thomas@python.org>
parents: 17841
diff changeset
875
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
876 /* XXX UNREF/NEWREF interface should be more symmetrical */
24184
c4dcb80f48ea object.h special-build macro minefield: renamed all the new lexical
Tim Peters <tim.peters@gmail.com>
parents: 23952
diff changeset
877 _Py_DEC_REFTOTAL;
57665
2ffd19a224d4 Issue #7466: segmentation fault when the garbage collector is called
Antoine Pitrou <solipsis@pitrou.net>
parents: 52292
diff changeset
878 if (_PyObject_GC_IS_TRACKED(v))
2ffd19a224d4 Issue #7466: segmentation fault when the garbage collector is called
Antoine Pitrou <solipsis@pitrou.net>
parents: 52292
diff changeset
879 _PyObject_GC_UNTRACK(v);
15037
c7d46eae95d8 Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14584
diff changeset
880 _Py_ForgetReference((PyObject *) v);
21293
b810c30b4aa1 _PyTuple_Resize(): this dumped core on tuple(globals()) for me. Turns
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
881 /* DECREF items deleted by shrinkage */
b810c30b4aa1 _PyTuple_Resize(): this dumped core on tuple(globals()) for me. Turns
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
882 for (i = newsize; i < oldsize; i++) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4733
diff changeset
883 Py_XDECREF(v->ob_item[i]);
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
884 v->ob_item[i] = NULL;
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1369
diff changeset
885 }
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18933
diff changeset
886 sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
15037
c7d46eae95d8 Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14584
diff changeset
887 if (sv == NULL) {
c7d46eae95d8 Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14584
diff changeset
888 *pv = NULL;
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18933
diff changeset
889 PyObject_GC_Del(v);
15037
c7d46eae95d8 Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14584
diff changeset
890 return -1;
12439
456869eaa7d5 Patch by Charles G Waldman to avoid a sneaky memory leak in
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
891 }
15037
c7d46eae95d8 Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14584
diff changeset
892 _Py_NewReference((PyObject *) sv);
21293
b810c30b4aa1 _PyTuple_Resize(): this dumped core on tuple(globals()) for me. Turns
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
893 /* Zero out items added by growing */
23834
8ae26d9be1b4 SF #561244 Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
894 if (newsize > oldsize)
23952
3834cb0e7be6 Fix for SF bug 571885
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23834
diff changeset
895 memset(&sv->ob_item[oldsize], 0,
3834cb0e7be6 Fix for SF bug 571885
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23834
diff changeset
896 sizeof(*sv->ob_item) * (newsize - oldsize));
15037
c7d46eae95d8 Simplify _PyTuple_Resize by not using the tuple free list and dropping
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14584
diff changeset
897 *pv = (PyObject *) sv;
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18933
diff changeset
898 _PyObject_GC_TRACK(sv);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
899 return 0;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1352
diff changeset
900 }
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
901
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
902 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
903 PyTuple_ClearFreeList(void)
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
904 {
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
905 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
906 #if PyTuple_MAXSAVESIZE > 0
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
907 int i;
45191
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
908 for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
909 PyTupleObject *p, *q;
45191
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
910 p = free_list[i];
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
911 freelist_size += numfree[i];
45191
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
912 free_list[i] = 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
913 numfree[i] = 0;
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
914 while (p) {
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
915 q = p;
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
916 p = (PyTupleObject *)(p->ob_item[0]);
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18933
diff changeset
917 PyObject_GC_Del(q);
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
918 }
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
919 }
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
920 #endif
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 return freelist_size;
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
922 }
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
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 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 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
926 {
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
927 #if PyTuple_MAXSAVESIZE > 0
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
928 /* empty tuples are used all over the place and applications may
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
929 * rely on the fact that an empty tuple is a singleton. */
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
930 Py_XDECREF(free_list[0]);
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 free_list[0] = NULL;
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
932
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 (void)PyTuple_ClearFreeList();
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
934 #endif
52288
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
935 #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
936 show_track();
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 52173
diff changeset
937 #endif
5936
80d24364b932 Added _Fini() routines to free up some memory
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
938 }
24683
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
939
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
940 /*********************** Tuple Iterator **************************/
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
941
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
942 typedef struct {
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
943 PyObject_HEAD
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
944 long it_index;
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
945 PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
946 } tupleiterobject;
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
947
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
948 static void
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
949 tupleiter_dealloc(tupleiterobject *it)
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
950 {
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
951 _PyObject_GC_UNTRACK(it);
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
952 Py_XDECREF(it->it_seq);
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
953 PyObject_GC_Del(it);
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
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
956 static int
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
957 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
958 {
37756
7b2e436a4a21 Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents: 37602
diff changeset
959 Py_VISIT(it->it_seq);
7b2e436a4a21 Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents: 37602
diff changeset
960 return 0;
24683
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
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
963 static PyObject *
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
964 tupleiter_next(tupleiterobject *it)
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
965 {
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
966 PyTupleObject *seq;
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
967 PyObject *item;
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
968
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
969 assert(it != NULL);
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
970 seq = it->it_seq;
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
971 if (seq == NULL)
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
972 return NULL;
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
973 assert(PyTuple_Check(seq));
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
974
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
975 if (it->it_index < PyTuple_GET_SIZE(seq)) {
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
976 item = PyTuple_GET_ITEM(seq, it->it_index);
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
977 ++it->it_index;
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
978 Py_INCREF(item);
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
979 return item;
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
980 }
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
981
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
982 Py_DECREF(seq);
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
983 it->it_seq = NULL;
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
984 return NULL;
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
985 }
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
986
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 32318
diff changeset
987 static PyObject *
31686
823279ae43eb Make iterators length transparent where possible.
Raymond Hettinger <python@rcn.com>
parents: 31663
diff changeset
988 tupleiter_len(tupleiterobject *it)
823279ae43eb Make iterators length transparent where possible.
Raymond Hettinger <python@rcn.com>
parents: 31663
diff changeset
989 {
36519
d6d415df0df0 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
990 Py_ssize_t len = 0;
31686
823279ae43eb Make iterators length transparent where possible.
Raymond Hettinger <python@rcn.com>
parents: 31663
diff changeset
991 if (it->it_seq)
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 32318
diff changeset
992 len = PyTuple_GET_SIZE(it->it_seq) - it->it_index;
36519
d6d415df0df0 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
993 return PyInt_FromSsize_t(len);
31686
823279ae43eb Make iterators length transparent where possible.
Raymond Hettinger <python@rcn.com>
parents: 31663
diff changeset
994 }
823279ae43eb Make iterators length transparent where possible.
Raymond Hettinger <python@rcn.com>
parents: 31663
diff changeset
995
36484
20f75d5e79e9 Renamed _length_cue() to __length_hint__(). See:
Armin Rigo <arigo@tunes.org>
parents: 35999
diff changeset
996 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
997
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 32318
diff changeset
998 static PyMethodDef tupleiter_methods[] = {
36484
20f75d5e79e9 Renamed _length_cue() to __length_hint__(). See:
Armin Rigo <arigo@tunes.org>
parents: 35999
diff changeset
999 {"__length_hint__", (PyCFunction)tupleiter_len, METH_NOARGS, length_hint_doc},
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 32318
diff changeset
1000 {NULL, NULL} /* sentinel */
31686
823279ae43eb Make iterators length transparent where possible.
Raymond Hettinger <python@rcn.com>
parents: 31663
diff changeset
1001 };
823279ae43eb Make iterators length transparent where possible.
Raymond Hettinger <python@rcn.com>
parents: 31663
diff changeset
1002
24683
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
1003 PyTypeObject PyTupleIter_Type = {
42333
6620a510e9a9 PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents: 40908
diff changeset
1004 PyVarObject_HEAD_INIT(&PyType_Type, 0)
24683
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
1005 "tupleiterator", /* tp_name */
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
1006 sizeof(tupleiterobject), /* tp_basicsize */
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
1007 0, /* tp_itemsize */
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
1008 /* methods */
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
1009 (destructor)tupleiter_dealloc, /* tp_dealloc */
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
1010 0, /* tp_print */
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
1011 0, /* tp_getattr */
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
1012 0, /* tp_setattr */
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
1013 0, /* tp_compare */
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
1014 0, /* tp_repr */
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
1015 0, /* tp_as_number */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 32318
diff changeset
1016 0, /* tp_as_sequence */
24683
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
1017 0, /* tp_as_mapping */
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
1018 0, /* tp_hash */
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
1019 0, /* tp_call */
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
1020 0, /* tp_str */
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
1021 PyObject_GenericGetAttr, /* tp_getattro */
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
1022 0, /* tp_setattro */
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
1023 0, /* tp_as_buffer */
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
1024 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
1025 0, /* tp_doc */
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
1026 (traverseproc)tupleiter_traverse, /* tp_traverse */
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
1027 0, /* tp_clear */
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
1028 0, /* tp_richcompare */
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
1029 0, /* tp_weaklistoffset */
28320
fd5ef7003469 Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents: 28308
diff changeset
1030 PyObject_SelfIter, /* tp_iter */
24683
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
1031 (iternextfunc)tupleiter_next, /* tp_iternext */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 32318
diff changeset
1032 tupleiter_methods, /* tp_methods */
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 32318
diff changeset
1033 0,
24683
567704bfa595 Moved special case for tuples from iterobject.c to
Raymond Hettinger <python@rcn.com>
parents: 24280
diff changeset
1034 };
37602
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37596
diff changeset
1035
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37596
diff changeset
1036 static PyObject *
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37596
diff changeset
1037 tuple_iter(PyObject *seq)
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37596
diff changeset
1038 {
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37596
diff changeset
1039 tupleiterobject *it;
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37596
diff changeset
1040
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37596
diff changeset
1041 if (!PyTuple_Check(seq)) {
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37596
diff changeset
1042 PyErr_BadInternalCall();
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37596
diff changeset
1043 return NULL;
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37596
diff changeset
1044 }
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37596
diff changeset
1045 it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37596
diff changeset
1046 if (it == NULL)
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37596
diff changeset
1047 return NULL;
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37596
diff changeset
1048 it->it_index = 0;
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37596
diff changeset
1049 Py_INCREF(seq);
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37596
diff changeset
1050 it->it_seq = (PyTupleObject *)seq;
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37596
diff changeset
1051 _PyObject_GC_TRACK(it);
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37596
diff changeset
1052 return (PyObject *)it;
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37596
diff changeset
1053 }