annotate Modules/_collectionsmodule.c @ 65035:87f4b32905df 2.7

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