blob: 252db8ad91062795190667d951747f34533ab561 [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,
5 describing explorations into dictionary design and optimization.
6 It covers typical dictionary use patterns, the parameters for
7 tuning dictionaries, and several ideas for possible optimizations.
8*/
9
Guido van Rossumc0b618a1997-05-02 03:12:38 +000010#include "Python.h"
Guido van Rossum4b1302b1993-03-27 18:11:32 +000011
Nicholas Bastin9e1bfe72004-06-18 19:57:13 +000012#ifdef __SUNPRO_C
13#pragma error_messages (off,E_END_OF_LOOP_CODE_NOT_REACHED)
14#endif
15
Tim Peters6d6c1a32001-08-02 04:15:00 +000016typedef PyDictEntry dictentry;
17typedef PyDictObject dictobject;
Guido van Rossum16e93a81997-01-28 00:00:11 +000018
Tim Peterseb28ef22001-06-02 05:27:19 +000019/* Define this out if you don't want conversion statistics on exit. */
Fred Drake1bff34a2000-08-31 19:31:38 +000020#undef SHOW_CONVERSION_COUNTS
21
Tim Peterseb28ef22001-06-02 05:27:19 +000022/* See large comment block below. This must be >= 1. */
23#define PERTURB_SHIFT 5
24
Guido van Rossum16e93a81997-01-28 00:00:11 +000025/*
Tim Peterseb28ef22001-06-02 05:27:19 +000026Major subtleties ahead: Most hash schemes depend on having a "good" hash
27function, in the sense of simulating randomness. Python doesn't: its most
28important hash functions (for strings and ints) are very regular in common
29cases:
Tim Peters15d49292001-05-27 07:39:22 +000030
Tim Peterseb28ef22001-06-02 05:27:19 +000031>>> map(hash, (0, 1, 2, 3))
32[0, 1, 2, 3]
33>>> map(hash, ("namea", "nameb", "namec", "named"))
34[-1658398457, -1658398460, -1658398459, -1658398462]
35>>>
Tim Peters15d49292001-05-27 07:39:22 +000036
Tim Peterseb28ef22001-06-02 05:27:19 +000037This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
38the low-order i bits as the initial table index is extremely fast, and there
39are no collisions at all for dicts indexed by a contiguous range of ints.
40The same is approximately true when keys are "consecutive" strings. So this
41gives better-than-random behavior in common cases, and that's very desirable.
Tim Peters15d49292001-05-27 07:39:22 +000042
Tim Peterseb28ef22001-06-02 05:27:19 +000043OTOH, when collisions occur, the tendency to fill contiguous slices of the
44hash table makes a good collision resolution strategy crucial. Taking only
45the last i bits of the hash code is also vulnerable: for example, consider
46[i << 16 for i in range(20000)] as a set of keys. Since ints are their own
47hash codes, and this fits in a dict of size 2**15, the last 15 bits of every
48hash code are all 0: they *all* map to the same table index.
Tim Peters15d49292001-05-27 07:39:22 +000049
Tim Peterseb28ef22001-06-02 05:27:19 +000050But catering to unusual cases should not slow the usual ones, so we just take
51the last i bits anyway. It's up to collision resolution to do the rest. If
52we *usually* find the key we're looking for on the first try (and, it turns
53out, we usually do -- the table load factor is kept under 2/3, so the odds
54are solidly in our favor), then it makes best sense to keep the initial index
55computation dirt cheap.
Tim Peters15d49292001-05-27 07:39:22 +000056
Tim Peterseb28ef22001-06-02 05:27:19 +000057The first half of collision resolution is to visit table indices via this
58recurrence:
Tim Peters15d49292001-05-27 07:39:22 +000059
Tim Peterseb28ef22001-06-02 05:27:19 +000060 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +000061
Tim Peterseb28ef22001-06-02 05:27:19 +000062For any initial j in range(2**i), repeating that 2**i times generates each
63int in range(2**i) exactly once (see any text on random-number generation for
64proof). By itself, this doesn't help much: like linear probing (setting
65j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
66order. This would be bad, except that's not the only thing we do, and it's
67actually *good* in the common cases where hash keys are consecutive. In an
68example that's really too small to make this entirely clear, for a table of
69size 2**3 the order of indices is:
Tim Peters15d49292001-05-27 07:39:22 +000070
Tim Peterseb28ef22001-06-02 05:27:19 +000071 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
72
73If two things come in at index 5, the first place we look after is index 2,
74not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
75Linear probing is deadly in this case because there the fixed probe order
76is the *same* as the order consecutive keys are likely to arrive. But it's
77extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
78and certain that consecutive hash codes do not.
79
80The other half of the strategy is to get the other bits of the hash code
81into play. This is done by initializing a (unsigned) vrbl "perturb" to the
82full hash code, and changing the recurrence to:
83
84 j = (5*j) + 1 + perturb;
85 perturb >>= PERTURB_SHIFT;
86 use j % 2**i as the next table index;
87
88Now the probe sequence depends (eventually) on every bit in the hash code,
89and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
90because it quickly magnifies small differences in the bits that didn't affect
91the initial index. Note that because perturb is unsigned, if the recurrence
92is executed often enough perturb eventually becomes and remains 0. At that
93point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
94that's certain to find an empty slot eventually (since it generates every int
95in range(2**i), and we make sure there's always at least one empty slot).
96
97Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
98small so that the high bits of the hash code continue to affect the probe
99sequence across iterations; but you want it large so that in really bad cases
100the high-order hash bits have an effect on early iterations. 5 was "the
101best" in minimizing total collisions across experiments Tim Peters ran (on
102both normal and pathological cases), but 4 and 6 weren't significantly worse.
103
104Historical: Reimer Behrends contributed the idea of using a polynomial-based
105approach, using repeated multiplication by x in GF(2**n) where an irreducible
106polynomial for each table size was chosen such that x was a primitive root.
107Christian Tismer later extended that to use division by x instead, as an
108efficient way to get the high bits of the hash code into play. This scheme
109also gave excellent collision statistics, but was more expensive: two
110if-tests were required inside the loop; computing "the next" index took about
111the same number of operations but without as much potential parallelism
112(e.g., computing 5*j can go on at the same time as computing 1+perturb in the
113above, and then shifting perturb can be done while the table index is being
114masked); and the dictobject struct required a member to hold the table's
115polynomial. In Tim's experiments the current scheme ran faster, produced
116equally good collision statistics, needed less code & used less memory.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000117*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000118
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000119/* Object used as dummy key to fill deleted entries */
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000120static PyObject *dummy; /* Initialized by first call to newdictobject() */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000121
Fred Drake1bff34a2000-08-31 19:31:38 +0000122/* forward declarations */
123static dictentry *
124lookdict_string(dictobject *mp, PyObject *key, long hash);
125
126#ifdef SHOW_CONVERSION_COUNTS
127static long created = 0L;
128static long converted = 0L;
129
130static void
131show_counts(void)
132{
133 fprintf(stderr, "created %ld string dicts\n", created);
134 fprintf(stderr, "converted %ld to normal dicts\n", converted);
135 fprintf(stderr, "%.2f%% conversion rate\n", (100.0*converted)/created);
136}
137#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000138
Tim Peters6d6c1a32001-08-02 04:15:00 +0000139/* Initialization macros.
140 There are two ways to create a dict: PyDict_New() is the main C API
141 function, and the tp_new slot maps to dict_new(). In the latter case we
142 can save a little time over what PyDict_New does because it's guaranteed
143 that the PyDictObject struct is already zeroed out.
144 Everyone except dict_new() should use EMPTY_TO_MINSIZE (unless they have
145 an excellent reason not to).
146*/
147
148#define INIT_NONZERO_DICT_SLOTS(mp) do { \
Tim Petersdea48ec2001-05-22 20:40:22 +0000149 (mp)->ma_table = (mp)->ma_smalltable; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000150 (mp)->ma_mask = PyDict_MINSIZE - 1; \
151 } while(0)
152
153#define EMPTY_TO_MINSIZE(mp) do { \
154 memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000155 (mp)->ma_used = (mp)->ma_fill = 0; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000156 INIT_NONZERO_DICT_SLOTS(mp); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000157 } while(0)
158
Raymond Hettinger43442782004-03-17 21:55:03 +0000159/* Dictionary reuse scheme to save calls to malloc, free, and memset */
160#define MAXFREEDICTS 80
161static PyDictObject *free_dicts[MAXFREEDICTS];
162static int num_free_dicts = 0;
163
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000164PyObject *
Thomas Wouters78890102000-07-22 19:25:51 +0000165PyDict_New(void)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000166{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000167 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000168 if (dummy == NULL) { /* Auto-initialize dummy */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000169 dummy = PyString_FromString("<dummy key>");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000170 if (dummy == NULL)
171 return NULL;
Fred Drake1bff34a2000-08-31 19:31:38 +0000172#ifdef SHOW_CONVERSION_COUNTS
173 Py_AtExit(show_counts);
174#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000175 }
Raymond Hettinger43442782004-03-17 21:55:03 +0000176 if (num_free_dicts) {
177 mp = free_dicts[--num_free_dicts];
178 assert (mp != NULL);
179 assert (mp->ob_type == &PyDict_Type);
180 _Py_NewReference((PyObject *)mp);
181 if (mp->ma_fill) {
182 EMPTY_TO_MINSIZE(mp);
183 }
184 assert (mp->ma_used == 0);
185 assert (mp->ma_table == mp->ma_smalltable);
186 assert (mp->ma_mask == PyDict_MINSIZE - 1);
187 } else {
188 mp = PyObject_GC_New(dictobject, &PyDict_Type);
189 if (mp == NULL)
190 return NULL;
191 EMPTY_TO_MINSIZE(mp);
192 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000193 mp->ma_lookup = lookdict_string;
194#ifdef SHOW_CONVERSION_COUNTS
195 ++created;
196#endif
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000197 _PyObject_GC_TRACK(mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000198 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000199}
200
201/*
202The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000203This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000204Open addressing is preferred over chaining since the link overhead for
205chaining would be substantial (100% with typical malloc overhead).
206
Tim Peterseb28ef22001-06-02 05:27:19 +0000207The initial probe index is computed as hash mod the table size. Subsequent
208probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000209
210All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000211
Tim Peterseb28ef22001-06-02 05:27:19 +0000212(The details in this version are due to Tim Peters, building on many past
213contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
214Christian Tismer).
Fred Drake1bff34a2000-08-31 19:31:38 +0000215
216This function must never return NULL; failures are indicated by returning
217a dictentry* for which the me_value field is NULL. Exceptions are never
218reported by this function, and outstanding exceptions are maintained.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000219*/
Tim Peterseb28ef22001-06-02 05:27:19 +0000220
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000221static dictentry *
Tim Peters1f5871e2000-07-04 17:44:48 +0000222lookdict(dictobject *mp, PyObject *key, register long hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000223{
Guido van Rossum16e93a81997-01-28 00:00:11 +0000224 register int i;
Tim Peterseb28ef22001-06-02 05:27:19 +0000225 register unsigned int perturb;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000226 register dictentry *freeslot;
Tim Petersafb6ae82001-06-04 21:00:21 +0000227 register unsigned int mask = mp->ma_mask;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000228 dictentry *ep0 = mp->ma_table;
229 register dictentry *ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000230 register int restore_error;
231 register int checked_error;
Fred Drakec88b99c2000-08-31 19:04:07 +0000232 register int cmp;
233 PyObject *err_type, *err_value, *err_tb;
Tim Peters453163d2001-06-03 04:54:32 +0000234 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000235
Tim Peters2f228e72001-05-13 00:19:31 +0000236 i = hash & mask;
Guido van Rossumefb46091997-01-29 15:53:56 +0000237 ep = &ep0[i];
Guido van Rossumc1c7b1a1998-10-06 16:01:14 +0000238 if (ep->me_key == NULL || ep->me_key == key)
Guido van Rossum7d181591997-01-16 21:06:45 +0000239 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000240
241 restore_error = checked_error = 0;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000242 if (ep->me_key == dummy)
Guido van Rossum7d181591997-01-16 21:06:45 +0000243 freeslot = ep;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000244 else {
Fred Drakec88b99c2000-08-31 19:04:07 +0000245 if (ep->me_hash == hash) {
246 /* error can't have been checked yet */
247 checked_error = 1;
248 if (PyErr_Occurred()) {
249 restore_error = 1;
250 PyErr_Fetch(&err_type, &err_value, &err_tb);
251 }
Tim Peters453163d2001-06-03 04:54:32 +0000252 startkey = ep->me_key;
253 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000254 if (cmp < 0)
Guido van Rossumb9324202001-01-18 00:39:02 +0000255 PyErr_Clear();
Tim Peters453163d2001-06-03 04:54:32 +0000256 if (ep0 == mp->ma_table && ep->me_key == startkey) {
257 if (cmp > 0)
258 goto Done;
259 }
260 else {
261 /* The compare did major nasty stuff to the
262 * dict: start over.
263 * XXX A clever adversary could prevent this
264 * XXX from terminating.
265 */
266 ep = lookdict(mp, key, hash);
267 goto Done;
268 }
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000269 }
270 freeslot = NULL;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000271 }
Tim Peters15d49292001-05-27 07:39:22 +0000272
Tim Peters342c65e2001-05-13 06:43:53 +0000273 /* In the loop, me_key == dummy is by far (factor of 100s) the
274 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000275 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
276 i = (i << 2) + i + perturb + 1;
277 ep = &ep0[i & mask];
Guido van Rossum16e93a81997-01-28 00:00:11 +0000278 if (ep->me_key == NULL) {
Tim Peters7b5d0af2001-06-03 04:14:43 +0000279 if (freeslot != NULL)
280 ep = freeslot;
281 break;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000282 }
Tim Peters7b5d0af2001-06-03 04:14:43 +0000283 if (ep->me_key == key)
284 break;
285 if (ep->me_hash == hash && ep->me_key != dummy) {
Fred Drakec88b99c2000-08-31 19:04:07 +0000286 if (!checked_error) {
287 checked_error = 1;
288 if (PyErr_Occurred()) {
289 restore_error = 1;
290 PyErr_Fetch(&err_type, &err_value,
291 &err_tb);
292 }
293 }
Tim Peters453163d2001-06-03 04:54:32 +0000294 startkey = ep->me_key;
295 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000296 if (cmp < 0)
Guido van Rossumb9324202001-01-18 00:39:02 +0000297 PyErr_Clear();
Tim Peters453163d2001-06-03 04:54:32 +0000298 if (ep0 == mp->ma_table && ep->me_key == startkey) {
299 if (cmp > 0)
300 break;
301 }
302 else {
303 /* The compare did major nasty stuff to the
304 * dict: start over.
305 * XXX A clever adversary could prevent this
306 * XXX from terminating.
307 */
308 ep = lookdict(mp, key, hash);
309 break;
310 }
Guido van Rossum16e93a81997-01-28 00:00:11 +0000311 }
Tim Peters342c65e2001-05-13 06:43:53 +0000312 else if (ep->me_key == dummy && freeslot == NULL)
313 freeslot = ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000314 }
Tim Peters7b5d0af2001-06-03 04:14:43 +0000315
316Done:
317 if (restore_error)
318 PyErr_Restore(err_type, err_value, err_tb);
319 return ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000320}
321
322/*
Fred Drake1bff34a2000-08-31 19:31:38 +0000323 * Hacked up version of lookdict which can assume keys are always strings;
324 * this assumption allows testing for errors during PyObject_Compare() to
325 * be dropped; string-string comparisons never raise exceptions. This also
326 * means we don't need to go through PyObject_Compare(); we can always use
Martin v. Löwiscd353062001-05-24 16:56:35 +0000327 * _PyString_Eq directly.
Fred Drake1bff34a2000-08-31 19:31:38 +0000328 *
Tim Peters0ab085c2001-09-14 00:25:33 +0000329 * This is valuable because the general-case error handling in lookdict() is
330 * expensive, and dicts with pure-string keys are very common.
Fred Drake1bff34a2000-08-31 19:31:38 +0000331 */
332static dictentry *
333lookdict_string(dictobject *mp, PyObject *key, register long hash)
334{
335 register int i;
Tim Peterseb28ef22001-06-02 05:27:19 +0000336 register unsigned int perturb;
Fred Drake1bff34a2000-08-31 19:31:38 +0000337 register dictentry *freeslot;
Tim Petersafb6ae82001-06-04 21:00:21 +0000338 register unsigned int mask = mp->ma_mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000339 dictentry *ep0 = mp->ma_table;
340 register dictentry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000341
Tim Peters0ab085c2001-09-14 00:25:33 +0000342 /* Make sure this function doesn't have to handle non-string keys,
343 including subclasses of str; e.g., one reason to subclass
344 strings is to override __eq__, and for speed we don't cater to
345 that here. */
346 if (!PyString_CheckExact(key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000347#ifdef SHOW_CONVERSION_COUNTS
348 ++converted;
349#endif
350 mp->ma_lookup = lookdict;
351 return lookdict(mp, key, hash);
352 }
Tim Peters2f228e72001-05-13 00:19:31 +0000353 i = hash & mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000354 ep = &ep0[i];
355 if (ep->me_key == NULL || ep->me_key == key)
356 return ep;
357 if (ep->me_key == dummy)
358 freeslot = ep;
359 else {
360 if (ep->me_hash == hash
Martin v. Löwiscd353062001-05-24 16:56:35 +0000361 && _PyString_Eq(ep->me_key, key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000362 return ep;
363 }
364 freeslot = NULL;
365 }
Tim Peters15d49292001-05-27 07:39:22 +0000366
Tim Peters342c65e2001-05-13 06:43:53 +0000367 /* In the loop, me_key == dummy is by far (factor of 100s) the
368 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000369 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
370 i = (i << 2) + i + perturb + 1;
371 ep = &ep0[i & mask];
Tim Peters342c65e2001-05-13 06:43:53 +0000372 if (ep->me_key == NULL)
373 return freeslot == NULL ? ep : freeslot;
374 if (ep->me_key == key
375 || (ep->me_hash == hash
376 && ep->me_key != dummy
Martin v. Löwiscd353062001-05-24 16:56:35 +0000377 && _PyString_Eq(ep->me_key, key)))
Fred Drake1bff34a2000-08-31 19:31:38 +0000378 return ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000379 if (ep->me_key == dummy && freeslot == NULL)
Tim Peters342c65e2001-05-13 06:43:53 +0000380 freeslot = ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000381 }
382}
383
384/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000385Internal routine to insert a new item into the table.
386Used both by the internal resize routine and by the public insert routine.
387Eats a reference to key and one to value.
388*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000389static void
Tim Peters1f5871e2000-07-04 17:44:48 +0000390insertdict(register dictobject *mp, PyObject *key, long hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000391{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000392 PyObject *old_value;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000393 register dictentry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000394 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
395
396 assert(mp->ma_lookup != NULL);
397 ep = mp->ma_lookup(mp, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000398 if (ep->me_value != NULL) {
Guido van Rossumd7047b31995-01-02 19:07:15 +0000399 old_value = ep->me_value;
400 ep->me_value = value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000401 Py_DECREF(old_value); /* which **CAN** re-enter */
402 Py_DECREF(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000403 }
404 else {
405 if (ep->me_key == NULL)
406 mp->ma_fill++;
407 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000408 Py_DECREF(ep->me_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000409 ep->me_key = key;
410 ep->me_hash = hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000411 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000412 mp->ma_used++;
413 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000414}
415
416/*
417Restructure the table by allocating a new table and reinserting all
418items again. When entries have been deleted, the new table may
419actually be smaller than the old one.
420*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000421static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000422dictresize(dictobject *mp, int minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000423{
Tim Peterseb28ef22001-06-02 05:27:19 +0000424 int newsize;
Tim Peters0c6010b2001-05-23 23:33:57 +0000425 dictentry *oldtable, *newtable, *ep;
426 int i;
427 int is_oldtable_malloced;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000428 dictentry small_copy[PyDict_MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000429
430 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000431
Tim Peterseb28ef22001-06-02 05:27:19 +0000432 /* Find the smallest table size > minused. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000433 for (newsize = PyDict_MINSIZE;
Tim Peterseb28ef22001-06-02 05:27:19 +0000434 newsize <= minused && newsize > 0;
435 newsize <<= 1)
436 ;
437 if (newsize <= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000438 PyErr_NoMemory();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000439 return -1;
440 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000441
442 /* Get space for a new table. */
443 oldtable = mp->ma_table;
444 assert(oldtable != NULL);
445 is_oldtable_malloced = oldtable != mp->ma_smalltable;
446
Tim Peters6d6c1a32001-08-02 04:15:00 +0000447 if (newsize == PyDict_MINSIZE) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000448 /* A large table is shrinking, or we can't get any smaller. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000449 newtable = mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000450 if (newtable == oldtable) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000451 if (mp->ma_fill == mp->ma_used) {
452 /* No dummies, so no point doing anything. */
Tim Peters0c6010b2001-05-23 23:33:57 +0000453 return 0;
Tim Petersf8a548c2001-05-24 16:26:40 +0000454 }
455 /* We're not going to resize it, but rebuild the
456 table anyway to purge old dummy entries.
457 Subtle: This is *necessary* if fill==size,
458 as lookdict needs at least one virgin slot to
459 terminate failing searches. If fill < size, it's
460 merely desirable, as dummies slow searches. */
461 assert(mp->ma_fill > mp->ma_used);
Tim Peters0c6010b2001-05-23 23:33:57 +0000462 memcpy(small_copy, oldtable, sizeof(small_copy));
463 oldtable = small_copy;
464 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000465 }
466 else {
467 newtable = PyMem_NEW(dictentry, newsize);
468 if (newtable == NULL) {
469 PyErr_NoMemory();
470 return -1;
471 }
472 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000473
474 /* Make the dict empty, using the new table. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000475 assert(newtable != oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000476 mp->ma_table = newtable;
Tim Petersafb6ae82001-06-04 21:00:21 +0000477 mp->ma_mask = newsize - 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000478 memset(newtable, 0, sizeof(dictentry) * newsize);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000479 mp->ma_used = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000480 i = mp->ma_fill;
481 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000482
Tim Peters19283142001-05-17 22:25:34 +0000483 /* Copy the data over; this is refcount-neutral for active entries;
484 dummy entries aren't copied over, of course */
Tim Petersdea48ec2001-05-22 20:40:22 +0000485 for (ep = oldtable; i > 0; ep++) {
486 if (ep->me_value != NULL) { /* active entry */
487 --i;
Tim Peters19283142001-05-17 22:25:34 +0000488 insertdict(mp, ep->me_key, ep->me_hash, ep->me_value);
Tim Petersdea48ec2001-05-22 20:40:22 +0000489 }
Tim Peters19283142001-05-17 22:25:34 +0000490 else if (ep->me_key != NULL) { /* dummy entry */
Tim Petersdea48ec2001-05-22 20:40:22 +0000491 --i;
Tim Peters19283142001-05-17 22:25:34 +0000492 assert(ep->me_key == dummy);
493 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000494 }
Tim Peters19283142001-05-17 22:25:34 +0000495 /* else key == value == NULL: nothing to do */
Guido van Rossumd7047b31995-01-02 19:07:15 +0000496 }
497
Tim Peters0c6010b2001-05-23 23:33:57 +0000498 if (is_oldtable_malloced)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000499 PyMem_DEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000500 return 0;
501}
502
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000503PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000504PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000505{
506 long hash;
Fred Drake1bff34a2000-08-31 19:31:38 +0000507 dictobject *mp = (dictobject *)op;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000508 if (!PyDict_Check(op)) {
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000509 return NULL;
510 }
Tim Peters0ab085c2001-09-14 00:25:33 +0000511 if (!PyString_CheckExact(key) ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000512 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Guido van Rossumca756f21997-01-23 19:39:29 +0000513 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000514 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000515 if (hash == -1) {
516 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000517 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000518 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000519 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000520 return (mp->ma_lookup)(mp, key, hash)->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000521}
522
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000523/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
524 * dictionary if it is merely replacing the value for an existing key.
525 * This is means that it's safe to loop over a dictionary with
526 * PyDict_Next() and occasionally replace a value -- but you can't
527 * insert new keys or remove them.
528 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000529int
Tim Peters1f5871e2000-07-04 17:44:48 +0000530PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000531{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000532 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000533 register long hash;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000534 register int n_used;
535
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000536 if (!PyDict_Check(op)) {
537 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000538 return -1;
539 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000540 mp = (dictobject *)op;
Tim Peters0ab085c2001-09-14 00:25:33 +0000541 if (PyString_CheckExact(key)) {
Guido van Rossum45ec02a2002-08-19 21:43:18 +0000542 hash = ((PyStringObject *)key)->ob_shash;
543 if (hash == -1)
544 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000545 }
Tim Peters1f7df352002-03-29 03:29:08 +0000546 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000547 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000548 if (hash == -1)
549 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000550 }
Tim Petersafb6ae82001-06-04 21:00:21 +0000551 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000552 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000553 Py_INCREF(value);
554 Py_INCREF(key);
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000555 insertdict(mp, key, hash, value);
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000556 /* If we added a key, we can safely resize. Otherwise just return!
557 * If fill >= 2/3 size, adjust size. Normally, this doubles or
558 * quaduples the size, but it's also possible for the dict to shrink
559 * (if ma_fill is much larger than ma_used, meaning a lot of dict
560 * keys have been * deleted).
561 *
562 * Quadrupling the size improves average dictionary sparseness
563 * (reducing collisions) at the cost of some memory and iteration
564 * speed (which loops over every possible entry). It also halves
565 * the number of expensive resize operations in a growing dictionary.
566 *
567 * Very large dictionaries (over 50K items) use doubling instead.
568 * This may help applications with severe memory constraints.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000569 */
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000570 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
571 return 0;
572 return dictresize(mp, mp->ma_used*(mp->ma_used>50000 ? 2 : 4));
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000573}
574
575int
Tim Peters1f5871e2000-07-04 17:44:48 +0000576PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000577{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000578 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000579 register long hash;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000580 register dictentry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000581 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000582
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000583 if (!PyDict_Check(op)) {
584 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000585 return -1;
586 }
Tim Peters0ab085c2001-09-14 00:25:33 +0000587 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +0000588 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000589 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000590 if (hash == -1)
591 return -1;
592 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000593 mp = (dictobject *)op;
Fred Drake1bff34a2000-08-31 19:31:38 +0000594 ep = (mp->ma_lookup)(mp, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000595 if (ep->me_value == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000596 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000597 return -1;
598 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000599 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000600 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000601 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000602 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000603 ep->me_value = NULL;
604 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000605 Py_DECREF(old_value);
606 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000607 return 0;
608}
609
Guido van Rossum25831651993-05-19 14:50:45 +0000610void
Tim Peters1f5871e2000-07-04 17:44:48 +0000611PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000612{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000613 dictobject *mp;
Tim Petersdea48ec2001-05-22 20:40:22 +0000614 dictentry *ep, *table;
615 int table_is_malloced;
616 int fill;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000617 dictentry small_copy[PyDict_MINSIZE];
Tim Petersdea48ec2001-05-22 20:40:22 +0000618#ifdef Py_DEBUG
619 int i, n;
620#endif
621
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000622 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000623 return;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000624 mp = (dictobject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000625#ifdef Py_DEBUG
Tim Petersafb6ae82001-06-04 21:00:21 +0000626 n = mp->ma_mask + 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000627 i = 0;
628#endif
629
630 table = mp->ma_table;
631 assert(table != NULL);
632 table_is_malloced = table != mp->ma_smalltable;
633
634 /* This is delicate. During the process of clearing the dict,
635 * decrefs can cause the dict to mutate. To avoid fatal confusion
636 * (voice of experience), we have to make the dict empty before
637 * clearing the slots, and never refer to anything via mp->xxx while
638 * clearing.
639 */
640 fill = mp->ma_fill;
641 if (table_is_malloced)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000642 EMPTY_TO_MINSIZE(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000643
644 else if (fill > 0) {
645 /* It's a small table with something that needs to be cleared.
646 * Afraid the only safe way is to copy the dict entries into
647 * another small table first.
648 */
649 memcpy(small_copy, table, sizeof(small_copy));
650 table = small_copy;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000651 EMPTY_TO_MINSIZE(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000652 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000653 /* else it's a small table that's already empty */
654
655 /* Now we can finally clear things. If C had refcounts, we could
656 * assert that the refcount on table is 1 now, i.e. that this function
657 * has unique access to it, so decref side-effects can't alter it.
658 */
659 for (ep = table; fill > 0; ++ep) {
660#ifdef Py_DEBUG
661 assert(i < n);
662 ++i;
663#endif
664 if (ep->me_key) {
665 --fill;
666 Py_DECREF(ep->me_key);
667 Py_XDECREF(ep->me_value);
668 }
669#ifdef Py_DEBUG
670 else
671 assert(ep->me_value == NULL);
672#endif
673 }
674
675 if (table_is_malloced)
676 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000677}
678
Tim Peters080c88b2003-02-15 03:01:11 +0000679/*
680 * Iterate over a dict. Use like so:
681 *
682 * int i;
683 * PyObject *key, *value;
684 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +0000685 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +0000686 * Refer to borrowed references in key and value.
687 * }
688 *
689 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +0000690 * mutates the dict. One exception: it is safe if the loop merely changes
691 * the values associated with the keys (but doesn't insert new keys or
692 * delete keys), via PyDict_SetItem().
693 */
Guido van Rossum25831651993-05-19 14:50:45 +0000694int
Tim Peters1f5871e2000-07-04 17:44:48 +0000695PyDict_Next(PyObject *op, int *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000696{
Raymond Hettinger43442782004-03-17 21:55:03 +0000697 register int i, mask;
698 register dictentry *ep;
699
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000700 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000701 return 0;
Guido van Rossum25831651993-05-19 14:50:45 +0000702 i = *ppos;
703 if (i < 0)
704 return 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000705 ep = ((dictobject *)op)->ma_table;
706 mask = ((dictobject *)op)->ma_mask;
707 while (i <= mask && ep[i].me_value == NULL)
Guido van Rossum25831651993-05-19 14:50:45 +0000708 i++;
709 *ppos = i+1;
Raymond Hettinger43442782004-03-17 21:55:03 +0000710 if (i > mask)
Guido van Rossum25831651993-05-19 14:50:45 +0000711 return 0;
712 if (pkey)
Raymond Hettinger43442782004-03-17 21:55:03 +0000713 *pkey = ep[i].me_key;
Guido van Rossum25831651993-05-19 14:50:45 +0000714 if (pvalue)
Raymond Hettinger43442782004-03-17 21:55:03 +0000715 *pvalue = ep[i].me_value;
Guido van Rossum25831651993-05-19 14:50:45 +0000716 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000717}
718
719/* Methods */
720
721static void
Tim Peters1f5871e2000-07-04 17:44:48 +0000722dict_dealloc(register dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000723{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000724 register dictentry *ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000725 int fill = mp->ma_fill;
Guido van Rossumff413af2002-03-28 20:34:59 +0000726 PyObject_GC_UnTrack(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000727 Py_TRASHCAN_SAFE_BEGIN(mp)
Tim Petersdea48ec2001-05-22 20:40:22 +0000728 for (ep = mp->ma_table; fill > 0; ep++) {
729 if (ep->me_key) {
730 --fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000731 Py_DECREF(ep->me_key);
Tim Petersdea48ec2001-05-22 20:40:22 +0000732 Py_XDECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000733 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000734 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000735 if (mp->ma_table != mp->ma_smalltable)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000736 PyMem_DEL(mp->ma_table);
Raymond Hettinger43442782004-03-17 21:55:03 +0000737 if (num_free_dicts < MAXFREEDICTS && mp->ob_type == &PyDict_Type)
738 free_dicts[num_free_dicts++] = mp;
739 else
740 mp->ob_type->tp_free((PyObject *)mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000741 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000742}
743
744static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000745dict_print(register dictobject *mp, register FILE *fp, register int flags)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000746{
747 register int i;
748 register int any;
Guido van Rossum255443b1998-04-10 22:47:14 +0000749
750 i = Py_ReprEnter((PyObject*)mp);
751 if (i != 0) {
752 if (i < 0)
753 return i;
754 fprintf(fp, "{...}");
755 return 0;
756 }
757
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000758 fprintf(fp, "{");
759 any = 0;
Tim Petersafb6ae82001-06-04 21:00:21 +0000760 for (i = 0; i <= mp->ma_mask; i++) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000761 dictentry *ep = mp->ma_table + i;
762 PyObject *pvalue = ep->me_value;
763 if (pvalue != NULL) {
764 /* Prevent PyObject_Repr from deleting value during
765 key format */
766 Py_INCREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000767 if (any++ > 0)
768 fprintf(fp, ", ");
Guido van Rossum255443b1998-04-10 22:47:14 +0000769 if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000770 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000771 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000772 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000773 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000774 fprintf(fp, ": ");
Tim Peters19b77cf2001-06-02 08:27:39 +0000775 if (PyObject_Print(pvalue, fp, 0) != 0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000776 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000777 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000778 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000779 }
Tim Peters23cf6be2001-06-02 08:02:56 +0000780 Py_DECREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000781 }
782 }
783 fprintf(fp, "}");
Guido van Rossum255443b1998-04-10 22:47:14 +0000784 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000785 return 0;
786}
787
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000788static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000789dict_repr(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000790{
Tim Petersc6057842001-06-16 07:52:53 +0000791 int i;
Tim Petersa7259592001-06-16 05:11:17 +0000792 PyObject *s, *temp, *colon = NULL;
793 PyObject *pieces = NULL, *result = NULL;
794 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +0000795
Tim Petersa7259592001-06-16 05:11:17 +0000796 i = Py_ReprEnter((PyObject *)mp);
Guido van Rossum255443b1998-04-10 22:47:14 +0000797 if (i != 0) {
Tim Petersa7259592001-06-16 05:11:17 +0000798 return i > 0 ? PyString_FromString("{...}") : NULL;
Guido van Rossum255443b1998-04-10 22:47:14 +0000799 }
800
Tim Petersa7259592001-06-16 05:11:17 +0000801 if (mp->ma_used == 0) {
802 result = PyString_FromString("{}");
803 goto Done;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000804 }
Tim Petersa7259592001-06-16 05:11:17 +0000805
806 pieces = PyList_New(0);
807 if (pieces == NULL)
808 goto Done;
809
810 colon = PyString_FromString(": ");
811 if (colon == NULL)
812 goto Done;
813
814 /* Do repr() on each key+value pair, and insert ": " between them.
815 Note that repr may mutate the dict. */
Tim Petersc6057842001-06-16 07:52:53 +0000816 i = 0;
817 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Tim Petersa7259592001-06-16 05:11:17 +0000818 int status;
819 /* Prevent repr from deleting value during key format. */
820 Py_INCREF(value);
821 s = PyObject_Repr(key);
822 PyString_Concat(&s, colon);
823 PyString_ConcatAndDel(&s, PyObject_Repr(value));
824 Py_DECREF(value);
825 if (s == NULL)
826 goto Done;
827 status = PyList_Append(pieces, s);
828 Py_DECREF(s); /* append created a new ref */
829 if (status < 0)
830 goto Done;
831 }
832
833 /* Add "{}" decorations to the first and last items. */
834 assert(PyList_GET_SIZE(pieces) > 0);
835 s = PyString_FromString("{");
836 if (s == NULL)
837 goto Done;
838 temp = PyList_GET_ITEM(pieces, 0);
839 PyString_ConcatAndDel(&s, temp);
840 PyList_SET_ITEM(pieces, 0, s);
841 if (s == NULL)
842 goto Done;
843
844 s = PyString_FromString("}");
845 if (s == NULL)
846 goto Done;
847 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
848 PyString_ConcatAndDel(&temp, s);
849 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
850 if (temp == NULL)
851 goto Done;
852
853 /* Paste them all together with ", " between. */
854 s = PyString_FromString(", ");
855 if (s == NULL)
856 goto Done;
857 result = _PyString_Join(s, pieces);
Tim Peters0ab085c2001-09-14 00:25:33 +0000858 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +0000859
860Done:
861 Py_XDECREF(pieces);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000862 Py_XDECREF(colon);
Tim Petersa7259592001-06-16 05:11:17 +0000863 Py_ReprLeave((PyObject *)mp);
864 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000865}
866
867static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000868dict_length(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000869{
870 return mp->ma_used;
871}
872
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000873static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000874dict_subscript(dictobject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000875{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000876 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000877 long hash;
Tim Petersdea48ec2001-05-22 20:40:22 +0000878 assert(mp->ma_table != NULL);
Tim Peters0ab085c2001-09-14 00:25:33 +0000879 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +0000880 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000881 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000882 if (hash == -1)
883 return NULL;
884 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000885 v = (mp->ma_lookup)(mp, key, hash) -> me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000886 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000887 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000888 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000889 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000890 return v;
891}
892
893static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000894dict_ass_sub(dictobject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000895{
896 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000897 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000898 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000899 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000900}
901
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000902static PyMappingMethods dict_as_mapping = {
903 (inquiry)dict_length, /*mp_length*/
904 (binaryfunc)dict_subscript, /*mp_subscript*/
905 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000906};
907
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000908static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000909dict_keys(register dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000910{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000911 register PyObject *v;
Raymond Hettinger43442782004-03-17 21:55:03 +0000912 register int i, j;
913 dictentry *ep;
914 int mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000915
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000916 again:
917 n = mp->ma_used;
918 v = PyList_New(n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000919 if (v == NULL)
920 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000921 if (n != mp->ma_used) {
922 /* Durnit. The allocations caused the dict to resize.
923 * Just start over, this shouldn't normally happen.
924 */
925 Py_DECREF(v);
926 goto again;
927 }
Raymond Hettinger43442782004-03-17 21:55:03 +0000928 ep = mp->ma_table;
929 mask = mp->ma_mask;
930 for (i = 0, j = 0; i <= mask; i++) {
931 if (ep[i].me_value != NULL) {
932 PyObject *key = ep[i].me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000933 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000934 PyList_SET_ITEM(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000935 j++;
936 }
937 }
Raymond Hettinger43442782004-03-17 21:55:03 +0000938 assert(j == n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000939 return v;
940}
941
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000942static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000943dict_values(register dictobject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +0000944{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000945 register PyObject *v;
Raymond Hettinger43442782004-03-17 21:55:03 +0000946 register int i, j;
947 dictentry *ep;
948 int mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000949
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000950 again:
951 n = mp->ma_used;
952 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +0000953 if (v == NULL)
954 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000955 if (n != mp->ma_used) {
956 /* Durnit. The allocations caused the dict to resize.
957 * Just start over, this shouldn't normally happen.
958 */
959 Py_DECREF(v);
960 goto again;
961 }
Raymond Hettinger43442782004-03-17 21:55:03 +0000962 ep = mp->ma_table;
963 mask = mp->ma_mask;
964 for (i = 0, j = 0; i <= mask; i++) {
965 if (ep[i].me_value != NULL) {
966 PyObject *value = ep[i].me_value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000967 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000968 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +0000969 j++;
970 }
971 }
Raymond Hettinger43442782004-03-17 21:55:03 +0000972 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +0000973 return v;
974}
975
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000976static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000977dict_items(register dictobject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +0000978{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000979 register PyObject *v;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000980 register int i, j, n;
Raymond Hettinger43442782004-03-17 21:55:03 +0000981 int mask;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000982 PyObject *item, *key, *value;
Raymond Hettinger43442782004-03-17 21:55:03 +0000983 dictentry *ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000984
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000985 /* Preallocate the list of tuples, to avoid allocations during
986 * the loop over the items, which could trigger GC, which
987 * could resize the dict. :-(
988 */
989 again:
990 n = mp->ma_used;
991 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +0000992 if (v == NULL)
993 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000994 for (i = 0; i < n; i++) {
995 item = PyTuple_New(2);
996 if (item == NULL) {
997 Py_DECREF(v);
998 return NULL;
999 }
1000 PyList_SET_ITEM(v, i, item);
1001 }
1002 if (n != mp->ma_used) {
1003 /* Durnit. The allocations caused the dict to resize.
1004 * Just start over, this shouldn't normally happen.
1005 */
1006 Py_DECREF(v);
1007 goto again;
1008 }
1009 /* Nothing we do below makes any function calls. */
Raymond Hettinger43442782004-03-17 21:55:03 +00001010 ep = mp->ma_table;
1011 mask = mp->ma_mask;
1012 for (i = 0, j = 0; i <= mask; i++) {
Raymond Hettinger06905122004-03-19 10:30:00 +00001013 if ((value=ep[i].me_value) != NULL) {
Raymond Hettinger43442782004-03-17 21:55:03 +00001014 key = ep[i].me_key;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001015 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001016 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001017 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001018 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001019 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001020 j++;
1021 }
1022 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001023 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001024 return v;
1025}
1026
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001027static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001028dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001029{
1030 PyObject *seq;
1031 PyObject *value = Py_None;
1032 PyObject *it; /* iter(seq) */
1033 PyObject *key;
1034 PyObject *d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001035 int status;
1036
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001037 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001038 return NULL;
1039
1040 d = PyObject_CallObject(cls, NULL);
1041 if (d == NULL)
1042 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001043
1044 it = PyObject_GetIter(seq);
1045 if (it == NULL){
1046 Py_DECREF(d);
1047 return NULL;
1048 }
1049
1050 for (;;) {
1051 key = PyIter_Next(it);
1052 if (key == NULL) {
1053 if (PyErr_Occurred())
1054 goto Fail;
1055 break;
1056 }
Raymond Hettingere03e5b12002-12-07 08:10:51 +00001057 status = PyObject_SetItem(d, key, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001058 Py_DECREF(key);
1059 if (status < 0)
1060 goto Fail;
1061 }
1062
1063 Py_DECREF(it);
1064 return d;
1065
1066Fail:
1067 Py_DECREF(it);
1068 Py_DECREF(d);
1069 return NULL;
1070}
1071
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001072static int
1073dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001074{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001075 PyObject *arg = NULL;
1076 int result = 0;
1077
1078 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1079 result = -1;
1080
1081 else if (arg != NULL) {
1082 if (PyObject_HasAttrString(arg, "keys"))
1083 result = PyDict_Merge(self, arg, 1);
1084 else
1085 result = PyDict_MergeFromSeq2(self, arg, 1);
1086 }
1087 if (result == 0 && kwds != NULL)
1088 result = PyDict_Merge(self, kwds, 1);
1089 return result;
1090}
1091
1092static PyObject *
1093dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1094{
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001095 if (dict_update_common(self, args, kwds, "update") != -1)
1096 Py_RETURN_NONE;
1097 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001098}
1099
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001100/* Update unconditionally replaces existing items.
1101 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001102 otherwise it leaves existing items unchanged.
1103
1104 PyDict_{Update,Merge} update/merge from a mapping object.
1105
Tim Petersf582b822001-12-11 18:51:08 +00001106 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001107 producing iterable objects of length 2.
1108*/
1109
Tim Petersf582b822001-12-11 18:51:08 +00001110int
Tim Peters1fc240e2001-10-26 05:06:50 +00001111PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1112{
1113 PyObject *it; /* iter(seq2) */
1114 int i; /* index into seq2 of current element */
1115 PyObject *item; /* seq2[i] */
1116 PyObject *fast; /* item as a 2-tuple or 2-list */
1117
1118 assert(d != NULL);
1119 assert(PyDict_Check(d));
1120 assert(seq2 != NULL);
1121
1122 it = PyObject_GetIter(seq2);
1123 if (it == NULL)
1124 return -1;
1125
1126 for (i = 0; ; ++i) {
1127 PyObject *key, *value;
1128 int n;
1129
1130 fast = NULL;
1131 item = PyIter_Next(it);
1132 if (item == NULL) {
1133 if (PyErr_Occurred())
1134 goto Fail;
1135 break;
1136 }
1137
1138 /* Convert item to sequence, and verify length 2. */
1139 fast = PySequence_Fast(item, "");
1140 if (fast == NULL) {
1141 if (PyErr_ExceptionMatches(PyExc_TypeError))
1142 PyErr_Format(PyExc_TypeError,
1143 "cannot convert dictionary update "
1144 "sequence element #%d to a sequence",
1145 i);
1146 goto Fail;
1147 }
1148 n = PySequence_Fast_GET_SIZE(fast);
1149 if (n != 2) {
1150 PyErr_Format(PyExc_ValueError,
1151 "dictionary update sequence element #%d "
1152 "has length %d; 2 is required",
1153 i, n);
1154 goto Fail;
1155 }
1156
1157 /* Update/merge with this (key, value) pair. */
1158 key = PySequence_Fast_GET_ITEM(fast, 0);
1159 value = PySequence_Fast_GET_ITEM(fast, 1);
1160 if (override || PyDict_GetItem(d, key) == NULL) {
1161 int status = PyDict_SetItem(d, key, value);
1162 if (status < 0)
1163 goto Fail;
1164 }
1165 Py_DECREF(fast);
1166 Py_DECREF(item);
1167 }
1168
1169 i = 0;
1170 goto Return;
1171Fail:
1172 Py_XDECREF(item);
1173 Py_XDECREF(fast);
1174 i = -1;
1175Return:
1176 Py_DECREF(it);
1177 return i;
1178}
1179
Tim Peters6d6c1a32001-08-02 04:15:00 +00001180int
1181PyDict_Update(PyObject *a, PyObject *b)
1182{
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001183 return PyDict_Merge(a, b, 1);
1184}
1185
1186int
1187PyDict_Merge(PyObject *a, PyObject *b, int override)
1188{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001189 register PyDictObject *mp, *other;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001190 register int i;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001191 dictentry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001192
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001193 /* We accept for the argument either a concrete dictionary object,
1194 * or an abstract "mapping" object. For the former, we can do
1195 * things quite efficiently. For the latter, we only require that
1196 * PyMapping_Keys() and PyObject_GetItem() be supported.
1197 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001198 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1199 PyErr_BadInternalCall();
1200 return -1;
1201 }
1202 mp = (dictobject*)a;
1203 if (PyDict_Check(b)) {
1204 other = (dictobject*)b;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001205 if (other == mp || other->ma_used == 0)
1206 /* a.update(a) or a.update({}); nothing to do */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001207 return 0;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001208 /* Do one big resize at the start, rather than
1209 * incrementally resizing as we insert new items. Expect
1210 * that there will be no (or few) overlapping keys.
1211 */
1212 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
Raymond Hettingerc8d22902003-05-07 00:49:40 +00001213 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001214 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001215 }
1216 for (i = 0; i <= other->ma_mask; i++) {
1217 entry = &other->ma_table[i];
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001218 if (entry->me_value != NULL &&
1219 (override ||
1220 PyDict_GetItem(a, entry->me_key) == NULL)) {
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001221 Py_INCREF(entry->me_key);
1222 Py_INCREF(entry->me_value);
1223 insertdict(mp, entry->me_key, entry->me_hash,
1224 entry->me_value);
1225 }
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001226 }
1227 }
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001228 else {
1229 /* Do it the generic, slower way */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001230 PyObject *keys = PyMapping_Keys(b);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001231 PyObject *iter;
1232 PyObject *key, *value;
1233 int status;
1234
1235 if (keys == NULL)
1236 /* Docstring says this is equivalent to E.keys() so
1237 * if E doesn't have a .keys() method we want
1238 * AttributeError to percolate up. Might as well
1239 * do the same for any other error.
1240 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001241 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001242
1243 iter = PyObject_GetIter(keys);
1244 Py_DECREF(keys);
1245 if (iter == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001246 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001247
1248 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001249 if (!override && PyDict_GetItem(a, key) != NULL) {
1250 Py_DECREF(key);
1251 continue;
1252 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001253 value = PyObject_GetItem(b, key);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001254 if (value == NULL) {
1255 Py_DECREF(iter);
1256 Py_DECREF(key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001257 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001258 }
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001259 status = PyDict_SetItem(a, key, value);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001260 Py_DECREF(key);
1261 Py_DECREF(value);
1262 if (status < 0) {
1263 Py_DECREF(iter);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001264 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001265 }
1266 }
1267 Py_DECREF(iter);
1268 if (PyErr_Occurred())
1269 /* Iterator completed, via error */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001270 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001271 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001272 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001273}
1274
1275static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001276dict_copy(register dictobject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001277{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001278 return PyDict_Copy((PyObject*)mp);
1279}
1280
1281PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001282PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001283{
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001284 PyObject *copy;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001285
1286 if (o == NULL || !PyDict_Check(o)) {
1287 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001288 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001289 }
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001290 copy = PyDict_New();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001291 if (copy == NULL)
1292 return NULL;
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001293 if (PyDict_Merge(copy, o, 1) == 0)
1294 return copy;
1295 Py_DECREF(copy);
1296 return copy;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001297}
1298
Guido van Rossum4199fac1993-11-05 10:18:44 +00001299int
Tim Peters1f5871e2000-07-04 17:44:48 +00001300PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001301{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001302 if (mp == NULL || !PyDict_Check(mp)) {
1303 PyErr_BadInternalCall();
Jeremy Hylton7083bb72004-02-17 20:10:11 +00001304 return -1;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001305 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001306 return ((dictobject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001307}
1308
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001309PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001310PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001311{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001312 if (mp == NULL || !PyDict_Check(mp)) {
1313 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001314 return NULL;
1315 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001316 return dict_keys((dictobject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001317}
1318
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001319PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001320PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001321{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001322 if (mp == NULL || !PyDict_Check(mp)) {
1323 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001324 return NULL;
1325 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001326 return dict_values((dictobject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001327}
1328
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001329PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001330PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001331{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001332 if (mp == NULL || !PyDict_Check(mp)) {
1333 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001334 return NULL;
1335 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001336 return dict_items((dictobject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001337}
1338
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001339/* Subroutine which returns the smallest key in a for which b's value
1340 is different or absent. The value is returned too, through the
Tim Peters95bf9392001-05-10 08:32:44 +00001341 pval argument. Both are NULL if no key in a is found for which b's status
1342 differs. The refcounts on (and only on) non-NULL *pval and function return
1343 values must be decremented by the caller (characterize() increments them
1344 to ensure that mutating comparison and PyDict_GetItem calls can't delete
1345 them before the caller is done looking at them). */
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001346
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001347static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001348characterize(dictobject *a, dictobject *b, PyObject **pval)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001349{
Tim Peters95bf9392001-05-10 08:32:44 +00001350 PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */
1351 PyObject *aval = NULL; /* a[akey] */
Guido van Rossumb9324202001-01-18 00:39:02 +00001352 int i, cmp;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001353
Tim Petersafb6ae82001-06-04 21:00:21 +00001354 for (i = 0; i <= a->ma_mask; i++) {
Tim Peters95bf9392001-05-10 08:32:44 +00001355 PyObject *thiskey, *thisaval, *thisbval;
1356 if (a->ma_table[i].me_value == NULL)
1357 continue;
1358 thiskey = a->ma_table[i].me_key;
1359 Py_INCREF(thiskey); /* keep alive across compares */
1360 if (akey != NULL) {
1361 cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT);
1362 if (cmp < 0) {
1363 Py_DECREF(thiskey);
1364 goto Fail;
1365 }
1366 if (cmp > 0 ||
Tim Petersafb6ae82001-06-04 21:00:21 +00001367 i > a->ma_mask ||
Tim Peters95bf9392001-05-10 08:32:44 +00001368 a->ma_table[i].me_value == NULL)
1369 {
1370 /* Not the *smallest* a key; or maybe it is
1371 * but the compare shrunk the dict so we can't
1372 * find its associated value anymore; or
1373 * maybe it is but the compare deleted the
1374 * a[thiskey] entry.
1375 */
1376 Py_DECREF(thiskey);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001377 continue;
Guido van Rossumb9324202001-01-18 00:39:02 +00001378 }
Tim Peters95bf9392001-05-10 08:32:44 +00001379 }
1380
1381 /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */
1382 thisaval = a->ma_table[i].me_value;
1383 assert(thisaval);
1384 Py_INCREF(thisaval); /* keep alive */
1385 thisbval = PyDict_GetItem((PyObject *)b, thiskey);
1386 if (thisbval == NULL)
1387 cmp = 0;
1388 else {
1389 /* both dicts have thiskey: same values? */
1390 cmp = PyObject_RichCompareBool(
1391 thisaval, thisbval, Py_EQ);
1392 if (cmp < 0) {
1393 Py_DECREF(thiskey);
1394 Py_DECREF(thisaval);
1395 goto Fail;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001396 }
1397 }
Tim Peters95bf9392001-05-10 08:32:44 +00001398 if (cmp == 0) {
1399 /* New winner. */
1400 Py_XDECREF(akey);
1401 Py_XDECREF(aval);
1402 akey = thiskey;
1403 aval = thisaval;
1404 }
1405 else {
1406 Py_DECREF(thiskey);
1407 Py_DECREF(thisaval);
1408 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001409 }
Tim Peters95bf9392001-05-10 08:32:44 +00001410 *pval = aval;
1411 return akey;
1412
1413Fail:
1414 Py_XDECREF(akey);
1415 Py_XDECREF(aval);
1416 *pval = NULL;
1417 return NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001418}
1419
1420static int
Tim Peters1f5871e2000-07-04 17:44:48 +00001421dict_compare(dictobject *a, dictobject *b)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001422{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001423 PyObject *adiff, *bdiff, *aval, *bval;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001424 int res;
1425
1426 /* Compare lengths first */
1427 if (a->ma_used < b->ma_used)
1428 return -1; /* a is shorter */
1429 else if (a->ma_used > b->ma_used)
1430 return 1; /* b is shorter */
Tim Peters95bf9392001-05-10 08:32:44 +00001431
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001432 /* Same length -- check all keys */
Tim Peters95bf9392001-05-10 08:32:44 +00001433 bdiff = bval = NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001434 adiff = characterize(a, b, &aval);
Tim Peters95bf9392001-05-10 08:32:44 +00001435 if (adiff == NULL) {
1436 assert(!aval);
Tim Peters3918fb22001-05-10 18:58:31 +00001437 /* Either an error, or a is a subset with the same length so
Tim Peters95bf9392001-05-10 08:32:44 +00001438 * must be equal.
1439 */
1440 res = PyErr_Occurred() ? -1 : 0;
1441 goto Finished;
1442 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001443 bdiff = characterize(b, a, &bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001444 if (bdiff == NULL && PyErr_Occurred()) {
1445 assert(!bval);
1446 res = -1;
1447 goto Finished;
1448 }
1449 res = 0;
1450 if (bdiff) {
1451 /* bdiff == NULL "should be" impossible now, but perhaps
1452 * the last comparison done by the characterize() on a had
1453 * the side effect of making the dicts equal!
1454 */
1455 res = PyObject_Compare(adiff, bdiff);
1456 }
1457 if (res == 0 && bval != NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001458 res = PyObject_Compare(aval, bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001459
1460Finished:
1461 Py_XDECREF(adiff);
1462 Py_XDECREF(bdiff);
1463 Py_XDECREF(aval);
1464 Py_XDECREF(bval);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001465 return res;
1466}
1467
Tim Peterse63415e2001-05-08 04:38:29 +00001468/* Return 1 if dicts equal, 0 if not, -1 if error.
1469 * Gets out as soon as any difference is detected.
1470 * Uses only Py_EQ comparison.
1471 */
1472static int
1473dict_equal(dictobject *a, dictobject *b)
1474{
1475 int i;
1476
1477 if (a->ma_used != b->ma_used)
1478 /* can't be equal if # of entries differ */
1479 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001480
Tim Peterse63415e2001-05-08 04:38:29 +00001481 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Tim Petersafb6ae82001-06-04 21:00:21 +00001482 for (i = 0; i <= a->ma_mask; i++) {
Tim Peterse63415e2001-05-08 04:38:29 +00001483 PyObject *aval = a->ma_table[i].me_value;
1484 if (aval != NULL) {
1485 int cmp;
1486 PyObject *bval;
1487 PyObject *key = a->ma_table[i].me_key;
1488 /* temporarily bump aval's refcount to ensure it stays
1489 alive until we're done with it */
1490 Py_INCREF(aval);
1491 bval = PyDict_GetItem((PyObject *)b, key);
1492 if (bval == NULL) {
1493 Py_DECREF(aval);
1494 return 0;
1495 }
1496 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1497 Py_DECREF(aval);
1498 if (cmp <= 0) /* error or not equal */
1499 return cmp;
1500 }
1501 }
1502 return 1;
1503 }
1504
1505static PyObject *
1506dict_richcompare(PyObject *v, PyObject *w, int op)
1507{
1508 int cmp;
1509 PyObject *res;
1510
1511 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1512 res = Py_NotImplemented;
1513 }
1514 else if (op == Py_EQ || op == Py_NE) {
1515 cmp = dict_equal((dictobject *)v, (dictobject *)w);
1516 if (cmp < 0)
1517 return NULL;
1518 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1519 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001520 else
1521 res = Py_NotImplemented;
Tim Peterse63415e2001-05-08 04:38:29 +00001522 Py_INCREF(res);
1523 return res;
1524 }
1525
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001526static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001527dict_has_key(register dictobject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001528{
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001529 long hash;
1530 register long ok;
Tim Peters0ab085c2001-09-14 00:25:33 +00001531 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001532 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001533 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001534 if (hash == -1)
1535 return NULL;
1536 }
Tim Petersdea48ec2001-05-22 20:40:22 +00001537 ok = (mp->ma_lookup)(mp, key, hash)->me_value != NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00001538 return PyBool_FromLong(ok);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001539}
1540
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001541static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001542dict_get(register dictobject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001543{
1544 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001545 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001546 PyObject *val = NULL;
1547 long hash;
1548
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001549 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001550 return NULL;
1551
Tim Peters0ab085c2001-09-14 00:25:33 +00001552 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001553 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Barry Warsawc38c5da1997-10-06 17:49:20 +00001554 hash = PyObject_Hash(key);
1555 if (hash == -1)
1556 return NULL;
1557 }
Fred Drake1bff34a2000-08-31 19:31:38 +00001558 val = (mp->ma_lookup)(mp, key, hash)->me_value;
Barry Warsaw320ac331997-10-20 17:26:25 +00001559
Barry Warsawc38c5da1997-10-06 17:49:20 +00001560 if (val == NULL)
1561 val = failobj;
1562 Py_INCREF(val);
1563 return val;
1564}
1565
1566
1567static PyObject *
Guido van Rossum164452c2000-08-08 16:12:54 +00001568dict_setdefault(register dictobject *mp, PyObject *args)
1569{
1570 PyObject *key;
1571 PyObject *failobj = Py_None;
1572 PyObject *val = NULL;
1573 long hash;
1574
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001575 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
Guido van Rossum164452c2000-08-08 16:12:54 +00001576 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001577
Tim Peters0ab085c2001-09-14 00:25:33 +00001578 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001579 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossum164452c2000-08-08 16:12:54 +00001580 hash = PyObject_Hash(key);
1581 if (hash == -1)
1582 return NULL;
1583 }
Fred Drake1bff34a2000-08-31 19:31:38 +00001584 val = (mp->ma_lookup)(mp, key, hash)->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001585 if (val == NULL) {
1586 val = failobj;
1587 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1588 val = NULL;
1589 }
1590 Py_XINCREF(val);
1591 return val;
1592}
1593
1594
1595static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001596dict_clear(register dictobject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001597{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001598 PyDict_Clear((PyObject *)mp);
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001599 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001600}
1601
Guido van Rossumba6ab842000-12-12 22:02:18 +00001602static PyObject *
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001603dict_pop(dictobject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00001604{
1605 long hash;
1606 dictentry *ep;
1607 PyObject *old_value, *old_key;
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001608 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001609
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001610 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1611 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001612 if (mp->ma_used == 0) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001613 if (deflt) {
1614 Py_INCREF(deflt);
1615 return deflt;
1616 }
Guido van Rossume027d982002-04-12 15:11:59 +00001617 PyErr_SetString(PyExc_KeyError,
1618 "pop(): dictionary is empty");
1619 return NULL;
1620 }
1621 if (!PyString_CheckExact(key) ||
1622 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1623 hash = PyObject_Hash(key);
1624 if (hash == -1)
1625 return NULL;
1626 }
1627 ep = (mp->ma_lookup)(mp, key, hash);
1628 if (ep->me_value == NULL) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001629 if (deflt) {
1630 Py_INCREF(deflt);
1631 return deflt;
1632 }
Guido van Rossume027d982002-04-12 15:11:59 +00001633 PyErr_SetObject(PyExc_KeyError, key);
1634 return NULL;
1635 }
1636 old_key = ep->me_key;
1637 Py_INCREF(dummy);
1638 ep->me_key = dummy;
1639 old_value = ep->me_value;
1640 ep->me_value = NULL;
1641 mp->ma_used--;
1642 Py_DECREF(old_key);
1643 return old_value;
1644}
1645
1646static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001647dict_popitem(dictobject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001648{
1649 int i = 0;
1650 dictentry *ep;
1651 PyObject *res;
1652
Tim Petersf4b33f62001-06-02 05:42:29 +00001653 /* Allocate the result tuple before checking the size. Believe it
1654 * or not, this allocation could trigger a garbage collection which
1655 * could empty the dict, so if we checked the size first and that
1656 * happened, the result would be an infinite loop (searching for an
1657 * entry that no longer exists). Note that the usual popitem()
1658 * idiom is "while d: k, v = d.popitem()". so needing to throw the
1659 * tuple away if the dict *is* empty isn't a significant
1660 * inefficiency -- possible, but unlikely in practice.
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001661 */
1662 res = PyTuple_New(2);
1663 if (res == NULL)
1664 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001665 if (mp->ma_used == 0) {
1666 Py_DECREF(res);
1667 PyErr_SetString(PyExc_KeyError,
1668 "popitem(): dictionary is empty");
1669 return NULL;
1670 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001671 /* Set ep to "the first" dict entry with a value. We abuse the hash
1672 * field of slot 0 to hold a search finger:
1673 * If slot 0 has a value, use slot 0.
1674 * Else slot 0 is being used to hold a search finger,
1675 * and we use its hash value as the first index to look.
1676 */
1677 ep = &mp->ma_table[0];
1678 if (ep->me_value == NULL) {
1679 i = (int)ep->me_hash;
Tim Petersdea48ec2001-05-22 20:40:22 +00001680 /* The hash field may be a real hash value, or it may be a
1681 * legit search finger, or it may be a once-legit search
1682 * finger that's out of bounds now because it wrapped around
1683 * or the table shrunk -- simply make sure it's in bounds now.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001684 */
Tim Petersafb6ae82001-06-04 21:00:21 +00001685 if (i > mp->ma_mask || i < 1)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001686 i = 1; /* skip slot 0 */
1687 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1688 i++;
Tim Petersafb6ae82001-06-04 21:00:21 +00001689 if (i > mp->ma_mask)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001690 i = 1;
1691 }
1692 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001693 PyTuple_SET_ITEM(res, 0, ep->me_key);
1694 PyTuple_SET_ITEM(res, 1, ep->me_value);
1695 Py_INCREF(dummy);
1696 ep->me_key = dummy;
1697 ep->me_value = NULL;
1698 mp->ma_used--;
1699 assert(mp->ma_table[0].me_value == NULL);
1700 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001701 return res;
1702}
1703
Jeremy Hylton8caad492000-06-23 14:18:11 +00001704static int
1705dict_traverse(PyObject *op, visitproc visit, void *arg)
1706{
1707 int i = 0, err;
1708 PyObject *pk;
1709 PyObject *pv;
1710
1711 while (PyDict_Next(op, &i, &pk, &pv)) {
1712 err = visit(pk, arg);
1713 if (err)
1714 return err;
1715 err = visit(pv, arg);
1716 if (err)
1717 return err;
1718 }
1719 return 0;
1720}
1721
1722static int
1723dict_tp_clear(PyObject *op)
1724{
1725 PyDict_Clear(op);
1726 return 0;
1727}
1728
Tim Petersf7f88b12000-12-13 23:18:45 +00001729
Raymond Hettinger019a1482004-03-18 02:41:19 +00001730extern PyTypeObject PyDictIterKey_Type; /* Forward */
1731extern PyTypeObject PyDictIterValue_Type; /* Forward */
1732extern PyTypeObject PyDictIterItem_Type; /* Forward */
1733static PyObject *dictiter_new(dictobject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001734
1735static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001736dict_iterkeys(dictobject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001737{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001738 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001739}
1740
1741static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001742dict_itervalues(dictobject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001743{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001744 return dictiter_new(dict, &PyDictIterValue_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001745}
1746
1747static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001748dict_iteritems(dictobject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001749{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001750 return dictiter_new(dict, &PyDictIterItem_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001751}
1752
1753
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001754PyDoc_STRVAR(has_key__doc__,
Raymond Hettinger574aa322003-09-01 22:12:08 +00001755"D.has_key(k) -> True if D has a key k, else False");
Tim Petersf7f88b12000-12-13 23:18:45 +00001756
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001757PyDoc_STRVAR(contains__doc__,
1758"D.__contains__(k) -> True if D has a key k, else False");
1759
1760PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
1761
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001762PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001763"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001764
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001765PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001766"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 +00001767
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001768PyDoc_STRVAR(pop__doc__,
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001769"D.pop(k[,d]) -> v, remove specified key and return the corresponding value\n\
1770If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00001771
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001772PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00001773"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000017742-tuple; but raise KeyError if D is empty");
Tim Petersf7f88b12000-12-13 23:18:45 +00001775
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001776PyDoc_STRVAR(keys__doc__,
1777"D.keys() -> list of D's keys");
Tim Petersf7f88b12000-12-13 23:18:45 +00001778
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001779PyDoc_STRVAR(items__doc__,
1780"D.items() -> list of D's (key, value) pairs, as 2-tuples");
Tim Petersf7f88b12000-12-13 23:18:45 +00001781
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001782PyDoc_STRVAR(values__doc__,
1783"D.values() -> list of D's values");
Tim Petersf7f88b12000-12-13 23:18:45 +00001784
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001785PyDoc_STRVAR(update__doc__,
Walter Dörwaldd70ad8a2004-05-28 20:59:21 +00001786"D.update(E, **F) -> None. Update D from E and F: for k in E: D[k] = E[k]\n\
1787(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 +00001788
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001789PyDoc_STRVAR(fromkeys__doc__,
1790"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
1791v defaults to None.");
1792
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001793PyDoc_STRVAR(clear__doc__,
1794"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001795
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001796PyDoc_STRVAR(copy__doc__,
1797"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00001798
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001799PyDoc_STRVAR(iterkeys__doc__,
1800"D.iterkeys() -> an iterator over the keys of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00001801
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001802PyDoc_STRVAR(itervalues__doc__,
1803"D.itervalues() -> an iterator over the values of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00001804
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001805PyDoc_STRVAR(iteritems__doc__,
1806"D.iteritems() -> an iterator over the (key, value) items of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00001807
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001808static PyMethodDef mapp_methods[] = {
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001809 {"__contains__",(PyCFunction)dict_has_key, METH_O | METH_COEXIST,
1810 contains__doc__},
Raymond Hettinger0c669672003-12-13 13:31:55 +00001811 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001812 getitem__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001813 {"has_key", (PyCFunction)dict_has_key, METH_O,
Tim Petersf7f88b12000-12-13 23:18:45 +00001814 has_key__doc__},
1815 {"get", (PyCFunction)dict_get, METH_VARARGS,
1816 get__doc__},
1817 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
1818 setdefault_doc__},
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001819 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
Just van Rossuma797d812002-11-23 09:45:04 +00001820 pop__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001821 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001822 popitem__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001823 {"keys", (PyCFunction)dict_keys, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001824 keys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001825 {"items", (PyCFunction)dict_items, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001826 items__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001827 {"values", (PyCFunction)dict_values, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001828 values__doc__},
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001829 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001830 update__doc__},
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001831 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
1832 fromkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001833 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001834 clear__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001835 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001836 copy__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001837 {"iterkeys", (PyCFunction)dict_iterkeys, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00001838 iterkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001839 {"itervalues", (PyCFunction)dict_itervalues, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00001840 itervalues__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001841 {"iteritems", (PyCFunction)dict_iteritems, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00001842 iteritems__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00001843 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001844};
1845
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001846int
1847PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001848{
1849 long hash;
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001850 dictobject *mp = (dictobject *)op;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001851
Tim Peters0ab085c2001-09-14 00:25:33 +00001852 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001853 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001854 hash = PyObject_Hash(key);
1855 if (hash == -1)
1856 return -1;
1857 }
Tim Petersdea48ec2001-05-22 20:40:22 +00001858 return (mp->ma_lookup)(mp, key, hash)->me_value != NULL;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001859}
1860
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001861/* Hack to implement "key in dict" */
1862static PySequenceMethods dict_as_sequence = {
1863 0, /* sq_length */
1864 0, /* sq_concat */
1865 0, /* sq_repeat */
1866 0, /* sq_item */
1867 0, /* sq_slice */
1868 0, /* sq_ass_item */
1869 0, /* sq_ass_slice */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001870 (objobjproc)PyDict_Contains, /* sq_contains */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001871 0, /* sq_inplace_concat */
1872 0, /* sq_inplace_repeat */
1873};
1874
Guido van Rossum09e563a2001-05-01 12:10:21 +00001875static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001876dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1877{
1878 PyObject *self;
1879
1880 assert(type != NULL && type->tp_alloc != NULL);
1881 self = type->tp_alloc(type, 0);
1882 if (self != NULL) {
1883 PyDictObject *d = (PyDictObject *)self;
1884 /* It's guaranteed that tp->alloc zeroed out the struct. */
1885 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
1886 INIT_NONZERO_DICT_SLOTS(d);
1887 d->ma_lookup = lookdict_string;
1888#ifdef SHOW_CONVERSION_COUNTS
1889 ++created;
1890#endif
1891 }
1892 return self;
1893}
1894
Tim Peters25786c02001-09-02 08:22:48 +00001895static int
1896dict_init(PyObject *self, PyObject *args, PyObject *kwds)
1897{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001898 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00001899}
1900
Guido van Rossumdbb53d92001-12-03 16:32:18 +00001901static long
1902dict_nohash(PyObject *self)
1903{
1904 PyErr_SetString(PyExc_TypeError, "dict objects are unhashable");
1905 return -1;
1906}
1907
Tim Peters6d6c1a32001-08-02 04:15:00 +00001908static PyObject *
Guido van Rossum09e563a2001-05-01 12:10:21 +00001909dict_iter(dictobject *dict)
1910{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001911 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001912}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001913
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001914PyDoc_STRVAR(dictionary_doc,
Tim Petersa427a2b2001-10-29 22:25:45 +00001915"dict() -> new empty dictionary.\n"
1916"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Tim Peters1fc240e2001-10-26 05:06:50 +00001917" (key, value) pairs.\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00001918"dict(seq) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00001919" d = {}\n"
1920" for k, v in seq:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00001921" d[k] = v\n"
1922"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
1923" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00001924
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001925PyTypeObject PyDict_Type = {
1926 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001927 0,
Tim Petersa427a2b2001-10-29 22:25:45 +00001928 "dict",
Neil Schemenauere83c00e2001-08-29 23:54:21 +00001929 sizeof(dictobject),
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001930 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00001931 (destructor)dict_dealloc, /* tp_dealloc */
1932 (printfunc)dict_print, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001933 0, /* tp_getattr */
Guido van Rossumb9324202001-01-18 00:39:02 +00001934 0, /* tp_setattr */
Tim Peters4fa58bf2001-05-10 21:45:19 +00001935 (cmpfunc)dict_compare, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00001936 (reprfunc)dict_repr, /* tp_repr */
1937 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001938 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00001939 &dict_as_mapping, /* tp_as_mapping */
Guido van Rossumdbb53d92001-12-03 16:32:18 +00001940 dict_nohash, /* tp_hash */
Guido van Rossumb9324202001-01-18 00:39:02 +00001941 0, /* tp_call */
1942 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001943 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumb9324202001-01-18 00:39:02 +00001944 0, /* tp_setattro */
1945 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00001946 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Tim Peters6d6c1a32001-08-02 04:15:00 +00001947 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters25786c02001-09-02 08:22:48 +00001948 dictionary_doc, /* tp_doc */
Guido van Rossumb9324202001-01-18 00:39:02 +00001949 (traverseproc)dict_traverse, /* tp_traverse */
1950 (inquiry)dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00001951 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001952 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00001953 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001954 0, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001955 mapp_methods, /* tp_methods */
1956 0, /* tp_members */
1957 0, /* tp_getset */
1958 0, /* tp_base */
1959 0, /* tp_dict */
1960 0, /* tp_descr_get */
1961 0, /* tp_descr_set */
1962 0, /* tp_dictoffset */
Tim Peters25786c02001-09-02 08:22:48 +00001963 (initproc)dict_init, /* tp_init */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001964 PyType_GenericAlloc, /* tp_alloc */
1965 dict_new, /* tp_new */
Neil Schemenauer6189b892002-04-12 02:43:00 +00001966 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001967};
1968
Guido van Rossum3cca2451997-05-16 14:23:33 +00001969/* For backward compatibility with old dictionary interface */
1970
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001971PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00001972PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001973{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001974 PyObject *kv, *rv;
1975 kv = PyString_FromString(key);
1976 if (kv == NULL)
1977 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00001978 rv = PyDict_GetItem(v, kv);
1979 Py_DECREF(kv);
1980 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001981}
1982
1983int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00001984PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001985{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001986 PyObject *kv;
1987 int err;
1988 kv = PyString_FromString(key);
1989 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00001990 return -1;
Guido van Rossum4f3bf1e1997-09-29 23:31:11 +00001991 PyString_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00001992 err = PyDict_SetItem(v, kv, item);
1993 Py_DECREF(kv);
1994 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001995}
1996
1997int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00001998PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001999{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002000 PyObject *kv;
2001 int err;
2002 kv = PyString_FromString(key);
2003 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002004 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002005 err = PyDict_DelItem(v, kv);
2006 Py_DECREF(kv);
2007 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002008}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002009
Raymond Hettinger019a1482004-03-18 02:41:19 +00002010/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002011
2012typedef struct {
2013 PyObject_HEAD
Guido van Rossum2147df72002-07-16 20:30:22 +00002014 dictobject *di_dict; /* Set to NULL when iterator is exhausted */
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00002015 int di_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002016 int di_pos;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002017 PyObject* di_result; /* reusable result tuple for iteritems */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002018 long len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002019} dictiterobject;
2020
2021static PyObject *
Raymond Hettinger019a1482004-03-18 02:41:19 +00002022dictiter_new(dictobject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002023{
2024 dictiterobject *di;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002025 di = PyObject_New(dictiterobject, itertype);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002026 if (di == NULL)
2027 return NULL;
2028 Py_INCREF(dict);
2029 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00002030 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002031 di->di_pos = 0;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002032 di->len = dict->ma_used;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002033 if (itertype == &PyDictIterItem_Type) {
2034 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2035 if (di->di_result == NULL) {
2036 Py_DECREF(di);
2037 return NULL;
2038 }
2039 }
2040 else
2041 di->di_result = NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002042 return (PyObject *)di;
2043}
2044
2045static void
2046dictiter_dealloc(dictiterobject *di)
2047{
Guido van Rossum2147df72002-07-16 20:30:22 +00002048 Py_XDECREF(di->di_dict);
Raymond Hettinger019a1482004-03-18 02:41:19 +00002049 Py_XDECREF(di->di_result);
Neil Schemenauer6189b892002-04-12 02:43:00 +00002050 PyObject_Del(di);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002051}
2052
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002053static int
2054dictiter_len(dictiterobject *di)
2055{
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00002056 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
2057 return di->len;
2058 return 0;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002059}
2060
2061static PySequenceMethods dictiter_as_sequence = {
2062 (inquiry)dictiter_len, /* sq_length */
2063 0, /* sq_concat */
2064};
2065
Raymond Hettinger019a1482004-03-18 02:41:19 +00002066static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002067{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002068 PyObject *key;
2069 register int i, mask;
2070 register dictentry *ep;
2071 dictobject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002072
Raymond Hettinger019a1482004-03-18 02:41:19 +00002073 if (d == NULL)
Guido van Rossum2147df72002-07-16 20:30:22 +00002074 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002075 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002076
Raymond Hettinger019a1482004-03-18 02:41:19 +00002077 if (di->di_used != d->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00002078 PyErr_SetString(PyExc_RuntimeError,
2079 "dictionary changed size during iteration");
Guido van Rossum2147df72002-07-16 20:30:22 +00002080 di->di_used = -1; /* Make this state sticky */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002081 return NULL;
2082 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002083
Raymond Hettinger019a1482004-03-18 02:41:19 +00002084 i = di->di_pos;
2085 if (i < 0)
2086 goto fail;
2087 ep = d->ma_table;
2088 mask = d->ma_mask;
2089 while (i <= mask && ep[i].me_value == NULL)
2090 i++;
2091 di->di_pos = i+1;
2092 if (i > mask)
2093 goto fail;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002094 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002095 key = ep[i].me_key;
2096 Py_INCREF(key);
2097 return key;
2098
2099fail:
2100 Py_DECREF(d);
Guido van Rossum2147df72002-07-16 20:30:22 +00002101 di->di_dict = NULL;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002102 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002103}
2104
Raymond Hettinger019a1482004-03-18 02:41:19 +00002105PyTypeObject PyDictIterKey_Type = {
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002106 PyObject_HEAD_INIT(&PyType_Type)
2107 0, /* ob_size */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002108 "dictionary-keyiterator", /* tp_name */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002109 sizeof(dictiterobject), /* tp_basicsize */
2110 0, /* tp_itemsize */
2111 /* methods */
2112 (destructor)dictiter_dealloc, /* tp_dealloc */
2113 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002114 0, /* tp_getattr */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002115 0, /* tp_setattr */
2116 0, /* tp_compare */
2117 0, /* tp_repr */
2118 0, /* tp_as_number */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002119 &dictiter_as_sequence, /* tp_as_sequence */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002120 0, /* tp_as_mapping */
2121 0, /* tp_hash */
2122 0, /* tp_call */
2123 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002124 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002125 0, /* tp_setattro */
2126 0, /* tp_as_buffer */
2127 Py_TPFLAGS_DEFAULT, /* tp_flags */
2128 0, /* tp_doc */
2129 0, /* tp_traverse */
2130 0, /* tp_clear */
2131 0, /* tp_richcompare */
2132 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002133 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002134 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
2135};
2136
2137static PyObject *dictiter_iternextvalue(dictiterobject *di)
2138{
2139 PyObject *value;
2140 register int i, mask;
2141 register dictentry *ep;
2142 dictobject *d = di->di_dict;
2143
2144 if (d == NULL)
2145 return NULL;
2146 assert (PyDict_Check(d));
2147
2148 if (di->di_used != d->ma_used) {
2149 PyErr_SetString(PyExc_RuntimeError,
2150 "dictionary changed size during iteration");
2151 di->di_used = -1; /* Make this state sticky */
2152 return NULL;
2153 }
2154
2155 i = di->di_pos;
Guido van Rossum09240f62004-03-20 19:11:58 +00002156 mask = d->ma_mask;
2157 if (i < 0 || i > mask)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002158 goto fail;
2159 ep = d->ma_table;
Guido van Rossum09240f62004-03-20 19:11:58 +00002160 while ((value=ep[i].me_value) == NULL) {
Raymond Hettinger019a1482004-03-18 02:41:19 +00002161 i++;
Guido van Rossum09240f62004-03-20 19:11:58 +00002162 if (i > mask)
2163 goto fail;
2164 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002165 di->di_pos = i+1;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002166 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002167 Py_INCREF(value);
2168 return value;
2169
2170fail:
2171 Py_DECREF(d);
2172 di->di_dict = NULL;
2173 return NULL;
2174}
2175
2176PyTypeObject PyDictIterValue_Type = {
2177 PyObject_HEAD_INIT(&PyType_Type)
2178 0, /* ob_size */
2179 "dictionary-valueiterator", /* tp_name */
2180 sizeof(dictiterobject), /* tp_basicsize */
2181 0, /* tp_itemsize */
2182 /* methods */
2183 (destructor)dictiter_dealloc, /* tp_dealloc */
2184 0, /* tp_print */
2185 0, /* tp_getattr */
2186 0, /* tp_setattr */
2187 0, /* tp_compare */
2188 0, /* tp_repr */
2189 0, /* tp_as_number */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002190 &dictiter_as_sequence, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002191 0, /* tp_as_mapping */
2192 0, /* tp_hash */
2193 0, /* tp_call */
2194 0, /* tp_str */
2195 PyObject_GenericGetAttr, /* tp_getattro */
2196 0, /* tp_setattro */
2197 0, /* tp_as_buffer */
2198 Py_TPFLAGS_DEFAULT, /* tp_flags */
2199 0, /* tp_doc */
2200 0, /* tp_traverse */
2201 0, /* tp_clear */
2202 0, /* tp_richcompare */
2203 0, /* tp_weaklistoffset */
2204 PyObject_SelfIter, /* tp_iter */
2205 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
2206};
2207
2208static PyObject *dictiter_iternextitem(dictiterobject *di)
2209{
2210 PyObject *key, *value, *result = di->di_result;
2211 register int i, mask;
2212 register dictentry *ep;
2213 dictobject *d = di->di_dict;
2214
2215 if (d == NULL)
2216 return NULL;
2217 assert (PyDict_Check(d));
2218
2219 if (di->di_used != d->ma_used) {
2220 PyErr_SetString(PyExc_RuntimeError,
2221 "dictionary changed size during iteration");
2222 di->di_used = -1; /* Make this state sticky */
2223 return NULL;
2224 }
2225
2226 i = di->di_pos;
2227 if (i < 0)
2228 goto fail;
2229 ep = d->ma_table;
2230 mask = d->ma_mask;
2231 while (i <= mask && ep[i].me_value == NULL)
2232 i++;
2233 di->di_pos = i+1;
2234 if (i > mask)
2235 goto fail;
2236
2237 if (result->ob_refcnt == 1) {
2238 Py_INCREF(result);
2239 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2240 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2241 } else {
2242 result = PyTuple_New(2);
2243 if (result == NULL)
2244 return NULL;
2245 }
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002246 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002247 key = ep[i].me_key;
2248 value = ep[i].me_value;
2249 Py_INCREF(key);
2250 Py_INCREF(value);
2251 PyTuple_SET_ITEM(result, 0, key);
2252 PyTuple_SET_ITEM(result, 1, value);
2253 return result;
2254
2255fail:
2256 Py_DECREF(d);
2257 di->di_dict = NULL;
2258 return NULL;
2259}
2260
2261PyTypeObject PyDictIterItem_Type = {
2262 PyObject_HEAD_INIT(&PyType_Type)
2263 0, /* ob_size */
2264 "dictionary-itemiterator", /* tp_name */
2265 sizeof(dictiterobject), /* tp_basicsize */
2266 0, /* tp_itemsize */
2267 /* methods */
2268 (destructor)dictiter_dealloc, /* tp_dealloc */
2269 0, /* tp_print */
2270 0, /* tp_getattr */
2271 0, /* tp_setattr */
2272 0, /* tp_compare */
2273 0, /* tp_repr */
2274 0, /* tp_as_number */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002275 &dictiter_as_sequence, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002276 0, /* tp_as_mapping */
2277 0, /* tp_hash */
2278 0, /* tp_call */
2279 0, /* tp_str */
2280 PyObject_GenericGetAttr, /* tp_getattro */
2281 0, /* tp_setattro */
2282 0, /* tp_as_buffer */
2283 Py_TPFLAGS_DEFAULT, /* tp_flags */
2284 0, /* tp_doc */
2285 0, /* tp_traverse */
2286 0, /* tp_clear */
2287 0, /* tp_richcompare */
2288 0, /* tp_weaklistoffset */
2289 PyObject_SelfIter, /* tp_iter */
2290 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002291};