blob: eaa490e320d41b9a270e4c9ed5afc77cf3410815 [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
Christian Heimesf75dbef2008-02-08 00:11:31 +0000208void
209PyDict_Fini(void)
210{
Christian Heimes48397d62008-02-08 00:14:34 +0000211 PyDictObject *op;
Christian Heimesf75dbef2008-02-08 00:11:31 +0000212
213 while (numfree) {
Christian Heimes48397d62008-02-08 00:14:34 +0000214 op = free_list[--numfree];
Christian Heimesf75dbef2008-02-08 00:11:31 +0000215 assert(PyDict_CheckExact(op));
216 PyObject_GC_Del(op);
217 }
218}
219
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000220PyObject *
Thomas Wouters78890102000-07-22 19:25:51 +0000221PyDict_New(void)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000222{
Brett Cannon77ae87c2007-10-10 00:07:50 +0000223 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000224 if (dummy == NULL) { /* Auto-initialize dummy */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000225 dummy = PyString_FromString("<dummy key>");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000226 if (dummy == NULL)
227 return NULL;
Fred Drake1bff34a2000-08-31 19:31:38 +0000228#ifdef SHOW_CONVERSION_COUNTS
229 Py_AtExit(show_counts);
230#endif
Christian Heimesb4ee4a12008-02-07 17:15:30 +0000231#ifdef SHOW_ALLOC_COUNT
232 Py_AtExit(show_alloc);
233#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000234 }
Christian Heimes5b970ad2008-02-06 13:33:44 +0000235 if (numfree) {
236 mp = free_list[--numfree];
Raymond Hettinger43442782004-03-17 21:55:03 +0000237 assert (mp != NULL);
Christian Heimese93237d2007-12-19 02:37:44 +0000238 assert (Py_TYPE(mp) == &PyDict_Type);
Raymond Hettinger43442782004-03-17 21:55:03 +0000239 _Py_NewReference((PyObject *)mp);
240 if (mp->ma_fill) {
241 EMPTY_TO_MINSIZE(mp);
242 }
243 assert (mp->ma_used == 0);
244 assert (mp->ma_table == mp->ma_smalltable);
245 assert (mp->ma_mask == PyDict_MINSIZE - 1);
Christian Heimesb4ee4a12008-02-07 17:15:30 +0000246#ifdef SHOW_ALLOC_COUNT
247 count_reuse++;
248#endif
Raymond Hettinger43442782004-03-17 21:55:03 +0000249 } else {
Brett Cannon77ae87c2007-10-10 00:07:50 +0000250 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
Raymond Hettinger43442782004-03-17 21:55:03 +0000251 if (mp == NULL)
252 return NULL;
253 EMPTY_TO_MINSIZE(mp);
Christian Heimesb4ee4a12008-02-07 17:15:30 +0000254#ifdef SHOW_ALLOC_COUNT
255 count_alloc++;
256#endif
Raymond Hettinger43442782004-03-17 21:55:03 +0000257 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000258 mp->ma_lookup = lookdict_string;
259#ifdef SHOW_CONVERSION_COUNTS
260 ++created;
261#endif
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000262 _PyObject_GC_TRACK(mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000263 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000264}
265
266/*
267The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000268This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000269Open addressing is preferred over chaining since the link overhead for
270chaining would be substantial (100% with typical malloc overhead).
271
Tim Peterseb28ef22001-06-02 05:27:19 +0000272The initial probe index is computed as hash mod the table size. Subsequent
273probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000274
275All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000276
Tim Peterseb28ef22001-06-02 05:27:19 +0000277(The details in this version are due to Tim Peters, building on many past
278contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
279Christian Tismer).
Fred Drake1bff34a2000-08-31 19:31:38 +0000280
Tim Petersd770ebd2006-06-01 15:50:44 +0000281lookdict() is general-purpose, and may return NULL if (and only if) a
282comparison raises an exception (this was new in Python 2.5).
283lookdict_string() below is specialized to string keys, comparison of which can
284never raise an exception; that function can never return NULL. For both, when
Brett Cannon77ae87c2007-10-10 00:07:50 +0000285the key isn't found a PyDictEntry* is returned for which the me_value field is
Tim Petersd770ebd2006-06-01 15:50:44 +0000286NULL; this is the slot in the dict at which the key would have been found, and
287the caller can (if it wishes) add the <key, value> pair to the returned
Brett Cannon77ae87c2007-10-10 00:07:50 +0000288PyDictEntry*.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000289*/
Brett Cannon77ae87c2007-10-10 00:07:50 +0000290static PyDictEntry *
291lookdict(PyDictObject *mp, PyObject *key, register long hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000292{
Tim Petersd770ebd2006-06-01 15:50:44 +0000293 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000294 register size_t perturb;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000295 register PyDictEntry *freeslot;
Tim Petersd770ebd2006-06-01 15:50:44 +0000296 register size_t mask = (size_t)mp->ma_mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000297 PyDictEntry *ep0 = mp->ma_table;
298 register PyDictEntry *ep;
Fred Drakec88b99c2000-08-31 19:04:07 +0000299 register int cmp;
Tim Peters453163d2001-06-03 04:54:32 +0000300 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000301
Tim Petersd770ebd2006-06-01 15:50:44 +0000302 i = (size_t)hash & mask;
Guido van Rossumefb46091997-01-29 15:53:56 +0000303 ep = &ep0[i];
Guido van Rossumc1c7b1a1998-10-06 16:01:14 +0000304 if (ep->me_key == NULL || ep->me_key == key)
Guido van Rossum7d181591997-01-16 21:06:45 +0000305 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000306
Guido van Rossum16e93a81997-01-28 00:00:11 +0000307 if (ep->me_key == dummy)
Guido van Rossum7d181591997-01-16 21:06:45 +0000308 freeslot = ep;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000309 else {
Fred Drakec88b99c2000-08-31 19:04:07 +0000310 if (ep->me_hash == hash) {
Tim Peters453163d2001-06-03 04:54:32 +0000311 startkey = ep->me_key;
Georg Brandlede3a322007-11-29 18:33:01 +0000312 Py_INCREF(startkey);
Tim Peters453163d2001-06-03 04:54:32 +0000313 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Georg Brandlede3a322007-11-29 18:33:01 +0000314 Py_DECREF(startkey);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000315 if (cmp < 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000316 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000317 if (ep0 == mp->ma_table && ep->me_key == startkey) {
318 if (cmp > 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000319 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000320 }
321 else {
322 /* The compare did major nasty stuff to the
323 * dict: start over.
324 * XXX A clever adversary could prevent this
325 * XXX from terminating.
326 */
Armin Rigo35f6d362006-06-01 13:19:12 +0000327 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000328 }
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000329 }
330 freeslot = NULL;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000331 }
Tim Peters15d49292001-05-27 07:39:22 +0000332
Tim Peters342c65e2001-05-13 06:43:53 +0000333 /* In the loop, me_key == dummy is by far (factor of 100s) the
334 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000335 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
336 i = (i << 2) + i + perturb + 1;
337 ep = &ep0[i & mask];
Armin Rigo35f6d362006-06-01 13:19:12 +0000338 if (ep->me_key == NULL)
339 return freeslot == NULL ? ep : freeslot;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000340 if (ep->me_key == key)
Armin Rigo35f6d362006-06-01 13:19:12 +0000341 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000342 if (ep->me_hash == hash && ep->me_key != dummy) {
Tim Peters453163d2001-06-03 04:54:32 +0000343 startkey = ep->me_key;
Georg Brandlede3a322007-11-29 18:33:01 +0000344 Py_INCREF(startkey);
Tim Peters453163d2001-06-03 04:54:32 +0000345 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Georg Brandlede3a322007-11-29 18:33:01 +0000346 Py_DECREF(startkey);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000347 if (cmp < 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000348 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000349 if (ep0 == mp->ma_table && ep->me_key == startkey) {
350 if (cmp > 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000351 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000352 }
353 else {
354 /* The compare did major nasty stuff to the
355 * dict: start over.
356 * XXX A clever adversary could prevent this
357 * XXX from terminating.
358 */
Armin Rigo35f6d362006-06-01 13:19:12 +0000359 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000360 }
Guido van Rossum16e93a81997-01-28 00:00:11 +0000361 }
Tim Peters342c65e2001-05-13 06:43:53 +0000362 else if (ep->me_key == dummy && freeslot == NULL)
363 freeslot = ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000364 }
Neal Norwitza5ccda92006-10-28 21:16:54 +0000365 assert(0); /* NOT REACHED */
366 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000367}
368
369/*
Fred Drake1bff34a2000-08-31 19:31:38 +0000370 * Hacked up version of lookdict which can assume keys are always strings;
Tim Petersd770ebd2006-06-01 15:50:44 +0000371 * this assumption allows testing for errors during PyObject_RichCompareBool()
372 * to be dropped; string-string comparisons never raise exceptions. This also
373 * means we don't need to go through PyObject_RichCompareBool(); we can always
374 * use _PyString_Eq() directly.
Fred Drake1bff34a2000-08-31 19:31:38 +0000375 *
Tim Petersd770ebd2006-06-01 15:50:44 +0000376 * This is valuable because dicts with only string keys are very common.
Fred Drake1bff34a2000-08-31 19:31:38 +0000377 */
Brett Cannon77ae87c2007-10-10 00:07:50 +0000378static PyDictEntry *
379lookdict_string(PyDictObject *mp, PyObject *key, register long hash)
Fred Drake1bff34a2000-08-31 19:31:38 +0000380{
Tim Petersd770ebd2006-06-01 15:50:44 +0000381 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000382 register size_t perturb;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000383 register PyDictEntry *freeslot;
Tim Petersd770ebd2006-06-01 15:50:44 +0000384 register size_t mask = (size_t)mp->ma_mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000385 PyDictEntry *ep0 = mp->ma_table;
386 register PyDictEntry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000387
Tim Peters0ab085c2001-09-14 00:25:33 +0000388 /* Make sure this function doesn't have to handle non-string keys,
389 including subclasses of str; e.g., one reason to subclass
390 strings is to override __eq__, and for speed we don't cater to
391 that here. */
392 if (!PyString_CheckExact(key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000393#ifdef SHOW_CONVERSION_COUNTS
394 ++converted;
395#endif
396 mp->ma_lookup = lookdict;
397 return lookdict(mp, key, hash);
398 }
Tim Peters2f228e72001-05-13 00:19:31 +0000399 i = hash & mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000400 ep = &ep0[i];
401 if (ep->me_key == NULL || ep->me_key == key)
402 return ep;
403 if (ep->me_key == dummy)
404 freeslot = ep;
405 else {
Tim Petersd770ebd2006-06-01 15:50:44 +0000406 if (ep->me_hash == hash && _PyString_Eq(ep->me_key, key))
Fred Drake1bff34a2000-08-31 19:31:38 +0000407 return ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000408 freeslot = NULL;
409 }
Tim Peters15d49292001-05-27 07:39:22 +0000410
Tim Peters342c65e2001-05-13 06:43:53 +0000411 /* In the loop, me_key == dummy is by far (factor of 100s) the
412 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000413 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
414 i = (i << 2) + i + perturb + 1;
415 ep = &ep0[i & mask];
Tim Peters342c65e2001-05-13 06:43:53 +0000416 if (ep->me_key == NULL)
417 return freeslot == NULL ? ep : freeslot;
418 if (ep->me_key == key
419 || (ep->me_hash == hash
420 && ep->me_key != dummy
Martin v. Löwiscd353062001-05-24 16:56:35 +0000421 && _PyString_Eq(ep->me_key, key)))
Fred Drake1bff34a2000-08-31 19:31:38 +0000422 return ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000423 if (ep->me_key == dummy && freeslot == NULL)
Tim Peters342c65e2001-05-13 06:43:53 +0000424 freeslot = ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000425 }
Neal Norwitza5ccda92006-10-28 21:16:54 +0000426 assert(0); /* NOT REACHED */
427 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000428}
429
430/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000431Internal routine to insert a new item into the table.
432Used both by the internal resize routine and by the public insert routine.
433Eats a reference to key and one to value.
Tim Petersd770ebd2006-06-01 15:50:44 +0000434Returns -1 if an error occurred, or 0 on success.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000435*/
Armin Rigo35f6d362006-06-01 13:19:12 +0000436static int
Brett Cannon77ae87c2007-10-10 00:07:50 +0000437insertdict(register PyDictObject *mp, PyObject *key, long hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000438{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000439 PyObject *old_value;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000440 register PyDictEntry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000441 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
442
443 assert(mp->ma_lookup != NULL);
444 ep = mp->ma_lookup(mp, key, hash);
Armin Rigo35f6d362006-06-01 13:19:12 +0000445 if (ep == NULL) {
446 Py_DECREF(key);
447 Py_DECREF(value);
448 return -1;
449 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000450 if (ep->me_value != NULL) {
Guido van Rossumd7047b31995-01-02 19:07:15 +0000451 old_value = ep->me_value;
452 ep->me_value = value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000453 Py_DECREF(old_value); /* which **CAN** re-enter */
454 Py_DECREF(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000455 }
456 else {
457 if (ep->me_key == NULL)
458 mp->ma_fill++;
Raymond Hettinger07ead172005-02-05 23:42:57 +0000459 else {
460 assert(ep->me_key == dummy);
461 Py_DECREF(dummy);
462 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000463 ep->me_key = key;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000464 ep->me_hash = (Py_ssize_t)hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000465 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000466 mp->ma_used++;
467 }
Armin Rigo35f6d362006-06-01 13:19:12 +0000468 return 0;
469}
470
471/*
472Internal routine used by dictresize() to insert an item which is
473known to be absent from the dict. This routine also assumes that
474the dict contains no deleted entries. Besides the performance benefit,
475using insertdict() in dictresize() is dangerous (SF bug #1456209).
Tim Petersd770ebd2006-06-01 15:50:44 +0000476Note that no refcounts are changed by this routine; if needed, the caller
477is responsible for incref'ing `key` and `value`.
Armin Rigo35f6d362006-06-01 13:19:12 +0000478*/
479static void
Brett Cannon77ae87c2007-10-10 00:07:50 +0000480insertdict_clean(register PyDictObject *mp, PyObject *key, long hash,
Armin Rigo35f6d362006-06-01 13:19:12 +0000481 PyObject *value)
482{
Tim Petersd770ebd2006-06-01 15:50:44 +0000483 register size_t i;
Armin Rigo35f6d362006-06-01 13:19:12 +0000484 register size_t perturb;
Tim Petersd770ebd2006-06-01 15:50:44 +0000485 register size_t mask = (size_t)mp->ma_mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000486 PyDictEntry *ep0 = mp->ma_table;
487 register PyDictEntry *ep;
Armin Rigo35f6d362006-06-01 13:19:12 +0000488
489 i = hash & mask;
490 ep = &ep0[i];
491 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
492 i = (i << 2) + i + perturb + 1;
493 ep = &ep0[i & mask];
494 }
Tim Petersd770ebd2006-06-01 15:50:44 +0000495 assert(ep->me_value == NULL);
Armin Rigo35f6d362006-06-01 13:19:12 +0000496 mp->ma_fill++;
497 ep->me_key = key;
498 ep->me_hash = (Py_ssize_t)hash;
499 ep->me_value = value;
500 mp->ma_used++;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000501}
502
503/*
504Restructure the table by allocating a new table and reinserting all
505items again. When entries have been deleted, the new table may
506actually be smaller than the old one.
507*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000508static int
Brett Cannon77ae87c2007-10-10 00:07:50 +0000509dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000510{
Tim Peters9b10f7e2006-05-30 04:16:25 +0000511 Py_ssize_t newsize;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000512 PyDictEntry *oldtable, *newtable, *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000513 Py_ssize_t i;
Tim Peters0c6010b2001-05-23 23:33:57 +0000514 int is_oldtable_malloced;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000515 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000516
517 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000518
Tim Peterseb28ef22001-06-02 05:27:19 +0000519 /* Find the smallest table size > minused. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000520 for (newsize = PyDict_MINSIZE;
Tim Peterseb28ef22001-06-02 05:27:19 +0000521 newsize <= minused && newsize > 0;
522 newsize <<= 1)
523 ;
524 if (newsize <= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000525 PyErr_NoMemory();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000526 return -1;
527 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000528
529 /* Get space for a new table. */
530 oldtable = mp->ma_table;
531 assert(oldtable != NULL);
532 is_oldtable_malloced = oldtable != mp->ma_smalltable;
533
Tim Peters6d6c1a32001-08-02 04:15:00 +0000534 if (newsize == PyDict_MINSIZE) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000535 /* A large table is shrinking, or we can't get any smaller. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000536 newtable = mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000537 if (newtable == oldtable) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000538 if (mp->ma_fill == mp->ma_used) {
539 /* No dummies, so no point doing anything. */
Tim Peters0c6010b2001-05-23 23:33:57 +0000540 return 0;
Tim Petersf8a548c2001-05-24 16:26:40 +0000541 }
542 /* We're not going to resize it, but rebuild the
543 table anyway to purge old dummy entries.
544 Subtle: This is *necessary* if fill==size,
545 as lookdict needs at least one virgin slot to
546 terminate failing searches. If fill < size, it's
547 merely desirable, as dummies slow searches. */
548 assert(mp->ma_fill > mp->ma_used);
Tim Peters0c6010b2001-05-23 23:33:57 +0000549 memcpy(small_copy, oldtable, sizeof(small_copy));
550 oldtable = small_copy;
551 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000552 }
553 else {
Brett Cannon77ae87c2007-10-10 00:07:50 +0000554 newtable = PyMem_NEW(PyDictEntry, newsize);
Tim Petersdea48ec2001-05-22 20:40:22 +0000555 if (newtable == NULL) {
556 PyErr_NoMemory();
557 return -1;
558 }
559 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000560
561 /* Make the dict empty, using the new table. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000562 assert(newtable != oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000563 mp->ma_table = newtable;
Tim Petersafb6ae82001-06-04 21:00:21 +0000564 mp->ma_mask = newsize - 1;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000565 memset(newtable, 0, sizeof(PyDictEntry) * newsize);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000566 mp->ma_used = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000567 i = mp->ma_fill;
568 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000569
Tim Peters19283142001-05-17 22:25:34 +0000570 /* Copy the data over; this is refcount-neutral for active entries;
571 dummy entries aren't copied over, of course */
Tim Petersdea48ec2001-05-22 20:40:22 +0000572 for (ep = oldtable; i > 0; ep++) {
573 if (ep->me_value != NULL) { /* active entry */
574 --i;
Armin Rigo35f6d362006-06-01 13:19:12 +0000575 insertdict_clean(mp, ep->me_key, (long)ep->me_hash,
576 ep->me_value);
Tim Petersdea48ec2001-05-22 20:40:22 +0000577 }
Tim Peters19283142001-05-17 22:25:34 +0000578 else if (ep->me_key != NULL) { /* dummy entry */
Tim Petersdea48ec2001-05-22 20:40:22 +0000579 --i;
Tim Peters19283142001-05-17 22:25:34 +0000580 assert(ep->me_key == dummy);
581 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000582 }
Tim Peters19283142001-05-17 22:25:34 +0000583 /* else key == value == NULL: nothing to do */
Guido van Rossumd7047b31995-01-02 19:07:15 +0000584 }
585
Tim Peters0c6010b2001-05-23 23:33:57 +0000586 if (is_oldtable_malloced)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000587 PyMem_DEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000588 return 0;
589}
590
Raymond Hettingerfd7ed402007-12-18 21:24:09 +0000591/* Create a new dictionary pre-sized to hold an estimated number of elements.
592 Underestimates are okay because the dictionary will resize as necessary.
593 Overestimates just mean the dictionary will be more sparse than usual.
594*/
595
596PyObject *
597_PyDict_NewPresized(Py_ssize_t minused)
598{
599 PyObject *op = PyDict_New();
600
601 if (minused>5 && op != NULL && dictresize((PyDictObject *)op, minused) == -1) {
602 Py_DECREF(op);
603 return NULL;
604 }
605 return op;
606}
607
Tim Petersd770ebd2006-06-01 15:50:44 +0000608/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
609 * that may occur (originally dicts supported only string keys, and exceptions
610 * weren't possible). So, while the original intent was that a NULL return
Andrew M. Kuchling0067b5f2006-08-04 20:37:43 +0000611 * meant the key wasn't present, in reality it can mean that, or that an error
Tim Petersd770ebd2006-06-01 15:50:44 +0000612 * (suppressed) occurred while computing the key's hash, or that some error
613 * (suppressed) occurred when comparing keys in the dict's internal probe
614 * sequence. A nasty example of the latter is when a Python-coded comparison
615 * function hits a stack-depth error, which can cause this to return NULL
616 * even if the key is present.
617 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000618PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000619PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000620{
621 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000622 PyDictObject *mp = (PyDictObject *)op;
623 PyDictEntry *ep;
Armin Rigo35f6d362006-06-01 13:19:12 +0000624 PyThreadState *tstate;
Tim Petersd770ebd2006-06-01 15:50:44 +0000625 if (!PyDict_Check(op))
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000626 return NULL;
Tim Peters0ab085c2001-09-14 00:25:33 +0000627 if (!PyString_CheckExact(key) ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000628 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Guido van Rossumca756f21997-01-23 19:39:29 +0000629 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000630 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000631 if (hash == -1) {
632 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000633 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000634 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000635 }
Armin Rigo35f6d362006-06-01 13:19:12 +0000636
637 /* We can arrive here with a NULL tstate during initialization:
638 try running "python -Wi" for an example related to string
639 interning. Let's just hope that no exception occurs then... */
Armin Rigoacd0d6d2006-06-10 10:57:40 +0000640 tstate = _PyThreadState_Current;
Armin Rigo35f6d362006-06-01 13:19:12 +0000641 if (tstate != NULL && tstate->curexc_type != NULL) {
642 /* preserve the existing exception */
643 PyObject *err_type, *err_value, *err_tb;
644 PyErr_Fetch(&err_type, &err_value, &err_tb);
645 ep = (mp->ma_lookup)(mp, key, hash);
646 /* ignore errors */
647 PyErr_Restore(err_type, err_value, err_tb);
648 if (ep == NULL)
649 return NULL;
650 }
651 else {
652 ep = (mp->ma_lookup)(mp, key, hash);
653 if (ep == NULL) {
654 PyErr_Clear();
655 return NULL;
656 }
657 }
658 return ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000659}
660
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000661/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
Tim Peters60b29962006-01-01 01:19:23 +0000662 * dictionary if it's merely replacing the value for an existing key.
663 * This means that it's safe to loop over a dictionary with PyDict_Next()
664 * and occasionally replace a value -- but you can't insert new keys or
665 * remove them.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000666 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000667int
Tim Peters1f5871e2000-07-04 17:44:48 +0000668PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000669{
Brett Cannon77ae87c2007-10-10 00:07:50 +0000670 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000671 register long hash;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000672 register Py_ssize_t n_used;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000673
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000674 if (!PyDict_Check(op)) {
675 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000676 return -1;
677 }
Neal Norwitz48808a12006-07-21 05:29:58 +0000678 assert(key);
679 assert(value);
Brett Cannon77ae87c2007-10-10 00:07:50 +0000680 mp = (PyDictObject *)op;
Tim Peters0ab085c2001-09-14 00:25:33 +0000681 if (PyString_CheckExact(key)) {
Guido van Rossum45ec02a2002-08-19 21:43:18 +0000682 hash = ((PyStringObject *)key)->ob_shash;
683 if (hash == -1)
684 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000685 }
Tim Peters1f7df352002-03-29 03:29:08 +0000686 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000687 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000688 if (hash == -1)
689 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000690 }
Tim Petersafb6ae82001-06-04 21:00:21 +0000691 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000692 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000693 Py_INCREF(value);
694 Py_INCREF(key);
Armin Rigo35f6d362006-06-01 13:19:12 +0000695 if (insertdict(mp, key, hash, value) != 0)
696 return -1;
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000697 /* If we added a key, we can safely resize. Otherwise just return!
698 * If fill >= 2/3 size, adjust size. Normally, this doubles or
699 * quaduples the size, but it's also possible for the dict to shrink
Tim Peters60b29962006-01-01 01:19:23 +0000700 * (if ma_fill is much larger than ma_used, meaning a lot of dict
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000701 * keys have been * deleted).
Tim Peters60b29962006-01-01 01:19:23 +0000702 *
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000703 * Quadrupling the size improves average dictionary sparseness
704 * (reducing collisions) at the cost of some memory and iteration
705 * speed (which loops over every possible entry). It also halves
Neal Norwitz80af59c2006-05-30 04:19:21 +0000706 * the number of expensive resize operations in a growing dictionary.
Tim Peters60b29962006-01-01 01:19:23 +0000707 *
708 * Very large dictionaries (over 50K items) use doubling instead.
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000709 * This may help applications with severe memory constraints.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000710 */
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000711 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
712 return 0;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000713 return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000714}
715
716int
Tim Peters1f5871e2000-07-04 17:44:48 +0000717PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000718{
Brett Cannon77ae87c2007-10-10 00:07:50 +0000719 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000720 register long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000721 register PyDictEntry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000722 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000723
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000724 if (!PyDict_Check(op)) {
725 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000726 return -1;
727 }
Neal Norwitz48808a12006-07-21 05:29:58 +0000728 assert(key);
Tim Peters0ab085c2001-09-14 00:25:33 +0000729 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +0000730 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000731 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000732 if (hash == -1)
733 return -1;
734 }
Brett Cannon77ae87c2007-10-10 00:07:50 +0000735 mp = (PyDictObject *)op;
Fred Drake1bff34a2000-08-31 19:31:38 +0000736 ep = (mp->ma_lookup)(mp, key, hash);
Armin Rigo35f6d362006-06-01 13:19:12 +0000737 if (ep == NULL)
738 return -1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000739 if (ep->me_value == NULL) {
Georg Brandlb9f4ad32006-10-29 18:31:42 +0000740 set_key_error(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000741 return -1;
742 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000743 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000744 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000745 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000746 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000747 ep->me_value = NULL;
748 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000749 Py_DECREF(old_value);
750 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000751 return 0;
752}
753
Guido van Rossum25831651993-05-19 14:50:45 +0000754void
Tim Peters1f5871e2000-07-04 17:44:48 +0000755PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000756{
Brett Cannon77ae87c2007-10-10 00:07:50 +0000757 PyDictObject *mp;
758 PyDictEntry *ep, *table;
Tim Petersdea48ec2001-05-22 20:40:22 +0000759 int table_is_malloced;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000760 Py_ssize_t fill;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000761 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Petersdea48ec2001-05-22 20:40:22 +0000762#ifdef Py_DEBUG
Tim Peters9b10f7e2006-05-30 04:16:25 +0000763 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +0000764#endif
765
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000766 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000767 return;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000768 mp = (PyDictObject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000769#ifdef Py_DEBUG
Tim Petersafb6ae82001-06-04 21:00:21 +0000770 n = mp->ma_mask + 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000771 i = 0;
772#endif
773
774 table = mp->ma_table;
775 assert(table != NULL);
776 table_is_malloced = table != mp->ma_smalltable;
777
778 /* This is delicate. During the process of clearing the dict,
779 * decrefs can cause the dict to mutate. To avoid fatal confusion
780 * (voice of experience), we have to make the dict empty before
781 * clearing the slots, and never refer to anything via mp->xxx while
782 * clearing.
783 */
784 fill = mp->ma_fill;
785 if (table_is_malloced)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000786 EMPTY_TO_MINSIZE(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000787
788 else if (fill > 0) {
789 /* It's a small table with something that needs to be cleared.
790 * Afraid the only safe way is to copy the dict entries into
791 * another small table first.
792 */
793 memcpy(small_copy, table, sizeof(small_copy));
794 table = small_copy;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000795 EMPTY_TO_MINSIZE(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000796 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000797 /* else it's a small table that's already empty */
798
799 /* Now we can finally clear things. If C had refcounts, we could
800 * assert that the refcount on table is 1 now, i.e. that this function
801 * has unique access to it, so decref side-effects can't alter it.
802 */
803 for (ep = table; fill > 0; ++ep) {
804#ifdef Py_DEBUG
805 assert(i < n);
806 ++i;
807#endif
808 if (ep->me_key) {
809 --fill;
810 Py_DECREF(ep->me_key);
811 Py_XDECREF(ep->me_value);
812 }
813#ifdef Py_DEBUG
814 else
815 assert(ep->me_value == NULL);
816#endif
817 }
818
819 if (table_is_malloced)
820 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000821}
822
Tim Peters080c88b2003-02-15 03:01:11 +0000823/*
824 * Iterate over a dict. Use like so:
825 *
Tim Peters9b10f7e2006-05-30 04:16:25 +0000826 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +0000827 * PyObject *key, *value;
828 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +0000829 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +0000830 * Refer to borrowed references in key and value.
831 * }
832 *
833 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +0000834 * mutates the dict. One exception: it is safe if the loop merely changes
835 * the values associated with the keys (but doesn't insert new keys or
836 * delete keys), via PyDict_SetItem().
837 */
Guido van Rossum25831651993-05-19 14:50:45 +0000838int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000839PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000840{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000841 register Py_ssize_t i;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000842 register Py_ssize_t mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000843 register PyDictEntry *ep;
Raymond Hettinger43442782004-03-17 21:55:03 +0000844
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000845 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000846 return 0;
Guido van Rossum25831651993-05-19 14:50:45 +0000847 i = *ppos;
848 if (i < 0)
849 return 0;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000850 ep = ((PyDictObject *)op)->ma_table;
851 mask = ((PyDictObject *)op)->ma_mask;
Raymond Hettinger43442782004-03-17 21:55:03 +0000852 while (i <= mask && ep[i].me_value == NULL)
Guido van Rossum25831651993-05-19 14:50:45 +0000853 i++;
854 *ppos = i+1;
Raymond Hettinger43442782004-03-17 21:55:03 +0000855 if (i > mask)
Guido van Rossum25831651993-05-19 14:50:45 +0000856 return 0;
857 if (pkey)
Raymond Hettinger43442782004-03-17 21:55:03 +0000858 *pkey = ep[i].me_key;
Guido van Rossum25831651993-05-19 14:50:45 +0000859 if (pvalue)
Raymond Hettinger43442782004-03-17 21:55:03 +0000860 *pvalue = ep[i].me_value;
Guido van Rossum25831651993-05-19 14:50:45 +0000861 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000862}
863
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000864/* Internal version of PyDict_Next that returns a hash value in addition to the key and value.*/
865int
866_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue, long *phash)
867{
868 register Py_ssize_t i;
869 register Py_ssize_t mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000870 register PyDictEntry *ep;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000871
872 if (!PyDict_Check(op))
873 return 0;
874 i = *ppos;
875 if (i < 0)
876 return 0;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000877 ep = ((PyDictObject *)op)->ma_table;
878 mask = ((PyDictObject *)op)->ma_mask;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000879 while (i <= mask && ep[i].me_value == NULL)
880 i++;
881 *ppos = i+1;
882 if (i > mask)
883 return 0;
884 *phash = (long)(ep[i].me_hash);
885 if (pkey)
886 *pkey = ep[i].me_key;
887 if (pvalue)
888 *pvalue = ep[i].me_value;
889 return 1;
890}
891
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000892/* Methods */
893
894static void
Brett Cannon77ae87c2007-10-10 00:07:50 +0000895dict_dealloc(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000896{
Brett Cannon77ae87c2007-10-10 00:07:50 +0000897 register PyDictEntry *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000898 Py_ssize_t fill = mp->ma_fill;
Guido van Rossumff413af2002-03-28 20:34:59 +0000899 PyObject_GC_UnTrack(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000900 Py_TRASHCAN_SAFE_BEGIN(mp)
Tim Petersdea48ec2001-05-22 20:40:22 +0000901 for (ep = mp->ma_table; fill > 0; ep++) {
902 if (ep->me_key) {
903 --fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000904 Py_DECREF(ep->me_key);
Tim Petersdea48ec2001-05-22 20:40:22 +0000905 Py_XDECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000906 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000907 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000908 if (mp->ma_table != mp->ma_smalltable)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000909 PyMem_DEL(mp->ma_table);
Christian Heimes5b970ad2008-02-06 13:33:44 +0000910 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
911 free_list[numfree++] = mp;
Tim Peters60b29962006-01-01 01:19:23 +0000912 else
Christian Heimese93237d2007-12-19 02:37:44 +0000913 Py_TYPE(mp)->tp_free((PyObject *)mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000914 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000915}
916
917static int
Brett Cannon77ae87c2007-10-10 00:07:50 +0000918dict_print(register PyDictObject *mp, register FILE *fp, register int flags)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000919{
Tim Peters9b10f7e2006-05-30 04:16:25 +0000920 register Py_ssize_t i;
921 register Py_ssize_t any;
Tim Peters33f4a6a2006-05-30 05:23:59 +0000922 int status;
Guido van Rossum255443b1998-04-10 22:47:14 +0000923
Tim Peters33f4a6a2006-05-30 05:23:59 +0000924 status = Py_ReprEnter((PyObject*)mp);
925 if (status != 0) {
926 if (status < 0)
927 return status;
Brett Cannon01531592007-09-17 03:28:34 +0000928 Py_BEGIN_ALLOW_THREADS
Guido van Rossum255443b1998-04-10 22:47:14 +0000929 fprintf(fp, "{...}");
Brett Cannon01531592007-09-17 03:28:34 +0000930 Py_END_ALLOW_THREADS
Guido van Rossum255443b1998-04-10 22:47:14 +0000931 return 0;
932 }
933
Brett Cannon01531592007-09-17 03:28:34 +0000934 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
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000937 any = 0;
Tim Petersafb6ae82001-06-04 21:00:21 +0000938 for (i = 0; i <= mp->ma_mask; i++) {
Brett Cannon77ae87c2007-10-10 00:07:50 +0000939 PyDictEntry *ep = mp->ma_table + i;
Tim Peters23cf6be2001-06-02 08:02:56 +0000940 PyObject *pvalue = ep->me_value;
941 if (pvalue != NULL) {
942 /* Prevent PyObject_Repr from deleting value during
943 key format */
944 Py_INCREF(pvalue);
Brett Cannon01531592007-09-17 03:28:34 +0000945 if (any++ > 0) {
946 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000947 fprintf(fp, ", ");
Brett Cannon01531592007-09-17 03:28:34 +0000948 Py_END_ALLOW_THREADS
949 }
Guido van Rossum255443b1998-04-10 22:47:14 +0000950 if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000951 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000952 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000953 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000954 }
Brett Cannon01531592007-09-17 03:28:34 +0000955 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000956 fprintf(fp, ": ");
Brett Cannon01531592007-09-17 03:28:34 +0000957 Py_END_ALLOW_THREADS
Tim Peters19b77cf2001-06-02 08:27:39 +0000958 if (PyObject_Print(pvalue, fp, 0) != 0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000959 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000960 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000961 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000962 }
Tim Peters23cf6be2001-06-02 08:02:56 +0000963 Py_DECREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000964 }
965 }
Brett Cannon01531592007-09-17 03:28:34 +0000966 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000967 fprintf(fp, "}");
Brett Cannon01531592007-09-17 03:28:34 +0000968 Py_END_ALLOW_THREADS
Guido van Rossum255443b1998-04-10 22:47:14 +0000969 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000970 return 0;
971}
972
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000973static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +0000974dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000975{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000976 Py_ssize_t i;
Tim Petersa7259592001-06-16 05:11:17 +0000977 PyObject *s, *temp, *colon = NULL;
978 PyObject *pieces = NULL, *result = NULL;
979 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +0000980
Tim Petersa7259592001-06-16 05:11:17 +0000981 i = Py_ReprEnter((PyObject *)mp);
Guido van Rossum255443b1998-04-10 22:47:14 +0000982 if (i != 0) {
Tim Petersa7259592001-06-16 05:11:17 +0000983 return i > 0 ? PyString_FromString("{...}") : NULL;
Guido van Rossum255443b1998-04-10 22:47:14 +0000984 }
985
Tim Petersa7259592001-06-16 05:11:17 +0000986 if (mp->ma_used == 0) {
987 result = PyString_FromString("{}");
988 goto Done;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000989 }
Tim Petersa7259592001-06-16 05:11:17 +0000990
991 pieces = PyList_New(0);
992 if (pieces == NULL)
993 goto Done;
994
995 colon = PyString_FromString(": ");
996 if (colon == NULL)
997 goto Done;
998
999 /* Do repr() on each key+value pair, and insert ": " between them.
1000 Note that repr may mutate the dict. */
Tim Petersc6057842001-06-16 07:52:53 +00001001 i = 0;
1002 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Tim Petersa7259592001-06-16 05:11:17 +00001003 int status;
1004 /* Prevent repr from deleting value during key format. */
1005 Py_INCREF(value);
1006 s = PyObject_Repr(key);
1007 PyString_Concat(&s, colon);
1008 PyString_ConcatAndDel(&s, PyObject_Repr(value));
1009 Py_DECREF(value);
1010 if (s == NULL)
1011 goto Done;
1012 status = PyList_Append(pieces, s);
1013 Py_DECREF(s); /* append created a new ref */
1014 if (status < 0)
1015 goto Done;
1016 }
1017
1018 /* Add "{}" decorations to the first and last items. */
1019 assert(PyList_GET_SIZE(pieces) > 0);
1020 s = PyString_FromString("{");
1021 if (s == NULL)
1022 goto Done;
1023 temp = PyList_GET_ITEM(pieces, 0);
1024 PyString_ConcatAndDel(&s, temp);
1025 PyList_SET_ITEM(pieces, 0, s);
1026 if (s == NULL)
1027 goto Done;
1028
1029 s = PyString_FromString("}");
1030 if (s == NULL)
1031 goto Done;
1032 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
1033 PyString_ConcatAndDel(&temp, s);
1034 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
1035 if (temp == NULL)
1036 goto Done;
1037
1038 /* Paste them all together with ", " between. */
1039 s = PyString_FromString(", ");
1040 if (s == NULL)
1041 goto Done;
1042 result = _PyString_Join(s, pieces);
Tim Peters0ab085c2001-09-14 00:25:33 +00001043 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +00001044
1045Done:
1046 Py_XDECREF(pieces);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001047 Py_XDECREF(colon);
Tim Petersa7259592001-06-16 05:11:17 +00001048 Py_ReprLeave((PyObject *)mp);
1049 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001050}
1051
Martin v. Löwis18e16552006-02-15 17:27:45 +00001052static Py_ssize_t
Brett Cannon77ae87c2007-10-10 00:07:50 +00001053dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001054{
1055 return mp->ma_used;
1056}
1057
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001058static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001059dict_subscript(PyDictObject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001060{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001061 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001062 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001063 PyDictEntry *ep;
Tim Petersdea48ec2001-05-22 20:40:22 +00001064 assert(mp->ma_table != NULL);
Tim Peters0ab085c2001-09-14 00:25:33 +00001065 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001066 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001067 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001068 if (hash == -1)
1069 return NULL;
1070 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001071 ep = (mp->ma_lookup)(mp, key, hash);
1072 if (ep == NULL)
1073 return NULL;
1074 v = ep->me_value;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001075 if (v == NULL) {
1076 if (!PyDict_CheckExact(mp)) {
1077 /* Look up __missing__ method if we're a subclass. */
Guido van Rossum4b92a822006-02-25 23:32:30 +00001078 PyObject *missing;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001079 static PyObject *missing_str = NULL;
1080 if (missing_str == NULL)
Tim Peters9b10f7e2006-05-30 04:16:25 +00001081 missing_str =
Guido van Rossum1968ad32006-02-25 22:38:04 +00001082 PyString_InternFromString("__missing__");
Christian Heimese93237d2007-12-19 02:37:44 +00001083 missing = _PyType_Lookup(Py_TYPE(mp), missing_str);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001084 if (missing != NULL)
1085 return PyObject_CallFunctionObjArgs(missing,
1086 (PyObject *)mp, key, NULL);
1087 }
Georg Brandlb9f4ad32006-10-29 18:31:42 +00001088 set_key_error(key);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001089 return NULL;
1090 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001091 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001092 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001093 return v;
1094}
1095
1096static int
Brett Cannon77ae87c2007-10-10 00:07:50 +00001097dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001098{
1099 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001100 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001101 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001102 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001103}
1104
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001105static PyMappingMethods dict_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001106 (lenfunc)dict_length, /*mp_length*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001107 (binaryfunc)dict_subscript, /*mp_subscript*/
1108 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001109};
1110
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001111static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001112dict_keys(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001113{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001114 register PyObject *v;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001115 register Py_ssize_t i, j;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001116 PyDictEntry *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001117 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001118
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001119 again:
1120 n = mp->ma_used;
1121 v = PyList_New(n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001122 if (v == NULL)
1123 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001124 if (n != mp->ma_used) {
1125 /* Durnit. The allocations caused the dict to resize.
1126 * Just start over, this shouldn't normally happen.
1127 */
1128 Py_DECREF(v);
1129 goto again;
1130 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001131 ep = mp->ma_table;
1132 mask = mp->ma_mask;
1133 for (i = 0, j = 0; i <= mask; i++) {
1134 if (ep[i].me_value != NULL) {
1135 PyObject *key = ep[i].me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001136 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001137 PyList_SET_ITEM(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001138 j++;
1139 }
1140 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001141 assert(j == n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001142 return v;
1143}
1144
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001145static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001146dict_values(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001147{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001148 register PyObject *v;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001149 register Py_ssize_t i, j;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001150 PyDictEntry *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001151 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001152
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001153 again:
1154 n = mp->ma_used;
1155 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001156 if (v == NULL)
1157 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001158 if (n != mp->ma_used) {
1159 /* Durnit. The allocations caused the dict to resize.
1160 * Just start over, this shouldn't normally happen.
1161 */
1162 Py_DECREF(v);
1163 goto again;
1164 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001165 ep = mp->ma_table;
1166 mask = mp->ma_mask;
1167 for (i = 0, j = 0; i <= mask; i++) {
1168 if (ep[i].me_value != NULL) {
1169 PyObject *value = ep[i].me_value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001170 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001171 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001172 j++;
1173 }
1174 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001175 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001176 return v;
1177}
1178
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001179static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001180dict_items(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001181{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001182 register PyObject *v;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001183 register Py_ssize_t i, j, n;
1184 Py_ssize_t mask;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001185 PyObject *item, *key, *value;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001186 PyDictEntry *ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001187
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001188 /* Preallocate the list of tuples, to avoid allocations during
1189 * the loop over the items, which could trigger GC, which
1190 * could resize the dict. :-(
1191 */
1192 again:
1193 n = mp->ma_used;
1194 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001195 if (v == NULL)
1196 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001197 for (i = 0; i < n; i++) {
1198 item = PyTuple_New(2);
1199 if (item == NULL) {
1200 Py_DECREF(v);
1201 return NULL;
1202 }
1203 PyList_SET_ITEM(v, i, item);
1204 }
1205 if (n != mp->ma_used) {
1206 /* Durnit. The allocations caused the dict to resize.
1207 * Just start over, this shouldn't normally happen.
1208 */
1209 Py_DECREF(v);
1210 goto again;
1211 }
1212 /* Nothing we do below makes any function calls. */
Raymond Hettinger43442782004-03-17 21:55:03 +00001213 ep = mp->ma_table;
1214 mask = mp->ma_mask;
1215 for (i = 0, j = 0; i <= mask; i++) {
Raymond Hettinger06905122004-03-19 10:30:00 +00001216 if ((value=ep[i].me_value) != NULL) {
Raymond Hettinger43442782004-03-17 21:55:03 +00001217 key = ep[i].me_key;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001218 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001219 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001220 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001221 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001222 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001223 j++;
1224 }
1225 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001226 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001227 return v;
1228}
1229
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001230static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001231dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001232{
1233 PyObject *seq;
1234 PyObject *value = Py_None;
1235 PyObject *it; /* iter(seq) */
1236 PyObject *key;
1237 PyObject *d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001238 int status;
1239
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001240 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001241 return NULL;
1242
1243 d = PyObject_CallObject(cls, NULL);
1244 if (d == NULL)
1245 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001246
Raymond Hettingercdcf8872007-11-07 02:26:17 +00001247 if (PyDict_CheckExact(d) && PyDict_CheckExact(seq)) {
1248 PyDictObject *mp = (PyDictObject *)d;
1249 PyObject *oldvalue;
1250 Py_ssize_t pos = 0;
1251 PyObject *key;
1252 long hash;
1253
Raymond Hettinger34448792007-11-09 23:14:44 +00001254 if (dictresize(mp, PySet_GET_SIZE(seq)))
Raymond Hettingercdcf8872007-11-07 02:26:17 +00001255 return NULL;
1256
1257 while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
1258 Py_INCREF(key);
1259 Py_INCREF(value);
1260 if (insertdict(mp, key, hash, value))
1261 return NULL;
1262 }
1263 return d;
1264 }
1265
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00001266 if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) {
Brett Cannon77ae87c2007-10-10 00:07:50 +00001267 PyDictObject *mp = (PyDictObject *)d;
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00001268 Py_ssize_t pos = 0;
1269 PyObject *key;
1270 long hash;
1271
1272 if (dictresize(mp, PySet_GET_SIZE(seq)))
1273 return NULL;
1274
1275 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
1276 Py_INCREF(key);
Raymond Hettingere3146f52007-03-21 20:33:57 +00001277 Py_INCREF(value);
1278 if (insertdict(mp, key, hash, value))
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00001279 return NULL;
1280 }
1281 return d;
1282 }
1283
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001284 it = PyObject_GetIter(seq);
1285 if (it == NULL){
1286 Py_DECREF(d);
1287 return NULL;
1288 }
1289
Raymond Hettinger34448792007-11-09 23:14:44 +00001290 if (PyDict_CheckExact(d)) {
1291 while ((key = PyIter_Next(it)) != NULL) {
1292 status = PyDict_SetItem(d, key, value);
1293 Py_DECREF(key);
1294 if (status < 0)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001295 goto Fail;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001296 }
Raymond Hettinger34448792007-11-09 23:14:44 +00001297 } else {
1298 while ((key = PyIter_Next(it)) != NULL) {
1299 status = PyObject_SetItem(d, key, value);
1300 Py_DECREF(key);
1301 if (status < 0)
1302 goto Fail;
1303 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001304 }
1305
Raymond Hettinger34448792007-11-09 23:14:44 +00001306 if (PyErr_Occurred())
1307 goto Fail;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001308 Py_DECREF(it);
1309 return d;
1310
1311Fail:
1312 Py_DECREF(it);
1313 Py_DECREF(d);
1314 return NULL;
1315}
1316
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001317static int
1318dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001319{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001320 PyObject *arg = NULL;
1321 int result = 0;
1322
1323 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1324 result = -1;
1325
1326 else if (arg != NULL) {
1327 if (PyObject_HasAttrString(arg, "keys"))
1328 result = PyDict_Merge(self, arg, 1);
1329 else
1330 result = PyDict_MergeFromSeq2(self, arg, 1);
1331 }
1332 if (result == 0 && kwds != NULL)
1333 result = PyDict_Merge(self, kwds, 1);
1334 return result;
1335}
1336
1337static PyObject *
1338dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1339{
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001340 if (dict_update_common(self, args, kwds, "update") != -1)
1341 Py_RETURN_NONE;
1342 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001343}
1344
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001345/* Update unconditionally replaces existing items.
1346 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001347 otherwise it leaves existing items unchanged.
1348
1349 PyDict_{Update,Merge} update/merge from a mapping object.
1350
Tim Petersf582b822001-12-11 18:51:08 +00001351 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001352 producing iterable objects of length 2.
1353*/
1354
Tim Petersf582b822001-12-11 18:51:08 +00001355int
Tim Peters1fc240e2001-10-26 05:06:50 +00001356PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1357{
1358 PyObject *it; /* iter(seq2) */
Tim Peters9b10f7e2006-05-30 04:16:25 +00001359 Py_ssize_t i; /* index into seq2 of current element */
Tim Peters1fc240e2001-10-26 05:06:50 +00001360 PyObject *item; /* seq2[i] */
1361 PyObject *fast; /* item as a 2-tuple or 2-list */
1362
1363 assert(d != NULL);
1364 assert(PyDict_Check(d));
1365 assert(seq2 != NULL);
1366
1367 it = PyObject_GetIter(seq2);
1368 if (it == NULL)
1369 return -1;
1370
1371 for (i = 0; ; ++i) {
1372 PyObject *key, *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001373 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001374
1375 fast = NULL;
1376 item = PyIter_Next(it);
1377 if (item == NULL) {
1378 if (PyErr_Occurred())
1379 goto Fail;
1380 break;
1381 }
1382
1383 /* Convert item to sequence, and verify length 2. */
1384 fast = PySequence_Fast(item, "");
1385 if (fast == NULL) {
1386 if (PyErr_ExceptionMatches(PyExc_TypeError))
1387 PyErr_Format(PyExc_TypeError,
1388 "cannot convert dictionary update "
Neal Norwitzd3881b02006-05-30 04:25:05 +00001389 "sequence element #%zd to a sequence",
Tim Peters1fc240e2001-10-26 05:06:50 +00001390 i);
1391 goto Fail;
1392 }
1393 n = PySequence_Fast_GET_SIZE(fast);
1394 if (n != 2) {
1395 PyErr_Format(PyExc_ValueError,
Neal Norwitzd3881b02006-05-30 04:25:05 +00001396 "dictionary update sequence element #%zd "
Martin v. Löwis2c95cc62006-02-16 06:54:25 +00001397 "has length %zd; 2 is required",
Martin v. Löwise0e89f72006-02-16 06:59:22 +00001398 i, n);
Tim Peters1fc240e2001-10-26 05:06:50 +00001399 goto Fail;
1400 }
1401
1402 /* Update/merge with this (key, value) pair. */
1403 key = PySequence_Fast_GET_ITEM(fast, 0);
1404 value = PySequence_Fast_GET_ITEM(fast, 1);
1405 if (override || PyDict_GetItem(d, key) == NULL) {
1406 int status = PyDict_SetItem(d, key, value);
1407 if (status < 0)
1408 goto Fail;
1409 }
1410 Py_DECREF(fast);
1411 Py_DECREF(item);
1412 }
1413
1414 i = 0;
1415 goto Return;
1416Fail:
1417 Py_XDECREF(item);
1418 Py_XDECREF(fast);
1419 i = -1;
1420Return:
1421 Py_DECREF(it);
Neal Norwitzd3881b02006-05-30 04:25:05 +00001422 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001423}
1424
Tim Peters6d6c1a32001-08-02 04:15:00 +00001425int
1426PyDict_Update(PyObject *a, PyObject *b)
1427{
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001428 return PyDict_Merge(a, b, 1);
1429}
1430
1431int
1432PyDict_Merge(PyObject *a, PyObject *b, int override)
1433{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001434 register PyDictObject *mp, *other;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001435 register Py_ssize_t i;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001436 PyDictEntry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001437
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001438 /* We accept for the argument either a concrete dictionary object,
1439 * or an abstract "mapping" object. For the former, we can do
1440 * things quite efficiently. For the latter, we only require that
1441 * PyMapping_Keys() and PyObject_GetItem() be supported.
1442 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001443 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1444 PyErr_BadInternalCall();
1445 return -1;
1446 }
Brett Cannon77ae87c2007-10-10 00:07:50 +00001447 mp = (PyDictObject*)a;
Raymond Hettinger3dbd4c52008-01-25 19:24:46 +00001448 if (PyDict_Check(b)) {
Brett Cannon77ae87c2007-10-10 00:07:50 +00001449 other = (PyDictObject*)b;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001450 if (other == mp || other->ma_used == 0)
1451 /* a.update(a) or a.update({}); nothing to do */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001452 return 0;
Raymond Hettinger186e7392005-05-14 18:08:25 +00001453 if (mp->ma_used == 0)
1454 /* Since the target dict is empty, PyDict_GetItem()
1455 * always returns NULL. Setting override to 1
1456 * skips the unnecessary test.
1457 */
1458 override = 1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001459 /* Do one big resize at the start, rather than
1460 * incrementally resizing as we insert new items. Expect
1461 * that there will be no (or few) overlapping keys.
1462 */
1463 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
Raymond Hettingerc8d22902003-05-07 00:49:40 +00001464 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001465 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001466 }
1467 for (i = 0; i <= other->ma_mask; i++) {
1468 entry = &other->ma_table[i];
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001469 if (entry->me_value != NULL &&
1470 (override ||
1471 PyDict_GetItem(a, entry->me_key) == NULL)) {
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001472 Py_INCREF(entry->me_key);
1473 Py_INCREF(entry->me_value);
Armin Rigo35f6d362006-06-01 13:19:12 +00001474 if (insertdict(mp, entry->me_key,
1475 (long)entry->me_hash,
1476 entry->me_value) != 0)
1477 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001478 }
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001479 }
1480 }
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001481 else {
1482 /* Do it the generic, slower way */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001483 PyObject *keys = PyMapping_Keys(b);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001484 PyObject *iter;
1485 PyObject *key, *value;
1486 int status;
1487
1488 if (keys == NULL)
1489 /* Docstring says this is equivalent to E.keys() so
1490 * if E doesn't have a .keys() method we want
1491 * AttributeError to percolate up. Might as well
1492 * do the same for any other error.
1493 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001494 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001495
1496 iter = PyObject_GetIter(keys);
1497 Py_DECREF(keys);
1498 if (iter == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001499 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001500
1501 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001502 if (!override && PyDict_GetItem(a, key) != NULL) {
1503 Py_DECREF(key);
1504 continue;
1505 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001506 value = PyObject_GetItem(b, key);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001507 if (value == NULL) {
1508 Py_DECREF(iter);
1509 Py_DECREF(key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001510 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001511 }
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001512 status = PyDict_SetItem(a, key, value);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001513 Py_DECREF(key);
1514 Py_DECREF(value);
1515 if (status < 0) {
1516 Py_DECREF(iter);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001517 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001518 }
1519 }
1520 Py_DECREF(iter);
1521 if (PyErr_Occurred())
1522 /* Iterator completed, via error */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001523 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001524 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001525 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001526}
1527
1528static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001529dict_copy(register PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001530{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001531 return PyDict_Copy((PyObject*)mp);
1532}
1533
1534PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001535PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001536{
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001537 PyObject *copy;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001538
1539 if (o == NULL || !PyDict_Check(o)) {
1540 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001541 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001542 }
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001543 copy = PyDict_New();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001544 if (copy == NULL)
1545 return NULL;
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001546 if (PyDict_Merge(copy, o, 1) == 0)
1547 return copy;
1548 Py_DECREF(copy);
Raymond Hettinger1356f782005-04-15 15:58:42 +00001549 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001550}
1551
Martin v. Löwis18e16552006-02-15 17:27:45 +00001552Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00001553PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001554{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001555 if (mp == NULL || !PyDict_Check(mp)) {
1556 PyErr_BadInternalCall();
Jeremy Hylton7083bb72004-02-17 20:10:11 +00001557 return -1;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001558 }
Brett Cannon77ae87c2007-10-10 00:07:50 +00001559 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001560}
1561
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001562PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001563PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001564{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001565 if (mp == NULL || !PyDict_Check(mp)) {
1566 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001567 return NULL;
1568 }
Brett Cannon77ae87c2007-10-10 00:07:50 +00001569 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001570}
1571
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001572PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001573PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001574{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001575 if (mp == NULL || !PyDict_Check(mp)) {
1576 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001577 return NULL;
1578 }
Brett Cannon77ae87c2007-10-10 00:07:50 +00001579 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001580}
1581
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001582PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001583PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001584{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001585 if (mp == NULL || !PyDict_Check(mp)) {
1586 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001587 return NULL;
1588 }
Brett Cannon77ae87c2007-10-10 00:07:50 +00001589 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001590}
1591
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001592/* Subroutine which returns the smallest key in a for which b's value
1593 is different or absent. The value is returned too, through the
Tim Peters95bf9392001-05-10 08:32:44 +00001594 pval argument. Both are NULL if no key in a is found for which b's status
1595 differs. The refcounts on (and only on) non-NULL *pval and function return
1596 values must be decremented by the caller (characterize() increments them
1597 to ensure that mutating comparison and PyDict_GetItem calls can't delete
1598 them before the caller is done looking at them). */
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001599
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001600static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001601characterize(PyDictObject *a, PyDictObject *b, PyObject **pval)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001602{
Tim Peters95bf9392001-05-10 08:32:44 +00001603 PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */
1604 PyObject *aval = NULL; /* a[akey] */
Tim Peters9b10f7e2006-05-30 04:16:25 +00001605 Py_ssize_t i;
1606 int cmp;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001607
Tim Petersafb6ae82001-06-04 21:00:21 +00001608 for (i = 0; i <= a->ma_mask; i++) {
Tim Peters95bf9392001-05-10 08:32:44 +00001609 PyObject *thiskey, *thisaval, *thisbval;
1610 if (a->ma_table[i].me_value == NULL)
1611 continue;
1612 thiskey = a->ma_table[i].me_key;
1613 Py_INCREF(thiskey); /* keep alive across compares */
1614 if (akey != NULL) {
1615 cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT);
1616 if (cmp < 0) {
1617 Py_DECREF(thiskey);
1618 goto Fail;
1619 }
1620 if (cmp > 0 ||
Tim Petersafb6ae82001-06-04 21:00:21 +00001621 i > a->ma_mask ||
Tim Peters95bf9392001-05-10 08:32:44 +00001622 a->ma_table[i].me_value == NULL)
1623 {
1624 /* Not the *smallest* a key; or maybe it is
1625 * but the compare shrunk the dict so we can't
1626 * find its associated value anymore; or
1627 * maybe it is but the compare deleted the
1628 * a[thiskey] entry.
Neal Norwitzd3881b02006-05-30 04:25:05 +00001629 */
Tim Peters95bf9392001-05-10 08:32:44 +00001630 Py_DECREF(thiskey);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001631 continue;
Guido van Rossumb9324202001-01-18 00:39:02 +00001632 }
Tim Peters95bf9392001-05-10 08:32:44 +00001633 }
1634
1635 /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */
1636 thisaval = a->ma_table[i].me_value;
1637 assert(thisaval);
1638 Py_INCREF(thisaval); /* keep alive */
1639 thisbval = PyDict_GetItem((PyObject *)b, thiskey);
1640 if (thisbval == NULL)
1641 cmp = 0;
1642 else {
1643 /* both dicts have thiskey: same values? */
1644 cmp = PyObject_RichCompareBool(
1645 thisaval, thisbval, Py_EQ);
1646 if (cmp < 0) {
1647 Py_DECREF(thiskey);
1648 Py_DECREF(thisaval);
1649 goto Fail;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001650 }
1651 }
Tim Peters95bf9392001-05-10 08:32:44 +00001652 if (cmp == 0) {
1653 /* New winner. */
1654 Py_XDECREF(akey);
1655 Py_XDECREF(aval);
1656 akey = thiskey;
1657 aval = thisaval;
1658 }
1659 else {
1660 Py_DECREF(thiskey);
1661 Py_DECREF(thisaval);
1662 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001663 }
Tim Peters95bf9392001-05-10 08:32:44 +00001664 *pval = aval;
1665 return akey;
1666
1667Fail:
1668 Py_XDECREF(akey);
1669 Py_XDECREF(aval);
1670 *pval = NULL;
1671 return NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001672}
1673
1674static int
Brett Cannon77ae87c2007-10-10 00:07:50 +00001675dict_compare(PyDictObject *a, PyDictObject *b)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001676{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001677 PyObject *adiff, *bdiff, *aval, *bval;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001678 int res;
1679
1680 /* Compare lengths first */
1681 if (a->ma_used < b->ma_used)
1682 return -1; /* a is shorter */
1683 else if (a->ma_used > b->ma_used)
1684 return 1; /* b is shorter */
Tim Peters95bf9392001-05-10 08:32:44 +00001685
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001686 /* Same length -- check all keys */
Tim Peters95bf9392001-05-10 08:32:44 +00001687 bdiff = bval = NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001688 adiff = characterize(a, b, &aval);
Tim Peters95bf9392001-05-10 08:32:44 +00001689 if (adiff == NULL) {
1690 assert(!aval);
Tim Peters3918fb22001-05-10 18:58:31 +00001691 /* Either an error, or a is a subset with the same length so
Tim Peters95bf9392001-05-10 08:32:44 +00001692 * must be equal.
1693 */
1694 res = PyErr_Occurred() ? -1 : 0;
1695 goto Finished;
1696 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001697 bdiff = characterize(b, a, &bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001698 if (bdiff == NULL && PyErr_Occurred()) {
1699 assert(!bval);
1700 res = -1;
1701 goto Finished;
1702 }
1703 res = 0;
1704 if (bdiff) {
1705 /* bdiff == NULL "should be" impossible now, but perhaps
1706 * the last comparison done by the characterize() on a had
1707 * the side effect of making the dicts equal!
1708 */
1709 res = PyObject_Compare(adiff, bdiff);
1710 }
1711 if (res == 0 && bval != NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001712 res = PyObject_Compare(aval, bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001713
1714Finished:
1715 Py_XDECREF(adiff);
1716 Py_XDECREF(bdiff);
1717 Py_XDECREF(aval);
1718 Py_XDECREF(bval);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001719 return res;
1720}
1721
Tim Peterse63415e2001-05-08 04:38:29 +00001722/* Return 1 if dicts equal, 0 if not, -1 if error.
1723 * Gets out as soon as any difference is detected.
1724 * Uses only Py_EQ comparison.
1725 */
1726static int
Brett Cannon77ae87c2007-10-10 00:07:50 +00001727dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00001728{
Tim Peters9b10f7e2006-05-30 04:16:25 +00001729 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00001730
1731 if (a->ma_used != b->ma_used)
1732 /* can't be equal if # of entries differ */
1733 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001734
Tim Peterse63415e2001-05-08 04:38:29 +00001735 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Tim Petersafb6ae82001-06-04 21:00:21 +00001736 for (i = 0; i <= a->ma_mask; i++) {
Tim Peterse63415e2001-05-08 04:38:29 +00001737 PyObject *aval = a->ma_table[i].me_value;
1738 if (aval != NULL) {
1739 int cmp;
1740 PyObject *bval;
1741 PyObject *key = a->ma_table[i].me_key;
1742 /* temporarily bump aval's refcount to ensure it stays
1743 alive until we're done with it */
1744 Py_INCREF(aval);
Neal Norwitza22975f2006-09-05 02:24:03 +00001745 /* ditto for key */
1746 Py_INCREF(key);
Tim Peterse63415e2001-05-08 04:38:29 +00001747 bval = PyDict_GetItem((PyObject *)b, key);
Neal Norwitza22975f2006-09-05 02:24:03 +00001748 Py_DECREF(key);
Tim Peterse63415e2001-05-08 04:38:29 +00001749 if (bval == NULL) {
1750 Py_DECREF(aval);
1751 return 0;
1752 }
1753 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1754 Py_DECREF(aval);
1755 if (cmp <= 0) /* error or not equal */
1756 return cmp;
1757 }
1758 }
1759 return 1;
1760 }
1761
1762static PyObject *
1763dict_richcompare(PyObject *v, PyObject *w, int op)
1764{
1765 int cmp;
1766 PyObject *res;
1767
1768 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1769 res = Py_NotImplemented;
1770 }
1771 else if (op == Py_EQ || op == Py_NE) {
Brett Cannon77ae87c2007-10-10 00:07:50 +00001772 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
Tim Peterse63415e2001-05-08 04:38:29 +00001773 if (cmp < 0)
1774 return NULL;
1775 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1776 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001777 else
1778 res = Py_NotImplemented;
Tim Peterse63415e2001-05-08 04:38:29 +00001779 Py_INCREF(res);
1780 return res;
1781 }
1782
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001783static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001784dict_contains(register PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001785{
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001786 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001787 PyDictEntry *ep;
Tim Petersd770ebd2006-06-01 15:50:44 +00001788
Tim Peters0ab085c2001-09-14 00:25:33 +00001789 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001790 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001791 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001792 if (hash == -1)
1793 return NULL;
1794 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001795 ep = (mp->ma_lookup)(mp, key, hash);
1796 if (ep == NULL)
1797 return NULL;
Tim Petersd770ebd2006-06-01 15:50:44 +00001798 return PyBool_FromLong(ep->me_value != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001799}
1800
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001801static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001802dict_has_key(register PyDictObject *mp, PyObject *key)
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +00001803{
1804 if (Py_Py3kWarningFlag &&
1805 PyErr_Warn(PyExc_DeprecationWarning,
1806 "dict.has_key() not supported in 3.x") < 0)
1807 return NULL;
1808 return dict_contains(mp, key);
1809}
1810
1811static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001812dict_get(register PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001813{
1814 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001815 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001816 PyObject *val = NULL;
1817 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001818 PyDictEntry *ep;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001819
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001820 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001821 return NULL;
1822
Tim Peters0ab085c2001-09-14 00:25:33 +00001823 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001824 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Barry Warsawc38c5da1997-10-06 17:49:20 +00001825 hash = PyObject_Hash(key);
1826 if (hash == -1)
1827 return NULL;
1828 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001829 ep = (mp->ma_lookup)(mp, key, hash);
1830 if (ep == NULL)
1831 return NULL;
1832 val = ep->me_value;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001833 if (val == NULL)
1834 val = failobj;
1835 Py_INCREF(val);
1836 return val;
1837}
1838
1839
1840static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001841dict_setdefault(register PyDictObject *mp, PyObject *args)
Guido van Rossum164452c2000-08-08 16:12:54 +00001842{
1843 PyObject *key;
1844 PyObject *failobj = Py_None;
1845 PyObject *val = NULL;
1846 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001847 PyDictEntry *ep;
Guido van Rossum164452c2000-08-08 16:12:54 +00001848
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001849 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
Guido van Rossum164452c2000-08-08 16:12:54 +00001850 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001851
Tim Peters0ab085c2001-09-14 00:25:33 +00001852 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001853 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossum164452c2000-08-08 16:12:54 +00001854 hash = PyObject_Hash(key);
1855 if (hash == -1)
1856 return NULL;
1857 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001858 ep = (mp->ma_lookup)(mp, key, hash);
1859 if (ep == NULL)
1860 return NULL;
1861 val = ep->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001862 if (val == NULL) {
1863 val = failobj;
1864 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1865 val = NULL;
1866 }
1867 Py_XINCREF(val);
1868 return val;
1869}
1870
1871
1872static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001873dict_clear(register PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001874{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001875 PyDict_Clear((PyObject *)mp);
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001876 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001877}
1878
Guido van Rossumba6ab842000-12-12 22:02:18 +00001879static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001880dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00001881{
1882 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001883 PyDictEntry *ep;
Guido van Rossume027d982002-04-12 15:11:59 +00001884 PyObject *old_value, *old_key;
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001885 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001886
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001887 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1888 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001889 if (mp->ma_used == 0) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001890 if (deflt) {
1891 Py_INCREF(deflt);
1892 return deflt;
1893 }
Guido van Rossume027d982002-04-12 15:11:59 +00001894 PyErr_SetString(PyExc_KeyError,
1895 "pop(): dictionary is empty");
1896 return NULL;
1897 }
1898 if (!PyString_CheckExact(key) ||
1899 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1900 hash = PyObject_Hash(key);
1901 if (hash == -1)
1902 return NULL;
1903 }
1904 ep = (mp->ma_lookup)(mp, key, hash);
Armin Rigo35f6d362006-06-01 13:19:12 +00001905 if (ep == NULL)
1906 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001907 if (ep->me_value == NULL) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001908 if (deflt) {
1909 Py_INCREF(deflt);
1910 return deflt;
1911 }
Georg Brandlb9f4ad32006-10-29 18:31:42 +00001912 set_key_error(key);
Guido van Rossume027d982002-04-12 15:11:59 +00001913 return NULL;
1914 }
1915 old_key = ep->me_key;
1916 Py_INCREF(dummy);
1917 ep->me_key = dummy;
1918 old_value = ep->me_value;
1919 ep->me_value = NULL;
1920 mp->ma_used--;
1921 Py_DECREF(old_key);
1922 return old_value;
1923}
1924
1925static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001926dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001927{
Tim Peters9b10f7e2006-05-30 04:16:25 +00001928 Py_ssize_t i = 0;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001929 PyDictEntry *ep;
Guido van Rossumba6ab842000-12-12 22:02:18 +00001930 PyObject *res;
1931
Tim Petersf4b33f62001-06-02 05:42:29 +00001932 /* Allocate the result tuple before checking the size. Believe it
1933 * or not, this allocation could trigger a garbage collection which
1934 * could empty the dict, so if we checked the size first and that
1935 * happened, the result would be an infinite loop (searching for an
1936 * entry that no longer exists). Note that the usual popitem()
1937 * idiom is "while d: k, v = d.popitem()". so needing to throw the
Tim Peters9b10f7e2006-05-30 04:16:25 +00001938 * tuple away if the dict *is* empty isn't a significant
Tim Petersf4b33f62001-06-02 05:42:29 +00001939 * inefficiency -- possible, but unlikely in practice.
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001940 */
1941 res = PyTuple_New(2);
1942 if (res == NULL)
1943 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001944 if (mp->ma_used == 0) {
1945 Py_DECREF(res);
1946 PyErr_SetString(PyExc_KeyError,
1947 "popitem(): dictionary is empty");
1948 return NULL;
1949 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001950 /* Set ep to "the first" dict entry with a value. We abuse the hash
1951 * field of slot 0 to hold a search finger:
1952 * If slot 0 has a value, use slot 0.
1953 * Else slot 0 is being used to hold a search finger,
Neal Norwitzd3881b02006-05-30 04:25:05 +00001954 * and we use its hash value as the first index to look.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001955 */
1956 ep = &mp->ma_table[0];
1957 if (ep->me_value == NULL) {
Tim Peters9b10f7e2006-05-30 04:16:25 +00001958 i = ep->me_hash;
Tim Petersdea48ec2001-05-22 20:40:22 +00001959 /* The hash field may be a real hash value, or it may be a
1960 * legit search finger, or it may be a once-legit search
1961 * finger that's out of bounds now because it wrapped around
1962 * or the table shrunk -- simply make sure it's in bounds now.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001963 */
Tim Petersafb6ae82001-06-04 21:00:21 +00001964 if (i > mp->ma_mask || i < 1)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001965 i = 1; /* skip slot 0 */
1966 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1967 i++;
Tim Petersafb6ae82001-06-04 21:00:21 +00001968 if (i > mp->ma_mask)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001969 i = 1;
1970 }
1971 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001972 PyTuple_SET_ITEM(res, 0, ep->me_key);
1973 PyTuple_SET_ITEM(res, 1, ep->me_value);
1974 Py_INCREF(dummy);
1975 ep->me_key = dummy;
1976 ep->me_value = NULL;
1977 mp->ma_used--;
1978 assert(mp->ma_table[0].me_value == NULL);
1979 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001980 return res;
1981}
1982
Jeremy Hylton8caad492000-06-23 14:18:11 +00001983static int
1984dict_traverse(PyObject *op, visitproc visit, void *arg)
1985{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001986 Py_ssize_t i = 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001987 PyObject *pk;
1988 PyObject *pv;
1989
1990 while (PyDict_Next(op, &i, &pk, &pv)) {
Thomas Woutersc6e55062006-04-15 21:47:09 +00001991 Py_VISIT(pk);
1992 Py_VISIT(pv);
Jeremy Hylton8caad492000-06-23 14:18:11 +00001993 }
1994 return 0;
1995}
1996
1997static int
1998dict_tp_clear(PyObject *op)
1999{
2000 PyDict_Clear(op);
2001 return 0;
2002}
2003
Tim Petersf7f88b12000-12-13 23:18:45 +00002004
Raymond Hettinger019a1482004-03-18 02:41:19 +00002005extern PyTypeObject PyDictIterKey_Type; /* Forward */
2006extern PyTypeObject PyDictIterValue_Type; /* Forward */
2007extern PyTypeObject PyDictIterItem_Type; /* Forward */
Brett Cannon77ae87c2007-10-10 00:07:50 +00002008static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002009
2010static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002011dict_iterkeys(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002012{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002013 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002014}
2015
2016static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002017dict_itervalues(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002018{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002019 return dictiter_new(dict, &PyDictIterValue_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002020}
2021
2022static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002023dict_iteritems(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002024{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002025 return dictiter_new(dict, &PyDictIterItem_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002026}
2027
2028
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002029PyDoc_STRVAR(has_key__doc__,
Raymond Hettinger574aa322003-09-01 22:12:08 +00002030"D.has_key(k) -> True if D has a key k, else False");
Tim Petersf7f88b12000-12-13 23:18:45 +00002031
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002032PyDoc_STRVAR(contains__doc__,
2033"D.__contains__(k) -> True if D has a key k, else False");
2034
2035PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
2036
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002037PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002038"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002039
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002040PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002041"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 +00002042
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002043PyDoc_STRVAR(pop__doc__,
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002044"D.pop(k[,d]) -> v, remove specified key and return the corresponding value\n\
2045If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00002046
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002047PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00002048"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000020492-tuple; but raise KeyError if D is empty");
Tim Petersf7f88b12000-12-13 23:18:45 +00002050
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002051PyDoc_STRVAR(keys__doc__,
2052"D.keys() -> list of D's keys");
Tim Petersf7f88b12000-12-13 23:18:45 +00002053
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002054PyDoc_STRVAR(items__doc__,
2055"D.items() -> list of D's (key, value) pairs, as 2-tuples");
Tim Petersf7f88b12000-12-13 23:18:45 +00002056
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002057PyDoc_STRVAR(values__doc__,
2058"D.values() -> list of D's values");
Tim Petersf7f88b12000-12-13 23:18:45 +00002059
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002060PyDoc_STRVAR(update__doc__,
Walter Dörwaldd70ad8a2004-05-28 20:59:21 +00002061"D.update(E, **F) -> None. Update D from E and F: for k in E: D[k] = E[k]\n\
2062(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 +00002063
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002064PyDoc_STRVAR(fromkeys__doc__,
2065"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
2066v defaults to None.");
2067
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002068PyDoc_STRVAR(clear__doc__,
2069"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002070
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002071PyDoc_STRVAR(copy__doc__,
2072"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00002073
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002074PyDoc_STRVAR(iterkeys__doc__,
2075"D.iterkeys() -> an iterator over the keys of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00002076
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002077PyDoc_STRVAR(itervalues__doc__,
2078"D.itervalues() -> an iterator over the values of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00002079
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002080PyDoc_STRVAR(iteritems__doc__,
2081"D.iteritems() -> an iterator over the (key, value) items of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00002082
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002083static PyMethodDef mapp_methods[] = {
Neal Norwitzc7926292007-05-23 06:57:35 +00002084 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002085 contains__doc__},
Raymond Hettinger0c669672003-12-13 13:31:55 +00002086 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002087 getitem__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002088 {"has_key", (PyCFunction)dict_has_key, METH_O,
Tim Petersf7f88b12000-12-13 23:18:45 +00002089 has_key__doc__},
2090 {"get", (PyCFunction)dict_get, METH_VARARGS,
2091 get__doc__},
2092 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
2093 setdefault_doc__},
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002094 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
Just van Rossuma797d812002-11-23 09:45:04 +00002095 pop__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002096 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002097 popitem__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002098 {"keys", (PyCFunction)dict_keys, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002099 keys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002100 {"items", (PyCFunction)dict_items, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002101 items__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002102 {"values", (PyCFunction)dict_values, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002103 values__doc__},
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002104 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002105 update__doc__},
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002106 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
2107 fromkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002108 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002109 clear__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002110 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002111 copy__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002112 {"iterkeys", (PyCFunction)dict_iterkeys, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00002113 iterkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002114 {"itervalues", (PyCFunction)dict_itervalues, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00002115 itervalues__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002116 {"iteritems", (PyCFunction)dict_iteritems, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00002117 iteritems__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00002118 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002119};
2120
Tim Petersd770ebd2006-06-01 15:50:44 +00002121/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002122int
2123PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002124{
2125 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +00002126 PyDictObject *mp = (PyDictObject *)op;
2127 PyDictEntry *ep;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002128
Tim Peters0ab085c2001-09-14 00:25:33 +00002129 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00002130 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002131 hash = PyObject_Hash(key);
2132 if (hash == -1)
2133 return -1;
2134 }
Armin Rigo35f6d362006-06-01 13:19:12 +00002135 ep = (mp->ma_lookup)(mp, key, hash);
Tim Petersd770ebd2006-06-01 15:50:44 +00002136 return ep == NULL ? -1 : (ep->me_value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002137}
2138
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +00002139/* Internal version of PyDict_Contains used when the hash value is already known */
2140int
2141_PyDict_Contains(PyObject *op, PyObject *key, long hash)
2142{
Brett Cannon77ae87c2007-10-10 00:07:50 +00002143 PyDictObject *mp = (PyDictObject *)op;
2144 PyDictEntry *ep;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +00002145
2146 ep = (mp->ma_lookup)(mp, key, hash);
2147 return ep == NULL ? -1 : (ep->me_value != NULL);
2148}
2149
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002150/* Hack to implement "key in dict" */
2151static PySequenceMethods dict_as_sequence = {
Georg Brandl347b3002006-03-30 11:57:00 +00002152 0, /* sq_length */
2153 0, /* sq_concat */
2154 0, /* sq_repeat */
2155 0, /* sq_item */
2156 0, /* sq_slice */
2157 0, /* sq_ass_item */
2158 0, /* sq_ass_slice */
2159 PyDict_Contains, /* sq_contains */
2160 0, /* sq_inplace_concat */
2161 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002162};
2163
Guido van Rossum09e563a2001-05-01 12:10:21 +00002164static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002165dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2166{
2167 PyObject *self;
2168
2169 assert(type != NULL && type->tp_alloc != NULL);
2170 self = type->tp_alloc(type, 0);
2171 if (self != NULL) {
2172 PyDictObject *d = (PyDictObject *)self;
2173 /* It's guaranteed that tp->alloc zeroed out the struct. */
2174 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
2175 INIT_NONZERO_DICT_SLOTS(d);
2176 d->ma_lookup = lookdict_string;
2177#ifdef SHOW_CONVERSION_COUNTS
2178 ++created;
2179#endif
2180 }
2181 return self;
2182}
2183
Tim Peters25786c02001-09-02 08:22:48 +00002184static int
2185dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2186{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002187 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002188}
2189
Tim Peters6d6c1a32001-08-02 04:15:00 +00002190static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002191dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002192{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002193 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002194}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002195
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002196PyDoc_STRVAR(dictionary_doc,
Tim Petersa427a2b2001-10-29 22:25:45 +00002197"dict() -> new empty dictionary.\n"
2198"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Tim Peters1fc240e2001-10-26 05:06:50 +00002199" (key, value) pairs.\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002200"dict(seq) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002201" d = {}\n"
2202" for k, v in seq:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002203" d[k] = v\n"
2204"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2205" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002206
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002207PyTypeObject PyDict_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002208 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Tim Petersa427a2b2001-10-29 22:25:45 +00002209 "dict",
Brett Cannon77ae87c2007-10-10 00:07:50 +00002210 sizeof(PyDictObject),
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002211 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00002212 (destructor)dict_dealloc, /* tp_dealloc */
2213 (printfunc)dict_print, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002214 0, /* tp_getattr */
Guido van Rossumb9324202001-01-18 00:39:02 +00002215 0, /* tp_setattr */
Tim Peters4fa58bf2001-05-10 21:45:19 +00002216 (cmpfunc)dict_compare, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00002217 (reprfunc)dict_repr, /* tp_repr */
2218 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002219 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00002220 &dict_as_mapping, /* tp_as_mapping */
Guido van Rossum64c06e32007-11-22 00:55:51 +00002221 0, /* tp_hash */
Guido van Rossumb9324202001-01-18 00:39:02 +00002222 0, /* tp_call */
2223 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002224 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumb9324202001-01-18 00:39:02 +00002225 0, /* tp_setattro */
2226 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002227 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Neal Norwitzee3a1b52007-02-25 19:44:48 +00002228 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
Tim Peters25786c02001-09-02 08:22:48 +00002229 dictionary_doc, /* tp_doc */
Georg Brandl347b3002006-03-30 11:57:00 +00002230 dict_traverse, /* tp_traverse */
2231 dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00002232 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002233 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00002234 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002235 0, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002236 mapp_methods, /* tp_methods */
2237 0, /* tp_members */
2238 0, /* tp_getset */
2239 0, /* tp_base */
2240 0, /* tp_dict */
2241 0, /* tp_descr_get */
2242 0, /* tp_descr_set */
2243 0, /* tp_dictoffset */
Georg Brandl347b3002006-03-30 11:57:00 +00002244 dict_init, /* tp_init */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002245 PyType_GenericAlloc, /* tp_alloc */
2246 dict_new, /* tp_new */
Neil Schemenauer6189b892002-04-12 02:43:00 +00002247 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002248};
2249
Guido van Rossum3cca2451997-05-16 14:23:33 +00002250/* For backward compatibility with old dictionary interface */
2251
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002252PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002253PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002254{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002255 PyObject *kv, *rv;
2256 kv = PyString_FromString(key);
2257 if (kv == NULL)
2258 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002259 rv = PyDict_GetItem(v, kv);
2260 Py_DECREF(kv);
2261 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002262}
2263
2264int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002265PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002266{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002267 PyObject *kv;
2268 int err;
2269 kv = PyString_FromString(key);
2270 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002271 return -1;
Guido van Rossum4f3bf1e1997-09-29 23:31:11 +00002272 PyString_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00002273 err = PyDict_SetItem(v, kv, item);
2274 Py_DECREF(kv);
2275 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002276}
2277
2278int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002279PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002280{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002281 PyObject *kv;
2282 int err;
2283 kv = PyString_FromString(key);
2284 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002285 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002286 err = PyDict_DelItem(v, kv);
2287 Py_DECREF(kv);
2288 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002289}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002290
Raymond Hettinger019a1482004-03-18 02:41:19 +00002291/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002292
2293typedef struct {
2294 PyObject_HEAD
Brett Cannon77ae87c2007-10-10 00:07:50 +00002295 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
Tim Peters9b10f7e2006-05-30 04:16:25 +00002296 Py_ssize_t di_used;
2297 Py_ssize_t di_pos;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002298 PyObject* di_result; /* reusable result tuple for iteritems */
Tim Peters9b10f7e2006-05-30 04:16:25 +00002299 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002300} dictiterobject;
2301
2302static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002303dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002304{
2305 dictiterobject *di;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002306 di = PyObject_New(dictiterobject, itertype);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002307 if (di == NULL)
2308 return NULL;
2309 Py_INCREF(dict);
2310 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00002311 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002312 di->di_pos = 0;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002313 di->len = dict->ma_used;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002314 if (itertype == &PyDictIterItem_Type) {
2315 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2316 if (di->di_result == NULL) {
2317 Py_DECREF(di);
2318 return NULL;
2319 }
2320 }
2321 else
2322 di->di_result = NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002323 return (PyObject *)di;
2324}
2325
2326static void
2327dictiter_dealloc(dictiterobject *di)
2328{
Guido van Rossum2147df72002-07-16 20:30:22 +00002329 Py_XDECREF(di->di_dict);
Raymond Hettinger019a1482004-03-18 02:41:19 +00002330 Py_XDECREF(di->di_result);
Neil Schemenauer6189b892002-04-12 02:43:00 +00002331 PyObject_Del(di);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002332}
2333
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002334static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002335dictiter_len(dictiterobject *di)
2336{
Tim Peters9b10f7e2006-05-30 04:16:25 +00002337 Py_ssize_t len = 0;
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00002338 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002339 len = di->len;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002340 return PyInt_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002341}
2342
Armin Rigof5b3e362006-02-11 21:32:43 +00002343PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002344
2345static PyMethodDef dictiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +00002346 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002347 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002348};
2349
Raymond Hettinger019a1482004-03-18 02:41:19 +00002350static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002351{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002352 PyObject *key;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002353 register Py_ssize_t i, mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +00002354 register PyDictEntry *ep;
2355 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002356
Raymond Hettinger019a1482004-03-18 02:41:19 +00002357 if (d == NULL)
Guido van Rossum2147df72002-07-16 20:30:22 +00002358 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002359 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002360
Raymond Hettinger019a1482004-03-18 02:41:19 +00002361 if (di->di_used != d->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00002362 PyErr_SetString(PyExc_RuntimeError,
2363 "dictionary changed size during iteration");
Guido van Rossum2147df72002-07-16 20:30:22 +00002364 di->di_used = -1; /* Make this state sticky */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002365 return NULL;
2366 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002367
Raymond Hettinger019a1482004-03-18 02:41:19 +00002368 i = di->di_pos;
2369 if (i < 0)
2370 goto fail;
2371 ep = d->ma_table;
2372 mask = d->ma_mask;
2373 while (i <= mask && ep[i].me_value == NULL)
2374 i++;
2375 di->di_pos = i+1;
2376 if (i > mask)
2377 goto fail;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002378 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002379 key = ep[i].me_key;
2380 Py_INCREF(key);
2381 return key;
2382
2383fail:
2384 Py_DECREF(d);
Guido van Rossum2147df72002-07-16 20:30:22 +00002385 di->di_dict = NULL;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002386 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002387}
2388
Raymond Hettinger019a1482004-03-18 02:41:19 +00002389PyTypeObject PyDictIterKey_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002390 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002391 "dictionary-keyiterator", /* tp_name */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002392 sizeof(dictiterobject), /* tp_basicsize */
2393 0, /* tp_itemsize */
2394 /* methods */
2395 (destructor)dictiter_dealloc, /* tp_dealloc */
2396 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002397 0, /* tp_getattr */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002398 0, /* tp_setattr */
2399 0, /* tp_compare */
2400 0, /* tp_repr */
2401 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002402 0, /* tp_as_sequence */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002403 0, /* tp_as_mapping */
2404 0, /* tp_hash */
2405 0, /* tp_call */
2406 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002407 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002408 0, /* tp_setattro */
2409 0, /* tp_as_buffer */
2410 Py_TPFLAGS_DEFAULT, /* tp_flags */
2411 0, /* tp_doc */
2412 0, /* tp_traverse */
2413 0, /* tp_clear */
2414 0, /* tp_richcompare */
2415 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002416 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002417 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002418 dictiter_methods, /* tp_methods */
2419 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002420};
2421
2422static PyObject *dictiter_iternextvalue(dictiterobject *di)
2423{
2424 PyObject *value;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002425 register Py_ssize_t i, mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +00002426 register PyDictEntry *ep;
2427 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002428
2429 if (d == NULL)
2430 return NULL;
2431 assert (PyDict_Check(d));
2432
2433 if (di->di_used != d->ma_used) {
2434 PyErr_SetString(PyExc_RuntimeError,
2435 "dictionary changed size during iteration");
2436 di->di_used = -1; /* Make this state sticky */
2437 return NULL;
2438 }
2439
2440 i = di->di_pos;
Guido van Rossum09240f62004-03-20 19:11:58 +00002441 mask = d->ma_mask;
2442 if (i < 0 || i > mask)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002443 goto fail;
2444 ep = d->ma_table;
Guido van Rossum09240f62004-03-20 19:11:58 +00002445 while ((value=ep[i].me_value) == NULL) {
Raymond Hettinger019a1482004-03-18 02:41:19 +00002446 i++;
Guido van Rossum09240f62004-03-20 19:11:58 +00002447 if (i > mask)
2448 goto fail;
2449 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002450 di->di_pos = i+1;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002451 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002452 Py_INCREF(value);
2453 return value;
2454
2455fail:
2456 Py_DECREF(d);
2457 di->di_dict = NULL;
2458 return NULL;
2459}
2460
2461PyTypeObject PyDictIterValue_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002462 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002463 "dictionary-valueiterator", /* tp_name */
2464 sizeof(dictiterobject), /* tp_basicsize */
2465 0, /* tp_itemsize */
2466 /* methods */
2467 (destructor)dictiter_dealloc, /* tp_dealloc */
2468 0, /* tp_print */
2469 0, /* tp_getattr */
2470 0, /* tp_setattr */
2471 0, /* tp_compare */
2472 0, /* tp_repr */
2473 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002474 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002475 0, /* tp_as_mapping */
2476 0, /* tp_hash */
2477 0, /* tp_call */
2478 0, /* tp_str */
2479 PyObject_GenericGetAttr, /* tp_getattro */
2480 0, /* tp_setattro */
2481 0, /* tp_as_buffer */
2482 Py_TPFLAGS_DEFAULT, /* tp_flags */
2483 0, /* tp_doc */
2484 0, /* tp_traverse */
2485 0, /* tp_clear */
2486 0, /* tp_richcompare */
2487 0, /* tp_weaklistoffset */
2488 PyObject_SelfIter, /* tp_iter */
2489 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002490 dictiter_methods, /* tp_methods */
2491 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002492};
2493
2494static PyObject *dictiter_iternextitem(dictiterobject *di)
2495{
2496 PyObject *key, *value, *result = di->di_result;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002497 register Py_ssize_t i, mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +00002498 register PyDictEntry *ep;
2499 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002500
2501 if (d == NULL)
2502 return NULL;
2503 assert (PyDict_Check(d));
2504
2505 if (di->di_used != d->ma_used) {
2506 PyErr_SetString(PyExc_RuntimeError,
2507 "dictionary changed size during iteration");
2508 di->di_used = -1; /* Make this state sticky */
2509 return NULL;
2510 }
2511
2512 i = di->di_pos;
2513 if (i < 0)
2514 goto fail;
2515 ep = d->ma_table;
2516 mask = d->ma_mask;
2517 while (i <= mask && ep[i].me_value == NULL)
2518 i++;
2519 di->di_pos = i+1;
2520 if (i > mask)
2521 goto fail;
2522
2523 if (result->ob_refcnt == 1) {
2524 Py_INCREF(result);
2525 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2526 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2527 } else {
2528 result = PyTuple_New(2);
Tim Peters60b29962006-01-01 01:19:23 +00002529 if (result == NULL)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002530 return NULL;
2531 }
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002532 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002533 key = ep[i].me_key;
2534 value = ep[i].me_value;
2535 Py_INCREF(key);
2536 Py_INCREF(value);
2537 PyTuple_SET_ITEM(result, 0, key);
2538 PyTuple_SET_ITEM(result, 1, value);
2539 return result;
2540
2541fail:
2542 Py_DECREF(d);
2543 di->di_dict = NULL;
2544 return NULL;
2545}
2546
2547PyTypeObject PyDictIterItem_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002548 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002549 "dictionary-itemiterator", /* tp_name */
2550 sizeof(dictiterobject), /* tp_basicsize */
2551 0, /* tp_itemsize */
2552 /* methods */
2553 (destructor)dictiter_dealloc, /* tp_dealloc */
2554 0, /* tp_print */
2555 0, /* tp_getattr */
2556 0, /* tp_setattr */
2557 0, /* tp_compare */
2558 0, /* tp_repr */
2559 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002560 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002561 0, /* tp_as_mapping */
2562 0, /* tp_hash */
2563 0, /* tp_call */
2564 0, /* tp_str */
2565 PyObject_GenericGetAttr, /* tp_getattro */
2566 0, /* tp_setattro */
2567 0, /* tp_as_buffer */
2568 Py_TPFLAGS_DEFAULT, /* tp_flags */
2569 0, /* tp_doc */
2570 0, /* tp_traverse */
2571 0, /* tp_clear */
2572 0, /* tp_richcompare */
2573 0, /* tp_weaklistoffset */
2574 PyObject_SelfIter, /* tp_iter */
2575 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002576 dictiter_methods, /* tp_methods */
2577 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002578};