source: vendor/python/2.5/Objects/dictobject.c

Last change on this file was 3225, checked in by bird, 18 years ago

Python 2.5

File size: 61.2 KB
Line 
1
2/* Dictionary object implementation using a hash table */
3
4/* The distribution includes a separate file, Objects/dictnotes.txt,
5 describing explorations into dictionary design and optimization.
6 It covers typical dictionary use patterns, the parameters for
7 tuning dictionaries, and several ideas for possible optimizations.
8*/
9
10#include "Python.h"
11
12typedef PyDictEntry dictentry;
13typedef PyDictObject dictobject;
14
15/* Define this out if you don't want conversion statistics on exit. */
16#undef SHOW_CONVERSION_COUNTS
17
18/* See large comment block below. This must be >= 1. */
19#define PERTURB_SHIFT 5
20
21/*
22Major subtleties ahead: Most hash schemes depend on having a "good" hash
23function, in the sense of simulating randomness. Python doesn't: its most
24important hash functions (for strings and ints) are very regular in common
25cases:
26
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>>>
32
33This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
34the low-order i bits as the initial table index is extremely fast, and there
35are no collisions at all for dicts indexed by a contiguous range of ints.
36The same is approximately true when keys are "consecutive" strings. So this
37gives better-than-random behavior in common cases, and that's very desirable.
38
39OTOH, when collisions occur, the tendency to fill contiguous slices of the
40hash table makes a good collision resolution strategy crucial. Taking only
41the 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
43hash codes, and this fits in a dict of size 2**15, the last 15 bits of every
44hash code are all 0: they *all* map to the same table index.
45
46But catering to unusual cases should not slow the usual ones, so we just take
47the last i bits anyway. It's up to collision resolution to do the rest. If
48we *usually* find the key we're looking for on the first try (and, it turns
49out, we usually do -- the table load factor is kept under 2/3, so the odds
50are solidly in our favor), then it makes best sense to keep the initial index
51computation dirt cheap.
52
53The first half of collision resolution is to visit table indices via this
54recurrence:
55
56 j = ((5*j) + 1) mod 2**i
57
58For any initial j in range(2**i), repeating that 2**i times generates each
59int in range(2**i) exactly once (see any text on random-number generation for
60proof). By itself, this doesn't help much: like linear probing (setting
61j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
62order. This would be bad, except that's not the only thing we do, and it's
63actually *good* in the common cases where hash keys are consecutive. In an
64example that's really too small to make this entirely clear, for a table of
65size 2**3 the order of indices is:
66
67 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
68
69If two things come in at index 5, the first place we look after is index 2,
70not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
71Linear probing is deadly in this case because there the fixed probe order
72is the *same* as the order consecutive keys are likely to arrive. But it's
73extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
74and certain that consecutive hash codes do not.
75
76The other half of the strategy is to get the other bits of the hash code
77into play. This is done by initializing a (unsigned) vrbl "perturb" to the
78full hash code, and changing the recurrence to:
79
80 j = (5*j) + 1 + perturb;
81 perturb >>= PERTURB_SHIFT;
82 use j % 2**i as the next table index;
83
84Now the probe sequence depends (eventually) on every bit in the hash code,
85and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
86because it quickly magnifies small differences in the bits that didn't affect
87the initial index. Note that because perturb is unsigned, if the recurrence
88is executed often enough perturb eventually becomes and remains 0. At that
89point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
90that's certain to find an empty slot eventually (since it generates every int
91in range(2**i), and we make sure there's always at least one empty slot).
92
93Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
94small so that the high bits of the hash code continue to affect the probe
95sequence across iterations; but you want it large so that in really bad cases
96the high-order hash bits have an effect on early iterations. 5 was "the
97best" in minimizing total collisions across experiments Tim Peters ran (on
98both normal and pathological cases), but 4 and 6 weren't significantly worse.
99
100Historical: Reimer Behrends contributed the idea of using a polynomial-based
101approach, using repeated multiplication by x in GF(2**n) where an irreducible
102polynomial for each table size was chosen such that x was a primitive root.
103Christian Tismer later extended that to use division by x instead, as an
104efficient way to get the high bits of the hash code into play. This scheme
105also gave excellent collision statistics, but was more expensive: two
106if-tests were required inside the loop; computing "the next" index took about
107the 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
109above, and then shifting perturb can be done while the table index is being
110masked); and the dictobject struct required a member to hold the table's
111polynomial. In Tim's experiments the current scheme ran faster, produced
112equally good collision statistics, needed less code & used less memory.
113
114Theoretical Python 2.5 headache: hash codes are only C "long", but
115sizeof(Py_ssize_t) > sizeof(long) may be possible. In that case, and if a
116dict is genuinely huge, then only the slots directly reachable via indexing
117by a C long can be the first slot in a probe sequence. The probe sequence
118will still eventually reach every slot in the table, but the collision rate
119on initial probes may be much higher than this scheme was designed for.
120Getting a hash code as fat as Py_ssize_t is the only real cure. But in
121practice, this probably won't make a lick of difference for many years (at
122which point everyone will have terabytes of RAM on 64-bit boxes).
123*/
124
125/* Object used as dummy key to fill deleted entries */
126static PyObject *dummy = NULL; /* Initialized by first call to newdictobject() */
127
128#ifdef Py_REF_DEBUG
129PyObject *
130_PyDict_Dummy(void)
131{
132 return dummy;
133}
134#endif
135
136/* forward declarations */
137static dictentry *
138lookdict_string(dictobject *mp, PyObject *key, long hash);
139
140#ifdef SHOW_CONVERSION_COUNTS
141static long created = 0L;
142static long converted = 0L;
143
144static void
145show_counts(void)
146{
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);
150}
151#endif
152
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).
160*/
161
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)
166
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)
172
173/* Dictionary reuse scheme to save calls to malloc, free, and memset */
174#define MAXFREEDICTS 80
175static PyDictObject *free_dicts[MAXFREEDICTS];
176static int num_free_dicts = 0;
177
178PyObject *
179PyDict_New(void)
180{
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
189 }
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);
197 }
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);
206 }
207 mp->ma_lookup = lookdict_string;
208#ifdef SHOW_CONVERSION_COUNTS
209 ++created;
210#endif
211 _PyObject_GC_TRACK(mp);
212 return (PyObject *)mp;
213}
214
215/*
216The basic lookup function used by all operations.
217This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
218Open addressing is preferred over chaining since the link overhead for
219chaining would be substantial (100% with typical malloc overhead).
220
221The initial probe index is computed as hash mod the table size. Subsequent
222probe indices are computed as explained earlier.
223
224All arithmetic on hash should ignore overflow.
225
226(The details in this version are due to Tim Peters, building on many past
227contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
228Christian Tismer).
229
230lookdict() is general-purpose, and may return NULL if (and only if) a
231comparison raises an exception (this was new in Python 2.5).
232lookdict_string() below is specialized to string keys, comparison of which can
233never raise an exception; that function can never return NULL. For both, when
234the key isn't found a dictentry* is returned for which the me_value field is
235NULL; this is the slot in the dict at which the key would have been found, and
236the caller can (if it wishes) add the <key, value> pair to the returned
237dictentry*.
238*/
239static dictentry *
240lookdict(dictobject *mp, PyObject *key, register long hash)
241{
242 register size_t i;
243 register size_t perturb;
244 register dictentry *freeslot;
245 register size_t mask = (size_t)mp->ma_mask;
246 dictentry *ep0 = mp->ma_table;
247 register dictentry *ep;
248 register int cmp;
249 PyObject *startkey;
250
251 i = (size_t)hash & mask;
252 ep = &ep0[i];
253 if (ep->me_key == NULL || ep->me_key == key)
254 return ep;
255
256 if (ep->me_key == dummy)
257 freeslot = ep;
258 else {
259 if (ep->me_hash == hash) {
260 startkey = ep->me_key;
261 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
262 if (cmp < 0)
263 return NULL;
264 if (ep0 == mp->ma_table && ep->me_key == startkey) {
265 if (cmp > 0)
266 return ep;
267 }
268 else {
269 /* The compare did major nasty stuff to the
270 * dict: start over.
271 * XXX A clever adversary could prevent this
272 * XXX from terminating.
273 */
274 return lookdict(mp, key, hash);
275 }
276 }
277 freeslot = NULL;
278 }
279
280 /* In the loop, me_key == dummy is by far (factor of 100s) the
281 least likely outcome, so test for that last. */
282 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
283 i = (i << 2) + i + perturb + 1;
284 ep = &ep0[i & mask];
285 if (ep->me_key == NULL)
286 return freeslot == NULL ? ep : freeslot;
287 if (ep->me_key == key)
288 return ep;
289 if (ep->me_hash == hash && ep->me_key != dummy) {
290 startkey = ep->me_key;
291 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
292 if (cmp < 0)
293 return NULL;
294 if (ep0 == mp->ma_table && ep->me_key == startkey) {
295 if (cmp > 0)
296 return ep;
297 }
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.
303 */
304 return lookdict(mp, key, hash);
305 }
306 }
307 else if (ep->me_key == dummy && freeslot == NULL)
308 freeslot = ep;
309 }
310}
311
312/*
313 * Hacked up version of lookdict which can assume keys are always strings;
314 * this assumption allows testing for errors during PyObject_RichCompareBool()
315 * to be dropped; string-string comparisons never raise exceptions. This also
316 * means we don't need to go through PyObject_RichCompareBool(); we can always
317 * use _PyString_Eq() directly.
318 *
319 * This is valuable because dicts with only string keys are very common.
320 */
321static dictentry *
322lookdict_string(dictobject *mp, PyObject *key, register long hash)
323{
324 register size_t i;
325 register size_t perturb;
326 register dictentry *freeslot;
327 register size_t mask = (size_t)mp->ma_mask;
328 dictentry *ep0 = mp->ma_table;
329 register dictentry *ep;
330
331 /* Make sure this function doesn't have to handle non-string keys,
332 including subclasses of str; e.g., one reason to subclass
333 strings is to override __eq__, and for speed we don't cater to
334 that here. */
335 if (!PyString_CheckExact(key)) {
336#ifdef SHOW_CONVERSION_COUNTS
337 ++converted;
338#endif
339 mp->ma_lookup = lookdict;
340 return lookdict(mp, key, hash);
341 }
342 i = hash & mask;
343 ep = &ep0[i];
344 if (ep->me_key == NULL || ep->me_key == key)
345 return ep;
346 if (ep->me_key == dummy)
347 freeslot = ep;
348 else {
349 if (ep->me_hash == hash && _PyString_Eq(ep->me_key, key))
350 return ep;
351 freeslot = NULL;
352 }
353
354 /* In the loop, me_key == dummy is by far (factor of 100s) the
355 least likely outcome, so test for that last. */
356 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
357 i = (i << 2) + i + perturb + 1;
358 ep = &ep0[i & mask];
359 if (ep->me_key == NULL)
360 return freeslot == NULL ? ep : freeslot;
361 if (ep->me_key == key
362 || (ep->me_hash == hash
363 && ep->me_key != dummy
364 && _PyString_Eq(ep->me_key, key)))
365 return ep;
366 if (ep->me_key == dummy && freeslot == NULL)
367 freeslot = ep;
368 }
369}
370
371/*
372Internal routine to insert a new item into the table.
373Used both by the internal resize routine and by the public insert routine.
374Eats a reference to key and one to value.
375Returns -1 if an error occurred, or 0 on success.
376*/
377static int
378insertdict(register dictobject *mp, PyObject *key, long hash, PyObject *value)
379{
380 PyObject *old_value;
381 register dictentry *ep;
382 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
383
384 assert(mp->ma_lookup != NULL);
385 ep = mp->ma_lookup(mp, key, hash);
386 if (ep == NULL) {
387 Py_DECREF(key);
388 Py_DECREF(value);
389 return -1;
390 }
391 if (ep->me_value != NULL) {
392 old_value = ep->me_value;
393 ep->me_value = value;
394 Py_DECREF(old_value); /* which **CAN** re-enter */
395 Py_DECREF(key);
396 }
397 else {
398 if (ep->me_key == NULL)
399 mp->ma_fill++;
400 else {
401 assert(ep->me_key == dummy);
402 Py_DECREF(dummy);
403 }
404 ep->me_key = key;
405 ep->me_hash = (Py_ssize_t)hash;
406 ep->me_value = value;
407 mp->ma_used++;
408 }
409 return 0;
410}
411
412/*
413Internal routine used by dictresize() to insert an item which is
414known to be absent from the dict. This routine also assumes that
415the dict contains no deleted entries. Besides the performance benefit,
416using insertdict() in dictresize() is dangerous (SF bug #1456209).
417Note that no refcounts are changed by this routine; if needed, the caller
418is responsible for incref'ing `key` and `value`.
419*/
420static void
421insertdict_clean(register dictobject *mp, PyObject *key, long hash,
422 PyObject *value)
423{
424 register size_t i;
425 register size_t perturb;
426 register size_t mask = (size_t)mp->ma_mask;
427 dictentry *ep0 = mp->ma_table;
428 register dictentry *ep;
429
430 i = hash & mask;
431 ep = &ep0[i];
432 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
433 i = (i << 2) + i + perturb + 1;
434 ep = &ep0[i & mask];
435 }
436 assert(ep->me_value == NULL);
437 mp->ma_fill++;
438 ep->me_key = key;
439 ep->me_hash = (Py_ssize_t)hash;
440 ep->me_value = value;
441 mp->ma_used++;
442}
443
444/*
445Restructure the table by allocating a new table and reinserting all
446items again. When entries have been deleted, the new table may
447actually be smaller than the old one.
448*/
449static int
450dictresize(dictobject *mp, Py_ssize_t minused)
451{
452 Py_ssize_t newsize;
453 dictentry *oldtable, *newtable, *ep;
454 Py_ssize_t i;
455 int is_oldtable_malloced;
456 dictentry small_copy[PyDict_MINSIZE];
457
458 assert(minused >= 0);
459
460 /* Find the smallest table size > minused. */
461 for (newsize = PyDict_MINSIZE;
462 newsize <= minused && newsize > 0;
463 newsize <<= 1)
464 ;
465 if (newsize <= 0) {
466 PyErr_NoMemory();
467 return -1;
468 }
469
470 /* Get space for a new table. */
471 oldtable = mp->ma_table;
472 assert(oldtable != NULL);
473 is_oldtable_malloced = oldtable != mp->ma_smalltable;
474
475 if (newsize == PyDict_MINSIZE) {
476 /* A large table is shrinking, or we can't get any smaller. */
477 newtable = mp->ma_smalltable;
478 if (newtable == oldtable) {
479 if (mp->ma_fill == mp->ma_used) {
480 /* No dummies, so no point doing anything. */
481 return 0;
482 }
483 /* We're not going to resize it, but rebuild the
484 table anyway to purge old dummy entries.
485 Subtle: This is *necessary* if fill==size,
486 as lookdict needs at least one virgin slot to
487 terminate failing searches. If fill < size, it's
488 merely desirable, as dummies slow searches. */
489 assert(mp->ma_fill > mp->ma_used);
490 memcpy(small_copy, oldtable, sizeof(small_copy));
491 oldtable = small_copy;
492 }
493 }
494 else {
495 newtable = PyMem_NEW(dictentry, newsize);
496 if (newtable == NULL) {
497 PyErr_NoMemory();
498 return -1;
499 }
500 }
501
502 /* Make the dict empty, using the new table. */
503 assert(newtable != oldtable);
504 mp->ma_table = newtable;
505 mp->ma_mask = newsize - 1;
506 memset(newtable, 0, sizeof(dictentry) * newsize);
507 mp->ma_used = 0;
508 i = mp->ma_fill;
509 mp->ma_fill = 0;
510
511 /* Copy the data over; this is refcount-neutral for active entries;
512 dummy entries aren't copied over, of course */
513 for (ep = oldtable; i > 0; ep++) {
514 if (ep->me_value != NULL) { /* active entry */
515 --i;
516 insertdict_clean(mp, ep->me_key, (long)ep->me_hash,
517 ep->me_value);
518 }
519 else if (ep->me_key != NULL) { /* dummy entry */
520 --i;
521 assert(ep->me_key == dummy);
522 Py_DECREF(ep->me_key);
523 }
524 /* else key == value == NULL: nothing to do */
525 }
526
527 if (is_oldtable_malloced)
528 PyMem_DEL(oldtable);
529 return 0;
530}
531
532/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
533 * that may occur (originally dicts supported only string keys, and exceptions
534 * weren't possible). So, while the original intent was that a NULL return
535 * meant the key wasn't present, in reality it can mean that, or that an error
536 * (suppressed) occurred while computing the key's hash, or that some error
537 * (suppressed) occurred when comparing keys in the dict's internal probe
538 * sequence. A nasty example of the latter is when a Python-coded comparison
539 * function hits a stack-depth error, which can cause this to return NULL
540 * even if the key is present.
541 */
542PyObject *
543PyDict_GetItem(PyObject *op, PyObject *key)
544{
545 long hash;
546 dictobject *mp = (dictobject *)op;
547 dictentry *ep;
548 PyThreadState *tstate;
549 if (!PyDict_Check(op))
550 return NULL;
551 if (!PyString_CheckExact(key) ||
552 (hash = ((PyStringObject *) key)->ob_shash) == -1)
553 {
554 hash = PyObject_Hash(key);
555 if (hash == -1) {
556 PyErr_Clear();
557 return NULL;
558 }
559 }
560
561 /* We can arrive here with a NULL tstate during initialization:
562 try running "python -Wi" for an example related to string
563 interning. Let's just hope that no exception occurs then... */
564 tstate = _PyThreadState_Current;
565 if (tstate != NULL && tstate->curexc_type != NULL) {
566 /* preserve the existing exception */
567 PyObject *err_type, *err_value, *err_tb;
568 PyErr_Fetch(&err_type, &err_value, &err_tb);
569 ep = (mp->ma_lookup)(mp, key, hash);
570 /* ignore errors */
571 PyErr_Restore(err_type, err_value, err_tb);
572 if (ep == NULL)
573 return NULL;
574 }
575 else {
576 ep = (mp->ma_lookup)(mp, key, hash);
577 if (ep == NULL) {
578 PyErr_Clear();
579 return NULL;
580 }
581 }
582 return ep->me_value;
583}
584
585/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
586 * dictionary if it's merely replacing the value for an existing key.
587 * This means that it's safe to loop over a dictionary with PyDict_Next()
588 * and occasionally replace a value -- but you can't insert new keys or
589 * remove them.
590 */
591int
592PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
593{
594 register dictobject *mp;
595 register long hash;
596 register Py_ssize_t n_used;
597
598 if (!PyDict_Check(op)) {
599 PyErr_BadInternalCall();
600 return -1;
601 }
602 assert(key);
603 assert(value);
604 mp = (dictobject *)op;
605 if (PyString_CheckExact(key)) {
606 hash = ((PyStringObject *)key)->ob_shash;
607 if (hash == -1)
608 hash = PyObject_Hash(key);
609 }
610 else {
611 hash = PyObject_Hash(key);
612 if (hash == -1)
613 return -1;
614 }
615 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
616 n_used = mp->ma_used;
617 Py_INCREF(value);
618 Py_INCREF(key);
619 if (insertdict(mp, key, hash, value) != 0)
620 return -1;
621 /* If we added a key, we can safely resize. Otherwise just return!
622 * If fill >= 2/3 size, adjust size. Normally, this doubles or
623 * quaduples the size, but it's also possible for the dict to shrink
624 * (if ma_fill is much larger than ma_used, meaning a lot of dict
625 * keys have been * deleted).
626 *
627 * Quadrupling the size improves average dictionary sparseness
628 * (reducing collisions) at the cost of some memory and iteration
629 * speed (which loops over every possible entry). It also halves
630 * the number of expensive resize operations in a growing dictionary.
631 *
632 * Very large dictionaries (over 50K items) use doubling instead.
633 * This may help applications with severe memory constraints.
634 */
635 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
636 return 0;
637 return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
638}
639
640int
641PyDict_DelItem(PyObject *op, PyObject *key)
642{
643 register dictobject *mp;
644 register long hash;
645 register dictentry *ep;
646 PyObject *old_value, *old_key;
647
648 if (!PyDict_Check(op)) {
649 PyErr_BadInternalCall();
650 return -1;
651 }
652 assert(key);
653 if (!PyString_CheckExact(key) ||
654 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
655 hash = PyObject_Hash(key);
656 if (hash == -1)
657 return -1;
658 }
659 mp = (dictobject *)op;
660 ep = (mp->ma_lookup)(mp, key, hash);
661 if (ep == NULL)
662 return -1;
663 if (ep->me_value == NULL) {
664 PyErr_SetObject(PyExc_KeyError, key);
665 return -1;
666 }
667 old_key = ep->me_key;
668 Py_INCREF(dummy);
669 ep->me_key = dummy;
670 old_value = ep->me_value;
671 ep->me_value = NULL;
672 mp->ma_used--;
673 Py_DECREF(old_value);
674 Py_DECREF(old_key);
675 return 0;
676}
677
678void
679PyDict_Clear(PyObject *op)
680{
681 dictobject *mp;
682 dictentry *ep, *table;
683 int table_is_malloced;
684 Py_ssize_t fill;
685 dictentry small_copy[PyDict_MINSIZE];
686#ifdef Py_DEBUG
687 Py_ssize_t i, n;
688#endif
689
690 if (!PyDict_Check(op))
691 return;
692 mp = (dictobject *)op;
693#ifdef Py_DEBUG
694 n = mp->ma_mask + 1;
695 i = 0;
696#endif
697
698 table = mp->ma_table;
699 assert(table != NULL);
700 table_is_malloced = table != mp->ma_smalltable;
701
702 /* This is delicate. During the process of clearing the dict,
703 * decrefs can cause the dict to mutate. To avoid fatal confusion
704 * (voice of experience), we have to make the dict empty before
705 * clearing the slots, and never refer to anything via mp->xxx while
706 * clearing.
707 */
708 fill = mp->ma_fill;
709 if (table_is_malloced)
710 EMPTY_TO_MINSIZE(mp);
711
712 else if (fill > 0) {
713 /* It's a small table with something that needs to be cleared.
714 * Afraid the only safe way is to copy the dict entries into
715 * another small table first.
716 */
717 memcpy(small_copy, table, sizeof(small_copy));
718 table = small_copy;
719 EMPTY_TO_MINSIZE(mp);
720 }
721 /* else it's a small table that's already empty */
722
723 /* Now we can finally clear things. If C had refcounts, we could
724 * assert that the refcount on table is 1 now, i.e. that this function
725 * has unique access to it, so decref side-effects can't alter it.
726 */
727 for (ep = table; fill > 0; ++ep) {
728#ifdef Py_DEBUG
729 assert(i < n);
730 ++i;
731#endif
732 if (ep->me_key) {
733 --fill;
734 Py_DECREF(ep->me_key);
735 Py_XDECREF(ep->me_value);
736 }
737#ifdef Py_DEBUG
738 else
739 assert(ep->me_value == NULL);
740#endif
741 }
742
743 if (table_is_malloced)
744 PyMem_DEL(table);
745}
746
747/*
748 * Iterate over a dict. Use like so:
749 *
750 * Py_ssize_t i;
751 * PyObject *key, *value;
752 * i = 0; # important! i should not otherwise be changed by you
753 * while (PyDict_Next(yourdict, &i, &key, &value)) {
754 * Refer to borrowed references in key and value.
755 * }
756 *
757 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
758 * mutates the dict. One exception: it is safe if the loop merely changes
759 * the values associated with the keys (but doesn't insert new keys or
760 * delete keys), via PyDict_SetItem().
761 */
762int
763PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
764{
765 register Py_ssize_t i;
766 register Py_ssize_t mask;
767 register dictentry *ep;
768
769 if (!PyDict_Check(op))
770 return 0;
771 i = *ppos;
772 if (i < 0)
773 return 0;
774 ep = ((dictobject *)op)->ma_table;
775 mask = ((dictobject *)op)->ma_mask;
776 while (i <= mask && ep[i].me_value == NULL)
777 i++;
778 *ppos = i+1;
779 if (i > mask)
780 return 0;
781 if (pkey)
782 *pkey = ep[i].me_key;
783 if (pvalue)
784 *pvalue = ep[i].me_value;
785 return 1;
786}
787
788/* Methods */
789
790static void
791dict_dealloc(register dictobject *mp)
792{
793 register dictentry *ep;
794 Py_ssize_t fill = mp->ma_fill;
795 PyObject_GC_UnTrack(mp);
796 Py_TRASHCAN_SAFE_BEGIN(mp)
797 for (ep = mp->ma_table; fill > 0; ep++) {
798 if (ep->me_key) {
799 --fill;
800 Py_DECREF(ep->me_key);
801 Py_XDECREF(ep->me_value);
802 }
803 }
804 if (mp->ma_table != mp->ma_smalltable)
805 PyMem_DEL(mp->ma_table);
806 if (num_free_dicts < MAXFREEDICTS && mp->ob_type == &PyDict_Type)
807 free_dicts[num_free_dicts++] = mp;
808 else
809 mp->ob_type->tp_free((PyObject *)mp);
810 Py_TRASHCAN_SAFE_END(mp)
811}
812
813static int
814dict_print(register dictobject *mp, register FILE *fp, register int flags)
815{
816 register Py_ssize_t i;
817 register Py_ssize_t any;
818 int status;
819
820 status = Py_ReprEnter((PyObject*)mp);
821 if (status != 0) {
822 if (status < 0)
823 return status;
824 fprintf(fp, "{...}");
825 return 0;
826 }
827
828 fprintf(fp, "{");
829 any = 0;
830 for (i = 0; i <= mp->ma_mask; i++) {
831 dictentry *ep = mp->ma_table + i;
832 PyObject *pvalue = ep->me_value;
833 if (pvalue != NULL) {
834 /* Prevent PyObject_Repr from deleting value during
835 key format */
836 Py_INCREF(pvalue);
837 if (any++ > 0)
838 fprintf(fp, ", ");
839 if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) {
840 Py_DECREF(pvalue);
841 Py_ReprLeave((PyObject*)mp);
842 return -1;
843 }
844 fprintf(fp, ": ");
845 if (PyObject_Print(pvalue, fp, 0) != 0) {
846 Py_DECREF(pvalue);
847 Py_ReprLeave((PyObject*)mp);
848 return -1;
849 }
850 Py_DECREF(pvalue);
851 }
852 }
853 fprintf(fp, "}");
854 Py_ReprLeave((PyObject*)mp);
855 return 0;
856}
857
858static PyObject *
859dict_repr(dictobject *mp)
860{
861 Py_ssize_t i;
862 PyObject *s, *temp, *colon = NULL;
863 PyObject *pieces = NULL, *result = NULL;
864 PyObject *key, *value;
865
866 i = Py_ReprEnter((PyObject *)mp);
867 if (i != 0) {
868 return i > 0 ? PyString_FromString("{...}") : NULL;
869 }
870
871 if (mp->ma_used == 0) {
872 result = PyString_FromString("{}");
873 goto Done;
874 }
875
876 pieces = PyList_New(0);
877 if (pieces == NULL)
878 goto Done;
879
880 colon = PyString_FromString(": ");
881 if (colon == NULL)
882 goto Done;
883
884 /* Do repr() on each key+value pair, and insert ": " between them.
885 Note that repr may mutate the dict. */
886 i = 0;
887 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
888 int status;
889 /* Prevent repr from deleting value during key format. */
890 Py_INCREF(value);
891 s = PyObject_Repr(key);
892 PyString_Concat(&s, colon);
893 PyString_ConcatAndDel(&s, PyObject_Repr(value));
894 Py_DECREF(value);
895 if (s == NULL)
896 goto Done;
897 status = PyList_Append(pieces, s);
898 Py_DECREF(s); /* append created a new ref */
899 if (status < 0)
900 goto Done;
901 }
902
903 /* Add "{}" decorations to the first and last items. */
904 assert(PyList_GET_SIZE(pieces) > 0);
905 s = PyString_FromString("{");
906 if (s == NULL)
907 goto Done;
908 temp = PyList_GET_ITEM(pieces, 0);
909 PyString_ConcatAndDel(&s, temp);
910 PyList_SET_ITEM(pieces, 0, s);
911 if (s == NULL)
912 goto Done;
913
914 s = PyString_FromString("}");
915 if (s == NULL)
916 goto Done;
917 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
918 PyString_ConcatAndDel(&temp, s);
919 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
920 if (temp == NULL)
921 goto Done;
922
923 /* Paste them all together with ", " between. */
924 s = PyString_FromString(", ");
925 if (s == NULL)
926 goto Done;
927 result = _PyString_Join(s, pieces);
928 Py_DECREF(s);
929
930Done:
931 Py_XDECREF(pieces);
932 Py_XDECREF(colon);
933 Py_ReprLeave((PyObject *)mp);
934 return result;
935}
936
937static Py_ssize_t
938dict_length(dictobject *mp)
939{
940 return mp->ma_used;
941}
942
943static PyObject *
944dict_subscript(dictobject *mp, register PyObject *key)
945{
946 PyObject *v;
947 long hash;
948 dictentry *ep;
949 assert(mp->ma_table != NULL);
950 if (!PyString_CheckExact(key) ||
951 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
952 hash = PyObject_Hash(key);
953 if (hash == -1)
954 return NULL;
955 }
956 ep = (mp->ma_lookup)(mp, key, hash);
957 if (ep == NULL)
958 return NULL;
959 v = ep->me_value;
960 if (v == NULL) {
961 if (!PyDict_CheckExact(mp)) {
962 /* Look up __missing__ method if we're a subclass. */
963 PyObject *missing;
964 static PyObject *missing_str = NULL;
965 if (missing_str == NULL)
966 missing_str =
967 PyString_InternFromString("__missing__");
968 missing = _PyType_Lookup(mp->ob_type, missing_str);
969 if (missing != NULL)
970 return PyObject_CallFunctionObjArgs(missing,
971 (PyObject *)mp, key, NULL);
972 }
973 PyErr_SetObject(PyExc_KeyError, key);
974 return NULL;
975 }
976 else
977 Py_INCREF(v);
978 return v;
979}
980
981static int
982dict_ass_sub(dictobject *mp, PyObject *v, PyObject *w)
983{
984 if (w == NULL)
985 return PyDict_DelItem((PyObject *)mp, v);
986 else
987 return PyDict_SetItem((PyObject *)mp, v, w);
988}
989
990static PyMappingMethods dict_as_mapping = {
991 (lenfunc)dict_length, /*mp_length*/
992 (binaryfunc)dict_subscript, /*mp_subscript*/
993 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
994};
995
996static PyObject *
997dict_keys(register dictobject *mp)
998{
999 register PyObject *v;
1000 register Py_ssize_t i, j;
1001 dictentry *ep;
1002 Py_ssize_t mask, n;
1003
1004 again:
1005 n = mp->ma_used;
1006 v = PyList_New(n);
1007 if (v == NULL)
1008 return NULL;
1009 if (n != mp->ma_used) {
1010 /* Durnit. The allocations caused the dict to resize.
1011 * Just start over, this shouldn't normally happen.
1012 */
1013 Py_DECREF(v);
1014 goto again;
1015 }
1016 ep = mp->ma_table;
1017 mask = mp->ma_mask;
1018 for (i = 0, j = 0; i <= mask; i++) {
1019 if (ep[i].me_value != NULL) {
1020 PyObject *key = ep[i].me_key;
1021 Py_INCREF(key);
1022 PyList_SET_ITEM(v, j, key);
1023 j++;
1024 }
1025 }
1026 assert(j == n);
1027 return v;
1028}
1029
1030static PyObject *
1031dict_values(register dictobject *mp)
1032{
1033 register PyObject *v;
1034 register Py_ssize_t i, j;
1035 dictentry *ep;
1036 Py_ssize_t mask, n;
1037
1038 again:
1039 n = mp->ma_used;
1040 v = PyList_New(n);
1041 if (v == NULL)
1042 return NULL;
1043 if (n != mp->ma_used) {
1044 /* Durnit. The allocations caused the dict to resize.
1045 * Just start over, this shouldn't normally happen.
1046 */
1047 Py_DECREF(v);
1048 goto again;
1049 }
1050 ep = mp->ma_table;
1051 mask = mp->ma_mask;
1052 for (i = 0, j = 0; i <= mask; i++) {
1053 if (ep[i].me_value != NULL) {
1054 PyObject *value = ep[i].me_value;
1055 Py_INCREF(value);
1056 PyList_SET_ITEM(v, j, value);
1057 j++;
1058 }
1059 }
1060 assert(j == n);
1061 return v;
1062}
1063
1064static PyObject *
1065dict_items(register dictobject *mp)
1066{
1067 register PyObject *v;
1068 register Py_ssize_t i, j, n;
1069 Py_ssize_t mask;
1070 PyObject *item, *key, *value;
1071 dictentry *ep;
1072
1073 /* Preallocate the list of tuples, to avoid allocations during
1074 * the loop over the items, which could trigger GC, which
1075 * could resize the dict. :-(
1076 */
1077 again:
1078 n = mp->ma_used;
1079 v = PyList_New(n);
1080 if (v == NULL)
1081 return NULL;
1082 for (i = 0; i < n; i++) {
1083 item = PyTuple_New(2);
1084 if (item == NULL) {
1085 Py_DECREF(v);
1086 return NULL;
1087 }
1088 PyList_SET_ITEM(v, i, item);
1089 }
1090 if (n != mp->ma_used) {
1091 /* Durnit. The allocations caused the dict to resize.
1092 * Just start over, this shouldn't normally happen.
1093 */
1094 Py_DECREF(v);
1095 goto again;
1096 }
1097 /* Nothing we do below makes any function calls. */
1098 ep = mp->ma_table;
1099 mask = mp->ma_mask;
1100 for (i = 0, j = 0; i <= mask; i++) {
1101 if ((value=ep[i].me_value) != NULL) {
1102 key = ep[i].me_key;
1103 item = PyList_GET_ITEM(v, j);
1104 Py_INCREF(key);
1105 PyTuple_SET_ITEM(item, 0, key);
1106 Py_INCREF(value);
1107 PyTuple_SET_ITEM(item, 1, value);
1108 j++;
1109 }
1110 }
1111 assert(j == n);
1112 return v;
1113}
1114
1115static PyObject *
1116dict_fromkeys(PyObject *cls, PyObject *args)
1117{
1118 PyObject *seq;
1119 PyObject *value = Py_None;
1120 PyObject *it; /* iter(seq) */
1121 PyObject *key;
1122 PyObject *d;
1123 int status;
1124
1125 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
1126 return NULL;
1127
1128 d = PyObject_CallObject(cls, NULL);
1129 if (d == NULL)
1130 return NULL;
1131
1132 it = PyObject_GetIter(seq);
1133 if (it == NULL){
1134 Py_DECREF(d);
1135 return NULL;
1136 }
1137
1138 for (;;) {
1139 key = PyIter_Next(it);
1140 if (key == NULL) {
1141 if (PyErr_Occurred())
1142 goto Fail;
1143 break;
1144 }
1145 status = PyObject_SetItem(d, key, value);
1146 Py_DECREF(key);
1147 if (status < 0)
1148 goto Fail;
1149 }
1150
1151 Py_DECREF(it);
1152 return d;
1153
1154Fail:
1155 Py_DECREF(it);
1156 Py_DECREF(d);
1157 return NULL;
1158}
1159
1160static int
1161dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
1162{
1163 PyObject *arg = NULL;
1164 int result = 0;
1165
1166 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1167 result = -1;
1168
1169 else if (arg != NULL) {
1170 if (PyObject_HasAttrString(arg, "keys"))
1171 result = PyDict_Merge(self, arg, 1);
1172 else
1173 result = PyDict_MergeFromSeq2(self, arg, 1);
1174 }
1175 if (result == 0 && kwds != NULL)
1176 result = PyDict_Merge(self, kwds, 1);
1177 return result;
1178}
1179
1180static PyObject *
1181dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1182{
1183 if (dict_update_common(self, args, kwds, "update") != -1)
1184 Py_RETURN_NONE;
1185 return NULL;
1186}
1187
1188/* Update unconditionally replaces existing items.
1189 Merge has a 3rd argument 'override'; if set, it acts like Update,
1190 otherwise it leaves existing items unchanged.
1191
1192 PyDict_{Update,Merge} update/merge from a mapping object.
1193
1194 PyDict_MergeFromSeq2 updates/merges from any iterable object
1195 producing iterable objects of length 2.
1196*/
1197
1198int
1199PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1200{
1201 PyObject *it; /* iter(seq2) */
1202 Py_ssize_t i; /* index into seq2 of current element */
1203 PyObject *item; /* seq2[i] */
1204 PyObject *fast; /* item as a 2-tuple or 2-list */
1205
1206 assert(d != NULL);
1207 assert(PyDict_Check(d));
1208 assert(seq2 != NULL);
1209
1210 it = PyObject_GetIter(seq2);
1211 if (it == NULL)
1212 return -1;
1213
1214 for (i = 0; ; ++i) {
1215 PyObject *key, *value;
1216 Py_ssize_t n;
1217
1218 fast = NULL;
1219 item = PyIter_Next(it);
1220 if (item == NULL) {
1221 if (PyErr_Occurred())
1222 goto Fail;
1223 break;
1224 }
1225
1226 /* Convert item to sequence, and verify length 2. */
1227 fast = PySequence_Fast(item, "");
1228 if (fast == NULL) {
1229 if (PyErr_ExceptionMatches(PyExc_TypeError))
1230 PyErr_Format(PyExc_TypeError,
1231 "cannot convert dictionary update "
1232 "sequence element #%zd to a sequence",
1233 i);
1234 goto Fail;
1235 }
1236 n = PySequence_Fast_GET_SIZE(fast);
1237 if (n != 2) {
1238 PyErr_Format(PyExc_ValueError,
1239 "dictionary update sequence element #%zd "
1240 "has length %zd; 2 is required",
1241 i, n);
1242 goto Fail;
1243 }
1244
1245 /* Update/merge with this (key, value) pair. */
1246 key = PySequence_Fast_GET_ITEM(fast, 0);
1247 value = PySequence_Fast_GET_ITEM(fast, 1);
1248 if (override || PyDict_GetItem(d, key) == NULL) {
1249 int status = PyDict_SetItem(d, key, value);
1250 if (status < 0)
1251 goto Fail;
1252 }
1253 Py_DECREF(fast);
1254 Py_DECREF(item);
1255 }
1256
1257 i = 0;
1258 goto Return;
1259Fail:
1260 Py_XDECREF(item);
1261 Py_XDECREF(fast);
1262 i = -1;
1263Return:
1264 Py_DECREF(it);
1265 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
1266}
1267
1268int
1269PyDict_Update(PyObject *a, PyObject *b)
1270{
1271 return PyDict_Merge(a, b, 1);
1272}
1273
1274int
1275PyDict_Merge(PyObject *a, PyObject *b, int override)
1276{
1277 register PyDictObject *mp, *other;
1278 register Py_ssize_t i;
1279 dictentry *entry;
1280
1281 /* We accept for the argument either a concrete dictionary object,
1282 * or an abstract "mapping" object. For the former, we can do
1283 * things quite efficiently. For the latter, we only require that
1284 * PyMapping_Keys() and PyObject_GetItem() be supported.
1285 */
1286 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1287 PyErr_BadInternalCall();
1288 return -1;
1289 }
1290 mp = (dictobject*)a;
1291 if (PyDict_Check(b)) {
1292 other = (dictobject*)b;
1293 if (other == mp || other->ma_used == 0)
1294 /* a.update(a) or a.update({}); nothing to do */
1295 return 0;
1296 if (mp->ma_used == 0)
1297 /* Since the target dict is empty, PyDict_GetItem()
1298 * always returns NULL. Setting override to 1
1299 * skips the unnecessary test.
1300 */
1301 override = 1;
1302 /* Do one big resize at the start, rather than
1303 * incrementally resizing as we insert new items. Expect
1304 * that there will be no (or few) overlapping keys.
1305 */
1306 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
1307 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
1308 return -1;
1309 }
1310 for (i = 0; i <= other->ma_mask; i++) {
1311 entry = &other->ma_table[i];
1312 if (entry->me_value != NULL &&
1313 (override ||
1314 PyDict_GetItem(a, entry->me_key) == NULL)) {
1315 Py_INCREF(entry->me_key);
1316 Py_INCREF(entry->me_value);
1317 if (insertdict(mp, entry->me_key,
1318 (long)entry->me_hash,
1319 entry->me_value) != 0)
1320 return -1;
1321 }
1322 }
1323 }
1324 else {
1325 /* Do it the generic, slower way */
1326 PyObject *keys = PyMapping_Keys(b);
1327 PyObject *iter;
1328 PyObject *key, *value;
1329 int status;
1330
1331 if (keys == NULL)
1332 /* Docstring says this is equivalent to E.keys() so
1333 * if E doesn't have a .keys() method we want
1334 * AttributeError to percolate up. Might as well
1335 * do the same for any other error.
1336 */
1337 return -1;
1338
1339 iter = PyObject_GetIter(keys);
1340 Py_DECREF(keys);
1341 if (iter == NULL)
1342 return -1;
1343
1344 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
1345 if (!override && PyDict_GetItem(a, key) != NULL) {
1346 Py_DECREF(key);
1347 continue;
1348 }
1349 value = PyObject_GetItem(b, key);
1350 if (value == NULL) {
1351 Py_DECREF(iter);
1352 Py_DECREF(key);
1353 return -1;
1354 }
1355 status = PyDict_SetItem(a, key, value);
1356 Py_DECREF(key);
1357 Py_DECREF(value);
1358 if (status < 0) {
1359 Py_DECREF(iter);
1360 return -1;
1361 }
1362 }
1363 Py_DECREF(iter);
1364 if (PyErr_Occurred())
1365 /* Iterator completed, via error */
1366 return -1;
1367 }
1368 return 0;
1369}
1370
1371static PyObject *
1372dict_copy(register dictobject *mp)
1373{
1374 return PyDict_Copy((PyObject*)mp);
1375}
1376
1377PyObject *
1378PyDict_Copy(PyObject *o)
1379{
1380 PyObject *copy;
1381
1382 if (o == NULL || !PyDict_Check(o)) {
1383 PyErr_BadInternalCall();
1384 return NULL;
1385 }
1386 copy = PyDict_New();
1387 if (copy == NULL)
1388 return NULL;
1389 if (PyDict_Merge(copy, o, 1) == 0)
1390 return copy;
1391 Py_DECREF(copy);
1392 return NULL;
1393}
1394
1395Py_ssize_t
1396PyDict_Size(PyObject *mp)
1397{
1398 if (mp == NULL || !PyDict_Check(mp)) {
1399 PyErr_BadInternalCall();
1400 return -1;
1401 }
1402 return ((dictobject *)mp)->ma_used;
1403}
1404
1405PyObject *
1406PyDict_Keys(PyObject *mp)
1407{
1408 if (mp == NULL || !PyDict_Check(mp)) {
1409 PyErr_BadInternalCall();
1410 return NULL;
1411 }
1412 return dict_keys((dictobject *)mp);
1413}
1414
1415PyObject *
1416PyDict_Values(PyObject *mp)
1417{
1418 if (mp == NULL || !PyDict_Check(mp)) {
1419 PyErr_BadInternalCall();
1420 return NULL;
1421 }
1422 return dict_values((dictobject *)mp);
1423}
1424
1425PyObject *
1426PyDict_Items(PyObject *mp)
1427{
1428 if (mp == NULL || !PyDict_Check(mp)) {
1429 PyErr_BadInternalCall();
1430 return NULL;
1431 }
1432 return dict_items((dictobject *)mp);
1433}
1434
1435/* Subroutine which returns the smallest key in a for which b's value
1436 is different or absent. The value is returned too, through the
1437 pval argument. Both are NULL if no key in a is found for which b's status
1438 differs. The refcounts on (and only on) non-NULL *pval and function return
1439 values must be decremented by the caller (characterize() increments them
1440 to ensure that mutating comparison and PyDict_GetItem calls can't delete
1441 them before the caller is done looking at them). */
1442
1443static PyObject *
1444characterize(dictobject *a, dictobject *b, PyObject **pval)
1445{
1446 PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */
1447 PyObject *aval = NULL; /* a[akey] */
1448 Py_ssize_t i;
1449 int cmp;
1450
1451 for (i = 0; i <= a->ma_mask; i++) {
1452 PyObject *thiskey, *thisaval, *thisbval;
1453 if (a->ma_table[i].me_value == NULL)
1454 continue;
1455 thiskey = a->ma_table[i].me_key;
1456 Py_INCREF(thiskey); /* keep alive across compares */
1457 if (akey != NULL) {
1458 cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT);
1459 if (cmp < 0) {
1460 Py_DECREF(thiskey);
1461 goto Fail;
1462 }
1463 if (cmp > 0 ||
1464 i > a->ma_mask ||
1465 a->ma_table[i].me_value == NULL)
1466 {
1467 /* Not the *smallest* a key; or maybe it is
1468 * but the compare shrunk the dict so we can't
1469 * find its associated value anymore; or
1470 * maybe it is but the compare deleted the
1471 * a[thiskey] entry.
1472 */
1473 Py_DECREF(thiskey);
1474 continue;
1475 }
1476 }
1477
1478 /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */
1479 thisaval = a->ma_table[i].me_value;
1480 assert(thisaval);
1481 Py_INCREF(thisaval); /* keep alive */
1482 thisbval = PyDict_GetItem((PyObject *)b, thiskey);
1483 if (thisbval == NULL)
1484 cmp = 0;
1485 else {
1486 /* both dicts have thiskey: same values? */
1487 cmp = PyObject_RichCompareBool(
1488 thisaval, thisbval, Py_EQ);
1489 if (cmp < 0) {
1490 Py_DECREF(thiskey);
1491 Py_DECREF(thisaval);
1492 goto Fail;
1493 }
1494 }
1495 if (cmp == 0) {
1496 /* New winner. */
1497 Py_XDECREF(akey);
1498 Py_XDECREF(aval);
1499 akey = thiskey;
1500 aval = thisaval;
1501 }
1502 else {
1503 Py_DECREF(thiskey);
1504 Py_DECREF(thisaval);
1505 }
1506 }
1507 *pval = aval;
1508 return akey;
1509
1510Fail:
1511 Py_XDECREF(akey);
1512 Py_XDECREF(aval);
1513 *pval = NULL;
1514 return NULL;
1515}
1516
1517static int
1518dict_compare(dictobject *a, dictobject *b)
1519{
1520 PyObject *adiff, *bdiff, *aval, *bval;
1521 int res;
1522
1523 /* Compare lengths first */
1524 if (a->ma_used < b->ma_used)
1525 return -1; /* a is shorter */
1526 else if (a->ma_used > b->ma_used)
1527 return 1; /* b is shorter */
1528
1529 /* Same length -- check all keys */
1530 bdiff = bval = NULL;
1531 adiff = characterize(a, b, &aval);
1532 if (adiff == NULL) {
1533 assert(!aval);
1534 /* Either an error, or a is a subset with the same length so
1535 * must be equal.
1536 */
1537 res = PyErr_Occurred() ? -1 : 0;
1538 goto Finished;
1539 }
1540 bdiff = characterize(b, a, &bval);
1541 if (bdiff == NULL && PyErr_Occurred()) {
1542 assert(!bval);
1543 res = -1;
1544 goto Finished;
1545 }
1546 res = 0;
1547 if (bdiff) {
1548 /* bdiff == NULL "should be" impossible now, but perhaps
1549 * the last comparison done by the characterize() on a had
1550 * the side effect of making the dicts equal!
1551 */
1552 res = PyObject_Compare(adiff, bdiff);
1553 }
1554 if (res == 0 && bval != NULL)
1555 res = PyObject_Compare(aval, bval);
1556
1557Finished:
1558 Py_XDECREF(adiff);
1559 Py_XDECREF(bdiff);
1560 Py_XDECREF(aval);
1561 Py_XDECREF(bval);
1562 return res;
1563}
1564
1565/* Return 1 if dicts equal, 0 if not, -1 if error.
1566 * Gets out as soon as any difference is detected.
1567 * Uses only Py_EQ comparison.
1568 */
1569static int
1570dict_equal(dictobject *a, dictobject *b)
1571{
1572 Py_ssize_t i;
1573
1574 if (a->ma_used != b->ma_used)
1575 /* can't be equal if # of entries differ */
1576 return 0;
1577
1578 /* Same # of entries -- check all of 'em. Exit early on any diff. */
1579 for (i = 0; i <= a->ma_mask; i++) {
1580 PyObject *aval = a->ma_table[i].me_value;
1581 if (aval != NULL) {
1582 int cmp;
1583 PyObject *bval;
1584 PyObject *key = a->ma_table[i].me_key;
1585 /* temporarily bump aval's refcount to ensure it stays
1586 alive until we're done with it */
1587 Py_INCREF(aval);
1588 /* ditto for key */
1589 Py_INCREF(key);
1590 bval = PyDict_GetItem((PyObject *)b, key);
1591 Py_DECREF(key);
1592 if (bval == NULL) {
1593 Py_DECREF(aval);
1594 return 0;
1595 }
1596 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1597 Py_DECREF(aval);
1598 if (cmp <= 0) /* error or not equal */
1599 return cmp;
1600 }
1601 }
1602 return 1;
1603 }
1604
1605static PyObject *
1606dict_richcompare(PyObject *v, PyObject *w, int op)
1607{
1608 int cmp;
1609 PyObject *res;
1610
1611 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1612 res = Py_NotImplemented;
1613 }
1614 else if (op == Py_EQ || op == Py_NE) {
1615 cmp = dict_equal((dictobject *)v, (dictobject *)w);
1616 if (cmp < 0)
1617 return NULL;
1618 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1619 }
1620 else
1621 res = Py_NotImplemented;
1622 Py_INCREF(res);
1623 return res;
1624 }
1625
1626static PyObject *
1627dict_has_key(register dictobject *mp, PyObject *key)
1628{
1629 long hash;
1630 dictentry *ep;
1631
1632 if (!PyString_CheckExact(key) ||
1633 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1634 hash = PyObject_Hash(key);
1635 if (hash == -1)
1636 return NULL;
1637 }
1638 ep = (mp->ma_lookup)(mp, key, hash);
1639 if (ep == NULL)
1640 return NULL;
1641 return PyBool_FromLong(ep->me_value != NULL);
1642}
1643
1644static PyObject *
1645dict_get(register dictobject *mp, PyObject *args)
1646{
1647 PyObject *key;
1648 PyObject *failobj = Py_None;
1649 PyObject *val = NULL;
1650 long hash;
1651 dictentry *ep;
1652
1653 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
1654 return NULL;
1655
1656 if (!PyString_CheckExact(key) ||
1657 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1658 hash = PyObject_Hash(key);
1659 if (hash == -1)
1660 return NULL;
1661 }
1662 ep = (mp->ma_lookup)(mp, key, hash);
1663 if (ep == NULL)
1664 return NULL;
1665 val = ep->me_value;
1666 if (val == NULL)
1667 val = failobj;
1668 Py_INCREF(val);
1669 return val;
1670}
1671
1672
1673static PyObject *
1674dict_setdefault(register dictobject *mp, PyObject *args)
1675{
1676 PyObject *key;
1677 PyObject *failobj = Py_None;
1678 PyObject *val = NULL;
1679 long hash;
1680 dictentry *ep;
1681
1682 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
1683 return NULL;
1684
1685 if (!PyString_CheckExact(key) ||
1686 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1687 hash = PyObject_Hash(key);
1688 if (hash == -1)
1689 return NULL;
1690 }
1691 ep = (mp->ma_lookup)(mp, key, hash);
1692 if (ep == NULL)
1693 return NULL;
1694 val = ep->me_value;
1695 if (val == NULL) {
1696 val = failobj;
1697 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1698 val = NULL;
1699 }
1700 Py_XINCREF(val);
1701 return val;
1702}
1703
1704
1705static PyObject *
1706dict_clear(register dictobject *mp)
1707{
1708 PyDict_Clear((PyObject *)mp);
1709 Py_RETURN_NONE;
1710}
1711
1712static PyObject *
1713dict_pop(dictobject *mp, PyObject *args)
1714{
1715 long hash;
1716 dictentry *ep;
1717 PyObject *old_value, *old_key;
1718 PyObject *key, *deflt = NULL;
1719
1720 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1721 return NULL;
1722 if (mp->ma_used == 0) {
1723 if (deflt) {
1724 Py_INCREF(deflt);
1725 return deflt;
1726 }
1727 PyErr_SetString(PyExc_KeyError,
1728 "pop(): dictionary is empty");
1729 return NULL;
1730 }
1731 if (!PyString_CheckExact(key) ||
1732 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1733 hash = PyObject_Hash(key);
1734 if (hash == -1)
1735 return NULL;
1736 }
1737 ep = (mp->ma_lookup)(mp, key, hash);
1738 if (ep == NULL)
1739 return NULL;
1740 if (ep->me_value == NULL) {
1741 if (deflt) {
1742 Py_INCREF(deflt);
1743 return deflt;
1744 }
1745 PyErr_SetObject(PyExc_KeyError, key);
1746 return NULL;
1747 }
1748 old_key = ep->me_key;
1749 Py_INCREF(dummy);
1750 ep->me_key = dummy;
1751 old_value = ep->me_value;
1752 ep->me_value = NULL;
1753 mp->ma_used--;
1754 Py_DECREF(old_key);
1755 return old_value;
1756}
1757
1758static PyObject *
1759dict_popitem(dictobject *mp)
1760{
1761 Py_ssize_t i = 0;
1762 dictentry *ep;
1763 PyObject *res;
1764
1765 /* Allocate the result tuple before checking the size. Believe it
1766 * or not, this allocation could trigger a garbage collection which
1767 * could empty the dict, so if we checked the size first and that
1768 * happened, the result would be an infinite loop (searching for an
1769 * entry that no longer exists). Note that the usual popitem()
1770 * idiom is "while d: k, v = d.popitem()". so needing to throw the
1771 * tuple away if the dict *is* empty isn't a significant
1772 * inefficiency -- possible, but unlikely in practice.
1773 */
1774 res = PyTuple_New(2);
1775 if (res == NULL)
1776 return NULL;
1777 if (mp->ma_used == 0) {
1778 Py_DECREF(res);
1779 PyErr_SetString(PyExc_KeyError,
1780 "popitem(): dictionary is empty");
1781 return NULL;
1782 }
1783 /* Set ep to "the first" dict entry with a value. We abuse the hash
1784 * field of slot 0 to hold a search finger:
1785 * If slot 0 has a value, use slot 0.
1786 * Else slot 0 is being used to hold a search finger,
1787 * and we use its hash value as the first index to look.
1788 */
1789 ep = &mp->ma_table[0];
1790 if (ep->me_value == NULL) {
1791 i = ep->me_hash;
1792 /* The hash field may be a real hash value, or it may be a
1793 * legit search finger, or it may be a once-legit search
1794 * finger that's out of bounds now because it wrapped around
1795 * or the table shrunk -- simply make sure it's in bounds now.
1796 */
1797 if (i > mp->ma_mask || i < 1)
1798 i = 1; /* skip slot 0 */
1799 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1800 i++;
1801 if (i > mp->ma_mask)
1802 i = 1;
1803 }
1804 }
1805 PyTuple_SET_ITEM(res, 0, ep->me_key);
1806 PyTuple_SET_ITEM(res, 1, ep->me_value);
1807 Py_INCREF(dummy);
1808 ep->me_key = dummy;
1809 ep->me_value = NULL;
1810 mp->ma_used--;
1811 assert(mp->ma_table[0].me_value == NULL);
1812 mp->ma_table[0].me_hash = i + 1; /* next place to start */
1813 return res;
1814}
1815
1816static int
1817dict_traverse(PyObject *op, visitproc visit, void *arg)
1818{
1819 Py_ssize_t i = 0;
1820 PyObject *pk;
1821 PyObject *pv;
1822
1823 while (PyDict_Next(op, &i, &pk, &pv)) {
1824 Py_VISIT(pk);
1825 Py_VISIT(pv);
1826 }
1827 return 0;
1828}
1829
1830static int
1831dict_tp_clear(PyObject *op)
1832{
1833 PyDict_Clear(op);
1834 return 0;
1835}
1836
1837
1838extern PyTypeObject PyDictIterKey_Type; /* Forward */
1839extern PyTypeObject PyDictIterValue_Type; /* Forward */
1840extern PyTypeObject PyDictIterItem_Type; /* Forward */
1841static PyObject *dictiter_new(dictobject *, PyTypeObject *);
1842
1843static PyObject *
1844dict_iterkeys(dictobject *dict)
1845{
1846 return dictiter_new(dict, &PyDictIterKey_Type);
1847}
1848
1849static PyObject *
1850dict_itervalues(dictobject *dict)
1851{
1852 return dictiter_new(dict, &PyDictIterValue_Type);
1853}
1854
1855static PyObject *
1856dict_iteritems(dictobject *dict)
1857{
1858 return dictiter_new(dict, &PyDictIterItem_Type);
1859}
1860
1861
1862PyDoc_STRVAR(has_key__doc__,
1863"D.has_key(k) -> True if D has a key k, else False");
1864
1865PyDoc_STRVAR(contains__doc__,
1866"D.__contains__(k) -> True if D has a key k, else False");
1867
1868PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
1869
1870PyDoc_STRVAR(get__doc__,
1871"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
1872
1873PyDoc_STRVAR(setdefault_doc__,
1874"D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D");
1875
1876PyDoc_STRVAR(pop__doc__,
1877"D.pop(k[,d]) -> v, remove specified key and return the corresponding value\n\
1878If key is not found, d is returned if given, otherwise KeyError is raised");
1879
1880PyDoc_STRVAR(popitem__doc__,
1881"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
18822-tuple; but raise KeyError if D is empty");
1883
1884PyDoc_STRVAR(keys__doc__,
1885"D.keys() -> list of D's keys");
1886
1887PyDoc_STRVAR(items__doc__,
1888"D.items() -> list of D's (key, value) pairs, as 2-tuples");
1889
1890PyDoc_STRVAR(values__doc__,
1891"D.values() -> list of D's values");
1892
1893PyDoc_STRVAR(update__doc__,
1894"D.update(E, **F) -> None. Update D from E and F: for k in E: D[k] = E[k]\n\
1895(if E has keys else: for (k, v) in E: D[k] = v) then: for k in F: D[k] = F[k]");
1896
1897PyDoc_STRVAR(fromkeys__doc__,
1898"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
1899v defaults to None.");
1900
1901PyDoc_STRVAR(clear__doc__,
1902"D.clear() -> None. Remove all items from D.");
1903
1904PyDoc_STRVAR(copy__doc__,
1905"D.copy() -> a shallow copy of D");
1906
1907PyDoc_STRVAR(iterkeys__doc__,
1908"D.iterkeys() -> an iterator over the keys of D");
1909
1910PyDoc_STRVAR(itervalues__doc__,
1911"D.itervalues() -> an iterator over the values of D");
1912
1913PyDoc_STRVAR(iteritems__doc__,
1914"D.iteritems() -> an iterator over the (key, value) items of D");
1915
1916static PyMethodDef mapp_methods[] = {
1917 {"__contains__",(PyCFunction)dict_has_key, METH_O | METH_COEXIST,
1918 contains__doc__},
1919 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
1920 getitem__doc__},
1921 {"has_key", (PyCFunction)dict_has_key, METH_O,
1922 has_key__doc__},
1923 {"get", (PyCFunction)dict_get, METH_VARARGS,
1924 get__doc__},
1925 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
1926 setdefault_doc__},
1927 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
1928 pop__doc__},
1929 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
1930 popitem__doc__},
1931 {"keys", (PyCFunction)dict_keys, METH_NOARGS,
1932 keys__doc__},
1933 {"items", (PyCFunction)dict_items, METH_NOARGS,
1934 items__doc__},
1935 {"values", (PyCFunction)dict_values, METH_NOARGS,
1936 values__doc__},
1937 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
1938 update__doc__},
1939 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
1940 fromkeys__doc__},
1941 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
1942 clear__doc__},
1943 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
1944 copy__doc__},
1945 {"iterkeys", (PyCFunction)dict_iterkeys, METH_NOARGS,
1946 iterkeys__doc__},
1947 {"itervalues", (PyCFunction)dict_itervalues, METH_NOARGS,
1948 itervalues__doc__},
1949 {"iteritems", (PyCFunction)dict_iteritems, METH_NOARGS,
1950 iteritems__doc__},
1951 {NULL, NULL} /* sentinel */
1952};
1953
1954/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
1955int
1956PyDict_Contains(PyObject *op, PyObject *key)
1957{
1958 long hash;
1959 dictobject *mp = (dictobject *)op;
1960 dictentry *ep;
1961
1962 if (!PyString_CheckExact(key) ||
1963 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1964 hash = PyObject_Hash(key);
1965 if (hash == -1)
1966 return -1;
1967 }
1968 ep = (mp->ma_lookup)(mp, key, hash);
1969 return ep == NULL ? -1 : (ep->me_value != NULL);
1970}
1971
1972/* Hack to implement "key in dict" */
1973static PySequenceMethods dict_as_sequence = {
1974 0, /* sq_length */
1975 0, /* sq_concat */
1976 0, /* sq_repeat */
1977 0, /* sq_item */
1978 0, /* sq_slice */
1979 0, /* sq_ass_item */
1980 0, /* sq_ass_slice */
1981 PyDict_Contains, /* sq_contains */
1982 0, /* sq_inplace_concat */
1983 0, /* sq_inplace_repeat */
1984};
1985
1986static PyObject *
1987dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1988{
1989 PyObject *self;
1990
1991 assert(type != NULL && type->tp_alloc != NULL);
1992 self = type->tp_alloc(type, 0);
1993 if (self != NULL) {
1994 PyDictObject *d = (PyDictObject *)self;
1995 /* It's guaranteed that tp->alloc zeroed out the struct. */
1996 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
1997 INIT_NONZERO_DICT_SLOTS(d);
1998 d->ma_lookup = lookdict_string;
1999#ifdef SHOW_CONVERSION_COUNTS
2000 ++created;
2001#endif
2002 }
2003 return self;
2004}
2005
2006static int
2007dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2008{
2009 return dict_update_common(self, args, kwds, "dict");
2010}
2011
2012static long
2013dict_nohash(PyObject *self)
2014{
2015 PyErr_SetString(PyExc_TypeError, "dict objects are unhashable");
2016 return -1;
2017}
2018
2019static PyObject *
2020dict_iter(dictobject *dict)
2021{
2022 return dictiter_new(dict, &PyDictIterKey_Type);
2023}
2024
2025PyDoc_STRVAR(dictionary_doc,
2026"dict() -> new empty dictionary.\n"
2027"dict(mapping) -> new dictionary initialized from a mapping object's\n"
2028" (key, value) pairs.\n"
2029"dict(seq) -> new dictionary initialized as if via:\n"
2030" d = {}\n"
2031" for k, v in seq:\n"
2032" d[k] = v\n"
2033"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2034" in the keyword argument list. For example: dict(one=1, two=2)");
2035
2036PyTypeObject PyDict_Type = {
2037 PyObject_HEAD_INIT(&PyType_Type)
2038 0,
2039 "dict",
2040 sizeof(dictobject),
2041 0,
2042 (destructor)dict_dealloc, /* tp_dealloc */
2043 (printfunc)dict_print, /* tp_print */
2044 0, /* tp_getattr */
2045 0, /* tp_setattr */
2046 (cmpfunc)dict_compare, /* tp_compare */
2047 (reprfunc)dict_repr, /* tp_repr */
2048 0, /* tp_as_number */
2049 &dict_as_sequence, /* tp_as_sequence */
2050 &dict_as_mapping, /* tp_as_mapping */
2051 dict_nohash, /* tp_hash */
2052 0, /* tp_call */
2053 0, /* tp_str */
2054 PyObject_GenericGetAttr, /* tp_getattro */
2055 0, /* tp_setattro */
2056 0, /* tp_as_buffer */
2057 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2058 Py_TPFLAGS_BASETYPE, /* tp_flags */
2059 dictionary_doc, /* tp_doc */
2060 dict_traverse, /* tp_traverse */
2061 dict_tp_clear, /* tp_clear */
2062 dict_richcompare, /* tp_richcompare */
2063 0, /* tp_weaklistoffset */
2064 (getiterfunc)dict_iter, /* tp_iter */
2065 0, /* tp_iternext */
2066 mapp_methods, /* tp_methods */
2067 0, /* tp_members */
2068 0, /* tp_getset */
2069 0, /* tp_base */
2070 0, /* tp_dict */
2071 0, /* tp_descr_get */
2072 0, /* tp_descr_set */
2073 0, /* tp_dictoffset */
2074 dict_init, /* tp_init */
2075 PyType_GenericAlloc, /* tp_alloc */
2076 dict_new, /* tp_new */
2077 PyObject_GC_Del, /* tp_free */
2078};
2079
2080/* For backward compatibility with old dictionary interface */
2081
2082PyObject *
2083PyDict_GetItemString(PyObject *v, const char *key)
2084{
2085 PyObject *kv, *rv;
2086 kv = PyString_FromString(key);
2087 if (kv == NULL)
2088 return NULL;
2089 rv = PyDict_GetItem(v, kv);
2090 Py_DECREF(kv);
2091 return rv;
2092}
2093
2094int
2095PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
2096{
2097 PyObject *kv;
2098 int err;
2099 kv = PyString_FromString(key);
2100 if (kv == NULL)
2101 return -1;
2102 PyString_InternInPlace(&kv); /* XXX Should we really? */
2103 err = PyDict_SetItem(v, kv, item);
2104 Py_DECREF(kv);
2105 return err;
2106}
2107
2108int
2109PyDict_DelItemString(PyObject *v, const char *key)
2110{
2111 PyObject *kv;
2112 int err;
2113 kv = PyString_FromString(key);
2114 if (kv == NULL)
2115 return -1;
2116 err = PyDict_DelItem(v, kv);
2117 Py_DECREF(kv);
2118 return err;
2119}
2120
2121/* Dictionary iterator types */
2122
2123typedef struct {
2124 PyObject_HEAD
2125 dictobject *di_dict; /* Set to NULL when iterator is exhausted */
2126 Py_ssize_t di_used;
2127 Py_ssize_t di_pos;
2128 PyObject* di_result; /* reusable result tuple for iteritems */
2129 Py_ssize_t len;
2130} dictiterobject;
2131
2132static PyObject *
2133dictiter_new(dictobject *dict, PyTypeObject *itertype)
2134{
2135 dictiterobject *di;
2136 di = PyObject_New(dictiterobject, itertype);
2137 if (di == NULL)
2138 return NULL;
2139 Py_INCREF(dict);
2140 di->di_dict = dict;
2141 di->di_used = dict->ma_used;
2142 di->di_pos = 0;
2143 di->len = dict->ma_used;
2144 if (itertype == &PyDictIterItem_Type) {
2145 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2146 if (di->di_result == NULL) {
2147 Py_DECREF(di);
2148 return NULL;
2149 }
2150 }
2151 else
2152 di->di_result = NULL;
2153 return (PyObject *)di;
2154}
2155
2156static void
2157dictiter_dealloc(dictiterobject *di)
2158{
2159 Py_XDECREF(di->di_dict);
2160 Py_XDECREF(di->di_result);
2161 PyObject_Del(di);
2162}
2163
2164static PyObject *
2165dictiter_len(dictiterobject *di)
2166{
2167 Py_ssize_t len = 0;
2168 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
2169 len = di->len;
2170 return PyInt_FromSize_t(len);
2171}
2172
2173PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
2174
2175static PyMethodDef dictiter_methods[] = {
2176 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS, length_hint_doc},
2177 {NULL, NULL} /* sentinel */
2178};
2179
2180static PyObject *dictiter_iternextkey(dictiterobject *di)
2181{
2182 PyObject *key;
2183 register Py_ssize_t i, mask;
2184 register dictentry *ep;
2185 dictobject *d = di->di_dict;
2186
2187 if (d == NULL)
2188 return NULL;
2189 assert (PyDict_Check(d));
2190
2191 if (di->di_used != d->ma_used) {
2192 PyErr_SetString(PyExc_RuntimeError,
2193 "dictionary changed size during iteration");
2194 di->di_used = -1; /* Make this state sticky */
2195 return NULL;
2196 }
2197
2198 i = di->di_pos;
2199 if (i < 0)
2200 goto fail;
2201 ep = d->ma_table;
2202 mask = d->ma_mask;
2203 while (i <= mask && ep[i].me_value == NULL)
2204 i++;
2205 di->di_pos = i+1;
2206 if (i > mask)
2207 goto fail;
2208 di->len--;
2209 key = ep[i].me_key;
2210 Py_INCREF(key);
2211 return key;
2212
2213fail:
2214 Py_DECREF(d);
2215 di->di_dict = NULL;
2216 return NULL;
2217}
2218
2219PyTypeObject PyDictIterKey_Type = {
2220 PyObject_HEAD_INIT(&PyType_Type)
2221 0, /* ob_size */
2222 "dictionary-keyiterator", /* tp_name */
2223 sizeof(dictiterobject), /* tp_basicsize */
2224 0, /* tp_itemsize */
2225 /* methods */
2226 (destructor)dictiter_dealloc, /* tp_dealloc */
2227 0, /* tp_print */
2228 0, /* tp_getattr */
2229 0, /* tp_setattr */
2230 0, /* tp_compare */
2231 0, /* tp_repr */
2232 0, /* tp_as_number */
2233 0, /* tp_as_sequence */
2234 0, /* tp_as_mapping */
2235 0, /* tp_hash */
2236 0, /* tp_call */
2237 0, /* tp_str */
2238 PyObject_GenericGetAttr, /* tp_getattro */
2239 0, /* tp_setattro */
2240 0, /* tp_as_buffer */
2241 Py_TPFLAGS_DEFAULT, /* tp_flags */
2242 0, /* tp_doc */
2243 0, /* tp_traverse */
2244 0, /* tp_clear */
2245 0, /* tp_richcompare */
2246 0, /* tp_weaklistoffset */
2247 PyObject_SelfIter, /* tp_iter */
2248 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
2249 dictiter_methods, /* tp_methods */
2250 0,
2251};
2252
2253static PyObject *dictiter_iternextvalue(dictiterobject *di)
2254{
2255 PyObject *value;
2256 register Py_ssize_t i, mask;
2257 register dictentry *ep;
2258 dictobject *d = di->di_dict;
2259
2260 if (d == NULL)
2261 return NULL;
2262 assert (PyDict_Check(d));
2263
2264 if (di->di_used != d->ma_used) {
2265 PyErr_SetString(PyExc_RuntimeError,
2266 "dictionary changed size during iteration");
2267 di->di_used = -1; /* Make this state sticky */
2268 return NULL;
2269 }
2270
2271 i = di->di_pos;
2272 mask = d->ma_mask;
2273 if (i < 0 || i > mask)
2274 goto fail;
2275 ep = d->ma_table;
2276 while ((value=ep[i].me_value) == NULL) {
2277 i++;
2278 if (i > mask)
2279 goto fail;
2280 }
2281 di->di_pos = i+1;
2282 di->len--;
2283 Py_INCREF(value);
2284 return value;
2285
2286fail:
2287 Py_DECREF(d);
2288 di->di_dict = NULL;
2289 return NULL;
2290}
2291
2292PyTypeObject PyDictIterValue_Type = {
2293 PyObject_HEAD_INIT(&PyType_Type)
2294 0, /* ob_size */
2295 "dictionary-valueiterator", /* tp_name */
2296 sizeof(dictiterobject), /* tp_basicsize */
2297 0, /* tp_itemsize */
2298 /* methods */
2299 (destructor)dictiter_dealloc, /* tp_dealloc */
2300 0, /* tp_print */
2301 0, /* tp_getattr */
2302 0, /* tp_setattr */
2303 0, /* tp_compare */
2304 0, /* tp_repr */
2305 0, /* tp_as_number */
2306 0, /* tp_as_sequence */
2307 0, /* tp_as_mapping */
2308 0, /* tp_hash */
2309 0, /* tp_call */
2310 0, /* tp_str */
2311 PyObject_GenericGetAttr, /* tp_getattro */
2312 0, /* tp_setattro */
2313 0, /* tp_as_buffer */
2314 Py_TPFLAGS_DEFAULT, /* tp_flags */
2315 0, /* tp_doc */
2316 0, /* tp_traverse */
2317 0, /* tp_clear */
2318 0, /* tp_richcompare */
2319 0, /* tp_weaklistoffset */
2320 PyObject_SelfIter, /* tp_iter */
2321 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
2322 dictiter_methods, /* tp_methods */
2323 0,
2324};
2325
2326static PyObject *dictiter_iternextitem(dictiterobject *di)
2327{
2328 PyObject *key, *value, *result = di->di_result;
2329 register Py_ssize_t i, mask;
2330 register dictentry *ep;
2331 dictobject *d = di->di_dict;
2332
2333 if (d == NULL)
2334 return NULL;
2335 assert (PyDict_Check(d));
2336
2337 if (di->di_used != d->ma_used) {
2338 PyErr_SetString(PyExc_RuntimeError,
2339 "dictionary changed size during iteration");
2340 di->di_used = -1; /* Make this state sticky */
2341 return NULL;
2342 }
2343
2344 i = di->di_pos;
2345 if (i < 0)
2346 goto fail;
2347 ep = d->ma_table;
2348 mask = d->ma_mask;
2349 while (i <= mask && ep[i].me_value == NULL)
2350 i++;
2351 di->di_pos = i+1;
2352 if (i > mask)
2353 goto fail;
2354
2355 if (result->ob_refcnt == 1) {
2356 Py_INCREF(result);
2357 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2358 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2359 } else {
2360 result = PyTuple_New(2);
2361 if (result == NULL)
2362 return NULL;
2363 }
2364 di->len--;
2365 key = ep[i].me_key;
2366 value = ep[i].me_value;
2367 Py_INCREF(key);
2368 Py_INCREF(value);
2369 PyTuple_SET_ITEM(result, 0, key);
2370 PyTuple_SET_ITEM(result, 1, value);
2371 return result;
2372
2373fail:
2374 Py_DECREF(d);
2375 di->di_dict = NULL;
2376 return NULL;
2377}
2378
2379PyTypeObject PyDictIterItem_Type = {
2380 PyObject_HEAD_INIT(&PyType_Type)
2381 0, /* ob_size */
2382 "dictionary-itemiterator", /* tp_name */
2383 sizeof(dictiterobject), /* tp_basicsize */
2384 0, /* tp_itemsize */
2385 /* methods */
2386 (destructor)dictiter_dealloc, /* tp_dealloc */
2387 0, /* tp_print */
2388 0, /* tp_getattr */
2389 0, /* tp_setattr */
2390 0, /* tp_compare */
2391 0, /* tp_repr */
2392 0, /* tp_as_number */
2393 0, /* tp_as_sequence */
2394 0, /* tp_as_mapping */
2395 0, /* tp_hash */
2396 0, /* tp_call */
2397 0, /* tp_str */
2398 PyObject_GenericGetAttr, /* tp_getattro */
2399 0, /* tp_setattro */
2400 0, /* tp_as_buffer */
2401 Py_TPFLAGS_DEFAULT, /* tp_flags */
2402 0, /* tp_doc */
2403 0, /* tp_traverse */
2404 0, /* tp_clear */
2405 0, /* tp_richcompare */
2406 0, /* tp_weaklistoffset */
2407 PyObject_SelfIter, /* tp_iter */
2408 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
2409 dictiter_methods, /* tp_methods */
2410 0,
2411};
Note: See TracBrowser for help on using the repository browser.