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