On 64-bit platforms running test_struct after test_tarfile would fail
[python.git] / Objects / dictobject.c
blobd4cd925b8b82b00603944e9b0d02956c166500a5
2 /* Dictionary object implementation using a hash table */
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 */
10 #include "Python.h"
12 typedef PyDictEntry dictentry;
13 typedef PyDictObject dictobject;
15 /* Define this out if you don't want conversion statistics on exit. */
16 #undef SHOW_CONVERSION_COUNTS
18 /* See large comment block below. This must be >= 1. */
19 #define PERTURB_SHIFT 5
22 Major subtleties ahead: Most hash schemes depend on having a "good" hash
23 function, in the sense of simulating randomness. Python doesn't: its most
24 important hash functions (for strings and ints) are very regular in common
25 cases:
27 >>> map(hash, (0, 1, 2, 3))
28 [0, 1, 2, 3]
29 >>> map(hash, ("namea", "nameb", "namec", "named"))
30 [-1658398457, -1658398460, -1658398459, -1658398462]
31 >>>
33 This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
34 the low-order i bits as the initial table index is extremely fast, and there
35 are no collisions at all for dicts indexed by a contiguous range of ints.
36 The same is approximately true when keys are "consecutive" strings. So this
37 gives better-than-random behavior in common cases, and that's very desirable.
39 OTOH, when collisions occur, the tendency to fill contiguous slices of the
40 hash table makes a good collision resolution strategy crucial. Taking only
41 the last i bits of the hash code is also vulnerable: for example, consider
42 [i << 16 for i in range(20000)] as a set of keys. Since ints are their own
43 hash codes, and this fits in a dict of size 2**15, the last 15 bits of every
44 hash code are all 0: they *all* map to the same table index.
46 But catering to unusual cases should not slow the usual ones, so we just take
47 the last i bits anyway. It's up to collision resolution to do the rest. If
48 we *usually* find the key we're looking for on the first try (and, it turns
49 out, we usually do -- the table load factor is kept under 2/3, so the odds
50 are solidly in our favor), then it makes best sense to keep the initial index
51 computation dirt cheap.
53 The first half of collision resolution is to visit table indices via this
54 recurrence:
56 j = ((5*j) + 1) mod 2**i
58 For any initial j in range(2**i), repeating that 2**i times generates each
59 int in range(2**i) exactly once (see any text on random-number generation for
60 proof). By itself, this doesn't help much: like linear probing (setting
61 j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
62 order. This would be bad, except that's not the only thing we do, and it's
63 actually *good* in the common cases where hash keys are consecutive. In an
64 example that's really too small to make this entirely clear, for a table of
65 size 2**3 the order of indices is:
67 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
69 If two things come in at index 5, the first place we look after is index 2,
70 not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
71 Linear probing is deadly in this case because there the fixed probe order
72 is the *same* as the order consecutive keys are likely to arrive. But it's
73 extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
74 and certain that consecutive hash codes do not.
76 The other half of the strategy is to get the other bits of the hash code
77 into play. This is done by initializing a (unsigned) vrbl "perturb" to the
78 full hash code, and changing the recurrence to:
80 j = (5*j) + 1 + perturb;
81 perturb >>= PERTURB_SHIFT;
82 use j % 2**i as the next table index;
84 Now the probe sequence depends (eventually) on every bit in the hash code,
85 and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
86 because it quickly magnifies small differences in the bits that didn't affect
87 the initial index. Note that because perturb is unsigned, if the recurrence
88 is executed often enough perturb eventually becomes and remains 0. At that
89 point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
90 that's certain to find an empty slot eventually (since it generates every int
91 in range(2**i), and we make sure there's always at least one empty slot).
93 Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
94 small so that the high bits of the hash code continue to affect the probe
95 sequence across iterations; but you want it large so that in really bad cases
96 the high-order hash bits have an effect on early iterations. 5 was "the
97 best" in minimizing total collisions across experiments Tim Peters ran (on
98 both normal and pathological cases), but 4 and 6 weren't significantly worse.
100 Historical: Reimer Behrends contributed the idea of using a polynomial-based
101 approach, using repeated multiplication by x in GF(2**n) where an irreducible
102 polynomial for each table size was chosen such that x was a primitive root.
103 Christian Tismer later extended that to use division by x instead, as an
104 efficient way to get the high bits of the hash code into play. This scheme
105 also gave excellent collision statistics, but was more expensive: two
106 if-tests were required inside the loop; computing "the next" index took about
107 the same number of operations but without as much potential parallelism
108 (e.g., computing 5*j can go on at the same time as computing 1+perturb in the
109 above, and then shifting perturb can be done while the table index is being
110 masked); and the dictobject struct required a member to hold the table's
111 polynomial. In Tim's experiments the current scheme ran faster, produced
112 equally good collision statistics, needed less code & used less memory.
114 Theoretical Python 2.5 headache: hash codes are only C "long", but
115 sizeof(Py_ssize_t) > sizeof(long) may be possible. In that case, and if a
116 dict is genuinely huge, then only the slots directly reachable via indexing
117 by a C long can be the first slot in a probe sequence. The probe sequence
118 will still eventually reach every slot in the table, but the collision rate
119 on initial probes may be much higher than this scheme was designed for.
120 Getting a hash code as fat as Py_ssize_t is the only real cure. But in
121 practice, this probably won't make a lick of difference for many years (at
122 which point everyone will have terabytes of RAM on 64-bit boxes).
125 /* Object used as dummy key to fill deleted entries */
126 static PyObject *dummy = NULL; /* Initialized by first call to newdictobject() */
128 #ifdef Py_REF_DEBUG
129 PyObject *
130 _PyDict_Dummy(void)
132 return dummy;
134 #endif
136 /* forward declarations */
137 static dictentry *
138 lookdict_string(dictobject *mp, PyObject *key, long hash);
140 #ifdef SHOW_CONVERSION_COUNTS
141 static long created = 0L;
142 static long converted = 0L;
144 static void
145 show_counts(void)
147 fprintf(stderr, "created %ld string dicts\n", created);
148 fprintf(stderr, "converted %ld to normal dicts\n", converted);
149 fprintf(stderr, "%.2f%% conversion rate\n", (100.0*converted)/created);
151 #endif
153 /* Initialization macros.
154 There are two ways to create a dict: PyDict_New() is the main C API
155 function, and the tp_new slot maps to dict_new(). In the latter case we
156 can save a little time over what PyDict_New does because it's guaranteed
157 that the PyDictObject struct is already zeroed out.
158 Everyone except dict_new() should use EMPTY_TO_MINSIZE (unless they have
159 an excellent reason not to).
162 #define INIT_NONZERO_DICT_SLOTS(mp) do { \
163 (mp)->ma_table = (mp)->ma_smalltable; \
164 (mp)->ma_mask = PyDict_MINSIZE - 1; \
165 } while(0)
167 #define EMPTY_TO_MINSIZE(mp) do { \
168 memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \
169 (mp)->ma_used = (mp)->ma_fill = 0; \
170 INIT_NONZERO_DICT_SLOTS(mp); \
171 } while(0)
173 /* Dictionary reuse scheme to save calls to malloc, free, and memset */
174 #define MAXFREEDICTS 80
175 static PyDictObject *free_dicts[MAXFREEDICTS];
176 static int num_free_dicts = 0;
178 PyObject *
179 PyDict_New(void)
181 register dictobject *mp;
182 if (dummy == NULL) { /* Auto-initialize dummy */
183 dummy = PyString_FromString("<dummy key>");
184 if (dummy == NULL)
185 return NULL;
186 #ifdef SHOW_CONVERSION_COUNTS
187 Py_AtExit(show_counts);
188 #endif
190 if (num_free_dicts) {
191 mp = free_dicts[--num_free_dicts];
192 assert (mp != NULL);
193 assert (mp->ob_type == &PyDict_Type);
194 _Py_NewReference((PyObject *)mp);
195 if (mp->ma_fill) {
196 EMPTY_TO_MINSIZE(mp);
198 assert (mp->ma_used == 0);
199 assert (mp->ma_table == mp->ma_smalltable);
200 assert (mp->ma_mask == PyDict_MINSIZE - 1);
201 } else {
202 mp = PyObject_GC_New(dictobject, &PyDict_Type);
203 if (mp == NULL)
204 return NULL;
205 EMPTY_TO_MINSIZE(mp);
207 mp->ma_lookup = lookdict_string;
208 #ifdef SHOW_CONVERSION_COUNTS
209 ++created;
210 #endif
211 _PyObject_GC_TRACK(mp);
212 return (PyObject *)mp;
216 The basic lookup function used by all operations.
217 This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
218 Open addressing is preferred over chaining since the link overhead for
219 chaining would be substantial (100% with typical malloc overhead).
221 The initial probe index is computed as hash mod the table size. Subsequent
222 probe indices are computed as explained earlier.
224 All arithmetic on hash should ignore overflow.
226 (The details in this version are due to Tim Peters, building on many past
227 contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
228 Christian Tismer).
230 This function must never return NULL; failures are indicated by returning
231 a dictentry* for which the me_value field is NULL. Exceptions are never
232 reported by this function, and outstanding exceptions are maintained.
235 static dictentry *
236 lookdict(dictobject *mp, PyObject *key, register long hash)
238 register Py_ssize_t i;
239 register size_t perturb;
240 register dictentry *freeslot;
241 register Py_ssize_t mask = mp->ma_mask;
242 dictentry *ep0 = mp->ma_table;
243 register dictentry *ep;
244 register int restore_error;
245 register int checked_error;
246 register int cmp;
247 PyObject *err_type, *err_value, *err_tb;
248 PyObject *startkey;
250 i = hash & mask;
251 ep = &ep0[i];
252 if (ep->me_key == NULL || ep->me_key == key)
253 return ep;
255 restore_error = checked_error = 0;
256 if (ep->me_key == dummy)
257 freeslot = ep;
258 else {
259 if (ep->me_hash == hash) {
260 /* error can't have been checked yet */
261 checked_error = 1;
262 if (PyErr_Occurred()) {
263 restore_error = 1;
264 PyErr_Fetch(&err_type, &err_value, &err_tb);
266 startkey = ep->me_key;
267 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
268 if (cmp < 0)
269 PyErr_Clear();
270 if (ep0 == mp->ma_table && ep->me_key == startkey) {
271 if (cmp > 0)
272 goto Done;
274 else {
275 /* The compare did major nasty stuff to the
276 * dict: start over.
277 * XXX A clever adversary could prevent this
278 * XXX from terminating.
280 ep = lookdict(mp, key, hash);
281 goto Done;
284 freeslot = NULL;
287 /* In the loop, me_key == dummy is by far (factor of 100s) the
288 least likely outcome, so test for that last. */
289 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
290 i = (i << 2) + i + perturb + 1;
291 ep = &ep0[i & mask];
292 if (ep->me_key == NULL) {
293 if (freeslot != NULL)
294 ep = freeslot;
295 break;
297 if (ep->me_key == key)
298 break;
299 if (ep->me_hash == hash && ep->me_key != dummy) {
300 if (!checked_error) {
301 checked_error = 1;
302 if (PyErr_Occurred()) {
303 restore_error = 1;
304 PyErr_Fetch(&err_type, &err_value,
305 &err_tb);
308 startkey = ep->me_key;
309 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
310 if (cmp < 0)
311 PyErr_Clear();
312 if (ep0 == mp->ma_table && ep->me_key == startkey) {
313 if (cmp > 0)
314 break;
316 else {
317 /* The compare did major nasty stuff to the
318 * dict: start over.
319 * XXX A clever adversary could prevent this
320 * XXX from terminating.
322 ep = lookdict(mp, key, hash);
323 break;
326 else if (ep->me_key == dummy && freeslot == NULL)
327 freeslot = ep;
330 Done:
331 if (restore_error)
332 PyErr_Restore(err_type, err_value, err_tb);
333 return ep;
337 * Hacked up version of lookdict which can assume keys are always strings;
338 * this assumption allows testing for errors during PyObject_Compare() to
339 * be dropped; string-string comparisons never raise exceptions. This also
340 * means we don't need to go through PyObject_Compare(); we can always use
341 * _PyString_Eq directly.
343 * This is valuable because the general-case error handling in lookdict() is
344 * expensive, and dicts with pure-string keys are very common.
346 static dictentry *
347 lookdict_string(dictobject *mp, PyObject *key, register long hash)
349 register Py_ssize_t i;
350 register size_t perturb;
351 register dictentry *freeslot;
352 register Py_ssize_t mask = mp->ma_mask;
353 dictentry *ep0 = mp->ma_table;
354 register dictentry *ep;
356 /* Make sure this function doesn't have to handle non-string keys,
357 including subclasses of str; e.g., one reason to subclass
358 strings is to override __eq__, and for speed we don't cater to
359 that here. */
360 if (!PyString_CheckExact(key)) {
361 #ifdef SHOW_CONVERSION_COUNTS
362 ++converted;
363 #endif
364 mp->ma_lookup = lookdict;
365 return lookdict(mp, key, hash);
367 i = hash & mask;
368 ep = &ep0[i];
369 if (ep->me_key == NULL || ep->me_key == key)
370 return ep;
371 if (ep->me_key == dummy)
372 freeslot = ep;
373 else {
374 if (ep->me_hash == hash
375 && _PyString_Eq(ep->me_key, key)) {
376 return ep;
378 freeslot = NULL;
381 /* In the loop, me_key == dummy is by far (factor of 100s) the
382 least likely outcome, so test for that last. */
383 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
384 i = (i << 2) + i + perturb + 1;
385 ep = &ep0[i & mask];
386 if (ep->me_key == NULL)
387 return freeslot == NULL ? ep : freeslot;
388 if (ep->me_key == key
389 || (ep->me_hash == hash
390 && ep->me_key != dummy
391 && _PyString_Eq(ep->me_key, key)))
392 return ep;
393 if (ep->me_key == dummy && freeslot == NULL)
394 freeslot = ep;
399 Internal routine to insert a new item into the table.
400 Used both by the internal resize routine and by the public insert routine.
401 Eats a reference to key and one to value.
403 static void
404 insertdict(register dictobject *mp, PyObject *key, long hash, PyObject *value)
406 PyObject *old_value;
407 register dictentry *ep;
408 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
410 assert(mp->ma_lookup != NULL);
411 ep = mp->ma_lookup(mp, key, hash);
412 if (ep->me_value != NULL) {
413 old_value = ep->me_value;
414 ep->me_value = value;
415 Py_DECREF(old_value); /* which **CAN** re-enter */
416 Py_DECREF(key);
418 else {
419 if (ep->me_key == NULL)
420 mp->ma_fill++;
421 else {
422 assert(ep->me_key == dummy);
423 Py_DECREF(dummy);
425 ep->me_key = key;
426 ep->me_hash = (Py_ssize_t)hash;
427 ep->me_value = value;
428 mp->ma_used++;
433 Restructure the table by allocating a new table and reinserting all
434 items again. When entries have been deleted, the new table may
435 actually be smaller than the old one.
437 static int
438 dictresize(dictobject *mp, Py_ssize_t minused)
440 Py_ssize_t newsize;
441 dictentry *oldtable, *newtable, *ep;
442 Py_ssize_t i;
443 int is_oldtable_malloced;
444 dictentry small_copy[PyDict_MINSIZE];
446 assert(minused >= 0);
448 /* Find the smallest table size > minused. */
449 for (newsize = PyDict_MINSIZE;
450 newsize <= minused && newsize > 0;
451 newsize <<= 1)
453 if (newsize <= 0) {
454 PyErr_NoMemory();
455 return -1;
458 /* Get space for a new table. */
459 oldtable = mp->ma_table;
460 assert(oldtable != NULL);
461 is_oldtable_malloced = oldtable != mp->ma_smalltable;
463 if (newsize == PyDict_MINSIZE) {
464 /* A large table is shrinking, or we can't get any smaller. */
465 newtable = mp->ma_smalltable;
466 if (newtable == oldtable) {
467 if (mp->ma_fill == mp->ma_used) {
468 /* No dummies, so no point doing anything. */
469 return 0;
471 /* We're not going to resize it, but rebuild the
472 table anyway to purge old dummy entries.
473 Subtle: This is *necessary* if fill==size,
474 as lookdict needs at least one virgin slot to
475 terminate failing searches. If fill < size, it's
476 merely desirable, as dummies slow searches. */
477 assert(mp->ma_fill > mp->ma_used);
478 memcpy(small_copy, oldtable, sizeof(small_copy));
479 oldtable = small_copy;
482 else {
483 newtable = PyMem_NEW(dictentry, newsize);
484 if (newtable == NULL) {
485 PyErr_NoMemory();
486 return -1;
490 /* Make the dict empty, using the new table. */
491 assert(newtable != oldtable);
492 mp->ma_table = newtable;
493 mp->ma_mask = newsize - 1;
494 memset(newtable, 0, sizeof(dictentry) * newsize);
495 mp->ma_used = 0;
496 i = mp->ma_fill;
497 mp->ma_fill = 0;
499 /* Copy the data over; this is refcount-neutral for active entries;
500 dummy entries aren't copied over, of course */
501 for (ep = oldtable; i > 0; ep++) {
502 if (ep->me_value != NULL) { /* active entry */
503 --i;
504 insertdict(mp, ep->me_key, ep->me_hash, ep->me_value);
506 else if (ep->me_key != NULL) { /* dummy entry */
507 --i;
508 assert(ep->me_key == dummy);
509 Py_DECREF(ep->me_key);
511 /* else key == value == NULL: nothing to do */
514 if (is_oldtable_malloced)
515 PyMem_DEL(oldtable);
516 return 0;
519 PyObject *
520 PyDict_GetItem(PyObject *op, PyObject *key)
522 long hash;
523 dictobject *mp = (dictobject *)op;
524 if (!PyDict_Check(op)) {
525 return NULL;
527 if (!PyString_CheckExact(key) ||
528 (hash = ((PyStringObject *) key)->ob_shash) == -1)
530 hash = PyObject_Hash(key);
531 if (hash == -1) {
532 PyErr_Clear();
533 return NULL;
536 return (mp->ma_lookup)(mp, key, hash)->me_value;
539 /* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
540 * dictionary if it's merely replacing the value for an existing key.
541 * This means that it's safe to loop over a dictionary with PyDict_Next()
542 * and occasionally replace a value -- but you can't insert new keys or
543 * remove them.
546 PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
548 register dictobject *mp;
549 register long hash;
550 register Py_ssize_t n_used;
552 if (!PyDict_Check(op)) {
553 PyErr_BadInternalCall();
554 return -1;
556 mp = (dictobject *)op;
557 if (PyString_CheckExact(key)) {
558 hash = ((PyStringObject *)key)->ob_shash;
559 if (hash == -1)
560 hash = PyObject_Hash(key);
562 else {
563 hash = PyObject_Hash(key);
564 if (hash == -1)
565 return -1;
567 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
568 n_used = mp->ma_used;
569 Py_INCREF(value);
570 Py_INCREF(key);
571 insertdict(mp, key, hash, value);
572 /* If we added a key, we can safely resize. Otherwise just return!
573 * If fill >= 2/3 size, adjust size. Normally, this doubles or
574 * quaduples the size, but it's also possible for the dict to shrink
575 * (if ma_fill is much larger than ma_used, meaning a lot of dict
576 * keys have been * deleted).
578 * Quadrupling the size improves average dictionary sparseness
579 * (reducing collisions) at the cost of some memory and iteration
580 * speed (which loops over every possible entry). It also halves
581 * the number of expensive resize operations in a growing dictionary.
583 * Very large dictionaries (over 50K items) use doubling instead.
584 * This may help applications with severe memory constraints.
586 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
587 return 0;
588 return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
592 PyDict_DelItem(PyObject *op, PyObject *key)
594 register dictobject *mp;
595 register long hash;
596 register dictentry *ep;
597 PyObject *old_value, *old_key;
599 if (!PyDict_Check(op)) {
600 PyErr_BadInternalCall();
601 return -1;
603 if (!PyString_CheckExact(key) ||
604 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
605 hash = PyObject_Hash(key);
606 if (hash == -1)
607 return -1;
609 mp = (dictobject *)op;
610 ep = (mp->ma_lookup)(mp, key, hash);
611 if (ep->me_value == NULL) {
612 PyErr_SetObject(PyExc_KeyError, key);
613 return -1;
615 old_key = ep->me_key;
616 Py_INCREF(dummy);
617 ep->me_key = dummy;
618 old_value = ep->me_value;
619 ep->me_value = NULL;
620 mp->ma_used--;
621 Py_DECREF(old_value);
622 Py_DECREF(old_key);
623 return 0;
626 void
627 PyDict_Clear(PyObject *op)
629 dictobject *mp;
630 dictentry *ep, *table;
631 int table_is_malloced;
632 Py_ssize_t fill;
633 dictentry small_copy[PyDict_MINSIZE];
634 #ifdef Py_DEBUG
635 Py_ssize_t i, n;
636 #endif
638 if (!PyDict_Check(op))
639 return;
640 mp = (dictobject *)op;
641 #ifdef Py_DEBUG
642 n = mp->ma_mask + 1;
643 i = 0;
644 #endif
646 table = mp->ma_table;
647 assert(table != NULL);
648 table_is_malloced = table != mp->ma_smalltable;
650 /* This is delicate. During the process of clearing the dict,
651 * decrefs can cause the dict to mutate. To avoid fatal confusion
652 * (voice of experience), we have to make the dict empty before
653 * clearing the slots, and never refer to anything via mp->xxx while
654 * clearing.
656 fill = mp->ma_fill;
657 if (table_is_malloced)
658 EMPTY_TO_MINSIZE(mp);
660 else if (fill > 0) {
661 /* It's a small table with something that needs to be cleared.
662 * Afraid the only safe way is to copy the dict entries into
663 * another small table first.
665 memcpy(small_copy, table, sizeof(small_copy));
666 table = small_copy;
667 EMPTY_TO_MINSIZE(mp);
669 /* else it's a small table that's already empty */
671 /* Now we can finally clear things. If C had refcounts, we could
672 * assert that the refcount on table is 1 now, i.e. that this function
673 * has unique access to it, so decref side-effects can't alter it.
675 for (ep = table; fill > 0; ++ep) {
676 #ifdef Py_DEBUG
677 assert(i < n);
678 ++i;
679 #endif
680 if (ep->me_key) {
681 --fill;
682 Py_DECREF(ep->me_key);
683 Py_XDECREF(ep->me_value);
685 #ifdef Py_DEBUG
686 else
687 assert(ep->me_value == NULL);
688 #endif
691 if (table_is_malloced)
692 PyMem_DEL(table);
696 * Iterate over a dict. Use like so:
698 * Py_ssize_t i;
699 * PyObject *key, *value;
700 * i = 0; # important! i should not otherwise be changed by you
701 * while (PyDict_Next(yourdict, &i, &key, &value)) {
702 * Refer to borrowed references in key and value.
705 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
706 * mutates the dict. One exception: it is safe if the loop merely changes
707 * the values associated with the keys (but doesn't insert new keys or
708 * delete keys), via PyDict_SetItem().
711 PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
713 register Py_ssize_t i;
714 register Py_ssize_t mask;
715 register dictentry *ep;
717 if (!PyDict_Check(op))
718 return 0;
719 i = *ppos;
720 if (i < 0)
721 return 0;
722 ep = ((dictobject *)op)->ma_table;
723 mask = ((dictobject *)op)->ma_mask;
724 while (i <= mask && ep[i].me_value == NULL)
725 i++;
726 *ppos = i+1;
727 if (i > mask)
728 return 0;
729 if (pkey)
730 *pkey = ep[i].me_key;
731 if (pvalue)
732 *pvalue = ep[i].me_value;
733 return 1;
736 /* Methods */
738 static void
739 dict_dealloc(register dictobject *mp)
741 register dictentry *ep;
742 Py_ssize_t fill = mp->ma_fill;
743 PyObject_GC_UnTrack(mp);
744 Py_TRASHCAN_SAFE_BEGIN(mp)
745 for (ep = mp->ma_table; fill > 0; ep++) {
746 if (ep->me_key) {
747 --fill;
748 Py_DECREF(ep->me_key);
749 Py_XDECREF(ep->me_value);
752 if (mp->ma_table != mp->ma_smalltable)
753 PyMem_DEL(mp->ma_table);
754 if (num_free_dicts < MAXFREEDICTS && mp->ob_type == &PyDict_Type)
755 free_dicts[num_free_dicts++] = mp;
756 else
757 mp->ob_type->tp_free((PyObject *)mp);
758 Py_TRASHCAN_SAFE_END(mp)
761 static int
762 dict_print(register dictobject *mp, register FILE *fp, register int flags)
764 register Py_ssize_t i;
765 register Py_ssize_t any;
766 int status;
768 status = Py_ReprEnter((PyObject*)mp);
769 if (status != 0) {
770 if (status < 0)
771 return status;
772 fprintf(fp, "{...}");
773 return 0;
776 fprintf(fp, "{");
777 any = 0;
778 for (i = 0; i <= mp->ma_mask; i++) {
779 dictentry *ep = mp->ma_table + i;
780 PyObject *pvalue = ep->me_value;
781 if (pvalue != NULL) {
782 /* Prevent PyObject_Repr from deleting value during
783 key format */
784 Py_INCREF(pvalue);
785 if (any++ > 0)
786 fprintf(fp, ", ");
787 if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) {
788 Py_DECREF(pvalue);
789 Py_ReprLeave((PyObject*)mp);
790 return -1;
792 fprintf(fp, ": ");
793 if (PyObject_Print(pvalue, fp, 0) != 0) {
794 Py_DECREF(pvalue);
795 Py_ReprLeave((PyObject*)mp);
796 return -1;
798 Py_DECREF(pvalue);
801 fprintf(fp, "}");
802 Py_ReprLeave((PyObject*)mp);
803 return 0;
806 static PyObject *
807 dict_repr(dictobject *mp)
809 Py_ssize_t i;
810 PyObject *s, *temp, *colon = NULL;
811 PyObject *pieces = NULL, *result = NULL;
812 PyObject *key, *value;
814 i = Py_ReprEnter((PyObject *)mp);
815 if (i != 0) {
816 return i > 0 ? PyString_FromString("{...}") : NULL;
819 if (mp->ma_used == 0) {
820 result = PyString_FromString("{}");
821 goto Done;
824 pieces = PyList_New(0);
825 if (pieces == NULL)
826 goto Done;
828 colon = PyString_FromString(": ");
829 if (colon == NULL)
830 goto Done;
832 /* Do repr() on each key+value pair, and insert ": " between them.
833 Note that repr may mutate the dict. */
834 i = 0;
835 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
836 int status;
837 /* Prevent repr from deleting value during key format. */
838 Py_INCREF(value);
839 s = PyObject_Repr(key);
840 PyString_Concat(&s, colon);
841 PyString_ConcatAndDel(&s, PyObject_Repr(value));
842 Py_DECREF(value);
843 if (s == NULL)
844 goto Done;
845 status = PyList_Append(pieces, s);
846 Py_DECREF(s); /* append created a new ref */
847 if (status < 0)
848 goto Done;
851 /* Add "{}" decorations to the first and last items. */
852 assert(PyList_GET_SIZE(pieces) > 0);
853 s = PyString_FromString("{");
854 if (s == NULL)
855 goto Done;
856 temp = PyList_GET_ITEM(pieces, 0);
857 PyString_ConcatAndDel(&s, temp);
858 PyList_SET_ITEM(pieces, 0, s);
859 if (s == NULL)
860 goto Done;
862 s = PyString_FromString("}");
863 if (s == NULL)
864 goto Done;
865 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
866 PyString_ConcatAndDel(&temp, s);
867 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
868 if (temp == NULL)
869 goto Done;
871 /* Paste them all together with ", " between. */
872 s = PyString_FromString(", ");
873 if (s == NULL)
874 goto Done;
875 result = _PyString_Join(s, pieces);
876 Py_DECREF(s);
878 Done:
879 Py_XDECREF(pieces);
880 Py_XDECREF(colon);
881 Py_ReprLeave((PyObject *)mp);
882 return result;
885 static Py_ssize_t
886 dict_length(dictobject *mp)
888 return mp->ma_used;
891 static PyObject *
892 dict_subscript(dictobject *mp, register PyObject *key)
894 PyObject *v;
895 long hash;
896 assert(mp->ma_table != NULL);
897 if (!PyString_CheckExact(key) ||
898 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
899 hash = PyObject_Hash(key);
900 if (hash == -1)
901 return NULL;
903 v = (mp->ma_lookup)(mp, key, hash) -> me_value;
904 if (v == NULL) {
905 if (!PyDict_CheckExact(mp)) {
906 /* Look up __missing__ method if we're a subclass. */
907 PyObject *missing;
908 static PyObject *missing_str = NULL;
909 if (missing_str == NULL)
910 missing_str =
911 PyString_InternFromString("__missing__");
912 missing = _PyType_Lookup(mp->ob_type, missing_str);
913 if (missing != NULL)
914 return PyObject_CallFunctionObjArgs(missing,
915 (PyObject *)mp, key, NULL);
917 PyErr_SetObject(PyExc_KeyError, key);
918 return NULL;
920 else
921 Py_INCREF(v);
922 return v;
925 static int
926 dict_ass_sub(dictobject *mp, PyObject *v, PyObject *w)
928 if (w == NULL)
929 return PyDict_DelItem((PyObject *)mp, v);
930 else
931 return PyDict_SetItem((PyObject *)mp, v, w);
934 static PyMappingMethods dict_as_mapping = {
935 (lenfunc)dict_length, /*mp_length*/
936 (binaryfunc)dict_subscript, /*mp_subscript*/
937 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
940 static PyObject *
941 dict_keys(register dictobject *mp)
943 register PyObject *v;
944 register Py_ssize_t i, j;
945 dictentry *ep;
946 Py_ssize_t mask, n;
948 again:
949 n = mp->ma_used;
950 v = PyList_New(n);
951 if (v == NULL)
952 return NULL;
953 if (n != mp->ma_used) {
954 /* Durnit. The allocations caused the dict to resize.
955 * Just start over, this shouldn't normally happen.
957 Py_DECREF(v);
958 goto again;
960 ep = mp->ma_table;
961 mask = mp->ma_mask;
962 for (i = 0, j = 0; i <= mask; i++) {
963 if (ep[i].me_value != NULL) {
964 PyObject *key = ep[i].me_key;
965 Py_INCREF(key);
966 PyList_SET_ITEM(v, j, key);
967 j++;
970 assert(j == n);
971 return v;
974 static PyObject *
975 dict_values(register dictobject *mp)
977 register PyObject *v;
978 register Py_ssize_t i, j;
979 dictentry *ep;
980 Py_ssize_t mask, n;
982 again:
983 n = mp->ma_used;
984 v = PyList_New(n);
985 if (v == NULL)
986 return NULL;
987 if (n != mp->ma_used) {
988 /* Durnit. The allocations caused the dict to resize.
989 * Just start over, this shouldn't normally happen.
991 Py_DECREF(v);
992 goto again;
994 ep = mp->ma_table;
995 mask = mp->ma_mask;
996 for (i = 0, j = 0; i <= mask; i++) {
997 if (ep[i].me_value != NULL) {
998 PyObject *value = ep[i].me_value;
999 Py_INCREF(value);
1000 PyList_SET_ITEM(v, j, value);
1001 j++;
1004 assert(j == n);
1005 return v;
1008 static PyObject *
1009 dict_items(register dictobject *mp)
1011 register PyObject *v;
1012 register Py_ssize_t i, j, n;
1013 Py_ssize_t mask;
1014 PyObject *item, *key, *value;
1015 dictentry *ep;
1017 /* Preallocate the list of tuples, to avoid allocations during
1018 * the loop over the items, which could trigger GC, which
1019 * could resize the dict. :-(
1021 again:
1022 n = mp->ma_used;
1023 v = PyList_New(n);
1024 if (v == NULL)
1025 return NULL;
1026 for (i = 0; i < n; i++) {
1027 item = PyTuple_New(2);
1028 if (item == NULL) {
1029 Py_DECREF(v);
1030 return NULL;
1032 PyList_SET_ITEM(v, i, item);
1034 if (n != mp->ma_used) {
1035 /* Durnit. The allocations caused the dict to resize.
1036 * Just start over, this shouldn't normally happen.
1038 Py_DECREF(v);
1039 goto again;
1041 /* Nothing we do below makes any function calls. */
1042 ep = mp->ma_table;
1043 mask = mp->ma_mask;
1044 for (i = 0, j = 0; i <= mask; i++) {
1045 if ((value=ep[i].me_value) != NULL) {
1046 key = ep[i].me_key;
1047 item = PyList_GET_ITEM(v, j);
1048 Py_INCREF(key);
1049 PyTuple_SET_ITEM(item, 0, key);
1050 Py_INCREF(value);
1051 PyTuple_SET_ITEM(item, 1, value);
1052 j++;
1055 assert(j == n);
1056 return v;
1059 static PyObject *
1060 dict_fromkeys(PyObject *cls, PyObject *args)
1062 PyObject *seq;
1063 PyObject *value = Py_None;
1064 PyObject *it; /* iter(seq) */
1065 PyObject *key;
1066 PyObject *d;
1067 int status;
1069 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
1070 return NULL;
1072 d = PyObject_CallObject(cls, NULL);
1073 if (d == NULL)
1074 return NULL;
1076 it = PyObject_GetIter(seq);
1077 if (it == NULL){
1078 Py_DECREF(d);
1079 return NULL;
1082 for (;;) {
1083 key = PyIter_Next(it);
1084 if (key == NULL) {
1085 if (PyErr_Occurred())
1086 goto Fail;
1087 break;
1089 status = PyObject_SetItem(d, key, value);
1090 Py_DECREF(key);
1091 if (status < 0)
1092 goto Fail;
1095 Py_DECREF(it);
1096 return d;
1098 Fail:
1099 Py_DECREF(it);
1100 Py_DECREF(d);
1101 return NULL;
1104 static int
1105 dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
1107 PyObject *arg = NULL;
1108 int result = 0;
1110 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1111 result = -1;
1113 else if (arg != NULL) {
1114 if (PyObject_HasAttrString(arg, "keys"))
1115 result = PyDict_Merge(self, arg, 1);
1116 else
1117 result = PyDict_MergeFromSeq2(self, arg, 1);
1119 if (result == 0 && kwds != NULL)
1120 result = PyDict_Merge(self, kwds, 1);
1121 return result;
1124 static PyObject *
1125 dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1127 if (dict_update_common(self, args, kwds, "update") != -1)
1128 Py_RETURN_NONE;
1129 return NULL;
1132 /* Update unconditionally replaces existing items.
1133 Merge has a 3rd argument 'override'; if set, it acts like Update,
1134 otherwise it leaves existing items unchanged.
1136 PyDict_{Update,Merge} update/merge from a mapping object.
1138 PyDict_MergeFromSeq2 updates/merges from any iterable object
1139 producing iterable objects of length 2.
1143 PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1145 PyObject *it; /* iter(seq2) */
1146 Py_ssize_t i; /* index into seq2 of current element */
1147 PyObject *item; /* seq2[i] */
1148 PyObject *fast; /* item as a 2-tuple or 2-list */
1150 assert(d != NULL);
1151 assert(PyDict_Check(d));
1152 assert(seq2 != NULL);
1154 it = PyObject_GetIter(seq2);
1155 if (it == NULL)
1156 return -1;
1158 for (i = 0; ; ++i) {
1159 PyObject *key, *value;
1160 Py_ssize_t n;
1162 fast = NULL;
1163 item = PyIter_Next(it);
1164 if (item == NULL) {
1165 if (PyErr_Occurred())
1166 goto Fail;
1167 break;
1170 /* Convert item to sequence, and verify length 2. */
1171 fast = PySequence_Fast(item, "");
1172 if (fast == NULL) {
1173 if (PyErr_ExceptionMatches(PyExc_TypeError))
1174 PyErr_Format(PyExc_TypeError,
1175 "cannot convert dictionary update "
1176 "sequence element #%zd to a sequence",
1178 goto Fail;
1180 n = PySequence_Fast_GET_SIZE(fast);
1181 if (n != 2) {
1182 PyErr_Format(PyExc_ValueError,
1183 "dictionary update sequence element #%zd "
1184 "has length %zd; 2 is required",
1185 i, n);
1186 goto Fail;
1189 /* Update/merge with this (key, value) pair. */
1190 key = PySequence_Fast_GET_ITEM(fast, 0);
1191 value = PySequence_Fast_GET_ITEM(fast, 1);
1192 if (override || PyDict_GetItem(d, key) == NULL) {
1193 int status = PyDict_SetItem(d, key, value);
1194 if (status < 0)
1195 goto Fail;
1197 Py_DECREF(fast);
1198 Py_DECREF(item);
1201 i = 0;
1202 goto Return;
1203 Fail:
1204 Py_XDECREF(item);
1205 Py_XDECREF(fast);
1206 i = -1;
1207 Return:
1208 Py_DECREF(it);
1209 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
1213 PyDict_Update(PyObject *a, PyObject *b)
1215 return PyDict_Merge(a, b, 1);
1219 PyDict_Merge(PyObject *a, PyObject *b, int override)
1221 register PyDictObject *mp, *other;
1222 register Py_ssize_t i;
1223 dictentry *entry;
1225 /* We accept for the argument either a concrete dictionary object,
1226 * or an abstract "mapping" object. For the former, we can do
1227 * things quite efficiently. For the latter, we only require that
1228 * PyMapping_Keys() and PyObject_GetItem() be supported.
1230 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1231 PyErr_BadInternalCall();
1232 return -1;
1234 mp = (dictobject*)a;
1235 if (PyDict_Check(b)) {
1236 other = (dictobject*)b;
1237 if (other == mp || other->ma_used == 0)
1238 /* a.update(a) or a.update({}); nothing to do */
1239 return 0;
1240 if (mp->ma_used == 0)
1241 /* Since the target dict is empty, PyDict_GetItem()
1242 * always returns NULL. Setting override to 1
1243 * skips the unnecessary test.
1245 override = 1;
1246 /* Do one big resize at the start, rather than
1247 * incrementally resizing as we insert new items. Expect
1248 * that there will be no (or few) overlapping keys.
1250 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
1251 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
1252 return -1;
1254 for (i = 0; i <= other->ma_mask; i++) {
1255 entry = &other->ma_table[i];
1256 if (entry->me_value != NULL &&
1257 (override ||
1258 PyDict_GetItem(a, entry->me_key) == NULL)) {
1259 Py_INCREF(entry->me_key);
1260 Py_INCREF(entry->me_value);
1261 insertdict(mp, entry->me_key,
1262 (long)entry->me_hash,
1263 entry->me_value);
1267 else {
1268 /* Do it the generic, slower way */
1269 PyObject *keys = PyMapping_Keys(b);
1270 PyObject *iter;
1271 PyObject *key, *value;
1272 int status;
1274 if (keys == NULL)
1275 /* Docstring says this is equivalent to E.keys() so
1276 * if E doesn't have a .keys() method we want
1277 * AttributeError to percolate up. Might as well
1278 * do the same for any other error.
1280 return -1;
1282 iter = PyObject_GetIter(keys);
1283 Py_DECREF(keys);
1284 if (iter == NULL)
1285 return -1;
1287 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
1288 if (!override && PyDict_GetItem(a, key) != NULL) {
1289 Py_DECREF(key);
1290 continue;
1292 value = PyObject_GetItem(b, key);
1293 if (value == NULL) {
1294 Py_DECREF(iter);
1295 Py_DECREF(key);
1296 return -1;
1298 status = PyDict_SetItem(a, key, value);
1299 Py_DECREF(key);
1300 Py_DECREF(value);
1301 if (status < 0) {
1302 Py_DECREF(iter);
1303 return -1;
1306 Py_DECREF(iter);
1307 if (PyErr_Occurred())
1308 /* Iterator completed, via error */
1309 return -1;
1311 return 0;
1314 static PyObject *
1315 dict_copy(register dictobject *mp)
1317 return PyDict_Copy((PyObject*)mp);
1320 PyObject *
1321 PyDict_Copy(PyObject *o)
1323 PyObject *copy;
1325 if (o == NULL || !PyDict_Check(o)) {
1326 PyErr_BadInternalCall();
1327 return NULL;
1329 copy = PyDict_New();
1330 if (copy == NULL)
1331 return NULL;
1332 if (PyDict_Merge(copy, o, 1) == 0)
1333 return copy;
1334 Py_DECREF(copy);
1335 return NULL;
1338 Py_ssize_t
1339 PyDict_Size(PyObject *mp)
1341 if (mp == NULL || !PyDict_Check(mp)) {
1342 PyErr_BadInternalCall();
1343 return -1;
1345 return ((dictobject *)mp)->ma_used;
1348 PyObject *
1349 PyDict_Keys(PyObject *mp)
1351 if (mp == NULL || !PyDict_Check(mp)) {
1352 PyErr_BadInternalCall();
1353 return NULL;
1355 return dict_keys((dictobject *)mp);
1358 PyObject *
1359 PyDict_Values(PyObject *mp)
1361 if (mp == NULL || !PyDict_Check(mp)) {
1362 PyErr_BadInternalCall();
1363 return NULL;
1365 return dict_values((dictobject *)mp);
1368 PyObject *
1369 PyDict_Items(PyObject *mp)
1371 if (mp == NULL || !PyDict_Check(mp)) {
1372 PyErr_BadInternalCall();
1373 return NULL;
1375 return dict_items((dictobject *)mp);
1378 /* Subroutine which returns the smallest key in a for which b's value
1379 is different or absent. The value is returned too, through the
1380 pval argument. Both are NULL if no key in a is found for which b's status
1381 differs. The refcounts on (and only on) non-NULL *pval and function return
1382 values must be decremented by the caller (characterize() increments them
1383 to ensure that mutating comparison and PyDict_GetItem calls can't delete
1384 them before the caller is done looking at them). */
1386 static PyObject *
1387 characterize(dictobject *a, dictobject *b, PyObject **pval)
1389 PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */
1390 PyObject *aval = NULL; /* a[akey] */
1391 Py_ssize_t i;
1392 int cmp;
1394 for (i = 0; i <= a->ma_mask; i++) {
1395 PyObject *thiskey, *thisaval, *thisbval;
1396 if (a->ma_table[i].me_value == NULL)
1397 continue;
1398 thiskey = a->ma_table[i].me_key;
1399 Py_INCREF(thiskey); /* keep alive across compares */
1400 if (akey != NULL) {
1401 cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT);
1402 if (cmp < 0) {
1403 Py_DECREF(thiskey);
1404 goto Fail;
1406 if (cmp > 0 ||
1407 i > a->ma_mask ||
1408 a->ma_table[i].me_value == NULL)
1410 /* Not the *smallest* a key; or maybe it is
1411 * but the compare shrunk the dict so we can't
1412 * find its associated value anymore; or
1413 * maybe it is but the compare deleted the
1414 * a[thiskey] entry.
1416 Py_DECREF(thiskey);
1417 continue;
1421 /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */
1422 thisaval = a->ma_table[i].me_value;
1423 assert(thisaval);
1424 Py_INCREF(thisaval); /* keep alive */
1425 thisbval = PyDict_GetItem((PyObject *)b, thiskey);
1426 if (thisbval == NULL)
1427 cmp = 0;
1428 else {
1429 /* both dicts have thiskey: same values? */
1430 cmp = PyObject_RichCompareBool(
1431 thisaval, thisbval, Py_EQ);
1432 if (cmp < 0) {
1433 Py_DECREF(thiskey);
1434 Py_DECREF(thisaval);
1435 goto Fail;
1438 if (cmp == 0) {
1439 /* New winner. */
1440 Py_XDECREF(akey);
1441 Py_XDECREF(aval);
1442 akey = thiskey;
1443 aval = thisaval;
1445 else {
1446 Py_DECREF(thiskey);
1447 Py_DECREF(thisaval);
1450 *pval = aval;
1451 return akey;
1453 Fail:
1454 Py_XDECREF(akey);
1455 Py_XDECREF(aval);
1456 *pval = NULL;
1457 return NULL;
1460 static int
1461 dict_compare(dictobject *a, dictobject *b)
1463 PyObject *adiff, *bdiff, *aval, *bval;
1464 int res;
1466 /* Compare lengths first */
1467 if (a->ma_used < b->ma_used)
1468 return -1; /* a is shorter */
1469 else if (a->ma_used > b->ma_used)
1470 return 1; /* b is shorter */
1472 /* Same length -- check all keys */
1473 bdiff = bval = NULL;
1474 adiff = characterize(a, b, &aval);
1475 if (adiff == NULL) {
1476 assert(!aval);
1477 /* Either an error, or a is a subset with the same length so
1478 * must be equal.
1480 res = PyErr_Occurred() ? -1 : 0;
1481 goto Finished;
1483 bdiff = characterize(b, a, &bval);
1484 if (bdiff == NULL && PyErr_Occurred()) {
1485 assert(!bval);
1486 res = -1;
1487 goto Finished;
1489 res = 0;
1490 if (bdiff) {
1491 /* bdiff == NULL "should be" impossible now, but perhaps
1492 * the last comparison done by the characterize() on a had
1493 * the side effect of making the dicts equal!
1495 res = PyObject_Compare(adiff, bdiff);
1497 if (res == 0 && bval != NULL)
1498 res = PyObject_Compare(aval, bval);
1500 Finished:
1501 Py_XDECREF(adiff);
1502 Py_XDECREF(bdiff);
1503 Py_XDECREF(aval);
1504 Py_XDECREF(bval);
1505 return res;
1508 /* Return 1 if dicts equal, 0 if not, -1 if error.
1509 * Gets out as soon as any difference is detected.
1510 * Uses only Py_EQ comparison.
1512 static int
1513 dict_equal(dictobject *a, dictobject *b)
1515 Py_ssize_t i;
1517 if (a->ma_used != b->ma_used)
1518 /* can't be equal if # of entries differ */
1519 return 0;
1521 /* Same # of entries -- check all of 'em. Exit early on any diff. */
1522 for (i = 0; i <= a->ma_mask; i++) {
1523 PyObject *aval = a->ma_table[i].me_value;
1524 if (aval != NULL) {
1525 int cmp;
1526 PyObject *bval;
1527 PyObject *key = a->ma_table[i].me_key;
1528 /* temporarily bump aval's refcount to ensure it stays
1529 alive until we're done with it */
1530 Py_INCREF(aval);
1531 bval = PyDict_GetItem((PyObject *)b, key);
1532 if (bval == NULL) {
1533 Py_DECREF(aval);
1534 return 0;
1536 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1537 Py_DECREF(aval);
1538 if (cmp <= 0) /* error or not equal */
1539 return cmp;
1542 return 1;
1545 static PyObject *
1546 dict_richcompare(PyObject *v, PyObject *w, int op)
1548 int cmp;
1549 PyObject *res;
1551 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1552 res = Py_NotImplemented;
1554 else if (op == Py_EQ || op == Py_NE) {
1555 cmp = dict_equal((dictobject *)v, (dictobject *)w);
1556 if (cmp < 0)
1557 return NULL;
1558 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1560 else
1561 res = Py_NotImplemented;
1562 Py_INCREF(res);
1563 return res;
1566 static PyObject *
1567 dict_has_key(register dictobject *mp, PyObject *key)
1569 long hash;
1570 register long ok;
1571 if (!PyString_CheckExact(key) ||
1572 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1573 hash = PyObject_Hash(key);
1574 if (hash == -1)
1575 return NULL;
1577 ok = (mp->ma_lookup)(mp, key, hash)->me_value != NULL;
1578 return PyBool_FromLong(ok);
1581 static PyObject *
1582 dict_get(register dictobject *mp, PyObject *args)
1584 PyObject *key;
1585 PyObject *failobj = Py_None;
1586 PyObject *val = NULL;
1587 long hash;
1589 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
1590 return NULL;
1592 if (!PyString_CheckExact(key) ||
1593 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1594 hash = PyObject_Hash(key);
1595 if (hash == -1)
1596 return NULL;
1598 val = (mp->ma_lookup)(mp, key, hash)->me_value;
1600 if (val == NULL)
1601 val = failobj;
1602 Py_INCREF(val);
1603 return val;
1607 static PyObject *
1608 dict_setdefault(register dictobject *mp, PyObject *args)
1610 PyObject *key;
1611 PyObject *failobj = Py_None;
1612 PyObject *val = NULL;
1613 long hash;
1615 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
1616 return NULL;
1618 if (!PyString_CheckExact(key) ||
1619 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1620 hash = PyObject_Hash(key);
1621 if (hash == -1)
1622 return NULL;
1624 val = (mp->ma_lookup)(mp, key, hash)->me_value;
1625 if (val == NULL) {
1626 val = failobj;
1627 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1628 val = NULL;
1630 Py_XINCREF(val);
1631 return val;
1635 static PyObject *
1636 dict_clear(register dictobject *mp)
1638 PyDict_Clear((PyObject *)mp);
1639 Py_RETURN_NONE;
1642 static PyObject *
1643 dict_pop(dictobject *mp, PyObject *args)
1645 long hash;
1646 dictentry *ep;
1647 PyObject *old_value, *old_key;
1648 PyObject *key, *deflt = NULL;
1650 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1651 return NULL;
1652 if (mp->ma_used == 0) {
1653 if (deflt) {
1654 Py_INCREF(deflt);
1655 return deflt;
1657 PyErr_SetString(PyExc_KeyError,
1658 "pop(): dictionary is empty");
1659 return NULL;
1661 if (!PyString_CheckExact(key) ||
1662 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1663 hash = PyObject_Hash(key);
1664 if (hash == -1)
1665 return NULL;
1667 ep = (mp->ma_lookup)(mp, key, hash);
1668 if (ep->me_value == NULL) {
1669 if (deflt) {
1670 Py_INCREF(deflt);
1671 return deflt;
1673 PyErr_SetObject(PyExc_KeyError, key);
1674 return NULL;
1676 old_key = ep->me_key;
1677 Py_INCREF(dummy);
1678 ep->me_key = dummy;
1679 old_value = ep->me_value;
1680 ep->me_value = NULL;
1681 mp->ma_used--;
1682 Py_DECREF(old_key);
1683 return old_value;
1686 static PyObject *
1687 dict_popitem(dictobject *mp)
1689 Py_ssize_t i = 0;
1690 dictentry *ep;
1691 PyObject *res;
1693 /* Allocate the result tuple before checking the size. Believe it
1694 * or not, this allocation could trigger a garbage collection which
1695 * could empty the dict, so if we checked the size first and that
1696 * happened, the result would be an infinite loop (searching for an
1697 * entry that no longer exists). Note that the usual popitem()
1698 * idiom is "while d: k, v = d.popitem()". so needing to throw the
1699 * tuple away if the dict *is* empty isn't a significant
1700 * inefficiency -- possible, but unlikely in practice.
1702 res = PyTuple_New(2);
1703 if (res == NULL)
1704 return NULL;
1705 if (mp->ma_used == 0) {
1706 Py_DECREF(res);
1707 PyErr_SetString(PyExc_KeyError,
1708 "popitem(): dictionary is empty");
1709 return NULL;
1711 /* Set ep to "the first" dict entry with a value. We abuse the hash
1712 * field of slot 0 to hold a search finger:
1713 * If slot 0 has a value, use slot 0.
1714 * Else slot 0 is being used to hold a search finger,
1715 * and we use its hash value as the first index to look.
1717 ep = &mp->ma_table[0];
1718 if (ep->me_value == NULL) {
1719 i = ep->me_hash;
1720 /* The hash field may be a real hash value, or it may be a
1721 * legit search finger, or it may be a once-legit search
1722 * finger that's out of bounds now because it wrapped around
1723 * or the table shrunk -- simply make sure it's in bounds now.
1725 if (i > mp->ma_mask || i < 1)
1726 i = 1; /* skip slot 0 */
1727 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1728 i++;
1729 if (i > mp->ma_mask)
1730 i = 1;
1733 PyTuple_SET_ITEM(res, 0, ep->me_key);
1734 PyTuple_SET_ITEM(res, 1, ep->me_value);
1735 Py_INCREF(dummy);
1736 ep->me_key = dummy;
1737 ep->me_value = NULL;
1738 mp->ma_used--;
1739 assert(mp->ma_table[0].me_value == NULL);
1740 mp->ma_table[0].me_hash = i + 1; /* next place to start */
1741 return res;
1744 static int
1745 dict_traverse(PyObject *op, visitproc visit, void *arg)
1747 Py_ssize_t i = 0;
1748 PyObject *pk;
1749 PyObject *pv;
1751 while (PyDict_Next(op, &i, &pk, &pv)) {
1752 Py_VISIT(pk);
1753 Py_VISIT(pv);
1755 return 0;
1758 static int
1759 dict_tp_clear(PyObject *op)
1761 PyDict_Clear(op);
1762 return 0;
1766 extern PyTypeObject PyDictIterKey_Type; /* Forward */
1767 extern PyTypeObject PyDictIterValue_Type; /* Forward */
1768 extern PyTypeObject PyDictIterItem_Type; /* Forward */
1769 static PyObject *dictiter_new(dictobject *, PyTypeObject *);
1771 static PyObject *
1772 dict_iterkeys(dictobject *dict)
1774 return dictiter_new(dict, &PyDictIterKey_Type);
1777 static PyObject *
1778 dict_itervalues(dictobject *dict)
1780 return dictiter_new(dict, &PyDictIterValue_Type);
1783 static PyObject *
1784 dict_iteritems(dictobject *dict)
1786 return dictiter_new(dict, &PyDictIterItem_Type);
1790 PyDoc_STRVAR(has_key__doc__,
1791 "D.has_key(k) -> True if D has a key k, else False");
1793 PyDoc_STRVAR(contains__doc__,
1794 "D.__contains__(k) -> True if D has a key k, else False");
1796 PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
1798 PyDoc_STRVAR(get__doc__,
1799 "D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
1801 PyDoc_STRVAR(setdefault_doc__,
1802 "D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D");
1804 PyDoc_STRVAR(pop__doc__,
1805 "D.pop(k[,d]) -> v, remove specified key and return the corresponding value\n\
1806 If key is not found, d is returned if given, otherwise KeyError is raised");
1808 PyDoc_STRVAR(popitem__doc__,
1809 "D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
1810 2-tuple; but raise KeyError if D is empty");
1812 PyDoc_STRVAR(keys__doc__,
1813 "D.keys() -> list of D's keys");
1815 PyDoc_STRVAR(items__doc__,
1816 "D.items() -> list of D's (key, value) pairs, as 2-tuples");
1818 PyDoc_STRVAR(values__doc__,
1819 "D.values() -> list of D's values");
1821 PyDoc_STRVAR(update__doc__,
1822 "D.update(E, **F) -> None. Update D from E and F: for k in E: D[k] = E[k]\n\
1823 (if E has keys else: for (k, v) in E: D[k] = v) then: for k in F: D[k] = F[k]");
1825 PyDoc_STRVAR(fromkeys__doc__,
1826 "dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
1827 v defaults to None.");
1829 PyDoc_STRVAR(clear__doc__,
1830 "D.clear() -> None. Remove all items from D.");
1832 PyDoc_STRVAR(copy__doc__,
1833 "D.copy() -> a shallow copy of D");
1835 PyDoc_STRVAR(iterkeys__doc__,
1836 "D.iterkeys() -> an iterator over the keys of D");
1838 PyDoc_STRVAR(itervalues__doc__,
1839 "D.itervalues() -> an iterator over the values of D");
1841 PyDoc_STRVAR(iteritems__doc__,
1842 "D.iteritems() -> an iterator over the (key, value) items of D");
1844 static PyMethodDef mapp_methods[] = {
1845 {"__contains__",(PyCFunction)dict_has_key, METH_O | METH_COEXIST,
1846 contains__doc__},
1847 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
1848 getitem__doc__},
1849 {"has_key", (PyCFunction)dict_has_key, METH_O,
1850 has_key__doc__},
1851 {"get", (PyCFunction)dict_get, METH_VARARGS,
1852 get__doc__},
1853 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
1854 setdefault_doc__},
1855 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
1856 pop__doc__},
1857 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
1858 popitem__doc__},
1859 {"keys", (PyCFunction)dict_keys, METH_NOARGS,
1860 keys__doc__},
1861 {"items", (PyCFunction)dict_items, METH_NOARGS,
1862 items__doc__},
1863 {"values", (PyCFunction)dict_values, METH_NOARGS,
1864 values__doc__},
1865 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
1866 update__doc__},
1867 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
1868 fromkeys__doc__},
1869 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
1870 clear__doc__},
1871 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
1872 copy__doc__},
1873 {"iterkeys", (PyCFunction)dict_iterkeys, METH_NOARGS,
1874 iterkeys__doc__},
1875 {"itervalues", (PyCFunction)dict_itervalues, METH_NOARGS,
1876 itervalues__doc__},
1877 {"iteritems", (PyCFunction)dict_iteritems, METH_NOARGS,
1878 iteritems__doc__},
1879 {NULL, NULL} /* sentinel */
1883 PyDict_Contains(PyObject *op, PyObject *key)
1885 long hash;
1886 dictobject *mp = (dictobject *)op;
1888 if (!PyString_CheckExact(key) ||
1889 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1890 hash = PyObject_Hash(key);
1891 if (hash == -1)
1892 return -1;
1894 return (mp->ma_lookup)(mp, key, hash)->me_value != NULL;
1897 /* Hack to implement "key in dict" */
1898 static PySequenceMethods dict_as_sequence = {
1899 0, /* sq_length */
1900 0, /* sq_concat */
1901 0, /* sq_repeat */
1902 0, /* sq_item */
1903 0, /* sq_slice */
1904 0, /* sq_ass_item */
1905 0, /* sq_ass_slice */
1906 PyDict_Contains, /* sq_contains */
1907 0, /* sq_inplace_concat */
1908 0, /* sq_inplace_repeat */
1911 static PyObject *
1912 dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1914 PyObject *self;
1916 assert(type != NULL && type->tp_alloc != NULL);
1917 self = type->tp_alloc(type, 0);
1918 if (self != NULL) {
1919 PyDictObject *d = (PyDictObject *)self;
1920 /* It's guaranteed that tp->alloc zeroed out the struct. */
1921 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
1922 INIT_NONZERO_DICT_SLOTS(d);
1923 d->ma_lookup = lookdict_string;
1924 #ifdef SHOW_CONVERSION_COUNTS
1925 ++created;
1926 #endif
1928 return self;
1931 static int
1932 dict_init(PyObject *self, PyObject *args, PyObject *kwds)
1934 return dict_update_common(self, args, kwds, "dict");
1937 static long
1938 dict_nohash(PyObject *self)
1940 PyErr_SetString(PyExc_TypeError, "dict objects are unhashable");
1941 return -1;
1944 static PyObject *
1945 dict_iter(dictobject *dict)
1947 return dictiter_new(dict, &PyDictIterKey_Type);
1950 PyDoc_STRVAR(dictionary_doc,
1951 "dict() -> new empty dictionary.\n"
1952 "dict(mapping) -> new dictionary initialized from a mapping object's\n"
1953 " (key, value) pairs.\n"
1954 "dict(seq) -> new dictionary initialized as if via:\n"
1955 " d = {}\n"
1956 " for k, v in seq:\n"
1957 " d[k] = v\n"
1958 "dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
1959 " in the keyword argument list. For example: dict(one=1, two=2)");
1961 PyTypeObject PyDict_Type = {
1962 PyObject_HEAD_INIT(&PyType_Type)
1964 "dict",
1965 sizeof(dictobject),
1967 (destructor)dict_dealloc, /* tp_dealloc */
1968 (printfunc)dict_print, /* tp_print */
1969 0, /* tp_getattr */
1970 0, /* tp_setattr */
1971 (cmpfunc)dict_compare, /* tp_compare */
1972 (reprfunc)dict_repr, /* tp_repr */
1973 0, /* tp_as_number */
1974 &dict_as_sequence, /* tp_as_sequence */
1975 &dict_as_mapping, /* tp_as_mapping */
1976 dict_nohash, /* tp_hash */
1977 0, /* tp_call */
1978 0, /* tp_str */
1979 PyObject_GenericGetAttr, /* tp_getattro */
1980 0, /* tp_setattro */
1981 0, /* tp_as_buffer */
1982 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1983 Py_TPFLAGS_BASETYPE, /* tp_flags */
1984 dictionary_doc, /* tp_doc */
1985 dict_traverse, /* tp_traverse */
1986 dict_tp_clear, /* tp_clear */
1987 dict_richcompare, /* tp_richcompare */
1988 0, /* tp_weaklistoffset */
1989 (getiterfunc)dict_iter, /* tp_iter */
1990 0, /* tp_iternext */
1991 mapp_methods, /* tp_methods */
1992 0, /* tp_members */
1993 0, /* tp_getset */
1994 0, /* tp_base */
1995 0, /* tp_dict */
1996 0, /* tp_descr_get */
1997 0, /* tp_descr_set */
1998 0, /* tp_dictoffset */
1999 dict_init, /* tp_init */
2000 PyType_GenericAlloc, /* tp_alloc */
2001 dict_new, /* tp_new */
2002 PyObject_GC_Del, /* tp_free */
2005 /* For backward compatibility with old dictionary interface */
2007 PyObject *
2008 PyDict_GetItemString(PyObject *v, const char *key)
2010 PyObject *kv, *rv;
2011 kv = PyString_FromString(key);
2012 if (kv == NULL)
2013 return NULL;
2014 rv = PyDict_GetItem(v, kv);
2015 Py_DECREF(kv);
2016 return rv;
2020 PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
2022 PyObject *kv;
2023 int err;
2024 kv = PyString_FromString(key);
2025 if (kv == NULL)
2026 return -1;
2027 PyString_InternInPlace(&kv); /* XXX Should we really? */
2028 err = PyDict_SetItem(v, kv, item);
2029 Py_DECREF(kv);
2030 return err;
2034 PyDict_DelItemString(PyObject *v, const char *key)
2036 PyObject *kv;
2037 int err;
2038 kv = PyString_FromString(key);
2039 if (kv == NULL)
2040 return -1;
2041 err = PyDict_DelItem(v, kv);
2042 Py_DECREF(kv);
2043 return err;
2046 /* Dictionary iterator types */
2048 typedef struct {
2049 PyObject_HEAD
2050 dictobject *di_dict; /* Set to NULL when iterator is exhausted */
2051 Py_ssize_t di_used;
2052 Py_ssize_t di_pos;
2053 PyObject* di_result; /* reusable result tuple for iteritems */
2054 Py_ssize_t len;
2055 } dictiterobject;
2057 static PyObject *
2058 dictiter_new(dictobject *dict, PyTypeObject *itertype)
2060 dictiterobject *di;
2061 di = PyObject_New(dictiterobject, itertype);
2062 if (di == NULL)
2063 return NULL;
2064 Py_INCREF(dict);
2065 di->di_dict = dict;
2066 di->di_used = dict->ma_used;
2067 di->di_pos = 0;
2068 di->len = dict->ma_used;
2069 if (itertype == &PyDictIterItem_Type) {
2070 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2071 if (di->di_result == NULL) {
2072 Py_DECREF(di);
2073 return NULL;
2076 else
2077 di->di_result = NULL;
2078 return (PyObject *)di;
2081 static void
2082 dictiter_dealloc(dictiterobject *di)
2084 Py_XDECREF(di->di_dict);
2085 Py_XDECREF(di->di_result);
2086 PyObject_Del(di);
2089 static PyObject *
2090 dictiter_len(dictiterobject *di)
2092 Py_ssize_t len = 0;
2093 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
2094 len = di->len;
2095 return PyInt_FromSize_t(len);
2098 PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
2100 static PyMethodDef dictiter_methods[] = {
2101 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS, length_hint_doc},
2102 {NULL, NULL} /* sentinel */
2105 static PyObject *dictiter_iternextkey(dictiterobject *di)
2107 PyObject *key;
2108 register Py_ssize_t i, mask;
2109 register dictentry *ep;
2110 dictobject *d = di->di_dict;
2112 if (d == NULL)
2113 return NULL;
2114 assert (PyDict_Check(d));
2116 if (di->di_used != d->ma_used) {
2117 PyErr_SetString(PyExc_RuntimeError,
2118 "dictionary changed size during iteration");
2119 di->di_used = -1; /* Make this state sticky */
2120 return NULL;
2123 i = di->di_pos;
2124 if (i < 0)
2125 goto fail;
2126 ep = d->ma_table;
2127 mask = d->ma_mask;
2128 while (i <= mask && ep[i].me_value == NULL)
2129 i++;
2130 di->di_pos = i+1;
2131 if (i > mask)
2132 goto fail;
2133 di->len--;
2134 key = ep[i].me_key;
2135 Py_INCREF(key);
2136 return key;
2138 fail:
2139 Py_DECREF(d);
2140 di->di_dict = NULL;
2141 return NULL;
2144 PyTypeObject PyDictIterKey_Type = {
2145 PyObject_HEAD_INIT(&PyType_Type)
2146 0, /* ob_size */
2147 "dictionary-keyiterator", /* tp_name */
2148 sizeof(dictiterobject), /* tp_basicsize */
2149 0, /* tp_itemsize */
2150 /* methods */
2151 (destructor)dictiter_dealloc, /* tp_dealloc */
2152 0, /* tp_print */
2153 0, /* tp_getattr */
2154 0, /* tp_setattr */
2155 0, /* tp_compare */
2156 0, /* tp_repr */
2157 0, /* tp_as_number */
2158 0, /* tp_as_sequence */
2159 0, /* tp_as_mapping */
2160 0, /* tp_hash */
2161 0, /* tp_call */
2162 0, /* tp_str */
2163 PyObject_GenericGetAttr, /* tp_getattro */
2164 0, /* tp_setattro */
2165 0, /* tp_as_buffer */
2166 Py_TPFLAGS_DEFAULT, /* tp_flags */
2167 0, /* tp_doc */
2168 0, /* tp_traverse */
2169 0, /* tp_clear */
2170 0, /* tp_richcompare */
2171 0, /* tp_weaklistoffset */
2172 PyObject_SelfIter, /* tp_iter */
2173 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
2174 dictiter_methods, /* tp_methods */
2178 static PyObject *dictiter_iternextvalue(dictiterobject *di)
2180 PyObject *value;
2181 register Py_ssize_t i, mask;
2182 register dictentry *ep;
2183 dictobject *d = di->di_dict;
2185 if (d == NULL)
2186 return NULL;
2187 assert (PyDict_Check(d));
2189 if (di->di_used != d->ma_used) {
2190 PyErr_SetString(PyExc_RuntimeError,
2191 "dictionary changed size during iteration");
2192 di->di_used = -1; /* Make this state sticky */
2193 return NULL;
2196 i = di->di_pos;
2197 mask = d->ma_mask;
2198 if (i < 0 || i > mask)
2199 goto fail;
2200 ep = d->ma_table;
2201 while ((value=ep[i].me_value) == NULL) {
2202 i++;
2203 if (i > mask)
2204 goto fail;
2206 di->di_pos = i+1;
2207 di->len--;
2208 Py_INCREF(value);
2209 return value;
2211 fail:
2212 Py_DECREF(d);
2213 di->di_dict = NULL;
2214 return NULL;
2217 PyTypeObject PyDictIterValue_Type = {
2218 PyObject_HEAD_INIT(&PyType_Type)
2219 0, /* ob_size */
2220 "dictionary-valueiterator", /* tp_name */
2221 sizeof(dictiterobject), /* tp_basicsize */
2222 0, /* tp_itemsize */
2223 /* methods */
2224 (destructor)dictiter_dealloc, /* tp_dealloc */
2225 0, /* tp_print */
2226 0, /* tp_getattr */
2227 0, /* tp_setattr */
2228 0, /* tp_compare */
2229 0, /* tp_repr */
2230 0, /* tp_as_number */
2231 0, /* tp_as_sequence */
2232 0, /* tp_as_mapping */
2233 0, /* tp_hash */
2234 0, /* tp_call */
2235 0, /* tp_str */
2236 PyObject_GenericGetAttr, /* tp_getattro */
2237 0, /* tp_setattro */
2238 0, /* tp_as_buffer */
2239 Py_TPFLAGS_DEFAULT, /* tp_flags */
2240 0, /* tp_doc */
2241 0, /* tp_traverse */
2242 0, /* tp_clear */
2243 0, /* tp_richcompare */
2244 0, /* tp_weaklistoffset */
2245 PyObject_SelfIter, /* tp_iter */
2246 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
2247 dictiter_methods, /* tp_methods */
2251 static PyObject *dictiter_iternextitem(dictiterobject *di)
2253 PyObject *key, *value, *result = di->di_result;
2254 register Py_ssize_t i, mask;
2255 register dictentry *ep;
2256 dictobject *d = di->di_dict;
2258 if (d == NULL)
2259 return NULL;
2260 assert (PyDict_Check(d));
2262 if (di->di_used != d->ma_used) {
2263 PyErr_SetString(PyExc_RuntimeError,
2264 "dictionary changed size during iteration");
2265 di->di_used = -1; /* Make this state sticky */
2266 return NULL;
2269 i = di->di_pos;
2270 if (i < 0)
2271 goto fail;
2272 ep = d->ma_table;
2273 mask = d->ma_mask;
2274 while (i <= mask && ep[i].me_value == NULL)
2275 i++;
2276 di->di_pos = i+1;
2277 if (i > mask)
2278 goto fail;
2280 if (result->ob_refcnt == 1) {
2281 Py_INCREF(result);
2282 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2283 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2284 } else {
2285 result = PyTuple_New(2);
2286 if (result == NULL)
2287 return NULL;
2289 di->len--;
2290 key = ep[i].me_key;
2291 value = ep[i].me_value;
2292 Py_INCREF(key);
2293 Py_INCREF(value);
2294 PyTuple_SET_ITEM(result, 0, key);
2295 PyTuple_SET_ITEM(result, 1, value);
2296 return result;
2298 fail:
2299 Py_DECREF(d);
2300 di->di_dict = NULL;
2301 return NULL;
2304 PyTypeObject PyDictIterItem_Type = {
2305 PyObject_HEAD_INIT(&PyType_Type)
2306 0, /* ob_size */
2307 "dictionary-itemiterator", /* tp_name */
2308 sizeof(dictiterobject), /* tp_basicsize */
2309 0, /* tp_itemsize */
2310 /* methods */
2311 (destructor)dictiter_dealloc, /* tp_dealloc */
2312 0, /* tp_print */
2313 0, /* tp_getattr */
2314 0, /* tp_setattr */
2315 0, /* tp_compare */
2316 0, /* tp_repr */
2317 0, /* tp_as_number */
2318 0, /* tp_as_sequence */
2319 0, /* tp_as_mapping */
2320 0, /* tp_hash */
2321 0, /* tp_call */
2322 0, /* tp_str */
2323 PyObject_GenericGetAttr, /* tp_getattro */
2324 0, /* tp_setattro */
2325 0, /* tp_as_buffer */
2326 Py_TPFLAGS_DEFAULT, /* tp_flags */
2327 0, /* tp_doc */
2328 0, /* tp_traverse */
2329 0, /* tp_clear */
2330 0, /* tp_richcompare */
2331 0, /* tp_weaklistoffset */
2332 PyObject_SelfIter, /* tp_iter */
2333 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
2334 dictiter_methods, /* tp_methods */