blob: 1fcfe1cc34907febf92e91761346ce9a2b56a880 [file] [log] [blame]
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001
Guido van Rossum2bc13791999-03-24 19:06:42 +00002/* Dictionary object implementation using a hash table */
Guido van Rossum9bfef441993-03-29 10:43:31 +00003
Raymond Hettinger930427b2003-05-03 06:51:59 +00004/* The distribution includes a separate file, Objects/dictnotes.txt,
Tim Peters60b29962006-01-01 01:19:23 +00005 describing explorations into dictionary design and optimization.
Raymond Hettinger930427b2003-05-03 06:51:59 +00006 It covers typical dictionary use patterns, the parameters for
7 tuning dictionaries, and several ideas for possible optimizations.
8*/
9
Guido van Rossumc0b618a1997-05-02 03:12:38 +000010#include "Python.h"
Guido van Rossum4b1302b1993-03-27 18:11:32 +000011
Tim Peters6d6c1a32001-08-02 04:15:00 +000012typedef PyDictEntry dictentry;
13typedef PyDictObject dictobject;
Guido van Rossum16e93a81997-01-28 00:00:11 +000014
Georg Brandlb9f4ad32006-10-29 18:31:42 +000015/* Set a key error with the specified argument, wrapping it in a
16 * tuple automatically so that tuple keys are not unpacked as the
17 * exception arguments. */
18static void
19set_key_error(PyObject *arg)
20{
21 PyObject *tup;
22 tup = PyTuple_Pack(1, arg);
23 if (!tup)
24 return; /* caller will expect error to be set anyway */
25 PyErr_SetObject(PyExc_KeyError, tup);
26}
27
Tim Peterseb28ef22001-06-02 05:27:19 +000028/* Define this out if you don't want conversion statistics on exit. */
Fred Drake1bff34a2000-08-31 19:31:38 +000029#undef SHOW_CONVERSION_COUNTS
30
Tim Peterseb28ef22001-06-02 05:27:19 +000031/* See large comment block below. This must be >= 1. */
32#define PERTURB_SHIFT 5
33
Guido van Rossum16e93a81997-01-28 00:00:11 +000034/*
Tim Peterseb28ef22001-06-02 05:27:19 +000035Major subtleties ahead: Most hash schemes depend on having a "good" hash
36function, in the sense of simulating randomness. Python doesn't: its most
37important hash functions (for strings and ints) are very regular in common
38cases:
Tim Peters15d49292001-05-27 07:39:22 +000039
Tim Peterseb28ef22001-06-02 05:27:19 +000040>>> map(hash, (0, 1, 2, 3))
41[0, 1, 2, 3]
42>>> map(hash, ("namea", "nameb", "namec", "named"))
43[-1658398457, -1658398460, -1658398459, -1658398462]
44>>>
Tim Peters15d49292001-05-27 07:39:22 +000045
Tim Peterseb28ef22001-06-02 05:27:19 +000046This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
47the low-order i bits as the initial table index is extremely fast, and there
48are no collisions at all for dicts indexed by a contiguous range of ints.
49The same is approximately true when keys are "consecutive" strings. So this
50gives better-than-random behavior in common cases, and that's very desirable.
Tim Peters15d49292001-05-27 07:39:22 +000051
Tim Peterseb28ef22001-06-02 05:27:19 +000052OTOH, when collisions occur, the tendency to fill contiguous slices of the
53hash table makes a good collision resolution strategy crucial. Taking only
54the last i bits of the hash code is also vulnerable: for example, consider
55[i << 16 for i in range(20000)] as a set of keys. Since ints are their own
56hash codes, and this fits in a dict of size 2**15, the last 15 bits of every
57hash code are all 0: they *all* map to the same table index.
Tim Peters15d49292001-05-27 07:39:22 +000058
Tim Peterseb28ef22001-06-02 05:27:19 +000059But catering to unusual cases should not slow the usual ones, so we just take
60the last i bits anyway. It's up to collision resolution to do the rest. If
61we *usually* find the key we're looking for on the first try (and, it turns
62out, we usually do -- the table load factor is kept under 2/3, so the odds
63are solidly in our favor), then it makes best sense to keep the initial index
64computation dirt cheap.
Tim Peters15d49292001-05-27 07:39:22 +000065
Tim Peterseb28ef22001-06-02 05:27:19 +000066The first half of collision resolution is to visit table indices via this
67recurrence:
Tim Peters15d49292001-05-27 07:39:22 +000068
Tim Peterseb28ef22001-06-02 05:27:19 +000069 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +000070
Tim Peterseb28ef22001-06-02 05:27:19 +000071For any initial j in range(2**i), repeating that 2**i times generates each
72int in range(2**i) exactly once (see any text on random-number generation for
73proof). By itself, this doesn't help much: like linear probing (setting
74j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
75order. This would be bad, except that's not the only thing we do, and it's
76actually *good* in the common cases where hash keys are consecutive. In an
77example that's really too small to make this entirely clear, for a table of
78size 2**3 the order of indices is:
Tim Peters15d49292001-05-27 07:39:22 +000079
Tim Peterseb28ef22001-06-02 05:27:19 +000080 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
81
82If two things come in at index 5, the first place we look after is index 2,
83not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
84Linear probing is deadly in this case because there the fixed probe order
85is the *same* as the order consecutive keys are likely to arrive. But it's
86extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
87and certain that consecutive hash codes do not.
88
89The other half of the strategy is to get the other bits of the hash code
90into play. This is done by initializing a (unsigned) vrbl "perturb" to the
91full hash code, and changing the recurrence to:
92
93 j = (5*j) + 1 + perturb;
94 perturb >>= PERTURB_SHIFT;
95 use j % 2**i as the next table index;
96
97Now the probe sequence depends (eventually) on every bit in the hash code,
98and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
99because it quickly magnifies small differences in the bits that didn't affect
100the initial index. Note that because perturb is unsigned, if the recurrence
101is executed often enough perturb eventually becomes and remains 0. At that
102point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
103that's certain to find an empty slot eventually (since it generates every int
104in range(2**i), and we make sure there's always at least one empty slot).
105
106Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
107small so that the high bits of the hash code continue to affect the probe
108sequence across iterations; but you want it large so that in really bad cases
109the high-order hash bits have an effect on early iterations. 5 was "the
110best" in minimizing total collisions across experiments Tim Peters ran (on
111both normal and pathological cases), but 4 and 6 weren't significantly worse.
112
113Historical: Reimer Behrends contributed the idea of using a polynomial-based
114approach, using repeated multiplication by x in GF(2**n) where an irreducible
115polynomial for each table size was chosen such that x was a primitive root.
116Christian Tismer later extended that to use division by x instead, as an
117efficient way to get the high bits of the hash code into play. This scheme
118also gave excellent collision statistics, but was more expensive: two
119if-tests were required inside the loop; computing "the next" index took about
120the same number of operations but without as much potential parallelism
121(e.g., computing 5*j can go on at the same time as computing 1+perturb in the
122above, and then shifting perturb can be done while the table index is being
123masked); and the dictobject struct required a member to hold the table's
124polynomial. In Tim's experiments the current scheme ran faster, produced
125equally good collision statistics, needed less code & used less memory.
Tim Peters9b10f7e2006-05-30 04:16:25 +0000126
127Theoretical Python 2.5 headache: hash codes are only C "long", but
128sizeof(Py_ssize_t) > sizeof(long) may be possible. In that case, and if a
129dict is genuinely huge, then only the slots directly reachable via indexing
130by a C long can be the first slot in a probe sequence. The probe sequence
131will still eventually reach every slot in the table, but the collision rate
132on initial probes may be much higher than this scheme was designed for.
133Getting a hash code as fat as Py_ssize_t is the only real cure. But in
134practice, this probably won't make a lick of difference for many years (at
135which point everyone will have terabytes of RAM on 64-bit boxes).
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000136*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000137
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000138/* Object used as dummy key to fill deleted entries */
Raymond Hettingerf81e4502005-08-17 02:19:36 +0000139static PyObject *dummy = NULL; /* Initialized by first call to newdictobject() */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000140
Armin Rigoe1709372006-04-12 17:06:05 +0000141#ifdef Py_REF_DEBUG
142PyObject *
143_PyDict_Dummy(void)
144{
145 return dummy;
146}
147#endif
148
Fred Drake1bff34a2000-08-31 19:31:38 +0000149/* forward declarations */
150static dictentry *
151lookdict_string(dictobject *mp, PyObject *key, long hash);
152
153#ifdef SHOW_CONVERSION_COUNTS
154static long created = 0L;
155static long converted = 0L;
156
157static void
158show_counts(void)
159{
160 fprintf(stderr, "created %ld string dicts\n", created);
161 fprintf(stderr, "converted %ld to normal dicts\n", converted);
162 fprintf(stderr, "%.2f%% conversion rate\n", (100.0*converted)/created);
163}
164#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000165
Tim Peters6d6c1a32001-08-02 04:15:00 +0000166/* Initialization macros.
167 There are two ways to create a dict: PyDict_New() is the main C API
168 function, and the tp_new slot maps to dict_new(). In the latter case we
169 can save a little time over what PyDict_New does because it's guaranteed
170 that the PyDictObject struct is already zeroed out.
171 Everyone except dict_new() should use EMPTY_TO_MINSIZE (unless they have
172 an excellent reason not to).
173*/
174
175#define INIT_NONZERO_DICT_SLOTS(mp) do { \
Tim Petersdea48ec2001-05-22 20:40:22 +0000176 (mp)->ma_table = (mp)->ma_smalltable; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000177 (mp)->ma_mask = PyDict_MINSIZE - 1; \
178 } while(0)
179
180#define EMPTY_TO_MINSIZE(mp) do { \
181 memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000182 (mp)->ma_used = (mp)->ma_fill = 0; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000183 INIT_NONZERO_DICT_SLOTS(mp); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000184 } while(0)
185
Raymond Hettinger43442782004-03-17 21:55:03 +0000186/* Dictionary reuse scheme to save calls to malloc, free, and memset */
187#define MAXFREEDICTS 80
188static PyDictObject *free_dicts[MAXFREEDICTS];
189static int num_free_dicts = 0;
190
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000191PyObject *
Thomas Wouters78890102000-07-22 19:25:51 +0000192PyDict_New(void)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000193{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000194 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000195 if (dummy == NULL) { /* Auto-initialize dummy */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000196 dummy = PyString_FromString("<dummy key>");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000197 if (dummy == NULL)
198 return NULL;
Fred Drake1bff34a2000-08-31 19:31:38 +0000199#ifdef SHOW_CONVERSION_COUNTS
200 Py_AtExit(show_counts);
201#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000202 }
Raymond Hettinger43442782004-03-17 21:55:03 +0000203 if (num_free_dicts) {
204 mp = free_dicts[--num_free_dicts];
205 assert (mp != NULL);
206 assert (mp->ob_type == &PyDict_Type);
207 _Py_NewReference((PyObject *)mp);
208 if (mp->ma_fill) {
209 EMPTY_TO_MINSIZE(mp);
210 }
211 assert (mp->ma_used == 0);
212 assert (mp->ma_table == mp->ma_smalltable);
213 assert (mp->ma_mask == PyDict_MINSIZE - 1);
214 } else {
215 mp = PyObject_GC_New(dictobject, &PyDict_Type);
216 if (mp == NULL)
217 return NULL;
218 EMPTY_TO_MINSIZE(mp);
219 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000220 mp->ma_lookup = lookdict_string;
221#ifdef SHOW_CONVERSION_COUNTS
222 ++created;
223#endif
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000224 _PyObject_GC_TRACK(mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000225 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000226}
227
228/*
229The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000230This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000231Open addressing is preferred over chaining since the link overhead for
232chaining would be substantial (100% with typical malloc overhead).
233
Tim Peterseb28ef22001-06-02 05:27:19 +0000234The initial probe index is computed as hash mod the table size. Subsequent
235probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000236
237All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000238
Tim Peterseb28ef22001-06-02 05:27:19 +0000239(The details in this version are due to Tim Peters, building on many past
240contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
241Christian Tismer).
Fred Drake1bff34a2000-08-31 19:31:38 +0000242
Tim Petersd770ebd2006-06-01 15:50:44 +0000243lookdict() is general-purpose, and may return NULL if (and only if) a
244comparison raises an exception (this was new in Python 2.5).
245lookdict_string() below is specialized to string keys, comparison of which can
246never raise an exception; that function can never return NULL. For both, when
247the key isn't found a dictentry* is returned for which the me_value field is
248NULL; this is the slot in the dict at which the key would have been found, and
249the caller can (if it wishes) add the <key, value> pair to the returned
250dictentry*.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000251*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000252static dictentry *
Tim Peters1f5871e2000-07-04 17:44:48 +0000253lookdict(dictobject *mp, PyObject *key, register long hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000254{
Tim Petersd770ebd2006-06-01 15:50:44 +0000255 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000256 register size_t perturb;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000257 register dictentry *freeslot;
Tim Petersd770ebd2006-06-01 15:50:44 +0000258 register size_t mask = (size_t)mp->ma_mask;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000259 dictentry *ep0 = mp->ma_table;
260 register dictentry *ep;
Fred Drakec88b99c2000-08-31 19:04:07 +0000261 register int cmp;
Tim Peters453163d2001-06-03 04:54:32 +0000262 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000263
Tim Petersd770ebd2006-06-01 15:50:44 +0000264 i = (size_t)hash & mask;
Guido van Rossumefb46091997-01-29 15:53:56 +0000265 ep = &ep0[i];
Guido van Rossumc1c7b1a1998-10-06 16:01:14 +0000266 if (ep->me_key == NULL || ep->me_key == key)
Guido van Rossum7d181591997-01-16 21:06:45 +0000267 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000268
Guido van Rossum16e93a81997-01-28 00:00:11 +0000269 if (ep->me_key == dummy)
Guido van Rossum7d181591997-01-16 21:06:45 +0000270 freeslot = ep;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000271 else {
Fred Drakec88b99c2000-08-31 19:04:07 +0000272 if (ep->me_hash == hash) {
Tim Peters453163d2001-06-03 04:54:32 +0000273 startkey = ep->me_key;
274 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000275 if (cmp < 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000276 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000277 if (ep0 == mp->ma_table && ep->me_key == startkey) {
278 if (cmp > 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000279 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000280 }
281 else {
282 /* The compare did major nasty stuff to the
283 * dict: start over.
284 * XXX A clever adversary could prevent this
285 * XXX from terminating.
286 */
Armin Rigo35f6d362006-06-01 13:19:12 +0000287 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000288 }
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000289 }
290 freeslot = NULL;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000291 }
Tim Peters15d49292001-05-27 07:39:22 +0000292
Tim Peters342c65e2001-05-13 06:43:53 +0000293 /* In the loop, me_key == dummy is by far (factor of 100s) the
294 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000295 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
296 i = (i << 2) + i + perturb + 1;
297 ep = &ep0[i & mask];
Armin Rigo35f6d362006-06-01 13:19:12 +0000298 if (ep->me_key == NULL)
299 return freeslot == NULL ? ep : freeslot;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000300 if (ep->me_key == key)
Armin Rigo35f6d362006-06-01 13:19:12 +0000301 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000302 if (ep->me_hash == hash && ep->me_key != dummy) {
Tim Peters453163d2001-06-03 04:54:32 +0000303 startkey = ep->me_key;
304 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000305 if (cmp < 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000306 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000307 if (ep0 == mp->ma_table && ep->me_key == startkey) {
308 if (cmp > 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000309 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000310 }
311 else {
312 /* The compare did major nasty stuff to the
313 * dict: start over.
314 * XXX A clever adversary could prevent this
315 * XXX from terminating.
316 */
Armin Rigo35f6d362006-06-01 13:19:12 +0000317 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000318 }
Guido van Rossum16e93a81997-01-28 00:00:11 +0000319 }
Tim Peters342c65e2001-05-13 06:43:53 +0000320 else if (ep->me_key == dummy && freeslot == NULL)
321 freeslot = ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000322 }
Neal Norwitza5ccda92006-10-28 21:16:54 +0000323 assert(0); /* NOT REACHED */
324 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000325}
326
327/*
Fred Drake1bff34a2000-08-31 19:31:38 +0000328 * Hacked up version of lookdict which can assume keys are always strings;
Tim Petersd770ebd2006-06-01 15:50:44 +0000329 * this assumption allows testing for errors during PyObject_RichCompareBool()
330 * to be dropped; string-string comparisons never raise exceptions. This also
331 * means we don't need to go through PyObject_RichCompareBool(); we can always
332 * use _PyString_Eq() directly.
Fred Drake1bff34a2000-08-31 19:31:38 +0000333 *
Tim Petersd770ebd2006-06-01 15:50:44 +0000334 * This is valuable because dicts with only string keys are very common.
Fred Drake1bff34a2000-08-31 19:31:38 +0000335 */
336static dictentry *
337lookdict_string(dictobject *mp, PyObject *key, register long hash)
338{
Tim Petersd770ebd2006-06-01 15:50:44 +0000339 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000340 register size_t perturb;
Fred Drake1bff34a2000-08-31 19:31:38 +0000341 register dictentry *freeslot;
Tim Petersd770ebd2006-06-01 15:50:44 +0000342 register size_t mask = (size_t)mp->ma_mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000343 dictentry *ep0 = mp->ma_table;
344 register dictentry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000345
Tim Peters0ab085c2001-09-14 00:25:33 +0000346 /* Make sure this function doesn't have to handle non-string keys,
347 including subclasses of str; e.g., one reason to subclass
348 strings is to override __eq__, and for speed we don't cater to
349 that here. */
350 if (!PyString_CheckExact(key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000351#ifdef SHOW_CONVERSION_COUNTS
352 ++converted;
353#endif
354 mp->ma_lookup = lookdict;
355 return lookdict(mp, key, hash);
356 }
Tim Peters2f228e72001-05-13 00:19:31 +0000357 i = hash & mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000358 ep = &ep0[i];
359 if (ep->me_key == NULL || ep->me_key == key)
360 return ep;
361 if (ep->me_key == dummy)
362 freeslot = ep;
363 else {
Tim Petersd770ebd2006-06-01 15:50:44 +0000364 if (ep->me_hash == hash && _PyString_Eq(ep->me_key, key))
Fred Drake1bff34a2000-08-31 19:31:38 +0000365 return ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000366 freeslot = NULL;
367 }
Tim Peters15d49292001-05-27 07:39:22 +0000368
Tim Peters342c65e2001-05-13 06:43:53 +0000369 /* In the loop, me_key == dummy is by far (factor of 100s) the
370 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000371 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
372 i = (i << 2) + i + perturb + 1;
373 ep = &ep0[i & mask];
Tim Peters342c65e2001-05-13 06:43:53 +0000374 if (ep->me_key == NULL)
375 return freeslot == NULL ? ep : freeslot;
376 if (ep->me_key == key
377 || (ep->me_hash == hash
378 && ep->me_key != dummy
Martin v. Löwiscd353062001-05-24 16:56:35 +0000379 && _PyString_Eq(ep->me_key, key)))
Fred Drake1bff34a2000-08-31 19:31:38 +0000380 return ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000381 if (ep->me_key == dummy && freeslot == NULL)
Tim Peters342c65e2001-05-13 06:43:53 +0000382 freeslot = ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000383 }
Neal Norwitza5ccda92006-10-28 21:16:54 +0000384 assert(0); /* NOT REACHED */
385 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000386}
387
388/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000389Internal routine to insert a new item into the table.
390Used both by the internal resize routine and by the public insert routine.
391Eats a reference to key and one to value.
Tim Petersd770ebd2006-06-01 15:50:44 +0000392Returns -1 if an error occurred, or 0 on success.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000393*/
Armin Rigo35f6d362006-06-01 13:19:12 +0000394static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000395insertdict(register dictobject *mp, PyObject *key, long hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000396{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000397 PyObject *old_value;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000398 register dictentry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000399 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
400
401 assert(mp->ma_lookup != NULL);
402 ep = mp->ma_lookup(mp, key, hash);
Armin Rigo35f6d362006-06-01 13:19:12 +0000403 if (ep == NULL) {
404 Py_DECREF(key);
405 Py_DECREF(value);
406 return -1;
407 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000408 if (ep->me_value != NULL) {
Guido van Rossumd7047b31995-01-02 19:07:15 +0000409 old_value = ep->me_value;
410 ep->me_value = value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000411 Py_DECREF(old_value); /* which **CAN** re-enter */
412 Py_DECREF(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000413 }
414 else {
415 if (ep->me_key == NULL)
416 mp->ma_fill++;
Raymond Hettinger07ead172005-02-05 23:42:57 +0000417 else {
418 assert(ep->me_key == dummy);
419 Py_DECREF(dummy);
420 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000421 ep->me_key = key;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000422 ep->me_hash = (Py_ssize_t)hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000423 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000424 mp->ma_used++;
425 }
Armin Rigo35f6d362006-06-01 13:19:12 +0000426 return 0;
427}
428
429/*
430Internal routine used by dictresize() to insert an item which is
431known to be absent from the dict. This routine also assumes that
432the dict contains no deleted entries. Besides the performance benefit,
433using insertdict() in dictresize() is dangerous (SF bug #1456209).
Tim Petersd770ebd2006-06-01 15:50:44 +0000434Note that no refcounts are changed by this routine; if needed, the caller
435is responsible for incref'ing `key` and `value`.
Armin Rigo35f6d362006-06-01 13:19:12 +0000436*/
437static void
438insertdict_clean(register dictobject *mp, PyObject *key, long hash,
439 PyObject *value)
440{
Tim Petersd770ebd2006-06-01 15:50:44 +0000441 register size_t i;
Armin Rigo35f6d362006-06-01 13:19:12 +0000442 register size_t perturb;
Tim Petersd770ebd2006-06-01 15:50:44 +0000443 register size_t mask = (size_t)mp->ma_mask;
Armin Rigo35f6d362006-06-01 13:19:12 +0000444 dictentry *ep0 = mp->ma_table;
445 register dictentry *ep;
446
447 i = hash & mask;
448 ep = &ep0[i];
449 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
450 i = (i << 2) + i + perturb + 1;
451 ep = &ep0[i & mask];
452 }
Tim Petersd770ebd2006-06-01 15:50:44 +0000453 assert(ep->me_value == NULL);
Armin Rigo35f6d362006-06-01 13:19:12 +0000454 mp->ma_fill++;
455 ep->me_key = key;
456 ep->me_hash = (Py_ssize_t)hash;
457 ep->me_value = value;
458 mp->ma_used++;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000459}
460
461/*
462Restructure the table by allocating a new table and reinserting all
463items again. When entries have been deleted, the new table may
464actually be smaller than the old one.
465*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000466static int
Tim Peters9b10f7e2006-05-30 04:16:25 +0000467dictresize(dictobject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000468{
Tim Peters9b10f7e2006-05-30 04:16:25 +0000469 Py_ssize_t newsize;
Tim Peters0c6010b2001-05-23 23:33:57 +0000470 dictentry *oldtable, *newtable, *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000471 Py_ssize_t i;
Tim Peters0c6010b2001-05-23 23:33:57 +0000472 int is_oldtable_malloced;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000473 dictentry small_copy[PyDict_MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000474
475 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000476
Tim Peterseb28ef22001-06-02 05:27:19 +0000477 /* Find the smallest table size > minused. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000478 for (newsize = PyDict_MINSIZE;
Tim Peterseb28ef22001-06-02 05:27:19 +0000479 newsize <= minused && newsize > 0;
480 newsize <<= 1)
481 ;
482 if (newsize <= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000483 PyErr_NoMemory();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000484 return -1;
485 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000486
487 /* Get space for a new table. */
488 oldtable = mp->ma_table;
489 assert(oldtable != NULL);
490 is_oldtable_malloced = oldtable != mp->ma_smalltable;
491
Tim Peters6d6c1a32001-08-02 04:15:00 +0000492 if (newsize == PyDict_MINSIZE) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000493 /* A large table is shrinking, or we can't get any smaller. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000494 newtable = mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000495 if (newtable == oldtable) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000496 if (mp->ma_fill == mp->ma_used) {
497 /* No dummies, so no point doing anything. */
Tim Peters0c6010b2001-05-23 23:33:57 +0000498 return 0;
Tim Petersf8a548c2001-05-24 16:26:40 +0000499 }
500 /* We're not going to resize it, but rebuild the
501 table anyway to purge old dummy entries.
502 Subtle: This is *necessary* if fill==size,
503 as lookdict needs at least one virgin slot to
504 terminate failing searches. If fill < size, it's
505 merely desirable, as dummies slow searches. */
506 assert(mp->ma_fill > mp->ma_used);
Tim Peters0c6010b2001-05-23 23:33:57 +0000507 memcpy(small_copy, oldtable, sizeof(small_copy));
508 oldtable = small_copy;
509 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000510 }
511 else {
512 newtable = PyMem_NEW(dictentry, newsize);
513 if (newtable == NULL) {
514 PyErr_NoMemory();
515 return -1;
516 }
517 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000518
519 /* Make the dict empty, using the new table. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000520 assert(newtable != oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000521 mp->ma_table = newtable;
Tim Petersafb6ae82001-06-04 21:00:21 +0000522 mp->ma_mask = newsize - 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000523 memset(newtable, 0, sizeof(dictentry) * newsize);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000524 mp->ma_used = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000525 i = mp->ma_fill;
526 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000527
Tim Peters19283142001-05-17 22:25:34 +0000528 /* Copy the data over; this is refcount-neutral for active entries;
529 dummy entries aren't copied over, of course */
Tim Petersdea48ec2001-05-22 20:40:22 +0000530 for (ep = oldtable; i > 0; ep++) {
531 if (ep->me_value != NULL) { /* active entry */
532 --i;
Armin Rigo35f6d362006-06-01 13:19:12 +0000533 insertdict_clean(mp, ep->me_key, (long)ep->me_hash,
534 ep->me_value);
Tim Petersdea48ec2001-05-22 20:40:22 +0000535 }
Tim Peters19283142001-05-17 22:25:34 +0000536 else if (ep->me_key != NULL) { /* dummy entry */
Tim Petersdea48ec2001-05-22 20:40:22 +0000537 --i;
Tim Peters19283142001-05-17 22:25:34 +0000538 assert(ep->me_key == dummy);
539 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000540 }
Tim Peters19283142001-05-17 22:25:34 +0000541 /* else key == value == NULL: nothing to do */
Guido van Rossumd7047b31995-01-02 19:07:15 +0000542 }
543
Tim Peters0c6010b2001-05-23 23:33:57 +0000544 if (is_oldtable_malloced)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000545 PyMem_DEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000546 return 0;
547}
548
Tim Petersd770ebd2006-06-01 15:50:44 +0000549/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
550 * that may occur (originally dicts supported only string keys, and exceptions
551 * weren't possible). So, while the original intent was that a NULL return
Andrew M. Kuchling0067b5f2006-08-04 20:37:43 +0000552 * meant the key wasn't present, in reality it can mean that, or that an error
Tim Petersd770ebd2006-06-01 15:50:44 +0000553 * (suppressed) occurred while computing the key's hash, or that some error
554 * (suppressed) occurred when comparing keys in the dict's internal probe
555 * sequence. A nasty example of the latter is when a Python-coded comparison
556 * function hits a stack-depth error, which can cause this to return NULL
557 * even if the key is present.
558 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000559PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000560PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000561{
562 long hash;
Fred Drake1bff34a2000-08-31 19:31:38 +0000563 dictobject *mp = (dictobject *)op;
Armin Rigo35f6d362006-06-01 13:19:12 +0000564 dictentry *ep;
565 PyThreadState *tstate;
Tim Petersd770ebd2006-06-01 15:50:44 +0000566 if (!PyDict_Check(op))
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000567 return NULL;
Tim Peters0ab085c2001-09-14 00:25:33 +0000568 if (!PyString_CheckExact(key) ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000569 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Guido van Rossumca756f21997-01-23 19:39:29 +0000570 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000571 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000572 if (hash == -1) {
573 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000574 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000575 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000576 }
Armin Rigo35f6d362006-06-01 13:19:12 +0000577
578 /* We can arrive here with a NULL tstate during initialization:
579 try running "python -Wi" for an example related to string
580 interning. Let's just hope that no exception occurs then... */
Armin Rigoacd0d6d2006-06-10 10:57:40 +0000581 tstate = _PyThreadState_Current;
Armin Rigo35f6d362006-06-01 13:19:12 +0000582 if (tstate != NULL && tstate->curexc_type != NULL) {
583 /* preserve the existing exception */
584 PyObject *err_type, *err_value, *err_tb;
585 PyErr_Fetch(&err_type, &err_value, &err_tb);
586 ep = (mp->ma_lookup)(mp, key, hash);
587 /* ignore errors */
588 PyErr_Restore(err_type, err_value, err_tb);
589 if (ep == NULL)
590 return NULL;
591 }
592 else {
593 ep = (mp->ma_lookup)(mp, key, hash);
594 if (ep == NULL) {
595 PyErr_Clear();
596 return NULL;
597 }
598 }
599 return ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000600}
601
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000602/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
Tim Peters60b29962006-01-01 01:19:23 +0000603 * dictionary if it's merely replacing the value for an existing key.
604 * This means that it's safe to loop over a dictionary with PyDict_Next()
605 * and occasionally replace a value -- but you can't insert new keys or
606 * remove them.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000607 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000608int
Tim Peters1f5871e2000-07-04 17:44:48 +0000609PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000610{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000611 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000612 register long hash;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000613 register Py_ssize_t n_used;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000614
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000615 if (!PyDict_Check(op)) {
616 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000617 return -1;
618 }
Neal Norwitz48808a12006-07-21 05:29:58 +0000619 assert(key);
620 assert(value);
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000621 mp = (dictobject *)op;
Tim Peters0ab085c2001-09-14 00:25:33 +0000622 if (PyString_CheckExact(key)) {
Guido van Rossum45ec02a2002-08-19 21:43:18 +0000623 hash = ((PyStringObject *)key)->ob_shash;
624 if (hash == -1)
625 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000626 }
Tim Peters1f7df352002-03-29 03:29:08 +0000627 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000628 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000629 if (hash == -1)
630 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000631 }
Tim Petersafb6ae82001-06-04 21:00:21 +0000632 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000633 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000634 Py_INCREF(value);
635 Py_INCREF(key);
Armin Rigo35f6d362006-06-01 13:19:12 +0000636 if (insertdict(mp, key, hash, value) != 0)
637 return -1;
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000638 /* If we added a key, we can safely resize. Otherwise just return!
639 * If fill >= 2/3 size, adjust size. Normally, this doubles or
640 * quaduples the size, but it's also possible for the dict to shrink
Tim Peters60b29962006-01-01 01:19:23 +0000641 * (if ma_fill is much larger than ma_used, meaning a lot of dict
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000642 * keys have been * deleted).
Tim Peters60b29962006-01-01 01:19:23 +0000643 *
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000644 * Quadrupling the size improves average dictionary sparseness
645 * (reducing collisions) at the cost of some memory and iteration
646 * speed (which loops over every possible entry). It also halves
Neal Norwitz80af59c2006-05-30 04:19:21 +0000647 * the number of expensive resize operations in a growing dictionary.
Tim Peters60b29962006-01-01 01:19:23 +0000648 *
649 * Very large dictionaries (over 50K items) use doubling instead.
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000650 * This may help applications with severe memory constraints.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000651 */
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000652 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
653 return 0;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000654 return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000655}
656
657int
Tim Peters1f5871e2000-07-04 17:44:48 +0000658PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000659{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000660 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000661 register long hash;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000662 register dictentry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000663 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000664
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000665 if (!PyDict_Check(op)) {
666 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000667 return -1;
668 }
Neal Norwitz48808a12006-07-21 05:29:58 +0000669 assert(key);
Tim Peters0ab085c2001-09-14 00:25:33 +0000670 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +0000671 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000672 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000673 if (hash == -1)
674 return -1;
675 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000676 mp = (dictobject *)op;
Fred Drake1bff34a2000-08-31 19:31:38 +0000677 ep = (mp->ma_lookup)(mp, key, hash);
Armin Rigo35f6d362006-06-01 13:19:12 +0000678 if (ep == NULL)
679 return -1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000680 if (ep->me_value == NULL) {
Georg Brandlb9f4ad32006-10-29 18:31:42 +0000681 set_key_error(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000682 return -1;
683 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000684 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000685 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000686 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000687 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000688 ep->me_value = NULL;
689 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000690 Py_DECREF(old_value);
691 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000692 return 0;
693}
694
Guido van Rossum25831651993-05-19 14:50:45 +0000695void
Tim Peters1f5871e2000-07-04 17:44:48 +0000696PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000697{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000698 dictobject *mp;
Tim Petersdea48ec2001-05-22 20:40:22 +0000699 dictentry *ep, *table;
700 int table_is_malloced;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000701 Py_ssize_t fill;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000702 dictentry small_copy[PyDict_MINSIZE];
Tim Petersdea48ec2001-05-22 20:40:22 +0000703#ifdef Py_DEBUG
Tim Peters9b10f7e2006-05-30 04:16:25 +0000704 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +0000705#endif
706
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000707 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000708 return;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000709 mp = (dictobject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000710#ifdef Py_DEBUG
Tim Petersafb6ae82001-06-04 21:00:21 +0000711 n = mp->ma_mask + 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000712 i = 0;
713#endif
714
715 table = mp->ma_table;
716 assert(table != NULL);
717 table_is_malloced = table != mp->ma_smalltable;
718
719 /* This is delicate. During the process of clearing the dict,
720 * decrefs can cause the dict to mutate. To avoid fatal confusion
721 * (voice of experience), we have to make the dict empty before
722 * clearing the slots, and never refer to anything via mp->xxx while
723 * clearing.
724 */
725 fill = mp->ma_fill;
726 if (table_is_malloced)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000727 EMPTY_TO_MINSIZE(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000728
729 else if (fill > 0) {
730 /* It's a small table with something that needs to be cleared.
731 * Afraid the only safe way is to copy the dict entries into
732 * another small table first.
733 */
734 memcpy(small_copy, table, sizeof(small_copy));
735 table = small_copy;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000736 EMPTY_TO_MINSIZE(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000737 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000738 /* else it's a small table that's already empty */
739
740 /* Now we can finally clear things. If C had refcounts, we could
741 * assert that the refcount on table is 1 now, i.e. that this function
742 * has unique access to it, so decref side-effects can't alter it.
743 */
744 for (ep = table; fill > 0; ++ep) {
745#ifdef Py_DEBUG
746 assert(i < n);
747 ++i;
748#endif
749 if (ep->me_key) {
750 --fill;
751 Py_DECREF(ep->me_key);
752 Py_XDECREF(ep->me_value);
753 }
754#ifdef Py_DEBUG
755 else
756 assert(ep->me_value == NULL);
757#endif
758 }
759
760 if (table_is_malloced)
761 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000762}
763
Tim Peters080c88b2003-02-15 03:01:11 +0000764/*
765 * Iterate over a dict. Use like so:
766 *
Tim Peters9b10f7e2006-05-30 04:16:25 +0000767 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +0000768 * PyObject *key, *value;
769 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +0000770 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +0000771 * Refer to borrowed references in key and value.
772 * }
773 *
774 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +0000775 * mutates the dict. One exception: it is safe if the loop merely changes
776 * the values associated with the keys (but doesn't insert new keys or
777 * delete keys), via PyDict_SetItem().
778 */
Guido van Rossum25831651993-05-19 14:50:45 +0000779int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000780PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000781{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000782 register Py_ssize_t i;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000783 register Py_ssize_t mask;
Raymond Hettinger43442782004-03-17 21:55:03 +0000784 register dictentry *ep;
785
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000786 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000787 return 0;
Guido van Rossum25831651993-05-19 14:50:45 +0000788 i = *ppos;
789 if (i < 0)
790 return 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000791 ep = ((dictobject *)op)->ma_table;
792 mask = ((dictobject *)op)->ma_mask;
793 while (i <= mask && ep[i].me_value == NULL)
Guido van Rossum25831651993-05-19 14:50:45 +0000794 i++;
795 *ppos = i+1;
Raymond Hettinger43442782004-03-17 21:55:03 +0000796 if (i > mask)
Guido van Rossum25831651993-05-19 14:50:45 +0000797 return 0;
798 if (pkey)
Raymond Hettinger43442782004-03-17 21:55:03 +0000799 *pkey = ep[i].me_key;
Guido van Rossum25831651993-05-19 14:50:45 +0000800 if (pvalue)
Raymond Hettinger43442782004-03-17 21:55:03 +0000801 *pvalue = ep[i].me_value;
Guido van Rossum25831651993-05-19 14:50:45 +0000802 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000803}
804
805/* Methods */
806
807static void
Tim Peters1f5871e2000-07-04 17:44:48 +0000808dict_dealloc(register dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000809{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000810 register dictentry *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000811 Py_ssize_t fill = mp->ma_fill;
Guido van Rossumff413af2002-03-28 20:34:59 +0000812 PyObject_GC_UnTrack(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000813 Py_TRASHCAN_SAFE_BEGIN(mp)
Tim Petersdea48ec2001-05-22 20:40:22 +0000814 for (ep = mp->ma_table; fill > 0; ep++) {
815 if (ep->me_key) {
816 --fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000817 Py_DECREF(ep->me_key);
Tim Petersdea48ec2001-05-22 20:40:22 +0000818 Py_XDECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000819 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000820 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000821 if (mp->ma_table != mp->ma_smalltable)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000822 PyMem_DEL(mp->ma_table);
Raymond Hettinger43442782004-03-17 21:55:03 +0000823 if (num_free_dicts < MAXFREEDICTS && mp->ob_type == &PyDict_Type)
824 free_dicts[num_free_dicts++] = mp;
Tim Peters60b29962006-01-01 01:19:23 +0000825 else
Raymond Hettinger43442782004-03-17 21:55:03 +0000826 mp->ob_type->tp_free((PyObject *)mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000827 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000828}
829
830static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000831dict_print(register dictobject *mp, register FILE *fp, register int flags)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000832{
Tim Peters9b10f7e2006-05-30 04:16:25 +0000833 register Py_ssize_t i;
834 register Py_ssize_t any;
Tim Peters33f4a6a2006-05-30 05:23:59 +0000835 int status;
Guido van Rossum255443b1998-04-10 22:47:14 +0000836
Tim Peters33f4a6a2006-05-30 05:23:59 +0000837 status = Py_ReprEnter((PyObject*)mp);
838 if (status != 0) {
839 if (status < 0)
840 return status;
Guido van Rossum255443b1998-04-10 22:47:14 +0000841 fprintf(fp, "{...}");
842 return 0;
843 }
844
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000845 fprintf(fp, "{");
846 any = 0;
Tim Petersafb6ae82001-06-04 21:00:21 +0000847 for (i = 0; i <= mp->ma_mask; i++) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000848 dictentry *ep = mp->ma_table + i;
849 PyObject *pvalue = ep->me_value;
850 if (pvalue != NULL) {
851 /* Prevent PyObject_Repr from deleting value during
852 key format */
853 Py_INCREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000854 if (any++ > 0)
855 fprintf(fp, ", ");
Guido van Rossum255443b1998-04-10 22:47:14 +0000856 if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000857 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000858 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000859 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000860 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000861 fprintf(fp, ": ");
Tim Peters19b77cf2001-06-02 08:27:39 +0000862 if (PyObject_Print(pvalue, fp, 0) != 0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000863 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000864 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000865 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000866 }
Tim Peters23cf6be2001-06-02 08:02:56 +0000867 Py_DECREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000868 }
869 }
870 fprintf(fp, "}");
Guido van Rossum255443b1998-04-10 22:47:14 +0000871 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000872 return 0;
873}
874
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000875static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000876dict_repr(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000877{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000878 Py_ssize_t i;
Tim Petersa7259592001-06-16 05:11:17 +0000879 PyObject *s, *temp, *colon = NULL;
880 PyObject *pieces = NULL, *result = NULL;
881 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +0000882
Tim Petersa7259592001-06-16 05:11:17 +0000883 i = Py_ReprEnter((PyObject *)mp);
Guido van Rossum255443b1998-04-10 22:47:14 +0000884 if (i != 0) {
Tim Petersa7259592001-06-16 05:11:17 +0000885 return i > 0 ? PyString_FromString("{...}") : NULL;
Guido van Rossum255443b1998-04-10 22:47:14 +0000886 }
887
Tim Petersa7259592001-06-16 05:11:17 +0000888 if (mp->ma_used == 0) {
889 result = PyString_FromString("{}");
890 goto Done;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000891 }
Tim Petersa7259592001-06-16 05:11:17 +0000892
893 pieces = PyList_New(0);
894 if (pieces == NULL)
895 goto Done;
896
897 colon = PyString_FromString(": ");
898 if (colon == NULL)
899 goto Done;
900
901 /* Do repr() on each key+value pair, and insert ": " between them.
902 Note that repr may mutate the dict. */
Tim Petersc6057842001-06-16 07:52:53 +0000903 i = 0;
904 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Tim Petersa7259592001-06-16 05:11:17 +0000905 int status;
906 /* Prevent repr from deleting value during key format. */
907 Py_INCREF(value);
908 s = PyObject_Repr(key);
909 PyString_Concat(&s, colon);
910 PyString_ConcatAndDel(&s, PyObject_Repr(value));
911 Py_DECREF(value);
912 if (s == NULL)
913 goto Done;
914 status = PyList_Append(pieces, s);
915 Py_DECREF(s); /* append created a new ref */
916 if (status < 0)
917 goto Done;
918 }
919
920 /* Add "{}" decorations to the first and last items. */
921 assert(PyList_GET_SIZE(pieces) > 0);
922 s = PyString_FromString("{");
923 if (s == NULL)
924 goto Done;
925 temp = PyList_GET_ITEM(pieces, 0);
926 PyString_ConcatAndDel(&s, temp);
927 PyList_SET_ITEM(pieces, 0, s);
928 if (s == NULL)
929 goto Done;
930
931 s = PyString_FromString("}");
932 if (s == NULL)
933 goto Done;
934 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
935 PyString_ConcatAndDel(&temp, s);
936 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
937 if (temp == NULL)
938 goto Done;
939
940 /* Paste them all together with ", " between. */
941 s = PyString_FromString(", ");
942 if (s == NULL)
943 goto Done;
944 result = _PyString_Join(s, pieces);
Tim Peters0ab085c2001-09-14 00:25:33 +0000945 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +0000946
947Done:
948 Py_XDECREF(pieces);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000949 Py_XDECREF(colon);
Tim Petersa7259592001-06-16 05:11:17 +0000950 Py_ReprLeave((PyObject *)mp);
951 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000952}
953
Martin v. Löwis18e16552006-02-15 17:27:45 +0000954static Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +0000955dict_length(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000956{
957 return mp->ma_used;
958}
959
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000960static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000961dict_subscript(dictobject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000962{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000963 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000964 long hash;
Armin Rigo35f6d362006-06-01 13:19:12 +0000965 dictentry *ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000966 assert(mp->ma_table != NULL);
Tim Peters0ab085c2001-09-14 00:25:33 +0000967 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +0000968 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000969 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000970 if (hash == -1)
971 return NULL;
972 }
Armin Rigo35f6d362006-06-01 13:19:12 +0000973 ep = (mp->ma_lookup)(mp, key, hash);
974 if (ep == NULL)
975 return NULL;
976 v = ep->me_value;
Guido van Rossum1968ad32006-02-25 22:38:04 +0000977 if (v == NULL) {
978 if (!PyDict_CheckExact(mp)) {
979 /* Look up __missing__ method if we're a subclass. */
Guido van Rossum4b92a822006-02-25 23:32:30 +0000980 PyObject *missing;
Guido van Rossum1968ad32006-02-25 22:38:04 +0000981 static PyObject *missing_str = NULL;
982 if (missing_str == NULL)
Tim Peters9b10f7e2006-05-30 04:16:25 +0000983 missing_str =
Guido van Rossum1968ad32006-02-25 22:38:04 +0000984 PyString_InternFromString("__missing__");
Guido van Rossum4b92a822006-02-25 23:32:30 +0000985 missing = _PyType_Lookup(mp->ob_type, missing_str);
Guido van Rossum1968ad32006-02-25 22:38:04 +0000986 if (missing != NULL)
987 return PyObject_CallFunctionObjArgs(missing,
988 (PyObject *)mp, key, NULL);
989 }
Georg Brandlb9f4ad32006-10-29 18:31:42 +0000990 set_key_error(key);
Guido van Rossum1968ad32006-02-25 22:38:04 +0000991 return NULL;
992 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000993 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000994 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000995 return v;
996}
997
998static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000999dict_ass_sub(dictobject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001000{
1001 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001002 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001003 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001004 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001005}
1006
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001007static PyMappingMethods dict_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001008 (lenfunc)dict_length, /*mp_length*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001009 (binaryfunc)dict_subscript, /*mp_subscript*/
1010 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001011};
1012
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001013static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001014dict_keys(register dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001015{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001016 register PyObject *v;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001017 register Py_ssize_t i, j;
Raymond Hettinger43442782004-03-17 21:55:03 +00001018 dictentry *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001019 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001020
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001021 again:
1022 n = mp->ma_used;
1023 v = PyList_New(n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001024 if (v == NULL)
1025 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001026 if (n != mp->ma_used) {
1027 /* Durnit. The allocations caused the dict to resize.
1028 * Just start over, this shouldn't normally happen.
1029 */
1030 Py_DECREF(v);
1031 goto again;
1032 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001033 ep = mp->ma_table;
1034 mask = mp->ma_mask;
1035 for (i = 0, j = 0; i <= mask; i++) {
1036 if (ep[i].me_value != NULL) {
1037 PyObject *key = ep[i].me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001038 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001039 PyList_SET_ITEM(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001040 j++;
1041 }
1042 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001043 assert(j == n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001044 return v;
1045}
1046
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001047static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001048dict_values(register dictobject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001049{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001050 register PyObject *v;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001051 register Py_ssize_t i, j;
Raymond Hettinger43442782004-03-17 21:55:03 +00001052 dictentry *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001053 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001054
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001055 again:
1056 n = mp->ma_used;
1057 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001058 if (v == NULL)
1059 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001060 if (n != mp->ma_used) {
1061 /* Durnit. The allocations caused the dict to resize.
1062 * Just start over, this shouldn't normally happen.
1063 */
1064 Py_DECREF(v);
1065 goto again;
1066 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001067 ep = mp->ma_table;
1068 mask = mp->ma_mask;
1069 for (i = 0, j = 0; i <= mask; i++) {
1070 if (ep[i].me_value != NULL) {
1071 PyObject *value = ep[i].me_value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001072 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001073 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001074 j++;
1075 }
1076 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001077 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001078 return v;
1079}
1080
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001081static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001082dict_items(register dictobject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001083{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001084 register PyObject *v;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001085 register Py_ssize_t i, j, n;
1086 Py_ssize_t mask;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001087 PyObject *item, *key, *value;
Raymond Hettinger43442782004-03-17 21:55:03 +00001088 dictentry *ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001089
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001090 /* Preallocate the list of tuples, to avoid allocations during
1091 * the loop over the items, which could trigger GC, which
1092 * could resize the dict. :-(
1093 */
1094 again:
1095 n = mp->ma_used;
1096 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001097 if (v == NULL)
1098 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001099 for (i = 0; i < n; i++) {
1100 item = PyTuple_New(2);
1101 if (item == NULL) {
1102 Py_DECREF(v);
1103 return NULL;
1104 }
1105 PyList_SET_ITEM(v, i, item);
1106 }
1107 if (n != mp->ma_used) {
1108 /* Durnit. The allocations caused the dict to resize.
1109 * Just start over, this shouldn't normally happen.
1110 */
1111 Py_DECREF(v);
1112 goto again;
1113 }
1114 /* Nothing we do below makes any function calls. */
Raymond Hettinger43442782004-03-17 21:55:03 +00001115 ep = mp->ma_table;
1116 mask = mp->ma_mask;
1117 for (i = 0, j = 0; i <= mask; i++) {
Raymond Hettinger06905122004-03-19 10:30:00 +00001118 if ((value=ep[i].me_value) != NULL) {
Raymond Hettinger43442782004-03-17 21:55:03 +00001119 key = ep[i].me_key;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001120 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001121 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001122 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001123 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001124 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001125 j++;
1126 }
1127 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001128 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001129 return v;
1130}
1131
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001132static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001133dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001134{
1135 PyObject *seq;
1136 PyObject *value = Py_None;
1137 PyObject *it; /* iter(seq) */
1138 PyObject *key;
1139 PyObject *d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001140 int status;
1141
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001142 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001143 return NULL;
1144
1145 d = PyObject_CallObject(cls, NULL);
1146 if (d == NULL)
1147 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001148
1149 it = PyObject_GetIter(seq);
1150 if (it == NULL){
1151 Py_DECREF(d);
1152 return NULL;
1153 }
1154
1155 for (;;) {
1156 key = PyIter_Next(it);
1157 if (key == NULL) {
1158 if (PyErr_Occurred())
1159 goto Fail;
1160 break;
1161 }
Raymond Hettingere03e5b12002-12-07 08:10:51 +00001162 status = PyObject_SetItem(d, key, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001163 Py_DECREF(key);
1164 if (status < 0)
1165 goto Fail;
1166 }
1167
1168 Py_DECREF(it);
1169 return d;
1170
1171Fail:
1172 Py_DECREF(it);
1173 Py_DECREF(d);
1174 return NULL;
1175}
1176
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001177static int
1178dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001179{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001180 PyObject *arg = NULL;
1181 int result = 0;
1182
1183 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1184 result = -1;
1185
1186 else if (arg != NULL) {
1187 if (PyObject_HasAttrString(arg, "keys"))
1188 result = PyDict_Merge(self, arg, 1);
1189 else
1190 result = PyDict_MergeFromSeq2(self, arg, 1);
1191 }
1192 if (result == 0 && kwds != NULL)
1193 result = PyDict_Merge(self, kwds, 1);
1194 return result;
1195}
1196
1197static PyObject *
1198dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1199{
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001200 if (dict_update_common(self, args, kwds, "update") != -1)
1201 Py_RETURN_NONE;
1202 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001203}
1204
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001205/* Update unconditionally replaces existing items.
1206 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001207 otherwise it leaves existing items unchanged.
1208
1209 PyDict_{Update,Merge} update/merge from a mapping object.
1210
Tim Petersf582b822001-12-11 18:51:08 +00001211 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001212 producing iterable objects of length 2.
1213*/
1214
Tim Petersf582b822001-12-11 18:51:08 +00001215int
Tim Peters1fc240e2001-10-26 05:06:50 +00001216PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1217{
1218 PyObject *it; /* iter(seq2) */
Tim Peters9b10f7e2006-05-30 04:16:25 +00001219 Py_ssize_t i; /* index into seq2 of current element */
Tim Peters1fc240e2001-10-26 05:06:50 +00001220 PyObject *item; /* seq2[i] */
1221 PyObject *fast; /* item as a 2-tuple or 2-list */
1222
1223 assert(d != NULL);
1224 assert(PyDict_Check(d));
1225 assert(seq2 != NULL);
1226
1227 it = PyObject_GetIter(seq2);
1228 if (it == NULL)
1229 return -1;
1230
1231 for (i = 0; ; ++i) {
1232 PyObject *key, *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001233 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001234
1235 fast = NULL;
1236 item = PyIter_Next(it);
1237 if (item == NULL) {
1238 if (PyErr_Occurred())
1239 goto Fail;
1240 break;
1241 }
1242
1243 /* Convert item to sequence, and verify length 2. */
1244 fast = PySequence_Fast(item, "");
1245 if (fast == NULL) {
1246 if (PyErr_ExceptionMatches(PyExc_TypeError))
1247 PyErr_Format(PyExc_TypeError,
1248 "cannot convert dictionary update "
Neal Norwitzd3881b02006-05-30 04:25:05 +00001249 "sequence element #%zd to a sequence",
Tim Peters1fc240e2001-10-26 05:06:50 +00001250 i);
1251 goto Fail;
1252 }
1253 n = PySequence_Fast_GET_SIZE(fast);
1254 if (n != 2) {
1255 PyErr_Format(PyExc_ValueError,
Neal Norwitzd3881b02006-05-30 04:25:05 +00001256 "dictionary update sequence element #%zd "
Martin v. Löwis2c95cc62006-02-16 06:54:25 +00001257 "has length %zd; 2 is required",
Martin v. Löwise0e89f72006-02-16 06:59:22 +00001258 i, n);
Tim Peters1fc240e2001-10-26 05:06:50 +00001259 goto Fail;
1260 }
1261
1262 /* Update/merge with this (key, value) pair. */
1263 key = PySequence_Fast_GET_ITEM(fast, 0);
1264 value = PySequence_Fast_GET_ITEM(fast, 1);
1265 if (override || PyDict_GetItem(d, key) == NULL) {
1266 int status = PyDict_SetItem(d, key, value);
1267 if (status < 0)
1268 goto Fail;
1269 }
1270 Py_DECREF(fast);
1271 Py_DECREF(item);
1272 }
1273
1274 i = 0;
1275 goto Return;
1276Fail:
1277 Py_XDECREF(item);
1278 Py_XDECREF(fast);
1279 i = -1;
1280Return:
1281 Py_DECREF(it);
Neal Norwitzd3881b02006-05-30 04:25:05 +00001282 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001283}
1284
Tim Peters6d6c1a32001-08-02 04:15:00 +00001285int
1286PyDict_Update(PyObject *a, PyObject *b)
1287{
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001288 return PyDict_Merge(a, b, 1);
1289}
1290
1291int
1292PyDict_Merge(PyObject *a, PyObject *b, int override)
1293{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001294 register PyDictObject *mp, *other;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001295 register Py_ssize_t i;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001296 dictentry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001297
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001298 /* We accept for the argument either a concrete dictionary object,
1299 * or an abstract "mapping" object. For the former, we can do
1300 * things quite efficiently. For the latter, we only require that
1301 * PyMapping_Keys() and PyObject_GetItem() be supported.
1302 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001303 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1304 PyErr_BadInternalCall();
1305 return -1;
1306 }
1307 mp = (dictobject*)a;
1308 if (PyDict_Check(b)) {
1309 other = (dictobject*)b;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001310 if (other == mp || other->ma_used == 0)
1311 /* a.update(a) or a.update({}); nothing to do */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001312 return 0;
Raymond Hettinger186e7392005-05-14 18:08:25 +00001313 if (mp->ma_used == 0)
1314 /* Since the target dict is empty, PyDict_GetItem()
1315 * always returns NULL. Setting override to 1
1316 * skips the unnecessary test.
1317 */
1318 override = 1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001319 /* Do one big resize at the start, rather than
1320 * incrementally resizing as we insert new items. Expect
1321 * that there will be no (or few) overlapping keys.
1322 */
1323 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
Raymond Hettingerc8d22902003-05-07 00:49:40 +00001324 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001325 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001326 }
1327 for (i = 0; i <= other->ma_mask; i++) {
1328 entry = &other->ma_table[i];
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001329 if (entry->me_value != NULL &&
1330 (override ||
1331 PyDict_GetItem(a, entry->me_key) == NULL)) {
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001332 Py_INCREF(entry->me_key);
1333 Py_INCREF(entry->me_value);
Armin Rigo35f6d362006-06-01 13:19:12 +00001334 if (insertdict(mp, entry->me_key,
1335 (long)entry->me_hash,
1336 entry->me_value) != 0)
1337 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001338 }
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001339 }
1340 }
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001341 else {
1342 /* Do it the generic, slower way */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001343 PyObject *keys = PyMapping_Keys(b);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001344 PyObject *iter;
1345 PyObject *key, *value;
1346 int status;
1347
1348 if (keys == NULL)
1349 /* Docstring says this is equivalent to E.keys() so
1350 * if E doesn't have a .keys() method we want
1351 * AttributeError to percolate up. Might as well
1352 * do the same for any other error.
1353 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001354 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001355
1356 iter = PyObject_GetIter(keys);
1357 Py_DECREF(keys);
1358 if (iter == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001359 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001360
1361 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001362 if (!override && PyDict_GetItem(a, key) != NULL) {
1363 Py_DECREF(key);
1364 continue;
1365 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001366 value = PyObject_GetItem(b, key);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001367 if (value == NULL) {
1368 Py_DECREF(iter);
1369 Py_DECREF(key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001370 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001371 }
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001372 status = PyDict_SetItem(a, key, value);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001373 Py_DECREF(key);
1374 Py_DECREF(value);
1375 if (status < 0) {
1376 Py_DECREF(iter);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001377 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001378 }
1379 }
1380 Py_DECREF(iter);
1381 if (PyErr_Occurred())
1382 /* Iterator completed, via error */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001383 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001384 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001385 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001386}
1387
1388static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001389dict_copy(register dictobject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001390{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001391 return PyDict_Copy((PyObject*)mp);
1392}
1393
1394PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001395PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001396{
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001397 PyObject *copy;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001398
1399 if (o == NULL || !PyDict_Check(o)) {
1400 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001401 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001402 }
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001403 copy = PyDict_New();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001404 if (copy == NULL)
1405 return NULL;
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001406 if (PyDict_Merge(copy, o, 1) == 0)
1407 return copy;
1408 Py_DECREF(copy);
Raymond Hettinger1356f782005-04-15 15:58:42 +00001409 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001410}
1411
Martin v. Löwis18e16552006-02-15 17:27:45 +00001412Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00001413PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001414{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001415 if (mp == NULL || !PyDict_Check(mp)) {
1416 PyErr_BadInternalCall();
Jeremy Hylton7083bb72004-02-17 20:10:11 +00001417 return -1;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001418 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001419 return ((dictobject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001420}
1421
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001422PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001423PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001424{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001425 if (mp == NULL || !PyDict_Check(mp)) {
1426 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001427 return NULL;
1428 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001429 return dict_keys((dictobject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001430}
1431
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001432PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001433PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001434{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001435 if (mp == NULL || !PyDict_Check(mp)) {
1436 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001437 return NULL;
1438 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001439 return dict_values((dictobject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001440}
1441
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001442PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001443PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001444{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001445 if (mp == NULL || !PyDict_Check(mp)) {
1446 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001447 return NULL;
1448 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001449 return dict_items((dictobject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001450}
1451
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001452/* Subroutine which returns the smallest key in a for which b's value
1453 is different or absent. The value is returned too, through the
Tim Peters95bf9392001-05-10 08:32:44 +00001454 pval argument. Both are NULL if no key in a is found for which b's status
1455 differs. The refcounts on (and only on) non-NULL *pval and function return
1456 values must be decremented by the caller (characterize() increments them
1457 to ensure that mutating comparison and PyDict_GetItem calls can't delete
1458 them before the caller is done looking at them). */
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001459
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001460static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001461characterize(dictobject *a, dictobject *b, PyObject **pval)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001462{
Tim Peters95bf9392001-05-10 08:32:44 +00001463 PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */
1464 PyObject *aval = NULL; /* a[akey] */
Tim Peters9b10f7e2006-05-30 04:16:25 +00001465 Py_ssize_t i;
1466 int cmp;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001467
Tim Petersafb6ae82001-06-04 21:00:21 +00001468 for (i = 0; i <= a->ma_mask; i++) {
Tim Peters95bf9392001-05-10 08:32:44 +00001469 PyObject *thiskey, *thisaval, *thisbval;
1470 if (a->ma_table[i].me_value == NULL)
1471 continue;
1472 thiskey = a->ma_table[i].me_key;
1473 Py_INCREF(thiskey); /* keep alive across compares */
1474 if (akey != NULL) {
1475 cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT);
1476 if (cmp < 0) {
1477 Py_DECREF(thiskey);
1478 goto Fail;
1479 }
1480 if (cmp > 0 ||
Tim Petersafb6ae82001-06-04 21:00:21 +00001481 i > a->ma_mask ||
Tim Peters95bf9392001-05-10 08:32:44 +00001482 a->ma_table[i].me_value == NULL)
1483 {
1484 /* Not the *smallest* a key; or maybe it is
1485 * but the compare shrunk the dict so we can't
1486 * find its associated value anymore; or
1487 * maybe it is but the compare deleted the
1488 * a[thiskey] entry.
Neal Norwitzd3881b02006-05-30 04:25:05 +00001489 */
Tim Peters95bf9392001-05-10 08:32:44 +00001490 Py_DECREF(thiskey);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001491 continue;
Guido van Rossumb9324202001-01-18 00:39:02 +00001492 }
Tim Peters95bf9392001-05-10 08:32:44 +00001493 }
1494
1495 /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */
1496 thisaval = a->ma_table[i].me_value;
1497 assert(thisaval);
1498 Py_INCREF(thisaval); /* keep alive */
1499 thisbval = PyDict_GetItem((PyObject *)b, thiskey);
1500 if (thisbval == NULL)
1501 cmp = 0;
1502 else {
1503 /* both dicts have thiskey: same values? */
1504 cmp = PyObject_RichCompareBool(
1505 thisaval, thisbval, Py_EQ);
1506 if (cmp < 0) {
1507 Py_DECREF(thiskey);
1508 Py_DECREF(thisaval);
1509 goto Fail;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001510 }
1511 }
Tim Peters95bf9392001-05-10 08:32:44 +00001512 if (cmp == 0) {
1513 /* New winner. */
1514 Py_XDECREF(akey);
1515 Py_XDECREF(aval);
1516 akey = thiskey;
1517 aval = thisaval;
1518 }
1519 else {
1520 Py_DECREF(thiskey);
1521 Py_DECREF(thisaval);
1522 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001523 }
Tim Peters95bf9392001-05-10 08:32:44 +00001524 *pval = aval;
1525 return akey;
1526
1527Fail:
1528 Py_XDECREF(akey);
1529 Py_XDECREF(aval);
1530 *pval = NULL;
1531 return NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001532}
1533
1534static int
Tim Peters1f5871e2000-07-04 17:44:48 +00001535dict_compare(dictobject *a, dictobject *b)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001536{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001537 PyObject *adiff, *bdiff, *aval, *bval;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001538 int res;
1539
1540 /* Compare lengths first */
1541 if (a->ma_used < b->ma_used)
1542 return -1; /* a is shorter */
1543 else if (a->ma_used > b->ma_used)
1544 return 1; /* b is shorter */
Tim Peters95bf9392001-05-10 08:32:44 +00001545
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001546 /* Same length -- check all keys */
Tim Peters95bf9392001-05-10 08:32:44 +00001547 bdiff = bval = NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001548 adiff = characterize(a, b, &aval);
Tim Peters95bf9392001-05-10 08:32:44 +00001549 if (adiff == NULL) {
1550 assert(!aval);
Tim Peters3918fb22001-05-10 18:58:31 +00001551 /* Either an error, or a is a subset with the same length so
Tim Peters95bf9392001-05-10 08:32:44 +00001552 * must be equal.
1553 */
1554 res = PyErr_Occurred() ? -1 : 0;
1555 goto Finished;
1556 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001557 bdiff = characterize(b, a, &bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001558 if (bdiff == NULL && PyErr_Occurred()) {
1559 assert(!bval);
1560 res = -1;
1561 goto Finished;
1562 }
1563 res = 0;
1564 if (bdiff) {
1565 /* bdiff == NULL "should be" impossible now, but perhaps
1566 * the last comparison done by the characterize() on a had
1567 * the side effect of making the dicts equal!
1568 */
1569 res = PyObject_Compare(adiff, bdiff);
1570 }
1571 if (res == 0 && bval != NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001572 res = PyObject_Compare(aval, bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001573
1574Finished:
1575 Py_XDECREF(adiff);
1576 Py_XDECREF(bdiff);
1577 Py_XDECREF(aval);
1578 Py_XDECREF(bval);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001579 return res;
1580}
1581
Tim Peterse63415e2001-05-08 04:38:29 +00001582/* Return 1 if dicts equal, 0 if not, -1 if error.
1583 * Gets out as soon as any difference is detected.
1584 * Uses only Py_EQ comparison.
1585 */
1586static int
1587dict_equal(dictobject *a, dictobject *b)
1588{
Tim Peters9b10f7e2006-05-30 04:16:25 +00001589 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00001590
1591 if (a->ma_used != b->ma_used)
1592 /* can't be equal if # of entries differ */
1593 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001594
Tim Peterse63415e2001-05-08 04:38:29 +00001595 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Tim Petersafb6ae82001-06-04 21:00:21 +00001596 for (i = 0; i <= a->ma_mask; i++) {
Tim Peterse63415e2001-05-08 04:38:29 +00001597 PyObject *aval = a->ma_table[i].me_value;
1598 if (aval != NULL) {
1599 int cmp;
1600 PyObject *bval;
1601 PyObject *key = a->ma_table[i].me_key;
1602 /* temporarily bump aval's refcount to ensure it stays
1603 alive until we're done with it */
1604 Py_INCREF(aval);
Neal Norwitza22975f2006-09-05 02:24:03 +00001605 /* ditto for key */
1606 Py_INCREF(key);
Tim Peterse63415e2001-05-08 04:38:29 +00001607 bval = PyDict_GetItem((PyObject *)b, key);
Neal Norwitza22975f2006-09-05 02:24:03 +00001608 Py_DECREF(key);
Tim Peterse63415e2001-05-08 04:38:29 +00001609 if (bval == NULL) {
1610 Py_DECREF(aval);
1611 return 0;
1612 }
1613 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1614 Py_DECREF(aval);
1615 if (cmp <= 0) /* error or not equal */
1616 return cmp;
1617 }
1618 }
1619 return 1;
1620 }
1621
1622static PyObject *
1623dict_richcompare(PyObject *v, PyObject *w, int op)
1624{
1625 int cmp;
1626 PyObject *res;
1627
1628 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1629 res = Py_NotImplemented;
1630 }
1631 else if (op == Py_EQ || op == Py_NE) {
1632 cmp = dict_equal((dictobject *)v, (dictobject *)w);
1633 if (cmp < 0)
1634 return NULL;
1635 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1636 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001637 else
1638 res = Py_NotImplemented;
Tim Peterse63415e2001-05-08 04:38:29 +00001639 Py_INCREF(res);
1640 return res;
1641 }
1642
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001643static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001644dict_has_key(register dictobject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001645{
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001646 long hash;
Armin Rigo35f6d362006-06-01 13:19:12 +00001647 dictentry *ep;
Tim Petersd770ebd2006-06-01 15:50:44 +00001648
Tim Peters0ab085c2001-09-14 00:25:33 +00001649 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001650 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001651 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001652 if (hash == -1)
1653 return NULL;
1654 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001655 ep = (mp->ma_lookup)(mp, key, hash);
1656 if (ep == NULL)
1657 return NULL;
Tim Petersd770ebd2006-06-01 15:50:44 +00001658 return PyBool_FromLong(ep->me_value != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001659}
1660
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001661static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001662dict_get(register dictobject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001663{
1664 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001665 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001666 PyObject *val = NULL;
1667 long hash;
Armin Rigo35f6d362006-06-01 13:19:12 +00001668 dictentry *ep;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001669
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001670 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001671 return NULL;
1672
Tim Peters0ab085c2001-09-14 00:25:33 +00001673 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001674 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Barry Warsawc38c5da1997-10-06 17:49:20 +00001675 hash = PyObject_Hash(key);
1676 if (hash == -1)
1677 return NULL;
1678 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001679 ep = (mp->ma_lookup)(mp, key, hash);
1680 if (ep == NULL)
1681 return NULL;
1682 val = ep->me_value;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001683 if (val == NULL)
1684 val = failobj;
1685 Py_INCREF(val);
1686 return val;
1687}
1688
1689
1690static PyObject *
Guido van Rossum164452c2000-08-08 16:12:54 +00001691dict_setdefault(register dictobject *mp, PyObject *args)
1692{
1693 PyObject *key;
1694 PyObject *failobj = Py_None;
1695 PyObject *val = NULL;
1696 long hash;
Armin Rigo35f6d362006-06-01 13:19:12 +00001697 dictentry *ep;
Guido van Rossum164452c2000-08-08 16:12:54 +00001698
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001699 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
Guido van Rossum164452c2000-08-08 16:12:54 +00001700 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001701
Tim Peters0ab085c2001-09-14 00:25:33 +00001702 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001703 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossum164452c2000-08-08 16:12:54 +00001704 hash = PyObject_Hash(key);
1705 if (hash == -1)
1706 return NULL;
1707 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001708 ep = (mp->ma_lookup)(mp, key, hash);
1709 if (ep == NULL)
1710 return NULL;
1711 val = ep->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001712 if (val == NULL) {
1713 val = failobj;
1714 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1715 val = NULL;
1716 }
1717 Py_XINCREF(val);
1718 return val;
1719}
1720
1721
1722static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001723dict_clear(register dictobject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001724{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001725 PyDict_Clear((PyObject *)mp);
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001726 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001727}
1728
Guido van Rossumba6ab842000-12-12 22:02:18 +00001729static PyObject *
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001730dict_pop(dictobject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00001731{
1732 long hash;
1733 dictentry *ep;
1734 PyObject *old_value, *old_key;
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001735 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001736
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001737 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1738 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001739 if (mp->ma_used == 0) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001740 if (deflt) {
1741 Py_INCREF(deflt);
1742 return deflt;
1743 }
Guido van Rossume027d982002-04-12 15:11:59 +00001744 PyErr_SetString(PyExc_KeyError,
1745 "pop(): dictionary is empty");
1746 return NULL;
1747 }
1748 if (!PyString_CheckExact(key) ||
1749 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1750 hash = PyObject_Hash(key);
1751 if (hash == -1)
1752 return NULL;
1753 }
1754 ep = (mp->ma_lookup)(mp, key, hash);
Armin Rigo35f6d362006-06-01 13:19:12 +00001755 if (ep == NULL)
1756 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001757 if (ep->me_value == NULL) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001758 if (deflt) {
1759 Py_INCREF(deflt);
1760 return deflt;
1761 }
Georg Brandlb9f4ad32006-10-29 18:31:42 +00001762 set_key_error(key);
Guido van Rossume027d982002-04-12 15:11:59 +00001763 return NULL;
1764 }
1765 old_key = ep->me_key;
1766 Py_INCREF(dummy);
1767 ep->me_key = dummy;
1768 old_value = ep->me_value;
1769 ep->me_value = NULL;
1770 mp->ma_used--;
1771 Py_DECREF(old_key);
1772 return old_value;
1773}
1774
1775static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001776dict_popitem(dictobject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001777{
Tim Peters9b10f7e2006-05-30 04:16:25 +00001778 Py_ssize_t i = 0;
Guido van Rossumba6ab842000-12-12 22:02:18 +00001779 dictentry *ep;
1780 PyObject *res;
1781
Tim Petersf4b33f62001-06-02 05:42:29 +00001782 /* Allocate the result tuple before checking the size. Believe it
1783 * or not, this allocation could trigger a garbage collection which
1784 * could empty the dict, so if we checked the size first and that
1785 * happened, the result would be an infinite loop (searching for an
1786 * entry that no longer exists). Note that the usual popitem()
1787 * idiom is "while d: k, v = d.popitem()". so needing to throw the
Tim Peters9b10f7e2006-05-30 04:16:25 +00001788 * tuple away if the dict *is* empty isn't a significant
Tim Petersf4b33f62001-06-02 05:42:29 +00001789 * inefficiency -- possible, but unlikely in practice.
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001790 */
1791 res = PyTuple_New(2);
1792 if (res == NULL)
1793 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001794 if (mp->ma_used == 0) {
1795 Py_DECREF(res);
1796 PyErr_SetString(PyExc_KeyError,
1797 "popitem(): dictionary is empty");
1798 return NULL;
1799 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001800 /* Set ep to "the first" dict entry with a value. We abuse the hash
1801 * field of slot 0 to hold a search finger:
1802 * If slot 0 has a value, use slot 0.
1803 * Else slot 0 is being used to hold a search finger,
Neal Norwitzd3881b02006-05-30 04:25:05 +00001804 * and we use its hash value as the first index to look.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001805 */
1806 ep = &mp->ma_table[0];
1807 if (ep->me_value == NULL) {
Tim Peters9b10f7e2006-05-30 04:16:25 +00001808 i = ep->me_hash;
Tim Petersdea48ec2001-05-22 20:40:22 +00001809 /* The hash field may be a real hash value, or it may be a
1810 * legit search finger, or it may be a once-legit search
1811 * finger that's out of bounds now because it wrapped around
1812 * or the table shrunk -- simply make sure it's in bounds now.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001813 */
Tim Petersafb6ae82001-06-04 21:00:21 +00001814 if (i > mp->ma_mask || i < 1)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001815 i = 1; /* skip slot 0 */
1816 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1817 i++;
Tim Petersafb6ae82001-06-04 21:00:21 +00001818 if (i > mp->ma_mask)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001819 i = 1;
1820 }
1821 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001822 PyTuple_SET_ITEM(res, 0, ep->me_key);
1823 PyTuple_SET_ITEM(res, 1, ep->me_value);
1824 Py_INCREF(dummy);
1825 ep->me_key = dummy;
1826 ep->me_value = NULL;
1827 mp->ma_used--;
1828 assert(mp->ma_table[0].me_value == NULL);
1829 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001830 return res;
1831}
1832
Jeremy Hylton8caad492000-06-23 14:18:11 +00001833static int
1834dict_traverse(PyObject *op, visitproc visit, void *arg)
1835{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001836 Py_ssize_t i = 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001837 PyObject *pk;
1838 PyObject *pv;
1839
1840 while (PyDict_Next(op, &i, &pk, &pv)) {
Thomas Woutersc6e55062006-04-15 21:47:09 +00001841 Py_VISIT(pk);
1842 Py_VISIT(pv);
Jeremy Hylton8caad492000-06-23 14:18:11 +00001843 }
1844 return 0;
1845}
1846
1847static int
1848dict_tp_clear(PyObject *op)
1849{
1850 PyDict_Clear(op);
1851 return 0;
1852}
1853
Tim Petersf7f88b12000-12-13 23:18:45 +00001854
Raymond Hettinger019a1482004-03-18 02:41:19 +00001855extern PyTypeObject PyDictIterKey_Type; /* Forward */
1856extern PyTypeObject PyDictIterValue_Type; /* Forward */
1857extern PyTypeObject PyDictIterItem_Type; /* Forward */
1858static PyObject *dictiter_new(dictobject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001859
1860static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001861dict_iterkeys(dictobject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001862{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001863 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001864}
1865
1866static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001867dict_itervalues(dictobject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001868{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001869 return dictiter_new(dict, &PyDictIterValue_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001870}
1871
1872static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001873dict_iteritems(dictobject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001874{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001875 return dictiter_new(dict, &PyDictIterItem_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001876}
1877
1878
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001879PyDoc_STRVAR(has_key__doc__,
Raymond Hettinger574aa322003-09-01 22:12:08 +00001880"D.has_key(k) -> True if D has a key k, else False");
Tim Petersf7f88b12000-12-13 23:18:45 +00001881
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001882PyDoc_STRVAR(contains__doc__,
1883"D.__contains__(k) -> True if D has a key k, else False");
1884
1885PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
1886
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001887PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001888"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001889
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001890PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001891"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 +00001892
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001893PyDoc_STRVAR(pop__doc__,
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001894"D.pop(k[,d]) -> v, remove specified key and return the corresponding value\n\
1895If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00001896
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001897PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00001898"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000018992-tuple; but raise KeyError if D is empty");
Tim Petersf7f88b12000-12-13 23:18:45 +00001900
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001901PyDoc_STRVAR(keys__doc__,
1902"D.keys() -> list of D's keys");
Tim Petersf7f88b12000-12-13 23:18:45 +00001903
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001904PyDoc_STRVAR(items__doc__,
1905"D.items() -> list of D's (key, value) pairs, as 2-tuples");
Tim Petersf7f88b12000-12-13 23:18:45 +00001906
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001907PyDoc_STRVAR(values__doc__,
1908"D.values() -> list of D's values");
Tim Petersf7f88b12000-12-13 23:18:45 +00001909
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001910PyDoc_STRVAR(update__doc__,
Walter Dörwaldd70ad8a2004-05-28 20:59:21 +00001911"D.update(E, **F) -> None. Update D from E and F: for k in E: D[k] = E[k]\n\
1912(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 +00001913
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001914PyDoc_STRVAR(fromkeys__doc__,
1915"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
1916v defaults to None.");
1917
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001918PyDoc_STRVAR(clear__doc__,
1919"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001920
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001921PyDoc_STRVAR(copy__doc__,
1922"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00001923
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001924PyDoc_STRVAR(iterkeys__doc__,
1925"D.iterkeys() -> an iterator over the keys of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00001926
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001927PyDoc_STRVAR(itervalues__doc__,
1928"D.itervalues() -> an iterator over the values of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00001929
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001930PyDoc_STRVAR(iteritems__doc__,
1931"D.iteritems() -> an iterator over the (key, value) items of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00001932
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001933static PyMethodDef mapp_methods[] = {
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001934 {"__contains__",(PyCFunction)dict_has_key, METH_O | METH_COEXIST,
1935 contains__doc__},
Raymond Hettinger0c669672003-12-13 13:31:55 +00001936 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001937 getitem__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001938 {"has_key", (PyCFunction)dict_has_key, METH_O,
Tim Petersf7f88b12000-12-13 23:18:45 +00001939 has_key__doc__},
1940 {"get", (PyCFunction)dict_get, METH_VARARGS,
1941 get__doc__},
1942 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
1943 setdefault_doc__},
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001944 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
Just van Rossuma797d812002-11-23 09:45:04 +00001945 pop__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001946 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001947 popitem__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001948 {"keys", (PyCFunction)dict_keys, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001949 keys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001950 {"items", (PyCFunction)dict_items, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001951 items__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001952 {"values", (PyCFunction)dict_values, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001953 values__doc__},
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001954 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001955 update__doc__},
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001956 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
1957 fromkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001958 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001959 clear__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001960 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001961 copy__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001962 {"iterkeys", (PyCFunction)dict_iterkeys, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00001963 iterkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001964 {"itervalues", (PyCFunction)dict_itervalues, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00001965 itervalues__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001966 {"iteritems", (PyCFunction)dict_iteritems, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00001967 iteritems__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00001968 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001969};
1970
Tim Petersd770ebd2006-06-01 15:50:44 +00001971/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001972int
1973PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001974{
1975 long hash;
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001976 dictobject *mp = (dictobject *)op;
Armin Rigo35f6d362006-06-01 13:19:12 +00001977 dictentry *ep;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001978
Tim Peters0ab085c2001-09-14 00:25:33 +00001979 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001980 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001981 hash = PyObject_Hash(key);
1982 if (hash == -1)
1983 return -1;
1984 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001985 ep = (mp->ma_lookup)(mp, key, hash);
Tim Petersd770ebd2006-06-01 15:50:44 +00001986 return ep == NULL ? -1 : (ep->me_value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001987}
1988
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001989/* Hack to implement "key in dict" */
1990static PySequenceMethods dict_as_sequence = {
Georg Brandl347b3002006-03-30 11:57:00 +00001991 0, /* sq_length */
1992 0, /* sq_concat */
1993 0, /* sq_repeat */
1994 0, /* sq_item */
1995 0, /* sq_slice */
1996 0, /* sq_ass_item */
1997 0, /* sq_ass_slice */
1998 PyDict_Contains, /* sq_contains */
1999 0, /* sq_inplace_concat */
2000 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002001};
2002
Guido van Rossum09e563a2001-05-01 12:10:21 +00002003static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002004dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2005{
2006 PyObject *self;
2007
2008 assert(type != NULL && type->tp_alloc != NULL);
2009 self = type->tp_alloc(type, 0);
2010 if (self != NULL) {
2011 PyDictObject *d = (PyDictObject *)self;
2012 /* It's guaranteed that tp->alloc zeroed out the struct. */
2013 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
2014 INIT_NONZERO_DICT_SLOTS(d);
2015 d->ma_lookup = lookdict_string;
2016#ifdef SHOW_CONVERSION_COUNTS
2017 ++created;
2018#endif
2019 }
2020 return self;
2021}
2022
Tim Peters25786c02001-09-02 08:22:48 +00002023static int
2024dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2025{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002026 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002027}
2028
Guido van Rossumdbb53d92001-12-03 16:32:18 +00002029static long
2030dict_nohash(PyObject *self)
2031{
2032 PyErr_SetString(PyExc_TypeError, "dict objects are unhashable");
2033 return -1;
2034}
2035
Tim Peters6d6c1a32001-08-02 04:15:00 +00002036static PyObject *
Guido van Rossum09e563a2001-05-01 12:10:21 +00002037dict_iter(dictobject *dict)
2038{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002039 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002040}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002041
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002042PyDoc_STRVAR(dictionary_doc,
Tim Petersa427a2b2001-10-29 22:25:45 +00002043"dict() -> new empty dictionary.\n"
2044"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Tim Peters1fc240e2001-10-26 05:06:50 +00002045" (key, value) pairs.\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002046"dict(seq) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002047" d = {}\n"
2048" for k, v in seq:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002049" d[k] = v\n"
2050"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2051" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002052
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002053PyTypeObject PyDict_Type = {
2054 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002055 0,
Tim Petersa427a2b2001-10-29 22:25:45 +00002056 "dict",
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002057 sizeof(dictobject),
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002058 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00002059 (destructor)dict_dealloc, /* tp_dealloc */
2060 (printfunc)dict_print, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002061 0, /* tp_getattr */
Guido van Rossumb9324202001-01-18 00:39:02 +00002062 0, /* tp_setattr */
Tim Peters4fa58bf2001-05-10 21:45:19 +00002063 (cmpfunc)dict_compare, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00002064 (reprfunc)dict_repr, /* tp_repr */
2065 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002066 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00002067 &dict_as_mapping, /* tp_as_mapping */
Guido van Rossumdbb53d92001-12-03 16:32:18 +00002068 dict_nohash, /* tp_hash */
Guido van Rossumb9324202001-01-18 00:39:02 +00002069 0, /* tp_call */
2070 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002071 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumb9324202001-01-18 00:39:02 +00002072 0, /* tp_setattro */
2073 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002074 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Tim Peters6d6c1a32001-08-02 04:15:00 +00002075 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters25786c02001-09-02 08:22:48 +00002076 dictionary_doc, /* tp_doc */
Georg Brandl347b3002006-03-30 11:57:00 +00002077 dict_traverse, /* tp_traverse */
2078 dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00002079 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002080 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00002081 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002082 0, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002083 mapp_methods, /* tp_methods */
2084 0, /* tp_members */
2085 0, /* tp_getset */
2086 0, /* tp_base */
2087 0, /* tp_dict */
2088 0, /* tp_descr_get */
2089 0, /* tp_descr_set */
2090 0, /* tp_dictoffset */
Georg Brandl347b3002006-03-30 11:57:00 +00002091 dict_init, /* tp_init */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002092 PyType_GenericAlloc, /* tp_alloc */
2093 dict_new, /* tp_new */
Neil Schemenauer6189b892002-04-12 02:43:00 +00002094 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002095};
2096
Guido van Rossum3cca2451997-05-16 14:23:33 +00002097/* For backward compatibility with old dictionary interface */
2098
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002099PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002100PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002101{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002102 PyObject *kv, *rv;
2103 kv = PyString_FromString(key);
2104 if (kv == NULL)
2105 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002106 rv = PyDict_GetItem(v, kv);
2107 Py_DECREF(kv);
2108 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002109}
2110
2111int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002112PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002113{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002114 PyObject *kv;
2115 int err;
2116 kv = PyString_FromString(key);
2117 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002118 return -1;
Guido van Rossum4f3bf1e1997-09-29 23:31:11 +00002119 PyString_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00002120 err = PyDict_SetItem(v, kv, item);
2121 Py_DECREF(kv);
2122 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002123}
2124
2125int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002126PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002127{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002128 PyObject *kv;
2129 int err;
2130 kv = PyString_FromString(key);
2131 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002132 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002133 err = PyDict_DelItem(v, kv);
2134 Py_DECREF(kv);
2135 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002136}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002137
Raymond Hettinger019a1482004-03-18 02:41:19 +00002138/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002139
2140typedef struct {
2141 PyObject_HEAD
Guido van Rossum2147df72002-07-16 20:30:22 +00002142 dictobject *di_dict; /* Set to NULL when iterator is exhausted */
Tim Peters9b10f7e2006-05-30 04:16:25 +00002143 Py_ssize_t di_used;
2144 Py_ssize_t di_pos;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002145 PyObject* di_result; /* reusable result tuple for iteritems */
Tim Peters9b10f7e2006-05-30 04:16:25 +00002146 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002147} dictiterobject;
2148
2149static PyObject *
Raymond Hettinger019a1482004-03-18 02:41:19 +00002150dictiter_new(dictobject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002151{
2152 dictiterobject *di;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002153 di = PyObject_New(dictiterobject, itertype);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002154 if (di == NULL)
2155 return NULL;
2156 Py_INCREF(dict);
2157 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00002158 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002159 di->di_pos = 0;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002160 di->len = dict->ma_used;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002161 if (itertype == &PyDictIterItem_Type) {
2162 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2163 if (di->di_result == NULL) {
2164 Py_DECREF(di);
2165 return NULL;
2166 }
2167 }
2168 else
2169 di->di_result = NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002170 return (PyObject *)di;
2171}
2172
2173static void
2174dictiter_dealloc(dictiterobject *di)
2175{
Guido van Rossum2147df72002-07-16 20:30:22 +00002176 Py_XDECREF(di->di_dict);
Raymond Hettinger019a1482004-03-18 02:41:19 +00002177 Py_XDECREF(di->di_result);
Neil Schemenauer6189b892002-04-12 02:43:00 +00002178 PyObject_Del(di);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002179}
2180
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002181static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002182dictiter_len(dictiterobject *di)
2183{
Tim Peters9b10f7e2006-05-30 04:16:25 +00002184 Py_ssize_t len = 0;
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00002185 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002186 len = di->len;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002187 return PyInt_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002188}
2189
Armin Rigof5b3e362006-02-11 21:32:43 +00002190PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002191
2192static PyMethodDef dictiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +00002193 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002194 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002195};
2196
Raymond Hettinger019a1482004-03-18 02:41:19 +00002197static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002198{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002199 PyObject *key;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002200 register Py_ssize_t i, mask;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002201 register dictentry *ep;
2202 dictobject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002203
Raymond Hettinger019a1482004-03-18 02:41:19 +00002204 if (d == NULL)
Guido van Rossum2147df72002-07-16 20:30:22 +00002205 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002206 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002207
Raymond Hettinger019a1482004-03-18 02:41:19 +00002208 if (di->di_used != d->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00002209 PyErr_SetString(PyExc_RuntimeError,
2210 "dictionary changed size during iteration");
Guido van Rossum2147df72002-07-16 20:30:22 +00002211 di->di_used = -1; /* Make this state sticky */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002212 return NULL;
2213 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002214
Raymond Hettinger019a1482004-03-18 02:41:19 +00002215 i = di->di_pos;
2216 if (i < 0)
2217 goto fail;
2218 ep = d->ma_table;
2219 mask = d->ma_mask;
2220 while (i <= mask && ep[i].me_value == NULL)
2221 i++;
2222 di->di_pos = i+1;
2223 if (i > mask)
2224 goto fail;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002225 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002226 key = ep[i].me_key;
2227 Py_INCREF(key);
2228 return key;
2229
2230fail:
2231 Py_DECREF(d);
Guido van Rossum2147df72002-07-16 20:30:22 +00002232 di->di_dict = NULL;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002233 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002234}
2235
Raymond Hettinger019a1482004-03-18 02:41:19 +00002236PyTypeObject PyDictIterKey_Type = {
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002237 PyObject_HEAD_INIT(&PyType_Type)
2238 0, /* ob_size */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002239 "dictionary-keyiterator", /* tp_name */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002240 sizeof(dictiterobject), /* tp_basicsize */
2241 0, /* tp_itemsize */
2242 /* methods */
2243 (destructor)dictiter_dealloc, /* tp_dealloc */
2244 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002245 0, /* tp_getattr */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002246 0, /* tp_setattr */
2247 0, /* tp_compare */
2248 0, /* tp_repr */
2249 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002250 0, /* tp_as_sequence */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002251 0, /* tp_as_mapping */
2252 0, /* tp_hash */
2253 0, /* tp_call */
2254 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002255 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002256 0, /* tp_setattro */
2257 0, /* tp_as_buffer */
2258 Py_TPFLAGS_DEFAULT, /* tp_flags */
2259 0, /* tp_doc */
2260 0, /* tp_traverse */
2261 0, /* tp_clear */
2262 0, /* tp_richcompare */
2263 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002264 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002265 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002266 dictiter_methods, /* tp_methods */
2267 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002268};
2269
2270static PyObject *dictiter_iternextvalue(dictiterobject *di)
2271{
2272 PyObject *value;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002273 register Py_ssize_t i, mask;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002274 register dictentry *ep;
2275 dictobject *d = di->di_dict;
2276
2277 if (d == NULL)
2278 return NULL;
2279 assert (PyDict_Check(d));
2280
2281 if (di->di_used != d->ma_used) {
2282 PyErr_SetString(PyExc_RuntimeError,
2283 "dictionary changed size during iteration");
2284 di->di_used = -1; /* Make this state sticky */
2285 return NULL;
2286 }
2287
2288 i = di->di_pos;
Guido van Rossum09240f62004-03-20 19:11:58 +00002289 mask = d->ma_mask;
2290 if (i < 0 || i > mask)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002291 goto fail;
2292 ep = d->ma_table;
Guido van Rossum09240f62004-03-20 19:11:58 +00002293 while ((value=ep[i].me_value) == NULL) {
Raymond Hettinger019a1482004-03-18 02:41:19 +00002294 i++;
Guido van Rossum09240f62004-03-20 19:11:58 +00002295 if (i > mask)
2296 goto fail;
2297 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002298 di->di_pos = i+1;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002299 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002300 Py_INCREF(value);
2301 return value;
2302
2303fail:
2304 Py_DECREF(d);
2305 di->di_dict = NULL;
2306 return NULL;
2307}
2308
2309PyTypeObject PyDictIterValue_Type = {
2310 PyObject_HEAD_INIT(&PyType_Type)
2311 0, /* ob_size */
2312 "dictionary-valueiterator", /* tp_name */
2313 sizeof(dictiterobject), /* tp_basicsize */
2314 0, /* tp_itemsize */
2315 /* methods */
2316 (destructor)dictiter_dealloc, /* tp_dealloc */
2317 0, /* tp_print */
2318 0, /* tp_getattr */
2319 0, /* tp_setattr */
2320 0, /* tp_compare */
2321 0, /* tp_repr */
2322 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002323 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002324 0, /* tp_as_mapping */
2325 0, /* tp_hash */
2326 0, /* tp_call */
2327 0, /* tp_str */
2328 PyObject_GenericGetAttr, /* tp_getattro */
2329 0, /* tp_setattro */
2330 0, /* tp_as_buffer */
2331 Py_TPFLAGS_DEFAULT, /* tp_flags */
2332 0, /* tp_doc */
2333 0, /* tp_traverse */
2334 0, /* tp_clear */
2335 0, /* tp_richcompare */
2336 0, /* tp_weaklistoffset */
2337 PyObject_SelfIter, /* tp_iter */
2338 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002339 dictiter_methods, /* tp_methods */
2340 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002341};
2342
2343static PyObject *dictiter_iternextitem(dictiterobject *di)
2344{
2345 PyObject *key, *value, *result = di->di_result;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002346 register Py_ssize_t i, mask;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002347 register dictentry *ep;
2348 dictobject *d = di->di_dict;
2349
2350 if (d == NULL)
2351 return NULL;
2352 assert (PyDict_Check(d));
2353
2354 if (di->di_used != d->ma_used) {
2355 PyErr_SetString(PyExc_RuntimeError,
2356 "dictionary changed size during iteration");
2357 di->di_used = -1; /* Make this state sticky */
2358 return NULL;
2359 }
2360
2361 i = di->di_pos;
2362 if (i < 0)
2363 goto fail;
2364 ep = d->ma_table;
2365 mask = d->ma_mask;
2366 while (i <= mask && ep[i].me_value == NULL)
2367 i++;
2368 di->di_pos = i+1;
2369 if (i > mask)
2370 goto fail;
2371
2372 if (result->ob_refcnt == 1) {
2373 Py_INCREF(result);
2374 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2375 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2376 } else {
2377 result = PyTuple_New(2);
Tim Peters60b29962006-01-01 01:19:23 +00002378 if (result == NULL)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002379 return NULL;
2380 }
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002381 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002382 key = ep[i].me_key;
2383 value = ep[i].me_value;
2384 Py_INCREF(key);
2385 Py_INCREF(value);
2386 PyTuple_SET_ITEM(result, 0, key);
2387 PyTuple_SET_ITEM(result, 1, value);
2388 return result;
2389
2390fail:
2391 Py_DECREF(d);
2392 di->di_dict = NULL;
2393 return NULL;
2394}
2395
2396PyTypeObject PyDictIterItem_Type = {
2397 PyObject_HEAD_INIT(&PyType_Type)
2398 0, /* ob_size */
2399 "dictionary-itemiterator", /* tp_name */
2400 sizeof(dictiterobject), /* tp_basicsize */
2401 0, /* tp_itemsize */
2402 /* methods */
2403 (destructor)dictiter_dealloc, /* tp_dealloc */
2404 0, /* tp_print */
2405 0, /* tp_getattr */
2406 0, /* tp_setattr */
2407 0, /* tp_compare */
2408 0, /* tp_repr */
2409 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002410 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002411 0, /* tp_as_mapping */
2412 0, /* tp_hash */
2413 0, /* tp_call */
2414 0, /* tp_str */
2415 PyObject_GenericGetAttr, /* tp_getattro */
2416 0, /* tp_setattro */
2417 0, /* tp_as_buffer */
2418 Py_TPFLAGS_DEFAULT, /* tp_flags */
2419 0, /* tp_doc */
2420 0, /* tp_traverse */
2421 0, /* tp_clear */
2422 0, /* tp_richcompare */
2423 0, /* tp_weaklistoffset */
2424 PyObject_SelfIter, /* tp_iter */
2425 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002426 dictiter_methods, /* tp_methods */
2427 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002428};