annotate Modules/_collectionsmodule.c @ 58765:2ba7677bfa95 legacy-trunk

testCopysign was defined twice in test_math; combine the definitions
author Mark Dickinson <dickinsm@gmail.com>
date Sat, 06 Feb 2010 23:11:25 +0000
parents 8276d00d3100
children c73a658da6fa
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
39232
cd8f20d400c1 defdict_reduce(): Plug leaks.
Tim Peters <tim.peters@gmail.com>
parents: 37748
diff changeset
13 * length of a cache line.
33643
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
39232
cd8f20d400c1 defdict_reduce(): Plug leaks.
Tim Peters <tim.peters@gmail.com>
parents: 37748
diff changeset
25 * on both ends, algorithms for left and right operations become
33641
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.
39232
cd8f20d400c1 defdict_reduce(): Plug leaks.
Tim Peters <tim.peters@gmail.com>
parents: 37748
diff changeset
27 *
33641
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 *
39232
cd8f20d400c1 defdict_reduce(): Plug leaks.
Tim Peters <tim.peters@gmail.com>
parents: 37748
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.
39232
cd8f20d400c1 defdict_reduce(): Plug leaks.
Tim Peters <tim.peters@gmail.com>
parents: 37748
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
39232
cd8f20d400c1 defdict_reduce(): Plug leaks.
Tim Peters <tim.peters@gmail.com>
parents: 37748
diff changeset
44 * become indices into distinct blocks and either may be larger than the
33642
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
43812
f82a20dda465 Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 43517
diff changeset
54 #define MAXFREEBLOCKS 10
48059
b68704457efe Finish-up the partial conversion from int to Py_ssize_t for deque indices and length.
Raymond Hettinger <python@rcn.com>
parents: 47961
diff changeset
55 static Py_ssize_t numfreeblocks = 0;
43812
f82a20dda465 Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 43517
diff changeset
56 static block *freeblocks[MAXFREEBLOCKS];
f82a20dda465 Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 43517
diff changeset
57
33635
836e92cc5c4f Definition consistency.
Tim Peters <tim.peters@gmail.com>
parents: 33634
diff changeset
58 static block *
48059
b68704457efe Finish-up the partial conversion from int to Py_ssize_t for deque indices and length.
Raymond Hettinger <python@rcn.com>
parents: 47961
diff changeset
59 newblock(block *leftlink, block *rightlink, Py_ssize_t len) {
33677
262e1cf41d34 Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
60 block *b;
48059
b68704457efe Finish-up the partial conversion from int to Py_ssize_t for deque indices and length.
Raymond Hettinger <python@rcn.com>
parents: 47961
diff changeset
61 /* To prevent len from overflowing PY_SSIZE_T_MAX on 64-bit machines, we
33677
262e1cf41d34 Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
62 * 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
63 * 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
64 * 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
65 * the blocks allocated to the deque, in the worst case, can only
48059
b68704457efe Finish-up the partial conversion from int to Py_ssize_t for deque indices and length.
Raymond Hettinger <python@rcn.com>
parents: 47961
diff changeset
66 * have PY_SSIZE_T_MAX-2 entries in total.
33677
262e1cf41d34 Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
67 */
48059
b68704457efe Finish-up the partial conversion from int to Py_ssize_t for deque indices and length.
Raymond Hettinger <python@rcn.com>
parents: 47961
diff changeset
68 if (len >= PY_SSIZE_T_MAX - 2*BLOCKLEN) {
33677
262e1cf41d34 Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
69 PyErr_SetString(PyExc_OverflowError,
262e1cf41d34 Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
70 "cannot add more blocks to the deque");
262e1cf41d34 Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
71 return NULL;
262e1cf41d34 Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
72 }
43812
f82a20dda465 Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 43517
diff changeset
73 if (numfreeblocks) {
f82a20dda465 Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 43517
diff changeset
74 numfreeblocks -= 1;
f82a20dda465 Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 43517
diff changeset
75 b = freeblocks[numfreeblocks];
f82a20dda465 Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 43517
diff changeset
76 } else {
f82a20dda465 Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 43517
diff changeset
77 b = PyMem_Malloc(sizeof(block));
f82a20dda465 Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 43517
diff changeset
78 if (b == NULL) {
f82a20dda465 Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 43517
diff changeset
79 PyErr_NoMemory();
f82a20dda465 Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 43517
diff changeset
80 return NULL;
f82a20dda465 Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 43517
diff changeset
81 }
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
82 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
83 b->leftlink = leftlink;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
84 b->rightlink = rightlink;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
85 return b;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
86 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
87
47678
a35da94e05fc Make more symbols static.
Martin v. Löwis <martin@v.loewis.de>
parents: 47527
diff changeset
88 static void
43812
f82a20dda465 Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 43517
diff changeset
89 freeblock(block *b)
f82a20dda465 Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 43517
diff changeset
90 {
f82a20dda465 Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 43517
diff changeset
91 if (numfreeblocks < MAXFREEBLOCKS) {
f82a20dda465 Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 43517
diff changeset
92 freeblocks[numfreeblocks] = b;
f82a20dda465 Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 43517
diff changeset
93 numfreeblocks++;
f82a20dda465 Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 43517
diff changeset
94 } else {
f82a20dda465 Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 43517
diff changeset
95 PyMem_Free(b);
f82a20dda465 Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 43517
diff changeset
96 }
f82a20dda465 Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 43517
diff changeset
97 }
f82a20dda465 Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 43517
diff changeset
98
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
99 typedef struct {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
100 PyObject_HEAD
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
101 block *leftblock;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
102 block *rightblock;
48059
b68704457efe Finish-up the partial conversion from int to Py_ssize_t for deque indices and length.
Raymond Hettinger <python@rcn.com>
parents: 47961
diff changeset
103 Py_ssize_t leftindex; /* in range(BLOCKLEN) */
b68704457efe Finish-up the partial conversion from int to Py_ssize_t for deque indices and length.
Raymond Hettinger <python@rcn.com>
parents: 47961
diff changeset
104 Py_ssize_t rightindex; /* in range(BLOCKLEN) */
b68704457efe Finish-up the partial conversion from int to Py_ssize_t for deque indices and length.
Raymond Hettinger <python@rcn.com>
parents: 47961
diff changeset
105 Py_ssize_t len;
b68704457efe Finish-up the partial conversion from int to Py_ssize_t for deque indices and length.
Raymond Hettinger <python@rcn.com>
parents: 47961
diff changeset
106 Py_ssize_t maxlen;
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
107 long state; /* incremented whenever the indices move */
32158
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 32119
diff changeset
108 PyObject *weakreflist; /* List of weak references */
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
109 } dequeobject;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
110
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
111 /* The deque's size limit is d.maxlen. The limit can be zero or positive.
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
112 * If there is no limit, then d.maxlen == -1.
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
113 *
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
114 * After an item is added to a deque, we check to see if the size has grown past
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
115 * the limit. If it has, we get the size back down to the limit by popping an
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
116 * item off of the opposite end. The methods that can trigger this are append(),
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
117 * appendleft(), extend(), and extendleft().
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
118 */
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
119
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
120 #define TRIM(d, popfunction) \
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
121 if (d->maxlen != -1 && d->len > d->maxlen) { \
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
122 PyObject *rv = popfunction(d, NULL); \
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
123 assert(rv != NULL && d->len <= d->maxlen); \
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
124 Py_DECREF(rv); \
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
125 }
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
126
31605
2fed331de014 Make deque_type static so namespace is not polluted.
Neal Norwitz <nnorwitz@gmail.com>
parents: 31603
diff changeset
127 static PyTypeObject deque_type;
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
128
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
129 static PyObject *
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
130 deque_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
131 {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
132 dequeobject *deque;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
133 block *b;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
134
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
135 /* create dequeobject structure */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
136 deque = (dequeobject *)type->tp_alloc(type, 0);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
137 if (deque == NULL)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
138 return NULL;
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
139
33677
262e1cf41d34 Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
140 b = newblock(NULL, NULL, 0);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
141 if (b == NULL) {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
142 Py_DECREF(deque);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
143 return NULL;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
144 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
145
33641
cd70f1ac3c25 * Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents: 33639
diff changeset
146 assert(BLOCKLEN >= 2);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
147 deque->leftblock = b;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
148 deque->rightblock = b;
33641
cd70f1ac3c25 * Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents: 33639
diff changeset
149 deque->leftindex = CENTER + 1;
cd70f1ac3c25 * Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents: 33639
diff changeset
150 deque->rightindex = CENTER;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
151 deque->len = 0;
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
152 deque->state = 0;
32158
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 32119
diff changeset
153 deque->weakreflist = NULL;
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
154 deque->maxlen = -1;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
155
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
156 return (PyObject *)deque;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
157 }
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 static PyObject *
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
160 deque_pop(dequeobject *deque, PyObject *unused)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
161 {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
162 PyObject *item;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
163 block *prevblock;
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 if (deque->len == 0) {
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
166 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
167 return NULL;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
168 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
169 item = deque->rightblock->data[deque->rightindex];
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
170 deque->rightindex--;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
171 deque->len--;
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
172 deque->state++;
31436
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->rightindex == -1) {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
175 if (deque->len == 0) {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
176 assert(deque->leftblock == deque->rightblock);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
177 assert(deque->leftindex == deque->rightindex+1);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
178 /* 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
179 deque->leftindex = CENTER + 1;
cd70f1ac3c25 * Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents: 33639
diff changeset
180 deque->rightindex = CENTER;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
181 } else {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
182 prevblock = deque->rightblock->leftlink;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
183 assert(deque->leftblock != deque->rightblock);
43812
f82a20dda465 Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 43517
diff changeset
184 freeblock(deque->rightblock);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
185 prevblock->rightlink = NULL;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
186 deque->rightblock = prevblock;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
187 deque->rightindex = BLOCKLEN - 1;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
188 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
189 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
190 return item;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
191 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
192
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
193 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
194
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
195 static PyObject *
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
196 deque_popleft(dequeobject *deque, PyObject *unused)
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 PyObject *item;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
199 block *prevblock;
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 if (deque->len == 0) {
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
202 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
203 return NULL;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
204 }
39475
249a59acb6ef Really address the issue of where to place the assert for leftblock.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39451
diff changeset
205 assert(deque->leftblock != NULL);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
206 item = deque->leftblock->data[deque->leftindex];
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
207 deque->leftindex++;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
208 deque->len--;
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
209 deque->state++;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
210
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
211 if (deque->leftindex == BLOCKLEN) {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
212 if (deque->len == 0) {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
213 assert(deque->leftblock == deque->rightblock);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
214 assert(deque->leftindex == deque->rightindex+1);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
215 /* 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
216 deque->leftindex = CENTER + 1;
cd70f1ac3c25 * Elaborate on the invariant comments and make them more precise.
Raymond Hettinger <python@rcn.com>
parents: 33639
diff changeset
217 deque->rightindex = CENTER;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
218 } else {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
219 assert(deque->leftblock != deque->rightblock);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
220 prevblock = deque->leftblock->rightlink;
43812
f82a20dda465 Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 43517
diff changeset
221 freeblock(deque->leftblock);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
222 assert(prevblock != NULL);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
223 prevblock->leftlink = NULL;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
224 deque->leftblock = prevblock;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
225 deque->leftindex = 0;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
226 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
227 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
228 return item;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
229 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
230
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
231 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
232
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
233 static PyObject *
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
234 deque_append(dequeobject *deque, PyObject *item)
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
235 {
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
236 deque->state++;
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
237 if (deque->rightindex == BLOCKLEN-1) {
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
238 block *b = newblock(deque->rightblock, NULL, deque->len);
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
239 if (b == NULL)
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
240 return NULL;
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
241 assert(deque->rightblock->rightlink == NULL);
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
242 deque->rightblock->rightlink = b;
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
243 deque->rightblock = b;
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
244 deque->rightindex = -1;
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
245 }
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
246 Py_INCREF(item);
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
247 deque->len++;
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
248 deque->rightindex++;
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
249 deque->rightblock->data[deque->rightindex] = item;
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
250 TRIM(deque, deque_popleft);
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
251 Py_RETURN_NONE;
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
252 }
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
253
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
254 PyDoc_STRVAR(append_doc, "Add an element to the right side of the deque.");
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
255
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
256 static PyObject *
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
257 deque_appendleft(dequeobject *deque, PyObject *item)
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
258 {
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
259 deque->state++;
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
260 if (deque->leftindex == 0) {
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
261 block *b = newblock(NULL, deque->leftblock, deque->len);
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
262 if (b == NULL)
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
263 return NULL;
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
264 assert(deque->leftblock->leftlink == NULL);
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
265 deque->leftblock->leftlink = b;
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
266 deque->leftblock = b;
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
267 deque->leftindex = BLOCKLEN;
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
268 }
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
269 Py_INCREF(item);
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
270 deque->len++;
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
271 deque->leftindex--;
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
272 deque->leftblock->data[deque->leftindex] = item;
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
273 TRIM(deque, deque_pop);
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
274 Py_RETURN_NONE;
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
275 }
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
276
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
277 PyDoc_STRVAR(appendleft_doc, "Add an element to the left side of the deque.");
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
278
52117
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
279
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
280 /* Run an iterator to exhaustion. Shortcut for
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
281 the extend/extendleft methods when maxlen == 0. */
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
282 static PyObject*
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
283 consume_iterator(PyObject *it)
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
284 {
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
285 PyObject *item;
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
286
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
287 while ((item = PyIter_Next(it)) != NULL) {
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
288 Py_DECREF(item);
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
289 }
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
290 Py_DECREF(it);
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
291 if (PyErr_Occurred())
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
292 return NULL;
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
293 Py_RETURN_NONE;
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
294 }
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
295
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
296 static PyObject *
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
297 deque_extend(dequeobject *deque, PyObject *iterable)
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
298 {
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
299 PyObject *it, *item;
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
300
57634
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
301 /* Handle case where id(deque) == id(iterable) */
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
302 if ((PyObject *)deque == iterable) {
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
303 PyObject *result;
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
304 PyObject *s = PySequence_List(iterable);
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
305 if (s == NULL)
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
306 return NULL;
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
307 result = deque_extend(deque, s);
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
308 Py_DECREF(s);
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
309 return result;
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
310 }
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
311
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
312 it = PyObject_GetIter(iterable);
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
313 if (it == NULL)
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
314 return NULL;
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
315
52117
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
316 if (deque->maxlen == 0)
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
317 return consume_iterator(it);
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
318
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
319 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
320 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
321 if (deque->rightindex == BLOCKLEN-1) {
33677
262e1cf41d34 Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
322 block *b = newblock(deque->rightblock, NULL,
262e1cf41d34 Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
323 deque->len);
31466
f04ccc288542 * Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents: 31464
diff changeset
324 if (b == NULL) {
f04ccc288542 * Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents: 31464
diff changeset
325 Py_DECREF(item);
f04ccc288542 * Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents: 31464
diff changeset
326 Py_DECREF(it);
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
327 return NULL;
31466
f04ccc288542 * Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents: 31464
diff changeset
328 }
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
329 assert(deque->rightblock->rightlink == NULL);
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
330 deque->rightblock->rightlink = b;
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
331 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
332 deque->rightindex = -1;
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
333 }
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
334 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
335 deque->rightindex++;
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
336 deque->rightblock->data[deque->rightindex] = item;
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
337 TRIM(deque, deque_popleft);
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
338 }
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
339 Py_DECREF(it);
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
340 if (PyErr_Occurred())
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
341 return NULL;
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
342 Py_RETURN_NONE;
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
343 }
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
344
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
345 PyDoc_STRVAR(extend_doc,
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
346 "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
347
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
348 static PyObject *
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
349 deque_extendleft(dequeobject *deque, PyObject *iterable)
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
350 {
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
351 PyObject *it, *item;
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
352
57634
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
353 /* Handle case where id(deque) == id(iterable) */
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
354 if ((PyObject *)deque == iterable) {
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
355 PyObject *result;
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
356 PyObject *s = PySequence_List(iterable);
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
357 if (s == NULL)
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
358 return NULL;
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
359 result = deque_extendleft(deque, s);
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
360 Py_DECREF(s);
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
361 return result;
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
362 }
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
363
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
364 it = PyObject_GetIter(iterable);
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
365 if (it == NULL)
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
366 return NULL;
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
367
52117
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
368 if (deque->maxlen == 0)
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
369 return consume_iterator(it);
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
370
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
371 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
372 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
373 if (deque->leftindex == 0) {
33677
262e1cf41d34 Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
374 block *b = newblock(NULL, deque->leftblock,
262e1cf41d34 Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
375 deque->len);
31466
f04ccc288542 * Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents: 31464
diff changeset
376 if (b == NULL) {
f04ccc288542 * Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents: 31464
diff changeset
377 Py_DECREF(item);
f04ccc288542 * Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents: 31464
diff changeset
378 Py_DECREF(it);
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
379 return NULL;
31466
f04ccc288542 * Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents: 31464
diff changeset
380 }
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
381 assert(deque->leftblock->leftlink == NULL);
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
382 deque->leftblock->leftlink = b;
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
383 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
384 deque->leftindex = BLOCKLEN;
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
385 }
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
386 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
387 deque->leftindex--;
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
388 deque->leftblock->data[deque->leftindex] = item;
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
389 TRIM(deque, deque_pop);
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
390 }
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
391 Py_DECREF(it);
32554
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32413
diff changeset
392 if (PyErr_Occurred())
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
393 return NULL;
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
394 Py_RETURN_NONE;
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
395 }
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
396
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
397 PyDoc_STRVAR(extendleft_doc,
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
398 "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
399
57634
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
400 static PyObject *
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
401 deque_inplace_concat(dequeobject *deque, PyObject *other)
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
402 {
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
403 PyObject *result;
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
404
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
405 result = deque_extend(deque, other);
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
406 if (result == NULL)
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
407 return result;
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
408 Py_DECREF(result);
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
409 Py_INCREF(deque);
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
410 return (PyObject *)deque;
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
411 }
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
412
33697
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
413 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
414 _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
415 {
48059
b68704457efe Finish-up the partial conversion from int to Py_ssize_t for deque indices and length.
Raymond Hettinger <python@rcn.com>
parents: 47961
diff changeset
416 Py_ssize_t 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
417 PyObject *item, *rv;
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
418
31476
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
419 if (len == 0)
33697
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
420 return 0;
31476
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
421 if (n > halflen || n < -halflen) {
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
422 n %= len;
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
423 if (n > halflen)
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
424 n -= len;
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
425 else if (n < -halflen)
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
426 n += len;
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
427 }
31474
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
428
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
429 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
430 item = deque_pop(deque, NULL);
32554
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32413
diff changeset
431 assert (item != NULL);
31474
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
432 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
433 Py_DECREF(item);
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
434 if (rv == NULL)
33697
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
435 return -1;
31474
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
436 Py_DECREF(rv);
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
437 }
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
438 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
439 item = deque_popleft(deque, NULL);
32554
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32413
diff changeset
440 assert (item != NULL);
31474
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
441 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
442 Py_DECREF(item);
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
443 if (rv == NULL)
33697
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
444 return -1;
31474
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
445 Py_DECREF(rv);
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
446 }
33697
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
447 return 0;
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
448 }
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
449
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
450 static PyObject *
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
451 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
452 {
48059
b68704457efe Finish-up the partial conversion from int to Py_ssize_t for deque indices and length.
Raymond Hettinger <python@rcn.com>
parents: 47961
diff changeset
453 Py_ssize_t n=1;
33697
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
454
48060
759a7323591e Parse to the correct datatype.
Raymond Hettinger <python@rcn.com>
parents: 48059
diff changeset
455 if (!PyArg_ParseTuple(args, "|n:rotate", &n))
33697
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
456 return NULL;
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
457 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
458 Py_RETURN_NONE;
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
459 return NULL;
31474
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
460 }
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
461
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
462 PyDoc_STRVAR(rotate_doc,
31476
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
463 "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
464
57635
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
465 static PyObject *
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
466 deque_reverse(dequeobject *deque, PyObject *unused)
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
467 {
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
468 block *leftblock = deque->leftblock;
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
469 block *rightblock = deque->rightblock;
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
470 Py_ssize_t leftindex = deque->leftindex;
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
471 Py_ssize_t rightindex = deque->rightindex;
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
472 Py_ssize_t n = (deque->len)/2;
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
473 Py_ssize_t i;
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
474 PyObject *tmp;
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
475
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
476 for (i=0 ; i<n ; i++) {
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
477 /* Validate that pointers haven't met in the middle */
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
478 assert(leftblock != rightblock || leftindex < rightindex);
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
479
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
480 /* Swap */
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
481 tmp = leftblock->data[leftindex];
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
482 leftblock->data[leftindex] = rightblock->data[rightindex];
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
483 rightblock->data[rightindex] = tmp;
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
484
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
485 /* Advance left block/index pair */
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
486 leftindex++;
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
487 if (leftindex == BLOCKLEN) {
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
488 assert (leftblock->rightlink != NULL);
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
489 leftblock = leftblock->rightlink;
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
490 leftindex = 0;
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
491 }
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
492
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
493 /* Step backwards with the right block/index pair */
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
494 rightindex--;
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
495 if (rightindex == -1) {
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
496 assert (rightblock->leftlink != NULL);
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
497 rightblock = rightblock->leftlink;
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
498 rightindex = BLOCKLEN - 1;
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
499 }
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
500 }
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
501 Py_RETURN_NONE;
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
502 }
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
503
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
504 PyDoc_STRVAR(reverse_doc,
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
505 "D.reverse() -- reverse *IN PLACE*");
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
506
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
507 static Py_ssize_t
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
508 deque_len(dequeobject *deque)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
509 {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
510 return deque->len;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
511 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
512
34771
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
513 static PyObject *
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
514 deque_remove(dequeobject *deque, PyObject *value)
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
515 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
516 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
517
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
518 for (i=0 ; i<n ; i++) {
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
519 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
520 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
521
2ee575b9b60e Apply remove's mutation test after every equality test.
Raymond Hettinger <python@rcn.com>
parents: 34771
diff changeset
522 if (deque->len != n) {
39232
cd8f20d400c1 defdict_reduce(): Plug leaks.
Tim Peters <tim.peters@gmail.com>
parents: 37748
diff changeset
523 PyErr_SetString(PyExc_IndexError,
34772
2ee575b9b60e Apply remove's mutation test after every equality test.
Raymond Hettinger <python@rcn.com>
parents: 34771
diff changeset
524 "deque mutated during remove().");
2ee575b9b60e Apply remove's mutation test after every equality test.
Raymond Hettinger <python@rcn.com>
parents: 34771
diff changeset
525 return NULL;
2ee575b9b60e Apply remove's mutation test after every equality test.
Raymond Hettinger <python@rcn.com>
parents: 34771
diff changeset
526 }
34771
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
527 if (cmp > 0) {
34772
2ee575b9b60e Apply remove's mutation test after every equality test.
Raymond Hettinger <python@rcn.com>
parents: 34771
diff changeset
528 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
529 assert (tgt != NULL);
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
530 Py_DECREF(tgt);
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
531 if (_deque_rotate(deque, i) == -1)
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
532 return NULL;
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
533 Py_RETURN_NONE;
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
534 }
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
535 else if (cmp < 0) {
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
536 _deque_rotate(deque, i);
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
537 return NULL;
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
538 }
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
539 _deque_rotate(deque, -1);
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
540 }
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
541 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
542 return NULL;
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
543 }
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
544
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
545 PyDoc_STRVAR(remove_doc,
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
546 "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
547
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
548 static int
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
549 deque_clear(dequeobject *deque)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
550 {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
551 PyObject *item;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
552
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
553 while (deque->len) {
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
554 item = deque_pop(deque, NULL);
32554
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32413
diff changeset
555 assert (item != NULL);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
556 Py_DECREF(item);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
557 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
558 assert(deque->leftblock == deque->rightblock &&
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
559 deque->leftindex - 1 == deque->rightindex &&
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
560 deque->len == 0);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
561 return 0;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
562 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
563
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
564 static PyObject *
48059
b68704457efe Finish-up the partial conversion from int to Py_ssize_t for deque indices and length.
Raymond Hettinger <python@rcn.com>
parents: 47961
diff changeset
565 deque_item(dequeobject *deque, Py_ssize_t i)
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
566 {
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
567 block *b;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
568 PyObject *item;
48059
b68704457efe Finish-up the partial conversion from int to Py_ssize_t for deque indices and length.
Raymond Hettinger <python@rcn.com>
parents: 47961
diff changeset
569 Py_ssize_t n, index=i;
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
570
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
571 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
572 PyErr_SetString(PyExc_IndexError,
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
573 "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
574 return NULL;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
575 }
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
576
31615
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
577 if (i == 0) {
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
578 i = deque->leftindex;
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
579 b = deque->leftblock;
31615
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
580 } else if (i == deque->len - 1) {
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
581 i = deque->rightindex;
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
582 b = deque->rightblock;
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
583 } else {
31615
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
584 i += deque->leftindex;
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
585 n = i / BLOCKLEN;
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
586 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
587 if (index < (deque->len >> 1)) {
31615
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
588 b = deque->leftblock;
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
589 while (n--)
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
590 b = b->rightlink;
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
591 } else {
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
592 n = (deque->leftindex + deque->len - 1) / BLOCKLEN - n;
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
593 b = deque->rightblock;
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
594 while (n--)
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
595 b = b->leftlink;
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
596 }
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
597 }
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
598 item = b->data[i];
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
599 Py_INCREF(item);
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
600 return item;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
601 }
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
602
32413
d7110bb6c13d Add a comment with implementation notes.
Raymond Hettinger <python@rcn.com>
parents: 32363
diff changeset
603 /* 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
604 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
605 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
606 (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
607 performance boost.
d7110bb6c13d Add a comment with implementation notes.
Raymond Hettinger <python@rcn.com>
parents: 32363
diff changeset
608 */
d7110bb6c13d Add a comment with implementation notes.
Raymond Hettinger <python@rcn.com>
parents: 32363
diff changeset
609
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
610 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
611 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
612 {
33697
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
613 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
614
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
615 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
616 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
617 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
618
245835ed8de8 Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents: 31908
diff changeset
619 item = deque_popleft(deque, NULL);
32554
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32413
diff changeset
620 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
621 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
622
33697
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
623 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
624 }
245835ed8de8 Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents: 31908
diff changeset
625
245835ed8de8 Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents: 31908
diff changeset
626 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
627 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
628 {
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
629 PyObject *old_value;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
630 block *b;
36518
9f3f87e920a6 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
631 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
632
32554
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32413
diff changeset
633 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
634 PyErr_SetString(PyExc_IndexError,
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
635 "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
636 return -1;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
637 }
32084
245835ed8de8 Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents: 31908
diff changeset
638 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
639 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
640
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
641 i += deque->leftindex;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
642 n = i / BLOCKLEN;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
643 i %= BLOCKLEN;
32554
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32413
diff changeset
644 if (index <= halflen) {
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
645 b = deque->leftblock;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
646 while (n--)
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
647 b = b->rightlink;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
648 } else {
32554
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32413
diff changeset
649 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
650 b = deque->rightblock;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
651 while (n--)
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
652 b = b->leftlink;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
653 }
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
654 Py_INCREF(v);
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
655 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
656 b->data[i] = v;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
657 Py_DECREF(old_value);
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
658 return 0;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
659 }
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
660
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
661 static PyObject *
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
662 deque_clearmethod(dequeobject *deque)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
663 {
32554
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32413
diff changeset
664 int rv;
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32413
diff changeset
665
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32413
diff changeset
666 rv = deque_clear(deque);
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32413
diff changeset
667 assert (rv != -1);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
668 Py_RETURN_NONE;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
669 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
670
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
671 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
672
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
673 static void
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
674 deque_dealloc(dequeobject *deque)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
675 {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
676 PyObject_GC_UnTrack(deque);
32158
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 32119
diff changeset
677 if (deque->weakreflist != NULL)
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 32119
diff changeset
678 PyObject_ClearWeakRefs((PyObject *) deque);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
679 if (deque->leftblock != NULL) {
32752
dbd3a04eb0c6 Silence a GCC unused variable warning in debug builds.
Raymond Hettinger <python@rcn.com>
parents: 32701
diff changeset
680 deque_clear(deque);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
681 assert(deque->leftblock != NULL);
43812
f82a20dda465 Use a freelist to speed-up block allocation and deallocation in collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 43517
diff changeset
682 freeblock(deque->leftblock);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
683 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
684 deque->leftblock = NULL;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
685 deque->rightblock = NULL;
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 44281
diff changeset
686 Py_TYPE(deque)->tp_free(deque);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
687 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
688
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
689 static int
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
690 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
691 {
33639
40350fbe665d deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents: 33638
diff changeset
692 block *b;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
693 PyObject *item;
48059
b68704457efe Finish-up the partial conversion from int to Py_ssize_t for deque indices and length.
Raymond Hettinger <python@rcn.com>
parents: 47961
diff changeset
694 Py_ssize_t index;
b68704457efe Finish-up the partial conversion from int to Py_ssize_t for deque indices and length.
Raymond Hettinger <python@rcn.com>
parents: 47961
diff changeset
695 Py_ssize_t indexlo = deque->leftindex;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
696
33639
40350fbe665d deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents: 33638
diff changeset
697 for (b = deque->leftblock; b != NULL; b = b->rightlink) {
48059
b68704457efe Finish-up the partial conversion from int to Py_ssize_t for deque indices and length.
Raymond Hettinger <python@rcn.com>
parents: 47961
diff changeset
698 const Py_ssize_t indexhi = b == deque->rightblock ?
33639
40350fbe665d deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents: 33638
diff changeset
699 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
700 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
701
40350fbe665d deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents: 33638
diff changeset
702 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
703 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
704 Py_VISIT(item);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
705 }
33639
40350fbe665d deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents: 33638
diff changeset
706 indexlo = 0;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
707 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
708 return 0;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
709 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
710
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
711 static PyObject *
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
712 deque_copy(PyObject *deque)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
713 {
43517
da52b4c776a4 Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents: 43475
diff changeset
714 if (((dequeobject *)deque)->maxlen == -1)
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 44281
diff changeset
715 return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "O", deque, NULL);
43517
da52b4c776a4 Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents: 43475
diff changeset
716 else
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 44281
diff changeset
717 return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)), "Oi",
43517
da52b4c776a4 Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents: 43475
diff changeset
718 deque, ((dequeobject *)deque)->maxlen, NULL);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
719 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
720
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
721 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
722
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
723 static PyObject *
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
724 deque_reduce(dequeobject *deque)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
725 {
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
726 PyObject *dict, *result, *aslist;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
727
33949
b9819b9a0dad SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33879
diff changeset
728 dict = PyObject_GetAttrString((PyObject *)deque, "__dict__");
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
729 if (dict == NULL)
33949
b9819b9a0dad SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33879
diff changeset
730 PyErr_Clear();
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
731 aslist = PySequence_List((PyObject *)deque);
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
732 if (aslist == NULL) {
43469
f2c9f24a0f5a dict could be NULL, so we need to XDECREF.
Neal Norwitz <nnorwitz@gmail.com>
parents: 43466
diff changeset
733 Py_XDECREF(dict);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
734 return NULL;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
735 }
43517
da52b4c776a4 Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents: 43475
diff changeset
736 if (dict == NULL) {
da52b4c776a4 Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents: 43475
diff changeset
737 if (deque->maxlen == -1)
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 44281
diff changeset
738 result = Py_BuildValue("O(O)", Py_TYPE(deque), aslist);
43517
da52b4c776a4 Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents: 43475
diff changeset
739 else
48059
b68704457efe Finish-up the partial conversion from int to Py_ssize_t for deque indices and length.
Raymond Hettinger <python@rcn.com>
parents: 47961
diff changeset
740 result = Py_BuildValue("O(On)", Py_TYPE(deque), aslist, deque->maxlen);
43517
da52b4c776a4 Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents: 43475
diff changeset
741 } else {
da52b4c776a4 Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents: 43475
diff changeset
742 if (deque->maxlen == -1)
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 44281
diff changeset
743 result = Py_BuildValue("O(OO)O", Py_TYPE(deque), aslist, Py_None, dict);
43517
da52b4c776a4 Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents: 43475
diff changeset
744 else
48059
b68704457efe Finish-up the partial conversion from int to Py_ssize_t for deque indices and length.
Raymond Hettinger <python@rcn.com>
parents: 47961
diff changeset
745 result = Py_BuildValue("O(On)O", Py_TYPE(deque), aslist, deque->maxlen, dict);
43517
da52b4c776a4 Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents: 43475
diff changeset
746 }
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
747 Py_XDECREF(dict);
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
748 Py_DECREF(aslist);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
749 return result;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
750 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
751
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
752 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
753
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
754 static PyObject *
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
755 deque_repr(PyObject *deque)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
756 {
43475
17b07370a782 Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents: 43469
diff changeset
757 PyObject *aslist, *result, *fmt;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
758 int i;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
759
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
760 i = Py_ReprEnter(deque);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
761 if (i != 0) {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
762 if (i < 0)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
763 return NULL;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
764 return PyString_FromString("[...]");
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
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
767 aslist = PySequence_List(deque);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
768 if (aslist == NULL) {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
769 Py_ReprLeave(deque);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
770 return NULL;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
771 }
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
772 if (((dequeobject *)deque)->maxlen != -1)
49138
145449109a31 #3743: PY_FORMAT_SIZE_T is designed for the OS "printf" functions, not for
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48832
diff changeset
773 fmt = PyString_FromFormat("deque(%%r, maxlen=%zd)",
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
774 ((dequeobject *)deque)->maxlen);
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
775 else
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
776 fmt = PyString_FromString("deque(%r)");
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
777 if (fmt == NULL) {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
778 Py_DECREF(aslist);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
779 Py_ReprLeave(deque);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
780 return NULL;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
781 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
782 result = PyString_Format(fmt, aslist);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
783 Py_DECREF(fmt);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
784 Py_DECREF(aslist);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
785 Py_ReprLeave(deque);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
786 return result;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
787 }
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 int
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
790 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
791 {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
792 PyObject *it, *item;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
793 char *emit = ""; /* No separator emitted on first pass */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
794 char *separator = ", ";
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
795 int i;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
796
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
797 i = Py_ReprEnter(deque);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
798 if (i != 0) {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
799 if (i < 0)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
800 return i;
43475
17b07370a782 Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents: 43469
diff changeset
801 Py_BEGIN_ALLOW_THREADS
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
802 fputs("[...]", fp);
43475
17b07370a782 Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents: 43469
diff changeset
803 Py_END_ALLOW_THREADS
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
804 return 0;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
805 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
806
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
807 it = PyObject_GetIter(deque);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
808 if (it == NULL)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
809 return -1;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
810
43475
17b07370a782 Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents: 43469
diff changeset
811 Py_BEGIN_ALLOW_THREADS
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
812 fputs("deque([", fp);
43475
17b07370a782 Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents: 43469
diff changeset
813 Py_END_ALLOW_THREADS
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
814 while ((item = PyIter_Next(it)) != NULL) {
43475
17b07370a782 Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents: 43469
diff changeset
815 Py_BEGIN_ALLOW_THREADS
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
816 fputs(emit, fp);
43475
17b07370a782 Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents: 43469
diff changeset
817 Py_END_ALLOW_THREADS
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
818 emit = separator;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
819 if (PyObject_Print(item, fp, 0) != 0) {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
820 Py_DECREF(item);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
821 Py_DECREF(it);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
822 Py_ReprLeave(deque);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
823 return -1;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
824 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
825 Py_DECREF(item);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
826 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
827 Py_ReprLeave(deque);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
828 Py_DECREF(it);
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
829 if (PyErr_Occurred())
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
830 return -1;
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
831
43475
17b07370a782 Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents: 43469
diff changeset
832 Py_BEGIN_ALLOW_THREADS
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
833 if (((dequeobject *)deque)->maxlen == -1)
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
834 fputs("])", fp);
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
835 else
48829
74899e6c87a5 Fixed two format strings in the _collections module. For example
Christian Heimes <christian@cheimes.de>
parents: 48060
diff changeset
836 fprintf(fp, "], maxlen=%" PY_FORMAT_SIZE_T "d)", ((dequeobject *)deque)->maxlen);
43475
17b07370a782 Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents: 43469
diff changeset
837 Py_END_ALLOW_THREADS
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
838 return 0;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
839 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
840
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
841 static PyObject *
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
842 deque_richcompare(PyObject *v, PyObject *w, int op)
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
843 {
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
844 PyObject *it1=NULL, *it2=NULL, *x, *y;
48059
b68704457efe Finish-up the partial conversion from int to Py_ssize_t for deque indices and length.
Raymond Hettinger <python@rcn.com>
parents: 47961
diff changeset
845 Py_ssize_t vs, ws;
b68704457efe Finish-up the partial conversion from int to Py_ssize_t for deque indices and length.
Raymond Hettinger <python@rcn.com>
parents: 47961
diff changeset
846 int b, cmp=-1;
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
847
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
848 if (!PyObject_TypeCheck(v, &deque_type) ||
32119
dd1e55deaf52 Make type check work with subclasses
Raymond Hettinger <python@rcn.com>
parents: 32084
diff changeset
849 !PyObject_TypeCheck(w, &deque_type)) {
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
850 Py_INCREF(Py_NotImplemented);
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
851 return Py_NotImplemented;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
852 }
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
853
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
854 /* Shortcuts */
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
855 vs = ((dequeobject *)v)->len;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
856 ws = ((dequeobject *)w)->len;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
857 if (op == Py_EQ) {
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
858 if (v == w)
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
859 Py_RETURN_TRUE;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
860 if (vs != ws)
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
861 Py_RETURN_FALSE;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
862 }
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
863 if (op == Py_NE) {
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
864 if (v == w)
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
865 Py_RETURN_FALSE;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
866 if (vs != ws)
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
867 Py_RETURN_TRUE;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
868 }
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
869
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
870 /* Search for the first index where items are different */
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
871 it1 = PyObject_GetIter(v);
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
872 if (it1 == NULL)
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
873 goto done;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
874 it2 = PyObject_GetIter(w);
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
875 if (it2 == NULL)
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
876 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
877 for (;;) {
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
878 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
879 if (x == NULL && PyErr_Occurred())
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
880 goto done;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
881 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
882 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
883 break;
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
884 b = PyObject_RichCompareBool(x, y, Py_EQ);
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
885 if (b == 0) {
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
886 cmp = PyObject_RichCompareBool(x, y, op);
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
887 Py_DECREF(x);
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
888 Py_DECREF(y);
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
889 goto done;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
890 }
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
891 Py_DECREF(x);
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
892 Py_DECREF(y);
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
893 if (b == -1)
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
894 goto done;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
895 }
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
896 /* 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
897 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
898 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
899 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
900 goto done;
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
901 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
902 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
903 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
904 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
905 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
906 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
907 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
908 }
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
909
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
910 done:
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
911 Py_XDECREF(it1);
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
912 Py_XDECREF(it2);
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
913 if (cmp == 1)
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
914 Py_RETURN_TRUE;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
915 if (cmp == 0)
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
916 Py_RETURN_FALSE;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
917 return NULL;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
918 }
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
919
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
920 static int
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
921 deque_init(dequeobject *deque, PyObject *args, PyObject *kwdargs)
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
922 {
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
923 PyObject *iterable = NULL;
43517
da52b4c776a4 Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents: 43475
diff changeset
924 PyObject *maxlenobj = NULL;
48059
b68704457efe Finish-up the partial conversion from int to Py_ssize_t for deque indices and length.
Raymond Hettinger <python@rcn.com>
parents: 47961
diff changeset
925 Py_ssize_t maxlen = -1;
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
926 char *kwlist[] = {"iterable", "maxlen", 0};
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
927
43517
da52b4c776a4 Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents: 43475
diff changeset
928 if (!PyArg_ParseTupleAndKeywords(args, kwdargs, "|OO:deque", kwlist, &iterable, &maxlenobj))
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
929 return -1;
43517
da52b4c776a4 Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents: 43475
diff changeset
930 if (maxlenobj != NULL && maxlenobj != Py_None) {
48059
b68704457efe Finish-up the partial conversion from int to Py_ssize_t for deque indices and length.
Raymond Hettinger <python@rcn.com>
parents: 47961
diff changeset
931 maxlen = PyInt_AsSsize_t(maxlenobj);
43517
da52b4c776a4 Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents: 43475
diff changeset
932 if (maxlen == -1 && PyErr_Occurred())
da52b4c776a4 Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents: 43475
diff changeset
933 return -1;
da52b4c776a4 Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents: 43475
diff changeset
934 if (maxlen < 0) {
da52b4c776a4 Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents: 43475
diff changeset
935 PyErr_SetString(PyExc_ValueError, "maxlen must be non-negative");
da52b4c776a4 Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents: 43475
diff changeset
936 return -1;
da52b4c776a4 Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents: 43475
diff changeset
937 }
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
938 }
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
939 deque->maxlen = maxlen;
44281
c2e72feb53b3 Fix bug 1604. deque.__init__() did not clear existing contents like list.__init__. Not a backport candidate.
Raymond Hettinger <python@rcn.com>
parents: 43812
diff changeset
940 deque_clear(deque);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
941 if (iterable != NULL) {
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
942 PyObject *rv = deque_extend(deque, iterable);
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
943 if (rv == NULL)
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
944 return -1;
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
945 Py_DECREF(rv);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
946 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
947 return 0;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
948 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
949
52119
b0911b5a002e For collections.deque() objects, expose the maxlen parameter as a read-only attribute.
Raymond Hettinger <python@rcn.com>
parents: 52117
diff changeset
950 static PyObject *
b0911b5a002e For collections.deque() objects, expose the maxlen parameter as a read-only attribute.
Raymond Hettinger <python@rcn.com>
parents: 52117
diff changeset
951 deque_get_maxlen(dequeobject *deque)
b0911b5a002e For collections.deque() objects, expose the maxlen parameter as a read-only attribute.
Raymond Hettinger <python@rcn.com>
parents: 52117
diff changeset
952 {
b0911b5a002e For collections.deque() objects, expose the maxlen parameter as a read-only attribute.
Raymond Hettinger <python@rcn.com>
parents: 52117
diff changeset
953 if (deque->maxlen == -1)
b0911b5a002e For collections.deque() objects, expose the maxlen parameter as a read-only attribute.
Raymond Hettinger <python@rcn.com>
parents: 52117
diff changeset
954 Py_RETURN_NONE;
b0911b5a002e For collections.deque() objects, expose the maxlen parameter as a read-only attribute.
Raymond Hettinger <python@rcn.com>
parents: 52117
diff changeset
955 return PyInt_FromSsize_t(deque->maxlen);
b0911b5a002e For collections.deque() objects, expose the maxlen parameter as a read-only attribute.
Raymond Hettinger <python@rcn.com>
parents: 52117
diff changeset
956 }
b0911b5a002e For collections.deque() objects, expose the maxlen parameter as a read-only attribute.
Raymond Hettinger <python@rcn.com>
parents: 52117
diff changeset
957
b0911b5a002e For collections.deque() objects, expose the maxlen parameter as a read-only attribute.
Raymond Hettinger <python@rcn.com>
parents: 52117
diff changeset
958 static PyGetSetDef deque_getset[] = {
b0911b5a002e For collections.deque() objects, expose the maxlen parameter as a read-only attribute.
Raymond Hettinger <python@rcn.com>
parents: 52117
diff changeset
959 {"maxlen", (getter)deque_get_maxlen, (setter)NULL,
b0911b5a002e For collections.deque() objects, expose the maxlen parameter as a read-only attribute.
Raymond Hettinger <python@rcn.com>
parents: 52117
diff changeset
960 "maximum size of a deque or None if unbounded"},
b0911b5a002e For collections.deque() objects, expose the maxlen parameter as a read-only attribute.
Raymond Hettinger <python@rcn.com>
parents: 52117
diff changeset
961 {0}
b0911b5a002e For collections.deque() objects, expose the maxlen parameter as a read-only attribute.
Raymond Hettinger <python@rcn.com>
parents: 52117
diff changeset
962 };
b0911b5a002e For collections.deque() objects, expose the maxlen parameter as a read-only attribute.
Raymond Hettinger <python@rcn.com>
parents: 52117
diff changeset
963
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
964 static PySequenceMethods deque_as_sequence = {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
965 (lenfunc)deque_len, /* sq_length */
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
966 0, /* sq_concat */
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
967 0, /* sq_repeat */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
968 (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
969 0, /* sq_slice */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
970 (ssizeobjargproc)deque_ass_item, /* sq_ass_item */
57634
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
971 0, /* sq_ass_slice */
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
972 0, /* sq_contains */
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
973 (binaryfunc)deque_inplace_concat, /* sq_inplace_concat */
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
974 0, /* sq_inplace_repeat */
57ae5d44e731 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 55535
diff changeset
975
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
976 };
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
977
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
978 /* deque object ********************************************************/
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
979
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
980 static PyObject *deque_iter(dequeobject *deque);
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
981 static PyObject *deque_reviter(dequeobject *deque);
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
982 PyDoc_STRVAR(reversed_doc,
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
983 "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
984
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
985 static PyMethodDef deque_methods[] = {
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
986 {"append", (PyCFunction)deque_append,
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
987 METH_O, append_doc},
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
988 {"appendleft", (PyCFunction)deque_appendleft,
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
989 METH_O, appendleft_doc},
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
990 {"clear", (PyCFunction)deque_clearmethod,
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
991 METH_NOARGS, clear_doc},
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
992 {"__copy__", (PyCFunction)deque_copy,
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
993 METH_NOARGS, copy_doc},
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
994 {"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
995 METH_O, extend_doc},
34771
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
996 {"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
997 METH_O, extendleft_doc},
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
998 {"pop", (PyCFunction)deque_pop,
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
999 METH_NOARGS, pop_doc},
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
1000 {"popleft", (PyCFunction)deque_popleft,
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1001 METH_NOARGS, popleft_doc},
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
1002 {"__reduce__", (PyCFunction)deque_reduce,
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1003 METH_NOARGS, reduce_doc},
34771
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
1004 {"remove", (PyCFunction)deque_remove,
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
1005 METH_O, remove_doc},
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
1006 {"__reversed__", (PyCFunction)deque_reviter,
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1007 METH_NOARGS, reversed_doc},
57635
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
1008 {"reverse", (PyCFunction)deque_reverse,
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
1009 METH_NOARGS, reverse_doc},
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
1010 {"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
1011 METH_VARARGS, rotate_doc},
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1012 {NULL, NULL} /* sentinel */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1013 };
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1014
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1015 PyDoc_STRVAR(deque_doc,
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
1016 "deque(iterable[, maxlen]) --> deque object\n\
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1017 \n\
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1018 Build an ordered collection accessible from endpoints only.");
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1019
31605
2fed331de014 Make deque_type static so namespace is not polluted.
Neal Norwitz <nnorwitz@gmail.com>
parents: 31603
diff changeset
1020 static PyTypeObject deque_type = {
42333
6620a510e9a9 PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents: 41019
diff changeset
1021 PyVarObject_HEAD_INIT(NULL, 0)
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1022 "collections.deque", /* tp_name */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1023 sizeof(dequeobject), /* tp_basicsize */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1024 0, /* tp_itemsize */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1025 /* methods */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1026 (destructor)deque_dealloc, /* tp_dealloc */
37305
5e1b6f553763 Remove unnecessary casts from type object initializers.
Georg Brandl <georg@python.org>
parents: 36656
diff changeset
1027 deque_tp_print, /* tp_print */
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1028 0, /* tp_getattr */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1029 0, /* tp_setattr */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1030 0, /* tp_compare */
37305
5e1b6f553763 Remove unnecessary casts from type object initializers.
Georg Brandl <georg@python.org>
parents: 36656
diff changeset
1031 deque_repr, /* tp_repr */
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1032 0, /* tp_as_number */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1033 &deque_as_sequence, /* tp_as_sequence */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1034 0, /* tp_as_mapping */
47961
f9b80a820a25 Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents: 47678
diff changeset
1035 (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1036 0, /* tp_call */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1037 0, /* tp_str */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1038 PyObject_GenericGetAttr, /* tp_getattro */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1039 0, /* tp_setattro */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1040 0, /* tp_as_buffer */
32158
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 32119
diff changeset
1041 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
1042 Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1043 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
1044 (traverseproc)deque_traverse, /* tp_traverse */
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1045 (inquiry)deque_clear, /* tp_clear */
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
1046 (richcmpfunc)deque_richcompare, /* tp_richcompare */
32158
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 32119
diff changeset
1047 offsetof(dequeobject, weakreflist), /* tp_weaklistoffset*/
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1048 (getiterfunc)deque_iter, /* tp_iter */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1049 0, /* tp_iternext */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1050 deque_methods, /* tp_methods */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1051 0, /* tp_members */
52119
b0911b5a002e For collections.deque() objects, expose the maxlen parameter as a read-only attribute.
Raymond Hettinger <python@rcn.com>
parents: 52117
diff changeset
1052 deque_getset, /* tp_getset */
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1053 0, /* tp_base */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1054 0, /* tp_dict */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1055 0, /* tp_descr_get */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1056 0, /* tp_descr_set */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1057 0, /* tp_dictoffset */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1058 (initproc)deque_init, /* tp_init */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1059 PyType_GenericAlloc, /* tp_alloc */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1060 deque_new, /* tp_new */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1061 PyObject_GC_Del, /* tp_free */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1062 };
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1063
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1064 /*********************** Deque Iterator **************************/
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1065
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1066 typedef struct {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1067 PyObject_HEAD
48059
b68704457efe Finish-up the partial conversion from int to Py_ssize_t for deque indices and length.
Raymond Hettinger <python@rcn.com>
parents: 47961
diff changeset
1068 Py_ssize_t index;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1069 block *b;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1070 dequeobject *deque;
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
1071 long state; /* state when the iterator is created */
48059
b68704457efe Finish-up the partial conversion from int to Py_ssize_t for deque indices and length.
Raymond Hettinger <python@rcn.com>
parents: 47961
diff changeset
1072 Py_ssize_t counter; /* number of items remaining for iteration */
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1073 } dequeiterobject;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1074
47678
a35da94e05fc Make more symbols static.
Martin v. Löwis <martin@v.loewis.de>
parents: 47527
diff changeset
1075 static PyTypeObject dequeiter_type;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1076
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1077 static PyObject *
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1078 deque_iter(dequeobject *deque)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1079 {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1080 dequeiterobject *it;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1081
50424
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49138
diff changeset
1082 it = PyObject_GC_New(dequeiterobject, &dequeiter_type);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1083 if (it == NULL)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1084 return NULL;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1085 it->b = deque->leftblock;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1086 it->index = deque->leftindex;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1087 Py_INCREF(deque);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1088 it->deque = deque;
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
1089 it->state = deque->state;
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1090 it->counter = deque->len;
50522
ee9ced92843a Merged revisions 68133-68134,68141-68142,68145-68146,68148-68149,68159-68162,68166,68171-68174,68179,68195-68196,68210,68214-68215,68217-68222 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50424
diff changeset
1091 PyObject_GC_Track(it);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1092 return (PyObject *)it;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1093 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1094
50424
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49138
diff changeset
1095 static int
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49138
diff changeset
1096 dequeiter_traverse(dequeiterobject *dio, visitproc visit, void *arg)
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49138
diff changeset
1097 {
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49138
diff changeset
1098 Py_VISIT(dio->deque);
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49138
diff changeset
1099 return 0;
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49138
diff changeset
1100 }
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49138
diff changeset
1101
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1102 static void
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1103 dequeiter_dealloc(dequeiterobject *dio)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1104 {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1105 Py_XDECREF(dio->deque);
50424
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49138
diff changeset
1106 PyObject_GC_Del(dio);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1107 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1108
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1109 static PyObject *
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1110 dequeiter_next(dequeiterobject *it)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1111 {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1112 PyObject *item;
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
1113
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
1114 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
1115 it->counter = 0;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1116 PyErr_SetString(PyExc_RuntimeError,
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
1117 "deque mutated during iteration");
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1118 return NULL;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1119 }
40573
bd7e41de982f Fix zero-length corner case for iterating over a mutating deque.
Raymond Hettinger <python@rcn.com>
parents: 39475
diff changeset
1120 if (it->counter == 0)
bd7e41de982f Fix zero-length corner case for iterating over a mutating deque.
Raymond Hettinger <python@rcn.com>
parents: 39475
diff changeset
1121 return NULL;
39232
cd8f20d400c1 defdict_reduce(): Plug leaks.
Tim Peters <tim.peters@gmail.com>
parents: 37748
diff changeset
1122 assert (!(it->b == it->deque->rightblock &&
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
1123 it->index > it->deque->rightindex));
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1124
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1125 item = it->b->data[it->index];
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1126 it->index++;
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
1127 it->counter--;
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
1128 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
1129 assert (it->b->rightlink != NULL);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1130 it->b = it->b->rightlink;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1131 it->index = 0;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1132 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1133 Py_INCREF(item);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1134 return item;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1135 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1136
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
1137 static PyObject *
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1138 dequeiter_len(dequeiterobject *it)
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1139 {
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
1140 return PyInt_FromLong(it->counter);
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1141 }
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1142
36484
20f75d5e79e9 Renamed _length_cue() to __length_hint__(). See:
Armin Rigo <arigo@tunes.org>
parents: 36324
diff changeset
1143 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
1144
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
1145 static PyMethodDef dequeiter_methods[] = {
36484
20f75d5e79e9 Renamed _length_cue() to __length_hint__(). See:
Armin Rigo <arigo@tunes.org>
parents: 36324
diff changeset
1146 {"__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
1147 {NULL, NULL} /* sentinel */
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1148 };
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1149
47678
a35da94e05fc Make more symbols static.
Martin v. Löwis <martin@v.loewis.de>
parents: 47527
diff changeset
1150 static PyTypeObject dequeiter_type = {
42333
6620a510e9a9 PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents: 41019
diff changeset
1151 PyVarObject_HEAD_INIT(NULL, 0)
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1152 "deque_iterator", /* tp_name */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1153 sizeof(dequeiterobject), /* tp_basicsize */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1154 0, /* tp_itemsize */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1155 /* methods */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1156 (destructor)dequeiter_dealloc, /* tp_dealloc */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1157 0, /* tp_print */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1158 0, /* tp_getattr */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1159 0, /* tp_setattr */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1160 0, /* tp_compare */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1161 0, /* tp_repr */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1162 0, /* tp_as_number */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
1163 0, /* tp_as_sequence */
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1164 0, /* tp_as_mapping */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1165 0, /* tp_hash */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1166 0, /* tp_call */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1167 0, /* tp_str */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1168 PyObject_GenericGetAttr, /* tp_getattro */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1169 0, /* tp_setattro */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1170 0, /* tp_as_buffer */
50424
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49138
diff changeset
1171 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1172 0, /* tp_doc */
50424
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49138
diff changeset
1173 (traverseproc)dequeiter_traverse, /* tp_traverse */
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1174 0, /* tp_clear */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1175 0, /* tp_richcompare */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1176 0, /* tp_weaklistoffset */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1177 PyObject_SelfIter, /* tp_iter */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1178 (iternextfunc)dequeiter_next, /* tp_iternext */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
1179 dequeiter_methods, /* tp_methods */
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1180 0,
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1181 };
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1182
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1183 /*********************** Deque Reverse Iterator **************************/
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1184
47678
a35da94e05fc Make more symbols static.
Martin v. Löwis <martin@v.loewis.de>
parents: 47527
diff changeset
1185 static PyTypeObject dequereviter_type;
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1186
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1187 static PyObject *
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1188 deque_reviter(dequeobject *deque)
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1189 {
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1190 dequeiterobject *it;
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1191
50424
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49138
diff changeset
1192 it = PyObject_GC_New(dequeiterobject, &dequereviter_type);
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1193 if (it == NULL)
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1194 return NULL;
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1195 it->b = deque->rightblock;
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1196 it->index = deque->rightindex;
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1197 Py_INCREF(deque);
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1198 it->deque = deque;
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
1199 it->state = deque->state;
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1200 it->counter = deque->len;
50522
ee9ced92843a Merged revisions 68133-68134,68141-68142,68145-68146,68148-68149,68159-68162,68166,68171-68174,68179,68195-68196,68210,68214-68215,68217-68222 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50424
diff changeset
1201 PyObject_GC_Track(it);
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1202 return (PyObject *)it;
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1203 }
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1204
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1205 static PyObject *
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1206 dequereviter_next(dequeiterobject *it)
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1207 {
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1208 PyObject *item;
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
1209 if (it->counter == 0)
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1210 return NULL;
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1211
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
1212 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
1213 it->counter = 0;
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1214 PyErr_SetString(PyExc_RuntimeError,
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
1215 "deque mutated during iteration");
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1216 return NULL;
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1217 }
39232
cd8f20d400c1 defdict_reduce(): Plug leaks.
Tim Peters <tim.peters@gmail.com>
parents: 37748
diff changeset
1218 assert (!(it->b == it->deque->leftblock &&
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
1219 it->index < it->deque->leftindex));
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1220
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1221 item = it->b->data[it->index];
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1222 it->index--;
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
1223 it->counter--;
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
1224 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
1225 assert (it->b->leftlink != NULL);
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1226 it->b = it->b->leftlink;
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1227 it->index = BLOCKLEN - 1;
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1228 }
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1229 Py_INCREF(item);
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1230 return item;
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1231 }
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1232
47678
a35da94e05fc Make more symbols static.
Martin v. Löwis <martin@v.loewis.de>
parents: 47527
diff changeset
1233 static PyTypeObject dequereviter_type = {
42333
6620a510e9a9 PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents: 41019
diff changeset
1234 PyVarObject_HEAD_INIT(NULL, 0)
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1235 "deque_reverse_iterator", /* tp_name */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1236 sizeof(dequeiterobject), /* tp_basicsize */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1237 0, /* tp_itemsize */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1238 /* methods */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1239 (destructor)dequeiter_dealloc, /* tp_dealloc */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1240 0, /* tp_print */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1241 0, /* tp_getattr */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1242 0, /* tp_setattr */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1243 0, /* tp_compare */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1244 0, /* tp_repr */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1245 0, /* tp_as_number */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
1246 0, /* tp_as_sequence */
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1247 0, /* tp_as_mapping */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1248 0, /* tp_hash */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1249 0, /* tp_call */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1250 0, /* tp_str */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1251 PyObject_GenericGetAttr, /* tp_getattro */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1252 0, /* tp_setattro */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1253 0, /* tp_as_buffer */
50424
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49138
diff changeset
1254 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1255 0, /* tp_doc */
50424
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49138
diff changeset
1256 (traverseproc)dequeiter_traverse, /* tp_traverse */
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1257 0, /* tp_clear */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1258 0, /* tp_richcompare */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1259 0, /* tp_weaklistoffset */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1260 PyObject_SelfIter, /* tp_iter */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1261 (iternextfunc)dequereviter_next, /* tp_iternext */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
1262 dequeiter_methods, /* tp_methods */
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1263 0,
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1264 };
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1265
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1266 /* defaultdict type *********************************************************/
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 typedef struct {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1269 PyDictObject dict;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1270 PyObject *default_factory;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1271 } defdictobject;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1272
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1273 static PyTypeObject defdict_type; /* Forward */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1274
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1275 PyDoc_STRVAR(defdict_missing_doc,
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1276 "__missing__(key) # Called by __getitem__ for missing key; pseudo-code:\n\
41019
b0f26a7813c9 Fix another reincarnation of bug #1576657 in defaultdict.
Georg Brandl <georg@python.org>
parents: 40989
diff changeset
1277 if self.default_factory is None: raise KeyError((key,))\n\
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1278 self[key] = value = self.default_factory()\n\
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1279 return value\n\
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1280 ");
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1281
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1282 static PyObject *
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1283 defdict_missing(defdictobject *dd, PyObject *key)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1284 {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1285 PyObject *factory = dd->default_factory;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1286 PyObject *value;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1287 if (factory == NULL || factory == Py_None) {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1288 /* XXX Call dict.__missing__(key) */
41019
b0f26a7813c9 Fix another reincarnation of bug #1576657 in defaultdict.
Georg Brandl <georg@python.org>
parents: 40989
diff changeset
1289 PyObject *tup;
b0f26a7813c9 Fix another reincarnation of bug #1576657 in defaultdict.
Georg Brandl <georg@python.org>
parents: 40989
diff changeset
1290 tup = PyTuple_Pack(1, key);
b0f26a7813c9 Fix another reincarnation of bug #1576657 in defaultdict.
Georg Brandl <georg@python.org>
parents: 40989
diff changeset
1291 if (!tup) return NULL;
b0f26a7813c9 Fix another reincarnation of bug #1576657 in defaultdict.
Georg Brandl <georg@python.org>
parents: 40989
diff changeset
1292 PyErr_SetObject(PyExc_KeyError, tup);
b0f26a7813c9 Fix another reincarnation of bug #1576657 in defaultdict.
Georg Brandl <georg@python.org>
parents: 40989
diff changeset
1293 Py_DECREF(tup);
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1294 return NULL;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1295 }
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1296 value = PyEval_CallObject(factory, NULL);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1297 if (value == NULL)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1298 return value;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1299 if (PyObject_SetItem((PyObject *)dd, key, value) < 0) {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1300 Py_DECREF(value);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1301 return NULL;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1302 }
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1303 return value;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1304 }
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1305
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1306 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
1307
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1308 static PyObject *
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1309 defdict_copy(defdictobject *dd)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1310 {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1311 /* This calls the object's class. That only works for subclasses
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1312 whose class constructor has the same signature. Subclasses that
45300
a47f5ffc0823 dict.copy() rises from the ashes. Revert r60687.
Raymond Hettinger <python@rcn.com>
parents: 45250
diff changeset
1313 define a different constructor signature must override copy().
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1314 */
55535
271d382dcabd Issue 6637: defaultdict.copy() failed with an empty factory.
Raymond Hettinger <python@rcn.com>
parents: 52119
diff changeset
1315
271d382dcabd Issue 6637: defaultdict.copy() failed with an empty factory.
Raymond Hettinger <python@rcn.com>
parents: 52119
diff changeset
1316 if (dd->default_factory == NULL)
271d382dcabd Issue 6637: defaultdict.copy() failed with an empty factory.
Raymond Hettinger <python@rcn.com>
parents: 52119
diff changeset
1317 return PyObject_CallFunctionObjArgs((PyObject*)Py_TYPE(dd), Py_None, dd, NULL);
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 44281
diff changeset
1318 return PyObject_CallFunctionObjArgs((PyObject*)Py_TYPE(dd),
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1319 dd->default_factory, dd, NULL);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1320 }
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1321
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1322 static PyObject *
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1323 defdict_reduce(defdictobject *dd)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1324 {
39232
cd8f20d400c1 defdict_reduce(): Plug leaks.
Tim Peters <tim.peters@gmail.com>
parents: 37748
diff changeset
1325 /* __reduce__ must return a 5-tuple as follows:
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1326
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1327 - factory function
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1328 - tuple of args for the factory function
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1329 - additional state (here None)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1330 - sequence iterator (here None)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1331 - dictionary iterator (yielding successive (key, value) pairs
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1332
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1333 This API is used by pickle.py and copy.py.
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1334
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1335 For this to be useful with pickle.py, the default_factory
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1336 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
1337 function in a module or package.
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1338
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1339 Both shallow and deep copying are supported, but for deep
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1340 copying, the default_factory must be deep-copyable; e.g. None,
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1341 or a built-in (functions are not copyable at this time).
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1342
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1343 This only works for subclasses as long as their constructor
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1344 signature is compatible; the first argument must be the
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1345 optional default_factory, defaulting to None.
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1346 */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1347 PyObject *args;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1348 PyObject *items;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1349 PyObject *result;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1350 if (dd->default_factory == NULL || dd->default_factory == Py_None)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1351 args = PyTuple_New(0);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1352 else
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1353 args = PyTuple_Pack(1, dd->default_factory);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1354 if (args == NULL)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1355 return NULL;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1356 items = PyObject_CallMethod((PyObject *)dd, "iteritems", "()");
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1357 if (items == NULL) {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1358 Py_DECREF(args);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1359 return NULL;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1360 }
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 44281
diff changeset
1361 result = PyTuple_Pack(5, Py_TYPE(dd), args,
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1362 Py_None, Py_None, items);
39232
cd8f20d400c1 defdict_reduce(): Plug leaks.
Tim Peters <tim.peters@gmail.com>
parents: 37748
diff changeset
1363 Py_DECREF(items);
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1364 Py_DECREF(args);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1365 return result;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1366 }
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1367
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1368 static PyMethodDef defdict_methods[] = {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1369 {"__missing__", (PyCFunction)defdict_missing, METH_O,
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1370 defdict_missing_doc},
45300
a47f5ffc0823 dict.copy() rises from the ashes. Revert r60687.
Raymond Hettinger <python@rcn.com>
parents: 45250
diff changeset
1371 {"copy", (PyCFunction)defdict_copy, METH_NOARGS,
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1372 defdict_copy_doc},
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1373 {"__copy__", (PyCFunction)defdict_copy, METH_NOARGS,
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1374 defdict_copy_doc},
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1375 {"__reduce__", (PyCFunction)defdict_reduce, METH_NOARGS,
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1376 reduce_doc},
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1377 {NULL}
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1378 };
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1379
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1380 static PyMemberDef defdict_members[] = {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1381 {"default_factory", T_OBJECT,
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1382 offsetof(defdictobject, default_factory), 0,
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1383 PyDoc_STR("Factory for default value called by __missing__().")},
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1384 {NULL}
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1385 };
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1386
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1387 static void
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1388 defdict_dealloc(defdictobject *dd)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1389 {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1390 Py_CLEAR(dd->default_factory);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1391 PyDict_Type.tp_dealloc((PyObject *)dd);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1392 }
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1393
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1394 static int
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1395 defdict_print(defdictobject *dd, FILE *fp, int flags)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1396 {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1397 int sts;
43475
17b07370a782 Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents: 43469
diff changeset
1398 Py_BEGIN_ALLOW_THREADS
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1399 fprintf(fp, "defaultdict(");
43475
17b07370a782 Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents: 43469
diff changeset
1400 Py_END_ALLOW_THREADS
17b07370a782 Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents: 43469
diff changeset
1401 if (dd->default_factory == NULL) {
17b07370a782 Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents: 43469
diff changeset
1402 Py_BEGIN_ALLOW_THREADS
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1403 fprintf(fp, "None");
43475
17b07370a782 Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents: 43469
diff changeset
1404 Py_END_ALLOW_THREADS
17b07370a782 Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents: 43469
diff changeset
1405 } else {
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1406 PyObject_Print(dd->default_factory, fp, 0);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1407 }
43475
17b07370a782 Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents: 43469
diff changeset
1408 Py_BEGIN_ALLOW_THREADS
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1409 fprintf(fp, ", ");
43475
17b07370a782 Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents: 43469
diff changeset
1410 Py_END_ALLOW_THREADS
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1411 sts = PyDict_Type.tp_print((PyObject *)dd, fp, 0);
43475
17b07370a782 Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents: 43469
diff changeset
1412 Py_BEGIN_ALLOW_THREADS
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1413 fprintf(fp, ")");
43475
17b07370a782 Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents: 43469
diff changeset
1414 Py_END_ALLOW_THREADS
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1415 return sts;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1416 }
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1417
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1418 static PyObject *
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1419 defdict_repr(defdictobject *dd)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1420 {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1421 PyObject *defrepr;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1422 PyObject *baserepr;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1423 PyObject *result;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1424 baserepr = PyDict_Type.tp_repr((PyObject *)dd);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1425 if (baserepr == NULL)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1426 return NULL;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1427 if (dd->default_factory == NULL)
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
1428 defrepr = PyString_FromString("None");
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1429 else
45231
e94af0c7aa12 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44350
diff changeset
1430 {
e94af0c7aa12 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44350
diff changeset
1431 int status = Py_ReprEnter(dd->default_factory);
e94af0c7aa12 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44350
diff changeset
1432 if (status != 0) {
e94af0c7aa12 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44350
diff changeset
1433 if (status < 0)
e94af0c7aa12 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44350
diff changeset
1434 return NULL;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
1435 defrepr = PyString_FromString("...");
45231
e94af0c7aa12 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44350
diff changeset
1436 }
e94af0c7aa12 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44350
diff changeset
1437 else
e94af0c7aa12 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44350
diff changeset
1438 defrepr = PyObject_Repr(dd->default_factory);
e94af0c7aa12 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44350
diff changeset
1439 Py_ReprLeave(dd->default_factory);
e94af0c7aa12 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44350
diff changeset
1440 }
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1441 if (defrepr == NULL) {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1442 Py_DECREF(baserepr);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1443 return NULL;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1444 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
1445 result = PyString_FromFormat("defaultdict(%s, %s)",
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
1446 PyString_AS_STRING(defrepr),
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
1447 PyString_AS_STRING(baserepr));
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1448 Py_DECREF(defrepr);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1449 Py_DECREF(baserepr);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1450 return result;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1451 }
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1452
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1453 static int
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1454 defdict_traverse(PyObject *self, visitproc visit, void *arg)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1455 {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1456 Py_VISIT(((defdictobject *)self)->default_factory);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1457 return PyDict_Type.tp_traverse(self, visit, arg);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1458 }
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1459
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1460 static int
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1461 defdict_tp_clear(defdictobject *dd)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1462 {
37748
d1547426ab61 Use Py_CLEAR instead of in-place DECREF/XDECREF or custom macros, for
Thomas Wouters <thomas@python.org>
parents: 37470
diff changeset
1463 Py_CLEAR(dd->default_factory);
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1464 return PyDict_Type.tp_clear((PyObject *)dd);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1465 }
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1466
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1467 static int
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1468 defdict_init(PyObject *self, PyObject *args, PyObject *kwds)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1469 {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1470 defdictobject *dd = (defdictobject *)self;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1471 PyObject *olddefault = dd->default_factory;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1472 PyObject *newdefault = NULL;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1473 PyObject *newargs;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1474 int result;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1475 if (args == NULL || !PyTuple_Check(args))
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1476 newargs = PyTuple_New(0);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1477 else {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1478 Py_ssize_t n = PyTuple_GET_SIZE(args);
40760
2900c7564055 Check for a common user error with defaultdict().
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
1479 if (n > 0) {
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1480 newdefault = PyTuple_GET_ITEM(args, 0);
55535
271d382dcabd Issue 6637: defaultdict.copy() failed with an empty factory.
Raymond Hettinger <python@rcn.com>
parents: 52119
diff changeset
1481 if (!PyCallable_Check(newdefault) && newdefault != Py_None) {
40760
2900c7564055 Check for a common user error with defaultdict().
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
1482 PyErr_SetString(PyExc_TypeError,
2900c7564055 Check for a common user error with defaultdict().
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
1483 "first argument must be callable");
2900c7564055 Check for a common user error with defaultdict().
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
1484 return -1;
2900c7564055 Check for a common user error with defaultdict().
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
1485 }
2900c7564055 Check for a common user error with defaultdict().
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
1486 }
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1487 newargs = PySequence_GetSlice(args, 1, n);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1488 }
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1489 if (newargs == NULL)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1490 return -1;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1491 Py_XINCREF(newdefault);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1492 dd->default_factory = newdefault;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1493 result = PyDict_Type.tp_init(self, newargs, kwds);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1494 Py_DECREF(newargs);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1495 Py_XDECREF(olddefault);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1496 return result;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1497 }
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1498
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1499 PyDoc_STRVAR(defdict_doc,
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1500 "defaultdict(default_factory) --> dict with default factory\n\
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1501 \n\
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1502 The default factory is called without arguments to produce\n\
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1503 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
1504 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
1505 ");
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1506
37470
9ecbce55a454 SF Bug #1448488 - make collectionsmodule build on Cygwin, using the same
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37305
diff changeset
1507 /* 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
1508 #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
1509
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1510 static PyTypeObject defdict_type = {
42333
6620a510e9a9 PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents: 41019
diff changeset
1511 PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1512 "collections.defaultdict", /* tp_name */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1513 sizeof(defdictobject), /* tp_basicsize */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1514 0, /* tp_itemsize */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1515 /* methods */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1516 (destructor)defdict_dealloc, /* tp_dealloc */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1517 (printfunc)defdict_print, /* tp_print */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1518 0, /* tp_getattr */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1519 0, /* tp_setattr */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1520 0, /* tp_compare */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1521 (reprfunc)defdict_repr, /* tp_repr */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1522 0, /* tp_as_number */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1523 0, /* tp_as_sequence */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1524 0, /* tp_as_mapping */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1525 0, /* tp_hash */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1526 0, /* tp_call */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1527 0, /* tp_str */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1528 PyObject_GenericGetAttr, /* tp_getattro */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1529 0, /* tp_setattro */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1530 0, /* tp_as_buffer */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1531 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1532 Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1533 defdict_doc, /* tp_doc */
37305
5e1b6f553763 Remove unnecessary casts from type object initializers.
Georg Brandl <georg@python.org>
parents: 36656
diff changeset
1534 defdict_traverse, /* tp_traverse */
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1535 (inquiry)defdict_tp_clear, /* tp_clear */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1536 0, /* tp_richcompare */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1537 0, /* tp_weaklistoffset*/
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1538 0, /* tp_iter */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1539 0, /* tp_iternext */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1540 defdict_methods, /* tp_methods */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1541 defdict_members, /* tp_members */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1542 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
1543 DEFERRED_ADDRESS(&PyDict_Type), /* tp_base */
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1544 0, /* tp_dict */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1545 0, /* tp_descr_get */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1546 0, /* tp_descr_set */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1547 0, /* tp_dictoffset */
37305
5e1b6f553763 Remove unnecessary casts from type object initializers.
Georg Brandl <georg@python.org>
parents: 36656
diff changeset
1548 defdict_init, /* tp_init */
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1549 PyType_GenericAlloc, /* tp_alloc */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1550 0, /* tp_new */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1551 PyObject_GC_Del, /* tp_free */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1552 };
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1553
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1554 /* module level code ********************************************************/
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1555
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1556 PyDoc_STRVAR(module_doc,
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1557 "High performance data structures.\n\
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1558 - deque: ordered collection accessible from endpoints only\n\
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1559 - 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
1560 ");
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1561
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1562 PyMODINIT_FUNC
40989
4087c0ec733b Prepare collections module for pure python code entries.
Raymond Hettinger <python@rcn.com>
parents: 40760
diff changeset
1563 init_collections(void)
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1564 {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1565 PyObject *m;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1566
40989
4087c0ec733b Prepare collections module for pure python code entries.
Raymond Hettinger <python@rcn.com>
parents: 40760
diff changeset
1567 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
1568 if (m == NULL)
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35636
diff changeset
1569 return;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1570
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1571 if (PyType_Ready(&deque_type) < 0)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1572 return;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1573 Py_INCREF(&deque_type);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1574 PyModule_AddObject(m, "deque", (PyObject *)&deque_type);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1575
37470
9ecbce55a454 SF Bug #1448488 - make collectionsmodule build on Cygwin, using the same
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37305
diff changeset
1576 defdict_type.tp_base = &PyDict_Type;
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1577 if (PyType_Ready(&defdict_type) < 0)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1578 return;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1579 Py_INCREF(&defdict_type);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1580 PyModule_AddObject(m, "defaultdict", (PyObject *)&defdict_type);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1581
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1582 if (PyType_Ready(&dequeiter_type) < 0)
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
1583 return;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1584
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1585 if (PyType_Ready(&dequereviter_type) < 0)
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1586 return;
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1587
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1588 return;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1589 }