blob: e127d96a3c79a8be51a56bce3f78f0e7fd104d2e [file] [log] [blame]
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001
Guido van Rossum2bc13791999-03-24 19:06:42 +00002/* Dictionary object implementation using a hash table */
Guido van Rossum9bfef441993-03-29 10:43:31 +00003
Raymond Hettinger930427b2003-05-03 06:51:59 +00004/* The distribution includes a separate file, Objects/dictnotes.txt,
Tim Peters60b29962006-01-01 01:19:23 +00005 describing explorations into dictionary design and optimization.
Raymond Hettinger930427b2003-05-03 06:51:59 +00006 It covers typical dictionary use patterns, the parameters for
7 tuning dictionaries, and several ideas for possible optimizations.
8*/
9
Guido van Rossumc0b618a1997-05-02 03:12:38 +000010#include "Python.h"
Guido van Rossum4b1302b1993-03-27 18:11:32 +000011
Tim Peters6d6c1a32001-08-02 04:15:00 +000012typedef PyDictEntry dictentry;
13typedef PyDictObject dictobject;
Guido van Rossum16e93a81997-01-28 00:00:11 +000014
Tim Peterseb28ef22001-06-02 05:27:19 +000015/* Define this out if you don't want conversion statistics on exit. */
Fred Drake1bff34a2000-08-31 19:31:38 +000016#undef SHOW_CONVERSION_COUNTS
17
Tim Peterseb28ef22001-06-02 05:27:19 +000018/* See large comment block below. This must be >= 1. */
19#define PERTURB_SHIFT 5
20
Guido van Rossum16e93a81997-01-28 00:00:11 +000021/*
Tim Peterseb28ef22001-06-02 05:27:19 +000022Major 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:
Tim Peters15d49292001-05-27 07:39:22 +000026
Tim Peterseb28ef22001-06-02 05:27:19 +000027>>> map(hash, (0, 1, 2, 3))
28[0, 1, 2, 3]
29>>> map(hash, ("namea", "nameb", "namec", "named"))
30[-1658398457, -1658398460, -1658398459, -1658398462]
31>>>
Tim Peters15d49292001-05-27 07:39:22 +000032
Tim Peterseb28ef22001-06-02 05:27:19 +000033This 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.
Tim Peters15d49292001-05-27 07:39:22 +000038
Tim Peterseb28ef22001-06-02 05:27:19 +000039OTOH, 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.
Tim Peters15d49292001-05-27 07:39:22 +000045
Tim Peterseb28ef22001-06-02 05:27:19 +000046But 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.
Tim Peters15d49292001-05-27 07:39:22 +000052
Tim Peterseb28ef22001-06-02 05:27:19 +000053The first half of collision resolution is to visit table indices via this
54recurrence:
Tim Peters15d49292001-05-27 07:39:22 +000055
Tim Peterseb28ef22001-06-02 05:27:19 +000056 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +000057
Tim Peterseb28ef22001-06-02 05:27:19 +000058For 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:
Tim Peters15d49292001-05-27 07:39:22 +000066
Tim Peterseb28ef22001-06-02 05:27:19 +000067 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.
Tim Peters9b10f7e2006-05-30 04:16:25 +0000113
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).
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000123*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000124
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000125/* Object used as dummy key to fill deleted entries */
Raymond Hettingerf81e4502005-08-17 02:19:36 +0000126static PyObject *dummy = NULL; /* Initialized by first call to newdictobject() */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000127
Armin Rigoe1709372006-04-12 17:06:05 +0000128#ifdef Py_REF_DEBUG
129PyObject *
130_PyDict_Dummy(void)
131{
132 return dummy;
133}
134#endif
135
Fred Drake1bff34a2000-08-31 19:31:38 +0000136/* 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
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000152
Tim Peters6d6c1a32001-08-02 04:15:00 +0000153/* 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 { \
Tim Petersdea48ec2001-05-22 20:40:22 +0000163 (mp)->ma_table = (mp)->ma_smalltable; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000164 (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)); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000169 (mp)->ma_used = (mp)->ma_fill = 0; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000170 INIT_NONZERO_DICT_SLOTS(mp); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000171 } while(0)
172
Raymond Hettinger43442782004-03-17 21:55:03 +0000173/* 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
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000178PyObject *
Thomas Wouters78890102000-07-22 19:25:51 +0000179PyDict_New(void)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000180{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000181 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000182 if (dummy == NULL) { /* Auto-initialize dummy */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000183 dummy = PyString_FromString("<dummy key>");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000184 if (dummy == NULL)
185 return NULL;
Fred Drake1bff34a2000-08-31 19:31:38 +0000186#ifdef SHOW_CONVERSION_COUNTS
187 Py_AtExit(show_counts);
188#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000189 }
Raymond Hettinger43442782004-03-17 21:55:03 +0000190 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 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000207 mp->ma_lookup = lookdict_string;
208#ifdef SHOW_CONVERSION_COUNTS
209 ++created;
210#endif
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000211 _PyObject_GC_TRACK(mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000212 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000213}
214
215/*
216The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000217This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000218Open addressing is preferred over chaining since the link overhead for
219chaining would be substantial (100% with typical malloc overhead).
220
Tim Peterseb28ef22001-06-02 05:27:19 +0000221The initial probe index is computed as hash mod the table size. Subsequent
222probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000223
224All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000225
Tim Peterseb28ef22001-06-02 05:27:19 +0000226(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).
Fred Drake1bff34a2000-08-31 19:31:38 +0000229
Tim Petersd770ebd2006-06-01 15:50:44 +0000230lookdict() 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*.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000238*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000239static dictentry *
Tim Peters1f5871e2000-07-04 17:44:48 +0000240lookdict(dictobject *mp, PyObject *key, register long hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000241{
Tim Petersd770ebd2006-06-01 15:50:44 +0000242 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000243 register size_t perturb;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000244 register dictentry *freeslot;
Tim Petersd770ebd2006-06-01 15:50:44 +0000245 register size_t mask = (size_t)mp->ma_mask;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000246 dictentry *ep0 = mp->ma_table;
247 register dictentry *ep;
Fred Drakec88b99c2000-08-31 19:04:07 +0000248 register int cmp;
Tim Peters453163d2001-06-03 04:54:32 +0000249 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000250
Tim Petersd770ebd2006-06-01 15:50:44 +0000251 i = (size_t)hash & mask;
Guido van Rossumefb46091997-01-29 15:53:56 +0000252 ep = &ep0[i];
Guido van Rossumc1c7b1a1998-10-06 16:01:14 +0000253 if (ep->me_key == NULL || ep->me_key == key)
Guido van Rossum7d181591997-01-16 21:06:45 +0000254 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000255
Guido van Rossum16e93a81997-01-28 00:00:11 +0000256 if (ep->me_key == dummy)
Guido van Rossum7d181591997-01-16 21:06:45 +0000257 freeslot = ep;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000258 else {
Fred Drakec88b99c2000-08-31 19:04:07 +0000259 if (ep->me_hash == hash) {
Tim Peters453163d2001-06-03 04:54:32 +0000260 startkey = ep->me_key;
261 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000262 if (cmp < 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000263 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000264 if (ep0 == mp->ma_table && ep->me_key == startkey) {
265 if (cmp > 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000266 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000267 }
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 */
Armin Rigo35f6d362006-06-01 13:19:12 +0000274 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000275 }
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000276 }
277 freeslot = NULL;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000278 }
Tim Peters15d49292001-05-27 07:39:22 +0000279
Tim Peters342c65e2001-05-13 06:43:53 +0000280 /* In the loop, me_key == dummy is by far (factor of 100s) the
281 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000282 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
283 i = (i << 2) + i + perturb + 1;
284 ep = &ep0[i & mask];
Armin Rigo35f6d362006-06-01 13:19:12 +0000285 if (ep->me_key == NULL)
286 return freeslot == NULL ? ep : freeslot;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000287 if (ep->me_key == key)
Armin Rigo35f6d362006-06-01 13:19:12 +0000288 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000289 if (ep->me_hash == hash && ep->me_key != dummy) {
Tim Peters453163d2001-06-03 04:54:32 +0000290 startkey = ep->me_key;
291 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000292 if (cmp < 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000293 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000294 if (ep0 == mp->ma_table && ep->me_key == startkey) {
295 if (cmp > 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000296 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000297 }
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 */
Armin Rigo35f6d362006-06-01 13:19:12 +0000304 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000305 }
Guido van Rossum16e93a81997-01-28 00:00:11 +0000306 }
Tim Peters342c65e2001-05-13 06:43:53 +0000307 else if (ep->me_key == dummy && freeslot == NULL)
308 freeslot = ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000309 }
Neal Norwitza5ccda92006-10-28 21:16:54 +0000310 assert(0); /* NOT REACHED */
311 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000312}
313
314/*
Fred Drake1bff34a2000-08-31 19:31:38 +0000315 * Hacked up version of lookdict which can assume keys are always strings;
Tim Petersd770ebd2006-06-01 15:50:44 +0000316 * this assumption allows testing for errors during PyObject_RichCompareBool()
317 * to be dropped; string-string comparisons never raise exceptions. This also
318 * means we don't need to go through PyObject_RichCompareBool(); we can always
319 * use _PyString_Eq() directly.
Fred Drake1bff34a2000-08-31 19:31:38 +0000320 *
Tim Petersd770ebd2006-06-01 15:50:44 +0000321 * This is valuable because dicts with only string keys are very common.
Fred Drake1bff34a2000-08-31 19:31:38 +0000322 */
323static dictentry *
324lookdict_string(dictobject *mp, PyObject *key, register long hash)
325{
Tim Petersd770ebd2006-06-01 15:50:44 +0000326 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000327 register size_t perturb;
Fred Drake1bff34a2000-08-31 19:31:38 +0000328 register dictentry *freeslot;
Tim Petersd770ebd2006-06-01 15:50:44 +0000329 register size_t mask = (size_t)mp->ma_mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000330 dictentry *ep0 = mp->ma_table;
331 register dictentry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000332
Tim Peters0ab085c2001-09-14 00:25:33 +0000333 /* Make sure this function doesn't have to handle non-string keys,
334 including subclasses of str; e.g., one reason to subclass
335 strings is to override __eq__, and for speed we don't cater to
336 that here. */
337 if (!PyString_CheckExact(key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000338#ifdef SHOW_CONVERSION_COUNTS
339 ++converted;
340#endif
341 mp->ma_lookup = lookdict;
342 return lookdict(mp, key, hash);
343 }
Tim Peters2f228e72001-05-13 00:19:31 +0000344 i = hash & mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000345 ep = &ep0[i];
346 if (ep->me_key == NULL || ep->me_key == key)
347 return ep;
348 if (ep->me_key == dummy)
349 freeslot = ep;
350 else {
Tim Petersd770ebd2006-06-01 15:50:44 +0000351 if (ep->me_hash == hash && _PyString_Eq(ep->me_key, key))
Fred Drake1bff34a2000-08-31 19:31:38 +0000352 return ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000353 freeslot = NULL;
354 }
Tim Peters15d49292001-05-27 07:39:22 +0000355
Tim Peters342c65e2001-05-13 06:43:53 +0000356 /* In the loop, me_key == dummy is by far (factor of 100s) the
357 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000358 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
359 i = (i << 2) + i + perturb + 1;
360 ep = &ep0[i & mask];
Tim Peters342c65e2001-05-13 06:43:53 +0000361 if (ep->me_key == NULL)
362 return freeslot == NULL ? ep : freeslot;
363 if (ep->me_key == key
364 || (ep->me_hash == hash
365 && ep->me_key != dummy
Martin v. Löwiscd353062001-05-24 16:56:35 +0000366 && _PyString_Eq(ep->me_key, key)))
Fred Drake1bff34a2000-08-31 19:31:38 +0000367 return ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000368 if (ep->me_key == dummy && freeslot == NULL)
Tim Peters342c65e2001-05-13 06:43:53 +0000369 freeslot = ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000370 }
Neal Norwitza5ccda92006-10-28 21:16:54 +0000371 assert(0); /* NOT REACHED */
372 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000373}
374
375/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000376Internal routine to insert a new item into the table.
377Used both by the internal resize routine and by the public insert routine.
378Eats a reference to key and one to value.
Tim Petersd770ebd2006-06-01 15:50:44 +0000379Returns -1 if an error occurred, or 0 on success.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000380*/
Armin Rigo35f6d362006-06-01 13:19:12 +0000381static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000382insertdict(register dictobject *mp, PyObject *key, long hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000383{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000384 PyObject *old_value;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000385 register dictentry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000386 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
387
388 assert(mp->ma_lookup != NULL);
389 ep = mp->ma_lookup(mp, key, hash);
Armin Rigo35f6d362006-06-01 13:19:12 +0000390 if (ep == NULL) {
391 Py_DECREF(key);
392 Py_DECREF(value);
393 return -1;
394 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000395 if (ep->me_value != NULL) {
Guido van Rossumd7047b31995-01-02 19:07:15 +0000396 old_value = ep->me_value;
397 ep->me_value = value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000398 Py_DECREF(old_value); /* which **CAN** re-enter */
399 Py_DECREF(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000400 }
401 else {
402 if (ep->me_key == NULL)
403 mp->ma_fill++;
Raymond Hettinger07ead172005-02-05 23:42:57 +0000404 else {
405 assert(ep->me_key == dummy);
406 Py_DECREF(dummy);
407 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000408 ep->me_key = key;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000409 ep->me_hash = (Py_ssize_t)hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000410 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000411 mp->ma_used++;
412 }
Armin Rigo35f6d362006-06-01 13:19:12 +0000413 return 0;
414}
415
416/*
417Internal routine used by dictresize() to insert an item which is
418known to be absent from the dict. This routine also assumes that
419the dict contains no deleted entries. Besides the performance benefit,
420using insertdict() in dictresize() is dangerous (SF bug #1456209).
Tim Petersd770ebd2006-06-01 15:50:44 +0000421Note that no refcounts are changed by this routine; if needed, the caller
422is responsible for incref'ing `key` and `value`.
Armin Rigo35f6d362006-06-01 13:19:12 +0000423*/
424static void
425insertdict_clean(register dictobject *mp, PyObject *key, long hash,
426 PyObject *value)
427{
Tim Petersd770ebd2006-06-01 15:50:44 +0000428 register size_t i;
Armin Rigo35f6d362006-06-01 13:19:12 +0000429 register size_t perturb;
Tim Petersd770ebd2006-06-01 15:50:44 +0000430 register size_t mask = (size_t)mp->ma_mask;
Armin Rigo35f6d362006-06-01 13:19:12 +0000431 dictentry *ep0 = mp->ma_table;
432 register dictentry *ep;
433
434 i = hash & mask;
435 ep = &ep0[i];
436 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
437 i = (i << 2) + i + perturb + 1;
438 ep = &ep0[i & mask];
439 }
Tim Petersd770ebd2006-06-01 15:50:44 +0000440 assert(ep->me_value == NULL);
Armin Rigo35f6d362006-06-01 13:19:12 +0000441 mp->ma_fill++;
442 ep->me_key = key;
443 ep->me_hash = (Py_ssize_t)hash;
444 ep->me_value = value;
445 mp->ma_used++;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000446}
447
448/*
449Restructure the table by allocating a new table and reinserting all
450items again. When entries have been deleted, the new table may
451actually be smaller than the old one.
452*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000453static int
Tim Peters9b10f7e2006-05-30 04:16:25 +0000454dictresize(dictobject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000455{
Tim Peters9b10f7e2006-05-30 04:16:25 +0000456 Py_ssize_t newsize;
Tim Peters0c6010b2001-05-23 23:33:57 +0000457 dictentry *oldtable, *newtable, *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000458 Py_ssize_t i;
Tim Peters0c6010b2001-05-23 23:33:57 +0000459 int is_oldtable_malloced;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000460 dictentry small_copy[PyDict_MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000461
462 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000463
Tim Peterseb28ef22001-06-02 05:27:19 +0000464 /* Find the smallest table size > minused. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000465 for (newsize = PyDict_MINSIZE;
Tim Peterseb28ef22001-06-02 05:27:19 +0000466 newsize <= minused && newsize > 0;
467 newsize <<= 1)
468 ;
469 if (newsize <= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000470 PyErr_NoMemory();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000471 return -1;
472 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000473
474 /* Get space for a new table. */
475 oldtable = mp->ma_table;
476 assert(oldtable != NULL);
477 is_oldtable_malloced = oldtable != mp->ma_smalltable;
478
Tim Peters6d6c1a32001-08-02 04:15:00 +0000479 if (newsize == PyDict_MINSIZE) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000480 /* A large table is shrinking, or we can't get any smaller. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000481 newtable = mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000482 if (newtable == oldtable) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000483 if (mp->ma_fill == mp->ma_used) {
484 /* No dummies, so no point doing anything. */
Tim Peters0c6010b2001-05-23 23:33:57 +0000485 return 0;
Tim Petersf8a548c2001-05-24 16:26:40 +0000486 }
487 /* We're not going to resize it, but rebuild the
488 table anyway to purge old dummy entries.
489 Subtle: This is *necessary* if fill==size,
490 as lookdict needs at least one virgin slot to
491 terminate failing searches. If fill < size, it's
492 merely desirable, as dummies slow searches. */
493 assert(mp->ma_fill > mp->ma_used);
Tim Peters0c6010b2001-05-23 23:33:57 +0000494 memcpy(small_copy, oldtable, sizeof(small_copy));
495 oldtable = small_copy;
496 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000497 }
498 else {
499 newtable = PyMem_NEW(dictentry, newsize);
500 if (newtable == NULL) {
501 PyErr_NoMemory();
502 return -1;
503 }
504 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000505
506 /* Make the dict empty, using the new table. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000507 assert(newtable != oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000508 mp->ma_table = newtable;
Tim Petersafb6ae82001-06-04 21:00:21 +0000509 mp->ma_mask = newsize - 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000510 memset(newtable, 0, sizeof(dictentry) * newsize);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000511 mp->ma_used = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000512 i = mp->ma_fill;
513 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000514
Tim Peters19283142001-05-17 22:25:34 +0000515 /* Copy the data over; this is refcount-neutral for active entries;
516 dummy entries aren't copied over, of course */
Tim Petersdea48ec2001-05-22 20:40:22 +0000517 for (ep = oldtable; i > 0; ep++) {
518 if (ep->me_value != NULL) { /* active entry */
519 --i;
Armin Rigo35f6d362006-06-01 13:19:12 +0000520 insertdict_clean(mp, ep->me_key, (long)ep->me_hash,
521 ep->me_value);
Tim Petersdea48ec2001-05-22 20:40:22 +0000522 }
Tim Peters19283142001-05-17 22:25:34 +0000523 else if (ep->me_key != NULL) { /* dummy entry */
Tim Petersdea48ec2001-05-22 20:40:22 +0000524 --i;
Tim Peters19283142001-05-17 22:25:34 +0000525 assert(ep->me_key == dummy);
526 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000527 }
Tim Peters19283142001-05-17 22:25:34 +0000528 /* else key == value == NULL: nothing to do */
Guido van Rossumd7047b31995-01-02 19:07:15 +0000529 }
530
Tim Peters0c6010b2001-05-23 23:33:57 +0000531 if (is_oldtable_malloced)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000532 PyMem_DEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000533 return 0;
534}
535
Tim Petersd770ebd2006-06-01 15:50:44 +0000536/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
537 * that may occur (originally dicts supported only string keys, and exceptions
538 * weren't possible). So, while the original intent was that a NULL return
Andrew M. Kuchling0067b5f2006-08-04 20:37:43 +0000539 * meant the key wasn't present, in reality it can mean that, or that an error
Tim Petersd770ebd2006-06-01 15:50:44 +0000540 * (suppressed) occurred while computing the key's hash, or that some error
541 * (suppressed) occurred when comparing keys in the dict's internal probe
542 * sequence. A nasty example of the latter is when a Python-coded comparison
543 * function hits a stack-depth error, which can cause this to return NULL
544 * even if the key is present.
545 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000546PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000547PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000548{
549 long hash;
Fred Drake1bff34a2000-08-31 19:31:38 +0000550 dictobject *mp = (dictobject *)op;
Armin Rigo35f6d362006-06-01 13:19:12 +0000551 dictentry *ep;
552 PyThreadState *tstate;
Tim Petersd770ebd2006-06-01 15:50:44 +0000553 if (!PyDict_Check(op))
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000554 return NULL;
Tim Peters0ab085c2001-09-14 00:25:33 +0000555 if (!PyString_CheckExact(key) ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000556 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Guido van Rossumca756f21997-01-23 19:39:29 +0000557 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000558 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000559 if (hash == -1) {
560 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000561 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000562 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000563 }
Armin Rigo35f6d362006-06-01 13:19:12 +0000564
565 /* We can arrive here with a NULL tstate during initialization:
566 try running "python -Wi" for an example related to string
567 interning. Let's just hope that no exception occurs then... */
Armin Rigoacd0d6d2006-06-10 10:57:40 +0000568 tstate = _PyThreadState_Current;
Armin Rigo35f6d362006-06-01 13:19:12 +0000569 if (tstate != NULL && tstate->curexc_type != NULL) {
570 /* preserve the existing exception */
571 PyObject *err_type, *err_value, *err_tb;
572 PyErr_Fetch(&err_type, &err_value, &err_tb);
573 ep = (mp->ma_lookup)(mp, key, hash);
574 /* ignore errors */
575 PyErr_Restore(err_type, err_value, err_tb);
576 if (ep == NULL)
577 return NULL;
578 }
579 else {
580 ep = (mp->ma_lookup)(mp, key, hash);
581 if (ep == NULL) {
582 PyErr_Clear();
583 return NULL;
584 }
585 }
586 return ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000587}
588
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000589/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
Tim Peters60b29962006-01-01 01:19:23 +0000590 * dictionary if it's merely replacing the value for an existing key.
591 * This means that it's safe to loop over a dictionary with PyDict_Next()
592 * and occasionally replace a value -- but you can't insert new keys or
593 * remove them.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000594 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000595int
Tim Peters1f5871e2000-07-04 17:44:48 +0000596PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000597{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000598 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000599 register long hash;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000600 register Py_ssize_t n_used;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000601
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000602 if (!PyDict_Check(op)) {
603 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000604 return -1;
605 }
Neal Norwitz48808a12006-07-21 05:29:58 +0000606 assert(key);
607 assert(value);
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000608 mp = (dictobject *)op;
Tim Peters0ab085c2001-09-14 00:25:33 +0000609 if (PyString_CheckExact(key)) {
Guido van Rossum45ec02a2002-08-19 21:43:18 +0000610 hash = ((PyStringObject *)key)->ob_shash;
611 if (hash == -1)
612 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000613 }
Tim Peters1f7df352002-03-29 03:29:08 +0000614 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000615 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000616 if (hash == -1)
617 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000618 }
Tim Petersafb6ae82001-06-04 21:00:21 +0000619 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000620 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000621 Py_INCREF(value);
622 Py_INCREF(key);
Armin Rigo35f6d362006-06-01 13:19:12 +0000623 if (insertdict(mp, key, hash, value) != 0)
624 return -1;
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000625 /* If we added a key, we can safely resize. Otherwise just return!
626 * If fill >= 2/3 size, adjust size. Normally, this doubles or
627 * quaduples the size, but it's also possible for the dict to shrink
Tim Peters60b29962006-01-01 01:19:23 +0000628 * (if ma_fill is much larger than ma_used, meaning a lot of dict
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000629 * keys have been * deleted).
Tim Peters60b29962006-01-01 01:19:23 +0000630 *
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000631 * Quadrupling the size improves average dictionary sparseness
632 * (reducing collisions) at the cost of some memory and iteration
633 * speed (which loops over every possible entry). It also halves
Neal Norwitz80af59c2006-05-30 04:19:21 +0000634 * the number of expensive resize operations in a growing dictionary.
Tim Peters60b29962006-01-01 01:19:23 +0000635 *
636 * Very large dictionaries (over 50K items) use doubling instead.
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000637 * This may help applications with severe memory constraints.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000638 */
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000639 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
640 return 0;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000641 return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000642}
643
644int
Tim Peters1f5871e2000-07-04 17:44:48 +0000645PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000646{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000647 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000648 register long hash;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000649 register dictentry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000650 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000651
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000652 if (!PyDict_Check(op)) {
653 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000654 return -1;
655 }
Neal Norwitz48808a12006-07-21 05:29:58 +0000656 assert(key);
Tim Peters0ab085c2001-09-14 00:25:33 +0000657 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +0000658 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000659 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000660 if (hash == -1)
661 return -1;
662 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000663 mp = (dictobject *)op;
Fred Drake1bff34a2000-08-31 19:31:38 +0000664 ep = (mp->ma_lookup)(mp, key, hash);
Armin Rigo35f6d362006-06-01 13:19:12 +0000665 if (ep == NULL)
666 return -1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000667 if (ep->me_value == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000668 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000669 return -1;
670 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000671 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000672 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000673 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000674 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000675 ep->me_value = NULL;
676 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000677 Py_DECREF(old_value);
678 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000679 return 0;
680}
681
Guido van Rossum25831651993-05-19 14:50:45 +0000682void
Tim Peters1f5871e2000-07-04 17:44:48 +0000683PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000684{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000685 dictobject *mp;
Tim Petersdea48ec2001-05-22 20:40:22 +0000686 dictentry *ep, *table;
687 int table_is_malloced;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000688 Py_ssize_t fill;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000689 dictentry small_copy[PyDict_MINSIZE];
Tim Petersdea48ec2001-05-22 20:40:22 +0000690#ifdef Py_DEBUG
Tim Peters9b10f7e2006-05-30 04:16:25 +0000691 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +0000692#endif
693
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000694 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000695 return;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000696 mp = (dictobject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000697#ifdef Py_DEBUG
Tim Petersafb6ae82001-06-04 21:00:21 +0000698 n = mp->ma_mask + 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000699 i = 0;
700#endif
701
702 table = mp->ma_table;
703 assert(table != NULL);
704 table_is_malloced = table != mp->ma_smalltable;
705
706 /* This is delicate. During the process of clearing the dict,
707 * decrefs can cause the dict to mutate. To avoid fatal confusion
708 * (voice of experience), we have to make the dict empty before
709 * clearing the slots, and never refer to anything via mp->xxx while
710 * clearing.
711 */
712 fill = mp->ma_fill;
713 if (table_is_malloced)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000714 EMPTY_TO_MINSIZE(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000715
716 else if (fill > 0) {
717 /* It's a small table with something that needs to be cleared.
718 * Afraid the only safe way is to copy the dict entries into
719 * another small table first.
720 */
721 memcpy(small_copy, table, sizeof(small_copy));
722 table = small_copy;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000723 EMPTY_TO_MINSIZE(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000724 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000725 /* else it's a small table that's already empty */
726
727 /* Now we can finally clear things. If C had refcounts, we could
728 * assert that the refcount on table is 1 now, i.e. that this function
729 * has unique access to it, so decref side-effects can't alter it.
730 */
731 for (ep = table; fill > 0; ++ep) {
732#ifdef Py_DEBUG
733 assert(i < n);
734 ++i;
735#endif
736 if (ep->me_key) {
737 --fill;
738 Py_DECREF(ep->me_key);
739 Py_XDECREF(ep->me_value);
740 }
741#ifdef Py_DEBUG
742 else
743 assert(ep->me_value == NULL);
744#endif
745 }
746
747 if (table_is_malloced)
748 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000749}
750
Tim Peters080c88b2003-02-15 03:01:11 +0000751/*
752 * Iterate over a dict. Use like so:
753 *
Tim Peters9b10f7e2006-05-30 04:16:25 +0000754 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +0000755 * PyObject *key, *value;
756 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +0000757 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +0000758 * Refer to borrowed references in key and value.
759 * }
760 *
761 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +0000762 * mutates the dict. One exception: it is safe if the loop merely changes
763 * the values associated with the keys (but doesn't insert new keys or
764 * delete keys), via PyDict_SetItem().
765 */
Guido van Rossum25831651993-05-19 14:50:45 +0000766int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000767PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000768{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000769 register Py_ssize_t i;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000770 register Py_ssize_t mask;
Raymond Hettinger43442782004-03-17 21:55:03 +0000771 register dictentry *ep;
772
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000773 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000774 return 0;
Guido van Rossum25831651993-05-19 14:50:45 +0000775 i = *ppos;
776 if (i < 0)
777 return 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000778 ep = ((dictobject *)op)->ma_table;
779 mask = ((dictobject *)op)->ma_mask;
780 while (i <= mask && ep[i].me_value == NULL)
Guido van Rossum25831651993-05-19 14:50:45 +0000781 i++;
782 *ppos = i+1;
Raymond Hettinger43442782004-03-17 21:55:03 +0000783 if (i > mask)
Guido van Rossum25831651993-05-19 14:50:45 +0000784 return 0;
785 if (pkey)
Raymond Hettinger43442782004-03-17 21:55:03 +0000786 *pkey = ep[i].me_key;
Guido van Rossum25831651993-05-19 14:50:45 +0000787 if (pvalue)
Raymond Hettinger43442782004-03-17 21:55:03 +0000788 *pvalue = ep[i].me_value;
Guido van Rossum25831651993-05-19 14:50:45 +0000789 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000790}
791
792/* Methods */
793
794static void
Tim Peters1f5871e2000-07-04 17:44:48 +0000795dict_dealloc(register dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000796{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000797 register dictentry *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000798 Py_ssize_t fill = mp->ma_fill;
Guido van Rossumff413af2002-03-28 20:34:59 +0000799 PyObject_GC_UnTrack(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000800 Py_TRASHCAN_SAFE_BEGIN(mp)
Tim Petersdea48ec2001-05-22 20:40:22 +0000801 for (ep = mp->ma_table; fill > 0; ep++) {
802 if (ep->me_key) {
803 --fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000804 Py_DECREF(ep->me_key);
Tim Petersdea48ec2001-05-22 20:40:22 +0000805 Py_XDECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000806 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000807 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000808 if (mp->ma_table != mp->ma_smalltable)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000809 PyMem_DEL(mp->ma_table);
Raymond Hettinger43442782004-03-17 21:55:03 +0000810 if (num_free_dicts < MAXFREEDICTS && mp->ob_type == &PyDict_Type)
811 free_dicts[num_free_dicts++] = mp;
Tim Peters60b29962006-01-01 01:19:23 +0000812 else
Raymond Hettinger43442782004-03-17 21:55:03 +0000813 mp->ob_type->tp_free((PyObject *)mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000814 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000815}
816
817static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000818dict_print(register dictobject *mp, register FILE *fp, register int flags)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000819{
Tim Peters9b10f7e2006-05-30 04:16:25 +0000820 register Py_ssize_t i;
821 register Py_ssize_t any;
Tim Peters33f4a6a2006-05-30 05:23:59 +0000822 int status;
Guido van Rossum255443b1998-04-10 22:47:14 +0000823
Tim Peters33f4a6a2006-05-30 05:23:59 +0000824 status = Py_ReprEnter((PyObject*)mp);
825 if (status != 0) {
826 if (status < 0)
827 return status;
Guido van Rossum255443b1998-04-10 22:47:14 +0000828 fprintf(fp, "{...}");
829 return 0;
830 }
831
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000832 fprintf(fp, "{");
833 any = 0;
Tim Petersafb6ae82001-06-04 21:00:21 +0000834 for (i = 0; i <= mp->ma_mask; i++) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000835 dictentry *ep = mp->ma_table + i;
836 PyObject *pvalue = ep->me_value;
837 if (pvalue != NULL) {
838 /* Prevent PyObject_Repr from deleting value during
839 key format */
840 Py_INCREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000841 if (any++ > 0)
842 fprintf(fp, ", ");
Guido van Rossum255443b1998-04-10 22:47:14 +0000843 if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000844 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000845 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000846 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000847 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000848 fprintf(fp, ": ");
Tim Peters19b77cf2001-06-02 08:27:39 +0000849 if (PyObject_Print(pvalue, fp, 0) != 0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000850 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000851 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000852 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000853 }
Tim Peters23cf6be2001-06-02 08:02:56 +0000854 Py_DECREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000855 }
856 }
857 fprintf(fp, "}");
Guido van Rossum255443b1998-04-10 22:47:14 +0000858 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000859 return 0;
860}
861
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000862static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000863dict_repr(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000864{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000865 Py_ssize_t i;
Tim Petersa7259592001-06-16 05:11:17 +0000866 PyObject *s, *temp, *colon = NULL;
867 PyObject *pieces = NULL, *result = NULL;
868 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +0000869
Tim Petersa7259592001-06-16 05:11:17 +0000870 i = Py_ReprEnter((PyObject *)mp);
Guido van Rossum255443b1998-04-10 22:47:14 +0000871 if (i != 0) {
Tim Petersa7259592001-06-16 05:11:17 +0000872 return i > 0 ? PyString_FromString("{...}") : NULL;
Guido van Rossum255443b1998-04-10 22:47:14 +0000873 }
874
Tim Petersa7259592001-06-16 05:11:17 +0000875 if (mp->ma_used == 0) {
876 result = PyString_FromString("{}");
877 goto Done;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000878 }
Tim Petersa7259592001-06-16 05:11:17 +0000879
880 pieces = PyList_New(0);
881 if (pieces == NULL)
882 goto Done;
883
884 colon = PyString_FromString(": ");
885 if (colon == NULL)
886 goto Done;
887
888 /* Do repr() on each key+value pair, and insert ": " between them.
889 Note that repr may mutate the dict. */
Tim Petersc6057842001-06-16 07:52:53 +0000890 i = 0;
891 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Tim Petersa7259592001-06-16 05:11:17 +0000892 int status;
893 /* Prevent repr from deleting value during key format. */
894 Py_INCREF(value);
895 s = PyObject_Repr(key);
896 PyString_Concat(&s, colon);
897 PyString_ConcatAndDel(&s, PyObject_Repr(value));
898 Py_DECREF(value);
899 if (s == NULL)
900 goto Done;
901 status = PyList_Append(pieces, s);
902 Py_DECREF(s); /* append created a new ref */
903 if (status < 0)
904 goto Done;
905 }
906
907 /* Add "{}" decorations to the first and last items. */
908 assert(PyList_GET_SIZE(pieces) > 0);
909 s = PyString_FromString("{");
910 if (s == NULL)
911 goto Done;
912 temp = PyList_GET_ITEM(pieces, 0);
913 PyString_ConcatAndDel(&s, temp);
914 PyList_SET_ITEM(pieces, 0, s);
915 if (s == NULL)
916 goto Done;
917
918 s = PyString_FromString("}");
919 if (s == NULL)
920 goto Done;
921 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
922 PyString_ConcatAndDel(&temp, s);
923 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
924 if (temp == NULL)
925 goto Done;
926
927 /* Paste them all together with ", " between. */
928 s = PyString_FromString(", ");
929 if (s == NULL)
930 goto Done;
931 result = _PyString_Join(s, pieces);
Tim Peters0ab085c2001-09-14 00:25:33 +0000932 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +0000933
934Done:
935 Py_XDECREF(pieces);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000936 Py_XDECREF(colon);
Tim Petersa7259592001-06-16 05:11:17 +0000937 Py_ReprLeave((PyObject *)mp);
938 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000939}
940
Martin v. Löwis18e16552006-02-15 17:27:45 +0000941static Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +0000942dict_length(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000943{
944 return mp->ma_used;
945}
946
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000947static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000948dict_subscript(dictobject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000949{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000950 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000951 long hash;
Armin Rigo35f6d362006-06-01 13:19:12 +0000952 dictentry *ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000953 assert(mp->ma_table != NULL);
Tim Peters0ab085c2001-09-14 00:25:33 +0000954 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +0000955 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000956 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000957 if (hash == -1)
958 return NULL;
959 }
Armin Rigo35f6d362006-06-01 13:19:12 +0000960 ep = (mp->ma_lookup)(mp, key, hash);
961 if (ep == NULL)
962 return NULL;
963 v = ep->me_value;
Guido van Rossum1968ad32006-02-25 22:38:04 +0000964 if (v == NULL) {
965 if (!PyDict_CheckExact(mp)) {
966 /* Look up __missing__ method if we're a subclass. */
Guido van Rossum4b92a822006-02-25 23:32:30 +0000967 PyObject *missing;
Guido van Rossum1968ad32006-02-25 22:38:04 +0000968 static PyObject *missing_str = NULL;
969 if (missing_str == NULL)
Tim Peters9b10f7e2006-05-30 04:16:25 +0000970 missing_str =
Guido van Rossum1968ad32006-02-25 22:38:04 +0000971 PyString_InternFromString("__missing__");
Guido van Rossum4b92a822006-02-25 23:32:30 +0000972 missing = _PyType_Lookup(mp->ob_type, missing_str);
Guido van Rossum1968ad32006-02-25 22:38:04 +0000973 if (missing != NULL)
974 return PyObject_CallFunctionObjArgs(missing,
975 (PyObject *)mp, key, NULL);
976 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000977 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum1968ad32006-02-25 22:38:04 +0000978 return NULL;
979 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000980 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000981 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000982 return v;
983}
984
985static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000986dict_ass_sub(dictobject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000987{
988 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000989 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000990 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000991 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000992}
993
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000994static PyMappingMethods dict_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000995 (lenfunc)dict_length, /*mp_length*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000996 (binaryfunc)dict_subscript, /*mp_subscript*/
997 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000998};
999
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001000static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001001dict_keys(register dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001002{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001003 register PyObject *v;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001004 register Py_ssize_t i, j;
Raymond Hettinger43442782004-03-17 21:55:03 +00001005 dictentry *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001006 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001007
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001008 again:
1009 n = mp->ma_used;
1010 v = PyList_New(n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001011 if (v == NULL)
1012 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001013 if (n != mp->ma_used) {
1014 /* Durnit. The allocations caused the dict to resize.
1015 * Just start over, this shouldn't normally happen.
1016 */
1017 Py_DECREF(v);
1018 goto again;
1019 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001020 ep = mp->ma_table;
1021 mask = mp->ma_mask;
1022 for (i = 0, j = 0; i <= mask; i++) {
1023 if (ep[i].me_value != NULL) {
1024 PyObject *key = ep[i].me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001025 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001026 PyList_SET_ITEM(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001027 j++;
1028 }
1029 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001030 assert(j == n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001031 return v;
1032}
1033
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001034static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001035dict_values(register dictobject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001036{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001037 register PyObject *v;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001038 register Py_ssize_t i, j;
Raymond Hettinger43442782004-03-17 21:55:03 +00001039 dictentry *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001040 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001041
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001042 again:
1043 n = mp->ma_used;
1044 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001045 if (v == NULL)
1046 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001047 if (n != mp->ma_used) {
1048 /* Durnit. The allocations caused the dict to resize.
1049 * Just start over, this shouldn't normally happen.
1050 */
1051 Py_DECREF(v);
1052 goto again;
1053 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001054 ep = mp->ma_table;
1055 mask = mp->ma_mask;
1056 for (i = 0, j = 0; i <= mask; i++) {
1057 if (ep[i].me_value != NULL) {
1058 PyObject *value = ep[i].me_value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001059 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001060 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001061 j++;
1062 }
1063 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001064 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001065 return v;
1066}
1067
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001068static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001069dict_items(register dictobject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001070{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001071 register PyObject *v;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001072 register Py_ssize_t i, j, n;
1073 Py_ssize_t mask;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001074 PyObject *item, *key, *value;
Raymond Hettinger43442782004-03-17 21:55:03 +00001075 dictentry *ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001076
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001077 /* Preallocate the list of tuples, to avoid allocations during
1078 * the loop over the items, which could trigger GC, which
1079 * could resize the dict. :-(
1080 */
1081 again:
1082 n = mp->ma_used;
1083 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001084 if (v == NULL)
1085 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001086 for (i = 0; i < n; i++) {
1087 item = PyTuple_New(2);
1088 if (item == NULL) {
1089 Py_DECREF(v);
1090 return NULL;
1091 }
1092 PyList_SET_ITEM(v, i, item);
1093 }
1094 if (n != mp->ma_used) {
1095 /* Durnit. The allocations caused the dict to resize.
1096 * Just start over, this shouldn't normally happen.
1097 */
1098 Py_DECREF(v);
1099 goto again;
1100 }
1101 /* Nothing we do below makes any function calls. */
Raymond Hettinger43442782004-03-17 21:55:03 +00001102 ep = mp->ma_table;
1103 mask = mp->ma_mask;
1104 for (i = 0, j = 0; i <= mask; i++) {
Raymond Hettinger06905122004-03-19 10:30:00 +00001105 if ((value=ep[i].me_value) != NULL) {
Raymond Hettinger43442782004-03-17 21:55:03 +00001106 key = ep[i].me_key;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001107 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001108 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001109 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001110 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001111 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001112 j++;
1113 }
1114 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001115 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001116 return v;
1117}
1118
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001119static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001120dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001121{
1122 PyObject *seq;
1123 PyObject *value = Py_None;
1124 PyObject *it; /* iter(seq) */
1125 PyObject *key;
1126 PyObject *d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001127 int status;
1128
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001129 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001130 return NULL;
1131
1132 d = PyObject_CallObject(cls, NULL);
1133 if (d == NULL)
1134 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001135
1136 it = PyObject_GetIter(seq);
1137 if (it == NULL){
1138 Py_DECREF(d);
1139 return NULL;
1140 }
1141
1142 for (;;) {
1143 key = PyIter_Next(it);
1144 if (key == NULL) {
1145 if (PyErr_Occurred())
1146 goto Fail;
1147 break;
1148 }
Raymond Hettingere03e5b12002-12-07 08:10:51 +00001149 status = PyObject_SetItem(d, key, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001150 Py_DECREF(key);
1151 if (status < 0)
1152 goto Fail;
1153 }
1154
1155 Py_DECREF(it);
1156 return d;
1157
1158Fail:
1159 Py_DECREF(it);
1160 Py_DECREF(d);
1161 return NULL;
1162}
1163
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001164static int
1165dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001166{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001167 PyObject *arg = NULL;
1168 int result = 0;
1169
1170 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1171 result = -1;
1172
1173 else if (arg != NULL) {
1174 if (PyObject_HasAttrString(arg, "keys"))
1175 result = PyDict_Merge(self, arg, 1);
1176 else
1177 result = PyDict_MergeFromSeq2(self, arg, 1);
1178 }
1179 if (result == 0 && kwds != NULL)
1180 result = PyDict_Merge(self, kwds, 1);
1181 return result;
1182}
1183
1184static PyObject *
1185dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1186{
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001187 if (dict_update_common(self, args, kwds, "update") != -1)
1188 Py_RETURN_NONE;
1189 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001190}
1191
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001192/* Update unconditionally replaces existing items.
1193 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001194 otherwise it leaves existing items unchanged.
1195
1196 PyDict_{Update,Merge} update/merge from a mapping object.
1197
Tim Petersf582b822001-12-11 18:51:08 +00001198 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001199 producing iterable objects of length 2.
1200*/
1201
Tim Petersf582b822001-12-11 18:51:08 +00001202int
Tim Peters1fc240e2001-10-26 05:06:50 +00001203PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1204{
1205 PyObject *it; /* iter(seq2) */
Tim Peters9b10f7e2006-05-30 04:16:25 +00001206 Py_ssize_t i; /* index into seq2 of current element */
Tim Peters1fc240e2001-10-26 05:06:50 +00001207 PyObject *item; /* seq2[i] */
1208 PyObject *fast; /* item as a 2-tuple or 2-list */
1209
1210 assert(d != NULL);
1211 assert(PyDict_Check(d));
1212 assert(seq2 != NULL);
1213
1214 it = PyObject_GetIter(seq2);
1215 if (it == NULL)
1216 return -1;
1217
1218 for (i = 0; ; ++i) {
1219 PyObject *key, *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001220 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001221
1222 fast = NULL;
1223 item = PyIter_Next(it);
1224 if (item == NULL) {
1225 if (PyErr_Occurred())
1226 goto Fail;
1227 break;
1228 }
1229
1230 /* Convert item to sequence, and verify length 2. */
1231 fast = PySequence_Fast(item, "");
1232 if (fast == NULL) {
1233 if (PyErr_ExceptionMatches(PyExc_TypeError))
1234 PyErr_Format(PyExc_TypeError,
1235 "cannot convert dictionary update "
Neal Norwitzd3881b02006-05-30 04:25:05 +00001236 "sequence element #%zd to a sequence",
Tim Peters1fc240e2001-10-26 05:06:50 +00001237 i);
1238 goto Fail;
1239 }
1240 n = PySequence_Fast_GET_SIZE(fast);
1241 if (n != 2) {
1242 PyErr_Format(PyExc_ValueError,
Neal Norwitzd3881b02006-05-30 04:25:05 +00001243 "dictionary update sequence element #%zd "
Martin v. Löwis2c95cc62006-02-16 06:54:25 +00001244 "has length %zd; 2 is required",
Martin v. Löwise0e89f72006-02-16 06:59:22 +00001245 i, n);
Tim Peters1fc240e2001-10-26 05:06:50 +00001246 goto Fail;
1247 }
1248
1249 /* Update/merge with this (key, value) pair. */
1250 key = PySequence_Fast_GET_ITEM(fast, 0);
1251 value = PySequence_Fast_GET_ITEM(fast, 1);
1252 if (override || PyDict_GetItem(d, key) == NULL) {
1253 int status = PyDict_SetItem(d, key, value);
1254 if (status < 0)
1255 goto Fail;
1256 }
1257 Py_DECREF(fast);
1258 Py_DECREF(item);
1259 }
1260
1261 i = 0;
1262 goto Return;
1263Fail:
1264 Py_XDECREF(item);
1265 Py_XDECREF(fast);
1266 i = -1;
1267Return:
1268 Py_DECREF(it);
Neal Norwitzd3881b02006-05-30 04:25:05 +00001269 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001270}
1271
Tim Peters6d6c1a32001-08-02 04:15:00 +00001272int
1273PyDict_Update(PyObject *a, PyObject *b)
1274{
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001275 return PyDict_Merge(a, b, 1);
1276}
1277
1278int
1279PyDict_Merge(PyObject *a, PyObject *b, int override)
1280{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001281 register PyDictObject *mp, *other;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001282 register Py_ssize_t i;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001283 dictentry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001284
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001285 /* We accept for the argument either a concrete dictionary object,
1286 * or an abstract "mapping" object. For the former, we can do
1287 * things quite efficiently. For the latter, we only require that
1288 * PyMapping_Keys() and PyObject_GetItem() be supported.
1289 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001290 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1291 PyErr_BadInternalCall();
1292 return -1;
1293 }
1294 mp = (dictobject*)a;
1295 if (PyDict_Check(b)) {
1296 other = (dictobject*)b;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001297 if (other == mp || other->ma_used == 0)
1298 /* a.update(a) or a.update({}); nothing to do */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001299 return 0;
Raymond Hettinger186e7392005-05-14 18:08:25 +00001300 if (mp->ma_used == 0)
1301 /* Since the target dict is empty, PyDict_GetItem()
1302 * always returns NULL. Setting override to 1
1303 * skips the unnecessary test.
1304 */
1305 override = 1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001306 /* Do one big resize at the start, rather than
1307 * incrementally resizing as we insert new items. Expect
1308 * that there will be no (or few) overlapping keys.
1309 */
1310 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
Raymond Hettingerc8d22902003-05-07 00:49:40 +00001311 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001312 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001313 }
1314 for (i = 0; i <= other->ma_mask; i++) {
1315 entry = &other->ma_table[i];
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001316 if (entry->me_value != NULL &&
1317 (override ||
1318 PyDict_GetItem(a, entry->me_key) == NULL)) {
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001319 Py_INCREF(entry->me_key);
1320 Py_INCREF(entry->me_value);
Armin Rigo35f6d362006-06-01 13:19:12 +00001321 if (insertdict(mp, entry->me_key,
1322 (long)entry->me_hash,
1323 entry->me_value) != 0)
1324 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001325 }
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001326 }
1327 }
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001328 else {
1329 /* Do it the generic, slower way */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001330 PyObject *keys = PyMapping_Keys(b);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001331 PyObject *iter;
1332 PyObject *key, *value;
1333 int status;
1334
1335 if (keys == NULL)
1336 /* Docstring says this is equivalent to E.keys() so
1337 * if E doesn't have a .keys() method we want
1338 * AttributeError to percolate up. Might as well
1339 * do the same for any other error.
1340 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001341 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001342
1343 iter = PyObject_GetIter(keys);
1344 Py_DECREF(keys);
1345 if (iter == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001346 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001347
1348 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001349 if (!override && PyDict_GetItem(a, key) != NULL) {
1350 Py_DECREF(key);
1351 continue;
1352 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001353 value = PyObject_GetItem(b, key);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001354 if (value == NULL) {
1355 Py_DECREF(iter);
1356 Py_DECREF(key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001357 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001358 }
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001359 status = PyDict_SetItem(a, key, value);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001360 Py_DECREF(key);
1361 Py_DECREF(value);
1362 if (status < 0) {
1363 Py_DECREF(iter);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001364 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001365 }
1366 }
1367 Py_DECREF(iter);
1368 if (PyErr_Occurred())
1369 /* Iterator completed, via error */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001370 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001371 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001372 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001373}
1374
1375static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001376dict_copy(register dictobject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001377{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001378 return PyDict_Copy((PyObject*)mp);
1379}
1380
1381PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001382PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001383{
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001384 PyObject *copy;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001385
1386 if (o == NULL || !PyDict_Check(o)) {
1387 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001388 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001389 }
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001390 copy = PyDict_New();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001391 if (copy == NULL)
1392 return NULL;
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001393 if (PyDict_Merge(copy, o, 1) == 0)
1394 return copy;
1395 Py_DECREF(copy);
Raymond Hettinger1356f782005-04-15 15:58:42 +00001396 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001397}
1398
Martin v. Löwis18e16552006-02-15 17:27:45 +00001399Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00001400PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001401{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001402 if (mp == NULL || !PyDict_Check(mp)) {
1403 PyErr_BadInternalCall();
Jeremy Hylton7083bb72004-02-17 20:10:11 +00001404 return -1;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001405 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001406 return ((dictobject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001407}
1408
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001409PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001410PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001411{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001412 if (mp == NULL || !PyDict_Check(mp)) {
1413 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001414 return NULL;
1415 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001416 return dict_keys((dictobject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001417}
1418
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001419PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001420PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001421{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001422 if (mp == NULL || !PyDict_Check(mp)) {
1423 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001424 return NULL;
1425 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001426 return dict_values((dictobject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001427}
1428
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001429PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001430PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001431{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001432 if (mp == NULL || !PyDict_Check(mp)) {
1433 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001434 return NULL;
1435 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001436 return dict_items((dictobject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001437}
1438
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001439/* Subroutine which returns the smallest key in a for which b's value
1440 is different or absent. The value is returned too, through the
Tim Peters95bf9392001-05-10 08:32:44 +00001441 pval argument. Both are NULL if no key in a is found for which b's status
1442 differs. The refcounts on (and only on) non-NULL *pval and function return
1443 values must be decremented by the caller (characterize() increments them
1444 to ensure that mutating comparison and PyDict_GetItem calls can't delete
1445 them before the caller is done looking at them). */
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001446
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001447static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001448characterize(dictobject *a, dictobject *b, PyObject **pval)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001449{
Tim Peters95bf9392001-05-10 08:32:44 +00001450 PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */
1451 PyObject *aval = NULL; /* a[akey] */
Tim Peters9b10f7e2006-05-30 04:16:25 +00001452 Py_ssize_t i;
1453 int cmp;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001454
Tim Petersafb6ae82001-06-04 21:00:21 +00001455 for (i = 0; i <= a->ma_mask; i++) {
Tim Peters95bf9392001-05-10 08:32:44 +00001456 PyObject *thiskey, *thisaval, *thisbval;
1457 if (a->ma_table[i].me_value == NULL)
1458 continue;
1459 thiskey = a->ma_table[i].me_key;
1460 Py_INCREF(thiskey); /* keep alive across compares */
1461 if (akey != NULL) {
1462 cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT);
1463 if (cmp < 0) {
1464 Py_DECREF(thiskey);
1465 goto Fail;
1466 }
1467 if (cmp > 0 ||
Tim Petersafb6ae82001-06-04 21:00:21 +00001468 i > a->ma_mask ||
Tim Peters95bf9392001-05-10 08:32:44 +00001469 a->ma_table[i].me_value == NULL)
1470 {
1471 /* Not the *smallest* a key; or maybe it is
1472 * but the compare shrunk the dict so we can't
1473 * find its associated value anymore; or
1474 * maybe it is but the compare deleted the
1475 * a[thiskey] entry.
Neal Norwitzd3881b02006-05-30 04:25:05 +00001476 */
Tim Peters95bf9392001-05-10 08:32:44 +00001477 Py_DECREF(thiskey);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001478 continue;
Guido van Rossumb9324202001-01-18 00:39:02 +00001479 }
Tim Peters95bf9392001-05-10 08:32:44 +00001480 }
1481
1482 /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */
1483 thisaval = a->ma_table[i].me_value;
1484 assert(thisaval);
1485 Py_INCREF(thisaval); /* keep alive */
1486 thisbval = PyDict_GetItem((PyObject *)b, thiskey);
1487 if (thisbval == NULL)
1488 cmp = 0;
1489 else {
1490 /* both dicts have thiskey: same values? */
1491 cmp = PyObject_RichCompareBool(
1492 thisaval, thisbval, Py_EQ);
1493 if (cmp < 0) {
1494 Py_DECREF(thiskey);
1495 Py_DECREF(thisaval);
1496 goto Fail;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001497 }
1498 }
Tim Peters95bf9392001-05-10 08:32:44 +00001499 if (cmp == 0) {
1500 /* New winner. */
1501 Py_XDECREF(akey);
1502 Py_XDECREF(aval);
1503 akey = thiskey;
1504 aval = thisaval;
1505 }
1506 else {
1507 Py_DECREF(thiskey);
1508 Py_DECREF(thisaval);
1509 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001510 }
Tim Peters95bf9392001-05-10 08:32:44 +00001511 *pval = aval;
1512 return akey;
1513
1514Fail:
1515 Py_XDECREF(akey);
1516 Py_XDECREF(aval);
1517 *pval = NULL;
1518 return NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001519}
1520
1521static int
Tim Peters1f5871e2000-07-04 17:44:48 +00001522dict_compare(dictobject *a, dictobject *b)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001523{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001524 PyObject *adiff, *bdiff, *aval, *bval;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001525 int res;
1526
1527 /* Compare lengths first */
1528 if (a->ma_used < b->ma_used)
1529 return -1; /* a is shorter */
1530 else if (a->ma_used > b->ma_used)
1531 return 1; /* b is shorter */
Tim Peters95bf9392001-05-10 08:32:44 +00001532
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001533 /* Same length -- check all keys */
Tim Peters95bf9392001-05-10 08:32:44 +00001534 bdiff = bval = NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001535 adiff = characterize(a, b, &aval);
Tim Peters95bf9392001-05-10 08:32:44 +00001536 if (adiff == NULL) {
1537 assert(!aval);
Tim Peters3918fb22001-05-10 18:58:31 +00001538 /* Either an error, or a is a subset with the same length so
Tim Peters95bf9392001-05-10 08:32:44 +00001539 * must be equal.
1540 */
1541 res = PyErr_Occurred() ? -1 : 0;
1542 goto Finished;
1543 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001544 bdiff = characterize(b, a, &bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001545 if (bdiff == NULL && PyErr_Occurred()) {
1546 assert(!bval);
1547 res = -1;
1548 goto Finished;
1549 }
1550 res = 0;
1551 if (bdiff) {
1552 /* bdiff == NULL "should be" impossible now, but perhaps
1553 * the last comparison done by the characterize() on a had
1554 * the side effect of making the dicts equal!
1555 */
1556 res = PyObject_Compare(adiff, bdiff);
1557 }
1558 if (res == 0 && bval != NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001559 res = PyObject_Compare(aval, bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001560
1561Finished:
1562 Py_XDECREF(adiff);
1563 Py_XDECREF(bdiff);
1564 Py_XDECREF(aval);
1565 Py_XDECREF(bval);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001566 return res;
1567}
1568
Tim Peterse63415e2001-05-08 04:38:29 +00001569/* Return 1 if dicts equal, 0 if not, -1 if error.
1570 * Gets out as soon as any difference is detected.
1571 * Uses only Py_EQ comparison.
1572 */
1573static int
1574dict_equal(dictobject *a, dictobject *b)
1575{
Tim Peters9b10f7e2006-05-30 04:16:25 +00001576 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00001577
1578 if (a->ma_used != b->ma_used)
1579 /* can't be equal if # of entries differ */
1580 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001581
Tim Peterse63415e2001-05-08 04:38:29 +00001582 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Tim Petersafb6ae82001-06-04 21:00:21 +00001583 for (i = 0; i <= a->ma_mask; i++) {
Tim Peterse63415e2001-05-08 04:38:29 +00001584 PyObject *aval = a->ma_table[i].me_value;
1585 if (aval != NULL) {
1586 int cmp;
1587 PyObject *bval;
1588 PyObject *key = a->ma_table[i].me_key;
1589 /* temporarily bump aval's refcount to ensure it stays
1590 alive until we're done with it */
1591 Py_INCREF(aval);
Neal Norwitza22975f2006-09-05 02:24:03 +00001592 /* ditto for key */
1593 Py_INCREF(key);
Tim Peterse63415e2001-05-08 04:38:29 +00001594 bval = PyDict_GetItem((PyObject *)b, key);
Neal Norwitza22975f2006-09-05 02:24:03 +00001595 Py_DECREF(key);
Tim Peterse63415e2001-05-08 04:38:29 +00001596 if (bval == NULL) {
1597 Py_DECREF(aval);
1598 return 0;
1599 }
1600 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1601 Py_DECREF(aval);
1602 if (cmp <= 0) /* error or not equal */
1603 return cmp;
1604 }
1605 }
1606 return 1;
1607 }
1608
1609static PyObject *
1610dict_richcompare(PyObject *v, PyObject *w, int op)
1611{
1612 int cmp;
1613 PyObject *res;
1614
1615 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1616 res = Py_NotImplemented;
1617 }
1618 else if (op == Py_EQ || op == Py_NE) {
1619 cmp = dict_equal((dictobject *)v, (dictobject *)w);
1620 if (cmp < 0)
1621 return NULL;
1622 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1623 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001624 else
1625 res = Py_NotImplemented;
Tim Peterse63415e2001-05-08 04:38:29 +00001626 Py_INCREF(res);
1627 return res;
1628 }
1629
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001630static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001631dict_has_key(register dictobject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001632{
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001633 long hash;
Armin Rigo35f6d362006-06-01 13:19:12 +00001634 dictentry *ep;
Tim Petersd770ebd2006-06-01 15:50:44 +00001635
Tim Peters0ab085c2001-09-14 00:25:33 +00001636 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001637 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001638 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001639 if (hash == -1)
1640 return NULL;
1641 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001642 ep = (mp->ma_lookup)(mp, key, hash);
1643 if (ep == NULL)
1644 return NULL;
Tim Petersd770ebd2006-06-01 15:50:44 +00001645 return PyBool_FromLong(ep->me_value != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001646}
1647
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001648static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001649dict_get(register dictobject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001650{
1651 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001652 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001653 PyObject *val = NULL;
1654 long hash;
Armin Rigo35f6d362006-06-01 13:19:12 +00001655 dictentry *ep;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001656
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001657 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001658 return NULL;
1659
Tim Peters0ab085c2001-09-14 00:25:33 +00001660 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001661 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Barry Warsawc38c5da1997-10-06 17:49:20 +00001662 hash = PyObject_Hash(key);
1663 if (hash == -1)
1664 return NULL;
1665 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001666 ep = (mp->ma_lookup)(mp, key, hash);
1667 if (ep == NULL)
1668 return NULL;
1669 val = ep->me_value;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001670 if (val == NULL)
1671 val = failobj;
1672 Py_INCREF(val);
1673 return val;
1674}
1675
1676
1677static PyObject *
Guido van Rossum164452c2000-08-08 16:12:54 +00001678dict_setdefault(register dictobject *mp, PyObject *args)
1679{
1680 PyObject *key;
1681 PyObject *failobj = Py_None;
1682 PyObject *val = NULL;
1683 long hash;
Armin Rigo35f6d362006-06-01 13:19:12 +00001684 dictentry *ep;
Guido van Rossum164452c2000-08-08 16:12:54 +00001685
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001686 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
Guido van Rossum164452c2000-08-08 16:12:54 +00001687 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001688
Tim Peters0ab085c2001-09-14 00:25:33 +00001689 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001690 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossum164452c2000-08-08 16:12:54 +00001691 hash = PyObject_Hash(key);
1692 if (hash == -1)
1693 return NULL;
1694 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001695 ep = (mp->ma_lookup)(mp, key, hash);
1696 if (ep == NULL)
1697 return NULL;
1698 val = ep->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001699 if (val == NULL) {
1700 val = failobj;
1701 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1702 val = NULL;
1703 }
1704 Py_XINCREF(val);
1705 return val;
1706}
1707
1708
1709static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001710dict_clear(register dictobject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001711{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001712 PyDict_Clear((PyObject *)mp);
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001713 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001714}
1715
Guido van Rossumba6ab842000-12-12 22:02:18 +00001716static PyObject *
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001717dict_pop(dictobject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00001718{
1719 long hash;
1720 dictentry *ep;
1721 PyObject *old_value, *old_key;
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001722 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001723
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001724 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1725 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001726 if (mp->ma_used == 0) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001727 if (deflt) {
1728 Py_INCREF(deflt);
1729 return deflt;
1730 }
Guido van Rossume027d982002-04-12 15:11:59 +00001731 PyErr_SetString(PyExc_KeyError,
1732 "pop(): dictionary is empty");
1733 return NULL;
1734 }
1735 if (!PyString_CheckExact(key) ||
1736 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1737 hash = PyObject_Hash(key);
1738 if (hash == -1)
1739 return NULL;
1740 }
1741 ep = (mp->ma_lookup)(mp, key, hash);
Armin Rigo35f6d362006-06-01 13:19:12 +00001742 if (ep == NULL)
1743 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001744 if (ep->me_value == NULL) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001745 if (deflt) {
1746 Py_INCREF(deflt);
1747 return deflt;
1748 }
Guido van Rossume027d982002-04-12 15:11:59 +00001749 PyErr_SetObject(PyExc_KeyError, key);
1750 return NULL;
1751 }
1752 old_key = ep->me_key;
1753 Py_INCREF(dummy);
1754 ep->me_key = dummy;
1755 old_value = ep->me_value;
1756 ep->me_value = NULL;
1757 mp->ma_used--;
1758 Py_DECREF(old_key);
1759 return old_value;
1760}
1761
1762static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001763dict_popitem(dictobject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001764{
Tim Peters9b10f7e2006-05-30 04:16:25 +00001765 Py_ssize_t i = 0;
Guido van Rossumba6ab842000-12-12 22:02:18 +00001766 dictentry *ep;
1767 PyObject *res;
1768
Tim Petersf4b33f62001-06-02 05:42:29 +00001769 /* Allocate the result tuple before checking the size. Believe it
1770 * or not, this allocation could trigger a garbage collection which
1771 * could empty the dict, so if we checked the size first and that
1772 * happened, the result would be an infinite loop (searching for an
1773 * entry that no longer exists). Note that the usual popitem()
1774 * idiom is "while d: k, v = d.popitem()". so needing to throw the
Tim Peters9b10f7e2006-05-30 04:16:25 +00001775 * tuple away if the dict *is* empty isn't a significant
Tim Petersf4b33f62001-06-02 05:42:29 +00001776 * inefficiency -- possible, but unlikely in practice.
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001777 */
1778 res = PyTuple_New(2);
1779 if (res == NULL)
1780 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001781 if (mp->ma_used == 0) {
1782 Py_DECREF(res);
1783 PyErr_SetString(PyExc_KeyError,
1784 "popitem(): dictionary is empty");
1785 return NULL;
1786 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001787 /* Set ep to "the first" dict entry with a value. We abuse the hash
1788 * field of slot 0 to hold a search finger:
1789 * If slot 0 has a value, use slot 0.
1790 * Else slot 0 is being used to hold a search finger,
Neal Norwitzd3881b02006-05-30 04:25:05 +00001791 * and we use its hash value as the first index to look.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001792 */
1793 ep = &mp->ma_table[0];
1794 if (ep->me_value == NULL) {
Tim Peters9b10f7e2006-05-30 04:16:25 +00001795 i = ep->me_hash;
Tim Petersdea48ec2001-05-22 20:40:22 +00001796 /* The hash field may be a real hash value, or it may be a
1797 * legit search finger, or it may be a once-legit search
1798 * finger that's out of bounds now because it wrapped around
1799 * or the table shrunk -- simply make sure it's in bounds now.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001800 */
Tim Petersafb6ae82001-06-04 21:00:21 +00001801 if (i > mp->ma_mask || i < 1)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001802 i = 1; /* skip slot 0 */
1803 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1804 i++;
Tim Petersafb6ae82001-06-04 21:00:21 +00001805 if (i > mp->ma_mask)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001806 i = 1;
1807 }
1808 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001809 PyTuple_SET_ITEM(res, 0, ep->me_key);
1810 PyTuple_SET_ITEM(res, 1, ep->me_value);
1811 Py_INCREF(dummy);
1812 ep->me_key = dummy;
1813 ep->me_value = NULL;
1814 mp->ma_used--;
1815 assert(mp->ma_table[0].me_value == NULL);
1816 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001817 return res;
1818}
1819
Jeremy Hylton8caad492000-06-23 14:18:11 +00001820static int
1821dict_traverse(PyObject *op, visitproc visit, void *arg)
1822{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001823 Py_ssize_t i = 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001824 PyObject *pk;
1825 PyObject *pv;
1826
1827 while (PyDict_Next(op, &i, &pk, &pv)) {
Thomas Woutersc6e55062006-04-15 21:47:09 +00001828 Py_VISIT(pk);
1829 Py_VISIT(pv);
Jeremy Hylton8caad492000-06-23 14:18:11 +00001830 }
1831 return 0;
1832}
1833
1834static int
1835dict_tp_clear(PyObject *op)
1836{
1837 PyDict_Clear(op);
1838 return 0;
1839}
1840
Tim Petersf7f88b12000-12-13 23:18:45 +00001841
Raymond Hettinger019a1482004-03-18 02:41:19 +00001842extern PyTypeObject PyDictIterKey_Type; /* Forward */
1843extern PyTypeObject PyDictIterValue_Type; /* Forward */
1844extern PyTypeObject PyDictIterItem_Type; /* Forward */
1845static PyObject *dictiter_new(dictobject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001846
1847static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001848dict_iterkeys(dictobject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001849{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001850 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001851}
1852
1853static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001854dict_itervalues(dictobject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001855{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001856 return dictiter_new(dict, &PyDictIterValue_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001857}
1858
1859static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001860dict_iteritems(dictobject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001861{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001862 return dictiter_new(dict, &PyDictIterItem_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001863}
1864
1865
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001866PyDoc_STRVAR(has_key__doc__,
Raymond Hettinger574aa322003-09-01 22:12:08 +00001867"D.has_key(k) -> True if D has a key k, else False");
Tim Petersf7f88b12000-12-13 23:18:45 +00001868
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001869PyDoc_STRVAR(contains__doc__,
1870"D.__contains__(k) -> True if D has a key k, else False");
1871
1872PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
1873
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001874PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001875"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001876
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001877PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001878"D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D");
Tim Petersf7f88b12000-12-13 23:18:45 +00001879
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001880PyDoc_STRVAR(pop__doc__,
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001881"D.pop(k[,d]) -> v, remove specified key and return the corresponding value\n\
1882If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00001883
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001884PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00001885"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000018862-tuple; but raise KeyError if D is empty");
Tim Petersf7f88b12000-12-13 23:18:45 +00001887
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001888PyDoc_STRVAR(keys__doc__,
1889"D.keys() -> list of D's keys");
Tim Petersf7f88b12000-12-13 23:18:45 +00001890
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001891PyDoc_STRVAR(items__doc__,
1892"D.items() -> list of D's (key, value) pairs, as 2-tuples");
Tim Petersf7f88b12000-12-13 23:18:45 +00001893
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001894PyDoc_STRVAR(values__doc__,
1895"D.values() -> list of D's values");
Tim Petersf7f88b12000-12-13 23:18:45 +00001896
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001897PyDoc_STRVAR(update__doc__,
Walter Dörwaldd70ad8a2004-05-28 20:59:21 +00001898"D.update(E, **F) -> None. Update D from E and F: for k in E: D[k] = E[k]\n\
1899(if E has keys else: for (k, v) in E: D[k] = v) then: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00001900
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001901PyDoc_STRVAR(fromkeys__doc__,
1902"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
1903v defaults to None.");
1904
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001905PyDoc_STRVAR(clear__doc__,
1906"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001907
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001908PyDoc_STRVAR(copy__doc__,
1909"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00001910
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001911PyDoc_STRVAR(iterkeys__doc__,
1912"D.iterkeys() -> an iterator over the keys of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00001913
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001914PyDoc_STRVAR(itervalues__doc__,
1915"D.itervalues() -> an iterator over the values of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00001916
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001917PyDoc_STRVAR(iteritems__doc__,
1918"D.iteritems() -> an iterator over the (key, value) items of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00001919
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001920static PyMethodDef mapp_methods[] = {
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001921 {"__contains__",(PyCFunction)dict_has_key, METH_O | METH_COEXIST,
1922 contains__doc__},
Raymond Hettinger0c669672003-12-13 13:31:55 +00001923 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001924 getitem__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001925 {"has_key", (PyCFunction)dict_has_key, METH_O,
Tim Petersf7f88b12000-12-13 23:18:45 +00001926 has_key__doc__},
1927 {"get", (PyCFunction)dict_get, METH_VARARGS,
1928 get__doc__},
1929 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
1930 setdefault_doc__},
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001931 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
Just van Rossuma797d812002-11-23 09:45:04 +00001932 pop__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001933 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001934 popitem__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001935 {"keys", (PyCFunction)dict_keys, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001936 keys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001937 {"items", (PyCFunction)dict_items, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001938 items__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001939 {"values", (PyCFunction)dict_values, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001940 values__doc__},
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001941 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001942 update__doc__},
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001943 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
1944 fromkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001945 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001946 clear__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001947 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001948 copy__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001949 {"iterkeys", (PyCFunction)dict_iterkeys, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00001950 iterkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001951 {"itervalues", (PyCFunction)dict_itervalues, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00001952 itervalues__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001953 {"iteritems", (PyCFunction)dict_iteritems, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00001954 iteritems__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00001955 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001956};
1957
Tim Petersd770ebd2006-06-01 15:50:44 +00001958/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001959int
1960PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001961{
1962 long hash;
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001963 dictobject *mp = (dictobject *)op;
Armin Rigo35f6d362006-06-01 13:19:12 +00001964 dictentry *ep;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001965
Tim Peters0ab085c2001-09-14 00:25:33 +00001966 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001967 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001968 hash = PyObject_Hash(key);
1969 if (hash == -1)
1970 return -1;
1971 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001972 ep = (mp->ma_lookup)(mp, key, hash);
Tim Petersd770ebd2006-06-01 15:50:44 +00001973 return ep == NULL ? -1 : (ep->me_value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001974}
1975
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001976/* Hack to implement "key in dict" */
1977static PySequenceMethods dict_as_sequence = {
Georg Brandl347b3002006-03-30 11:57:00 +00001978 0, /* sq_length */
1979 0, /* sq_concat */
1980 0, /* sq_repeat */
1981 0, /* sq_item */
1982 0, /* sq_slice */
1983 0, /* sq_ass_item */
1984 0, /* sq_ass_slice */
1985 PyDict_Contains, /* sq_contains */
1986 0, /* sq_inplace_concat */
1987 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001988};
1989
Guido van Rossum09e563a2001-05-01 12:10:21 +00001990static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001991dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1992{
1993 PyObject *self;
1994
1995 assert(type != NULL && type->tp_alloc != NULL);
1996 self = type->tp_alloc(type, 0);
1997 if (self != NULL) {
1998 PyDictObject *d = (PyDictObject *)self;
1999 /* It's guaranteed that tp->alloc zeroed out the struct. */
2000 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
2001 INIT_NONZERO_DICT_SLOTS(d);
2002 d->ma_lookup = lookdict_string;
2003#ifdef SHOW_CONVERSION_COUNTS
2004 ++created;
2005#endif
2006 }
2007 return self;
2008}
2009
Tim Peters25786c02001-09-02 08:22:48 +00002010static int
2011dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2012{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002013 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002014}
2015
Guido van Rossumdbb53d92001-12-03 16:32:18 +00002016static long
2017dict_nohash(PyObject *self)
2018{
2019 PyErr_SetString(PyExc_TypeError, "dict objects are unhashable");
2020 return -1;
2021}
2022
Tim Peters6d6c1a32001-08-02 04:15:00 +00002023static PyObject *
Guido van Rossum09e563a2001-05-01 12:10:21 +00002024dict_iter(dictobject *dict)
2025{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002026 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002027}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002028
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002029PyDoc_STRVAR(dictionary_doc,
Tim Petersa427a2b2001-10-29 22:25:45 +00002030"dict() -> new empty dictionary.\n"
2031"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Tim Peters1fc240e2001-10-26 05:06:50 +00002032" (key, value) pairs.\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002033"dict(seq) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002034" d = {}\n"
2035" for k, v in seq:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002036" d[k] = v\n"
2037"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2038" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002039
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002040PyTypeObject PyDict_Type = {
2041 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002042 0,
Tim Petersa427a2b2001-10-29 22:25:45 +00002043 "dict",
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002044 sizeof(dictobject),
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002045 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00002046 (destructor)dict_dealloc, /* tp_dealloc */
2047 (printfunc)dict_print, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002048 0, /* tp_getattr */
Guido van Rossumb9324202001-01-18 00:39:02 +00002049 0, /* tp_setattr */
Tim Peters4fa58bf2001-05-10 21:45:19 +00002050 (cmpfunc)dict_compare, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00002051 (reprfunc)dict_repr, /* tp_repr */
2052 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002053 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00002054 &dict_as_mapping, /* tp_as_mapping */
Guido van Rossumdbb53d92001-12-03 16:32:18 +00002055 dict_nohash, /* tp_hash */
Guido van Rossumb9324202001-01-18 00:39:02 +00002056 0, /* tp_call */
2057 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002058 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumb9324202001-01-18 00:39:02 +00002059 0, /* tp_setattro */
2060 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002061 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Tim Peters6d6c1a32001-08-02 04:15:00 +00002062 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters25786c02001-09-02 08:22:48 +00002063 dictionary_doc, /* tp_doc */
Georg Brandl347b3002006-03-30 11:57:00 +00002064 dict_traverse, /* tp_traverse */
2065 dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00002066 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002067 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00002068 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002069 0, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002070 mapp_methods, /* tp_methods */
2071 0, /* tp_members */
2072 0, /* tp_getset */
2073 0, /* tp_base */
2074 0, /* tp_dict */
2075 0, /* tp_descr_get */
2076 0, /* tp_descr_set */
2077 0, /* tp_dictoffset */
Georg Brandl347b3002006-03-30 11:57:00 +00002078 dict_init, /* tp_init */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002079 PyType_GenericAlloc, /* tp_alloc */
2080 dict_new, /* tp_new */
Neil Schemenauer6189b892002-04-12 02:43:00 +00002081 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002082};
2083
Guido van Rossum3cca2451997-05-16 14:23:33 +00002084/* For backward compatibility with old dictionary interface */
2085
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002086PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002087PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002088{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002089 PyObject *kv, *rv;
2090 kv = PyString_FromString(key);
2091 if (kv == NULL)
2092 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002093 rv = PyDict_GetItem(v, kv);
2094 Py_DECREF(kv);
2095 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002096}
2097
2098int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002099PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002100{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002101 PyObject *kv;
2102 int err;
2103 kv = PyString_FromString(key);
2104 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002105 return -1;
Guido van Rossum4f3bf1e1997-09-29 23:31:11 +00002106 PyString_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00002107 err = PyDict_SetItem(v, kv, item);
2108 Py_DECREF(kv);
2109 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002110}
2111
2112int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002113PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002114{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002115 PyObject *kv;
2116 int err;
2117 kv = PyString_FromString(key);
2118 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002119 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002120 err = PyDict_DelItem(v, kv);
2121 Py_DECREF(kv);
2122 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002123}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002124
Raymond Hettinger019a1482004-03-18 02:41:19 +00002125/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002126
2127typedef struct {
2128 PyObject_HEAD
Guido van Rossum2147df72002-07-16 20:30:22 +00002129 dictobject *di_dict; /* Set to NULL when iterator is exhausted */
Tim Peters9b10f7e2006-05-30 04:16:25 +00002130 Py_ssize_t di_used;
2131 Py_ssize_t di_pos;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002132 PyObject* di_result; /* reusable result tuple for iteritems */
Tim Peters9b10f7e2006-05-30 04:16:25 +00002133 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002134} dictiterobject;
2135
2136static PyObject *
Raymond Hettinger019a1482004-03-18 02:41:19 +00002137dictiter_new(dictobject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002138{
2139 dictiterobject *di;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002140 di = PyObject_New(dictiterobject, itertype);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002141 if (di == NULL)
2142 return NULL;
2143 Py_INCREF(dict);
2144 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00002145 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002146 di->di_pos = 0;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002147 di->len = dict->ma_used;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002148 if (itertype == &PyDictIterItem_Type) {
2149 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2150 if (di->di_result == NULL) {
2151 Py_DECREF(di);
2152 return NULL;
2153 }
2154 }
2155 else
2156 di->di_result = NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002157 return (PyObject *)di;
2158}
2159
2160static void
2161dictiter_dealloc(dictiterobject *di)
2162{
Guido van Rossum2147df72002-07-16 20:30:22 +00002163 Py_XDECREF(di->di_dict);
Raymond Hettinger019a1482004-03-18 02:41:19 +00002164 Py_XDECREF(di->di_result);
Neil Schemenauer6189b892002-04-12 02:43:00 +00002165 PyObject_Del(di);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002166}
2167
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002168static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002169dictiter_len(dictiterobject *di)
2170{
Tim Peters9b10f7e2006-05-30 04:16:25 +00002171 Py_ssize_t len = 0;
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00002172 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002173 len = di->len;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002174 return PyInt_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002175}
2176
Armin Rigof5b3e362006-02-11 21:32:43 +00002177PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002178
2179static PyMethodDef dictiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +00002180 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002181 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002182};
2183
Raymond Hettinger019a1482004-03-18 02:41:19 +00002184static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002185{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002186 PyObject *key;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002187 register Py_ssize_t i, mask;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002188 register dictentry *ep;
2189 dictobject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002190
Raymond Hettinger019a1482004-03-18 02:41:19 +00002191 if (d == NULL)
Guido van Rossum2147df72002-07-16 20:30:22 +00002192 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002193 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002194
Raymond Hettinger019a1482004-03-18 02:41:19 +00002195 if (di->di_used != d->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00002196 PyErr_SetString(PyExc_RuntimeError,
2197 "dictionary changed size during iteration");
Guido van Rossum2147df72002-07-16 20:30:22 +00002198 di->di_used = -1; /* Make this state sticky */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002199 return NULL;
2200 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002201
Raymond Hettinger019a1482004-03-18 02:41:19 +00002202 i = di->di_pos;
2203 if (i < 0)
2204 goto fail;
2205 ep = d->ma_table;
2206 mask = d->ma_mask;
2207 while (i <= mask && ep[i].me_value == NULL)
2208 i++;
2209 di->di_pos = i+1;
2210 if (i > mask)
2211 goto fail;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002212 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002213 key = ep[i].me_key;
2214 Py_INCREF(key);
2215 return key;
2216
2217fail:
2218 Py_DECREF(d);
Guido van Rossum2147df72002-07-16 20:30:22 +00002219 di->di_dict = NULL;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002220 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002221}
2222
Raymond Hettinger019a1482004-03-18 02:41:19 +00002223PyTypeObject PyDictIterKey_Type = {
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002224 PyObject_HEAD_INIT(&PyType_Type)
2225 0, /* ob_size */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002226 "dictionary-keyiterator", /* tp_name */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002227 sizeof(dictiterobject), /* tp_basicsize */
2228 0, /* tp_itemsize */
2229 /* methods */
2230 (destructor)dictiter_dealloc, /* tp_dealloc */
2231 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002232 0, /* tp_getattr */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002233 0, /* tp_setattr */
2234 0, /* tp_compare */
2235 0, /* tp_repr */
2236 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002237 0, /* tp_as_sequence */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002238 0, /* tp_as_mapping */
2239 0, /* tp_hash */
2240 0, /* tp_call */
2241 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002242 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002243 0, /* tp_setattro */
2244 0, /* tp_as_buffer */
2245 Py_TPFLAGS_DEFAULT, /* tp_flags */
2246 0, /* tp_doc */
2247 0, /* tp_traverse */
2248 0, /* tp_clear */
2249 0, /* tp_richcompare */
2250 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002251 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002252 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002253 dictiter_methods, /* tp_methods */
2254 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002255};
2256
2257static PyObject *dictiter_iternextvalue(dictiterobject *di)
2258{
2259 PyObject *value;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002260 register Py_ssize_t i, mask;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002261 register dictentry *ep;
2262 dictobject *d = di->di_dict;
2263
2264 if (d == NULL)
2265 return NULL;
2266 assert (PyDict_Check(d));
2267
2268 if (di->di_used != d->ma_used) {
2269 PyErr_SetString(PyExc_RuntimeError,
2270 "dictionary changed size during iteration");
2271 di->di_used = -1; /* Make this state sticky */
2272 return NULL;
2273 }
2274
2275 i = di->di_pos;
Guido van Rossum09240f62004-03-20 19:11:58 +00002276 mask = d->ma_mask;
2277 if (i < 0 || i > mask)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002278 goto fail;
2279 ep = d->ma_table;
Guido van Rossum09240f62004-03-20 19:11:58 +00002280 while ((value=ep[i].me_value) == NULL) {
Raymond Hettinger019a1482004-03-18 02:41:19 +00002281 i++;
Guido van Rossum09240f62004-03-20 19:11:58 +00002282 if (i > mask)
2283 goto fail;
2284 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002285 di->di_pos = i+1;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002286 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002287 Py_INCREF(value);
2288 return value;
2289
2290fail:
2291 Py_DECREF(d);
2292 di->di_dict = NULL;
2293 return NULL;
2294}
2295
2296PyTypeObject PyDictIterValue_Type = {
2297 PyObject_HEAD_INIT(&PyType_Type)
2298 0, /* ob_size */
2299 "dictionary-valueiterator", /* tp_name */
2300 sizeof(dictiterobject), /* tp_basicsize */
2301 0, /* tp_itemsize */
2302 /* methods */
2303 (destructor)dictiter_dealloc, /* tp_dealloc */
2304 0, /* tp_print */
2305 0, /* tp_getattr */
2306 0, /* tp_setattr */
2307 0, /* tp_compare */
2308 0, /* tp_repr */
2309 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002310 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002311 0, /* tp_as_mapping */
2312 0, /* tp_hash */
2313 0, /* tp_call */
2314 0, /* tp_str */
2315 PyObject_GenericGetAttr, /* tp_getattro */
2316 0, /* tp_setattro */
2317 0, /* tp_as_buffer */
2318 Py_TPFLAGS_DEFAULT, /* tp_flags */
2319 0, /* tp_doc */
2320 0, /* tp_traverse */
2321 0, /* tp_clear */
2322 0, /* tp_richcompare */
2323 0, /* tp_weaklistoffset */
2324 PyObject_SelfIter, /* tp_iter */
2325 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002326 dictiter_methods, /* tp_methods */
2327 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002328};
2329
2330static PyObject *dictiter_iternextitem(dictiterobject *di)
2331{
2332 PyObject *key, *value, *result = di->di_result;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002333 register Py_ssize_t i, mask;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002334 register dictentry *ep;
2335 dictobject *d = di->di_dict;
2336
2337 if (d == NULL)
2338 return NULL;
2339 assert (PyDict_Check(d));
2340
2341 if (di->di_used != d->ma_used) {
2342 PyErr_SetString(PyExc_RuntimeError,
2343 "dictionary changed size during iteration");
2344 di->di_used = -1; /* Make this state sticky */
2345 return NULL;
2346 }
2347
2348 i = di->di_pos;
2349 if (i < 0)
2350 goto fail;
2351 ep = d->ma_table;
2352 mask = d->ma_mask;
2353 while (i <= mask && ep[i].me_value == NULL)
2354 i++;
2355 di->di_pos = i+1;
2356 if (i > mask)
2357 goto fail;
2358
2359 if (result->ob_refcnt == 1) {
2360 Py_INCREF(result);
2361 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2362 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2363 } else {
2364 result = PyTuple_New(2);
Tim Peters60b29962006-01-01 01:19:23 +00002365 if (result == NULL)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002366 return NULL;
2367 }
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002368 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002369 key = ep[i].me_key;
2370 value = ep[i].me_value;
2371 Py_INCREF(key);
2372 Py_INCREF(value);
2373 PyTuple_SET_ITEM(result, 0, key);
2374 PyTuple_SET_ITEM(result, 1, value);
2375 return result;
2376
2377fail:
2378 Py_DECREF(d);
2379 di->di_dict = NULL;
2380 return NULL;
2381}
2382
2383PyTypeObject PyDictIterItem_Type = {
2384 PyObject_HEAD_INIT(&PyType_Type)
2385 0, /* ob_size */
2386 "dictionary-itemiterator", /* tp_name */
2387 sizeof(dictiterobject), /* tp_basicsize */
2388 0, /* tp_itemsize */
2389 /* methods */
2390 (destructor)dictiter_dealloc, /* tp_dealloc */
2391 0, /* tp_print */
2392 0, /* tp_getattr */
2393 0, /* tp_setattr */
2394 0, /* tp_compare */
2395 0, /* tp_repr */
2396 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002397 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002398 0, /* tp_as_mapping */
2399 0, /* tp_hash */
2400 0, /* tp_call */
2401 0, /* tp_str */
2402 PyObject_GenericGetAttr, /* tp_getattro */
2403 0, /* tp_setattro */
2404 0, /* tp_as_buffer */
2405 Py_TPFLAGS_DEFAULT, /* tp_flags */
2406 0, /* tp_doc */
2407 0, /* tp_traverse */
2408 0, /* tp_clear */
2409 0, /* tp_richcompare */
2410 0, /* tp_weaklistoffset */
2411 PyObject_SelfIter, /* tp_iter */
2412 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002413 dictiter_methods, /* tp_methods */
2414 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002415};