Oops. Fix syntax for C89 compilers.
[python.git] / Objects / dictobject.c
blob0eccdbba9f00abe2d09861a831e3bf0a2168e062
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.
115 /* Object used as dummy key to fill deleted entries */
116 static PyObject *dummy = NULL; /* Initialized by first call to newdictobject() */
118 /* forward declarations */
119 static dictentry *
120 lookdict_string(dictobject *mp, PyObject *key, long hash);
122 #ifdef SHOW_CONVERSION_COUNTS
123 static long created = 0L;
124 static long converted = 0L;
126 static void
127 show_counts(void)
129 fprintf(stderr, "created %ld string dicts\n", created);
130 fprintf(stderr, "converted %ld to normal dicts\n", converted);
131 fprintf(stderr, "%.2f%% conversion rate\n", (100.0*converted)/created);
133 #endif
135 /* Initialization macros.
136 There are two ways to create a dict: PyDict_New() is the main C API
137 function, and the tp_new slot maps to dict_new(). In the latter case we
138 can save a little time over what PyDict_New does because it's guaranteed
139 that the PyDictObject struct is already zeroed out.
140 Everyone except dict_new() should use EMPTY_TO_MINSIZE (unless they have
141 an excellent reason not to).
144 #define INIT_NONZERO_DICT_SLOTS(mp) do { \
145 (mp)->ma_table = (mp)->ma_smalltable; \
146 (mp)->ma_mask = PyDict_MINSIZE - 1; \
147 } while(0)
149 #define EMPTY_TO_MINSIZE(mp) do { \
150 memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \
151 (mp)->ma_used = (mp)->ma_fill = 0; \
152 INIT_NONZERO_DICT_SLOTS(mp); \
153 } while(0)
155 /* Dictionary reuse scheme to save calls to malloc, free, and memset */
156 #define MAXFREEDICTS 80
157 static PyDictObject *free_dicts[MAXFREEDICTS];
158 static int num_free_dicts = 0;
160 PyObject *
161 PyDict_New(void)
163 register dictobject *mp;
164 if (dummy == NULL) { /* Auto-initialize dummy */
165 dummy = PyString_FromString("<dummy key>");
166 if (dummy == NULL)
167 return NULL;
168 #ifdef SHOW_CONVERSION_COUNTS
169 Py_AtExit(show_counts);
170 #endif
172 if (num_free_dicts) {
173 mp = free_dicts[--num_free_dicts];
174 assert (mp != NULL);
175 assert (mp->ob_type == &PyDict_Type);
176 _Py_NewReference((PyObject *)mp);
177 if (mp->ma_fill) {
178 EMPTY_TO_MINSIZE(mp);
180 assert (mp->ma_used == 0);
181 assert (mp->ma_table == mp->ma_smalltable);
182 assert (mp->ma_mask == PyDict_MINSIZE - 1);
183 } else {
184 mp = PyObject_GC_New(dictobject, &PyDict_Type);
185 if (mp == NULL)
186 return NULL;
187 EMPTY_TO_MINSIZE(mp);
189 mp->ma_lookup = lookdict_string;
190 #ifdef SHOW_CONVERSION_COUNTS
191 ++created;
192 #endif
193 _PyObject_GC_TRACK(mp);
194 return (PyObject *)mp;
198 The basic lookup function used by all operations.
199 This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
200 Open addressing is preferred over chaining since the link overhead for
201 chaining would be substantial (100% with typical malloc overhead).
203 The initial probe index is computed as hash mod the table size. Subsequent
204 probe indices are computed as explained earlier.
206 All arithmetic on hash should ignore overflow.
208 (The details in this version are due to Tim Peters, building on many past
209 contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
210 Christian Tismer).
212 This function must never return NULL; failures are indicated by returning
213 a dictentry* for which the me_value field is NULL. Exceptions are never
214 reported by this function, and outstanding exceptions are maintained.
217 static dictentry *
218 lookdict(dictobject *mp, PyObject *key, register long hash)
220 register Py_ssize_t i;
221 register size_t perturb;
222 register dictentry *freeslot;
223 register unsigned int mask = mp->ma_mask;
224 dictentry *ep0 = mp->ma_table;
225 register dictentry *ep;
226 register int restore_error;
227 register int checked_error;
228 register int cmp;
229 PyObject *err_type, *err_value, *err_tb;
230 PyObject *startkey;
232 i = hash & mask;
233 ep = &ep0[i];
234 if (ep->me_key == NULL || ep->me_key == key)
235 return ep;
237 restore_error = checked_error = 0;
238 if (ep->me_key == dummy)
239 freeslot = ep;
240 else {
241 if (ep->me_hash == hash) {
242 /* error can't have been checked yet */
243 checked_error = 1;
244 if (PyErr_Occurred()) {
245 restore_error = 1;
246 PyErr_Fetch(&err_type, &err_value, &err_tb);
248 startkey = ep->me_key;
249 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
250 if (cmp < 0)
251 PyErr_Clear();
252 if (ep0 == mp->ma_table && ep->me_key == startkey) {
253 if (cmp > 0)
254 goto Done;
256 else {
257 /* The compare did major nasty stuff to the
258 * dict: start over.
259 * XXX A clever adversary could prevent this
260 * XXX from terminating.
262 ep = lookdict(mp, key, hash);
263 goto Done;
266 freeslot = NULL;
269 /* In the loop, me_key == dummy is by far (factor of 100s) the
270 least likely outcome, so test for that last. */
271 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
272 i = (i << 2) + i + perturb + 1;
273 ep = &ep0[i & mask];
274 if (ep->me_key == NULL) {
275 if (freeslot != NULL)
276 ep = freeslot;
277 break;
279 if (ep->me_key == key)
280 break;
281 if (ep->me_hash == hash && ep->me_key != dummy) {
282 if (!checked_error) {
283 checked_error = 1;
284 if (PyErr_Occurred()) {
285 restore_error = 1;
286 PyErr_Fetch(&err_type, &err_value,
287 &err_tb);
290 startkey = ep->me_key;
291 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
292 if (cmp < 0)
293 PyErr_Clear();
294 if (ep0 == mp->ma_table && ep->me_key == startkey) {
295 if (cmp > 0)
296 break;
298 else {
299 /* The compare did major nasty stuff to the
300 * dict: start over.
301 * XXX A clever adversary could prevent this
302 * XXX from terminating.
304 ep = lookdict(mp, key, hash);
305 break;
308 else if (ep->me_key == dummy && freeslot == NULL)
309 freeslot = ep;
312 Done:
313 if (restore_error)
314 PyErr_Restore(err_type, err_value, err_tb);
315 return ep;
319 * Hacked up version of lookdict which can assume keys are always strings;
320 * this assumption allows testing for errors during PyObject_Compare() to
321 * be dropped; string-string comparisons never raise exceptions. This also
322 * means we don't need to go through PyObject_Compare(); we can always use
323 * _PyString_Eq directly.
325 * This is valuable because the general-case error handling in lookdict() is
326 * expensive, and dicts with pure-string keys are very common.
328 static dictentry *
329 lookdict_string(dictobject *mp, PyObject *key, register long hash)
331 register Py_ssize_t i;
332 register size_t perturb;
333 register dictentry *freeslot;
334 register unsigned int mask = mp->ma_mask;
335 dictentry *ep0 = mp->ma_table;
336 register dictentry *ep;
338 /* Make sure this function doesn't have to handle non-string keys,
339 including subclasses of str; e.g., one reason to subclass
340 strings is to override __eq__, and for speed we don't cater to
341 that here. */
342 if (!PyString_CheckExact(key)) {
343 #ifdef SHOW_CONVERSION_COUNTS
344 ++converted;
345 #endif
346 mp->ma_lookup = lookdict;
347 return lookdict(mp, key, hash);
349 i = hash & mask;
350 ep = &ep0[i];
351 if (ep->me_key == NULL || ep->me_key == key)
352 return ep;
353 if (ep->me_key == dummy)
354 freeslot = ep;
355 else {
356 if (ep->me_hash == hash
357 && _PyString_Eq(ep->me_key, key)) {
358 return ep;
360 freeslot = NULL;
363 /* In the loop, me_key == dummy is by far (factor of 100s) the
364 least likely outcome, so test for that last. */
365 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
366 i = (i << 2) + i + perturb + 1;
367 ep = &ep0[i & mask];
368 if (ep->me_key == NULL)
369 return freeslot == NULL ? ep : freeslot;
370 if (ep->me_key == key
371 || (ep->me_hash == hash
372 && ep->me_key != dummy
373 && _PyString_Eq(ep->me_key, key)))
374 return ep;
375 if (ep->me_key == dummy && freeslot == NULL)
376 freeslot = ep;
381 Internal routine to insert a new item into the table.
382 Used both by the internal resize routine and by the public insert routine.
383 Eats a reference to key and one to value.
385 static void
386 insertdict(register dictobject *mp, PyObject *key, long hash, PyObject *value)
388 PyObject *old_value;
389 register dictentry *ep;
390 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
392 assert(mp->ma_lookup != NULL);
393 ep = mp->ma_lookup(mp, key, hash);
394 if (ep->me_value != NULL) {
395 old_value = ep->me_value;
396 ep->me_value = value;
397 Py_DECREF(old_value); /* which **CAN** re-enter */
398 Py_DECREF(key);
400 else {
401 if (ep->me_key == NULL)
402 mp->ma_fill++;
403 else {
404 assert(ep->me_key == dummy);
405 Py_DECREF(dummy);
407 ep->me_key = key;
408 ep->me_hash = hash;
409 ep->me_value = value;
410 mp->ma_used++;
415 Restructure the table by allocating a new table and reinserting all
416 items again. When entries have been deleted, the new table may
417 actually be smaller than the old one.
419 static int
420 dictresize(dictobject *mp, int minused)
422 int newsize;
423 dictentry *oldtable, *newtable, *ep;
424 int i;
425 int is_oldtable_malloced;
426 dictentry small_copy[PyDict_MINSIZE];
428 assert(minused >= 0);
430 /* Find the smallest table size > minused. */
431 for (newsize = PyDict_MINSIZE;
432 newsize <= minused && newsize > 0;
433 newsize <<= 1)
435 if (newsize <= 0) {
436 PyErr_NoMemory();
437 return -1;
440 /* Get space for a new table. */
441 oldtable = mp->ma_table;
442 assert(oldtable != NULL);
443 is_oldtable_malloced = oldtable != mp->ma_smalltable;
445 if (newsize == PyDict_MINSIZE) {
446 /* A large table is shrinking, or we can't get any smaller. */
447 newtable = mp->ma_smalltable;
448 if (newtable == oldtable) {
449 if (mp->ma_fill == mp->ma_used) {
450 /* No dummies, so no point doing anything. */
451 return 0;
453 /* We're not going to resize it, but rebuild the
454 table anyway to purge old dummy entries.
455 Subtle: This is *necessary* if fill==size,
456 as lookdict needs at least one virgin slot to
457 terminate failing searches. If fill < size, it's
458 merely desirable, as dummies slow searches. */
459 assert(mp->ma_fill > mp->ma_used);
460 memcpy(small_copy, oldtable, sizeof(small_copy));
461 oldtable = small_copy;
464 else {
465 newtable = PyMem_NEW(dictentry, newsize);
466 if (newtable == NULL) {
467 PyErr_NoMemory();
468 return -1;
472 /* Make the dict empty, using the new table. */
473 assert(newtable != oldtable);
474 mp->ma_table = newtable;
475 mp->ma_mask = newsize - 1;
476 memset(newtable, 0, sizeof(dictentry) * newsize);
477 mp->ma_used = 0;
478 i = mp->ma_fill;
479 mp->ma_fill = 0;
481 /* Copy the data over; this is refcount-neutral for active entries;
482 dummy entries aren't copied over, of course */
483 for (ep = oldtable; i > 0; ep++) {
484 if (ep->me_value != NULL) { /* active entry */
485 --i;
486 insertdict(mp, ep->me_key, ep->me_hash, ep->me_value);
488 else if (ep->me_key != NULL) { /* dummy entry */
489 --i;
490 assert(ep->me_key == dummy);
491 Py_DECREF(ep->me_key);
493 /* else key == value == NULL: nothing to do */
496 if (is_oldtable_malloced)
497 PyMem_DEL(oldtable);
498 return 0;
501 PyObject *
502 PyDict_GetItem(PyObject *op, PyObject *key)
504 long hash;
505 dictobject *mp = (dictobject *)op;
506 if (!PyDict_Check(op)) {
507 return NULL;
509 if (!PyString_CheckExact(key) ||
510 (hash = ((PyStringObject *) key)->ob_shash) == -1)
512 hash = PyObject_Hash(key);
513 if (hash == -1) {
514 PyErr_Clear();
515 return NULL;
518 return (mp->ma_lookup)(mp, key, hash)->me_value;
521 /* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
522 * dictionary if it's merely replacing the value for an existing key.
523 * This means that it's safe to loop over a dictionary with PyDict_Next()
524 * and occasionally replace a value -- but you can't insert new keys or
525 * remove them.
528 PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
530 register dictobject *mp;
531 register long hash;
532 register int n_used;
534 if (!PyDict_Check(op)) {
535 PyErr_BadInternalCall();
536 return -1;
538 mp = (dictobject *)op;
539 if (PyString_CheckExact(key)) {
540 hash = ((PyStringObject *)key)->ob_shash;
541 if (hash == -1)
542 hash = PyObject_Hash(key);
544 else {
545 hash = PyObject_Hash(key);
546 if (hash == -1)
547 return -1;
549 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
550 n_used = mp->ma_used;
551 Py_INCREF(value);
552 Py_INCREF(key);
553 insertdict(mp, key, hash, value);
554 /* If we added a key, we can safely resize. Otherwise just return!
555 * If fill >= 2/3 size, adjust size. Normally, this doubles or
556 * quaduples the size, but it's also possible for the dict to shrink
557 * (if ma_fill is much larger than ma_used, meaning a lot of dict
558 * keys have been * deleted).
560 * Quadrupling the size improves average dictionary sparseness
561 * (reducing collisions) at the cost of some memory and iteration
562 * speed (which loops over every possible entry). It also halves
563 * the number of expensive resize operations in a growing dictionary.
565 * Very large dictionaries (over 50K items) use doubling instead.
566 * This may help applications with severe memory constraints.
568 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
569 return 0;
570 return dictresize(mp, (mp->ma_used>50000 ? mp->ma_used*2 : mp->ma_used*4));
574 PyDict_DelItem(PyObject *op, PyObject *key)
576 register dictobject *mp;
577 register long hash;
578 register dictentry *ep;
579 PyObject *old_value, *old_key;
581 if (!PyDict_Check(op)) {
582 PyErr_BadInternalCall();
583 return -1;
585 if (!PyString_CheckExact(key) ||
586 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
587 hash = PyObject_Hash(key);
588 if (hash == -1)
589 return -1;
591 mp = (dictobject *)op;
592 ep = (mp->ma_lookup)(mp, key, hash);
593 if (ep->me_value == NULL) {
594 PyErr_SetObject(PyExc_KeyError, key);
595 return -1;
597 old_key = ep->me_key;
598 Py_INCREF(dummy);
599 ep->me_key = dummy;
600 old_value = ep->me_value;
601 ep->me_value = NULL;
602 mp->ma_used--;
603 Py_DECREF(old_value);
604 Py_DECREF(old_key);
605 return 0;
608 void
609 PyDict_Clear(PyObject *op)
611 dictobject *mp;
612 dictentry *ep, *table;
613 int table_is_malloced;
614 int fill;
615 dictentry small_copy[PyDict_MINSIZE];
616 #ifdef Py_DEBUG
617 int i, n;
618 #endif
620 if (!PyDict_Check(op))
621 return;
622 mp = (dictobject *)op;
623 #ifdef Py_DEBUG
624 n = mp->ma_mask + 1;
625 i = 0;
626 #endif
628 table = mp->ma_table;
629 assert(table != NULL);
630 table_is_malloced = table != mp->ma_smalltable;
632 /* This is delicate. During the process of clearing the dict,
633 * decrefs can cause the dict to mutate. To avoid fatal confusion
634 * (voice of experience), we have to make the dict empty before
635 * clearing the slots, and never refer to anything via mp->xxx while
636 * clearing.
638 fill = mp->ma_fill;
639 if (table_is_malloced)
640 EMPTY_TO_MINSIZE(mp);
642 else if (fill > 0) {
643 /* It's a small table with something that needs to be cleared.
644 * Afraid the only safe way is to copy the dict entries into
645 * another small table first.
647 memcpy(small_copy, table, sizeof(small_copy));
648 table = small_copy;
649 EMPTY_TO_MINSIZE(mp);
651 /* else it's a small table that's already empty */
653 /* Now we can finally clear things. If C had refcounts, we could
654 * assert that the refcount on table is 1 now, i.e. that this function
655 * has unique access to it, so decref side-effects can't alter it.
657 for (ep = table; fill > 0; ++ep) {
658 #ifdef Py_DEBUG
659 assert(i < n);
660 ++i;
661 #endif
662 if (ep->me_key) {
663 --fill;
664 Py_DECREF(ep->me_key);
665 Py_XDECREF(ep->me_value);
667 #ifdef Py_DEBUG
668 else
669 assert(ep->me_value == NULL);
670 #endif
673 if (table_is_malloced)
674 PyMem_DEL(table);
678 * Iterate over a dict. Use like so:
680 * int i;
681 * PyObject *key, *value;
682 * i = 0; # important! i should not otherwise be changed by you
683 * while (PyDict_Next(yourdict, &i, &key, &value)) {
684 * Refer to borrowed references in key and value.
687 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
688 * mutates the dict. One exception: it is safe if the loop merely changes
689 * the values associated with the keys (but doesn't insert new keys or
690 * delete keys), via PyDict_SetItem().
693 PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
695 register Py_ssize_t i;
696 register int mask;
697 register dictentry *ep;
699 if (!PyDict_Check(op))
700 return 0;
701 i = *ppos;
702 if (i < 0)
703 return 0;
704 ep = ((dictobject *)op)->ma_table;
705 mask = ((dictobject *)op)->ma_mask;
706 while (i <= mask && ep[i].me_value == NULL)
707 i++;
708 *ppos = i+1;
709 if (i > mask)
710 return 0;
711 if (pkey)
712 *pkey = ep[i].me_key;
713 if (pvalue)
714 *pvalue = ep[i].me_value;
715 return 1;
718 /* Methods */
720 static void
721 dict_dealloc(register dictobject *mp)
723 register dictentry *ep;
724 int fill = mp->ma_fill;
725 PyObject_GC_UnTrack(mp);
726 Py_TRASHCAN_SAFE_BEGIN(mp)
727 for (ep = mp->ma_table; fill > 0; ep++) {
728 if (ep->me_key) {
729 --fill;
730 Py_DECREF(ep->me_key);
731 Py_XDECREF(ep->me_value);
734 if (mp->ma_table != mp->ma_smalltable)
735 PyMem_DEL(mp->ma_table);
736 if (num_free_dicts < MAXFREEDICTS && mp->ob_type == &PyDict_Type)
737 free_dicts[num_free_dicts++] = mp;
738 else
739 mp->ob_type->tp_free((PyObject *)mp);
740 Py_TRASHCAN_SAFE_END(mp)
743 static int
744 dict_print(register dictobject *mp, register FILE *fp, register int flags)
746 register int i;
747 register int any;
749 i = Py_ReprEnter((PyObject*)mp);
750 if (i != 0) {
751 if (i < 0)
752 return i;
753 fprintf(fp, "{...}");
754 return 0;
757 fprintf(fp, "{");
758 any = 0;
759 for (i = 0; i <= mp->ma_mask; i++) {
760 dictentry *ep = mp->ma_table + i;
761 PyObject *pvalue = ep->me_value;
762 if (pvalue != NULL) {
763 /* Prevent PyObject_Repr from deleting value during
764 key format */
765 Py_INCREF(pvalue);
766 if (any++ > 0)
767 fprintf(fp, ", ");
768 if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) {
769 Py_DECREF(pvalue);
770 Py_ReprLeave((PyObject*)mp);
771 return -1;
773 fprintf(fp, ": ");
774 if (PyObject_Print(pvalue, fp, 0) != 0) {
775 Py_DECREF(pvalue);
776 Py_ReprLeave((PyObject*)mp);
777 return -1;
779 Py_DECREF(pvalue);
782 fprintf(fp, "}");
783 Py_ReprLeave((PyObject*)mp);
784 return 0;
787 static PyObject *
788 dict_repr(dictobject *mp)
790 Py_ssize_t i;
791 PyObject *s, *temp, *colon = NULL;
792 PyObject *pieces = NULL, *result = NULL;
793 PyObject *key, *value;
795 i = Py_ReprEnter((PyObject *)mp);
796 if (i != 0) {
797 return i > 0 ? PyString_FromString("{...}") : NULL;
800 if (mp->ma_used == 0) {
801 result = PyString_FromString("{}");
802 goto Done;
805 pieces = PyList_New(0);
806 if (pieces == NULL)
807 goto Done;
809 colon = PyString_FromString(": ");
810 if (colon == NULL)
811 goto Done;
813 /* Do repr() on each key+value pair, and insert ": " between them.
814 Note that repr may mutate the dict. */
815 i = 0;
816 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
817 int status;
818 /* Prevent repr from deleting value during key format. */
819 Py_INCREF(value);
820 s = PyObject_Repr(key);
821 PyString_Concat(&s, colon);
822 PyString_ConcatAndDel(&s, PyObject_Repr(value));
823 Py_DECREF(value);
824 if (s == NULL)
825 goto Done;
826 status = PyList_Append(pieces, s);
827 Py_DECREF(s); /* append created a new ref */
828 if (status < 0)
829 goto Done;
832 /* Add "{}" decorations to the first and last items. */
833 assert(PyList_GET_SIZE(pieces) > 0);
834 s = PyString_FromString("{");
835 if (s == NULL)
836 goto Done;
837 temp = PyList_GET_ITEM(pieces, 0);
838 PyString_ConcatAndDel(&s, temp);
839 PyList_SET_ITEM(pieces, 0, s);
840 if (s == NULL)
841 goto Done;
843 s = PyString_FromString("}");
844 if (s == NULL)
845 goto Done;
846 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
847 PyString_ConcatAndDel(&temp, s);
848 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
849 if (temp == NULL)
850 goto Done;
852 /* Paste them all together with ", " between. */
853 s = PyString_FromString(", ");
854 if (s == NULL)
855 goto Done;
856 result = _PyString_Join(s, pieces);
857 Py_DECREF(s);
859 Done:
860 Py_XDECREF(pieces);
861 Py_XDECREF(colon);
862 Py_ReprLeave((PyObject *)mp);
863 return result;
866 static Py_ssize_t
867 dict_length(dictobject *mp)
869 return mp->ma_used;
872 static PyObject *
873 dict_subscript(dictobject *mp, register PyObject *key)
875 PyObject *v;
876 long hash;
877 assert(mp->ma_table != NULL);
878 if (!PyString_CheckExact(key) ||
879 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
880 hash = PyObject_Hash(key);
881 if (hash == -1)
882 return NULL;
884 v = (mp->ma_lookup)(mp, key, hash) -> me_value;
885 if (v == NULL) {
886 if (!PyDict_CheckExact(mp)) {
887 /* Look up __missing__ method if we're a subclass. */
888 PyObject *missing;
889 static PyObject *missing_str = NULL;
890 if (missing_str == NULL)
891 missing_str =
892 PyString_InternFromString("__missing__");
893 missing = _PyType_Lookup(mp->ob_type, missing_str);
894 if (missing != NULL)
895 return PyObject_CallFunctionObjArgs(missing,
896 (PyObject *)mp, key, NULL);
898 PyErr_SetObject(PyExc_KeyError, key);
899 return NULL;
901 else
902 Py_INCREF(v);
903 return v;
906 static int
907 dict_ass_sub(dictobject *mp, PyObject *v, PyObject *w)
909 if (w == NULL)
910 return PyDict_DelItem((PyObject *)mp, v);
911 else
912 return PyDict_SetItem((PyObject *)mp, v, w);
915 static PyMappingMethods dict_as_mapping = {
916 (lenfunc)dict_length, /*mp_length*/
917 (binaryfunc)dict_subscript, /*mp_subscript*/
918 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
921 static PyObject *
922 dict_keys(register dictobject *mp)
924 register PyObject *v;
925 register int i, j;
926 dictentry *ep;
927 int mask, n;
929 again:
930 n = mp->ma_used;
931 v = PyList_New(n);
932 if (v == NULL)
933 return NULL;
934 if (n != mp->ma_used) {
935 /* Durnit. The allocations caused the dict to resize.
936 * Just start over, this shouldn't normally happen.
938 Py_DECREF(v);
939 goto again;
941 ep = mp->ma_table;
942 mask = mp->ma_mask;
943 for (i = 0, j = 0; i <= mask; i++) {
944 if (ep[i].me_value != NULL) {
945 PyObject *key = ep[i].me_key;
946 Py_INCREF(key);
947 PyList_SET_ITEM(v, j, key);
948 j++;
951 assert(j == n);
952 return v;
955 static PyObject *
956 dict_values(register dictobject *mp)
958 register PyObject *v;
959 register int i, j;
960 dictentry *ep;
961 int mask, n;
963 again:
964 n = mp->ma_used;
965 v = PyList_New(n);
966 if (v == NULL)
967 return NULL;
968 if (n != mp->ma_used) {
969 /* Durnit. The allocations caused the dict to resize.
970 * Just start over, this shouldn't normally happen.
972 Py_DECREF(v);
973 goto again;
975 ep = mp->ma_table;
976 mask = mp->ma_mask;
977 for (i = 0, j = 0; i <= mask; i++) {
978 if (ep[i].me_value != NULL) {
979 PyObject *value = ep[i].me_value;
980 Py_INCREF(value);
981 PyList_SET_ITEM(v, j, value);
982 j++;
985 assert(j == n);
986 return v;
989 static PyObject *
990 dict_items(register dictobject *mp)
992 register PyObject *v;
993 register int i, j, n;
994 int mask;
995 PyObject *item, *key, *value;
996 dictentry *ep;
998 /* Preallocate the list of tuples, to avoid allocations during
999 * the loop over the items, which could trigger GC, which
1000 * could resize the dict. :-(
1002 again:
1003 n = mp->ma_used;
1004 v = PyList_New(n);
1005 if (v == NULL)
1006 return NULL;
1007 for (i = 0; i < n; i++) {
1008 item = PyTuple_New(2);
1009 if (item == NULL) {
1010 Py_DECREF(v);
1011 return NULL;
1013 PyList_SET_ITEM(v, i, item);
1015 if (n != mp->ma_used) {
1016 /* Durnit. The allocations caused the dict to resize.
1017 * Just start over, this shouldn't normally happen.
1019 Py_DECREF(v);
1020 goto again;
1022 /* Nothing we do below makes any function calls. */
1023 ep = mp->ma_table;
1024 mask = mp->ma_mask;
1025 for (i = 0, j = 0; i <= mask; i++) {
1026 if ((value=ep[i].me_value) != NULL) {
1027 key = ep[i].me_key;
1028 item = PyList_GET_ITEM(v, j);
1029 Py_INCREF(key);
1030 PyTuple_SET_ITEM(item, 0, key);
1031 Py_INCREF(value);
1032 PyTuple_SET_ITEM(item, 1, value);
1033 j++;
1036 assert(j == n);
1037 return v;
1040 static PyObject *
1041 dict_fromkeys(PyObject *cls, PyObject *args)
1043 PyObject *seq;
1044 PyObject *value = Py_None;
1045 PyObject *it; /* iter(seq) */
1046 PyObject *key;
1047 PyObject *d;
1048 int status;
1050 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
1051 return NULL;
1053 d = PyObject_CallObject(cls, NULL);
1054 if (d == NULL)
1055 return NULL;
1057 it = PyObject_GetIter(seq);
1058 if (it == NULL){
1059 Py_DECREF(d);
1060 return NULL;
1063 for (;;) {
1064 key = PyIter_Next(it);
1065 if (key == NULL) {
1066 if (PyErr_Occurred())
1067 goto Fail;
1068 break;
1070 status = PyObject_SetItem(d, key, value);
1071 Py_DECREF(key);
1072 if (status < 0)
1073 goto Fail;
1076 Py_DECREF(it);
1077 return d;
1079 Fail:
1080 Py_DECREF(it);
1081 Py_DECREF(d);
1082 return NULL;
1085 static int
1086 dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
1088 PyObject *arg = NULL;
1089 int result = 0;
1091 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1092 result = -1;
1094 else if (arg != NULL) {
1095 if (PyObject_HasAttrString(arg, "keys"))
1096 result = PyDict_Merge(self, arg, 1);
1097 else
1098 result = PyDict_MergeFromSeq2(self, arg, 1);
1100 if (result == 0 && kwds != NULL)
1101 result = PyDict_Merge(self, kwds, 1);
1102 return result;
1105 static PyObject *
1106 dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1108 if (dict_update_common(self, args, kwds, "update") != -1)
1109 Py_RETURN_NONE;
1110 return NULL;
1113 /* Update unconditionally replaces existing items.
1114 Merge has a 3rd argument 'override'; if set, it acts like Update,
1115 otherwise it leaves existing items unchanged.
1117 PyDict_{Update,Merge} update/merge from a mapping object.
1119 PyDict_MergeFromSeq2 updates/merges from any iterable object
1120 producing iterable objects of length 2.
1124 PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1126 PyObject *it; /* iter(seq2) */
1127 int i; /* index into seq2 of current element */
1128 PyObject *item; /* seq2[i] */
1129 PyObject *fast; /* item as a 2-tuple or 2-list */
1131 assert(d != NULL);
1132 assert(PyDict_Check(d));
1133 assert(seq2 != NULL);
1135 it = PyObject_GetIter(seq2);
1136 if (it == NULL)
1137 return -1;
1139 for (i = 0; ; ++i) {
1140 PyObject *key, *value;
1141 Py_ssize_t n;
1143 fast = NULL;
1144 item = PyIter_Next(it);
1145 if (item == NULL) {
1146 if (PyErr_Occurred())
1147 goto Fail;
1148 break;
1151 /* Convert item to sequence, and verify length 2. */
1152 fast = PySequence_Fast(item, "");
1153 if (fast == NULL) {
1154 if (PyErr_ExceptionMatches(PyExc_TypeError))
1155 PyErr_Format(PyExc_TypeError,
1156 "cannot convert dictionary update "
1157 "sequence element #%d to a sequence",
1159 goto Fail;
1161 n = PySequence_Fast_GET_SIZE(fast);
1162 if (n != 2) {
1163 PyErr_Format(PyExc_ValueError,
1164 "dictionary update sequence element #%d "
1165 "has length %zd; 2 is required",
1166 i, n);
1167 goto Fail;
1170 /* Update/merge with this (key, value) pair. */
1171 key = PySequence_Fast_GET_ITEM(fast, 0);
1172 value = PySequence_Fast_GET_ITEM(fast, 1);
1173 if (override || PyDict_GetItem(d, key) == NULL) {
1174 int status = PyDict_SetItem(d, key, value);
1175 if (status < 0)
1176 goto Fail;
1178 Py_DECREF(fast);
1179 Py_DECREF(item);
1182 i = 0;
1183 goto Return;
1184 Fail:
1185 Py_XDECREF(item);
1186 Py_XDECREF(fast);
1187 i = -1;
1188 Return:
1189 Py_DECREF(it);
1190 return i;
1194 PyDict_Update(PyObject *a, PyObject *b)
1196 return PyDict_Merge(a, b, 1);
1200 PyDict_Merge(PyObject *a, PyObject *b, int override)
1202 register PyDictObject *mp, *other;
1203 register int i;
1204 dictentry *entry;
1206 /* We accept for the argument either a concrete dictionary object,
1207 * or an abstract "mapping" object. For the former, we can do
1208 * things quite efficiently. For the latter, we only require that
1209 * PyMapping_Keys() and PyObject_GetItem() be supported.
1211 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1212 PyErr_BadInternalCall();
1213 return -1;
1215 mp = (dictobject*)a;
1216 if (PyDict_Check(b)) {
1217 other = (dictobject*)b;
1218 if (other == mp || other->ma_used == 0)
1219 /* a.update(a) or a.update({}); nothing to do */
1220 return 0;
1221 if (mp->ma_used == 0)
1222 /* Since the target dict is empty, PyDict_GetItem()
1223 * always returns NULL. Setting override to 1
1224 * skips the unnecessary test.
1226 override = 1;
1227 /* Do one big resize at the start, rather than
1228 * incrementally resizing as we insert new items. Expect
1229 * that there will be no (or few) overlapping keys.
1231 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
1232 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
1233 return -1;
1235 for (i = 0; i <= other->ma_mask; i++) {
1236 entry = &other->ma_table[i];
1237 if (entry->me_value != NULL &&
1238 (override ||
1239 PyDict_GetItem(a, entry->me_key) == NULL)) {
1240 Py_INCREF(entry->me_key);
1241 Py_INCREF(entry->me_value);
1242 insertdict(mp, entry->me_key, entry->me_hash,
1243 entry->me_value);
1247 else {
1248 /* Do it the generic, slower way */
1249 PyObject *keys = PyMapping_Keys(b);
1250 PyObject *iter;
1251 PyObject *key, *value;
1252 int status;
1254 if (keys == NULL)
1255 /* Docstring says this is equivalent to E.keys() so
1256 * if E doesn't have a .keys() method we want
1257 * AttributeError to percolate up. Might as well
1258 * do the same for any other error.
1260 return -1;
1262 iter = PyObject_GetIter(keys);
1263 Py_DECREF(keys);
1264 if (iter == NULL)
1265 return -1;
1267 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
1268 if (!override && PyDict_GetItem(a, key) != NULL) {
1269 Py_DECREF(key);
1270 continue;
1272 value = PyObject_GetItem(b, key);
1273 if (value == NULL) {
1274 Py_DECREF(iter);
1275 Py_DECREF(key);
1276 return -1;
1278 status = PyDict_SetItem(a, key, value);
1279 Py_DECREF(key);
1280 Py_DECREF(value);
1281 if (status < 0) {
1282 Py_DECREF(iter);
1283 return -1;
1286 Py_DECREF(iter);
1287 if (PyErr_Occurred())
1288 /* Iterator completed, via error */
1289 return -1;
1291 return 0;
1294 static PyObject *
1295 dict_copy(register dictobject *mp)
1297 return PyDict_Copy((PyObject*)mp);
1300 PyObject *
1301 PyDict_Copy(PyObject *o)
1303 PyObject *copy;
1305 if (o == NULL || !PyDict_Check(o)) {
1306 PyErr_BadInternalCall();
1307 return NULL;
1309 copy = PyDict_New();
1310 if (copy == NULL)
1311 return NULL;
1312 if (PyDict_Merge(copy, o, 1) == 0)
1313 return copy;
1314 Py_DECREF(copy);
1315 return NULL;
1318 Py_ssize_t
1319 PyDict_Size(PyObject *mp)
1321 if (mp == NULL || !PyDict_Check(mp)) {
1322 PyErr_BadInternalCall();
1323 return -1;
1325 return ((dictobject *)mp)->ma_used;
1328 PyObject *
1329 PyDict_Keys(PyObject *mp)
1331 if (mp == NULL || !PyDict_Check(mp)) {
1332 PyErr_BadInternalCall();
1333 return NULL;
1335 return dict_keys((dictobject *)mp);
1338 PyObject *
1339 PyDict_Values(PyObject *mp)
1341 if (mp == NULL || !PyDict_Check(mp)) {
1342 PyErr_BadInternalCall();
1343 return NULL;
1345 return dict_values((dictobject *)mp);
1348 PyObject *
1349 PyDict_Items(PyObject *mp)
1351 if (mp == NULL || !PyDict_Check(mp)) {
1352 PyErr_BadInternalCall();
1353 return NULL;
1355 return dict_items((dictobject *)mp);
1358 /* Subroutine which returns the smallest key in a for which b's value
1359 is different or absent. The value is returned too, through the
1360 pval argument. Both are NULL if no key in a is found for which b's status
1361 differs. The refcounts on (and only on) non-NULL *pval and function return
1362 values must be decremented by the caller (characterize() increments them
1363 to ensure that mutating comparison and PyDict_GetItem calls can't delete
1364 them before the caller is done looking at them). */
1366 static PyObject *
1367 characterize(dictobject *a, dictobject *b, PyObject **pval)
1369 PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */
1370 PyObject *aval = NULL; /* a[akey] */
1371 int i, cmp;
1373 for (i = 0; i <= a->ma_mask; i++) {
1374 PyObject *thiskey, *thisaval, *thisbval;
1375 if (a->ma_table[i].me_value == NULL)
1376 continue;
1377 thiskey = a->ma_table[i].me_key;
1378 Py_INCREF(thiskey); /* keep alive across compares */
1379 if (akey != NULL) {
1380 cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT);
1381 if (cmp < 0) {
1382 Py_DECREF(thiskey);
1383 goto Fail;
1385 if (cmp > 0 ||
1386 i > a->ma_mask ||
1387 a->ma_table[i].me_value == NULL)
1389 /* Not the *smallest* a key; or maybe it is
1390 * but the compare shrunk the dict so we can't
1391 * find its associated value anymore; or
1392 * maybe it is but the compare deleted the
1393 * a[thiskey] entry.
1395 Py_DECREF(thiskey);
1396 continue;
1400 /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */
1401 thisaval = a->ma_table[i].me_value;
1402 assert(thisaval);
1403 Py_INCREF(thisaval); /* keep alive */
1404 thisbval = PyDict_GetItem((PyObject *)b, thiskey);
1405 if (thisbval == NULL)
1406 cmp = 0;
1407 else {
1408 /* both dicts have thiskey: same values? */
1409 cmp = PyObject_RichCompareBool(
1410 thisaval, thisbval, Py_EQ);
1411 if (cmp < 0) {
1412 Py_DECREF(thiskey);
1413 Py_DECREF(thisaval);
1414 goto Fail;
1417 if (cmp == 0) {
1418 /* New winner. */
1419 Py_XDECREF(akey);
1420 Py_XDECREF(aval);
1421 akey = thiskey;
1422 aval = thisaval;
1424 else {
1425 Py_DECREF(thiskey);
1426 Py_DECREF(thisaval);
1429 *pval = aval;
1430 return akey;
1432 Fail:
1433 Py_XDECREF(akey);
1434 Py_XDECREF(aval);
1435 *pval = NULL;
1436 return NULL;
1439 static int
1440 dict_compare(dictobject *a, dictobject *b)
1442 PyObject *adiff, *bdiff, *aval, *bval;
1443 int res;
1445 /* Compare lengths first */
1446 if (a->ma_used < b->ma_used)
1447 return -1; /* a is shorter */
1448 else if (a->ma_used > b->ma_used)
1449 return 1; /* b is shorter */
1451 /* Same length -- check all keys */
1452 bdiff = bval = NULL;
1453 adiff = characterize(a, b, &aval);
1454 if (adiff == NULL) {
1455 assert(!aval);
1456 /* Either an error, or a is a subset with the same length so
1457 * must be equal.
1459 res = PyErr_Occurred() ? -1 : 0;
1460 goto Finished;
1462 bdiff = characterize(b, a, &bval);
1463 if (bdiff == NULL && PyErr_Occurred()) {
1464 assert(!bval);
1465 res = -1;
1466 goto Finished;
1468 res = 0;
1469 if (bdiff) {
1470 /* bdiff == NULL "should be" impossible now, but perhaps
1471 * the last comparison done by the characterize() on a had
1472 * the side effect of making the dicts equal!
1474 res = PyObject_Compare(adiff, bdiff);
1476 if (res == 0 && bval != NULL)
1477 res = PyObject_Compare(aval, bval);
1479 Finished:
1480 Py_XDECREF(adiff);
1481 Py_XDECREF(bdiff);
1482 Py_XDECREF(aval);
1483 Py_XDECREF(bval);
1484 return res;
1487 /* Return 1 if dicts equal, 0 if not, -1 if error.
1488 * Gets out as soon as any difference is detected.
1489 * Uses only Py_EQ comparison.
1491 static int
1492 dict_equal(dictobject *a, dictobject *b)
1494 int i;
1496 if (a->ma_used != b->ma_used)
1497 /* can't be equal if # of entries differ */
1498 return 0;
1500 /* Same # of entries -- check all of 'em. Exit early on any diff. */
1501 for (i = 0; i <= a->ma_mask; i++) {
1502 PyObject *aval = a->ma_table[i].me_value;
1503 if (aval != NULL) {
1504 int cmp;
1505 PyObject *bval;
1506 PyObject *key = a->ma_table[i].me_key;
1507 /* temporarily bump aval's refcount to ensure it stays
1508 alive until we're done with it */
1509 Py_INCREF(aval);
1510 bval = PyDict_GetItem((PyObject *)b, key);
1511 if (bval == NULL) {
1512 Py_DECREF(aval);
1513 return 0;
1515 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1516 Py_DECREF(aval);
1517 if (cmp <= 0) /* error or not equal */
1518 return cmp;
1521 return 1;
1524 static PyObject *
1525 dict_richcompare(PyObject *v, PyObject *w, int op)
1527 int cmp;
1528 PyObject *res;
1530 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1531 res = Py_NotImplemented;
1533 else if (op == Py_EQ || op == Py_NE) {
1534 cmp = dict_equal((dictobject *)v, (dictobject *)w);
1535 if (cmp < 0)
1536 return NULL;
1537 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1539 else
1540 res = Py_NotImplemented;
1541 Py_INCREF(res);
1542 return res;
1545 static PyObject *
1546 dict_has_key(register dictobject *mp, PyObject *key)
1548 long hash;
1549 register long ok;
1550 if (!PyString_CheckExact(key) ||
1551 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1552 hash = PyObject_Hash(key);
1553 if (hash == -1)
1554 return NULL;
1556 ok = (mp->ma_lookup)(mp, key, hash)->me_value != NULL;
1557 return PyBool_FromLong(ok);
1560 static PyObject *
1561 dict_get(register dictobject *mp, PyObject *args)
1563 PyObject *key;
1564 PyObject *failobj = Py_None;
1565 PyObject *val = NULL;
1566 long hash;
1568 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
1569 return NULL;
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 val = (mp->ma_lookup)(mp, key, hash)->me_value;
1579 if (val == NULL)
1580 val = failobj;
1581 Py_INCREF(val);
1582 return val;
1586 static PyObject *
1587 dict_setdefault(register dictobject *mp, PyObject *args)
1589 PyObject *key;
1590 PyObject *failobj = Py_None;
1591 PyObject *val = NULL;
1592 long hash;
1594 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
1595 return NULL;
1597 if (!PyString_CheckExact(key) ||
1598 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1599 hash = PyObject_Hash(key);
1600 if (hash == -1)
1601 return NULL;
1603 val = (mp->ma_lookup)(mp, key, hash)->me_value;
1604 if (val == NULL) {
1605 val = failobj;
1606 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1607 val = NULL;
1609 Py_XINCREF(val);
1610 return val;
1614 static PyObject *
1615 dict_clear(register dictobject *mp)
1617 PyDict_Clear((PyObject *)mp);
1618 Py_RETURN_NONE;
1621 static PyObject *
1622 dict_pop(dictobject *mp, PyObject *args)
1624 long hash;
1625 dictentry *ep;
1626 PyObject *old_value, *old_key;
1627 PyObject *key, *deflt = NULL;
1629 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1630 return NULL;
1631 if (mp->ma_used == 0) {
1632 if (deflt) {
1633 Py_INCREF(deflt);
1634 return deflt;
1636 PyErr_SetString(PyExc_KeyError,
1637 "pop(): dictionary is empty");
1638 return NULL;
1640 if (!PyString_CheckExact(key) ||
1641 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1642 hash = PyObject_Hash(key);
1643 if (hash == -1)
1644 return NULL;
1646 ep = (mp->ma_lookup)(mp, key, hash);
1647 if (ep->me_value == NULL) {
1648 if (deflt) {
1649 Py_INCREF(deflt);
1650 return deflt;
1652 PyErr_SetObject(PyExc_KeyError, key);
1653 return NULL;
1655 old_key = ep->me_key;
1656 Py_INCREF(dummy);
1657 ep->me_key = dummy;
1658 old_value = ep->me_value;
1659 ep->me_value = NULL;
1660 mp->ma_used--;
1661 Py_DECREF(old_key);
1662 return old_value;
1665 static PyObject *
1666 dict_popitem(dictobject *mp)
1668 int i = 0;
1669 dictentry *ep;
1670 PyObject *res;
1672 /* Allocate the result tuple before checking the size. Believe it
1673 * or not, this allocation could trigger a garbage collection which
1674 * could empty the dict, so if we checked the size first and that
1675 * happened, the result would be an infinite loop (searching for an
1676 * entry that no longer exists). Note that the usual popitem()
1677 * idiom is "while d: k, v = d.popitem()". so needing to throw the
1678 * tuple away if the dict *is* empty isn't a significant
1679 * inefficiency -- possible, but unlikely in practice.
1681 res = PyTuple_New(2);
1682 if (res == NULL)
1683 return NULL;
1684 if (mp->ma_used == 0) {
1685 Py_DECREF(res);
1686 PyErr_SetString(PyExc_KeyError,
1687 "popitem(): dictionary is empty");
1688 return NULL;
1690 /* Set ep to "the first" dict entry with a value. We abuse the hash
1691 * field of slot 0 to hold a search finger:
1692 * If slot 0 has a value, use slot 0.
1693 * Else slot 0 is being used to hold a search finger,
1694 * and we use its hash value as the first index to look.
1696 ep = &mp->ma_table[0];
1697 if (ep->me_value == NULL) {
1698 i = (int)ep->me_hash;
1699 /* The hash field may be a real hash value, or it may be a
1700 * legit search finger, or it may be a once-legit search
1701 * finger that's out of bounds now because it wrapped around
1702 * or the table shrunk -- simply make sure it's in bounds now.
1704 if (i > mp->ma_mask || i < 1)
1705 i = 1; /* skip slot 0 */
1706 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1707 i++;
1708 if (i > mp->ma_mask)
1709 i = 1;
1712 PyTuple_SET_ITEM(res, 0, ep->me_key);
1713 PyTuple_SET_ITEM(res, 1, ep->me_value);
1714 Py_INCREF(dummy);
1715 ep->me_key = dummy;
1716 ep->me_value = NULL;
1717 mp->ma_used--;
1718 assert(mp->ma_table[0].me_value == NULL);
1719 mp->ma_table[0].me_hash = i + 1; /* next place to start */
1720 return res;
1723 static int
1724 dict_traverse(PyObject *op, visitproc visit, void *arg)
1726 Py_ssize_t i = 0;
1727 int err;
1728 PyObject *pk;
1729 PyObject *pv;
1731 while (PyDict_Next(op, &i, &pk, &pv)) {
1732 err = visit(pk, arg);
1733 if (err)
1734 return err;
1735 err = visit(pv, arg);
1736 if (err)
1737 return err;
1739 return 0;
1742 static int
1743 dict_tp_clear(PyObject *op)
1745 PyDict_Clear(op);
1746 return 0;
1750 extern PyTypeObject PyDictIterKey_Type; /* Forward */
1751 extern PyTypeObject PyDictIterValue_Type; /* Forward */
1752 extern PyTypeObject PyDictIterItem_Type; /* Forward */
1753 static PyObject *dictiter_new(dictobject *, PyTypeObject *);
1755 static PyObject *
1756 dict_iterkeys(dictobject *dict)
1758 return dictiter_new(dict, &PyDictIterKey_Type);
1761 static PyObject *
1762 dict_itervalues(dictobject *dict)
1764 return dictiter_new(dict, &PyDictIterValue_Type);
1767 static PyObject *
1768 dict_iteritems(dictobject *dict)
1770 return dictiter_new(dict, &PyDictIterItem_Type);
1774 PyDoc_STRVAR(has_key__doc__,
1775 "D.has_key(k) -> True if D has a key k, else False");
1777 PyDoc_STRVAR(contains__doc__,
1778 "D.__contains__(k) -> True if D has a key k, else False");
1780 PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
1782 PyDoc_STRVAR(get__doc__,
1783 "D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
1785 PyDoc_STRVAR(setdefault_doc__,
1786 "D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D");
1788 PyDoc_STRVAR(pop__doc__,
1789 "D.pop(k[,d]) -> v, remove specified key and return the corresponding value\n\
1790 If key is not found, d is returned if given, otherwise KeyError is raised");
1792 PyDoc_STRVAR(popitem__doc__,
1793 "D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
1794 2-tuple; but raise KeyError if D is empty");
1796 PyDoc_STRVAR(keys__doc__,
1797 "D.keys() -> list of D's keys");
1799 PyDoc_STRVAR(items__doc__,
1800 "D.items() -> list of D's (key, value) pairs, as 2-tuples");
1802 PyDoc_STRVAR(values__doc__,
1803 "D.values() -> list of D's values");
1805 PyDoc_STRVAR(update__doc__,
1806 "D.update(E, **F) -> None. Update D from E and F: for k in E: D[k] = E[k]\n\
1807 (if E has keys else: for (k, v) in E: D[k] = v) then: for k in F: D[k] = F[k]");
1809 PyDoc_STRVAR(fromkeys__doc__,
1810 "dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
1811 v defaults to None.");
1813 PyDoc_STRVAR(clear__doc__,
1814 "D.clear() -> None. Remove all items from D.");
1816 PyDoc_STRVAR(copy__doc__,
1817 "D.copy() -> a shallow copy of D");
1819 PyDoc_STRVAR(iterkeys__doc__,
1820 "D.iterkeys() -> an iterator over the keys of D");
1822 PyDoc_STRVAR(itervalues__doc__,
1823 "D.itervalues() -> an iterator over the values of D");
1825 PyDoc_STRVAR(iteritems__doc__,
1826 "D.iteritems() -> an iterator over the (key, value) items of D");
1828 static PyMethodDef mapp_methods[] = {
1829 {"__contains__",(PyCFunction)dict_has_key, METH_O | METH_COEXIST,
1830 contains__doc__},
1831 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
1832 getitem__doc__},
1833 {"has_key", (PyCFunction)dict_has_key, METH_O,
1834 has_key__doc__},
1835 {"get", (PyCFunction)dict_get, METH_VARARGS,
1836 get__doc__},
1837 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
1838 setdefault_doc__},
1839 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
1840 pop__doc__},
1841 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
1842 popitem__doc__},
1843 {"keys", (PyCFunction)dict_keys, METH_NOARGS,
1844 keys__doc__},
1845 {"items", (PyCFunction)dict_items, METH_NOARGS,
1846 items__doc__},
1847 {"values", (PyCFunction)dict_values, METH_NOARGS,
1848 values__doc__},
1849 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
1850 update__doc__},
1851 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
1852 fromkeys__doc__},
1853 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
1854 clear__doc__},
1855 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
1856 copy__doc__},
1857 {"iterkeys", (PyCFunction)dict_iterkeys, METH_NOARGS,
1858 iterkeys__doc__},
1859 {"itervalues", (PyCFunction)dict_itervalues, METH_NOARGS,
1860 itervalues__doc__},
1861 {"iteritems", (PyCFunction)dict_iteritems, METH_NOARGS,
1862 iteritems__doc__},
1863 {NULL, NULL} /* sentinel */
1867 PyDict_Contains(PyObject *op, PyObject *key)
1869 long hash;
1870 dictobject *mp = (dictobject *)op;
1872 if (!PyString_CheckExact(key) ||
1873 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1874 hash = PyObject_Hash(key);
1875 if (hash == -1)
1876 return -1;
1878 return (mp->ma_lookup)(mp, key, hash)->me_value != NULL;
1881 /* Hack to implement "key in dict" */
1882 static PySequenceMethods dict_as_sequence = {
1883 0, /* sq_length */
1884 0, /* sq_concat */
1885 0, /* sq_repeat */
1886 0, /* sq_item */
1887 0, /* sq_slice */
1888 0, /* sq_ass_item */
1889 0, /* sq_ass_slice */
1890 (objobjproc)PyDict_Contains, /* sq_contains */
1891 0, /* sq_inplace_concat */
1892 0, /* sq_inplace_repeat */
1895 static PyObject *
1896 dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1898 PyObject *self;
1900 assert(type != NULL && type->tp_alloc != NULL);
1901 self = type->tp_alloc(type, 0);
1902 if (self != NULL) {
1903 PyDictObject *d = (PyDictObject *)self;
1904 /* It's guaranteed that tp->alloc zeroed out the struct. */
1905 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
1906 INIT_NONZERO_DICT_SLOTS(d);
1907 d->ma_lookup = lookdict_string;
1908 #ifdef SHOW_CONVERSION_COUNTS
1909 ++created;
1910 #endif
1912 return self;
1915 static int
1916 dict_init(PyObject *self, PyObject *args, PyObject *kwds)
1918 return dict_update_common(self, args, kwds, "dict");
1921 static long
1922 dict_nohash(PyObject *self)
1924 PyErr_SetString(PyExc_TypeError, "dict objects are unhashable");
1925 return -1;
1928 static PyObject *
1929 dict_iter(dictobject *dict)
1931 return dictiter_new(dict, &PyDictIterKey_Type);
1934 PyDoc_STRVAR(dictionary_doc,
1935 "dict() -> new empty dictionary.\n"
1936 "dict(mapping) -> new dictionary initialized from a mapping object's\n"
1937 " (key, value) pairs.\n"
1938 "dict(seq) -> new dictionary initialized as if via:\n"
1939 " d = {}\n"
1940 " for k, v in seq:\n"
1941 " d[k] = v\n"
1942 "dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
1943 " in the keyword argument list. For example: dict(one=1, two=2)");
1945 PyTypeObject PyDict_Type = {
1946 PyObject_HEAD_INIT(&PyType_Type)
1948 "dict",
1949 sizeof(dictobject),
1951 (destructor)dict_dealloc, /* tp_dealloc */
1952 (printfunc)dict_print, /* tp_print */
1953 0, /* tp_getattr */
1954 0, /* tp_setattr */
1955 (cmpfunc)dict_compare, /* tp_compare */
1956 (reprfunc)dict_repr, /* tp_repr */
1957 0, /* tp_as_number */
1958 &dict_as_sequence, /* tp_as_sequence */
1959 &dict_as_mapping, /* tp_as_mapping */
1960 dict_nohash, /* tp_hash */
1961 0, /* tp_call */
1962 0, /* tp_str */
1963 PyObject_GenericGetAttr, /* tp_getattro */
1964 0, /* tp_setattro */
1965 0, /* tp_as_buffer */
1966 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1967 Py_TPFLAGS_BASETYPE, /* tp_flags */
1968 dictionary_doc, /* tp_doc */
1969 (traverseproc)dict_traverse, /* tp_traverse */
1970 (inquiry)dict_tp_clear, /* tp_clear */
1971 dict_richcompare, /* tp_richcompare */
1972 0, /* tp_weaklistoffset */
1973 (getiterfunc)dict_iter, /* tp_iter */
1974 0, /* tp_iternext */
1975 mapp_methods, /* tp_methods */
1976 0, /* tp_members */
1977 0, /* tp_getset */
1978 0, /* tp_base */
1979 0, /* tp_dict */
1980 0, /* tp_descr_get */
1981 0, /* tp_descr_set */
1982 0, /* tp_dictoffset */
1983 (initproc)dict_init, /* tp_init */
1984 PyType_GenericAlloc, /* tp_alloc */
1985 dict_new, /* tp_new */
1986 PyObject_GC_Del, /* tp_free */
1989 /* For backward compatibility with old dictionary interface */
1991 PyObject *
1992 PyDict_GetItemString(PyObject *v, const char *key)
1994 PyObject *kv, *rv;
1995 kv = PyString_FromString(key);
1996 if (kv == NULL)
1997 return NULL;
1998 rv = PyDict_GetItem(v, kv);
1999 Py_DECREF(kv);
2000 return rv;
2004 PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
2006 PyObject *kv;
2007 int err;
2008 kv = PyString_FromString(key);
2009 if (kv == NULL)
2010 return -1;
2011 PyString_InternInPlace(&kv); /* XXX Should we really? */
2012 err = PyDict_SetItem(v, kv, item);
2013 Py_DECREF(kv);
2014 return err;
2018 PyDict_DelItemString(PyObject *v, const char *key)
2020 PyObject *kv;
2021 int err;
2022 kv = PyString_FromString(key);
2023 if (kv == NULL)
2024 return -1;
2025 err = PyDict_DelItem(v, kv);
2026 Py_DECREF(kv);
2027 return err;
2030 /* Dictionary iterator types */
2032 typedef struct {
2033 PyObject_HEAD
2034 dictobject *di_dict; /* Set to NULL when iterator is exhausted */
2035 int di_used;
2036 int di_pos;
2037 PyObject* di_result; /* reusable result tuple for iteritems */
2038 long len;
2039 } dictiterobject;
2041 static PyObject *
2042 dictiter_new(dictobject *dict, PyTypeObject *itertype)
2044 dictiterobject *di;
2045 di = PyObject_New(dictiterobject, itertype);
2046 if (di == NULL)
2047 return NULL;
2048 Py_INCREF(dict);
2049 di->di_dict = dict;
2050 di->di_used = dict->ma_used;
2051 di->di_pos = 0;
2052 di->len = dict->ma_used;
2053 if (itertype == &PyDictIterItem_Type) {
2054 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2055 if (di->di_result == NULL) {
2056 Py_DECREF(di);
2057 return NULL;
2060 else
2061 di->di_result = NULL;
2062 return (PyObject *)di;
2065 static void
2066 dictiter_dealloc(dictiterobject *di)
2068 Py_XDECREF(di->di_dict);
2069 Py_XDECREF(di->di_result);
2070 PyObject_Del(di);
2073 static PyObject *
2074 dictiter_len(dictiterobject *di)
2076 long len = 0;
2077 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
2078 len = di->len;
2079 return PyInt_FromLong(len);
2082 PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
2084 static PyMethodDef dictiter_methods[] = {
2085 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS, length_hint_doc},
2086 {NULL, NULL} /* sentinel */
2089 static PyObject *dictiter_iternextkey(dictiterobject *di)
2091 PyObject *key;
2092 register int i, mask;
2093 register dictentry *ep;
2094 dictobject *d = di->di_dict;
2096 if (d == NULL)
2097 return NULL;
2098 assert (PyDict_Check(d));
2100 if (di->di_used != d->ma_used) {
2101 PyErr_SetString(PyExc_RuntimeError,
2102 "dictionary changed size during iteration");
2103 di->di_used = -1; /* Make this state sticky */
2104 return NULL;
2107 i = di->di_pos;
2108 if (i < 0)
2109 goto fail;
2110 ep = d->ma_table;
2111 mask = d->ma_mask;
2112 while (i <= mask && ep[i].me_value == NULL)
2113 i++;
2114 di->di_pos = i+1;
2115 if (i > mask)
2116 goto fail;
2117 di->len--;
2118 key = ep[i].me_key;
2119 Py_INCREF(key);
2120 return key;
2122 fail:
2123 Py_DECREF(d);
2124 di->di_dict = NULL;
2125 return NULL;
2128 PyTypeObject PyDictIterKey_Type = {
2129 PyObject_HEAD_INIT(&PyType_Type)
2130 0, /* ob_size */
2131 "dictionary-keyiterator", /* tp_name */
2132 sizeof(dictiterobject), /* tp_basicsize */
2133 0, /* tp_itemsize */
2134 /* methods */
2135 (destructor)dictiter_dealloc, /* tp_dealloc */
2136 0, /* tp_print */
2137 0, /* tp_getattr */
2138 0, /* tp_setattr */
2139 0, /* tp_compare */
2140 0, /* tp_repr */
2141 0, /* tp_as_number */
2142 0, /* tp_as_sequence */
2143 0, /* tp_as_mapping */
2144 0, /* tp_hash */
2145 0, /* tp_call */
2146 0, /* tp_str */
2147 PyObject_GenericGetAttr, /* tp_getattro */
2148 0, /* tp_setattro */
2149 0, /* tp_as_buffer */
2150 Py_TPFLAGS_DEFAULT, /* tp_flags */
2151 0, /* tp_doc */
2152 0, /* tp_traverse */
2153 0, /* tp_clear */
2154 0, /* tp_richcompare */
2155 0, /* tp_weaklistoffset */
2156 PyObject_SelfIter, /* tp_iter */
2157 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
2158 dictiter_methods, /* tp_methods */
2162 static PyObject *dictiter_iternextvalue(dictiterobject *di)
2164 PyObject *value;
2165 register int i, mask;
2166 register dictentry *ep;
2167 dictobject *d = di->di_dict;
2169 if (d == NULL)
2170 return NULL;
2171 assert (PyDict_Check(d));
2173 if (di->di_used != d->ma_used) {
2174 PyErr_SetString(PyExc_RuntimeError,
2175 "dictionary changed size during iteration");
2176 di->di_used = -1; /* Make this state sticky */
2177 return NULL;
2180 i = di->di_pos;
2181 mask = d->ma_mask;
2182 if (i < 0 || i > mask)
2183 goto fail;
2184 ep = d->ma_table;
2185 while ((value=ep[i].me_value) == NULL) {
2186 i++;
2187 if (i > mask)
2188 goto fail;
2190 di->di_pos = i+1;
2191 di->len--;
2192 Py_INCREF(value);
2193 return value;
2195 fail:
2196 Py_DECREF(d);
2197 di->di_dict = NULL;
2198 return NULL;
2201 PyTypeObject PyDictIterValue_Type = {
2202 PyObject_HEAD_INIT(&PyType_Type)
2203 0, /* ob_size */
2204 "dictionary-valueiterator", /* tp_name */
2205 sizeof(dictiterobject), /* tp_basicsize */
2206 0, /* tp_itemsize */
2207 /* methods */
2208 (destructor)dictiter_dealloc, /* tp_dealloc */
2209 0, /* tp_print */
2210 0, /* tp_getattr */
2211 0, /* tp_setattr */
2212 0, /* tp_compare */
2213 0, /* tp_repr */
2214 0, /* tp_as_number */
2215 0, /* tp_as_sequence */
2216 0, /* tp_as_mapping */
2217 0, /* tp_hash */
2218 0, /* tp_call */
2219 0, /* tp_str */
2220 PyObject_GenericGetAttr, /* tp_getattro */
2221 0, /* tp_setattro */
2222 0, /* tp_as_buffer */
2223 Py_TPFLAGS_DEFAULT, /* tp_flags */
2224 0, /* tp_doc */
2225 0, /* tp_traverse */
2226 0, /* tp_clear */
2227 0, /* tp_richcompare */
2228 0, /* tp_weaklistoffset */
2229 PyObject_SelfIter, /* tp_iter */
2230 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
2231 dictiter_methods, /* tp_methods */
2235 static PyObject *dictiter_iternextitem(dictiterobject *di)
2237 PyObject *key, *value, *result = di->di_result;
2238 register int i, mask;
2239 register dictentry *ep;
2240 dictobject *d = di->di_dict;
2242 if (d == NULL)
2243 return NULL;
2244 assert (PyDict_Check(d));
2246 if (di->di_used != d->ma_used) {
2247 PyErr_SetString(PyExc_RuntimeError,
2248 "dictionary changed size during iteration");
2249 di->di_used = -1; /* Make this state sticky */
2250 return NULL;
2253 i = di->di_pos;
2254 if (i < 0)
2255 goto fail;
2256 ep = d->ma_table;
2257 mask = d->ma_mask;
2258 while (i <= mask && ep[i].me_value == NULL)
2259 i++;
2260 di->di_pos = i+1;
2261 if (i > mask)
2262 goto fail;
2264 if (result->ob_refcnt == 1) {
2265 Py_INCREF(result);
2266 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2267 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2268 } else {
2269 result = PyTuple_New(2);
2270 if (result == NULL)
2271 return NULL;
2273 di->len--;
2274 key = ep[i].me_key;
2275 value = ep[i].me_value;
2276 Py_INCREF(key);
2277 Py_INCREF(value);
2278 PyTuple_SET_ITEM(result, 0, key);
2279 PyTuple_SET_ITEM(result, 1, value);
2280 return result;
2282 fail:
2283 Py_DECREF(d);
2284 di->di_dict = NULL;
2285 return NULL;
2288 PyTypeObject PyDictIterItem_Type = {
2289 PyObject_HEAD_INIT(&PyType_Type)
2290 0, /* ob_size */
2291 "dictionary-itemiterator", /* tp_name */
2292 sizeof(dictiterobject), /* tp_basicsize */
2293 0, /* tp_itemsize */
2294 /* methods */
2295 (destructor)dictiter_dealloc, /* tp_dealloc */
2296 0, /* tp_print */
2297 0, /* tp_getattr */
2298 0, /* tp_setattr */
2299 0, /* tp_compare */
2300 0, /* tp_repr */
2301 0, /* tp_as_number */
2302 0, /* tp_as_sequence */
2303 0, /* tp_as_mapping */
2304 0, /* tp_hash */
2305 0, /* tp_call */
2306 0, /* tp_str */
2307 PyObject_GenericGetAttr, /* tp_getattro */
2308 0, /* tp_setattro */
2309 0, /* tp_as_buffer */
2310 Py_TPFLAGS_DEFAULT, /* tp_flags */
2311 0, /* tp_doc */
2312 0, /* tp_traverse */
2313 0, /* tp_clear */
2314 0, /* tp_richcompare */
2315 0, /* tp_weaklistoffset */
2316 PyObject_SelfIter, /* tp_iter */
2317 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
2318 dictiter_methods, /* tp_methods */