Bug Summary

File:./Modules/_collectionsmodule.c
Location:line 1573, column 13
Description:Access to field 'ob_refcnt' results in a dereference of a null pointer (loaded from variable 'key')

Annotated Source Code

1#include "Python.h"
2#include "structmember.h"
3
4/* collections module implementation of a deque() datatype
5 Written and maintained by Raymond D. Hettinger <python@rcn.com>
6 Copyright (c) 2004 Python Software Foundation.
7 All rights reserved.
8*/
9
10/* The block length may be set to any number over 1. Larger numbers
11 * reduce the number of calls to the memory allocator but take more
12 * memory. Ideally, BLOCKLEN should be set with an eye to the
13 * length of a cache line.
14 */
15
16#define BLOCKLEN62 62
17#define CENTER((62 - 1) / 2) ((BLOCKLEN62 - 1) / 2)
18
19/* A `dequeobject` is composed of a doubly-linked list of `block` nodes.
20 * This list is not circular (the leftmost block has leftlink==NULL,
21 * and the rightmost block has rightlink==NULL). A deque d's first
22 * element is at d.leftblock[leftindex] and its last element is at
23 * d.rightblock[rightindex]; note that, unlike as for Python slice
24 * indices, these indices are inclusive on both ends. By being inclusive
25 * on both ends, algorithms for left and right operations become
26 * symmetrical which simplifies the design.
27 *
28 * The list of blocks is never empty, so d.leftblock and d.rightblock
29 * are never equal to NULL.
30 *
31 * The indices, d.leftindex and d.rightindex are always in the range
32 * 0 <= index < BLOCKLEN.
33 * Their exact relationship is:
34 * (d.leftindex + d.len - 1) % BLOCKLEN == d.rightindex.
35 *
36 * Empty deques have d.len == 0; d.leftblock==d.rightblock;
37 * d.leftindex == CENTER+1; and d.rightindex == CENTER.
38 * Checking for d.len == 0 is the intended way to see whether d is empty.
39 *
40 * Whenever d.leftblock == d.rightblock,
41 * d.leftindex + d.len - 1 == d.rightindex.
42 *
43 * However, when d.leftblock != d.rightblock, d.leftindex and d.rightindex
44 * become indices into distinct blocks and either may be larger than the
45 * other.
46 */
47
48typedef struct BLOCK {
49 struct BLOCK *leftlink;
50 struct BLOCK *rightlink;
51 PyObject *data[BLOCKLEN62];
52} block;
53
54#define MAXFREEBLOCKS10 10
55static Py_ssize_t numfreeblocks = 0;
56static block *freeblocks[MAXFREEBLOCKS10];
57
58static block *
59newblock(block *leftlink, block *rightlink, Py_ssize_t len) {
60 block *b;
61 /* To prevent len from overflowing PY_SSIZE_T_MAX on 64-bit machines, we
62 * refuse to allocate new blocks if the current len is dangerously
63 * close. There is some extra margin to prevent spurious arithmetic
64 * overflows at various places. The following check ensures that
65 * the blocks allocated to the deque, in the worst case, can only
66 * have PY_SSIZE_T_MAX-2 entries in total.
67 */
68 if (len >= PY_SSIZE_T_MAX((Py_ssize_t)(((size_t)-1)>>1)) - 2*BLOCKLEN62) {
69 PyErr_SetString(PyExc_OverflowError,
70 "cannot add more blocks to the deque");
71 return NULL((void*)0);
72 }
73 if (numfreeblocks) {
74 numfreeblocks -= 1;
75 b = freeblocks[numfreeblocks];
76 } else {
77 b = PyMem_Malloc(sizeof(block));
78 if (b == NULL((void*)0)) {
79 PyErr_NoMemory();
80 return NULL((void*)0);
81 }
82 }
83 b->leftlink = leftlink;
84 b->rightlink = rightlink;
85 return b;
86}
87
88static void
89freeblock(block *b)
90{
91 if (numfreeblocks < MAXFREEBLOCKS10) {
92 freeblocks[numfreeblocks] = b;
93 numfreeblocks++;
94 } else {
95 PyMem_Free(b);
96 }
97}
98
99typedef struct {
100 PyObject_HEADPyObject ob_base;
101 block *leftblock;
102 block *rightblock;
103 Py_ssize_t leftindex; /* in range(BLOCKLEN) */
104 Py_ssize_t rightindex; /* in range(BLOCKLEN) */
105 Py_ssize_t len;
106 Py_ssize_t maxlen;
107 long state; /* incremented whenever the indices move */
108 PyObject *weakreflist; /* List of weak references */
109} dequeobject;
110
111/* The deque's size limit is d.maxlen. The limit can be zero or positive.
112 * If there is no limit, then d.maxlen == -1.
113 *
114 * After an item is added to a deque, we check to see if the size has grown past
115 * the limit. If it has, we get the size back down to the limit by popping an
116 * item off of the opposite end. The methods that can trigger this are append(),
117 * appendleft(), extend(), and extendleft().
118 */
119
120#define TRIM(d, popfunction)if (d->maxlen != -1 && d->len > d->maxlen
) { PyObject *rv = popfunction(d, ((void*)0)); (__builtin_expect
(!(rv != ((void*)0) && d->len <= d->maxlen),
0) ? __assert_rtn(__func__, "./Modules/_collectionsmodule.c"
, 120, "rv != NULL && d->len <= d->maxlen") :
(void)0); do { if (_Py_RefTotal-- , --((PyObject*)(rv))->
ob_refcnt != 0) { if (((PyObject*)rv)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 120, (PyObject *)(rv)); } else
_Py_Dealloc((PyObject *)(rv)); } while (0); }
\
121 if (d->maxlen != -1 && d->len > d->maxlen) { \
122 PyObject *rv = popfunction(d, NULL((void*)0)); \
123 assert(rv != NULL && d->len <= d->maxlen)(__builtin_expect(!(rv != ((void*)0) && d->len <=
d->maxlen), 0) ? __assert_rtn(__func__, "./Modules/_collectionsmodule.c"
, 123, "rv != NULL && d->len <= d->maxlen") :
(void)0)
; \
124 Py_DECREF(rv)do { if (_Py_RefTotal-- , --((PyObject*)(rv))->ob_refcnt !=
0) { if (((PyObject*)rv)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 124, (PyObject *)(rv)); } else
_Py_Dealloc((PyObject *)(rv)); } while (0)
; \
125 }
126
127static PyTypeObject deque_type;
128
129static PyObject *
130deque_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
131{
132 dequeobject *deque;
133 block *b;
134
135 /* create dequeobject structure */
136 deque = (dequeobject *)type->tp_alloc(type, 0);
137 if (deque == NULL((void*)0))
138 return NULL((void*)0);
139
140 b = newblock(NULL((void*)0), NULL((void*)0), 0);
141 if (b == NULL((void*)0)) {
142 Py_DECREF(deque)do { if (_Py_RefTotal-- , --((PyObject*)(deque))->ob_refcnt
!= 0) { if (((PyObject*)deque)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 142, (PyObject *)(deque));
} else _Py_Dealloc((PyObject *)(deque)); } while (0)
;
143 return NULL((void*)0);
144 }
145
146 assert(BLOCKLEN >= 2)(__builtin_expect(!(62 >= 2), 0) ? __assert_rtn(__func__, "./Modules/_collectionsmodule.c"
, 146, "BLOCKLEN >= 2") : (void)0)
;
147 deque->leftblock = b;
148 deque->rightblock = b;
149 deque->leftindex = CENTER((62 - 1) / 2) + 1;
150 deque->rightindex = CENTER((62 - 1) / 2);
151 deque->len = 0;
152 deque->state = 0;
153 deque->weakreflist = NULL((void*)0);
154 deque->maxlen = -1;
155
156 return (PyObject *)deque;
157}
158
159static PyObject *
160deque_pop(dequeobject *deque, PyObject *unused)
161{
162 PyObject *item;
163 block *prevblock;
164
165 if (deque->len == 0) {
166 PyErr_SetString(PyExc_IndexError, "pop from an empty deque");
167 return NULL((void*)0);
168 }
169 item = deque->rightblock->data[deque->rightindex];
170 deque->rightindex--;
171 deque->len--;
172 deque->state++;
173
174 if (deque->rightindex == -1) {
175 if (deque->len == 0) {
176 assert(deque->leftblock == deque->rightblock)(__builtin_expect(!(deque->leftblock == deque->rightblock
), 0) ? __assert_rtn(__func__, "./Modules/_collectionsmodule.c"
, 176, "deque->leftblock == deque->rightblock") : (void
)0)
;
177 assert(deque->leftindex == deque->rightindex+1)(__builtin_expect(!(deque->leftindex == deque->rightindex
+1), 0) ? __assert_rtn(__func__, "./Modules/_collectionsmodule.c"
, 177, "deque->leftindex == deque->rightindex+1") : (void
)0)
;
178 /* re-center instead of freeing a block */
179 deque->leftindex = CENTER((62 - 1) / 2) + 1;
180 deque->rightindex = CENTER((62 - 1) / 2);
181 } else {
182 prevblock = deque->rightblock->leftlink;
183 assert(deque->leftblock != deque->rightblock)(__builtin_expect(!(deque->leftblock != deque->rightblock
), 0) ? __assert_rtn(__func__, "./Modules/_collectionsmodule.c"
, 183, "deque->leftblock != deque->rightblock") : (void
)0)
;
184 freeblock(deque->rightblock);
185 prevblock->rightlink = NULL((void*)0);
186 deque->rightblock = prevblock;
187 deque->rightindex = BLOCKLEN62 - 1;
188 }
189 }
190 return item;
191}
192
193PyDoc_STRVAR(pop_doc, "Remove and return the rightmost element.")static char pop_doc[] = "Remove and return the rightmost element.";
194
195static PyObject *
196deque_popleft(dequeobject *deque, PyObject *unused)
197{
198 PyObject *item;
199 block *prevblock;
200
201 if (deque->len == 0) {
202 PyErr_SetString(PyExc_IndexError, "pop from an empty deque");
203 return NULL((void*)0);
204 }
205 assert(deque->leftblock != NULL)(__builtin_expect(!(deque->leftblock != ((void*)0)), 0) ? __assert_rtn
(__func__, "./Modules/_collectionsmodule.c", 205, "deque->leftblock != NULL"
) : (void)0)
;
206 item = deque->leftblock->data[deque->leftindex];
207 deque->leftindex++;
208 deque->len--;
209 deque->state++;
210
211 if (deque->leftindex == BLOCKLEN62) {
212 if (deque->len == 0) {
213 assert(deque->leftblock == deque->rightblock)(__builtin_expect(!(deque->leftblock == deque->rightblock
), 0) ? __assert_rtn(__func__, "./Modules/_collectionsmodule.c"
, 213, "deque->leftblock == deque->rightblock") : (void
)0)
;
214 assert(deque->leftindex == deque->rightindex+1)(__builtin_expect(!(deque->leftindex == deque->rightindex
+1), 0) ? __assert_rtn(__func__, "./Modules/_collectionsmodule.c"
, 214, "deque->leftindex == deque->rightindex+1") : (void
)0)
;
215 /* re-center instead of freeing a block */
216 deque->leftindex = CENTER((62 - 1) / 2) + 1;
217 deque->rightindex = CENTER((62 - 1) / 2);
218 } else {
219 assert(deque->leftblock != deque->rightblock)(__builtin_expect(!(deque->leftblock != deque->rightblock
), 0) ? __assert_rtn(__func__, "./Modules/_collectionsmodule.c"
, 219, "deque->leftblock != deque->rightblock") : (void
)0)
;
220 prevblock = deque->leftblock->rightlink;
221 freeblock(deque->leftblock);
222 assert(prevblock != NULL)(__builtin_expect(!(prevblock != ((void*)0)), 0) ? __assert_rtn
(__func__, "./Modules/_collectionsmodule.c", 222, "prevblock != NULL"
) : (void)0)
;
223 prevblock->leftlink = NULL((void*)0);
224 deque->leftblock = prevblock;
225 deque->leftindex = 0;
226 }
227 }
228 return item;
229}
230
231PyDoc_STRVAR(popleft_doc, "Remove and return the leftmost element.")static char popleft_doc[] = "Remove and return the leftmost element.";
232
233static PyObject *
234deque_append(dequeobject *deque, PyObject *item)
235{
236 deque->state++;
237 if (deque->rightindex == BLOCKLEN62-1) {
238 block *b = newblock(deque->rightblock, NULL((void*)0), deque->len);
239 if (b == NULL((void*)0))
240 return NULL((void*)0);
241 assert(deque->rightblock->rightlink == NULL)(__builtin_expect(!(deque->rightblock->rightlink == ((void
*)0)), 0) ? __assert_rtn(__func__, "./Modules/_collectionsmodule.c"
, 241, "deque->rightblock->rightlink == NULL") : (void)
0)
;
242 deque->rightblock->rightlink = b;
243 deque->rightblock = b;
244 deque->rightindex = -1;
245 }
246 Py_INCREF(item)( _Py_RefTotal++ , ((PyObject*)(item))->ob_refcnt++);
247 deque->len++;
248 deque->rightindex++;
249 deque->rightblock->data[deque->rightindex] = item;
250 TRIM(deque, deque_popleft)if (deque->maxlen != -1 && deque->len > deque
->maxlen) { PyObject *rv = deque_popleft(deque, ((void*)0)
); (__builtin_expect(!(rv != ((void*)0) && deque->
len <= deque->maxlen), 0) ? __assert_rtn(__func__, "./Modules/_collectionsmodule.c"
, 250, "rv != NULL && deque->len <= deque->maxlen"
) : (void)0); do { if (_Py_RefTotal-- , --((PyObject*)(rv))->
ob_refcnt != 0) { if (((PyObject*)rv)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 250, (PyObject *)(rv)); } else
_Py_Dealloc((PyObject *)(rv)); } while (0); }
;
251 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
252}
253
254PyDoc_STRVAR(append_doc, "Add an element to the right side of the deque.")static char append_doc[] = "Add an element to the right side of the deque.";
255
256static PyObject *
257deque_appendleft(dequeobject *deque, PyObject *item)
258{
259 deque->state++;
260 if (deque->leftindex == 0) {
261 block *b = newblock(NULL((void*)0), deque->leftblock, deque->len);
262 if (b == NULL((void*)0))
263 return NULL((void*)0);
264 assert(deque->leftblock->leftlink == NULL)(__builtin_expect(!(deque->leftblock->leftlink == ((void
*)0)), 0) ? __assert_rtn(__func__, "./Modules/_collectionsmodule.c"
, 264, "deque->leftblock->leftlink == NULL") : (void)0)
;
265 deque->leftblock->leftlink = b;
266 deque->leftblock = b;
267 deque->leftindex = BLOCKLEN62;
268 }
269 Py_INCREF(item)( _Py_RefTotal++ , ((PyObject*)(item))->ob_refcnt++);
270 deque->len++;
271 deque->leftindex--;
272 deque->leftblock->data[deque->leftindex] = item;
273 TRIM(deque, deque_pop)if (deque->maxlen != -1 && deque->len > deque
->maxlen) { PyObject *rv = deque_pop(deque, ((void*)0)); (
__builtin_expect(!(rv != ((void*)0) && deque->len <=
deque->maxlen), 0) ? __assert_rtn(__func__, "./Modules/_collectionsmodule.c"
, 273, "rv != NULL && deque->len <= deque->maxlen"
) : (void)0); do { if (_Py_RefTotal-- , --((PyObject*)(rv))->
ob_refcnt != 0) { if (((PyObject*)rv)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 273, (PyObject *)(rv)); } else
_Py_Dealloc((PyObject *)(rv)); } while (0); }
;
274 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
275}
276
277PyDoc_STRVAR(appendleft_doc, "Add an element to the left side of the deque.")static char appendleft_doc[] = "Add an element to the left side of the deque.";
278
279
280/* Run an iterator to exhaustion. Shortcut for
281 the extend/extendleft methods when maxlen == 0. */
282static PyObject*
283consume_iterator(PyObject *it)
284{
285 PyObject *item;
286
287 while ((item = PyIter_Next(it)) != NULL((void*)0)) {
288 Py_DECREF(item)do { if (_Py_RefTotal-- , --((PyObject*)(item))->ob_refcnt
!= 0) { if (((PyObject*)item)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 288, (PyObject *)(item)); }
else _Py_Dealloc((PyObject *)(item)); } while (0)
;
289 }
290 Py_DECREF(it)do { if (_Py_RefTotal-- , --((PyObject*)(it))->ob_refcnt !=
0) { if (((PyObject*)it)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 290, (PyObject *)(it)); } else
_Py_Dealloc((PyObject *)(it)); } while (0)
;
291 if (PyErr_Occurred())
292 return NULL((void*)0);
293 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
294}
295
296static PyObject *
297deque_extend(dequeobject *deque, PyObject *iterable)
298{
299 PyObject *it, *item;
300
301 /* Handle case where id(deque) == id(iterable) */
302 if ((PyObject *)deque == iterable) {
303 PyObject *result;
304 PyObject *s = PySequence_List(iterable);
305 if (s == NULL((void*)0))
306 return NULL((void*)0);
307 result = deque_extend(deque, s);
308 Py_DECREF(s)do { if (_Py_RefTotal-- , --((PyObject*)(s))->ob_refcnt !=
0) { if (((PyObject*)s)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 308, (PyObject *)(s)); } else
_Py_Dealloc((PyObject *)(s)); } while (0)
;
309 return result;
310 }
311
312 it = PyObject_GetIter(iterable);
313 if (it == NULL((void*)0))
314 return NULL((void*)0);
315
316 if (deque->maxlen == 0)
317 return consume_iterator(it);
318
319 while ((item = PyIter_Next(it)) != NULL((void*)0)) {
320 deque->state++;
321 if (deque->rightindex == BLOCKLEN62-1) {
322 block *b = newblock(deque->rightblock, NULL((void*)0),
323 deque->len);
324 if (b == NULL((void*)0)) {
325 Py_DECREF(item)do { if (_Py_RefTotal-- , --((PyObject*)(item))->ob_refcnt
!= 0) { if (((PyObject*)item)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 325, (PyObject *)(item)); }
else _Py_Dealloc((PyObject *)(item)); } while (0)
;
326 Py_DECREF(it)do { if (_Py_RefTotal-- , --((PyObject*)(it))->ob_refcnt !=
0) { if (((PyObject*)it)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 326, (PyObject *)(it)); } else
_Py_Dealloc((PyObject *)(it)); } while (0)
;
327 return NULL((void*)0);
328 }
329 assert(deque->rightblock->rightlink == NULL)(__builtin_expect(!(deque->rightblock->rightlink == ((void
*)0)), 0) ? __assert_rtn(__func__, "./Modules/_collectionsmodule.c"
, 329, "deque->rightblock->rightlink == NULL") : (void)
0)
;
330 deque->rightblock->rightlink = b;
331 deque->rightblock = b;
332 deque->rightindex = -1;
333 }
334 deque->len++;
335 deque->rightindex++;
336 deque->rightblock->data[deque->rightindex] = item;
337 TRIM(deque, deque_popleft)if (deque->maxlen != -1 && deque->len > deque
->maxlen) { PyObject *rv = deque_popleft(deque, ((void*)0)
); (__builtin_expect(!(rv != ((void*)0) && deque->
len <= deque->maxlen), 0) ? __assert_rtn(__func__, "./Modules/_collectionsmodule.c"
, 337, "rv != NULL && deque->len <= deque->maxlen"
) : (void)0); do { if (_Py_RefTotal-- , --((PyObject*)(rv))->
ob_refcnt != 0) { if (((PyObject*)rv)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 337, (PyObject *)(rv)); } else
_Py_Dealloc((PyObject *)(rv)); } while (0); }
;
338 }
339 Py_DECREF(it)do { if (_Py_RefTotal-- , --((PyObject*)(it))->ob_refcnt !=
0) { if (((PyObject*)it)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 339, (PyObject *)(it)); } else
_Py_Dealloc((PyObject *)(it)); } while (0)
;
340 if (PyErr_Occurred())
341 return NULL((void*)0);
342 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
343}
344
345PyDoc_STRVAR(extend_doc,static char extend_doc[] = "Extend the right side of the deque with elements from the iterable"
346"Extend the right side of the deque with elements from the iterable")static char extend_doc[] = "Extend the right side of the deque with elements from the iterable";
347
348static PyObject *
349deque_extendleft(dequeobject *deque, PyObject *iterable)
350{
351 PyObject *it, *item;
352
353 /* Handle case where id(deque) == id(iterable) */
354 if ((PyObject *)deque == iterable) {
355 PyObject *result;
356 PyObject *s = PySequence_List(iterable);
357 if (s == NULL((void*)0))
358 return NULL((void*)0);
359 result = deque_extendleft(deque, s);
360 Py_DECREF(s)do { if (_Py_RefTotal-- , --((PyObject*)(s))->ob_refcnt !=
0) { if (((PyObject*)s)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 360, (PyObject *)(s)); } else
_Py_Dealloc((PyObject *)(s)); } while (0)
;
361 return result;
362 }
363
364 it = PyObject_GetIter(iterable);
365 if (it == NULL((void*)0))
366 return NULL((void*)0);
367
368 if (deque->maxlen == 0)
369 return consume_iterator(it);
370
371 while ((item = PyIter_Next(it)) != NULL((void*)0)) {
372 deque->state++;
373 if (deque->leftindex == 0) {
374 block *b = newblock(NULL((void*)0), deque->leftblock,
375 deque->len);
376 if (b == NULL((void*)0)) {
377 Py_DECREF(item)do { if (_Py_RefTotal-- , --((PyObject*)(item))->ob_refcnt
!= 0) { if (((PyObject*)item)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 377, (PyObject *)(item)); }
else _Py_Dealloc((PyObject *)(item)); } while (0)
;
378 Py_DECREF(it)do { if (_Py_RefTotal-- , --((PyObject*)(it))->ob_refcnt !=
0) { if (((PyObject*)it)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 378, (PyObject *)(it)); } else
_Py_Dealloc((PyObject *)(it)); } while (0)
;
379 return NULL((void*)0);
380 }
381 assert(deque->leftblock->leftlink == NULL)(__builtin_expect(!(deque->leftblock->leftlink == ((void
*)0)), 0) ? __assert_rtn(__func__, "./Modules/_collectionsmodule.c"
, 381, "deque->leftblock->leftlink == NULL") : (void)0)
;
382 deque->leftblock->leftlink = b;
383 deque->leftblock = b;
384 deque->leftindex = BLOCKLEN62;
385 }
386 deque->len++;
387 deque->leftindex--;
388 deque->leftblock->data[deque->leftindex] = item;
389 TRIM(deque, deque_pop)if (deque->maxlen != -1 && deque->len > deque
->maxlen) { PyObject *rv = deque_pop(deque, ((void*)0)); (
__builtin_expect(!(rv != ((void*)0) && deque->len <=
deque->maxlen), 0) ? __assert_rtn(__func__, "./Modules/_collectionsmodule.c"
, 389, "rv != NULL && deque->len <= deque->maxlen"
) : (void)0); do { if (_Py_RefTotal-- , --((PyObject*)(rv))->
ob_refcnt != 0) { if (((PyObject*)rv)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 389, (PyObject *)(rv)); } else
_Py_Dealloc((PyObject *)(rv)); } while (0); }
;
390 }
391 Py_DECREF(it)do { if (_Py_RefTotal-- , --((PyObject*)(it))->ob_refcnt !=
0) { if (((PyObject*)it)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 391, (PyObject *)(it)); } else
_Py_Dealloc((PyObject *)(it)); } while (0)
;
392 if (PyErr_Occurred())
393 return NULL((void*)0);
394 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
395}
396
397PyDoc_STRVAR(extendleft_doc,static char extendleft_doc[] = "Extend the left side of the deque with elements from the iterable"
398"Extend the left side of the deque with elements from the iterable")static char extendleft_doc[] = "Extend the left side of the deque with elements from the iterable";
399
400static PyObject *
401deque_inplace_concat(dequeobject *deque, PyObject *other)
402{
403 PyObject *result;
404
405 result = deque_extend(deque, other);
406 if (result == NULL((void*)0))
407 return result;
408 Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt
!= 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 408, (PyObject *)(result))
; } else _Py_Dealloc((PyObject *)(result)); } while (0)
;
409 Py_INCREF(deque)( _Py_RefTotal++ , ((PyObject*)(deque))->ob_refcnt++);
410 return (PyObject *)deque;
411}
412
413static int
414_deque_rotate(dequeobject *deque, Py_ssize_t n)
415{
416 Py_ssize_t i, len=deque->len, halflen=(len+1)>>1;
417 PyObject *item, *rv;
418
419 if (len == 0)
420 return 0;
421 if (n > halflen || n < -halflen) {
422 n %= len;
423 if (n > halflen)
424 n -= len;
425 else if (n < -halflen)
426 n += len;
427 }
428
429 for (i=0 ; i<n ; i++) {
430 item = deque_pop(deque, NULL((void*)0));
431 assert (item != NULL)(__builtin_expect(!(item != ((void*)0)), 0) ? __assert_rtn(__func__
, "./Modules/_collectionsmodule.c", 431, "item != NULL") : (void
)0)
;
432 rv = deque_appendleft(deque, item);
433 Py_DECREF(item)do { if (_Py_RefTotal-- , --((PyObject*)(item))->ob_refcnt
!= 0) { if (((PyObject*)item)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 433, (PyObject *)(item)); }
else _Py_Dealloc((PyObject *)(item)); } while (0)
;
434 if (rv == NULL((void*)0))
435 return -1;
436 Py_DECREF(rv)do { if (_Py_RefTotal-- , --((PyObject*)(rv))->ob_refcnt !=
0) { if (((PyObject*)rv)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 436, (PyObject *)(rv)); } else
_Py_Dealloc((PyObject *)(rv)); } while (0)
;
437 }
438 for (i=0 ; i>n ; i--) {
439 item = deque_popleft(deque, NULL((void*)0));
440 assert (item != NULL)(__builtin_expect(!(item != ((void*)0)), 0) ? __assert_rtn(__func__
, "./Modules/_collectionsmodule.c", 440, "item != NULL") : (void
)0)
;
441 rv = deque_append(deque, item);
442 Py_DECREF(item)do { if (_Py_RefTotal-- , --((PyObject*)(item))->ob_refcnt
!= 0) { if (((PyObject*)item)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 442, (PyObject *)(item)); }
else _Py_Dealloc((PyObject *)(item)); } while (0)
;
443 if (rv == NULL((void*)0))
444 return -1;
445 Py_DECREF(rv)do { if (_Py_RefTotal-- , --((PyObject*)(rv))->ob_refcnt !=
0) { if (((PyObject*)rv)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 445, (PyObject *)(rv)); } else
_Py_Dealloc((PyObject *)(rv)); } while (0)
;
446 }
447 return 0;
448}
449
450static PyObject *
451deque_rotate(dequeobject *deque, PyObject *args)
452{
453 Py_ssize_t n=1;
454
455 if (!PyArg_ParseTuple(args, "|n:rotate", &n))
456 return NULL((void*)0);
457 if (_deque_rotate(deque, n) == 0)
458 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
459 return NULL((void*)0);
460}
461
462PyDoc_STRVAR(rotate_doc,static char rotate_doc[] = "Rotate the deque n steps to the right (default n=1). If n is negative, rotates left."
463"Rotate the deque n steps to the right (default n=1). If n is negative, rotates left.")static char rotate_doc[] = "Rotate the deque n steps to the right (default n=1). If n is negative, rotates left.";
464
465static PyObject *
466deque_reverse(dequeobject *deque, PyObject *unused)
467{
468 block *leftblock = deque->leftblock;
469 block *rightblock = deque->rightblock;
470 Py_ssize_t leftindex = deque->leftindex;
471 Py_ssize_t rightindex = deque->rightindex;
472 Py_ssize_t n = (deque->len)/2;
473 Py_ssize_t i;
474 PyObject *tmp;
475
476 for (i=0 ; i<n ; i++) {
477 /* Validate that pointers haven't met in the middle */
478 assert(leftblock != rightblock || leftindex < rightindex)(__builtin_expect(!(leftblock != rightblock || leftindex <
rightindex), 0) ? __assert_rtn(__func__, "./Modules/_collectionsmodule.c"
, 478, "leftblock != rightblock || leftindex < rightindex"
) : (void)0)
;
479
480 /* Swap */
481 tmp = leftblock->data[leftindex];
482 leftblock->data[leftindex] = rightblock->data[rightindex];
483 rightblock->data[rightindex] = tmp;
484
485 /* Advance left block/index pair */
486 leftindex++;
487 if (leftindex == BLOCKLEN62) {
488 if (leftblock->rightlink == NULL((void*)0))
489 break;
490 leftblock = leftblock->rightlink;
491 leftindex = 0;
492 }
493
494 /* Step backwards with the right block/index pair */
495 rightindex--;
496 if (rightindex == -1) {
497 if (rightblock->leftlink == NULL((void*)0))
498 break;
499 rightblock = rightblock->leftlink;
500 rightindex = BLOCKLEN62 - 1;
501 }
502 }
503 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
504}
505
506PyDoc_STRVAR(reverse_doc,static char reverse_doc[] = "D.reverse() -- reverse *IN PLACE*"
507"D.reverse() -- reverse *IN PLACE*")static char reverse_doc[] = "D.reverse() -- reverse *IN PLACE*";
508
509static PyObject *
510deque_count(dequeobject *deque, PyObject *v)
511{
512 block *leftblock = deque->leftblock;
513 Py_ssize_t leftindex = deque->leftindex;
514 Py_ssize_t n = deque->len;
515 Py_ssize_t i;
516 Py_ssize_t count = 0;
517 PyObject *item;
518 long start_state = deque->state;
519 int cmp;
520
521 for (i=0 ; i<n ; i++) {
522 item = leftblock->data[leftindex];
523 cmp = PyObject_RichCompareBool(item, v, Py_EQ2);
524 if (cmp > 0)
525 count++;
526 else if (cmp < 0)
527 return NULL((void*)0);
528
529 if (start_state != deque->state) {
530 PyErr_SetString(PyExc_RuntimeError,
531 "deque mutated during iteration");
532 return NULL((void*)0);
533 }
534
535 /* Advance left block/index pair */
536 leftindex++;
537 if (leftindex == BLOCKLEN62) {
538 if (leftblock->rightlink == NULL((void*)0)) /* can occur when i==n-1 */
539 break;
540 leftblock = leftblock->rightlink;
541 leftindex = 0;
542 }
543 }
544 return PyLong_FromSsize_t(count);
545}
546
547PyDoc_STRVAR(count_doc,static char count_doc[] = "D.count(value) -> integer -- return number of occurrences of value"
548"D.count(value) -> integer -- return number of occurrences of value")static char count_doc[] = "D.count(value) -> integer -- return number of occurrences of value";
549
550static Py_ssize_t
551deque_len(dequeobject *deque)
552{
553 return deque->len;
554}
555
556static PyObject *
557deque_remove(dequeobject *deque, PyObject *value)
558{
559 Py_ssize_t i, n=deque->len;
560
561 for (i=0 ; i<n ; i++) {
562 PyObject *item = deque->leftblock->data[deque->leftindex];
563 int cmp = PyObject_RichCompareBool(item, value, Py_EQ2);
564
565 if (deque->len != n) {
566 PyErr_SetString(PyExc_IndexError,
567 "deque mutated during remove().");
568 return NULL((void*)0);
569 }
570 if (cmp > 0) {
571 PyObject *tgt = deque_popleft(deque, NULL((void*)0));
572 assert (tgt != NULL)(__builtin_expect(!(tgt != ((void*)0)), 0) ? __assert_rtn(__func__
, "./Modules/_collectionsmodule.c", 572, "tgt != NULL") : (void
)0)
;
573 Py_DECREF(tgt)do { if (_Py_RefTotal-- , --((PyObject*)(tgt))->ob_refcnt !=
0) { if (((PyObject*)tgt)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 573, (PyObject *)(tgt)); }
else _Py_Dealloc((PyObject *)(tgt)); } while (0)
;
574 if (_deque_rotate(deque, i) == -1)
575 return NULL((void*)0);
576 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
577 }
578 else if (cmp < 0) {
579 _deque_rotate(deque, i);
580 return NULL((void*)0);
581 }
582 _deque_rotate(deque, -1);
583 }
584 PyErr_SetString(PyExc_ValueError, "deque.remove(x): x not in deque");
585 return NULL((void*)0);
586}
587
588PyDoc_STRVAR(remove_doc,static char remove_doc[] = "D.remove(value) -- remove first occurrence of value."
589"D.remove(value) -- remove first occurrence of value.")static char remove_doc[] = "D.remove(value) -- remove first occurrence of value.";
590
591static int
592deque_clear(dequeobject *deque)
593{
594 PyObject *item;
595
596 while (deque->len) {
597 item = deque_pop(deque, NULL((void*)0));
598 assert (item != NULL)(__builtin_expect(!(item != ((void*)0)), 0) ? __assert_rtn(__func__
, "./Modules/_collectionsmodule.c", 598, "item != NULL") : (void
)0)
;
599 Py_DECREF(item)do { if (_Py_RefTotal-- , --((PyObject*)(item))->ob_refcnt
!= 0) { if (((PyObject*)item)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 599, (PyObject *)(item)); }
else _Py_Dealloc((PyObject *)(item)); } while (0)
;
600 }
601 assert(deque->leftblock == deque->rightblock &&(__builtin_expect(!(deque->leftblock == deque->rightblock
&& deque->leftindex - 1 == deque->rightindex &&
deque->len == 0), 0) ? __assert_rtn(__func__, "./Modules/_collectionsmodule.c"
, 603, "deque->leftblock == deque->rightblock && deque->leftindex - 1 == deque->rightindex && deque->len == 0"
) : (void)0)
602 deque->leftindex - 1 == deque->rightindex &&(__builtin_expect(!(deque->leftblock == deque->rightblock
&& deque->leftindex - 1 == deque->rightindex &&
deque->len == 0), 0) ? __assert_rtn(__func__, "./Modules/_collectionsmodule.c"
, 603, "deque->leftblock == deque->rightblock && deque->leftindex - 1 == deque->rightindex && deque->len == 0"
) : (void)0)
603 deque->len == 0)(__builtin_expect(!(deque->leftblock == deque->rightblock
&& deque->leftindex - 1 == deque->rightindex &&
deque->len == 0), 0) ? __assert_rtn(__func__, "./Modules/_collectionsmodule.c"
, 603, "deque->leftblock == deque->rightblock && deque->leftindex - 1 == deque->rightindex && deque->len == 0"
) : (void)0)
;
604 return 0;
605}
606
607static PyObject *
608deque_item(dequeobject *deque, Py_ssize_t i)
609{
610 block *b;
611 PyObject *item;
612 Py_ssize_t n, index=i;
613
614 if (i < 0 || i >= deque->len) {
615 PyErr_SetString(PyExc_IndexError,
616 "deque index out of range");
617 return NULL((void*)0);
618 }
619
620 if (i == 0) {
621 i = deque->leftindex;
622 b = deque->leftblock;
623 } else if (i == deque->len - 1) {
624 i = deque->rightindex;
625 b = deque->rightblock;
626 } else {
627 i += deque->leftindex;
628 n = i / BLOCKLEN62;
629 i %= BLOCKLEN62;
630 if (index < (deque->len >> 1)) {
631 b = deque->leftblock;
632 while (n--)
633 b = b->rightlink;
634 } else {
635 n = (deque->leftindex + deque->len - 1) / BLOCKLEN62 - n;
636 b = deque->rightblock;
637 while (n--)
638 b = b->leftlink;
639 }
640 }
641 item = b->data[i];
642 Py_INCREF(item)( _Py_RefTotal++ , ((PyObject*)(item))->ob_refcnt++);
643 return item;
644}
645
646/* delitem() implemented in terms of rotate for simplicity and reasonable
647 performance near the end points. If for some reason this method becomes
648 popular, it is not hard to re-implement this using direct data movement
649 (similar to code in list slice assignment) and achieve a two or threefold
650 performance boost.
651*/
652
653static int
654deque_del_item(dequeobject *deque, Py_ssize_t i)
655{
656 PyObject *item;
657
658 assert (i >= 0 && i < deque->len)(__builtin_expect(!(i >= 0 && i < deque->len
), 0) ? __assert_rtn(__func__, "./Modules/_collectionsmodule.c"
, 658, "i >= 0 && i < deque->len") : (void)0
)
;
659 if (_deque_rotate(deque, -i) == -1)
660 return -1;
661
662 item = deque_popleft(deque, NULL((void*)0));
663 assert (item != NULL)(__builtin_expect(!(item != ((void*)0)), 0) ? __assert_rtn(__func__
, "./Modules/_collectionsmodule.c", 663, "item != NULL") : (void
)0)
;
664 Py_DECREF(item)do { if (_Py_RefTotal-- , --((PyObject*)(item))->ob_refcnt
!= 0) { if (((PyObject*)item)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 664, (PyObject *)(item)); }
else _Py_Dealloc((PyObject *)(item)); } while (0)
;
665
666 return _deque_rotate(deque, i);
667}
668
669static int
670deque_ass_item(dequeobject *deque, Py_ssize_t i, PyObject *v)
671{
672 PyObject *old_value;
673 block *b;
674 Py_ssize_t n, len=deque->len, halflen=(len+1)>>1, index=i;
675
676 if (i < 0 || i >= len) {
677 PyErr_SetString(PyExc_IndexError,
678 "deque index out of range");
679 return -1;
680 }
681 if (v == NULL((void*)0))
682 return deque_del_item(deque, i);
683
684 i += deque->leftindex;
685 n = i / BLOCKLEN62;
686 i %= BLOCKLEN62;
687 if (index <= halflen) {
688 b = deque->leftblock;
689 while (n--)
690 b = b->rightlink;
691 } else {
692 n = (deque->leftindex + len - 1) / BLOCKLEN62 - n;
693 b = deque->rightblock;
694 while (n--)
695 b = b->leftlink;
696 }
697 Py_INCREF(v)( _Py_RefTotal++ , ((PyObject*)(v))->ob_refcnt++);
698 old_value = b->data[i];
699 b->data[i] = v;
700 Py_DECREF(old_value)do { if (_Py_RefTotal-- , --((PyObject*)(old_value))->ob_refcnt
!= 0) { if (((PyObject*)old_value)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 700, (PyObject *)(old_value
)); } else _Py_Dealloc((PyObject *)(old_value)); } while (0)
;
701 return 0;
702}
703
704static PyObject *
705deque_clearmethod(dequeobject *deque)
706{
707 int rv;
708
709 rv = deque_clear(deque);
710 assert (rv != -1)(__builtin_expect(!(rv != -1), 0) ? __assert_rtn(__func__, "./Modules/_collectionsmodule.c"
, 710, "rv != -1") : (void)0)
;
711 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
712}
713
714PyDoc_STRVAR(clear_doc, "Remove all elements from the deque.")static char clear_doc[] = "Remove all elements from the deque.";
715
716static void
717deque_dealloc(dequeobject *deque)
718{
719 PyObject_GC_UnTrack(deque);
720 if (deque->weakreflist != NULL((void*)0))
721 PyObject_ClearWeakRefs((PyObject *) deque);
722 if (deque->leftblock != NULL((void*)0)) {
723 deque_clear(deque);
724 assert(deque->leftblock != NULL)(__builtin_expect(!(deque->leftblock != ((void*)0)), 0) ? __assert_rtn
(__func__, "./Modules/_collectionsmodule.c", 724, "deque->leftblock != NULL"
) : (void)0)
;
725 freeblock(deque->leftblock);
726 }
727 deque->leftblock = NULL((void*)0);
728 deque->rightblock = NULL((void*)0);
729 Py_TYPE(deque)(((PyObject*)(deque))->ob_type)->tp_free(deque);
730}
731
732static int
733deque_traverse(dequeobject *deque, visitproc visit, void *arg)
734{
735 block *b;
736 PyObject *item;
737 Py_ssize_t index;
738 Py_ssize_t indexlo = deque->leftindex;
739
740 for (b = deque->leftblock; b != NULL((void*)0); b = b->rightlink) {
741 const Py_ssize_t indexhi = b == deque->rightblock ?
742 deque->rightindex :
743 BLOCKLEN62 - 1;
744
745 for (index = indexlo; index <= indexhi; ++index) {
746 item = b->data[index];
747 Py_VISIT(item)do { if (item) { int vret = visit((PyObject *)(item), arg); if
(vret) return vret; } } while (0)
;
748 }
749 indexlo = 0;
750 }
751 return 0;
752}
753
754static PyObject *
755deque_copy(PyObject *deque)
756{
757 if (((dequeobject *)deque)->maxlen == -1)
758 return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)(((PyObject*)(deque))->ob_type)), "O", deque, NULL((void*)0));
759 else
760 return PyObject_CallFunction((PyObject *)(Py_TYPE(deque)(((PyObject*)(deque))->ob_type)), "Oi",
761 deque, ((dequeobject *)deque)->maxlen, NULL((void*)0));
762}
763
764PyDoc_STRVAR(copy_doc, "Return a shallow copy of a deque.")static char copy_doc[] = "Return a shallow copy of a deque.";
765
766static PyObject *
767deque_reduce(dequeobject *deque)
768{
769 PyObject *dict, *result, *aslist;
770
771 dict = PyObject_GetAttrString((PyObject *)deque, "__dict__");
772 if (dict == NULL((void*)0))
773 PyErr_Clear();
774 aslist = PySequence_List((PyObject *)deque);
775 if (aslist == NULL((void*)0)) {
776 Py_XDECREF(dict)do { if ((dict) == ((void*)0)) ; else do { if (_Py_RefTotal--
, --((PyObject*)(dict))->ob_refcnt != 0) { if (((PyObject
*)dict)->ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_collectionsmodule.c"
, 776, (PyObject *)(dict)); } else _Py_Dealloc((PyObject *)(dict
)); } while (0); } while (0)
;
777 return NULL((void*)0);
778 }
779 if (dict == NULL((void*)0)) {
780 if (deque->maxlen == -1)
781 result = Py_BuildValue("O(O)", Py_TYPE(deque)(((PyObject*)(deque))->ob_type), aslist);
782 else
783 result = Py_BuildValue("O(On)", Py_TYPE(deque)(((PyObject*)(deque))->ob_type), aslist, deque->maxlen);
784 } else {
785 if (deque->maxlen == -1)
786 result = Py_BuildValue("O(OO)O", Py_TYPE(deque)(((PyObject*)(deque))->ob_type), aslist, Py_None(&_Py_NoneStruct), dict);
787 else
788 result = Py_BuildValue("O(On)O", Py_TYPE(deque)(((PyObject*)(deque))->ob_type), aslist, deque->maxlen, dict);
789 }
790 Py_XDECREF(dict)do { if ((dict) == ((void*)0)) ; else do { if (_Py_RefTotal--
, --((PyObject*)(dict))->ob_refcnt != 0) { if (((PyObject
*)dict)->ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_collectionsmodule.c"
, 790, (PyObject *)(dict)); } else _Py_Dealloc((PyObject *)(dict
)); } while (0); } while (0)
;
791 Py_DECREF(aslist)do { if (_Py_RefTotal-- , --((PyObject*)(aslist))->ob_refcnt
!= 0) { if (((PyObject*)aslist)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 791, (PyObject *)(aslist))
; } else _Py_Dealloc((PyObject *)(aslist)); } while (0)
;
792 return result;
793}
794
795PyDoc_STRVAR(reduce_doc, "Return state information for pickling.")static char reduce_doc[] = "Return state information for pickling.";
796
797static PyObject *
798deque_repr(PyObject *deque)
799{
800 PyObject *aslist, *result;
801 int i;
802
803 i = Py_ReprEnter(deque);
804 if (i != 0) {
805 if (i < 0)
806 return NULL((void*)0);
807 return PyUnicode_FromStringPyUnicodeUCS2_FromString("[...]");
808 }
809
810 aslist = PySequence_List(deque);
811 if (aslist == NULL((void*)0)) {
812 Py_ReprLeave(deque);
813 return NULL((void*)0);
814 }
815 if (((dequeobject *)deque)->maxlen != -1)
816
817 result = PyUnicode_FromFormatPyUnicodeUCS2_FromFormat("deque(%R, maxlen=%zd)",
818 aslist, ((dequeobject *)deque)->maxlen);
819 else
820 result = PyUnicode_FromFormatPyUnicodeUCS2_FromFormat("deque(%R)", aslist);
821 Py_DECREF(aslist)do { if (_Py_RefTotal-- , --((PyObject*)(aslist))->ob_refcnt
!= 0) { if (((PyObject*)aslist)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 821, (PyObject *)(aslist))
; } else _Py_Dealloc((PyObject *)(aslist)); } while (0)
;
822 Py_ReprLeave(deque);
823 return result;
824}
825
826static PyObject *
827deque_richcompare(PyObject *v, PyObject *w, int op)
828{
829 PyObject *it1=NULL((void*)0), *it2=NULL((void*)0), *x, *y;
830 Py_ssize_t vs, ws;
831 int b, cmp=-1;
832
833 if (!PyObject_TypeCheck(v, &deque_type)((((PyObject*)(v))->ob_type) == (&deque_type) || PyType_IsSubtype
((((PyObject*)(v))->ob_type), (&deque_type)))
||
834 !PyObject_TypeCheck(w, &deque_type)((((PyObject*)(w))->ob_type) == (&deque_type) || PyType_IsSubtype
((((PyObject*)(w))->ob_type), (&deque_type)))
) {
835 Py_INCREF(Py_NotImplemented)( _Py_RefTotal++ , ((PyObject*)((&_Py_NotImplementedStruct
)))->ob_refcnt++)
;
836 return Py_NotImplemented(&_Py_NotImplementedStruct);
837 }
838
839 /* Shortcuts */
840 vs = ((dequeobject *)v)->len;
841 ws = ((dequeobject *)w)->len;
842 if (op == Py_EQ2) {
843 if (v == w)
844 Py_RETURN_TRUEreturn ( _Py_RefTotal++ , ((PyObject*)(((PyObject *) &_Py_TrueStruct
)))->ob_refcnt++), ((PyObject *) &_Py_TrueStruct)
;
845 if (vs != ws)
846 Py_RETURN_FALSEreturn ( _Py_RefTotal++ , ((PyObject*)(((PyObject *) &_Py_FalseStruct
)))->ob_refcnt++), ((PyObject *) &_Py_FalseStruct)
;
847 }
848 if (op == Py_NE3) {
849 if (v == w)
850 Py_RETURN_FALSEreturn ( _Py_RefTotal++ , ((PyObject*)(((PyObject *) &_Py_FalseStruct
)))->ob_refcnt++), ((PyObject *) &_Py_FalseStruct)
;
851 if (vs != ws)
852 Py_RETURN_TRUEreturn ( _Py_RefTotal++ , ((PyObject*)(((PyObject *) &_Py_TrueStruct
)))->ob_refcnt++), ((PyObject *) &_Py_TrueStruct)
;
853 }
854
855 /* Search for the first index where items are different */
856 it1 = PyObject_GetIter(v);
857 if (it1 == NULL((void*)0))
858 goto done;
859 it2 = PyObject_GetIter(w);
860 if (it2 == NULL((void*)0))
861 goto done;
862 for (;;) {
863 x = PyIter_Next(it1);
864 if (x == NULL((void*)0) && PyErr_Occurred())
865 goto done;
866 y = PyIter_Next(it2);
867 if (x == NULL((void*)0) || y == NULL((void*)0))
868 break;
869 b = PyObject_RichCompareBool(x, y, Py_EQ2);
870 if (b == 0) {
871 cmp = PyObject_RichCompareBool(x, y, op);
872 Py_DECREF(x)do { if (_Py_RefTotal-- , --((PyObject*)(x))->ob_refcnt !=
0) { if (((PyObject*)x)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 872, (PyObject *)(x)); } else
_Py_Dealloc((PyObject *)(x)); } while (0)
;
873 Py_DECREF(y)do { if (_Py_RefTotal-- , --((PyObject*)(y))->ob_refcnt !=
0) { if (((PyObject*)y)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 873, (PyObject *)(y)); } else
_Py_Dealloc((PyObject *)(y)); } while (0)
;
874 goto done;
875 }
876 Py_DECREF(x)do { if (_Py_RefTotal-- , --((PyObject*)(x))->ob_refcnt !=
0) { if (((PyObject*)x)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 876, (PyObject *)(x)); } else
_Py_Dealloc((PyObject *)(x)); } while (0)
;
877 Py_DECREF(y)do { if (_Py_RefTotal-- , --((PyObject*)(y))->ob_refcnt !=
0) { if (((PyObject*)y)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 877, (PyObject *)(y)); } else
_Py_Dealloc((PyObject *)(y)); } while (0)
;
878 if (b == -1)
879 goto done;
880 }
881 /* We reached the end of one deque or both */
882 Py_XDECREF(x)do { if ((x) == ((void*)0)) ; else do { if (_Py_RefTotal-- , --
((PyObject*)(x))->ob_refcnt != 0) { if (((PyObject*)x)->
ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_collectionsmodule.c"
, 882, (PyObject *)(x)); } else _Py_Dealloc((PyObject *)(x));
} while (0); } while (0)
;
883 Py_XDECREF(y)do { if ((y) == ((void*)0)) ; else do { if (_Py_RefTotal-- , --
((PyObject*)(y))->ob_refcnt != 0) { if (((PyObject*)y)->
ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_collectionsmodule.c"
, 883, (PyObject *)(y)); } else _Py_Dealloc((PyObject *)(y));
} while (0); } while (0)
;
884 if (PyErr_Occurred())
885 goto done;
886 switch (op) {
887 case Py_LT0: cmp = y != NULL((void*)0); break; /* if w was longer */
888 case Py_LE1: cmp = x == NULL((void*)0); break; /* if v was not longer */
889 case Py_EQ2: cmp = x == y; break; /* if we reached the end of both */
890 case Py_NE3: cmp = x != y; break; /* if one deque continues */
891 case Py_GT4: cmp = x != NULL((void*)0); break; /* if v was longer */
892 case Py_GE5: cmp = y == NULL((void*)0); break; /* if w was not longer */
893 }
894
895done:
896 Py_XDECREF(it1)do { if ((it1) == ((void*)0)) ; else do { if (_Py_RefTotal-- ,
--((PyObject*)(it1))->ob_refcnt != 0) { if (((PyObject*)it1
)->ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_collectionsmodule.c"
, 896, (PyObject *)(it1)); } else _Py_Dealloc((PyObject *)(it1
)); } while (0); } while (0)
;
897 Py_XDECREF(it2)do { if ((it2) == ((void*)0)) ; else do { if (_Py_RefTotal-- ,
--((PyObject*)(it2))->ob_refcnt != 0) { if (((PyObject*)it2
)->ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_collectionsmodule.c"
, 897, (PyObject *)(it2)); } else _Py_Dealloc((PyObject *)(it2
)); } while (0); } while (0)
;
898 if (cmp == 1)
899 Py_RETURN_TRUEreturn ( _Py_RefTotal++ , ((PyObject*)(((PyObject *) &_Py_TrueStruct
)))->ob_refcnt++), ((PyObject *) &_Py_TrueStruct)
;
900 if (cmp == 0)
901 Py_RETURN_FALSEreturn ( _Py_RefTotal++ , ((PyObject*)(((PyObject *) &_Py_FalseStruct
)))->ob_refcnt++), ((PyObject *) &_Py_FalseStruct)
;
902 return NULL((void*)0);
903}
904
905static int
906deque_init(dequeobject *deque, PyObject *args, PyObject *kwdargs)
907{
908 PyObject *iterable = NULL((void*)0);
909 PyObject *maxlenobj = NULL((void*)0);
910 Py_ssize_t maxlen = -1;
911 char *kwlist[] = {"iterable", "maxlen", 0};
912
913 if (!PyArg_ParseTupleAndKeywords(args, kwdargs, "|OO:deque", kwlist, &iterable, &maxlenobj))
914 return -1;
915 if (maxlenobj != NULL((void*)0) && maxlenobj != Py_None(&_Py_NoneStruct)) {
916 maxlen = PyLong_AsSsize_t(maxlenobj);
917 if (maxlen == -1 && PyErr_Occurred())
918 return -1;
919 if (maxlen < 0) {
920 PyErr_SetString(PyExc_ValueError, "maxlen must be non-negative");
921 return -1;
922 }
923 }
924 deque->maxlen = maxlen;
925 deque_clear(deque);
926 if (iterable != NULL((void*)0)) {
927 PyObject *rv = deque_extend(deque, iterable);
928 if (rv == NULL((void*)0))
929 return -1;
930 Py_DECREF(rv)do { if (_Py_RefTotal-- , --((PyObject*)(rv))->ob_refcnt !=
0) { if (((PyObject*)rv)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 930, (PyObject *)(rv)); } else
_Py_Dealloc((PyObject *)(rv)); } while (0)
;
931 }
932 return 0;
933}
934
935static PyObject *
936deque_get_maxlen(dequeobject *deque)
937{
938 if (deque->maxlen == -1)
939 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
940 return PyLong_FromSsize_t(deque->maxlen);
941}
942
943static PyGetSetDef deque_getset[] = {
944 {"maxlen", (getter)deque_get_maxlen, (setter)NULL((void*)0),
945 "maximum size of a deque or None if unbounded"},
946 {0}
947};
948
949static PySequenceMethods deque_as_sequence = {
950 (lenfunc)deque_len, /* sq_length */
951 0, /* sq_concat */
952 0, /* sq_repeat */
953 (ssizeargfunc)deque_item, /* sq_item */
954 0, /* sq_slice */
955 (ssizeobjargproc)deque_ass_item, /* sq_ass_item */
956 0, /* sq_ass_slice */
957 0, /* sq_contains */
958 (binaryfunc)deque_inplace_concat, /* sq_inplace_concat */
959 0, /* sq_inplace_repeat */
960
961};
962
963/* deque object ********************************************************/
964
965static PyObject *deque_iter(dequeobject *deque);
966static PyObject *deque_reviter(dequeobject *deque);
967PyDoc_STRVAR(reversed_doc,static char reversed_doc[] = "D.__reversed__() -- return a reverse iterator over the deque"
968 "D.__reversed__() -- return a reverse iterator over the deque")static char reversed_doc[] = "D.__reversed__() -- return a reverse iterator over the deque";
969
970static PyMethodDef deque_methods[] = {
971 {"append", (PyCFunction)deque_append,
972 METH_O0x0008, append_doc},
973 {"appendleft", (PyCFunction)deque_appendleft,
974 METH_O0x0008, appendleft_doc},
975 {"clear", (PyCFunction)deque_clearmethod,
976 METH_NOARGS0x0004, clear_doc},
977 {"__copy__", (PyCFunction)deque_copy,
978 METH_NOARGS0x0004, copy_doc},
979 {"count", (PyCFunction)deque_count,
980 METH_O0x0008, count_doc},
981 {"extend", (PyCFunction)deque_extend,
982 METH_O0x0008, extend_doc},
983 {"extendleft", (PyCFunction)deque_extendleft,
984 METH_O0x0008, extendleft_doc},
985 {"pop", (PyCFunction)deque_pop,
986 METH_NOARGS0x0004, pop_doc},
987 {"popleft", (PyCFunction)deque_popleft,
988 METH_NOARGS0x0004, popleft_doc},
989 {"__reduce__", (PyCFunction)deque_reduce,
990 METH_NOARGS0x0004, reduce_doc},
991 {"remove", (PyCFunction)deque_remove,
992 METH_O0x0008, remove_doc},
993 {"__reversed__", (PyCFunction)deque_reviter,
994 METH_NOARGS0x0004, reversed_doc},
995 {"reverse", (PyCFunction)deque_reverse,
996 METH_NOARGS0x0004, reverse_doc},
997 {"rotate", (PyCFunction)deque_rotate,
998 METH_VARARGS0x0001, rotate_doc},
999 {NULL((void*)0), NULL((void*)0)} /* sentinel */
1000};
1001
1002PyDoc_STRVAR(deque_doc,static char deque_doc[] = "deque(iterable[, maxlen]) --> deque object\n\nBuild an ordered collection accessible from endpoints only."
1003"deque(iterable[, maxlen]) --> deque object\n\static char deque_doc[] = "deque(iterable[, maxlen]) --> deque object\n\nBuild an ordered collection accessible from endpoints only."
1004\n\static char deque_doc[] = "deque(iterable[, maxlen]) --> deque object\n\nBuild an ordered collection accessible from endpoints only."
1005Build an ordered collection accessible from endpoints only.")static char deque_doc[] = "deque(iterable[, maxlen]) --> deque object\n\nBuild an ordered collection accessible from endpoints only.";
1006
1007static PyTypeObject deque_type = {
1008 PyVarObject_HEAD_INIT(NULL, 0){ { 0, 0, 1, ((void*)0) }, 0 },
1009 "collections.deque", /* tp_name */
1010 sizeof(dequeobject), /* tp_basicsize */
1011 0, /* tp_itemsize */
1012 /* methods */
1013 (destructor)deque_dealloc, /* tp_dealloc */
1014 0, /* tp_print */
1015 0, /* tp_getattr */
1016 0, /* tp_setattr */
1017 0, /* tp_reserved */
1018 deque_repr, /* tp_repr */
1019 0, /* tp_as_number */
1020 &deque_as_sequence, /* tp_as_sequence */
1021 0, /* tp_as_mapping */
1022 PyObject_HashNotImplemented, /* tp_hash */
1023 0, /* tp_call */
1024 0, /* tp_str */
1025 PyObject_GenericGetAttr, /* tp_getattro */
1026 0, /* tp_setattro */
1027 0, /* tp_as_buffer */
1028 Py_TPFLAGS_DEFAULT( 0 | (1L<<18) | 0) | Py_TPFLAGS_BASETYPE(1L<<10) | Py_TPFLAGS_HAVE_GC(1L<<14),
1029 /* tp_flags */
1030 deque_doc, /* tp_doc */
1031 (traverseproc)deque_traverse, /* tp_traverse */
1032 (inquiry)deque_clear, /* tp_clear */
1033 (richcmpfunc)deque_richcompare, /* tp_richcompare */
1034 offsetof(dequeobject, weakreflist)__builtin_offsetof(dequeobject, weakreflist), /* tp_weaklistoffset*/
1035 (getiterfunc)deque_iter, /* tp_iter */
1036 0, /* tp_iternext */
1037 deque_methods, /* tp_methods */
1038 0, /* tp_members */
1039 deque_getset, /* tp_getset */
1040 0, /* tp_base */
1041 0, /* tp_dict */
1042 0, /* tp_descr_get */
1043 0, /* tp_descr_set */
1044 0, /* tp_dictoffset */
1045 (initproc)deque_init, /* tp_init */
1046 PyType_GenericAlloc, /* tp_alloc */
1047 deque_new, /* tp_new */
1048 PyObject_GC_Del, /* tp_free */
1049};
1050
1051/*********************** Deque Iterator **************************/
1052
1053typedef struct {
1054 PyObject_HEADPyObject ob_base;
1055 Py_ssize_t index;
1056 block *b;
1057 dequeobject *deque;
1058 long state; /* state when the iterator is created */
1059 Py_ssize_t counter; /* number of items remaining for iteration */
1060} dequeiterobject;
1061
1062static PyTypeObject dequeiter_type;
1063
1064static PyObject *
1065deque_iter(dequeobject *deque)
1066{
1067 dequeiterobject *it;
1068
1069 it = PyObject_GC_New(dequeiterobject, &dequeiter_type)( (dequeiterobject *) _PyObject_GC_New(&dequeiter_type) );
1070 if (it == NULL((void*)0))
1071 return NULL((void*)0);
1072 it->b = deque->leftblock;
1073 it->index = deque->leftindex;
1074 Py_INCREF(deque)( _Py_RefTotal++ , ((PyObject*)(deque))->ob_refcnt++);
1075 it->deque = deque;
1076 it->state = deque->state;
1077 it->counter = deque->len;
1078 PyObject_GC_Track(it);
1079 return (PyObject *)it;
1080}
1081
1082static int
1083dequeiter_traverse(dequeiterobject *dio, visitproc visit, void *arg)
1084{
1085 Py_VISIT(dio->deque)do { if (dio->deque) { int vret = visit((PyObject *)(dio->
deque), arg); if (vret) return vret; } } while (0)
;
1086 return 0;
1087}
1088
1089static void
1090dequeiter_dealloc(dequeiterobject *dio)
1091{
1092 Py_XDECREF(dio->deque)do { if ((dio->deque) == ((void*)0)) ; else do { if (_Py_RefTotal
-- , --((PyObject*)(dio->deque))->ob_refcnt != 0) { if (
((PyObject*)dio->deque)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 1092, (PyObject *)(dio->
deque)); } else _Py_Dealloc((PyObject *)(dio->deque)); } while
(0); } while (0)
;
1093 PyObject_GC_Del(dio);
1094}
1095
1096static PyObject *
1097dequeiter_next(dequeiterobject *it)
1098{
1099 PyObject *item;
1100
1101 if (it->deque->state != it->state) {
1102 it->counter = 0;
1103 PyErr_SetString(PyExc_RuntimeError,
1104 "deque mutated during iteration");
1105 return NULL((void*)0);
1106 }
1107 if (it->counter == 0)
1108 return NULL((void*)0);
1109 assert (!(it->b == it->deque->rightblock &&(__builtin_expect(!(!(it->b == it->deque->rightblock
&& it->index > it->deque->rightindex)), 0
) ? __assert_rtn(__func__, "./Modules/_collectionsmodule.c", 1110
, "!(it->b == it->deque->rightblock && it->index > it->deque->rightindex)"
) : (void)0)
1110 it->index > it->deque->rightindex))(__builtin_expect(!(!(it->b == it->deque->rightblock
&& it->index > it->deque->rightindex)), 0
) ? __assert_rtn(__func__, "./Modules/_collectionsmodule.c", 1110
, "!(it->b == it->deque->rightblock && it->index > it->deque->rightindex)"
) : (void)0)
;
1111
1112 item = it->b->data[it->index];
1113 it->index++;
1114 it->counter--;
1115 if (it->index == BLOCKLEN62 && it->counter > 0) {
1116 assert (it->b->rightlink != NULL)(__builtin_expect(!(it->b->rightlink != ((void*)0)), 0)
? __assert_rtn(__func__, "./Modules/_collectionsmodule.c", 1116
, "it->b->rightlink != NULL") : (void)0)
;
1117 it->b = it->b->rightlink;
1118 it->index = 0;
1119 }
1120 Py_INCREF(item)( _Py_RefTotal++ , ((PyObject*)(item))->ob_refcnt++);
1121 return item;
1122}
1123
1124static PyObject *
1125dequeiter_len(dequeiterobject *it)
1126{
1127 return PyLong_FromSsize_t(it->counter);
1128}
1129
1130PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).")static char length_hint_doc[] = "Private method returning an estimate of len(list(it)).";
1131
1132static PyMethodDef dequeiter_methods[] = {
1133 {"__length_hint__", (PyCFunction)dequeiter_len, METH_NOARGS0x0004, length_hint_doc},
1134 {NULL((void*)0), NULL((void*)0)} /* sentinel */
1135};
1136
1137static PyTypeObject dequeiter_type = {
1138 PyVarObject_HEAD_INIT(NULL, 0){ { 0, 0, 1, ((void*)0) }, 0 },
1139 "deque_iterator", /* tp_name */
1140 sizeof(dequeiterobject), /* tp_basicsize */
1141 0, /* tp_itemsize */
1142 /* methods */
1143 (destructor)dequeiter_dealloc, /* tp_dealloc */
1144 0, /* tp_print */
1145 0, /* tp_getattr */
1146 0, /* tp_setattr */
1147 0, /* tp_reserved */
1148 0, /* tp_repr */
1149 0, /* tp_as_number */
1150 0, /* tp_as_sequence */
1151 0, /* tp_as_mapping */
1152 0, /* tp_hash */
1153 0, /* tp_call */
1154 0, /* tp_str */
1155 PyObject_GenericGetAttr, /* tp_getattro */
1156 0, /* tp_setattro */
1157 0, /* tp_as_buffer */
1158 Py_TPFLAGS_DEFAULT( 0 | (1L<<18) | 0) | Py_TPFLAGS_HAVE_GC(1L<<14),/* tp_flags */
1159 0, /* tp_doc */
1160 (traverseproc)dequeiter_traverse, /* tp_traverse */
1161 0, /* tp_clear */
1162 0, /* tp_richcompare */
1163 0, /* tp_weaklistoffset */
1164 PyObject_SelfIter, /* tp_iter */
1165 (iternextfunc)dequeiter_next, /* tp_iternext */
1166 dequeiter_methods, /* tp_methods */
1167 0,
1168};
1169
1170/*********************** Deque Reverse Iterator **************************/
1171
1172static PyTypeObject dequereviter_type;
1173
1174static PyObject *
1175deque_reviter(dequeobject *deque)
1176{
1177 dequeiterobject *it;
1178
1179 it = PyObject_GC_New(dequeiterobject, &dequereviter_type)( (dequeiterobject *) _PyObject_GC_New(&dequereviter_type
) )
;
1180 if (it == NULL((void*)0))
1181 return NULL((void*)0);
1182 it->b = deque->rightblock;
1183 it->index = deque->rightindex;
1184 Py_INCREF(deque)( _Py_RefTotal++ , ((PyObject*)(deque))->ob_refcnt++);
1185 it->deque = deque;
1186 it->state = deque->state;
1187 it->counter = deque->len;
1188 PyObject_GC_Track(it);
1189 return (PyObject *)it;
1190}
1191
1192static PyObject *
1193dequereviter_next(dequeiterobject *it)
1194{
1195 PyObject *item;
1196 if (it->counter == 0)
1197 return NULL((void*)0);
1198
1199 if (it->deque->state != it->state) {
1200 it->counter = 0;
1201 PyErr_SetString(PyExc_RuntimeError,
1202 "deque mutated during iteration");
1203 return NULL((void*)0);
1204 }
1205 assert (!(it->b == it->deque->leftblock &&(__builtin_expect(!(!(it->b == it->deque->leftblock &&
it->index < it->deque->leftindex)), 0) ? __assert_rtn
(__func__, "./Modules/_collectionsmodule.c", 1206, "!(it->b == it->deque->leftblock && it->index < it->deque->leftindex)"
) : (void)0)
1206 it->index < it->deque->leftindex))(__builtin_expect(!(!(it->b == it->deque->leftblock &&
it->index < it->deque->leftindex)), 0) ? __assert_rtn
(__func__, "./Modules/_collectionsmodule.c", 1206, "!(it->b == it->deque->leftblock && it->index < it->deque->leftindex)"
) : (void)0)
;
1207
1208 item = it->b->data[it->index];
1209 it->index--;
1210 it->counter--;
1211 if (it->index == -1 && it->counter > 0) {
1212 assert (it->b->leftlink != NULL)(__builtin_expect(!(it->b->leftlink != ((void*)0)), 0) ?
__assert_rtn(__func__, "./Modules/_collectionsmodule.c", 1212
, "it->b->leftlink != NULL") : (void)0)
;
1213 it->b = it->b->leftlink;
1214 it->index = BLOCKLEN62 - 1;
1215 }
1216 Py_INCREF(item)( _Py_RefTotal++ , ((PyObject*)(item))->ob_refcnt++);
1217 return item;
1218}
1219
1220static PyTypeObject dequereviter_type = {
1221 PyVarObject_HEAD_INIT(NULL, 0){ { 0, 0, 1, ((void*)0) }, 0 },
1222 "deque_reverse_iterator", /* tp_name */
1223 sizeof(dequeiterobject), /* tp_basicsize */
1224 0, /* tp_itemsize */
1225 /* methods */
1226 (destructor)dequeiter_dealloc, /* tp_dealloc */
1227 0, /* tp_print */
1228 0, /* tp_getattr */
1229 0, /* tp_setattr */
1230 0, /* tp_reserved */
1231 0, /* tp_repr */
1232 0, /* tp_as_number */
1233 0, /* tp_as_sequence */
1234 0, /* tp_as_mapping */
1235 0, /* tp_hash */
1236 0, /* tp_call */
1237 0, /* tp_str */
1238 PyObject_GenericGetAttr, /* tp_getattro */
1239 0, /* tp_setattro */
1240 0, /* tp_as_buffer */
1241 Py_TPFLAGS_DEFAULT( 0 | (1L<<18) | 0) | Py_TPFLAGS_HAVE_GC(1L<<14),/* tp_flags */
1242 0, /* tp_doc */
1243 (traverseproc)dequeiter_traverse, /* tp_traverse */
1244 0, /* tp_clear */
1245 0, /* tp_richcompare */
1246 0, /* tp_weaklistoffset */
1247 PyObject_SelfIter, /* tp_iter */
1248 (iternextfunc)dequereviter_next, /* tp_iternext */
1249 dequeiter_methods, /* tp_methods */
1250 0,
1251};
1252
1253/* defaultdict type *********************************************************/
1254
1255typedef struct {
1256 PyDictObject dict;
1257 PyObject *default_factory;
1258} defdictobject;
1259
1260static PyTypeObject defdict_type; /* Forward */
1261
1262PyDoc_STRVAR(defdict_missing_doc,static char defdict_missing_doc[] = "__missing__(key) # Called by __getitem__ for missing key; pseudo-code:\n if self.default_factory is None: raise KeyError((key,))\n self[key] = value = self.default_factory()\n return value\n"
1263"__missing__(key) # Called by __getitem__ for missing key; pseudo-code:\n\static char defdict_missing_doc[] = "__missing__(key) # Called by __getitem__ for missing key; pseudo-code:\n if self.default_factory is None: raise KeyError((key,))\n self[key] = value = self.default_factory()\n return value\n"
1264 if self.default_factory is None: raise KeyError((key,))\n\static char defdict_missing_doc[] = "__missing__(key) # Called by __getitem__ for missing key; pseudo-code:\n if self.default_factory is None: raise KeyError((key,))\n self[key] = value = self.default_factory()\n return value\n"
1265 self[key] = value = self.default_factory()\n\static char defdict_missing_doc[] = "__missing__(key) # Called by __getitem__ for missing key; pseudo-code:\n if self.default_factory is None: raise KeyError((key,))\n self[key] = value = self.default_factory()\n return value\n"
1266 return value\n\static char defdict_missing_doc[] = "__missing__(key) # Called by __getitem__ for missing key; pseudo-code:\n if self.default_factory is None: raise KeyError((key,))\n self[key] = value = self.default_factory()\n return value\n"
1267")static char defdict_missing_doc[] = "__missing__(key) # Called by __getitem__ for missing key; pseudo-code:\n if self.default_factory is None: raise KeyError((key,))\n self[key] = value = self.default_factory()\n return value\n";
1268
1269static PyObject *
1270defdict_missing(defdictobject *dd, PyObject *key)
1271{
1272 PyObject *factory = dd->default_factory;
1273 PyObject *value;
1274 if (factory == NULL((void*)0) || factory == Py_None(&_Py_NoneStruct)) {
1275 /* XXX Call dict.__missing__(key) */
1276 PyObject *tup;
1277 tup = PyTuple_Pack(1, key);
1278 if (!tup) return NULL((void*)0);
1279 PyErr_SetObject(PyExc_KeyError, tup);
1280 Py_DECREF(tup)do { if (_Py_RefTotal-- , --((PyObject*)(tup))->ob_refcnt !=
0) { if (((PyObject*)tup)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 1280, (PyObject *)(tup)); }
else _Py_Dealloc((PyObject *)(tup)); } while (0)
;
1281 return NULL((void*)0);
1282 }
1283 value = PyEval_CallObject(factory, NULL)PyEval_CallObjectWithKeywords(factory, ((void*)0), (PyObject *
)((void*)0))
;
1284 if (value == NULL((void*)0))
1285 return value;
1286 if (PyObject_SetItem((PyObject *)dd, key, value) < 0) {
1287 Py_DECREF(value)do { if (_Py_RefTotal-- , --((PyObject*)(value))->ob_refcnt
!= 0) { if (((PyObject*)value)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 1287, (PyObject *)(value))
; } else _Py_Dealloc((PyObject *)(value)); } while (0)
;
1288 return NULL((void*)0);
1289 }
1290 return value;
1291}
1292
1293PyDoc_STRVAR(defdict_copy_doc, "D.copy() -> a shallow copy of D.")static char defdict_copy_doc[] = "D.copy() -> a shallow copy of D.";
1294
1295static PyObject *
1296defdict_copy(defdictobject *dd)
1297{
1298 /* This calls the object's class. That only works for subclasses
1299 whose class constructor has the same signature. Subclasses that
1300 define a different constructor signature must override copy().
1301 */
1302
1303 if (dd->default_factory == NULL((void*)0))
1304 return PyObject_CallFunctionObjArgs((PyObject*)Py_TYPE(dd)(((PyObject*)(dd))->ob_type), Py_None(&_Py_NoneStruct), dd, NULL((void*)0));
1305 return PyObject_CallFunctionObjArgs((PyObject*)Py_TYPE(dd)(((PyObject*)(dd))->ob_type),
1306 dd->default_factory, dd, NULL((void*)0));
1307}
1308
1309static PyObject *
1310defdict_reduce(defdictobject *dd)
1311{
1312 /* __reduce__ must return a 5-tuple as follows:
1313
1314 - factory function
1315 - tuple of args for the factory function
1316 - additional state (here None)
1317 - sequence iterator (here None)
1318 - dictionary iterator (yielding successive (key, value) pairs
1319
1320 This API is used by pickle.py and copy.py.
1321
1322 For this to be useful with pickle.py, the default_factory
1323 must be picklable; e.g., None, a built-in, or a global
1324 function in a module or package.
1325
1326 Both shallow and deep copying are supported, but for deep
1327 copying, the default_factory must be deep-copyable; e.g. None,
1328 or a built-in (functions are not copyable at this time).
1329
1330 This only works for subclasses as long as their constructor
1331 signature is compatible; the first argument must be the
1332 optional default_factory, defaulting to None.
1333 */
1334 PyObject *args;
1335 PyObject *items;
1336 PyObject *iter;
1337 PyObject *result;
1338 if (dd->default_factory == NULL((void*)0) || dd->default_factory == Py_None(&_Py_NoneStruct))
1339 args = PyTuple_New(0);
1340 else
1341 args = PyTuple_Pack(1, dd->default_factory);
1342 if (args == NULL((void*)0))
1343 return NULL((void*)0);
1344 items = PyObject_CallMethod((PyObject *)dd, "items", "()");
1345 if (items == NULL((void*)0)) {
1346 Py_DECREF(args)do { if (_Py_RefTotal-- , --((PyObject*)(args))->ob_refcnt
!= 0) { if (((PyObject*)args)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 1346, (PyObject *)(args));
} else _Py_Dealloc((PyObject *)(args)); } while (0)
;
1347 return NULL((void*)0);
1348 }
1349 iter = PyObject_GetIter(items);
1350 if (iter == NULL((void*)0)) {
1351 Py_DECREF(items)do { if (_Py_RefTotal-- , --((PyObject*)(items))->ob_refcnt
!= 0) { if (((PyObject*)items)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 1351, (PyObject *)(items))
; } else _Py_Dealloc((PyObject *)(items)); } while (0)
;
1352 Py_DECREF(args)do { if (_Py_RefTotal-- , --((PyObject*)(args))->ob_refcnt
!= 0) { if (((PyObject*)args)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 1352, (PyObject *)(args));
} else _Py_Dealloc((PyObject *)(args)); } while (0)
;
1353 return NULL((void*)0);
1354 }
1355 result = PyTuple_Pack(5, Py_TYPE(dd)(((PyObject*)(dd))->ob_type), args,
1356 Py_None(&_Py_NoneStruct), Py_None(&_Py_NoneStruct), iter);
1357 Py_DECREF(iter)do { if (_Py_RefTotal-- , --((PyObject*)(iter))->ob_refcnt
!= 0) { if (((PyObject*)iter)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 1357, (PyObject *)(iter));
} else _Py_Dealloc((PyObject *)(iter)); } while (0)
;
1358 Py_DECREF(items)do { if (_Py_RefTotal-- , --((PyObject*)(items))->ob_refcnt
!= 0) { if (((PyObject*)items)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 1358, (PyObject *)(items))
; } else _Py_Dealloc((PyObject *)(items)); } while (0)
;
1359 Py_DECREF(args)do { if (_Py_RefTotal-- , --((PyObject*)(args))->ob_refcnt
!= 0) { if (((PyObject*)args)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 1359, (PyObject *)(args));
} else _Py_Dealloc((PyObject *)(args)); } while (0)
;
1360 return result;
1361}
1362
1363static PyMethodDef defdict_methods[] = {
1364 {"__missing__", (PyCFunction)defdict_missing, METH_O0x0008,
1365 defdict_missing_doc},
1366 {"copy", (PyCFunction)defdict_copy, METH_NOARGS0x0004,
1367 defdict_copy_doc},
1368 {"__copy__", (PyCFunction)defdict_copy, METH_NOARGS0x0004,
1369 defdict_copy_doc},
1370 {"__reduce__", (PyCFunction)defdict_reduce, METH_NOARGS0x0004,
1371 reduce_doc},
1372 {NULL((void*)0)}
1373};
1374
1375static PyMemberDef defdict_members[] = {
1376 {"default_factory", T_OBJECT6,
1377 offsetof(defdictobject, default_factory)__builtin_offsetof(defdictobject, default_factory), 0,
1378 PyDoc_STR("Factory for default value called by __missing__().")"Factory for default value called by __missing__()."},
1379 {NULL((void*)0)}
1380};
1381
1382static void
1383defdict_dealloc(defdictobject *dd)
1384{
1385 Py_CLEAR(dd->default_factory)do { if (dd->default_factory) { PyObject *_py_tmp = (PyObject
*)(dd->default_factory); (dd->default_factory) = ((void
*)0); do { if (_Py_RefTotal-- , --((PyObject*)(_py_tmp))->
ob_refcnt != 0) { if (((PyObject*)_py_tmp)->ob_refcnt <
0) _Py_NegativeRefcount("./Modules/_collectionsmodule.c", 1385
, (PyObject *)(_py_tmp)); } else _Py_Dealloc((PyObject *)(_py_tmp
)); } while (0); } } while (0)
;
1386 PyDict_Type.tp_dealloc((PyObject *)dd);
1387}
1388
1389static PyObject *
1390defdict_repr(defdictobject *dd)
1391{
1392 PyObject *baserepr;
1393 PyObject *defrepr;
1394 PyObject *result;
1395 baserepr = PyDict_Type.tp_repr((PyObject *)dd);
1396 if (baserepr == NULL((void*)0))
1397 return NULL((void*)0);
1398 if (dd->default_factory == NULL((void*)0))
1399 defrepr = PyUnicode_FromStringPyUnicodeUCS2_FromString("None");
1400 else
1401 {
1402 int status = Py_ReprEnter(dd->default_factory);
1403 if (status != 0) {
1404 if (status < 0)
1405 return NULL((void*)0);
1406 defrepr = PyUnicode_FromStringPyUnicodeUCS2_FromString("...");
1407 }
1408 else
1409 defrepr = PyObject_Repr(dd->default_factory);
1410 Py_ReprLeave(dd->default_factory);
1411 }
1412 if (defrepr == NULL((void*)0)) {
1413 Py_DECREF(baserepr)do { if (_Py_RefTotal-- , --((PyObject*)(baserepr))->ob_refcnt
!= 0) { if (((PyObject*)baserepr)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 1413, (PyObject *)(baserepr
)); } else _Py_Dealloc((PyObject *)(baserepr)); } while (0)
;
1414 return NULL((void*)0);
1415 }
1416 result = PyUnicode_FromFormatPyUnicodeUCS2_FromFormat("defaultdict(%U, %U)",
1417 defrepr, baserepr);
1418 Py_DECREF(defrepr)do { if (_Py_RefTotal-- , --((PyObject*)(defrepr))->ob_refcnt
!= 0) { if (((PyObject*)defrepr)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 1418, (PyObject *)(defrepr
)); } else _Py_Dealloc((PyObject *)(defrepr)); } while (0)
;
1419 Py_DECREF(baserepr)do { if (_Py_RefTotal-- , --((PyObject*)(baserepr))->ob_refcnt
!= 0) { if (((PyObject*)baserepr)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 1419, (PyObject *)(baserepr
)); } else _Py_Dealloc((PyObject *)(baserepr)); } while (0)
;
1420 return result;
1421}
1422
1423static int
1424defdict_traverse(PyObject *self, visitproc visit, void *arg)
1425{
1426 Py_VISIT(((defdictobject *)self)->default_factory)do { if (((defdictobject *)self)->default_factory) { int vret
= visit((PyObject *)(((defdictobject *)self)->default_factory
), arg); if (vret) return vret; } } while (0)
;
1427 return PyDict_Type.tp_traverse(self, visit, arg);
1428}
1429
1430static int
1431defdict_tp_clear(defdictobject *dd)
1432{
1433 Py_CLEAR(dd->default_factory)do { if (dd->default_factory) { PyObject *_py_tmp = (PyObject
*)(dd->default_factory); (dd->default_factory) = ((void
*)0); do { if (_Py_RefTotal-- , --((PyObject*)(_py_tmp))->
ob_refcnt != 0) { if (((PyObject*)_py_tmp)->ob_refcnt <
0) _Py_NegativeRefcount("./Modules/_collectionsmodule.c", 1433
, (PyObject *)(_py_tmp)); } else _Py_Dealloc((PyObject *)(_py_tmp
)); } while (0); } } while (0)
;
1434 return PyDict_Type.tp_clear((PyObject *)dd);
1435}
1436
1437static int
1438defdict_init(PyObject *self, PyObject *args, PyObject *kwds)
1439{
1440 defdictobject *dd = (defdictobject *)self;
1441 PyObject *olddefault = dd->default_factory;
1442 PyObject *newdefault = NULL((void*)0);
1443 PyObject *newargs;
1444 int result;
1445 if (args == NULL((void*)0) || !PyTuple_Check(args)((((((PyObject*)(args))->ob_type))->tp_flags & ((1L
<<26))) != 0)
)
1446 newargs = PyTuple_New(0);
1447 else {
1448 Py_ssize_t n = PyTuple_GET_SIZE(args)(((PyVarObject*)(args))->ob_size);
1449 if (n > 0) {
1450 newdefault = PyTuple_GET_ITEM(args, 0)(((PyTupleObject *)(args))->ob_item[0]);
1451 if (!PyCallable_Check(newdefault) && newdefault != Py_None(&_Py_NoneStruct)) {
1452 PyErr_SetString(PyExc_TypeError,
1453 "first argument must be callable");
1454 return -1;
1455 }
1456 }
1457 newargs = PySequence_GetSlice(args, 1, n);
1458 }
1459 if (newargs == NULL((void*)0))
1460 return -1;
1461 Py_XINCREF(newdefault)do { if ((newdefault) == ((void*)0)) ; else ( _Py_RefTotal++ ,
((PyObject*)(newdefault))->ob_refcnt++); } while (0)
;
1462 dd->default_factory = newdefault;
1463 result = PyDict_Type.tp_init(self, newargs, kwds);
1464 Py_DECREF(newargs)do { if (_Py_RefTotal-- , --((PyObject*)(newargs))->ob_refcnt
!= 0) { if (((PyObject*)newargs)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 1464, (PyObject *)(newargs
)); } else _Py_Dealloc((PyObject *)(newargs)); } while (0)
;
1465 Py_XDECREF(olddefault)do { if ((olddefault) == ((void*)0)) ; else do { if (_Py_RefTotal
-- , --((PyObject*)(olddefault))->ob_refcnt != 0) { if (((
PyObject*)olddefault)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 1465, (PyObject *)(olddefault
)); } else _Py_Dealloc((PyObject *)(olddefault)); } while (0)
; } while (0)
;
1466 return result;
1467}
1468
1469PyDoc_STRVAR(defdict_doc,static char defdict_doc[] = "defaultdict(default_factory) --> dict with default factory\n\nThe default factory is called without arguments to produce\na new value when a key is not present, in __getitem__ only.\nA defaultdict compares equal to a dict with the same items.\n"
1470"defaultdict(default_factory) --> dict with default factory\n\static char defdict_doc[] = "defaultdict(default_factory) --> dict with default factory\n\nThe default factory is called without arguments to produce\na new value when a key is not present, in __getitem__ only.\nA defaultdict compares equal to a dict with the same items.\n"
1471\n\static char defdict_doc[] = "defaultdict(default_factory) --> dict with default factory\n\nThe default factory is called without arguments to produce\na new value when a key is not present, in __getitem__ only.\nA defaultdict compares equal to a dict with the same items.\n"
1472The default factory is called without arguments to produce\n\static char defdict_doc[] = "defaultdict(default_factory) --> dict with default factory\n\nThe default factory is called without arguments to produce\na new value when a key is not present, in __getitem__ only.\nA defaultdict compares equal to a dict with the same items.\n"
1473a new value when a key is not present, in __getitem__ only.\n\static char defdict_doc[] = "defaultdict(default_factory) --> dict with default factory\n\nThe default factory is called without arguments to produce\na new value when a key is not present, in __getitem__ only.\nA defaultdict compares equal to a dict with the same items.\n"
1474A defaultdict compares equal to a dict with the same items.\n\static char defdict_doc[] = "defaultdict(default_factory) --> dict with default factory\n\nThe default factory is called without arguments to produce\na new value when a key is not present, in __getitem__ only.\nA defaultdict compares equal to a dict with the same items.\n"
1475")static char defdict_doc[] = "defaultdict(default_factory) --> dict with default factory\n\nThe default factory is called without arguments to produce\na new value when a key is not present, in __getitem__ only.\nA defaultdict compares equal to a dict with the same items.\n";
1476
1477/* See comment in xxsubtype.c */
1478#define DEFERRED_ADDRESS(ADDR)0 0
1479
1480static PyTypeObject defdict_type = {
1481 PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0){ { 0, 0, 1, 0 }, 0 },
1482 "collections.defaultdict", /* tp_name */
1483 sizeof(defdictobject), /* tp_basicsize */
1484 0, /* tp_itemsize */
1485 /* methods */
1486 (destructor)defdict_dealloc, /* tp_dealloc */
1487 0, /* tp_print */
1488 0, /* tp_getattr */
1489 0, /* tp_setattr */
1490 0, /* tp_reserved */
1491 (reprfunc)defdict_repr, /* tp_repr */
1492 0, /* tp_as_number */
1493 0, /* tp_as_sequence */
1494 0, /* tp_as_mapping */
1495 0, /* tp_hash */
1496 0, /* tp_call */
1497 0, /* tp_str */
1498 PyObject_GenericGetAttr, /* tp_getattro */
1499 0, /* tp_setattro */
1500 0, /* tp_as_buffer */
1501 Py_TPFLAGS_DEFAULT( 0 | (1L<<18) | 0) | Py_TPFLAGS_BASETYPE(1L<<10) | Py_TPFLAGS_HAVE_GC(1L<<14),
1502 /* tp_flags */
1503 defdict_doc, /* tp_doc */
1504 defdict_traverse, /* tp_traverse */
1505 (inquiry)defdict_tp_clear, /* tp_clear */
1506 0, /* tp_richcompare */
1507 0, /* tp_weaklistoffset*/
1508 0, /* tp_iter */
1509 0, /* tp_iternext */
1510 defdict_methods, /* tp_methods */
1511 defdict_members, /* tp_members */
1512 0, /* tp_getset */
1513 DEFERRED_ADDRESS(&PyDict_Type)0, /* tp_base */
1514 0, /* tp_dict */
1515 0, /* tp_descr_get */
1516 0, /* tp_descr_set */
1517 0, /* tp_dictoffset */
1518 defdict_init, /* tp_init */
1519 PyType_GenericAlloc, /* tp_alloc */
1520 0, /* tp_new */
1521 PyObject_GC_Del, /* tp_free */
1522};
1523
1524/* helper function for Counter *********************************************/
1525
1526PyDoc_STRVAR(_count_elements_doc,static char _count_elements_doc[] = "_count_elements(mapping, iterable) -> None\n\nCount elements in the iterable, updating the mappping"
1527"_count_elements(mapping, iterable) -> None\n\static char _count_elements_doc[] = "_count_elements(mapping, iterable) -> None\n\nCount elements in the iterable, updating the mappping"
1528\n\static char _count_elements_doc[] = "_count_elements(mapping, iterable) -> None\n\nCount elements in the iterable, updating the mappping"
1529Count elements in the iterable, updating the mappping")static char _count_elements_doc[] = "_count_elements(mapping, iterable) -> None\n\nCount elements in the iterable, updating the mappping";
1530
1531static PyObject *
1532_count_elements(PyObject *self, PyObject *args)
1533{
1534 PyObject *it, *iterable, *mapping, *oldval;
1535 PyObject *newval = NULL((void*)0);
1536 PyObject *key = NULL((void*)0);
1537 PyObject *one = NULL((void*)0);
1538
1539 if (!PyArg_UnpackTuple(args, "_count_elements", 2, 2, &mapping, &iterable))
1
Taking false branch
1540 return NULL((void*)0);
1541
1542 it = PyObject_GetIter(iterable);
1543 if (it == NULL((void*)0))
2
Taking false branch
1544 return NULL((void*)0);
1545
1546 one = PyLong_FromLong(1);
1547 if (one == NULL((void*)0)) {
3
Taking false branch
1548 Py_DECREF(it)do { if (_Py_RefTotal-- , --((PyObject*)(it))->ob_refcnt !=
0) { if (((PyObject*)it)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 1548, (PyObject *)(it)); }
else _Py_Dealloc((PyObject *)(it)); } while (0)
;
1549 return NULL((void*)0);
1550 }
1551
1552 if (PyDict_CheckExact(mapping)((((PyObject*)(mapping))->ob_type) == &PyDict_Type)) {
4
Taking true branch
1553 while (1) {
5
Loop condition is true. Entering loop body
1554 key = PyIter_Next(it);
1555 if (key == NULL((void*)0)) {
6
Taking true branch
1556 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_StopIteration))
7
Taking true branch
1557 PyErr_Clear();
1558 else
1559 break;
1560 }
1561 oldval = PyDict_GetItem(mapping, key);
1562 if (oldval == NULL((void*)0)) {
8
Taking true branch
1563 if (PyDict_SetItem(mapping, key, one) == -1)
9
Taking false branch
1564 break;
1565 } else {
1566 newval = PyNumber_Add(oldval, one);
1567 if (newval == NULL((void*)0))
1568 break;
1569 if (PyDict_SetItem(mapping, key, newval) == -1)
1570 break;
1571 Py_CLEAR(newval)do { if (newval) { PyObject *_py_tmp = (PyObject *)(newval); (
newval) = ((void*)0); do { if (_Py_RefTotal-- , --((PyObject*
)(_py_tmp))->ob_refcnt != 0) { if (((PyObject*)_py_tmp)->
ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_collectionsmodule.c"
, 1571, (PyObject *)(_py_tmp)); } else _Py_Dealloc((PyObject *
)(_py_tmp)); } while (0); } } while (0)
;
1572 }
1573 Py_DECREF(key)do { if (_Py_RefTotal-- , --((PyObject*)(key))->ob_refcnt !=
0) { if (((PyObject*)key)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 1573, (PyObject *)(key)); }
else _Py_Dealloc((PyObject *)(key)); } while (0)
;
10
Within the expansion of the macro 'Py_DECREF':
a
Access to field 'ob_refcnt' results in a dereference of a null pointer (loaded from variable 'key')
1574 }
1575 } else {
1576 while (1) {
1577 key = PyIter_Next(it);
1578 if (key == NULL((void*)0)) {
1579 if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_StopIteration))
1580 PyErr_Clear();
1581 else
1582 break;
1583 }
1584 oldval = PyObject_GetItem(mapping, key);
1585 if (oldval == NULL((void*)0)) {
1586 if (!PyErr_Occurred() || !PyErr_ExceptionMatches(PyExc_KeyError))
1587 break;
1588 PyErr_Clear();
1589 Py_INCREF(one)( _Py_RefTotal++ , ((PyObject*)(one))->ob_refcnt++);
1590 newval = one;
1591 } else {
1592 newval = PyNumber_Add(oldval, one);
1593 Py_DECREF(oldval)do { if (_Py_RefTotal-- , --((PyObject*)(oldval))->ob_refcnt
!= 0) { if (((PyObject*)oldval)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 1593, (PyObject *)(oldval)
); } else _Py_Dealloc((PyObject *)(oldval)); } while (0)
;
1594 if (newval == NULL((void*)0))
1595 break;
1596 }
1597 if (PyObject_SetItem(mapping, key, newval) == -1)
1598 break;
1599 Py_CLEAR(newval)do { if (newval) { PyObject *_py_tmp = (PyObject *)(newval); (
newval) = ((void*)0); do { if (_Py_RefTotal-- , --((PyObject*
)(_py_tmp))->ob_refcnt != 0) { if (((PyObject*)_py_tmp)->
ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_collectionsmodule.c"
, 1599, (PyObject *)(_py_tmp)); } else _Py_Dealloc((PyObject *
)(_py_tmp)); } while (0); } } while (0)
;
1600 Py_DECREF(key)do { if (_Py_RefTotal-- , --((PyObject*)(key))->ob_refcnt !=
0) { if (((PyObject*)key)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 1600, (PyObject *)(key)); }
else _Py_Dealloc((PyObject *)(key)); } while (0)
;
1601 }
1602 }
1603
1604 Py_DECREF(it)do { if (_Py_RefTotal-- , --((PyObject*)(it))->ob_refcnt !=
0) { if (((PyObject*)it)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 1604, (PyObject *)(it)); }
else _Py_Dealloc((PyObject *)(it)); } while (0)
;
1605 Py_XDECREF(key)do { if ((key) == ((void*)0)) ; else do { if (_Py_RefTotal-- ,
--((PyObject*)(key))->ob_refcnt != 0) { if (((PyObject*)key
)->ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_collectionsmodule.c"
, 1605, (PyObject *)(key)); } else _Py_Dealloc((PyObject *)(key
)); } while (0); } while (0)
;
1606 Py_XDECREF(newval)do { if ((newval) == ((void*)0)) ; else do { if (_Py_RefTotal
-- , --((PyObject*)(newval))->ob_refcnt != 0) { if (((PyObject
*)newval)->ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_collectionsmodule.c"
, 1606, (PyObject *)(newval)); } else _Py_Dealloc((PyObject *
)(newval)); } while (0); } while (0)
;
1607 Py_DECREF(one)do { if (_Py_RefTotal-- , --((PyObject*)(one))->ob_refcnt !=
0) { if (((PyObject*)one)->ob_refcnt < 0) _Py_NegativeRefcount
("./Modules/_collectionsmodule.c", 1607, (PyObject *)(one)); }
else _Py_Dealloc((PyObject *)(one)); } while (0)
;
1608 if (PyErr_Occurred())
1609 return NULL((void*)0);
1610 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
1611}
1612
1613/* module level code ********************************************************/
1614
1615PyDoc_STRVAR(module_doc,static char module_doc[] = "High performance data structures.\n- deque: ordered collection accessible from endpoints only\n- defaultdict: dict subclass with a default value factory\n"
1616"High performance data structures.\n\static char module_doc[] = "High performance data structures.\n- deque: ordered collection accessible from endpoints only\n- defaultdict: dict subclass with a default value factory\n"
1617- deque: ordered collection accessible from endpoints only\n\static char module_doc[] = "High performance data structures.\n- deque: ordered collection accessible from endpoints only\n- defaultdict: dict subclass with a default value factory\n"
1618- defaultdict: dict subclass with a default value factory\n\static char module_doc[] = "High performance data structures.\n- deque: ordered collection accessible from endpoints only\n- defaultdict: dict subclass with a default value factory\n"
1619")static char module_doc[] = "High performance data structures.\n- deque: ordered collection accessible from endpoints only\n- defaultdict: dict subclass with a default value factory\n";
1620
1621static struct PyMethodDef module_functions[] = {
1622 {"_count_elements", _count_elements, METH_VARARGS0x0001, _count_elements_doc},
1623 {NULL((void*)0), NULL((void*)0)} /* sentinel */
1624};
1625
1626static struct PyModuleDef _collectionsmodule = {
1627 PyModuleDef_HEAD_INIT{ { 0, 0, 1, ((void*)0) }, ((void*)0), 0, ((void*)0), },
1628 "_collections",
1629 module_doc,
1630 -1,
1631 module_functions,
1632 NULL((void*)0),
1633 NULL((void*)0),
1634 NULL((void*)0),
1635 NULL((void*)0)
1636};
1637
1638PyMODINIT_FUNCPyObject*
1639PyInit__collections(void)
1640{
1641 PyObject *m;
1642
1643 m = PyModule_Create(&_collectionsmodule)PyModule_Create2TraceRefs(&_collectionsmodule, 1013);
1644 if (m == NULL((void*)0))
1645 return NULL((void*)0);
1646
1647 if (PyType_Ready(&deque_type) < 0)
1648 return NULL((void*)0);
1649 Py_INCREF(&deque_type)( _Py_RefTotal++ , ((PyObject*)(&deque_type))->ob_refcnt
++)
;
1650 PyModule_AddObject(m, "deque", (PyObject *)&deque_type);
1651
1652 defdict_type.tp_base = &PyDict_Type;
1653 if (PyType_Ready(&defdict_type) < 0)
1654 return NULL((void*)0);
1655 Py_INCREF(&defdict_type)( _Py_RefTotal++ , ((PyObject*)(&defdict_type))->ob_refcnt
++)
;
1656 PyModule_AddObject(m, "defaultdict", (PyObject *)&defdict_type);
1657
1658 if (PyType_Ready(&dequeiter_type) < 0)
1659 return NULL((void*)0);
1660
1661 if (PyType_Ready(&dequereviter_type) < 0)
1662 return NULL((void*)0);
1663
1664 return m;
1665}