annotate Modules/_collectionsmodule.c @ 58925:eecf1bef052d 2.6

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