blob: 0eccdbba9f00abe2d09861a831e3bf0a2168e062 [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
Fred Drake1bff34a2000-08-31 19:31:38 +0000118/* forward declarations */
119static dictentry *
120lookdict_string(dictobject *mp, PyObject *key, long hash);
121
122#ifdef SHOW_CONVERSION_COUNTS
123static long created = 0L;
124static long converted = 0L;
125
126static void
127show_counts(void)
128{
129 fprintf(stderr, "created %ld string dicts\n", created);
130 fprintf(stderr, "converted %ld to normal dicts\n", converted);
131 fprintf(stderr, "%.2f%% conversion rate\n", (100.0*converted)/created);
132}
133#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000134
Tim Peters6d6c1a32001-08-02 04:15:00 +0000135/* Initialization macros.
136 There are two ways to create a dict: PyDict_New() is the main C API
137 function, and the tp_new slot maps to dict_new(). In the latter case we
138 can save a little time over what PyDict_New does because it's guaranteed
139 that the PyDictObject struct is already zeroed out.
140 Everyone except dict_new() should use EMPTY_TO_MINSIZE (unless they have
141 an excellent reason not to).
142*/
143
144#define INIT_NONZERO_DICT_SLOTS(mp) do { \
Tim Petersdea48ec2001-05-22 20:40:22 +0000145 (mp)->ma_table = (mp)->ma_smalltable; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000146 (mp)->ma_mask = PyDict_MINSIZE - 1; \
147 } while(0)
148
149#define EMPTY_TO_MINSIZE(mp) do { \
150 memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000151 (mp)->ma_used = (mp)->ma_fill = 0; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000152 INIT_NONZERO_DICT_SLOTS(mp); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000153 } while(0)
154
Raymond Hettinger43442782004-03-17 21:55:03 +0000155/* Dictionary reuse scheme to save calls to malloc, free, and memset */
156#define MAXFREEDICTS 80
157static PyDictObject *free_dicts[MAXFREEDICTS];
158static int num_free_dicts = 0;
159
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000160PyObject *
Thomas Wouters78890102000-07-22 19:25:51 +0000161PyDict_New(void)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000162{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000163 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000164 if (dummy == NULL) { /* Auto-initialize dummy */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000165 dummy = PyString_FromString("<dummy key>");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000166 if (dummy == NULL)
167 return NULL;
Fred Drake1bff34a2000-08-31 19:31:38 +0000168#ifdef SHOW_CONVERSION_COUNTS
169 Py_AtExit(show_counts);
170#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000171 }
Raymond Hettinger43442782004-03-17 21:55:03 +0000172 if (num_free_dicts) {
173 mp = free_dicts[--num_free_dicts];
174 assert (mp != NULL);
175 assert (mp->ob_type == &PyDict_Type);
176 _Py_NewReference((PyObject *)mp);
177 if (mp->ma_fill) {
178 EMPTY_TO_MINSIZE(mp);
179 }
180 assert (mp->ma_used == 0);
181 assert (mp->ma_table == mp->ma_smalltable);
182 assert (mp->ma_mask == PyDict_MINSIZE - 1);
183 } else {
184 mp = PyObject_GC_New(dictobject, &PyDict_Type);
185 if (mp == NULL)
186 return NULL;
187 EMPTY_TO_MINSIZE(mp);
188 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000189 mp->ma_lookup = lookdict_string;
190#ifdef SHOW_CONVERSION_COUNTS
191 ++created;
192#endif
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000193 _PyObject_GC_TRACK(mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000194 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000195}
196
197/*
198The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000199This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000200Open addressing is preferred over chaining since the link overhead for
201chaining would be substantial (100% with typical malloc overhead).
202
Tim Peterseb28ef22001-06-02 05:27:19 +0000203The initial probe index is computed as hash mod the table size. Subsequent
204probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000205
206All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000207
Tim Peterseb28ef22001-06-02 05:27:19 +0000208(The details in this version are due to Tim Peters, building on many past
209contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
210Christian Tismer).
Fred Drake1bff34a2000-08-31 19:31:38 +0000211
212This function must never return NULL; failures are indicated by returning
213a dictentry* for which the me_value field is NULL. Exceptions are never
214reported by this function, and outstanding exceptions are maintained.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000215*/
Tim Peterseb28ef22001-06-02 05:27:19 +0000216
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000217static dictentry *
Tim Peters1f5871e2000-07-04 17:44:48 +0000218lookdict(dictobject *mp, PyObject *key, register long hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000219{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000220 register Py_ssize_t i;
221 register size_t perturb;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000222 register dictentry *freeslot;
Tim Petersafb6ae82001-06-04 21:00:21 +0000223 register unsigned int mask = mp->ma_mask;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000224 dictentry *ep0 = mp->ma_table;
225 register dictentry *ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000226 register int restore_error;
227 register int checked_error;
Fred Drakec88b99c2000-08-31 19:04:07 +0000228 register int cmp;
229 PyObject *err_type, *err_value, *err_tb;
Tim Peters453163d2001-06-03 04:54:32 +0000230 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000231
Tim Peters2f228e72001-05-13 00:19:31 +0000232 i = hash & mask;
Guido van Rossumefb46091997-01-29 15:53:56 +0000233 ep = &ep0[i];
Guido van Rossumc1c7b1a1998-10-06 16:01:14 +0000234 if (ep->me_key == NULL || ep->me_key == key)
Guido van Rossum7d181591997-01-16 21:06:45 +0000235 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000236
237 restore_error = checked_error = 0;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000238 if (ep->me_key == dummy)
Guido van Rossum7d181591997-01-16 21:06:45 +0000239 freeslot = ep;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000240 else {
Fred Drakec88b99c2000-08-31 19:04:07 +0000241 if (ep->me_hash == hash) {
242 /* error can't have been checked yet */
243 checked_error = 1;
244 if (PyErr_Occurred()) {
245 restore_error = 1;
246 PyErr_Fetch(&err_type, &err_value, &err_tb);
247 }
Tim Peters453163d2001-06-03 04:54:32 +0000248 startkey = ep->me_key;
249 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000250 if (cmp < 0)
Guido van Rossumb9324202001-01-18 00:39:02 +0000251 PyErr_Clear();
Tim Peters453163d2001-06-03 04:54:32 +0000252 if (ep0 == mp->ma_table && ep->me_key == startkey) {
253 if (cmp > 0)
254 goto Done;
255 }
256 else {
257 /* The compare did major nasty stuff to the
258 * dict: start over.
259 * XXX A clever adversary could prevent this
260 * XXX from terminating.
261 */
262 ep = lookdict(mp, key, hash);
263 goto Done;
264 }
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000265 }
266 freeslot = NULL;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000267 }
Tim Peters15d49292001-05-27 07:39:22 +0000268
Tim Peters342c65e2001-05-13 06:43:53 +0000269 /* In the loop, me_key == dummy is by far (factor of 100s) the
270 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000271 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
272 i = (i << 2) + i + perturb + 1;
273 ep = &ep0[i & mask];
Guido van Rossum16e93a81997-01-28 00:00:11 +0000274 if (ep->me_key == NULL) {
Tim Peters7b5d0af2001-06-03 04:14:43 +0000275 if (freeslot != NULL)
276 ep = freeslot;
277 break;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000278 }
Tim Peters7b5d0af2001-06-03 04:14:43 +0000279 if (ep->me_key == key)
280 break;
281 if (ep->me_hash == hash && ep->me_key != dummy) {
Fred Drakec88b99c2000-08-31 19:04:07 +0000282 if (!checked_error) {
283 checked_error = 1;
284 if (PyErr_Occurred()) {
285 restore_error = 1;
286 PyErr_Fetch(&err_type, &err_value,
287 &err_tb);
288 }
289 }
Tim Peters453163d2001-06-03 04:54:32 +0000290 startkey = ep->me_key;
291 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000292 if (cmp < 0)
Guido van Rossumb9324202001-01-18 00:39:02 +0000293 PyErr_Clear();
Tim Peters453163d2001-06-03 04:54:32 +0000294 if (ep0 == mp->ma_table && ep->me_key == startkey) {
295 if (cmp > 0)
296 break;
297 }
298 else {
299 /* The compare did major nasty stuff to the
300 * dict: start over.
301 * XXX A clever adversary could prevent this
302 * XXX from terminating.
303 */
304 ep = lookdict(mp, key, hash);
305 break;
306 }
Guido van Rossum16e93a81997-01-28 00:00:11 +0000307 }
Tim Peters342c65e2001-05-13 06:43:53 +0000308 else if (ep->me_key == dummy && freeslot == NULL)
309 freeslot = ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000310 }
Tim Peters7b5d0af2001-06-03 04:14:43 +0000311
312Done:
313 if (restore_error)
314 PyErr_Restore(err_type, err_value, err_tb);
315 return ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000316}
317
318/*
Fred Drake1bff34a2000-08-31 19:31:38 +0000319 * Hacked up version of lookdict which can assume keys are always strings;
320 * this assumption allows testing for errors during PyObject_Compare() to
321 * be dropped; string-string comparisons never raise exceptions. This also
322 * means we don't need to go through PyObject_Compare(); we can always use
Martin v. Löwiscd353062001-05-24 16:56:35 +0000323 * _PyString_Eq directly.
Fred Drake1bff34a2000-08-31 19:31:38 +0000324 *
Tim Peters0ab085c2001-09-14 00:25:33 +0000325 * This is valuable because the general-case error handling in lookdict() is
326 * expensive, and dicts with pure-string keys are very common.
Fred Drake1bff34a2000-08-31 19:31:38 +0000327 */
328static dictentry *
329lookdict_string(dictobject *mp, PyObject *key, register long hash)
330{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000331 register Py_ssize_t i;
332 register size_t perturb;
Fred Drake1bff34a2000-08-31 19:31:38 +0000333 register dictentry *freeslot;
Tim Petersafb6ae82001-06-04 21:00:21 +0000334 register unsigned int mask = mp->ma_mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000335 dictentry *ep0 = mp->ma_table;
336 register dictentry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000337
Tim Peters0ab085c2001-09-14 00:25:33 +0000338 /* Make sure this function doesn't have to handle non-string keys,
339 including subclasses of str; e.g., one reason to subclass
340 strings is to override __eq__, and for speed we don't cater to
341 that here. */
342 if (!PyString_CheckExact(key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000343#ifdef SHOW_CONVERSION_COUNTS
344 ++converted;
345#endif
346 mp->ma_lookup = lookdict;
347 return lookdict(mp, key, hash);
348 }
Tim Peters2f228e72001-05-13 00:19:31 +0000349 i = hash & mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000350 ep = &ep0[i];
351 if (ep->me_key == NULL || ep->me_key == key)
352 return ep;
353 if (ep->me_key == dummy)
354 freeslot = ep;
355 else {
356 if (ep->me_hash == hash
Martin v. Löwiscd353062001-05-24 16:56:35 +0000357 && _PyString_Eq(ep->me_key, key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000358 return ep;
359 }
360 freeslot = NULL;
361 }
Tim Peters15d49292001-05-27 07:39:22 +0000362
Tim Peters342c65e2001-05-13 06:43:53 +0000363 /* In the loop, me_key == dummy is by far (factor of 100s) the
364 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000365 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
366 i = (i << 2) + i + perturb + 1;
367 ep = &ep0[i & mask];
Tim Peters342c65e2001-05-13 06:43:53 +0000368 if (ep->me_key == NULL)
369 return freeslot == NULL ? ep : freeslot;
370 if (ep->me_key == key
371 || (ep->me_hash == hash
372 && ep->me_key != dummy
Martin v. Löwiscd353062001-05-24 16:56:35 +0000373 && _PyString_Eq(ep->me_key, key)))
Fred Drake1bff34a2000-08-31 19:31:38 +0000374 return ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000375 if (ep->me_key == dummy && freeslot == NULL)
Tim Peters342c65e2001-05-13 06:43:53 +0000376 freeslot = ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000377 }
378}
379
380/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000381Internal routine to insert a new item into the table.
382Used both by the internal resize routine and by the public insert routine.
383Eats a reference to key and one to value.
384*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000385static void
Tim Peters1f5871e2000-07-04 17:44:48 +0000386insertdict(register dictobject *mp, PyObject *key, long hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000387{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000388 PyObject *old_value;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000389 register dictentry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000390 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
391
392 assert(mp->ma_lookup != NULL);
393 ep = mp->ma_lookup(mp, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000394 if (ep->me_value != NULL) {
Guido van Rossumd7047b31995-01-02 19:07:15 +0000395 old_value = ep->me_value;
396 ep->me_value = value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000397 Py_DECREF(old_value); /* which **CAN** re-enter */
398 Py_DECREF(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000399 }
400 else {
401 if (ep->me_key == NULL)
402 mp->ma_fill++;
Raymond Hettinger07ead172005-02-05 23:42:57 +0000403 else {
404 assert(ep->me_key == dummy);
405 Py_DECREF(dummy);
406 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000407 ep->me_key = key;
408 ep->me_hash = hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000409 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000410 mp->ma_used++;
411 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000412}
413
414/*
415Restructure the table by allocating a new table and reinserting all
416items again. When entries have been deleted, the new table may
417actually be smaller than the old one.
418*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000419static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000420dictresize(dictobject *mp, int minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000421{
Tim Peterseb28ef22001-06-02 05:27:19 +0000422 int newsize;
Tim Peters0c6010b2001-05-23 23:33:57 +0000423 dictentry *oldtable, *newtable, *ep;
424 int i;
425 int is_oldtable_malloced;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000426 dictentry small_copy[PyDict_MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000427
428 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000429
Tim Peterseb28ef22001-06-02 05:27:19 +0000430 /* Find the smallest table size > minused. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000431 for (newsize = PyDict_MINSIZE;
Tim Peterseb28ef22001-06-02 05:27:19 +0000432 newsize <= minused && newsize > 0;
433 newsize <<= 1)
434 ;
435 if (newsize <= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000436 PyErr_NoMemory();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000437 return -1;
438 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000439
440 /* Get space for a new table. */
441 oldtable = mp->ma_table;
442 assert(oldtable != NULL);
443 is_oldtable_malloced = oldtable != mp->ma_smalltable;
444
Tim Peters6d6c1a32001-08-02 04:15:00 +0000445 if (newsize == PyDict_MINSIZE) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000446 /* A large table is shrinking, or we can't get any smaller. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000447 newtable = mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000448 if (newtable == oldtable) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000449 if (mp->ma_fill == mp->ma_used) {
450 /* No dummies, so no point doing anything. */
Tim Peters0c6010b2001-05-23 23:33:57 +0000451 return 0;
Tim Petersf8a548c2001-05-24 16:26:40 +0000452 }
453 /* We're not going to resize it, but rebuild the
454 table anyway to purge old dummy entries.
455 Subtle: This is *necessary* if fill==size,
456 as lookdict needs at least one virgin slot to
457 terminate failing searches. If fill < size, it's
458 merely desirable, as dummies slow searches. */
459 assert(mp->ma_fill > mp->ma_used);
Tim Peters0c6010b2001-05-23 23:33:57 +0000460 memcpy(small_copy, oldtable, sizeof(small_copy));
461 oldtable = small_copy;
462 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000463 }
464 else {
465 newtable = PyMem_NEW(dictentry, newsize);
466 if (newtable == NULL) {
467 PyErr_NoMemory();
468 return -1;
469 }
470 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000471
472 /* Make the dict empty, using the new table. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000473 assert(newtable != oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000474 mp->ma_table = newtable;
Tim Petersafb6ae82001-06-04 21:00:21 +0000475 mp->ma_mask = newsize - 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000476 memset(newtable, 0, sizeof(dictentry) * newsize);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000477 mp->ma_used = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000478 i = mp->ma_fill;
479 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000480
Tim Peters19283142001-05-17 22:25:34 +0000481 /* Copy the data over; this is refcount-neutral for active entries;
482 dummy entries aren't copied over, of course */
Tim Petersdea48ec2001-05-22 20:40:22 +0000483 for (ep = oldtable; i > 0; ep++) {
484 if (ep->me_value != NULL) { /* active entry */
485 --i;
Tim Peters19283142001-05-17 22:25:34 +0000486 insertdict(mp, ep->me_key, ep->me_hash, ep->me_value);
Tim Petersdea48ec2001-05-22 20:40:22 +0000487 }
Tim Peters19283142001-05-17 22:25:34 +0000488 else if (ep->me_key != NULL) { /* dummy entry */
Tim Petersdea48ec2001-05-22 20:40:22 +0000489 --i;
Tim Peters19283142001-05-17 22:25:34 +0000490 assert(ep->me_key == dummy);
491 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000492 }
Tim Peters19283142001-05-17 22:25:34 +0000493 /* else key == value == NULL: nothing to do */
Guido van Rossumd7047b31995-01-02 19:07:15 +0000494 }
495
Tim Peters0c6010b2001-05-23 23:33:57 +0000496 if (is_oldtable_malloced)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000497 PyMem_DEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000498 return 0;
499}
500
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000501PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000502PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000503{
504 long hash;
Fred Drake1bff34a2000-08-31 19:31:38 +0000505 dictobject *mp = (dictobject *)op;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000506 if (!PyDict_Check(op)) {
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000507 return NULL;
508 }
Tim Peters0ab085c2001-09-14 00:25:33 +0000509 if (!PyString_CheckExact(key) ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000510 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Guido van Rossumca756f21997-01-23 19:39:29 +0000511 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000512 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000513 if (hash == -1) {
514 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000515 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000516 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000517 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000518 return (mp->ma_lookup)(mp, key, hash)->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000519}
520
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000521/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
Tim Peters60b29962006-01-01 01:19:23 +0000522 * dictionary if it's merely replacing the value for an existing key.
523 * This means that it's safe to loop over a dictionary with PyDict_Next()
524 * and occasionally replace a value -- but you can't insert new keys or
525 * remove them.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000526 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000527int
Tim Peters1f5871e2000-07-04 17:44:48 +0000528PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000529{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000530 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000531 register long hash;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000532 register int n_used;
533
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000534 if (!PyDict_Check(op)) {
535 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000536 return -1;
537 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000538 mp = (dictobject *)op;
Tim Peters0ab085c2001-09-14 00:25:33 +0000539 if (PyString_CheckExact(key)) {
Guido van Rossum45ec02a2002-08-19 21:43:18 +0000540 hash = ((PyStringObject *)key)->ob_shash;
541 if (hash == -1)
542 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000543 }
Tim Peters1f7df352002-03-29 03:29:08 +0000544 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000545 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000546 if (hash == -1)
547 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000548 }
Tim Petersafb6ae82001-06-04 21:00:21 +0000549 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000550 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000551 Py_INCREF(value);
552 Py_INCREF(key);
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000553 insertdict(mp, key, hash, value);
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000554 /* If we added a key, we can safely resize. Otherwise just return!
555 * If fill >= 2/3 size, adjust size. Normally, this doubles or
556 * quaduples the size, but it's also possible for the dict to shrink
Tim Peters60b29962006-01-01 01:19:23 +0000557 * (if ma_fill is much larger than ma_used, meaning a lot of dict
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000558 * keys have been * deleted).
Tim Peters60b29962006-01-01 01:19:23 +0000559 *
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000560 * Quadrupling the size improves average dictionary sparseness
561 * (reducing collisions) at the cost of some memory and iteration
562 * speed (which loops over every possible entry). It also halves
563 * the number of expensive resize operations in a growing dictionary.
Tim Peters60b29962006-01-01 01:19:23 +0000564 *
565 * Very large dictionaries (over 50K items) use doubling instead.
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000566 * This may help applications with severe memory constraints.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000567 */
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000568 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
569 return 0;
Raymond Hettingerf81e4502005-08-17 02:19:36 +0000570 return dictresize(mp, (mp->ma_used>50000 ? mp->ma_used*2 : mp->ma_used*4));
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000571}
572
573int
Tim Peters1f5871e2000-07-04 17:44:48 +0000574PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000575{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000576 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000577 register long hash;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000578 register dictentry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000579 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000580
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000581 if (!PyDict_Check(op)) {
582 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000583 return -1;
584 }
Tim Peters0ab085c2001-09-14 00:25:33 +0000585 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +0000586 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000587 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000588 if (hash == -1)
589 return -1;
590 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000591 mp = (dictobject *)op;
Fred Drake1bff34a2000-08-31 19:31:38 +0000592 ep = (mp->ma_lookup)(mp, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000593 if (ep->me_value == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000594 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000595 return -1;
596 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000597 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000598 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000599 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000600 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000601 ep->me_value = NULL;
602 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000603 Py_DECREF(old_value);
604 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000605 return 0;
606}
607
Guido van Rossum25831651993-05-19 14:50:45 +0000608void
Tim Peters1f5871e2000-07-04 17:44:48 +0000609PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000610{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000611 dictobject *mp;
Tim Petersdea48ec2001-05-22 20:40:22 +0000612 dictentry *ep, *table;
613 int table_is_malloced;
614 int fill;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000615 dictentry small_copy[PyDict_MINSIZE];
Tim Petersdea48ec2001-05-22 20:40:22 +0000616#ifdef Py_DEBUG
617 int i, n;
618#endif
619
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000620 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000621 return;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000622 mp = (dictobject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000623#ifdef Py_DEBUG
Tim Petersafb6ae82001-06-04 21:00:21 +0000624 n = mp->ma_mask + 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000625 i = 0;
626#endif
627
628 table = mp->ma_table;
629 assert(table != NULL);
630 table_is_malloced = table != mp->ma_smalltable;
631
632 /* This is delicate. During the process of clearing the dict,
633 * decrefs can cause the dict to mutate. To avoid fatal confusion
634 * (voice of experience), we have to make the dict empty before
635 * clearing the slots, and never refer to anything via mp->xxx while
636 * clearing.
637 */
638 fill = mp->ma_fill;
639 if (table_is_malloced)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000640 EMPTY_TO_MINSIZE(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000641
642 else if (fill > 0) {
643 /* It's a small table with something that needs to be cleared.
644 * Afraid the only safe way is to copy the dict entries into
645 * another small table first.
646 */
647 memcpy(small_copy, table, sizeof(small_copy));
648 table = small_copy;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000649 EMPTY_TO_MINSIZE(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000650 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000651 /* else it's a small table that's already empty */
652
653 /* Now we can finally clear things. If C had refcounts, we could
654 * assert that the refcount on table is 1 now, i.e. that this function
655 * has unique access to it, so decref side-effects can't alter it.
656 */
657 for (ep = table; fill > 0; ++ep) {
658#ifdef Py_DEBUG
659 assert(i < n);
660 ++i;
661#endif
662 if (ep->me_key) {
663 --fill;
664 Py_DECREF(ep->me_key);
665 Py_XDECREF(ep->me_value);
666 }
667#ifdef Py_DEBUG
668 else
669 assert(ep->me_value == NULL);
670#endif
671 }
672
673 if (table_is_malloced)
674 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000675}
676
Tim Peters080c88b2003-02-15 03:01:11 +0000677/*
678 * Iterate over a dict. Use like so:
679 *
680 * int i;
681 * PyObject *key, *value;
682 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +0000683 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +0000684 * Refer to borrowed references in key and value.
685 * }
686 *
687 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +0000688 * mutates the dict. One exception: it is safe if the loop merely changes
689 * the values associated with the keys (but doesn't insert new keys or
690 * delete keys), via PyDict_SetItem().
691 */
Guido van Rossum25831651993-05-19 14:50:45 +0000692int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000693PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000694{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000695 register Py_ssize_t i;
696 register int mask;
Raymond Hettinger43442782004-03-17 21:55:03 +0000697 register dictentry *ep;
698
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000699 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000700 return 0;
Guido van Rossum25831651993-05-19 14:50:45 +0000701 i = *ppos;
702 if (i < 0)
703 return 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000704 ep = ((dictobject *)op)->ma_table;
705 mask = ((dictobject *)op)->ma_mask;
706 while (i <= mask && ep[i].me_value == NULL)
Guido van Rossum25831651993-05-19 14:50:45 +0000707 i++;
708 *ppos = i+1;
Raymond Hettinger43442782004-03-17 21:55:03 +0000709 if (i > mask)
Guido van Rossum25831651993-05-19 14:50:45 +0000710 return 0;
711 if (pkey)
Raymond Hettinger43442782004-03-17 21:55:03 +0000712 *pkey = ep[i].me_key;
Guido van Rossum25831651993-05-19 14:50:45 +0000713 if (pvalue)
Raymond Hettinger43442782004-03-17 21:55:03 +0000714 *pvalue = ep[i].me_value;
Guido van Rossum25831651993-05-19 14:50:45 +0000715 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000716}
717
718/* Methods */
719
720static void
Tim Peters1f5871e2000-07-04 17:44:48 +0000721dict_dealloc(register dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000722{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000723 register dictentry *ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000724 int fill = mp->ma_fill;
Guido van Rossumff413af2002-03-28 20:34:59 +0000725 PyObject_GC_UnTrack(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000726 Py_TRASHCAN_SAFE_BEGIN(mp)
Tim Petersdea48ec2001-05-22 20:40:22 +0000727 for (ep = mp->ma_table; fill > 0; ep++) {
728 if (ep->me_key) {
729 --fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000730 Py_DECREF(ep->me_key);
Tim Petersdea48ec2001-05-22 20:40:22 +0000731 Py_XDECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000732 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000733 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000734 if (mp->ma_table != mp->ma_smalltable)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000735 PyMem_DEL(mp->ma_table);
Raymond Hettinger43442782004-03-17 21:55:03 +0000736 if (num_free_dicts < MAXFREEDICTS && mp->ob_type == &PyDict_Type)
737 free_dicts[num_free_dicts++] = mp;
Tim Peters60b29962006-01-01 01:19:23 +0000738 else
Raymond Hettinger43442782004-03-17 21:55:03 +0000739 mp->ob_type->tp_free((PyObject *)mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000740 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000741}
742
743static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000744dict_print(register dictobject *mp, register FILE *fp, register int flags)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000745{
746 register int i;
747 register int any;
Guido van Rossum255443b1998-04-10 22:47:14 +0000748
749 i = Py_ReprEnter((PyObject*)mp);
750 if (i != 0) {
751 if (i < 0)
752 return i;
753 fprintf(fp, "{...}");
754 return 0;
755 }
756
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000757 fprintf(fp, "{");
758 any = 0;
Tim Petersafb6ae82001-06-04 21:00:21 +0000759 for (i = 0; i <= mp->ma_mask; i++) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000760 dictentry *ep = mp->ma_table + i;
761 PyObject *pvalue = ep->me_value;
762 if (pvalue != NULL) {
763 /* Prevent PyObject_Repr from deleting value during
764 key format */
765 Py_INCREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000766 if (any++ > 0)
767 fprintf(fp, ", ");
Guido van Rossum255443b1998-04-10 22:47:14 +0000768 if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000769 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000770 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000771 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000772 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000773 fprintf(fp, ": ");
Tim Peters19b77cf2001-06-02 08:27:39 +0000774 if (PyObject_Print(pvalue, fp, 0) != 0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000775 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000776 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000777 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000778 }
Tim Peters23cf6be2001-06-02 08:02:56 +0000779 Py_DECREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000780 }
781 }
782 fprintf(fp, "}");
Guido van Rossum255443b1998-04-10 22:47:14 +0000783 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000784 return 0;
785}
786
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000787static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000788dict_repr(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000789{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000790 Py_ssize_t i;
Tim Petersa7259592001-06-16 05:11:17 +0000791 PyObject *s, *temp, *colon = NULL;
792 PyObject *pieces = NULL, *result = NULL;
793 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +0000794
Tim Petersa7259592001-06-16 05:11:17 +0000795 i = Py_ReprEnter((PyObject *)mp);
Guido van Rossum255443b1998-04-10 22:47:14 +0000796 if (i != 0) {
Tim Petersa7259592001-06-16 05:11:17 +0000797 return i > 0 ? PyString_FromString("{...}") : NULL;
Guido van Rossum255443b1998-04-10 22:47:14 +0000798 }
799
Tim Petersa7259592001-06-16 05:11:17 +0000800 if (mp->ma_used == 0) {
801 result = PyString_FromString("{}");
802 goto Done;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000803 }
Tim Petersa7259592001-06-16 05:11:17 +0000804
805 pieces = PyList_New(0);
806 if (pieces == NULL)
807 goto Done;
808
809 colon = PyString_FromString(": ");
810 if (colon == NULL)
811 goto Done;
812
813 /* Do repr() on each key+value pair, and insert ": " between them.
814 Note that repr may mutate the dict. */
Tim Petersc6057842001-06-16 07:52:53 +0000815 i = 0;
816 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Tim Petersa7259592001-06-16 05:11:17 +0000817 int status;
818 /* Prevent repr from deleting value during key format. */
819 Py_INCREF(value);
820 s = PyObject_Repr(key);
821 PyString_Concat(&s, colon);
822 PyString_ConcatAndDel(&s, PyObject_Repr(value));
823 Py_DECREF(value);
824 if (s == NULL)
825 goto Done;
826 status = PyList_Append(pieces, s);
827 Py_DECREF(s); /* append created a new ref */
828 if (status < 0)
829 goto Done;
830 }
831
832 /* Add "{}" decorations to the first and last items. */
833 assert(PyList_GET_SIZE(pieces) > 0);
834 s = PyString_FromString("{");
835 if (s == NULL)
836 goto Done;
837 temp = PyList_GET_ITEM(pieces, 0);
838 PyString_ConcatAndDel(&s, temp);
839 PyList_SET_ITEM(pieces, 0, s);
840 if (s == NULL)
841 goto Done;
842
843 s = PyString_FromString("}");
844 if (s == NULL)
845 goto Done;
846 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
847 PyString_ConcatAndDel(&temp, s);
848 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
849 if (temp == NULL)
850 goto Done;
851
852 /* Paste them all together with ", " between. */
853 s = PyString_FromString(", ");
854 if (s == NULL)
855 goto Done;
856 result = _PyString_Join(s, pieces);
Tim Peters0ab085c2001-09-14 00:25:33 +0000857 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +0000858
859Done:
860 Py_XDECREF(pieces);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000861 Py_XDECREF(colon);
Tim Petersa7259592001-06-16 05:11:17 +0000862 Py_ReprLeave((PyObject *)mp);
863 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000864}
865
Martin v. Löwis18e16552006-02-15 17:27:45 +0000866static Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +0000867dict_length(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000868{
869 return mp->ma_used;
870}
871
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000872static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000873dict_subscript(dictobject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000874{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000875 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000876 long hash;
Tim Petersdea48ec2001-05-22 20:40:22 +0000877 assert(mp->ma_table != NULL);
Tim Peters0ab085c2001-09-14 00:25:33 +0000878 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +0000879 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000880 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000881 if (hash == -1)
882 return NULL;
883 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000884 v = (mp->ma_lookup)(mp, key, hash) -> me_value;
Guido van Rossum1968ad32006-02-25 22:38:04 +0000885 if (v == NULL) {
886 if (!PyDict_CheckExact(mp)) {
887 /* Look up __missing__ method if we're a subclass. */
Guido van Rossum4b92a822006-02-25 23:32:30 +0000888 PyObject *missing;
Guido van Rossum1968ad32006-02-25 22:38:04 +0000889 static PyObject *missing_str = NULL;
890 if (missing_str == NULL)
891 missing_str =
892 PyString_InternFromString("__missing__");
Guido van Rossum4b92a822006-02-25 23:32:30 +0000893 missing = _PyType_Lookup(mp->ob_type, missing_str);
Guido van Rossum1968ad32006-02-25 22:38:04 +0000894 if (missing != NULL)
895 return PyObject_CallFunctionObjArgs(missing,
896 (PyObject *)mp, key, NULL);
897 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000898 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum1968ad32006-02-25 22:38:04 +0000899 return NULL;
900 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000901 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000902 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000903 return v;
904}
905
906static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000907dict_ass_sub(dictobject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000908{
909 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000910 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000911 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000912 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000913}
914
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000915static PyMappingMethods dict_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000916 (lenfunc)dict_length, /*mp_length*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000917 (binaryfunc)dict_subscript, /*mp_subscript*/
918 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000919};
920
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000921static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000922dict_keys(register dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000923{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000924 register PyObject *v;
Raymond Hettinger43442782004-03-17 21:55:03 +0000925 register int i, j;
926 dictentry *ep;
927 int mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000928
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000929 again:
930 n = mp->ma_used;
931 v = PyList_New(n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000932 if (v == NULL)
933 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000934 if (n != mp->ma_used) {
935 /* Durnit. The allocations caused the dict to resize.
936 * Just start over, this shouldn't normally happen.
937 */
938 Py_DECREF(v);
939 goto again;
940 }
Raymond Hettinger43442782004-03-17 21:55:03 +0000941 ep = mp->ma_table;
942 mask = mp->ma_mask;
943 for (i = 0, j = 0; i <= mask; i++) {
944 if (ep[i].me_value != NULL) {
945 PyObject *key = ep[i].me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000946 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000947 PyList_SET_ITEM(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000948 j++;
949 }
950 }
Raymond Hettinger43442782004-03-17 21:55:03 +0000951 assert(j == n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000952 return v;
953}
954
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000955static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000956dict_values(register dictobject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +0000957{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000958 register PyObject *v;
Raymond Hettinger43442782004-03-17 21:55:03 +0000959 register int i, j;
960 dictentry *ep;
961 int mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000962
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000963 again:
964 n = mp->ma_used;
965 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +0000966 if (v == NULL)
967 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000968 if (n != mp->ma_used) {
969 /* Durnit. The allocations caused the dict to resize.
970 * Just start over, this shouldn't normally happen.
971 */
972 Py_DECREF(v);
973 goto again;
974 }
Raymond Hettinger43442782004-03-17 21:55:03 +0000975 ep = mp->ma_table;
976 mask = mp->ma_mask;
977 for (i = 0, j = 0; i <= mask; i++) {
978 if (ep[i].me_value != NULL) {
979 PyObject *value = ep[i].me_value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000980 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000981 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +0000982 j++;
983 }
984 }
Raymond Hettinger43442782004-03-17 21:55:03 +0000985 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +0000986 return v;
987}
988
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000989static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000990dict_items(register dictobject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +0000991{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000992 register PyObject *v;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000993 register int i, j, n;
Raymond Hettinger43442782004-03-17 21:55:03 +0000994 int mask;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000995 PyObject *item, *key, *value;
Raymond Hettinger43442782004-03-17 21:55:03 +0000996 dictentry *ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000997
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000998 /* Preallocate the list of tuples, to avoid allocations during
999 * the loop over the items, which could trigger GC, which
1000 * could resize the dict. :-(
1001 */
1002 again:
1003 n = mp->ma_used;
1004 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001005 if (v == NULL)
1006 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001007 for (i = 0; i < n; i++) {
1008 item = PyTuple_New(2);
1009 if (item == NULL) {
1010 Py_DECREF(v);
1011 return NULL;
1012 }
1013 PyList_SET_ITEM(v, i, item);
1014 }
1015 if (n != mp->ma_used) {
1016 /* Durnit. The allocations caused the dict to resize.
1017 * Just start over, this shouldn't normally happen.
1018 */
1019 Py_DECREF(v);
1020 goto again;
1021 }
1022 /* Nothing we do below makes any function calls. */
Raymond Hettinger43442782004-03-17 21:55:03 +00001023 ep = mp->ma_table;
1024 mask = mp->ma_mask;
1025 for (i = 0, j = 0; i <= mask; i++) {
Raymond Hettinger06905122004-03-19 10:30:00 +00001026 if ((value=ep[i].me_value) != NULL) {
Raymond Hettinger43442782004-03-17 21:55:03 +00001027 key = ep[i].me_key;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001028 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001029 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001030 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001031 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001032 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001033 j++;
1034 }
1035 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001036 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001037 return v;
1038}
1039
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001040static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001041dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001042{
1043 PyObject *seq;
1044 PyObject *value = Py_None;
1045 PyObject *it; /* iter(seq) */
1046 PyObject *key;
1047 PyObject *d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001048 int status;
1049
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001050 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001051 return NULL;
1052
1053 d = PyObject_CallObject(cls, NULL);
1054 if (d == NULL)
1055 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001056
1057 it = PyObject_GetIter(seq);
1058 if (it == NULL){
1059 Py_DECREF(d);
1060 return NULL;
1061 }
1062
1063 for (;;) {
1064 key = PyIter_Next(it);
1065 if (key == NULL) {
1066 if (PyErr_Occurred())
1067 goto Fail;
1068 break;
1069 }
Raymond Hettingere03e5b12002-12-07 08:10:51 +00001070 status = PyObject_SetItem(d, key, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001071 Py_DECREF(key);
1072 if (status < 0)
1073 goto Fail;
1074 }
1075
1076 Py_DECREF(it);
1077 return d;
1078
1079Fail:
1080 Py_DECREF(it);
1081 Py_DECREF(d);
1082 return NULL;
1083}
1084
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001085static int
1086dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001087{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001088 PyObject *arg = NULL;
1089 int result = 0;
1090
1091 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1092 result = -1;
1093
1094 else if (arg != NULL) {
1095 if (PyObject_HasAttrString(arg, "keys"))
1096 result = PyDict_Merge(self, arg, 1);
1097 else
1098 result = PyDict_MergeFromSeq2(self, arg, 1);
1099 }
1100 if (result == 0 && kwds != NULL)
1101 result = PyDict_Merge(self, kwds, 1);
1102 return result;
1103}
1104
1105static PyObject *
1106dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1107{
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001108 if (dict_update_common(self, args, kwds, "update") != -1)
1109 Py_RETURN_NONE;
1110 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001111}
1112
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001113/* Update unconditionally replaces existing items.
1114 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001115 otherwise it leaves existing items unchanged.
1116
1117 PyDict_{Update,Merge} update/merge from a mapping object.
1118
Tim Petersf582b822001-12-11 18:51:08 +00001119 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001120 producing iterable objects of length 2.
1121*/
1122
Tim Petersf582b822001-12-11 18:51:08 +00001123int
Tim Peters1fc240e2001-10-26 05:06:50 +00001124PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1125{
1126 PyObject *it; /* iter(seq2) */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001127 int i; /* index into seq2 of current element */
Tim Peters1fc240e2001-10-26 05:06:50 +00001128 PyObject *item; /* seq2[i] */
1129 PyObject *fast; /* item as a 2-tuple or 2-list */
1130
1131 assert(d != NULL);
1132 assert(PyDict_Check(d));
1133 assert(seq2 != NULL);
1134
1135 it = PyObject_GetIter(seq2);
1136 if (it == NULL)
1137 return -1;
1138
1139 for (i = 0; ; ++i) {
1140 PyObject *key, *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001141 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001142
1143 fast = NULL;
1144 item = PyIter_Next(it);
1145 if (item == NULL) {
1146 if (PyErr_Occurred())
1147 goto Fail;
1148 break;
1149 }
1150
1151 /* Convert item to sequence, and verify length 2. */
1152 fast = PySequence_Fast(item, "");
1153 if (fast == NULL) {
1154 if (PyErr_ExceptionMatches(PyExc_TypeError))
1155 PyErr_Format(PyExc_TypeError,
1156 "cannot convert dictionary update "
1157 "sequence element #%d to a sequence",
1158 i);
1159 goto Fail;
1160 }
1161 n = PySequence_Fast_GET_SIZE(fast);
1162 if (n != 2) {
1163 PyErr_Format(PyExc_ValueError,
1164 "dictionary update sequence element #%d "
Martin v. Löwis2c95cc62006-02-16 06:54:25 +00001165 "has length %zd; 2 is required",
Martin v. Löwise0e89f72006-02-16 06:59:22 +00001166 i, n);
Tim Peters1fc240e2001-10-26 05:06:50 +00001167 goto Fail;
1168 }
1169
1170 /* Update/merge with this (key, value) pair. */
1171 key = PySequence_Fast_GET_ITEM(fast, 0);
1172 value = PySequence_Fast_GET_ITEM(fast, 1);
1173 if (override || PyDict_GetItem(d, key) == NULL) {
1174 int status = PyDict_SetItem(d, key, value);
1175 if (status < 0)
1176 goto Fail;
1177 }
1178 Py_DECREF(fast);
1179 Py_DECREF(item);
1180 }
1181
1182 i = 0;
1183 goto Return;
1184Fail:
1185 Py_XDECREF(item);
1186 Py_XDECREF(fast);
1187 i = -1;
1188Return:
1189 Py_DECREF(it);
1190 return i;
1191}
1192
Tim Peters6d6c1a32001-08-02 04:15:00 +00001193int
1194PyDict_Update(PyObject *a, PyObject *b)
1195{
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001196 return PyDict_Merge(a, b, 1);
1197}
1198
1199int
1200PyDict_Merge(PyObject *a, PyObject *b, int override)
1201{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001202 register PyDictObject *mp, *other;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001203 register int i;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001204 dictentry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001205
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001206 /* We accept for the argument either a concrete dictionary object,
1207 * or an abstract "mapping" object. For the former, we can do
1208 * things quite efficiently. For the latter, we only require that
1209 * PyMapping_Keys() and PyObject_GetItem() be supported.
1210 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001211 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1212 PyErr_BadInternalCall();
1213 return -1;
1214 }
1215 mp = (dictobject*)a;
1216 if (PyDict_Check(b)) {
1217 other = (dictobject*)b;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001218 if (other == mp || other->ma_used == 0)
1219 /* a.update(a) or a.update({}); nothing to do */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001220 return 0;
Raymond Hettinger186e7392005-05-14 18:08:25 +00001221 if (mp->ma_used == 0)
1222 /* Since the target dict is empty, PyDict_GetItem()
1223 * always returns NULL. Setting override to 1
1224 * skips the unnecessary test.
1225 */
1226 override = 1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001227 /* Do one big resize at the start, rather than
1228 * incrementally resizing as we insert new items. Expect
1229 * that there will be no (or few) overlapping keys.
1230 */
1231 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
Raymond Hettingerc8d22902003-05-07 00:49:40 +00001232 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001233 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001234 }
1235 for (i = 0; i <= other->ma_mask; i++) {
1236 entry = &other->ma_table[i];
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001237 if (entry->me_value != NULL &&
1238 (override ||
1239 PyDict_GetItem(a, entry->me_key) == NULL)) {
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001240 Py_INCREF(entry->me_key);
1241 Py_INCREF(entry->me_value);
1242 insertdict(mp, entry->me_key, entry->me_hash,
1243 entry->me_value);
1244 }
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001245 }
1246 }
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001247 else {
1248 /* Do it the generic, slower way */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001249 PyObject *keys = PyMapping_Keys(b);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001250 PyObject *iter;
1251 PyObject *key, *value;
1252 int status;
1253
1254 if (keys == NULL)
1255 /* Docstring says this is equivalent to E.keys() so
1256 * if E doesn't have a .keys() method we want
1257 * AttributeError to percolate up. Might as well
1258 * do the same for any other error.
1259 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001260 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001261
1262 iter = PyObject_GetIter(keys);
1263 Py_DECREF(keys);
1264 if (iter == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001265 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001266
1267 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001268 if (!override && PyDict_GetItem(a, key) != NULL) {
1269 Py_DECREF(key);
1270 continue;
1271 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001272 value = PyObject_GetItem(b, key);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001273 if (value == NULL) {
1274 Py_DECREF(iter);
1275 Py_DECREF(key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001276 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001277 }
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001278 status = PyDict_SetItem(a, key, value);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001279 Py_DECREF(key);
1280 Py_DECREF(value);
1281 if (status < 0) {
1282 Py_DECREF(iter);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001283 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001284 }
1285 }
1286 Py_DECREF(iter);
1287 if (PyErr_Occurred())
1288 /* Iterator completed, via error */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001289 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001290 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001291 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001292}
1293
1294static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001295dict_copy(register dictobject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001296{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001297 return PyDict_Copy((PyObject*)mp);
1298}
1299
1300PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001301PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001302{
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001303 PyObject *copy;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001304
1305 if (o == NULL || !PyDict_Check(o)) {
1306 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001307 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001308 }
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001309 copy = PyDict_New();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001310 if (copy == NULL)
1311 return NULL;
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001312 if (PyDict_Merge(copy, o, 1) == 0)
1313 return copy;
1314 Py_DECREF(copy);
Raymond Hettinger1356f782005-04-15 15:58:42 +00001315 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001316}
1317
Martin v. Löwis18e16552006-02-15 17:27:45 +00001318Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00001319PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001320{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001321 if (mp == NULL || !PyDict_Check(mp)) {
1322 PyErr_BadInternalCall();
Jeremy Hylton7083bb72004-02-17 20:10:11 +00001323 return -1;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001324 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001325 return ((dictobject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001326}
1327
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001328PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001329PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001330{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001331 if (mp == NULL || !PyDict_Check(mp)) {
1332 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001333 return NULL;
1334 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001335 return dict_keys((dictobject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001336}
1337
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001338PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001339PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001340{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001341 if (mp == NULL || !PyDict_Check(mp)) {
1342 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001343 return NULL;
1344 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001345 return dict_values((dictobject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001346}
1347
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001348PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001349PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001350{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001351 if (mp == NULL || !PyDict_Check(mp)) {
1352 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001353 return NULL;
1354 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001355 return dict_items((dictobject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001356}
1357
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001358/* Subroutine which returns the smallest key in a for which b's value
1359 is different or absent. The value is returned too, through the
Tim Peters95bf9392001-05-10 08:32:44 +00001360 pval argument. Both are NULL if no key in a is found for which b's status
1361 differs. The refcounts on (and only on) non-NULL *pval and function return
1362 values must be decremented by the caller (characterize() increments them
1363 to ensure that mutating comparison and PyDict_GetItem calls can't delete
1364 them before the caller is done looking at them). */
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001365
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001366static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001367characterize(dictobject *a, dictobject *b, PyObject **pval)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001368{
Tim Peters95bf9392001-05-10 08:32:44 +00001369 PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */
1370 PyObject *aval = NULL; /* a[akey] */
Guido van Rossumb9324202001-01-18 00:39:02 +00001371 int i, cmp;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001372
Tim Petersafb6ae82001-06-04 21:00:21 +00001373 for (i = 0; i <= a->ma_mask; i++) {
Tim Peters95bf9392001-05-10 08:32:44 +00001374 PyObject *thiskey, *thisaval, *thisbval;
1375 if (a->ma_table[i].me_value == NULL)
1376 continue;
1377 thiskey = a->ma_table[i].me_key;
1378 Py_INCREF(thiskey); /* keep alive across compares */
1379 if (akey != NULL) {
1380 cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT);
1381 if (cmp < 0) {
1382 Py_DECREF(thiskey);
1383 goto Fail;
1384 }
1385 if (cmp > 0 ||
Tim Petersafb6ae82001-06-04 21:00:21 +00001386 i > a->ma_mask ||
Tim Peters95bf9392001-05-10 08:32:44 +00001387 a->ma_table[i].me_value == NULL)
1388 {
1389 /* Not the *smallest* a key; or maybe it is
1390 * but the compare shrunk the dict so we can't
1391 * find its associated value anymore; or
1392 * maybe it is but the compare deleted the
1393 * a[thiskey] entry.
1394 */
1395 Py_DECREF(thiskey);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001396 continue;
Guido van Rossumb9324202001-01-18 00:39:02 +00001397 }
Tim Peters95bf9392001-05-10 08:32:44 +00001398 }
1399
1400 /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */
1401 thisaval = a->ma_table[i].me_value;
1402 assert(thisaval);
1403 Py_INCREF(thisaval); /* keep alive */
1404 thisbval = PyDict_GetItem((PyObject *)b, thiskey);
1405 if (thisbval == NULL)
1406 cmp = 0;
1407 else {
1408 /* both dicts have thiskey: same values? */
1409 cmp = PyObject_RichCompareBool(
1410 thisaval, thisbval, Py_EQ);
1411 if (cmp < 0) {
1412 Py_DECREF(thiskey);
1413 Py_DECREF(thisaval);
1414 goto Fail;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001415 }
1416 }
Tim Peters95bf9392001-05-10 08:32:44 +00001417 if (cmp == 0) {
1418 /* New winner. */
1419 Py_XDECREF(akey);
1420 Py_XDECREF(aval);
1421 akey = thiskey;
1422 aval = thisaval;
1423 }
1424 else {
1425 Py_DECREF(thiskey);
1426 Py_DECREF(thisaval);
1427 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001428 }
Tim Peters95bf9392001-05-10 08:32:44 +00001429 *pval = aval;
1430 return akey;
1431
1432Fail:
1433 Py_XDECREF(akey);
1434 Py_XDECREF(aval);
1435 *pval = NULL;
1436 return NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001437}
1438
1439static int
Tim Peters1f5871e2000-07-04 17:44:48 +00001440dict_compare(dictobject *a, dictobject *b)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001441{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001442 PyObject *adiff, *bdiff, *aval, *bval;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001443 int res;
1444
1445 /* Compare lengths first */
1446 if (a->ma_used < b->ma_used)
1447 return -1; /* a is shorter */
1448 else if (a->ma_used > b->ma_used)
1449 return 1; /* b is shorter */
Tim Peters95bf9392001-05-10 08:32:44 +00001450
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001451 /* Same length -- check all keys */
Tim Peters95bf9392001-05-10 08:32:44 +00001452 bdiff = bval = NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001453 adiff = characterize(a, b, &aval);
Tim Peters95bf9392001-05-10 08:32:44 +00001454 if (adiff == NULL) {
1455 assert(!aval);
Tim Peters3918fb22001-05-10 18:58:31 +00001456 /* Either an error, or a is a subset with the same length so
Tim Peters95bf9392001-05-10 08:32:44 +00001457 * must be equal.
1458 */
1459 res = PyErr_Occurred() ? -1 : 0;
1460 goto Finished;
1461 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001462 bdiff = characterize(b, a, &bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001463 if (bdiff == NULL && PyErr_Occurred()) {
1464 assert(!bval);
1465 res = -1;
1466 goto Finished;
1467 }
1468 res = 0;
1469 if (bdiff) {
1470 /* bdiff == NULL "should be" impossible now, but perhaps
1471 * the last comparison done by the characterize() on a had
1472 * the side effect of making the dicts equal!
1473 */
1474 res = PyObject_Compare(adiff, bdiff);
1475 }
1476 if (res == 0 && bval != NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001477 res = PyObject_Compare(aval, bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001478
1479Finished:
1480 Py_XDECREF(adiff);
1481 Py_XDECREF(bdiff);
1482 Py_XDECREF(aval);
1483 Py_XDECREF(bval);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001484 return res;
1485}
1486
Tim Peterse63415e2001-05-08 04:38:29 +00001487/* Return 1 if dicts equal, 0 if not, -1 if error.
1488 * Gets out as soon as any difference is detected.
1489 * Uses only Py_EQ comparison.
1490 */
1491static int
1492dict_equal(dictobject *a, dictobject *b)
1493{
1494 int i;
1495
1496 if (a->ma_used != b->ma_used)
1497 /* can't be equal if # of entries differ */
1498 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001499
Tim Peterse63415e2001-05-08 04:38:29 +00001500 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Tim Petersafb6ae82001-06-04 21:00:21 +00001501 for (i = 0; i <= a->ma_mask; i++) {
Tim Peterse63415e2001-05-08 04:38:29 +00001502 PyObject *aval = a->ma_table[i].me_value;
1503 if (aval != NULL) {
1504 int cmp;
1505 PyObject *bval;
1506 PyObject *key = a->ma_table[i].me_key;
1507 /* temporarily bump aval's refcount to ensure it stays
1508 alive until we're done with it */
1509 Py_INCREF(aval);
1510 bval = PyDict_GetItem((PyObject *)b, key);
1511 if (bval == NULL) {
1512 Py_DECREF(aval);
1513 return 0;
1514 }
1515 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1516 Py_DECREF(aval);
1517 if (cmp <= 0) /* error or not equal */
1518 return cmp;
1519 }
1520 }
1521 return 1;
1522 }
1523
1524static PyObject *
1525dict_richcompare(PyObject *v, PyObject *w, int op)
1526{
1527 int cmp;
1528 PyObject *res;
1529
1530 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1531 res = Py_NotImplemented;
1532 }
1533 else if (op == Py_EQ || op == Py_NE) {
1534 cmp = dict_equal((dictobject *)v, (dictobject *)w);
1535 if (cmp < 0)
1536 return NULL;
1537 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1538 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001539 else
1540 res = Py_NotImplemented;
Tim Peterse63415e2001-05-08 04:38:29 +00001541 Py_INCREF(res);
1542 return res;
1543 }
1544
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001545static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001546dict_has_key(register dictobject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001547{
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001548 long hash;
1549 register long ok;
Tim Peters0ab085c2001-09-14 00:25:33 +00001550 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001551 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001552 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001553 if (hash == -1)
1554 return NULL;
1555 }
Tim Petersdea48ec2001-05-22 20:40:22 +00001556 ok = (mp->ma_lookup)(mp, key, hash)->me_value != NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00001557 return PyBool_FromLong(ok);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001558}
1559
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001560static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001561dict_get(register dictobject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001562{
1563 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001564 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001565 PyObject *val = NULL;
1566 long hash;
1567
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001568 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001569 return NULL;
1570
Tim Peters0ab085c2001-09-14 00:25:33 +00001571 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001572 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Barry Warsawc38c5da1997-10-06 17:49:20 +00001573 hash = PyObject_Hash(key);
1574 if (hash == -1)
1575 return NULL;
1576 }
Fred Drake1bff34a2000-08-31 19:31:38 +00001577 val = (mp->ma_lookup)(mp, key, hash)->me_value;
Barry Warsaw320ac331997-10-20 17:26:25 +00001578
Barry Warsawc38c5da1997-10-06 17:49:20 +00001579 if (val == NULL)
1580 val = failobj;
1581 Py_INCREF(val);
1582 return val;
1583}
1584
1585
1586static PyObject *
Guido van Rossum164452c2000-08-08 16:12:54 +00001587dict_setdefault(register dictobject *mp, PyObject *args)
1588{
1589 PyObject *key;
1590 PyObject *failobj = Py_None;
1591 PyObject *val = NULL;
1592 long hash;
1593
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001594 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
Guido van Rossum164452c2000-08-08 16:12:54 +00001595 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001596
Tim Peters0ab085c2001-09-14 00:25:33 +00001597 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001598 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossum164452c2000-08-08 16:12:54 +00001599 hash = PyObject_Hash(key);
1600 if (hash == -1)
1601 return NULL;
1602 }
Fred Drake1bff34a2000-08-31 19:31:38 +00001603 val = (mp->ma_lookup)(mp, key, hash)->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001604 if (val == NULL) {
1605 val = failobj;
1606 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1607 val = NULL;
1608 }
1609 Py_XINCREF(val);
1610 return val;
1611}
1612
1613
1614static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001615dict_clear(register dictobject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001616{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001617 PyDict_Clear((PyObject *)mp);
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001618 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001619}
1620
Guido van Rossumba6ab842000-12-12 22:02:18 +00001621static PyObject *
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001622dict_pop(dictobject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00001623{
1624 long hash;
1625 dictentry *ep;
1626 PyObject *old_value, *old_key;
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001627 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001628
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001629 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1630 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001631 if (mp->ma_used == 0) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001632 if (deflt) {
1633 Py_INCREF(deflt);
1634 return deflt;
1635 }
Guido van Rossume027d982002-04-12 15:11:59 +00001636 PyErr_SetString(PyExc_KeyError,
1637 "pop(): dictionary is empty");
1638 return NULL;
1639 }
1640 if (!PyString_CheckExact(key) ||
1641 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1642 hash = PyObject_Hash(key);
1643 if (hash == -1)
1644 return NULL;
1645 }
1646 ep = (mp->ma_lookup)(mp, key, hash);
1647 if (ep->me_value == NULL) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001648 if (deflt) {
1649 Py_INCREF(deflt);
1650 return deflt;
1651 }
Guido van Rossume027d982002-04-12 15:11:59 +00001652 PyErr_SetObject(PyExc_KeyError, key);
1653 return NULL;
1654 }
1655 old_key = ep->me_key;
1656 Py_INCREF(dummy);
1657 ep->me_key = dummy;
1658 old_value = ep->me_value;
1659 ep->me_value = NULL;
1660 mp->ma_used--;
1661 Py_DECREF(old_key);
1662 return old_value;
1663}
1664
1665static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001666dict_popitem(dictobject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001667{
1668 int i = 0;
1669 dictentry *ep;
1670 PyObject *res;
1671
Tim Petersf4b33f62001-06-02 05:42:29 +00001672 /* Allocate the result tuple before checking the size. Believe it
1673 * or not, this allocation could trigger a garbage collection which
1674 * could empty the dict, so if we checked the size first and that
1675 * happened, the result would be an infinite loop (searching for an
1676 * entry that no longer exists). Note that the usual popitem()
1677 * idiom is "while d: k, v = d.popitem()". so needing to throw the
1678 * tuple away if the dict *is* empty isn't a significant
1679 * inefficiency -- possible, but unlikely in practice.
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001680 */
1681 res = PyTuple_New(2);
1682 if (res == NULL)
1683 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001684 if (mp->ma_used == 0) {
1685 Py_DECREF(res);
1686 PyErr_SetString(PyExc_KeyError,
1687 "popitem(): dictionary is empty");
1688 return NULL;
1689 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001690 /* Set ep to "the first" dict entry with a value. We abuse the hash
1691 * field of slot 0 to hold a search finger:
1692 * If slot 0 has a value, use slot 0.
1693 * Else slot 0 is being used to hold a search finger,
1694 * and we use its hash value as the first index to look.
1695 */
1696 ep = &mp->ma_table[0];
1697 if (ep->me_value == NULL) {
1698 i = (int)ep->me_hash;
Tim Petersdea48ec2001-05-22 20:40:22 +00001699 /* The hash field may be a real hash value, or it may be a
1700 * legit search finger, or it may be a once-legit search
1701 * finger that's out of bounds now because it wrapped around
1702 * or the table shrunk -- simply make sure it's in bounds now.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001703 */
Tim Petersafb6ae82001-06-04 21:00:21 +00001704 if (i > mp->ma_mask || i < 1)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001705 i = 1; /* skip slot 0 */
1706 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1707 i++;
Tim Petersafb6ae82001-06-04 21:00:21 +00001708 if (i > mp->ma_mask)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001709 i = 1;
1710 }
1711 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001712 PyTuple_SET_ITEM(res, 0, ep->me_key);
1713 PyTuple_SET_ITEM(res, 1, ep->me_value);
1714 Py_INCREF(dummy);
1715 ep->me_key = dummy;
1716 ep->me_value = NULL;
1717 mp->ma_used--;
1718 assert(mp->ma_table[0].me_value == NULL);
1719 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001720 return res;
1721}
1722
Jeremy Hylton8caad492000-06-23 14:18:11 +00001723static int
1724dict_traverse(PyObject *op, visitproc visit, void *arg)
1725{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001726 Py_ssize_t i = 0;
1727 int err;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001728 PyObject *pk;
1729 PyObject *pv;
1730
1731 while (PyDict_Next(op, &i, &pk, &pv)) {
1732 err = visit(pk, arg);
1733 if (err)
1734 return err;
1735 err = visit(pv, arg);
1736 if (err)
1737 return err;
1738 }
1739 return 0;
1740}
1741
1742static int
1743dict_tp_clear(PyObject *op)
1744{
1745 PyDict_Clear(op);
1746 return 0;
1747}
1748
Tim Petersf7f88b12000-12-13 23:18:45 +00001749
Raymond Hettinger019a1482004-03-18 02:41:19 +00001750extern PyTypeObject PyDictIterKey_Type; /* Forward */
1751extern PyTypeObject PyDictIterValue_Type; /* Forward */
1752extern PyTypeObject PyDictIterItem_Type; /* Forward */
1753static PyObject *dictiter_new(dictobject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001754
1755static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001756dict_iterkeys(dictobject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001757{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001758 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001759}
1760
1761static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001762dict_itervalues(dictobject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001763{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001764 return dictiter_new(dict, &PyDictIterValue_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001765}
1766
1767static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001768dict_iteritems(dictobject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001769{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001770 return dictiter_new(dict, &PyDictIterItem_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001771}
1772
1773
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001774PyDoc_STRVAR(has_key__doc__,
Raymond Hettinger574aa322003-09-01 22:12:08 +00001775"D.has_key(k) -> True if D has a key k, else False");
Tim Petersf7f88b12000-12-13 23:18:45 +00001776
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001777PyDoc_STRVAR(contains__doc__,
1778"D.__contains__(k) -> True if D has a key k, else False");
1779
1780PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
1781
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001782PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001783"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001784
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001785PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001786"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 +00001787
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001788PyDoc_STRVAR(pop__doc__,
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001789"D.pop(k[,d]) -> v, remove specified key and return the corresponding value\n\
1790If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00001791
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001792PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00001793"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000017942-tuple; but raise KeyError if D is empty");
Tim Petersf7f88b12000-12-13 23:18:45 +00001795
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001796PyDoc_STRVAR(keys__doc__,
1797"D.keys() -> list of D's keys");
Tim Petersf7f88b12000-12-13 23:18:45 +00001798
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001799PyDoc_STRVAR(items__doc__,
1800"D.items() -> list of D's (key, value) pairs, as 2-tuples");
Tim Petersf7f88b12000-12-13 23:18:45 +00001801
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001802PyDoc_STRVAR(values__doc__,
1803"D.values() -> list of D's values");
Tim Petersf7f88b12000-12-13 23:18:45 +00001804
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001805PyDoc_STRVAR(update__doc__,
Walter Dörwaldd70ad8a2004-05-28 20:59:21 +00001806"D.update(E, **F) -> None. Update D from E and F: for k in E: D[k] = E[k]\n\
1807(if E has keys else: for (k, v) in E: D[k] = v) then: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00001808
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001809PyDoc_STRVAR(fromkeys__doc__,
1810"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
1811v defaults to None.");
1812
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001813PyDoc_STRVAR(clear__doc__,
1814"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001815
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001816PyDoc_STRVAR(copy__doc__,
1817"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00001818
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001819PyDoc_STRVAR(iterkeys__doc__,
1820"D.iterkeys() -> an iterator over the keys of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00001821
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001822PyDoc_STRVAR(itervalues__doc__,
1823"D.itervalues() -> an iterator over the values of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00001824
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001825PyDoc_STRVAR(iteritems__doc__,
1826"D.iteritems() -> an iterator over the (key, value) items of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00001827
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001828static PyMethodDef mapp_methods[] = {
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001829 {"__contains__",(PyCFunction)dict_has_key, METH_O | METH_COEXIST,
1830 contains__doc__},
Raymond Hettinger0c669672003-12-13 13:31:55 +00001831 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001832 getitem__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001833 {"has_key", (PyCFunction)dict_has_key, METH_O,
Tim Petersf7f88b12000-12-13 23:18:45 +00001834 has_key__doc__},
1835 {"get", (PyCFunction)dict_get, METH_VARARGS,
1836 get__doc__},
1837 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
1838 setdefault_doc__},
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001839 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
Just van Rossuma797d812002-11-23 09:45:04 +00001840 pop__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001841 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001842 popitem__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001843 {"keys", (PyCFunction)dict_keys, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001844 keys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001845 {"items", (PyCFunction)dict_items, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001846 items__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001847 {"values", (PyCFunction)dict_values, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001848 values__doc__},
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001849 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001850 update__doc__},
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001851 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
1852 fromkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001853 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001854 clear__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001855 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001856 copy__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001857 {"iterkeys", (PyCFunction)dict_iterkeys, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00001858 iterkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001859 {"itervalues", (PyCFunction)dict_itervalues, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00001860 itervalues__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001861 {"iteritems", (PyCFunction)dict_iteritems, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00001862 iteritems__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00001863 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001864};
1865
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001866int
1867PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001868{
1869 long hash;
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001870 dictobject *mp = (dictobject *)op;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001871
Tim Peters0ab085c2001-09-14 00:25:33 +00001872 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001873 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001874 hash = PyObject_Hash(key);
1875 if (hash == -1)
1876 return -1;
1877 }
Tim Petersdea48ec2001-05-22 20:40:22 +00001878 return (mp->ma_lookup)(mp, key, hash)->me_value != NULL;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001879}
1880
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001881/* Hack to implement "key in dict" */
1882static PySequenceMethods dict_as_sequence = {
1883 0, /* sq_length */
1884 0, /* sq_concat */
1885 0, /* sq_repeat */
1886 0, /* sq_item */
1887 0, /* sq_slice */
1888 0, /* sq_ass_item */
1889 0, /* sq_ass_slice */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001890 (objobjproc)PyDict_Contains, /* sq_contains */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001891 0, /* sq_inplace_concat */
1892 0, /* sq_inplace_repeat */
1893};
1894
Guido van Rossum09e563a2001-05-01 12:10:21 +00001895static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001896dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1897{
1898 PyObject *self;
1899
1900 assert(type != NULL && type->tp_alloc != NULL);
1901 self = type->tp_alloc(type, 0);
1902 if (self != NULL) {
1903 PyDictObject *d = (PyDictObject *)self;
1904 /* It's guaranteed that tp->alloc zeroed out the struct. */
1905 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
1906 INIT_NONZERO_DICT_SLOTS(d);
1907 d->ma_lookup = lookdict_string;
1908#ifdef SHOW_CONVERSION_COUNTS
1909 ++created;
1910#endif
1911 }
1912 return self;
1913}
1914
Tim Peters25786c02001-09-02 08:22:48 +00001915static int
1916dict_init(PyObject *self, PyObject *args, PyObject *kwds)
1917{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001918 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00001919}
1920
Guido van Rossumdbb53d92001-12-03 16:32:18 +00001921static long
1922dict_nohash(PyObject *self)
1923{
1924 PyErr_SetString(PyExc_TypeError, "dict objects are unhashable");
1925 return -1;
1926}
1927
Tim Peters6d6c1a32001-08-02 04:15:00 +00001928static PyObject *
Guido van Rossum09e563a2001-05-01 12:10:21 +00001929dict_iter(dictobject *dict)
1930{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001931 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001932}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001933
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001934PyDoc_STRVAR(dictionary_doc,
Tim Petersa427a2b2001-10-29 22:25:45 +00001935"dict() -> new empty dictionary.\n"
1936"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Tim Peters1fc240e2001-10-26 05:06:50 +00001937" (key, value) pairs.\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00001938"dict(seq) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00001939" d = {}\n"
1940" for k, v in seq:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00001941" d[k] = v\n"
1942"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
1943" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00001944
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001945PyTypeObject PyDict_Type = {
1946 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001947 0,
Tim Petersa427a2b2001-10-29 22:25:45 +00001948 "dict",
Neil Schemenauere83c00e2001-08-29 23:54:21 +00001949 sizeof(dictobject),
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001950 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00001951 (destructor)dict_dealloc, /* tp_dealloc */
1952 (printfunc)dict_print, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001953 0, /* tp_getattr */
Guido van Rossumb9324202001-01-18 00:39:02 +00001954 0, /* tp_setattr */
Tim Peters4fa58bf2001-05-10 21:45:19 +00001955 (cmpfunc)dict_compare, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00001956 (reprfunc)dict_repr, /* tp_repr */
1957 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001958 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00001959 &dict_as_mapping, /* tp_as_mapping */
Guido van Rossumdbb53d92001-12-03 16:32:18 +00001960 dict_nohash, /* tp_hash */
Guido van Rossumb9324202001-01-18 00:39:02 +00001961 0, /* tp_call */
1962 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001963 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumb9324202001-01-18 00:39:02 +00001964 0, /* tp_setattro */
1965 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00001966 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Tim Peters6d6c1a32001-08-02 04:15:00 +00001967 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters25786c02001-09-02 08:22:48 +00001968 dictionary_doc, /* tp_doc */
Guido van Rossumb9324202001-01-18 00:39:02 +00001969 (traverseproc)dict_traverse, /* tp_traverse */
1970 (inquiry)dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00001971 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001972 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00001973 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001974 0, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001975 mapp_methods, /* tp_methods */
1976 0, /* tp_members */
1977 0, /* tp_getset */
1978 0, /* tp_base */
1979 0, /* tp_dict */
1980 0, /* tp_descr_get */
1981 0, /* tp_descr_set */
1982 0, /* tp_dictoffset */
Tim Peters25786c02001-09-02 08:22:48 +00001983 (initproc)dict_init, /* tp_init */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001984 PyType_GenericAlloc, /* tp_alloc */
1985 dict_new, /* tp_new */
Neil Schemenauer6189b892002-04-12 02:43:00 +00001986 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001987};
1988
Guido van Rossum3cca2451997-05-16 14:23:33 +00001989/* For backward compatibility with old dictionary interface */
1990
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001991PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00001992PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001993{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001994 PyObject *kv, *rv;
1995 kv = PyString_FromString(key);
1996 if (kv == NULL)
1997 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00001998 rv = PyDict_GetItem(v, kv);
1999 Py_DECREF(kv);
2000 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002001}
2002
2003int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002004PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002005{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002006 PyObject *kv;
2007 int err;
2008 kv = PyString_FromString(key);
2009 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002010 return -1;
Guido van Rossum4f3bf1e1997-09-29 23:31:11 +00002011 PyString_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00002012 err = PyDict_SetItem(v, kv, item);
2013 Py_DECREF(kv);
2014 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002015}
2016
2017int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002018PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002019{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002020 PyObject *kv;
2021 int err;
2022 kv = PyString_FromString(key);
2023 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002024 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002025 err = PyDict_DelItem(v, kv);
2026 Py_DECREF(kv);
2027 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002028}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002029
Raymond Hettinger019a1482004-03-18 02:41:19 +00002030/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002031
2032typedef struct {
2033 PyObject_HEAD
Guido van Rossum2147df72002-07-16 20:30:22 +00002034 dictobject *di_dict; /* Set to NULL when iterator is exhausted */
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00002035 int di_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002036 int di_pos;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002037 PyObject* di_result; /* reusable result tuple for iteritems */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002038 long len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002039} dictiterobject;
2040
2041static PyObject *
Raymond Hettinger019a1482004-03-18 02:41:19 +00002042dictiter_new(dictobject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002043{
2044 dictiterobject *di;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002045 di = PyObject_New(dictiterobject, itertype);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002046 if (di == NULL)
2047 return NULL;
2048 Py_INCREF(dict);
2049 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00002050 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002051 di->di_pos = 0;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002052 di->len = dict->ma_used;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002053 if (itertype == &PyDictIterItem_Type) {
2054 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2055 if (di->di_result == NULL) {
2056 Py_DECREF(di);
2057 return NULL;
2058 }
2059 }
2060 else
2061 di->di_result = NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002062 return (PyObject *)di;
2063}
2064
2065static void
2066dictiter_dealloc(dictiterobject *di)
2067{
Guido van Rossum2147df72002-07-16 20:30:22 +00002068 Py_XDECREF(di->di_dict);
Raymond Hettinger019a1482004-03-18 02:41:19 +00002069 Py_XDECREF(di->di_result);
Neil Schemenauer6189b892002-04-12 02:43:00 +00002070 PyObject_Del(di);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002071}
2072
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002073static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002074dictiter_len(dictiterobject *di)
2075{
Martin v. Löwis18e16552006-02-15 17:27:45 +00002076 long len = 0;
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00002077 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002078 len = di->len;
2079 return PyInt_FromLong(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002080}
2081
Armin Rigof5b3e362006-02-11 21:32:43 +00002082PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002083
2084static PyMethodDef dictiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +00002085 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002086 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002087};
2088
Raymond Hettinger019a1482004-03-18 02:41:19 +00002089static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002090{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002091 PyObject *key;
2092 register int i, mask;
2093 register dictentry *ep;
2094 dictobject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002095
Raymond Hettinger019a1482004-03-18 02:41:19 +00002096 if (d == NULL)
Guido van Rossum2147df72002-07-16 20:30:22 +00002097 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002098 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002099
Raymond Hettinger019a1482004-03-18 02:41:19 +00002100 if (di->di_used != d->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00002101 PyErr_SetString(PyExc_RuntimeError,
2102 "dictionary changed size during iteration");
Guido van Rossum2147df72002-07-16 20:30:22 +00002103 di->di_used = -1; /* Make this state sticky */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002104 return NULL;
2105 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002106
Raymond Hettinger019a1482004-03-18 02:41:19 +00002107 i = di->di_pos;
2108 if (i < 0)
2109 goto fail;
2110 ep = d->ma_table;
2111 mask = d->ma_mask;
2112 while (i <= mask && ep[i].me_value == NULL)
2113 i++;
2114 di->di_pos = i+1;
2115 if (i > mask)
2116 goto fail;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002117 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002118 key = ep[i].me_key;
2119 Py_INCREF(key);
2120 return key;
2121
2122fail:
2123 Py_DECREF(d);
Guido van Rossum2147df72002-07-16 20:30:22 +00002124 di->di_dict = NULL;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002125 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002126}
2127
Raymond Hettinger019a1482004-03-18 02:41:19 +00002128PyTypeObject PyDictIterKey_Type = {
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002129 PyObject_HEAD_INIT(&PyType_Type)
2130 0, /* ob_size */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002131 "dictionary-keyiterator", /* tp_name */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002132 sizeof(dictiterobject), /* tp_basicsize */
2133 0, /* tp_itemsize */
2134 /* methods */
2135 (destructor)dictiter_dealloc, /* tp_dealloc */
2136 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002137 0, /* tp_getattr */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002138 0, /* tp_setattr */
2139 0, /* tp_compare */
2140 0, /* tp_repr */
2141 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002142 0, /* tp_as_sequence */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002143 0, /* tp_as_mapping */
2144 0, /* tp_hash */
2145 0, /* tp_call */
2146 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002147 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002148 0, /* tp_setattro */
2149 0, /* tp_as_buffer */
2150 Py_TPFLAGS_DEFAULT, /* tp_flags */
2151 0, /* tp_doc */
2152 0, /* tp_traverse */
2153 0, /* tp_clear */
2154 0, /* tp_richcompare */
2155 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002156 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002157 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002158 dictiter_methods, /* tp_methods */
2159 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002160};
2161
2162static PyObject *dictiter_iternextvalue(dictiterobject *di)
2163{
2164 PyObject *value;
2165 register int i, mask;
2166 register dictentry *ep;
2167 dictobject *d = di->di_dict;
2168
2169 if (d == NULL)
2170 return NULL;
2171 assert (PyDict_Check(d));
2172
2173 if (di->di_used != d->ma_used) {
2174 PyErr_SetString(PyExc_RuntimeError,
2175 "dictionary changed size during iteration");
2176 di->di_used = -1; /* Make this state sticky */
2177 return NULL;
2178 }
2179
2180 i = di->di_pos;
Guido van Rossum09240f62004-03-20 19:11:58 +00002181 mask = d->ma_mask;
2182 if (i < 0 || i > mask)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002183 goto fail;
2184 ep = d->ma_table;
Guido van Rossum09240f62004-03-20 19:11:58 +00002185 while ((value=ep[i].me_value) == NULL) {
Raymond Hettinger019a1482004-03-18 02:41:19 +00002186 i++;
Guido van Rossum09240f62004-03-20 19:11:58 +00002187 if (i > mask)
2188 goto fail;
2189 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002190 di->di_pos = i+1;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002191 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002192 Py_INCREF(value);
2193 return value;
2194
2195fail:
2196 Py_DECREF(d);
2197 di->di_dict = NULL;
2198 return NULL;
2199}
2200
2201PyTypeObject PyDictIterValue_Type = {
2202 PyObject_HEAD_INIT(&PyType_Type)
2203 0, /* ob_size */
2204 "dictionary-valueiterator", /* tp_name */
2205 sizeof(dictiterobject), /* tp_basicsize */
2206 0, /* tp_itemsize */
2207 /* methods */
2208 (destructor)dictiter_dealloc, /* tp_dealloc */
2209 0, /* tp_print */
2210 0, /* tp_getattr */
2211 0, /* tp_setattr */
2212 0, /* tp_compare */
2213 0, /* tp_repr */
2214 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002215 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002216 0, /* tp_as_mapping */
2217 0, /* tp_hash */
2218 0, /* tp_call */
2219 0, /* tp_str */
2220 PyObject_GenericGetAttr, /* tp_getattro */
2221 0, /* tp_setattro */
2222 0, /* tp_as_buffer */
2223 Py_TPFLAGS_DEFAULT, /* tp_flags */
2224 0, /* tp_doc */
2225 0, /* tp_traverse */
2226 0, /* tp_clear */
2227 0, /* tp_richcompare */
2228 0, /* tp_weaklistoffset */
2229 PyObject_SelfIter, /* tp_iter */
2230 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002231 dictiter_methods, /* tp_methods */
2232 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002233};
2234
2235static PyObject *dictiter_iternextitem(dictiterobject *di)
2236{
2237 PyObject *key, *value, *result = di->di_result;
2238 register int i, mask;
2239 register dictentry *ep;
2240 dictobject *d = di->di_dict;
2241
2242 if (d == NULL)
2243 return NULL;
2244 assert (PyDict_Check(d));
2245
2246 if (di->di_used != d->ma_used) {
2247 PyErr_SetString(PyExc_RuntimeError,
2248 "dictionary changed size during iteration");
2249 di->di_used = -1; /* Make this state sticky */
2250 return NULL;
2251 }
2252
2253 i = di->di_pos;
2254 if (i < 0)
2255 goto fail;
2256 ep = d->ma_table;
2257 mask = d->ma_mask;
2258 while (i <= mask && ep[i].me_value == NULL)
2259 i++;
2260 di->di_pos = i+1;
2261 if (i > mask)
2262 goto fail;
2263
2264 if (result->ob_refcnt == 1) {
2265 Py_INCREF(result);
2266 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2267 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2268 } else {
2269 result = PyTuple_New(2);
Tim Peters60b29962006-01-01 01:19:23 +00002270 if (result == NULL)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002271 return NULL;
2272 }
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002273 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002274 key = ep[i].me_key;
2275 value = ep[i].me_value;
2276 Py_INCREF(key);
2277 Py_INCREF(value);
2278 PyTuple_SET_ITEM(result, 0, key);
2279 PyTuple_SET_ITEM(result, 1, value);
2280 return result;
2281
2282fail:
2283 Py_DECREF(d);
2284 di->di_dict = NULL;
2285 return NULL;
2286}
2287
2288PyTypeObject PyDictIterItem_Type = {
2289 PyObject_HEAD_INIT(&PyType_Type)
2290 0, /* ob_size */
2291 "dictionary-itemiterator", /* tp_name */
2292 sizeof(dictiterobject), /* tp_basicsize */
2293 0, /* tp_itemsize */
2294 /* methods */
2295 (destructor)dictiter_dealloc, /* tp_dealloc */
2296 0, /* tp_print */
2297 0, /* tp_getattr */
2298 0, /* tp_setattr */
2299 0, /* tp_compare */
2300 0, /* tp_repr */
2301 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002302 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002303 0, /* tp_as_mapping */
2304 0, /* tp_hash */
2305 0, /* tp_call */
2306 0, /* tp_str */
2307 PyObject_GenericGetAttr, /* tp_getattro */
2308 0, /* tp_setattro */
2309 0, /* tp_as_buffer */
2310 Py_TPFLAGS_DEFAULT, /* tp_flags */
2311 0, /* tp_doc */
2312 0, /* tp_traverse */
2313 0, /* tp_clear */
2314 0, /* tp_richcompare */
2315 0, /* tp_weaklistoffset */
2316 PyObject_SelfIter, /* tp_iter */
2317 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002318 dictiter_methods, /* tp_methods */
2319 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002320};