annotate Modules/_collectionsmodule.c @ 49258:25f809123df6 legacy-trunk

Issue #3925: Ignores shutil.rmtree error on cygwin too. Reviewed by Benjamin Peterson.
author Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
date Sun, 21 Sep 2008 20:48:41 +0000
parents 145449109a31
children df3923c36ec5
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
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
279 static PyObject *
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
280 deque_extend(dequeobject *deque, PyObject *iterable)
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
281 {
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
282 PyObject *it, *item;
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
283
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
284 it = PyObject_GetIter(iterable);
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
285 if (it == NULL)
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
286 return NULL;
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
287
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
288 while ((item = PyIter_Next(it)) != NULL) {
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
289 deque->state++;
33648
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33644
diff changeset
290 if (deque->rightindex == BLOCKLEN-1) {
33677
262e1cf41d34 Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
291 block *b = newblock(deque->rightblock, NULL,
262e1cf41d34 Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
292 deque->len);
31466
f04ccc288542 * Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents: 31464
diff changeset
293 if (b == NULL) {
f04ccc288542 * Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents: 31464
diff changeset
294 Py_DECREF(item);
f04ccc288542 * Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents: 31464
diff changeset
295 Py_DECREF(it);
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
296 return NULL;
31466
f04ccc288542 * Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents: 31464
diff changeset
297 }
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
298 assert(deque->rightblock->rightlink == NULL);
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
299 deque->rightblock->rightlink = b;
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
300 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
301 deque->rightindex = -1;
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
302 }
33648
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33644
diff changeset
303 deque->len++;
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33644
diff changeset
304 deque->rightindex++;
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
305 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
306 TRIM(deque, deque_popleft);
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
307 }
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
308 Py_DECREF(it);
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
309 if (PyErr_Occurred())
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
310 return NULL;
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
311 Py_RETURN_NONE;
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
312 }
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
313
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
314 PyDoc_STRVAR(extend_doc,
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
315 "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
316
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
317 static PyObject *
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
318 deque_extendleft(dequeobject *deque, PyObject *iterable)
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
319 {
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
320 PyObject *it, *item;
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
321
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
322 it = PyObject_GetIter(iterable);
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
323 if (it == NULL)
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
324 return NULL;
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
325
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
326 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
327 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
328 if (deque->leftindex == 0) {
33677
262e1cf41d34 Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
329 block *b = newblock(NULL, deque->leftblock,
262e1cf41d34 Armin's patch to prevent overflows.
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
330 deque->len);
31466
f04ccc288542 * Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents: 31464
diff changeset
331 if (b == NULL) {
f04ccc288542 * Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents: 31464
diff changeset
332 Py_DECREF(item);
f04ccc288542 * Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents: 31464
diff changeset
333 Py_DECREF(it);
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
334 return NULL;
31466
f04ccc288542 * Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents: 31464
diff changeset
335 }
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
336 assert(deque->leftblock->leftlink == NULL);
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
337 deque->leftblock->leftlink = b;
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
338 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
339 deque->leftindex = BLOCKLEN;
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
340 }
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
341 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
342 deque->leftindex--;
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
343 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
344 TRIM(deque, deque_pop);
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
345 }
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
346 Py_DECREF(it);
32554
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32413
diff changeset
347 if (PyErr_Occurred())
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
348 return NULL;
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
349 Py_RETURN_NONE;
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
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
352 PyDoc_STRVAR(extendleft_doc,
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
353 "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
354
33697
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
355 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
356 _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
357 {
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
358 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
359 PyObject *item, *rv;
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
360
31476
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
361 if (len == 0)
33697
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
362 return 0;
31476
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
363 if (n > halflen || n < -halflen) {
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
364 n %= len;
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
365 if (n > halflen)
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
366 n -= len;
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
367 else if (n < -halflen)
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
368 n += len;
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
369 }
31474
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
370
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
371 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
372 item = deque_pop(deque, NULL);
32554
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32413
diff changeset
373 assert (item != NULL);
31474
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
374 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
375 Py_DECREF(item);
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
376 if (rv == NULL)
33697
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
377 return -1;
31474
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
378 Py_DECREF(rv);
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
379 }
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
380 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
381 item = deque_popleft(deque, NULL);
32554
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32413
diff changeset
382 assert (item != NULL);
31474
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
383 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
384 Py_DECREF(item);
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
385 if (rv == NULL)
33697
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
386 return -1;
31474
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
387 Py_DECREF(rv);
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
388 }
33697
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
389 return 0;
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
390 }
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
391
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
392 static PyObject *
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
393 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
394 {
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
395 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
396
48060
759a7323591e Parse to the correct datatype.
Raymond Hettinger <python@rcn.com>
parents: 48059
diff changeset
397 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
398 return NULL;
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
399 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
400 Py_RETURN_NONE;
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
401 return NULL;
31474
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
402 }
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
403
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
404 PyDoc_STRVAR(rotate_doc,
31476
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
405 "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
406
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
407 static Py_ssize_t
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
408 deque_len(dequeobject *deque)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
409 {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
410 return deque->len;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
411 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
412
34771
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
413 static PyObject *
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
414 deque_remove(dequeobject *deque, PyObject *value)
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
415 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
416 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
417
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
418 for (i=0 ; i<n ; i++) {
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
419 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
420 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
421
2ee575b9b60e Apply remove's mutation test after every equality test.
Raymond Hettinger <python@rcn.com>
parents: 34771
diff changeset
422 if (deque->len != n) {
39232
cd8f20d400c1 defdict_reduce(): Plug leaks.
Tim Peters <tim.peters@gmail.com>
parents: 37748
diff changeset
423 PyErr_SetString(PyExc_IndexError,
34772
2ee575b9b60e Apply remove's mutation test after every equality test.
Raymond Hettinger <python@rcn.com>
parents: 34771
diff changeset
424 "deque mutated during remove().");
2ee575b9b60e Apply remove's mutation test after every equality test.
Raymond Hettinger <python@rcn.com>
parents: 34771
diff changeset
425 return NULL;
2ee575b9b60e Apply remove's mutation test after every equality test.
Raymond Hettinger <python@rcn.com>
parents: 34771
diff changeset
426 }
34771
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
427 if (cmp > 0) {
34772
2ee575b9b60e Apply remove's mutation test after every equality test.
Raymond Hettinger <python@rcn.com>
parents: 34771
diff changeset
428 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
429 assert (tgt != NULL);
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
430 Py_DECREF(tgt);
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
431 if (_deque_rotate(deque, i) == -1)
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
432 return NULL;
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
433 Py_RETURN_NONE;
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
434 }
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
435 else if (cmp < 0) {
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
436 _deque_rotate(deque, i);
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
437 return NULL;
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
438 }
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
439 _deque_rotate(deque, -1);
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
440 }
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
441 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
442 return NULL;
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
443 }
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
444
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
445 PyDoc_STRVAR(remove_doc,
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
446 "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
447
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
448 static int
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
449 deque_clear(dequeobject *deque)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
450 {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
451 PyObject *item;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
452
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
453 while (deque->len) {
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
454 item = deque_pop(deque, NULL);
32554
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32413
diff changeset
455 assert (item != NULL);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
456 Py_DECREF(item);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
457 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
458 assert(deque->leftblock == deque->rightblock &&
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
459 deque->leftindex - 1 == deque->rightindex &&
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
460 deque->len == 0);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
461 return 0;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
462 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
463
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
464 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
465 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
466 {
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
467 block *b;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
468 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
469 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
470
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
471 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
472 PyErr_SetString(PyExc_IndexError,
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
473 "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
474 return NULL;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
475 }
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
476
31615
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
477 if (i == 0) {
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
478 i = deque->leftindex;
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
479 b = deque->leftblock;
31615
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
480 } else if (i == deque->len - 1) {
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
481 i = deque->rightindex;
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
482 b = deque->rightblock;
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
483 } else {
31615
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
484 i += deque->leftindex;
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
485 n = i / BLOCKLEN;
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
486 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
487 if (index < (deque->len >> 1)) {
31615
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
488 b = deque->leftblock;
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
489 while (n--)
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
490 b = b->rightlink;
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
491 } else {
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
492 n = (deque->leftindex + deque->len - 1) / BLOCKLEN - n;
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
493 b = deque->rightblock;
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
494 while (n--)
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
495 b = b->leftlink;
739ab9de2a1b Special case endpoint access for speed.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
496 }
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
497 }
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
498 item = b->data[i];
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
499 Py_INCREF(item);
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
500 return item;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
501 }
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
502
32413
d7110bb6c13d Add a comment with implementation notes.
Raymond Hettinger <python@rcn.com>
parents: 32363
diff changeset
503 /* 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
504 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
505 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
506 (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
507 performance boost.
d7110bb6c13d Add a comment with implementation notes.
Raymond Hettinger <python@rcn.com>
parents: 32363
diff changeset
508 */
d7110bb6c13d Add a comment with implementation notes.
Raymond Hettinger <python@rcn.com>
parents: 32363
diff changeset
509
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
510 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
511 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
512 {
33697
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
513 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
514
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
515 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
516 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
517 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
518
245835ed8de8 Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents: 31908
diff changeset
519 item = deque_popleft(deque, NULL);
32554
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32413
diff changeset
520 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
521 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
522
33697
aac4cc6618b5 Simplify delitem() code by calling rotate() directly instead of using
Raymond Hettinger <python@rcn.com>
parents: 33677
diff changeset
523 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
524 }
245835ed8de8 Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents: 31908
diff changeset
525
245835ed8de8 Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents: 31908
diff changeset
526 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
527 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
528 {
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
529 PyObject *old_value;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
530 block *b;
36518
9f3f87e920a6 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
531 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
532
32554
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32413
diff changeset
533 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
534 PyErr_SetString(PyExc_IndexError,
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
535 "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
536 return -1;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
537 }
32084
245835ed8de8 Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents: 31908
diff changeset
538 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
539 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
540
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
541 i += deque->leftindex;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
542 n = i / BLOCKLEN;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
543 i %= BLOCKLEN;
32554
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32413
diff changeset
544 if (index <= halflen) {
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
545 b = deque->leftblock;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
546 while (n--)
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
547 b = b->rightlink;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
548 } else {
32554
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32413
diff changeset
549 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
550 b = deque->rightblock;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
551 while (n--)
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
552 b = b->leftlink;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
553 }
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
554 Py_INCREF(v);
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
555 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
556 b->data[i] = v;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
557 Py_DECREF(old_value);
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
558 return 0;
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
559 }
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
560
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
561 static PyObject *
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
562 deque_clearmethod(dequeobject *deque)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
563 {
32554
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32413
diff changeset
564 int rv;
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32413
diff changeset
565
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32413
diff changeset
566 rv = deque_clear(deque);
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32413
diff changeset
567 assert (rv != -1);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
568 Py_RETURN_NONE;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
569 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
570
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
571 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
572
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
573 static void
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
574 deque_dealloc(dequeobject *deque)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
575 {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
576 PyObject_GC_UnTrack(deque);
32158
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 32119
diff changeset
577 if (deque->weakreflist != NULL)
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 32119
diff changeset
578 PyObject_ClearWeakRefs((PyObject *) deque);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
579 if (deque->leftblock != NULL) {
32752
dbd3a04eb0c6 Silence a GCC unused variable warning in debug builds.
Raymond Hettinger <python@rcn.com>
parents: 32701
diff changeset
580 deque_clear(deque);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
581 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
582 freeblock(deque->leftblock);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
583 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
584 deque->leftblock = NULL;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
585 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
586 Py_TYPE(deque)->tp_free(deque);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
587 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
588
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
589 static int
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
590 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
591 {
33639
40350fbe665d deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents: 33638
diff changeset
592 block *b;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
593 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
594 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
595 Py_ssize_t indexlo = deque->leftindex;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
596
33639
40350fbe665d deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents: 33638
diff changeset
597 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
598 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
599 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
600 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
601
40350fbe665d deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents: 33638
diff changeset
602 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
603 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
604 Py_VISIT(item);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
605 }
33639
40350fbe665d deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents: 33638
diff changeset
606 indexlo = 0;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
607 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
608 return 0;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
609 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
610
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
611 static PyObject *
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
612 deque_copy(PyObject *deque)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
613 {
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
614 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
615 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
616 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
617 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
618 deque, ((dequeobject *)deque)->maxlen, NULL);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
619 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
620
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
621 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
622
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
623 static PyObject *
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
624 deque_reduce(dequeobject *deque)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
625 {
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
626 PyObject *dict, *result, *aslist;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
627
33949
b9819b9a0dad SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33879
diff changeset
628 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
629 if (dict == NULL)
33949
b9819b9a0dad SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33879
diff changeset
630 PyErr_Clear();
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
631 aslist = PySequence_List((PyObject *)deque);
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
632 if (aslist == NULL) {
43469
f2c9f24a0f5a dict could be NULL, so we need to XDECREF.
Neal Norwitz <nnorwitz@gmail.com>
parents: 43466
diff changeset
633 Py_XDECREF(dict);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
634 return NULL;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
635 }
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
636 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
637 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
638 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
639 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
640 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
641 } 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
642 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
643 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
644 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
645 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
646 }
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
647 Py_XDECREF(dict);
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
648 Py_DECREF(aslist);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
649 return result;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
650 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
651
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
652 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
653
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
654 static PyObject *
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
655 deque_repr(PyObject *deque)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
656 {
43475
17b07370a782 Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents: 43469
diff changeset
657 PyObject *aslist, *result, *fmt;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
658 int i;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
659
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
660 i = Py_ReprEnter(deque);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
661 if (i != 0) {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
662 if (i < 0)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
663 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
664 return PyString_FromString("[...]");
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
665 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
666
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
667 aslist = PySequence_List(deque);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
668 if (aslist == NULL) {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
669 Py_ReprLeave(deque);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
670 return NULL;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
671 }
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
672 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
673 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
674 ((dequeobject *)deque)->maxlen);
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
675 else
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
676 fmt = PyString_FromString("deque(%r)");
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
677 if (fmt == NULL) {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
678 Py_DECREF(aslist);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
679 Py_ReprLeave(deque);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
680 return NULL;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
681 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
682 result = PyString_Format(fmt, aslist);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
683 Py_DECREF(fmt);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
684 Py_DECREF(aslist);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
685 Py_ReprLeave(deque);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
686 return result;
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
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
690 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
691 {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
692 PyObject *it, *item;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
693 char *emit = ""; /* No separator emitted on first pass */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
694 char *separator = ", ";
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
695 int i;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
696
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
697 i = Py_ReprEnter(deque);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
698 if (i != 0) {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
699 if (i < 0)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
700 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
701 Py_BEGIN_ALLOW_THREADS
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
702 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
703 Py_END_ALLOW_THREADS
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
704 return 0;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
705 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
706
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
707 it = PyObject_GetIter(deque);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
708 if (it == NULL)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
709 return -1;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
710
43475
17b07370a782 Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents: 43469
diff changeset
711 Py_BEGIN_ALLOW_THREADS
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
712 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
713 Py_END_ALLOW_THREADS
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
714 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
715 Py_BEGIN_ALLOW_THREADS
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
716 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
717 Py_END_ALLOW_THREADS
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
718 emit = separator;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
719 if (PyObject_Print(item, fp, 0) != 0) {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
720 Py_DECREF(item);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
721 Py_DECREF(it);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
722 Py_ReprLeave(deque);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
723 return -1;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
724 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
725 Py_DECREF(item);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
726 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
727 Py_ReprLeave(deque);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
728 Py_DECREF(it);
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
729 if (PyErr_Occurred())
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
730 return -1;
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
731
43475
17b07370a782 Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents: 43469
diff changeset
732 Py_BEGIN_ALLOW_THREADS
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
733 if (((dequeobject *)deque)->maxlen == -1)
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
734 fputs("])", fp);
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
735 else
48829
74899e6c87a5 Fixed two format strings in the _collections module. For example
Christian Heimes <christian@cheimes.de>
parents: 48060
diff changeset
736 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
737 Py_END_ALLOW_THREADS
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
738 return 0;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
739 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
740
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
741 static PyObject *
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
742 deque_richcompare(PyObject *v, PyObject *w, int op)
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
743 {
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
744 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
745 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
746 int b, cmp=-1;
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
747
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
748 if (!PyObject_TypeCheck(v, &deque_type) ||
32119
dd1e55deaf52 Make type check work with subclasses
Raymond Hettinger <python@rcn.com>
parents: 32084
diff changeset
749 !PyObject_TypeCheck(w, &deque_type)) {
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
750 Py_INCREF(Py_NotImplemented);
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
751 return Py_NotImplemented;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
752 }
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
753
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
754 /* Shortcuts */
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
755 vs = ((dequeobject *)v)->len;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
756 ws = ((dequeobject *)w)->len;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
757 if (op == Py_EQ) {
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
758 if (v == w)
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
759 Py_RETURN_TRUE;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
760 if (vs != ws)
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
761 Py_RETURN_FALSE;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
762 }
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
763 if (op == Py_NE) {
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
764 if (v == w)
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
765 Py_RETURN_FALSE;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
766 if (vs != ws)
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
767 Py_RETURN_TRUE;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
768 }
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
769
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
770 /* Search for the first index where items are different */
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
771 it1 = PyObject_GetIter(v);
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
772 if (it1 == NULL)
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
773 goto done;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
774 it2 = PyObject_GetIter(w);
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
775 if (it2 == NULL)
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
776 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
777 for (;;) {
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
778 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
779 if (x == NULL && PyErr_Occurred())
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
780 goto done;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
781 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
782 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
783 break;
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
784 b = PyObject_RichCompareBool(x, y, Py_EQ);
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
785 if (b == 0) {
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
786 cmp = PyObject_RichCompareBool(x, y, op);
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
787 Py_DECREF(x);
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
788 Py_DECREF(y);
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
789 goto done;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
790 }
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
791 Py_DECREF(x);
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
792 Py_DECREF(y);
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
793 if (b == -1)
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
794 goto done;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
795 }
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
796 /* 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
797 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
798 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
799 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
800 goto done;
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
801 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
802 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
803 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
804 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
805 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
806 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
807 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
808 }
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
809
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
810 done:
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
811 Py_XDECREF(it1);
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
812 Py_XDECREF(it2);
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
813 if (cmp == 1)
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
814 Py_RETURN_TRUE;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
815 if (cmp == 0)
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
816 Py_RETURN_FALSE;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
817 return NULL;
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
818 }
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
819
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
820 static int
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
821 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
822 {
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
823 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
824 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
825 Py_ssize_t maxlen = -1;
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
826 char *kwlist[] = {"iterable", "maxlen", 0};
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
827
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
828 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
829 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
830 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
831 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
832 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
833 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
834 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
835 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
836 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
837 }
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
838 }
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
839 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
840 deque_clear(deque);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
841 if (iterable != NULL) {
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
842 PyObject *rv = deque_extend(deque, iterable);
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
843 if (rv == NULL)
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
844 return -1;
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31438
diff changeset
845 Py_DECREF(rv);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
846 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
847 return 0;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
848 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
849
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
850 static PySequenceMethods deque_as_sequence = {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
851 (lenfunc)deque_len, /* sq_length */
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
852 0, /* sq_concat */
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31605
diff changeset
853 0, /* sq_repeat */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
854 (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
855 0, /* sq_slice */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
856 (ssizeobjargproc)deque_ass_item, /* sq_ass_item */
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
857 };
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
858
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
859 /* deque object ********************************************************/
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
860
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
861 static PyObject *deque_iter(dequeobject *deque);
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
862 static PyObject *deque_reviter(dequeobject *deque);
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
863 PyDoc_STRVAR(reversed_doc,
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
864 "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
865
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
866 static PyMethodDef deque_methods[] = {
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
867 {"append", (PyCFunction)deque_append,
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
868 METH_O, append_doc},
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
869 {"appendleft", (PyCFunction)deque_appendleft,
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
870 METH_O, appendleft_doc},
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
871 {"clear", (PyCFunction)deque_clearmethod,
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
872 METH_NOARGS, clear_doc},
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
873 {"__copy__", (PyCFunction)deque_copy,
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
874 METH_NOARGS, copy_doc},
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
875 {"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
876 METH_O, extend_doc},
34771
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
877 {"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
878 METH_O, extendleft_doc},
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
879 {"pop", (PyCFunction)deque_pop,
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
880 METH_NOARGS, pop_doc},
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
881 {"popleft", (PyCFunction)deque_popleft,
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
882 METH_NOARGS, popleft_doc},
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
883 {"__reduce__", (PyCFunction)deque_reduce,
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
884 METH_NOARGS, reduce_doc},
34771
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
885 {"remove", (PyCFunction)deque_remove,
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
886 METH_O, remove_doc},
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
887 {"__reversed__", (PyCFunction)deque_reviter,
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
888 METH_NOARGS, reversed_doc},
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
889 {"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
890 METH_VARARGS, rotate_doc},
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
891 {NULL, NULL} /* sentinel */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
892 };
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
893
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
894 PyDoc_STRVAR(deque_doc,
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 43378
diff changeset
895 "deque(iterable[, maxlen]) --> deque object\n\
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
896 \n\
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
897 Build an ordered collection accessible from endpoints only.");
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
898
31605
2fed331de014 Make deque_type static so namespace is not polluted.
Neal Norwitz <nnorwitz@gmail.com>
parents: 31603
diff changeset
899 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
900 PyVarObject_HEAD_INIT(NULL, 0)
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
901 "collections.deque", /* tp_name */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
902 sizeof(dequeobject), /* tp_basicsize */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
903 0, /* tp_itemsize */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
904 /* methods */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
905 (destructor)deque_dealloc, /* tp_dealloc */
37305
5e1b6f553763 Remove unnecessary casts from type object initializers.
Georg Brandl <georg@python.org>
parents: 36656
diff changeset
906 deque_tp_print, /* tp_print */
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
907 0, /* tp_getattr */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
908 0, /* tp_setattr */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
909 0, /* tp_compare */
37305
5e1b6f553763 Remove unnecessary casts from type object initializers.
Georg Brandl <georg@python.org>
parents: 36656
diff changeset
910 deque_repr, /* tp_repr */
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
911 0, /* tp_as_number */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
912 &deque_as_sequence, /* tp_as_sequence */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
913 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
914 (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
915 0, /* tp_call */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
916 0, /* tp_str */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
917 PyObject_GenericGetAttr, /* tp_getattro */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
918 0, /* tp_setattro */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
919 0, /* tp_as_buffer */
32158
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 32119
diff changeset
920 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
921 Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
922 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
923 (traverseproc)deque_traverse, /* tp_traverse */
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
924 (inquiry)deque_clear, /* tp_clear */
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31595
diff changeset
925 (richcmpfunc)deque_richcompare, /* tp_richcompare */
32158
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 32119
diff changeset
926 offsetof(dequeobject, weakreflist), /* tp_weaklistoffset*/
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
927 (getiterfunc)deque_iter, /* tp_iter */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
928 0, /* tp_iternext */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
929 deque_methods, /* tp_methods */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
930 0, /* tp_members */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
931 0, /* tp_getset */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
932 0, /* tp_base */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
933 0, /* tp_dict */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
934 0, /* tp_descr_get */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
935 0, /* tp_descr_set */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
936 0, /* tp_dictoffset */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
937 (initproc)deque_init, /* tp_init */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
938 PyType_GenericAlloc, /* tp_alloc */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
939 deque_new, /* tp_new */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
940 PyObject_GC_Del, /* tp_free */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
941 };
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
942
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
943 /*********************** Deque Iterator **************************/
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
944
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
945 typedef struct {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
946 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
947 Py_ssize_t index;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
948 block *b;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
949 dequeobject *deque;
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
950 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
951 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
952 } dequeiterobject;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
953
47678
a35da94e05fc Make more symbols static.
Martin v. Löwis <martin@v.loewis.de>
parents: 47527
diff changeset
954 static PyTypeObject dequeiter_type;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
955
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
956 static PyObject *
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
957 deque_iter(dequeobject *deque)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
958 {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
959 dequeiterobject *it;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
960
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
961 it = PyObject_New(dequeiterobject, &dequeiter_type);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
962 if (it == NULL)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
963 return NULL;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
964 it->b = deque->leftblock;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
965 it->index = deque->leftindex;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
966 Py_INCREF(deque);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
967 it->deque = deque;
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
968 it->state = deque->state;
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
969 it->counter = deque->len;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
970 return (PyObject *)it;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
971 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
972
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
973 static void
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
974 dequeiter_dealloc(dequeiterobject *dio)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
975 {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
976 Py_XDECREF(dio->deque);
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
977 Py_TYPE(dio)->tp_free(dio);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
978 }
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 *
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
981 dequeiter_next(dequeiterobject *it)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
982 {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
983 PyObject *item;
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
984
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
985 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
986 it->counter = 0;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
987 PyErr_SetString(PyExc_RuntimeError,
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
988 "deque mutated during iteration");
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
989 return NULL;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
990 }
40573
bd7e41de982f Fix zero-length corner case for iterating over a mutating deque.
Raymond Hettinger <python@rcn.com>
parents: 39475
diff changeset
991 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
992 return NULL;
39232
cd8f20d400c1 defdict_reduce(): Plug leaks.
Tim Peters <tim.peters@gmail.com>
parents: 37748
diff changeset
993 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
994 it->index > it->deque->rightindex));
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
995
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
996 item = it->b->data[it->index];
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
997 it->index++;
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
998 it->counter--;
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
999 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
1000 assert (it->b->rightlink != NULL);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1001 it->b = it->b->rightlink;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1002 it->index = 0;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1003 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1004 Py_INCREF(item);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1005 return item;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1006 }
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1007
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
1008 static PyObject *
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1009 dequeiter_len(dequeiterobject *it)
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1010 {
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
1011 return PyInt_FromLong(it->counter);
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1012 }
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1013
36484
20f75d5e79e9 Renamed _length_cue() to __length_hint__(). See:
Armin Rigo <arigo@tunes.org>
parents: 36324
diff changeset
1014 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
1015
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
1016 static PyMethodDef dequeiter_methods[] = {
36484
20f75d5e79e9 Renamed _length_cue() to __length_hint__(). See:
Armin Rigo <arigo@tunes.org>
parents: 36324
diff changeset
1017 {"__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
1018 {NULL, NULL} /* sentinel */
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1019 };
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1020
47678
a35da94e05fc Make more symbols static.
Martin v. Löwis <martin@v.loewis.de>
parents: 47527
diff changeset
1021 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
1022 PyVarObject_HEAD_INIT(NULL, 0)
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1023 "deque_iterator", /* tp_name */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1024 sizeof(dequeiterobject), /* tp_basicsize */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1025 0, /* tp_itemsize */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1026 /* methods */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1027 (destructor)dequeiter_dealloc, /* tp_dealloc */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1028 0, /* tp_print */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1029 0, /* tp_getattr */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1030 0, /* tp_setattr */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1031 0, /* tp_compare */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1032 0, /* tp_repr */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1033 0, /* tp_as_number */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
1034 0, /* tp_as_sequence */
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1035 0, /* tp_as_mapping */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1036 0, /* tp_hash */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1037 0, /* tp_call */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1038 0, /* tp_str */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1039 PyObject_GenericGetAttr, /* tp_getattro */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1040 0, /* tp_setattro */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1041 0, /* tp_as_buffer */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1042 Py_TPFLAGS_DEFAULT, /* tp_flags */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1043 0, /* tp_doc */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1044 0, /* tp_traverse */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1045 0, /* tp_clear */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1046 0, /* tp_richcompare */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1047 0, /* tp_weaklistoffset */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1048 PyObject_SelfIter, /* tp_iter */
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1049 (iternextfunc)dequeiter_next, /* tp_iternext */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
1050 dequeiter_methods, /* tp_methods */
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1051 0,
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1052 };
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1053
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1054 /*********************** Deque Reverse Iterator **************************/
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1055
47678
a35da94e05fc Make more symbols static.
Martin v. Löwis <martin@v.loewis.de>
parents: 47527
diff changeset
1056 static PyTypeObject dequereviter_type;
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1057
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1058 static PyObject *
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1059 deque_reviter(dequeobject *deque)
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1060 {
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1061 dequeiterobject *it;
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1062
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1063 it = PyObject_New(dequeiterobject, &dequereviter_type);
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1064 if (it == NULL)
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1065 return NULL;
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1066 it->b = deque->rightblock;
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1067 it->index = deque->rightindex;
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1068 Py_INCREF(deque);
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1069 it->deque = deque;
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
1070 it->state = deque->state;
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1071 it->counter = deque->len;
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1072 return (PyObject *)it;
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1073 }
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1074
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1075 static PyObject *
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1076 dequereviter_next(dequeiterobject *it)
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1077 {
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1078 PyObject *item;
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
1079 if (it->counter == 0)
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1080 return NULL;
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1081
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
1082 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
1083 it->counter = 0;
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1084 PyErr_SetString(PyExc_RuntimeError,
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
1085 "deque mutated during iteration");
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1086 return NULL;
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1087 }
39232
cd8f20d400c1 defdict_reduce(): Plug leaks.
Tim Peters <tim.peters@gmail.com>
parents: 37748
diff changeset
1088 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
1089 it->index < it->deque->leftindex));
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1090
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1091 item = it->b->data[it->index];
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1092 it->index--;
33644
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
1093 it->counter--;
00c36e1bc50b * Bulletproof the method for detecting mutations during iteration.
Raymond Hettinger <python@rcn.com>
parents: 33643
diff changeset
1094 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
1095 assert (it->b->leftlink != NULL);
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1096 it->b = it->b->leftlink;
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1097 it->index = BLOCKLEN - 1;
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1098 }
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1099 Py_INCREF(item);
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1100 return item;
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1101 }
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1102
47678
a35da94e05fc Make more symbols static.
Martin v. Löwis <martin@v.loewis.de>
parents: 47527
diff changeset
1103 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
1104 PyVarObject_HEAD_INIT(NULL, 0)
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1105 "deque_reverse_iterator", /* tp_name */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1106 sizeof(dequeiterobject), /* tp_basicsize */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1107 0, /* tp_itemsize */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1108 /* methods */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1109 (destructor)dequeiter_dealloc, /* tp_dealloc */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1110 0, /* tp_print */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1111 0, /* tp_getattr */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1112 0, /* tp_setattr */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1113 0, /* tp_compare */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1114 0, /* tp_repr */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1115 0, /* tp_as_number */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
1116 0, /* tp_as_sequence */
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1117 0, /* tp_as_mapping */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1118 0, /* tp_hash */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1119 0, /* tp_call */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1120 0, /* tp_str */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1121 PyObject_GenericGetAttr, /* tp_getattro */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1122 0, /* tp_setattro */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1123 0, /* tp_as_buffer */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1124 Py_TPFLAGS_DEFAULT, /* tp_flags */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1125 0, /* tp_doc */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1126 0, /* tp_traverse */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1127 0, /* tp_clear */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1128 0, /* tp_richcompare */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1129 0, /* tp_weaklistoffset */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1130 PyObject_SelfIter, /* tp_iter */
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1131 (iternextfunc)dequereviter_next, /* tp_iternext */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
1132 dequeiter_methods, /* tp_methods */
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1133 0,
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1134 };
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1135
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1136 /* defaultdict type *********************************************************/
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1137
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1138 typedef struct {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1139 PyDictObject dict;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1140 PyObject *default_factory;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1141 } defdictobject;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1142
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1143 static PyTypeObject defdict_type; /* Forward */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1144
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1145 PyDoc_STRVAR(defdict_missing_doc,
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1146 "__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
1147 if self.default_factory is None: raise KeyError((key,))\n\
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1148 self[key] = value = self.default_factory()\n\
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1149 return value\n\
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1150 ");
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1151
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1152 static PyObject *
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1153 defdict_missing(defdictobject *dd, PyObject *key)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1154 {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1155 PyObject *factory = dd->default_factory;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1156 PyObject *value;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1157 if (factory == NULL || factory == Py_None) {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1158 /* XXX Call dict.__missing__(key) */
41019
b0f26a7813c9 Fix another reincarnation of bug #1576657 in defaultdict.
Georg Brandl <georg@python.org>
parents: 40989
diff changeset
1159 PyObject *tup;
b0f26a7813c9 Fix another reincarnation of bug #1576657 in defaultdict.
Georg Brandl <georg@python.org>
parents: 40989
diff changeset
1160 tup = PyTuple_Pack(1, key);
b0f26a7813c9 Fix another reincarnation of bug #1576657 in defaultdict.
Georg Brandl <georg@python.org>
parents: 40989
diff changeset
1161 if (!tup) return NULL;
b0f26a7813c9 Fix another reincarnation of bug #1576657 in defaultdict.
Georg Brandl <georg@python.org>
parents: 40989
diff changeset
1162 PyErr_SetObject(PyExc_KeyError, tup);
b0f26a7813c9 Fix another reincarnation of bug #1576657 in defaultdict.
Georg Brandl <georg@python.org>
parents: 40989
diff changeset
1163 Py_DECREF(tup);
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1164 return NULL;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1165 }
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1166 value = PyEval_CallObject(factory, NULL);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1167 if (value == NULL)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1168 return value;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1169 if (PyObject_SetItem((PyObject *)dd, key, value) < 0) {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1170 Py_DECREF(value);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1171 return NULL;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1172 }
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1173 return value;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1174 }
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1175
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1176 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
1177
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1178 static PyObject *
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1179 defdict_copy(defdictobject *dd)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1180 {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1181 /* This calls the object's class. That only works for subclasses
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1182 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
1183 define a different constructor signature must override copy().
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1184 */
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
1185 return PyObject_CallFunctionObjArgs((PyObject*)Py_TYPE(dd),
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1186 dd->default_factory, dd, NULL);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1187 }
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1188
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1189 static PyObject *
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1190 defdict_reduce(defdictobject *dd)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1191 {
39232
cd8f20d400c1 defdict_reduce(): Plug leaks.
Tim Peters <tim.peters@gmail.com>
parents: 37748
diff changeset
1192 /* __reduce__ must return a 5-tuple as follows:
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1193
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1194 - factory function
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1195 - tuple of args for the factory function
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1196 - additional state (here None)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1197 - sequence iterator (here None)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1198 - dictionary iterator (yielding successive (key, value) pairs
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1199
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1200 This API is used by pickle.py and copy.py.
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1201
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1202 For this to be useful with pickle.py, the default_factory
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1203 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
1204 function in a module or package.
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1205
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1206 Both shallow and deep copying are supported, but for deep
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1207 copying, the default_factory must be deep-copyable; e.g. None,
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1208 or a built-in (functions are not copyable at this time).
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1209
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1210 This only works for subclasses as long as their constructor
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1211 signature is compatible; the first argument must be the
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1212 optional default_factory, defaulting to None.
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1213 */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1214 PyObject *args;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1215 PyObject *items;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1216 PyObject *result;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1217 if (dd->default_factory == NULL || dd->default_factory == Py_None)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1218 args = PyTuple_New(0);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1219 else
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1220 args = PyTuple_Pack(1, dd->default_factory);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1221 if (args == NULL)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1222 return NULL;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1223 items = PyObject_CallMethod((PyObject *)dd, "iteritems", "()");
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1224 if (items == NULL) {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1225 Py_DECREF(args);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1226 return NULL;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1227 }
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
1228 result = PyTuple_Pack(5, Py_TYPE(dd), args,
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1229 Py_None, Py_None, items);
39232
cd8f20d400c1 defdict_reduce(): Plug leaks.
Tim Peters <tim.peters@gmail.com>
parents: 37748
diff changeset
1230 Py_DECREF(items);
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1231 Py_DECREF(args);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1232 return result;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1233 }
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1234
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1235 static PyMethodDef defdict_methods[] = {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1236 {"__missing__", (PyCFunction)defdict_missing, METH_O,
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1237 defdict_missing_doc},
45300
a47f5ffc0823 dict.copy() rises from the ashes. Revert r60687.
Raymond Hettinger <python@rcn.com>
parents: 45250
diff changeset
1238 {"copy", (PyCFunction)defdict_copy, METH_NOARGS,
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1239 defdict_copy_doc},
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1240 {"__copy__", (PyCFunction)defdict_copy, METH_NOARGS,
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1241 defdict_copy_doc},
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1242 {"__reduce__", (PyCFunction)defdict_reduce, METH_NOARGS,
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1243 reduce_doc},
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1244 {NULL}
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1245 };
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1246
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1247 static PyMemberDef defdict_members[] = {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1248 {"default_factory", T_OBJECT,
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1249 offsetof(defdictobject, default_factory), 0,
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1250 PyDoc_STR("Factory for default value called by __missing__().")},
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1251 {NULL}
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1252 };
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1253
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1254 static void
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1255 defdict_dealloc(defdictobject *dd)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1256 {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1257 Py_CLEAR(dd->default_factory);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1258 PyDict_Type.tp_dealloc((PyObject *)dd);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1259 }
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1260
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1261 static int
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1262 defdict_print(defdictobject *dd, FILE *fp, int flags)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1263 {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1264 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
1265 Py_BEGIN_ALLOW_THREADS
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1266 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
1267 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
1268 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
1269 Py_BEGIN_ALLOW_THREADS
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1270 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
1271 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
1272 } else {
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1273 PyObject_Print(dd->default_factory, fp, 0);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1274 }
43475
17b07370a782 Restore BEGIN/END THREADS macros which were squashed in the previous checkin
Raymond Hettinger <python@rcn.com>
parents: 43469
diff changeset
1275 Py_BEGIN_ALLOW_THREADS
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1276 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
1277 Py_END_ALLOW_THREADS
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1278 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
1279 Py_BEGIN_ALLOW_THREADS
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1280 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
1281 Py_END_ALLOW_THREADS
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1282 return sts;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1283 }
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 static PyObject *
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1286 defdict_repr(defdictobject *dd)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1287 {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1288 PyObject *defrepr;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1289 PyObject *baserepr;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1290 PyObject *result;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1291 baserepr = PyDict_Type.tp_repr((PyObject *)dd);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1292 if (baserepr == NULL)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1293 return NULL;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1294 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
1295 defrepr = PyString_FromString("None");
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1296 else
45231
e94af0c7aa12 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44350
diff changeset
1297 {
e94af0c7aa12 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44350
diff changeset
1298 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
1299 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
1300 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
1301 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
1302 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
1303 }
e94af0c7aa12 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44350
diff changeset
1304 else
e94af0c7aa12 issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44350
diff changeset
1305 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
1306 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
1307 }
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1308 if (defrepr == NULL) {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1309 Py_DECREF(baserepr);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1310 return NULL;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1311 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
1312 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
1313 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
1314 PyString_AS_STRING(baserepr));
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1315 Py_DECREF(defrepr);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1316 Py_DECREF(baserepr);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1317 return result;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1318 }
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1319
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1320 static int
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1321 defdict_traverse(PyObject *self, visitproc visit, void *arg)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1322 {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1323 Py_VISIT(((defdictobject *)self)->default_factory);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1324 return PyDict_Type.tp_traverse(self, visit, arg);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1325 }
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 static int
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1328 defdict_tp_clear(defdictobject *dd)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1329 {
37748
d1547426ab61 Use Py_CLEAR instead of in-place DECREF/XDECREF or custom macros, for
Thomas Wouters <thomas@python.org>
parents: 37470
diff changeset
1330 Py_CLEAR(dd->default_factory);
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1331 return PyDict_Type.tp_clear((PyObject *)dd);
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
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1334 static int
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1335 defdict_init(PyObject *self, PyObject *args, PyObject *kwds)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1336 {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1337 defdictobject *dd = (defdictobject *)self;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1338 PyObject *olddefault = dd->default_factory;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1339 PyObject *newdefault = NULL;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1340 PyObject *newargs;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1341 int result;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1342 if (args == NULL || !PyTuple_Check(args))
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1343 newargs = PyTuple_New(0);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1344 else {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1345 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
1346 if (n > 0) {
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1347 newdefault = PyTuple_GET_ITEM(args, 0);
40760
2900c7564055 Check for a common user error with defaultdict().
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
1348 if (!PyCallable_Check(newdefault)) {
2900c7564055 Check for a common user error with defaultdict().
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
1349 PyErr_SetString(PyExc_TypeError,
2900c7564055 Check for a common user error with defaultdict().
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
1350 "first argument must be callable");
2900c7564055 Check for a common user error with defaultdict().
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
1351 return -1;
2900c7564055 Check for a common user error with defaultdict().
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
1352 }
2900c7564055 Check for a common user error with defaultdict().
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
1353 }
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1354 newargs = PySequence_GetSlice(args, 1, n);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1355 }
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1356 if (newargs == NULL)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1357 return -1;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1358 Py_XINCREF(newdefault);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1359 dd->default_factory = newdefault;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1360 result = PyDict_Type.tp_init(self, newargs, kwds);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1361 Py_DECREF(newargs);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1362 Py_XDECREF(olddefault);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1363 return result;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1364 }
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1365
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1366 PyDoc_STRVAR(defdict_doc,
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1367 "defaultdict(default_factory) --> dict with default factory\n\
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1368 \n\
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1369 The default factory is called without arguments to produce\n\
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1370 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
1371 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
1372 ");
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1373
37470
9ecbce55a454 SF Bug #1448488 - make collectionsmodule build on Cygwin, using the same
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37305
diff changeset
1374 /* 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
1375 #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
1376
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1377 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
1378 PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1379 "collections.defaultdict", /* tp_name */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1380 sizeof(defdictobject), /* tp_basicsize */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1381 0, /* tp_itemsize */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1382 /* methods */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1383 (destructor)defdict_dealloc, /* tp_dealloc */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1384 (printfunc)defdict_print, /* tp_print */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1385 0, /* tp_getattr */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1386 0, /* tp_setattr */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1387 0, /* tp_compare */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1388 (reprfunc)defdict_repr, /* tp_repr */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1389 0, /* tp_as_number */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1390 0, /* tp_as_sequence */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1391 0, /* tp_as_mapping */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1392 0, /* tp_hash */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1393 0, /* tp_call */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1394 0, /* tp_str */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1395 PyObject_GenericGetAttr, /* tp_getattro */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1396 0, /* tp_setattro */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1397 0, /* tp_as_buffer */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1398 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1399 Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1400 defdict_doc, /* tp_doc */
37305
5e1b6f553763 Remove unnecessary casts from type object initializers.
Georg Brandl <georg@python.org>
parents: 36656
diff changeset
1401 defdict_traverse, /* tp_traverse */
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1402 (inquiry)defdict_tp_clear, /* tp_clear */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1403 0, /* tp_richcompare */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1404 0, /* tp_weaklistoffset*/
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1405 0, /* tp_iter */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1406 0, /* tp_iternext */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1407 defdict_methods, /* tp_methods */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1408 defdict_members, /* tp_members */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1409 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
1410 DEFERRED_ADDRESS(&PyDict_Type), /* tp_base */
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1411 0, /* tp_dict */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1412 0, /* tp_descr_get */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1413 0, /* tp_descr_set */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1414 0, /* tp_dictoffset */
37305
5e1b6f553763 Remove unnecessary casts from type object initializers.
Georg Brandl <georg@python.org>
parents: 36656
diff changeset
1415 defdict_init, /* tp_init */
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1416 PyType_GenericAlloc, /* tp_alloc */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1417 0, /* tp_new */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1418 PyObject_GC_Del, /* tp_free */
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1419 };
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1420
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1421 /* module level code ********************************************************/
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1422
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1423 PyDoc_STRVAR(module_doc,
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1424 "High performance data structures.\n\
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1425 - deque: ordered collection accessible from endpoints only\n\
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1426 - 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
1427 ");
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1428
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1429 PyMODINIT_FUNC
40989
4087c0ec733b Prepare collections module for pure python code entries.
Raymond Hettinger <python@rcn.com>
parents: 40760
diff changeset
1430 init_collections(void)
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1431 {
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1432 PyObject *m;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1433
40989
4087c0ec733b Prepare collections module for pure python code entries.
Raymond Hettinger <python@rcn.com>
parents: 40760
diff changeset
1434 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
1435 if (m == NULL)
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35636
diff changeset
1436 return;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1437
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1438 if (PyType_Ready(&deque_type) < 0)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1439 return;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1440 Py_INCREF(&deque_type);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1441 PyModule_AddObject(m, "deque", (PyObject *)&deque_type);
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1442
37470
9ecbce55a454 SF Bug #1448488 - make collectionsmodule build on Cygwin, using the same
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37305
diff changeset
1443 defdict_type.tp_base = &PyDict_Type;
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1444 if (PyType_Ready(&defdict_type) < 0)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1445 return;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1446 Py_INCREF(&defdict_type);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1447 PyModule_AddObject(m, "defaultdict", (PyObject *)&defdict_type);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
1448
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1449 if (PyType_Ready(&dequeiter_type) < 0)
33634
6bd4a1056716 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32752
diff changeset
1450 return;
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1451
31685
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1452 if (PyType_Ready(&dequereviter_type) < 0)
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1453 return;
aafe5fef58ba Improve deque iteration.
Raymond Hettinger <python@rcn.com>
parents: 31615
diff changeset
1454
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1455 return;
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1456 }