blob: cd1338aed3e020ab643113a38f223f9794c3cd4a [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
Guido van Rossum16e93a81997-01-28 00:00:11 +000012
Georg Brandlb9f4ad32006-10-29 18:31:42 +000013/* Set a key error with the specified argument, wrapping it in a
14 * tuple automatically so that tuple keys are not unpacked as the
15 * exception arguments. */
16static void
17set_key_error(PyObject *arg)
18{
19 PyObject *tup;
20 tup = PyTuple_Pack(1, arg);
21 if (!tup)
22 return; /* caller will expect error to be set anyway */
23 PyErr_SetObject(PyExc_KeyError, tup);
Neal Norwitz7b932da2006-10-29 23:39:03 +000024 Py_DECREF(tup);
Georg Brandlb9f4ad32006-10-29 18:31:42 +000025}
26
Tim Peterseb28ef22001-06-02 05:27:19 +000027/* Define this out if you don't want conversion statistics on exit. */
Fred Drake1bff34a2000-08-31 19:31:38 +000028#undef SHOW_CONVERSION_COUNTS
29
Tim Peterseb28ef22001-06-02 05:27:19 +000030/* See large comment block below. This must be >= 1. */
31#define PERTURB_SHIFT 5
32
Guido van Rossum16e93a81997-01-28 00:00:11 +000033/*
Tim Peterseb28ef22001-06-02 05:27:19 +000034Major subtleties ahead: Most hash schemes depend on having a "good" hash
35function, in the sense of simulating randomness. Python doesn't: its most
36important hash functions (for strings and ints) are very regular in common
37cases:
Tim Peters15d49292001-05-27 07:39:22 +000038
Tim Peterseb28ef22001-06-02 05:27:19 +000039>>> map(hash, (0, 1, 2, 3))
40[0, 1, 2, 3]
41>>> map(hash, ("namea", "nameb", "namec", "named"))
42[-1658398457, -1658398460, -1658398459, -1658398462]
43>>>
Tim Peters15d49292001-05-27 07:39:22 +000044
Tim Peterseb28ef22001-06-02 05:27:19 +000045This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
46the low-order i bits as the initial table index is extremely fast, and there
47are no collisions at all for dicts indexed by a contiguous range of ints.
48The same is approximately true when keys are "consecutive" strings. So this
49gives better-than-random behavior in common cases, and that's very desirable.
Tim Peters15d49292001-05-27 07:39:22 +000050
Tim Peterseb28ef22001-06-02 05:27:19 +000051OTOH, when collisions occur, the tendency to fill contiguous slices of the
52hash table makes a good collision resolution strategy crucial. Taking only
53the last i bits of the hash code is also vulnerable: for example, consider
54[i << 16 for i in range(20000)] as a set of keys. Since ints are their own
55hash codes, and this fits in a dict of size 2**15, the last 15 bits of every
56hash code are all 0: they *all* map to the same table index.
Tim Peters15d49292001-05-27 07:39:22 +000057
Tim Peterseb28ef22001-06-02 05:27:19 +000058But catering to unusual cases should not slow the usual ones, so we just take
59the last i bits anyway. It's up to collision resolution to do the rest. If
60we *usually* find the key we're looking for on the first try (and, it turns
61out, we usually do -- the table load factor is kept under 2/3, so the odds
62are solidly in our favor), then it makes best sense to keep the initial index
63computation dirt cheap.
Tim Peters15d49292001-05-27 07:39:22 +000064
Tim Peterseb28ef22001-06-02 05:27:19 +000065The first half of collision resolution is to visit table indices via this
66recurrence:
Tim Peters15d49292001-05-27 07:39:22 +000067
Tim Peterseb28ef22001-06-02 05:27:19 +000068 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +000069
Tim Peterseb28ef22001-06-02 05:27:19 +000070For any initial j in range(2**i), repeating that 2**i times generates each
71int in range(2**i) exactly once (see any text on random-number generation for
72proof). By itself, this doesn't help much: like linear probing (setting
73j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
74order. This would be bad, except that's not the only thing we do, and it's
75actually *good* in the common cases where hash keys are consecutive. In an
76example that's really too small to make this entirely clear, for a table of
77size 2**3 the order of indices is:
Tim Peters15d49292001-05-27 07:39:22 +000078
Tim Peterseb28ef22001-06-02 05:27:19 +000079 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
80
81If two things come in at index 5, the first place we look after is index 2,
82not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
83Linear probing is deadly in this case because there the fixed probe order
84is the *same* as the order consecutive keys are likely to arrive. But it's
85extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
86and certain that consecutive hash codes do not.
87
88The other half of the strategy is to get the other bits of the hash code
89into play. This is done by initializing a (unsigned) vrbl "perturb" to the
90full hash code, and changing the recurrence to:
91
92 j = (5*j) + 1 + perturb;
93 perturb >>= PERTURB_SHIFT;
94 use j % 2**i as the next table index;
95
96Now the probe sequence depends (eventually) on every bit in the hash code,
97and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
98because it quickly magnifies small differences in the bits that didn't affect
99the initial index. Note that because perturb is unsigned, if the recurrence
100is executed often enough perturb eventually becomes and remains 0. At that
101point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
102that's certain to find an empty slot eventually (since it generates every int
103in range(2**i), and we make sure there's always at least one empty slot).
104
105Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
106small so that the high bits of the hash code continue to affect the probe
107sequence across iterations; but you want it large so that in really bad cases
108the high-order hash bits have an effect on early iterations. 5 was "the
109best" in minimizing total collisions across experiments Tim Peters ran (on
110both normal and pathological cases), but 4 and 6 weren't significantly worse.
111
112Historical: Reimer Behrends contributed the idea of using a polynomial-based
113approach, using repeated multiplication by x in GF(2**n) where an irreducible
114polynomial for each table size was chosen such that x was a primitive root.
115Christian Tismer later extended that to use division by x instead, as an
116efficient way to get the high bits of the hash code into play. This scheme
117also gave excellent collision statistics, but was more expensive: two
118if-tests were required inside the loop; computing "the next" index took about
119the same number of operations but without as much potential parallelism
120(e.g., computing 5*j can go on at the same time as computing 1+perturb in the
121above, and then shifting perturb can be done while the table index is being
Brett Cannon77ae87c2007-10-10 00:07:50 +0000122masked); and the PyDictObject struct required a member to hold the table's
Tim Peterseb28ef22001-06-02 05:27:19 +0000123polynomial. In Tim's experiments the current scheme ran faster, produced
124equally good collision statistics, needed less code & used less memory.
Tim Peters9b10f7e2006-05-30 04:16:25 +0000125
126Theoretical Python 2.5 headache: hash codes are only C "long", but
127sizeof(Py_ssize_t) > sizeof(long) may be possible. In that case, and if a
128dict is genuinely huge, then only the slots directly reachable via indexing
129by a C long can be the first slot in a probe sequence. The probe sequence
130will still eventually reach every slot in the table, but the collision rate
131on initial probes may be much higher than this scheme was designed for.
132Getting a hash code as fat as Py_ssize_t is the only real cure. But in
133practice, this probably won't make a lick of difference for many years (at
134which point everyone will have terabytes of RAM on 64-bit boxes).
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000135*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000136
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000137/* Object used as dummy key to fill deleted entries */
Brett Cannon77ae87c2007-10-10 00:07:50 +0000138static PyObject *dummy = NULL; /* Initialized by first call to newPyDictObject() */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000139
Armin Rigoe1709372006-04-12 17:06:05 +0000140#ifdef Py_REF_DEBUG
141PyObject *
142_PyDict_Dummy(void)
143{
144 return dummy;
145}
146#endif
147
Fred Drake1bff34a2000-08-31 19:31:38 +0000148/* forward declarations */
Brett Cannon77ae87c2007-10-10 00:07:50 +0000149static PyDictEntry *
150lookdict_string(PyDictObject *mp, PyObject *key, long hash);
Fred Drake1bff34a2000-08-31 19:31:38 +0000151
152#ifdef SHOW_CONVERSION_COUNTS
153static long created = 0L;
154static long converted = 0L;
155
156static void
157show_counts(void)
158{
159 fprintf(stderr, "created %ld string dicts\n", created);
160 fprintf(stderr, "converted %ld to normal dicts\n", converted);
161 fprintf(stderr, "%.2f%% conversion rate\n", (100.0*converted)/created);
162}
163#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000164
Tim Peters6d6c1a32001-08-02 04:15:00 +0000165/* Initialization macros.
166 There are two ways to create a dict: PyDict_New() is the main C API
167 function, and the tp_new slot maps to dict_new(). In the latter case we
168 can save a little time over what PyDict_New does because it's guaranteed
169 that the PyDictObject struct is already zeroed out.
170 Everyone except dict_new() should use EMPTY_TO_MINSIZE (unless they have
171 an excellent reason not to).
172*/
173
174#define INIT_NONZERO_DICT_SLOTS(mp) do { \
Tim Petersdea48ec2001-05-22 20:40:22 +0000175 (mp)->ma_table = (mp)->ma_smalltable; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000176 (mp)->ma_mask = PyDict_MINSIZE - 1; \
177 } while(0)
178
179#define EMPTY_TO_MINSIZE(mp) do { \
180 memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000181 (mp)->ma_used = (mp)->ma_fill = 0; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000182 INIT_NONZERO_DICT_SLOTS(mp); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000183 } while(0)
184
Raymond Hettinger43442782004-03-17 21:55:03 +0000185/* Dictionary reuse scheme to save calls to malloc, free, and memset */
186#define MAXFREEDICTS 80
187static PyDictObject *free_dicts[MAXFREEDICTS];
188static int num_free_dicts = 0;
189
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000190PyObject *
Thomas Wouters78890102000-07-22 19:25:51 +0000191PyDict_New(void)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000192{
Brett Cannon77ae87c2007-10-10 00:07:50 +0000193 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000194 if (dummy == NULL) { /* Auto-initialize dummy */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000195 dummy = PyString_FromString("<dummy key>");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000196 if (dummy == NULL)
197 return NULL;
Fred Drake1bff34a2000-08-31 19:31:38 +0000198#ifdef SHOW_CONVERSION_COUNTS
199 Py_AtExit(show_counts);
200#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000201 }
Raymond Hettinger43442782004-03-17 21:55:03 +0000202 if (num_free_dicts) {
203 mp = free_dicts[--num_free_dicts];
204 assert (mp != NULL);
Martin v. Löwis68192102007-07-21 06:55:02 +0000205 assert (Py_Type(mp) == &PyDict_Type);
Raymond Hettinger43442782004-03-17 21:55:03 +0000206 _Py_NewReference((PyObject *)mp);
207 if (mp->ma_fill) {
208 EMPTY_TO_MINSIZE(mp);
209 }
210 assert (mp->ma_used == 0);
211 assert (mp->ma_table == mp->ma_smalltable);
212 assert (mp->ma_mask == PyDict_MINSIZE - 1);
213 } else {
Brett Cannon77ae87c2007-10-10 00:07:50 +0000214 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
Raymond Hettinger43442782004-03-17 21:55:03 +0000215 if (mp == NULL)
216 return NULL;
217 EMPTY_TO_MINSIZE(mp);
218 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000219 mp->ma_lookup = lookdict_string;
220#ifdef SHOW_CONVERSION_COUNTS
221 ++created;
222#endif
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000223 _PyObject_GC_TRACK(mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000224 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000225}
226
227/*
228The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000229This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000230Open addressing is preferred over chaining since the link overhead for
231chaining would be substantial (100% with typical malloc overhead).
232
Tim Peterseb28ef22001-06-02 05:27:19 +0000233The initial probe index is computed as hash mod the table size. Subsequent
234probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000235
236All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000237
Tim Peterseb28ef22001-06-02 05:27:19 +0000238(The details in this version are due to Tim Peters, building on many past
239contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
240Christian Tismer).
Fred Drake1bff34a2000-08-31 19:31:38 +0000241
Tim Petersd770ebd2006-06-01 15:50:44 +0000242lookdict() is general-purpose, and may return NULL if (and only if) a
243comparison raises an exception (this was new in Python 2.5).
244lookdict_string() below is specialized to string keys, comparison of which can
245never raise an exception; that function can never return NULL. For both, when
Brett Cannon77ae87c2007-10-10 00:07:50 +0000246the key isn't found a PyDictEntry* is returned for which the me_value field is
Tim Petersd770ebd2006-06-01 15:50:44 +0000247NULL; this is the slot in the dict at which the key would have been found, and
248the caller can (if it wishes) add the <key, value> pair to the returned
Brett Cannon77ae87c2007-10-10 00:07:50 +0000249PyDictEntry*.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000250*/
Brett Cannon77ae87c2007-10-10 00:07:50 +0000251static PyDictEntry *
252lookdict(PyDictObject *mp, PyObject *key, register long hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000253{
Tim Petersd770ebd2006-06-01 15:50:44 +0000254 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000255 register size_t perturb;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000256 register PyDictEntry *freeslot;
Tim Petersd770ebd2006-06-01 15:50:44 +0000257 register size_t mask = (size_t)mp->ma_mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000258 PyDictEntry *ep0 = mp->ma_table;
259 register PyDictEntry *ep;
Fred Drakec88b99c2000-08-31 19:04:07 +0000260 register int cmp;
Tim Peters453163d2001-06-03 04:54:32 +0000261 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000262
Tim Petersd770ebd2006-06-01 15:50:44 +0000263 i = (size_t)hash & mask;
Guido van Rossumefb46091997-01-29 15:53:56 +0000264 ep = &ep0[i];
Guido van Rossumc1c7b1a1998-10-06 16:01:14 +0000265 if (ep->me_key == NULL || ep->me_key == key)
Guido van Rossum7d181591997-01-16 21:06:45 +0000266 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000267
Guido van Rossum16e93a81997-01-28 00:00:11 +0000268 if (ep->me_key == dummy)
Guido van Rossum7d181591997-01-16 21:06:45 +0000269 freeslot = ep;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000270 else {
Fred Drakec88b99c2000-08-31 19:04:07 +0000271 if (ep->me_hash == hash) {
Tim Peters453163d2001-06-03 04:54:32 +0000272 startkey = ep->me_key;
Guido van Rossum31645ba2007-11-29 18:25:12 +0000273 Py_INCREF(startkey);
Tim Peters453163d2001-06-03 04:54:32 +0000274 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Guido van Rossum31645ba2007-11-29 18:25:12 +0000275 Py_DECREF(startkey);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000276 if (cmp < 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000277 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000278 if (ep0 == mp->ma_table && ep->me_key == startkey) {
279 if (cmp > 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000280 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000281 }
282 else {
283 /* The compare did major nasty stuff to the
284 * dict: start over.
285 * XXX A clever adversary could prevent this
286 * XXX from terminating.
287 */
Armin Rigo35f6d362006-06-01 13:19:12 +0000288 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000289 }
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000290 }
291 freeslot = NULL;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000292 }
Tim Peters15d49292001-05-27 07:39:22 +0000293
Tim Peters342c65e2001-05-13 06:43:53 +0000294 /* In the loop, me_key == dummy is by far (factor of 100s) the
295 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000296 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
297 i = (i << 2) + i + perturb + 1;
298 ep = &ep0[i & mask];
Armin Rigo35f6d362006-06-01 13:19:12 +0000299 if (ep->me_key == NULL)
300 return freeslot == NULL ? ep : freeslot;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000301 if (ep->me_key == key)
Armin Rigo35f6d362006-06-01 13:19:12 +0000302 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000303 if (ep->me_hash == hash && ep->me_key != dummy) {
Tim Peters453163d2001-06-03 04:54:32 +0000304 startkey = ep->me_key;
Guido van Rossum31645ba2007-11-29 18:25:12 +0000305 Py_INCREF(startkey);
Tim Peters453163d2001-06-03 04:54:32 +0000306 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Guido van Rossum31645ba2007-11-29 18:25:12 +0000307 Py_DECREF(startkey);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000308 if (cmp < 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000309 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000310 if (ep0 == mp->ma_table && ep->me_key == startkey) {
311 if (cmp > 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000312 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000313 }
314 else {
315 /* The compare did major nasty stuff to the
316 * dict: start over.
317 * XXX A clever adversary could prevent this
318 * XXX from terminating.
319 */
Armin Rigo35f6d362006-06-01 13:19:12 +0000320 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000321 }
Guido van Rossum16e93a81997-01-28 00:00:11 +0000322 }
Tim Peters342c65e2001-05-13 06:43:53 +0000323 else if (ep->me_key == dummy && freeslot == NULL)
324 freeslot = ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000325 }
Neal Norwitza5ccda92006-10-28 21:16:54 +0000326 assert(0); /* NOT REACHED */
327 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000328}
329
330/*
Fred Drake1bff34a2000-08-31 19:31:38 +0000331 * Hacked up version of lookdict which can assume keys are always strings;
Tim Petersd770ebd2006-06-01 15:50:44 +0000332 * this assumption allows testing for errors during PyObject_RichCompareBool()
333 * to be dropped; string-string comparisons never raise exceptions. This also
334 * means we don't need to go through PyObject_RichCompareBool(); we can always
335 * use _PyString_Eq() directly.
Fred Drake1bff34a2000-08-31 19:31:38 +0000336 *
Tim Petersd770ebd2006-06-01 15:50:44 +0000337 * This is valuable because dicts with only string keys are very common.
Fred Drake1bff34a2000-08-31 19:31:38 +0000338 */
Brett Cannon77ae87c2007-10-10 00:07:50 +0000339static PyDictEntry *
340lookdict_string(PyDictObject *mp, PyObject *key, register long hash)
Fred Drake1bff34a2000-08-31 19:31:38 +0000341{
Tim Petersd770ebd2006-06-01 15:50:44 +0000342 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000343 register size_t perturb;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000344 register PyDictEntry *freeslot;
Tim Petersd770ebd2006-06-01 15:50:44 +0000345 register size_t mask = (size_t)mp->ma_mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000346 PyDictEntry *ep0 = mp->ma_table;
347 register PyDictEntry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000348
Tim Peters0ab085c2001-09-14 00:25:33 +0000349 /* Make sure this function doesn't have to handle non-string keys,
350 including subclasses of str; e.g., one reason to subclass
351 strings is to override __eq__, and for speed we don't cater to
352 that here. */
353 if (!PyString_CheckExact(key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000354#ifdef SHOW_CONVERSION_COUNTS
355 ++converted;
356#endif
357 mp->ma_lookup = lookdict;
358 return lookdict(mp, key, hash);
359 }
Tim Peters2f228e72001-05-13 00:19:31 +0000360 i = hash & mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000361 ep = &ep0[i];
362 if (ep->me_key == NULL || ep->me_key == key)
363 return ep;
364 if (ep->me_key == dummy)
365 freeslot = ep;
366 else {
Tim Petersd770ebd2006-06-01 15:50:44 +0000367 if (ep->me_hash == hash && _PyString_Eq(ep->me_key, key))
Fred Drake1bff34a2000-08-31 19:31:38 +0000368 return ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000369 freeslot = NULL;
370 }
Tim Peters15d49292001-05-27 07:39:22 +0000371
Tim Peters342c65e2001-05-13 06:43:53 +0000372 /* In the loop, me_key == dummy is by far (factor of 100s) the
373 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000374 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
375 i = (i << 2) + i + perturb + 1;
376 ep = &ep0[i & mask];
Tim Peters342c65e2001-05-13 06:43:53 +0000377 if (ep->me_key == NULL)
378 return freeslot == NULL ? ep : freeslot;
379 if (ep->me_key == key
380 || (ep->me_hash == hash
381 && ep->me_key != dummy
Martin v. Löwiscd353062001-05-24 16:56:35 +0000382 && _PyString_Eq(ep->me_key, key)))
Fred Drake1bff34a2000-08-31 19:31:38 +0000383 return ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000384 if (ep->me_key == dummy && freeslot == NULL)
Tim Peters342c65e2001-05-13 06:43:53 +0000385 freeslot = ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000386 }
Neal Norwitza5ccda92006-10-28 21:16:54 +0000387 assert(0); /* NOT REACHED */
388 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000389}
390
391/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000392Internal routine to insert a new item into the table.
393Used both by the internal resize routine and by the public insert routine.
394Eats a reference to key and one to value.
Tim Petersd770ebd2006-06-01 15:50:44 +0000395Returns -1 if an error occurred, or 0 on success.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000396*/
Armin Rigo35f6d362006-06-01 13:19:12 +0000397static int
Brett Cannon77ae87c2007-10-10 00:07:50 +0000398insertdict(register PyDictObject *mp, PyObject *key, long hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000399{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000400 PyObject *old_value;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000401 register PyDictEntry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000402 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
403
404 assert(mp->ma_lookup != NULL);
405 ep = mp->ma_lookup(mp, key, hash);
Armin Rigo35f6d362006-06-01 13:19:12 +0000406 if (ep == NULL) {
407 Py_DECREF(key);
408 Py_DECREF(value);
409 return -1;
410 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000411 if (ep->me_value != NULL) {
Guido van Rossumd7047b31995-01-02 19:07:15 +0000412 old_value = ep->me_value;
413 ep->me_value = value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000414 Py_DECREF(old_value); /* which **CAN** re-enter */
415 Py_DECREF(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000416 }
417 else {
418 if (ep->me_key == NULL)
419 mp->ma_fill++;
Raymond Hettinger07ead172005-02-05 23:42:57 +0000420 else {
421 assert(ep->me_key == dummy);
422 Py_DECREF(dummy);
423 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000424 ep->me_key = key;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000425 ep->me_hash = (Py_ssize_t)hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000426 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000427 mp->ma_used++;
428 }
Armin Rigo35f6d362006-06-01 13:19:12 +0000429 return 0;
430}
431
432/*
433Internal routine used by dictresize() to insert an item which is
434known to be absent from the dict. This routine also assumes that
435the dict contains no deleted entries. Besides the performance benefit,
436using insertdict() in dictresize() is dangerous (SF bug #1456209).
Tim Petersd770ebd2006-06-01 15:50:44 +0000437Note that no refcounts are changed by this routine; if needed, the caller
438is responsible for incref'ing `key` and `value`.
Armin Rigo35f6d362006-06-01 13:19:12 +0000439*/
440static void
Brett Cannon77ae87c2007-10-10 00:07:50 +0000441insertdict_clean(register PyDictObject *mp, PyObject *key, long hash,
Armin Rigo35f6d362006-06-01 13:19:12 +0000442 PyObject *value)
443{
Tim Petersd770ebd2006-06-01 15:50:44 +0000444 register size_t i;
Armin Rigo35f6d362006-06-01 13:19:12 +0000445 register size_t perturb;
Tim Petersd770ebd2006-06-01 15:50:44 +0000446 register size_t mask = (size_t)mp->ma_mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000447 PyDictEntry *ep0 = mp->ma_table;
448 register PyDictEntry *ep;
Armin Rigo35f6d362006-06-01 13:19:12 +0000449
450 i = hash & mask;
451 ep = &ep0[i];
452 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
453 i = (i << 2) + i + perturb + 1;
454 ep = &ep0[i & mask];
455 }
Tim Petersd770ebd2006-06-01 15:50:44 +0000456 assert(ep->me_value == NULL);
Armin Rigo35f6d362006-06-01 13:19:12 +0000457 mp->ma_fill++;
458 ep->me_key = key;
459 ep->me_hash = (Py_ssize_t)hash;
460 ep->me_value = value;
461 mp->ma_used++;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000462}
463
464/*
465Restructure the table by allocating a new table and reinserting all
466items again. When entries have been deleted, the new table may
467actually be smaller than the old one.
468*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000469static int
Brett Cannon77ae87c2007-10-10 00:07:50 +0000470dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000471{
Tim Peters9b10f7e2006-05-30 04:16:25 +0000472 Py_ssize_t newsize;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000473 PyDictEntry *oldtable, *newtable, *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000474 Py_ssize_t i;
Tim Peters0c6010b2001-05-23 23:33:57 +0000475 int is_oldtable_malloced;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000476 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000477
478 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000479
Tim Peterseb28ef22001-06-02 05:27:19 +0000480 /* Find the smallest table size > minused. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000481 for (newsize = PyDict_MINSIZE;
Tim Peterseb28ef22001-06-02 05:27:19 +0000482 newsize <= minused && newsize > 0;
483 newsize <<= 1)
484 ;
485 if (newsize <= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000486 PyErr_NoMemory();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000487 return -1;
488 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000489
490 /* Get space for a new table. */
491 oldtable = mp->ma_table;
492 assert(oldtable != NULL);
493 is_oldtable_malloced = oldtable != mp->ma_smalltable;
494
Tim Peters6d6c1a32001-08-02 04:15:00 +0000495 if (newsize == PyDict_MINSIZE) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000496 /* A large table is shrinking, or we can't get any smaller. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000497 newtable = mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000498 if (newtable == oldtable) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000499 if (mp->ma_fill == mp->ma_used) {
500 /* No dummies, so no point doing anything. */
Tim Peters0c6010b2001-05-23 23:33:57 +0000501 return 0;
Tim Petersf8a548c2001-05-24 16:26:40 +0000502 }
503 /* We're not going to resize it, but rebuild the
504 table anyway to purge old dummy entries.
505 Subtle: This is *necessary* if fill==size,
506 as lookdict needs at least one virgin slot to
507 terminate failing searches. If fill < size, it's
508 merely desirable, as dummies slow searches. */
509 assert(mp->ma_fill > mp->ma_used);
Tim Peters0c6010b2001-05-23 23:33:57 +0000510 memcpy(small_copy, oldtable, sizeof(small_copy));
511 oldtable = small_copy;
512 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000513 }
514 else {
Brett Cannon77ae87c2007-10-10 00:07:50 +0000515 newtable = PyMem_NEW(PyDictEntry, newsize);
Tim Petersdea48ec2001-05-22 20:40:22 +0000516 if (newtable == NULL) {
517 PyErr_NoMemory();
518 return -1;
519 }
520 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000521
522 /* Make the dict empty, using the new table. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000523 assert(newtable != oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000524 mp->ma_table = newtable;
Tim Petersafb6ae82001-06-04 21:00:21 +0000525 mp->ma_mask = newsize - 1;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000526 memset(newtable, 0, sizeof(PyDictEntry) * newsize);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000527 mp->ma_used = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000528 i = mp->ma_fill;
529 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000530
Tim Peters19283142001-05-17 22:25:34 +0000531 /* Copy the data over; this is refcount-neutral for active entries;
532 dummy entries aren't copied over, of course */
Tim Petersdea48ec2001-05-22 20:40:22 +0000533 for (ep = oldtable; i > 0; ep++) {
534 if (ep->me_value != NULL) { /* active entry */
535 --i;
Armin Rigo35f6d362006-06-01 13:19:12 +0000536 insertdict_clean(mp, ep->me_key, (long)ep->me_hash,
537 ep->me_value);
Tim Petersdea48ec2001-05-22 20:40:22 +0000538 }
Tim Peters19283142001-05-17 22:25:34 +0000539 else if (ep->me_key != NULL) { /* dummy entry */
Tim Petersdea48ec2001-05-22 20:40:22 +0000540 --i;
Tim Peters19283142001-05-17 22:25:34 +0000541 assert(ep->me_key == dummy);
542 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000543 }
Tim Peters19283142001-05-17 22:25:34 +0000544 /* else key == value == NULL: nothing to do */
Guido van Rossumd7047b31995-01-02 19:07:15 +0000545 }
546
Tim Peters0c6010b2001-05-23 23:33:57 +0000547 if (is_oldtable_malloced)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000548 PyMem_DEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000549 return 0;
550}
551
Tim Petersd770ebd2006-06-01 15:50:44 +0000552/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
553 * that may occur (originally dicts supported only string keys, and exceptions
554 * weren't possible). So, while the original intent was that a NULL return
Andrew M. Kuchling0067b5f2006-08-04 20:37:43 +0000555 * meant the key wasn't present, in reality it can mean that, or that an error
Tim Petersd770ebd2006-06-01 15:50:44 +0000556 * (suppressed) occurred while computing the key's hash, or that some error
557 * (suppressed) occurred when comparing keys in the dict's internal probe
558 * sequence. A nasty example of the latter is when a Python-coded comparison
559 * function hits a stack-depth error, which can cause this to return NULL
560 * even if the key is present.
561 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000562PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000563PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000564{
565 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000566 PyDictObject *mp = (PyDictObject *)op;
567 PyDictEntry *ep;
Armin Rigo35f6d362006-06-01 13:19:12 +0000568 PyThreadState *tstate;
Tim Petersd770ebd2006-06-01 15:50:44 +0000569 if (!PyDict_Check(op))
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000570 return NULL;
Tim Peters0ab085c2001-09-14 00:25:33 +0000571 if (!PyString_CheckExact(key) ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000572 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Guido van Rossumca756f21997-01-23 19:39:29 +0000573 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000574 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000575 if (hash == -1) {
576 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000577 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000578 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000579 }
Armin Rigo35f6d362006-06-01 13:19:12 +0000580
581 /* We can arrive here with a NULL tstate during initialization:
582 try running "python -Wi" for an example related to string
583 interning. Let's just hope that no exception occurs then... */
Armin Rigoacd0d6d2006-06-10 10:57:40 +0000584 tstate = _PyThreadState_Current;
Armin Rigo35f6d362006-06-01 13:19:12 +0000585 if (tstate != NULL && tstate->curexc_type != NULL) {
586 /* preserve the existing exception */
587 PyObject *err_type, *err_value, *err_tb;
588 PyErr_Fetch(&err_type, &err_value, &err_tb);
589 ep = (mp->ma_lookup)(mp, key, hash);
590 /* ignore errors */
591 PyErr_Restore(err_type, err_value, err_tb);
592 if (ep == NULL)
593 return NULL;
594 }
595 else {
596 ep = (mp->ma_lookup)(mp, key, hash);
597 if (ep == NULL) {
598 PyErr_Clear();
599 return NULL;
600 }
601 }
602 return ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000603}
604
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000605/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
Tim Peters60b29962006-01-01 01:19:23 +0000606 * dictionary if it's merely replacing the value for an existing key.
607 * This means that it's safe to loop over a dictionary with PyDict_Next()
608 * and occasionally replace a value -- but you can't insert new keys or
609 * remove them.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000610 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000611int
Tim Peters1f5871e2000-07-04 17:44:48 +0000612PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000613{
Brett Cannon77ae87c2007-10-10 00:07:50 +0000614 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000615 register long hash;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000616 register Py_ssize_t n_used;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000617
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000618 if (!PyDict_Check(op)) {
619 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000620 return -1;
621 }
Neal Norwitz48808a12006-07-21 05:29:58 +0000622 assert(key);
623 assert(value);
Brett Cannon77ae87c2007-10-10 00:07:50 +0000624 mp = (PyDictObject *)op;
Tim Peters0ab085c2001-09-14 00:25:33 +0000625 if (PyString_CheckExact(key)) {
Guido van Rossum45ec02a2002-08-19 21:43:18 +0000626 hash = ((PyStringObject *)key)->ob_shash;
627 if (hash == -1)
628 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000629 }
Tim Peters1f7df352002-03-29 03:29:08 +0000630 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000631 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000632 if (hash == -1)
633 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000634 }
Tim Petersafb6ae82001-06-04 21:00:21 +0000635 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000636 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000637 Py_INCREF(value);
638 Py_INCREF(key);
Armin Rigo35f6d362006-06-01 13:19:12 +0000639 if (insertdict(mp, key, hash, value) != 0)
640 return -1;
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000641 /* If we added a key, we can safely resize. Otherwise just return!
642 * If fill >= 2/3 size, adjust size. Normally, this doubles or
643 * quaduples the size, but it's also possible for the dict to shrink
Tim Peters60b29962006-01-01 01:19:23 +0000644 * (if ma_fill is much larger than ma_used, meaning a lot of dict
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000645 * keys have been * deleted).
Tim Peters60b29962006-01-01 01:19:23 +0000646 *
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000647 * Quadrupling the size improves average dictionary sparseness
648 * (reducing collisions) at the cost of some memory and iteration
649 * speed (which loops over every possible entry). It also halves
Neal Norwitz80af59c2006-05-30 04:19:21 +0000650 * the number of expensive resize operations in a growing dictionary.
Tim Peters60b29962006-01-01 01:19:23 +0000651 *
652 * Very large dictionaries (over 50K items) use doubling instead.
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000653 * This may help applications with severe memory constraints.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000654 */
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000655 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
656 return 0;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000657 return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000658}
659
660int
Tim Peters1f5871e2000-07-04 17:44:48 +0000661PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000662{
Brett Cannon77ae87c2007-10-10 00:07:50 +0000663 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000664 register long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000665 register PyDictEntry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000666 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000667
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000668 if (!PyDict_Check(op)) {
669 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000670 return -1;
671 }
Neal Norwitz48808a12006-07-21 05:29:58 +0000672 assert(key);
Tim Peters0ab085c2001-09-14 00:25:33 +0000673 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +0000674 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000675 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000676 if (hash == -1)
677 return -1;
678 }
Brett Cannon77ae87c2007-10-10 00:07:50 +0000679 mp = (PyDictObject *)op;
Fred Drake1bff34a2000-08-31 19:31:38 +0000680 ep = (mp->ma_lookup)(mp, key, hash);
Armin Rigo35f6d362006-06-01 13:19:12 +0000681 if (ep == NULL)
682 return -1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000683 if (ep->me_value == NULL) {
Georg Brandlb9f4ad32006-10-29 18:31:42 +0000684 set_key_error(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000685 return -1;
686 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000687 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000688 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000689 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000690 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000691 ep->me_value = NULL;
692 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000693 Py_DECREF(old_value);
694 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000695 return 0;
696}
697
Guido van Rossum25831651993-05-19 14:50:45 +0000698void
Tim Peters1f5871e2000-07-04 17:44:48 +0000699PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000700{
Brett Cannon77ae87c2007-10-10 00:07:50 +0000701 PyDictObject *mp;
702 PyDictEntry *ep, *table;
Tim Petersdea48ec2001-05-22 20:40:22 +0000703 int table_is_malloced;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000704 Py_ssize_t fill;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000705 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Petersdea48ec2001-05-22 20:40:22 +0000706#ifdef Py_DEBUG
Tim Peters9b10f7e2006-05-30 04:16:25 +0000707 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +0000708#endif
709
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000710 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000711 return;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000712 mp = (PyDictObject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000713#ifdef Py_DEBUG
Tim Petersafb6ae82001-06-04 21:00:21 +0000714 n = mp->ma_mask + 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000715 i = 0;
716#endif
717
718 table = mp->ma_table;
719 assert(table != NULL);
720 table_is_malloced = table != mp->ma_smalltable;
721
722 /* This is delicate. During the process of clearing the dict,
723 * decrefs can cause the dict to mutate. To avoid fatal confusion
724 * (voice of experience), we have to make the dict empty before
725 * clearing the slots, and never refer to anything via mp->xxx while
726 * clearing.
727 */
728 fill = mp->ma_fill;
729 if (table_is_malloced)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000730 EMPTY_TO_MINSIZE(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000731
732 else if (fill > 0) {
733 /* It's a small table with something that needs to be cleared.
734 * Afraid the only safe way is to copy the dict entries into
735 * another small table first.
736 */
737 memcpy(small_copy, table, sizeof(small_copy));
738 table = small_copy;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000739 EMPTY_TO_MINSIZE(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000740 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000741 /* else it's a small table that's already empty */
742
743 /* Now we can finally clear things. If C had refcounts, we could
744 * assert that the refcount on table is 1 now, i.e. that this function
745 * has unique access to it, so decref side-effects can't alter it.
746 */
747 for (ep = table; fill > 0; ++ep) {
748#ifdef Py_DEBUG
749 assert(i < n);
750 ++i;
751#endif
752 if (ep->me_key) {
753 --fill;
754 Py_DECREF(ep->me_key);
755 Py_XDECREF(ep->me_value);
756 }
757#ifdef Py_DEBUG
758 else
759 assert(ep->me_value == NULL);
760#endif
761 }
762
763 if (table_is_malloced)
764 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000765}
766
Tim Peters080c88b2003-02-15 03:01:11 +0000767/*
768 * Iterate over a dict. Use like so:
769 *
Tim Peters9b10f7e2006-05-30 04:16:25 +0000770 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +0000771 * PyObject *key, *value;
772 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +0000773 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +0000774 * Refer to borrowed references in key and value.
775 * }
776 *
777 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +0000778 * mutates the dict. One exception: it is safe if the loop merely changes
779 * the values associated with the keys (but doesn't insert new keys or
780 * delete keys), via PyDict_SetItem().
781 */
Guido van Rossum25831651993-05-19 14:50:45 +0000782int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000783PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000784{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000785 register Py_ssize_t i;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000786 register Py_ssize_t mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000787 register PyDictEntry *ep;
Raymond Hettinger43442782004-03-17 21:55:03 +0000788
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000789 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000790 return 0;
Guido van Rossum25831651993-05-19 14:50:45 +0000791 i = *ppos;
792 if (i < 0)
793 return 0;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000794 ep = ((PyDictObject *)op)->ma_table;
795 mask = ((PyDictObject *)op)->ma_mask;
Raymond Hettinger43442782004-03-17 21:55:03 +0000796 while (i <= mask && ep[i].me_value == NULL)
Guido van Rossum25831651993-05-19 14:50:45 +0000797 i++;
798 *ppos = i+1;
Raymond Hettinger43442782004-03-17 21:55:03 +0000799 if (i > mask)
Guido van Rossum25831651993-05-19 14:50:45 +0000800 return 0;
801 if (pkey)
Raymond Hettinger43442782004-03-17 21:55:03 +0000802 *pkey = ep[i].me_key;
Guido van Rossum25831651993-05-19 14:50:45 +0000803 if (pvalue)
Raymond Hettinger43442782004-03-17 21:55:03 +0000804 *pvalue = ep[i].me_value;
Guido van Rossum25831651993-05-19 14:50:45 +0000805 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000806}
807
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000808/* Internal version of PyDict_Next that returns a hash value in addition to the key and value.*/
809int
810_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue, long *phash)
811{
812 register Py_ssize_t i;
813 register Py_ssize_t mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000814 register PyDictEntry *ep;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000815
816 if (!PyDict_Check(op))
817 return 0;
818 i = *ppos;
819 if (i < 0)
820 return 0;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000821 ep = ((PyDictObject *)op)->ma_table;
822 mask = ((PyDictObject *)op)->ma_mask;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000823 while (i <= mask && ep[i].me_value == NULL)
824 i++;
825 *ppos = i+1;
826 if (i > mask)
827 return 0;
828 *phash = (long)(ep[i].me_hash);
829 if (pkey)
830 *pkey = ep[i].me_key;
831 if (pvalue)
832 *pvalue = ep[i].me_value;
833 return 1;
834}
835
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000836/* Methods */
837
838static void
Brett Cannon77ae87c2007-10-10 00:07:50 +0000839dict_dealloc(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000840{
Brett Cannon77ae87c2007-10-10 00:07:50 +0000841 register PyDictEntry *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000842 Py_ssize_t fill = mp->ma_fill;
Guido van Rossumff413af2002-03-28 20:34:59 +0000843 PyObject_GC_UnTrack(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000844 Py_TRASHCAN_SAFE_BEGIN(mp)
Tim Petersdea48ec2001-05-22 20:40:22 +0000845 for (ep = mp->ma_table; fill > 0; ep++) {
846 if (ep->me_key) {
847 --fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000848 Py_DECREF(ep->me_key);
Tim Petersdea48ec2001-05-22 20:40:22 +0000849 Py_XDECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000850 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000851 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000852 if (mp->ma_table != mp->ma_smalltable)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000853 PyMem_DEL(mp->ma_table);
Martin v. Löwis68192102007-07-21 06:55:02 +0000854 if (num_free_dicts < MAXFREEDICTS && Py_Type(mp) == &PyDict_Type)
Raymond Hettinger43442782004-03-17 21:55:03 +0000855 free_dicts[num_free_dicts++] = mp;
Tim Peters60b29962006-01-01 01:19:23 +0000856 else
Martin v. Löwis68192102007-07-21 06:55:02 +0000857 Py_Type(mp)->tp_free((PyObject *)mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000858 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000859}
860
861static int
Brett Cannon77ae87c2007-10-10 00:07:50 +0000862dict_print(register PyDictObject *mp, register FILE *fp, register int flags)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000863{
Tim Peters9b10f7e2006-05-30 04:16:25 +0000864 register Py_ssize_t i;
865 register Py_ssize_t any;
Tim Peters33f4a6a2006-05-30 05:23:59 +0000866 int status;
Guido van Rossum255443b1998-04-10 22:47:14 +0000867
Tim Peters33f4a6a2006-05-30 05:23:59 +0000868 status = Py_ReprEnter((PyObject*)mp);
869 if (status != 0) {
870 if (status < 0)
871 return status;
Brett Cannon01531592007-09-17 03:28:34 +0000872 Py_BEGIN_ALLOW_THREADS
Guido van Rossum255443b1998-04-10 22:47:14 +0000873 fprintf(fp, "{...}");
Brett Cannon01531592007-09-17 03:28:34 +0000874 Py_END_ALLOW_THREADS
Guido van Rossum255443b1998-04-10 22:47:14 +0000875 return 0;
876 }
877
Brett Cannon01531592007-09-17 03:28:34 +0000878 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000879 fprintf(fp, "{");
Brett Cannon01531592007-09-17 03:28:34 +0000880 Py_END_ALLOW_THREADS
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000881 any = 0;
Tim Petersafb6ae82001-06-04 21:00:21 +0000882 for (i = 0; i <= mp->ma_mask; i++) {
Brett Cannon77ae87c2007-10-10 00:07:50 +0000883 PyDictEntry *ep = mp->ma_table + i;
Tim Peters23cf6be2001-06-02 08:02:56 +0000884 PyObject *pvalue = ep->me_value;
885 if (pvalue != NULL) {
886 /* Prevent PyObject_Repr from deleting value during
887 key format */
888 Py_INCREF(pvalue);
Brett Cannon01531592007-09-17 03:28:34 +0000889 if (any++ > 0) {
890 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000891 fprintf(fp, ", ");
Brett Cannon01531592007-09-17 03:28:34 +0000892 Py_END_ALLOW_THREADS
893 }
Guido van Rossum255443b1998-04-10 22:47:14 +0000894 if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000895 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000896 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000897 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000898 }
Brett Cannon01531592007-09-17 03:28:34 +0000899 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000900 fprintf(fp, ": ");
Brett Cannon01531592007-09-17 03:28:34 +0000901 Py_END_ALLOW_THREADS
Tim Peters19b77cf2001-06-02 08:27:39 +0000902 if (PyObject_Print(pvalue, fp, 0) != 0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000903 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000904 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000905 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000906 }
Tim Peters23cf6be2001-06-02 08:02:56 +0000907 Py_DECREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000908 }
909 }
Brett Cannon01531592007-09-17 03:28:34 +0000910 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000911 fprintf(fp, "}");
Brett Cannon01531592007-09-17 03:28:34 +0000912 Py_END_ALLOW_THREADS
Guido van Rossum255443b1998-04-10 22:47:14 +0000913 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000914 return 0;
915}
916
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000917static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +0000918dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000919{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000920 Py_ssize_t i;
Tim Petersa7259592001-06-16 05:11:17 +0000921 PyObject *s, *temp, *colon = NULL;
922 PyObject *pieces = NULL, *result = NULL;
923 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +0000924
Tim Petersa7259592001-06-16 05:11:17 +0000925 i = Py_ReprEnter((PyObject *)mp);
Guido van Rossum255443b1998-04-10 22:47:14 +0000926 if (i != 0) {
Tim Petersa7259592001-06-16 05:11:17 +0000927 return i > 0 ? PyString_FromString("{...}") : NULL;
Guido van Rossum255443b1998-04-10 22:47:14 +0000928 }
929
Tim Petersa7259592001-06-16 05:11:17 +0000930 if (mp->ma_used == 0) {
931 result = PyString_FromString("{}");
932 goto Done;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000933 }
Tim Petersa7259592001-06-16 05:11:17 +0000934
935 pieces = PyList_New(0);
936 if (pieces == NULL)
937 goto Done;
938
939 colon = PyString_FromString(": ");
940 if (colon == NULL)
941 goto Done;
942
943 /* Do repr() on each key+value pair, and insert ": " between them.
944 Note that repr may mutate the dict. */
Tim Petersc6057842001-06-16 07:52:53 +0000945 i = 0;
946 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Tim Petersa7259592001-06-16 05:11:17 +0000947 int status;
948 /* Prevent repr from deleting value during key format. */
949 Py_INCREF(value);
950 s = PyObject_Repr(key);
951 PyString_Concat(&s, colon);
952 PyString_ConcatAndDel(&s, PyObject_Repr(value));
953 Py_DECREF(value);
954 if (s == NULL)
955 goto Done;
956 status = PyList_Append(pieces, s);
957 Py_DECREF(s); /* append created a new ref */
958 if (status < 0)
959 goto Done;
960 }
961
962 /* Add "{}" decorations to the first and last items. */
963 assert(PyList_GET_SIZE(pieces) > 0);
964 s = PyString_FromString("{");
965 if (s == NULL)
966 goto Done;
967 temp = PyList_GET_ITEM(pieces, 0);
968 PyString_ConcatAndDel(&s, temp);
969 PyList_SET_ITEM(pieces, 0, s);
970 if (s == NULL)
971 goto Done;
972
973 s = PyString_FromString("}");
974 if (s == NULL)
975 goto Done;
976 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
977 PyString_ConcatAndDel(&temp, s);
978 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
979 if (temp == NULL)
980 goto Done;
981
982 /* Paste them all together with ", " between. */
983 s = PyString_FromString(", ");
984 if (s == NULL)
985 goto Done;
986 result = _PyString_Join(s, pieces);
Tim Peters0ab085c2001-09-14 00:25:33 +0000987 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +0000988
989Done:
990 Py_XDECREF(pieces);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000991 Py_XDECREF(colon);
Tim Petersa7259592001-06-16 05:11:17 +0000992 Py_ReprLeave((PyObject *)mp);
993 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000994}
995
Martin v. Löwis18e16552006-02-15 17:27:45 +0000996static Py_ssize_t
Brett Cannon77ae87c2007-10-10 00:07:50 +0000997dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000998{
999 return mp->ma_used;
1000}
1001
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001002static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001003dict_subscript(PyDictObject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001004{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001005 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001006 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001007 PyDictEntry *ep;
Tim Petersdea48ec2001-05-22 20:40:22 +00001008 assert(mp->ma_table != NULL);
Tim Peters0ab085c2001-09-14 00:25:33 +00001009 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001010 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001011 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001012 if (hash == -1)
1013 return NULL;
1014 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001015 ep = (mp->ma_lookup)(mp, key, hash);
1016 if (ep == NULL)
1017 return NULL;
1018 v = ep->me_value;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001019 if (v == NULL) {
1020 if (!PyDict_CheckExact(mp)) {
1021 /* Look up __missing__ method if we're a subclass. */
Guido van Rossum4b92a822006-02-25 23:32:30 +00001022 PyObject *missing;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001023 static PyObject *missing_str = NULL;
1024 if (missing_str == NULL)
Tim Peters9b10f7e2006-05-30 04:16:25 +00001025 missing_str =
Guido van Rossum1968ad32006-02-25 22:38:04 +00001026 PyString_InternFromString("__missing__");
Martin v. Löwis68192102007-07-21 06:55:02 +00001027 missing = _PyType_Lookup(Py_Type(mp), missing_str);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001028 if (missing != NULL)
1029 return PyObject_CallFunctionObjArgs(missing,
1030 (PyObject *)mp, key, NULL);
1031 }
Georg Brandlb9f4ad32006-10-29 18:31:42 +00001032 set_key_error(key);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001033 return NULL;
1034 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001035 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001036 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001037 return v;
1038}
1039
1040static int
Brett Cannon77ae87c2007-10-10 00:07:50 +00001041dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001042{
1043 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001044 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001045 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001046 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001047}
1048
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001049static PyMappingMethods dict_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001050 (lenfunc)dict_length, /*mp_length*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001051 (binaryfunc)dict_subscript, /*mp_subscript*/
1052 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001053};
1054
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001055static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001056dict_keys(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001057{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001058 register PyObject *v;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001059 register Py_ssize_t i, j;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001060 PyDictEntry *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001061 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001062
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001063 again:
1064 n = mp->ma_used;
1065 v = PyList_New(n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001066 if (v == NULL)
1067 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001068 if (n != mp->ma_used) {
1069 /* Durnit. The allocations caused the dict to resize.
1070 * Just start over, this shouldn't normally happen.
1071 */
1072 Py_DECREF(v);
1073 goto again;
1074 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001075 ep = mp->ma_table;
1076 mask = mp->ma_mask;
1077 for (i = 0, j = 0; i <= mask; i++) {
1078 if (ep[i].me_value != NULL) {
1079 PyObject *key = ep[i].me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001080 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001081 PyList_SET_ITEM(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001082 j++;
1083 }
1084 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001085 assert(j == n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001086 return v;
1087}
1088
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001089static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001090dict_values(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001091{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001092 register PyObject *v;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001093 register Py_ssize_t i, j;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001094 PyDictEntry *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001095 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001096
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001097 again:
1098 n = mp->ma_used;
1099 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001100 if (v == NULL)
1101 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001102 if (n != mp->ma_used) {
1103 /* Durnit. The allocations caused the dict to resize.
1104 * Just start over, this shouldn't normally happen.
1105 */
1106 Py_DECREF(v);
1107 goto again;
1108 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001109 ep = mp->ma_table;
1110 mask = mp->ma_mask;
1111 for (i = 0, j = 0; i <= mask; i++) {
1112 if (ep[i].me_value != NULL) {
1113 PyObject *value = ep[i].me_value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001114 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001115 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001116 j++;
1117 }
1118 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001119 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001120 return v;
1121}
1122
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001123static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001124dict_items(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001125{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001126 register PyObject *v;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001127 register Py_ssize_t i, j, n;
1128 Py_ssize_t mask;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001129 PyObject *item, *key, *value;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001130 PyDictEntry *ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001131
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001132 /* Preallocate the list of tuples, to avoid allocations during
1133 * the loop over the items, which could trigger GC, which
1134 * could resize the dict. :-(
1135 */
1136 again:
1137 n = mp->ma_used;
1138 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001139 if (v == NULL)
1140 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001141 for (i = 0; i < n; i++) {
1142 item = PyTuple_New(2);
1143 if (item == NULL) {
1144 Py_DECREF(v);
1145 return NULL;
1146 }
1147 PyList_SET_ITEM(v, i, item);
1148 }
1149 if (n != mp->ma_used) {
1150 /* Durnit. The allocations caused the dict to resize.
1151 * Just start over, this shouldn't normally happen.
1152 */
1153 Py_DECREF(v);
1154 goto again;
1155 }
1156 /* Nothing we do below makes any function calls. */
Raymond Hettinger43442782004-03-17 21:55:03 +00001157 ep = mp->ma_table;
1158 mask = mp->ma_mask;
1159 for (i = 0, j = 0; i <= mask; i++) {
Raymond Hettinger06905122004-03-19 10:30:00 +00001160 if ((value=ep[i].me_value) != NULL) {
Raymond Hettinger43442782004-03-17 21:55:03 +00001161 key = ep[i].me_key;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001162 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001163 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001164 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001165 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001166 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001167 j++;
1168 }
1169 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001170 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001171 return v;
1172}
1173
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001174static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001175dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001176{
1177 PyObject *seq;
1178 PyObject *value = Py_None;
1179 PyObject *it; /* iter(seq) */
1180 PyObject *key;
1181 PyObject *d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001182 int status;
1183
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001184 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001185 return NULL;
1186
1187 d = PyObject_CallObject(cls, NULL);
1188 if (d == NULL)
1189 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001190
Raymond Hettingercdcf8872007-11-07 02:26:17 +00001191 if (PyDict_CheckExact(d) && PyDict_CheckExact(seq)) {
1192 PyDictObject *mp = (PyDictObject *)d;
1193 PyObject *oldvalue;
1194 Py_ssize_t pos = 0;
1195 PyObject *key;
1196 long hash;
1197
Raymond Hettinger34448792007-11-09 23:14:44 +00001198 if (dictresize(mp, PySet_GET_SIZE(seq)))
Raymond Hettingercdcf8872007-11-07 02:26:17 +00001199 return NULL;
1200
1201 while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
1202 Py_INCREF(key);
1203 Py_INCREF(value);
1204 if (insertdict(mp, key, hash, value))
1205 return NULL;
1206 }
1207 return d;
1208 }
1209
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00001210 if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) {
Brett Cannon77ae87c2007-10-10 00:07:50 +00001211 PyDictObject *mp = (PyDictObject *)d;
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00001212 Py_ssize_t pos = 0;
1213 PyObject *key;
1214 long hash;
1215
1216 if (dictresize(mp, PySet_GET_SIZE(seq)))
1217 return NULL;
1218
1219 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
1220 Py_INCREF(key);
Raymond Hettingere3146f52007-03-21 20:33:57 +00001221 Py_INCREF(value);
1222 if (insertdict(mp, key, hash, value))
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00001223 return NULL;
1224 }
1225 return d;
1226 }
1227
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001228 it = PyObject_GetIter(seq);
1229 if (it == NULL){
1230 Py_DECREF(d);
1231 return NULL;
1232 }
1233
Raymond Hettinger34448792007-11-09 23:14:44 +00001234 if (PyDict_CheckExact(d)) {
1235 while ((key = PyIter_Next(it)) != NULL) {
1236 status = PyDict_SetItem(d, key, value);
1237 Py_DECREF(key);
1238 if (status < 0)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001239 goto Fail;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001240 }
Raymond Hettinger34448792007-11-09 23:14:44 +00001241 } else {
1242 while ((key = PyIter_Next(it)) != NULL) {
1243 status = PyObject_SetItem(d, key, value);
1244 Py_DECREF(key);
1245 if (status < 0)
1246 goto Fail;
1247 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001248 }
1249
Raymond Hettinger34448792007-11-09 23:14:44 +00001250 if (PyErr_Occurred())
1251 goto Fail;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001252 Py_DECREF(it);
1253 return d;
1254
1255Fail:
1256 Py_DECREF(it);
1257 Py_DECREF(d);
1258 return NULL;
1259}
1260
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001261static int
1262dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001263{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001264 PyObject *arg = NULL;
1265 int result = 0;
1266
1267 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1268 result = -1;
1269
1270 else if (arg != NULL) {
1271 if (PyObject_HasAttrString(arg, "keys"))
1272 result = PyDict_Merge(self, arg, 1);
1273 else
1274 result = PyDict_MergeFromSeq2(self, arg, 1);
1275 }
1276 if (result == 0 && kwds != NULL)
1277 result = PyDict_Merge(self, kwds, 1);
1278 return result;
1279}
1280
1281static PyObject *
1282dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1283{
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001284 if (dict_update_common(self, args, kwds, "update") != -1)
1285 Py_RETURN_NONE;
1286 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001287}
1288
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001289/* Update unconditionally replaces existing items.
1290 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001291 otherwise it leaves existing items unchanged.
1292
1293 PyDict_{Update,Merge} update/merge from a mapping object.
1294
Tim Petersf582b822001-12-11 18:51:08 +00001295 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001296 producing iterable objects of length 2.
1297*/
1298
Tim Petersf582b822001-12-11 18:51:08 +00001299int
Tim Peters1fc240e2001-10-26 05:06:50 +00001300PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1301{
1302 PyObject *it; /* iter(seq2) */
Tim Peters9b10f7e2006-05-30 04:16:25 +00001303 Py_ssize_t i; /* index into seq2 of current element */
Tim Peters1fc240e2001-10-26 05:06:50 +00001304 PyObject *item; /* seq2[i] */
1305 PyObject *fast; /* item as a 2-tuple or 2-list */
1306
1307 assert(d != NULL);
1308 assert(PyDict_Check(d));
1309 assert(seq2 != NULL);
1310
1311 it = PyObject_GetIter(seq2);
1312 if (it == NULL)
1313 return -1;
1314
1315 for (i = 0; ; ++i) {
1316 PyObject *key, *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001317 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001318
1319 fast = NULL;
1320 item = PyIter_Next(it);
1321 if (item == NULL) {
1322 if (PyErr_Occurred())
1323 goto Fail;
1324 break;
1325 }
1326
1327 /* Convert item to sequence, and verify length 2. */
1328 fast = PySequence_Fast(item, "");
1329 if (fast == NULL) {
1330 if (PyErr_ExceptionMatches(PyExc_TypeError))
1331 PyErr_Format(PyExc_TypeError,
1332 "cannot convert dictionary update "
Neal Norwitzd3881b02006-05-30 04:25:05 +00001333 "sequence element #%zd to a sequence",
Tim Peters1fc240e2001-10-26 05:06:50 +00001334 i);
1335 goto Fail;
1336 }
1337 n = PySequence_Fast_GET_SIZE(fast);
1338 if (n != 2) {
1339 PyErr_Format(PyExc_ValueError,
Neal Norwitzd3881b02006-05-30 04:25:05 +00001340 "dictionary update sequence element #%zd "
Martin v. Löwis2c95cc62006-02-16 06:54:25 +00001341 "has length %zd; 2 is required",
Martin v. Löwise0e89f72006-02-16 06:59:22 +00001342 i, n);
Tim Peters1fc240e2001-10-26 05:06:50 +00001343 goto Fail;
1344 }
1345
1346 /* Update/merge with this (key, value) pair. */
1347 key = PySequence_Fast_GET_ITEM(fast, 0);
1348 value = PySequence_Fast_GET_ITEM(fast, 1);
1349 if (override || PyDict_GetItem(d, key) == NULL) {
1350 int status = PyDict_SetItem(d, key, value);
1351 if (status < 0)
1352 goto Fail;
1353 }
1354 Py_DECREF(fast);
1355 Py_DECREF(item);
1356 }
1357
1358 i = 0;
1359 goto Return;
1360Fail:
1361 Py_XDECREF(item);
1362 Py_XDECREF(fast);
1363 i = -1;
1364Return:
1365 Py_DECREF(it);
Neal Norwitzd3881b02006-05-30 04:25:05 +00001366 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001367}
1368
Tim Peters6d6c1a32001-08-02 04:15:00 +00001369int
1370PyDict_Update(PyObject *a, PyObject *b)
1371{
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001372 return PyDict_Merge(a, b, 1);
1373}
1374
1375int
1376PyDict_Merge(PyObject *a, PyObject *b, int override)
1377{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001378 register PyDictObject *mp, *other;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001379 register Py_ssize_t i;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001380 PyDictEntry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001381
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001382 /* We accept for the argument either a concrete dictionary object,
1383 * or an abstract "mapping" object. For the former, we can do
1384 * things quite efficiently. For the latter, we only require that
1385 * PyMapping_Keys() and PyObject_GetItem() be supported.
1386 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001387 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1388 PyErr_BadInternalCall();
1389 return -1;
1390 }
Brett Cannon77ae87c2007-10-10 00:07:50 +00001391 mp = (PyDictObject*)a;
Raymond Hettinger0922d712007-02-07 20:08:22 +00001392 if (PyDict_CheckExact(b)) {
Brett Cannon77ae87c2007-10-10 00:07:50 +00001393 other = (PyDictObject*)b;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001394 if (other == mp || other->ma_used == 0)
1395 /* a.update(a) or a.update({}); nothing to do */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001396 return 0;
Raymond Hettinger186e7392005-05-14 18:08:25 +00001397 if (mp->ma_used == 0)
1398 /* Since the target dict is empty, PyDict_GetItem()
1399 * always returns NULL. Setting override to 1
1400 * skips the unnecessary test.
1401 */
1402 override = 1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001403 /* Do one big resize at the start, rather than
1404 * incrementally resizing as we insert new items. Expect
1405 * that there will be no (or few) overlapping keys.
1406 */
1407 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
Raymond Hettingerc8d22902003-05-07 00:49:40 +00001408 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001409 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001410 }
1411 for (i = 0; i <= other->ma_mask; i++) {
1412 entry = &other->ma_table[i];
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001413 if (entry->me_value != NULL &&
1414 (override ||
1415 PyDict_GetItem(a, entry->me_key) == NULL)) {
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001416 Py_INCREF(entry->me_key);
1417 Py_INCREF(entry->me_value);
Armin Rigo35f6d362006-06-01 13:19:12 +00001418 if (insertdict(mp, entry->me_key,
1419 (long)entry->me_hash,
1420 entry->me_value) != 0)
1421 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001422 }
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001423 }
1424 }
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001425 else {
1426 /* Do it the generic, slower way */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001427 PyObject *keys = PyMapping_Keys(b);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001428 PyObject *iter;
1429 PyObject *key, *value;
1430 int status;
1431
1432 if (keys == NULL)
1433 /* Docstring says this is equivalent to E.keys() so
1434 * if E doesn't have a .keys() method we want
1435 * AttributeError to percolate up. Might as well
1436 * do the same for any other error.
1437 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001438 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001439
1440 iter = PyObject_GetIter(keys);
1441 Py_DECREF(keys);
1442 if (iter == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001443 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001444
1445 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001446 if (!override && PyDict_GetItem(a, key) != NULL) {
1447 Py_DECREF(key);
1448 continue;
1449 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001450 value = PyObject_GetItem(b, key);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001451 if (value == NULL) {
1452 Py_DECREF(iter);
1453 Py_DECREF(key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001454 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001455 }
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001456 status = PyDict_SetItem(a, key, value);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001457 Py_DECREF(key);
1458 Py_DECREF(value);
1459 if (status < 0) {
1460 Py_DECREF(iter);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001461 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001462 }
1463 }
1464 Py_DECREF(iter);
1465 if (PyErr_Occurred())
1466 /* Iterator completed, via error */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001467 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001468 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001469 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001470}
1471
1472static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001473dict_copy(register PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001474{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001475 return PyDict_Copy((PyObject*)mp);
1476}
1477
1478PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001479PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001480{
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001481 PyObject *copy;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001482
1483 if (o == NULL || !PyDict_Check(o)) {
1484 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001485 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001486 }
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001487 copy = PyDict_New();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001488 if (copy == NULL)
1489 return NULL;
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001490 if (PyDict_Merge(copy, o, 1) == 0)
1491 return copy;
1492 Py_DECREF(copy);
Raymond Hettinger1356f782005-04-15 15:58:42 +00001493 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001494}
1495
Martin v. Löwis18e16552006-02-15 17:27:45 +00001496Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00001497PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001498{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001499 if (mp == NULL || !PyDict_Check(mp)) {
1500 PyErr_BadInternalCall();
Jeremy Hylton7083bb72004-02-17 20:10:11 +00001501 return -1;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001502 }
Brett Cannon77ae87c2007-10-10 00:07:50 +00001503 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001504}
1505
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001506PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001507PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001508{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001509 if (mp == NULL || !PyDict_Check(mp)) {
1510 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001511 return NULL;
1512 }
Brett Cannon77ae87c2007-10-10 00:07:50 +00001513 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001514}
1515
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001516PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001517PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001518{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001519 if (mp == NULL || !PyDict_Check(mp)) {
1520 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001521 return NULL;
1522 }
Brett Cannon77ae87c2007-10-10 00:07:50 +00001523 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001524}
1525
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001526PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001527PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001528{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001529 if (mp == NULL || !PyDict_Check(mp)) {
1530 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001531 return NULL;
1532 }
Brett Cannon77ae87c2007-10-10 00:07:50 +00001533 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001534}
1535
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001536/* Subroutine which returns the smallest key in a for which b's value
1537 is different or absent. The value is returned too, through the
Tim Peters95bf9392001-05-10 08:32:44 +00001538 pval argument. Both are NULL if no key in a is found for which b's status
1539 differs. The refcounts on (and only on) non-NULL *pval and function return
1540 values must be decremented by the caller (characterize() increments them
1541 to ensure that mutating comparison and PyDict_GetItem calls can't delete
1542 them before the caller is done looking at them). */
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001543
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001544static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001545characterize(PyDictObject *a, PyDictObject *b, PyObject **pval)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001546{
Tim Peters95bf9392001-05-10 08:32:44 +00001547 PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */
1548 PyObject *aval = NULL; /* a[akey] */
Tim Peters9b10f7e2006-05-30 04:16:25 +00001549 Py_ssize_t i;
1550 int cmp;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001551
Tim Petersafb6ae82001-06-04 21:00:21 +00001552 for (i = 0; i <= a->ma_mask; i++) {
Tim Peters95bf9392001-05-10 08:32:44 +00001553 PyObject *thiskey, *thisaval, *thisbval;
1554 if (a->ma_table[i].me_value == NULL)
1555 continue;
1556 thiskey = a->ma_table[i].me_key;
1557 Py_INCREF(thiskey); /* keep alive across compares */
1558 if (akey != NULL) {
1559 cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT);
1560 if (cmp < 0) {
1561 Py_DECREF(thiskey);
1562 goto Fail;
1563 }
1564 if (cmp > 0 ||
Tim Petersafb6ae82001-06-04 21:00:21 +00001565 i > a->ma_mask ||
Tim Peters95bf9392001-05-10 08:32:44 +00001566 a->ma_table[i].me_value == NULL)
1567 {
1568 /* Not the *smallest* a key; or maybe it is
1569 * but the compare shrunk the dict so we can't
1570 * find its associated value anymore; or
1571 * maybe it is but the compare deleted the
1572 * a[thiskey] entry.
Neal Norwitzd3881b02006-05-30 04:25:05 +00001573 */
Tim Peters95bf9392001-05-10 08:32:44 +00001574 Py_DECREF(thiskey);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001575 continue;
Guido van Rossumb9324202001-01-18 00:39:02 +00001576 }
Tim Peters95bf9392001-05-10 08:32:44 +00001577 }
1578
1579 /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */
1580 thisaval = a->ma_table[i].me_value;
1581 assert(thisaval);
1582 Py_INCREF(thisaval); /* keep alive */
1583 thisbval = PyDict_GetItem((PyObject *)b, thiskey);
1584 if (thisbval == NULL)
1585 cmp = 0;
1586 else {
1587 /* both dicts have thiskey: same values? */
1588 cmp = PyObject_RichCompareBool(
1589 thisaval, thisbval, Py_EQ);
1590 if (cmp < 0) {
1591 Py_DECREF(thiskey);
1592 Py_DECREF(thisaval);
1593 goto Fail;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001594 }
1595 }
Tim Peters95bf9392001-05-10 08:32:44 +00001596 if (cmp == 0) {
1597 /* New winner. */
1598 Py_XDECREF(akey);
1599 Py_XDECREF(aval);
1600 akey = thiskey;
1601 aval = thisaval;
1602 }
1603 else {
1604 Py_DECREF(thiskey);
1605 Py_DECREF(thisaval);
1606 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001607 }
Tim Peters95bf9392001-05-10 08:32:44 +00001608 *pval = aval;
1609 return akey;
1610
1611Fail:
1612 Py_XDECREF(akey);
1613 Py_XDECREF(aval);
1614 *pval = NULL;
1615 return NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001616}
1617
1618static int
Brett Cannon77ae87c2007-10-10 00:07:50 +00001619dict_compare(PyDictObject *a, PyDictObject *b)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001620{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001621 PyObject *adiff, *bdiff, *aval, *bval;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001622 int res;
1623
1624 /* Compare lengths first */
1625 if (a->ma_used < b->ma_used)
1626 return -1; /* a is shorter */
1627 else if (a->ma_used > b->ma_used)
1628 return 1; /* b is shorter */
Tim Peters95bf9392001-05-10 08:32:44 +00001629
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001630 /* Same length -- check all keys */
Tim Peters95bf9392001-05-10 08:32:44 +00001631 bdiff = bval = NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001632 adiff = characterize(a, b, &aval);
Tim Peters95bf9392001-05-10 08:32:44 +00001633 if (adiff == NULL) {
1634 assert(!aval);
Tim Peters3918fb22001-05-10 18:58:31 +00001635 /* Either an error, or a is a subset with the same length so
Tim Peters95bf9392001-05-10 08:32:44 +00001636 * must be equal.
1637 */
1638 res = PyErr_Occurred() ? -1 : 0;
1639 goto Finished;
1640 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001641 bdiff = characterize(b, a, &bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001642 if (bdiff == NULL && PyErr_Occurred()) {
1643 assert(!bval);
1644 res = -1;
1645 goto Finished;
1646 }
1647 res = 0;
1648 if (bdiff) {
1649 /* bdiff == NULL "should be" impossible now, but perhaps
1650 * the last comparison done by the characterize() on a had
1651 * the side effect of making the dicts equal!
1652 */
1653 res = PyObject_Compare(adiff, bdiff);
1654 }
1655 if (res == 0 && bval != NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001656 res = PyObject_Compare(aval, bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001657
1658Finished:
1659 Py_XDECREF(adiff);
1660 Py_XDECREF(bdiff);
1661 Py_XDECREF(aval);
1662 Py_XDECREF(bval);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001663 return res;
1664}
1665
Tim Peterse63415e2001-05-08 04:38:29 +00001666/* Return 1 if dicts equal, 0 if not, -1 if error.
1667 * Gets out as soon as any difference is detected.
1668 * Uses only Py_EQ comparison.
1669 */
1670static int
Brett Cannon77ae87c2007-10-10 00:07:50 +00001671dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00001672{
Tim Peters9b10f7e2006-05-30 04:16:25 +00001673 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00001674
1675 if (a->ma_used != b->ma_used)
1676 /* can't be equal if # of entries differ */
1677 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001678
Tim Peterse63415e2001-05-08 04:38:29 +00001679 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Tim Petersafb6ae82001-06-04 21:00:21 +00001680 for (i = 0; i <= a->ma_mask; i++) {
Tim Peterse63415e2001-05-08 04:38:29 +00001681 PyObject *aval = a->ma_table[i].me_value;
1682 if (aval != NULL) {
1683 int cmp;
1684 PyObject *bval;
1685 PyObject *key = a->ma_table[i].me_key;
1686 /* temporarily bump aval's refcount to ensure it stays
1687 alive until we're done with it */
1688 Py_INCREF(aval);
Neal Norwitza22975f2006-09-05 02:24:03 +00001689 /* ditto for key */
1690 Py_INCREF(key);
Tim Peterse63415e2001-05-08 04:38:29 +00001691 bval = PyDict_GetItem((PyObject *)b, key);
Neal Norwitza22975f2006-09-05 02:24:03 +00001692 Py_DECREF(key);
Tim Peterse63415e2001-05-08 04:38:29 +00001693 if (bval == NULL) {
1694 Py_DECREF(aval);
1695 return 0;
1696 }
1697 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1698 Py_DECREF(aval);
1699 if (cmp <= 0) /* error or not equal */
1700 return cmp;
1701 }
1702 }
1703 return 1;
1704 }
1705
1706static PyObject *
1707dict_richcompare(PyObject *v, PyObject *w, int op)
1708{
1709 int cmp;
1710 PyObject *res;
1711
1712 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1713 res = Py_NotImplemented;
1714 }
1715 else if (op == Py_EQ || op == Py_NE) {
Brett Cannon77ae87c2007-10-10 00:07:50 +00001716 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
Tim Peterse63415e2001-05-08 04:38:29 +00001717 if (cmp < 0)
1718 return NULL;
1719 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1720 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001721 else
1722 res = Py_NotImplemented;
Tim Peterse63415e2001-05-08 04:38:29 +00001723 Py_INCREF(res);
1724 return res;
1725 }
1726
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001727static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001728dict_contains(register PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001729{
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001730 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001731 PyDictEntry *ep;
Tim Petersd770ebd2006-06-01 15:50:44 +00001732
Tim Peters0ab085c2001-09-14 00:25:33 +00001733 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001734 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001735 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001736 if (hash == -1)
1737 return NULL;
1738 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001739 ep = (mp->ma_lookup)(mp, key, hash);
1740 if (ep == NULL)
1741 return NULL;
Tim Petersd770ebd2006-06-01 15:50:44 +00001742 return PyBool_FromLong(ep->me_value != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001743}
1744
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001745static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001746dict_has_key(register PyDictObject *mp, PyObject *key)
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +00001747{
1748 if (Py_Py3kWarningFlag &&
1749 PyErr_Warn(PyExc_DeprecationWarning,
1750 "dict.has_key() not supported in 3.x") < 0)
1751 return NULL;
1752 return dict_contains(mp, key);
1753}
1754
1755static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001756dict_get(register PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001757{
1758 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001759 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001760 PyObject *val = NULL;
1761 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001762 PyDictEntry *ep;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001763
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001764 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001765 return NULL;
1766
Tim Peters0ab085c2001-09-14 00:25:33 +00001767 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001768 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Barry Warsawc38c5da1997-10-06 17:49:20 +00001769 hash = PyObject_Hash(key);
1770 if (hash == -1)
1771 return NULL;
1772 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001773 ep = (mp->ma_lookup)(mp, key, hash);
1774 if (ep == NULL)
1775 return NULL;
1776 val = ep->me_value;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001777 if (val == NULL)
1778 val = failobj;
1779 Py_INCREF(val);
1780 return val;
1781}
1782
1783
1784static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001785dict_setdefault(register PyDictObject *mp, PyObject *args)
Guido van Rossum164452c2000-08-08 16:12:54 +00001786{
1787 PyObject *key;
1788 PyObject *failobj = Py_None;
1789 PyObject *val = NULL;
1790 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001791 PyDictEntry *ep;
Guido van Rossum164452c2000-08-08 16:12:54 +00001792
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001793 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
Guido van Rossum164452c2000-08-08 16:12:54 +00001794 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001795
Tim Peters0ab085c2001-09-14 00:25:33 +00001796 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001797 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossum164452c2000-08-08 16:12:54 +00001798 hash = PyObject_Hash(key);
1799 if (hash == -1)
1800 return NULL;
1801 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001802 ep = (mp->ma_lookup)(mp, key, hash);
1803 if (ep == NULL)
1804 return NULL;
1805 val = ep->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001806 if (val == NULL) {
1807 val = failobj;
1808 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1809 val = NULL;
1810 }
1811 Py_XINCREF(val);
1812 return val;
1813}
1814
1815
1816static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001817dict_clear(register PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001818{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001819 PyDict_Clear((PyObject *)mp);
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001820 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001821}
1822
Guido van Rossumba6ab842000-12-12 22:02:18 +00001823static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001824dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00001825{
1826 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001827 PyDictEntry *ep;
Guido van Rossume027d982002-04-12 15:11:59 +00001828 PyObject *old_value, *old_key;
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001829 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001830
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001831 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1832 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001833 if (mp->ma_used == 0) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001834 if (deflt) {
1835 Py_INCREF(deflt);
1836 return deflt;
1837 }
Guido van Rossume027d982002-04-12 15:11:59 +00001838 PyErr_SetString(PyExc_KeyError,
1839 "pop(): dictionary is empty");
1840 return NULL;
1841 }
1842 if (!PyString_CheckExact(key) ||
1843 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1844 hash = PyObject_Hash(key);
1845 if (hash == -1)
1846 return NULL;
1847 }
1848 ep = (mp->ma_lookup)(mp, key, hash);
Armin Rigo35f6d362006-06-01 13:19:12 +00001849 if (ep == NULL)
1850 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001851 if (ep->me_value == NULL) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001852 if (deflt) {
1853 Py_INCREF(deflt);
1854 return deflt;
1855 }
Georg Brandlb9f4ad32006-10-29 18:31:42 +00001856 set_key_error(key);
Guido van Rossume027d982002-04-12 15:11:59 +00001857 return NULL;
1858 }
1859 old_key = ep->me_key;
1860 Py_INCREF(dummy);
1861 ep->me_key = dummy;
1862 old_value = ep->me_value;
1863 ep->me_value = NULL;
1864 mp->ma_used--;
1865 Py_DECREF(old_key);
1866 return old_value;
1867}
1868
1869static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001870dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001871{
Tim Peters9b10f7e2006-05-30 04:16:25 +00001872 Py_ssize_t i = 0;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001873 PyDictEntry *ep;
Guido van Rossumba6ab842000-12-12 22:02:18 +00001874 PyObject *res;
1875
Tim Petersf4b33f62001-06-02 05:42:29 +00001876 /* Allocate the result tuple before checking the size. Believe it
1877 * or not, this allocation could trigger a garbage collection which
1878 * could empty the dict, so if we checked the size first and that
1879 * happened, the result would be an infinite loop (searching for an
1880 * entry that no longer exists). Note that the usual popitem()
1881 * idiom is "while d: k, v = d.popitem()". so needing to throw the
Tim Peters9b10f7e2006-05-30 04:16:25 +00001882 * tuple away if the dict *is* empty isn't a significant
Tim Petersf4b33f62001-06-02 05:42:29 +00001883 * inefficiency -- possible, but unlikely in practice.
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001884 */
1885 res = PyTuple_New(2);
1886 if (res == NULL)
1887 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001888 if (mp->ma_used == 0) {
1889 Py_DECREF(res);
1890 PyErr_SetString(PyExc_KeyError,
1891 "popitem(): dictionary is empty");
1892 return NULL;
1893 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001894 /* Set ep to "the first" dict entry with a value. We abuse the hash
1895 * field of slot 0 to hold a search finger:
1896 * If slot 0 has a value, use slot 0.
1897 * Else slot 0 is being used to hold a search finger,
Neal Norwitzd3881b02006-05-30 04:25:05 +00001898 * and we use its hash value as the first index to look.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001899 */
1900 ep = &mp->ma_table[0];
1901 if (ep->me_value == NULL) {
Tim Peters9b10f7e2006-05-30 04:16:25 +00001902 i = ep->me_hash;
Tim Petersdea48ec2001-05-22 20:40:22 +00001903 /* The hash field may be a real hash value, or it may be a
1904 * legit search finger, or it may be a once-legit search
1905 * finger that's out of bounds now because it wrapped around
1906 * or the table shrunk -- simply make sure it's in bounds now.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001907 */
Tim Petersafb6ae82001-06-04 21:00:21 +00001908 if (i > mp->ma_mask || i < 1)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001909 i = 1; /* skip slot 0 */
1910 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1911 i++;
Tim Petersafb6ae82001-06-04 21:00:21 +00001912 if (i > mp->ma_mask)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001913 i = 1;
1914 }
1915 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001916 PyTuple_SET_ITEM(res, 0, ep->me_key);
1917 PyTuple_SET_ITEM(res, 1, ep->me_value);
1918 Py_INCREF(dummy);
1919 ep->me_key = dummy;
1920 ep->me_value = NULL;
1921 mp->ma_used--;
1922 assert(mp->ma_table[0].me_value == NULL);
1923 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001924 return res;
1925}
1926
Jeremy Hylton8caad492000-06-23 14:18:11 +00001927static int
1928dict_traverse(PyObject *op, visitproc visit, void *arg)
1929{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001930 Py_ssize_t i = 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001931 PyObject *pk;
1932 PyObject *pv;
1933
1934 while (PyDict_Next(op, &i, &pk, &pv)) {
Thomas Woutersc6e55062006-04-15 21:47:09 +00001935 Py_VISIT(pk);
1936 Py_VISIT(pv);
Jeremy Hylton8caad492000-06-23 14:18:11 +00001937 }
1938 return 0;
1939}
1940
1941static int
1942dict_tp_clear(PyObject *op)
1943{
1944 PyDict_Clear(op);
1945 return 0;
1946}
1947
Tim Petersf7f88b12000-12-13 23:18:45 +00001948
Raymond Hettinger019a1482004-03-18 02:41:19 +00001949extern PyTypeObject PyDictIterKey_Type; /* Forward */
1950extern PyTypeObject PyDictIterValue_Type; /* Forward */
1951extern PyTypeObject PyDictIterItem_Type; /* Forward */
Brett Cannon77ae87c2007-10-10 00:07:50 +00001952static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001953
1954static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001955dict_iterkeys(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001956{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001957 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001958}
1959
1960static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001961dict_itervalues(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001962{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001963 return dictiter_new(dict, &PyDictIterValue_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001964}
1965
1966static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001967dict_iteritems(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001968{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001969 return dictiter_new(dict, &PyDictIterItem_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001970}
1971
1972
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001973PyDoc_STRVAR(has_key__doc__,
Raymond Hettinger574aa322003-09-01 22:12:08 +00001974"D.has_key(k) -> True if D has a key k, else False");
Tim Petersf7f88b12000-12-13 23:18:45 +00001975
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001976PyDoc_STRVAR(contains__doc__,
1977"D.__contains__(k) -> True if D has a key k, else False");
1978
1979PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
1980
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001981PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001982"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001983
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001984PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001985"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 +00001986
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001987PyDoc_STRVAR(pop__doc__,
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001988"D.pop(k[,d]) -> v, remove specified key and return the corresponding value\n\
1989If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00001990
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001991PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00001992"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000019932-tuple; but raise KeyError if D is empty");
Tim Petersf7f88b12000-12-13 23:18:45 +00001994
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001995PyDoc_STRVAR(keys__doc__,
1996"D.keys() -> list of D's keys");
Tim Petersf7f88b12000-12-13 23:18:45 +00001997
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001998PyDoc_STRVAR(items__doc__,
1999"D.items() -> list of D's (key, value) pairs, as 2-tuples");
Tim Petersf7f88b12000-12-13 23:18:45 +00002000
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002001PyDoc_STRVAR(values__doc__,
2002"D.values() -> list of D's values");
Tim Petersf7f88b12000-12-13 23:18:45 +00002003
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002004PyDoc_STRVAR(update__doc__,
Walter Dörwaldd70ad8a2004-05-28 20:59:21 +00002005"D.update(E, **F) -> None. Update D from E and F: for k in E: D[k] = E[k]\n\
2006(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 +00002007
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002008PyDoc_STRVAR(fromkeys__doc__,
2009"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
2010v defaults to None.");
2011
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002012PyDoc_STRVAR(clear__doc__,
2013"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002014
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002015PyDoc_STRVAR(copy__doc__,
2016"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00002017
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002018PyDoc_STRVAR(iterkeys__doc__,
2019"D.iterkeys() -> an iterator over the keys of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00002020
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002021PyDoc_STRVAR(itervalues__doc__,
2022"D.itervalues() -> an iterator over the values of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00002023
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002024PyDoc_STRVAR(iteritems__doc__,
2025"D.iteritems() -> an iterator over the (key, value) items of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00002026
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002027static PyMethodDef mapp_methods[] = {
Neal Norwitzc7926292007-05-23 06:57:35 +00002028 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002029 contains__doc__},
Raymond Hettinger0c669672003-12-13 13:31:55 +00002030 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002031 getitem__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002032 {"has_key", (PyCFunction)dict_has_key, METH_O,
Tim Petersf7f88b12000-12-13 23:18:45 +00002033 has_key__doc__},
2034 {"get", (PyCFunction)dict_get, METH_VARARGS,
2035 get__doc__},
2036 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
2037 setdefault_doc__},
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002038 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
Just van Rossuma797d812002-11-23 09:45:04 +00002039 pop__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002040 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002041 popitem__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002042 {"keys", (PyCFunction)dict_keys, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002043 keys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002044 {"items", (PyCFunction)dict_items, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002045 items__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002046 {"values", (PyCFunction)dict_values, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002047 values__doc__},
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002048 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002049 update__doc__},
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002050 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
2051 fromkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002052 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002053 clear__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002054 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002055 copy__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002056 {"iterkeys", (PyCFunction)dict_iterkeys, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00002057 iterkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002058 {"itervalues", (PyCFunction)dict_itervalues, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00002059 itervalues__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002060 {"iteritems", (PyCFunction)dict_iteritems, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00002061 iteritems__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00002062 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002063};
2064
Tim Petersd770ebd2006-06-01 15:50:44 +00002065/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002066int
2067PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002068{
2069 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +00002070 PyDictObject *mp = (PyDictObject *)op;
2071 PyDictEntry *ep;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002072
Tim Peters0ab085c2001-09-14 00:25:33 +00002073 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00002074 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002075 hash = PyObject_Hash(key);
2076 if (hash == -1)
2077 return -1;
2078 }
Armin Rigo35f6d362006-06-01 13:19:12 +00002079 ep = (mp->ma_lookup)(mp, key, hash);
Tim Petersd770ebd2006-06-01 15:50:44 +00002080 return ep == NULL ? -1 : (ep->me_value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002081}
2082
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +00002083/* Internal version of PyDict_Contains used when the hash value is already known */
2084int
2085_PyDict_Contains(PyObject *op, PyObject *key, long hash)
2086{
Brett Cannon77ae87c2007-10-10 00:07:50 +00002087 PyDictObject *mp = (PyDictObject *)op;
2088 PyDictEntry *ep;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +00002089
2090 ep = (mp->ma_lookup)(mp, key, hash);
2091 return ep == NULL ? -1 : (ep->me_value != NULL);
2092}
2093
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002094/* Hack to implement "key in dict" */
2095static PySequenceMethods dict_as_sequence = {
Georg Brandl347b3002006-03-30 11:57:00 +00002096 0, /* sq_length */
2097 0, /* sq_concat */
2098 0, /* sq_repeat */
2099 0, /* sq_item */
2100 0, /* sq_slice */
2101 0, /* sq_ass_item */
2102 0, /* sq_ass_slice */
2103 PyDict_Contains, /* sq_contains */
2104 0, /* sq_inplace_concat */
2105 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002106};
2107
Guido van Rossum09e563a2001-05-01 12:10:21 +00002108static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002109dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2110{
2111 PyObject *self;
2112
2113 assert(type != NULL && type->tp_alloc != NULL);
2114 self = type->tp_alloc(type, 0);
2115 if (self != NULL) {
2116 PyDictObject *d = (PyDictObject *)self;
2117 /* It's guaranteed that tp->alloc zeroed out the struct. */
2118 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
2119 INIT_NONZERO_DICT_SLOTS(d);
2120 d->ma_lookup = lookdict_string;
2121#ifdef SHOW_CONVERSION_COUNTS
2122 ++created;
2123#endif
2124 }
2125 return self;
2126}
2127
Tim Peters25786c02001-09-02 08:22:48 +00002128static int
2129dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2130{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002131 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002132}
2133
Tim Peters6d6c1a32001-08-02 04:15:00 +00002134static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002135dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002136{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002137 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002138}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002139
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002140PyDoc_STRVAR(dictionary_doc,
Tim Petersa427a2b2001-10-29 22:25:45 +00002141"dict() -> new empty dictionary.\n"
2142"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Tim Peters1fc240e2001-10-26 05:06:50 +00002143" (key, value) pairs.\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002144"dict(seq) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002145" d = {}\n"
2146" for k, v in seq:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002147" d[k] = v\n"
2148"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2149" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002150
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002151PyTypeObject PyDict_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002152 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Tim Petersa427a2b2001-10-29 22:25:45 +00002153 "dict",
Brett Cannon77ae87c2007-10-10 00:07:50 +00002154 sizeof(PyDictObject),
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002155 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00002156 (destructor)dict_dealloc, /* tp_dealloc */
2157 (printfunc)dict_print, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002158 0, /* tp_getattr */
Guido van Rossumb9324202001-01-18 00:39:02 +00002159 0, /* tp_setattr */
Tim Peters4fa58bf2001-05-10 21:45:19 +00002160 (cmpfunc)dict_compare, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00002161 (reprfunc)dict_repr, /* tp_repr */
2162 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002163 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00002164 &dict_as_mapping, /* tp_as_mapping */
Guido van Rossum64c06e32007-11-22 00:55:51 +00002165 0, /* tp_hash */
Guido van Rossumb9324202001-01-18 00:39:02 +00002166 0, /* tp_call */
2167 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002168 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumb9324202001-01-18 00:39:02 +00002169 0, /* tp_setattro */
2170 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002171 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Neal Norwitzee3a1b52007-02-25 19:44:48 +00002172 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
Tim Peters25786c02001-09-02 08:22:48 +00002173 dictionary_doc, /* tp_doc */
Georg Brandl347b3002006-03-30 11:57:00 +00002174 dict_traverse, /* tp_traverse */
2175 dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00002176 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002177 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00002178 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002179 0, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002180 mapp_methods, /* tp_methods */
2181 0, /* tp_members */
2182 0, /* tp_getset */
2183 0, /* tp_base */
2184 0, /* tp_dict */
2185 0, /* tp_descr_get */
2186 0, /* tp_descr_set */
2187 0, /* tp_dictoffset */
Georg Brandl347b3002006-03-30 11:57:00 +00002188 dict_init, /* tp_init */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002189 PyType_GenericAlloc, /* tp_alloc */
2190 dict_new, /* tp_new */
Neil Schemenauer6189b892002-04-12 02:43:00 +00002191 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002192};
2193
Guido van Rossum3cca2451997-05-16 14:23:33 +00002194/* For backward compatibility with old dictionary interface */
2195
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002196PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002197PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002198{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002199 PyObject *kv, *rv;
2200 kv = PyString_FromString(key);
2201 if (kv == NULL)
2202 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002203 rv = PyDict_GetItem(v, kv);
2204 Py_DECREF(kv);
2205 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002206}
2207
2208int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002209PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002210{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002211 PyObject *kv;
2212 int err;
2213 kv = PyString_FromString(key);
2214 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002215 return -1;
Guido van Rossum4f3bf1e1997-09-29 23:31:11 +00002216 PyString_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00002217 err = PyDict_SetItem(v, kv, item);
2218 Py_DECREF(kv);
2219 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002220}
2221
2222int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002223PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002224{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002225 PyObject *kv;
2226 int err;
2227 kv = PyString_FromString(key);
2228 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002229 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002230 err = PyDict_DelItem(v, kv);
2231 Py_DECREF(kv);
2232 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002233}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002234
Raymond Hettinger019a1482004-03-18 02:41:19 +00002235/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002236
2237typedef struct {
2238 PyObject_HEAD
Brett Cannon77ae87c2007-10-10 00:07:50 +00002239 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
Tim Peters9b10f7e2006-05-30 04:16:25 +00002240 Py_ssize_t di_used;
2241 Py_ssize_t di_pos;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002242 PyObject* di_result; /* reusable result tuple for iteritems */
Tim Peters9b10f7e2006-05-30 04:16:25 +00002243 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002244} dictiterobject;
2245
2246static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002247dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002248{
2249 dictiterobject *di;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002250 di = PyObject_New(dictiterobject, itertype);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002251 if (di == NULL)
2252 return NULL;
2253 Py_INCREF(dict);
2254 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00002255 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002256 di->di_pos = 0;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002257 di->len = dict->ma_used;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002258 if (itertype == &PyDictIterItem_Type) {
2259 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2260 if (di->di_result == NULL) {
2261 Py_DECREF(di);
2262 return NULL;
2263 }
2264 }
2265 else
2266 di->di_result = NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002267 return (PyObject *)di;
2268}
2269
2270static void
2271dictiter_dealloc(dictiterobject *di)
2272{
Guido van Rossum2147df72002-07-16 20:30:22 +00002273 Py_XDECREF(di->di_dict);
Raymond Hettinger019a1482004-03-18 02:41:19 +00002274 Py_XDECREF(di->di_result);
Neil Schemenauer6189b892002-04-12 02:43:00 +00002275 PyObject_Del(di);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002276}
2277
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002278static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002279dictiter_len(dictiterobject *di)
2280{
Tim Peters9b10f7e2006-05-30 04:16:25 +00002281 Py_ssize_t len = 0;
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00002282 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002283 len = di->len;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002284 return PyInt_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002285}
2286
Armin Rigof5b3e362006-02-11 21:32:43 +00002287PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002288
2289static PyMethodDef dictiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +00002290 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002291 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002292};
2293
Raymond Hettinger019a1482004-03-18 02:41:19 +00002294static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002295{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002296 PyObject *key;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002297 register Py_ssize_t i, mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +00002298 register PyDictEntry *ep;
2299 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002300
Raymond Hettinger019a1482004-03-18 02:41:19 +00002301 if (d == NULL)
Guido van Rossum2147df72002-07-16 20:30:22 +00002302 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002303 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002304
Raymond Hettinger019a1482004-03-18 02:41:19 +00002305 if (di->di_used != d->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00002306 PyErr_SetString(PyExc_RuntimeError,
2307 "dictionary changed size during iteration");
Guido van Rossum2147df72002-07-16 20:30:22 +00002308 di->di_used = -1; /* Make this state sticky */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002309 return NULL;
2310 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002311
Raymond Hettinger019a1482004-03-18 02:41:19 +00002312 i = di->di_pos;
2313 if (i < 0)
2314 goto fail;
2315 ep = d->ma_table;
2316 mask = d->ma_mask;
2317 while (i <= mask && ep[i].me_value == NULL)
2318 i++;
2319 di->di_pos = i+1;
2320 if (i > mask)
2321 goto fail;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002322 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002323 key = ep[i].me_key;
2324 Py_INCREF(key);
2325 return key;
2326
2327fail:
2328 Py_DECREF(d);
Guido van Rossum2147df72002-07-16 20:30:22 +00002329 di->di_dict = NULL;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002330 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002331}
2332
Raymond Hettinger019a1482004-03-18 02:41:19 +00002333PyTypeObject PyDictIterKey_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002334 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002335 "dictionary-keyiterator", /* tp_name */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002336 sizeof(dictiterobject), /* tp_basicsize */
2337 0, /* tp_itemsize */
2338 /* methods */
2339 (destructor)dictiter_dealloc, /* tp_dealloc */
2340 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002341 0, /* tp_getattr */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002342 0, /* tp_setattr */
2343 0, /* tp_compare */
2344 0, /* tp_repr */
2345 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002346 0, /* tp_as_sequence */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002347 0, /* tp_as_mapping */
2348 0, /* tp_hash */
2349 0, /* tp_call */
2350 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002351 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002352 0, /* tp_setattro */
2353 0, /* tp_as_buffer */
2354 Py_TPFLAGS_DEFAULT, /* tp_flags */
2355 0, /* tp_doc */
2356 0, /* tp_traverse */
2357 0, /* tp_clear */
2358 0, /* tp_richcompare */
2359 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002360 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002361 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002362 dictiter_methods, /* tp_methods */
2363 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002364};
2365
2366static PyObject *dictiter_iternextvalue(dictiterobject *di)
2367{
2368 PyObject *value;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002369 register Py_ssize_t i, mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +00002370 register PyDictEntry *ep;
2371 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002372
2373 if (d == NULL)
2374 return NULL;
2375 assert (PyDict_Check(d));
2376
2377 if (di->di_used != d->ma_used) {
2378 PyErr_SetString(PyExc_RuntimeError,
2379 "dictionary changed size during iteration");
2380 di->di_used = -1; /* Make this state sticky */
2381 return NULL;
2382 }
2383
2384 i = di->di_pos;
Guido van Rossum09240f62004-03-20 19:11:58 +00002385 mask = d->ma_mask;
2386 if (i < 0 || i > mask)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002387 goto fail;
2388 ep = d->ma_table;
Guido van Rossum09240f62004-03-20 19:11:58 +00002389 while ((value=ep[i].me_value) == NULL) {
Raymond Hettinger019a1482004-03-18 02:41:19 +00002390 i++;
Guido van Rossum09240f62004-03-20 19:11:58 +00002391 if (i > mask)
2392 goto fail;
2393 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002394 di->di_pos = i+1;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002395 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002396 Py_INCREF(value);
2397 return value;
2398
2399fail:
2400 Py_DECREF(d);
2401 di->di_dict = NULL;
2402 return NULL;
2403}
2404
2405PyTypeObject PyDictIterValue_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002406 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002407 "dictionary-valueiterator", /* tp_name */
2408 sizeof(dictiterobject), /* tp_basicsize */
2409 0, /* tp_itemsize */
2410 /* methods */
2411 (destructor)dictiter_dealloc, /* tp_dealloc */
2412 0, /* tp_print */
2413 0, /* tp_getattr */
2414 0, /* tp_setattr */
2415 0, /* tp_compare */
2416 0, /* tp_repr */
2417 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002418 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002419 0, /* tp_as_mapping */
2420 0, /* tp_hash */
2421 0, /* tp_call */
2422 0, /* tp_str */
2423 PyObject_GenericGetAttr, /* tp_getattro */
2424 0, /* tp_setattro */
2425 0, /* tp_as_buffer */
2426 Py_TPFLAGS_DEFAULT, /* tp_flags */
2427 0, /* tp_doc */
2428 0, /* tp_traverse */
2429 0, /* tp_clear */
2430 0, /* tp_richcompare */
2431 0, /* tp_weaklistoffset */
2432 PyObject_SelfIter, /* tp_iter */
2433 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002434 dictiter_methods, /* tp_methods */
2435 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002436};
2437
2438static PyObject *dictiter_iternextitem(dictiterobject *di)
2439{
2440 PyObject *key, *value, *result = di->di_result;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002441 register Py_ssize_t i, mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +00002442 register PyDictEntry *ep;
2443 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002444
2445 if (d == NULL)
2446 return NULL;
2447 assert (PyDict_Check(d));
2448
2449 if (di->di_used != d->ma_used) {
2450 PyErr_SetString(PyExc_RuntimeError,
2451 "dictionary changed size during iteration");
2452 di->di_used = -1; /* Make this state sticky */
2453 return NULL;
2454 }
2455
2456 i = di->di_pos;
2457 if (i < 0)
2458 goto fail;
2459 ep = d->ma_table;
2460 mask = d->ma_mask;
2461 while (i <= mask && ep[i].me_value == NULL)
2462 i++;
2463 di->di_pos = i+1;
2464 if (i > mask)
2465 goto fail;
2466
2467 if (result->ob_refcnt == 1) {
2468 Py_INCREF(result);
2469 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2470 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2471 } else {
2472 result = PyTuple_New(2);
Tim Peters60b29962006-01-01 01:19:23 +00002473 if (result == NULL)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002474 return NULL;
2475 }
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002476 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002477 key = ep[i].me_key;
2478 value = ep[i].me_value;
2479 Py_INCREF(key);
2480 Py_INCREF(value);
2481 PyTuple_SET_ITEM(result, 0, key);
2482 PyTuple_SET_ITEM(result, 1, value);
2483 return result;
2484
2485fail:
2486 Py_DECREF(d);
2487 di->di_dict = NULL;
2488 return NULL;
2489}
2490
2491PyTypeObject PyDictIterItem_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002492 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002493 "dictionary-itemiterator", /* tp_name */
2494 sizeof(dictiterobject), /* tp_basicsize */
2495 0, /* tp_itemsize */
2496 /* methods */
2497 (destructor)dictiter_dealloc, /* tp_dealloc */
2498 0, /* tp_print */
2499 0, /* tp_getattr */
2500 0, /* tp_setattr */
2501 0, /* tp_compare */
2502 0, /* tp_repr */
2503 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002504 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002505 0, /* tp_as_mapping */
2506 0, /* tp_hash */
2507 0, /* tp_call */
2508 0, /* tp_str */
2509 PyObject_GenericGetAttr, /* tp_getattro */
2510 0, /* tp_setattro */
2511 0, /* tp_as_buffer */
2512 Py_TPFLAGS_DEFAULT, /* tp_flags */
2513 0, /* tp_doc */
2514 0, /* tp_traverse */
2515 0, /* tp_clear */
2516 0, /* tp_richcompare */
2517 0, /* tp_weaklistoffset */
2518 PyObject_SelfIter, /* tp_iter */
2519 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002520 dictiter_methods, /* tp_methods */
2521 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002522};