blob: 410b9670e4b90b3fd6da210d9b5bc80744e8b541 [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
230This function must never return NULL; failures are indicated by returning
231a dictentry* for which the me_value field is NULL. Exceptions are never
232reported by this function, and outstanding exceptions are maintained.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000233*/
Tim Peterseb28ef22001-06-02 05:27:19 +0000234
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000235static dictentry *
Tim Peters1f5871e2000-07-04 17:44:48 +0000236lookdict(dictobject *mp, PyObject *key, register long hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000237{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000238 register Py_ssize_t i;
239 register size_t perturb;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000240 register dictentry *freeslot;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000241 register Py_ssize_t mask = mp->ma_mask;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000242 dictentry *ep0 = mp->ma_table;
243 register dictentry *ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000244 register int restore_error;
245 register int checked_error;
Fred Drakec88b99c2000-08-31 19:04:07 +0000246 register int cmp;
247 PyObject *err_type, *err_value, *err_tb;
Tim Peters453163d2001-06-03 04:54:32 +0000248 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000249
Tim Peters2f228e72001-05-13 00:19:31 +0000250 i = hash & mask;
Guido van Rossumefb46091997-01-29 15:53:56 +0000251 ep = &ep0[i];
Guido van Rossumc1c7b1a1998-10-06 16:01:14 +0000252 if (ep->me_key == NULL || ep->me_key == key)
Guido van Rossum7d181591997-01-16 21:06:45 +0000253 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000254
255 restore_error = checked_error = 0;
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) {
260 /* error can't have been checked yet */
261 checked_error = 1;
262 if (PyErr_Occurred()) {
263 restore_error = 1;
264 PyErr_Fetch(&err_type, &err_value, &err_tb);
265 }
Tim Peters453163d2001-06-03 04:54:32 +0000266 startkey = ep->me_key;
267 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000268 if (cmp < 0)
Guido van Rossumb9324202001-01-18 00:39:02 +0000269 PyErr_Clear();
Tim Peters453163d2001-06-03 04:54:32 +0000270 if (ep0 == mp->ma_table && ep->me_key == startkey) {
271 if (cmp > 0)
272 goto Done;
273 }
274 else {
275 /* The compare did major nasty stuff to the
276 * dict: start over.
277 * XXX A clever adversary could prevent this
278 * XXX from terminating.
279 */
280 ep = lookdict(mp, key, hash);
281 goto Done;
282 }
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000283 }
284 freeslot = NULL;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000285 }
Tim Peters15d49292001-05-27 07:39:22 +0000286
Tim Peters342c65e2001-05-13 06:43:53 +0000287 /* In the loop, me_key == dummy is by far (factor of 100s) the
288 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000289 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
290 i = (i << 2) + i + perturb + 1;
291 ep = &ep0[i & mask];
Guido van Rossum16e93a81997-01-28 00:00:11 +0000292 if (ep->me_key == NULL) {
Tim Peters7b5d0af2001-06-03 04:14:43 +0000293 if (freeslot != NULL)
294 ep = freeslot;
295 break;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000296 }
Tim Peters7b5d0af2001-06-03 04:14:43 +0000297 if (ep->me_key == key)
298 break;
299 if (ep->me_hash == hash && ep->me_key != dummy) {
Fred Drakec88b99c2000-08-31 19:04:07 +0000300 if (!checked_error) {
301 checked_error = 1;
302 if (PyErr_Occurred()) {
303 restore_error = 1;
304 PyErr_Fetch(&err_type, &err_value,
305 &err_tb);
306 }
307 }
Tim Peters453163d2001-06-03 04:54:32 +0000308 startkey = ep->me_key;
309 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000310 if (cmp < 0)
Guido van Rossumb9324202001-01-18 00:39:02 +0000311 PyErr_Clear();
Tim Peters453163d2001-06-03 04:54:32 +0000312 if (ep0 == mp->ma_table && ep->me_key == startkey) {
313 if (cmp > 0)
314 break;
315 }
316 else {
317 /* The compare did major nasty stuff to the
318 * dict: start over.
319 * XXX A clever adversary could prevent this
320 * XXX from terminating.
321 */
322 ep = lookdict(mp, key, hash);
323 break;
324 }
Guido van Rossum16e93a81997-01-28 00:00:11 +0000325 }
Tim Peters342c65e2001-05-13 06:43:53 +0000326 else if (ep->me_key == dummy && freeslot == NULL)
327 freeslot = ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000328 }
Tim Peters7b5d0af2001-06-03 04:14:43 +0000329
330Done:
331 if (restore_error)
332 PyErr_Restore(err_type, err_value, err_tb);
333 return ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000334}
335
336/*
Fred Drake1bff34a2000-08-31 19:31:38 +0000337 * Hacked up version of lookdict which can assume keys are always strings;
338 * this assumption allows testing for errors during PyObject_Compare() to
339 * be dropped; string-string comparisons never raise exceptions. This also
340 * means we don't need to go through PyObject_Compare(); we can always use
Martin v. Löwiscd353062001-05-24 16:56:35 +0000341 * _PyString_Eq directly.
Fred Drake1bff34a2000-08-31 19:31:38 +0000342 *
Tim Peters0ab085c2001-09-14 00:25:33 +0000343 * This is valuable because the general-case error handling in lookdict() is
344 * expensive, and dicts with pure-string keys are very common.
Fred Drake1bff34a2000-08-31 19:31:38 +0000345 */
346static dictentry *
347lookdict_string(dictobject *mp, PyObject *key, register long hash)
348{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000349 register Py_ssize_t i;
350 register size_t perturb;
Fred Drake1bff34a2000-08-31 19:31:38 +0000351 register dictentry *freeslot;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000352 register Py_ssize_t mask = mp->ma_mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000353 dictentry *ep0 = mp->ma_table;
354 register dictentry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000355
Tim Peters0ab085c2001-09-14 00:25:33 +0000356 /* Make sure this function doesn't have to handle non-string keys,
357 including subclasses of str; e.g., one reason to subclass
358 strings is to override __eq__, and for speed we don't cater to
359 that here. */
360 if (!PyString_CheckExact(key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000361#ifdef SHOW_CONVERSION_COUNTS
362 ++converted;
363#endif
364 mp->ma_lookup = lookdict;
365 return lookdict(mp, key, hash);
366 }
Tim Peters2f228e72001-05-13 00:19:31 +0000367 i = hash & mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000368 ep = &ep0[i];
369 if (ep->me_key == NULL || ep->me_key == key)
370 return ep;
371 if (ep->me_key == dummy)
372 freeslot = ep;
373 else {
374 if (ep->me_hash == hash
Martin v. Löwiscd353062001-05-24 16:56:35 +0000375 && _PyString_Eq(ep->me_key, key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000376 return ep;
377 }
378 freeslot = NULL;
379 }
Tim Peters15d49292001-05-27 07:39:22 +0000380
Tim Peters342c65e2001-05-13 06:43:53 +0000381 /* In the loop, me_key == dummy is by far (factor of 100s) the
382 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000383 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
384 i = (i << 2) + i + perturb + 1;
385 ep = &ep0[i & mask];
Tim Peters342c65e2001-05-13 06:43:53 +0000386 if (ep->me_key == NULL)
387 return freeslot == NULL ? ep : freeslot;
388 if (ep->me_key == key
389 || (ep->me_hash == hash
390 && ep->me_key != dummy
Martin v. Löwiscd353062001-05-24 16:56:35 +0000391 && _PyString_Eq(ep->me_key, key)))
Fred Drake1bff34a2000-08-31 19:31:38 +0000392 return ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000393 if (ep->me_key == dummy && freeslot == NULL)
Tim Peters342c65e2001-05-13 06:43:53 +0000394 freeslot = ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000395 }
396}
397
398/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000399Internal routine to insert a new item into the table.
400Used both by the internal resize routine and by the public insert routine.
401Eats a reference to key and one to value.
402*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000403static void
Tim Peters1f5871e2000-07-04 17:44:48 +0000404insertdict(register dictobject *mp, PyObject *key, long hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000405{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000406 PyObject *old_value;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000407 register dictentry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000408 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
409
410 assert(mp->ma_lookup != NULL);
411 ep = mp->ma_lookup(mp, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000412 if (ep->me_value != NULL) {
Guido van Rossumd7047b31995-01-02 19:07:15 +0000413 old_value = ep->me_value;
414 ep->me_value = value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000415 Py_DECREF(old_value); /* which **CAN** re-enter */
416 Py_DECREF(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000417 }
418 else {
419 if (ep->me_key == NULL)
420 mp->ma_fill++;
Raymond Hettinger07ead172005-02-05 23:42:57 +0000421 else {
422 assert(ep->me_key == dummy);
423 Py_DECREF(dummy);
424 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000425 ep->me_key = key;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000426 ep->me_hash = (Py_ssize_t)hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000427 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000428 mp->ma_used++;
429 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000430}
431
432/*
433Restructure the table by allocating a new table and reinserting all
434items again. When entries have been deleted, the new table may
435actually be smaller than the old one.
436*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000437static int
Tim Peters9b10f7e2006-05-30 04:16:25 +0000438dictresize(dictobject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000439{
Tim Peters9b10f7e2006-05-30 04:16:25 +0000440 Py_ssize_t newsize;
Tim Peters0c6010b2001-05-23 23:33:57 +0000441 dictentry *oldtable, *newtable, *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000442 Py_ssize_t i;
Tim Peters0c6010b2001-05-23 23:33:57 +0000443 int is_oldtable_malloced;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000444 dictentry small_copy[PyDict_MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000445
446 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000447
Tim Peterseb28ef22001-06-02 05:27:19 +0000448 /* Find the smallest table size > minused. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000449 for (newsize = PyDict_MINSIZE;
Tim Peterseb28ef22001-06-02 05:27:19 +0000450 newsize <= minused && newsize > 0;
451 newsize <<= 1)
452 ;
453 if (newsize <= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000454 PyErr_NoMemory();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000455 return -1;
456 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000457
458 /* Get space for a new table. */
459 oldtable = mp->ma_table;
460 assert(oldtable != NULL);
461 is_oldtable_malloced = oldtable != mp->ma_smalltable;
462
Tim Peters6d6c1a32001-08-02 04:15:00 +0000463 if (newsize == PyDict_MINSIZE) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000464 /* A large table is shrinking, or we can't get any smaller. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000465 newtable = mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000466 if (newtable == oldtable) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000467 if (mp->ma_fill == mp->ma_used) {
468 /* No dummies, so no point doing anything. */
Tim Peters0c6010b2001-05-23 23:33:57 +0000469 return 0;
Tim Petersf8a548c2001-05-24 16:26:40 +0000470 }
471 /* We're not going to resize it, but rebuild the
472 table anyway to purge old dummy entries.
473 Subtle: This is *necessary* if fill==size,
474 as lookdict needs at least one virgin slot to
475 terminate failing searches. If fill < size, it's
476 merely desirable, as dummies slow searches. */
477 assert(mp->ma_fill > mp->ma_used);
Tim Peters0c6010b2001-05-23 23:33:57 +0000478 memcpy(small_copy, oldtable, sizeof(small_copy));
479 oldtable = small_copy;
480 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000481 }
482 else {
483 newtable = PyMem_NEW(dictentry, newsize);
484 if (newtable == NULL) {
485 PyErr_NoMemory();
486 return -1;
487 }
488 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000489
490 /* Make the dict empty, using the new table. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000491 assert(newtable != oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000492 mp->ma_table = newtable;
Tim Petersafb6ae82001-06-04 21:00:21 +0000493 mp->ma_mask = newsize - 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000494 memset(newtable, 0, sizeof(dictentry) * newsize);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000495 mp->ma_used = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000496 i = mp->ma_fill;
497 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000498
Tim Peters19283142001-05-17 22:25:34 +0000499 /* Copy the data over; this is refcount-neutral for active entries;
500 dummy entries aren't copied over, of course */
Tim Petersdea48ec2001-05-22 20:40:22 +0000501 for (ep = oldtable; i > 0; ep++) {
502 if (ep->me_value != NULL) { /* active entry */
503 --i;
Tim Peters19283142001-05-17 22:25:34 +0000504 insertdict(mp, ep->me_key, ep->me_hash, ep->me_value);
Tim Petersdea48ec2001-05-22 20:40:22 +0000505 }
Tim Peters19283142001-05-17 22:25:34 +0000506 else if (ep->me_key != NULL) { /* dummy entry */
Tim Petersdea48ec2001-05-22 20:40:22 +0000507 --i;
Tim Peters19283142001-05-17 22:25:34 +0000508 assert(ep->me_key == dummy);
509 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000510 }
Tim Peters19283142001-05-17 22:25:34 +0000511 /* else key == value == NULL: nothing to do */
Guido van Rossumd7047b31995-01-02 19:07:15 +0000512 }
513
Tim Peters0c6010b2001-05-23 23:33:57 +0000514 if (is_oldtable_malloced)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000515 PyMem_DEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000516 return 0;
517}
518
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000519PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000520PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000521{
522 long hash;
Fred Drake1bff34a2000-08-31 19:31:38 +0000523 dictobject *mp = (dictobject *)op;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000524 if (!PyDict_Check(op)) {
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000525 return NULL;
526 }
Tim Peters0ab085c2001-09-14 00:25:33 +0000527 if (!PyString_CheckExact(key) ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000528 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Guido van Rossumca756f21997-01-23 19:39:29 +0000529 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000530 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000531 if (hash == -1) {
532 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000533 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000534 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000535 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000536 return (mp->ma_lookup)(mp, key, hash)->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000537}
538
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000539/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
Tim Peters60b29962006-01-01 01:19:23 +0000540 * dictionary if it's merely replacing the value for an existing key.
541 * This means that it's safe to loop over a dictionary with PyDict_Next()
542 * and occasionally replace a value -- but you can't insert new keys or
543 * remove them.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000544 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000545int
Tim Peters1f5871e2000-07-04 17:44:48 +0000546PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000547{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000548 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000549 register long hash;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000550 register Py_ssize_t n_used;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000551
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000552 if (!PyDict_Check(op)) {
553 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000554 return -1;
555 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000556 mp = (dictobject *)op;
Tim Peters0ab085c2001-09-14 00:25:33 +0000557 if (PyString_CheckExact(key)) {
Guido van Rossum45ec02a2002-08-19 21:43:18 +0000558 hash = ((PyStringObject *)key)->ob_shash;
559 if (hash == -1)
560 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000561 }
Tim Peters1f7df352002-03-29 03:29:08 +0000562 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000563 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000564 if (hash == -1)
565 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000566 }
Tim Petersafb6ae82001-06-04 21:00:21 +0000567 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000568 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000569 Py_INCREF(value);
570 Py_INCREF(key);
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000571 insertdict(mp, key, hash, value);
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000572 /* If we added a key, we can safely resize. Otherwise just return!
573 * If fill >= 2/3 size, adjust size. Normally, this doubles or
574 * quaduples the size, but it's also possible for the dict to shrink
Tim Peters60b29962006-01-01 01:19:23 +0000575 * (if ma_fill is much larger than ma_used, meaning a lot of dict
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000576 * keys have been * deleted).
Tim Peters60b29962006-01-01 01:19:23 +0000577 *
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000578 * Quadrupling the size improves average dictionary sparseness
579 * (reducing collisions) at the cost of some memory and iteration
580 * speed (which loops over every possible entry). It also halves
Neal Norwitz80af59c2006-05-30 04:19:21 +0000581 * the number of expensive resize operations in a growing dictionary.
Tim Peters60b29962006-01-01 01:19:23 +0000582 *
583 * Very large dictionaries (over 50K items) use doubling instead.
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000584 * This may help applications with severe memory constraints.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000585 */
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000586 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
587 return 0;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000588 return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000589}
590
591int
Tim Peters1f5871e2000-07-04 17:44:48 +0000592PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000593{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000594 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000595 register long hash;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000596 register dictentry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000597 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000598
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000599 if (!PyDict_Check(op)) {
600 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000601 return -1;
602 }
Tim Peters0ab085c2001-09-14 00:25:33 +0000603 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +0000604 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000605 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000606 if (hash == -1)
607 return -1;
608 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000609 mp = (dictobject *)op;
Fred Drake1bff34a2000-08-31 19:31:38 +0000610 ep = (mp->ma_lookup)(mp, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000611 if (ep->me_value == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000612 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000613 return -1;
614 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000615 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000616 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000617 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000618 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000619 ep->me_value = NULL;
620 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000621 Py_DECREF(old_value);
622 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000623 return 0;
624}
625
Guido van Rossum25831651993-05-19 14:50:45 +0000626void
Tim Peters1f5871e2000-07-04 17:44:48 +0000627PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000628{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000629 dictobject *mp;
Tim Petersdea48ec2001-05-22 20:40:22 +0000630 dictentry *ep, *table;
631 int table_is_malloced;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000632 Py_ssize_t fill;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000633 dictentry small_copy[PyDict_MINSIZE];
Tim Petersdea48ec2001-05-22 20:40:22 +0000634#ifdef Py_DEBUG
Tim Peters9b10f7e2006-05-30 04:16:25 +0000635 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +0000636#endif
637
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000638 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000639 return;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000640 mp = (dictobject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000641#ifdef Py_DEBUG
Tim Petersafb6ae82001-06-04 21:00:21 +0000642 n = mp->ma_mask + 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000643 i = 0;
644#endif
645
646 table = mp->ma_table;
647 assert(table != NULL);
648 table_is_malloced = table != mp->ma_smalltable;
649
650 /* This is delicate. During the process of clearing the dict,
651 * decrefs can cause the dict to mutate. To avoid fatal confusion
652 * (voice of experience), we have to make the dict empty before
653 * clearing the slots, and never refer to anything via mp->xxx while
654 * clearing.
655 */
656 fill = mp->ma_fill;
657 if (table_is_malloced)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000658 EMPTY_TO_MINSIZE(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000659
660 else if (fill > 0) {
661 /* It's a small table with something that needs to be cleared.
662 * Afraid the only safe way is to copy the dict entries into
663 * another small table first.
664 */
665 memcpy(small_copy, table, sizeof(small_copy));
666 table = small_copy;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000667 EMPTY_TO_MINSIZE(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000668 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000669 /* else it's a small table that's already empty */
670
671 /* Now we can finally clear things. If C had refcounts, we could
672 * assert that the refcount on table is 1 now, i.e. that this function
673 * has unique access to it, so decref side-effects can't alter it.
674 */
675 for (ep = table; fill > 0; ++ep) {
676#ifdef Py_DEBUG
677 assert(i < n);
678 ++i;
679#endif
680 if (ep->me_key) {
681 --fill;
682 Py_DECREF(ep->me_key);
683 Py_XDECREF(ep->me_value);
684 }
685#ifdef Py_DEBUG
686 else
687 assert(ep->me_value == NULL);
688#endif
689 }
690
691 if (table_is_malloced)
692 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000693}
694
Tim Peters080c88b2003-02-15 03:01:11 +0000695/*
696 * Iterate over a dict. Use like so:
697 *
Tim Peters9b10f7e2006-05-30 04:16:25 +0000698 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +0000699 * PyObject *key, *value;
700 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +0000701 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +0000702 * Refer to borrowed references in key and value.
703 * }
704 *
705 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +0000706 * mutates the dict. One exception: it is safe if the loop merely changes
707 * the values associated with the keys (but doesn't insert new keys or
708 * delete keys), via PyDict_SetItem().
709 */
Guido van Rossum25831651993-05-19 14:50:45 +0000710int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000711PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000712{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000713 register Py_ssize_t i;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000714 register Py_ssize_t mask;
Raymond Hettinger43442782004-03-17 21:55:03 +0000715 register dictentry *ep;
716
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000717 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000718 return 0;
Guido van Rossum25831651993-05-19 14:50:45 +0000719 i = *ppos;
720 if (i < 0)
721 return 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000722 ep = ((dictobject *)op)->ma_table;
723 mask = ((dictobject *)op)->ma_mask;
724 while (i <= mask && ep[i].me_value == NULL)
Guido van Rossum25831651993-05-19 14:50:45 +0000725 i++;
726 *ppos = i+1;
Raymond Hettinger43442782004-03-17 21:55:03 +0000727 if (i > mask)
Guido van Rossum25831651993-05-19 14:50:45 +0000728 return 0;
729 if (pkey)
Raymond Hettinger43442782004-03-17 21:55:03 +0000730 *pkey = ep[i].me_key;
Guido van Rossum25831651993-05-19 14:50:45 +0000731 if (pvalue)
Raymond Hettinger43442782004-03-17 21:55:03 +0000732 *pvalue = ep[i].me_value;
Guido van Rossum25831651993-05-19 14:50:45 +0000733 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000734}
735
736/* Methods */
737
738static void
Tim Peters1f5871e2000-07-04 17:44:48 +0000739dict_dealloc(register dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000740{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000741 register dictentry *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000742 Py_ssize_t fill = mp->ma_fill;
Guido van Rossumff413af2002-03-28 20:34:59 +0000743 PyObject_GC_UnTrack(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000744 Py_TRASHCAN_SAFE_BEGIN(mp)
Tim Petersdea48ec2001-05-22 20:40:22 +0000745 for (ep = mp->ma_table; fill > 0; ep++) {
746 if (ep->me_key) {
747 --fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000748 Py_DECREF(ep->me_key);
Tim Petersdea48ec2001-05-22 20:40:22 +0000749 Py_XDECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000750 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000751 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000752 if (mp->ma_table != mp->ma_smalltable)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000753 PyMem_DEL(mp->ma_table);
Raymond Hettinger43442782004-03-17 21:55:03 +0000754 if (num_free_dicts < MAXFREEDICTS && mp->ob_type == &PyDict_Type)
755 free_dicts[num_free_dicts++] = mp;
Tim Peters60b29962006-01-01 01:19:23 +0000756 else
Raymond Hettinger43442782004-03-17 21:55:03 +0000757 mp->ob_type->tp_free((PyObject *)mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000758 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000759}
760
761static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000762dict_print(register dictobject *mp, register FILE *fp, register int flags)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000763{
Tim Peters9b10f7e2006-05-30 04:16:25 +0000764 register Py_ssize_t i;
765 register Py_ssize_t any;
Guido van Rossum255443b1998-04-10 22:47:14 +0000766
Neal Norwitz5e1b45d2006-05-30 04:43:23 +0000767 i = Py_ReprEnter((PyObject*)mp);
Guido van Rossum255443b1998-04-10 22:47:14 +0000768 if (i != 0) {
769 if (i < 0)
Tim Peters63814432006-05-30 05:04:59 +0000770 return (int)i;
Guido van Rossum255443b1998-04-10 22:47:14 +0000771 fprintf(fp, "{...}");
772 return 0;
773 }
774
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000775 fprintf(fp, "{");
776 any = 0;
Tim Petersafb6ae82001-06-04 21:00:21 +0000777 for (i = 0; i <= mp->ma_mask; i++) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000778 dictentry *ep = mp->ma_table + i;
779 PyObject *pvalue = ep->me_value;
780 if (pvalue != NULL) {
781 /* Prevent PyObject_Repr from deleting value during
782 key format */
783 Py_INCREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000784 if (any++ > 0)
785 fprintf(fp, ", ");
Guido van Rossum255443b1998-04-10 22:47:14 +0000786 if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000787 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000788 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000789 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000790 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000791 fprintf(fp, ": ");
Tim Peters19b77cf2001-06-02 08:27:39 +0000792 if (PyObject_Print(pvalue, fp, 0) != 0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000793 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000794 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000795 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000796 }
Tim Peters23cf6be2001-06-02 08:02:56 +0000797 Py_DECREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000798 }
799 }
800 fprintf(fp, "}");
Guido van Rossum255443b1998-04-10 22:47:14 +0000801 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000802 return 0;
803}
804
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000805static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000806dict_repr(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000807{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000808 Py_ssize_t i;
Tim Petersa7259592001-06-16 05:11:17 +0000809 PyObject *s, *temp, *colon = NULL;
810 PyObject *pieces = NULL, *result = NULL;
811 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +0000812
Tim Petersa7259592001-06-16 05:11:17 +0000813 i = Py_ReprEnter((PyObject *)mp);
Guido van Rossum255443b1998-04-10 22:47:14 +0000814 if (i != 0) {
Tim Petersa7259592001-06-16 05:11:17 +0000815 return i > 0 ? PyString_FromString("{...}") : NULL;
Guido van Rossum255443b1998-04-10 22:47:14 +0000816 }
817
Tim Petersa7259592001-06-16 05:11:17 +0000818 if (mp->ma_used == 0) {
819 result = PyString_FromString("{}");
820 goto Done;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000821 }
Tim Petersa7259592001-06-16 05:11:17 +0000822
823 pieces = PyList_New(0);
824 if (pieces == NULL)
825 goto Done;
826
827 colon = PyString_FromString(": ");
828 if (colon == NULL)
829 goto Done;
830
831 /* Do repr() on each key+value pair, and insert ": " between them.
832 Note that repr may mutate the dict. */
Tim Petersc6057842001-06-16 07:52:53 +0000833 i = 0;
834 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Tim Petersa7259592001-06-16 05:11:17 +0000835 int status;
836 /* Prevent repr from deleting value during key format. */
837 Py_INCREF(value);
838 s = PyObject_Repr(key);
839 PyString_Concat(&s, colon);
840 PyString_ConcatAndDel(&s, PyObject_Repr(value));
841 Py_DECREF(value);
842 if (s == NULL)
843 goto Done;
844 status = PyList_Append(pieces, s);
845 Py_DECREF(s); /* append created a new ref */
846 if (status < 0)
847 goto Done;
848 }
849
850 /* Add "{}" decorations to the first and last items. */
851 assert(PyList_GET_SIZE(pieces) > 0);
852 s = PyString_FromString("{");
853 if (s == NULL)
854 goto Done;
855 temp = PyList_GET_ITEM(pieces, 0);
856 PyString_ConcatAndDel(&s, temp);
857 PyList_SET_ITEM(pieces, 0, s);
858 if (s == NULL)
859 goto Done;
860
861 s = PyString_FromString("}");
862 if (s == NULL)
863 goto Done;
864 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
865 PyString_ConcatAndDel(&temp, s);
866 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
867 if (temp == NULL)
868 goto Done;
869
870 /* Paste them all together with ", " between. */
871 s = PyString_FromString(", ");
872 if (s == NULL)
873 goto Done;
874 result = _PyString_Join(s, pieces);
Tim Peters0ab085c2001-09-14 00:25:33 +0000875 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +0000876
877Done:
878 Py_XDECREF(pieces);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000879 Py_XDECREF(colon);
Tim Petersa7259592001-06-16 05:11:17 +0000880 Py_ReprLeave((PyObject *)mp);
881 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000882}
883
Martin v. Löwis18e16552006-02-15 17:27:45 +0000884static Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +0000885dict_length(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000886{
887 return mp->ma_used;
888}
889
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000890static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000891dict_subscript(dictobject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000892{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000893 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000894 long hash;
Tim Petersdea48ec2001-05-22 20:40:22 +0000895 assert(mp->ma_table != NULL);
Tim Peters0ab085c2001-09-14 00:25:33 +0000896 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +0000897 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000898 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000899 if (hash == -1)
900 return NULL;
901 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000902 v = (mp->ma_lookup)(mp, key, hash) -> me_value;
Guido van Rossum1968ad32006-02-25 22:38:04 +0000903 if (v == NULL) {
904 if (!PyDict_CheckExact(mp)) {
905 /* Look up __missing__ method if we're a subclass. */
Guido van Rossum4b92a822006-02-25 23:32:30 +0000906 PyObject *missing;
Guido van Rossum1968ad32006-02-25 22:38:04 +0000907 static PyObject *missing_str = NULL;
908 if (missing_str == NULL)
Tim Peters9b10f7e2006-05-30 04:16:25 +0000909 missing_str =
Guido van Rossum1968ad32006-02-25 22:38:04 +0000910 PyString_InternFromString("__missing__");
Guido van Rossum4b92a822006-02-25 23:32:30 +0000911 missing = _PyType_Lookup(mp->ob_type, missing_str);
Guido van Rossum1968ad32006-02-25 22:38:04 +0000912 if (missing != NULL)
913 return PyObject_CallFunctionObjArgs(missing,
914 (PyObject *)mp, key, NULL);
915 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000916 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum1968ad32006-02-25 22:38:04 +0000917 return NULL;
918 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000919 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000920 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000921 return v;
922}
923
924static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000925dict_ass_sub(dictobject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000926{
927 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000928 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000929 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000930 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000931}
932
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000933static PyMappingMethods dict_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000934 (lenfunc)dict_length, /*mp_length*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000935 (binaryfunc)dict_subscript, /*mp_subscript*/
936 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000937};
938
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000939static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000940dict_keys(register dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000941{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000942 register PyObject *v;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000943 register Py_ssize_t i, j;
Raymond Hettinger43442782004-03-17 21:55:03 +0000944 dictentry *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000945 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000946
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000947 again:
948 n = mp->ma_used;
949 v = PyList_New(n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000950 if (v == NULL)
951 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000952 if (n != mp->ma_used) {
953 /* Durnit. The allocations caused the dict to resize.
954 * Just start over, this shouldn't normally happen.
955 */
956 Py_DECREF(v);
957 goto again;
958 }
Raymond Hettinger43442782004-03-17 21:55:03 +0000959 ep = mp->ma_table;
960 mask = mp->ma_mask;
961 for (i = 0, j = 0; i <= mask; i++) {
962 if (ep[i].me_value != NULL) {
963 PyObject *key = ep[i].me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000964 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000965 PyList_SET_ITEM(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000966 j++;
967 }
968 }
Raymond Hettinger43442782004-03-17 21:55:03 +0000969 assert(j == n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000970 return v;
971}
972
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000973static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000974dict_values(register dictobject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +0000975{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000976 register PyObject *v;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000977 register Py_ssize_t i, j;
Raymond Hettinger43442782004-03-17 21:55:03 +0000978 dictentry *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000979 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000980
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000981 again:
982 n = mp->ma_used;
983 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +0000984 if (v == NULL)
985 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000986 if (n != mp->ma_used) {
987 /* Durnit. The allocations caused the dict to resize.
988 * Just start over, this shouldn't normally happen.
989 */
990 Py_DECREF(v);
991 goto again;
992 }
Raymond Hettinger43442782004-03-17 21:55:03 +0000993 ep = mp->ma_table;
994 mask = mp->ma_mask;
995 for (i = 0, j = 0; i <= mask; i++) {
996 if (ep[i].me_value != NULL) {
997 PyObject *value = ep[i].me_value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000998 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000999 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001000 j++;
1001 }
1002 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001003 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001004 return v;
1005}
1006
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001007static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001008dict_items(register dictobject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001009{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001010 register PyObject *v;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001011 register Py_ssize_t i, j, n;
1012 Py_ssize_t mask;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001013 PyObject *item, *key, *value;
Raymond Hettinger43442782004-03-17 21:55:03 +00001014 dictentry *ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001015
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001016 /* Preallocate the list of tuples, to avoid allocations during
1017 * the loop over the items, which could trigger GC, which
1018 * could resize the dict. :-(
1019 */
1020 again:
1021 n = mp->ma_used;
1022 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001023 if (v == NULL)
1024 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001025 for (i = 0; i < n; i++) {
1026 item = PyTuple_New(2);
1027 if (item == NULL) {
1028 Py_DECREF(v);
1029 return NULL;
1030 }
1031 PyList_SET_ITEM(v, i, item);
1032 }
1033 if (n != mp->ma_used) {
1034 /* Durnit. The allocations caused the dict to resize.
1035 * Just start over, this shouldn't normally happen.
1036 */
1037 Py_DECREF(v);
1038 goto again;
1039 }
1040 /* Nothing we do below makes any function calls. */
Raymond Hettinger43442782004-03-17 21:55:03 +00001041 ep = mp->ma_table;
1042 mask = mp->ma_mask;
1043 for (i = 0, j = 0; i <= mask; i++) {
Raymond Hettinger06905122004-03-19 10:30:00 +00001044 if ((value=ep[i].me_value) != NULL) {
Raymond Hettinger43442782004-03-17 21:55:03 +00001045 key = ep[i].me_key;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001046 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001047 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001048 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001049 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001050 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001051 j++;
1052 }
1053 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001054 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001055 return v;
1056}
1057
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001058static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001059dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001060{
1061 PyObject *seq;
1062 PyObject *value = Py_None;
1063 PyObject *it; /* iter(seq) */
1064 PyObject *key;
1065 PyObject *d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001066 int status;
1067
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001068 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001069 return NULL;
1070
1071 d = PyObject_CallObject(cls, NULL);
1072 if (d == NULL)
1073 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001074
1075 it = PyObject_GetIter(seq);
1076 if (it == NULL){
1077 Py_DECREF(d);
1078 return NULL;
1079 }
1080
1081 for (;;) {
1082 key = PyIter_Next(it);
1083 if (key == NULL) {
1084 if (PyErr_Occurred())
1085 goto Fail;
1086 break;
1087 }
Raymond Hettingere03e5b12002-12-07 08:10:51 +00001088 status = PyObject_SetItem(d, key, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001089 Py_DECREF(key);
1090 if (status < 0)
1091 goto Fail;
1092 }
1093
1094 Py_DECREF(it);
1095 return d;
1096
1097Fail:
1098 Py_DECREF(it);
1099 Py_DECREF(d);
1100 return NULL;
1101}
1102
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001103static int
1104dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001105{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001106 PyObject *arg = NULL;
1107 int result = 0;
1108
1109 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1110 result = -1;
1111
1112 else if (arg != NULL) {
1113 if (PyObject_HasAttrString(arg, "keys"))
1114 result = PyDict_Merge(self, arg, 1);
1115 else
1116 result = PyDict_MergeFromSeq2(self, arg, 1);
1117 }
1118 if (result == 0 && kwds != NULL)
1119 result = PyDict_Merge(self, kwds, 1);
1120 return result;
1121}
1122
1123static PyObject *
1124dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1125{
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001126 if (dict_update_common(self, args, kwds, "update") != -1)
1127 Py_RETURN_NONE;
1128 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001129}
1130
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001131/* Update unconditionally replaces existing items.
1132 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001133 otherwise it leaves existing items unchanged.
1134
1135 PyDict_{Update,Merge} update/merge from a mapping object.
1136
Tim Petersf582b822001-12-11 18:51:08 +00001137 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001138 producing iterable objects of length 2.
1139*/
1140
Tim Petersf582b822001-12-11 18:51:08 +00001141int
Tim Peters1fc240e2001-10-26 05:06:50 +00001142PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1143{
1144 PyObject *it; /* iter(seq2) */
Tim Peters9b10f7e2006-05-30 04:16:25 +00001145 Py_ssize_t i; /* index into seq2 of current element */
Tim Peters1fc240e2001-10-26 05:06:50 +00001146 PyObject *item; /* seq2[i] */
1147 PyObject *fast; /* item as a 2-tuple or 2-list */
1148
1149 assert(d != NULL);
1150 assert(PyDict_Check(d));
1151 assert(seq2 != NULL);
1152
1153 it = PyObject_GetIter(seq2);
1154 if (it == NULL)
1155 return -1;
1156
1157 for (i = 0; ; ++i) {
1158 PyObject *key, *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001159 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001160
1161 fast = NULL;
1162 item = PyIter_Next(it);
1163 if (item == NULL) {
1164 if (PyErr_Occurred())
1165 goto Fail;
1166 break;
1167 }
1168
1169 /* Convert item to sequence, and verify length 2. */
1170 fast = PySequence_Fast(item, "");
1171 if (fast == NULL) {
1172 if (PyErr_ExceptionMatches(PyExc_TypeError))
1173 PyErr_Format(PyExc_TypeError,
1174 "cannot convert dictionary update "
Neal Norwitzd3881b02006-05-30 04:25:05 +00001175 "sequence element #%zd to a sequence",
Tim Peters1fc240e2001-10-26 05:06:50 +00001176 i);
1177 goto Fail;
1178 }
1179 n = PySequence_Fast_GET_SIZE(fast);
1180 if (n != 2) {
1181 PyErr_Format(PyExc_ValueError,
Neal Norwitzd3881b02006-05-30 04:25:05 +00001182 "dictionary update sequence element #%zd "
Martin v. Löwis2c95cc62006-02-16 06:54:25 +00001183 "has length %zd; 2 is required",
Martin v. Löwise0e89f72006-02-16 06:59:22 +00001184 i, n);
Tim Peters1fc240e2001-10-26 05:06:50 +00001185 goto Fail;
1186 }
1187
1188 /* Update/merge with this (key, value) pair. */
1189 key = PySequence_Fast_GET_ITEM(fast, 0);
1190 value = PySequence_Fast_GET_ITEM(fast, 1);
1191 if (override || PyDict_GetItem(d, key) == NULL) {
1192 int status = PyDict_SetItem(d, key, value);
1193 if (status < 0)
1194 goto Fail;
1195 }
1196 Py_DECREF(fast);
1197 Py_DECREF(item);
1198 }
1199
1200 i = 0;
1201 goto Return;
1202Fail:
1203 Py_XDECREF(item);
1204 Py_XDECREF(fast);
1205 i = -1;
1206Return:
1207 Py_DECREF(it);
Neal Norwitzd3881b02006-05-30 04:25:05 +00001208 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001209}
1210
Tim Peters6d6c1a32001-08-02 04:15:00 +00001211int
1212PyDict_Update(PyObject *a, PyObject *b)
1213{
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001214 return PyDict_Merge(a, b, 1);
1215}
1216
1217int
1218PyDict_Merge(PyObject *a, PyObject *b, int override)
1219{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001220 register PyDictObject *mp, *other;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001221 register Py_ssize_t i;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001222 dictentry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001223
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001224 /* We accept for the argument either a concrete dictionary object,
1225 * or an abstract "mapping" object. For the former, we can do
1226 * things quite efficiently. For the latter, we only require that
1227 * PyMapping_Keys() and PyObject_GetItem() be supported.
1228 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001229 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1230 PyErr_BadInternalCall();
1231 return -1;
1232 }
1233 mp = (dictobject*)a;
1234 if (PyDict_Check(b)) {
1235 other = (dictobject*)b;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001236 if (other == mp || other->ma_used == 0)
1237 /* a.update(a) or a.update({}); nothing to do */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001238 return 0;
Raymond Hettinger186e7392005-05-14 18:08:25 +00001239 if (mp->ma_used == 0)
1240 /* Since the target dict is empty, PyDict_GetItem()
1241 * always returns NULL. Setting override to 1
1242 * skips the unnecessary test.
1243 */
1244 override = 1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001245 /* Do one big resize at the start, rather than
1246 * incrementally resizing as we insert new items. Expect
1247 * that there will be no (or few) overlapping keys.
1248 */
1249 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
Raymond Hettingerc8d22902003-05-07 00:49:40 +00001250 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001251 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001252 }
1253 for (i = 0; i <= other->ma_mask; i++) {
1254 entry = &other->ma_table[i];
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001255 if (entry->me_value != NULL &&
1256 (override ||
1257 PyDict_GetItem(a, entry->me_key) == NULL)) {
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001258 Py_INCREF(entry->me_key);
1259 Py_INCREF(entry->me_value);
Tim Peters9b10f7e2006-05-30 04:16:25 +00001260 insertdict(mp, entry->me_key,
1261 (long)entry->me_hash,
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001262 entry->me_value);
1263 }
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001264 }
1265 }
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001266 else {
1267 /* Do it the generic, slower way */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001268 PyObject *keys = PyMapping_Keys(b);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001269 PyObject *iter;
1270 PyObject *key, *value;
1271 int status;
1272
1273 if (keys == NULL)
1274 /* Docstring says this is equivalent to E.keys() so
1275 * if E doesn't have a .keys() method we want
1276 * AttributeError to percolate up. Might as well
1277 * do the same for any other error.
1278 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001279 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001280
1281 iter = PyObject_GetIter(keys);
1282 Py_DECREF(keys);
1283 if (iter == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001284 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001285
1286 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001287 if (!override && PyDict_GetItem(a, key) != NULL) {
1288 Py_DECREF(key);
1289 continue;
1290 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001291 value = PyObject_GetItem(b, key);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001292 if (value == NULL) {
1293 Py_DECREF(iter);
1294 Py_DECREF(key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001295 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001296 }
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001297 status = PyDict_SetItem(a, key, value);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001298 Py_DECREF(key);
1299 Py_DECREF(value);
1300 if (status < 0) {
1301 Py_DECREF(iter);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001302 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001303 }
1304 }
1305 Py_DECREF(iter);
1306 if (PyErr_Occurred())
1307 /* Iterator completed, via error */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001308 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001309 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001310 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001311}
1312
1313static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001314dict_copy(register dictobject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001315{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001316 return PyDict_Copy((PyObject*)mp);
1317}
1318
1319PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001320PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001321{
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001322 PyObject *copy;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001323
1324 if (o == NULL || !PyDict_Check(o)) {
1325 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001326 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001327 }
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001328 copy = PyDict_New();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001329 if (copy == NULL)
1330 return NULL;
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001331 if (PyDict_Merge(copy, o, 1) == 0)
1332 return copy;
1333 Py_DECREF(copy);
Raymond Hettinger1356f782005-04-15 15:58:42 +00001334 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001335}
1336
Martin v. Löwis18e16552006-02-15 17:27:45 +00001337Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00001338PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001339{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001340 if (mp == NULL || !PyDict_Check(mp)) {
1341 PyErr_BadInternalCall();
Jeremy Hylton7083bb72004-02-17 20:10:11 +00001342 return -1;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001343 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001344 return ((dictobject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001345}
1346
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001347PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001348PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001349{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001350 if (mp == NULL || !PyDict_Check(mp)) {
1351 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001352 return NULL;
1353 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001354 return dict_keys((dictobject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001355}
1356
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001357PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001358PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001359{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001360 if (mp == NULL || !PyDict_Check(mp)) {
1361 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001362 return NULL;
1363 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001364 return dict_values((dictobject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001365}
1366
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001367PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001368PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001369{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001370 if (mp == NULL || !PyDict_Check(mp)) {
1371 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001372 return NULL;
1373 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001374 return dict_items((dictobject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001375}
1376
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001377/* Subroutine which returns the smallest key in a for which b's value
1378 is different or absent. The value is returned too, through the
Tim Peters95bf9392001-05-10 08:32:44 +00001379 pval argument. Both are NULL if no key in a is found for which b's status
1380 differs. The refcounts on (and only on) non-NULL *pval and function return
1381 values must be decremented by the caller (characterize() increments them
1382 to ensure that mutating comparison and PyDict_GetItem calls can't delete
1383 them before the caller is done looking at them). */
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001384
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001385static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001386characterize(dictobject *a, dictobject *b, PyObject **pval)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001387{
Tim Peters95bf9392001-05-10 08:32:44 +00001388 PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */
1389 PyObject *aval = NULL; /* a[akey] */
Tim Peters9b10f7e2006-05-30 04:16:25 +00001390 Py_ssize_t i;
1391 int cmp;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001392
Tim Petersafb6ae82001-06-04 21:00:21 +00001393 for (i = 0; i <= a->ma_mask; i++) {
Tim Peters95bf9392001-05-10 08:32:44 +00001394 PyObject *thiskey, *thisaval, *thisbval;
1395 if (a->ma_table[i].me_value == NULL)
1396 continue;
1397 thiskey = a->ma_table[i].me_key;
1398 Py_INCREF(thiskey); /* keep alive across compares */
1399 if (akey != NULL) {
1400 cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT);
1401 if (cmp < 0) {
1402 Py_DECREF(thiskey);
1403 goto Fail;
1404 }
1405 if (cmp > 0 ||
Tim Petersafb6ae82001-06-04 21:00:21 +00001406 i > a->ma_mask ||
Tim Peters95bf9392001-05-10 08:32:44 +00001407 a->ma_table[i].me_value == NULL)
1408 {
1409 /* Not the *smallest* a key; or maybe it is
1410 * but the compare shrunk the dict so we can't
1411 * find its associated value anymore; or
1412 * maybe it is but the compare deleted the
1413 * a[thiskey] entry.
Neal Norwitzd3881b02006-05-30 04:25:05 +00001414 */
Tim Peters95bf9392001-05-10 08:32:44 +00001415 Py_DECREF(thiskey);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001416 continue;
Guido van Rossumb9324202001-01-18 00:39:02 +00001417 }
Tim Peters95bf9392001-05-10 08:32:44 +00001418 }
1419
1420 /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */
1421 thisaval = a->ma_table[i].me_value;
1422 assert(thisaval);
1423 Py_INCREF(thisaval); /* keep alive */
1424 thisbval = PyDict_GetItem((PyObject *)b, thiskey);
1425 if (thisbval == NULL)
1426 cmp = 0;
1427 else {
1428 /* both dicts have thiskey: same values? */
1429 cmp = PyObject_RichCompareBool(
1430 thisaval, thisbval, Py_EQ);
1431 if (cmp < 0) {
1432 Py_DECREF(thiskey);
1433 Py_DECREF(thisaval);
1434 goto Fail;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001435 }
1436 }
Tim Peters95bf9392001-05-10 08:32:44 +00001437 if (cmp == 0) {
1438 /* New winner. */
1439 Py_XDECREF(akey);
1440 Py_XDECREF(aval);
1441 akey = thiskey;
1442 aval = thisaval;
1443 }
1444 else {
1445 Py_DECREF(thiskey);
1446 Py_DECREF(thisaval);
1447 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001448 }
Tim Peters95bf9392001-05-10 08:32:44 +00001449 *pval = aval;
1450 return akey;
1451
1452Fail:
1453 Py_XDECREF(akey);
1454 Py_XDECREF(aval);
1455 *pval = NULL;
1456 return NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001457}
1458
1459static int
Tim Peters1f5871e2000-07-04 17:44:48 +00001460dict_compare(dictobject *a, dictobject *b)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001461{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001462 PyObject *adiff, *bdiff, *aval, *bval;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001463 int res;
1464
1465 /* Compare lengths first */
1466 if (a->ma_used < b->ma_used)
1467 return -1; /* a is shorter */
1468 else if (a->ma_used > b->ma_used)
1469 return 1; /* b is shorter */
Tim Peters95bf9392001-05-10 08:32:44 +00001470
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001471 /* Same length -- check all keys */
Tim Peters95bf9392001-05-10 08:32:44 +00001472 bdiff = bval = NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001473 adiff = characterize(a, b, &aval);
Tim Peters95bf9392001-05-10 08:32:44 +00001474 if (adiff == NULL) {
1475 assert(!aval);
Tim Peters3918fb22001-05-10 18:58:31 +00001476 /* Either an error, or a is a subset with the same length so
Tim Peters95bf9392001-05-10 08:32:44 +00001477 * must be equal.
1478 */
1479 res = PyErr_Occurred() ? -1 : 0;
1480 goto Finished;
1481 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001482 bdiff = characterize(b, a, &bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001483 if (bdiff == NULL && PyErr_Occurred()) {
1484 assert(!bval);
1485 res = -1;
1486 goto Finished;
1487 }
1488 res = 0;
1489 if (bdiff) {
1490 /* bdiff == NULL "should be" impossible now, but perhaps
1491 * the last comparison done by the characterize() on a had
1492 * the side effect of making the dicts equal!
1493 */
1494 res = PyObject_Compare(adiff, bdiff);
1495 }
1496 if (res == 0 && bval != NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001497 res = PyObject_Compare(aval, bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001498
1499Finished:
1500 Py_XDECREF(adiff);
1501 Py_XDECREF(bdiff);
1502 Py_XDECREF(aval);
1503 Py_XDECREF(bval);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001504 return res;
1505}
1506
Tim Peterse63415e2001-05-08 04:38:29 +00001507/* Return 1 if dicts equal, 0 if not, -1 if error.
1508 * Gets out as soon as any difference is detected.
1509 * Uses only Py_EQ comparison.
1510 */
1511static int
1512dict_equal(dictobject *a, dictobject *b)
1513{
Tim Peters9b10f7e2006-05-30 04:16:25 +00001514 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00001515
1516 if (a->ma_used != b->ma_used)
1517 /* can't be equal if # of entries differ */
1518 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001519
Tim Peterse63415e2001-05-08 04:38:29 +00001520 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Tim Petersafb6ae82001-06-04 21:00:21 +00001521 for (i = 0; i <= a->ma_mask; i++) {
Tim Peterse63415e2001-05-08 04:38:29 +00001522 PyObject *aval = a->ma_table[i].me_value;
1523 if (aval != NULL) {
1524 int cmp;
1525 PyObject *bval;
1526 PyObject *key = a->ma_table[i].me_key;
1527 /* temporarily bump aval's refcount to ensure it stays
1528 alive until we're done with it */
1529 Py_INCREF(aval);
1530 bval = PyDict_GetItem((PyObject *)b, key);
1531 if (bval == NULL) {
1532 Py_DECREF(aval);
1533 return 0;
1534 }
1535 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1536 Py_DECREF(aval);
1537 if (cmp <= 0) /* error or not equal */
1538 return cmp;
1539 }
1540 }
1541 return 1;
1542 }
1543
1544static PyObject *
1545dict_richcompare(PyObject *v, PyObject *w, int op)
1546{
1547 int cmp;
1548 PyObject *res;
1549
1550 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1551 res = Py_NotImplemented;
1552 }
1553 else if (op == Py_EQ || op == Py_NE) {
1554 cmp = dict_equal((dictobject *)v, (dictobject *)w);
1555 if (cmp < 0)
1556 return NULL;
1557 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1558 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001559 else
1560 res = Py_NotImplemented;
Tim Peterse63415e2001-05-08 04:38:29 +00001561 Py_INCREF(res);
1562 return res;
1563 }
1564
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001565static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001566dict_has_key(register dictobject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001567{
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001568 long hash;
1569 register long ok;
Tim Peters0ab085c2001-09-14 00:25:33 +00001570 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001571 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001572 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001573 if (hash == -1)
1574 return NULL;
1575 }
Tim Petersdea48ec2001-05-22 20:40:22 +00001576 ok = (mp->ma_lookup)(mp, key, hash)->me_value != NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00001577 return PyBool_FromLong(ok);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001578}
1579
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001580static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001581dict_get(register dictobject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001582{
1583 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001584 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001585 PyObject *val = NULL;
1586 long hash;
1587
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001588 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001589 return NULL;
1590
Tim Peters0ab085c2001-09-14 00:25:33 +00001591 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001592 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Barry Warsawc38c5da1997-10-06 17:49:20 +00001593 hash = PyObject_Hash(key);
1594 if (hash == -1)
1595 return NULL;
1596 }
Fred Drake1bff34a2000-08-31 19:31:38 +00001597 val = (mp->ma_lookup)(mp, key, hash)->me_value;
Barry Warsaw320ac331997-10-20 17:26:25 +00001598
Barry Warsawc38c5da1997-10-06 17:49:20 +00001599 if (val == NULL)
1600 val = failobj;
1601 Py_INCREF(val);
1602 return val;
1603}
1604
1605
1606static PyObject *
Guido van Rossum164452c2000-08-08 16:12:54 +00001607dict_setdefault(register dictobject *mp, PyObject *args)
1608{
1609 PyObject *key;
1610 PyObject *failobj = Py_None;
1611 PyObject *val = NULL;
1612 long hash;
1613
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001614 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
Guido van Rossum164452c2000-08-08 16:12:54 +00001615 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001616
Tim Peters0ab085c2001-09-14 00:25:33 +00001617 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001618 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossum164452c2000-08-08 16:12:54 +00001619 hash = PyObject_Hash(key);
1620 if (hash == -1)
1621 return NULL;
1622 }
Fred Drake1bff34a2000-08-31 19:31:38 +00001623 val = (mp->ma_lookup)(mp, key, hash)->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001624 if (val == NULL) {
1625 val = failobj;
1626 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1627 val = NULL;
1628 }
1629 Py_XINCREF(val);
1630 return val;
1631}
1632
1633
1634static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001635dict_clear(register dictobject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001636{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001637 PyDict_Clear((PyObject *)mp);
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001638 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001639}
1640
Guido van Rossumba6ab842000-12-12 22:02:18 +00001641static PyObject *
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001642dict_pop(dictobject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00001643{
1644 long hash;
1645 dictentry *ep;
1646 PyObject *old_value, *old_key;
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001647 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001648
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001649 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1650 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001651 if (mp->ma_used == 0) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001652 if (deflt) {
1653 Py_INCREF(deflt);
1654 return deflt;
1655 }
Guido van Rossume027d982002-04-12 15:11:59 +00001656 PyErr_SetString(PyExc_KeyError,
1657 "pop(): dictionary is empty");
1658 return NULL;
1659 }
1660 if (!PyString_CheckExact(key) ||
1661 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1662 hash = PyObject_Hash(key);
1663 if (hash == -1)
1664 return NULL;
1665 }
1666 ep = (mp->ma_lookup)(mp, key, hash);
1667 if (ep->me_value == NULL) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001668 if (deflt) {
1669 Py_INCREF(deflt);
1670 return deflt;
1671 }
Guido van Rossume027d982002-04-12 15:11:59 +00001672 PyErr_SetObject(PyExc_KeyError, key);
1673 return NULL;
1674 }
1675 old_key = ep->me_key;
1676 Py_INCREF(dummy);
1677 ep->me_key = dummy;
1678 old_value = ep->me_value;
1679 ep->me_value = NULL;
1680 mp->ma_used--;
1681 Py_DECREF(old_key);
1682 return old_value;
1683}
1684
1685static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001686dict_popitem(dictobject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001687{
Tim Peters9b10f7e2006-05-30 04:16:25 +00001688 Py_ssize_t i = 0;
Guido van Rossumba6ab842000-12-12 22:02:18 +00001689 dictentry *ep;
1690 PyObject *res;
1691
Tim Petersf4b33f62001-06-02 05:42:29 +00001692 /* Allocate the result tuple before checking the size. Believe it
1693 * or not, this allocation could trigger a garbage collection which
1694 * could empty the dict, so if we checked the size first and that
1695 * happened, the result would be an infinite loop (searching for an
1696 * entry that no longer exists). Note that the usual popitem()
1697 * idiom is "while d: k, v = d.popitem()". so needing to throw the
Tim Peters9b10f7e2006-05-30 04:16:25 +00001698 * tuple away if the dict *is* empty isn't a significant
Tim Petersf4b33f62001-06-02 05:42:29 +00001699 * inefficiency -- possible, but unlikely in practice.
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001700 */
1701 res = PyTuple_New(2);
1702 if (res == NULL)
1703 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001704 if (mp->ma_used == 0) {
1705 Py_DECREF(res);
1706 PyErr_SetString(PyExc_KeyError,
1707 "popitem(): dictionary is empty");
1708 return NULL;
1709 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001710 /* Set ep to "the first" dict entry with a value. We abuse the hash
1711 * field of slot 0 to hold a search finger:
1712 * If slot 0 has a value, use slot 0.
1713 * Else slot 0 is being used to hold a search finger,
Neal Norwitzd3881b02006-05-30 04:25:05 +00001714 * and we use its hash value as the first index to look.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001715 */
1716 ep = &mp->ma_table[0];
1717 if (ep->me_value == NULL) {
Tim Peters9b10f7e2006-05-30 04:16:25 +00001718 i = ep->me_hash;
Tim Petersdea48ec2001-05-22 20:40:22 +00001719 /* The hash field may be a real hash value, or it may be a
1720 * legit search finger, or it may be a once-legit search
1721 * finger that's out of bounds now because it wrapped around
1722 * or the table shrunk -- simply make sure it's in bounds now.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001723 */
Tim Petersafb6ae82001-06-04 21:00:21 +00001724 if (i > mp->ma_mask || i < 1)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001725 i = 1; /* skip slot 0 */
1726 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1727 i++;
Tim Petersafb6ae82001-06-04 21:00:21 +00001728 if (i > mp->ma_mask)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001729 i = 1;
1730 }
1731 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001732 PyTuple_SET_ITEM(res, 0, ep->me_key);
1733 PyTuple_SET_ITEM(res, 1, ep->me_value);
1734 Py_INCREF(dummy);
1735 ep->me_key = dummy;
1736 ep->me_value = NULL;
1737 mp->ma_used--;
1738 assert(mp->ma_table[0].me_value == NULL);
1739 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001740 return res;
1741}
1742
Jeremy Hylton8caad492000-06-23 14:18:11 +00001743static int
1744dict_traverse(PyObject *op, visitproc visit, void *arg)
1745{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001746 Py_ssize_t i = 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001747 PyObject *pk;
1748 PyObject *pv;
1749
1750 while (PyDict_Next(op, &i, &pk, &pv)) {
Thomas Woutersc6e55062006-04-15 21:47:09 +00001751 Py_VISIT(pk);
1752 Py_VISIT(pv);
Jeremy Hylton8caad492000-06-23 14:18:11 +00001753 }
1754 return 0;
1755}
1756
1757static int
1758dict_tp_clear(PyObject *op)
1759{
1760 PyDict_Clear(op);
1761 return 0;
1762}
1763
Tim Petersf7f88b12000-12-13 23:18:45 +00001764
Raymond Hettinger019a1482004-03-18 02:41:19 +00001765extern PyTypeObject PyDictIterKey_Type; /* Forward */
1766extern PyTypeObject PyDictIterValue_Type; /* Forward */
1767extern PyTypeObject PyDictIterItem_Type; /* Forward */
1768static PyObject *dictiter_new(dictobject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001769
1770static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001771dict_iterkeys(dictobject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001772{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001773 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001774}
1775
1776static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001777dict_itervalues(dictobject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001778{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001779 return dictiter_new(dict, &PyDictIterValue_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001780}
1781
1782static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001783dict_iteritems(dictobject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001784{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001785 return dictiter_new(dict, &PyDictIterItem_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001786}
1787
1788
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001789PyDoc_STRVAR(has_key__doc__,
Raymond Hettinger574aa322003-09-01 22:12:08 +00001790"D.has_key(k) -> True if D has a key k, else False");
Tim Petersf7f88b12000-12-13 23:18:45 +00001791
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001792PyDoc_STRVAR(contains__doc__,
1793"D.__contains__(k) -> True if D has a key k, else False");
1794
1795PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
1796
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001797PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001798"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001799
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001800PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001801"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 +00001802
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001803PyDoc_STRVAR(pop__doc__,
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001804"D.pop(k[,d]) -> v, remove specified key and return the corresponding value\n\
1805If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00001806
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001807PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00001808"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000018092-tuple; but raise KeyError if D is empty");
Tim Petersf7f88b12000-12-13 23:18:45 +00001810
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001811PyDoc_STRVAR(keys__doc__,
1812"D.keys() -> list of D's keys");
Tim Petersf7f88b12000-12-13 23:18:45 +00001813
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001814PyDoc_STRVAR(items__doc__,
1815"D.items() -> list of D's (key, value) pairs, as 2-tuples");
Tim Petersf7f88b12000-12-13 23:18:45 +00001816
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001817PyDoc_STRVAR(values__doc__,
1818"D.values() -> list of D's values");
Tim Petersf7f88b12000-12-13 23:18:45 +00001819
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001820PyDoc_STRVAR(update__doc__,
Walter Dörwaldd70ad8a2004-05-28 20:59:21 +00001821"D.update(E, **F) -> None. Update D from E and F: for k in E: D[k] = E[k]\n\
1822(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 +00001823
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001824PyDoc_STRVAR(fromkeys__doc__,
1825"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
1826v defaults to None.");
1827
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001828PyDoc_STRVAR(clear__doc__,
1829"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001830
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001831PyDoc_STRVAR(copy__doc__,
1832"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00001833
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001834PyDoc_STRVAR(iterkeys__doc__,
1835"D.iterkeys() -> an iterator over the keys of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00001836
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001837PyDoc_STRVAR(itervalues__doc__,
1838"D.itervalues() -> an iterator over the values of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00001839
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001840PyDoc_STRVAR(iteritems__doc__,
1841"D.iteritems() -> an iterator over the (key, value) items of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00001842
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001843static PyMethodDef mapp_methods[] = {
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001844 {"__contains__",(PyCFunction)dict_has_key, METH_O | METH_COEXIST,
1845 contains__doc__},
Raymond Hettinger0c669672003-12-13 13:31:55 +00001846 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001847 getitem__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001848 {"has_key", (PyCFunction)dict_has_key, METH_O,
Tim Petersf7f88b12000-12-13 23:18:45 +00001849 has_key__doc__},
1850 {"get", (PyCFunction)dict_get, METH_VARARGS,
1851 get__doc__},
1852 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
1853 setdefault_doc__},
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001854 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
Just van Rossuma797d812002-11-23 09:45:04 +00001855 pop__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001856 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001857 popitem__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001858 {"keys", (PyCFunction)dict_keys, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001859 keys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001860 {"items", (PyCFunction)dict_items, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001861 items__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001862 {"values", (PyCFunction)dict_values, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001863 values__doc__},
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001864 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001865 update__doc__},
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001866 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
1867 fromkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001868 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001869 clear__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001870 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001871 copy__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001872 {"iterkeys", (PyCFunction)dict_iterkeys, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00001873 iterkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001874 {"itervalues", (PyCFunction)dict_itervalues, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00001875 itervalues__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001876 {"iteritems", (PyCFunction)dict_iteritems, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00001877 iteritems__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00001878 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001879};
1880
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001881int
1882PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001883{
1884 long hash;
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001885 dictobject *mp = (dictobject *)op;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001886
Tim Peters0ab085c2001-09-14 00:25:33 +00001887 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001888 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001889 hash = PyObject_Hash(key);
1890 if (hash == -1)
1891 return -1;
1892 }
Tim Petersdea48ec2001-05-22 20:40:22 +00001893 return (mp->ma_lookup)(mp, key, hash)->me_value != NULL;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001894}
1895
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001896/* Hack to implement "key in dict" */
1897static PySequenceMethods dict_as_sequence = {
Georg Brandl347b3002006-03-30 11:57:00 +00001898 0, /* sq_length */
1899 0, /* sq_concat */
1900 0, /* sq_repeat */
1901 0, /* sq_item */
1902 0, /* sq_slice */
1903 0, /* sq_ass_item */
1904 0, /* sq_ass_slice */
1905 PyDict_Contains, /* sq_contains */
1906 0, /* sq_inplace_concat */
1907 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001908};
1909
Guido van Rossum09e563a2001-05-01 12:10:21 +00001910static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001911dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1912{
1913 PyObject *self;
1914
1915 assert(type != NULL && type->tp_alloc != NULL);
1916 self = type->tp_alloc(type, 0);
1917 if (self != NULL) {
1918 PyDictObject *d = (PyDictObject *)self;
1919 /* It's guaranteed that tp->alloc zeroed out the struct. */
1920 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
1921 INIT_NONZERO_DICT_SLOTS(d);
1922 d->ma_lookup = lookdict_string;
1923#ifdef SHOW_CONVERSION_COUNTS
1924 ++created;
1925#endif
1926 }
1927 return self;
1928}
1929
Tim Peters25786c02001-09-02 08:22:48 +00001930static int
1931dict_init(PyObject *self, PyObject *args, PyObject *kwds)
1932{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001933 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00001934}
1935
Guido van Rossumdbb53d92001-12-03 16:32:18 +00001936static long
1937dict_nohash(PyObject *self)
1938{
1939 PyErr_SetString(PyExc_TypeError, "dict objects are unhashable");
1940 return -1;
1941}
1942
Tim Peters6d6c1a32001-08-02 04:15:00 +00001943static PyObject *
Guido van Rossum09e563a2001-05-01 12:10:21 +00001944dict_iter(dictobject *dict)
1945{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001946 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001947}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001948
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001949PyDoc_STRVAR(dictionary_doc,
Tim Petersa427a2b2001-10-29 22:25:45 +00001950"dict() -> new empty dictionary.\n"
1951"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Tim Peters1fc240e2001-10-26 05:06:50 +00001952" (key, value) pairs.\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00001953"dict(seq) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00001954" d = {}\n"
1955" for k, v in seq:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00001956" d[k] = v\n"
1957"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
1958" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00001959
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001960PyTypeObject PyDict_Type = {
1961 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001962 0,
Tim Petersa427a2b2001-10-29 22:25:45 +00001963 "dict",
Neil Schemenauere83c00e2001-08-29 23:54:21 +00001964 sizeof(dictobject),
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001965 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00001966 (destructor)dict_dealloc, /* tp_dealloc */
1967 (printfunc)dict_print, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001968 0, /* tp_getattr */
Guido van Rossumb9324202001-01-18 00:39:02 +00001969 0, /* tp_setattr */
Tim Peters4fa58bf2001-05-10 21:45:19 +00001970 (cmpfunc)dict_compare, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00001971 (reprfunc)dict_repr, /* tp_repr */
1972 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001973 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00001974 &dict_as_mapping, /* tp_as_mapping */
Guido van Rossumdbb53d92001-12-03 16:32:18 +00001975 dict_nohash, /* tp_hash */
Guido van Rossumb9324202001-01-18 00:39:02 +00001976 0, /* tp_call */
1977 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001978 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumb9324202001-01-18 00:39:02 +00001979 0, /* tp_setattro */
1980 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00001981 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Tim Peters6d6c1a32001-08-02 04:15:00 +00001982 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters25786c02001-09-02 08:22:48 +00001983 dictionary_doc, /* tp_doc */
Georg Brandl347b3002006-03-30 11:57:00 +00001984 dict_traverse, /* tp_traverse */
1985 dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00001986 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001987 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00001988 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001989 0, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001990 mapp_methods, /* tp_methods */
1991 0, /* tp_members */
1992 0, /* tp_getset */
1993 0, /* tp_base */
1994 0, /* tp_dict */
1995 0, /* tp_descr_get */
1996 0, /* tp_descr_set */
1997 0, /* tp_dictoffset */
Georg Brandl347b3002006-03-30 11:57:00 +00001998 dict_init, /* tp_init */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001999 PyType_GenericAlloc, /* tp_alloc */
2000 dict_new, /* tp_new */
Neil Schemenauer6189b892002-04-12 02:43:00 +00002001 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002002};
2003
Guido van Rossum3cca2451997-05-16 14:23:33 +00002004/* For backward compatibility with old dictionary interface */
2005
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002006PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002007PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002008{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002009 PyObject *kv, *rv;
2010 kv = PyString_FromString(key);
2011 if (kv == NULL)
2012 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002013 rv = PyDict_GetItem(v, kv);
2014 Py_DECREF(kv);
2015 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002016}
2017
2018int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002019PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002020{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002021 PyObject *kv;
2022 int err;
2023 kv = PyString_FromString(key);
2024 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002025 return -1;
Guido van Rossum4f3bf1e1997-09-29 23:31:11 +00002026 PyString_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00002027 err = PyDict_SetItem(v, kv, item);
2028 Py_DECREF(kv);
2029 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002030}
2031
2032int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002033PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002034{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002035 PyObject *kv;
2036 int err;
2037 kv = PyString_FromString(key);
2038 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002039 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002040 err = PyDict_DelItem(v, kv);
2041 Py_DECREF(kv);
2042 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002043}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002044
Raymond Hettinger019a1482004-03-18 02:41:19 +00002045/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002046
2047typedef struct {
2048 PyObject_HEAD
Guido van Rossum2147df72002-07-16 20:30:22 +00002049 dictobject *di_dict; /* Set to NULL when iterator is exhausted */
Tim Peters9b10f7e2006-05-30 04:16:25 +00002050 Py_ssize_t di_used;
2051 Py_ssize_t di_pos;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002052 PyObject* di_result; /* reusable result tuple for iteritems */
Tim Peters9b10f7e2006-05-30 04:16:25 +00002053 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002054} dictiterobject;
2055
2056static PyObject *
Raymond Hettinger019a1482004-03-18 02:41:19 +00002057dictiter_new(dictobject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002058{
2059 dictiterobject *di;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002060 di = PyObject_New(dictiterobject, itertype);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002061 if (di == NULL)
2062 return NULL;
2063 Py_INCREF(dict);
2064 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00002065 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002066 di->di_pos = 0;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002067 di->len = dict->ma_used;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002068 if (itertype == &PyDictIterItem_Type) {
2069 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2070 if (di->di_result == NULL) {
2071 Py_DECREF(di);
2072 return NULL;
2073 }
2074 }
2075 else
2076 di->di_result = NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002077 return (PyObject *)di;
2078}
2079
2080static void
2081dictiter_dealloc(dictiterobject *di)
2082{
Guido van Rossum2147df72002-07-16 20:30:22 +00002083 Py_XDECREF(di->di_dict);
Raymond Hettinger019a1482004-03-18 02:41:19 +00002084 Py_XDECREF(di->di_result);
Neil Schemenauer6189b892002-04-12 02:43:00 +00002085 PyObject_Del(di);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002086}
2087
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002088static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002089dictiter_len(dictiterobject *di)
2090{
Tim Peters9b10f7e2006-05-30 04:16:25 +00002091 Py_ssize_t len = 0;
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00002092 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002093 len = di->len;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002094 return PyInt_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002095}
2096
Armin Rigof5b3e362006-02-11 21:32:43 +00002097PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002098
2099static PyMethodDef dictiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +00002100 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002101 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002102};
2103
Raymond Hettinger019a1482004-03-18 02:41:19 +00002104static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002105{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002106 PyObject *key;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002107 register Py_ssize_t i, mask;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002108 register dictentry *ep;
2109 dictobject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002110
Raymond Hettinger019a1482004-03-18 02:41:19 +00002111 if (d == NULL)
Guido van Rossum2147df72002-07-16 20:30:22 +00002112 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002113 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002114
Raymond Hettinger019a1482004-03-18 02:41:19 +00002115 if (di->di_used != d->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00002116 PyErr_SetString(PyExc_RuntimeError,
2117 "dictionary changed size during iteration");
Guido van Rossum2147df72002-07-16 20:30:22 +00002118 di->di_used = -1; /* Make this state sticky */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002119 return NULL;
2120 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002121
Raymond Hettinger019a1482004-03-18 02:41:19 +00002122 i = di->di_pos;
2123 if (i < 0)
2124 goto fail;
2125 ep = d->ma_table;
2126 mask = d->ma_mask;
2127 while (i <= mask && ep[i].me_value == NULL)
2128 i++;
2129 di->di_pos = i+1;
2130 if (i > mask)
2131 goto fail;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002132 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002133 key = ep[i].me_key;
2134 Py_INCREF(key);
2135 return key;
2136
2137fail:
2138 Py_DECREF(d);
Guido van Rossum2147df72002-07-16 20:30:22 +00002139 di->di_dict = NULL;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002140 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002141}
2142
Raymond Hettinger019a1482004-03-18 02:41:19 +00002143PyTypeObject PyDictIterKey_Type = {
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002144 PyObject_HEAD_INIT(&PyType_Type)
2145 0, /* ob_size */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002146 "dictionary-keyiterator", /* tp_name */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002147 sizeof(dictiterobject), /* tp_basicsize */
2148 0, /* tp_itemsize */
2149 /* methods */
2150 (destructor)dictiter_dealloc, /* tp_dealloc */
2151 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002152 0, /* tp_getattr */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002153 0, /* tp_setattr */
2154 0, /* tp_compare */
2155 0, /* tp_repr */
2156 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002157 0, /* tp_as_sequence */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002158 0, /* tp_as_mapping */
2159 0, /* tp_hash */
2160 0, /* tp_call */
2161 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002162 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002163 0, /* tp_setattro */
2164 0, /* tp_as_buffer */
2165 Py_TPFLAGS_DEFAULT, /* tp_flags */
2166 0, /* tp_doc */
2167 0, /* tp_traverse */
2168 0, /* tp_clear */
2169 0, /* tp_richcompare */
2170 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002171 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002172 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002173 dictiter_methods, /* tp_methods */
2174 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002175};
2176
2177static PyObject *dictiter_iternextvalue(dictiterobject *di)
2178{
2179 PyObject *value;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002180 register Py_ssize_t i, mask;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002181 register dictentry *ep;
2182 dictobject *d = di->di_dict;
2183
2184 if (d == NULL)
2185 return NULL;
2186 assert (PyDict_Check(d));
2187
2188 if (di->di_used != d->ma_used) {
2189 PyErr_SetString(PyExc_RuntimeError,
2190 "dictionary changed size during iteration");
2191 di->di_used = -1; /* Make this state sticky */
2192 return NULL;
2193 }
2194
2195 i = di->di_pos;
Guido van Rossum09240f62004-03-20 19:11:58 +00002196 mask = d->ma_mask;
2197 if (i < 0 || i > mask)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002198 goto fail;
2199 ep = d->ma_table;
Guido van Rossum09240f62004-03-20 19:11:58 +00002200 while ((value=ep[i].me_value) == NULL) {
Raymond Hettinger019a1482004-03-18 02:41:19 +00002201 i++;
Guido van Rossum09240f62004-03-20 19:11:58 +00002202 if (i > mask)
2203 goto fail;
2204 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002205 di->di_pos = i+1;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002206 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002207 Py_INCREF(value);
2208 return value;
2209
2210fail:
2211 Py_DECREF(d);
2212 di->di_dict = NULL;
2213 return NULL;
2214}
2215
2216PyTypeObject PyDictIterValue_Type = {
2217 PyObject_HEAD_INIT(&PyType_Type)
2218 0, /* ob_size */
2219 "dictionary-valueiterator", /* tp_name */
2220 sizeof(dictiterobject), /* tp_basicsize */
2221 0, /* tp_itemsize */
2222 /* methods */
2223 (destructor)dictiter_dealloc, /* tp_dealloc */
2224 0, /* tp_print */
2225 0, /* tp_getattr */
2226 0, /* tp_setattr */
2227 0, /* tp_compare */
2228 0, /* tp_repr */
2229 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002230 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002231 0, /* tp_as_mapping */
2232 0, /* tp_hash */
2233 0, /* tp_call */
2234 0, /* tp_str */
2235 PyObject_GenericGetAttr, /* tp_getattro */
2236 0, /* tp_setattro */
2237 0, /* tp_as_buffer */
2238 Py_TPFLAGS_DEFAULT, /* tp_flags */
2239 0, /* tp_doc */
2240 0, /* tp_traverse */
2241 0, /* tp_clear */
2242 0, /* tp_richcompare */
2243 0, /* tp_weaklistoffset */
2244 PyObject_SelfIter, /* tp_iter */
2245 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002246 dictiter_methods, /* tp_methods */
2247 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002248};
2249
2250static PyObject *dictiter_iternextitem(dictiterobject *di)
2251{
2252 PyObject *key, *value, *result = di->di_result;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002253 register Py_ssize_t i, mask;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002254 register dictentry *ep;
2255 dictobject *d = di->di_dict;
2256
2257 if (d == NULL)
2258 return NULL;
2259 assert (PyDict_Check(d));
2260
2261 if (di->di_used != d->ma_used) {
2262 PyErr_SetString(PyExc_RuntimeError,
2263 "dictionary changed size during iteration");
2264 di->di_used = -1; /* Make this state sticky */
2265 return NULL;
2266 }
2267
2268 i = di->di_pos;
2269 if (i < 0)
2270 goto fail;
2271 ep = d->ma_table;
2272 mask = d->ma_mask;
2273 while (i <= mask && ep[i].me_value == NULL)
2274 i++;
2275 di->di_pos = i+1;
2276 if (i > mask)
2277 goto fail;
2278
2279 if (result->ob_refcnt == 1) {
2280 Py_INCREF(result);
2281 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2282 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2283 } else {
2284 result = PyTuple_New(2);
Tim Peters60b29962006-01-01 01:19:23 +00002285 if (result == NULL)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002286 return NULL;
2287 }
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002288 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002289 key = ep[i].me_key;
2290 value = ep[i].me_value;
2291 Py_INCREF(key);
2292 Py_INCREF(value);
2293 PyTuple_SET_ITEM(result, 0, key);
2294 PyTuple_SET_ITEM(result, 1, value);
2295 return result;
2296
2297fail:
2298 Py_DECREF(d);
2299 di->di_dict = NULL;
2300 return NULL;
2301}
2302
2303PyTypeObject PyDictIterItem_Type = {
2304 PyObject_HEAD_INIT(&PyType_Type)
2305 0, /* ob_size */
2306 "dictionary-itemiterator", /* tp_name */
2307 sizeof(dictiterobject), /* tp_basicsize */
2308 0, /* tp_itemsize */
2309 /* methods */
2310 (destructor)dictiter_dealloc, /* tp_dealloc */
2311 0, /* tp_print */
2312 0, /* tp_getattr */
2313 0, /* tp_setattr */
2314 0, /* tp_compare */
2315 0, /* tp_repr */
2316 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002317 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002318 0, /* tp_as_mapping */
2319 0, /* tp_hash */
2320 0, /* tp_call */
2321 0, /* tp_str */
2322 PyObject_GenericGetAttr, /* tp_getattro */
2323 0, /* tp_setattro */
2324 0, /* tp_as_buffer */
2325 Py_TPFLAGS_DEFAULT, /* tp_flags */
2326 0, /* tp_doc */
2327 0, /* tp_traverse */
2328 0, /* tp_clear */
2329 0, /* tp_richcompare */
2330 0, /* tp_weaklistoffset */
2331 PyObject_SelfIter, /* tp_iter */
2332 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002333 dictiter_methods, /* tp_methods */
2334 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002335};