blob: 82d247f3f9515309962e5355536f4784bff94192 [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
Christian Heimesb4ee4a12008-02-07 17:15:30 +0000165/* Debug statistic to compare allocations with reuse through the free list */
166#undef SHOW_ALLOC_COUNT
167#ifdef SHOW_ALLOC_COUNT
168static size_t count_alloc = 0;
169static size_t count_reuse = 0;
170
171static void
172show_alloc(void)
173{
174 fprintf(stderr, "Dict allocations: %zd\n", count_alloc);
175 fprintf(stderr, "Dict reuse through freelist: %zd\n", count_reuse);
176 fprintf(stderr, "%.2f%% reuse rate\n\n",
177 (100.0*count_reuse/(count_alloc+count_reuse)));
178}
179#endif
180
Tim Peters6d6c1a32001-08-02 04:15:00 +0000181/* Initialization macros.
182 There are two ways to create a dict: PyDict_New() is the main C API
183 function, and the tp_new slot maps to dict_new(). In the latter case we
184 can save a little time over what PyDict_New does because it's guaranteed
185 that the PyDictObject struct is already zeroed out.
186 Everyone except dict_new() should use EMPTY_TO_MINSIZE (unless they have
187 an excellent reason not to).
188*/
189
190#define INIT_NONZERO_DICT_SLOTS(mp) do { \
Tim Petersdea48ec2001-05-22 20:40:22 +0000191 (mp)->ma_table = (mp)->ma_smalltable; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000192 (mp)->ma_mask = PyDict_MINSIZE - 1; \
193 } while(0)
194
195#define EMPTY_TO_MINSIZE(mp) do { \
196 memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000197 (mp)->ma_used = (mp)->ma_fill = 0; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000198 INIT_NONZERO_DICT_SLOTS(mp); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000199 } while(0)
200
Raymond Hettinger43442782004-03-17 21:55:03 +0000201/* Dictionary reuse scheme to save calls to malloc, free, and memset */
Christian Heimes5b970ad2008-02-06 13:33:44 +0000202#ifndef PyDict_MAXFREELIST
203#define PyDict_MAXFREELIST 80
204#endif
205static PyDictObject *free_list[PyDict_MAXFREELIST];
206static int numfree = 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000207
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000208PyObject *
Thomas Wouters78890102000-07-22 19:25:51 +0000209PyDict_New(void)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000210{
Brett Cannon77ae87c2007-10-10 00:07:50 +0000211 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000212 if (dummy == NULL) { /* Auto-initialize dummy */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000213 dummy = PyString_FromString("<dummy key>");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000214 if (dummy == NULL)
215 return NULL;
Fred Drake1bff34a2000-08-31 19:31:38 +0000216#ifdef SHOW_CONVERSION_COUNTS
217 Py_AtExit(show_counts);
218#endif
Christian Heimesb4ee4a12008-02-07 17:15:30 +0000219#ifdef SHOW_ALLOC_COUNT
220 Py_AtExit(show_alloc);
221#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000222 }
Christian Heimes5b970ad2008-02-06 13:33:44 +0000223 if (numfree) {
224 mp = free_list[--numfree];
Raymond Hettinger43442782004-03-17 21:55:03 +0000225 assert (mp != NULL);
Christian Heimese93237d2007-12-19 02:37:44 +0000226 assert (Py_TYPE(mp) == &PyDict_Type);
Raymond Hettinger43442782004-03-17 21:55:03 +0000227 _Py_NewReference((PyObject *)mp);
228 if (mp->ma_fill) {
229 EMPTY_TO_MINSIZE(mp);
230 }
231 assert (mp->ma_used == 0);
232 assert (mp->ma_table == mp->ma_smalltable);
233 assert (mp->ma_mask == PyDict_MINSIZE - 1);
Christian Heimesb4ee4a12008-02-07 17:15:30 +0000234#ifdef SHOW_ALLOC_COUNT
235 count_reuse++;
236#endif
Raymond Hettinger43442782004-03-17 21:55:03 +0000237 } else {
Brett Cannon77ae87c2007-10-10 00:07:50 +0000238 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
Raymond Hettinger43442782004-03-17 21:55:03 +0000239 if (mp == NULL)
240 return NULL;
241 EMPTY_TO_MINSIZE(mp);
Christian Heimesb4ee4a12008-02-07 17:15:30 +0000242#ifdef SHOW_ALLOC_COUNT
243 count_alloc++;
244#endif
Raymond Hettinger43442782004-03-17 21:55:03 +0000245 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000246 mp->ma_lookup = lookdict_string;
247#ifdef SHOW_CONVERSION_COUNTS
248 ++created;
249#endif
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000250 _PyObject_GC_TRACK(mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000251 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000252}
253
254/*
255The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000256This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000257Open addressing is preferred over chaining since the link overhead for
258chaining would be substantial (100% with typical malloc overhead).
259
Tim Peterseb28ef22001-06-02 05:27:19 +0000260The initial probe index is computed as hash mod the table size. Subsequent
261probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000262
263All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000264
Tim Peterseb28ef22001-06-02 05:27:19 +0000265(The details in this version are due to Tim Peters, building on many past
266contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
267Christian Tismer).
Fred Drake1bff34a2000-08-31 19:31:38 +0000268
Tim Petersd770ebd2006-06-01 15:50:44 +0000269lookdict() is general-purpose, and may return NULL if (and only if) a
270comparison raises an exception (this was new in Python 2.5).
271lookdict_string() below is specialized to string keys, comparison of which can
272never raise an exception; that function can never return NULL. For both, when
Brett Cannon77ae87c2007-10-10 00:07:50 +0000273the key isn't found a PyDictEntry* is returned for which the me_value field is
Tim Petersd770ebd2006-06-01 15:50:44 +0000274NULL; this is the slot in the dict at which the key would have been found, and
275the caller can (if it wishes) add the <key, value> pair to the returned
Brett Cannon77ae87c2007-10-10 00:07:50 +0000276PyDictEntry*.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000277*/
Brett Cannon77ae87c2007-10-10 00:07:50 +0000278static PyDictEntry *
279lookdict(PyDictObject *mp, PyObject *key, register long hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000280{
Tim Petersd770ebd2006-06-01 15:50:44 +0000281 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000282 register size_t perturb;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000283 register PyDictEntry *freeslot;
Tim Petersd770ebd2006-06-01 15:50:44 +0000284 register size_t mask = (size_t)mp->ma_mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000285 PyDictEntry *ep0 = mp->ma_table;
286 register PyDictEntry *ep;
Fred Drakec88b99c2000-08-31 19:04:07 +0000287 register int cmp;
Tim Peters453163d2001-06-03 04:54:32 +0000288 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000289
Tim Petersd770ebd2006-06-01 15:50:44 +0000290 i = (size_t)hash & mask;
Guido van Rossumefb46091997-01-29 15:53:56 +0000291 ep = &ep0[i];
Guido van Rossumc1c7b1a1998-10-06 16:01:14 +0000292 if (ep->me_key == NULL || ep->me_key == key)
Guido van Rossum7d181591997-01-16 21:06:45 +0000293 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000294
Guido van Rossum16e93a81997-01-28 00:00:11 +0000295 if (ep->me_key == dummy)
Guido van Rossum7d181591997-01-16 21:06:45 +0000296 freeslot = ep;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000297 else {
Fred Drakec88b99c2000-08-31 19:04:07 +0000298 if (ep->me_hash == hash) {
Tim Peters453163d2001-06-03 04:54:32 +0000299 startkey = ep->me_key;
Georg Brandlede3a322007-11-29 18:33:01 +0000300 Py_INCREF(startkey);
Tim Peters453163d2001-06-03 04:54:32 +0000301 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Georg Brandlede3a322007-11-29 18:33:01 +0000302 Py_DECREF(startkey);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000303 if (cmp < 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000304 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000305 if (ep0 == mp->ma_table && ep->me_key == startkey) {
306 if (cmp > 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000307 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000308 }
309 else {
310 /* The compare did major nasty stuff to the
311 * dict: start over.
312 * XXX A clever adversary could prevent this
313 * XXX from terminating.
314 */
Armin Rigo35f6d362006-06-01 13:19:12 +0000315 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000316 }
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000317 }
318 freeslot = NULL;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000319 }
Tim Peters15d49292001-05-27 07:39:22 +0000320
Tim Peters342c65e2001-05-13 06:43:53 +0000321 /* In the loop, me_key == dummy is by far (factor of 100s) the
322 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000323 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
324 i = (i << 2) + i + perturb + 1;
325 ep = &ep0[i & mask];
Armin Rigo35f6d362006-06-01 13:19:12 +0000326 if (ep->me_key == NULL)
327 return freeslot == NULL ? ep : freeslot;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000328 if (ep->me_key == key)
Armin Rigo35f6d362006-06-01 13:19:12 +0000329 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000330 if (ep->me_hash == hash && ep->me_key != dummy) {
Tim Peters453163d2001-06-03 04:54:32 +0000331 startkey = ep->me_key;
Georg Brandlede3a322007-11-29 18:33:01 +0000332 Py_INCREF(startkey);
Tim Peters453163d2001-06-03 04:54:32 +0000333 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Georg Brandlede3a322007-11-29 18:33:01 +0000334 Py_DECREF(startkey);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000335 if (cmp < 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000336 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000337 if (ep0 == mp->ma_table && ep->me_key == startkey) {
338 if (cmp > 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000339 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000340 }
341 else {
342 /* The compare did major nasty stuff to the
343 * dict: start over.
344 * XXX A clever adversary could prevent this
345 * XXX from terminating.
346 */
Armin Rigo35f6d362006-06-01 13:19:12 +0000347 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000348 }
Guido van Rossum16e93a81997-01-28 00:00:11 +0000349 }
Tim Peters342c65e2001-05-13 06:43:53 +0000350 else if (ep->me_key == dummy && freeslot == NULL)
351 freeslot = ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000352 }
Neal Norwitza5ccda92006-10-28 21:16:54 +0000353 assert(0); /* NOT REACHED */
354 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000355}
356
357/*
Fred Drake1bff34a2000-08-31 19:31:38 +0000358 * Hacked up version of lookdict which can assume keys are always strings;
Tim Petersd770ebd2006-06-01 15:50:44 +0000359 * this assumption allows testing for errors during PyObject_RichCompareBool()
360 * to be dropped; string-string comparisons never raise exceptions. This also
361 * means we don't need to go through PyObject_RichCompareBool(); we can always
362 * use _PyString_Eq() directly.
Fred Drake1bff34a2000-08-31 19:31:38 +0000363 *
Tim Petersd770ebd2006-06-01 15:50:44 +0000364 * This is valuable because dicts with only string keys are very common.
Fred Drake1bff34a2000-08-31 19:31:38 +0000365 */
Brett Cannon77ae87c2007-10-10 00:07:50 +0000366static PyDictEntry *
367lookdict_string(PyDictObject *mp, PyObject *key, register long hash)
Fred Drake1bff34a2000-08-31 19:31:38 +0000368{
Tim Petersd770ebd2006-06-01 15:50:44 +0000369 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000370 register size_t perturb;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000371 register PyDictEntry *freeslot;
Tim Petersd770ebd2006-06-01 15:50:44 +0000372 register size_t mask = (size_t)mp->ma_mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000373 PyDictEntry *ep0 = mp->ma_table;
374 register PyDictEntry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000375
Tim Peters0ab085c2001-09-14 00:25:33 +0000376 /* Make sure this function doesn't have to handle non-string keys,
377 including subclasses of str; e.g., one reason to subclass
378 strings is to override __eq__, and for speed we don't cater to
379 that here. */
380 if (!PyString_CheckExact(key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000381#ifdef SHOW_CONVERSION_COUNTS
382 ++converted;
383#endif
384 mp->ma_lookup = lookdict;
385 return lookdict(mp, key, hash);
386 }
Tim Peters2f228e72001-05-13 00:19:31 +0000387 i = hash & mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000388 ep = &ep0[i];
389 if (ep->me_key == NULL || ep->me_key == key)
390 return ep;
391 if (ep->me_key == dummy)
392 freeslot = ep;
393 else {
Tim Petersd770ebd2006-06-01 15:50:44 +0000394 if (ep->me_hash == hash && _PyString_Eq(ep->me_key, key))
Fred Drake1bff34a2000-08-31 19:31:38 +0000395 return ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000396 freeslot = NULL;
397 }
Tim Peters15d49292001-05-27 07:39:22 +0000398
Tim Peters342c65e2001-05-13 06:43:53 +0000399 /* In the loop, me_key == dummy is by far (factor of 100s) the
400 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000401 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
402 i = (i << 2) + i + perturb + 1;
403 ep = &ep0[i & mask];
Tim Peters342c65e2001-05-13 06:43:53 +0000404 if (ep->me_key == NULL)
405 return freeslot == NULL ? ep : freeslot;
406 if (ep->me_key == key
407 || (ep->me_hash == hash
408 && ep->me_key != dummy
Martin v. Löwiscd353062001-05-24 16:56:35 +0000409 && _PyString_Eq(ep->me_key, key)))
Fred Drake1bff34a2000-08-31 19:31:38 +0000410 return ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000411 if (ep->me_key == dummy && freeslot == NULL)
Tim Peters342c65e2001-05-13 06:43:53 +0000412 freeslot = ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000413 }
Neal Norwitza5ccda92006-10-28 21:16:54 +0000414 assert(0); /* NOT REACHED */
415 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000416}
417
418/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000419Internal routine to insert a new item into the table.
420Used both by the internal resize routine and by the public insert routine.
421Eats a reference to key and one to value.
Tim Petersd770ebd2006-06-01 15:50:44 +0000422Returns -1 if an error occurred, or 0 on success.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000423*/
Armin Rigo35f6d362006-06-01 13:19:12 +0000424static int
Brett Cannon77ae87c2007-10-10 00:07:50 +0000425insertdict(register PyDictObject *mp, PyObject *key, long hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000426{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000427 PyObject *old_value;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000428 register PyDictEntry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000429 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
430
431 assert(mp->ma_lookup != NULL);
432 ep = mp->ma_lookup(mp, key, hash);
Armin Rigo35f6d362006-06-01 13:19:12 +0000433 if (ep == NULL) {
434 Py_DECREF(key);
435 Py_DECREF(value);
436 return -1;
437 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000438 if (ep->me_value != NULL) {
Guido van Rossumd7047b31995-01-02 19:07:15 +0000439 old_value = ep->me_value;
440 ep->me_value = value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000441 Py_DECREF(old_value); /* which **CAN** re-enter */
442 Py_DECREF(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000443 }
444 else {
445 if (ep->me_key == NULL)
446 mp->ma_fill++;
Raymond Hettinger07ead172005-02-05 23:42:57 +0000447 else {
448 assert(ep->me_key == dummy);
449 Py_DECREF(dummy);
450 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000451 ep->me_key = key;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000452 ep->me_hash = (Py_ssize_t)hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000453 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000454 mp->ma_used++;
455 }
Armin Rigo35f6d362006-06-01 13:19:12 +0000456 return 0;
457}
458
459/*
460Internal routine used by dictresize() to insert an item which is
461known to be absent from the dict. This routine also assumes that
462the dict contains no deleted entries. Besides the performance benefit,
463using insertdict() in dictresize() is dangerous (SF bug #1456209).
Tim Petersd770ebd2006-06-01 15:50:44 +0000464Note that no refcounts are changed by this routine; if needed, the caller
465is responsible for incref'ing `key` and `value`.
Armin Rigo35f6d362006-06-01 13:19:12 +0000466*/
467static void
Brett Cannon77ae87c2007-10-10 00:07:50 +0000468insertdict_clean(register PyDictObject *mp, PyObject *key, long hash,
Armin Rigo35f6d362006-06-01 13:19:12 +0000469 PyObject *value)
470{
Tim Petersd770ebd2006-06-01 15:50:44 +0000471 register size_t i;
Armin Rigo35f6d362006-06-01 13:19:12 +0000472 register size_t perturb;
Tim Petersd770ebd2006-06-01 15:50:44 +0000473 register size_t mask = (size_t)mp->ma_mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000474 PyDictEntry *ep0 = mp->ma_table;
475 register PyDictEntry *ep;
Armin Rigo35f6d362006-06-01 13:19:12 +0000476
477 i = hash & mask;
478 ep = &ep0[i];
479 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
480 i = (i << 2) + i + perturb + 1;
481 ep = &ep0[i & mask];
482 }
Tim Petersd770ebd2006-06-01 15:50:44 +0000483 assert(ep->me_value == NULL);
Armin Rigo35f6d362006-06-01 13:19:12 +0000484 mp->ma_fill++;
485 ep->me_key = key;
486 ep->me_hash = (Py_ssize_t)hash;
487 ep->me_value = value;
488 mp->ma_used++;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000489}
490
491/*
492Restructure the table by allocating a new table and reinserting all
493items again. When entries have been deleted, the new table may
494actually be smaller than the old one.
495*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000496static int
Brett Cannon77ae87c2007-10-10 00:07:50 +0000497dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000498{
Tim Peters9b10f7e2006-05-30 04:16:25 +0000499 Py_ssize_t newsize;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000500 PyDictEntry *oldtable, *newtable, *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000501 Py_ssize_t i;
Tim Peters0c6010b2001-05-23 23:33:57 +0000502 int is_oldtable_malloced;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000503 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000504
505 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000506
Tim Peterseb28ef22001-06-02 05:27:19 +0000507 /* Find the smallest table size > minused. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000508 for (newsize = PyDict_MINSIZE;
Tim Peterseb28ef22001-06-02 05:27:19 +0000509 newsize <= minused && newsize > 0;
510 newsize <<= 1)
511 ;
512 if (newsize <= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000513 PyErr_NoMemory();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000514 return -1;
515 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000516
517 /* Get space for a new table. */
518 oldtable = mp->ma_table;
519 assert(oldtable != NULL);
520 is_oldtable_malloced = oldtable != mp->ma_smalltable;
521
Tim Peters6d6c1a32001-08-02 04:15:00 +0000522 if (newsize == PyDict_MINSIZE) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000523 /* A large table is shrinking, or we can't get any smaller. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000524 newtable = mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000525 if (newtable == oldtable) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000526 if (mp->ma_fill == mp->ma_used) {
527 /* No dummies, so no point doing anything. */
Tim Peters0c6010b2001-05-23 23:33:57 +0000528 return 0;
Tim Petersf8a548c2001-05-24 16:26:40 +0000529 }
530 /* We're not going to resize it, but rebuild the
531 table anyway to purge old dummy entries.
532 Subtle: This is *necessary* if fill==size,
533 as lookdict needs at least one virgin slot to
534 terminate failing searches. If fill < size, it's
535 merely desirable, as dummies slow searches. */
536 assert(mp->ma_fill > mp->ma_used);
Tim Peters0c6010b2001-05-23 23:33:57 +0000537 memcpy(small_copy, oldtable, sizeof(small_copy));
538 oldtable = small_copy;
539 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000540 }
541 else {
Brett Cannon77ae87c2007-10-10 00:07:50 +0000542 newtable = PyMem_NEW(PyDictEntry, newsize);
Tim Petersdea48ec2001-05-22 20:40:22 +0000543 if (newtable == NULL) {
544 PyErr_NoMemory();
545 return -1;
546 }
547 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000548
549 /* Make the dict empty, using the new table. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000550 assert(newtable != oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000551 mp->ma_table = newtable;
Tim Petersafb6ae82001-06-04 21:00:21 +0000552 mp->ma_mask = newsize - 1;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000553 memset(newtable, 0, sizeof(PyDictEntry) * newsize);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000554 mp->ma_used = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000555 i = mp->ma_fill;
556 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000557
Tim Peters19283142001-05-17 22:25:34 +0000558 /* Copy the data over; this is refcount-neutral for active entries;
559 dummy entries aren't copied over, of course */
Tim Petersdea48ec2001-05-22 20:40:22 +0000560 for (ep = oldtable; i > 0; ep++) {
561 if (ep->me_value != NULL) { /* active entry */
562 --i;
Armin Rigo35f6d362006-06-01 13:19:12 +0000563 insertdict_clean(mp, ep->me_key, (long)ep->me_hash,
564 ep->me_value);
Tim Petersdea48ec2001-05-22 20:40:22 +0000565 }
Tim Peters19283142001-05-17 22:25:34 +0000566 else if (ep->me_key != NULL) { /* dummy entry */
Tim Petersdea48ec2001-05-22 20:40:22 +0000567 --i;
Tim Peters19283142001-05-17 22:25:34 +0000568 assert(ep->me_key == dummy);
569 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000570 }
Tim Peters19283142001-05-17 22:25:34 +0000571 /* else key == value == NULL: nothing to do */
Guido van Rossumd7047b31995-01-02 19:07:15 +0000572 }
573
Tim Peters0c6010b2001-05-23 23:33:57 +0000574 if (is_oldtable_malloced)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000575 PyMem_DEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000576 return 0;
577}
578
Raymond Hettingerfd7ed402007-12-18 21:24:09 +0000579/* Create a new dictionary pre-sized to hold an estimated number of elements.
580 Underestimates are okay because the dictionary will resize as necessary.
581 Overestimates just mean the dictionary will be more sparse than usual.
582*/
583
584PyObject *
585_PyDict_NewPresized(Py_ssize_t minused)
586{
587 PyObject *op = PyDict_New();
588
589 if (minused>5 && op != NULL && dictresize((PyDictObject *)op, minused) == -1) {
590 Py_DECREF(op);
591 return NULL;
592 }
593 return op;
594}
595
Tim Petersd770ebd2006-06-01 15:50:44 +0000596/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
597 * that may occur (originally dicts supported only string keys, and exceptions
598 * weren't possible). So, while the original intent was that a NULL return
Andrew M. Kuchling0067b5f2006-08-04 20:37:43 +0000599 * meant the key wasn't present, in reality it can mean that, or that an error
Tim Petersd770ebd2006-06-01 15:50:44 +0000600 * (suppressed) occurred while computing the key's hash, or that some error
601 * (suppressed) occurred when comparing keys in the dict's internal probe
602 * sequence. A nasty example of the latter is when a Python-coded comparison
603 * function hits a stack-depth error, which can cause this to return NULL
604 * even if the key is present.
605 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000606PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000607PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000608{
609 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000610 PyDictObject *mp = (PyDictObject *)op;
611 PyDictEntry *ep;
Armin Rigo35f6d362006-06-01 13:19:12 +0000612 PyThreadState *tstate;
Tim Petersd770ebd2006-06-01 15:50:44 +0000613 if (!PyDict_Check(op))
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000614 return NULL;
Tim Peters0ab085c2001-09-14 00:25:33 +0000615 if (!PyString_CheckExact(key) ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000616 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Guido van Rossumca756f21997-01-23 19:39:29 +0000617 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000618 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000619 if (hash == -1) {
620 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000621 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000622 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000623 }
Armin Rigo35f6d362006-06-01 13:19:12 +0000624
625 /* We can arrive here with a NULL tstate during initialization:
626 try running "python -Wi" for an example related to string
627 interning. Let's just hope that no exception occurs then... */
Armin Rigoacd0d6d2006-06-10 10:57:40 +0000628 tstate = _PyThreadState_Current;
Armin Rigo35f6d362006-06-01 13:19:12 +0000629 if (tstate != NULL && tstate->curexc_type != NULL) {
630 /* preserve the existing exception */
631 PyObject *err_type, *err_value, *err_tb;
632 PyErr_Fetch(&err_type, &err_value, &err_tb);
633 ep = (mp->ma_lookup)(mp, key, hash);
634 /* ignore errors */
635 PyErr_Restore(err_type, err_value, err_tb);
636 if (ep == NULL)
637 return NULL;
638 }
639 else {
640 ep = (mp->ma_lookup)(mp, key, hash);
641 if (ep == NULL) {
642 PyErr_Clear();
643 return NULL;
644 }
645 }
646 return ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000647}
648
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000649/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
Tim Peters60b29962006-01-01 01:19:23 +0000650 * dictionary if it's merely replacing the value for an existing key.
651 * This means that it's safe to loop over a dictionary with PyDict_Next()
652 * and occasionally replace a value -- but you can't insert new keys or
653 * remove them.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000654 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000655int
Tim Peters1f5871e2000-07-04 17:44:48 +0000656PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000657{
Brett Cannon77ae87c2007-10-10 00:07:50 +0000658 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000659 register long hash;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000660 register Py_ssize_t n_used;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000661
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000662 if (!PyDict_Check(op)) {
663 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000664 return -1;
665 }
Neal Norwitz48808a12006-07-21 05:29:58 +0000666 assert(key);
667 assert(value);
Brett Cannon77ae87c2007-10-10 00:07:50 +0000668 mp = (PyDictObject *)op;
Tim Peters0ab085c2001-09-14 00:25:33 +0000669 if (PyString_CheckExact(key)) {
Guido van Rossum45ec02a2002-08-19 21:43:18 +0000670 hash = ((PyStringObject *)key)->ob_shash;
671 if (hash == -1)
672 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000673 }
Tim Peters1f7df352002-03-29 03:29:08 +0000674 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000675 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000676 if (hash == -1)
677 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000678 }
Tim Petersafb6ae82001-06-04 21:00:21 +0000679 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000680 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000681 Py_INCREF(value);
682 Py_INCREF(key);
Armin Rigo35f6d362006-06-01 13:19:12 +0000683 if (insertdict(mp, key, hash, value) != 0)
684 return -1;
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000685 /* If we added a key, we can safely resize. Otherwise just return!
686 * If fill >= 2/3 size, adjust size. Normally, this doubles or
687 * quaduples the size, but it's also possible for the dict to shrink
Tim Peters60b29962006-01-01 01:19:23 +0000688 * (if ma_fill is much larger than ma_used, meaning a lot of dict
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000689 * keys have been * deleted).
Tim Peters60b29962006-01-01 01:19:23 +0000690 *
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000691 * Quadrupling the size improves average dictionary sparseness
692 * (reducing collisions) at the cost of some memory and iteration
693 * speed (which loops over every possible entry). It also halves
Neal Norwitz80af59c2006-05-30 04:19:21 +0000694 * the number of expensive resize operations in a growing dictionary.
Tim Peters60b29962006-01-01 01:19:23 +0000695 *
696 * Very large dictionaries (over 50K items) use doubling instead.
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000697 * This may help applications with severe memory constraints.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000698 */
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000699 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
700 return 0;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000701 return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000702}
703
704int
Tim Peters1f5871e2000-07-04 17:44:48 +0000705PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000706{
Brett Cannon77ae87c2007-10-10 00:07:50 +0000707 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000708 register long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000709 register PyDictEntry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000710 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000711
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000712 if (!PyDict_Check(op)) {
713 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000714 return -1;
715 }
Neal Norwitz48808a12006-07-21 05:29:58 +0000716 assert(key);
Tim Peters0ab085c2001-09-14 00:25:33 +0000717 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +0000718 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000719 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000720 if (hash == -1)
721 return -1;
722 }
Brett Cannon77ae87c2007-10-10 00:07:50 +0000723 mp = (PyDictObject *)op;
Fred Drake1bff34a2000-08-31 19:31:38 +0000724 ep = (mp->ma_lookup)(mp, key, hash);
Armin Rigo35f6d362006-06-01 13:19:12 +0000725 if (ep == NULL)
726 return -1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000727 if (ep->me_value == NULL) {
Georg Brandlb9f4ad32006-10-29 18:31:42 +0000728 set_key_error(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000729 return -1;
730 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000731 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000732 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000733 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000734 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000735 ep->me_value = NULL;
736 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000737 Py_DECREF(old_value);
738 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000739 return 0;
740}
741
Guido van Rossum25831651993-05-19 14:50:45 +0000742void
Tim Peters1f5871e2000-07-04 17:44:48 +0000743PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000744{
Brett Cannon77ae87c2007-10-10 00:07:50 +0000745 PyDictObject *mp;
746 PyDictEntry *ep, *table;
Tim Petersdea48ec2001-05-22 20:40:22 +0000747 int table_is_malloced;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000748 Py_ssize_t fill;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000749 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Petersdea48ec2001-05-22 20:40:22 +0000750#ifdef Py_DEBUG
Tim Peters9b10f7e2006-05-30 04:16:25 +0000751 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +0000752#endif
753
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000754 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000755 return;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000756 mp = (PyDictObject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000757#ifdef Py_DEBUG
Tim Petersafb6ae82001-06-04 21:00:21 +0000758 n = mp->ma_mask + 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000759 i = 0;
760#endif
761
762 table = mp->ma_table;
763 assert(table != NULL);
764 table_is_malloced = table != mp->ma_smalltable;
765
766 /* This is delicate. During the process of clearing the dict,
767 * decrefs can cause the dict to mutate. To avoid fatal confusion
768 * (voice of experience), we have to make the dict empty before
769 * clearing the slots, and never refer to anything via mp->xxx while
770 * clearing.
771 */
772 fill = mp->ma_fill;
773 if (table_is_malloced)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000774 EMPTY_TO_MINSIZE(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000775
776 else if (fill > 0) {
777 /* It's a small table with something that needs to be cleared.
778 * Afraid the only safe way is to copy the dict entries into
779 * another small table first.
780 */
781 memcpy(small_copy, table, sizeof(small_copy));
782 table = small_copy;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000783 EMPTY_TO_MINSIZE(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000784 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000785 /* else it's a small table that's already empty */
786
787 /* Now we can finally clear things. If C had refcounts, we could
788 * assert that the refcount on table is 1 now, i.e. that this function
789 * has unique access to it, so decref side-effects can't alter it.
790 */
791 for (ep = table; fill > 0; ++ep) {
792#ifdef Py_DEBUG
793 assert(i < n);
794 ++i;
795#endif
796 if (ep->me_key) {
797 --fill;
798 Py_DECREF(ep->me_key);
799 Py_XDECREF(ep->me_value);
800 }
801#ifdef Py_DEBUG
802 else
803 assert(ep->me_value == NULL);
804#endif
805 }
806
807 if (table_is_malloced)
808 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000809}
810
Tim Peters080c88b2003-02-15 03:01:11 +0000811/*
812 * Iterate over a dict. Use like so:
813 *
Tim Peters9b10f7e2006-05-30 04:16:25 +0000814 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +0000815 * PyObject *key, *value;
816 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +0000817 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +0000818 * Refer to borrowed references in key and value.
819 * }
820 *
821 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +0000822 * mutates the dict. One exception: it is safe if the loop merely changes
823 * the values associated with the keys (but doesn't insert new keys or
824 * delete keys), via PyDict_SetItem().
825 */
Guido van Rossum25831651993-05-19 14:50:45 +0000826int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000827PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000828{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000829 register Py_ssize_t i;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000830 register Py_ssize_t mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000831 register PyDictEntry *ep;
Raymond Hettinger43442782004-03-17 21:55:03 +0000832
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000833 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000834 return 0;
Guido van Rossum25831651993-05-19 14:50:45 +0000835 i = *ppos;
836 if (i < 0)
837 return 0;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000838 ep = ((PyDictObject *)op)->ma_table;
839 mask = ((PyDictObject *)op)->ma_mask;
Raymond Hettinger43442782004-03-17 21:55:03 +0000840 while (i <= mask && ep[i].me_value == NULL)
Guido van Rossum25831651993-05-19 14:50:45 +0000841 i++;
842 *ppos = i+1;
Raymond Hettinger43442782004-03-17 21:55:03 +0000843 if (i > mask)
Guido van Rossum25831651993-05-19 14:50:45 +0000844 return 0;
845 if (pkey)
Raymond Hettinger43442782004-03-17 21:55:03 +0000846 *pkey = ep[i].me_key;
Guido van Rossum25831651993-05-19 14:50:45 +0000847 if (pvalue)
Raymond Hettinger43442782004-03-17 21:55:03 +0000848 *pvalue = ep[i].me_value;
Guido van Rossum25831651993-05-19 14:50:45 +0000849 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000850}
851
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000852/* Internal version of PyDict_Next that returns a hash value in addition to the key and value.*/
853int
854_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue, long *phash)
855{
856 register Py_ssize_t i;
857 register Py_ssize_t mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000858 register PyDictEntry *ep;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000859
860 if (!PyDict_Check(op))
861 return 0;
862 i = *ppos;
863 if (i < 0)
864 return 0;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000865 ep = ((PyDictObject *)op)->ma_table;
866 mask = ((PyDictObject *)op)->ma_mask;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000867 while (i <= mask && ep[i].me_value == NULL)
868 i++;
869 *ppos = i+1;
870 if (i > mask)
871 return 0;
872 *phash = (long)(ep[i].me_hash);
873 if (pkey)
874 *pkey = ep[i].me_key;
875 if (pvalue)
876 *pvalue = ep[i].me_value;
877 return 1;
878}
879
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000880/* Methods */
881
882static void
Brett Cannon77ae87c2007-10-10 00:07:50 +0000883dict_dealloc(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000884{
Brett Cannon77ae87c2007-10-10 00:07:50 +0000885 register PyDictEntry *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000886 Py_ssize_t fill = mp->ma_fill;
Guido van Rossumff413af2002-03-28 20:34:59 +0000887 PyObject_GC_UnTrack(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000888 Py_TRASHCAN_SAFE_BEGIN(mp)
Tim Petersdea48ec2001-05-22 20:40:22 +0000889 for (ep = mp->ma_table; fill > 0; ep++) {
890 if (ep->me_key) {
891 --fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000892 Py_DECREF(ep->me_key);
Tim Petersdea48ec2001-05-22 20:40:22 +0000893 Py_XDECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000894 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000895 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000896 if (mp->ma_table != mp->ma_smalltable)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000897 PyMem_DEL(mp->ma_table);
Christian Heimes5b970ad2008-02-06 13:33:44 +0000898 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
899 free_list[numfree++] = mp;
Tim Peters60b29962006-01-01 01:19:23 +0000900 else
Christian Heimese93237d2007-12-19 02:37:44 +0000901 Py_TYPE(mp)->tp_free((PyObject *)mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000902 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000903}
904
905static int
Brett Cannon77ae87c2007-10-10 00:07:50 +0000906dict_print(register PyDictObject *mp, register FILE *fp, register int flags)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000907{
Tim Peters9b10f7e2006-05-30 04:16:25 +0000908 register Py_ssize_t i;
909 register Py_ssize_t any;
Tim Peters33f4a6a2006-05-30 05:23:59 +0000910 int status;
Guido van Rossum255443b1998-04-10 22:47:14 +0000911
Tim Peters33f4a6a2006-05-30 05:23:59 +0000912 status = Py_ReprEnter((PyObject*)mp);
913 if (status != 0) {
914 if (status < 0)
915 return status;
Brett Cannon01531592007-09-17 03:28:34 +0000916 Py_BEGIN_ALLOW_THREADS
Guido van Rossum255443b1998-04-10 22:47:14 +0000917 fprintf(fp, "{...}");
Brett Cannon01531592007-09-17 03:28:34 +0000918 Py_END_ALLOW_THREADS
Guido van Rossum255443b1998-04-10 22:47:14 +0000919 return 0;
920 }
921
Brett Cannon01531592007-09-17 03:28:34 +0000922 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000923 fprintf(fp, "{");
Brett Cannon01531592007-09-17 03:28:34 +0000924 Py_END_ALLOW_THREADS
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000925 any = 0;
Tim Petersafb6ae82001-06-04 21:00:21 +0000926 for (i = 0; i <= mp->ma_mask; i++) {
Brett Cannon77ae87c2007-10-10 00:07:50 +0000927 PyDictEntry *ep = mp->ma_table + i;
Tim Peters23cf6be2001-06-02 08:02:56 +0000928 PyObject *pvalue = ep->me_value;
929 if (pvalue != NULL) {
930 /* Prevent PyObject_Repr from deleting value during
931 key format */
932 Py_INCREF(pvalue);
Brett Cannon01531592007-09-17 03:28:34 +0000933 if (any++ > 0) {
934 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000935 fprintf(fp, ", ");
Brett Cannon01531592007-09-17 03:28:34 +0000936 Py_END_ALLOW_THREADS
937 }
Guido van Rossum255443b1998-04-10 22:47:14 +0000938 if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000939 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000940 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000941 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000942 }
Brett Cannon01531592007-09-17 03:28:34 +0000943 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000944 fprintf(fp, ": ");
Brett Cannon01531592007-09-17 03:28:34 +0000945 Py_END_ALLOW_THREADS
Tim Peters19b77cf2001-06-02 08:27:39 +0000946 if (PyObject_Print(pvalue, fp, 0) != 0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000947 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000948 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000949 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000950 }
Tim Peters23cf6be2001-06-02 08:02:56 +0000951 Py_DECREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000952 }
953 }
Brett Cannon01531592007-09-17 03:28:34 +0000954 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000955 fprintf(fp, "}");
Brett Cannon01531592007-09-17 03:28:34 +0000956 Py_END_ALLOW_THREADS
Guido van Rossum255443b1998-04-10 22:47:14 +0000957 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000958 return 0;
959}
960
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000961static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +0000962dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000963{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000964 Py_ssize_t i;
Tim Petersa7259592001-06-16 05:11:17 +0000965 PyObject *s, *temp, *colon = NULL;
966 PyObject *pieces = NULL, *result = NULL;
967 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +0000968
Tim Petersa7259592001-06-16 05:11:17 +0000969 i = Py_ReprEnter((PyObject *)mp);
Guido van Rossum255443b1998-04-10 22:47:14 +0000970 if (i != 0) {
Tim Petersa7259592001-06-16 05:11:17 +0000971 return i > 0 ? PyString_FromString("{...}") : NULL;
Guido van Rossum255443b1998-04-10 22:47:14 +0000972 }
973
Tim Petersa7259592001-06-16 05:11:17 +0000974 if (mp->ma_used == 0) {
975 result = PyString_FromString("{}");
976 goto Done;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000977 }
Tim Petersa7259592001-06-16 05:11:17 +0000978
979 pieces = PyList_New(0);
980 if (pieces == NULL)
981 goto Done;
982
983 colon = PyString_FromString(": ");
984 if (colon == NULL)
985 goto Done;
986
987 /* Do repr() on each key+value pair, and insert ": " between them.
988 Note that repr may mutate the dict. */
Tim Petersc6057842001-06-16 07:52:53 +0000989 i = 0;
990 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Tim Petersa7259592001-06-16 05:11:17 +0000991 int status;
992 /* Prevent repr from deleting value during key format. */
993 Py_INCREF(value);
994 s = PyObject_Repr(key);
995 PyString_Concat(&s, colon);
996 PyString_ConcatAndDel(&s, PyObject_Repr(value));
997 Py_DECREF(value);
998 if (s == NULL)
999 goto Done;
1000 status = PyList_Append(pieces, s);
1001 Py_DECREF(s); /* append created a new ref */
1002 if (status < 0)
1003 goto Done;
1004 }
1005
1006 /* Add "{}" decorations to the first and last items. */
1007 assert(PyList_GET_SIZE(pieces) > 0);
1008 s = PyString_FromString("{");
1009 if (s == NULL)
1010 goto Done;
1011 temp = PyList_GET_ITEM(pieces, 0);
1012 PyString_ConcatAndDel(&s, temp);
1013 PyList_SET_ITEM(pieces, 0, s);
1014 if (s == NULL)
1015 goto Done;
1016
1017 s = PyString_FromString("}");
1018 if (s == NULL)
1019 goto Done;
1020 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
1021 PyString_ConcatAndDel(&temp, s);
1022 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
1023 if (temp == NULL)
1024 goto Done;
1025
1026 /* Paste them all together with ", " between. */
1027 s = PyString_FromString(", ");
1028 if (s == NULL)
1029 goto Done;
1030 result = _PyString_Join(s, pieces);
Tim Peters0ab085c2001-09-14 00:25:33 +00001031 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +00001032
1033Done:
1034 Py_XDECREF(pieces);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001035 Py_XDECREF(colon);
Tim Petersa7259592001-06-16 05:11:17 +00001036 Py_ReprLeave((PyObject *)mp);
1037 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001038}
1039
Martin v. Löwis18e16552006-02-15 17:27:45 +00001040static Py_ssize_t
Brett Cannon77ae87c2007-10-10 00:07:50 +00001041dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001042{
1043 return mp->ma_used;
1044}
1045
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001046static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001047dict_subscript(PyDictObject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001048{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001049 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001050 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001051 PyDictEntry *ep;
Tim Petersdea48ec2001-05-22 20:40:22 +00001052 assert(mp->ma_table != NULL);
Tim Peters0ab085c2001-09-14 00:25:33 +00001053 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001054 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001055 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001056 if (hash == -1)
1057 return NULL;
1058 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001059 ep = (mp->ma_lookup)(mp, key, hash);
1060 if (ep == NULL)
1061 return NULL;
1062 v = ep->me_value;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001063 if (v == NULL) {
1064 if (!PyDict_CheckExact(mp)) {
1065 /* Look up __missing__ method if we're a subclass. */
Guido van Rossum4b92a822006-02-25 23:32:30 +00001066 PyObject *missing;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001067 static PyObject *missing_str = NULL;
1068 if (missing_str == NULL)
Tim Peters9b10f7e2006-05-30 04:16:25 +00001069 missing_str =
Guido van Rossum1968ad32006-02-25 22:38:04 +00001070 PyString_InternFromString("__missing__");
Christian Heimese93237d2007-12-19 02:37:44 +00001071 missing = _PyType_Lookup(Py_TYPE(mp), missing_str);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001072 if (missing != NULL)
1073 return PyObject_CallFunctionObjArgs(missing,
1074 (PyObject *)mp, key, NULL);
1075 }
Georg Brandlb9f4ad32006-10-29 18:31:42 +00001076 set_key_error(key);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001077 return NULL;
1078 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001079 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001080 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001081 return v;
1082}
1083
1084static int
Brett Cannon77ae87c2007-10-10 00:07:50 +00001085dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001086{
1087 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001088 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001089 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001090 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001091}
1092
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001093static PyMappingMethods dict_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001094 (lenfunc)dict_length, /*mp_length*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001095 (binaryfunc)dict_subscript, /*mp_subscript*/
1096 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001097};
1098
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001099static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001100dict_keys(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001101{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001102 register PyObject *v;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001103 register Py_ssize_t i, j;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001104 PyDictEntry *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001105 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001106
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001107 again:
1108 n = mp->ma_used;
1109 v = PyList_New(n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001110 if (v == NULL)
1111 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001112 if (n != mp->ma_used) {
1113 /* Durnit. The allocations caused the dict to resize.
1114 * Just start over, this shouldn't normally happen.
1115 */
1116 Py_DECREF(v);
1117 goto again;
1118 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001119 ep = mp->ma_table;
1120 mask = mp->ma_mask;
1121 for (i = 0, j = 0; i <= mask; i++) {
1122 if (ep[i].me_value != NULL) {
1123 PyObject *key = ep[i].me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001124 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001125 PyList_SET_ITEM(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001126 j++;
1127 }
1128 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001129 assert(j == n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001130 return v;
1131}
1132
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001133static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001134dict_values(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001135{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001136 register PyObject *v;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001137 register Py_ssize_t i, j;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001138 PyDictEntry *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001139 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001140
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001141 again:
1142 n = mp->ma_used;
1143 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001144 if (v == NULL)
1145 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001146 if (n != mp->ma_used) {
1147 /* Durnit. The allocations caused the dict to resize.
1148 * Just start over, this shouldn't normally happen.
1149 */
1150 Py_DECREF(v);
1151 goto again;
1152 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001153 ep = mp->ma_table;
1154 mask = mp->ma_mask;
1155 for (i = 0, j = 0; i <= mask; i++) {
1156 if (ep[i].me_value != NULL) {
1157 PyObject *value = ep[i].me_value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001158 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001159 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001160 j++;
1161 }
1162 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001163 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001164 return v;
1165}
1166
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001167static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001168dict_items(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001169{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001170 register PyObject *v;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001171 register Py_ssize_t i, j, n;
1172 Py_ssize_t mask;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001173 PyObject *item, *key, *value;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001174 PyDictEntry *ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001175
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001176 /* Preallocate the list of tuples, to avoid allocations during
1177 * the loop over the items, which could trigger GC, which
1178 * could resize the dict. :-(
1179 */
1180 again:
1181 n = mp->ma_used;
1182 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001183 if (v == NULL)
1184 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001185 for (i = 0; i < n; i++) {
1186 item = PyTuple_New(2);
1187 if (item == NULL) {
1188 Py_DECREF(v);
1189 return NULL;
1190 }
1191 PyList_SET_ITEM(v, i, item);
1192 }
1193 if (n != mp->ma_used) {
1194 /* Durnit. The allocations caused the dict to resize.
1195 * Just start over, this shouldn't normally happen.
1196 */
1197 Py_DECREF(v);
1198 goto again;
1199 }
1200 /* Nothing we do below makes any function calls. */
Raymond Hettinger43442782004-03-17 21:55:03 +00001201 ep = mp->ma_table;
1202 mask = mp->ma_mask;
1203 for (i = 0, j = 0; i <= mask; i++) {
Raymond Hettinger06905122004-03-19 10:30:00 +00001204 if ((value=ep[i].me_value) != NULL) {
Raymond Hettinger43442782004-03-17 21:55:03 +00001205 key = ep[i].me_key;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001206 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001207 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001208 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001209 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001210 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001211 j++;
1212 }
1213 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001214 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001215 return v;
1216}
1217
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001218static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001219dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001220{
1221 PyObject *seq;
1222 PyObject *value = Py_None;
1223 PyObject *it; /* iter(seq) */
1224 PyObject *key;
1225 PyObject *d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001226 int status;
1227
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001228 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001229 return NULL;
1230
1231 d = PyObject_CallObject(cls, NULL);
1232 if (d == NULL)
1233 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001234
Raymond Hettingercdcf8872007-11-07 02:26:17 +00001235 if (PyDict_CheckExact(d) && PyDict_CheckExact(seq)) {
1236 PyDictObject *mp = (PyDictObject *)d;
1237 PyObject *oldvalue;
1238 Py_ssize_t pos = 0;
1239 PyObject *key;
1240 long hash;
1241
Raymond Hettinger34448792007-11-09 23:14:44 +00001242 if (dictresize(mp, PySet_GET_SIZE(seq)))
Raymond Hettingercdcf8872007-11-07 02:26:17 +00001243 return NULL;
1244
1245 while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
1246 Py_INCREF(key);
1247 Py_INCREF(value);
1248 if (insertdict(mp, key, hash, value))
1249 return NULL;
1250 }
1251 return d;
1252 }
1253
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00001254 if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) {
Brett Cannon77ae87c2007-10-10 00:07:50 +00001255 PyDictObject *mp = (PyDictObject *)d;
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00001256 Py_ssize_t pos = 0;
1257 PyObject *key;
1258 long hash;
1259
1260 if (dictresize(mp, PySet_GET_SIZE(seq)))
1261 return NULL;
1262
1263 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
1264 Py_INCREF(key);
Raymond Hettingere3146f52007-03-21 20:33:57 +00001265 Py_INCREF(value);
1266 if (insertdict(mp, key, hash, value))
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00001267 return NULL;
1268 }
1269 return d;
1270 }
1271
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001272 it = PyObject_GetIter(seq);
1273 if (it == NULL){
1274 Py_DECREF(d);
1275 return NULL;
1276 }
1277
Raymond Hettinger34448792007-11-09 23:14:44 +00001278 if (PyDict_CheckExact(d)) {
1279 while ((key = PyIter_Next(it)) != NULL) {
1280 status = PyDict_SetItem(d, key, value);
1281 Py_DECREF(key);
1282 if (status < 0)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001283 goto Fail;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001284 }
Raymond Hettinger34448792007-11-09 23:14:44 +00001285 } else {
1286 while ((key = PyIter_Next(it)) != NULL) {
1287 status = PyObject_SetItem(d, key, value);
1288 Py_DECREF(key);
1289 if (status < 0)
1290 goto Fail;
1291 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001292 }
1293
Raymond Hettinger34448792007-11-09 23:14:44 +00001294 if (PyErr_Occurred())
1295 goto Fail;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001296 Py_DECREF(it);
1297 return d;
1298
1299Fail:
1300 Py_DECREF(it);
1301 Py_DECREF(d);
1302 return NULL;
1303}
1304
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001305static int
1306dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001307{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001308 PyObject *arg = NULL;
1309 int result = 0;
1310
1311 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1312 result = -1;
1313
1314 else if (arg != NULL) {
1315 if (PyObject_HasAttrString(arg, "keys"))
1316 result = PyDict_Merge(self, arg, 1);
1317 else
1318 result = PyDict_MergeFromSeq2(self, arg, 1);
1319 }
1320 if (result == 0 && kwds != NULL)
1321 result = PyDict_Merge(self, kwds, 1);
1322 return result;
1323}
1324
1325static PyObject *
1326dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1327{
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001328 if (dict_update_common(self, args, kwds, "update") != -1)
1329 Py_RETURN_NONE;
1330 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001331}
1332
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001333/* Update unconditionally replaces existing items.
1334 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001335 otherwise it leaves existing items unchanged.
1336
1337 PyDict_{Update,Merge} update/merge from a mapping object.
1338
Tim Petersf582b822001-12-11 18:51:08 +00001339 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001340 producing iterable objects of length 2.
1341*/
1342
Tim Petersf582b822001-12-11 18:51:08 +00001343int
Tim Peters1fc240e2001-10-26 05:06:50 +00001344PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1345{
1346 PyObject *it; /* iter(seq2) */
Tim Peters9b10f7e2006-05-30 04:16:25 +00001347 Py_ssize_t i; /* index into seq2 of current element */
Tim Peters1fc240e2001-10-26 05:06:50 +00001348 PyObject *item; /* seq2[i] */
1349 PyObject *fast; /* item as a 2-tuple or 2-list */
1350
1351 assert(d != NULL);
1352 assert(PyDict_Check(d));
1353 assert(seq2 != NULL);
1354
1355 it = PyObject_GetIter(seq2);
1356 if (it == NULL)
1357 return -1;
1358
1359 for (i = 0; ; ++i) {
1360 PyObject *key, *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001361 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001362
1363 fast = NULL;
1364 item = PyIter_Next(it);
1365 if (item == NULL) {
1366 if (PyErr_Occurred())
1367 goto Fail;
1368 break;
1369 }
1370
1371 /* Convert item to sequence, and verify length 2. */
1372 fast = PySequence_Fast(item, "");
1373 if (fast == NULL) {
1374 if (PyErr_ExceptionMatches(PyExc_TypeError))
1375 PyErr_Format(PyExc_TypeError,
1376 "cannot convert dictionary update "
Neal Norwitzd3881b02006-05-30 04:25:05 +00001377 "sequence element #%zd to a sequence",
Tim Peters1fc240e2001-10-26 05:06:50 +00001378 i);
1379 goto Fail;
1380 }
1381 n = PySequence_Fast_GET_SIZE(fast);
1382 if (n != 2) {
1383 PyErr_Format(PyExc_ValueError,
Neal Norwitzd3881b02006-05-30 04:25:05 +00001384 "dictionary update sequence element #%zd "
Martin v. Löwis2c95cc62006-02-16 06:54:25 +00001385 "has length %zd; 2 is required",
Martin v. Löwise0e89f72006-02-16 06:59:22 +00001386 i, n);
Tim Peters1fc240e2001-10-26 05:06:50 +00001387 goto Fail;
1388 }
1389
1390 /* Update/merge with this (key, value) pair. */
1391 key = PySequence_Fast_GET_ITEM(fast, 0);
1392 value = PySequence_Fast_GET_ITEM(fast, 1);
1393 if (override || PyDict_GetItem(d, key) == NULL) {
1394 int status = PyDict_SetItem(d, key, value);
1395 if (status < 0)
1396 goto Fail;
1397 }
1398 Py_DECREF(fast);
1399 Py_DECREF(item);
1400 }
1401
1402 i = 0;
1403 goto Return;
1404Fail:
1405 Py_XDECREF(item);
1406 Py_XDECREF(fast);
1407 i = -1;
1408Return:
1409 Py_DECREF(it);
Neal Norwitzd3881b02006-05-30 04:25:05 +00001410 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001411}
1412
Tim Peters6d6c1a32001-08-02 04:15:00 +00001413int
1414PyDict_Update(PyObject *a, PyObject *b)
1415{
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001416 return PyDict_Merge(a, b, 1);
1417}
1418
1419int
1420PyDict_Merge(PyObject *a, PyObject *b, int override)
1421{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001422 register PyDictObject *mp, *other;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001423 register Py_ssize_t i;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001424 PyDictEntry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001425
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001426 /* We accept for the argument either a concrete dictionary object,
1427 * or an abstract "mapping" object. For the former, we can do
1428 * things quite efficiently. For the latter, we only require that
1429 * PyMapping_Keys() and PyObject_GetItem() be supported.
1430 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001431 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1432 PyErr_BadInternalCall();
1433 return -1;
1434 }
Brett Cannon77ae87c2007-10-10 00:07:50 +00001435 mp = (PyDictObject*)a;
Raymond Hettinger3dbd4c52008-01-25 19:24:46 +00001436 if (PyDict_Check(b)) {
Brett Cannon77ae87c2007-10-10 00:07:50 +00001437 other = (PyDictObject*)b;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001438 if (other == mp || other->ma_used == 0)
1439 /* a.update(a) or a.update({}); nothing to do */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001440 return 0;
Raymond Hettinger186e7392005-05-14 18:08:25 +00001441 if (mp->ma_used == 0)
1442 /* Since the target dict is empty, PyDict_GetItem()
1443 * always returns NULL. Setting override to 1
1444 * skips the unnecessary test.
1445 */
1446 override = 1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001447 /* Do one big resize at the start, rather than
1448 * incrementally resizing as we insert new items. Expect
1449 * that there will be no (or few) overlapping keys.
1450 */
1451 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
Raymond Hettingerc8d22902003-05-07 00:49:40 +00001452 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001453 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001454 }
1455 for (i = 0; i <= other->ma_mask; i++) {
1456 entry = &other->ma_table[i];
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001457 if (entry->me_value != NULL &&
1458 (override ||
1459 PyDict_GetItem(a, entry->me_key) == NULL)) {
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001460 Py_INCREF(entry->me_key);
1461 Py_INCREF(entry->me_value);
Armin Rigo35f6d362006-06-01 13:19:12 +00001462 if (insertdict(mp, entry->me_key,
1463 (long)entry->me_hash,
1464 entry->me_value) != 0)
1465 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001466 }
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001467 }
1468 }
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001469 else {
1470 /* Do it the generic, slower way */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001471 PyObject *keys = PyMapping_Keys(b);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001472 PyObject *iter;
1473 PyObject *key, *value;
1474 int status;
1475
1476 if (keys == NULL)
1477 /* Docstring says this is equivalent to E.keys() so
1478 * if E doesn't have a .keys() method we want
1479 * AttributeError to percolate up. Might as well
1480 * do the same for any other error.
1481 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001482 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001483
1484 iter = PyObject_GetIter(keys);
1485 Py_DECREF(keys);
1486 if (iter == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001487 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001488
1489 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001490 if (!override && PyDict_GetItem(a, key) != NULL) {
1491 Py_DECREF(key);
1492 continue;
1493 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001494 value = PyObject_GetItem(b, key);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001495 if (value == NULL) {
1496 Py_DECREF(iter);
1497 Py_DECREF(key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001498 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001499 }
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001500 status = PyDict_SetItem(a, key, value);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001501 Py_DECREF(key);
1502 Py_DECREF(value);
1503 if (status < 0) {
1504 Py_DECREF(iter);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001505 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001506 }
1507 }
1508 Py_DECREF(iter);
1509 if (PyErr_Occurred())
1510 /* Iterator completed, via error */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001511 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001512 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001513 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001514}
1515
1516static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001517dict_copy(register PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001518{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001519 return PyDict_Copy((PyObject*)mp);
1520}
1521
1522PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001523PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001524{
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001525 PyObject *copy;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001526
1527 if (o == NULL || !PyDict_Check(o)) {
1528 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001529 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001530 }
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001531 copy = PyDict_New();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001532 if (copy == NULL)
1533 return NULL;
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001534 if (PyDict_Merge(copy, o, 1) == 0)
1535 return copy;
1536 Py_DECREF(copy);
Raymond Hettinger1356f782005-04-15 15:58:42 +00001537 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001538}
1539
Martin v. Löwis18e16552006-02-15 17:27:45 +00001540Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00001541PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001542{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001543 if (mp == NULL || !PyDict_Check(mp)) {
1544 PyErr_BadInternalCall();
Jeremy Hylton7083bb72004-02-17 20:10:11 +00001545 return -1;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001546 }
Brett Cannon77ae87c2007-10-10 00:07:50 +00001547 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001548}
1549
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001550PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001551PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001552{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001553 if (mp == NULL || !PyDict_Check(mp)) {
1554 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001555 return NULL;
1556 }
Brett Cannon77ae87c2007-10-10 00:07:50 +00001557 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001558}
1559
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001560PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001561PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001562{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001563 if (mp == NULL || !PyDict_Check(mp)) {
1564 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001565 return NULL;
1566 }
Brett Cannon77ae87c2007-10-10 00:07:50 +00001567 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001568}
1569
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001570PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001571PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001572{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001573 if (mp == NULL || !PyDict_Check(mp)) {
1574 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001575 return NULL;
1576 }
Brett Cannon77ae87c2007-10-10 00:07:50 +00001577 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001578}
1579
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001580/* Subroutine which returns the smallest key in a for which b's value
1581 is different or absent. The value is returned too, through the
Tim Peters95bf9392001-05-10 08:32:44 +00001582 pval argument. Both are NULL if no key in a is found for which b's status
1583 differs. The refcounts on (and only on) non-NULL *pval and function return
1584 values must be decremented by the caller (characterize() increments them
1585 to ensure that mutating comparison and PyDict_GetItem calls can't delete
1586 them before the caller is done looking at them). */
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001587
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001588static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001589characterize(PyDictObject *a, PyDictObject *b, PyObject **pval)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001590{
Tim Peters95bf9392001-05-10 08:32:44 +00001591 PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */
1592 PyObject *aval = NULL; /* a[akey] */
Tim Peters9b10f7e2006-05-30 04:16:25 +00001593 Py_ssize_t i;
1594 int cmp;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001595
Tim Petersafb6ae82001-06-04 21:00:21 +00001596 for (i = 0; i <= a->ma_mask; i++) {
Tim Peters95bf9392001-05-10 08:32:44 +00001597 PyObject *thiskey, *thisaval, *thisbval;
1598 if (a->ma_table[i].me_value == NULL)
1599 continue;
1600 thiskey = a->ma_table[i].me_key;
1601 Py_INCREF(thiskey); /* keep alive across compares */
1602 if (akey != NULL) {
1603 cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT);
1604 if (cmp < 0) {
1605 Py_DECREF(thiskey);
1606 goto Fail;
1607 }
1608 if (cmp > 0 ||
Tim Petersafb6ae82001-06-04 21:00:21 +00001609 i > a->ma_mask ||
Tim Peters95bf9392001-05-10 08:32:44 +00001610 a->ma_table[i].me_value == NULL)
1611 {
1612 /* Not the *smallest* a key; or maybe it is
1613 * but the compare shrunk the dict so we can't
1614 * find its associated value anymore; or
1615 * maybe it is but the compare deleted the
1616 * a[thiskey] entry.
Neal Norwitzd3881b02006-05-30 04:25:05 +00001617 */
Tim Peters95bf9392001-05-10 08:32:44 +00001618 Py_DECREF(thiskey);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001619 continue;
Guido van Rossumb9324202001-01-18 00:39:02 +00001620 }
Tim Peters95bf9392001-05-10 08:32:44 +00001621 }
1622
1623 /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */
1624 thisaval = a->ma_table[i].me_value;
1625 assert(thisaval);
1626 Py_INCREF(thisaval); /* keep alive */
1627 thisbval = PyDict_GetItem((PyObject *)b, thiskey);
1628 if (thisbval == NULL)
1629 cmp = 0;
1630 else {
1631 /* both dicts have thiskey: same values? */
1632 cmp = PyObject_RichCompareBool(
1633 thisaval, thisbval, Py_EQ);
1634 if (cmp < 0) {
1635 Py_DECREF(thiskey);
1636 Py_DECREF(thisaval);
1637 goto Fail;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001638 }
1639 }
Tim Peters95bf9392001-05-10 08:32:44 +00001640 if (cmp == 0) {
1641 /* New winner. */
1642 Py_XDECREF(akey);
1643 Py_XDECREF(aval);
1644 akey = thiskey;
1645 aval = thisaval;
1646 }
1647 else {
1648 Py_DECREF(thiskey);
1649 Py_DECREF(thisaval);
1650 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001651 }
Tim Peters95bf9392001-05-10 08:32:44 +00001652 *pval = aval;
1653 return akey;
1654
1655Fail:
1656 Py_XDECREF(akey);
1657 Py_XDECREF(aval);
1658 *pval = NULL;
1659 return NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001660}
1661
1662static int
Brett Cannon77ae87c2007-10-10 00:07:50 +00001663dict_compare(PyDictObject *a, PyDictObject *b)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001664{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001665 PyObject *adiff, *bdiff, *aval, *bval;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001666 int res;
1667
1668 /* Compare lengths first */
1669 if (a->ma_used < b->ma_used)
1670 return -1; /* a is shorter */
1671 else if (a->ma_used > b->ma_used)
1672 return 1; /* b is shorter */
Tim Peters95bf9392001-05-10 08:32:44 +00001673
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001674 /* Same length -- check all keys */
Tim Peters95bf9392001-05-10 08:32:44 +00001675 bdiff = bval = NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001676 adiff = characterize(a, b, &aval);
Tim Peters95bf9392001-05-10 08:32:44 +00001677 if (adiff == NULL) {
1678 assert(!aval);
Tim Peters3918fb22001-05-10 18:58:31 +00001679 /* Either an error, or a is a subset with the same length so
Tim Peters95bf9392001-05-10 08:32:44 +00001680 * must be equal.
1681 */
1682 res = PyErr_Occurred() ? -1 : 0;
1683 goto Finished;
1684 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001685 bdiff = characterize(b, a, &bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001686 if (bdiff == NULL && PyErr_Occurred()) {
1687 assert(!bval);
1688 res = -1;
1689 goto Finished;
1690 }
1691 res = 0;
1692 if (bdiff) {
1693 /* bdiff == NULL "should be" impossible now, but perhaps
1694 * the last comparison done by the characterize() on a had
1695 * the side effect of making the dicts equal!
1696 */
1697 res = PyObject_Compare(adiff, bdiff);
1698 }
1699 if (res == 0 && bval != NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001700 res = PyObject_Compare(aval, bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001701
1702Finished:
1703 Py_XDECREF(adiff);
1704 Py_XDECREF(bdiff);
1705 Py_XDECREF(aval);
1706 Py_XDECREF(bval);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001707 return res;
1708}
1709
Tim Peterse63415e2001-05-08 04:38:29 +00001710/* Return 1 if dicts equal, 0 if not, -1 if error.
1711 * Gets out as soon as any difference is detected.
1712 * Uses only Py_EQ comparison.
1713 */
1714static int
Brett Cannon77ae87c2007-10-10 00:07:50 +00001715dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00001716{
Tim Peters9b10f7e2006-05-30 04:16:25 +00001717 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00001718
1719 if (a->ma_used != b->ma_used)
1720 /* can't be equal if # of entries differ */
1721 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001722
Tim Peterse63415e2001-05-08 04:38:29 +00001723 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Tim Petersafb6ae82001-06-04 21:00:21 +00001724 for (i = 0; i <= a->ma_mask; i++) {
Tim Peterse63415e2001-05-08 04:38:29 +00001725 PyObject *aval = a->ma_table[i].me_value;
1726 if (aval != NULL) {
1727 int cmp;
1728 PyObject *bval;
1729 PyObject *key = a->ma_table[i].me_key;
1730 /* temporarily bump aval's refcount to ensure it stays
1731 alive until we're done with it */
1732 Py_INCREF(aval);
Neal Norwitza22975f2006-09-05 02:24:03 +00001733 /* ditto for key */
1734 Py_INCREF(key);
Tim Peterse63415e2001-05-08 04:38:29 +00001735 bval = PyDict_GetItem((PyObject *)b, key);
Neal Norwitza22975f2006-09-05 02:24:03 +00001736 Py_DECREF(key);
Tim Peterse63415e2001-05-08 04:38:29 +00001737 if (bval == NULL) {
1738 Py_DECREF(aval);
1739 return 0;
1740 }
1741 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1742 Py_DECREF(aval);
1743 if (cmp <= 0) /* error or not equal */
1744 return cmp;
1745 }
1746 }
1747 return 1;
1748 }
1749
1750static PyObject *
1751dict_richcompare(PyObject *v, PyObject *w, int op)
1752{
1753 int cmp;
1754 PyObject *res;
1755
1756 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1757 res = Py_NotImplemented;
1758 }
1759 else if (op == Py_EQ || op == Py_NE) {
Brett Cannon77ae87c2007-10-10 00:07:50 +00001760 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
Tim Peterse63415e2001-05-08 04:38:29 +00001761 if (cmp < 0)
1762 return NULL;
1763 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1764 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001765 else
1766 res = Py_NotImplemented;
Tim Peterse63415e2001-05-08 04:38:29 +00001767 Py_INCREF(res);
1768 return res;
1769 }
1770
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001771static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001772dict_contains(register PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001773{
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001774 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001775 PyDictEntry *ep;
Tim Petersd770ebd2006-06-01 15:50:44 +00001776
Tim Peters0ab085c2001-09-14 00:25:33 +00001777 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001778 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001779 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001780 if (hash == -1)
1781 return NULL;
1782 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001783 ep = (mp->ma_lookup)(mp, key, hash);
1784 if (ep == NULL)
1785 return NULL;
Tim Petersd770ebd2006-06-01 15:50:44 +00001786 return PyBool_FromLong(ep->me_value != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001787}
1788
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001789static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001790dict_has_key(register PyDictObject *mp, PyObject *key)
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +00001791{
1792 if (Py_Py3kWarningFlag &&
1793 PyErr_Warn(PyExc_DeprecationWarning,
1794 "dict.has_key() not supported in 3.x") < 0)
1795 return NULL;
1796 return dict_contains(mp, key);
1797}
1798
1799static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001800dict_get(register PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001801{
1802 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001803 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001804 PyObject *val = NULL;
1805 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001806 PyDictEntry *ep;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001807
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001808 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001809 return NULL;
1810
Tim Peters0ab085c2001-09-14 00:25:33 +00001811 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001812 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Barry Warsawc38c5da1997-10-06 17:49:20 +00001813 hash = PyObject_Hash(key);
1814 if (hash == -1)
1815 return NULL;
1816 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001817 ep = (mp->ma_lookup)(mp, key, hash);
1818 if (ep == NULL)
1819 return NULL;
1820 val = ep->me_value;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001821 if (val == NULL)
1822 val = failobj;
1823 Py_INCREF(val);
1824 return val;
1825}
1826
1827
1828static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001829dict_setdefault(register PyDictObject *mp, PyObject *args)
Guido van Rossum164452c2000-08-08 16:12:54 +00001830{
1831 PyObject *key;
1832 PyObject *failobj = Py_None;
1833 PyObject *val = NULL;
1834 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001835 PyDictEntry *ep;
Guido van Rossum164452c2000-08-08 16:12:54 +00001836
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001837 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
Guido van Rossum164452c2000-08-08 16:12:54 +00001838 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001839
Tim Peters0ab085c2001-09-14 00:25:33 +00001840 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001841 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossum164452c2000-08-08 16:12:54 +00001842 hash = PyObject_Hash(key);
1843 if (hash == -1)
1844 return NULL;
1845 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001846 ep = (mp->ma_lookup)(mp, key, hash);
1847 if (ep == NULL)
1848 return NULL;
1849 val = ep->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001850 if (val == NULL) {
1851 val = failobj;
1852 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1853 val = NULL;
1854 }
1855 Py_XINCREF(val);
1856 return val;
1857}
1858
1859
1860static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001861dict_clear(register PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001862{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001863 PyDict_Clear((PyObject *)mp);
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001864 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001865}
1866
Guido van Rossumba6ab842000-12-12 22:02:18 +00001867static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001868dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00001869{
1870 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001871 PyDictEntry *ep;
Guido van Rossume027d982002-04-12 15:11:59 +00001872 PyObject *old_value, *old_key;
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001873 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001874
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001875 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1876 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001877 if (mp->ma_used == 0) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001878 if (deflt) {
1879 Py_INCREF(deflt);
1880 return deflt;
1881 }
Guido van Rossume027d982002-04-12 15:11:59 +00001882 PyErr_SetString(PyExc_KeyError,
1883 "pop(): dictionary is empty");
1884 return NULL;
1885 }
1886 if (!PyString_CheckExact(key) ||
1887 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1888 hash = PyObject_Hash(key);
1889 if (hash == -1)
1890 return NULL;
1891 }
1892 ep = (mp->ma_lookup)(mp, key, hash);
Armin Rigo35f6d362006-06-01 13:19:12 +00001893 if (ep == NULL)
1894 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001895 if (ep->me_value == NULL) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001896 if (deflt) {
1897 Py_INCREF(deflt);
1898 return deflt;
1899 }
Georg Brandlb9f4ad32006-10-29 18:31:42 +00001900 set_key_error(key);
Guido van Rossume027d982002-04-12 15:11:59 +00001901 return NULL;
1902 }
1903 old_key = ep->me_key;
1904 Py_INCREF(dummy);
1905 ep->me_key = dummy;
1906 old_value = ep->me_value;
1907 ep->me_value = NULL;
1908 mp->ma_used--;
1909 Py_DECREF(old_key);
1910 return old_value;
1911}
1912
1913static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001914dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001915{
Tim Peters9b10f7e2006-05-30 04:16:25 +00001916 Py_ssize_t i = 0;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001917 PyDictEntry *ep;
Guido van Rossumba6ab842000-12-12 22:02:18 +00001918 PyObject *res;
1919
Tim Petersf4b33f62001-06-02 05:42:29 +00001920 /* Allocate the result tuple before checking the size. Believe it
1921 * or not, this allocation could trigger a garbage collection which
1922 * could empty the dict, so if we checked the size first and that
1923 * happened, the result would be an infinite loop (searching for an
1924 * entry that no longer exists). Note that the usual popitem()
1925 * idiom is "while d: k, v = d.popitem()". so needing to throw the
Tim Peters9b10f7e2006-05-30 04:16:25 +00001926 * tuple away if the dict *is* empty isn't a significant
Tim Petersf4b33f62001-06-02 05:42:29 +00001927 * inefficiency -- possible, but unlikely in practice.
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001928 */
1929 res = PyTuple_New(2);
1930 if (res == NULL)
1931 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001932 if (mp->ma_used == 0) {
1933 Py_DECREF(res);
1934 PyErr_SetString(PyExc_KeyError,
1935 "popitem(): dictionary is empty");
1936 return NULL;
1937 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001938 /* Set ep to "the first" dict entry with a value. We abuse the hash
1939 * field of slot 0 to hold a search finger:
1940 * If slot 0 has a value, use slot 0.
1941 * Else slot 0 is being used to hold a search finger,
Neal Norwitzd3881b02006-05-30 04:25:05 +00001942 * and we use its hash value as the first index to look.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001943 */
1944 ep = &mp->ma_table[0];
1945 if (ep->me_value == NULL) {
Tim Peters9b10f7e2006-05-30 04:16:25 +00001946 i = ep->me_hash;
Tim Petersdea48ec2001-05-22 20:40:22 +00001947 /* The hash field may be a real hash value, or it may be a
1948 * legit search finger, or it may be a once-legit search
1949 * finger that's out of bounds now because it wrapped around
1950 * or the table shrunk -- simply make sure it's in bounds now.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001951 */
Tim Petersafb6ae82001-06-04 21:00:21 +00001952 if (i > mp->ma_mask || i < 1)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001953 i = 1; /* skip slot 0 */
1954 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1955 i++;
Tim Petersafb6ae82001-06-04 21:00:21 +00001956 if (i > mp->ma_mask)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001957 i = 1;
1958 }
1959 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001960 PyTuple_SET_ITEM(res, 0, ep->me_key);
1961 PyTuple_SET_ITEM(res, 1, ep->me_value);
1962 Py_INCREF(dummy);
1963 ep->me_key = dummy;
1964 ep->me_value = NULL;
1965 mp->ma_used--;
1966 assert(mp->ma_table[0].me_value == NULL);
1967 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001968 return res;
1969}
1970
Jeremy Hylton8caad492000-06-23 14:18:11 +00001971static int
1972dict_traverse(PyObject *op, visitproc visit, void *arg)
1973{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001974 Py_ssize_t i = 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001975 PyObject *pk;
1976 PyObject *pv;
1977
1978 while (PyDict_Next(op, &i, &pk, &pv)) {
Thomas Woutersc6e55062006-04-15 21:47:09 +00001979 Py_VISIT(pk);
1980 Py_VISIT(pv);
Jeremy Hylton8caad492000-06-23 14:18:11 +00001981 }
1982 return 0;
1983}
1984
1985static int
1986dict_tp_clear(PyObject *op)
1987{
1988 PyDict_Clear(op);
1989 return 0;
1990}
1991
Tim Petersf7f88b12000-12-13 23:18:45 +00001992
Raymond Hettinger019a1482004-03-18 02:41:19 +00001993extern PyTypeObject PyDictIterKey_Type; /* Forward */
1994extern PyTypeObject PyDictIterValue_Type; /* Forward */
1995extern PyTypeObject PyDictIterItem_Type; /* Forward */
Brett Cannon77ae87c2007-10-10 00:07:50 +00001996static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001997
1998static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001999dict_iterkeys(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002000{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002001 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002002}
2003
2004static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002005dict_itervalues(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002006{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002007 return dictiter_new(dict, &PyDictIterValue_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002008}
2009
2010static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002011dict_iteritems(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002012{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002013 return dictiter_new(dict, &PyDictIterItem_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002014}
2015
2016
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002017PyDoc_STRVAR(has_key__doc__,
Raymond Hettinger574aa322003-09-01 22:12:08 +00002018"D.has_key(k) -> True if D has a key k, else False");
Tim Petersf7f88b12000-12-13 23:18:45 +00002019
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002020PyDoc_STRVAR(contains__doc__,
2021"D.__contains__(k) -> True if D has a key k, else False");
2022
2023PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
2024
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002025PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002026"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002027
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002028PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002029"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 +00002030
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002031PyDoc_STRVAR(pop__doc__,
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002032"D.pop(k[,d]) -> v, remove specified key and return the corresponding value\n\
2033If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00002034
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002035PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00002036"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000020372-tuple; but raise KeyError if D is empty");
Tim Petersf7f88b12000-12-13 23:18:45 +00002038
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002039PyDoc_STRVAR(keys__doc__,
2040"D.keys() -> list of D's keys");
Tim Petersf7f88b12000-12-13 23:18:45 +00002041
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002042PyDoc_STRVAR(items__doc__,
2043"D.items() -> list of D's (key, value) pairs, as 2-tuples");
Tim Petersf7f88b12000-12-13 23:18:45 +00002044
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002045PyDoc_STRVAR(values__doc__,
2046"D.values() -> list of D's values");
Tim Petersf7f88b12000-12-13 23:18:45 +00002047
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002048PyDoc_STRVAR(update__doc__,
Walter Dörwaldd70ad8a2004-05-28 20:59:21 +00002049"D.update(E, **F) -> None. Update D from E and F: for k in E: D[k] = E[k]\n\
2050(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 +00002051
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002052PyDoc_STRVAR(fromkeys__doc__,
2053"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
2054v defaults to None.");
2055
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002056PyDoc_STRVAR(clear__doc__,
2057"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002058
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002059PyDoc_STRVAR(copy__doc__,
2060"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00002061
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002062PyDoc_STRVAR(iterkeys__doc__,
2063"D.iterkeys() -> an iterator over the keys of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00002064
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002065PyDoc_STRVAR(itervalues__doc__,
2066"D.itervalues() -> an iterator over the values of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00002067
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002068PyDoc_STRVAR(iteritems__doc__,
2069"D.iteritems() -> an iterator over the (key, value) items of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00002070
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002071static PyMethodDef mapp_methods[] = {
Neal Norwitzc7926292007-05-23 06:57:35 +00002072 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002073 contains__doc__},
Raymond Hettinger0c669672003-12-13 13:31:55 +00002074 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002075 getitem__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002076 {"has_key", (PyCFunction)dict_has_key, METH_O,
Tim Petersf7f88b12000-12-13 23:18:45 +00002077 has_key__doc__},
2078 {"get", (PyCFunction)dict_get, METH_VARARGS,
2079 get__doc__},
2080 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
2081 setdefault_doc__},
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002082 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
Just van Rossuma797d812002-11-23 09:45:04 +00002083 pop__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002084 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002085 popitem__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002086 {"keys", (PyCFunction)dict_keys, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002087 keys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002088 {"items", (PyCFunction)dict_items, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002089 items__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002090 {"values", (PyCFunction)dict_values, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002091 values__doc__},
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002092 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002093 update__doc__},
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002094 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
2095 fromkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002096 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002097 clear__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002098 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002099 copy__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002100 {"iterkeys", (PyCFunction)dict_iterkeys, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00002101 iterkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002102 {"itervalues", (PyCFunction)dict_itervalues, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00002103 itervalues__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002104 {"iteritems", (PyCFunction)dict_iteritems, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00002105 iteritems__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00002106 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002107};
2108
Tim Petersd770ebd2006-06-01 15:50:44 +00002109/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002110int
2111PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002112{
2113 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +00002114 PyDictObject *mp = (PyDictObject *)op;
2115 PyDictEntry *ep;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002116
Tim Peters0ab085c2001-09-14 00:25:33 +00002117 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00002118 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002119 hash = PyObject_Hash(key);
2120 if (hash == -1)
2121 return -1;
2122 }
Armin Rigo35f6d362006-06-01 13:19:12 +00002123 ep = (mp->ma_lookup)(mp, key, hash);
Tim Petersd770ebd2006-06-01 15:50:44 +00002124 return ep == NULL ? -1 : (ep->me_value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002125}
2126
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +00002127/* Internal version of PyDict_Contains used when the hash value is already known */
2128int
2129_PyDict_Contains(PyObject *op, PyObject *key, long hash)
2130{
Brett Cannon77ae87c2007-10-10 00:07:50 +00002131 PyDictObject *mp = (PyDictObject *)op;
2132 PyDictEntry *ep;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +00002133
2134 ep = (mp->ma_lookup)(mp, key, hash);
2135 return ep == NULL ? -1 : (ep->me_value != NULL);
2136}
2137
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002138/* Hack to implement "key in dict" */
2139static PySequenceMethods dict_as_sequence = {
Georg Brandl347b3002006-03-30 11:57:00 +00002140 0, /* sq_length */
2141 0, /* sq_concat */
2142 0, /* sq_repeat */
2143 0, /* sq_item */
2144 0, /* sq_slice */
2145 0, /* sq_ass_item */
2146 0, /* sq_ass_slice */
2147 PyDict_Contains, /* sq_contains */
2148 0, /* sq_inplace_concat */
2149 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002150};
2151
Guido van Rossum09e563a2001-05-01 12:10:21 +00002152static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002153dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2154{
2155 PyObject *self;
2156
2157 assert(type != NULL && type->tp_alloc != NULL);
2158 self = type->tp_alloc(type, 0);
2159 if (self != NULL) {
2160 PyDictObject *d = (PyDictObject *)self;
2161 /* It's guaranteed that tp->alloc zeroed out the struct. */
2162 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
2163 INIT_NONZERO_DICT_SLOTS(d);
2164 d->ma_lookup = lookdict_string;
2165#ifdef SHOW_CONVERSION_COUNTS
2166 ++created;
2167#endif
2168 }
2169 return self;
2170}
2171
Tim Peters25786c02001-09-02 08:22:48 +00002172static int
2173dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2174{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002175 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002176}
2177
Tim Peters6d6c1a32001-08-02 04:15:00 +00002178static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002179dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002180{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002181 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002182}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002183
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002184PyDoc_STRVAR(dictionary_doc,
Tim Petersa427a2b2001-10-29 22:25:45 +00002185"dict() -> new empty dictionary.\n"
2186"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Tim Peters1fc240e2001-10-26 05:06:50 +00002187" (key, value) pairs.\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002188"dict(seq) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002189" d = {}\n"
2190" for k, v in seq:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002191" d[k] = v\n"
2192"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2193" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002194
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002195PyTypeObject PyDict_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002196 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Tim Petersa427a2b2001-10-29 22:25:45 +00002197 "dict",
Brett Cannon77ae87c2007-10-10 00:07:50 +00002198 sizeof(PyDictObject),
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002199 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00002200 (destructor)dict_dealloc, /* tp_dealloc */
2201 (printfunc)dict_print, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002202 0, /* tp_getattr */
Guido van Rossumb9324202001-01-18 00:39:02 +00002203 0, /* tp_setattr */
Tim Peters4fa58bf2001-05-10 21:45:19 +00002204 (cmpfunc)dict_compare, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00002205 (reprfunc)dict_repr, /* tp_repr */
2206 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002207 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00002208 &dict_as_mapping, /* tp_as_mapping */
Guido van Rossum64c06e32007-11-22 00:55:51 +00002209 0, /* tp_hash */
Guido van Rossumb9324202001-01-18 00:39:02 +00002210 0, /* tp_call */
2211 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002212 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumb9324202001-01-18 00:39:02 +00002213 0, /* tp_setattro */
2214 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002215 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Neal Norwitzee3a1b52007-02-25 19:44:48 +00002216 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
Tim Peters25786c02001-09-02 08:22:48 +00002217 dictionary_doc, /* tp_doc */
Georg Brandl347b3002006-03-30 11:57:00 +00002218 dict_traverse, /* tp_traverse */
2219 dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00002220 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002221 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00002222 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002223 0, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002224 mapp_methods, /* tp_methods */
2225 0, /* tp_members */
2226 0, /* tp_getset */
2227 0, /* tp_base */
2228 0, /* tp_dict */
2229 0, /* tp_descr_get */
2230 0, /* tp_descr_set */
2231 0, /* tp_dictoffset */
Georg Brandl347b3002006-03-30 11:57:00 +00002232 dict_init, /* tp_init */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002233 PyType_GenericAlloc, /* tp_alloc */
2234 dict_new, /* tp_new */
Neil Schemenauer6189b892002-04-12 02:43:00 +00002235 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002236};
2237
Guido van Rossum3cca2451997-05-16 14:23:33 +00002238/* For backward compatibility with old dictionary interface */
2239
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002240PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002241PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002242{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002243 PyObject *kv, *rv;
2244 kv = PyString_FromString(key);
2245 if (kv == NULL)
2246 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002247 rv = PyDict_GetItem(v, kv);
2248 Py_DECREF(kv);
2249 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002250}
2251
2252int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002253PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002254{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002255 PyObject *kv;
2256 int err;
2257 kv = PyString_FromString(key);
2258 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002259 return -1;
Guido van Rossum4f3bf1e1997-09-29 23:31:11 +00002260 PyString_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00002261 err = PyDict_SetItem(v, kv, item);
2262 Py_DECREF(kv);
2263 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002264}
2265
2266int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002267PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002268{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002269 PyObject *kv;
2270 int err;
2271 kv = PyString_FromString(key);
2272 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002273 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002274 err = PyDict_DelItem(v, kv);
2275 Py_DECREF(kv);
2276 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002277}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002278
Raymond Hettinger019a1482004-03-18 02:41:19 +00002279/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002280
2281typedef struct {
2282 PyObject_HEAD
Brett Cannon77ae87c2007-10-10 00:07:50 +00002283 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
Tim Peters9b10f7e2006-05-30 04:16:25 +00002284 Py_ssize_t di_used;
2285 Py_ssize_t di_pos;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002286 PyObject* di_result; /* reusable result tuple for iteritems */
Tim Peters9b10f7e2006-05-30 04:16:25 +00002287 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002288} dictiterobject;
2289
2290static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002291dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002292{
2293 dictiterobject *di;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002294 di = PyObject_New(dictiterobject, itertype);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002295 if (di == NULL)
2296 return NULL;
2297 Py_INCREF(dict);
2298 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00002299 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002300 di->di_pos = 0;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002301 di->len = dict->ma_used;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002302 if (itertype == &PyDictIterItem_Type) {
2303 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2304 if (di->di_result == NULL) {
2305 Py_DECREF(di);
2306 return NULL;
2307 }
2308 }
2309 else
2310 di->di_result = NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002311 return (PyObject *)di;
2312}
2313
2314static void
2315dictiter_dealloc(dictiterobject *di)
2316{
Guido van Rossum2147df72002-07-16 20:30:22 +00002317 Py_XDECREF(di->di_dict);
Raymond Hettinger019a1482004-03-18 02:41:19 +00002318 Py_XDECREF(di->di_result);
Neil Schemenauer6189b892002-04-12 02:43:00 +00002319 PyObject_Del(di);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002320}
2321
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002322static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002323dictiter_len(dictiterobject *di)
2324{
Tim Peters9b10f7e2006-05-30 04:16:25 +00002325 Py_ssize_t len = 0;
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00002326 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002327 len = di->len;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002328 return PyInt_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002329}
2330
Armin Rigof5b3e362006-02-11 21:32:43 +00002331PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002332
2333static PyMethodDef dictiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +00002334 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002335 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002336};
2337
Raymond Hettinger019a1482004-03-18 02:41:19 +00002338static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002339{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002340 PyObject *key;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002341 register Py_ssize_t i, mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +00002342 register PyDictEntry *ep;
2343 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002344
Raymond Hettinger019a1482004-03-18 02:41:19 +00002345 if (d == NULL)
Guido van Rossum2147df72002-07-16 20:30:22 +00002346 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002347 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002348
Raymond Hettinger019a1482004-03-18 02:41:19 +00002349 if (di->di_used != d->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00002350 PyErr_SetString(PyExc_RuntimeError,
2351 "dictionary changed size during iteration");
Guido van Rossum2147df72002-07-16 20:30:22 +00002352 di->di_used = -1; /* Make this state sticky */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002353 return NULL;
2354 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002355
Raymond Hettinger019a1482004-03-18 02:41:19 +00002356 i = di->di_pos;
2357 if (i < 0)
2358 goto fail;
2359 ep = d->ma_table;
2360 mask = d->ma_mask;
2361 while (i <= mask && ep[i].me_value == NULL)
2362 i++;
2363 di->di_pos = i+1;
2364 if (i > mask)
2365 goto fail;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002366 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002367 key = ep[i].me_key;
2368 Py_INCREF(key);
2369 return key;
2370
2371fail:
2372 Py_DECREF(d);
Guido van Rossum2147df72002-07-16 20:30:22 +00002373 di->di_dict = NULL;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002374 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002375}
2376
Raymond Hettinger019a1482004-03-18 02:41:19 +00002377PyTypeObject PyDictIterKey_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002378 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002379 "dictionary-keyiterator", /* tp_name */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002380 sizeof(dictiterobject), /* tp_basicsize */
2381 0, /* tp_itemsize */
2382 /* methods */
2383 (destructor)dictiter_dealloc, /* tp_dealloc */
2384 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002385 0, /* tp_getattr */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002386 0, /* tp_setattr */
2387 0, /* tp_compare */
2388 0, /* tp_repr */
2389 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002390 0, /* tp_as_sequence */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002391 0, /* tp_as_mapping */
2392 0, /* tp_hash */
2393 0, /* tp_call */
2394 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002395 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002396 0, /* tp_setattro */
2397 0, /* tp_as_buffer */
2398 Py_TPFLAGS_DEFAULT, /* tp_flags */
2399 0, /* tp_doc */
2400 0, /* tp_traverse */
2401 0, /* tp_clear */
2402 0, /* tp_richcompare */
2403 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002404 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002405 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002406 dictiter_methods, /* tp_methods */
2407 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002408};
2409
2410static PyObject *dictiter_iternextvalue(dictiterobject *di)
2411{
2412 PyObject *value;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002413 register Py_ssize_t i, mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +00002414 register PyDictEntry *ep;
2415 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002416
2417 if (d == NULL)
2418 return NULL;
2419 assert (PyDict_Check(d));
2420
2421 if (di->di_used != d->ma_used) {
2422 PyErr_SetString(PyExc_RuntimeError,
2423 "dictionary changed size during iteration");
2424 di->di_used = -1; /* Make this state sticky */
2425 return NULL;
2426 }
2427
2428 i = di->di_pos;
Guido van Rossum09240f62004-03-20 19:11:58 +00002429 mask = d->ma_mask;
2430 if (i < 0 || i > mask)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002431 goto fail;
2432 ep = d->ma_table;
Guido van Rossum09240f62004-03-20 19:11:58 +00002433 while ((value=ep[i].me_value) == NULL) {
Raymond Hettinger019a1482004-03-18 02:41:19 +00002434 i++;
Guido van Rossum09240f62004-03-20 19:11:58 +00002435 if (i > mask)
2436 goto fail;
2437 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002438 di->di_pos = i+1;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002439 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002440 Py_INCREF(value);
2441 return value;
2442
2443fail:
2444 Py_DECREF(d);
2445 di->di_dict = NULL;
2446 return NULL;
2447}
2448
2449PyTypeObject PyDictIterValue_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002450 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002451 "dictionary-valueiterator", /* tp_name */
2452 sizeof(dictiterobject), /* tp_basicsize */
2453 0, /* tp_itemsize */
2454 /* methods */
2455 (destructor)dictiter_dealloc, /* tp_dealloc */
2456 0, /* tp_print */
2457 0, /* tp_getattr */
2458 0, /* tp_setattr */
2459 0, /* tp_compare */
2460 0, /* tp_repr */
2461 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002462 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002463 0, /* tp_as_mapping */
2464 0, /* tp_hash */
2465 0, /* tp_call */
2466 0, /* tp_str */
2467 PyObject_GenericGetAttr, /* tp_getattro */
2468 0, /* tp_setattro */
2469 0, /* tp_as_buffer */
2470 Py_TPFLAGS_DEFAULT, /* tp_flags */
2471 0, /* tp_doc */
2472 0, /* tp_traverse */
2473 0, /* tp_clear */
2474 0, /* tp_richcompare */
2475 0, /* tp_weaklistoffset */
2476 PyObject_SelfIter, /* tp_iter */
2477 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002478 dictiter_methods, /* tp_methods */
2479 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002480};
2481
2482static PyObject *dictiter_iternextitem(dictiterobject *di)
2483{
2484 PyObject *key, *value, *result = di->di_result;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002485 register Py_ssize_t i, mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +00002486 register PyDictEntry *ep;
2487 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002488
2489 if (d == NULL)
2490 return NULL;
2491 assert (PyDict_Check(d));
2492
2493 if (di->di_used != d->ma_used) {
2494 PyErr_SetString(PyExc_RuntimeError,
2495 "dictionary changed size during iteration");
2496 di->di_used = -1; /* Make this state sticky */
2497 return NULL;
2498 }
2499
2500 i = di->di_pos;
2501 if (i < 0)
2502 goto fail;
2503 ep = d->ma_table;
2504 mask = d->ma_mask;
2505 while (i <= mask && ep[i].me_value == NULL)
2506 i++;
2507 di->di_pos = i+1;
2508 if (i > mask)
2509 goto fail;
2510
2511 if (result->ob_refcnt == 1) {
2512 Py_INCREF(result);
2513 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2514 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2515 } else {
2516 result = PyTuple_New(2);
Tim Peters60b29962006-01-01 01:19:23 +00002517 if (result == NULL)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002518 return NULL;
2519 }
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002520 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002521 key = ep[i].me_key;
2522 value = ep[i].me_value;
2523 Py_INCREF(key);
2524 Py_INCREF(value);
2525 PyTuple_SET_ITEM(result, 0, key);
2526 PyTuple_SET_ITEM(result, 1, value);
2527 return result;
2528
2529fail:
2530 Py_DECREF(d);
2531 di->di_dict = NULL;
2532 return NULL;
2533}
2534
2535PyTypeObject PyDictIterItem_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002536 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002537 "dictionary-itemiterator", /* tp_name */
2538 sizeof(dictiterobject), /* tp_basicsize */
2539 0, /* tp_itemsize */
2540 /* methods */
2541 (destructor)dictiter_dealloc, /* tp_dealloc */
2542 0, /* tp_print */
2543 0, /* tp_getattr */
2544 0, /* tp_setattr */
2545 0, /* tp_compare */
2546 0, /* tp_repr */
2547 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002548 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002549 0, /* tp_as_mapping */
2550 0, /* tp_hash */
2551 0, /* tp_call */
2552 0, /* tp_str */
2553 PyObject_GenericGetAttr, /* tp_getattro */
2554 0, /* tp_setattro */
2555 0, /* tp_as_buffer */
2556 Py_TPFLAGS_DEFAULT, /* tp_flags */
2557 0, /* tp_doc */
2558 0, /* tp_traverse */
2559 0, /* tp_clear */
2560 0, /* tp_richcompare */
2561 0, /* tp_weaklistoffset */
2562 PyObject_SelfIter, /* tp_iter */
2563 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002564 dictiter_methods, /* tp_methods */
2565 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002566};