annotate Modules/collectionsmodule.c @ 39042:c02fa187e0f4 legacy-trunk

Part of SF patch #1484695. This removes dead code. The chksum was already verified in .frombuf() on the lines above. If there was a problem an exception is raised, so there was no way this condition could have been true.
author Neal Norwitz <nnorwitz@gmail.com>
date Mon, 10 Jul 2006 00:23:17 +0000
parents d1547426ab61
children cd8f20d400c1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1 #include "Python.h"
32158
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 32119
diff changeset
2 #include "structmember.h"
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
3
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
4 /* collections module implementation of a deque() datatype
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
5 Written and maintained by Raymond D. Hettinger <python@rcn.com>
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
6 Copyright (c) 2004 Python Software Foundation.
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
7 All rights reserved.
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
8 */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
9
33643
91634f3ba213 Restore the block length and add a comment.
Raymond Hettinger <python@rcn.com>
parents: 33642
diff changeset
10 /* The block length may be set to any number over 1. Larger numbers
91634f3ba213 Restore the block length and add a comment.
Raymond Hettinger <python@rcn.com>
parents: 33642
diff changeset
11 * reduce the number of calls to the memory allocator but take more
91634f3ba213 Restore the block length and add a comment.
Raymond Hettinger <python@rcn.com>
parents: 33642
diff changeset
12 * memory. Ideally, BLOCKLEN should be set with an eye to the
91634f3ba213 Restore the block length and add a comment.
Raymond Hettinger <python@rcn.com>
parents: 33642
diff changeset
13 * length of a cache line.
91634f3ba213 Restore the block length and add a comment.
Raymond Hettinger <python@rcn.com>
parents: 33642
diff changeset
14 */
91634f3ba213 Restore the block length and add a comment.
Raymond Hettinger <python@rcn.com>
parents: 33642
diff changeset
15
33879
687362fa471c Bump-up block size.
Raymond Hettinger <python@rcn.com>
parents: 33697
diff changeset
16 #define BLOCKLEN 62
33641
cd70f1ac3c25 * Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents: 33639
diff changeset
17 #define CENTER ((BLOCKLEN - 1) / 2)
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
18
33637
6f4bcb3bd202 Document some reverse-engineered invariants and pragmatic hints.
Tim Peters <tim.peters@gmail.com>
parents: 33635
diff changeset
19 /* A `dequeobject` is composed of a doubly-linked list of `block` nodes.
6f4bcb3bd202 Document some reverse-engineered invariants and pragmatic hints.
Tim Peters <tim.peters@gmail.com>
parents: 33635
diff changeset
20 * This list is not circular (the leftmost block has leftlink==NULL,
6f4bcb3bd202 Document some reverse-engineered invariants and pragmatic hints.
Tim Peters <tim.peters@gmail.com>
parents: 33635
diff changeset
21 * and the rightmost block has rightlink==NULL). A deque d's first
6f4bcb3bd202 Document some reverse-engineered invariants and pragmatic hints.
Tim Peters <tim.peters@gmail.com>
parents: 33635
diff changeset
22 * element is at d.leftblock[leftindex] and its last element is at
6f4bcb3bd202 Document some reverse-engineered invariants and pragmatic hints.
Tim Peters <tim.peters@gmail.com>
parents: 33635
diff changeset
23 * d.rightblock[rightindex]; note that, unlike as for Python slice
33641
cd70f1ac3c25 * Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents: 33639
diff changeset
24 * indices, these indices are inclusive on both ends. By being inclusive
cd70f1ac3c25 * Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents: 33639
diff changeset
25 * on both ends, algorithms for left and right operations become
cd70f1ac3c25 * Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents: 33639
diff changeset
26 * symmetrical which simplifies the design.
cd70f1ac3c25 * Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents: 33639
diff changeset
27 *
cd70f1ac3c25 * Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents: 33639
diff changeset
28 * The list of blocks is never empty, so d.leftblock and d.rightblock
cd70f1ac3c25 * Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents: 33639
diff changeset
29 * are never equal to NULL.
cd70f1ac3c25 * Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents: 33639
diff changeset
30 *
cd70f1ac3c25 * Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents: 33639
diff changeset
31 * The indices, d.leftindex and d.rightindex are always in the range
cd70f1ac3c25 * Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents: 33639
diff changeset
32 * 0 <= index < BLOCKLEN.
33642
4fb8c89f66a0 Clarify the relationship between indices.
Raymond Hettinger <python@rcn.com>
parents: 33641
diff changeset
33 * Their exact relationship is:
4fb8c89f66a0 Clarify the relationship between indices.
Raymond Hettinger <python@rcn.com>
parents: 33641
diff changeset
34 * (d.leftindex + d.len - 1) % BLOCKLEN == d.rightindex.
33641
cd70f1ac3c25 * Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents: 33639
diff changeset
35 *
cd70f1ac3c25 * Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents: 33639
diff changeset
36 * Empty deques have d.len == 0; d.leftblock==d.rightblock;
cd70f1ac3c25 * Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents: 33639
diff changeset
37 * d.leftindex == CENTER+1; and d.rightindex == CENTER.
cd70f1ac3c25 * Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents: 33639
diff changeset
38 * Checking for d.len == 0 is the intended way to see whether d is empty.
cd70f1ac3c25 * Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents: 33639
diff changeset
39 *
cd70f1ac3c25 * Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents: 33639
diff changeset
40 * Whenever d.leftblock == d.rightblock,
33642
4fb8c89f66a0 Clarify the relationship between indices.
Raymond Hettinger <python@rcn.com>
parents: 33641
diff changeset
41 * d.leftindex + d.len - 1 == d.rightindex.
33641
cd70f1ac3c25 * Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents: 33639
diff changeset
42 *
33642
4fb8c89f66a0 Clarify the relationship between indices.
Raymond Hettinger <python@rcn.com>
parents: 33641
diff changeset
43 * However, when d.leftblock != d.rightblock, d.leftindex and d.rightindex
4fb8c89f66a0 Clarify the relationship between indices.
Raymond Hettinger <python@rcn.com>
parents: 33641
diff changeset
44 * become indices into distinct blocks and either may be larger than the
4fb8c89f66a0 Clarify the relationship between indices.
Raymond Hettinger <python@rcn.com>
parents: 33641
diff changeset
45 * other.
33637
6f4bcb3bd202 Document some reverse-engineered invariants and pragmatic hints.
Tim Peters <tim.peters@gmail.com>
parents: 33635
diff changeset
46 */
6f4bcb3bd202 Document some reverse-engineered invariants and pragmatic hints.
Tim Peters <tim.peters@gmail.com>
parents: 33635
diff changeset
47
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
48 typedef struct BLOCK {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
49 struct BLOCK *leftlink;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
50 struct BLOCK *rightlink;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
51 PyObject *data[BLOCKLEN];
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
52 } block;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
53
33635
836e92cc5c4f Definition consistency.
Tim Peters <tim.peters@gmail.com>
parents: 33634
diff changeset
54 static block *
33677
262e1cf41d34 Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
55 newblock(block *leftlink, block *rightlink, int len) {
262e1cf41d34 Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
56 block *b;
262e1cf41d34 Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
57 /* To prevent len from overflowing INT_MAX on 64-bit machines, we
262e1cf41d34 Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
58 * refuse to allocate new blocks if the current len is dangerously
262e1cf41d34 Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
59 * close. There is some extra margin to prevent spurious arithmetic
262e1cf41d34 Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
60 * overflows at various places. The following check ensures that
262e1cf41d34 Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
61 * the blocks allocated to the deque, in the worst case, can only
262e1cf41d34 Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
62 * have INT_MAX-2 entries in total.
262e1cf41d34 Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
63 */
262e1cf41d34 Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
64 if (len >= INT_MAX - 2*BLOCKLEN) {
262e1cf41d34 Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
65 PyErr_SetString(PyExc_OverflowError,
262e1cf41d34 Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
66 "cannot add more blocks to the deque");
262e1cf41d34 Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
67 return NULL;
262e1cf41d34 Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
68 }
262e1cf41d34 Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
69 b = PyMem_Malloc(sizeof(block));
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
70 if (b == NULL) {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
71 PyErr_NoMemory();
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
72 return NULL;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
73 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
74 b->leftlink = leftlink;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
75 b->rightlink = rightlink;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
76 return b;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
77 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
78
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
79 typedef struct {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
80 PyObject_HEAD
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
81 block *leftblock;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
82 block *rightblock;
33637
6f4bcb3bd202 Document some reverse-engineered invariants and pragmatic hints.
Tim Peters <tim.peters@gmail.com>
parents: 33635
diff changeset
83 int leftindex; /* in range(BLOCKLEN) */
6f4bcb3bd202 Document some reverse-engineered invariants and pragmatic hints.
Tim Peters <tim.peters@gmail.com>
parents: 33635
diff changeset
84 int rightindex; /* in range(BLOCKLEN) */
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
85 int len;
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
86 long state; /* incremented whenever the indices move */
32158
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 32119
diff changeset
87 PyObject *weakreflist; /* List of weak references */
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
88 } dequeobject;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
89
31605
2fed331de014 Make deque_type static so namespace is not polluted.
Neal Norwitz <nnorwitz@gmail.com>
parents: 31603
diff changeset
90 static PyTypeObject deque_type;
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
91
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
92 static PyObject *
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
93 deque_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
94 {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
95 dequeobject *deque;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
96 block *b;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
97
35449
1fdfd0130e28 Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents: 34772
diff changeset
98 if (!_PyArg_NoKeywords("deque()", kwds))
1fdfd0130e28 Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents: 34772
diff changeset
99 return NULL;
1fdfd0130e28 Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents: 34772
diff changeset
100
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
101 /* create dequeobject structure */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
102 deque = (dequeobject *)type->tp_alloc(type, 0);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
103 if (deque == NULL)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
104 return NULL;
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
105
33677
262e1cf41d34 Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
106 b = newblock(NULL, NULL, 0);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
107 if (b == NULL) {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
108 Py_DECREF(deque);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
109 return NULL;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
110 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
111
33641
cd70f1ac3c25 * Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents: 33639
diff changeset
112 assert(BLOCKLEN >= 2);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
113 deque->leftblock = b;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
114 deque->rightblock = b;
33641
cd70f1ac3c25 * Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents: 33639
diff changeset
115 deque->leftindex = CENTER + 1;
cd70f1ac3c25 * Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents: 33639
diff changeset
116 deque->rightindex = CENTER;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
117 deque->len = 0;
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
118 deque->state = 0;
32158
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 32119
diff changeset
119 deque->weakreflist = NULL;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
120
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
121 return (PyObject *)deque;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
122 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
123
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
124 static PyObject *
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
125 deque_append(dequeobject *deque, PyObject *item)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
126 {
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
127 deque->state++;
33648
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33644
diff changeset
128 if (deque->rightindex == BLOCKLEN-1) {
33677
262e1cf41d34 Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
129 block *b = newblock(deque->rightblock, NULL, deque->len);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
130 if (b == NULL)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
131 return NULL;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
132 assert(deque->rightblock->rightlink == NULL);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
133 deque->rightblock->rightlink = b;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
134 deque->rightblock = b;
33648
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33644
diff changeset
135 deque->rightindex = -1;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
136 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
137 Py_INCREF(item);
33648
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33644
diff changeset
138 deque->len++;
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33644
diff changeset
139 deque->rightindex++;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
140 deque->rightblock->data[deque->rightindex] = item;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
141 Py_RETURN_NONE;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
142 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
143
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
144 PyDoc_STRVAR(append_doc, "Add an element to the right side of the deque.");
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
145
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
146 static PyObject *
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
147 deque_appendleft(dequeobject *deque, PyObject *item)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
148 {
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
149 deque->state++;
33648
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33644
diff changeset
150 if (deque->leftindex == 0) {
33677
262e1cf41d34 Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
151 block *b = newblock(NULL, deque->leftblock, deque->len);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
152 if (b == NULL)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
153 return NULL;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
154 assert(deque->leftblock->leftlink == NULL);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
155 deque->leftblock->leftlink = b;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
156 deque->leftblock = b;
33648
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33644
diff changeset
157 deque->leftindex = BLOCKLEN;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
158 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
159 Py_INCREF(item);
33648
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33644
diff changeset
160 deque->len++;
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33644
diff changeset
161 deque->leftindex--;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
162 deque->leftblock->data[deque->leftindex] = item;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
163 Py_RETURN_NONE;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
164 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
165
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
166 PyDoc_STRVAR(appendleft_doc, "Add an element to the left side of the deque.");
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
167
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
168 static PyObject *
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
169 deque_pop(dequeobject *deque, PyObject *unused)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
170 {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
171 PyObject *item;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
172 block *prevblock;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
173
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
174 if (deque->len == 0) {
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
175 PyErr_SetString(PyExc_IndexError, "pop from an empty deque");
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
176 return NULL;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
177 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
178 item = deque->rightblock->data[deque->rightindex];
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
179 deque->rightindex--;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
180 deque->len--;
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
181 deque->state++;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
182
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
183 if (deque->rightindex == -1) {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
184 if (deque->len == 0) {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
185 assert(deque->leftblock == deque->rightblock);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
186 assert(deque->leftindex == deque->rightindex+1);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
187 /* re-center instead of freeing a block */
33641
cd70f1ac3c25 * Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents: 33639
diff changeset
188 deque->leftindex = CENTER + 1;
cd70f1ac3c25 * Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents: 33639
diff changeset
189 deque->rightindex = CENTER;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
190 } else {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
191 prevblock = deque->rightblock->leftlink;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
192 assert(deque->leftblock != deque->rightblock);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
193 PyMem_Free(deque->rightblock);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
194 prevblock->rightlink = NULL;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
195 deque->rightblock = prevblock;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
196 deque->rightindex = BLOCKLEN - 1;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
197 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
198 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
199 return item;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
200 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
201
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
202 PyDoc_STRVAR(pop_doc, "Remove and return the rightmost element.");
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
203
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
204 static PyObject *
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
205 deque_popleft(dequeobject *deque, PyObject *unused)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
206 {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
207 PyObject *item;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
208 block *prevblock;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
209
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
210 if (deque->len == 0) {
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
211 PyErr_SetString(PyExc_IndexError, "pop from an empty deque");
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
212 return NULL;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
213 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
214 item = deque->leftblock->data[deque->leftindex];
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
215 deque->leftindex++;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
216 deque->len--;
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
217 deque->state++;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
218
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
219 if (deque->leftindex == BLOCKLEN) {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
220 if (deque->len == 0) {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
221 assert(deque->leftblock == deque->rightblock);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
222 assert(deque->leftindex == deque->rightindex+1);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
223 /* re-center instead of freeing a block */
33641
cd70f1ac3c25 * Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents: 33639
diff changeset
224 deque->leftindex = CENTER + 1;
cd70f1ac3c25 * Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents: 33639
diff changeset
225 deque->rightindex = CENTER;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
226 } else {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
227 assert(deque->leftblock != deque->rightblock);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
228 prevblock = deque->leftblock->rightlink;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
229 assert(deque->leftblock != NULL);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
230 PyMem_Free(deque->leftblock);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
231 assert(prevblock != NULL);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
232 prevblock->leftlink = NULL;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
233 deque->leftblock = prevblock;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
234 deque->leftindex = 0;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
235 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
236 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
237 return item;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
238 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
239
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
240 PyDoc_STRVAR(popleft_doc, "Remove and return the leftmost element.");
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
241
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
242 static PyObject *
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
243 deque_extend(dequeobject *deque, PyObject *iterable)
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
244 {
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
245 PyObject *it, *item;
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
246
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
247 it = PyObject_GetIter(iterable);
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
248 if (it == NULL)
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
249 return NULL;
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
250
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
251 while ((item = PyIter_Next(it)) != NULL) {
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
252 deque->state++;
33648
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33644
diff changeset
253 if (deque->rightindex == BLOCKLEN-1) {
33677
262e1cf41d34 Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
254 block *b = newblock(deque->rightblock, NULL,
262e1cf41d34 Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
255 deque->len);
31466
f04ccc288542 * Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents: 31464
diff changeset
256 if (b == NULL) {
f04ccc288542 * Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents: 31464
diff changeset
257 Py_DECREF(item);
f04ccc288542 * Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents: 31464
diff changeset
258 Py_DECREF(it);
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
259 return NULL;
31466
f04ccc288542 * Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents: 31464
diff changeset
260 }
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
261 assert(deque->rightblock->rightlink == NULL);
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
262 deque->rightblock->rightlink = b;
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
263 deque->rightblock = b;
33648
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33644
diff changeset
264 deque->rightindex = -1;
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
265 }
33648
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33644
diff changeset
266 deque->len++;
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33644
diff changeset
267 deque->rightindex++;
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
268 deque->rightblock->data[deque->rightindex] = item;
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
269 }
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
270 Py_DECREF(it);
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
271 if (PyErr_Occurred())
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
272 return NULL;
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
273 Py_RETURN_NONE;
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
274 }
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
275
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
276 PyDoc_STRVAR(extend_doc,
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
277 "Extend the right side of the deque with elements from the iterable");
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
278
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
279 static PyObject *
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
280 deque_extendleft(dequeobject *deque, PyObject *iterable)
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
281 {
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
282 PyObject *it, *item;
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
283
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
284 it = PyObject_GetIter(iterable);
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
285 if (it == NULL)
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
286 return NULL;
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
287
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
288 while ((item = PyIter_Next(it)) != NULL) {
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
289 deque->state++;
33648
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33644
diff changeset
290 if (deque->leftindex == 0) {
33677
262e1cf41d34 Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
291 block *b = newblock(NULL, deque->leftblock,
262e1cf41d34 Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
292 deque->len);
31466
f04ccc288542 * Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents: 31464
diff changeset
293 if (b == NULL) {
f04ccc288542 * Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents: 31464
diff changeset
294 Py_DECREF(item);
f04ccc288542 * Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents: 31464
diff changeset
295 Py_DECREF(it);
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
296 return NULL;
31466
f04ccc288542 * Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents: 31464
diff changeset
297 }
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
298 assert(deque->leftblock->leftlink == NULL);
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
299 deque->leftblock->leftlink = b;
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
300 deque->leftblock = b;
33648
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33644
diff changeset
301 deque->leftindex = BLOCKLEN;
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
302 }
33648
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33644
diff changeset
303 deque->len++;
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33644
diff changeset
304 deque->leftindex--;
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
305 deque->leftblock->data[deque->leftindex] = item;
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
306 }
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
307 Py_DECREF(it);
32554
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32413
diff changeset
308 if (PyErr_Occurred())
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
309 return NULL;
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
310 Py_RETURN_NONE;
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
311 }
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
312
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
313 PyDoc_STRVAR(extendleft_doc,
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
314 "Extend the left side of the deque with elements from the iterable");
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
315
33697
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
316 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
317 _deque_rotate(dequeobject *deque, Py_ssize_t n)
31474
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
318 {
33697
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
319 int i, len=deque->len, halflen=(len+1)>>1;
31474
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
320 PyObject *item, *rv;
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
321
31476
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
322 if (len == 0)
33697
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
323 return 0;
31476
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
324 if (n > halflen || n < -halflen) {
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
325 n %= len;
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
326 if (n > halflen)
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
327 n -= len;
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
328 else if (n < -halflen)
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
329 n += len;
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
330 }
31474
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
331
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
332 for (i=0 ; i<n ; i++) {
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
333 item = deque_pop(deque, NULL);
32554
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32413
diff changeset
334 assert (item != NULL);
31474
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
335 rv = deque_appendleft(deque, item);
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
336 Py_DECREF(item);
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
337 if (rv == NULL)
33697
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
338 return -1;
31474
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
339 Py_DECREF(rv);
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
340 }
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
341 for (i=0 ; i>n ; i--) {
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
342 item = deque_popleft(deque, NULL);
32554
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32413
diff changeset
343 assert (item != NULL);
31474
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
344 rv = deque_append(deque, item);
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
345 Py_DECREF(item);
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
346 if (rv == NULL)
33697
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
347 return -1;
31474
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
348 Py_DECREF(rv);
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
349 }
33697
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
350 return 0;
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
351 }
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
352
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
353 static PyObject *
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
354 deque_rotate(dequeobject *deque, PyObject *args)
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
355 {
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
356 int n=1;
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
357
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
358 if (!PyArg_ParseTuple(args, "|i:rotate", &n))
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
359 return NULL;
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
360 if (_deque_rotate(deque, n) == 0)
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
361 Py_RETURN_NONE;
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
362 return NULL;
31474
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
363 }
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
364
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
365 PyDoc_STRVAR(rotate_doc,
31476
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
366 "Rotate the deque n steps to the right (default n=1). If n is negative, rotates left.");
31474
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
367
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
368 static Py_ssize_t
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
369 deque_len(dequeobject *deque)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
370 {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
371 return deque->len;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
372 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
373
34771
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
374 static PyObject *
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
375 deque_remove(dequeobject *deque, PyObject *value)
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
376 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
377 Py_ssize_t i, n=deque->len;
34771
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
378
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
379 for (i=0 ; i<n ; i++) {
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
380 PyObject *item = deque->leftblock->data[deque->leftindex];
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
381 int cmp = PyObject_RichCompareBool(item, value, Py_EQ);
34772
2ee575b9b60e Apply remove's mutation test after every equality test.
Raymond Hettinger <python@rcn.com>
parents: 34771
diff changeset
382
2ee575b9b60e Apply remove's mutation test after every equality test.
Raymond Hettinger <python@rcn.com>
parents: 34771
diff changeset
383 if (deque->len != n) {
2ee575b9b60e Apply remove's mutation test after every equality test.
Raymond Hettinger <python@rcn.com>
parents: 34771
diff changeset
384 PyErr_SetString(PyExc_IndexError,
2ee575b9b60e Apply remove's mutation test after every equality test.
Raymond Hettinger <python@rcn.com>
parents: 34771
diff changeset
385 "deque mutated during remove().");
2ee575b9b60e Apply remove's mutation test after every equality test.
Raymond Hettinger <python@rcn.com>
parents: 34771
diff changeset
386 return NULL;
2ee575b9b60e Apply remove's mutation test after every equality test.
Raymond Hettinger <python@rcn.com>
parents: 34771
diff changeset
387 }
34771
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
388 if (cmp > 0) {
34772
2ee575b9b60e Apply remove's mutation test after every equality test.
Raymond Hettinger <python@rcn.com>
parents: 34771
diff changeset
389 PyObject *tgt = deque_popleft(deque, NULL);
34771
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
390 assert (tgt != NULL);
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
391 Py_DECREF(tgt);
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
392 if (_deque_rotate(deque, i) == -1)
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
393 return NULL;
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
394 Py_RETURN_NONE;
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
395 }
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
396 else if (cmp < 0) {
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
397 _deque_rotate(deque, i);
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
398 return NULL;
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
399 }
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
400 _deque_rotate(deque, -1);
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
401 }
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
402 PyErr_SetString(PyExc_ValueError, "deque.remove(x): x not in deque");
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
403 return NULL;
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
404 }
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
405
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
406 PyDoc_STRVAR(remove_doc,
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
407 "D.remove(value) -- remove first occurrence of value.");
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
408
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
409 static int
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
410 deque_clear(dequeobject *deque)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
411 {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
412 PyObject *item;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
413
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
414 while (deque->len) {
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
415 item = deque_pop(deque, NULL);
32554
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32413
diff changeset
416 assert (item != NULL);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
417 Py_DECREF(item);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
418 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
419 assert(deque->leftblock == deque->rightblock &&
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
420 deque->leftindex - 1 == deque->rightindex &&
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
421 deque->len == 0);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
422 return 0;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
423 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
424
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
425 static PyObject *
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
426 deque_item(dequeobject *deque, int i)
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
427 {
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
428 block *b;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
429 PyObject *item;
33648
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33644
diff changeset
430 int n, index=i;
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
431
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
432 if (i < 0 || i >= deque->len) {
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
433 PyErr_SetString(PyExc_IndexError,
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
434 "deque index out of range");
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
435 return NULL;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
436 }
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
437
31615
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
438 if (i == 0) {
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
439 i = deque->leftindex;
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
440 b = deque->leftblock;
31615
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
441 } else if (i == deque->len - 1) {
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
442 i = deque->rightindex;
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
443 b = deque->rightblock;
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
444 } else {
31615
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
445 i += deque->leftindex;
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
446 n = i / BLOCKLEN;
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
447 i %= BLOCKLEN;
33648
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33644
diff changeset
448 if (index < (deque->len >> 1)) {
31615
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
449 b = deque->leftblock;
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
450 while (n--)
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
451 b = b->rightlink;
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
452 } else {
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
453 n = (deque->leftindex + deque->len - 1) / BLOCKLEN - n;
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
454 b = deque->rightblock;
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
455 while (n--)
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
456 b = b->leftlink;
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
457 }
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
458 }
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
459 item = b->data[i];
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
460 Py_INCREF(item);
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
461 return item;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
462 }
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
463
32413
d7110bb6c13d Add a comment with implementation notes.
Raymond Hettinger <python@rcn.com>
parents: 32363
diff changeset
464 /* delitem() implemented in terms of rotate for simplicity and reasonable
d7110bb6c13d Add a comment with implementation notes.
Raymond Hettinger <python@rcn.com>
parents: 32363
diff changeset
465 performance near the end points. If for some reason this method becomes
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
466 popular, it is not hard to re-implement this using direct data movement
32413
d7110bb6c13d Add a comment with implementation notes.
Raymond Hettinger <python@rcn.com>
parents: 32363
diff changeset
467 (similar to code in list slice assignment) and achieve a two or threefold
d7110bb6c13d Add a comment with implementation notes.
Raymond Hettinger <python@rcn.com>
parents: 32363
diff changeset
468 performance boost.
d7110bb6c13d Add a comment with implementation notes.
Raymond Hettinger <python@rcn.com>
parents: 32363
diff changeset
469 */
d7110bb6c13d Add a comment with implementation notes.
Raymond Hettinger <python@rcn.com>
parents: 32363
diff changeset
470
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
471 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
472 deque_del_item(dequeobject *deque, Py_ssize_t i)
32084
245835ed8de8 Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents: 31908
diff changeset
473 {
33697
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
474 PyObject *item;
32084
245835ed8de8 Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents: 31908
diff changeset
475
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
476 assert (i >= 0 && i < deque->len);
33697
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
477 if (_deque_rotate(deque, -i) == -1)
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
478 return -1;
32084
245835ed8de8 Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents: 31908
diff changeset
479
245835ed8de8 Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents: 31908
diff changeset
480 item = deque_popleft(deque, NULL);
32554
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32413
diff changeset
481 assert (item != NULL);
32084
245835ed8de8 Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents: 31908
diff changeset
482 Py_DECREF(item);
245835ed8de8 Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents: 31908
diff changeset
483
33697
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
484 return _deque_rotate(deque, i);
32084
245835ed8de8 Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents: 31908
diff changeset
485 }
245835ed8de8 Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents: 31908
diff changeset
486
245835ed8de8 Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents: 31908
diff changeset
487 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
488 deque_ass_item(dequeobject *deque, Py_ssize_t i, PyObject *v)
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
489 {
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
490 PyObject *old_value;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
491 block *b;
36518
9f3f87e920a6 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
492 Py_ssize_t n, len=deque->len, halflen=(len+1)>>1, index=i;
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
493
32554
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32413
diff changeset
494 if (i < 0 || i >= len) {
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
495 PyErr_SetString(PyExc_IndexError,
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
496 "deque index out of range");
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
497 return -1;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
498 }
32084
245835ed8de8 Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents: 31908
diff changeset
499 if (v == NULL)
245835ed8de8 Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents: 31908
diff changeset
500 return deque_del_item(deque, i);
245835ed8de8 Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents: 31908
diff changeset
501
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
502 i += deque->leftindex;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
503 n = i / BLOCKLEN;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
504 i %= BLOCKLEN;
32554
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32413
diff changeset
505 if (index <= halflen) {
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
506 b = deque->leftblock;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
507 while (n--)
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
508 b = b->rightlink;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
509 } else {
32554
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32413
diff changeset
510 n = (deque->leftindex + len - 1) / BLOCKLEN - n;
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
511 b = deque->rightblock;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
512 while (n--)
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
513 b = b->leftlink;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
514 }
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
515 Py_INCREF(v);
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
516 old_value = b->data[i];
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
517 b->data[i] = v;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
518 Py_DECREF(old_value);
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
519 return 0;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
520 }
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
521
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
522 static PyObject *
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
523 deque_clearmethod(dequeobject *deque)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
524 {
32554
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32413
diff changeset
525 int rv;
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32413
diff changeset
526
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32413
diff changeset
527 rv = deque_clear(deque);
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32413
diff changeset
528 assert (rv != -1);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
529 Py_RETURN_NONE;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
530 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
531
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
532 PyDoc_STRVAR(clear_doc, "Remove all elements from the deque.");
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
533
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
534 static void
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
535 deque_dealloc(dequeobject *deque)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
536 {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
537 PyObject_GC_UnTrack(deque);
32158
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 32119
diff changeset
538 if (deque->weakreflist != NULL)
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 32119
diff changeset
539 PyObject_ClearWeakRefs((PyObject *) deque);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
540 if (deque->leftblock != NULL) {
32752
dbd3a04eb0c6 Silence a GCC unused variable warning in debug builds.
Raymond Hettinger <python@rcn.com>
parents: 32701
diff changeset
541 deque_clear(deque);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
542 assert(deque->leftblock != NULL);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
543 PyMem_Free(deque->leftblock);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
544 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
545 deque->leftblock = NULL;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
546 deque->rightblock = NULL;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
547 deque->ob_type->tp_free(deque);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
548 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
549
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
550 static int
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
551 deque_traverse(dequeobject *deque, visitproc visit, void *arg)
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
552 {
33639
40350fbe665d deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents: 33638
diff changeset
553 block *b;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
554 PyObject *item;
33639
40350fbe665d deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents: 33638
diff changeset
555 int index;
40350fbe665d deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents: 33638
diff changeset
556 int indexlo = deque->leftindex;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
557
33639
40350fbe665d deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents: 33638
diff changeset
558 for (b = deque->leftblock; b != NULL; b = b->rightlink) {
40350fbe665d deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents: 33638
diff changeset
559 const int indexhi = b == deque->rightblock ?
40350fbe665d deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents: 33638
diff changeset
560 deque->rightindex :
40350fbe665d deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents: 33638
diff changeset
561 BLOCKLEN - 1;
40350fbe665d deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents: 33638
diff changeset
562
40350fbe665d deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents: 33638
diff changeset
563 for (index = indexlo; index <= indexhi; ++index) {
40350fbe665d deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents: 33638
diff changeset
564 item = b->data[index];
40350fbe665d deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents: 33638
diff changeset
565 Py_VISIT(item);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
566 }
33639
40350fbe665d deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents: 33638
diff changeset
567 indexlo = 0;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
568 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
569 return 0;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
570 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
571
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
572 static long
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
573 deque_nohash(PyObject *self)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
574 {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
575 PyErr_SetString(PyExc_TypeError, "deque objects are unhashable");
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
576 return -1;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
577 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
578
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
579 static PyObject *
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
580 deque_copy(PyObject *deque)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
581 {
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
582 return PyObject_CallFunctionObjArgs((PyObject *)(deque->ob_type),
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
583 deque, NULL);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
584 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
585
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
586 PyDoc_STRVAR(copy_doc, "Return a shallow copy of a deque.");
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
587
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
588 static PyObject *
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
589 deque_reduce(dequeobject *deque)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
590 {
33949
b9819b9a0dad SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33879
diff changeset
591 PyObject *dict, *result, *it;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
592
33949
b9819b9a0dad SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33879
diff changeset
593 dict = PyObject_GetAttrString((PyObject *)deque, "__dict__");
b9819b9a0dad SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33879
diff changeset
594 if (dict == NULL) {
b9819b9a0dad SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33879
diff changeset
595 PyErr_Clear();
b9819b9a0dad SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33879
diff changeset
596 dict = Py_None;
b9819b9a0dad SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33879
diff changeset
597 Py_INCREF(dict);
b9819b9a0dad SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33879
diff changeset
598 }
b9819b9a0dad SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33879
diff changeset
599 it = PyObject_GetIter((PyObject *)deque);
b9819b9a0dad SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33879
diff changeset
600 if (it == NULL) {
b9819b9a0dad SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33879
diff changeset
601 Py_DECREF(dict);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
602 return NULL;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
603 }
33949
b9819b9a0dad SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33879
diff changeset
604 result = Py_BuildValue("O()ON", deque->ob_type, dict, it);
b9819b9a0dad SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33879
diff changeset
605 Py_DECREF(dict);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
606 return result;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
607 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
608
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
609 PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
610
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
611 static PyObject *
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
612 deque_repr(PyObject *deque)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
613 {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
614 PyObject *aslist, *result, *fmt;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
615 int i;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
616
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
617 i = Py_ReprEnter(deque);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
618 if (i != 0) {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
619 if (i < 0)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
620 return NULL;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
621 return PyString_FromString("[...]");
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
622 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
623
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
624 aslist = PySequence_List(deque);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
625 if (aslist == NULL) {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
626 Py_ReprLeave(deque);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
627 return NULL;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
628 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
629
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
630 fmt = PyString_FromString("deque(%r)");
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
631 if (fmt == NULL) {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
632 Py_DECREF(aslist);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
633 Py_ReprLeave(deque);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
634 return NULL;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
635 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
636 result = PyString_Format(fmt, aslist);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
637 Py_DECREF(fmt);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
638 Py_DECREF(aslist);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
639 Py_ReprLeave(deque);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
640 return result;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
641 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
642
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
643 static int
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
644 deque_tp_print(PyObject *deque, FILE *fp, int flags)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
645 {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
646 PyObject *it, *item;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
647 char *emit = ""; /* No separator emitted on first pass */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
648 char *separator = ", ";
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
649 int i;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
650
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
651 i = Py_ReprEnter(deque);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
652 if (i != 0) {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
653 if (i < 0)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
654 return i;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
655 fputs("[...]", fp);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
656 return 0;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
657 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
658
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
659 it = PyObject_GetIter(deque);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
660 if (it == NULL)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
661 return -1;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
662
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
663 fputs("deque([", fp);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
664 while ((item = PyIter_Next(it)) != NULL) {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
665 fputs(emit, fp);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
666 emit = separator;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
667 if (PyObject_Print(item, fp, 0) != 0) {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
668 Py_DECREF(item);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
669 Py_DECREF(it);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
670 Py_ReprLeave(deque);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
671 return -1;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
672 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
673 Py_DECREF(item);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
674 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
675 Py_ReprLeave(deque);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
676 Py_DECREF(it);
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
677 if (PyErr_Occurred())
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
678 return -1;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
679 fputs("])", fp);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
680 return 0;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
681 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
682
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
683 static PyObject *
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
684 deque_richcompare(PyObject *v, PyObject *w, int op)
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
685 {
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
686 PyObject *it1=NULL, *it2=NULL, *x, *y;
33648
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33644
diff changeset
687 int b, vs, ws, cmp=-1;
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
688
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
689 if (!PyObject_TypeCheck(v, &deque_type) ||
32119
dd1e55deaf52 Make type check work with subclasses
Raymond Hettinger <python@rcn.com>
parents: 32084
diff changeset
690 !PyObject_TypeCheck(w, &deque_type)) {
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
691 Py_INCREF(Py_NotImplemented);
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
692 return Py_NotImplemented;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
693 }
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
694
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
695 /* Shortcuts */
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
696 vs = ((dequeobject *)v)->len;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
697 ws = ((dequeobject *)w)->len;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
698 if (op == Py_EQ) {
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
699 if (v == w)
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
700 Py_RETURN_TRUE;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
701 if (vs != ws)
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
702 Py_RETURN_FALSE;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
703 }
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
704 if (op == Py_NE) {
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
705 if (v == w)
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
706 Py_RETURN_FALSE;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
707 if (vs != ws)
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
708 Py_RETURN_TRUE;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
709 }
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
710
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
711 /* Search for the first index where items are different */
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
712 it1 = PyObject_GetIter(v);
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
713 if (it1 == NULL)
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
714 goto done;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
715 it2 = PyObject_GetIter(w);
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
716 if (it2 == NULL)
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
717 goto done;
33648
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33644
diff changeset
718 for (;;) {
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
719 x = PyIter_Next(it1);
33648
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33644
diff changeset
720 if (x == NULL && PyErr_Occurred())
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
721 goto done;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
722 y = PyIter_Next(it2);
33648
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33644
diff changeset
723 if (x == NULL || y == NULL)
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33644
diff changeset
724 break;
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
725 b = PyObject_RichCompareBool(x, y, Py_EQ);
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
726 if (b == 0) {
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
727 cmp = PyObject_RichCompareBool(x, y, op);
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
728 Py_DECREF(x);
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
729 Py_DECREF(y);
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
730 goto done;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
731 }
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
732 Py_DECREF(x);
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
733 Py_DECREF(y);
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
734 if (b == -1)
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
735 goto done;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
736 }
33648
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33644
diff changeset
737 /* We reached the end of one deque or both */
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33644
diff changeset
738 Py_XDECREF(x);
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33644
diff changeset
739 Py_XDECREF(y);
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33644
diff changeset
740 if (PyErr_Occurred())
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33644
diff changeset
741 goto done;
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
742 switch (op) {
33648
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33644
diff changeset
743 case Py_LT: cmp = y != NULL; break; /* if w was longer */
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33644
diff changeset
744 case Py_LE: cmp = x == NULL; break; /* if v was not longer */
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33644
diff changeset
745 case Py_EQ: cmp = x == y; break; /* if we reached the end of both */
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33644
diff changeset
746 case Py_NE: cmp = x != y; break; /* if one deque continues */
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33644
diff changeset
747 case Py_GT: cmp = x != NULL; break; /* if v was longer */
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33644
diff changeset
748 case Py_GE: cmp = y == NULL; break; /* if w was not longer */
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
749 }
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
750
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
751 done:
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
752 Py_XDECREF(it1);
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
753 Py_XDECREF(it2);
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
754 if (cmp == 1)
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
755 Py_RETURN_TRUE;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
756 if (cmp == 0)
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
757 Py_RETURN_FALSE;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
758 return NULL;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
759 }
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
760
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
761 static int
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
762 deque_init(dequeobject *deque, PyObject *args, PyObject *kwds)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
763 {
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
764 PyObject *iterable = NULL;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
765
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
766 if (!PyArg_UnpackTuple(args, "deque", 0, 1, &iterable))
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
767 return -1;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
768
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
769 if (iterable != NULL) {
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
770 PyObject *rv = deque_extend(deque, iterable);
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
771 if (rv == NULL)
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
772 return -1;
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
773 Py_DECREF(rv);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
774 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
775 return 0;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
776 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
777
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
778 static PySequenceMethods deque_as_sequence = {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
779 (lenfunc)deque_len, /* sq_length */
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
780 0, /* sq_concat */
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
781 0, /* sq_repeat */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
782 (ssizeargfunc)deque_item, /* sq_item */
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
783 0, /* sq_slice */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
784 (ssizeobjargproc)deque_ass_item, /* sq_ass_item */
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
785 };
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
786
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
787 /* deque object ********************************************************/
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
788
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
789 static PyObject *deque_iter(dequeobject *deque);
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
790 static PyObject *deque_reviter(dequeobject *deque);
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
791 PyDoc_STRVAR(reversed_doc,
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
792 "D.__reversed__() -- return a reverse iterator over the deque");
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
793
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
794 static PyMethodDef deque_methods[] = {
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
795 {"append", (PyCFunction)deque_append,
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
796 METH_O, append_doc},
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
797 {"appendleft", (PyCFunction)deque_appendleft,
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
798 METH_O, appendleft_doc},
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
799 {"clear", (PyCFunction)deque_clearmethod,
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
800 METH_NOARGS, clear_doc},
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
801 {"__copy__", (PyCFunction)deque_copy,
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
802 METH_NOARGS, copy_doc},
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
803 {"extend", (PyCFunction)deque_extend,
31474
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
804 METH_O, extend_doc},
34771
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
805 {"extendleft", (PyCFunction)deque_extendleft,
31474
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
806 METH_O, extendleft_doc},
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
807 {"pop", (PyCFunction)deque_pop,
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
808 METH_NOARGS, pop_doc},
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
809 {"popleft", (PyCFunction)deque_popleft,
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
810 METH_NOARGS, popleft_doc},
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
811 {"__reduce__", (PyCFunction)deque_reduce,
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
812 METH_NOARGS, reduce_doc},
34771
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
813 {"remove", (PyCFunction)deque_remove,
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
814 METH_O, remove_doc},
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
815 {"__reversed__", (PyCFunction)deque_reviter,
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
816 METH_NOARGS, reversed_doc},
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
817 {"rotate", (PyCFunction)deque_rotate,
31474
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
818 METH_VARARGS, rotate_doc},
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
819 {NULL, NULL} /* sentinel */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
820 };
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
821
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
822 PyDoc_STRVAR(deque_doc,
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
823 "deque(iterable) --> deque object\n\
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
824 \n\
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
825 Build an ordered collection accessible from endpoints only.");
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
826
31605
2fed331de014 Make deque_type static so namespace is not polluted.
Neal Norwitz <nnorwitz@gmail.com>
parents: 31603
diff changeset
827 static PyTypeObject deque_type = {
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
828 PyObject_HEAD_INIT(NULL)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
829 0, /* ob_size */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
830 "collections.deque", /* tp_name */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
831 sizeof(dequeobject), /* tp_basicsize */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
832 0, /* tp_itemsize */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
833 /* methods */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
834 (destructor)deque_dealloc, /* tp_dealloc */
37305
5e1b6f553763 Remove unnecessary casts from type object initializers.
Georg Brandl <georg@python.org>
parents: 36656
diff changeset
835 deque_tp_print, /* tp_print */
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
836 0, /* tp_getattr */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
837 0, /* tp_setattr */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
838 0, /* tp_compare */
37305
5e1b6f553763 Remove unnecessary casts from type object initializers.
Georg Brandl <georg@python.org>
parents: 36656
diff changeset
839 deque_repr, /* tp_repr */
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
840 0, /* tp_as_number */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
841 &deque_as_sequence, /* tp_as_sequence */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
842 0, /* tp_as_mapping */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
843 deque_nohash, /* tp_hash */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
844 0, /* tp_call */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
845 0, /* tp_str */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
846 PyObject_GenericGetAttr, /* tp_getattro */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
847 0, /* tp_setattro */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
848 0, /* tp_as_buffer */
32158
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 32119
diff changeset
849 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 32119
diff changeset
850 Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
851 deque_doc, /* tp_doc */
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
852 (traverseproc)deque_traverse, /* tp_traverse */
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
853 (inquiry)deque_clear, /* tp_clear */
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
854 (richcmpfunc)deque_richcompare, /* tp_richcompare */
32158
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 32119
diff changeset
855 offsetof(dequeobject, weakreflist), /* tp_weaklistoffset*/
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
856 (getiterfunc)deque_iter, /* tp_iter */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
857 0, /* tp_iternext */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
858 deque_methods, /* tp_methods */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
859 0, /* tp_members */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
860 0, /* tp_getset */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
861 0, /* tp_base */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
862 0, /* tp_dict */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
863 0, /* tp_descr_get */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
864 0, /* tp_descr_set */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
865 0, /* tp_dictoffset */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
866 (initproc)deque_init, /* tp_init */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
867 PyType_GenericAlloc, /* tp_alloc */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
868 deque_new, /* tp_new */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
869 PyObject_GC_Del, /* tp_free */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
870 };
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
871
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
872 /*********************** Deque Iterator **************************/
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
873
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
874 typedef struct {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
875 PyObject_HEAD
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
876 int index;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
877 block *b;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
878 dequeobject *deque;
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
879 long state; /* state when the iterator is created */
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
880 int counter; /* number of items remaining for iteration */
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
881 } dequeiterobject;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
882
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
883 PyTypeObject dequeiter_type;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
884
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
885 static PyObject *
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
886 deque_iter(dequeobject *deque)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
887 {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
888 dequeiterobject *it;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
889
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
890 it = PyObject_New(dequeiterobject, &dequeiter_type);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
891 if (it == NULL)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
892 return NULL;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
893 it->b = deque->leftblock;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
894 it->index = deque->leftindex;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
895 Py_INCREF(deque);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
896 it->deque = deque;
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
897 it->state = deque->state;
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
898 it->counter = deque->len;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
899 return (PyObject *)it;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
900 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
901
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
902 static void
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
903 dequeiter_dealloc(dequeiterobject *dio)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
904 {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
905 Py_XDECREF(dio->deque);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
906 dio->ob_type->tp_free(dio);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
907 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
908
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
909 static PyObject *
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
910 dequeiter_next(dequeiterobject *it)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
911 {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
912 PyObject *item;
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
913
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
914 if (it->counter == 0)
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
915 return NULL;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
916
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
917 if (it->deque->state != it->state) {
31908
058821d8aefd * Add unittests for iterators that report their length
Raymond Hettinger <python@rcn.com>
parents: 31685
diff changeset
918 it->counter = 0;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
919 PyErr_SetString(PyExc_RuntimeError,
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
920 "deque mutated during iteration");
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
921 return NULL;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
922 }
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
923 assert (!(it->b == it->deque->rightblock &&
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
924 it->index > it->deque->rightindex));
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
925
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
926 item = it->b->data[it->index];
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
927 it->index++;
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
928 it->counter--;
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
929 if (it->index == BLOCKLEN && it->counter > 0) {
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
930 assert (it->b->rightlink != NULL);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
931 it->b = it->b->rightlink;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
932 it->index = 0;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
933 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
934 Py_INCREF(item);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
935 return item;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
936 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
937
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
938 static PyObject *
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
939 dequeiter_len(dequeiterobject *it)
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
940 {
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
941 return PyInt_FromLong(it->counter);
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
942 }
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
943
36484
20f75d5e79e9 Renamed _length_cue() to __length_hint__(). See:
Armin Rigo <arigo@tunes.org>
parents: 36324
diff changeset
944 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: 35449
diff changeset
945
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
946 static PyMethodDef dequeiter_methods[] = {
36484
20f75d5e79e9 Renamed _length_cue() to __length_hint__(). See:
Armin Rigo <arigo@tunes.org>
parents: 36324
diff changeset
947 {"__length_hint__", (PyCFunction)dequeiter_len, METH_NOARGS, length_hint_doc},
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
948 {NULL, NULL} /* sentinel */
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
949 };
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
950
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
951 PyTypeObject dequeiter_type = {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
952 PyObject_HEAD_INIT(NULL)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
953 0, /* ob_size */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
954 "deque_iterator", /* tp_name */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
955 sizeof(dequeiterobject), /* tp_basicsize */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
956 0, /* tp_itemsize */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
957 /* methods */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
958 (destructor)dequeiter_dealloc, /* tp_dealloc */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
959 0, /* tp_print */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
960 0, /* tp_getattr */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
961 0, /* tp_setattr */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
962 0, /* tp_compare */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
963 0, /* tp_repr */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
964 0, /* tp_as_number */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
965 0, /* tp_as_sequence */
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
966 0, /* tp_as_mapping */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
967 0, /* tp_hash */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
968 0, /* tp_call */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
969 0, /* tp_str */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
970 PyObject_GenericGetAttr, /* tp_getattro */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
971 0, /* tp_setattro */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
972 0, /* tp_as_buffer */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
973 Py_TPFLAGS_DEFAULT, /* tp_flags */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
974 0, /* tp_doc */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
975 0, /* tp_traverse */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
976 0, /* tp_clear */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
977 0, /* tp_richcompare */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
978 0, /* tp_weaklistoffset */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
979 PyObject_SelfIter, /* tp_iter */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
980 (iternextfunc)dequeiter_next, /* tp_iternext */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
981 dequeiter_methods, /* tp_methods */
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
982 0,
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
983 };
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
984
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
985 /*********************** Deque Reverse Iterator **************************/
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
986
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
987 PyTypeObject dequereviter_type;
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
988
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
989 static PyObject *
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
990 deque_reviter(dequeobject *deque)
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
991 {
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
992 dequeiterobject *it;
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
993
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
994 it = PyObject_New(dequeiterobject, &dequereviter_type);
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
995 if (it == NULL)
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
996 return NULL;
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
997 it->b = deque->rightblock;
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
998 it->index = deque->rightindex;
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
999 Py_INCREF(deque);
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1000 it->deque = deque;
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
1001 it->state = deque->state;
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1002 it->counter = deque->len;
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1003 return (PyObject *)it;
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1004 }
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1005
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1006 static PyObject *
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1007 dequereviter_next(dequeiterobject *it)
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1008 {
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1009 PyObject *item;
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
1010 if (it->counter == 0)
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1011 return NULL;
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1012
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
1013 if (it->deque->state != it->state) {
31908
058821d8aefd * Add unittests for iterators that report their length
Raymond Hettinger <python@rcn.com>
parents: 31685
diff changeset
1014 it->counter = 0;
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1015 PyErr_SetString(PyExc_RuntimeError,
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
1016 "deque mutated during iteration");
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1017 return NULL;
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1018 }
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
1019 assert (!(it->b == it->deque->leftblock &&
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
1020 it->index < it->deque->leftindex));
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1021
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1022 item = it->b->data[it->index];
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1023 it->index--;
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
1024 it->counter--;
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
1025 if (it->index == -1 && it->counter > 0) {
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
1026 assert (it->b->leftlink != NULL);
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1027 it->b = it->b->leftlink;
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1028 it->index = BLOCKLEN - 1;
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1029 }
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1030 Py_INCREF(item);
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1031 return item;
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1032 }
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1033
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1034 PyTypeObject dequereviter_type = {
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1035 PyObject_HEAD_INIT(NULL)
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1036 0, /* ob_size */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1037 "deque_reverse_iterator", /* tp_name */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1038 sizeof(dequeiterobject), /* tp_basicsize */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1039 0, /* tp_itemsize */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1040 /* methods */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1041 (destructor)dequeiter_dealloc, /* tp_dealloc */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1042 0, /* tp_print */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1043 0, /* tp_getattr */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1044 0, /* tp_setattr */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1045 0, /* tp_compare */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1046 0, /* tp_repr */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1047 0, /* tp_as_number */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
1048 0, /* tp_as_sequence */
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1049 0, /* tp_as_mapping */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1050 0, /* tp_hash */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1051 0, /* tp_call */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1052 0, /* tp_str */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1053 PyObject_GenericGetAttr, /* tp_getattro */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1054 0, /* tp_setattro */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1055 0, /* tp_as_buffer */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1056 Py_TPFLAGS_DEFAULT, /* tp_flags */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1057 0, /* tp_doc */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1058 0, /* tp_traverse */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1059 0, /* tp_clear */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1060 0, /* tp_richcompare */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1061 0, /* tp_weaklistoffset */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1062 PyObject_SelfIter, /* tp_iter */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1063 (iternextfunc)dequereviter_next, /* tp_iternext */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
1064 dequeiter_methods, /* tp_methods */
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1065 0,
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1066 };
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1067
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1068 /* defaultdict type *********************************************************/
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1069
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1070 typedef struct {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1071 PyDictObject dict;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1072 PyObject *default_factory;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1073 } defdictobject;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1074
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1075 static PyTypeObject defdict_type; /* Forward */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1076
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1077 PyDoc_STRVAR(defdict_missing_doc,
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1078 "__missing__(key) # Called by __getitem__ for missing key; pseudo-code:\n\
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1079 if self.default_factory is None: raise KeyError(key)\n\
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1080 self[key] = value = self.default_factory()\n\
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1081 return value\n\
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1082 ");
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1083
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1084 static PyObject *
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1085 defdict_missing(defdictobject *dd, PyObject *key)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1086 {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1087 PyObject *factory = dd->default_factory;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1088 PyObject *value;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1089 if (factory == NULL || factory == Py_None) {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1090 /* XXX Call dict.__missing__(key) */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1091 PyErr_SetObject(PyExc_KeyError, key);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1092 return NULL;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1093 }
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1094 value = PyEval_CallObject(factory, NULL);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1095 if (value == NULL)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1096 return value;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1097 if (PyObject_SetItem((PyObject *)dd, key, value) < 0) {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1098 Py_DECREF(value);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1099 return NULL;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1100 }
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1101 return value;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1102 }
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1103
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1104 PyDoc_STRVAR(defdict_copy_doc, "D.copy() -> a shallow copy of D.");
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1105
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1106 static PyObject *
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1107 defdict_copy(defdictobject *dd)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1108 {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1109 /* This calls the object's class. That only works for subclasses
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1110 whose class constructor has the same signature. Subclasses that
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1111 define a different constructor signature must override copy().
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1112 */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1113 return PyObject_CallFunctionObjArgs((PyObject *)dd->dict.ob_type,
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1114 dd->default_factory, dd, NULL);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1115 }
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1116
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1117 static PyObject *
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1118 defdict_reduce(defdictobject *dd)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1119 {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1120 /* __reduce__ must returns a 5-tuple as follows:
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1121
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1122 - factory function
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1123 - tuple of args for the factory function
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1124 - additional state (here None)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1125 - sequence iterator (here None)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1126 - dictionary iterator (yielding successive (key, value) pairs
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1127
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1128 This API is used by pickle.py and copy.py.
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1129
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1130 For this to be useful with pickle.py, the default_factory
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1131 must be picklable; e.g., None, a built-in, or a global
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1132 function in a module or package.
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1133
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1134 Both shallow and deep copying are supported, but for deep
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1135 copying, the default_factory must be deep-copyable; e.g. None,
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1136 or a built-in (functions are not copyable at this time).
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1137
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1138 This only works for subclasses as long as their constructor
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1139 signature is compatible; the first argument must be the
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1140 optional default_factory, defaulting to None.
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1141 */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1142 PyObject *args;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1143 PyObject *items;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1144 PyObject *result;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1145 if (dd->default_factory == NULL || dd->default_factory == Py_None)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1146 args = PyTuple_New(0);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1147 else
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1148 args = PyTuple_Pack(1, dd->default_factory);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1149 if (args == NULL)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1150 return NULL;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1151 items = PyObject_CallMethod((PyObject *)dd, "iteritems", "()");
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1152 if (items == NULL) {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1153 Py_DECREF(args);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1154 return NULL;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1155 }
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1156 result = PyTuple_Pack(5, dd->dict.ob_type, args,
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1157 Py_None, Py_None, items);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1158 Py_DECREF(args);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1159 return result;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1160 }
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1161
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1162 static PyMethodDef defdict_methods[] = {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1163 {"__missing__", (PyCFunction)defdict_missing, METH_O,
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1164 defdict_missing_doc},
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1165 {"copy", (PyCFunction)defdict_copy, METH_NOARGS,
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1166 defdict_copy_doc},
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1167 {"__copy__", (PyCFunction)defdict_copy, METH_NOARGS,
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1168 defdict_copy_doc},
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1169 {"__reduce__", (PyCFunction)defdict_reduce, METH_NOARGS,
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1170 reduce_doc},
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1171 {NULL}
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1172 };
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1173
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1174 static PyMemberDef defdict_members[] = {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1175 {"default_factory", T_OBJECT,
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1176 offsetof(defdictobject, default_factory), 0,
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1177 PyDoc_STR("Factory for default value called by __missing__().")},
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1178 {NULL}
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1179 };
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1180
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1181 static void
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1182 defdict_dealloc(defdictobject *dd)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1183 {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1184 Py_CLEAR(dd->default_factory);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1185 PyDict_Type.tp_dealloc((PyObject *)dd);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1186 }
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1187
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1188 static int
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1189 defdict_print(defdictobject *dd, FILE *fp, int flags)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1190 {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1191 int sts;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1192 fprintf(fp, "defaultdict(");
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1193 if (dd->default_factory == NULL)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1194 fprintf(fp, "None");
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1195 else {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1196 PyObject_Print(dd->default_factory, fp, 0);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1197 }
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1198 fprintf(fp, ", ");
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1199 sts = PyDict_Type.tp_print((PyObject *)dd, fp, 0);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1200 fprintf(fp, ")");
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1201 return sts;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1202 }
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1203
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1204 static PyObject *
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1205 defdict_repr(defdictobject *dd)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1206 {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1207 PyObject *defrepr;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1208 PyObject *baserepr;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1209 PyObject *result;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1210 baserepr = PyDict_Type.tp_repr((PyObject *)dd);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1211 if (baserepr == NULL)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1212 return NULL;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1213 if (dd->default_factory == NULL)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1214 defrepr = PyString_FromString("None");
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1215 else
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1216 defrepr = PyObject_Repr(dd->default_factory);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1217 if (defrepr == NULL) {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1218 Py_DECREF(baserepr);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1219 return NULL;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1220 }
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1221 result = PyString_FromFormat("defaultdict(%s, %s)",
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1222 PyString_AS_STRING(defrepr),
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1223 PyString_AS_STRING(baserepr));
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1224 Py_DECREF(defrepr);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1225 Py_DECREF(baserepr);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1226 return result;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1227 }
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1228
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1229 static int
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1230 defdict_traverse(PyObject *self, visitproc visit, void *arg)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1231 {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1232 Py_VISIT(((defdictobject *)self)->default_factory);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1233 return PyDict_Type.tp_traverse(self, visit, arg);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1234 }
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1235
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1236 static int
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1237 defdict_tp_clear(defdictobject *dd)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1238 {
37748
d1547426ab61 Use Py_CLEAR instead of in-place DECREF/XDECREF or custom macros, for
Thomas Wouters <thomas@python.org>
parents: 37470
diff changeset
1239 Py_CLEAR(dd->default_factory);
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1240 return PyDict_Type.tp_clear((PyObject *)dd);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1241 }
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1242
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1243 static int
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1244 defdict_init(PyObject *self, PyObject *args, PyObject *kwds)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1245 {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1246 defdictobject *dd = (defdictobject *)self;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1247 PyObject *olddefault = dd->default_factory;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1248 PyObject *newdefault = NULL;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1249 PyObject *newargs;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1250 int result;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1251 if (args == NULL || !PyTuple_Check(args))
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1252 newargs = PyTuple_New(0);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1253 else {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1254 Py_ssize_t n = PyTuple_GET_SIZE(args);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1255 if (n > 0)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1256 newdefault = PyTuple_GET_ITEM(args, 0);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1257 newargs = PySequence_GetSlice(args, 1, n);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1258 }
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1259 if (newargs == NULL)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1260 return -1;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1261 Py_XINCREF(newdefault);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1262 dd->default_factory = newdefault;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1263 result = PyDict_Type.tp_init(self, newargs, kwds);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1264 Py_DECREF(newargs);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1265 Py_XDECREF(olddefault);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1266 return result;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1267 }
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1268
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1269 PyDoc_STRVAR(defdict_doc,
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1270 "defaultdict(default_factory) --> dict with default factory\n\
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1271 \n\
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1272 The default factory is called without arguments to produce\n\
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1273 a new value when a key is not present, in __getitem__ only.\n\
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1274 A defaultdict compares equal to a dict with the same items.\n\
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1275 ");
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1276
37470
9ecbce55a454 SF Bug #1448488 - make collectionsmodule build on Cygwin, using the same
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37305
diff changeset
1277 /* See comment in xxsubtype.c */
9ecbce55a454 SF Bug #1448488 - make collectionsmodule build on Cygwin, using the same
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37305
diff changeset
1278 #define DEFERRED_ADDRESS(ADDR) 0
9ecbce55a454 SF Bug #1448488 - make collectionsmodule build on Cygwin, using the same
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37305
diff changeset
1279
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1280 static PyTypeObject defdict_type = {
37470
9ecbce55a454 SF Bug #1448488 - make collectionsmodule build on Cygwin, using the same
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37305
diff changeset
1281 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type))
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1282 0, /* ob_size */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1283 "collections.defaultdict", /* tp_name */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1284 sizeof(defdictobject), /* tp_basicsize */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1285 0, /* tp_itemsize */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1286 /* methods */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1287 (destructor)defdict_dealloc, /* tp_dealloc */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1288 (printfunc)defdict_print, /* tp_print */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1289 0, /* tp_getattr */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1290 0, /* tp_setattr */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1291 0, /* tp_compare */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1292 (reprfunc)defdict_repr, /* tp_repr */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1293 0, /* tp_as_number */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1294 0, /* tp_as_sequence */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1295 0, /* tp_as_mapping */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1296 0, /* tp_hash */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1297 0, /* tp_call */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1298 0, /* tp_str */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1299 PyObject_GenericGetAttr, /* tp_getattro */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1300 0, /* tp_setattro */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1301 0, /* tp_as_buffer */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1302 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1303 Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1304 defdict_doc, /* tp_doc */
37305
5e1b6f553763 Remove unnecessary casts from type object initializers.
Georg Brandl <georg@python.org>
parents: 36656
diff changeset
1305 defdict_traverse, /* tp_traverse */
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1306 (inquiry)defdict_tp_clear, /* tp_clear */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1307 0, /* tp_richcompare */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1308 0, /* tp_weaklistoffset*/
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1309 0, /* tp_iter */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1310 0, /* tp_iternext */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1311 defdict_methods, /* tp_methods */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1312 defdict_members, /* tp_members */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1313 0, /* tp_getset */
37470
9ecbce55a454 SF Bug #1448488 - make collectionsmodule build on Cygwin, using the same
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37305
diff changeset
1314 DEFERRED_ADDRESS(&PyDict_Type), /* tp_base */
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1315 0, /* tp_dict */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1316 0, /* tp_descr_get */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1317 0, /* tp_descr_set */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1318 0, /* tp_dictoffset */
37305
5e1b6f553763 Remove unnecessary casts from type object initializers.
Georg Brandl <georg@python.org>
parents: 36656
diff changeset
1319 defdict_init, /* tp_init */
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1320 PyType_GenericAlloc, /* tp_alloc */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1321 0, /* tp_new */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1322 PyObject_GC_Del, /* tp_free */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1323 };
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1324
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1325 /* module level code ********************************************************/
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1326
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1327 PyDoc_STRVAR(module_doc,
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1328 "High performance data structures.\n\
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1329 - deque: ordered collection accessible from endpoints only\n\
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1330 - defaultdict: dict subclass with a default value factory\n\
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1331 ");
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1332
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1333 PyMODINIT_FUNC
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1334 initcollections(void)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1335 {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1336 PyObject *m;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1337
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1338 m = Py_InitModule3("collections", NULL, module_doc);
36324
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35636
diff changeset
1339 if (m == NULL)
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35636
diff changeset
1340 return;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1341
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1342 if (PyType_Ready(&deque_type) < 0)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1343 return;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1344 Py_INCREF(&deque_type);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1345 PyModule_AddObject(m, "deque", (PyObject *)&deque_type);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1346
37470
9ecbce55a454 SF Bug #1448488 - make collectionsmodule build on Cygwin, using the same
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37305
diff changeset
1347 defdict_type.tp_base = &PyDict_Type;
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1348 if (PyType_Ready(&defdict_type) < 0)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1349 return;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1350 Py_INCREF(&defdict_type);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1351 PyModule_AddObject(m, "defaultdict", (PyObject *)&defdict_type);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1352
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1353 if (PyType_Ready(&dequeiter_type) < 0)
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
1354 return;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1355
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1356 if (PyType_Ready(&dequereviter_type) < 0)
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1357 return;
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1358
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1359 return;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1360 }