Bug Summary

File:Objects/dictobject.c
Location:line 338, column 40
Description:Both operands to '==' always have the same value

Annotated Source Code

1
2/* Dictionary object implementation using a hash table */
3
4/* The distribution includes a separate file, Objects/dictnotes.txt,
5 describing explorations into dictionary design and optimization.
6 It covers typical dictionary use patterns, the parameters for
7 tuning dictionaries, and several ideas for possible optimizations.
8*/
9
10#include "Python.h"
11#include "stringlib/eq.h"
12
13
14/* Set a key error with the specified argument, wrapping it in a
15 * tuple automatically so that tuple keys are not unpacked as the
16 * exception arguments. */
17static void
18set_key_error(PyObject *arg)
19{
20 PyObject *tup;
21 tup = PyTuple_Pack(1, arg);
22 if (!tup)
23 return; /* caller will expect error to be set anyway */
24 PyErr_SetObject(PyExc_KeyError, tup);
25 Py_DECREF(tup)do { if (_Py_RefTotal-- , --((PyObject*)(tup))->ob_refcnt !=
0) { if (((PyObject*)tup)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 25, (PyObject *)(tup)); } else _Py_Dealloc
((PyObject *)(tup)); } while (0)
;
26}
27
28/* Define this out if you don't want conversion statistics on exit. */
29#undef SHOW_CONVERSION_COUNTS
30
31/* See large comment block below. This must be >= 1. */
32#define PERTURB_SHIFT5 5
33
34/*
35Major subtleties ahead: Most hash schemes depend on having a "good" hash
36function, in the sense of simulating randomness. Python doesn't: its most
37important hash functions (for strings and ints) are very regular in common
38cases:
39
40 >>> map(hash, (0, 1, 2, 3))
41 [0, 1, 2, 3]
42 >>> map(hash, ("namea", "nameb", "namec", "named"))
43 [-1658398457, -1658398460, -1658398459, -1658398462]
44 >>>
45
46This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
47the low-order i bits as the initial table index is extremely fast, and there
48are no collisions at all for dicts indexed by a contiguous range of ints.
49The same is approximately true when keys are "consecutive" strings. So this
50gives better-than-random behavior in common cases, and that's very desirable.
51
52OTOH, when collisions occur, the tendency to fill contiguous slices of the
53hash table makes a good collision resolution strategy crucial. Taking only
54the last i bits of the hash code is also vulnerable: for example, consider
55the list [i << 16 for i in range(20000)] as a set of keys. Since ints are
56their own hash codes, and this fits in a dict of size 2**15, the last 15 bits
57 of every hash code are all 0: they *all* map to the same table index.
58
59But catering to unusual cases should not slow the usual ones, so we just take
60the last i bits anyway. It's up to collision resolution to do the rest. If
61we *usually* find the key we're looking for on the first try (and, it turns
62out, we usually do -- the table load factor is kept under 2/3, so the odds
63are solidly in our favor), then it makes best sense to keep the initial index
64computation dirt cheap.
65
66The first half of collision resolution is to visit table indices via this
67recurrence:
68
69 j = ((5*j) + 1) mod 2**i
70
71For any initial j in range(2**i), repeating that 2**i times generates each
72int in range(2**i) exactly once (see any text on random-number generation for
73proof). By itself, this doesn't help much: like linear probing (setting
74j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
75order. This would be bad, except that's not the only thing we do, and it's
76actually *good* in the common cases where hash keys are consecutive. In an
77example that's really too small to make this entirely clear, for a table of
78size 2**3 the order of indices is:
79
80 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
81
82If two things come in at index 5, the first place we look after is index 2,
83not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
84Linear probing is deadly in this case because there the fixed probe order
85is the *same* as the order consecutive keys are likely to arrive. But it's
86extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
87and certain that consecutive hash codes do not.
88
89The other half of the strategy is to get the other bits of the hash code
90into play. This is done by initializing a (unsigned) vrbl "perturb" to the
91full hash code, and changing the recurrence to:
92
93 j = (5*j) + 1 + perturb;
94 perturb >>= PERTURB_SHIFT;
95 use j % 2**i as the next table index;
96
97Now the probe sequence depends (eventually) on every bit in the hash code,
98and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
99because it quickly magnifies small differences in the bits that didn't affect
100the initial index. Note that because perturb is unsigned, if the recurrence
101is executed often enough perturb eventually becomes and remains 0. At that
102point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
103that's certain to find an empty slot eventually (since it generates every int
104in range(2**i), and we make sure there's always at least one empty slot).
105
106Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
107small so that the high bits of the hash code continue to affect the probe
108sequence across iterations; but you want it large so that in really bad cases
109the high-order hash bits have an effect on early iterations. 5 was "the
110best" in minimizing total collisions across experiments Tim Peters ran (on
111both normal and pathological cases), but 4 and 6 weren't significantly worse.
112
113Historical: Reimer Behrends contributed the idea of using a polynomial-based
114approach, using repeated multiplication by x in GF(2**n) where an irreducible
115polynomial for each table size was chosen such that x was a primitive root.
116Christian Tismer later extended that to use division by x instead, as an
117efficient way to get the high bits of the hash code into play. This scheme
118also gave excellent collision statistics, but was more expensive: two
119if-tests were required inside the loop; computing "the next" index took about
120the same number of operations but without as much potential parallelism
121(e.g., computing 5*j can go on at the same time as computing 1+perturb in the
122above, and then shifting perturb can be done while the table index is being
123masked); and the PyDictObject struct required a member to hold the table's
124polynomial. In Tim's experiments the current scheme ran faster, produced
125equally good collision statistics, needed less code & used less memory.
126
127*/
128
129/* Object used as dummy key to fill deleted entries */
130static PyObject *dummy = NULL((void *)0); /* Initialized by first call to newPyDictObject() */
131
132#ifdef Py_REF_DEBUG
133PyObject *
134_PyDict_Dummy(void)
135{
136 return dummy;
137}
138#endif
139
140/* forward declarations */
141static PyDictEntry *
142lookdict_unicode(PyDictObject *mp, PyObject *key, Py_hash_t hash);
143
144#ifdef SHOW_CONVERSION_COUNTS
145static long created = 0L;
146static long converted = 0L;
147
148static void
149show_counts(void)
150{
151 fprintf(stderr__stderrp, "created %ld string dicts\n", created);
152 fprintf(stderr__stderrp, "converted %ld to normal dicts\n", converted);
153 fprintf(stderr__stderrp, "%.2f%% conversion rate\n", (100.0*converted)/created);
154}
155#endif
156
157/* Debug statistic to compare allocations with reuse through the free list */
158#undef SHOW_ALLOC_COUNT
159#ifdef SHOW_ALLOC_COUNT
160static size_t count_alloc = 0;
161static size_t count_reuse = 0;
162
163static void
164show_alloc(void)
165{
166 fprintf(stderr__stderrp, "Dict allocations: %" PY_FORMAT_SIZE_T"l" "d\n",
167 count_alloc);
168 fprintf(stderr__stderrp, "Dict reuse through freelist: %" PY_FORMAT_SIZE_T"l"
169 "d\n", count_reuse);
170 fprintf(stderr__stderrp, "%.2f%% reuse rate\n\n",
171 (100.0*count_reuse/(count_alloc+count_reuse)));
172}
173#endif
174
175/* Debug statistic to count GC tracking of dicts */
176#ifdef SHOW_TRACK_COUNT
177static Py_ssize_t count_untracked = 0;
178static Py_ssize_t count_tracked = 0;
179
180static void
181show_track(void)
182{
183 fprintf(stderr__stderrp, "Dicts created: %" PY_FORMAT_SIZE_T"l" "d\n",
184 count_tracked + count_untracked);
185 fprintf(stderr__stderrp, "Dicts tracked by the GC: %" PY_FORMAT_SIZE_T"l"
186 "d\n", count_tracked);
187 fprintf(stderr__stderrp, "%.2f%% dict tracking rate\n\n",
188 (100.0*count_tracked/(count_untracked+count_tracked)));
189}
190#endif
191
192
193/* Initialization macros.
194 There are two ways to create a dict: PyDict_New() is the main C API
195 function, and the tp_new slot maps to dict_new(). In the latter case we
196 can save a little time over what PyDict_New does because it's guaranteed
197 that the PyDictObject struct is already zeroed out.
198 Everyone except dict_new() should use EMPTY_TO_MINSIZE (unless they have
199 an excellent reason not to).
200*/
201
202#define INIT_NONZERO_DICT_SLOTS(mp)do { (mp)->ma_table = (mp)->ma_smalltable; (mp)->ma_mask
= 8 - 1; } while(0)
do { \
203 (mp)->ma_table = (mp)->ma_smalltable; \
204 (mp)->ma_mask = PyDict_MINSIZE8 - 1; \
205 } while(0)
206
207#define EMPTY_TO_MINSIZE(mp)do { ((__builtin_object_size ((mp)->ma_smalltable, 0) != (
size_t) -1) ? __builtin___memset_chk ((mp)->ma_smalltable,
0, sizeof((mp)->ma_smalltable), __builtin_object_size ((mp
)->ma_smalltable, 0)) : __inline_memset_chk ((mp)->ma_smalltable
, 0, sizeof((mp)->ma_smalltable))); (mp)->ma_used = (mp
)->ma_fill = 0; do { (mp)->ma_table = (mp)->ma_smalltable
; (mp)->ma_mask = 8 - 1; } while(0); } while(0)
do { \
208 memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable))((__builtin_object_size ((mp)->ma_smalltable, 0) != (size_t
) -1) ? __builtin___memset_chk ((mp)->ma_smalltable, 0, sizeof
((mp)->ma_smalltable), __builtin_object_size ((mp)->ma_smalltable
, 0)) : __inline_memset_chk ((mp)->ma_smalltable, 0, sizeof
((mp)->ma_smalltable)))
; \
209 (mp)->ma_used = (mp)->ma_fill = 0; \
210 INIT_NONZERO_DICT_SLOTS(mp)do { (mp)->ma_table = (mp)->ma_smalltable; (mp)->ma_mask
= 8 - 1; } while(0)
; \
211 } while(0)
212
213/* Dictionary reuse scheme to save calls to malloc, free, and memset */
214#ifndef PyDict_MAXFREELIST80
215#define PyDict_MAXFREELIST80 80
216#endif
217static PyDictObject *free_list[PyDict_MAXFREELIST80];
218static int numfree = 0;
219
220void
221PyDict_Fini(void)
222{
223 PyDictObject *op;
224
225 while (numfree) {
226 op = free_list[--numfree];
227 assert(PyDict_CheckExact(op))(__builtin_expect(!(((((PyObject*)(op))->ob_type) == &
PyDict_Type)), 0) ? __assert_rtn(__func__, "Objects/dictobject.c"
, 227, "PyDict_CheckExact(op)") : (void)0)
;
228 PyObject_GC_Del(op);
229 }
230}
231
232PyObject *
233PyDict_New(void)
234{
235 register PyDictObject *mp;
236 if (dummy == NULL((void *)0)) { /* Auto-initialize dummy */
237 dummy = PyUnicode_FromStringPyUnicodeUCS2_FromString("<dummy key>");
238 if (dummy == NULL((void *)0))
239 return NULL((void *)0);
240#ifdef SHOW_CONVERSION_COUNTS
241 Py_AtExit(show_counts);
242#endif
243#ifdef SHOW_ALLOC_COUNT
244 Py_AtExit(show_alloc);
245#endif
246#ifdef SHOW_TRACK_COUNT
247 Py_AtExit(show_track);
248#endif
249 }
250 if (numfree) {
251 mp = free_list[--numfree];
252 assert (mp != NULL)(__builtin_expect(!(mp != ((void *)0)), 0) ? __assert_rtn(__func__
, "Objects/dictobject.c", 252, "mp != NULL") : (void)0)
;
253 assert (Py_TYPE(mp) == &PyDict_Type)(__builtin_expect(!((((PyObject*)(mp))->ob_type) == &PyDict_Type
), 0) ? __assert_rtn(__func__, "Objects/dictobject.c", 253, "Py_TYPE(mp) == &PyDict_Type"
) : (void)0)
;
254 _Py_NewReference((PyObject *)mp);
255 if (mp->ma_fill) {
256 EMPTY_TO_MINSIZE(mp)do { ((__builtin_object_size ((mp)->ma_smalltable, 0) != (
size_t) -1) ? __builtin___memset_chk ((mp)->ma_smalltable,
0, sizeof((mp)->ma_smalltable), __builtin_object_size ((mp
)->ma_smalltable, 0)) : __inline_memset_chk ((mp)->ma_smalltable
, 0, sizeof((mp)->ma_smalltable))); (mp)->ma_used = (mp
)->ma_fill = 0; do { (mp)->ma_table = (mp)->ma_smalltable
; (mp)->ma_mask = 8 - 1; } while(0); } while(0)
;
257 } else {
258 /* At least set ma_table and ma_mask; these are wrong
259 if an empty but presized dict is added to freelist */
260 INIT_NONZERO_DICT_SLOTS(mp)do { (mp)->ma_table = (mp)->ma_smalltable; (mp)->ma_mask
= 8 - 1; } while(0)
;
261 }
262 assert (mp->ma_used == 0)(__builtin_expect(!(mp->ma_used == 0), 0) ? __assert_rtn(__func__
, "Objects/dictobject.c", 262, "mp->ma_used == 0") : (void
)0)
;
263 assert (mp->ma_table == mp->ma_smalltable)(__builtin_expect(!(mp->ma_table == mp->ma_smalltable),
0) ? __assert_rtn(__func__, "Objects/dictobject.c", 263, "mp->ma_table == mp->ma_smalltable"
) : (void)0)
;
264 assert (mp->ma_mask == PyDict_MINSIZE - 1)(__builtin_expect(!(mp->ma_mask == 8 - 1), 0) ? __assert_rtn
(__func__, "Objects/dictobject.c", 264, "mp->ma_mask == PyDict_MINSIZE - 1"
) : (void)0)
;
265#ifdef SHOW_ALLOC_COUNT
266 count_reuse++;
267#endif
268 } else {
269 mp = PyObject_GC_New(PyDictObject, &PyDict_Type)( (PyDictObject *) _PyObject_GC_New(&PyDict_Type) );
270 if (mp == NULL((void *)0))
271 return NULL((void *)0);
272 EMPTY_TO_MINSIZE(mp)do { ((__builtin_object_size ((mp)->ma_smalltable, 0) != (
size_t) -1) ? __builtin___memset_chk ((mp)->ma_smalltable,
0, sizeof((mp)->ma_smalltable), __builtin_object_size ((mp
)->ma_smalltable, 0)) : __inline_memset_chk ((mp)->ma_smalltable
, 0, sizeof((mp)->ma_smalltable))); (mp)->ma_used = (mp
)->ma_fill = 0; do { (mp)->ma_table = (mp)->ma_smalltable
; (mp)->ma_mask = 8 - 1; } while(0); } while(0)
;
273#ifdef SHOW_ALLOC_COUNT
274 count_alloc++;
275#endif
276 }
277 mp->ma_lookup = lookdict_unicode;
278#ifdef SHOW_TRACK_COUNT
279 count_untracked++;
280#endif
281#ifdef SHOW_CONVERSION_COUNTS
282 ++created;
283#endif
284 return (PyObject *)mp;
285}
286
287/*
288The basic lookup function used by all operations.
289This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
290Open addressing is preferred over chaining since the link overhead for
291chaining would be substantial (100% with typical malloc overhead).
292
293The initial probe index is computed as hash mod the table size. Subsequent
294probe indices are computed as explained earlier.
295
296All arithmetic on hash should ignore overflow.
297
298The details in this version are due to Tim Peters, building on many past
299contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
300Christian Tismer.
301
302lookdict() is general-purpose, and may return NULL if (and only if) a
303comparison raises an exception (this was new in Python 2.5).
304lookdict_unicode() below is specialized to string keys, comparison of which can
305never raise an exception; that function can never return NULL. For both, when
306the key isn't found a PyDictEntry* is returned for which the me_value field is
307NULL; this is the slot in the dict at which the key would have been found, and
308the caller can (if it wishes) add the <key, value> pair to the returned
309PyDictEntry*.
310*/
311static PyDictEntry *
312lookdict(PyDictObject *mp, PyObject *key, register Py_hash_t hash)
313{
314 register size_t i;
315 register size_t perturb;
316 register PyDictEntry *freeslot;
317 register size_t mask = (size_t)mp->ma_mask;
318 PyDictEntry *ep0 = mp->ma_table;
319 register PyDictEntry *ep;
320 register int cmp;
321 PyObject *startkey;
322
323 i = (size_t)hash & mask;
324 ep = &ep0[i];
325 if (ep->me_key == NULL((void *)0) || ep->me_key == key)
1
Taking false branch
326 return ep;
327
328 if (ep->me_key == dummy)
2
Taking false branch
329 freeslot = ep;
330 else {
331 if (ep->me_hash == hash) {
3
Taking true branch
332 startkey = ep->me_key;
333 Py_INCREF(startkey)( _Py_RefTotal++ , ((PyObject*)(startkey))->ob_refcnt++);
334 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ2);
335 Py_DECREF(startkey)do { if (_Py_RefTotal-- , --((PyObject*)(startkey))->ob_refcnt
!= 0) { if (((PyObject*)startkey)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 335, (PyObject *)(startkey)); } else
_Py_Dealloc((PyObject *)(startkey)); } while (0)
;
336 if (cmp < 0)
4
Taking false branch
337 return NULL((void *)0);
338 if (ep0 == mp->ma_table && ep->me_key == startkey) {
5
Both operands to '==' always have the same value
339 if (cmp > 0)
340 return ep;
341 }
342 else {
343 /* The compare did major nasty stuff to the
344 * dict: start over.
345 * XXX A clever adversary could prevent this
346 * XXX from terminating.
347 */
348 return lookdict(mp, key, hash);
349 }
350 }
351 freeslot = NULL((void *)0);
352 }
353
354 /* In the loop, me_key == dummy is by far (factor of 100s) the
355 least likely outcome, so test for that last. */
356 for (perturb = hash; ; perturb >>= PERTURB_SHIFT5) {
357 i = (i << 2) + i + perturb + 1;
358 ep = &ep0[i & mask];
359 if (ep->me_key == NULL((void *)0))
360 return freeslot == NULL((void *)0) ? ep : freeslot;
361 if (ep->me_key == key)
362 return ep;
363 if (ep->me_hash == hash && ep->me_key != dummy) {
364 startkey = ep->me_key;
365 Py_INCREF(startkey)( _Py_RefTotal++ , ((PyObject*)(startkey))->ob_refcnt++);
366 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ2);
367 Py_DECREF(startkey)do { if (_Py_RefTotal-- , --((PyObject*)(startkey))->ob_refcnt
!= 0) { if (((PyObject*)startkey)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 367, (PyObject *)(startkey)); } else
_Py_Dealloc((PyObject *)(startkey)); } while (0)
;
368 if (cmp < 0)
369 return NULL((void *)0);
370 if (ep0 == mp->ma_table && ep->me_key == startkey) {
371 if (cmp > 0)
372 return ep;
373 }
374 else {
375 /* The compare did major nasty stuff to the
376 * dict: start over.
377 * XXX A clever adversary could prevent this
378 * XXX from terminating.
379 */
380 return lookdict(mp, key, hash);
381 }
382 }
383 else if (ep->me_key == dummy && freeslot == NULL((void *)0))
384 freeslot = ep;
385 }
386 assert(0)(__builtin_expect(!(0), 0) ? __assert_rtn(__func__, "Objects/dictobject.c"
, 386, "0") : (void)0)
; /* NOT REACHED */
387 return 0;
388}
389
390/*
391 * Hacked up version of lookdict which can assume keys are always
392 * unicodes; this assumption allows testing for errors during
393 * PyObject_RichCompareBool() to be dropped; unicode-unicode
394 * comparisons never raise exceptions. This also means we don't need
395 * to go through PyObject_RichCompareBool(); we can always use
396 * unicode_eq() directly.
397 *
398 * This is valuable because dicts with only unicode keys are very common.
399 */
400static PyDictEntry *
401lookdict_unicode(PyDictObject *mp, PyObject *key, register Py_hash_t hash)
402{
403 register size_t i;
404 register size_t perturb;
405 register PyDictEntry *freeslot;
406 register size_t mask = (size_t)mp->ma_mask;
407 PyDictEntry *ep0 = mp->ma_table;
408 register PyDictEntry *ep;
409
410 /* Make sure this function doesn't have to handle non-unicode keys,
411 including subclasses of str; e.g., one reason to subclass
412 unicodes is to override __eq__, and for speed we don't cater to
413 that here. */
414 if (!PyUnicode_CheckExact(key)((((PyObject*)(key))->ob_type) == &PyUnicode_Type)) {
415#ifdef SHOW_CONVERSION_COUNTS
416 ++converted;
417#endif
418 mp->ma_lookup = lookdict;
419 return lookdict(mp, key, hash);
420 }
421 i = hash & mask;
422 ep = &ep0[i];
423 if (ep->me_key == NULL((void *)0) || ep->me_key == key)
424 return ep;
425 if (ep->me_key == dummy)
426 freeslot = ep;
427 else {
428 if (ep->me_hash == hash && unicode_eq(ep->me_key, key))
429 return ep;
430 freeslot = NULL((void *)0);
431 }
432
433 /* In the loop, me_key == dummy is by far (factor of 100s) the
434 least likely outcome, so test for that last. */
435 for (perturb = hash; ; perturb >>= PERTURB_SHIFT5) {
436 i = (i << 2) + i + perturb + 1;
437 ep = &ep0[i & mask];
438 if (ep->me_key == NULL((void *)0))
439 return freeslot == NULL((void *)0) ? ep : freeslot;
440 if (ep->me_key == key
441 || (ep->me_hash == hash
442 && ep->me_key != dummy
443 && unicode_eq(ep->me_key, key)))
444 return ep;
445 if (ep->me_key == dummy && freeslot == NULL((void *)0))
446 freeslot = ep;
447 }
448 assert(0)(__builtin_expect(!(0), 0) ? __assert_rtn(__func__, "Objects/dictobject.c"
, 448, "0") : (void)0)
; /* NOT REACHED */
449 return 0;
450}
451
452int
453_PyDict_HasOnlyStringKeys(PyObject *dict)
454{
455 Py_ssize_t pos = 0;
456 PyObject *key, *value;
457 assert(PyDict_Check(dict))(__builtin_expect(!(((((((PyObject*)(dict))->ob_type))->
tp_flags & ((1L<<29))) != 0)), 0) ? __assert_rtn(__func__
, "Objects/dictobject.c", 457, "PyDict_Check(dict)") : (void)
0)
;
458 /* Shortcut */
459 if (((PyDictObject *)dict)->ma_lookup == lookdict_unicode)
460 return 1;
461 while (PyDict_Next(dict, &pos, &key, &value))
462 if (!PyUnicode_Check(key)((((((PyObject*)(key))->ob_type))->tp_flags & ((1L<<
28))) != 0)
)
463 return 0;
464 return 1;
465}
466
467#ifdef SHOW_TRACK_COUNT
468#define INCREASE_TRACK_COUNT \
469 (count_tracked++, count_untracked--);
470#define DECREASE_TRACK_COUNT \
471 (count_tracked--, count_untracked++);
472#else
473#define INCREASE_TRACK_COUNT
474#define DECREASE_TRACK_COUNT
475#endif
476
477#define MAINTAIN_TRACKING(mp, key, value)do { if (!((((PyGC_Head *)(mp)-1))->gc.gc_refs != (-2))) {
if ((((((((((PyObject*)(key))->ob_type)))->tp_flags &
((1L<<14))) != 0) && ((((PyObject*)(key))->
ob_type)->tp_is_gc == ((void *)0) || (((PyObject*)(key))->
ob_type)->tp_is_gc(key))) && (!((((PyObject*)(key)
)->ob_type) == &PyTuple_Type) || ((((PyGC_Head *)(key)
-1))->gc.gc_refs != (-2)))) || (((((((((PyObject*)(value))
->ob_type)))->tp_flags & ((1L<<14))) != 0) &&
((((PyObject*)(value))->ob_type)->tp_is_gc == ((void *
)0) || (((PyObject*)(value))->ob_type)->tp_is_gc(value)
)) && (!((((PyObject*)(value))->ob_type) == &PyTuple_Type
) || ((((PyGC_Head *)(value)-1))->gc.gc_refs != (-2))))) {
do { PyGC_Head *g = ((PyGC_Head *)(mp)-1); if (g->gc.gc_refs
!= (-2)) Py_FatalError("GC object already tracked"); g->gc
.gc_refs = (-3); g->gc.gc_next = _PyGC_generation0; g->
gc.gc_prev = _PyGC_generation0->gc.gc_prev; g->gc.gc_prev
->gc.gc_next = g; _PyGC_generation0->gc.gc_prev = g; } while
(0);; } } } while(0)
\
478 do { \
479 if (!_PyObject_GC_IS_TRACKED(mp)((((PyGC_Head *)(mp)-1))->gc.gc_refs != (-2))) { \
480 if (_PyObject_GC_MAY_BE_TRACKED(key)(((((((((PyObject*)(key))->ob_type)))->tp_flags & (
(1L<<14))) != 0) && ((((PyObject*)(key))->ob_type
)->tp_is_gc == ((void *)0) || (((PyObject*)(key))->ob_type
)->tp_is_gc(key))) && (!((((PyObject*)(key))->ob_type
) == &PyTuple_Type) || ((((PyGC_Head *)(key)-1))->gc.gc_refs
!= (-2))))
|| \
481 _PyObject_GC_MAY_BE_TRACKED(value)(((((((((PyObject*)(value))->ob_type)))->tp_flags &
((1L<<14))) != 0) && ((((PyObject*)(value))->
ob_type)->tp_is_gc == ((void *)0) || (((PyObject*)(value))
->ob_type)->tp_is_gc(value))) && (!((((PyObject
*)(value))->ob_type) == &PyTuple_Type) || ((((PyGC_Head
*)(value)-1))->gc.gc_refs != (-2))))
) { \
482 _PyObject_GC_TRACK(mp)do { PyGC_Head *g = ((PyGC_Head *)(mp)-1); if (g->gc.gc_refs
!= (-2)) Py_FatalError("GC object already tracked"); g->gc
.gc_refs = (-3); g->gc.gc_next = _PyGC_generation0; g->
gc.gc_prev = _PyGC_generation0->gc.gc_prev; g->gc.gc_prev
->gc.gc_next = g; _PyGC_generation0->gc.gc_prev = g; } while
(0);
; \
483 INCREASE_TRACK_COUNT \
484 } \
485 } \
486 } while(0)
487
488void
489_PyDict_MaybeUntrack(PyObject *op)
490{
491 PyDictObject *mp;
492 PyObject *value;
493 Py_ssize_t mask, i;
494 PyDictEntry *ep;
495
496 if (!PyDict_CheckExact(op)((((PyObject*)(op))->ob_type) == &PyDict_Type) || !_PyObject_GC_IS_TRACKED(op)((((PyGC_Head *)(op)-1))->gc.gc_refs != (-2)))
497 return;
498
499 mp = (PyDictObject *) op;
500 ep = mp->ma_table;
501 mask = mp->ma_mask;
502 for (i = 0; i <= mask; i++) {
503 if ((value = ep[i].me_value) == NULL((void *)0))
504 continue;
505 if (_PyObject_GC_MAY_BE_TRACKED(value)(((((((((PyObject*)(value))->ob_type)))->tp_flags &
((1L<<14))) != 0) && ((((PyObject*)(value))->
ob_type)->tp_is_gc == ((void *)0) || (((PyObject*)(value))
->ob_type)->tp_is_gc(value))) && (!((((PyObject
*)(value))->ob_type) == &PyTuple_Type) || ((((PyGC_Head
*)(value)-1))->gc.gc_refs != (-2))))
||
506 _PyObject_GC_MAY_BE_TRACKED(ep[i].me_key)(((((((((PyObject*)(ep[i].me_key))->ob_type)))->tp_flags
& ((1L<<14))) != 0) && ((((PyObject*)(ep[i
].me_key))->ob_type)->tp_is_gc == ((void *)0) || (((PyObject
*)(ep[i].me_key))->ob_type)->tp_is_gc(ep[i].me_key))) &&
(!((((PyObject*)(ep[i].me_key))->ob_type) == &PyTuple_Type
) || ((((PyGC_Head *)(ep[i].me_key)-1))->gc.gc_refs != (-2
))))
)
507 return;
508 }
509 DECREASE_TRACK_COUNT
510 _PyObject_GC_UNTRACK(op)do { PyGC_Head *g = ((PyGC_Head *)(op)-1); (__builtin_expect(
!(g->gc.gc_refs != (-2)), 0) ? __assert_rtn(__func__, "Objects/dictobject.c"
, 510, "g->gc.gc_refs != _PyGC_REFS_UNTRACKED") : (void)0)
; g->gc.gc_refs = (-2); g->gc.gc_prev->gc.gc_next = g
->gc.gc_next; g->gc.gc_next->gc.gc_prev = g->gc.gc_prev
; g->gc.gc_next = ((void *)0); } while (0);
;
511}
512
513
514/*
515Internal routine to insert a new item into the table.
516Used both by the internal resize routine and by the public insert routine.
517Eats a reference to key and one to value.
518Returns -1 if an error occurred, or 0 on success.
519*/
520static int
521insertdict(register PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
522{
523 PyObject *old_value;
524 register PyDictEntry *ep;
525 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, Py_hash_t);
526
527 assert(mp->ma_lookup != NULL)(__builtin_expect(!(mp->ma_lookup != ((void *)0)), 0) ? __assert_rtn
(__func__, "Objects/dictobject.c", 527, "mp->ma_lookup != NULL"
) : (void)0)
;
528 ep = mp->ma_lookup(mp, key, hash);
529 if (ep == NULL((void *)0)) {
530 Py_DECREF(key)do { if (_Py_RefTotal-- , --((PyObject*)(key))->ob_refcnt !=
0) { if (((PyObject*)key)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 530, (PyObject *)(key)); } else _Py_Dealloc
((PyObject *)(key)); } while (0)
;
531 Py_DECREF(value)do { if (_Py_RefTotal-- , --((PyObject*)(value))->ob_refcnt
!= 0) { if (((PyObject*)value)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 531, (PyObject *)(value)); } else _Py_Dealloc
((PyObject *)(value)); } while (0)
;
532 return -1;
533 }
534 MAINTAIN_TRACKING(mp, key, value)do { if (!((((PyGC_Head *)(mp)-1))->gc.gc_refs != (-2))) {
if ((((((((((PyObject*)(key))->ob_type)))->tp_flags &
((1L<<14))) != 0) && ((((PyObject*)(key))->
ob_type)->tp_is_gc == ((void *)0) || (((PyObject*)(key))->
ob_type)->tp_is_gc(key))) && (!((((PyObject*)(key)
)->ob_type) == &PyTuple_Type) || ((((PyGC_Head *)(key)
-1))->gc.gc_refs != (-2)))) || (((((((((PyObject*)(value))
->ob_type)))->tp_flags & ((1L<<14))) != 0) &&
((((PyObject*)(value))->ob_type)->tp_is_gc == ((void *
)0) || (((PyObject*)(value))->ob_type)->tp_is_gc(value)
)) && (!((((PyObject*)(value))->ob_type) == &PyTuple_Type
) || ((((PyGC_Head *)(value)-1))->gc.gc_refs != (-2))))) {
do { PyGC_Head *g = ((PyGC_Head *)(mp)-1); if (g->gc.gc_refs
!= (-2)) Py_FatalError("GC object already tracked"); g->gc
.gc_refs = (-3); g->gc.gc_next = _PyGC_generation0; g->
gc.gc_prev = _PyGC_generation0->gc.gc_prev; g->gc.gc_prev
->gc.gc_next = g; _PyGC_generation0->gc.gc_prev = g; } while
(0);; } } } while(0)
;
535 if (ep->me_value != NULL((void *)0)) {
536 old_value = ep->me_value;
537 ep->me_value = value;
538 Py_DECREF(old_value)do { if (_Py_RefTotal-- , --((PyObject*)(old_value))->ob_refcnt
!= 0) { if (((PyObject*)old_value)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 538, (PyObject *)(old_value)); } else
_Py_Dealloc((PyObject *)(old_value)); } while (0)
; /* which **CAN** re-enter */
539 Py_DECREF(key)do { if (_Py_RefTotal-- , --((PyObject*)(key))->ob_refcnt !=
0) { if (((PyObject*)key)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 539, (PyObject *)(key)); } else _Py_Dealloc
((PyObject *)(key)); } while (0)
;
540 }
541 else {
542 if (ep->me_key == NULL((void *)0))
543 mp->ma_fill++;
544 else {
545 assert(ep->me_key == dummy)(__builtin_expect(!(ep->me_key == dummy), 0) ? __assert_rtn
(__func__, "Objects/dictobject.c", 545, "ep->me_key == dummy"
) : (void)0)
;
546 Py_DECREF(dummy)do { if (_Py_RefTotal-- , --((PyObject*)(dummy))->ob_refcnt
!= 0) { if (((PyObject*)dummy)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 546, (PyObject *)(dummy)); } else _Py_Dealloc
((PyObject *)(dummy)); } while (0)
;
547 }
548 ep->me_key = key;
549 ep->me_hash = hash;
550 ep->me_value = value;
551 mp->ma_used++;
552 }
553 return 0;
554}
555
556/*
557Internal routine used by dictresize() to insert an item which is
558known to be absent from the dict. This routine also assumes that
559the dict contains no deleted entries. Besides the performance benefit,
560using insertdict() in dictresize() is dangerous (SF bug #1456209).
561Note that no refcounts are changed by this routine; if needed, the caller
562is responsible for incref'ing `key` and `value`.
563*/
564static void
565insertdict_clean(register PyDictObject *mp, PyObject *key, Py_hash_t hash,
566 PyObject *value)
567{
568 register size_t i;
569 register size_t perturb;
570 register size_t mask = (size_t)mp->ma_mask;
571 PyDictEntry *ep0 = mp->ma_table;
572 register PyDictEntry *ep;
573
574 MAINTAIN_TRACKING(mp, key, value)do { if (!((((PyGC_Head *)(mp)-1))->gc.gc_refs != (-2))) {
if ((((((((((PyObject*)(key))->ob_type)))->tp_flags &
((1L<<14))) != 0) && ((((PyObject*)(key))->
ob_type)->tp_is_gc == ((void *)0) || (((PyObject*)(key))->
ob_type)->tp_is_gc(key))) && (!((((PyObject*)(key)
)->ob_type) == &PyTuple_Type) || ((((PyGC_Head *)(key)
-1))->gc.gc_refs != (-2)))) || (((((((((PyObject*)(value))
->ob_type)))->tp_flags & ((1L<<14))) != 0) &&
((((PyObject*)(value))->ob_type)->tp_is_gc == ((void *
)0) || (((PyObject*)(value))->ob_type)->tp_is_gc(value)
)) && (!((((PyObject*)(value))->ob_type) == &PyTuple_Type
) || ((((PyGC_Head *)(value)-1))->gc.gc_refs != (-2))))) {
do { PyGC_Head *g = ((PyGC_Head *)(mp)-1); if (g->gc.gc_refs
!= (-2)) Py_FatalError("GC object already tracked"); g->gc
.gc_refs = (-3); g->gc.gc_next = _PyGC_generation0; g->
gc.gc_prev = _PyGC_generation0->gc.gc_prev; g->gc.gc_prev
->gc.gc_next = g; _PyGC_generation0->gc.gc_prev = g; } while
(0);; } } } while(0)
;
575 i = hash & mask;
576 ep = &ep0[i];
577 for (perturb = hash; ep->me_key != NULL((void *)0); perturb >>= PERTURB_SHIFT5) {
578 i = (i << 2) + i + perturb + 1;
579 ep = &ep0[i & mask];
580 }
581 assert(ep->me_value == NULL)(__builtin_expect(!(ep->me_value == ((void *)0)), 0) ? __assert_rtn
(__func__, "Objects/dictobject.c", 581, "ep->me_value == NULL"
) : (void)0)
;
582 mp->ma_fill++;
583 ep->me_key = key;
584 ep->me_hash = hash;
585 ep->me_value = value;
586 mp->ma_used++;
587}
588
589/*
590Restructure the table by allocating a new table and reinserting all
591items again. When entries have been deleted, the new table may
592actually be smaller than the old one.
593*/
594static int
595dictresize(PyDictObject *mp, Py_ssize_t minused)
596{
597 Py_ssize_t newsize;
598 PyDictEntry *oldtable, *newtable, *ep;
599 Py_ssize_t i;
600 int is_oldtable_malloced;
601 PyDictEntry small_copy[PyDict_MINSIZE8];
602
603 assert(minused >= 0)(__builtin_expect(!(minused >= 0), 0) ? __assert_rtn(__func__
, "Objects/dictobject.c", 603, "minused >= 0") : (void)0)
;
604
605 /* Find the smallest table size > minused. */
606 for (newsize = PyDict_MINSIZE8;
607 newsize <= minused && newsize > 0;
608 newsize <<= 1)
609 ;
610 if (newsize <= 0) {
611 PyErr_NoMemory();
612 return -1;
613 }
614
615 /* Get space for a new table. */
616 oldtable = mp->ma_table;
617 assert(oldtable != NULL)(__builtin_expect(!(oldtable != ((void *)0)), 0) ? __assert_rtn
(__func__, "Objects/dictobject.c", 617, "oldtable != NULL") :
(void)0)
;
618 is_oldtable_malloced = oldtable != mp->ma_smalltable;
619
620 if (newsize == PyDict_MINSIZE8) {
621 /* A large table is shrinking, or we can't get any smaller. */
622 newtable = mp->ma_smalltable;
623 if (newtable == oldtable) {
624 if (mp->ma_fill == mp->ma_used) {
625 /* No dummies, so no point doing anything. */
626 return 0;
627 }
628 /* We're not going to resize it, but rebuild the
629 table anyway to purge old dummy entries.
630 Subtle: This is *necessary* if fill==size,
631 as lookdict needs at least one virgin slot to
632 terminate failing searches. If fill < size, it's
633 merely desirable, as dummies slow searches. */
634 assert(mp->ma_fill > mp->ma_used)(__builtin_expect(!(mp->ma_fill > mp->ma_used), 0) ?
__assert_rtn(__func__, "Objects/dictobject.c", 634, "mp->ma_fill > mp->ma_used"
) : (void)0)
;
635 memcpy(small_copy, oldtable, sizeof(small_copy))((__builtin_object_size (small_copy, 0) != (size_t) -1) ? __builtin___memcpy_chk
(small_copy, oldtable, sizeof(small_copy), __builtin_object_size
(small_copy, 0)) : __inline_memcpy_chk (small_copy, oldtable
, sizeof(small_copy)))
;
636 oldtable = small_copy;
637 }
638 }
639 else {
640 newtable = PyMem_NEW(PyDictEntry, newsize)( ((size_t)(newsize) > ((Py_ssize_t)(((size_t)-1)>>1
)) / sizeof(PyDictEntry)) ? ((void *)0) : ( (PyDictEntry *) _PyMem_DebugMalloc
((newsize) * sizeof(PyDictEntry)) ) )
;
641 if (newtable == NULL((void *)0)) {
642 PyErr_NoMemory();
643 return -1;
644 }
645 }
646
647 /* Make the dict empty, using the new table. */
648 assert(newtable != oldtable)(__builtin_expect(!(newtable != oldtable), 0) ? __assert_rtn(
__func__, "Objects/dictobject.c", 648, "newtable != oldtable"
) : (void)0)
;
649 mp->ma_table = newtable;
650 mp->ma_mask = newsize - 1;
651 memset(newtable, 0, sizeof(PyDictEntry) * newsize)((__builtin_object_size (newtable, 0) != (size_t) -1) ? __builtin___memset_chk
(newtable, 0, sizeof(PyDictEntry) * newsize, __builtin_object_size
(newtable, 0)) : __inline_memset_chk (newtable, 0, sizeof(PyDictEntry
) * newsize))
;
652 mp->ma_used = 0;
653 i = mp->ma_fill;
654 mp->ma_fill = 0;
655
656 /* Copy the data over; this is refcount-neutral for active entries;
657 dummy entries aren't copied over, of course */
658 for (ep = oldtable; i > 0; ep++) {
659 if (ep->me_value != NULL((void *)0)) { /* active entry */
660 --i;
661 insertdict_clean(mp, ep->me_key, ep->me_hash, ep->me_value);
662 }
663 else if (ep->me_key != NULL((void *)0)) { /* dummy entry */
664 --i;
665 assert(ep->me_key == dummy)(__builtin_expect(!(ep->me_key == dummy), 0) ? __assert_rtn
(__func__, "Objects/dictobject.c", 665, "ep->me_key == dummy"
) : (void)0)
;
666 Py_DECREF(ep->me_key)do { if (_Py_RefTotal-- , --((PyObject*)(ep->me_key))->
ob_refcnt != 0) { if (((PyObject*)ep->me_key)->ob_refcnt
< 0) _Py_NegativeRefcount("Objects/dictobject.c", 666, (PyObject
*)(ep->me_key)); } else _Py_Dealloc((PyObject *)(ep->me_key
)); } while (0)
;
667 }
668 /* else key == value == NULL: nothing to do */
669 }
670
671 if (is_oldtable_malloced)
672 PyMem_DEL_PyMem_DebugFree(oldtable);
673 return 0;
674}
675
676/* Create a new dictionary pre-sized to hold an estimated number of elements.
677 Underestimates are okay because the dictionary will resize as necessary.
678 Overestimates just mean the dictionary will be more sparse than usual.
679*/
680
681PyObject *
682_PyDict_NewPresized(Py_ssize_t minused)
683{
684 PyObject *op = PyDict_New();
685
686 if (minused>5 && op != NULL((void *)0) && dictresize((PyDictObject *)op, minused) == -1) {
687 Py_DECREF(op)do { if (_Py_RefTotal-- , --((PyObject*)(op))->ob_refcnt !=
0) { if (((PyObject*)op)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 687, (PyObject *)(op)); } else _Py_Dealloc
((PyObject *)(op)); } while (0)
;
688 return NULL((void *)0);
689 }
690 return op;
691}
692
693/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
694 * that may occur (originally dicts supported only string keys, and exceptions
695 * weren't possible). So, while the original intent was that a NULL return
696 * meant the key wasn't present, in reality it can mean that, or that an error
697 * (suppressed) occurred while computing the key's hash, or that some error
698 * (suppressed) occurred when comparing keys in the dict's internal probe
699 * sequence. A nasty example of the latter is when a Python-coded comparison
700 * function hits a stack-depth error, which can cause this to return NULL
701 * even if the key is present.
702 */
703PyObject *
704PyDict_GetItem(PyObject *op, PyObject *key)
705{
706 Py_hash_t hash;
707 PyDictObject *mp = (PyDictObject *)op;
708 PyDictEntry *ep;
709 PyThreadState *tstate;
710 if (!PyDict_Check(op)((((((PyObject*)(op))->ob_type))->tp_flags & ((1L<<
29))) != 0)
)
711 return NULL((void *)0);
712 if (!PyUnicode_CheckExact(key)((((PyObject*)(key))->ob_type) == &PyUnicode_Type) ||
713 (hash = ((PyUnicodeObject *) key)->hash) == -1)
714 {
715 hash = PyObject_Hash(key);
716 if (hash == -1) {
717 PyErr_Clear();
718 return NULL((void *)0);
719 }
720 }
721
722 /* We can arrive here with a NULL tstate during initialization: try
723 running "python -Wi" for an example related to string interning.
724 Let's just hope that no exception occurs then... This must be
725 _PyThreadState_Current and not PyThreadState_GET() because in debug
726 mode, the latter complains if tstate is NULL. */
727 tstate = (PyThreadState*)_Py_atomic_load_relaxed(__extension__ ({ __typeof__(&_PyThreadState_Current) atomic_val
= &_PyThreadState_Current; __typeof__(atomic_val->_value
) result; volatile __typeof__(result) *volatile_data = &atomic_val
->_value; _Py_memory_order order = _Py_memory_order_relaxed
; _Py_ANNOTATE_MEMORY_ORDER(atomic_val, order); ; switch(order
) { case _Py_memory_order_release: case _Py_memory_order_acq_rel
: case _Py_memory_order_seq_cst: _Py_atomic_thread_fence(_Py_memory_order_release
); break; default: break; } result = *volatile_data; switch(order
) { case _Py_memory_order_acquire: case _Py_memory_order_acq_rel
: case _Py_memory_order_seq_cst: _Py_atomic_signal_fence(_Py_memory_order_acquire
); break; default: break; } ; result; })
728 &_PyThreadState_Current)__extension__ ({ __typeof__(&_PyThreadState_Current) atomic_val
= &_PyThreadState_Current; __typeof__(atomic_val->_value
) result; volatile __typeof__(result) *volatile_data = &atomic_val
->_value; _Py_memory_order order = _Py_memory_order_relaxed
; _Py_ANNOTATE_MEMORY_ORDER(atomic_val, order); ; switch(order
) { case _Py_memory_order_release: case _Py_memory_order_acq_rel
: case _Py_memory_order_seq_cst: _Py_atomic_thread_fence(_Py_memory_order_release
); break; default: break; } result = *volatile_data; switch(order
) { case _Py_memory_order_acquire: case _Py_memory_order_acq_rel
: case _Py_memory_order_seq_cst: _Py_atomic_signal_fence(_Py_memory_order_acquire
); break; default: break; } ; result; })
;
729 if (tstate != NULL((void *)0) && tstate->curexc_type != NULL((void *)0)) {
730 /* preserve the existing exception */
731 PyObject *err_type, *err_value, *err_tb;
732 PyErr_Fetch(&err_type, &err_value, &err_tb);
733 ep = (mp->ma_lookup)(mp, key, hash);
734 /* ignore errors */
735 PyErr_Restore(err_type, err_value, err_tb);
736 if (ep == NULL((void *)0))
737 return NULL((void *)0);
738 }
739 else {
740 ep = (mp->ma_lookup)(mp, key, hash);
741 if (ep == NULL((void *)0)) {
742 PyErr_Clear();
743 return NULL((void *)0);
744 }
745 }
746 return ep->me_value;
747}
748
749/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
750 This returns NULL *with* an exception set if an exception occurred.
751 It returns NULL *without* an exception set if the key wasn't present.
752*/
753PyObject *
754PyDict_GetItemWithError(PyObject *op, PyObject *key)
755{
756 Py_hash_t hash;
757 PyDictObject*mp = (PyDictObject *)op;
758 PyDictEntry *ep;
759
760 if (!PyDict_Check(op)((((((PyObject*)(op))->ob_type))->tp_flags & ((1L<<
29))) != 0)
) {
761 PyErr_BadInternalCall()_PyErr_BadInternalCall("Objects/dictobject.c", 761);
762 return NULL((void *)0);
763 }
764 if (!PyUnicode_CheckExact(key)((((PyObject*)(key))->ob_type) == &PyUnicode_Type) ||
765 (hash = ((PyUnicodeObject *) key)->hash) == -1)
766 {
767 hash = PyObject_Hash(key);
768 if (hash == -1) {
769 return NULL((void *)0);
770 }
771 }
772
773 ep = (mp->ma_lookup)(mp, key, hash);
774 if (ep == NULL((void *)0))
775 return NULL((void *)0);
776 return ep->me_value;
777}
778
779/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
780 * dictionary if it's merely replacing the value for an existing key.
781 * This means that it's safe to loop over a dictionary with PyDict_Next()
782 * and occasionally replace a value -- but you can't insert new keys or
783 * remove them.
784 */
785int
786PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
787{
788 register PyDictObject *mp;
789 register Py_hash_t hash;
790 register Py_ssize_t n_used;
791
792 if (!PyDict_Check(op)((((((PyObject*)(op))->ob_type))->tp_flags & ((1L<<
29))) != 0)
) {
793 PyErr_BadInternalCall()_PyErr_BadInternalCall("Objects/dictobject.c", 793);
794 return -1;
795 }
796 assert(key)(__builtin_expect(!(key), 0) ? __assert_rtn(__func__, "Objects/dictobject.c"
, 796, "key") : (void)0)
;
797 assert(value)(__builtin_expect(!(value), 0) ? __assert_rtn(__func__, "Objects/dictobject.c"
, 797, "value") : (void)0)
;
798 mp = (PyDictObject *)op;
799 if (!PyUnicode_CheckExact(key)((((PyObject*)(key))->ob_type) == &PyUnicode_Type) ||
800 (hash = ((PyUnicodeObject *) key)->hash) == -1)
801 {
802 hash = PyObject_Hash(key);
803 if (hash == -1)
804 return -1;
805 }
806 assert(mp->ma_fill <= mp->ma_mask)(__builtin_expect(!(mp->ma_fill <= mp->ma_mask), 0) ?
__assert_rtn(__func__, "Objects/dictobject.c", 806, "mp->ma_fill <= mp->ma_mask"
) : (void)0)
; /* at least one empty slot */
807 n_used = mp->ma_used;
808 Py_INCREF(value)( _Py_RefTotal++ , ((PyObject*)(value))->ob_refcnt++);
809 Py_INCREF(key)( _Py_RefTotal++ , ((PyObject*)(key))->ob_refcnt++);
810 if (insertdict(mp, key, hash, value) != 0)
811 return -1;
812 /* If we added a key, we can safely resize. Otherwise just return!
813 * If fill >= 2/3 size, adjust size. Normally, this doubles or
814 * quaduples the size, but it's also possible for the dict to shrink
815 * (if ma_fill is much larger than ma_used, meaning a lot of dict
816 * keys have been * deleted).
817 *
818 * Quadrupling the size improves average dictionary sparseness
819 * (reducing collisions) at the cost of some memory and iteration
820 * speed (which loops over every possible entry). It also halves
821 * the number of expensive resize operations in a growing dictionary.
822 *
823 * Very large dictionaries (over 50K items) use doubling instead.
824 * This may help applications with severe memory constraints.
825 */
826 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
827 return 0;
828 return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
829}
830
831int
832PyDict_DelItem(PyObject *op, PyObject *key)
833{
834 register PyDictObject *mp;
835 register Py_hash_t hash;
836 register PyDictEntry *ep;
837 PyObject *old_value, *old_key;
838
839 if (!PyDict_Check(op)((((((PyObject*)(op))->ob_type))->tp_flags & ((1L<<
29))) != 0)
) {
840 PyErr_BadInternalCall()_PyErr_BadInternalCall("Objects/dictobject.c", 840);
841 return -1;
842 }
843 assert(key)(__builtin_expect(!(key), 0) ? __assert_rtn(__func__, "Objects/dictobject.c"
, 843, "key") : (void)0)
;
844 if (!PyUnicode_CheckExact(key)((((PyObject*)(key))->ob_type) == &PyUnicode_Type) ||
845 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
846 hash = PyObject_Hash(key);
847 if (hash == -1)
848 return -1;
849 }
850 mp = (PyDictObject *)op;
851 ep = (mp->ma_lookup)(mp, key, hash);
852 if (ep == NULL((void *)0))
853 return -1;
854 if (ep->me_value == NULL((void *)0)) {
855 set_key_error(key);
856 return -1;
857 }
858 old_key = ep->me_key;
859 Py_INCREF(dummy)( _Py_RefTotal++ , ((PyObject*)(dummy))->ob_refcnt++);
860 ep->me_key = dummy;
861 old_value = ep->me_value;
862 ep->me_value = NULL((void *)0);
863 mp->ma_used--;
864 Py_DECREF(old_value)do { if (_Py_RefTotal-- , --((PyObject*)(old_value))->ob_refcnt
!= 0) { if (((PyObject*)old_value)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 864, (PyObject *)(old_value)); } else
_Py_Dealloc((PyObject *)(old_value)); } while (0)
;
865 Py_DECREF(old_key)do { if (_Py_RefTotal-- , --((PyObject*)(old_key))->ob_refcnt
!= 0) { if (((PyObject*)old_key)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 865, (PyObject *)(old_key)); } else _Py_Dealloc
((PyObject *)(old_key)); } while (0)
;
866 return 0;
867}
868
869void
870PyDict_Clear(PyObject *op)
871{
872 PyDictObject *mp;
873 PyDictEntry *ep, *table;
874 int table_is_malloced;
875 Py_ssize_t fill;
876 PyDictEntry small_copy[PyDict_MINSIZE8];
877#ifdef Py_DEBUG1
878 Py_ssize_t i, n;
879#endif
880
881 if (!PyDict_Check(op)((((((PyObject*)(op))->ob_type))->tp_flags & ((1L<<
29))) != 0)
)
882 return;
883 mp = (PyDictObject *)op;
884#ifdef Py_DEBUG1
885 n = mp->ma_mask + 1;
886 i = 0;
887#endif
888
889 table = mp->ma_table;
890 assert(table != NULL)(__builtin_expect(!(table != ((void *)0)), 0) ? __assert_rtn(
__func__, "Objects/dictobject.c", 890, "table != NULL") : (void
)0)
;
891 table_is_malloced = table != mp->ma_smalltable;
892
893 /* This is delicate. During the process of clearing the dict,
894 * decrefs can cause the dict to mutate. To avoid fatal confusion
895 * (voice of experience), we have to make the dict empty before
896 * clearing the slots, and never refer to anything via mp->xxx while
897 * clearing.
898 */
899 fill = mp->ma_fill;
900 if (table_is_malloced)
901 EMPTY_TO_MINSIZE(mp)do { ((__builtin_object_size ((mp)->ma_smalltable, 0) != (
size_t) -1) ? __builtin___memset_chk ((mp)->ma_smalltable,
0, sizeof((mp)->ma_smalltable), __builtin_object_size ((mp
)->ma_smalltable, 0)) : __inline_memset_chk ((mp)->ma_smalltable
, 0, sizeof((mp)->ma_smalltable))); (mp)->ma_used = (mp
)->ma_fill = 0; do { (mp)->ma_table = (mp)->ma_smalltable
; (mp)->ma_mask = 8 - 1; } while(0); } while(0)
;
902
903 else if (fill > 0) {
904 /* It's a small table with something that needs to be cleared.
905 * Afraid the only safe way is to copy the dict entries into
906 * another small table first.
907 */
908 memcpy(small_copy, table, sizeof(small_copy))((__builtin_object_size (small_copy, 0) != (size_t) -1) ? __builtin___memcpy_chk
(small_copy, table, sizeof(small_copy), __builtin_object_size
(small_copy, 0)) : __inline_memcpy_chk (small_copy, table, sizeof
(small_copy)))
;
909 table = small_copy;
910 EMPTY_TO_MINSIZE(mp)do { ((__builtin_object_size ((mp)->ma_smalltable, 0) != (
size_t) -1) ? __builtin___memset_chk ((mp)->ma_smalltable,
0, sizeof((mp)->ma_smalltable), __builtin_object_size ((mp
)->ma_smalltable, 0)) : __inline_memset_chk ((mp)->ma_smalltable
, 0, sizeof((mp)->ma_smalltable))); (mp)->ma_used = (mp
)->ma_fill = 0; do { (mp)->ma_table = (mp)->ma_smalltable
; (mp)->ma_mask = 8 - 1; } while(0); } while(0)
;
911 }
912 /* else it's a small table that's already empty */
913
914 /* Now we can finally clear things. If C had refcounts, we could
915 * assert that the refcount on table is 1 now, i.e. that this function
916 * has unique access to it, so decref side-effects can't alter it.
917 */
918 for (ep = table; fill > 0; ++ep) {
919#ifdef Py_DEBUG1
920 assert(i < n)(__builtin_expect(!(i < n), 0) ? __assert_rtn(__func__, "Objects/dictobject.c"
, 920, "i < n") : (void)0)
;
921 ++i;
922#endif
923 if (ep->me_key) {
924 --fill;
925 Py_DECREF(ep->me_key)do { if (_Py_RefTotal-- , --((PyObject*)(ep->me_key))->
ob_refcnt != 0) { if (((PyObject*)ep->me_key)->ob_refcnt
< 0) _Py_NegativeRefcount("Objects/dictobject.c", 925, (PyObject
*)(ep->me_key)); } else _Py_Dealloc((PyObject *)(ep->me_key
)); } while (0)
;
926 Py_XDECREF(ep->me_value)do { if ((ep->me_value) == ((void *)0)) ; else do { if (_Py_RefTotal
-- , --((PyObject*)(ep->me_value))->ob_refcnt != 0) { if
(((PyObject*)ep->me_value)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 926, (PyObject *)(ep->me_value));
} else _Py_Dealloc((PyObject *)(ep->me_value)); } while (
0); } while (0)
;
927 }
928#ifdef Py_DEBUG1
929 else
930 assert(ep->me_value == NULL)(__builtin_expect(!(ep->me_value == ((void *)0)), 0) ? __assert_rtn
(__func__, "Objects/dictobject.c", 930, "ep->me_value == NULL"
) : (void)0)
;
931#endif
932 }
933
934 if (table_is_malloced)
935 PyMem_DEL_PyMem_DebugFree(table);
936}
937
938/*
939 * Iterate over a dict. Use like so:
940 *
941 * Py_ssize_t i;
942 * PyObject *key, *value;
943 * i = 0; # important! i should not otherwise be changed by you
944 * while (PyDict_Next(yourdict, &i, &key, &value)) {
945 * Refer to borrowed references in key and value.
946 * }
947 *
948 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
949 * mutates the dict. One exception: it is safe if the loop merely changes
950 * the values associated with the keys (but doesn't insert new keys or
951 * delete keys), via PyDict_SetItem().
952 */
953int
954PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
955{
956 register Py_ssize_t i;
957 register Py_ssize_t mask;
958 register PyDictEntry *ep;
959
960 if (!PyDict_Check(op)((((((PyObject*)(op))->ob_type))->tp_flags & ((1L<<
29))) != 0)
)
961 return 0;
962 i = *ppos;
963 if (i < 0)
964 return 0;
965 ep = ((PyDictObject *)op)->ma_table;
966 mask = ((PyDictObject *)op)->ma_mask;
967 while (i <= mask && ep[i].me_value == NULL((void *)0))
968 i++;
969 *ppos = i+1;
970 if (i > mask)
971 return 0;
972 if (pkey)
973 *pkey = ep[i].me_key;
974 if (pvalue)
975 *pvalue = ep[i].me_value;
976 return 1;
977}
978
979/* Internal version of PyDict_Next that returns a hash value in addition to the key and value.*/
980int
981_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue, Py_hash_t *phash)
982{
983 register Py_ssize_t i;
984 register Py_ssize_t mask;
985 register PyDictEntry *ep;
986
987 if (!PyDict_Check(op)((((((PyObject*)(op))->ob_type))->tp_flags & ((1L<<
29))) != 0)
)
988 return 0;
989 i = *ppos;
990 if (i < 0)
991 return 0;
992 ep = ((PyDictObject *)op)->ma_table;
993 mask = ((PyDictObject *)op)->ma_mask;
994 while (i <= mask && ep[i].me_value == NULL((void *)0))
995 i++;
996 *ppos = i+1;
997 if (i > mask)
998 return 0;
999 *phash = ep[i].me_hash;
1000 if (pkey)
1001 *pkey = ep[i].me_key;
1002 if (pvalue)
1003 *pvalue = ep[i].me_value;
1004 return 1;
1005}
1006
1007/* Methods */
1008
1009static void
1010dict_dealloc(register PyDictObject *mp)
1011{
1012 register PyDictEntry *ep;
1013 Py_ssize_t fill = mp->ma_fill;
1014 PyObject_GC_UnTrack(mp);
1015 Py_TRASHCAN_SAFE_BEGIN(mp)if (_PyTrash_delete_nesting < 50) { ++_PyTrash_delete_nesting
;
1016 for (ep = mp->ma_table; fill > 0; ep++) {
1017 if (ep->me_key) {
1018 --fill;
1019 Py_DECREF(ep->me_key)do { if (_Py_RefTotal-- , --((PyObject*)(ep->me_key))->
ob_refcnt != 0) { if (((PyObject*)ep->me_key)->ob_refcnt
< 0) _Py_NegativeRefcount("Objects/dictobject.c", 1019, (
PyObject *)(ep->me_key)); } else _Py_Dealloc((PyObject *)(
ep->me_key)); } while (0)
;
1020 Py_XDECREF(ep->me_value)do { if ((ep->me_value) == ((void *)0)) ; else do { if (_Py_RefTotal
-- , --((PyObject*)(ep->me_value))->ob_refcnt != 0) { if
(((PyObject*)ep->me_value)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 1020, (PyObject *)(ep->me_value))
; } else _Py_Dealloc((PyObject *)(ep->me_value)); } while (
0); } while (0)
;
1021 }
1022 }
1023 if (mp->ma_table != mp->ma_smalltable)
1024 PyMem_DEL_PyMem_DebugFree(mp->ma_table);
1025 if (numfree < PyDict_MAXFREELIST80 && Py_TYPE(mp)(((PyObject*)(mp))->ob_type) == &PyDict_Type)
1026 free_list[numfree++] = mp;
1027 else
1028 Py_TYPE(mp)(((PyObject*)(mp))->ob_type)->tp_free((PyObject *)mp);
1029 Py_TRASHCAN_SAFE_END(mp)--_PyTrash_delete_nesting; if (_PyTrash_delete_later &&
_PyTrash_delete_nesting <= 0) _PyTrash_destroy_chain(); }
else _PyTrash_deposit_object((PyObject*)mp);
1030}
1031
1032static PyObject *
1033dict_repr(PyDictObject *mp)
1034{
1035 Py_ssize_t i;
1036 PyObject *s, *temp, *colon = NULL((void *)0);
1037 PyObject *pieces = NULL((void *)0), *result = NULL((void *)0);
1038 PyObject *key, *value;
1039
1040 i = Py_ReprEnter((PyObject *)mp);
1041 if (i != 0) {
1042 return i > 0 ? PyUnicode_FromStringPyUnicodeUCS2_FromString("{...}") : NULL((void *)0);
1043 }
1044
1045 if (mp->ma_used == 0) {
1046 result = PyUnicode_FromStringPyUnicodeUCS2_FromString("{}");
1047 goto Done;
1048 }
1049
1050 pieces = PyList_New(0);
1051 if (pieces == NULL((void *)0))
1052 goto Done;
1053
1054 colon = PyUnicode_FromStringPyUnicodeUCS2_FromString(": ");
1055 if (colon == NULL((void *)0))
1056 goto Done;
1057
1058 /* Do repr() on each key+value pair, and insert ": " between them.
1059 Note that repr may mutate the dict. */
1060 i = 0;
1061 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
1062 int status;
1063 /* Prevent repr from deleting value during key format. */
1064 Py_INCREF(value)( _Py_RefTotal++ , ((PyObject*)(value))->ob_refcnt++);
1065 s = PyObject_Repr(key);
1066 PyUnicode_AppendPyUnicodeUCS2_Append(&s, colon);
1067 PyUnicode_AppendAndDelPyUnicodeUCS2_AppendAndDel(&s, PyObject_Repr(value));
1068 Py_DECREF(value)do { if (_Py_RefTotal-- , --((PyObject*)(value))->ob_refcnt
!= 0) { if (((PyObject*)value)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 1068, (PyObject *)(value)); } else _Py_Dealloc
((PyObject *)(value)); } while (0)
;
1069 if (s == NULL((void *)0))
1070 goto Done;
1071 status = PyList_Append(pieces, s);
1072 Py_DECREF(s)do { if (_Py_RefTotal-- , --((PyObject*)(s))->ob_refcnt !=
0) { if (((PyObject*)s)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 1072, (PyObject *)(s)); } else _Py_Dealloc
((PyObject *)(s)); } while (0)
; /* append created a new ref */
1073 if (status < 0)
1074 goto Done;
1075 }
1076
1077 /* Add "{}" decorations to the first and last items. */
1078 assert(PyList_GET_SIZE(pieces) > 0)(__builtin_expect(!((((PyVarObject*)(pieces))->ob_size) >
0), 0) ? __assert_rtn(__func__, "Objects/dictobject.c", 1078
, "PyList_GET_SIZE(pieces) > 0") : (void)0)
;
1079 s = PyUnicode_FromStringPyUnicodeUCS2_FromString("{");
1080 if (s == NULL((void *)0))
1081 goto Done;
1082 temp = PyList_GET_ITEM(pieces, 0)(((PyListObject *)(pieces))->ob_item[0]);
1083 PyUnicode_AppendAndDelPyUnicodeUCS2_AppendAndDel(&s, temp);
1084 PyList_SET_ITEM(pieces, 0, s)(((PyListObject *)(pieces))->ob_item[0] = (s));
1085 if (s == NULL((void *)0))
1086 goto Done;
1087
1088 s = PyUnicode_FromStringPyUnicodeUCS2_FromString("}");
1089 if (s == NULL((void *)0))
1090 goto Done;
1091 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1)(((PyListObject *)(pieces))->ob_item[(((PyVarObject*)(pieces
))->ob_size) - 1])
;
1092 PyUnicode_AppendAndDelPyUnicodeUCS2_AppendAndDel(&temp, s);
1093 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp)(((PyListObject *)(pieces))->ob_item[(((PyVarObject*)(pieces
))->ob_size) - 1] = (temp))
;
1094 if (temp == NULL((void *)0))
1095 goto Done;
1096
1097 /* Paste them all together with ", " between. */
1098 s = PyUnicode_FromStringPyUnicodeUCS2_FromString(", ");
1099 if (s == NULL((void *)0))
1100 goto Done;
1101 result = PyUnicode_JoinPyUnicodeUCS2_Join(s, pieces);
1102 Py_DECREF(s)do { if (_Py_RefTotal-- , --((PyObject*)(s))->ob_refcnt !=
0) { if (((PyObject*)s)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 1102, (PyObject *)(s)); } else _Py_Dealloc
((PyObject *)(s)); } while (0)
;
1103
1104Done:
1105 Py_XDECREF(pieces)do { if ((pieces) == ((void *)0)) ; else do { if (_Py_RefTotal
-- , --((PyObject*)(pieces))->ob_refcnt != 0) { if (((PyObject
*)pieces)->ob_refcnt < 0) _Py_NegativeRefcount("Objects/dictobject.c"
, 1105, (PyObject *)(pieces)); } else _Py_Dealloc((PyObject *
)(pieces)); } while (0); } while (0)
;
1106 Py_XDECREF(colon)do { if ((colon) == ((void *)0)) ; else do { if (_Py_RefTotal
-- , --((PyObject*)(colon))->ob_refcnt != 0) { if (((PyObject
*)colon)->ob_refcnt < 0) _Py_NegativeRefcount("Objects/dictobject.c"
, 1106, (PyObject *)(colon)); } else _Py_Dealloc((PyObject *)
(colon)); } while (0); } while (0)
;
1107 Py_ReprLeave((PyObject *)mp);
1108 return result;
1109}
1110
1111static Py_ssize_t
1112dict_length(PyDictObject *mp)
1113{
1114 return mp->ma_used;
1115}
1116
1117static PyObject *
1118dict_subscript(PyDictObject *mp, register PyObject *key)
1119{
1120 PyObject *v;
1121 Py_hash_t hash;
1122 PyDictEntry *ep;
1123 assert(mp->ma_table != NULL)(__builtin_expect(!(mp->ma_table != ((void *)0)), 0) ? __assert_rtn
(__func__, "Objects/dictobject.c", 1123, "mp->ma_table != NULL"
) : (void)0)
;
1124 if (!PyUnicode_CheckExact(key)((((PyObject*)(key))->ob_type) == &PyUnicode_Type) ||
1125 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
1126 hash = PyObject_Hash(key);
1127 if (hash == -1)
1128 return NULL((void *)0);
1129 }
1130 ep = (mp->ma_lookup)(mp, key, hash);
1131 if (ep == NULL((void *)0))
1132 return NULL((void *)0);
1133 v = ep->me_value;
1134 if (v == NULL((void *)0)) {
1135 if (!PyDict_CheckExact(mp)((((PyObject*)(mp))->ob_type) == &PyDict_Type)) {
1136 /* Look up __missing__ method if we're a subclass. */
1137 PyObject *missing, *res;
1138 static PyObject *missing_str = NULL((void *)0);
1139 missing = _PyObject_LookupSpecial((PyObject *)mp,
1140 "__missing__",
1141 &missing_str);
1142 if (missing != NULL((void *)0)) {
1143 res = PyObject_CallFunctionObjArgs(missing,
1144 key, NULL((void *)0));
1145 Py_DECREF(missing)do { if (_Py_RefTotal-- , --((PyObject*)(missing))->ob_refcnt
!= 0) { if (((PyObject*)missing)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 1145, (PyObject *)(missing)); } else
_Py_Dealloc((PyObject *)(missing)); } while (0)
;
1146 return res;
1147 }
1148 else if (PyErr_Occurred())
1149 return NULL((void *)0);
1150 }
1151 set_key_error(key);
1152 return NULL((void *)0);
1153 }
1154 else
1155 Py_INCREF(v)( _Py_RefTotal++ , ((PyObject*)(v))->ob_refcnt++);
1156 return v;
1157}
1158
1159static int
1160dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
1161{
1162 if (w == NULL((void *)0))
1163 return PyDict_DelItem((PyObject *)mp, v);
1164 else
1165 return PyDict_SetItem((PyObject *)mp, v, w);
1166}
1167
1168static PyMappingMethods dict_as_mapping = {
1169 (lenfunc)dict_length, /*mp_length*/
1170 (binaryfunc)dict_subscript, /*mp_subscript*/
1171 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
1172};
1173
1174static PyObject *
1175dict_keys(register PyDictObject *mp)
1176{
1177 register PyObject *v;
1178 register Py_ssize_t i, j;
1179 PyDictEntry *ep;
1180 Py_ssize_t mask, n;
1181
1182 again:
1183 n = mp->ma_used;
1184 v = PyList_New(n);
1185 if (v == NULL((void *)0))
1186 return NULL((void *)0);
1187 if (n != mp->ma_used) {
1188 /* Durnit. The allocations caused the dict to resize.
1189 * Just start over, this shouldn't normally happen.
1190 */
1191 Py_DECREF(v)do { if (_Py_RefTotal-- , --((PyObject*)(v))->ob_refcnt !=
0) { if (((PyObject*)v)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 1191, (PyObject *)(v)); } else _Py_Dealloc
((PyObject *)(v)); } while (0)
;
1192 goto again;
1193 }
1194 ep = mp->ma_table;
1195 mask = mp->ma_mask;
1196 for (i = 0, j = 0; i <= mask; i++) {
1197 if (ep[i].me_value != NULL((void *)0)) {
1198 PyObject *key = ep[i].me_key;
1199 Py_INCREF(key)( _Py_RefTotal++ , ((PyObject*)(key))->ob_refcnt++);
1200 PyList_SET_ITEM(v, j, key)(((PyListObject *)(v))->ob_item[j] = (key));
1201 j++;
1202 }
1203 }
1204 assert(j == n)(__builtin_expect(!(j == n), 0) ? __assert_rtn(__func__, "Objects/dictobject.c"
, 1204, "j == n") : (void)0)
;
1205 return v;
1206}
1207
1208static PyObject *
1209dict_values(register PyDictObject *mp)
1210{
1211 register PyObject *v;
1212 register Py_ssize_t i, j;
1213 PyDictEntry *ep;
1214 Py_ssize_t mask, n;
1215
1216 again:
1217 n = mp->ma_used;
1218 v = PyList_New(n);
1219 if (v == NULL((void *)0))
1220 return NULL((void *)0);
1221 if (n != mp->ma_used) {
1222 /* Durnit. The allocations caused the dict to resize.
1223 * Just start over, this shouldn't normally happen.
1224 */
1225 Py_DECREF(v)do { if (_Py_RefTotal-- , --((PyObject*)(v))->ob_refcnt !=
0) { if (((PyObject*)v)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 1225, (PyObject *)(v)); } else _Py_Dealloc
((PyObject *)(v)); } while (0)
;
1226 goto again;
1227 }
1228 ep = mp->ma_table;
1229 mask = mp->ma_mask;
1230 for (i = 0, j = 0; i <= mask; i++) {
1231 if (ep[i].me_value != NULL((void *)0)) {
1232 PyObject *value = ep[i].me_value;
1233 Py_INCREF(value)( _Py_RefTotal++ , ((PyObject*)(value))->ob_refcnt++);
1234 PyList_SET_ITEM(v, j, value)(((PyListObject *)(v))->ob_item[j] = (value));
1235 j++;
1236 }
1237 }
1238 assert(j == n)(__builtin_expect(!(j == n), 0) ? __assert_rtn(__func__, "Objects/dictobject.c"
, 1238, "j == n") : (void)0)
;
1239 return v;
1240}
1241
1242static PyObject *
1243dict_items(register PyDictObject *mp)
1244{
1245 register PyObject *v;
1246 register Py_ssize_t i, j, n;
1247 Py_ssize_t mask;
1248 PyObject *item, *key, *value;
1249 PyDictEntry *ep;
1250
1251 /* Preallocate the list of tuples, to avoid allocations during
1252 * the loop over the items, which could trigger GC, which
1253 * could resize the dict. :-(
1254 */
1255 again:
1256 n = mp->ma_used;
1257 v = PyList_New(n);
1258 if (v == NULL((void *)0))
1259 return NULL((void *)0);
1260 for (i = 0; i < n; i++) {
1261 item = PyTuple_New(2);
1262 if (item == NULL((void *)0)) {
1263 Py_DECREF(v)do { if (_Py_RefTotal-- , --((PyObject*)(v))->ob_refcnt !=
0) { if (((PyObject*)v)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 1263, (PyObject *)(v)); } else _Py_Dealloc
((PyObject *)(v)); } while (0)
;
1264 return NULL((void *)0);
1265 }
1266 PyList_SET_ITEM(v, i, item)(((PyListObject *)(v))->ob_item[i] = (item));
1267 }
1268 if (n != mp->ma_used) {
1269 /* Durnit. The allocations caused the dict to resize.
1270 * Just start over, this shouldn't normally happen.
1271 */
1272 Py_DECREF(v)do { if (_Py_RefTotal-- , --((PyObject*)(v))->ob_refcnt !=
0) { if (((PyObject*)v)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 1272, (PyObject *)(v)); } else _Py_Dealloc
((PyObject *)(v)); } while (0)
;
1273 goto again;
1274 }
1275 /* Nothing we do below makes any function calls. */
1276 ep = mp->ma_table;
1277 mask = mp->ma_mask;
1278 for (i = 0, j = 0; i <= mask; i++) {
1279 if ((value=ep[i].me_value) != NULL((void *)0)) {
1280 key = ep[i].me_key;
1281 item = PyList_GET_ITEM(v, j)(((PyListObject *)(v))->ob_item[j]);
1282 Py_INCREF(key)( _Py_RefTotal++ , ((PyObject*)(key))->ob_refcnt++);
1283 PyTuple_SET_ITEM(item, 0, key)(((PyTupleObject *)(item))->ob_item[0] = key);
1284 Py_INCREF(value)( _Py_RefTotal++ , ((PyObject*)(value))->ob_refcnt++);
1285 PyTuple_SET_ITEM(item, 1, value)(((PyTupleObject *)(item))->ob_item[1] = value);
1286 j++;
1287 }
1288 }
1289 assert(j == n)(__builtin_expect(!(j == n), 0) ? __assert_rtn(__func__, "Objects/dictobject.c"
, 1289, "j == n") : (void)0)
;
1290 return v;
1291}
1292
1293static PyObject *
1294dict_fromkeys(PyObject *cls, PyObject *args)
1295{
1296 PyObject *seq;
1297 PyObject *value = Py_None(&_Py_NoneStruct);
1298 PyObject *it; /* iter(seq) */
1299 PyObject *key;
1300 PyObject *d;
1301 int status;
1302
1303 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
1304 return NULL((void *)0);
1305
1306 d = PyObject_CallObject(cls, NULL((void *)0));
1307 if (d == NULL((void *)0))
1308 return NULL((void *)0);
1309
1310 if (PyDict_CheckExact(d)((((PyObject*)(d))->ob_type) == &PyDict_Type) && PyDict_CheckExact(seq)((((PyObject*)(seq))->ob_type) == &PyDict_Type)) {
1311 PyDictObject *mp = (PyDictObject *)d;
1312 PyObject *oldvalue;
1313 Py_ssize_t pos = 0;
1314 PyObject *key;
1315 Py_hash_t hash;
1316
1317 if (dictresize(mp, Py_SIZE(seq)(((PyVarObject*)(seq))->ob_size)))
1318 return NULL((void *)0);
1319
1320 while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
1321 Py_INCREF(key)( _Py_RefTotal++ , ((PyObject*)(key))->ob_refcnt++);
1322 Py_INCREF(value)( _Py_RefTotal++ , ((PyObject*)(value))->ob_refcnt++);
1323 if (insertdict(mp, key, hash, value))
1324 return NULL((void *)0);
1325 }
1326 return d;
1327 }
1328
1329 if (PyDict_CheckExact(d)((((PyObject*)(d))->ob_type) == &PyDict_Type) && PyAnySet_CheckExact(seq)((((PyObject*)(seq))->ob_type) == &PySet_Type || (((PyObject
*)(seq))->ob_type) == &PyFrozenSet_Type)
) {
1330 PyDictObject *mp = (PyDictObject *)d;
1331 Py_ssize_t pos = 0;
1332 PyObject *key;
1333 Py_hash_t hash;
1334
1335 if (dictresize(mp, PySet_GET_SIZE(seq)(((PySetObject *)(seq))->used)))
1336 return NULL((void *)0);
1337
1338 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
1339 Py_INCREF(key)( _Py_RefTotal++ , ((PyObject*)(key))->ob_refcnt++);
1340 Py_INCREF(value)( _Py_RefTotal++ , ((PyObject*)(value))->ob_refcnt++);
1341 if (insertdict(mp, key, hash, value))
1342 return NULL((void *)0);
1343 }
1344 return d;
1345 }
1346
1347 it = PyObject_GetIter(seq);
1348 if (it == NULL((void *)0)){
1349 Py_DECREF(d)do { if (_Py_RefTotal-- , --((PyObject*)(d))->ob_refcnt !=
0) { if (((PyObject*)d)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 1349, (PyObject *)(d)); } else _Py_Dealloc
((PyObject *)(d)); } while (0)
;
1350 return NULL((void *)0);
1351 }
1352
1353 if (PyDict_CheckExact(d)((((PyObject*)(d))->ob_type) == &PyDict_Type)) {
1354 while ((key = PyIter_Next(it)) != NULL((void *)0)) {
1355 status = PyDict_SetItem(d, key, value);
1356 Py_DECREF(key)do { if (_Py_RefTotal-- , --((PyObject*)(key))->ob_refcnt !=
0) { if (((PyObject*)key)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 1356, (PyObject *)(key)); } else _Py_Dealloc
((PyObject *)(key)); } while (0)
;
1357 if (status < 0)
1358 goto Fail;
1359 }
1360 } else {
1361 while ((key = PyIter_Next(it)) != NULL((void *)0)) {
1362 status = PyObject_SetItem(d, key, value);
1363 Py_DECREF(key)do { if (_Py_RefTotal-- , --((PyObject*)(key))->ob_refcnt !=
0) { if (((PyObject*)key)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 1363, (PyObject *)(key)); } else _Py_Dealloc
((PyObject *)(key)); } while (0)
;
1364 if (status < 0)
1365 goto Fail;
1366 }
1367 }
1368
1369 if (PyErr_Occurred())
1370 goto Fail;
1371 Py_DECREF(it)do { if (_Py_RefTotal-- , --((PyObject*)(it))->ob_refcnt !=
0) { if (((PyObject*)it)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 1371, (PyObject *)(it)); } else _Py_Dealloc
((PyObject *)(it)); } while (0)
;
1372 return d;
1373
1374Fail:
1375 Py_DECREF(it)do { if (_Py_RefTotal-- , --((PyObject*)(it))->ob_refcnt !=
0) { if (((PyObject*)it)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 1375, (PyObject *)(it)); } else _Py_Dealloc
((PyObject *)(it)); } while (0)
;
1376 Py_DECREF(d)do { if (_Py_RefTotal-- , --((PyObject*)(d))->ob_refcnt !=
0) { if (((PyObject*)d)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 1376, (PyObject *)(d)); } else _Py_Dealloc
((PyObject *)(d)); } while (0)
;
1377 return NULL((void *)0);
1378}
1379
1380static int
1381dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
1382{
1383 PyObject *arg = NULL((void *)0);
1384 int result = 0;
1385
1386 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1387 result = -1;
1388
1389 else if (arg != NULL((void *)0)) {
1390 if (PyObject_HasAttrString(arg, "keys"))
1391 result = PyDict_Merge(self, arg, 1);
1392 else
1393 result = PyDict_MergeFromSeq2(self, arg, 1);
1394 }
1395 if (result == 0 && kwds != NULL((void *)0)) {
1396 if (PyArg_ValidateKeywordArguments(kwds))
1397 result = PyDict_Merge(self, kwds, 1);
1398 else
1399 result = -1;
1400 }
1401 return result;
1402}
1403
1404static PyObject *
1405dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1406{
1407 if (dict_update_common(self, args, kwds, "update") != -1)
1408 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
1409 return NULL((void *)0);
1410}
1411
1412/* Update unconditionally replaces existing items.
1413 Merge has a 3rd argument 'override'; if set, it acts like Update,
1414 otherwise it leaves existing items unchanged.
1415
1416 PyDict_{Update,Merge} update/merge from a mapping object.
1417
1418 PyDict_MergeFromSeq2 updates/merges from any iterable object
1419 producing iterable objects of length 2.
1420*/
1421
1422int
1423PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1424{
1425 PyObject *it; /* iter(seq2) */
1426 Py_ssize_t i; /* index into seq2 of current element */
1427 PyObject *item; /* seq2[i] */
1428 PyObject *fast; /* item as a 2-tuple or 2-list */
1429
1430 assert(d != NULL)(__builtin_expect(!(d != ((void *)0)), 0) ? __assert_rtn(__func__
, "Objects/dictobject.c", 1430, "d != NULL") : (void)0)
;
1431 assert(PyDict_Check(d))(__builtin_expect(!(((((((PyObject*)(d))->ob_type))->tp_flags
& ((1L<<29))) != 0)), 0) ? __assert_rtn(__func__, "Objects/dictobject.c"
, 1431, "PyDict_Check(d)") : (void)0)
;
1432 assert(seq2 != NULL)(__builtin_expect(!(seq2 != ((void *)0)), 0) ? __assert_rtn(__func__
, "Objects/dictobject.c", 1432, "seq2 != NULL") : (void)0)
;
1433
1434 it = PyObject_GetIter(seq2);
1435 if (it == NULL((void *)0))
1436 return -1;
1437
1438 for (i = 0; ; ++i) {
1439 PyObject *key, *value;
1440 Py_ssize_t n;
1441
1442 fast = NULL((void *)0);
1443 item = PyIter_Next(it);
1444 if (item == NULL((void *)0)) {
1445 if (PyErr_Occurred())
1446 goto Fail;
1447 break;
1448 }
1449
1450 /* Convert item to sequence, and verify length 2. */
1451 fast = PySequence_Fast(item, "");
1452 if (fast == NULL((void *)0)) {
1453 if (PyErr_ExceptionMatches(PyExc_TypeError))
1454 PyErr_Format(PyExc_TypeError,
1455 "cannot convert dictionary update "
1456 "sequence element #%zd to a sequence",
1457 i);
1458 goto Fail;
1459 }
1460 n = PySequence_Fast_GET_SIZE(fast)(((((((PyObject*)(fast))->ob_type))->tp_flags & ((1L
<<25))) != 0) ? (((PyVarObject*)(fast))->ob_size) : (
((PyVarObject*)(fast))->ob_size))
;
1461 if (n != 2) {
1462 PyErr_Format(PyExc_ValueError,
1463 "dictionary update sequence element #%zd "
1464 "has length %zd; 2 is required",
1465 i, n);
1466 goto Fail;
1467 }
1468
1469 /* Update/merge with this (key, value) pair. */
1470 key = PySequence_Fast_GET_ITEM(fast, 0)(((((((PyObject*)(fast))->ob_type))->tp_flags & ((1L
<<25))) != 0) ? (((PyListObject *)(fast))->ob_item[0
]) : (((PyTupleObject *)(fast))->ob_item[0]))
;
1471 value = PySequence_Fast_GET_ITEM(fast, 1)(((((((PyObject*)(fast))->ob_type))->tp_flags & ((1L
<<25))) != 0) ? (((PyListObject *)(fast))->ob_item[1
]) : (((PyTupleObject *)(fast))->ob_item[1]))
;
1472 if (override || PyDict_GetItem(d, key) == NULL((void *)0)) {
1473 int status = PyDict_SetItem(d, key, value);
1474 if (status < 0)
1475 goto Fail;
1476 }
1477 Py_DECREF(fast)do { if (_Py_RefTotal-- , --((PyObject*)(fast))->ob_refcnt
!= 0) { if (((PyObject*)fast)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 1477, (PyObject *)(fast)); } else _Py_Dealloc
((PyObject *)(fast)); } while (0)
;
1478 Py_DECREF(item)do { if (_Py_RefTotal-- , --((PyObject*)(item))->ob_refcnt
!= 0) { if (((PyObject*)item)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 1478, (PyObject *)(item)); } else _Py_Dealloc
((PyObject *)(item)); } while (0)
;
1479 }
1480
1481 i = 0;
1482 goto Return;
1483Fail:
1484 Py_XDECREF(item)do { if ((item) == ((void *)0)) ; else do { if (_Py_RefTotal--
, --((PyObject*)(item))->ob_refcnt != 0) { if (((PyObject
*)item)->ob_refcnt < 0) _Py_NegativeRefcount("Objects/dictobject.c"
, 1484, (PyObject *)(item)); } else _Py_Dealloc((PyObject *)(
item)); } while (0); } while (0)
;
1485 Py_XDECREF(fast)do { if ((fast) == ((void *)0)) ; else do { if (_Py_RefTotal--
, --((PyObject*)(fast))->ob_refcnt != 0) { if (((PyObject
*)fast)->ob_refcnt < 0) _Py_NegativeRefcount("Objects/dictobject.c"
, 1485, (PyObject *)(fast)); } else _Py_Dealloc((PyObject *)(
fast)); } while (0); } while (0)
;
1486 i = -1;
1487Return:
1488 Py_DECREF(it)do { if (_Py_RefTotal-- , --((PyObject*)(it))->ob_refcnt !=
0) { if (((PyObject*)it)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 1488, (PyObject *)(it)); } else _Py_Dealloc
((PyObject *)(it)); } while (0)
;
1489 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int)((__builtin_expect(!((Py_ssize_t)(int)(i) == (i)), 0) ? __assert_rtn
(__func__, "Objects/dictobject.c", 1489, "(Py_ssize_t)(int)(i) == (i)"
) : (void)0), (int)(i))
;
1490}
1491
1492int
1493PyDict_Update(PyObject *a, PyObject *b)
1494{
1495 return PyDict_Merge(a, b, 1);
1496}
1497
1498int
1499PyDict_Merge(PyObject *a, PyObject *b, int override)
1500{
1501 register PyDictObject *mp, *other;
1502 register Py_ssize_t i;
1503 PyDictEntry *entry;
1504
1505 /* We accept for the argument either a concrete dictionary object,
1506 * or an abstract "mapping" object. For the former, we can do
1507 * things quite efficiently. For the latter, we only require that
1508 * PyMapping_Keys() and PyObject_GetItem() be supported.
1509 */
1510 if (a == NULL((void *)0) || !PyDict_Check(a)((((((PyObject*)(a))->ob_type))->tp_flags & ((1L<<
29))) != 0)
|| b == NULL((void *)0)) {
1511 PyErr_BadInternalCall()_PyErr_BadInternalCall("Objects/dictobject.c", 1511);
1512 return -1;
1513 }
1514 mp = (PyDictObject*)a;
1515 if (PyDict_Check(b)((((((PyObject*)(b))->ob_type))->tp_flags & ((1L<<
29))) != 0)
) {
1516 other = (PyDictObject*)b;
1517 if (other == mp || other->ma_used == 0)
1518 /* a.update(a) or a.update({}); nothing to do */
1519 return 0;
1520 if (mp->ma_used == 0)
1521 /* Since the target dict is empty, PyDict_GetItem()
1522 * always returns NULL. Setting override to 1
1523 * skips the unnecessary test.
1524 */
1525 override = 1;
1526 /* Do one big resize at the start, rather than
1527 * incrementally resizing as we insert new items. Expect
1528 * that there will be no (or few) overlapping keys.
1529 */
1530 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
1531 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
1532 return -1;
1533 }
1534 for (i = 0; i <= other->ma_mask; i++) {
1535 entry = &other->ma_table[i];
1536 if (entry->me_value != NULL((void *)0) &&
1537 (override ||
1538 PyDict_GetItem(a, entry->me_key) == NULL((void *)0))) {
1539 Py_INCREF(entry->me_key)( _Py_RefTotal++ , ((PyObject*)(entry->me_key))->ob_refcnt
++)
;
1540 Py_INCREF(entry->me_value)( _Py_RefTotal++ , ((PyObject*)(entry->me_value))->ob_refcnt
++)
;
1541 if (insertdict(mp, entry->me_key,
1542 entry->me_hash,
1543 entry->me_value) != 0)
1544 return -1;
1545 }
1546 }
1547 }
1548 else {
1549 /* Do it the generic, slower way */
1550 PyObject *keys = PyMapping_Keys(b);
1551 PyObject *iter;
1552 PyObject *key, *value;
1553 int status;
1554
1555 if (keys == NULL((void *)0))
1556 /* Docstring says this is equivalent to E.keys() so
1557 * if E doesn't have a .keys() method we want
1558 * AttributeError to percolate up. Might as well
1559 * do the same for any other error.
1560 */
1561 return -1;
1562
1563 iter = PyObject_GetIter(keys);
1564 Py_DECREF(keys)do { if (_Py_RefTotal-- , --((PyObject*)(keys))->ob_refcnt
!= 0) { if (((PyObject*)keys)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 1564, (PyObject *)(keys)); } else _Py_Dealloc
((PyObject *)(keys)); } while (0)
;
1565 if (iter == NULL((void *)0))
1566 return -1;
1567
1568 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
1569 if (!override && PyDict_GetItem(a, key) != NULL((void *)0)) {
1570 Py_DECREF(key)do { if (_Py_RefTotal-- , --((PyObject*)(key))->ob_refcnt !=
0) { if (((PyObject*)key)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 1570, (PyObject *)(key)); } else _Py_Dealloc
((PyObject *)(key)); } while (0)
;
1571 continue;
1572 }
1573 value = PyObject_GetItem(b, key);
1574 if (value == NULL((void *)0)) {
1575 Py_DECREF(iter)do { if (_Py_RefTotal-- , --((PyObject*)(iter))->ob_refcnt
!= 0) { if (((PyObject*)iter)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 1575, (PyObject *)(iter)); } else _Py_Dealloc
((PyObject *)(iter)); } while (0)
;
1576 Py_DECREF(key)do { if (_Py_RefTotal-- , --((PyObject*)(key))->ob_refcnt !=
0) { if (((PyObject*)key)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 1576, (PyObject *)(key)); } else _Py_Dealloc
((PyObject *)(key)); } while (0)
;
1577 return -1;
1578 }
1579 status = PyDict_SetItem(a, key, value);
1580 Py_DECREF(key)do { if (_Py_RefTotal-- , --((PyObject*)(key))->ob_refcnt !=
0) { if (((PyObject*)key)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 1580, (PyObject *)(key)); } else _Py_Dealloc
((PyObject *)(key)); } while (0)
;
1581 Py_DECREF(value)do { if (_Py_RefTotal-- , --((PyObject*)(value))->ob_refcnt
!= 0) { if (((PyObject*)value)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 1581, (PyObject *)(value)); } else _Py_Dealloc
((PyObject *)(value)); } while (0)
;
1582 if (status < 0) {
1583 Py_DECREF(iter)do { if (_Py_RefTotal-- , --((PyObject*)(iter))->ob_refcnt
!= 0) { if (((PyObject*)iter)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 1583, (PyObject *)(iter)); } else _Py_Dealloc
((PyObject *)(iter)); } while (0)
;
1584 return -1;
1585 }
1586 }
1587 Py_DECREF(iter)do { if (_Py_RefTotal-- , --((PyObject*)(iter))->ob_refcnt
!= 0) { if (((PyObject*)iter)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 1587, (PyObject *)(iter)); } else _Py_Dealloc
((PyObject *)(iter)); } while (0)
;
1588 if (PyErr_Occurred())
1589 /* Iterator completed, via error */
1590 return -1;
1591 }
1592 return 0;
1593}
1594
1595static PyObject *
1596dict_copy(register PyDictObject *mp)
1597{
1598 return PyDict_Copy((PyObject*)mp);
1599}
1600
1601PyObject *
1602PyDict_Copy(PyObject *o)
1603{
1604 PyObject *copy;
1605
1606 if (o == NULL((void *)0) || !PyDict_Check(o)((((((PyObject*)(o))->ob_type))->tp_flags & ((1L<<
29))) != 0)
) {
1607 PyErr_BadInternalCall()_PyErr_BadInternalCall("Objects/dictobject.c", 1607);
1608 return NULL((void *)0);
1609 }
1610 copy = PyDict_New();
1611 if (copy == NULL((void *)0))
1612 return NULL((void *)0);
1613 if (PyDict_Merge(copy, o, 1) == 0)
1614 return copy;
1615 Py_DECREF(copy)do { if (_Py_RefTotal-- , --((PyObject*)(copy))->ob_refcnt
!= 0) { if (((PyObject*)copy)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 1615, (PyObject *)(copy)); } else _Py_Dealloc
((PyObject *)(copy)); } while (0)
;
1616 return NULL((void *)0);
1617}
1618
1619Py_ssize_t
1620PyDict_Size(PyObject *mp)
1621{
1622 if (mp == NULL((void *)0) || !PyDict_Check(mp)((((((PyObject*)(mp))->ob_type))->tp_flags & ((1L<<
29))) != 0)
) {
1623 PyErr_BadInternalCall()_PyErr_BadInternalCall("Objects/dictobject.c", 1623);
1624 return -1;
1625 }
1626 return ((PyDictObject *)mp)->ma_used;
1627}
1628
1629PyObject *
1630PyDict_Keys(PyObject *mp)
1631{
1632 if (mp == NULL((void *)0) || !PyDict_Check(mp)((((((PyObject*)(mp))->ob_type))->tp_flags & ((1L<<
29))) != 0)
) {
1633 PyErr_BadInternalCall()_PyErr_BadInternalCall("Objects/dictobject.c", 1633);
1634 return NULL((void *)0);
1635 }
1636 return dict_keys((PyDictObject *)mp);
1637}
1638
1639PyObject *
1640PyDict_Values(PyObject *mp)
1641{
1642 if (mp == NULL((void *)0) || !PyDict_Check(mp)((((((PyObject*)(mp))->ob_type))->tp_flags & ((1L<<
29))) != 0)
) {
1643 PyErr_BadInternalCall()_PyErr_BadInternalCall("Objects/dictobject.c", 1643);
1644 return NULL((void *)0);
1645 }
1646 return dict_values((PyDictObject *)mp);
1647}
1648
1649PyObject *
1650PyDict_Items(PyObject *mp)
1651{
1652 if (mp == NULL((void *)0) || !PyDict_Check(mp)((((((PyObject*)(mp))->ob_type))->tp_flags & ((1L<<
29))) != 0)
) {
1653 PyErr_BadInternalCall()_PyErr_BadInternalCall("Objects/dictobject.c", 1653);
1654 return NULL((void *)0);
1655 }
1656 return dict_items((PyDictObject *)mp);
1657}
1658
1659/* Return 1 if dicts equal, 0 if not, -1 if error.
1660 * Gets out as soon as any difference is detected.
1661 * Uses only Py_EQ comparison.
1662 */
1663static int
1664dict_equal(PyDictObject *a, PyDictObject *b)
1665{
1666 Py_ssize_t i;
1667
1668 if (a->ma_used != b->ma_used)
1669 /* can't be equal if # of entries differ */
1670 return 0;
1671
1672 /* Same # of entries -- check all of 'em. Exit early on any diff. */
1673 for (i = 0; i <= a->ma_mask; i++) {
1674 PyObject *aval = a->ma_table[i].me_value;
1675 if (aval != NULL((void *)0)) {
1676 int cmp;
1677 PyObject *bval;
1678 PyObject *key = a->ma_table[i].me_key;
1679 /* temporarily bump aval's refcount to ensure it stays
1680 alive until we're done with it */
1681 Py_INCREF(aval)( _Py_RefTotal++ , ((PyObject*)(aval))->ob_refcnt++);
1682 /* ditto for key */
1683 Py_INCREF(key)( _Py_RefTotal++ , ((PyObject*)(key))->ob_refcnt++);
1684 bval = PyDict_GetItemWithError((PyObject *)b, key);
1685 Py_DECREF(key)do { if (_Py_RefTotal-- , --((PyObject*)(key))->ob_refcnt !=
0) { if (((PyObject*)key)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 1685, (PyObject *)(key)); } else _Py_Dealloc
((PyObject *)(key)); } while (0)
;
1686 if (bval == NULL((void *)0)) {
1687 Py_DECREF(aval)do { if (_Py_RefTotal-- , --((PyObject*)(aval))->ob_refcnt
!= 0) { if (((PyObject*)aval)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 1687, (PyObject *)(aval)); } else _Py_Dealloc
((PyObject *)(aval)); } while (0)
;
1688 if (PyErr_Occurred())
1689 return -1;
1690 return 0;
1691 }
1692 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ2);
1693 Py_DECREF(aval)do { if (_Py_RefTotal-- , --((PyObject*)(aval))->ob_refcnt
!= 0) { if (((PyObject*)aval)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 1693, (PyObject *)(aval)); } else _Py_Dealloc
((PyObject *)(aval)); } while (0)
;
1694 if (cmp <= 0) /* error or not equal */
1695 return cmp;
1696 }
1697 }
1698 return 1;
1699 }
1700
1701static PyObject *
1702dict_richcompare(PyObject *v, PyObject *w, int op)
1703{
1704 int cmp;
1705 PyObject *res;
1706
1707 if (!PyDict_Check(v)((((((PyObject*)(v))->ob_type))->tp_flags & ((1L<<
29))) != 0)
|| !PyDict_Check(w)((((((PyObject*)(w))->ob_type))->tp_flags & ((1L<<
29))) != 0)
) {
1708 res = Py_NotImplemented(&_Py_NotImplementedStruct);
1709 }
1710 else if (op == Py_EQ2 || op == Py_NE3) {
1711 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
1712 if (cmp < 0)
1713 return NULL((void *)0);
1714 res = (cmp == (op == Py_EQ2)) ? Py_True((PyObject *) &_Py_TrueStruct) : Py_False((PyObject *) &_Py_FalseStruct);
1715 }
1716 else
1717 res = Py_NotImplemented(&_Py_NotImplementedStruct);
1718 Py_INCREF(res)( _Py_RefTotal++ , ((PyObject*)(res))->ob_refcnt++);
1719 return res;
1720 }
1721
1722static PyObject *
1723dict_contains(register PyDictObject *mp, PyObject *key)
1724{
1725 Py_hash_t hash;
1726 PyDictEntry *ep;
1727
1728 if (!PyUnicode_CheckExact(key)((((PyObject*)(key))->ob_type) == &PyUnicode_Type) ||
1729 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
1730 hash = PyObject_Hash(key);
1731 if (hash == -1)
1732 return NULL((void *)0);
1733 }
1734 ep = (mp->ma_lookup)(mp, key, hash);
1735 if (ep == NULL((void *)0))
1736 return NULL((void *)0);
1737 return PyBool_FromLong(ep->me_value != NULL((void *)0));
1738}
1739
1740static PyObject *
1741dict_get(register PyDictObject *mp, PyObject *args)
1742{
1743 PyObject *key;
1744 PyObject *failobj = Py_None(&_Py_NoneStruct);
1745 PyObject *val = NULL((void *)0);
1746 Py_hash_t hash;
1747 PyDictEntry *ep;
1748
1749 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
1750 return NULL((void *)0);
1751
1752 if (!PyUnicode_CheckExact(key)((((PyObject*)(key))->ob_type) == &PyUnicode_Type) ||
1753 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
1754 hash = PyObject_Hash(key);
1755 if (hash == -1)
1756 return NULL((void *)0);
1757 }
1758 ep = (mp->ma_lookup)(mp, key, hash);
1759 if (ep == NULL((void *)0))
1760 return NULL((void *)0);
1761 val = ep->me_value;
1762 if (val == NULL((void *)0))
1763 val = failobj;
1764 Py_INCREF(val)( _Py_RefTotal++ , ((PyObject*)(val))->ob_refcnt++);
1765 return val;
1766}
1767
1768
1769static PyObject *
1770dict_setdefault(register PyDictObject *mp, PyObject *args)
1771{
1772 PyObject *key;
1773 PyObject *failobj = Py_None(&_Py_NoneStruct);
1774 PyObject *val = NULL((void *)0);
1775 Py_hash_t hash;
1776 PyDictEntry *ep;
1777
1778 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
1779 return NULL((void *)0);
1780
1781 if (!PyUnicode_CheckExact(key)((((PyObject*)(key))->ob_type) == &PyUnicode_Type) ||
1782 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
1783 hash = PyObject_Hash(key);
1784 if (hash == -1)
1785 return NULL((void *)0);
1786 }
1787 ep = (mp->ma_lookup)(mp, key, hash);
1788 if (ep == NULL((void *)0))
1789 return NULL((void *)0);
1790 val = ep->me_value;
1791 if (val == NULL((void *)0)) {
1792 val = failobj;
1793 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1794 val = NULL((void *)0);
1795 }
1796 Py_XINCREF(val)do { if ((val) == ((void *)0)) ; else ( _Py_RefTotal++ , ((PyObject
*)(val))->ob_refcnt++); } while (0)
;
1797 return val;
1798}
1799
1800
1801static PyObject *
1802dict_clear(register PyDictObject *mp)
1803{
1804 PyDict_Clear((PyObject *)mp);
1805 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
1806}
1807
1808static PyObject *
1809dict_pop(PyDictObject *mp, PyObject *args)
1810{
1811 Py_hash_t hash;
1812 PyDictEntry *ep;
1813 PyObject *old_value, *old_key;
1814 PyObject *key, *deflt = NULL((void *)0);
1815
1816 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1817 return NULL((void *)0);
1818 if (mp->ma_used == 0) {
1819 if (deflt) {
1820 Py_INCREF(deflt)( _Py_RefTotal++ , ((PyObject*)(deflt))->ob_refcnt++);
1821 return deflt;
1822 }
1823 set_key_error(key);
1824 return NULL((void *)0);
1825 }
1826 if (!PyUnicode_CheckExact(key)((((PyObject*)(key))->ob_type) == &PyUnicode_Type) ||
1827 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
1828 hash = PyObject_Hash(key);
1829 if (hash == -1)
1830 return NULL((void *)0);
1831 }
1832 ep = (mp->ma_lookup)(mp, key, hash);
1833 if (ep == NULL((void *)0))
1834 return NULL((void *)0);
1835 if (ep->me_value == NULL((void *)0)) {
1836 if (deflt) {
1837 Py_INCREF(deflt)( _Py_RefTotal++ , ((PyObject*)(deflt))->ob_refcnt++);
1838 return deflt;
1839 }
1840 set_key_error(key);
1841 return NULL((void *)0);
1842 }
1843 old_key = ep->me_key;
1844 Py_INCREF(dummy)( _Py_RefTotal++ , ((PyObject*)(dummy))->ob_refcnt++);
1845 ep->me_key = dummy;
1846 old_value = ep->me_value;
1847 ep->me_value = NULL((void *)0);
1848 mp->ma_used--;
1849 Py_DECREF(old_key)do { if (_Py_RefTotal-- , --((PyObject*)(old_key))->ob_refcnt
!= 0) { if (((PyObject*)old_key)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 1849, (PyObject *)(old_key)); } else
_Py_Dealloc((PyObject *)(old_key)); } while (0)
;
1850 return old_value;
1851}
1852
1853static PyObject *
1854dict_popitem(PyDictObject *mp)
1855{
1856 Py_hash_t i = 0;
1857 PyDictEntry *ep;
1858 PyObject *res;
1859
1860 /* Allocate the result tuple before checking the size. Believe it
1861 * or not, this allocation could trigger a garbage collection which
1862 * could empty the dict, so if we checked the size first and that
1863 * happened, the result would be an infinite loop (searching for an
1864 * entry that no longer exists). Note that the usual popitem()
1865 * idiom is "while d: k, v = d.popitem()". so needing to throw the
1866 * tuple away if the dict *is* empty isn't a significant
1867 * inefficiency -- possible, but unlikely in practice.
1868 */
1869 res = PyTuple_New(2);
1870 if (res == NULL((void *)0))
1871 return NULL((void *)0);
1872 if (mp->ma_used == 0) {
1873 Py_DECREF(res)do { if (_Py_RefTotal-- , --((PyObject*)(res))->ob_refcnt !=
0) { if (((PyObject*)res)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 1873, (PyObject *)(res)); } else _Py_Dealloc
((PyObject *)(res)); } while (0)
;
1874 PyErr_SetString(PyExc_KeyError,
1875 "popitem(): dictionary is empty");
1876 return NULL((void *)0);
1877 }
1878 /* Set ep to "the first" dict entry with a value. We abuse the hash
1879 * field of slot 0 to hold a search finger:
1880 * If slot 0 has a value, use slot 0.
1881 * Else slot 0 is being used to hold a search finger,
1882 * and we use its hash value as the first index to look.
1883 */
1884 ep = &mp->ma_table[0];
1885 if (ep->me_value == NULL((void *)0)) {
1886 i = ep->me_hash;
1887 /* The hash field may be a real hash value, or it may be a
1888 * legit search finger, or it may be a once-legit search
1889 * finger that's out of bounds now because it wrapped around
1890 * or the table shrunk -- simply make sure it's in bounds now.
1891 */
1892 if (i > mp->ma_mask || i < 1)
1893 i = 1; /* skip slot 0 */
1894 while ((ep = &mp->ma_table[i])->me_value == NULL((void *)0)) {
1895 i++;
1896 if (i > mp->ma_mask)
1897 i = 1;
1898 }
1899 }
1900 PyTuple_SET_ITEM(res, 0, ep->me_key)(((PyTupleObject *)(res))->ob_item[0] = ep->me_key);
1901 PyTuple_SET_ITEM(res, 1, ep->me_value)(((PyTupleObject *)(res))->ob_item[1] = ep->me_value);
1902 Py_INCREF(dummy)( _Py_RefTotal++ , ((PyObject*)(dummy))->ob_refcnt++);
1903 ep->me_key = dummy;
1904 ep->me_value = NULL((void *)0);
1905 mp->ma_used--;
1906 assert(mp->ma_table[0].me_value == NULL)(__builtin_expect(!(mp->ma_table[0].me_value == ((void *)0
)), 0) ? __assert_rtn(__func__, "Objects/dictobject.c", 1906,
"mp->ma_table[0].me_value == NULL") : (void)0)
;
1907 mp->ma_table[0].me_hash = i + 1; /* next place to start */
1908 return res;
1909}
1910
1911static int
1912dict_traverse(PyObject *op, visitproc visit, void *arg)
1913{
1914 Py_ssize_t i = 0;
1915 PyObject *pk;
1916 PyObject *pv;
1917
1918 while (PyDict_Next(op, &i, &pk, &pv)) {
1919 Py_VISIT(pk)do { if (pk) { int vret = visit((PyObject *)(pk), arg); if (vret
) return vret; } } while (0)
;
1920 Py_VISIT(pv)do { if (pv) { int vret = visit((PyObject *)(pv), arg); if (vret
) return vret; } } while (0)
;
1921 }
1922 return 0;
1923}
1924
1925static int
1926dict_tp_clear(PyObject *op)
1927{
1928 PyDict_Clear(op);
1929 return 0;
1930}
1931
1932static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
1933
1934static PyObject *
1935dict_sizeof(PyDictObject *mp)
1936{
1937 Py_ssize_t res;
1938
1939 res = sizeof(PyDictObject);
1940 if (mp->ma_table != mp->ma_smalltable)
1941 res = res + (mp->ma_mask + 1) * sizeof(PyDictEntry);
1942 return PyLong_FromSsize_t(res);
1943}
1944
1945PyDoc_STRVAR(contains__doc__,static char contains__doc__[] = "D.__contains__(k) -> True if D has a key k, else False"
1946"D.__contains__(k) -> True if D has a key k, else False")static char contains__doc__[] = "D.__contains__(k) -> True if D has a key k, else False";
1947
1948PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]")static char getitem__doc__[] = "x.__getitem__(y) <==> x[y]";
1949
1950PyDoc_STRVAR(sizeof__doc__,static char sizeof__doc__[] = "D.__sizeof__() -> size of D in memory, in bytes"
1951"D.__sizeof__() -> size of D in memory, in bytes")static char sizeof__doc__[] = "D.__sizeof__() -> size of D in memory, in bytes";
1952
1953PyDoc_STRVAR(get__doc__,static char get__doc__[] = "D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None."
1954"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.")static char get__doc__[] = "D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.";
1955
1956PyDoc_STRVAR(setdefault_doc__,static char setdefault_doc__[] = "D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D"
1957"D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D")static char setdefault_doc__[] = "D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D";
1958
1959PyDoc_STRVAR(pop__doc__,static char pop__doc__[] = "D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\nIf key is not found, d is returned if given, otherwise KeyError is raised"
1960"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\static char pop__doc__[] = "D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\nIf key is not found, d is returned if given, otherwise KeyError is raised"
1961If key is not found, d is returned if given, otherwise KeyError is raised")static char pop__doc__[] = "D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\nIf key is not found, d is returned if given, otherwise KeyError is raised";
1962
1963PyDoc_STRVAR(popitem__doc__,static char popitem__doc__[] = "D.popitem() -> (k, v), remove and return some (key, value) pair as a\n2-tuple; but raise KeyError if D is empty."
1964"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\static char popitem__doc__[] = "D.popitem() -> (k, v), remove and return some (key, value) pair as a\n2-tuple; but raise KeyError if D is empty."
19652-tuple; but raise KeyError if D is empty.")static char popitem__doc__[] = "D.popitem() -> (k, v), remove and return some (key, value) pair as a\n2-tuple; but raise KeyError if D is empty.";
1966
1967PyDoc_STRVAR(update__doc__,static char update__doc__[] = "D.update(E, **F) -> None. Update D from dict/iterable E and F.\n"
"If E has a .keys() method, does: for k in E: D[k] = E[k]\nIf E lacks .keys() method, does: for (k, v) in E: D[k] = v\nIn either case, this is followed by: for k in F: D[k] = F[k]"
1968"D.update(E, **F) -> None. Update D from dict/iterable E and F.\n"static char update__doc__[] = "D.update(E, **F) -> None. Update D from dict/iterable E and F.\n"
"If E has a .keys() method, does: for k in E: D[k] = E[k]\nIf E lacks .keys() method, does: for (k, v) in E: D[k] = v\nIn either case, this is followed by: for k in F: D[k] = F[k]"
1969"If E has a .keys() method, does: for k in E: D[k] = E[k]\n\static char update__doc__[] = "D.update(E, **F) -> None. Update D from dict/iterable E and F.\n"
"If E has a .keys() method, does: for k in E: D[k] = E[k]\nIf E lacks .keys() method, does: for (k, v) in E: D[k] = v\nIn either case, this is followed by: for k in F: D[k] = F[k]"
1970If E lacks .keys() method, does: for (k, v) in E: D[k] = v\n\static char update__doc__[] = "D.update(E, **F) -> None. Update D from dict/iterable E and F.\n"
"If E has a .keys() method, does: for k in E: D[k] = E[k]\nIf E lacks .keys() method, does: for (k, v) in E: D[k] = v\nIn either case, this is followed by: for k in F: D[k] = F[k]"
1971In either case, this is followed by: for k in F: D[k] = F[k]")static char update__doc__[] = "D.update(E, **F) -> None. Update D from dict/iterable E and F.\n"
"If E has a .keys() method, does: for k in E: D[k] = E[k]\nIf E lacks .keys() method, does: for (k, v) in E: D[k] = v\nIn either case, this is followed by: for k in F: D[k] = F[k]"
;
1972
1973PyDoc_STRVAR(fromkeys__doc__,static char fromkeys__doc__[] = "dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\nv defaults to None."
1974"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\static char fromkeys__doc__[] = "dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\nv defaults to None."
1975v defaults to None.")static char fromkeys__doc__[] = "dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\nv defaults to None.";
1976
1977PyDoc_STRVAR(clear__doc__,static char clear__doc__[] = "D.clear() -> None. Remove all items from D."
1978"D.clear() -> None. Remove all items from D.")static char clear__doc__[] = "D.clear() -> None. Remove all items from D.";
1979
1980PyDoc_STRVAR(copy__doc__,static char copy__doc__[] = "D.copy() -> a shallow copy of D"
1981"D.copy() -> a shallow copy of D")static char copy__doc__[] = "D.copy() -> a shallow copy of D";
1982
1983/* Forward */
1984static PyObject *dictkeys_new(PyObject *);
1985static PyObject *dictitems_new(PyObject *);
1986static PyObject *dictvalues_new(PyObject *);
1987
1988PyDoc_STRVAR(keys__doc__,static char keys__doc__[] = "D.keys() -> a set-like object providing a view on D's keys"
1989 "D.keys() -> a set-like object providing a view on D's keys")static char keys__doc__[] = "D.keys() -> a set-like object providing a view on D's keys";
1990PyDoc_STRVAR(items__doc__,static char items__doc__[] = "D.items() -> a set-like object providing a view on D's items"
1991 "D.items() -> a set-like object providing a view on D's items")static char items__doc__[] = "D.items() -> a set-like object providing a view on D's items";
1992PyDoc_STRVAR(values__doc__,static char values__doc__[] = "D.values() -> an object providing a view on D's values"
1993 "D.values() -> an object providing a view on D's values")static char values__doc__[] = "D.values() -> an object providing a view on D's values";
1994
1995static PyMethodDef mapp_methods[] = {
1996 {"__contains__",(PyCFunction)dict_contains, METH_O0x0008 | METH_COEXIST0x0040,
1997 contains__doc__},
1998 {"__getitem__", (PyCFunction)dict_subscript, METH_O0x0008 | METH_COEXIST0x0040,
1999 getitem__doc__},
2000 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS0x0004,
2001 sizeof__doc__},
2002 {"get", (PyCFunction)dict_get, METH_VARARGS0x0001,
2003 get__doc__},
2004 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS0x0001,
2005 setdefault_doc__},
2006 {"pop", (PyCFunction)dict_pop, METH_VARARGS0x0001,
2007 pop__doc__},
2008 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS0x0004,
2009 popitem__doc__},
2010 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS0x0004,
2011 keys__doc__},
2012 {"items", (PyCFunction)dictitems_new, METH_NOARGS0x0004,
2013 items__doc__},
2014 {"values", (PyCFunction)dictvalues_new, METH_NOARGS0x0004,
2015 values__doc__},
2016 {"update", (PyCFunction)dict_update, METH_VARARGS0x0001 | METH_KEYWORDS0x0002,
2017 update__doc__},
2018 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS0x0001 | METH_CLASS0x0010,
2019 fromkeys__doc__},
2020 {"clear", (PyCFunction)dict_clear, METH_NOARGS0x0004,
2021 clear__doc__},
2022 {"copy", (PyCFunction)dict_copy, METH_NOARGS0x0004,
2023 copy__doc__},
2024 {NULL((void *)0), NULL((void *)0)} /* sentinel */
2025};
2026
2027/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
2028int
2029PyDict_Contains(PyObject *op, PyObject *key)
2030{
2031 Py_hash_t hash;
2032 PyDictObject *mp = (PyDictObject *)op;
2033 PyDictEntry *ep;
2034
2035 if (!PyUnicode_CheckExact(key)((((PyObject*)(key))->ob_type) == &PyUnicode_Type) ||
2036 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
2037 hash = PyObject_Hash(key);
2038 if (hash == -1)
2039 return -1;
2040 }
2041 ep = (mp->ma_lookup)(mp, key, hash);
2042 return ep == NULL((void *)0) ? -1 : (ep->me_value != NULL((void *)0));
2043}
2044
2045/* Internal version of PyDict_Contains used when the hash value is already known */
2046int
2047_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
2048{
2049 PyDictObject *mp = (PyDictObject *)op;
2050 PyDictEntry *ep;
2051
2052 ep = (mp->ma_lookup)(mp, key, hash);
2053 return ep == NULL((void *)0) ? -1 : (ep->me_value != NULL((void *)0));
2054}
2055
2056/* Hack to implement "key in dict" */
2057static PySequenceMethods dict_as_sequence = {
2058 0, /* sq_length */
2059 0, /* sq_concat */
2060 0, /* sq_repeat */
2061 0, /* sq_item */
2062 0, /* sq_slice */
2063 0, /* sq_ass_item */
2064 0, /* sq_ass_slice */
2065 PyDict_Contains, /* sq_contains */
2066 0, /* sq_inplace_concat */
2067 0, /* sq_inplace_repeat */
2068};
2069
2070static PyObject *
2071dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2072{
2073 PyObject *self;
2074
2075 assert(type != NULL && type->tp_alloc != NULL)(__builtin_expect(!(type != ((void *)0) && type->tp_alloc
!= ((void *)0)), 0) ? __assert_rtn(__func__, "Objects/dictobject.c"
, 2075, "type != NULL && type->tp_alloc != NULL") :
(void)0)
;
2076 self = type->tp_alloc(type, 0);
2077 if (self != NULL((void *)0)) {
2078 PyDictObject *d = (PyDictObject *)self;
2079 /* It's guaranteed that tp->alloc zeroed out the struct. */
2080 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0)(__builtin_expect(!(d->ma_table == ((void *)0) && d
->ma_fill == 0 && d->ma_used == 0), 0) ? __assert_rtn
(__func__, "Objects/dictobject.c", 2080, "d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0"
) : (void)0)
;
2081 INIT_NONZERO_DICT_SLOTS(d)do { (d)->ma_table = (d)->ma_smalltable; (d)->ma_mask
= 8 - 1; } while(0)
;
2082 d->ma_lookup = lookdict_unicode;
2083 /* The object has been implicitely tracked by tp_alloc */
2084 if (type == &PyDict_Type)
2085 _PyObject_GC_UNTRACK(d)do { PyGC_Head *g = ((PyGC_Head *)(d)-1); (__builtin_expect(!
(g->gc.gc_refs != (-2)), 0) ? __assert_rtn(__func__, "Objects/dictobject.c"
, 2085, "g->gc.gc_refs != _PyGC_REFS_UNTRACKED") : (void)0
); g->gc.gc_refs = (-2); g->gc.gc_prev->gc.gc_next =
g->gc.gc_next; g->gc.gc_next->gc.gc_prev = g->gc
.gc_prev; g->gc.gc_next = ((void *)0); } while (0);
;
2086#ifdef SHOW_CONVERSION_COUNTS
2087 ++created;
2088#endif
2089#ifdef SHOW_TRACK_COUNT
2090 if (_PyObject_GC_IS_TRACKED(d)((((PyGC_Head *)(d)-1))->gc.gc_refs != (-2)))
2091 count_tracked++;
2092 else
2093 count_untracked++;
2094#endif
2095 }
2096 return self;
2097}
2098
2099static int
2100dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2101{
2102 return dict_update_common(self, args, kwds, "dict");
2103}
2104
2105static PyObject *
2106dict_iter(PyDictObject *dict)
2107{
2108 return dictiter_new(dict, &PyDictIterKey_Type);
2109}
2110
2111PyDoc_STRVAR(dictionary_doc,static char dictionary_doc[] = "dict() -> new empty dictionary\n"
"dict(mapping) -> new dictionary initialized from a mapping object's\n"
" (key, value) pairs\n""dict(iterable) -> new dictionary initialized as if via:\n"
" d = {}\n"" for k, v in iterable:\n"" d[k] = v\n"
"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
" in the keyword argument list. For example: dict(one=1, two=2)"
2112"dict() -> new empty dictionary\n"static char dictionary_doc[] = "dict() -> new empty dictionary\n"
"dict(mapping) -> new dictionary initialized from a mapping object's\n"
" (key, value) pairs\n""dict(iterable) -> new dictionary initialized as if via:\n"
" d = {}\n"" for k, v in iterable:\n"" d[k] = v\n"
"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
" in the keyword argument list. For example: dict(one=1, two=2)"
2113"dict(mapping) -> new dictionary initialized from a mapping object's\n"static char dictionary_doc[] = "dict() -> new empty dictionary\n"
"dict(mapping) -> new dictionary initialized from a mapping object's\n"
" (key, value) pairs\n""dict(iterable) -> new dictionary initialized as if via:\n"
" d = {}\n"" for k, v in iterable:\n"" d[k] = v\n"
"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
" in the keyword argument list. For example: dict(one=1, two=2)"
2114" (key, value) pairs\n"static char dictionary_doc[] = "dict() -> new empty dictionary\n"
"dict(mapping) -> new dictionary initialized from a mapping object's\n"
" (key, value) pairs\n""dict(iterable) -> new dictionary initialized as if via:\n"
" d = {}\n"" for k, v in iterable:\n"" d[k] = v\n"
"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
" in the keyword argument list. For example: dict(one=1, two=2)"
2115"dict(iterable) -> new dictionary initialized as if via:\n"static char dictionary_doc[] = "dict() -> new empty dictionary\n"
"dict(mapping) -> new dictionary initialized from a mapping object's\n"
" (key, value) pairs\n""dict(iterable) -> new dictionary initialized as if via:\n"
" d = {}\n"" for k, v in iterable:\n"" d[k] = v\n"
"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
" in the keyword argument list. For example: dict(one=1, two=2)"
2116" d = {}\n"static char dictionary_doc[] = "dict() -> new empty dictionary\n"
"dict(mapping) -> new dictionary initialized from a mapping object's\n"
" (key, value) pairs\n""dict(iterable) -> new dictionary initialized as if via:\n"
" d = {}\n"" for k, v in iterable:\n"" d[k] = v\n"
"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
" in the keyword argument list. For example: dict(one=1, two=2)"
2117" for k, v in iterable:\n"static char dictionary_doc[] = "dict() -> new empty dictionary\n"
"dict(mapping) -> new dictionary initialized from a mapping object's\n"
" (key, value) pairs\n""dict(iterable) -> new dictionary initialized as if via:\n"
" d = {}\n"" for k, v in iterable:\n"" d[k] = v\n"
"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
" in the keyword argument list. For example: dict(one=1, two=2)"
2118" d[k] = v\n"static char dictionary_doc[] = "dict() -> new empty dictionary\n"
"dict(mapping) -> new dictionary initialized from a mapping object's\n"
" (key, value) pairs\n""dict(iterable) -> new dictionary initialized as if via:\n"
" d = {}\n"" for k, v in iterable:\n"" d[k] = v\n"
"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
" in the keyword argument list. For example: dict(one=1, two=2)"
2119"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"static char dictionary_doc[] = "dict() -> new empty dictionary\n"
"dict(mapping) -> new dictionary initialized from a mapping object's\n"
" (key, value) pairs\n""dict(iterable) -> new dictionary initialized as if via:\n"
" d = {}\n"" for k, v in iterable:\n"" d[k] = v\n"
"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
" in the keyword argument list. For example: dict(one=1, two=2)"
2120" in the keyword argument list. For example: dict(one=1, two=2)")static char dictionary_doc[] = "dict() -> new empty dictionary\n"
"dict(mapping) -> new dictionary initialized from a mapping object's\n"
" (key, value) pairs\n""dict(iterable) -> new dictionary initialized as if via:\n"
" d = {}\n"" for k, v in iterable:\n"" d[k] = v\n"
"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
" in the keyword argument list. For example: dict(one=1, two=2)"
;
2121
2122PyTypeObject PyDict_Type = {
2123 PyVarObject_HEAD_INIT(&PyType_Type, 0){ { 0, 0, 1, &PyType_Type }, 0 },
2124 "dict",
2125 sizeof(PyDictObject),
2126 0,
2127 (destructor)dict_dealloc, /* tp_dealloc */
2128 0, /* tp_print */
2129 0, /* tp_getattr */
2130 0, /* tp_setattr */
2131 0, /* tp_reserved */
2132 (reprfunc)dict_repr, /* tp_repr */
2133 0, /* tp_as_number */
2134 &dict_as_sequence, /* tp_as_sequence */
2135 &dict_as_mapping, /* tp_as_mapping */
2136 PyObject_HashNotImplemented, /* tp_hash */
2137 0, /* tp_call */
2138 0, /* tp_str */
2139 PyObject_GenericGetAttr, /* tp_getattro */
2140 0, /* tp_setattro */
2141 0, /* tp_as_buffer */
2142 Py_TPFLAGS_DEFAULT( 0 | (1L<<18) | 0) | Py_TPFLAGS_HAVE_GC(1L<<14) |
2143 Py_TPFLAGS_BASETYPE(1L<<10) | Py_TPFLAGS_DICT_SUBCLASS(1L<<29), /* tp_flags */
2144 dictionary_doc, /* tp_doc */
2145 dict_traverse, /* tp_traverse */
2146 dict_tp_clear, /* tp_clear */
2147 dict_richcompare, /* tp_richcompare */
2148 0, /* tp_weaklistoffset */
2149 (getiterfunc)dict_iter, /* tp_iter */
2150 0, /* tp_iternext */
2151 mapp_methods, /* tp_methods */
2152 0, /* tp_members */
2153 0, /* tp_getset */
2154 0, /* tp_base */
2155 0, /* tp_dict */
2156 0, /* tp_descr_get */
2157 0, /* tp_descr_set */
2158 0, /* tp_dictoffset */
2159 dict_init, /* tp_init */
2160 PyType_GenericAlloc, /* tp_alloc */
2161 dict_new, /* tp_new */
2162 PyObject_GC_Del, /* tp_free */
2163};
2164
2165/* For backward compatibility with old dictionary interface */
2166
2167PyObject *
2168PyDict_GetItemString(PyObject *v, const char *key)
2169{
2170 PyObject *kv, *rv;
2171 kv = PyUnicode_FromStringPyUnicodeUCS2_FromString(key);
2172 if (kv == NULL((void *)0))
2173 return NULL((void *)0);
2174 rv = PyDict_GetItem(v, kv);
2175 Py_DECREF(kv)do { if (_Py_RefTotal-- , --((PyObject*)(kv))->ob_refcnt !=
0) { if (((PyObject*)kv)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 2175, (PyObject *)(kv)); } else _Py_Dealloc
((PyObject *)(kv)); } while (0)
;
2176 return rv;
2177}
2178
2179int
2180PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
2181{
2182 PyObject *kv;
2183 int err;
2184 kv = PyUnicode_FromStringPyUnicodeUCS2_FromString(key);
2185 if (kv == NULL((void *)0))
2186 return -1;
2187 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
2188 err = PyDict_SetItem(v, kv, item);
2189 Py_DECREF(kv)do { if (_Py_RefTotal-- , --((PyObject*)(kv))->ob_refcnt !=
0) { if (((PyObject*)kv)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 2189, (PyObject *)(kv)); } else _Py_Dealloc
((PyObject *)(kv)); } while (0)
;
2190 return err;
2191}
2192
2193int
2194PyDict_DelItemString(PyObject *v, const char *key)
2195{
2196 PyObject *kv;
2197 int err;
2198 kv = PyUnicode_FromStringPyUnicodeUCS2_FromString(key);
2199 if (kv == NULL((void *)0))
2200 return -1;
2201 err = PyDict_DelItem(v, kv);
2202 Py_DECREF(kv)do { if (_Py_RefTotal-- , --((PyObject*)(kv))->ob_refcnt !=
0) { if (((PyObject*)kv)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 2202, (PyObject *)(kv)); } else _Py_Dealloc
((PyObject *)(kv)); } while (0)
;
2203 return err;
2204}
2205
2206/* Dictionary iterator types */
2207
2208typedef struct {
2209 PyObject_HEADPyObject ob_base;
2210 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
2211 Py_ssize_t di_used;
2212 Py_ssize_t di_pos;
2213 PyObject* di_result; /* reusable result tuple for iteritems */
2214 Py_ssize_t len;
2215} dictiterobject;
2216
2217static PyObject *
2218dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
2219{
2220 dictiterobject *di;
2221 di = PyObject_GC_New(dictiterobject, itertype)( (dictiterobject *) _PyObject_GC_New(itertype) );
2222 if (di == NULL((void *)0))
2223 return NULL((void *)0);
2224 Py_INCREF(dict)( _Py_RefTotal++ , ((PyObject*)(dict))->ob_refcnt++);
2225 di->di_dict = dict;
2226 di->di_used = dict->ma_used;
2227 di->di_pos = 0;
2228 di->len = dict->ma_used;
2229 if (itertype == &PyDictIterItem_Type) {
2230 di->di_result = PyTuple_Pack(2, Py_None(&_Py_NoneStruct), Py_None(&_Py_NoneStruct));
2231 if (di->di_result == NULL((void *)0)) {
2232 Py_DECREF(di)do { if (_Py_RefTotal-- , --((PyObject*)(di))->ob_refcnt !=
0) { if (((PyObject*)di)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 2232, (PyObject *)(di)); } else _Py_Dealloc
((PyObject *)(di)); } while (0)
;
2233 return NULL((void *)0);
2234 }
2235 }
2236 else
2237 di->di_result = NULL((void *)0);
2238 _PyObject_GC_TRACK(di)do { PyGC_Head *g = ((PyGC_Head *)(di)-1); if (g->gc.gc_refs
!= (-2)) Py_FatalError("GC object already tracked"); g->gc
.gc_refs = (-3); g->gc.gc_next = _PyGC_generation0; g->
gc.gc_prev = _PyGC_generation0->gc.gc_prev; g->gc.gc_prev
->gc.gc_next = g; _PyGC_generation0->gc.gc_prev = g; } while
(0);
;
2239 return (PyObject *)di;
2240}
2241
2242static void
2243dictiter_dealloc(dictiterobject *di)
2244{
2245 Py_XDECREF(di->di_dict)do { if ((di->di_dict) == ((void *)0)) ; else do { if (_Py_RefTotal
-- , --((PyObject*)(di->di_dict))->ob_refcnt != 0) { if
(((PyObject*)di->di_dict)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 2245, (PyObject *)(di->di_dict));
} else _Py_Dealloc((PyObject *)(di->di_dict)); } while (0
); } while (0)
;
2246 Py_XDECREF(di->di_result)do { if ((di->di_result) == ((void *)0)) ; else do { if (_Py_RefTotal
-- , --((PyObject*)(di->di_result))->ob_refcnt != 0) { if
(((PyObject*)di->di_result)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 2246, (PyObject *)(di->di_result)
); } else _Py_Dealloc((PyObject *)(di->di_result)); } while
(0); } while (0)
;
2247 PyObject_GC_Del(di);
2248}
2249
2250static int
2251dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
2252{
2253 Py_VISIT(di->di_dict)do { if (di->di_dict) { int vret = visit((PyObject *)(di->
di_dict), arg); if (vret) return vret; } } while (0)
;
2254 Py_VISIT(di->di_result)do { if (di->di_result) { int vret = visit((PyObject *)(di
->di_result), arg); if (vret) return vret; } } while (0)
;
2255 return 0;
2256}
2257
2258static PyObject *
2259dictiter_len(dictiterobject *di)
2260{
2261 Py_ssize_t len = 0;
2262 if (di->di_dict != NULL((void *)0) && di->di_used == di->di_dict->ma_used)
2263 len = di->len;
2264 return PyLong_FromSize_t(len);
2265}
2266
2267PyDoc_STRVAR(length_hint_doc,static char length_hint_doc[] = "Private method returning an estimate of len(list(it))."
2268 "Private method returning an estimate of len(list(it)).")static char length_hint_doc[] = "Private method returning an estimate of len(list(it)).";
2269
2270static PyMethodDef dictiter_methods[] = {
2271 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS0x0004,
2272 length_hint_doc},
2273 {NULL((void *)0), NULL((void *)0)} /* sentinel */
2274};
2275
2276static PyObject *dictiter_iternextkey(dictiterobject *di)
2277{
2278 PyObject *key;
2279 register Py_ssize_t i, mask;
2280 register PyDictEntry *ep;
2281 PyDictObject *d = di->di_dict;
2282
2283 if (d == NULL((void *)0))
2284 return NULL((void *)0);
2285 assert (PyDict_Check(d))(__builtin_expect(!(((((((PyObject*)(d))->ob_type))->tp_flags
& ((1L<<29))) != 0)), 0) ? __assert_rtn(__func__, "Objects/dictobject.c"
, 2285, "PyDict_Check(d)") : (void)0)
;
2286
2287 if (di->di_used != d->ma_used) {
2288 PyErr_SetString(PyExc_RuntimeError,
2289 "dictionary changed size during iteration");
2290 di->di_used = -1; /* Make this state sticky */
2291 return NULL((void *)0);
2292 }
2293
2294 i = di->di_pos;
2295 if (i < 0)
2296 goto fail;
2297 ep = d->ma_table;
2298 mask = d->ma_mask;
2299 while (i <= mask && ep[i].me_value == NULL((void *)0))
2300 i++;
2301 di->di_pos = i+1;
2302 if (i > mask)
2303 goto fail;
2304 di->len--;
2305 key = ep[i].me_key;
2306 Py_INCREF(key)( _Py_RefTotal++ , ((PyObject*)(key))->ob_refcnt++);
2307 return key;
2308
2309fail:
2310 Py_DECREF(d)do { if (_Py_RefTotal-- , --((PyObject*)(d))->ob_refcnt !=
0) { if (((PyObject*)d)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 2310, (PyObject *)(d)); } else _Py_Dealloc
((PyObject *)(d)); } while (0)
;
2311 di->di_dict = NULL((void *)0);
2312 return NULL((void *)0);
2313}
2314
2315PyTypeObject PyDictIterKey_Type = {
2316 PyVarObject_HEAD_INIT(&PyType_Type, 0){ { 0, 0, 1, &PyType_Type }, 0 },
2317 "dict_keyiterator", /* tp_name */
2318 sizeof(dictiterobject), /* tp_basicsize */
2319 0, /* tp_itemsize */
2320 /* methods */
2321 (destructor)dictiter_dealloc, /* tp_dealloc */
2322 0, /* tp_print */
2323 0, /* tp_getattr */
2324 0, /* tp_setattr */
2325 0, /* tp_reserved */
2326 0, /* tp_repr */
2327 0, /* tp_as_number */
2328 0, /* tp_as_sequence */
2329 0, /* tp_as_mapping */
2330 0, /* tp_hash */
2331 0, /* tp_call */
2332 0, /* tp_str */
2333 PyObject_GenericGetAttr, /* tp_getattro */
2334 0, /* tp_setattro */
2335 0, /* tp_as_buffer */
2336 Py_TPFLAGS_DEFAULT( 0 | (1L<<18) | 0) | Py_TPFLAGS_HAVE_GC(1L<<14),/* tp_flags */
2337 0, /* tp_doc */
2338 (traverseproc)dictiter_traverse, /* tp_traverse */
2339 0, /* tp_clear */
2340 0, /* tp_richcompare */
2341 0, /* tp_weaklistoffset */
2342 PyObject_SelfIter, /* tp_iter */
2343 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
2344 dictiter_methods, /* tp_methods */
2345 0,
2346};
2347
2348static PyObject *dictiter_iternextvalue(dictiterobject *di)
2349{
2350 PyObject *value;
2351 register Py_ssize_t i, mask;
2352 register PyDictEntry *ep;
2353 PyDictObject *d = di->di_dict;
2354
2355 if (d == NULL((void *)0))
2356 return NULL((void *)0);
2357 assert (PyDict_Check(d))(__builtin_expect(!(((((((PyObject*)(d))->ob_type))->tp_flags
& ((1L<<29))) != 0)), 0) ? __assert_rtn(__func__, "Objects/dictobject.c"
, 2357, "PyDict_Check(d)") : (void)0)
;
2358
2359 if (di->di_used != d->ma_used) {
2360 PyErr_SetString(PyExc_RuntimeError,
2361 "dictionary changed size during iteration");
2362 di->di_used = -1; /* Make this state sticky */
2363 return NULL((void *)0);
2364 }
2365
2366 i = di->di_pos;
2367 mask = d->ma_mask;
2368 if (i < 0 || i > mask)
2369 goto fail;
2370 ep = d->ma_table;
2371 while ((value=ep[i].me_value) == NULL((void *)0)) {
2372 i++;
2373 if (i > mask)
2374 goto fail;
2375 }
2376 di->di_pos = i+1;
2377 di->len--;
2378 Py_INCREF(value)( _Py_RefTotal++ , ((PyObject*)(value))->ob_refcnt++);
2379 return value;
2380
2381fail:
2382 Py_DECREF(d)do { if (_Py_RefTotal-- , --((PyObject*)(d))->ob_refcnt !=
0) { if (((PyObject*)d)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 2382, (PyObject *)(d)); } else _Py_Dealloc
((PyObject *)(d)); } while (0)
;
2383 di->di_dict = NULL((void *)0);
2384 return NULL((void *)0);
2385}
2386
2387PyTypeObject PyDictIterValue_Type = {
2388 PyVarObject_HEAD_INIT(&PyType_Type, 0){ { 0, 0, 1, &PyType_Type }, 0 },
2389 "dict_valueiterator", /* tp_name */
2390 sizeof(dictiterobject), /* tp_basicsize */
2391 0, /* tp_itemsize */
2392 /* methods */
2393 (destructor)dictiter_dealloc, /* tp_dealloc */
2394 0, /* tp_print */
2395 0, /* tp_getattr */
2396 0, /* tp_setattr */
2397 0, /* tp_reserved */
2398 0, /* tp_repr */
2399 0, /* tp_as_number */
2400 0, /* tp_as_sequence */
2401 0, /* tp_as_mapping */
2402 0, /* tp_hash */
2403 0, /* tp_call */
2404 0, /* tp_str */
2405 PyObject_GenericGetAttr, /* tp_getattro */
2406 0, /* tp_setattro */
2407 0, /* tp_as_buffer */
2408 Py_TPFLAGS_DEFAULT( 0 | (1L<<18) | 0) | Py_TPFLAGS_HAVE_GC(1L<<14),/* tp_flags */
2409 0, /* tp_doc */
2410 (traverseproc)dictiter_traverse, /* tp_traverse */
2411 0, /* tp_clear */
2412 0, /* tp_richcompare */
2413 0, /* tp_weaklistoffset */
2414 PyObject_SelfIter, /* tp_iter */
2415 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
2416 dictiter_methods, /* tp_methods */
2417 0,
2418};
2419
2420static PyObject *dictiter_iternextitem(dictiterobject *di)
2421{
2422 PyObject *key, *value, *result = di->di_result;
2423 register Py_ssize_t i, mask;
2424 register PyDictEntry *ep;
2425 PyDictObject *d = di->di_dict;
2426
2427 if (d == NULL((void *)0))
2428 return NULL((void *)0);
2429 assert (PyDict_Check(d))(__builtin_expect(!(((((((PyObject*)(d))->ob_type))->tp_flags
& ((1L<<29))) != 0)), 0) ? __assert_rtn(__func__, "Objects/dictobject.c"
, 2429, "PyDict_Check(d)") : (void)0)
;
2430
2431 if (di->di_used != d->ma_used) {
2432 PyErr_SetString(PyExc_RuntimeError,
2433 "dictionary changed size during iteration");
2434 di->di_used = -1; /* Make this state sticky */
2435 return NULL((void *)0);
2436 }
2437
2438 i = di->di_pos;
2439 if (i < 0)
2440 goto fail;
2441 ep = d->ma_table;
2442 mask = d->ma_mask;
2443 while (i <= mask && ep[i].me_value == NULL((void *)0))
2444 i++;
2445 di->di_pos = i+1;
2446 if (i > mask)
2447 goto fail;
2448
2449 if (result->ob_refcnt == 1) {
2450 Py_INCREF(result)( _Py_RefTotal++ , ((PyObject*)(result))->ob_refcnt++);
2451 Py_DECREF(PyTuple_GET_ITEM(result, 0))do { if (_Py_RefTotal-- , --((PyObject*)((((PyTupleObject *)(
result))->ob_item[0])))->ob_refcnt != 0) { if (((PyObject
*)(((PyTupleObject *)(result))->ob_item[0]))->ob_refcnt
< 0) _Py_NegativeRefcount("Objects/dictobject.c", 2451, (
PyObject *)((((PyTupleObject *)(result))->ob_item[0]))); }
else _Py_Dealloc((PyObject *)((((PyTupleObject *)(result))->
ob_item[0]))); } while (0)
;
2452 Py_DECREF(PyTuple_GET_ITEM(result, 1))do { if (_Py_RefTotal-- , --((PyObject*)((((PyTupleObject *)(
result))->ob_item[1])))->ob_refcnt != 0) { if (((PyObject
*)(((PyTupleObject *)(result))->ob_item[1]))->ob_refcnt
< 0) _Py_NegativeRefcount("Objects/dictobject.c", 2452, (
PyObject *)((((PyTupleObject *)(result))->ob_item[1]))); }
else _Py_Dealloc((PyObject *)((((PyTupleObject *)(result))->
ob_item[1]))); } while (0)
;
2453 } else {
2454 result = PyTuple_New(2);
2455 if (result == NULL((void *)0))
2456 return NULL((void *)0);
2457 }
2458 di->len--;
2459 key = ep[i].me_key;
2460 value = ep[i].me_value;
2461 Py_INCREF(key)( _Py_RefTotal++ , ((PyObject*)(key))->ob_refcnt++);
2462 Py_INCREF(value)( _Py_RefTotal++ , ((PyObject*)(value))->ob_refcnt++);
2463 PyTuple_SET_ITEM(result, 0, key)(((PyTupleObject *)(result))->ob_item[0] = key);
2464 PyTuple_SET_ITEM(result, 1, value)(((PyTupleObject *)(result))->ob_item[1] = value);
2465 return result;
2466
2467fail:
2468 Py_DECREF(d)do { if (_Py_RefTotal-- , --((PyObject*)(d))->ob_refcnt !=
0) { if (((PyObject*)d)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 2468, (PyObject *)(d)); } else _Py_Dealloc
((PyObject *)(d)); } while (0)
;
2469 di->di_dict = NULL((void *)0);
2470 return NULL((void *)0);
2471}
2472
2473PyTypeObject PyDictIterItem_Type = {
2474 PyVarObject_HEAD_INIT(&PyType_Type, 0){ { 0, 0, 1, &PyType_Type }, 0 },
2475 "dict_itemiterator", /* tp_name */
2476 sizeof(dictiterobject), /* tp_basicsize */
2477 0, /* tp_itemsize */
2478 /* methods */
2479 (destructor)dictiter_dealloc, /* tp_dealloc */
2480 0, /* tp_print */
2481 0, /* tp_getattr */
2482 0, /* tp_setattr */
2483 0, /* tp_reserved */
2484 0, /* tp_repr */
2485 0, /* tp_as_number */
2486 0, /* tp_as_sequence */
2487 0, /* tp_as_mapping */
2488 0, /* tp_hash */
2489 0, /* tp_call */
2490 0, /* tp_str */
2491 PyObject_GenericGetAttr, /* tp_getattro */
2492 0, /* tp_setattro */
2493 0, /* tp_as_buffer */
2494 Py_TPFLAGS_DEFAULT( 0 | (1L<<18) | 0) | Py_TPFLAGS_HAVE_GC(1L<<14),/* tp_flags */
2495 0, /* tp_doc */
2496 (traverseproc)dictiter_traverse, /* tp_traverse */
2497 0, /* tp_clear */
2498 0, /* tp_richcompare */
2499 0, /* tp_weaklistoffset */
2500 PyObject_SelfIter, /* tp_iter */
2501 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
2502 dictiter_methods, /* tp_methods */
2503 0,
2504};
2505
2506
2507/***********************************************/
2508/* View objects for keys(), items(), values(). */
2509/***********************************************/
2510
2511/* The instance lay-out is the same for all three; but the type differs. */
2512
2513typedef struct {
2514 PyObject_HEADPyObject ob_base;
2515 PyDictObject *dv_dict;
2516} dictviewobject;
2517
2518
2519static void
2520dictview_dealloc(dictviewobject *dv)
2521{
2522 Py_XDECREF(dv->dv_dict)do { if ((dv->dv_dict) == ((void *)0)) ; else do { if (_Py_RefTotal
-- , --((PyObject*)(dv->dv_dict))->ob_refcnt != 0) { if
(((PyObject*)dv->dv_dict)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 2522, (PyObject *)(dv->dv_dict));
} else _Py_Dealloc((PyObject *)(dv->dv_dict)); } while (0
); } while (0)
;
2523 PyObject_GC_Del(dv);
2524}
2525
2526static int
2527dictview_traverse(dictviewobject *dv, visitproc visit, void *arg)
2528{
2529 Py_VISIT(dv->dv_dict)do { if (dv->dv_dict) { int vret = visit((PyObject *)(dv->
dv_dict), arg); if (vret) return vret; } } while (0)
;
2530 return 0;
2531}
2532
2533static Py_ssize_t
2534dictview_len(dictviewobject *dv)
2535{
2536 Py_ssize_t len = 0;
2537 if (dv->dv_dict != NULL((void *)0))
2538 len = dv->dv_dict->ma_used;
2539 return len;
2540}
2541
2542static PyObject *
2543dictview_new(PyObject *dict, PyTypeObject *type)
2544{
2545 dictviewobject *dv;
2546 if (dict == NULL((void *)0)) {
2547 PyErr_BadInternalCall()_PyErr_BadInternalCall("Objects/dictobject.c", 2547);
2548 return NULL((void *)0);
2549 }
2550 if (!PyDict_Check(dict)((((((PyObject*)(dict))->ob_type))->tp_flags & ((1L
<<29))) != 0)
) {
2551 /* XXX Get rid of this restriction later */
2552 PyErr_Format(PyExc_TypeError,
2553 "%s() requires a dict argument, not '%s'",
2554 type->tp_name, dict->ob_type->tp_name);
2555 return NULL((void *)0);
2556 }
2557 dv = PyObject_GC_New(dictviewobject, type)( (dictviewobject *) _PyObject_GC_New(type) );
2558 if (dv == NULL((void *)0))
2559 return NULL((void *)0);
2560 Py_INCREF(dict)( _Py_RefTotal++ , ((PyObject*)(dict))->ob_refcnt++);
2561 dv->dv_dict = (PyDictObject *)dict;
2562 _PyObject_GC_TRACK(dv)do { PyGC_Head *g = ((PyGC_Head *)(dv)-1); if (g->gc.gc_refs
!= (-2)) Py_FatalError("GC object already tracked"); g->gc
.gc_refs = (-3); g->gc.gc_next = _PyGC_generation0; g->
gc.gc_prev = _PyGC_generation0->gc.gc_prev; g->gc.gc_prev
->gc.gc_next = g; _PyGC_generation0->gc.gc_prev = g; } while
(0);
;
2563 return (PyObject *)dv;
2564}
2565
2566/* TODO(guido): The views objects are not complete:
2567
2568 * support more set operations
2569 * support arbitrary mappings?
2570 - either these should be static or exported in dictobject.h
2571 - if public then they should probably be in builtins
2572*/
2573
2574/* Return 1 if self is a subset of other, iterating over self;
2575 0 if not; -1 if an error occurred. */
2576static int
2577all_contained_in(PyObject *self, PyObject *other)
2578{
2579 PyObject *iter = PyObject_GetIter(self);
2580 int ok = 1;
2581
2582 if (iter == NULL((void *)0))
2583 return -1;
2584 for (;;) {
2585 PyObject *next = PyIter_Next(iter);
2586 if (next == NULL((void *)0)) {
2587 if (PyErr_Occurred())
2588 ok = -1;
2589 break;
2590 }
2591 ok = PySequence_Contains(other, next);
2592 Py_DECREF(next)do { if (_Py_RefTotal-- , --((PyObject*)(next))->ob_refcnt
!= 0) { if (((PyObject*)next)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 2592, (PyObject *)(next)); } else _Py_Dealloc
((PyObject *)(next)); } while (0)
;
2593 if (ok <= 0)
2594 break;
2595 }
2596 Py_DECREF(iter)do { if (_Py_RefTotal-- , --((PyObject*)(iter))->ob_refcnt
!= 0) { if (((PyObject*)iter)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 2596, (PyObject *)(iter)); } else _Py_Dealloc
((PyObject *)(iter)); } while (0)
;
2597 return ok;
2598}
2599
2600static PyObject *
2601dictview_richcompare(PyObject *self, PyObject *other, int op)
2602{
2603 Py_ssize_t len_self, len_other;
2604 int ok;
2605 PyObject *result;
2606
2607 assert(self != NULL)(__builtin_expect(!(self != ((void *)0)), 0) ? __assert_rtn(__func__
, "Objects/dictobject.c", 2607, "self != NULL") : (void)0)
;
2608 assert(PyDictViewSet_Check(self))(__builtin_expect(!((((((PyObject*)(self))->ob_type) == &
PyDictKeys_Type) || ((((PyObject*)(self))->ob_type) == &
PyDictItems_Type))), 0) ? __assert_rtn(__func__, "Objects/dictobject.c"
, 2608, "PyDictViewSet_Check(self)") : (void)0)
;
2609 assert(other != NULL)(__builtin_expect(!(other != ((void *)0)), 0) ? __assert_rtn(
__func__, "Objects/dictobject.c", 2609, "other != NULL") : (void
)0)
;
2610
2611 if (!PyAnySet_Check(other)((((PyObject*)(other))->ob_type) == &PySet_Type || (((
PyObject*)(other))->ob_type) == &PyFrozenSet_Type || PyType_IsSubtype
((((PyObject*)(other))->ob_type), &PySet_Type) || PyType_IsSubtype
((((PyObject*)(other))->ob_type), &PyFrozenSet_Type))
&& !PyDictViewSet_Check(other)(((((PyObject*)(other))->ob_type) == &PyDictKeys_Type)
|| ((((PyObject*)(other))->ob_type) == &PyDictItems_Type
))
) {
2612 Py_INCREF(Py_NotImplemented)( _Py_RefTotal++ , ((PyObject*)((&_Py_NotImplementedStruct
)))->ob_refcnt++)
;
2613 return Py_NotImplemented(&_Py_NotImplementedStruct);
2614 }
2615
2616 len_self = PyObject_Size(self);
2617 if (len_self < 0)
2618 return NULL((void *)0);
2619 len_other = PyObject_Size(other);
2620 if (len_other < 0)
2621 return NULL((void *)0);
2622
2623 ok = 0;
2624 switch(op) {
2625
2626 case Py_NE3:
2627 case Py_EQ2:
2628 if (len_self == len_other)
2629 ok = all_contained_in(self, other);
2630 if (op == Py_NE3 && ok >= 0)
2631 ok = !ok;
2632 break;
2633
2634 case Py_LT0:
2635 if (len_self < len_other)
2636 ok = all_contained_in(self, other);
2637 break;
2638
2639 case Py_LE1:
2640 if (len_self <= len_other)
2641 ok = all_contained_in(self, other);
2642 break;
2643
2644 case Py_GT4:
2645 if (len_self > len_other)
2646 ok = all_contained_in(other, self);
2647 break;
2648
2649 case Py_GE5:
2650 if (len_self >= len_other)
2651 ok = all_contained_in(other, self);
2652 break;
2653
2654 }
2655 if (ok < 0)
2656 return NULL((void *)0);
2657 result = ok ? Py_True((PyObject *) &_Py_TrueStruct) : Py_False((PyObject *) &_Py_FalseStruct);
2658 Py_INCREF(result)( _Py_RefTotal++ , ((PyObject*)(result))->ob_refcnt++);
2659 return result;
2660}
2661
2662static PyObject *
2663dictview_repr(dictviewobject *dv)
2664{
2665 PyObject *seq;
2666 PyObject *result;
2667
2668 seq = PySequence_List((PyObject *)dv);
2669 if (seq == NULL((void *)0))
2670 return NULL((void *)0);
2671
2672 result = PyUnicode_FromFormatPyUnicodeUCS2_FromFormat("%s(%R)", Py_TYPE(dv)(((PyObject*)(dv))->ob_type)->tp_name, seq);
2673 Py_DECREF(seq)do { if (_Py_RefTotal-- , --((PyObject*)(seq))->ob_refcnt !=
0) { if (((PyObject*)seq)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 2673, (PyObject *)(seq)); } else _Py_Dealloc
((PyObject *)(seq)); } while (0)
;
2674 return result;
2675}
2676
2677/*** dict_keys ***/
2678
2679static PyObject *
2680dictkeys_iter(dictviewobject *dv)
2681{
2682 if (dv->dv_dict == NULL((void *)0)) {
2683 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
2684 }
2685 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
2686}
2687
2688static int
2689dictkeys_contains(dictviewobject *dv, PyObject *obj)
2690{
2691 if (dv->dv_dict == NULL((void *)0))
2692 return 0;
2693 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
2694}
2695
2696static PySequenceMethods dictkeys_as_sequence = {
2697 (lenfunc)dictview_len, /* sq_length */
2698 0, /* sq_concat */
2699 0, /* sq_repeat */
2700 0, /* sq_item */
2701 0, /* sq_slice */
2702 0, /* sq_ass_item */
2703 0, /* sq_ass_slice */
2704 (objobjproc)dictkeys_contains, /* sq_contains */
2705};
2706
2707static PyObject*
2708dictviews_sub(PyObject* self, PyObject *other)
2709{
2710 PyObject *result = PySet_New(self);
2711 PyObject *tmp;
2712 if (result == NULL((void *)0))
2713 return NULL((void *)0);
2714
2715 tmp = PyObject_CallMethod(result, "difference_update", "O", other);
2716 if (tmp == NULL((void *)0)) {
2717 Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt
!= 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 2717, (PyObject *)(result)); } else _Py_Dealloc
((PyObject *)(result)); } while (0)
;
2718 return NULL((void *)0);
2719 }
2720
2721 Py_DECREF(tmp)do { if (_Py_RefTotal-- , --((PyObject*)(tmp))->ob_refcnt !=
0) { if (((PyObject*)tmp)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 2721, (PyObject *)(tmp)); } else _Py_Dealloc
((PyObject *)(tmp)); } while (0)
;
2722 return result;
2723}
2724
2725static PyObject*
2726dictviews_and(PyObject* self, PyObject *other)
2727{
2728 PyObject *result = PySet_New(self);
2729 PyObject *tmp;
2730 if (result == NULL((void *)0))
2731 return NULL((void *)0);
2732
2733 tmp = PyObject_CallMethod(result, "intersection_update", "O", other);
2734 if (tmp == NULL((void *)0)) {
2735 Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt
!= 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 2735, (PyObject *)(result)); } else _Py_Dealloc
((PyObject *)(result)); } while (0)
;
2736 return NULL((void *)0);
2737 }
2738
2739 Py_DECREF(tmp)do { if (_Py_RefTotal-- , --((PyObject*)(tmp))->ob_refcnt !=
0) { if (((PyObject*)tmp)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 2739, (PyObject *)(tmp)); } else _Py_Dealloc
((PyObject *)(tmp)); } while (0)
;
2740 return result;
2741}
2742
2743static PyObject*
2744dictviews_or(PyObject* self, PyObject *other)
2745{
2746 PyObject *result = PySet_New(self);
2747 PyObject *tmp;
2748 if (result == NULL((void *)0))
2749 return NULL((void *)0);
2750
2751 tmp = PyObject_CallMethod(result, "update", "O", other);
2752 if (tmp == NULL((void *)0)) {
2753 Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt
!= 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 2753, (PyObject *)(result)); } else _Py_Dealloc
((PyObject *)(result)); } while (0)
;
2754 return NULL((void *)0);
2755 }
2756
2757 Py_DECREF(tmp)do { if (_Py_RefTotal-- , --((PyObject*)(tmp))->ob_refcnt !=
0) { if (((PyObject*)tmp)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 2757, (PyObject *)(tmp)); } else _Py_Dealloc
((PyObject *)(tmp)); } while (0)
;
2758 return result;
2759}
2760
2761static PyObject*
2762dictviews_xor(PyObject* self, PyObject *other)
2763{
2764 PyObject *result = PySet_New(self);
2765 PyObject *tmp;
2766 if (result == NULL((void *)0))
2767 return NULL((void *)0);
2768
2769 tmp = PyObject_CallMethod(result, "symmetric_difference_update", "O",
2770 other);
2771 if (tmp == NULL((void *)0)) {
2772 Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt
!= 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 2772, (PyObject *)(result)); } else _Py_Dealloc
((PyObject *)(result)); } while (0)
;
2773 return NULL((void *)0);
2774 }
2775
2776 Py_DECREF(tmp)do { if (_Py_RefTotal-- , --((PyObject*)(tmp))->ob_refcnt !=
0) { if (((PyObject*)tmp)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 2776, (PyObject *)(tmp)); } else _Py_Dealloc
((PyObject *)(tmp)); } while (0)
;
2777 return result;
2778}
2779
2780static PyNumberMethods dictviews_as_number = {
2781 0, /*nb_add*/
2782 (binaryfunc)dictviews_sub, /*nb_subtract*/
2783 0, /*nb_multiply*/
2784 0, /*nb_remainder*/
2785 0, /*nb_divmod*/
2786 0, /*nb_power*/
2787 0, /*nb_negative*/
2788 0, /*nb_positive*/
2789 0, /*nb_absolute*/
2790 0, /*nb_bool*/
2791 0, /*nb_invert*/
2792 0, /*nb_lshift*/
2793 0, /*nb_rshift*/
2794 (binaryfunc)dictviews_and, /*nb_and*/
2795 (binaryfunc)dictviews_xor, /*nb_xor*/
2796 (binaryfunc)dictviews_or, /*nb_or*/
2797};
2798
2799static PyObject*
2800dictviews_isdisjoint(PyObject *self, PyObject *other)
2801{
2802 PyObject *it;
2803 PyObject *item = NULL((void *)0);
2804
2805 if (self == other) {
2806 if (dictview_len((dictviewobject *)self) == 0)
2807 Py_RETURN_TRUEreturn ( _Py_RefTotal++ , ((PyObject*)(((PyObject *) &_Py_TrueStruct
)))->ob_refcnt++), ((PyObject *) &_Py_TrueStruct)
;
2808 else
2809 Py_RETURN_FALSEreturn ( _Py_RefTotal++ , ((PyObject*)(((PyObject *) &_Py_FalseStruct
)))->ob_refcnt++), ((PyObject *) &_Py_FalseStruct)
;
2810 }
2811
2812 /* Iterate over the shorter object (only if other is a set,
2813 * because PySequence_Contains may be expensive otherwise): */
2814 if (PyAnySet_Check(other)((((PyObject*)(other))->ob_type) == &PySet_Type || (((
PyObject*)(other))->ob_type) == &PyFrozenSet_Type || PyType_IsSubtype
((((PyObject*)(other))->ob_type), &PySet_Type) || PyType_IsSubtype
((((PyObject*)(other))->ob_type), &PyFrozenSet_Type))
|| PyDictViewSet_Check(other)(((((PyObject*)(other))->ob_type) == &PyDictKeys_Type)
|| ((((PyObject*)(other))->ob_type) == &PyDictItems_Type
))
) {
2815 Py_ssize_t len_self = dictview_len((dictviewobject *)self);
2816 Py_ssize_t len_other = PyObject_Size(other);
2817 if (len_other == -1)
2818 return NULL((void *)0);
2819
2820 if ((len_other > len_self)) {
2821 PyObject *tmp = other;
2822 other = self;
2823 self = tmp;
2824 }
2825 }
2826
2827 it = PyObject_GetIter(other);
2828 if (it == NULL((void *)0))
2829 return NULL((void *)0);
2830
2831 while ((item = PyIter_Next(it)) != NULL((void *)0)) {
2832 int contains = PySequence_Contains(self, item);
2833 Py_DECREF(item)do { if (_Py_RefTotal-- , --((PyObject*)(item))->ob_refcnt
!= 0) { if (((PyObject*)item)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 2833, (PyObject *)(item)); } else _Py_Dealloc
((PyObject *)(item)); } while (0)
;
2834 if (contains == -1) {
2835 Py_DECREF(it)do { if (_Py_RefTotal-- , --((PyObject*)(it))->ob_refcnt !=
0) { if (((PyObject*)it)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 2835, (PyObject *)(it)); } else _Py_Dealloc
((PyObject *)(it)); } while (0)
;
2836 return NULL((void *)0);
2837 }
2838
2839 if (contains) {
2840 Py_DECREF(it)do { if (_Py_RefTotal-- , --((PyObject*)(it))->ob_refcnt !=
0) { if (((PyObject*)it)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 2840, (PyObject *)(it)); } else _Py_Dealloc
((PyObject *)(it)); } while (0)
;
2841 Py_RETURN_FALSEreturn ( _Py_RefTotal++ , ((PyObject*)(((PyObject *) &_Py_FalseStruct
)))->ob_refcnt++), ((PyObject *) &_Py_FalseStruct)
;
2842 }
2843 }
2844 Py_DECREF(it)do { if (_Py_RefTotal-- , --((PyObject*)(it))->ob_refcnt !=
0) { if (((PyObject*)it)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/dictobject.c", 2844, (PyObject *)(it)); } else _Py_Dealloc
((PyObject *)(it)); } while (0)
;
2845 if (PyErr_Occurred())
2846 return NULL((void *)0); /* PyIter_Next raised an exception. */
2847 Py_RETURN_TRUEreturn ( _Py_RefTotal++ , ((PyObject*)(((PyObject *) &_Py_TrueStruct
)))->ob_refcnt++), ((PyObject *) &_Py_TrueStruct)
;
2848}
2849
2850PyDoc_STRVAR(isdisjoint_doc,static char isdisjoint_doc[] = "Return True if the view and the given iterable have a null intersection."
2851"Return True if the view and the given iterable have a null intersection.")static char isdisjoint_doc[] = "Return True if the view and the given iterable have a null intersection.";
2852
2853static PyMethodDef dictkeys_methods[] = {
2854 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O0x0008,
2855 isdisjoint_doc},
2856 {NULL((void *)0), NULL((void *)0)} /* sentinel */
2857};
2858
2859PyTypeObject PyDictKeys_Type = {
2860 PyVarObject_HEAD_INIT(&PyType_Type, 0){ { 0, 0, 1, &PyType_Type }, 0 },
2861 "dict_keys", /* tp_name */
2862 sizeof(dictviewobject), /* tp_basicsize */
2863 0, /* tp_itemsize */
2864 /* methods */
2865 (destructor)dictview_dealloc, /* tp_dealloc */
2866 0, /* tp_print */
2867 0, /* tp_getattr */
2868 0, /* tp_setattr */
2869 0, /* tp_reserved */
2870 (reprfunc)dictview_repr, /* tp_repr */
2871 &dictviews_as_number, /* tp_as_number */
2872 &dictkeys_as_sequence, /* tp_as_sequence */
2873 0, /* tp_as_mapping */
2874 0, /* tp_hash */
2875 0, /* tp_call */
2876 0, /* tp_str */
2877 PyObject_GenericGetAttr, /* tp_getattro */
2878 0, /* tp_setattro */
2879 0, /* tp_as_buffer */
2880 Py_TPFLAGS_DEFAULT( 0 | (1L<<18) | 0) | Py_TPFLAGS_HAVE_GC(1L<<14),/* tp_flags */
2881 0, /* tp_doc */
2882 (traverseproc)dictview_traverse, /* tp_traverse */
2883 0, /* tp_clear */
2884 dictview_richcompare, /* tp_richcompare */
2885 0, /* tp_weaklistoffset */
2886 (getiterfunc)dictkeys_iter, /* tp_iter */
2887 0, /* tp_iternext */
2888 dictkeys_methods, /* tp_methods */
2889 0,
2890};
2891
2892static PyObject *
2893dictkeys_new(PyObject *dict)
2894{
2895 return dictview_new(dict, &PyDictKeys_Type);
2896}
2897
2898/*** dict_items ***/
2899
2900static PyObject *
2901dictitems_iter(dictviewobject *dv)
2902{
2903 if (dv->dv_dict == NULL((void *)0)) {
2904 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
2905 }
2906 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
2907}
2908
2909static int
2910dictitems_contains(dictviewobject *dv, PyObject *obj)
2911{
2912 PyObject *key, *value, *found;
2913 if (dv->dv_dict == NULL((void *)0))
2914 return 0;
2915 if (!PyTuple_Check(obj)((((((PyObject*)(obj))->ob_type))->tp_flags & ((1L<<
26))) != 0)
|| PyTuple_GET_SIZE(obj)(((PyVarObject*)(obj))->ob_size) != 2)
2916 return 0;
2917 key = PyTuple_GET_ITEM(obj, 0)(((PyTupleObject *)(obj))->ob_item[0]);
2918 value = PyTuple_GET_ITEM(obj, 1)(((PyTupleObject *)(obj))->ob_item[1]);
2919 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
2920 if (found == NULL((void *)0)) {
2921 if (PyErr_Occurred())
2922 return -1;
2923 return 0;
2924 }
2925 return PyObject_RichCompareBool(value, found, Py_EQ2);
2926}
2927
2928static PySequenceMethods dictitems_as_sequence = {
2929 (lenfunc)dictview_len, /* sq_length */
2930 0, /* sq_concat */
2931 0, /* sq_repeat */
2932 0, /* sq_item */
2933 0, /* sq_slice */
2934 0, /* sq_ass_item */
2935 0, /* sq_ass_slice */
2936 (objobjproc)dictitems_contains, /* sq_contains */
2937};
2938
2939static PyMethodDef dictitems_methods[] = {
2940 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O0x0008,
2941 isdisjoint_doc},
2942 {NULL((void *)0), NULL((void *)0)} /* sentinel */
2943};
2944
2945PyTypeObject PyDictItems_Type = {
2946 PyVarObject_HEAD_INIT(&PyType_Type, 0){ { 0, 0, 1, &PyType_Type }, 0 },
2947 "dict_items", /* tp_name */
2948 sizeof(dictviewobject), /* tp_basicsize */
2949 0, /* tp_itemsize */
2950 /* methods */
2951 (destructor)dictview_dealloc, /* tp_dealloc */
2952 0, /* tp_print */
2953 0, /* tp_getattr */
2954 0, /* tp_setattr */
2955 0, /* tp_reserved */
2956 (reprfunc)dictview_repr, /* tp_repr */
2957 &dictviews_as_number, /* tp_as_number */
2958 &dictitems_as_sequence, /* tp_as_sequence */
2959 0, /* tp_as_mapping */
2960 0, /* tp_hash */
2961 0, /* tp_call */
2962 0, /* tp_str */
2963 PyObject_GenericGetAttr, /* tp_getattro */
2964 0, /* tp_setattro */
2965 0, /* tp_as_buffer */
2966 Py_TPFLAGS_DEFAULT( 0 | (1L<<18) | 0) | Py_TPFLAGS_HAVE_GC(1L<<14),/* tp_flags */
2967 0, /* tp_doc */
2968 (traverseproc)dictview_traverse, /* tp_traverse */
2969 0, /* tp_clear */
2970 dictview_richcompare, /* tp_richcompare */
2971 0, /* tp_weaklistoffset */
2972 (getiterfunc)dictitems_iter, /* tp_iter */
2973 0, /* tp_iternext */
2974 dictitems_methods, /* tp_methods */
2975 0,
2976};
2977
2978static PyObject *
2979dictitems_new(PyObject *dict)
2980{
2981 return dictview_new(dict, &PyDictItems_Type);
2982}
2983
2984/*** dict_values ***/
2985
2986static PyObject *
2987dictvalues_iter(dictviewobject *dv)
2988{
2989 if (dv->dv_dict == NULL((void *)0)) {
2990 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
2991 }
2992 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
2993}
2994
2995static PySequenceMethods dictvalues_as_sequence = {
2996 (lenfunc)dictview_len, /* sq_length */
2997 0, /* sq_concat */
2998 0, /* sq_repeat */
2999 0, /* sq_item */
3000 0, /* sq_slice */
3001 0, /* sq_ass_item */
3002 0, /* sq_ass_slice */
3003 (objobjproc)0, /* sq_contains */
3004};
3005
3006static PyMethodDef dictvalues_methods[] = {
3007 {NULL((void *)0), NULL((void *)0)} /* sentinel */
3008};
3009
3010PyTypeObject PyDictValues_Type = {
3011 PyVarObject_HEAD_INIT(&PyType_Type, 0){ { 0, 0, 1, &PyType_Type }, 0 },
3012 "dict_values", /* tp_name */
3013 sizeof(dictviewobject), /* tp_basicsize */
3014 0, /* tp_itemsize */
3015 /* methods */
3016 (destructor)dictview_dealloc, /* tp_dealloc */
3017 0, /* tp_print */
3018 0, /* tp_getattr */
3019 0, /* tp_setattr */
3020 0, /* tp_reserved */
3021 (reprfunc)dictview_repr, /* tp_repr */
3022 0, /* tp_as_number */
3023 &dictvalues_as_sequence, /* tp_as_sequence */
3024 0, /* tp_as_mapping */
3025 0, /* tp_hash */
3026 0, /* tp_call */
3027 0, /* tp_str */
3028 PyObject_GenericGetAttr, /* tp_getattro */
3029 0, /* tp_setattro */
3030 0, /* tp_as_buffer */
3031 Py_TPFLAGS_DEFAULT( 0 | (1L<<18) | 0) | Py_TPFLAGS_HAVE_GC(1L<<14),/* tp_flags */
3032 0, /* tp_doc */
3033 (traverseproc)dictview_traverse, /* tp_traverse */
3034 0, /* tp_clear */
3035 0, /* tp_richcompare */
3036 0, /* tp_weaklistoffset */
3037 (getiterfunc)dictvalues_iter, /* tp_iter */
3038 0, /* tp_iternext */
3039 dictvalues_methods, /* tp_methods */
3040 0,
3041};
3042
3043static PyObject *
3044dictvalues_new(PyObject *dict)
3045{
3046 return dictview_new(dict, &PyDictValues_Type);
3047}