blob: f5799ee09ffc16917a2e3b279deb4719309879bd [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.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000113*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000114
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000115/* Object used as dummy key to fill deleted entries */
Raymond Hettingerf81e4502005-08-17 02:19:36 +0000116static PyObject *dummy = NULL; /* Initialized by first call to newdictobject() */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000117
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000118#ifdef Py_REF_DEBUG
119PyObject *
120_PyDict_Dummy(void)
121{
122 return dummy;
123}
124#endif
125
Fred Drake1bff34a2000-08-31 19:31:38 +0000126/* forward declarations */
127static dictentry *
128lookdict_string(dictobject *mp, PyObject *key, long hash);
129
130#ifdef SHOW_CONVERSION_COUNTS
131static long created = 0L;
132static long converted = 0L;
133
134static void
135show_counts(void)
136{
137 fprintf(stderr, "created %ld string dicts\n", created);
138 fprintf(stderr, "converted %ld to normal dicts\n", converted);
139 fprintf(stderr, "%.2f%% conversion rate\n", (100.0*converted)/created);
140}
141#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000142
Tim Peters6d6c1a32001-08-02 04:15:00 +0000143/* Initialization macros.
144 There are two ways to create a dict: PyDict_New() is the main C API
145 function, and the tp_new slot maps to dict_new(). In the latter case we
146 can save a little time over what PyDict_New does because it's guaranteed
147 that the PyDictObject struct is already zeroed out.
148 Everyone except dict_new() should use EMPTY_TO_MINSIZE (unless they have
149 an excellent reason not to).
150*/
151
152#define INIT_NONZERO_DICT_SLOTS(mp) do { \
Tim Petersdea48ec2001-05-22 20:40:22 +0000153 (mp)->ma_table = (mp)->ma_smalltable; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000154 (mp)->ma_mask = PyDict_MINSIZE - 1; \
155 } while(0)
156
157#define EMPTY_TO_MINSIZE(mp) do { \
158 memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000159 (mp)->ma_used = (mp)->ma_fill = 0; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000160 INIT_NONZERO_DICT_SLOTS(mp); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000161 } while(0)
162
Raymond Hettinger43442782004-03-17 21:55:03 +0000163/* Dictionary reuse scheme to save calls to malloc, free, and memset */
164#define MAXFREEDICTS 80
165static PyDictObject *free_dicts[MAXFREEDICTS];
166static int num_free_dicts = 0;
167
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000168PyObject *
Thomas Wouters78890102000-07-22 19:25:51 +0000169PyDict_New(void)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000170{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000171 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000172 if (dummy == NULL) { /* Auto-initialize dummy */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000173 dummy = PyString_FromString("<dummy key>");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000174 if (dummy == NULL)
175 return NULL;
Fred Drake1bff34a2000-08-31 19:31:38 +0000176#ifdef SHOW_CONVERSION_COUNTS
177 Py_AtExit(show_counts);
178#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000179 }
Raymond Hettinger43442782004-03-17 21:55:03 +0000180 if (num_free_dicts) {
181 mp = free_dicts[--num_free_dicts];
182 assert (mp != NULL);
183 assert (mp->ob_type == &PyDict_Type);
184 _Py_NewReference((PyObject *)mp);
185 if (mp->ma_fill) {
186 EMPTY_TO_MINSIZE(mp);
187 }
188 assert (mp->ma_used == 0);
189 assert (mp->ma_table == mp->ma_smalltable);
190 assert (mp->ma_mask == PyDict_MINSIZE - 1);
191 } else {
192 mp = PyObject_GC_New(dictobject, &PyDict_Type);
193 if (mp == NULL)
194 return NULL;
195 EMPTY_TO_MINSIZE(mp);
196 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000197 mp->ma_lookup = lookdict_string;
198#ifdef SHOW_CONVERSION_COUNTS
199 ++created;
200#endif
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000201 _PyObject_GC_TRACK(mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000202 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000203}
204
205/*
206The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000207This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000208Open addressing is preferred over chaining since the link overhead for
209chaining would be substantial (100% with typical malloc overhead).
210
Tim Peterseb28ef22001-06-02 05:27:19 +0000211The initial probe index is computed as hash mod the table size. Subsequent
212probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000213
214All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000215
Tim Peterseb28ef22001-06-02 05:27:19 +0000216(The details in this version are due to Tim Peters, building on many past
217contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
218Christian Tismer).
Fred Drake1bff34a2000-08-31 19:31:38 +0000219
220This function must never return NULL; failures are indicated by returning
221a dictentry* for which the me_value field is NULL. Exceptions are never
222reported by this function, and outstanding exceptions are maintained.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000223*/
Tim Peterseb28ef22001-06-02 05:27:19 +0000224
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000225static dictentry *
Tim Peters1f5871e2000-07-04 17:44:48 +0000226lookdict(dictobject *mp, PyObject *key, register long hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000227{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000228 register Py_ssize_t i;
229 register size_t perturb;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000230 register dictentry *freeslot;
Tim Petersafb6ae82001-06-04 21:00:21 +0000231 register unsigned int mask = mp->ma_mask;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000232 dictentry *ep0 = mp->ma_table;
233 register dictentry *ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000234 register int restore_error;
235 register int checked_error;
Fred Drakec88b99c2000-08-31 19:04:07 +0000236 register int cmp;
237 PyObject *err_type, *err_value, *err_tb;
Tim Peters453163d2001-06-03 04:54:32 +0000238 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000239
Tim Peters2f228e72001-05-13 00:19:31 +0000240 i = hash & mask;
Guido van Rossumefb46091997-01-29 15:53:56 +0000241 ep = &ep0[i];
Guido van Rossumc1c7b1a1998-10-06 16:01:14 +0000242 if (ep->me_key == NULL || ep->me_key == key)
Guido van Rossum7d181591997-01-16 21:06:45 +0000243 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000244
245 restore_error = checked_error = 0;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000246 if (ep->me_key == dummy)
Guido van Rossum7d181591997-01-16 21:06:45 +0000247 freeslot = ep;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000248 else {
Fred Drakec88b99c2000-08-31 19:04:07 +0000249 if (ep->me_hash == hash) {
250 /* error can't have been checked yet */
251 checked_error = 1;
252 if (PyErr_Occurred()) {
253 restore_error = 1;
254 PyErr_Fetch(&err_type, &err_value, &err_tb);
255 }
Tim Peters453163d2001-06-03 04:54:32 +0000256 startkey = ep->me_key;
257 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000258 if (cmp < 0)
Guido van Rossumb9324202001-01-18 00:39:02 +0000259 PyErr_Clear();
Tim Peters453163d2001-06-03 04:54:32 +0000260 if (ep0 == mp->ma_table && ep->me_key == startkey) {
261 if (cmp > 0)
262 goto Done;
263 }
264 else {
265 /* The compare did major nasty stuff to the
266 * dict: start over.
267 * XXX A clever adversary could prevent this
268 * XXX from terminating.
269 */
270 ep = lookdict(mp, key, hash);
271 goto Done;
272 }
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000273 }
274 freeslot = NULL;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000275 }
Tim Peters15d49292001-05-27 07:39:22 +0000276
Tim Peters342c65e2001-05-13 06:43:53 +0000277 /* In the loop, me_key == dummy is by far (factor of 100s) the
278 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000279 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
280 i = (i << 2) + i + perturb + 1;
281 ep = &ep0[i & mask];
Guido van Rossum16e93a81997-01-28 00:00:11 +0000282 if (ep->me_key == NULL) {
Tim Peters7b5d0af2001-06-03 04:14:43 +0000283 if (freeslot != NULL)
284 ep = freeslot;
285 break;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000286 }
Tim Peters7b5d0af2001-06-03 04:14:43 +0000287 if (ep->me_key == key)
288 break;
289 if (ep->me_hash == hash && ep->me_key != dummy) {
Fred Drakec88b99c2000-08-31 19:04:07 +0000290 if (!checked_error) {
291 checked_error = 1;
292 if (PyErr_Occurred()) {
293 restore_error = 1;
294 PyErr_Fetch(&err_type, &err_value,
295 &err_tb);
296 }
297 }
Tim Peters453163d2001-06-03 04:54:32 +0000298 startkey = ep->me_key;
299 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000300 if (cmp < 0)
Guido van Rossumb9324202001-01-18 00:39:02 +0000301 PyErr_Clear();
Tim Peters453163d2001-06-03 04:54:32 +0000302 if (ep0 == mp->ma_table && ep->me_key == startkey) {
303 if (cmp > 0)
304 break;
305 }
306 else {
307 /* The compare did major nasty stuff to the
308 * dict: start over.
309 * XXX A clever adversary could prevent this
310 * XXX from terminating.
311 */
312 ep = lookdict(mp, key, hash);
313 break;
314 }
Guido van Rossum16e93a81997-01-28 00:00:11 +0000315 }
Tim Peters342c65e2001-05-13 06:43:53 +0000316 else if (ep->me_key == dummy && freeslot == NULL)
317 freeslot = ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000318 }
Tim Peters7b5d0af2001-06-03 04:14:43 +0000319
320Done:
321 if (restore_error)
322 PyErr_Restore(err_type, err_value, err_tb);
323 return ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000324}
325
326/*
Fred Drake1bff34a2000-08-31 19:31:38 +0000327 * Hacked up version of lookdict which can assume keys are always strings;
328 * this assumption allows testing for errors during PyObject_Compare() to
329 * be dropped; string-string comparisons never raise exceptions. This also
330 * means we don't need to go through PyObject_Compare(); we can always use
Martin v. Löwiscd353062001-05-24 16:56:35 +0000331 * _PyString_Eq directly.
Fred Drake1bff34a2000-08-31 19:31:38 +0000332 *
Tim Peters0ab085c2001-09-14 00:25:33 +0000333 * This is valuable because the general-case error handling in lookdict() is
334 * expensive, and dicts with pure-string keys are very common.
Fred Drake1bff34a2000-08-31 19:31:38 +0000335 */
336static dictentry *
337lookdict_string(dictobject *mp, PyObject *key, register long hash)
338{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000339 register Py_ssize_t i;
340 register size_t perturb;
Fred Drake1bff34a2000-08-31 19:31:38 +0000341 register dictentry *freeslot;
Tim Petersafb6ae82001-06-04 21:00:21 +0000342 register unsigned int mask = mp->ma_mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000343 dictentry *ep0 = mp->ma_table;
344 register dictentry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000345
Tim Peters0ab085c2001-09-14 00:25:33 +0000346 /* Make sure this function doesn't have to handle non-string keys,
347 including subclasses of str; e.g., one reason to subclass
348 strings is to override __eq__, and for speed we don't cater to
349 that here. */
350 if (!PyString_CheckExact(key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000351#ifdef SHOW_CONVERSION_COUNTS
352 ++converted;
353#endif
354 mp->ma_lookup = lookdict;
355 return lookdict(mp, key, hash);
356 }
Tim Peters2f228e72001-05-13 00:19:31 +0000357 i = hash & mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000358 ep = &ep0[i];
359 if (ep->me_key == NULL || ep->me_key == key)
360 return ep;
361 if (ep->me_key == dummy)
362 freeslot = ep;
363 else {
364 if (ep->me_hash == hash
Martin v. Löwiscd353062001-05-24 16:56:35 +0000365 && _PyString_Eq(ep->me_key, key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000366 return ep;
367 }
368 freeslot = NULL;
369 }
Tim Peters15d49292001-05-27 07:39:22 +0000370
Tim Peters342c65e2001-05-13 06:43:53 +0000371 /* In the loop, me_key == dummy is by far (factor of 100s) the
372 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000373 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
374 i = (i << 2) + i + perturb + 1;
375 ep = &ep0[i & mask];
Tim Peters342c65e2001-05-13 06:43:53 +0000376 if (ep->me_key == NULL)
377 return freeslot == NULL ? ep : freeslot;
378 if (ep->me_key == key
379 || (ep->me_hash == hash
380 && ep->me_key != dummy
Martin v. Löwiscd353062001-05-24 16:56:35 +0000381 && _PyString_Eq(ep->me_key, key)))
Fred Drake1bff34a2000-08-31 19:31:38 +0000382 return ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000383 if (ep->me_key == dummy && freeslot == NULL)
Tim Peters342c65e2001-05-13 06:43:53 +0000384 freeslot = ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000385 }
386}
387
388/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000389Internal routine to insert a new item into the table.
390Used both by the internal resize routine and by the public insert routine.
391Eats a reference to key and one to value.
392*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000393static void
Tim Peters1f5871e2000-07-04 17:44:48 +0000394insertdict(register dictobject *mp, PyObject *key, long hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000395{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000396 PyObject *old_value;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000397 register dictentry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000398 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
399
400 assert(mp->ma_lookup != NULL);
401 ep = mp->ma_lookup(mp, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000402 if (ep->me_value != NULL) {
Guido van Rossumd7047b31995-01-02 19:07:15 +0000403 old_value = ep->me_value;
404 ep->me_value = value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000405 Py_DECREF(old_value); /* which **CAN** re-enter */
406 Py_DECREF(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000407 }
408 else {
409 if (ep->me_key == NULL)
410 mp->ma_fill++;
Raymond Hettinger07ead172005-02-05 23:42:57 +0000411 else {
412 assert(ep->me_key == dummy);
413 Py_DECREF(dummy);
414 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000415 ep->me_key = key;
416 ep->me_hash = hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000417 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000418 mp->ma_used++;
419 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000420}
421
422/*
423Restructure the table by allocating a new table and reinserting all
424items again. When entries have been deleted, the new table may
425actually be smaller than the old one.
426*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000427static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000428dictresize(dictobject *mp, int minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000429{
Tim Peterseb28ef22001-06-02 05:27:19 +0000430 int newsize;
Tim Peters0c6010b2001-05-23 23:33:57 +0000431 dictentry *oldtable, *newtable, *ep;
432 int i;
433 int is_oldtable_malloced;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000434 dictentry small_copy[PyDict_MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000435
436 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000437
Tim Peterseb28ef22001-06-02 05:27:19 +0000438 /* Find the smallest table size > minused. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000439 for (newsize = PyDict_MINSIZE;
Tim Peterseb28ef22001-06-02 05:27:19 +0000440 newsize <= minused && newsize > 0;
441 newsize <<= 1)
442 ;
443 if (newsize <= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000444 PyErr_NoMemory();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000445 return -1;
446 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000447
448 /* Get space for a new table. */
449 oldtable = mp->ma_table;
450 assert(oldtable != NULL);
451 is_oldtable_malloced = oldtable != mp->ma_smalltable;
452
Tim Peters6d6c1a32001-08-02 04:15:00 +0000453 if (newsize == PyDict_MINSIZE) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000454 /* A large table is shrinking, or we can't get any smaller. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000455 newtable = mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000456 if (newtable == oldtable) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000457 if (mp->ma_fill == mp->ma_used) {
458 /* No dummies, so no point doing anything. */
Tim Peters0c6010b2001-05-23 23:33:57 +0000459 return 0;
Tim Petersf8a548c2001-05-24 16:26:40 +0000460 }
461 /* We're not going to resize it, but rebuild the
462 table anyway to purge old dummy entries.
463 Subtle: This is *necessary* if fill==size,
464 as lookdict needs at least one virgin slot to
465 terminate failing searches. If fill < size, it's
466 merely desirable, as dummies slow searches. */
467 assert(mp->ma_fill > mp->ma_used);
Tim Peters0c6010b2001-05-23 23:33:57 +0000468 memcpy(small_copy, oldtable, sizeof(small_copy));
469 oldtable = small_copy;
470 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000471 }
472 else {
473 newtable = PyMem_NEW(dictentry, newsize);
474 if (newtable == NULL) {
475 PyErr_NoMemory();
476 return -1;
477 }
478 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000479
480 /* Make the dict empty, using the new table. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000481 assert(newtable != oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000482 mp->ma_table = newtable;
Tim Petersafb6ae82001-06-04 21:00:21 +0000483 mp->ma_mask = newsize - 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000484 memset(newtable, 0, sizeof(dictentry) * newsize);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000485 mp->ma_used = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000486 i = mp->ma_fill;
487 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000488
Tim Peters19283142001-05-17 22:25:34 +0000489 /* Copy the data over; this is refcount-neutral for active entries;
490 dummy entries aren't copied over, of course */
Tim Petersdea48ec2001-05-22 20:40:22 +0000491 for (ep = oldtable; i > 0; ep++) {
492 if (ep->me_value != NULL) { /* active entry */
493 --i;
Tim Peters19283142001-05-17 22:25:34 +0000494 insertdict(mp, ep->me_key, ep->me_hash, ep->me_value);
Tim Petersdea48ec2001-05-22 20:40:22 +0000495 }
Tim Peters19283142001-05-17 22:25:34 +0000496 else if (ep->me_key != NULL) { /* dummy entry */
Tim Petersdea48ec2001-05-22 20:40:22 +0000497 --i;
Tim Peters19283142001-05-17 22:25:34 +0000498 assert(ep->me_key == dummy);
499 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000500 }
Tim Peters19283142001-05-17 22:25:34 +0000501 /* else key == value == NULL: nothing to do */
Guido van Rossumd7047b31995-01-02 19:07:15 +0000502 }
503
Tim Peters0c6010b2001-05-23 23:33:57 +0000504 if (is_oldtable_malloced)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000505 PyMem_DEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000506 return 0;
507}
508
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000509PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000510PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000511{
512 long hash;
Fred Drake1bff34a2000-08-31 19:31:38 +0000513 dictobject *mp = (dictobject *)op;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000514 if (!PyDict_Check(op)) {
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000515 return NULL;
516 }
Tim Peters0ab085c2001-09-14 00:25:33 +0000517 if (!PyString_CheckExact(key) ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000518 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Guido van Rossumca756f21997-01-23 19:39:29 +0000519 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000520 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000521 if (hash == -1) {
522 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000523 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000524 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000525 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000526 return (mp->ma_lookup)(mp, key, hash)->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000527}
528
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000529/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
Tim Peters60b29962006-01-01 01:19:23 +0000530 * dictionary if it's merely replacing the value for an existing key.
531 * This means that it's safe to loop over a dictionary with PyDict_Next()
532 * and occasionally replace a value -- but you can't insert new keys or
533 * remove them.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000534 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000535int
Tim Peters1f5871e2000-07-04 17:44:48 +0000536PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000537{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000538 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000539 register long hash;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000540 register int n_used;
541
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000542 if (!PyDict_Check(op)) {
543 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000544 return -1;
545 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000546 mp = (dictobject *)op;
Tim Peters0ab085c2001-09-14 00:25:33 +0000547 if (PyString_CheckExact(key)) {
Guido van Rossum45ec02a2002-08-19 21:43:18 +0000548 hash = ((PyStringObject *)key)->ob_shash;
549 if (hash == -1)
550 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000551 }
Tim Peters1f7df352002-03-29 03:29:08 +0000552 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000553 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000554 if (hash == -1)
555 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000556 }
Tim Petersafb6ae82001-06-04 21:00:21 +0000557 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000558 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000559 Py_INCREF(value);
560 Py_INCREF(key);
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000561 insertdict(mp, key, hash, value);
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000562 /* If we added a key, we can safely resize. Otherwise just return!
563 * If fill >= 2/3 size, adjust size. Normally, this doubles or
564 * quaduples the size, but it's also possible for the dict to shrink
Tim Peters60b29962006-01-01 01:19:23 +0000565 * (if ma_fill is much larger than ma_used, meaning a lot of dict
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000566 * keys have been * deleted).
Tim Peters60b29962006-01-01 01:19:23 +0000567 *
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000568 * Quadrupling the size improves average dictionary sparseness
569 * (reducing collisions) at the cost of some memory and iteration
570 * speed (which loops over every possible entry). It also halves
571 * the number of expensive resize operations in a growing dictionary.
Tim Peters60b29962006-01-01 01:19:23 +0000572 *
573 * Very large dictionaries (over 50K items) use doubling instead.
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000574 * This may help applications with severe memory constraints.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000575 */
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000576 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
577 return 0;
Raymond Hettingerf81e4502005-08-17 02:19:36 +0000578 return dictresize(mp, (mp->ma_used>50000 ? mp->ma_used*2 : mp->ma_used*4));
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000579}
580
581int
Tim Peters1f5871e2000-07-04 17:44:48 +0000582PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000583{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000584 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000585 register long hash;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000586 register dictentry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000587 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000588
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000589 if (!PyDict_Check(op)) {
590 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000591 return -1;
592 }
Tim Peters0ab085c2001-09-14 00:25:33 +0000593 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +0000594 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000595 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000596 if (hash == -1)
597 return -1;
598 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000599 mp = (dictobject *)op;
Fred Drake1bff34a2000-08-31 19:31:38 +0000600 ep = (mp->ma_lookup)(mp, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000601 if (ep->me_value == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000602 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000603 return -1;
604 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000605 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000606 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000607 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000608 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000609 ep->me_value = NULL;
610 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000611 Py_DECREF(old_value);
612 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000613 return 0;
614}
615
Guido van Rossum25831651993-05-19 14:50:45 +0000616void
Tim Peters1f5871e2000-07-04 17:44:48 +0000617PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000618{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000619 dictobject *mp;
Tim Petersdea48ec2001-05-22 20:40:22 +0000620 dictentry *ep, *table;
621 int table_is_malloced;
622 int fill;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000623 dictentry small_copy[PyDict_MINSIZE];
Tim Petersdea48ec2001-05-22 20:40:22 +0000624#ifdef Py_DEBUG
625 int i, n;
626#endif
627
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000628 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000629 return;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000630 mp = (dictobject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000631#ifdef Py_DEBUG
Tim Petersafb6ae82001-06-04 21:00:21 +0000632 n = mp->ma_mask + 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000633 i = 0;
634#endif
635
636 table = mp->ma_table;
637 assert(table != NULL);
638 table_is_malloced = table != mp->ma_smalltable;
639
640 /* This is delicate. During the process of clearing the dict,
641 * decrefs can cause the dict to mutate. To avoid fatal confusion
642 * (voice of experience), we have to make the dict empty before
643 * clearing the slots, and never refer to anything via mp->xxx while
644 * clearing.
645 */
646 fill = mp->ma_fill;
647 if (table_is_malloced)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000648 EMPTY_TO_MINSIZE(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000649
650 else if (fill > 0) {
651 /* It's a small table with something that needs to be cleared.
652 * Afraid the only safe way is to copy the dict entries into
653 * another small table first.
654 */
655 memcpy(small_copy, table, sizeof(small_copy));
656 table = small_copy;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000657 EMPTY_TO_MINSIZE(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000658 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000659 /* else it's a small table that's already empty */
660
661 /* Now we can finally clear things. If C had refcounts, we could
662 * assert that the refcount on table is 1 now, i.e. that this function
663 * has unique access to it, so decref side-effects can't alter it.
664 */
665 for (ep = table; fill > 0; ++ep) {
666#ifdef Py_DEBUG
667 assert(i < n);
668 ++i;
669#endif
670 if (ep->me_key) {
671 --fill;
672 Py_DECREF(ep->me_key);
673 Py_XDECREF(ep->me_value);
674 }
675#ifdef Py_DEBUG
676 else
677 assert(ep->me_value == NULL);
678#endif
679 }
680
681 if (table_is_malloced)
682 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000683}
684
Tim Peters080c88b2003-02-15 03:01:11 +0000685/*
686 * Iterate over a dict. Use like so:
687 *
688 * int i;
689 * PyObject *key, *value;
690 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +0000691 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +0000692 * Refer to borrowed references in key and value.
693 * }
694 *
695 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +0000696 * mutates the dict. One exception: it is safe if the loop merely changes
697 * the values associated with the keys (but doesn't insert new keys or
698 * delete keys), via PyDict_SetItem().
699 */
Guido van Rossum25831651993-05-19 14:50:45 +0000700int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000701PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000702{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000703 register Py_ssize_t i;
704 register int mask;
Raymond Hettinger43442782004-03-17 21:55:03 +0000705 register dictentry *ep;
706
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000707 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000708 return 0;
Guido van Rossum25831651993-05-19 14:50:45 +0000709 i = *ppos;
710 if (i < 0)
711 return 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000712 ep = ((dictobject *)op)->ma_table;
713 mask = ((dictobject *)op)->ma_mask;
714 while (i <= mask && ep[i].me_value == NULL)
Guido van Rossum25831651993-05-19 14:50:45 +0000715 i++;
716 *ppos = i+1;
Raymond Hettinger43442782004-03-17 21:55:03 +0000717 if (i > mask)
Guido van Rossum25831651993-05-19 14:50:45 +0000718 return 0;
719 if (pkey)
Raymond Hettinger43442782004-03-17 21:55:03 +0000720 *pkey = ep[i].me_key;
Guido van Rossum25831651993-05-19 14:50:45 +0000721 if (pvalue)
Raymond Hettinger43442782004-03-17 21:55:03 +0000722 *pvalue = ep[i].me_value;
Guido van Rossum25831651993-05-19 14:50:45 +0000723 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000724}
725
726/* Methods */
727
728static void
Tim Peters1f5871e2000-07-04 17:44:48 +0000729dict_dealloc(register dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000730{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000731 register dictentry *ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000732 int fill = mp->ma_fill;
Guido van Rossumff413af2002-03-28 20:34:59 +0000733 PyObject_GC_UnTrack(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000734 Py_TRASHCAN_SAFE_BEGIN(mp)
Tim Petersdea48ec2001-05-22 20:40:22 +0000735 for (ep = mp->ma_table; fill > 0; ep++) {
736 if (ep->me_key) {
737 --fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000738 Py_DECREF(ep->me_key);
Tim Petersdea48ec2001-05-22 20:40:22 +0000739 Py_XDECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000740 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000741 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000742 if (mp->ma_table != mp->ma_smalltable)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000743 PyMem_DEL(mp->ma_table);
Raymond Hettinger43442782004-03-17 21:55:03 +0000744 if (num_free_dicts < MAXFREEDICTS && mp->ob_type == &PyDict_Type)
745 free_dicts[num_free_dicts++] = mp;
Tim Peters60b29962006-01-01 01:19:23 +0000746 else
Raymond Hettinger43442782004-03-17 21:55:03 +0000747 mp->ob_type->tp_free((PyObject *)mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000748 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000749}
750
751static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000752dict_print(register dictobject *mp, register FILE *fp, register int flags)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000753{
754 register int i;
755 register int any;
Guido van Rossum255443b1998-04-10 22:47:14 +0000756
757 i = Py_ReprEnter((PyObject*)mp);
758 if (i != 0) {
759 if (i < 0)
760 return i;
761 fprintf(fp, "{...}");
762 return 0;
763 }
764
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000765 fprintf(fp, "{");
766 any = 0;
Tim Petersafb6ae82001-06-04 21:00:21 +0000767 for (i = 0; i <= mp->ma_mask; i++) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000768 dictentry *ep = mp->ma_table + i;
769 PyObject *pvalue = ep->me_value;
770 if (pvalue != NULL) {
771 /* Prevent PyObject_Repr from deleting value during
772 key format */
773 Py_INCREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000774 if (any++ > 0)
775 fprintf(fp, ", ");
Guido van Rossum255443b1998-04-10 22:47:14 +0000776 if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000777 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000778 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000779 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000780 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000781 fprintf(fp, ": ");
Tim Peters19b77cf2001-06-02 08:27:39 +0000782 if (PyObject_Print(pvalue, fp, 0) != 0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000783 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000784 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000785 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000786 }
Tim Peters23cf6be2001-06-02 08:02:56 +0000787 Py_DECREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000788 }
789 }
790 fprintf(fp, "}");
Guido van Rossum255443b1998-04-10 22:47:14 +0000791 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000792 return 0;
793}
794
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000795static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000796dict_repr(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000797{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000798 Py_ssize_t i;
Tim Petersa7259592001-06-16 05:11:17 +0000799 PyObject *s, *temp, *colon = NULL;
800 PyObject *pieces = NULL, *result = NULL;
801 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +0000802
Tim Petersa7259592001-06-16 05:11:17 +0000803 i = Py_ReprEnter((PyObject *)mp);
Guido van Rossum255443b1998-04-10 22:47:14 +0000804 if (i != 0) {
Tim Petersa7259592001-06-16 05:11:17 +0000805 return i > 0 ? PyString_FromString("{...}") : NULL;
Guido van Rossum255443b1998-04-10 22:47:14 +0000806 }
807
Tim Petersa7259592001-06-16 05:11:17 +0000808 if (mp->ma_used == 0) {
809 result = PyString_FromString("{}");
810 goto Done;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000811 }
Tim Petersa7259592001-06-16 05:11:17 +0000812
813 pieces = PyList_New(0);
814 if (pieces == NULL)
815 goto Done;
816
817 colon = PyString_FromString(": ");
818 if (colon == NULL)
819 goto Done;
820
821 /* Do repr() on each key+value pair, and insert ": " between them.
822 Note that repr may mutate the dict. */
Tim Petersc6057842001-06-16 07:52:53 +0000823 i = 0;
824 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Tim Petersa7259592001-06-16 05:11:17 +0000825 int status;
826 /* Prevent repr from deleting value during key format. */
827 Py_INCREF(value);
828 s = PyObject_Repr(key);
829 PyString_Concat(&s, colon);
830 PyString_ConcatAndDel(&s, PyObject_Repr(value));
831 Py_DECREF(value);
832 if (s == NULL)
833 goto Done;
834 status = PyList_Append(pieces, s);
835 Py_DECREF(s); /* append created a new ref */
836 if (status < 0)
837 goto Done;
838 }
839
840 /* Add "{}" decorations to the first and last items. */
841 assert(PyList_GET_SIZE(pieces) > 0);
842 s = PyString_FromString("{");
843 if (s == NULL)
844 goto Done;
845 temp = PyList_GET_ITEM(pieces, 0);
846 PyString_ConcatAndDel(&s, temp);
847 PyList_SET_ITEM(pieces, 0, s);
848 if (s == NULL)
849 goto Done;
850
851 s = PyString_FromString("}");
852 if (s == NULL)
853 goto Done;
854 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
855 PyString_ConcatAndDel(&temp, s);
856 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
857 if (temp == NULL)
858 goto Done;
859
860 /* Paste them all together with ", " between. */
861 s = PyString_FromString(", ");
862 if (s == NULL)
863 goto Done;
864 result = _PyString_Join(s, pieces);
Tim Peters0ab085c2001-09-14 00:25:33 +0000865 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +0000866
867Done:
868 Py_XDECREF(pieces);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000869 Py_XDECREF(colon);
Tim Petersa7259592001-06-16 05:11:17 +0000870 Py_ReprLeave((PyObject *)mp);
871 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000872}
873
Martin v. Löwis18e16552006-02-15 17:27:45 +0000874static Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +0000875dict_length(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000876{
877 return mp->ma_used;
878}
879
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000880static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000881dict_subscript(dictobject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000882{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000883 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000884 long hash;
Tim Petersdea48ec2001-05-22 20:40:22 +0000885 assert(mp->ma_table != NULL);
Tim Peters0ab085c2001-09-14 00:25:33 +0000886 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +0000887 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000888 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000889 if (hash == -1)
890 return NULL;
891 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000892 v = (mp->ma_lookup)(mp, key, hash) -> me_value;
Guido van Rossum1968ad32006-02-25 22:38:04 +0000893 if (v == NULL) {
894 if (!PyDict_CheckExact(mp)) {
895 /* Look up __missing__ method if we're a subclass. */
Guido van Rossum4b92a822006-02-25 23:32:30 +0000896 PyObject *missing;
Guido van Rossum1968ad32006-02-25 22:38:04 +0000897 static PyObject *missing_str = NULL;
898 if (missing_str == NULL)
899 missing_str =
900 PyString_InternFromString("__missing__");
Guido van Rossum4b92a822006-02-25 23:32:30 +0000901 missing = _PyType_Lookup(mp->ob_type, missing_str);
Guido van Rossum1968ad32006-02-25 22:38:04 +0000902 if (missing != NULL)
903 return PyObject_CallFunctionObjArgs(missing,
904 (PyObject *)mp, key, NULL);
905 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000906 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum1968ad32006-02-25 22:38:04 +0000907 return NULL;
908 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000909 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000910 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000911 return v;
912}
913
914static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000915dict_ass_sub(dictobject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000916{
917 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000918 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000919 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000920 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000921}
922
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000923static PyMappingMethods dict_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000924 (lenfunc)dict_length, /*mp_length*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000925 (binaryfunc)dict_subscript, /*mp_subscript*/
926 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000927};
928
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000929static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000930dict_keys(register dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000931{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000932 register PyObject *v;
Raymond Hettinger43442782004-03-17 21:55:03 +0000933 register int i, j;
934 dictentry *ep;
935 int mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000936
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000937 again:
938 n = mp->ma_used;
939 v = PyList_New(n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000940 if (v == NULL)
941 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000942 if (n != mp->ma_used) {
943 /* Durnit. The allocations caused the dict to resize.
944 * Just start over, this shouldn't normally happen.
945 */
946 Py_DECREF(v);
947 goto again;
948 }
Raymond Hettinger43442782004-03-17 21:55:03 +0000949 ep = mp->ma_table;
950 mask = mp->ma_mask;
951 for (i = 0, j = 0; i <= mask; i++) {
952 if (ep[i].me_value != NULL) {
953 PyObject *key = ep[i].me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000954 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000955 PyList_SET_ITEM(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000956 j++;
957 }
958 }
Raymond Hettinger43442782004-03-17 21:55:03 +0000959 assert(j == n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000960 return v;
961}
962
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000963static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000964dict_values(register dictobject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +0000965{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000966 register PyObject *v;
Raymond Hettinger43442782004-03-17 21:55:03 +0000967 register int i, j;
968 dictentry *ep;
969 int mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000970
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000971 again:
972 n = mp->ma_used;
973 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +0000974 if (v == NULL)
975 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000976 if (n != mp->ma_used) {
977 /* Durnit. The allocations caused the dict to resize.
978 * Just start over, this shouldn't normally happen.
979 */
980 Py_DECREF(v);
981 goto again;
982 }
Raymond Hettinger43442782004-03-17 21:55:03 +0000983 ep = mp->ma_table;
984 mask = mp->ma_mask;
985 for (i = 0, j = 0; i <= mask; i++) {
986 if (ep[i].me_value != NULL) {
987 PyObject *value = ep[i].me_value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000988 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000989 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +0000990 j++;
991 }
992 }
Raymond Hettinger43442782004-03-17 21:55:03 +0000993 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +0000994 return v;
995}
996
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000997static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000998dict_items(register dictobject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +0000999{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001000 register PyObject *v;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001001 register int i, j, n;
Raymond Hettinger43442782004-03-17 21:55:03 +00001002 int mask;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001003 PyObject *item, *key, *value;
Raymond Hettinger43442782004-03-17 21:55:03 +00001004 dictentry *ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001005
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001006 /* Preallocate the list of tuples, to avoid allocations during
1007 * the loop over the items, which could trigger GC, which
1008 * could resize the dict. :-(
1009 */
1010 again:
1011 n = mp->ma_used;
1012 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001013 if (v == NULL)
1014 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001015 for (i = 0; i < n; i++) {
1016 item = PyTuple_New(2);
1017 if (item == NULL) {
1018 Py_DECREF(v);
1019 return NULL;
1020 }
1021 PyList_SET_ITEM(v, i, item);
1022 }
1023 if (n != mp->ma_used) {
1024 /* Durnit. The allocations caused the dict to resize.
1025 * Just start over, this shouldn't normally happen.
1026 */
1027 Py_DECREF(v);
1028 goto again;
1029 }
1030 /* Nothing we do below makes any function calls. */
Raymond Hettinger43442782004-03-17 21:55:03 +00001031 ep = mp->ma_table;
1032 mask = mp->ma_mask;
1033 for (i = 0, j = 0; i <= mask; i++) {
Raymond Hettinger06905122004-03-19 10:30:00 +00001034 if ((value=ep[i].me_value) != NULL) {
Raymond Hettinger43442782004-03-17 21:55:03 +00001035 key = ep[i].me_key;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001036 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001037 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001038 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001039 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001040 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001041 j++;
1042 }
1043 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001044 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001045 return v;
1046}
1047
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001048static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001049dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001050{
1051 PyObject *seq;
1052 PyObject *value = Py_None;
1053 PyObject *it; /* iter(seq) */
1054 PyObject *key;
1055 PyObject *d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001056 int status;
1057
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001058 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001059 return NULL;
1060
1061 d = PyObject_CallObject(cls, NULL);
1062 if (d == NULL)
1063 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001064
1065 it = PyObject_GetIter(seq);
1066 if (it == NULL){
1067 Py_DECREF(d);
1068 return NULL;
1069 }
1070
1071 for (;;) {
1072 key = PyIter_Next(it);
1073 if (key == NULL) {
1074 if (PyErr_Occurred())
1075 goto Fail;
1076 break;
1077 }
Raymond Hettingere03e5b12002-12-07 08:10:51 +00001078 status = PyObject_SetItem(d, key, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001079 Py_DECREF(key);
1080 if (status < 0)
1081 goto Fail;
1082 }
1083
1084 Py_DECREF(it);
1085 return d;
1086
1087Fail:
1088 Py_DECREF(it);
1089 Py_DECREF(d);
1090 return NULL;
1091}
1092
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001093static int
1094dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001095{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001096 PyObject *arg = NULL;
1097 int result = 0;
1098
1099 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1100 result = -1;
1101
1102 else if (arg != NULL) {
1103 if (PyObject_HasAttrString(arg, "keys"))
1104 result = PyDict_Merge(self, arg, 1);
1105 else
1106 result = PyDict_MergeFromSeq2(self, arg, 1);
1107 }
1108 if (result == 0 && kwds != NULL)
1109 result = PyDict_Merge(self, kwds, 1);
1110 return result;
1111}
1112
1113static PyObject *
1114dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1115{
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001116 if (dict_update_common(self, args, kwds, "update") != -1)
1117 Py_RETURN_NONE;
1118 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001119}
1120
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001121/* Update unconditionally replaces existing items.
1122 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001123 otherwise it leaves existing items unchanged.
1124
1125 PyDict_{Update,Merge} update/merge from a mapping object.
1126
Tim Petersf582b822001-12-11 18:51:08 +00001127 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001128 producing iterable objects of length 2.
1129*/
1130
Tim Petersf582b822001-12-11 18:51:08 +00001131int
Tim Peters1fc240e2001-10-26 05:06:50 +00001132PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1133{
1134 PyObject *it; /* iter(seq2) */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001135 int i; /* index into seq2 of current element */
Tim Peters1fc240e2001-10-26 05:06:50 +00001136 PyObject *item; /* seq2[i] */
1137 PyObject *fast; /* item as a 2-tuple or 2-list */
1138
1139 assert(d != NULL);
1140 assert(PyDict_Check(d));
1141 assert(seq2 != NULL);
1142
1143 it = PyObject_GetIter(seq2);
1144 if (it == NULL)
1145 return -1;
1146
1147 for (i = 0; ; ++i) {
1148 PyObject *key, *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001149 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001150
1151 fast = NULL;
1152 item = PyIter_Next(it);
1153 if (item == NULL) {
1154 if (PyErr_Occurred())
1155 goto Fail;
1156 break;
1157 }
1158
1159 /* Convert item to sequence, and verify length 2. */
1160 fast = PySequence_Fast(item, "");
1161 if (fast == NULL) {
1162 if (PyErr_ExceptionMatches(PyExc_TypeError))
1163 PyErr_Format(PyExc_TypeError,
1164 "cannot convert dictionary update "
1165 "sequence element #%d to a sequence",
1166 i);
1167 goto Fail;
1168 }
1169 n = PySequence_Fast_GET_SIZE(fast);
1170 if (n != 2) {
1171 PyErr_Format(PyExc_ValueError,
1172 "dictionary update sequence element #%d "
Martin v. Löwis2c95cc62006-02-16 06:54:25 +00001173 "has length %zd; 2 is required",
Martin v. Löwise0e89f72006-02-16 06:59:22 +00001174 i, n);
Tim Peters1fc240e2001-10-26 05:06:50 +00001175 goto Fail;
1176 }
1177
1178 /* Update/merge with this (key, value) pair. */
1179 key = PySequence_Fast_GET_ITEM(fast, 0);
1180 value = PySequence_Fast_GET_ITEM(fast, 1);
1181 if (override || PyDict_GetItem(d, key) == NULL) {
1182 int status = PyDict_SetItem(d, key, value);
1183 if (status < 0)
1184 goto Fail;
1185 }
1186 Py_DECREF(fast);
1187 Py_DECREF(item);
1188 }
1189
1190 i = 0;
1191 goto Return;
1192Fail:
1193 Py_XDECREF(item);
1194 Py_XDECREF(fast);
1195 i = -1;
1196Return:
1197 Py_DECREF(it);
1198 return i;
1199}
1200
Tim Peters6d6c1a32001-08-02 04:15:00 +00001201int
1202PyDict_Update(PyObject *a, PyObject *b)
1203{
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001204 return PyDict_Merge(a, b, 1);
1205}
1206
1207int
1208PyDict_Merge(PyObject *a, PyObject *b, int override)
1209{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001210 register PyDictObject *mp, *other;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001211 register int i;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001212 dictentry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001213
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001214 /* We accept for the argument either a concrete dictionary object,
1215 * or an abstract "mapping" object. For the former, we can do
1216 * things quite efficiently. For the latter, we only require that
1217 * PyMapping_Keys() and PyObject_GetItem() be supported.
1218 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001219 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1220 PyErr_BadInternalCall();
1221 return -1;
1222 }
1223 mp = (dictobject*)a;
1224 if (PyDict_Check(b)) {
1225 other = (dictobject*)b;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001226 if (other == mp || other->ma_used == 0)
1227 /* a.update(a) or a.update({}); nothing to do */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001228 return 0;
Raymond Hettinger186e7392005-05-14 18:08:25 +00001229 if (mp->ma_used == 0)
1230 /* Since the target dict is empty, PyDict_GetItem()
1231 * always returns NULL. Setting override to 1
1232 * skips the unnecessary test.
1233 */
1234 override = 1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001235 /* Do one big resize at the start, rather than
1236 * incrementally resizing as we insert new items. Expect
1237 * that there will be no (or few) overlapping keys.
1238 */
1239 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
Raymond Hettingerc8d22902003-05-07 00:49:40 +00001240 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001241 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001242 }
1243 for (i = 0; i <= other->ma_mask; i++) {
1244 entry = &other->ma_table[i];
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001245 if (entry->me_value != NULL &&
1246 (override ||
1247 PyDict_GetItem(a, entry->me_key) == NULL)) {
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001248 Py_INCREF(entry->me_key);
1249 Py_INCREF(entry->me_value);
1250 insertdict(mp, entry->me_key, entry->me_hash,
1251 entry->me_value);
1252 }
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001253 }
1254 }
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001255 else {
1256 /* Do it the generic, slower way */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001257 PyObject *keys = PyMapping_Keys(b);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001258 PyObject *iter;
1259 PyObject *key, *value;
1260 int status;
1261
1262 if (keys == NULL)
1263 /* Docstring says this is equivalent to E.keys() so
1264 * if E doesn't have a .keys() method we want
1265 * AttributeError to percolate up. Might as well
1266 * do the same for any other error.
1267 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001268 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001269
1270 iter = PyObject_GetIter(keys);
1271 Py_DECREF(keys);
1272 if (iter == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001273 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001274
1275 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001276 if (!override && PyDict_GetItem(a, key) != NULL) {
1277 Py_DECREF(key);
1278 continue;
1279 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001280 value = PyObject_GetItem(b, key);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001281 if (value == NULL) {
1282 Py_DECREF(iter);
1283 Py_DECREF(key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001284 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001285 }
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001286 status = PyDict_SetItem(a, key, value);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001287 Py_DECREF(key);
1288 Py_DECREF(value);
1289 if (status < 0) {
1290 Py_DECREF(iter);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001291 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001292 }
1293 }
1294 Py_DECREF(iter);
1295 if (PyErr_Occurred())
1296 /* Iterator completed, via error */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001297 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001298 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001299 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001300}
1301
1302static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001303dict_copy(register dictobject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001304{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001305 return PyDict_Copy((PyObject*)mp);
1306}
1307
1308PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001309PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001310{
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001311 PyObject *copy;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001312
1313 if (o == NULL || !PyDict_Check(o)) {
1314 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001315 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001316 }
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001317 copy = PyDict_New();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001318 if (copy == NULL)
1319 return NULL;
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001320 if (PyDict_Merge(copy, o, 1) == 0)
1321 return copy;
1322 Py_DECREF(copy);
Raymond Hettinger1356f782005-04-15 15:58:42 +00001323 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001324}
1325
Martin v. Löwis18e16552006-02-15 17:27:45 +00001326Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00001327PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001328{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001329 if (mp == NULL || !PyDict_Check(mp)) {
1330 PyErr_BadInternalCall();
Jeremy Hylton7083bb72004-02-17 20:10:11 +00001331 return -1;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001332 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001333 return ((dictobject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001334}
1335
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001336PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001337PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001338{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001339 if (mp == NULL || !PyDict_Check(mp)) {
1340 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001341 return NULL;
1342 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001343 return dict_keys((dictobject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001344}
1345
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001346PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001347PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001348{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001349 if (mp == NULL || !PyDict_Check(mp)) {
1350 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001351 return NULL;
1352 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001353 return dict_values((dictobject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001354}
1355
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001356PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001357PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001358{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001359 if (mp == NULL || !PyDict_Check(mp)) {
1360 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001361 return NULL;
1362 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001363 return dict_items((dictobject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001364}
1365
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001366/* Subroutine which returns the smallest key in a for which b's value
1367 is different or absent. The value is returned too, through the
Tim Peters95bf9392001-05-10 08:32:44 +00001368 pval argument. Both are NULL if no key in a is found for which b's status
1369 differs. The refcounts on (and only on) non-NULL *pval and function return
1370 values must be decremented by the caller (characterize() increments them
1371 to ensure that mutating comparison and PyDict_GetItem calls can't delete
1372 them before the caller is done looking at them). */
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001373
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001374static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001375characterize(dictobject *a, dictobject *b, PyObject **pval)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001376{
Tim Peters95bf9392001-05-10 08:32:44 +00001377 PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */
1378 PyObject *aval = NULL; /* a[akey] */
Guido van Rossumb9324202001-01-18 00:39:02 +00001379 int i, cmp;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001380
Tim Petersafb6ae82001-06-04 21:00:21 +00001381 for (i = 0; i <= a->ma_mask; i++) {
Tim Peters95bf9392001-05-10 08:32:44 +00001382 PyObject *thiskey, *thisaval, *thisbval;
1383 if (a->ma_table[i].me_value == NULL)
1384 continue;
1385 thiskey = a->ma_table[i].me_key;
1386 Py_INCREF(thiskey); /* keep alive across compares */
1387 if (akey != NULL) {
1388 cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT);
1389 if (cmp < 0) {
1390 Py_DECREF(thiskey);
1391 goto Fail;
1392 }
1393 if (cmp > 0 ||
Tim Petersafb6ae82001-06-04 21:00:21 +00001394 i > a->ma_mask ||
Tim Peters95bf9392001-05-10 08:32:44 +00001395 a->ma_table[i].me_value == NULL)
1396 {
1397 /* Not the *smallest* a key; or maybe it is
1398 * but the compare shrunk the dict so we can't
1399 * find its associated value anymore; or
1400 * maybe it is but the compare deleted the
1401 * a[thiskey] entry.
1402 */
1403 Py_DECREF(thiskey);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001404 continue;
Guido van Rossumb9324202001-01-18 00:39:02 +00001405 }
Tim Peters95bf9392001-05-10 08:32:44 +00001406 }
1407
1408 /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */
1409 thisaval = a->ma_table[i].me_value;
1410 assert(thisaval);
1411 Py_INCREF(thisaval); /* keep alive */
1412 thisbval = PyDict_GetItem((PyObject *)b, thiskey);
1413 if (thisbval == NULL)
1414 cmp = 0;
1415 else {
1416 /* both dicts have thiskey: same values? */
1417 cmp = PyObject_RichCompareBool(
1418 thisaval, thisbval, Py_EQ);
1419 if (cmp < 0) {
1420 Py_DECREF(thiskey);
1421 Py_DECREF(thisaval);
1422 goto Fail;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001423 }
1424 }
Tim Peters95bf9392001-05-10 08:32:44 +00001425 if (cmp == 0) {
1426 /* New winner. */
1427 Py_XDECREF(akey);
1428 Py_XDECREF(aval);
1429 akey = thiskey;
1430 aval = thisaval;
1431 }
1432 else {
1433 Py_DECREF(thiskey);
1434 Py_DECREF(thisaval);
1435 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001436 }
Tim Peters95bf9392001-05-10 08:32:44 +00001437 *pval = aval;
1438 return akey;
1439
1440Fail:
1441 Py_XDECREF(akey);
1442 Py_XDECREF(aval);
1443 *pval = NULL;
1444 return NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001445}
1446
1447static int
Tim Peters1f5871e2000-07-04 17:44:48 +00001448dict_compare(dictobject *a, dictobject *b)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001449{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001450 PyObject *adiff, *bdiff, *aval, *bval;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001451 int res;
1452
1453 /* Compare lengths first */
1454 if (a->ma_used < b->ma_used)
1455 return -1; /* a is shorter */
1456 else if (a->ma_used > b->ma_used)
1457 return 1; /* b is shorter */
Tim Peters95bf9392001-05-10 08:32:44 +00001458
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001459 /* Same length -- check all keys */
Tim Peters95bf9392001-05-10 08:32:44 +00001460 bdiff = bval = NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001461 adiff = characterize(a, b, &aval);
Tim Peters95bf9392001-05-10 08:32:44 +00001462 if (adiff == NULL) {
1463 assert(!aval);
Tim Peters3918fb22001-05-10 18:58:31 +00001464 /* Either an error, or a is a subset with the same length so
Tim Peters95bf9392001-05-10 08:32:44 +00001465 * must be equal.
1466 */
1467 res = PyErr_Occurred() ? -1 : 0;
1468 goto Finished;
1469 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001470 bdiff = characterize(b, a, &bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001471 if (bdiff == NULL && PyErr_Occurred()) {
1472 assert(!bval);
1473 res = -1;
1474 goto Finished;
1475 }
1476 res = 0;
1477 if (bdiff) {
1478 /* bdiff == NULL "should be" impossible now, but perhaps
1479 * the last comparison done by the characterize() on a had
1480 * the side effect of making the dicts equal!
1481 */
1482 res = PyObject_Compare(adiff, bdiff);
1483 }
1484 if (res == 0 && bval != NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001485 res = PyObject_Compare(aval, bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001486
1487Finished:
1488 Py_XDECREF(adiff);
1489 Py_XDECREF(bdiff);
1490 Py_XDECREF(aval);
1491 Py_XDECREF(bval);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001492 return res;
1493}
1494
Tim Peterse63415e2001-05-08 04:38:29 +00001495/* Return 1 if dicts equal, 0 if not, -1 if error.
1496 * Gets out as soon as any difference is detected.
1497 * Uses only Py_EQ comparison.
1498 */
1499static int
1500dict_equal(dictobject *a, dictobject *b)
1501{
1502 int i;
1503
1504 if (a->ma_used != b->ma_used)
1505 /* can't be equal if # of entries differ */
1506 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001507
Tim Peterse63415e2001-05-08 04:38:29 +00001508 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Tim Petersafb6ae82001-06-04 21:00:21 +00001509 for (i = 0; i <= a->ma_mask; i++) {
Tim Peterse63415e2001-05-08 04:38:29 +00001510 PyObject *aval = a->ma_table[i].me_value;
1511 if (aval != NULL) {
1512 int cmp;
1513 PyObject *bval;
1514 PyObject *key = a->ma_table[i].me_key;
1515 /* temporarily bump aval's refcount to ensure it stays
1516 alive until we're done with it */
1517 Py_INCREF(aval);
1518 bval = PyDict_GetItem((PyObject *)b, key);
1519 if (bval == NULL) {
1520 Py_DECREF(aval);
1521 return 0;
1522 }
1523 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1524 Py_DECREF(aval);
1525 if (cmp <= 0) /* error or not equal */
1526 return cmp;
1527 }
1528 }
1529 return 1;
1530 }
1531
1532static PyObject *
1533dict_richcompare(PyObject *v, PyObject *w, int op)
1534{
1535 int cmp;
1536 PyObject *res;
1537
1538 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1539 res = Py_NotImplemented;
1540 }
1541 else if (op == Py_EQ || op == Py_NE) {
1542 cmp = dict_equal((dictobject *)v, (dictobject *)w);
1543 if (cmp < 0)
1544 return NULL;
1545 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1546 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001547 else
1548 res = Py_NotImplemented;
Tim Peterse63415e2001-05-08 04:38:29 +00001549 Py_INCREF(res);
1550 return res;
1551 }
1552
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001553static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001554dict_has_key(register dictobject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001555{
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001556 long hash;
1557 register long ok;
Tim Peters0ab085c2001-09-14 00:25:33 +00001558 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001559 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001560 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001561 if (hash == -1)
1562 return NULL;
1563 }
Tim Petersdea48ec2001-05-22 20:40:22 +00001564 ok = (mp->ma_lookup)(mp, key, hash)->me_value != NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00001565 return PyBool_FromLong(ok);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001566}
1567
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001568static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001569dict_get(register dictobject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001570{
1571 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001572 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001573 PyObject *val = NULL;
1574 long hash;
1575
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001576 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001577 return NULL;
1578
Tim Peters0ab085c2001-09-14 00:25:33 +00001579 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001580 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Barry Warsawc38c5da1997-10-06 17:49:20 +00001581 hash = PyObject_Hash(key);
1582 if (hash == -1)
1583 return NULL;
1584 }
Fred Drake1bff34a2000-08-31 19:31:38 +00001585 val = (mp->ma_lookup)(mp, key, hash)->me_value;
Barry Warsaw320ac331997-10-20 17:26:25 +00001586
Barry Warsawc38c5da1997-10-06 17:49:20 +00001587 if (val == NULL)
1588 val = failobj;
1589 Py_INCREF(val);
1590 return val;
1591}
1592
1593
1594static PyObject *
Guido van Rossum164452c2000-08-08 16:12:54 +00001595dict_setdefault(register dictobject *mp, PyObject *args)
1596{
1597 PyObject *key;
1598 PyObject *failobj = Py_None;
1599 PyObject *val = NULL;
1600 long hash;
1601
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001602 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
Guido van Rossum164452c2000-08-08 16:12:54 +00001603 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001604
Tim Peters0ab085c2001-09-14 00:25:33 +00001605 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001606 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossum164452c2000-08-08 16:12:54 +00001607 hash = PyObject_Hash(key);
1608 if (hash == -1)
1609 return NULL;
1610 }
Fred Drake1bff34a2000-08-31 19:31:38 +00001611 val = (mp->ma_lookup)(mp, key, hash)->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001612 if (val == NULL) {
1613 val = failobj;
1614 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1615 val = NULL;
1616 }
1617 Py_XINCREF(val);
1618 return val;
1619}
1620
1621
1622static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001623dict_clear(register dictobject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001624{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001625 PyDict_Clear((PyObject *)mp);
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001626 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001627}
1628
Guido van Rossumba6ab842000-12-12 22:02:18 +00001629static PyObject *
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001630dict_pop(dictobject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00001631{
1632 long hash;
1633 dictentry *ep;
1634 PyObject *old_value, *old_key;
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001635 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001636
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001637 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1638 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001639 if (mp->ma_used == 0) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001640 if (deflt) {
1641 Py_INCREF(deflt);
1642 return deflt;
1643 }
Guido van Rossume027d982002-04-12 15:11:59 +00001644 PyErr_SetString(PyExc_KeyError,
1645 "pop(): dictionary is empty");
1646 return NULL;
1647 }
1648 if (!PyString_CheckExact(key) ||
1649 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1650 hash = PyObject_Hash(key);
1651 if (hash == -1)
1652 return NULL;
1653 }
1654 ep = (mp->ma_lookup)(mp, key, hash);
1655 if (ep->me_value == NULL) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001656 if (deflt) {
1657 Py_INCREF(deflt);
1658 return deflt;
1659 }
Guido van Rossume027d982002-04-12 15:11:59 +00001660 PyErr_SetObject(PyExc_KeyError, key);
1661 return NULL;
1662 }
1663 old_key = ep->me_key;
1664 Py_INCREF(dummy);
1665 ep->me_key = dummy;
1666 old_value = ep->me_value;
1667 ep->me_value = NULL;
1668 mp->ma_used--;
1669 Py_DECREF(old_key);
1670 return old_value;
1671}
1672
1673static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001674dict_popitem(dictobject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001675{
1676 int i = 0;
1677 dictentry *ep;
1678 PyObject *res;
1679
Tim Petersf4b33f62001-06-02 05:42:29 +00001680 /* Allocate the result tuple before checking the size. Believe it
1681 * or not, this allocation could trigger a garbage collection which
1682 * could empty the dict, so if we checked the size first and that
1683 * happened, the result would be an infinite loop (searching for an
1684 * entry that no longer exists). Note that the usual popitem()
1685 * idiom is "while d: k, v = d.popitem()". so needing to throw the
1686 * tuple away if the dict *is* empty isn't a significant
1687 * inefficiency -- possible, but unlikely in practice.
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001688 */
1689 res = PyTuple_New(2);
1690 if (res == NULL)
1691 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001692 if (mp->ma_used == 0) {
1693 Py_DECREF(res);
1694 PyErr_SetString(PyExc_KeyError,
1695 "popitem(): dictionary is empty");
1696 return NULL;
1697 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001698 /* Set ep to "the first" dict entry with a value. We abuse the hash
1699 * field of slot 0 to hold a search finger:
1700 * If slot 0 has a value, use slot 0.
1701 * Else slot 0 is being used to hold a search finger,
1702 * and we use its hash value as the first index to look.
1703 */
1704 ep = &mp->ma_table[0];
1705 if (ep->me_value == NULL) {
1706 i = (int)ep->me_hash;
Tim Petersdea48ec2001-05-22 20:40:22 +00001707 /* The hash field may be a real hash value, or it may be a
1708 * legit search finger, or it may be a once-legit search
1709 * finger that's out of bounds now because it wrapped around
1710 * or the table shrunk -- simply make sure it's in bounds now.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001711 */
Tim Petersafb6ae82001-06-04 21:00:21 +00001712 if (i > mp->ma_mask || i < 1)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001713 i = 1; /* skip slot 0 */
1714 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1715 i++;
Tim Petersafb6ae82001-06-04 21:00:21 +00001716 if (i > mp->ma_mask)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001717 i = 1;
1718 }
1719 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001720 PyTuple_SET_ITEM(res, 0, ep->me_key);
1721 PyTuple_SET_ITEM(res, 1, ep->me_value);
1722 Py_INCREF(dummy);
1723 ep->me_key = dummy;
1724 ep->me_value = NULL;
1725 mp->ma_used--;
1726 assert(mp->ma_table[0].me_value == NULL);
1727 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001728 return res;
1729}
1730
Jeremy Hylton8caad492000-06-23 14:18:11 +00001731static int
1732dict_traverse(PyObject *op, visitproc visit, void *arg)
1733{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001734 Py_ssize_t i = 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001735 PyObject *pk;
1736 PyObject *pv;
1737
1738 while (PyDict_Next(op, &i, &pk, &pv)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001739 Py_VISIT(pk);
1740 Py_VISIT(pv);
Jeremy Hylton8caad492000-06-23 14:18:11 +00001741 }
1742 return 0;
1743}
1744
1745static int
1746dict_tp_clear(PyObject *op)
1747{
1748 PyDict_Clear(op);
1749 return 0;
1750}
1751
Tim Petersf7f88b12000-12-13 23:18:45 +00001752
Raymond Hettinger019a1482004-03-18 02:41:19 +00001753extern PyTypeObject PyDictIterKey_Type; /* Forward */
1754extern PyTypeObject PyDictIterValue_Type; /* Forward */
1755extern PyTypeObject PyDictIterItem_Type; /* Forward */
1756static PyObject *dictiter_new(dictobject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001757
1758static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001759dict_iterkeys(dictobject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001760{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001761 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001762}
1763
1764static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001765dict_itervalues(dictobject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001766{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001767 return dictiter_new(dict, &PyDictIterValue_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001768}
1769
1770static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001771dict_iteritems(dictobject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001772{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001773 return dictiter_new(dict, &PyDictIterItem_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001774}
1775
1776
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001777PyDoc_STRVAR(has_key__doc__,
Raymond Hettinger574aa322003-09-01 22:12:08 +00001778"D.has_key(k) -> True if D has a key k, else False");
Tim Petersf7f88b12000-12-13 23:18:45 +00001779
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001780PyDoc_STRVAR(contains__doc__,
1781"D.__contains__(k) -> True if D has a key k, else False");
1782
1783PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
1784
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001785PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001786"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001787
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001788PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001789"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 +00001790
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001791PyDoc_STRVAR(pop__doc__,
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001792"D.pop(k[,d]) -> v, remove specified key and return the corresponding value\n\
1793If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00001794
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001795PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00001796"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000017972-tuple; but raise KeyError if D is empty");
Tim Petersf7f88b12000-12-13 23:18:45 +00001798
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001799PyDoc_STRVAR(keys__doc__,
1800"D.keys() -> list of D's keys");
Tim Petersf7f88b12000-12-13 23:18:45 +00001801
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001802PyDoc_STRVAR(items__doc__,
1803"D.items() -> list of D's (key, value) pairs, as 2-tuples");
Tim Petersf7f88b12000-12-13 23:18:45 +00001804
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001805PyDoc_STRVAR(values__doc__,
1806"D.values() -> list of D's values");
Tim Petersf7f88b12000-12-13 23:18:45 +00001807
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001808PyDoc_STRVAR(update__doc__,
Walter Dörwaldd70ad8a2004-05-28 20:59:21 +00001809"D.update(E, **F) -> None. Update D from E and F: for k in E: D[k] = E[k]\n\
1810(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 +00001811
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001812PyDoc_STRVAR(fromkeys__doc__,
1813"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
1814v defaults to None.");
1815
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001816PyDoc_STRVAR(clear__doc__,
1817"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001818
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001819PyDoc_STRVAR(copy__doc__,
1820"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00001821
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001822PyDoc_STRVAR(iterkeys__doc__,
1823"D.iterkeys() -> an iterator over the keys of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00001824
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001825PyDoc_STRVAR(itervalues__doc__,
1826"D.itervalues() -> an iterator over the values of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00001827
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001828PyDoc_STRVAR(iteritems__doc__,
1829"D.iteritems() -> an iterator over the (key, value) items of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00001830
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001831static PyMethodDef mapp_methods[] = {
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001832 {"__contains__",(PyCFunction)dict_has_key, METH_O | METH_COEXIST,
1833 contains__doc__},
Raymond Hettinger0c669672003-12-13 13:31:55 +00001834 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001835 getitem__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001836 {"has_key", (PyCFunction)dict_has_key, METH_O,
Tim Petersf7f88b12000-12-13 23:18:45 +00001837 has_key__doc__},
1838 {"get", (PyCFunction)dict_get, METH_VARARGS,
1839 get__doc__},
1840 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
1841 setdefault_doc__},
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001842 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
Just van Rossuma797d812002-11-23 09:45:04 +00001843 pop__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001844 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001845 popitem__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001846 {"keys", (PyCFunction)dict_keys, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001847 keys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001848 {"items", (PyCFunction)dict_items, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001849 items__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001850 {"values", (PyCFunction)dict_values, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001851 values__doc__},
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001852 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001853 update__doc__},
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001854 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
1855 fromkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001856 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001857 clear__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001858 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001859 copy__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001860 {"iterkeys", (PyCFunction)dict_iterkeys, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00001861 iterkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001862 {"itervalues", (PyCFunction)dict_itervalues, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00001863 itervalues__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001864 {"iteritems", (PyCFunction)dict_iteritems, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00001865 iteritems__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00001866 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001867};
1868
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001869int
1870PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001871{
1872 long hash;
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001873 dictobject *mp = (dictobject *)op;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001874
Tim Peters0ab085c2001-09-14 00:25:33 +00001875 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001876 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001877 hash = PyObject_Hash(key);
1878 if (hash == -1)
1879 return -1;
1880 }
Tim Petersdea48ec2001-05-22 20:40:22 +00001881 return (mp->ma_lookup)(mp, key, hash)->me_value != NULL;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001882}
1883
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001884/* Hack to implement "key in dict" */
1885static PySequenceMethods dict_as_sequence = {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001886 0, /* sq_length */
1887 0, /* sq_concat */
1888 0, /* sq_repeat */
1889 0, /* sq_item */
1890 0, /* sq_slice */
1891 0, /* sq_ass_item */
1892 0, /* sq_ass_slice */
1893 PyDict_Contains, /* sq_contains */
1894 0, /* sq_inplace_concat */
1895 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001896};
1897
Guido van Rossum09e563a2001-05-01 12:10:21 +00001898static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001899dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1900{
1901 PyObject *self;
1902
1903 assert(type != NULL && type->tp_alloc != NULL);
1904 self = type->tp_alloc(type, 0);
1905 if (self != NULL) {
1906 PyDictObject *d = (PyDictObject *)self;
1907 /* It's guaranteed that tp->alloc zeroed out the struct. */
1908 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
1909 INIT_NONZERO_DICT_SLOTS(d);
1910 d->ma_lookup = lookdict_string;
1911#ifdef SHOW_CONVERSION_COUNTS
1912 ++created;
1913#endif
1914 }
1915 return self;
1916}
1917
Tim Peters25786c02001-09-02 08:22:48 +00001918static int
1919dict_init(PyObject *self, PyObject *args, PyObject *kwds)
1920{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001921 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00001922}
1923
Guido van Rossumdbb53d92001-12-03 16:32:18 +00001924static long
1925dict_nohash(PyObject *self)
1926{
1927 PyErr_SetString(PyExc_TypeError, "dict objects are unhashable");
1928 return -1;
1929}
1930
Tim Peters6d6c1a32001-08-02 04:15:00 +00001931static PyObject *
Guido van Rossum09e563a2001-05-01 12:10:21 +00001932dict_iter(dictobject *dict)
1933{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001934 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001935}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001936
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001937PyDoc_STRVAR(dictionary_doc,
Tim Petersa427a2b2001-10-29 22:25:45 +00001938"dict() -> new empty dictionary.\n"
1939"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Tim Peters1fc240e2001-10-26 05:06:50 +00001940" (key, value) pairs.\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00001941"dict(seq) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00001942" d = {}\n"
1943" for k, v in seq:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00001944" d[k] = v\n"
1945"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
1946" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00001947
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001948PyTypeObject PyDict_Type = {
1949 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001950 0,
Tim Petersa427a2b2001-10-29 22:25:45 +00001951 "dict",
Neil Schemenauere83c00e2001-08-29 23:54:21 +00001952 sizeof(dictobject),
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001953 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00001954 (destructor)dict_dealloc, /* tp_dealloc */
1955 (printfunc)dict_print, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001956 0, /* tp_getattr */
Guido van Rossumb9324202001-01-18 00:39:02 +00001957 0, /* tp_setattr */
Tim Peters4fa58bf2001-05-10 21:45:19 +00001958 (cmpfunc)dict_compare, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00001959 (reprfunc)dict_repr, /* tp_repr */
1960 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001961 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00001962 &dict_as_mapping, /* tp_as_mapping */
Guido van Rossumdbb53d92001-12-03 16:32:18 +00001963 dict_nohash, /* tp_hash */
Guido van Rossumb9324202001-01-18 00:39:02 +00001964 0, /* tp_call */
1965 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001966 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumb9324202001-01-18 00:39:02 +00001967 0, /* tp_setattro */
1968 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00001969 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Tim Peters6d6c1a32001-08-02 04:15:00 +00001970 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters25786c02001-09-02 08:22:48 +00001971 dictionary_doc, /* tp_doc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001972 dict_traverse, /* tp_traverse */
1973 dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00001974 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001975 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00001976 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001977 0, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001978 mapp_methods, /* tp_methods */
1979 0, /* tp_members */
1980 0, /* tp_getset */
1981 0, /* tp_base */
1982 0, /* tp_dict */
1983 0, /* tp_descr_get */
1984 0, /* tp_descr_set */
1985 0, /* tp_dictoffset */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001986 dict_init, /* tp_init */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001987 PyType_GenericAlloc, /* tp_alloc */
1988 dict_new, /* tp_new */
Neil Schemenauer6189b892002-04-12 02:43:00 +00001989 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001990};
1991
Guido van Rossum3cca2451997-05-16 14:23:33 +00001992/* For backward compatibility with old dictionary interface */
1993
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001994PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00001995PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001996{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001997 PyObject *kv, *rv;
1998 kv = PyString_FromString(key);
1999 if (kv == NULL)
2000 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002001 rv = PyDict_GetItem(v, kv);
2002 Py_DECREF(kv);
2003 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002004}
2005
2006int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002007PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002008{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002009 PyObject *kv;
2010 int err;
2011 kv = PyString_FromString(key);
2012 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002013 return -1;
Guido van Rossum4f3bf1e1997-09-29 23:31:11 +00002014 PyString_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00002015 err = PyDict_SetItem(v, kv, item);
2016 Py_DECREF(kv);
2017 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002018}
2019
2020int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002021PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002022{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002023 PyObject *kv;
2024 int err;
2025 kv = PyString_FromString(key);
2026 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002027 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002028 err = PyDict_DelItem(v, kv);
2029 Py_DECREF(kv);
2030 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002031}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002032
Raymond Hettinger019a1482004-03-18 02:41:19 +00002033/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002034
2035typedef struct {
2036 PyObject_HEAD
Guido van Rossum2147df72002-07-16 20:30:22 +00002037 dictobject *di_dict; /* Set to NULL when iterator is exhausted */
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00002038 int di_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002039 int di_pos;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002040 PyObject* di_result; /* reusable result tuple for iteritems */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002041 long len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002042} dictiterobject;
2043
2044static PyObject *
Raymond Hettinger019a1482004-03-18 02:41:19 +00002045dictiter_new(dictobject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002046{
2047 dictiterobject *di;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002048 di = PyObject_New(dictiterobject, itertype);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002049 if (di == NULL)
2050 return NULL;
2051 Py_INCREF(dict);
2052 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00002053 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002054 di->di_pos = 0;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002055 di->len = dict->ma_used;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002056 if (itertype == &PyDictIterItem_Type) {
2057 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2058 if (di->di_result == NULL) {
2059 Py_DECREF(di);
2060 return NULL;
2061 }
2062 }
2063 else
2064 di->di_result = NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002065 return (PyObject *)di;
2066}
2067
2068static void
2069dictiter_dealloc(dictiterobject *di)
2070{
Guido van Rossum2147df72002-07-16 20:30:22 +00002071 Py_XDECREF(di->di_dict);
Raymond Hettinger019a1482004-03-18 02:41:19 +00002072 Py_XDECREF(di->di_result);
Neil Schemenauer6189b892002-04-12 02:43:00 +00002073 PyObject_Del(di);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002074}
2075
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002076static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002077dictiter_len(dictiterobject *di)
2078{
Martin v. Löwis18e16552006-02-15 17:27:45 +00002079 long len = 0;
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00002080 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002081 len = di->len;
2082 return PyInt_FromLong(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002083}
2084
Armin Rigof5b3e362006-02-11 21:32:43 +00002085PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002086
2087static PyMethodDef dictiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +00002088 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002089 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002090};
2091
Raymond Hettinger019a1482004-03-18 02:41:19 +00002092static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002093{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002094 PyObject *key;
2095 register int i, mask;
2096 register dictentry *ep;
2097 dictobject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002098
Raymond Hettinger019a1482004-03-18 02:41:19 +00002099 if (d == NULL)
Guido van Rossum2147df72002-07-16 20:30:22 +00002100 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002101 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002102
Raymond Hettinger019a1482004-03-18 02:41:19 +00002103 if (di->di_used != d->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00002104 PyErr_SetString(PyExc_RuntimeError,
2105 "dictionary changed size during iteration");
Guido van Rossum2147df72002-07-16 20:30:22 +00002106 di->di_used = -1; /* Make this state sticky */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002107 return NULL;
2108 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002109
Raymond Hettinger019a1482004-03-18 02:41:19 +00002110 i = di->di_pos;
2111 if (i < 0)
2112 goto fail;
2113 ep = d->ma_table;
2114 mask = d->ma_mask;
2115 while (i <= mask && ep[i].me_value == NULL)
2116 i++;
2117 di->di_pos = i+1;
2118 if (i > mask)
2119 goto fail;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002120 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002121 key = ep[i].me_key;
2122 Py_INCREF(key);
2123 return key;
2124
2125fail:
2126 Py_DECREF(d);
Guido van Rossum2147df72002-07-16 20:30:22 +00002127 di->di_dict = NULL;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002128 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002129}
2130
Raymond Hettinger019a1482004-03-18 02:41:19 +00002131PyTypeObject PyDictIterKey_Type = {
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002132 PyObject_HEAD_INIT(&PyType_Type)
2133 0, /* ob_size */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002134 "dictionary-keyiterator", /* tp_name */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002135 sizeof(dictiterobject), /* tp_basicsize */
2136 0, /* tp_itemsize */
2137 /* methods */
2138 (destructor)dictiter_dealloc, /* tp_dealloc */
2139 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002140 0, /* tp_getattr */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002141 0, /* tp_setattr */
2142 0, /* tp_compare */
2143 0, /* tp_repr */
2144 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002145 0, /* tp_as_sequence */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002146 0, /* tp_as_mapping */
2147 0, /* tp_hash */
2148 0, /* tp_call */
2149 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002150 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002151 0, /* tp_setattro */
2152 0, /* tp_as_buffer */
2153 Py_TPFLAGS_DEFAULT, /* tp_flags */
2154 0, /* tp_doc */
2155 0, /* tp_traverse */
2156 0, /* tp_clear */
2157 0, /* tp_richcompare */
2158 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002159 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002160 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002161 dictiter_methods, /* tp_methods */
2162 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002163};
2164
2165static PyObject *dictiter_iternextvalue(dictiterobject *di)
2166{
2167 PyObject *value;
2168 register int i, mask;
2169 register dictentry *ep;
2170 dictobject *d = di->di_dict;
2171
2172 if (d == NULL)
2173 return NULL;
2174 assert (PyDict_Check(d));
2175
2176 if (di->di_used != d->ma_used) {
2177 PyErr_SetString(PyExc_RuntimeError,
2178 "dictionary changed size during iteration");
2179 di->di_used = -1; /* Make this state sticky */
2180 return NULL;
2181 }
2182
2183 i = di->di_pos;
Guido van Rossum09240f62004-03-20 19:11:58 +00002184 mask = d->ma_mask;
2185 if (i < 0 || i > mask)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002186 goto fail;
2187 ep = d->ma_table;
Guido van Rossum09240f62004-03-20 19:11:58 +00002188 while ((value=ep[i].me_value) == NULL) {
Raymond Hettinger019a1482004-03-18 02:41:19 +00002189 i++;
Guido van Rossum09240f62004-03-20 19:11:58 +00002190 if (i > mask)
2191 goto fail;
2192 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002193 di->di_pos = i+1;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002194 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002195 Py_INCREF(value);
2196 return value;
2197
2198fail:
2199 Py_DECREF(d);
2200 di->di_dict = NULL;
2201 return NULL;
2202}
2203
2204PyTypeObject PyDictIterValue_Type = {
2205 PyObject_HEAD_INIT(&PyType_Type)
2206 0, /* ob_size */
2207 "dictionary-valueiterator", /* tp_name */
2208 sizeof(dictiterobject), /* tp_basicsize */
2209 0, /* tp_itemsize */
2210 /* methods */
2211 (destructor)dictiter_dealloc, /* tp_dealloc */
2212 0, /* tp_print */
2213 0, /* tp_getattr */
2214 0, /* tp_setattr */
2215 0, /* tp_compare */
2216 0, /* tp_repr */
2217 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002218 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002219 0, /* tp_as_mapping */
2220 0, /* tp_hash */
2221 0, /* tp_call */
2222 0, /* tp_str */
2223 PyObject_GenericGetAttr, /* tp_getattro */
2224 0, /* tp_setattro */
2225 0, /* tp_as_buffer */
2226 Py_TPFLAGS_DEFAULT, /* tp_flags */
2227 0, /* tp_doc */
2228 0, /* tp_traverse */
2229 0, /* tp_clear */
2230 0, /* tp_richcompare */
2231 0, /* tp_weaklistoffset */
2232 PyObject_SelfIter, /* tp_iter */
2233 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002234 dictiter_methods, /* tp_methods */
2235 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002236};
2237
2238static PyObject *dictiter_iternextitem(dictiterobject *di)
2239{
2240 PyObject *key, *value, *result = di->di_result;
2241 register int i, mask;
2242 register dictentry *ep;
2243 dictobject *d = di->di_dict;
2244
2245 if (d == NULL)
2246 return NULL;
2247 assert (PyDict_Check(d));
2248
2249 if (di->di_used != d->ma_used) {
2250 PyErr_SetString(PyExc_RuntimeError,
2251 "dictionary changed size during iteration");
2252 di->di_used = -1; /* Make this state sticky */
2253 return NULL;
2254 }
2255
2256 i = di->di_pos;
2257 if (i < 0)
2258 goto fail;
2259 ep = d->ma_table;
2260 mask = d->ma_mask;
2261 while (i <= mask && ep[i].me_value == NULL)
2262 i++;
2263 di->di_pos = i+1;
2264 if (i > mask)
2265 goto fail;
2266
2267 if (result->ob_refcnt == 1) {
2268 Py_INCREF(result);
2269 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2270 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2271 } else {
2272 result = PyTuple_New(2);
Tim Peters60b29962006-01-01 01:19:23 +00002273 if (result == NULL)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002274 return NULL;
2275 }
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002276 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002277 key = ep[i].me_key;
2278 value = ep[i].me_value;
2279 Py_INCREF(key);
2280 Py_INCREF(value);
2281 PyTuple_SET_ITEM(result, 0, key);
2282 PyTuple_SET_ITEM(result, 1, value);
2283 return result;
2284
2285fail:
2286 Py_DECREF(d);
2287 di->di_dict = NULL;
2288 return NULL;
2289}
2290
2291PyTypeObject PyDictIterItem_Type = {
2292 PyObject_HEAD_INIT(&PyType_Type)
2293 0, /* ob_size */
2294 "dictionary-itemiterator", /* tp_name */
2295 sizeof(dictiterobject), /* tp_basicsize */
2296 0, /* tp_itemsize */
2297 /* methods */
2298 (destructor)dictiter_dealloc, /* tp_dealloc */
2299 0, /* tp_print */
2300 0, /* tp_getattr */
2301 0, /* tp_setattr */
2302 0, /* tp_compare */
2303 0, /* tp_repr */
2304 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002305 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002306 0, /* tp_as_mapping */
2307 0, /* tp_hash */
2308 0, /* tp_call */
2309 0, /* tp_str */
2310 PyObject_GenericGetAttr, /* tp_getattro */
2311 0, /* tp_setattro */
2312 0, /* tp_as_buffer */
2313 Py_TPFLAGS_DEFAULT, /* tp_flags */
2314 0, /* tp_doc */
2315 0, /* tp_traverse */
2316 0, /* tp_clear */
2317 0, /* tp_richcompare */
2318 0, /* tp_weaklistoffset */
2319 PyObject_SelfIter, /* tp_iter */
2320 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002321 dictiter_methods, /* tp_methods */
2322 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002323};