blob: 64585a41d6384bdd3446c8e7fe566d669cf3d071 [file] [log] [blame]
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001
Guido van Rossum2bc13791999-03-24 19:06:42 +00002/* Dictionary object implementation using a hash table */
Guido van Rossum9bfef441993-03-29 10:43:31 +00003
Raymond Hettinger930427b2003-05-03 06:51:59 +00004/* The distribution includes a separate file, Objects/dictnotes.txt,
Tim Peters60b29962006-01-01 01:19:23 +00005 describing explorations into dictionary design and optimization.
Raymond Hettinger930427b2003-05-03 06:51:59 +00006 It covers typical dictionary use patterns, the parameters for
7 tuning dictionaries, and several ideas for possible optimizations.
8*/
9
Guido van Rossumc0b618a1997-05-02 03:12:38 +000010#include "Python.h"
Guido van Rossum4b1302b1993-03-27 18:11:32 +000011
Tim Peters6d6c1a32001-08-02 04:15:00 +000012typedef PyDictEntry dictentry;
13typedef PyDictObject dictobject;
Guido van Rossum16e93a81997-01-28 00:00:11 +000014
Tim Peterseb28ef22001-06-02 05:27:19 +000015/* Define this out if you don't want conversion statistics on exit. */
Fred Drake1bff34a2000-08-31 19:31:38 +000016#undef SHOW_CONVERSION_COUNTS
17
Tim Peterseb28ef22001-06-02 05:27:19 +000018/* See large comment block below. This must be >= 1. */
19#define PERTURB_SHIFT 5
20
Guido van Rossum16e93a81997-01-28 00:00:11 +000021/*
Tim Peterseb28ef22001-06-02 05:27:19 +000022Major subtleties ahead: Most hash schemes depend on having a "good" hash
23function, in the sense of simulating randomness. Python doesn't: its most
24important hash functions (for strings and ints) are very regular in common
25cases:
Tim Peters15d49292001-05-27 07:39:22 +000026
Guido van Rossumdc5f6b22006-08-24 21:29:26 +000027 >>> map(hash, (0, 1, 2, 3))
28 [0, 1, 2, 3]
29 >>> map(hash, ("namea", "nameb", "namec", "named"))
30 [-1658398457, -1658398460, -1658398459, -1658398462]
31 >>>
Tim Peters15d49292001-05-27 07:39:22 +000032
Tim Peterseb28ef22001-06-02 05:27:19 +000033This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
34the low-order i bits as the initial table index is extremely fast, and there
35are no collisions at all for dicts indexed by a contiguous range of ints.
36The same is approximately true when keys are "consecutive" strings. So this
37gives better-than-random behavior in common cases, and that's very desirable.
Tim Peters15d49292001-05-27 07:39:22 +000038
Tim Peterseb28ef22001-06-02 05:27:19 +000039OTOH, when collisions occur, the tendency to fill contiguous slices of the
40hash table makes a good collision resolution strategy crucial. Taking only
41the last i bits of the hash code is also vulnerable: for example, consider
Guido van Rossumdc5f6b22006-08-24 21:29:26 +000042the list [i << 16 for i in range(20000)] as a set of keys. Since ints are
43their own hash codes, and this fits in a dict of size 2**15, the last 15 bits
44 of every hash code are all 0: they *all* map to the same table index.
Tim Peters15d49292001-05-27 07:39:22 +000045
Tim Peterseb28ef22001-06-02 05:27:19 +000046But catering to unusual cases should not slow the usual ones, so we just take
47the last i bits anyway. It's up to collision resolution to do the rest. If
48we *usually* find the key we're looking for on the first try (and, it turns
49out, we usually do -- the table load factor is kept under 2/3, so the odds
50are solidly in our favor), then it makes best sense to keep the initial index
51computation dirt cheap.
Tim Peters15d49292001-05-27 07:39:22 +000052
Tim Peterseb28ef22001-06-02 05:27:19 +000053The first half of collision resolution is to visit table indices via this
54recurrence:
Tim Peters15d49292001-05-27 07:39:22 +000055
Tim Peterseb28ef22001-06-02 05:27:19 +000056 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +000057
Tim Peterseb28ef22001-06-02 05:27:19 +000058For any initial j in range(2**i), repeating that 2**i times generates each
59int in range(2**i) exactly once (see any text on random-number generation for
60proof). By itself, this doesn't help much: like linear probing (setting
61j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
62order. This would be bad, except that's not the only thing we do, and it's
63actually *good* in the common cases where hash keys are consecutive. In an
64example that's really too small to make this entirely clear, for a table of
65size 2**3 the order of indices is:
Tim Peters15d49292001-05-27 07:39:22 +000066
Tim Peterseb28ef22001-06-02 05:27:19 +000067 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
68
69If two things come in at index 5, the first place we look after is index 2,
70not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
71Linear probing is deadly in this case because there the fixed probe order
72is the *same* as the order consecutive keys are likely to arrive. But it's
73extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
74and certain that consecutive hash codes do not.
75
76The other half of the strategy is to get the other bits of the hash code
77into play. This is done by initializing a (unsigned) vrbl "perturb" to the
78full hash code, and changing the recurrence to:
79
80 j = (5*j) + 1 + perturb;
81 perturb >>= PERTURB_SHIFT;
82 use j % 2**i as the next table index;
83
84Now the probe sequence depends (eventually) on every bit in the hash code,
85and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
86because it quickly magnifies small differences in the bits that didn't affect
87the initial index. Note that because perturb is unsigned, if the recurrence
88is executed often enough perturb eventually becomes and remains 0. At that
89point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
90that's certain to find an empty slot eventually (since it generates every int
91in range(2**i), and we make sure there's always at least one empty slot).
92
93Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
94small so that the high bits of the hash code continue to affect the probe
95sequence across iterations; but you want it large so that in really bad cases
96the high-order hash bits have an effect on early iterations. 5 was "the
97best" in minimizing total collisions across experiments Tim Peters ran (on
98both normal and pathological cases), but 4 and 6 weren't significantly worse.
99
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000100Historical: Reimer Behrends contributed the idea of using a polynomial-based
Tim Peterseb28ef22001-06-02 05:27:19 +0000101approach, using repeated multiplication by x in GF(2**n) where an irreducible
102polynomial for each table size was chosen such that x was a primitive root.
103Christian Tismer later extended that to use division by x instead, as an
104efficient way to get the high bits of the hash code into play. This scheme
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000105also gave excellent collision statistics, but was more expensive: two if-tests
106were required inside the loop; computing "the next" index took about the same
107number of operations but without as much potential parallelism (e.g.,
108computing 5*j can go on at the same time as computing 1+perturb in the above,
109and then shifting perturb can be done while the table index is being masked);
110and the dictobject struct required a member to hold the table's polynomial.
111In Tim's experiments the current scheme ran faster, produced equally good
112collision statistics, needed less code & used less memory.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000113
114Theoretical Python 2.5 headache: hash codes are only C "long", but
115sizeof(Py_ssize_t) > sizeof(long) may be possible. In that case, and if a
116dict is genuinely huge, then only the slots directly reachable via indexing
117by a C long can be the first slot in a probe sequence. The probe sequence
118will still eventually reach every slot in the table, but the collision rate
119on initial probes may be much higher than this scheme was designed for.
120Getting a hash code as fat as Py_ssize_t is the only real cure. But in
121practice, this probably won't make a lick of difference for many years (at
122which point everyone will have terabytes of RAM on 64-bit boxes).
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000123*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000124
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000125/* Object used as dummy key to fill deleted entries */
Raymond Hettingerf81e4502005-08-17 02:19:36 +0000126static PyObject *dummy = NULL; /* Initialized by first call to newdictobject() */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000127
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000128#ifdef Py_REF_DEBUG
129PyObject *
130_PyDict_Dummy(void)
131{
132 return dummy;
133}
134#endif
135
Fred Drake1bff34a2000-08-31 19:31:38 +0000136/* forward declarations */
137static dictentry *
138lookdict_string(dictobject *mp, PyObject *key, long hash);
139
140#ifdef SHOW_CONVERSION_COUNTS
141static long created = 0L;
142static long converted = 0L;
143
144static void
145show_counts(void)
146{
147 fprintf(stderr, "created %ld string dicts\n", created);
148 fprintf(stderr, "converted %ld to normal dicts\n", converted);
149 fprintf(stderr, "%.2f%% conversion rate\n", (100.0*converted)/created);
150}
151#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000152
Tim Peters6d6c1a32001-08-02 04:15:00 +0000153/* Initialization macros.
154 There are two ways to create a dict: PyDict_New() is the main C API
155 function, and the tp_new slot maps to dict_new(). In the latter case we
156 can save a little time over what PyDict_New does because it's guaranteed
157 that the PyDictObject struct is already zeroed out.
158 Everyone except dict_new() should use EMPTY_TO_MINSIZE (unless they have
159 an excellent reason not to).
160*/
161
162#define INIT_NONZERO_DICT_SLOTS(mp) do { \
Tim Petersdea48ec2001-05-22 20:40:22 +0000163 (mp)->ma_table = (mp)->ma_smalltable; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000164 (mp)->ma_mask = PyDict_MINSIZE - 1; \
165 } while(0)
166
167#define EMPTY_TO_MINSIZE(mp) do { \
168 memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000169 (mp)->ma_used = (mp)->ma_fill = 0; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000170 INIT_NONZERO_DICT_SLOTS(mp); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000171 } while(0)
172
Raymond Hettinger43442782004-03-17 21:55:03 +0000173/* Dictionary reuse scheme to save calls to malloc, free, and memset */
174#define MAXFREEDICTS 80
175static PyDictObject *free_dicts[MAXFREEDICTS];
176static int num_free_dicts = 0;
177
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000178PyObject *
Thomas Wouters78890102000-07-22 19:25:51 +0000179PyDict_New(void)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000180{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000181 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000182 if (dummy == NULL) { /* Auto-initialize dummy */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000183 dummy = PyString_FromString("<dummy key>");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000184 if (dummy == NULL)
185 return NULL;
Fred Drake1bff34a2000-08-31 19:31:38 +0000186#ifdef SHOW_CONVERSION_COUNTS
187 Py_AtExit(show_counts);
188#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000189 }
Raymond Hettinger43442782004-03-17 21:55:03 +0000190 if (num_free_dicts) {
191 mp = free_dicts[--num_free_dicts];
192 assert (mp != NULL);
193 assert (mp->ob_type == &PyDict_Type);
194 _Py_NewReference((PyObject *)mp);
195 if (mp->ma_fill) {
196 EMPTY_TO_MINSIZE(mp);
197 }
198 assert (mp->ma_used == 0);
199 assert (mp->ma_table == mp->ma_smalltable);
200 assert (mp->ma_mask == PyDict_MINSIZE - 1);
201 } else {
202 mp = PyObject_GC_New(dictobject, &PyDict_Type);
203 if (mp == NULL)
204 return NULL;
205 EMPTY_TO_MINSIZE(mp);
206 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000207 mp->ma_lookup = lookdict_string;
208#ifdef SHOW_CONVERSION_COUNTS
209 ++created;
210#endif
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000211 _PyObject_GC_TRACK(mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000212 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000213}
214
215/*
216The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000217This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000218Open addressing is preferred over chaining since the link overhead for
219chaining would be substantial (100% with typical malloc overhead).
220
Tim Peterseb28ef22001-06-02 05:27:19 +0000221The initial probe index is computed as hash mod the table size. Subsequent
222probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000223
224All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000225
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000226The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000227contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000228Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000229
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000230lookdict() is general-purpose, and may return NULL if (and only if) a
231comparison raises an exception (this was new in Python 2.5).
232lookdict_string() below is specialized to string keys, comparison of which can
233never raise an exception; that function can never return NULL. For both, when
234the key isn't found a dictentry* is returned for which the me_value field is
235NULL; this is the slot in the dict at which the key would have been found, and
236the caller can (if it wishes) add the <key, value> pair to the returned
237dictentry*.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000238*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000239static dictentry *
Tim Peters1f5871e2000-07-04 17:44:48 +0000240lookdict(dictobject *mp, PyObject *key, register long hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000241{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000242 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000243 register size_t perturb;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000244 register dictentry *freeslot;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000245 register size_t mask = (size_t)mp->ma_mask;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000246 dictentry *ep0 = mp->ma_table;
247 register dictentry *ep;
Fred Drakec88b99c2000-08-31 19:04:07 +0000248 register int cmp;
Tim Peters453163d2001-06-03 04:54:32 +0000249 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000250
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000251 i = (size_t)hash & mask;
Guido van Rossumefb46091997-01-29 15:53:56 +0000252 ep = &ep0[i];
Guido van Rossumc1c7b1a1998-10-06 16:01:14 +0000253 if (ep->me_key == NULL || ep->me_key == key)
Guido van Rossum7d181591997-01-16 21:06:45 +0000254 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000255
Guido van Rossum16e93a81997-01-28 00:00:11 +0000256 if (ep->me_key == dummy)
Guido van Rossum7d181591997-01-16 21:06:45 +0000257 freeslot = ep;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000258 else {
Fred Drakec88b99c2000-08-31 19:04:07 +0000259 if (ep->me_hash == hash) {
Tim Peters453163d2001-06-03 04:54:32 +0000260 startkey = ep->me_key;
261 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000262 if (cmp < 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000263 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000264 if (ep0 == mp->ma_table && ep->me_key == startkey) {
265 if (cmp > 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000266 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000267 }
268 else {
269 /* The compare did major nasty stuff to the
270 * dict: start over.
271 * XXX A clever adversary could prevent this
272 * XXX from terminating.
273 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000274 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000275 }
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000276 }
277 freeslot = NULL;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000278 }
Tim Peters15d49292001-05-27 07:39:22 +0000279
Tim Peters342c65e2001-05-13 06:43:53 +0000280 /* In the loop, me_key == dummy is by far (factor of 100s) the
281 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000282 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
283 i = (i << 2) + i + perturb + 1;
284 ep = &ep0[i & mask];
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000285 if (ep->me_key == NULL)
286 return freeslot == NULL ? ep : freeslot;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000287 if (ep->me_key == key)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000288 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000289 if (ep->me_hash == hash && ep->me_key != dummy) {
Tim Peters453163d2001-06-03 04:54:32 +0000290 startkey = ep->me_key;
291 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000292 if (cmp < 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000293 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000294 if (ep0 == mp->ma_table && ep->me_key == startkey) {
295 if (cmp > 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000296 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000297 }
298 else {
299 /* The compare did major nasty stuff to the
300 * dict: start over.
301 * XXX A clever adversary could prevent this
302 * XXX from terminating.
303 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000304 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000305 }
Guido van Rossum16e93a81997-01-28 00:00:11 +0000306 }
Tim Peters342c65e2001-05-13 06:43:53 +0000307 else if (ep->me_key == dummy && freeslot == NULL)
308 freeslot = ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000309 }
310}
311
312/*
Fred Drake1bff34a2000-08-31 19:31:38 +0000313 * Hacked up version of lookdict which can assume keys are always strings;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000314 * this assumption allows testing for errors during PyObject_RichCompareBool()
315 * to be dropped; string-string comparisons never raise exceptions. This also
316 * means we don't need to go through PyObject_RichCompareBool(); we can always
317 * use _PyString_Eq() directly.
Fred Drake1bff34a2000-08-31 19:31:38 +0000318 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000319 * This is valuable because dicts with only string keys are very common.
Fred Drake1bff34a2000-08-31 19:31:38 +0000320 */
321static dictentry *
322lookdict_string(dictobject *mp, PyObject *key, register long hash)
323{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000324 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000325 register size_t perturb;
Fred Drake1bff34a2000-08-31 19:31:38 +0000326 register dictentry *freeslot;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000327 register size_t mask = (size_t)mp->ma_mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000328 dictentry *ep0 = mp->ma_table;
329 register dictentry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000330
Tim Peters0ab085c2001-09-14 00:25:33 +0000331 /* Make sure this function doesn't have to handle non-string keys,
332 including subclasses of str; e.g., one reason to subclass
333 strings is to override __eq__, and for speed we don't cater to
334 that here. */
335 if (!PyString_CheckExact(key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000336#ifdef SHOW_CONVERSION_COUNTS
337 ++converted;
338#endif
339 mp->ma_lookup = lookdict;
340 return lookdict(mp, key, hash);
341 }
Tim Peters2f228e72001-05-13 00:19:31 +0000342 i = hash & mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000343 ep = &ep0[i];
344 if (ep->me_key == NULL || ep->me_key == key)
345 return ep;
346 if (ep->me_key == dummy)
347 freeslot = ep;
348 else {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000349 if (ep->me_hash == hash && _PyString_Eq(ep->me_key, key))
Fred Drake1bff34a2000-08-31 19:31:38 +0000350 return ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000351 freeslot = NULL;
352 }
Tim Peters15d49292001-05-27 07:39:22 +0000353
Tim Peters342c65e2001-05-13 06:43:53 +0000354 /* In the loop, me_key == dummy is by far (factor of 100s) the
355 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000356 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
357 i = (i << 2) + i + perturb + 1;
358 ep = &ep0[i & mask];
Tim Peters342c65e2001-05-13 06:43:53 +0000359 if (ep->me_key == NULL)
360 return freeslot == NULL ? ep : freeslot;
361 if (ep->me_key == key
362 || (ep->me_hash == hash
363 && ep->me_key != dummy
Martin v. Löwiscd353062001-05-24 16:56:35 +0000364 && _PyString_Eq(ep->me_key, key)))
Fred Drake1bff34a2000-08-31 19:31:38 +0000365 return ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000366 if (ep->me_key == dummy && freeslot == NULL)
Tim Peters342c65e2001-05-13 06:43:53 +0000367 freeslot = ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000368 }
369}
370
371/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000372Internal routine to insert a new item into the table.
373Used both by the internal resize routine and by the public insert routine.
374Eats a reference to key and one to value.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000375Returns -1 if an error occurred, or 0 on success.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000376*/
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000377static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000378insertdict(register dictobject *mp, PyObject *key, long hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000379{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000380 PyObject *old_value;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000381 register dictentry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000382 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
383
384 assert(mp->ma_lookup != NULL);
385 ep = mp->ma_lookup(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000386 if (ep == NULL) {
387 Py_DECREF(key);
388 Py_DECREF(value);
389 return -1;
390 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000391 if (ep->me_value != NULL) {
Guido van Rossumd7047b31995-01-02 19:07:15 +0000392 old_value = ep->me_value;
393 ep->me_value = value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000394 Py_DECREF(old_value); /* which **CAN** re-enter */
395 Py_DECREF(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000396 }
397 else {
398 if (ep->me_key == NULL)
399 mp->ma_fill++;
Raymond Hettinger07ead172005-02-05 23:42:57 +0000400 else {
401 assert(ep->me_key == dummy);
402 Py_DECREF(dummy);
403 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000404 ep->me_key = key;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000405 ep->me_hash = (Py_ssize_t)hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000406 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000407 mp->ma_used++;
408 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000409 return 0;
410}
411
412/*
413Internal routine used by dictresize() to insert an item which is
414known to be absent from the dict. This routine also assumes that
415the dict contains no deleted entries. Besides the performance benefit,
416using insertdict() in dictresize() is dangerous (SF bug #1456209).
417Note that no refcounts are changed by this routine; if needed, the caller
418is responsible for incref'ing `key` and `value`.
419*/
420static void
421insertdict_clean(register dictobject *mp, PyObject *key, long hash,
422 PyObject *value)
423{
424 register size_t i;
425 register size_t perturb;
426 register size_t mask = (size_t)mp->ma_mask;
427 dictentry *ep0 = mp->ma_table;
428 register dictentry *ep;
429
430 i = hash & mask;
431 ep = &ep0[i];
432 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
433 i = (i << 2) + i + perturb + 1;
434 ep = &ep0[i & mask];
435 }
436 assert(ep->me_value == NULL);
437 mp->ma_fill++;
438 ep->me_key = key;
439 ep->me_hash = (Py_ssize_t)hash;
440 ep->me_value = value;
441 mp->ma_used++;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000442}
443
444/*
445Restructure the table by allocating a new table and reinserting all
446items again. When entries have been deleted, the new table may
447actually be smaller than the old one.
448*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000449static int
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000450dictresize(dictobject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000451{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000452 Py_ssize_t newsize;
Tim Peters0c6010b2001-05-23 23:33:57 +0000453 dictentry *oldtable, *newtable, *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000454 Py_ssize_t i;
Tim Peters0c6010b2001-05-23 23:33:57 +0000455 int is_oldtable_malloced;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000456 dictentry small_copy[PyDict_MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000457
458 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000459
Tim Peterseb28ef22001-06-02 05:27:19 +0000460 /* Find the smallest table size > minused. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000461 for (newsize = PyDict_MINSIZE;
Tim Peterseb28ef22001-06-02 05:27:19 +0000462 newsize <= minused && newsize > 0;
463 newsize <<= 1)
464 ;
465 if (newsize <= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000466 PyErr_NoMemory();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000467 return -1;
468 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000469
470 /* Get space for a new table. */
471 oldtable = mp->ma_table;
472 assert(oldtable != NULL);
473 is_oldtable_malloced = oldtable != mp->ma_smalltable;
474
Tim Peters6d6c1a32001-08-02 04:15:00 +0000475 if (newsize == PyDict_MINSIZE) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000476 /* A large table is shrinking, or we can't get any smaller. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000477 newtable = mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000478 if (newtable == oldtable) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000479 if (mp->ma_fill == mp->ma_used) {
480 /* No dummies, so no point doing anything. */
Tim Peters0c6010b2001-05-23 23:33:57 +0000481 return 0;
Tim Petersf8a548c2001-05-24 16:26:40 +0000482 }
483 /* We're not going to resize it, but rebuild the
484 table anyway to purge old dummy entries.
485 Subtle: This is *necessary* if fill==size,
486 as lookdict needs at least one virgin slot to
487 terminate failing searches. If fill < size, it's
488 merely desirable, as dummies slow searches. */
489 assert(mp->ma_fill > mp->ma_used);
Tim Peters0c6010b2001-05-23 23:33:57 +0000490 memcpy(small_copy, oldtable, sizeof(small_copy));
491 oldtable = small_copy;
492 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000493 }
494 else {
495 newtable = PyMem_NEW(dictentry, newsize);
496 if (newtable == NULL) {
497 PyErr_NoMemory();
498 return -1;
499 }
500 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000501
502 /* Make the dict empty, using the new table. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000503 assert(newtable != oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000504 mp->ma_table = newtable;
Tim Petersafb6ae82001-06-04 21:00:21 +0000505 mp->ma_mask = newsize - 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000506 memset(newtable, 0, sizeof(dictentry) * newsize);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000507 mp->ma_used = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000508 i = mp->ma_fill;
509 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000510
Tim Peters19283142001-05-17 22:25:34 +0000511 /* Copy the data over; this is refcount-neutral for active entries;
512 dummy entries aren't copied over, of course */
Tim Petersdea48ec2001-05-22 20:40:22 +0000513 for (ep = oldtable; i > 0; ep++) {
514 if (ep->me_value != NULL) { /* active entry */
515 --i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000516 insertdict_clean(mp, ep->me_key, (long)ep->me_hash,
517 ep->me_value);
Tim Petersdea48ec2001-05-22 20:40:22 +0000518 }
Tim Peters19283142001-05-17 22:25:34 +0000519 else if (ep->me_key != NULL) { /* dummy entry */
Tim Petersdea48ec2001-05-22 20:40:22 +0000520 --i;
Tim Peters19283142001-05-17 22:25:34 +0000521 assert(ep->me_key == dummy);
522 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000523 }
Tim Peters19283142001-05-17 22:25:34 +0000524 /* else key == value == NULL: nothing to do */
Guido van Rossumd7047b31995-01-02 19:07:15 +0000525 }
526
Tim Peters0c6010b2001-05-23 23:33:57 +0000527 if (is_oldtable_malloced)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000528 PyMem_DEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000529 return 0;
530}
531
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000532/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
533 * that may occur (originally dicts supported only string keys, and exceptions
534 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000535 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000536 * (suppressed) occurred while computing the key's hash, or that some error
537 * (suppressed) occurred when comparing keys in the dict's internal probe
538 * sequence. A nasty example of the latter is when a Python-coded comparison
539 * function hits a stack-depth error, which can cause this to return NULL
540 * even if the key is present.
541 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000542PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000543PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000544{
545 long hash;
Fred Drake1bff34a2000-08-31 19:31:38 +0000546 dictobject *mp = (dictobject *)op;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000547 dictentry *ep;
548 PyThreadState *tstate;
549 if (!PyDict_Check(op))
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000550 return NULL;
Tim Peters0ab085c2001-09-14 00:25:33 +0000551 if (!PyString_CheckExact(key) ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000552 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Guido van Rossumca756f21997-01-23 19:39:29 +0000553 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000554 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000555 if (hash == -1) {
556 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000557 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000558 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000559 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000560
561 /* We can arrive here with a NULL tstate during initialization:
562 try running "python -Wi" for an example related to string
563 interning. Let's just hope that no exception occurs then... */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000564 tstate = _PyThreadState_Current;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000565 if (tstate != NULL && tstate->curexc_type != NULL) {
566 /* preserve the existing exception */
567 PyObject *err_type, *err_value, *err_tb;
568 PyErr_Fetch(&err_type, &err_value, &err_tb);
569 ep = (mp->ma_lookup)(mp, key, hash);
570 /* ignore errors */
571 PyErr_Restore(err_type, err_value, err_tb);
572 if (ep == NULL)
573 return NULL;
574 }
575 else {
576 ep = (mp->ma_lookup)(mp, key, hash);
577 if (ep == NULL) {
578 PyErr_Clear();
579 return NULL;
580 }
581 }
582 return ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000583}
584
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000585/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
586 This returns NULL *with* an exception set if an exception occurred.
587 It returns NULL *without* an exception set if the key wasn't present.
588*/
589PyObject *
590PyDict_GetItemWithError(PyObject *op, PyObject *key)
591{
592 long hash;
593 dictobject *mp = (dictobject *)op;
594 dictentry *ep;
595
596 if (!PyDict_Check(op)) {
597 PyErr_BadInternalCall();
598 return NULL;
599 }
600 if (!PyString_CheckExact(key) ||
601 (hash = ((PyStringObject *) key)->ob_shash) == -1)
602 {
603 hash = PyObject_Hash(key);
604 if (hash == -1) {
605 return NULL;
606 }
607 }
608
609 ep = (mp->ma_lookup)(mp, key, hash);
610 if (ep == NULL)
611 return NULL;
612 return ep->me_value;
613}
614
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000615/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
Tim Peters60b29962006-01-01 01:19:23 +0000616 * dictionary if it's merely replacing the value for an existing key.
617 * This means that it's safe to loop over a dictionary with PyDict_Next()
618 * and occasionally replace a value -- but you can't insert new keys or
619 * remove them.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000620 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000621int
Tim Peters1f5871e2000-07-04 17:44:48 +0000622PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000623{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000624 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000625 register long hash;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000626 register Py_ssize_t n_used;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000627
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000628 if (!PyDict_Check(op)) {
629 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000630 return -1;
631 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000632 assert(key);
633 assert(value);
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000634 mp = (dictobject *)op;
Tim Peters0ab085c2001-09-14 00:25:33 +0000635 if (PyString_CheckExact(key)) {
Guido van Rossum45ec02a2002-08-19 21:43:18 +0000636 hash = ((PyStringObject *)key)->ob_shash;
637 if (hash == -1)
638 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000639 }
Tim Peters1f7df352002-03-29 03:29:08 +0000640 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000641 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000642 if (hash == -1)
643 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000644 }
Tim Petersafb6ae82001-06-04 21:00:21 +0000645 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000646 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000647 Py_INCREF(value);
648 Py_INCREF(key);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000649 if (insertdict(mp, key, hash, value) != 0)
650 return -1;
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000651 /* If we added a key, we can safely resize. Otherwise just return!
652 * If fill >= 2/3 size, adjust size. Normally, this doubles or
653 * quaduples the size, but it's also possible for the dict to shrink
Tim Peters60b29962006-01-01 01:19:23 +0000654 * (if ma_fill is much larger than ma_used, meaning a lot of dict
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000655 * keys have been * deleted).
Tim Peters60b29962006-01-01 01:19:23 +0000656 *
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000657 * Quadrupling the size improves average dictionary sparseness
658 * (reducing collisions) at the cost of some memory and iteration
659 * speed (which loops over every possible entry). It also halves
660 * the number of expensive resize operations in a growing dictionary.
Tim Peters60b29962006-01-01 01:19:23 +0000661 *
662 * Very large dictionaries (over 50K items) use doubling instead.
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000663 * This may help applications with severe memory constraints.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000664 */
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000665 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
666 return 0;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000667 return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000668}
669
670int
Tim Peters1f5871e2000-07-04 17:44:48 +0000671PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000672{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000673 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000674 register long hash;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000675 register dictentry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000676 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000677
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000678 if (!PyDict_Check(op)) {
679 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000680 return -1;
681 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000682 assert(key);
Tim Peters0ab085c2001-09-14 00:25:33 +0000683 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +0000684 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000685 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000686 if (hash == -1)
687 return -1;
688 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000689 mp = (dictobject *)op;
Fred Drake1bff34a2000-08-31 19:31:38 +0000690 ep = (mp->ma_lookup)(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000691 if (ep == NULL)
692 return -1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000693 if (ep->me_value == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000694 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000695 return -1;
696 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000697 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000698 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000699 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000700 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000701 ep->me_value = NULL;
702 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000703 Py_DECREF(old_value);
704 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000705 return 0;
706}
707
Guido van Rossum25831651993-05-19 14:50:45 +0000708void
Tim Peters1f5871e2000-07-04 17:44:48 +0000709PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000710{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000711 dictobject *mp;
Tim Petersdea48ec2001-05-22 20:40:22 +0000712 dictentry *ep, *table;
713 int table_is_malloced;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000714 Py_ssize_t fill;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000715 dictentry small_copy[PyDict_MINSIZE];
Tim Petersdea48ec2001-05-22 20:40:22 +0000716#ifdef Py_DEBUG
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000717 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +0000718#endif
719
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000720 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000721 return;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000722 mp = (dictobject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000723#ifdef Py_DEBUG
Tim Petersafb6ae82001-06-04 21:00:21 +0000724 n = mp->ma_mask + 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000725 i = 0;
726#endif
727
728 table = mp->ma_table;
729 assert(table != NULL);
730 table_is_malloced = table != mp->ma_smalltable;
731
732 /* This is delicate. During the process of clearing the dict,
733 * decrefs can cause the dict to mutate. To avoid fatal confusion
734 * (voice of experience), we have to make the dict empty before
735 * clearing the slots, and never refer to anything via mp->xxx while
736 * clearing.
737 */
738 fill = mp->ma_fill;
739 if (table_is_malloced)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000740 EMPTY_TO_MINSIZE(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000741
742 else if (fill > 0) {
743 /* It's a small table with something that needs to be cleared.
744 * Afraid the only safe way is to copy the dict entries into
745 * another small table first.
746 */
747 memcpy(small_copy, table, sizeof(small_copy));
748 table = small_copy;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000749 EMPTY_TO_MINSIZE(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000750 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000751 /* else it's a small table that's already empty */
752
753 /* Now we can finally clear things. If C had refcounts, we could
754 * assert that the refcount on table is 1 now, i.e. that this function
755 * has unique access to it, so decref side-effects can't alter it.
756 */
757 for (ep = table; fill > 0; ++ep) {
758#ifdef Py_DEBUG
759 assert(i < n);
760 ++i;
761#endif
762 if (ep->me_key) {
763 --fill;
764 Py_DECREF(ep->me_key);
765 Py_XDECREF(ep->me_value);
766 }
767#ifdef Py_DEBUG
768 else
769 assert(ep->me_value == NULL);
770#endif
771 }
772
773 if (table_is_malloced)
774 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000775}
776
Tim Peters080c88b2003-02-15 03:01:11 +0000777/*
778 * Iterate over a dict. Use like so:
779 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000780 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +0000781 * PyObject *key, *value;
782 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +0000783 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +0000784 * Refer to borrowed references in key and value.
785 * }
786 *
787 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +0000788 * mutates the dict. One exception: it is safe if the loop merely changes
789 * the values associated with the keys (but doesn't insert new keys or
790 * delete keys), via PyDict_SetItem().
791 */
Guido van Rossum25831651993-05-19 14:50:45 +0000792int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000793PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000794{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000795 register Py_ssize_t i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000796 register Py_ssize_t mask;
Raymond Hettinger43442782004-03-17 21:55:03 +0000797 register dictentry *ep;
798
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000799 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000800 return 0;
Guido van Rossum25831651993-05-19 14:50:45 +0000801 i = *ppos;
802 if (i < 0)
803 return 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000804 ep = ((dictobject *)op)->ma_table;
805 mask = ((dictobject *)op)->ma_mask;
806 while (i <= mask && ep[i].me_value == NULL)
Guido van Rossum25831651993-05-19 14:50:45 +0000807 i++;
808 *ppos = i+1;
Raymond Hettinger43442782004-03-17 21:55:03 +0000809 if (i > mask)
Guido van Rossum25831651993-05-19 14:50:45 +0000810 return 0;
811 if (pkey)
Raymond Hettinger43442782004-03-17 21:55:03 +0000812 *pkey = ep[i].me_key;
Guido van Rossum25831651993-05-19 14:50:45 +0000813 if (pvalue)
Raymond Hettinger43442782004-03-17 21:55:03 +0000814 *pvalue = ep[i].me_value;
Guido van Rossum25831651993-05-19 14:50:45 +0000815 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000816}
817
818/* Methods */
819
820static void
Tim Peters1f5871e2000-07-04 17:44:48 +0000821dict_dealloc(register dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000822{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000823 register dictentry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000824 Py_ssize_t fill = mp->ma_fill;
Guido van Rossumff413af2002-03-28 20:34:59 +0000825 PyObject_GC_UnTrack(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000826 Py_TRASHCAN_SAFE_BEGIN(mp)
Tim Petersdea48ec2001-05-22 20:40:22 +0000827 for (ep = mp->ma_table; fill > 0; ep++) {
828 if (ep->me_key) {
829 --fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000830 Py_DECREF(ep->me_key);
Tim Petersdea48ec2001-05-22 20:40:22 +0000831 Py_XDECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000832 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000833 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000834 if (mp->ma_table != mp->ma_smalltable)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000835 PyMem_DEL(mp->ma_table);
Raymond Hettinger43442782004-03-17 21:55:03 +0000836 if (num_free_dicts < MAXFREEDICTS && mp->ob_type == &PyDict_Type)
837 free_dicts[num_free_dicts++] = mp;
Tim Peters60b29962006-01-01 01:19:23 +0000838 else
Raymond Hettinger43442782004-03-17 21:55:03 +0000839 mp->ob_type->tp_free((PyObject *)mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000840 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000841}
842
843static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000844dict_print(register dictobject *mp, register FILE *fp, register int flags)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000845{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000846 register Py_ssize_t i;
847 register Py_ssize_t any;
848 int status;
Guido van Rossum255443b1998-04-10 22:47:14 +0000849
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000850 status = Py_ReprEnter((PyObject*)mp);
851 if (status != 0) {
852 if (status < 0)
853 return status;
Guido van Rossum255443b1998-04-10 22:47:14 +0000854 fprintf(fp, "{...}");
855 return 0;
856 }
857
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000858 fprintf(fp, "{");
859 any = 0;
Tim Petersafb6ae82001-06-04 21:00:21 +0000860 for (i = 0; i <= mp->ma_mask; i++) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000861 dictentry *ep = mp->ma_table + i;
862 PyObject *pvalue = ep->me_value;
863 if (pvalue != NULL) {
864 /* Prevent PyObject_Repr from deleting value during
865 key format */
866 Py_INCREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000867 if (any++ > 0)
868 fprintf(fp, ", ");
Guido van Rossum255443b1998-04-10 22:47:14 +0000869 if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000870 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000871 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000872 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000873 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000874 fprintf(fp, ": ");
Tim Peters19b77cf2001-06-02 08:27:39 +0000875 if (PyObject_Print(pvalue, fp, 0) != 0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000876 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000877 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000878 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000879 }
Tim Peters23cf6be2001-06-02 08:02:56 +0000880 Py_DECREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000881 }
882 }
883 fprintf(fp, "}");
Guido van Rossum255443b1998-04-10 22:47:14 +0000884 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000885 return 0;
886}
887
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000888static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000889dict_repr(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000890{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000891 Py_ssize_t i;
Tim Petersa7259592001-06-16 05:11:17 +0000892 PyObject *s, *temp, *colon = NULL;
893 PyObject *pieces = NULL, *result = NULL;
894 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +0000895
Tim Petersa7259592001-06-16 05:11:17 +0000896 i = Py_ReprEnter((PyObject *)mp);
Guido van Rossum255443b1998-04-10 22:47:14 +0000897 if (i != 0) {
Tim Petersa7259592001-06-16 05:11:17 +0000898 return i > 0 ? PyString_FromString("{...}") : NULL;
Guido van Rossum255443b1998-04-10 22:47:14 +0000899 }
900
Tim Petersa7259592001-06-16 05:11:17 +0000901 if (mp->ma_used == 0) {
902 result = PyString_FromString("{}");
903 goto Done;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000904 }
Tim Petersa7259592001-06-16 05:11:17 +0000905
906 pieces = PyList_New(0);
907 if (pieces == NULL)
908 goto Done;
909
910 colon = PyString_FromString(": ");
911 if (colon == NULL)
912 goto Done;
913
914 /* Do repr() on each key+value pair, and insert ": " between them.
915 Note that repr may mutate the dict. */
Tim Petersc6057842001-06-16 07:52:53 +0000916 i = 0;
917 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Tim Petersa7259592001-06-16 05:11:17 +0000918 int status;
919 /* Prevent repr from deleting value during key format. */
920 Py_INCREF(value);
921 s = PyObject_Repr(key);
922 PyString_Concat(&s, colon);
923 PyString_ConcatAndDel(&s, PyObject_Repr(value));
924 Py_DECREF(value);
925 if (s == NULL)
926 goto Done;
927 status = PyList_Append(pieces, s);
928 Py_DECREF(s); /* append created a new ref */
929 if (status < 0)
930 goto Done;
931 }
932
933 /* Add "{}" decorations to the first and last items. */
934 assert(PyList_GET_SIZE(pieces) > 0);
935 s = PyString_FromString("{");
936 if (s == NULL)
937 goto Done;
938 temp = PyList_GET_ITEM(pieces, 0);
939 PyString_ConcatAndDel(&s, temp);
940 PyList_SET_ITEM(pieces, 0, s);
941 if (s == NULL)
942 goto Done;
943
944 s = PyString_FromString("}");
945 if (s == NULL)
946 goto Done;
947 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
948 PyString_ConcatAndDel(&temp, s);
949 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
950 if (temp == NULL)
951 goto Done;
952
953 /* Paste them all together with ", " between. */
954 s = PyString_FromString(", ");
955 if (s == NULL)
956 goto Done;
957 result = _PyString_Join(s, pieces);
Tim Peters0ab085c2001-09-14 00:25:33 +0000958 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +0000959
960Done:
961 Py_XDECREF(pieces);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000962 Py_XDECREF(colon);
Tim Petersa7259592001-06-16 05:11:17 +0000963 Py_ReprLeave((PyObject *)mp);
964 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000965}
966
Martin v. Löwis18e16552006-02-15 17:27:45 +0000967static Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +0000968dict_length(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000969{
970 return mp->ma_used;
971}
972
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000973static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000974dict_subscript(dictobject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000975{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000976 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000977 long hash;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000978 dictentry *ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000979 assert(mp->ma_table != NULL);
Tim Peters0ab085c2001-09-14 00:25:33 +0000980 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +0000981 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000982 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000983 if (hash == -1)
984 return NULL;
985 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000986 ep = (mp->ma_lookup)(mp, key, hash);
987 if (ep == NULL)
988 return NULL;
989 v = ep->me_value;
Guido van Rossum1968ad32006-02-25 22:38:04 +0000990 if (v == NULL) {
991 if (!PyDict_CheckExact(mp)) {
992 /* Look up __missing__ method if we're a subclass. */
Guido van Rossum4b92a822006-02-25 23:32:30 +0000993 PyObject *missing;
Guido van Rossum1968ad32006-02-25 22:38:04 +0000994 static PyObject *missing_str = NULL;
995 if (missing_str == NULL)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000996 missing_str =
Guido van Rossum1968ad32006-02-25 22:38:04 +0000997 PyString_InternFromString("__missing__");
Guido van Rossum4b92a822006-02-25 23:32:30 +0000998 missing = _PyType_Lookup(mp->ob_type, missing_str);
Guido van Rossum1968ad32006-02-25 22:38:04 +0000999 if (missing != NULL)
1000 return PyObject_CallFunctionObjArgs(missing,
1001 (PyObject *)mp, key, NULL);
1002 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001003 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001004 return NULL;
1005 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001006 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001007 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001008 return v;
1009}
1010
1011static int
Tim Peters1f5871e2000-07-04 17:44:48 +00001012dict_ass_sub(dictobject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001013{
1014 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001015 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001016 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001017 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001018}
1019
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001020static PyMappingMethods dict_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001021 (lenfunc)dict_length, /*mp_length*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001022 (binaryfunc)dict_subscript, /*mp_subscript*/
1023 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001024};
1025
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001026static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001027dict_keys(register dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001028{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001029 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001030 register Py_ssize_t i, j;
Raymond Hettinger43442782004-03-17 21:55:03 +00001031 dictentry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001032 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001033
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001034 again:
1035 n = mp->ma_used;
1036 v = PyList_New(n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001037 if (v == NULL)
1038 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001039 if (n != mp->ma_used) {
1040 /* Durnit. The allocations caused the dict to resize.
1041 * Just start over, this shouldn't normally happen.
1042 */
1043 Py_DECREF(v);
1044 goto again;
1045 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001046 ep = mp->ma_table;
1047 mask = mp->ma_mask;
1048 for (i = 0, j = 0; i <= mask; i++) {
1049 if (ep[i].me_value != NULL) {
1050 PyObject *key = ep[i].me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001051 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001052 PyList_SET_ITEM(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001053 j++;
1054 }
1055 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001056 assert(j == n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001057 return v;
1058}
1059
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001060static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001061dict_values(register dictobject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001062{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001063 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001064 register Py_ssize_t i, j;
Raymond Hettinger43442782004-03-17 21:55:03 +00001065 dictentry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001066 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001067
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001068 again:
1069 n = mp->ma_used;
1070 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001071 if (v == NULL)
1072 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001073 if (n != mp->ma_used) {
1074 /* Durnit. The allocations caused the dict to resize.
1075 * Just start over, this shouldn't normally happen.
1076 */
1077 Py_DECREF(v);
1078 goto again;
1079 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001080 ep = mp->ma_table;
1081 mask = mp->ma_mask;
1082 for (i = 0, j = 0; i <= mask; i++) {
1083 if (ep[i].me_value != NULL) {
1084 PyObject *value = ep[i].me_value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001085 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001086 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001087 j++;
1088 }
1089 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001090 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001091 return v;
1092}
1093
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001094static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001095dict_items(register dictobject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001096{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001097 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001098 register Py_ssize_t i, j, n;
1099 Py_ssize_t mask;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001100 PyObject *item, *key, *value;
Raymond Hettinger43442782004-03-17 21:55:03 +00001101 dictentry *ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001102
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001103 /* Preallocate the list of tuples, to avoid allocations during
1104 * the loop over the items, which could trigger GC, which
1105 * could resize the dict. :-(
1106 */
1107 again:
1108 n = mp->ma_used;
1109 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001110 if (v == NULL)
1111 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001112 for (i = 0; i < n; i++) {
1113 item = PyTuple_New(2);
1114 if (item == NULL) {
1115 Py_DECREF(v);
1116 return NULL;
1117 }
1118 PyList_SET_ITEM(v, i, item);
1119 }
1120 if (n != mp->ma_used) {
1121 /* Durnit. The allocations caused the dict to resize.
1122 * Just start over, this shouldn't normally happen.
1123 */
1124 Py_DECREF(v);
1125 goto again;
1126 }
1127 /* Nothing we do below makes any function calls. */
Raymond Hettinger43442782004-03-17 21:55:03 +00001128 ep = mp->ma_table;
1129 mask = mp->ma_mask;
1130 for (i = 0, j = 0; i <= mask; i++) {
Raymond Hettinger06905122004-03-19 10:30:00 +00001131 if ((value=ep[i].me_value) != NULL) {
Raymond Hettinger43442782004-03-17 21:55:03 +00001132 key = ep[i].me_key;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001133 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001134 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001135 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001136 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001137 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001138 j++;
1139 }
1140 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001141 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001142 return v;
1143}
1144
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001145static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001146dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001147{
1148 PyObject *seq;
1149 PyObject *value = Py_None;
1150 PyObject *it; /* iter(seq) */
1151 PyObject *key;
1152 PyObject *d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001153 int status;
1154
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001155 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001156 return NULL;
1157
1158 d = PyObject_CallObject(cls, NULL);
1159 if (d == NULL)
1160 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001161
1162 it = PyObject_GetIter(seq);
1163 if (it == NULL){
1164 Py_DECREF(d);
1165 return NULL;
1166 }
1167
1168 for (;;) {
1169 key = PyIter_Next(it);
1170 if (key == NULL) {
1171 if (PyErr_Occurred())
1172 goto Fail;
1173 break;
1174 }
Raymond Hettingere03e5b12002-12-07 08:10:51 +00001175 status = PyObject_SetItem(d, key, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001176 Py_DECREF(key);
1177 if (status < 0)
1178 goto Fail;
1179 }
1180
1181 Py_DECREF(it);
1182 return d;
1183
1184Fail:
1185 Py_DECREF(it);
1186 Py_DECREF(d);
1187 return NULL;
1188}
1189
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001190static int
1191dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001192{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001193 PyObject *arg = NULL;
1194 int result = 0;
1195
1196 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1197 result = -1;
1198
1199 else if (arg != NULL) {
1200 if (PyObject_HasAttrString(arg, "keys"))
1201 result = PyDict_Merge(self, arg, 1);
1202 else
1203 result = PyDict_MergeFromSeq2(self, arg, 1);
1204 }
1205 if (result == 0 && kwds != NULL)
1206 result = PyDict_Merge(self, kwds, 1);
1207 return result;
1208}
1209
1210static PyObject *
1211dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1212{
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001213 if (dict_update_common(self, args, kwds, "update") != -1)
1214 Py_RETURN_NONE;
1215 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001216}
1217
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001218/* Update unconditionally replaces existing items.
1219 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001220 otherwise it leaves existing items unchanged.
1221
1222 PyDict_{Update,Merge} update/merge from a mapping object.
1223
Tim Petersf582b822001-12-11 18:51:08 +00001224 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001225 producing iterable objects of length 2.
1226*/
1227
Tim Petersf582b822001-12-11 18:51:08 +00001228int
Tim Peters1fc240e2001-10-26 05:06:50 +00001229PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1230{
1231 PyObject *it; /* iter(seq2) */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001232 Py_ssize_t i; /* index into seq2 of current element */
Tim Peters1fc240e2001-10-26 05:06:50 +00001233 PyObject *item; /* seq2[i] */
1234 PyObject *fast; /* item as a 2-tuple or 2-list */
1235
1236 assert(d != NULL);
1237 assert(PyDict_Check(d));
1238 assert(seq2 != NULL);
1239
1240 it = PyObject_GetIter(seq2);
1241 if (it == NULL)
1242 return -1;
1243
1244 for (i = 0; ; ++i) {
1245 PyObject *key, *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001246 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001247
1248 fast = NULL;
1249 item = PyIter_Next(it);
1250 if (item == NULL) {
1251 if (PyErr_Occurred())
1252 goto Fail;
1253 break;
1254 }
1255
1256 /* Convert item to sequence, and verify length 2. */
1257 fast = PySequence_Fast(item, "");
1258 if (fast == NULL) {
1259 if (PyErr_ExceptionMatches(PyExc_TypeError))
1260 PyErr_Format(PyExc_TypeError,
1261 "cannot convert dictionary update "
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001262 "sequence element #%zd to a sequence",
Tim Peters1fc240e2001-10-26 05:06:50 +00001263 i);
1264 goto Fail;
1265 }
1266 n = PySequence_Fast_GET_SIZE(fast);
1267 if (n != 2) {
1268 PyErr_Format(PyExc_ValueError,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001269 "dictionary update sequence element #%zd "
Martin v. Löwis2c95cc62006-02-16 06:54:25 +00001270 "has length %zd; 2 is required",
Martin v. Löwise0e89f72006-02-16 06:59:22 +00001271 i, n);
Tim Peters1fc240e2001-10-26 05:06:50 +00001272 goto Fail;
1273 }
1274
1275 /* Update/merge with this (key, value) pair. */
1276 key = PySequence_Fast_GET_ITEM(fast, 0);
1277 value = PySequence_Fast_GET_ITEM(fast, 1);
1278 if (override || PyDict_GetItem(d, key) == NULL) {
1279 int status = PyDict_SetItem(d, key, value);
1280 if (status < 0)
1281 goto Fail;
1282 }
1283 Py_DECREF(fast);
1284 Py_DECREF(item);
1285 }
1286
1287 i = 0;
1288 goto Return;
1289Fail:
1290 Py_XDECREF(item);
1291 Py_XDECREF(fast);
1292 i = -1;
1293Return:
1294 Py_DECREF(it);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001295 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001296}
1297
Tim Peters6d6c1a32001-08-02 04:15:00 +00001298int
1299PyDict_Update(PyObject *a, PyObject *b)
1300{
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001301 return PyDict_Merge(a, b, 1);
1302}
1303
1304int
1305PyDict_Merge(PyObject *a, PyObject *b, int override)
1306{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001307 register PyDictObject *mp, *other;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001308 register Py_ssize_t i;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001309 dictentry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001310
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001311 /* We accept for the argument either a concrete dictionary object,
1312 * or an abstract "mapping" object. For the former, we can do
1313 * things quite efficiently. For the latter, we only require that
1314 * PyMapping_Keys() and PyObject_GetItem() be supported.
1315 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001316 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1317 PyErr_BadInternalCall();
1318 return -1;
1319 }
1320 mp = (dictobject*)a;
1321 if (PyDict_Check(b)) {
1322 other = (dictobject*)b;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001323 if (other == mp || other->ma_used == 0)
1324 /* a.update(a) or a.update({}); nothing to do */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001325 return 0;
Raymond Hettinger186e7392005-05-14 18:08:25 +00001326 if (mp->ma_used == 0)
1327 /* Since the target dict is empty, PyDict_GetItem()
1328 * always returns NULL. Setting override to 1
1329 * skips the unnecessary test.
1330 */
1331 override = 1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001332 /* Do one big resize at the start, rather than
1333 * incrementally resizing as we insert new items. Expect
1334 * that there will be no (or few) overlapping keys.
1335 */
1336 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
Raymond Hettingerc8d22902003-05-07 00:49:40 +00001337 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001338 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001339 }
1340 for (i = 0; i <= other->ma_mask; i++) {
1341 entry = &other->ma_table[i];
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001342 if (entry->me_value != NULL &&
1343 (override ||
1344 PyDict_GetItem(a, entry->me_key) == NULL)) {
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001345 Py_INCREF(entry->me_key);
1346 Py_INCREF(entry->me_value);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001347 if (insertdict(mp, entry->me_key,
1348 (long)entry->me_hash,
1349 entry->me_value) != 0)
1350 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001351 }
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001352 }
1353 }
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001354 else {
1355 /* Do it the generic, slower way */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001356 PyObject *keys = PyMapping_Keys(b);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001357 PyObject *iter;
1358 PyObject *key, *value;
1359 int status;
1360
1361 if (keys == NULL)
1362 /* Docstring says this is equivalent to E.keys() so
1363 * if E doesn't have a .keys() method we want
1364 * AttributeError to percolate up. Might as well
1365 * do the same for any other error.
1366 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001367 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001368
1369 iter = PyObject_GetIter(keys);
1370 Py_DECREF(keys);
1371 if (iter == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001372 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001373
1374 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001375 if (!override && PyDict_GetItem(a, key) != NULL) {
1376 Py_DECREF(key);
1377 continue;
1378 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001379 value = PyObject_GetItem(b, key);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001380 if (value == NULL) {
1381 Py_DECREF(iter);
1382 Py_DECREF(key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001383 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001384 }
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001385 status = PyDict_SetItem(a, key, value);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001386 Py_DECREF(key);
1387 Py_DECREF(value);
1388 if (status < 0) {
1389 Py_DECREF(iter);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001390 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001391 }
1392 }
1393 Py_DECREF(iter);
1394 if (PyErr_Occurred())
1395 /* Iterator completed, via error */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001396 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001397 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001398 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001399}
1400
1401static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001402dict_copy(register dictobject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001403{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001404 return PyDict_Copy((PyObject*)mp);
1405}
1406
1407PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001408PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001409{
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001410 PyObject *copy;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001411
1412 if (o == NULL || !PyDict_Check(o)) {
1413 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001414 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001415 }
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001416 copy = PyDict_New();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001417 if (copy == NULL)
1418 return NULL;
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001419 if (PyDict_Merge(copy, o, 1) == 0)
1420 return copy;
1421 Py_DECREF(copy);
Raymond Hettinger1356f782005-04-15 15:58:42 +00001422 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001423}
1424
Martin v. Löwis18e16552006-02-15 17:27:45 +00001425Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00001426PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001427{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001428 if (mp == NULL || !PyDict_Check(mp)) {
1429 PyErr_BadInternalCall();
Jeremy Hylton7083bb72004-02-17 20:10:11 +00001430 return -1;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001431 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001432 return ((dictobject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001433}
1434
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001435PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001436PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001437{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001438 if (mp == NULL || !PyDict_Check(mp)) {
1439 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001440 return NULL;
1441 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001442 return dict_keys((dictobject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001443}
1444
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001445PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001446PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001447{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001448 if (mp == NULL || !PyDict_Check(mp)) {
1449 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001450 return NULL;
1451 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001452 return dict_values((dictobject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001453}
1454
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001455PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001456PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001457{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001458 if (mp == NULL || !PyDict_Check(mp)) {
1459 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001460 return NULL;
1461 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001462 return dict_items((dictobject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001463}
1464
Tim Peterse63415e2001-05-08 04:38:29 +00001465/* Return 1 if dicts equal, 0 if not, -1 if error.
1466 * Gets out as soon as any difference is detected.
1467 * Uses only Py_EQ comparison.
1468 */
1469static int
1470dict_equal(dictobject *a, dictobject *b)
1471{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001472 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00001473
1474 if (a->ma_used != b->ma_used)
1475 /* can't be equal if # of entries differ */
1476 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001477
Tim Peterse63415e2001-05-08 04:38:29 +00001478 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Tim Petersafb6ae82001-06-04 21:00:21 +00001479 for (i = 0; i <= a->ma_mask; i++) {
Tim Peterse63415e2001-05-08 04:38:29 +00001480 PyObject *aval = a->ma_table[i].me_value;
1481 if (aval != NULL) {
1482 int cmp;
1483 PyObject *bval;
1484 PyObject *key = a->ma_table[i].me_key;
1485 /* temporarily bump aval's refcount to ensure it stays
1486 alive until we're done with it */
1487 Py_INCREF(aval);
Guido van Rossumdc5f6b22006-08-24 21:29:26 +00001488 /* ditto for key */
1489 Py_INCREF(key);
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001490 bval = PyDict_GetItemWithError((PyObject *)b, key);
Guido van Rossumdc5f6b22006-08-24 21:29:26 +00001491 Py_DECREF(key);
Tim Peterse63415e2001-05-08 04:38:29 +00001492 if (bval == NULL) {
1493 Py_DECREF(aval);
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001494 if (PyErr_Occurred())
1495 return -1;
Tim Peterse63415e2001-05-08 04:38:29 +00001496 return 0;
1497 }
1498 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1499 Py_DECREF(aval);
1500 if (cmp <= 0) /* error or not equal */
1501 return cmp;
1502 }
1503 }
1504 return 1;
1505 }
1506
1507static PyObject *
1508dict_richcompare(PyObject *v, PyObject *w, int op)
1509{
1510 int cmp;
1511 PyObject *res;
1512
1513 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1514 res = Py_NotImplemented;
1515 }
1516 else if (op == Py_EQ || op == Py_NE) {
1517 cmp = dict_equal((dictobject *)v, (dictobject *)w);
1518 if (cmp < 0)
1519 return NULL;
1520 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1521 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001522 else
1523 res = Py_NotImplemented;
Tim Peterse63415e2001-05-08 04:38:29 +00001524 Py_INCREF(res);
1525 return res;
1526 }
1527
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001528static PyObject *
Guido van Rossume2b70bc2006-08-18 22:13:04 +00001529dict_contains(register dictobject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001530{
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001531 long hash;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001532 dictentry *ep;
1533
Tim Peters0ab085c2001-09-14 00:25:33 +00001534 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001535 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001536 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001537 if (hash == -1)
1538 return NULL;
1539 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001540 ep = (mp->ma_lookup)(mp, key, hash);
1541 if (ep == NULL)
1542 return NULL;
1543 return PyBool_FromLong(ep->me_value != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001544}
1545
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001546static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001547dict_get(register dictobject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001548{
1549 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001550 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001551 PyObject *val = NULL;
1552 long hash;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001553 dictentry *ep;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001554
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001555 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001556 return NULL;
1557
Tim Peters0ab085c2001-09-14 00:25:33 +00001558 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001559 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Barry Warsawc38c5da1997-10-06 17:49:20 +00001560 hash = PyObject_Hash(key);
1561 if (hash == -1)
1562 return NULL;
1563 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001564 ep = (mp->ma_lookup)(mp, key, hash);
1565 if (ep == NULL)
1566 return NULL;
1567 val = ep->me_value;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001568 if (val == NULL)
1569 val = failobj;
1570 Py_INCREF(val);
1571 return val;
1572}
1573
1574
1575static PyObject *
Guido van Rossum164452c2000-08-08 16:12:54 +00001576dict_setdefault(register dictobject *mp, PyObject *args)
1577{
1578 PyObject *key;
1579 PyObject *failobj = Py_None;
1580 PyObject *val = NULL;
1581 long hash;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001582 dictentry *ep;
Guido van Rossum164452c2000-08-08 16:12:54 +00001583
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001584 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
Guido van Rossum164452c2000-08-08 16:12:54 +00001585 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001586
Tim Peters0ab085c2001-09-14 00:25:33 +00001587 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001588 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossum164452c2000-08-08 16:12:54 +00001589 hash = PyObject_Hash(key);
1590 if (hash == -1)
1591 return NULL;
1592 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001593 ep = (mp->ma_lookup)(mp, key, hash);
1594 if (ep == NULL)
1595 return NULL;
1596 val = ep->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001597 if (val == NULL) {
1598 val = failobj;
1599 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1600 val = NULL;
1601 }
1602 Py_XINCREF(val);
1603 return val;
1604}
1605
1606
1607static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001608dict_clear(register dictobject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001609{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001610 PyDict_Clear((PyObject *)mp);
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001611 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001612}
1613
Guido van Rossumba6ab842000-12-12 22:02:18 +00001614static PyObject *
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001615dict_pop(dictobject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00001616{
1617 long hash;
1618 dictentry *ep;
1619 PyObject *old_value, *old_key;
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001620 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001621
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001622 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1623 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001624 if (mp->ma_used == 0) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001625 if (deflt) {
1626 Py_INCREF(deflt);
1627 return deflt;
1628 }
Guido van Rossume027d982002-04-12 15:11:59 +00001629 PyErr_SetString(PyExc_KeyError,
1630 "pop(): dictionary is empty");
1631 return NULL;
1632 }
1633 if (!PyString_CheckExact(key) ||
1634 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1635 hash = PyObject_Hash(key);
1636 if (hash == -1)
1637 return NULL;
1638 }
1639 ep = (mp->ma_lookup)(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001640 if (ep == NULL)
1641 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001642 if (ep->me_value == NULL) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001643 if (deflt) {
1644 Py_INCREF(deflt);
1645 return deflt;
1646 }
Guido van Rossume027d982002-04-12 15:11:59 +00001647 PyErr_SetObject(PyExc_KeyError, key);
1648 return NULL;
1649 }
1650 old_key = ep->me_key;
1651 Py_INCREF(dummy);
1652 ep->me_key = dummy;
1653 old_value = ep->me_value;
1654 ep->me_value = NULL;
1655 mp->ma_used--;
1656 Py_DECREF(old_key);
1657 return old_value;
1658}
1659
1660static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001661dict_popitem(dictobject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001662{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001663 Py_ssize_t i = 0;
Guido van Rossumba6ab842000-12-12 22:02:18 +00001664 dictentry *ep;
1665 PyObject *res;
1666
Tim Petersf4b33f62001-06-02 05:42:29 +00001667 /* Allocate the result tuple before checking the size. Believe it
1668 * or not, this allocation could trigger a garbage collection which
1669 * could empty the dict, so if we checked the size first and that
1670 * happened, the result would be an infinite loop (searching for an
1671 * entry that no longer exists). Note that the usual popitem()
1672 * idiom is "while d: k, v = d.popitem()". so needing to throw the
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001673 * tuple away if the dict *is* empty isn't a significant
Tim Petersf4b33f62001-06-02 05:42:29 +00001674 * inefficiency -- possible, but unlikely in practice.
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001675 */
1676 res = PyTuple_New(2);
1677 if (res == NULL)
1678 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001679 if (mp->ma_used == 0) {
1680 Py_DECREF(res);
1681 PyErr_SetString(PyExc_KeyError,
1682 "popitem(): dictionary is empty");
1683 return NULL;
1684 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001685 /* Set ep to "the first" dict entry with a value. We abuse the hash
1686 * field of slot 0 to hold a search finger:
1687 * If slot 0 has a value, use slot 0.
1688 * Else slot 0 is being used to hold a search finger,
1689 * and we use its hash value as the first index to look.
1690 */
1691 ep = &mp->ma_table[0];
1692 if (ep->me_value == NULL) {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001693 i = ep->me_hash;
Tim Petersdea48ec2001-05-22 20:40:22 +00001694 /* The hash field may be a real hash value, or it may be a
1695 * legit search finger, or it may be a once-legit search
1696 * finger that's out of bounds now because it wrapped around
1697 * or the table shrunk -- simply make sure it's in bounds now.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001698 */
Tim Petersafb6ae82001-06-04 21:00:21 +00001699 if (i > mp->ma_mask || i < 1)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001700 i = 1; /* skip slot 0 */
1701 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1702 i++;
Tim Petersafb6ae82001-06-04 21:00:21 +00001703 if (i > mp->ma_mask)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001704 i = 1;
1705 }
1706 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001707 PyTuple_SET_ITEM(res, 0, ep->me_key);
1708 PyTuple_SET_ITEM(res, 1, ep->me_value);
1709 Py_INCREF(dummy);
1710 ep->me_key = dummy;
1711 ep->me_value = NULL;
1712 mp->ma_used--;
1713 assert(mp->ma_table[0].me_value == NULL);
1714 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001715 return res;
1716}
1717
Jeremy Hylton8caad492000-06-23 14:18:11 +00001718static int
1719dict_traverse(PyObject *op, visitproc visit, void *arg)
1720{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001721 Py_ssize_t i = 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001722 PyObject *pk;
1723 PyObject *pv;
1724
1725 while (PyDict_Next(op, &i, &pk, &pv)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001726 Py_VISIT(pk);
1727 Py_VISIT(pv);
Jeremy Hylton8caad492000-06-23 14:18:11 +00001728 }
1729 return 0;
1730}
1731
1732static int
1733dict_tp_clear(PyObject *op)
1734{
1735 PyDict_Clear(op);
1736 return 0;
1737}
1738
Tim Petersf7f88b12000-12-13 23:18:45 +00001739
Raymond Hettinger019a1482004-03-18 02:41:19 +00001740extern PyTypeObject PyDictIterKey_Type; /* Forward */
1741extern PyTypeObject PyDictIterValue_Type; /* Forward */
1742extern PyTypeObject PyDictIterItem_Type; /* Forward */
1743static PyObject *dictiter_new(dictobject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001744
1745static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001746dict_iterkeys(dictobject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001747{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001748 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001749}
1750
1751static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001752dict_itervalues(dictobject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001753{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001754 return dictiter_new(dict, &PyDictIterValue_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001755}
1756
1757static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001758dict_iteritems(dictobject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001759{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001760 return dictiter_new(dict, &PyDictIterItem_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001761}
1762
1763
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001764PyDoc_STRVAR(contains__doc__,
1765"D.__contains__(k) -> True if D has a key k, else False");
1766
1767PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
1768
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001769PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001770"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001771
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001772PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001773"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 +00001774
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001775PyDoc_STRVAR(pop__doc__,
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001776"D.pop(k[,d]) -> v, remove specified key and return the corresponding value\n\
1777If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00001778
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001779PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00001780"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000017812-tuple; but raise KeyError if D is empty");
Tim Petersf7f88b12000-12-13 23:18:45 +00001782
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001783PyDoc_STRVAR(keys__doc__,
1784"D.keys() -> list of D's keys");
Tim Petersf7f88b12000-12-13 23:18:45 +00001785
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001786PyDoc_STRVAR(items__doc__,
1787"D.items() -> list of D's (key, value) pairs, as 2-tuples");
Tim Petersf7f88b12000-12-13 23:18:45 +00001788
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001789PyDoc_STRVAR(values__doc__,
1790"D.values() -> list of D's values");
Tim Petersf7f88b12000-12-13 23:18:45 +00001791
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001792PyDoc_STRVAR(update__doc__,
Walter Dörwaldd70ad8a2004-05-28 20:59:21 +00001793"D.update(E, **F) -> None. Update D from E and F: for k in E: D[k] = E[k]\n\
1794(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 +00001795
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001796PyDoc_STRVAR(fromkeys__doc__,
1797"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
1798v defaults to None.");
1799
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001800PyDoc_STRVAR(clear__doc__,
1801"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001802
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001803PyDoc_STRVAR(copy__doc__,
1804"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00001805
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001806PyDoc_STRVAR(iterkeys__doc__,
1807"D.iterkeys() -> an iterator over the keys of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00001808
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001809PyDoc_STRVAR(itervalues__doc__,
1810"D.itervalues() -> an iterator over the values of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00001811
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001812PyDoc_STRVAR(iteritems__doc__,
1813"D.iteritems() -> an iterator over the (key, value) items of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00001814
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001815static PyMethodDef mapp_methods[] = {
Guido van Rossume2b70bc2006-08-18 22:13:04 +00001816 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001817 contains__doc__},
Raymond Hettinger0c669672003-12-13 13:31:55 +00001818 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001819 getitem__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00001820 {"get", (PyCFunction)dict_get, METH_VARARGS,
1821 get__doc__},
1822 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
1823 setdefault_doc__},
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001824 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
Just van Rossuma797d812002-11-23 09:45:04 +00001825 pop__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001826 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001827 popitem__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001828 {"keys", (PyCFunction)dict_keys, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001829 keys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001830 {"items", (PyCFunction)dict_items, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001831 items__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001832 {"values", (PyCFunction)dict_values, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001833 values__doc__},
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001834 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001835 update__doc__},
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001836 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
1837 fromkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001838 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001839 clear__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001840 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001841 copy__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001842 {"iterkeys", (PyCFunction)dict_iterkeys, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00001843 iterkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001844 {"itervalues", (PyCFunction)dict_itervalues, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00001845 itervalues__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001846 {"iteritems", (PyCFunction)dict_iteritems, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00001847 iteritems__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00001848 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001849};
1850
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001851/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001852int
1853PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001854{
1855 long hash;
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001856 dictobject *mp = (dictobject *)op;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001857 dictentry *ep;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001858
Tim Peters0ab085c2001-09-14 00:25:33 +00001859 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001860 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001861 hash = PyObject_Hash(key);
1862 if (hash == -1)
1863 return -1;
1864 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001865 ep = (mp->ma_lookup)(mp, key, hash);
1866 return ep == NULL ? -1 : (ep->me_value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001867}
1868
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001869/* Hack to implement "key in dict" */
1870static PySequenceMethods dict_as_sequence = {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001871 0, /* sq_length */
1872 0, /* sq_concat */
1873 0, /* sq_repeat */
1874 0, /* sq_item */
1875 0, /* sq_slice */
1876 0, /* sq_ass_item */
1877 0, /* sq_ass_slice */
1878 PyDict_Contains, /* sq_contains */
1879 0, /* sq_inplace_concat */
1880 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001881};
1882
Guido van Rossum09e563a2001-05-01 12:10:21 +00001883static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001884dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1885{
1886 PyObject *self;
1887
1888 assert(type != NULL && type->tp_alloc != NULL);
1889 self = type->tp_alloc(type, 0);
1890 if (self != NULL) {
1891 PyDictObject *d = (PyDictObject *)self;
1892 /* It's guaranteed that tp->alloc zeroed out the struct. */
1893 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
1894 INIT_NONZERO_DICT_SLOTS(d);
1895 d->ma_lookup = lookdict_string;
1896#ifdef SHOW_CONVERSION_COUNTS
1897 ++created;
1898#endif
1899 }
1900 return self;
1901}
1902
Tim Peters25786c02001-09-02 08:22:48 +00001903static int
1904dict_init(PyObject *self, PyObject *args, PyObject *kwds)
1905{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001906 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00001907}
1908
Tim Peters6d6c1a32001-08-02 04:15:00 +00001909static PyObject *
Guido van Rossum09e563a2001-05-01 12:10:21 +00001910dict_iter(dictobject *dict)
1911{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001912 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001913}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001914
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001915PyDoc_STRVAR(dictionary_doc,
Tim Petersa427a2b2001-10-29 22:25:45 +00001916"dict() -> new empty dictionary.\n"
1917"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Tim Peters1fc240e2001-10-26 05:06:50 +00001918" (key, value) pairs.\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00001919"dict(seq) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00001920" d = {}\n"
1921" for k, v in seq:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00001922" d[k] = v\n"
1923"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
1924" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00001925
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001926PyTypeObject PyDict_Type = {
1927 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001928 0,
Tim Petersa427a2b2001-10-29 22:25:45 +00001929 "dict",
Neil Schemenauere83c00e2001-08-29 23:54:21 +00001930 sizeof(dictobject),
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001931 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00001932 (destructor)dict_dealloc, /* tp_dealloc */
1933 (printfunc)dict_print, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001934 0, /* tp_getattr */
Guido van Rossumb9324202001-01-18 00:39:02 +00001935 0, /* tp_setattr */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001936 0, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00001937 (reprfunc)dict_repr, /* tp_repr */
1938 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001939 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00001940 &dict_as_mapping, /* tp_as_mapping */
Guido van Rossum50e9fb92006-08-17 05:42:55 +00001941 0, /* tp_hash */
Guido van Rossumb9324202001-01-18 00:39:02 +00001942 0, /* tp_call */
1943 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001944 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumb9324202001-01-18 00:39:02 +00001945 0, /* tp_setattro */
1946 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00001947 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Tim Peters6d6c1a32001-08-02 04:15:00 +00001948 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters25786c02001-09-02 08:22:48 +00001949 dictionary_doc, /* tp_doc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001950 dict_traverse, /* tp_traverse */
1951 dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00001952 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001953 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00001954 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001955 0, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001956 mapp_methods, /* tp_methods */
1957 0, /* tp_members */
1958 0, /* tp_getset */
1959 0, /* tp_base */
1960 0, /* tp_dict */
1961 0, /* tp_descr_get */
1962 0, /* tp_descr_set */
1963 0, /* tp_dictoffset */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001964 dict_init, /* tp_init */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001965 PyType_GenericAlloc, /* tp_alloc */
1966 dict_new, /* tp_new */
Neil Schemenauer6189b892002-04-12 02:43:00 +00001967 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001968};
1969
Guido van Rossum3cca2451997-05-16 14:23:33 +00001970/* For backward compatibility with old dictionary interface */
1971
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001972PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00001973PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001974{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001975 PyObject *kv, *rv;
1976 kv = PyString_FromString(key);
1977 if (kv == NULL)
1978 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00001979 rv = PyDict_GetItem(v, kv);
1980 Py_DECREF(kv);
1981 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001982}
1983
1984int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00001985PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001986{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001987 PyObject *kv;
1988 int err;
1989 kv = PyString_FromString(key);
1990 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00001991 return -1;
Guido van Rossum4f3bf1e1997-09-29 23:31:11 +00001992 PyString_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00001993 err = PyDict_SetItem(v, kv, item);
1994 Py_DECREF(kv);
1995 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001996}
1997
1998int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00001999PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002000{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002001 PyObject *kv;
2002 int err;
2003 kv = PyString_FromString(key);
2004 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002005 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002006 err = PyDict_DelItem(v, kv);
2007 Py_DECREF(kv);
2008 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002009}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002010
Raymond Hettinger019a1482004-03-18 02:41:19 +00002011/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002012
2013typedef struct {
2014 PyObject_HEAD
Guido van Rossum2147df72002-07-16 20:30:22 +00002015 dictobject *di_dict; /* Set to NULL when iterator is exhausted */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002016 Py_ssize_t di_used;
2017 Py_ssize_t di_pos;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002018 PyObject* di_result; /* reusable result tuple for iteritems */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002019 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002020} dictiterobject;
2021
2022static PyObject *
Raymond Hettinger019a1482004-03-18 02:41:19 +00002023dictiter_new(dictobject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002024{
2025 dictiterobject *di;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002026 di = PyObject_New(dictiterobject, itertype);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002027 if (di == NULL)
2028 return NULL;
2029 Py_INCREF(dict);
2030 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00002031 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002032 di->di_pos = 0;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002033 di->len = dict->ma_used;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002034 if (itertype == &PyDictIterItem_Type) {
2035 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2036 if (di->di_result == NULL) {
2037 Py_DECREF(di);
2038 return NULL;
2039 }
2040 }
2041 else
2042 di->di_result = NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002043 return (PyObject *)di;
2044}
2045
2046static void
2047dictiter_dealloc(dictiterobject *di)
2048{
Guido van Rossum2147df72002-07-16 20:30:22 +00002049 Py_XDECREF(di->di_dict);
Raymond Hettinger019a1482004-03-18 02:41:19 +00002050 Py_XDECREF(di->di_result);
Neil Schemenauer6189b892002-04-12 02:43:00 +00002051 PyObject_Del(di);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002052}
2053
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002054static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002055dictiter_len(dictiterobject *di)
2056{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002057 Py_ssize_t len = 0;
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00002058 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002059 len = di->len;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002060 return PyInt_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002061}
2062
Armin Rigof5b3e362006-02-11 21:32:43 +00002063PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002064
2065static PyMethodDef dictiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +00002066 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002067 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002068};
2069
Raymond Hettinger019a1482004-03-18 02:41:19 +00002070static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002071{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002072 PyObject *key;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002073 register Py_ssize_t i, mask;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002074 register dictentry *ep;
2075 dictobject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002076
Raymond Hettinger019a1482004-03-18 02:41:19 +00002077 if (d == NULL)
Guido van Rossum2147df72002-07-16 20:30:22 +00002078 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002079 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002080
Raymond Hettinger019a1482004-03-18 02:41:19 +00002081 if (di->di_used != d->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00002082 PyErr_SetString(PyExc_RuntimeError,
2083 "dictionary changed size during iteration");
Guido van Rossum2147df72002-07-16 20:30:22 +00002084 di->di_used = -1; /* Make this state sticky */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002085 return NULL;
2086 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002087
Raymond Hettinger019a1482004-03-18 02:41:19 +00002088 i = di->di_pos;
2089 if (i < 0)
2090 goto fail;
2091 ep = d->ma_table;
2092 mask = d->ma_mask;
2093 while (i <= mask && ep[i].me_value == NULL)
2094 i++;
2095 di->di_pos = i+1;
2096 if (i > mask)
2097 goto fail;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002098 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002099 key = ep[i].me_key;
2100 Py_INCREF(key);
2101 return key;
2102
2103fail:
2104 Py_DECREF(d);
Guido van Rossum2147df72002-07-16 20:30:22 +00002105 di->di_dict = NULL;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002106 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002107}
2108
Raymond Hettinger019a1482004-03-18 02:41:19 +00002109PyTypeObject PyDictIterKey_Type = {
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002110 PyObject_HEAD_INIT(&PyType_Type)
2111 0, /* ob_size */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002112 "dictionary-keyiterator", /* tp_name */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002113 sizeof(dictiterobject), /* tp_basicsize */
2114 0, /* tp_itemsize */
2115 /* methods */
2116 (destructor)dictiter_dealloc, /* tp_dealloc */
2117 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002118 0, /* tp_getattr */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002119 0, /* tp_setattr */
2120 0, /* tp_compare */
2121 0, /* tp_repr */
2122 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002123 0, /* tp_as_sequence */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002124 0, /* tp_as_mapping */
2125 0, /* tp_hash */
2126 0, /* tp_call */
2127 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002128 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002129 0, /* tp_setattro */
2130 0, /* tp_as_buffer */
2131 Py_TPFLAGS_DEFAULT, /* tp_flags */
2132 0, /* tp_doc */
2133 0, /* tp_traverse */
2134 0, /* tp_clear */
2135 0, /* tp_richcompare */
2136 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002137 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002138 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002139 dictiter_methods, /* tp_methods */
2140 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002141};
2142
2143static PyObject *dictiter_iternextvalue(dictiterobject *di)
2144{
2145 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002146 register Py_ssize_t i, mask;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002147 register dictentry *ep;
2148 dictobject *d = di->di_dict;
2149
2150 if (d == NULL)
2151 return NULL;
2152 assert (PyDict_Check(d));
2153
2154 if (di->di_used != d->ma_used) {
2155 PyErr_SetString(PyExc_RuntimeError,
2156 "dictionary changed size during iteration");
2157 di->di_used = -1; /* Make this state sticky */
2158 return NULL;
2159 }
2160
2161 i = di->di_pos;
Guido van Rossum09240f62004-03-20 19:11:58 +00002162 mask = d->ma_mask;
2163 if (i < 0 || i > mask)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002164 goto fail;
2165 ep = d->ma_table;
Guido van Rossum09240f62004-03-20 19:11:58 +00002166 while ((value=ep[i].me_value) == NULL) {
Raymond Hettinger019a1482004-03-18 02:41:19 +00002167 i++;
Guido van Rossum09240f62004-03-20 19:11:58 +00002168 if (i > mask)
2169 goto fail;
2170 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002171 di->di_pos = i+1;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002172 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002173 Py_INCREF(value);
2174 return value;
2175
2176fail:
2177 Py_DECREF(d);
2178 di->di_dict = NULL;
2179 return NULL;
2180}
2181
2182PyTypeObject PyDictIterValue_Type = {
2183 PyObject_HEAD_INIT(&PyType_Type)
2184 0, /* ob_size */
2185 "dictionary-valueiterator", /* tp_name */
2186 sizeof(dictiterobject), /* tp_basicsize */
2187 0, /* tp_itemsize */
2188 /* methods */
2189 (destructor)dictiter_dealloc, /* tp_dealloc */
2190 0, /* tp_print */
2191 0, /* tp_getattr */
2192 0, /* tp_setattr */
2193 0, /* tp_compare */
2194 0, /* tp_repr */
2195 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002196 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002197 0, /* tp_as_mapping */
2198 0, /* tp_hash */
2199 0, /* tp_call */
2200 0, /* tp_str */
2201 PyObject_GenericGetAttr, /* tp_getattro */
2202 0, /* tp_setattro */
2203 0, /* tp_as_buffer */
2204 Py_TPFLAGS_DEFAULT, /* tp_flags */
2205 0, /* tp_doc */
2206 0, /* tp_traverse */
2207 0, /* tp_clear */
2208 0, /* tp_richcompare */
2209 0, /* tp_weaklistoffset */
2210 PyObject_SelfIter, /* tp_iter */
2211 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002212 dictiter_methods, /* tp_methods */
2213 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002214};
2215
2216static PyObject *dictiter_iternextitem(dictiterobject *di)
2217{
2218 PyObject *key, *value, *result = di->di_result;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002219 register Py_ssize_t i, mask;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002220 register dictentry *ep;
2221 dictobject *d = di->di_dict;
2222
2223 if (d == NULL)
2224 return NULL;
2225 assert (PyDict_Check(d));
2226
2227 if (di->di_used != d->ma_used) {
2228 PyErr_SetString(PyExc_RuntimeError,
2229 "dictionary changed size during iteration");
2230 di->di_used = -1; /* Make this state sticky */
2231 return NULL;
2232 }
2233
2234 i = di->di_pos;
2235 if (i < 0)
2236 goto fail;
2237 ep = d->ma_table;
2238 mask = d->ma_mask;
2239 while (i <= mask && ep[i].me_value == NULL)
2240 i++;
2241 di->di_pos = i+1;
2242 if (i > mask)
2243 goto fail;
2244
2245 if (result->ob_refcnt == 1) {
2246 Py_INCREF(result);
2247 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2248 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2249 } else {
2250 result = PyTuple_New(2);
Tim Peters60b29962006-01-01 01:19:23 +00002251 if (result == NULL)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002252 return NULL;
2253 }
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002254 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002255 key = ep[i].me_key;
2256 value = ep[i].me_value;
2257 Py_INCREF(key);
2258 Py_INCREF(value);
2259 PyTuple_SET_ITEM(result, 0, key);
2260 PyTuple_SET_ITEM(result, 1, value);
2261 return result;
2262
2263fail:
2264 Py_DECREF(d);
2265 di->di_dict = NULL;
2266 return NULL;
2267}
2268
2269PyTypeObject PyDictIterItem_Type = {
2270 PyObject_HEAD_INIT(&PyType_Type)
2271 0, /* ob_size */
2272 "dictionary-itemiterator", /* tp_name */
2273 sizeof(dictiterobject), /* tp_basicsize */
2274 0, /* tp_itemsize */
2275 /* methods */
2276 (destructor)dictiter_dealloc, /* tp_dealloc */
2277 0, /* tp_print */
2278 0, /* tp_getattr */
2279 0, /* tp_setattr */
2280 0, /* tp_compare */
2281 0, /* tp_repr */
2282 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002283 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002284 0, /* tp_as_mapping */
2285 0, /* tp_hash */
2286 0, /* tp_call */
2287 0, /* tp_str */
2288 PyObject_GenericGetAttr, /* tp_getattro */
2289 0, /* tp_setattro */
2290 0, /* tp_as_buffer */
2291 Py_TPFLAGS_DEFAULT, /* tp_flags */
2292 0, /* tp_doc */
2293 0, /* tp_traverse */
2294 0, /* tp_clear */
2295 0, /* tp_richcompare */
2296 0, /* tp_weaklistoffset */
2297 PyObject_SelfIter, /* tp_iter */
2298 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002299 dictiter_methods, /* tp_methods */
2300 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002301};