blob: 165221e8d48cdda8e18fc2b75d4900c674a8b5d3 [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{
Raymond Hettinger17a74c32008-02-09 04:37:49 +00001531 if (Py_Py3kWarningFlag &&
1532 PyErr_Warn(PyExc_DeprecationWarning,
1533 "dict.copy() not supported in 3.x") < 0)
1534 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001535 return PyDict_Copy((PyObject*)mp);
1536}
1537
1538PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001539PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001540{
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001541 PyObject *copy;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001542
1543 if (o == NULL || !PyDict_Check(o)) {
1544 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001545 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001546 }
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001547 copy = PyDict_New();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001548 if (copy == NULL)
1549 return NULL;
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001550 if (PyDict_Merge(copy, o, 1) == 0)
1551 return copy;
1552 Py_DECREF(copy);
Raymond Hettinger1356f782005-04-15 15:58:42 +00001553 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001554}
1555
Martin v. Löwis18e16552006-02-15 17:27:45 +00001556Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00001557PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001558{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001559 if (mp == NULL || !PyDict_Check(mp)) {
1560 PyErr_BadInternalCall();
Jeremy Hylton7083bb72004-02-17 20:10:11 +00001561 return -1;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001562 }
Brett Cannon77ae87c2007-10-10 00:07:50 +00001563 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001564}
1565
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001566PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001567PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001568{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001569 if (mp == NULL || !PyDict_Check(mp)) {
1570 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001571 return NULL;
1572 }
Brett Cannon77ae87c2007-10-10 00:07:50 +00001573 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001574}
1575
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001576PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001577PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001578{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001579 if (mp == NULL || !PyDict_Check(mp)) {
1580 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001581 return NULL;
1582 }
Brett Cannon77ae87c2007-10-10 00:07:50 +00001583 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001584}
1585
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001586PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001587PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001588{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001589 if (mp == NULL || !PyDict_Check(mp)) {
1590 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001591 return NULL;
1592 }
Brett Cannon77ae87c2007-10-10 00:07:50 +00001593 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001594}
1595
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001596/* Subroutine which returns the smallest key in a for which b's value
1597 is different or absent. The value is returned too, through the
Tim Peters95bf9392001-05-10 08:32:44 +00001598 pval argument. Both are NULL if no key in a is found for which b's status
1599 differs. The refcounts on (and only on) non-NULL *pval and function return
1600 values must be decremented by the caller (characterize() increments them
1601 to ensure that mutating comparison and PyDict_GetItem calls can't delete
1602 them before the caller is done looking at them). */
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001603
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001604static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001605characterize(PyDictObject *a, PyDictObject *b, PyObject **pval)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001606{
Tim Peters95bf9392001-05-10 08:32:44 +00001607 PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */
1608 PyObject *aval = NULL; /* a[akey] */
Tim Peters9b10f7e2006-05-30 04:16:25 +00001609 Py_ssize_t i;
1610 int cmp;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001611
Tim Petersafb6ae82001-06-04 21:00:21 +00001612 for (i = 0; i <= a->ma_mask; i++) {
Tim Peters95bf9392001-05-10 08:32:44 +00001613 PyObject *thiskey, *thisaval, *thisbval;
1614 if (a->ma_table[i].me_value == NULL)
1615 continue;
1616 thiskey = a->ma_table[i].me_key;
1617 Py_INCREF(thiskey); /* keep alive across compares */
1618 if (akey != NULL) {
1619 cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT);
1620 if (cmp < 0) {
1621 Py_DECREF(thiskey);
1622 goto Fail;
1623 }
1624 if (cmp > 0 ||
Tim Petersafb6ae82001-06-04 21:00:21 +00001625 i > a->ma_mask ||
Tim Peters95bf9392001-05-10 08:32:44 +00001626 a->ma_table[i].me_value == NULL)
1627 {
1628 /* Not the *smallest* a key; or maybe it is
1629 * but the compare shrunk the dict so we can't
1630 * find its associated value anymore; or
1631 * maybe it is but the compare deleted the
1632 * a[thiskey] entry.
Neal Norwitzd3881b02006-05-30 04:25:05 +00001633 */
Tim Peters95bf9392001-05-10 08:32:44 +00001634 Py_DECREF(thiskey);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001635 continue;
Guido van Rossumb9324202001-01-18 00:39:02 +00001636 }
Tim Peters95bf9392001-05-10 08:32:44 +00001637 }
1638
1639 /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */
1640 thisaval = a->ma_table[i].me_value;
1641 assert(thisaval);
1642 Py_INCREF(thisaval); /* keep alive */
1643 thisbval = PyDict_GetItem((PyObject *)b, thiskey);
1644 if (thisbval == NULL)
1645 cmp = 0;
1646 else {
1647 /* both dicts have thiskey: same values? */
1648 cmp = PyObject_RichCompareBool(
1649 thisaval, thisbval, Py_EQ);
1650 if (cmp < 0) {
1651 Py_DECREF(thiskey);
1652 Py_DECREF(thisaval);
1653 goto Fail;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001654 }
1655 }
Tim Peters95bf9392001-05-10 08:32:44 +00001656 if (cmp == 0) {
1657 /* New winner. */
1658 Py_XDECREF(akey);
1659 Py_XDECREF(aval);
1660 akey = thiskey;
1661 aval = thisaval;
1662 }
1663 else {
1664 Py_DECREF(thiskey);
1665 Py_DECREF(thisaval);
1666 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001667 }
Tim Peters95bf9392001-05-10 08:32:44 +00001668 *pval = aval;
1669 return akey;
1670
1671Fail:
1672 Py_XDECREF(akey);
1673 Py_XDECREF(aval);
1674 *pval = NULL;
1675 return NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001676}
1677
1678static int
Brett Cannon77ae87c2007-10-10 00:07:50 +00001679dict_compare(PyDictObject *a, PyDictObject *b)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001680{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001681 PyObject *adiff, *bdiff, *aval, *bval;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001682 int res;
1683
1684 /* Compare lengths first */
1685 if (a->ma_used < b->ma_used)
1686 return -1; /* a is shorter */
1687 else if (a->ma_used > b->ma_used)
1688 return 1; /* b is shorter */
Tim Peters95bf9392001-05-10 08:32:44 +00001689
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001690 /* Same length -- check all keys */
Tim Peters95bf9392001-05-10 08:32:44 +00001691 bdiff = bval = NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001692 adiff = characterize(a, b, &aval);
Tim Peters95bf9392001-05-10 08:32:44 +00001693 if (adiff == NULL) {
1694 assert(!aval);
Tim Peters3918fb22001-05-10 18:58:31 +00001695 /* Either an error, or a is a subset with the same length so
Tim Peters95bf9392001-05-10 08:32:44 +00001696 * must be equal.
1697 */
1698 res = PyErr_Occurred() ? -1 : 0;
1699 goto Finished;
1700 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001701 bdiff = characterize(b, a, &bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001702 if (bdiff == NULL && PyErr_Occurred()) {
1703 assert(!bval);
1704 res = -1;
1705 goto Finished;
1706 }
1707 res = 0;
1708 if (bdiff) {
1709 /* bdiff == NULL "should be" impossible now, but perhaps
1710 * the last comparison done by the characterize() on a had
1711 * the side effect of making the dicts equal!
1712 */
1713 res = PyObject_Compare(adiff, bdiff);
1714 }
1715 if (res == 0 && bval != NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001716 res = PyObject_Compare(aval, bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001717
1718Finished:
1719 Py_XDECREF(adiff);
1720 Py_XDECREF(bdiff);
1721 Py_XDECREF(aval);
1722 Py_XDECREF(bval);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001723 return res;
1724}
1725
Tim Peterse63415e2001-05-08 04:38:29 +00001726/* Return 1 if dicts equal, 0 if not, -1 if error.
1727 * Gets out as soon as any difference is detected.
1728 * Uses only Py_EQ comparison.
1729 */
1730static int
Brett Cannon77ae87c2007-10-10 00:07:50 +00001731dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00001732{
Tim Peters9b10f7e2006-05-30 04:16:25 +00001733 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00001734
1735 if (a->ma_used != b->ma_used)
1736 /* can't be equal if # of entries differ */
1737 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001738
Tim Peterse63415e2001-05-08 04:38:29 +00001739 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Tim Petersafb6ae82001-06-04 21:00:21 +00001740 for (i = 0; i <= a->ma_mask; i++) {
Tim Peterse63415e2001-05-08 04:38:29 +00001741 PyObject *aval = a->ma_table[i].me_value;
1742 if (aval != NULL) {
1743 int cmp;
1744 PyObject *bval;
1745 PyObject *key = a->ma_table[i].me_key;
1746 /* temporarily bump aval's refcount to ensure it stays
1747 alive until we're done with it */
1748 Py_INCREF(aval);
Neal Norwitza22975f2006-09-05 02:24:03 +00001749 /* ditto for key */
1750 Py_INCREF(key);
Tim Peterse63415e2001-05-08 04:38:29 +00001751 bval = PyDict_GetItem((PyObject *)b, key);
Neal Norwitza22975f2006-09-05 02:24:03 +00001752 Py_DECREF(key);
Tim Peterse63415e2001-05-08 04:38:29 +00001753 if (bval == NULL) {
1754 Py_DECREF(aval);
1755 return 0;
1756 }
1757 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1758 Py_DECREF(aval);
1759 if (cmp <= 0) /* error or not equal */
1760 return cmp;
1761 }
1762 }
1763 return 1;
1764 }
1765
1766static PyObject *
1767dict_richcompare(PyObject *v, PyObject *w, int op)
1768{
1769 int cmp;
1770 PyObject *res;
1771
1772 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1773 res = Py_NotImplemented;
1774 }
1775 else if (op == Py_EQ || op == Py_NE) {
Brett Cannon77ae87c2007-10-10 00:07:50 +00001776 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
Tim Peterse63415e2001-05-08 04:38:29 +00001777 if (cmp < 0)
1778 return NULL;
1779 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1780 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001781 else
1782 res = Py_NotImplemented;
Tim Peterse63415e2001-05-08 04:38:29 +00001783 Py_INCREF(res);
1784 return res;
1785 }
1786
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001787static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001788dict_contains(register PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001789{
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001790 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001791 PyDictEntry *ep;
Tim Petersd770ebd2006-06-01 15:50:44 +00001792
Tim Peters0ab085c2001-09-14 00:25:33 +00001793 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001794 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001795 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001796 if (hash == -1)
1797 return NULL;
1798 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001799 ep = (mp->ma_lookup)(mp, key, hash);
1800 if (ep == NULL)
1801 return NULL;
Tim Petersd770ebd2006-06-01 15:50:44 +00001802 return PyBool_FromLong(ep->me_value != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001803}
1804
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001805static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001806dict_has_key(register PyDictObject *mp, PyObject *key)
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +00001807{
1808 if (Py_Py3kWarningFlag &&
1809 PyErr_Warn(PyExc_DeprecationWarning,
1810 "dict.has_key() not supported in 3.x") < 0)
1811 return NULL;
1812 return dict_contains(mp, key);
1813}
1814
1815static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001816dict_get(register PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001817{
1818 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001819 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001820 PyObject *val = NULL;
1821 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001822 PyDictEntry *ep;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001823
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001824 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001825 return NULL;
1826
Tim Peters0ab085c2001-09-14 00:25:33 +00001827 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001828 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Barry Warsawc38c5da1997-10-06 17:49:20 +00001829 hash = PyObject_Hash(key);
1830 if (hash == -1)
1831 return NULL;
1832 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001833 ep = (mp->ma_lookup)(mp, key, hash);
1834 if (ep == NULL)
1835 return NULL;
1836 val = ep->me_value;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001837 if (val == NULL)
1838 val = failobj;
1839 Py_INCREF(val);
1840 return val;
1841}
1842
1843
1844static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001845dict_setdefault(register PyDictObject *mp, PyObject *args)
Guido van Rossum164452c2000-08-08 16:12:54 +00001846{
1847 PyObject *key;
1848 PyObject *failobj = Py_None;
1849 PyObject *val = NULL;
1850 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001851 PyDictEntry *ep;
Guido van Rossum164452c2000-08-08 16:12:54 +00001852
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001853 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
Guido van Rossum164452c2000-08-08 16:12:54 +00001854 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001855
Tim Peters0ab085c2001-09-14 00:25:33 +00001856 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001857 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossum164452c2000-08-08 16:12:54 +00001858 hash = PyObject_Hash(key);
1859 if (hash == -1)
1860 return NULL;
1861 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001862 ep = (mp->ma_lookup)(mp, key, hash);
1863 if (ep == NULL)
1864 return NULL;
1865 val = ep->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001866 if (val == NULL) {
1867 val = failobj;
1868 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1869 val = NULL;
1870 }
1871 Py_XINCREF(val);
1872 return val;
1873}
1874
1875
1876static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001877dict_clear(register PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001878{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001879 PyDict_Clear((PyObject *)mp);
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001880 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001881}
1882
Guido van Rossumba6ab842000-12-12 22:02:18 +00001883static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001884dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00001885{
1886 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001887 PyDictEntry *ep;
Guido van Rossume027d982002-04-12 15:11:59 +00001888 PyObject *old_value, *old_key;
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001889 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001890
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001891 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1892 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001893 if (mp->ma_used == 0) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001894 if (deflt) {
1895 Py_INCREF(deflt);
1896 return deflt;
1897 }
Guido van Rossume027d982002-04-12 15:11:59 +00001898 PyErr_SetString(PyExc_KeyError,
1899 "pop(): dictionary is empty");
1900 return NULL;
1901 }
1902 if (!PyString_CheckExact(key) ||
1903 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1904 hash = PyObject_Hash(key);
1905 if (hash == -1)
1906 return NULL;
1907 }
1908 ep = (mp->ma_lookup)(mp, key, hash);
Armin Rigo35f6d362006-06-01 13:19:12 +00001909 if (ep == NULL)
1910 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001911 if (ep->me_value == NULL) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001912 if (deflt) {
1913 Py_INCREF(deflt);
1914 return deflt;
1915 }
Georg Brandlb9f4ad32006-10-29 18:31:42 +00001916 set_key_error(key);
Guido van Rossume027d982002-04-12 15:11:59 +00001917 return NULL;
1918 }
1919 old_key = ep->me_key;
1920 Py_INCREF(dummy);
1921 ep->me_key = dummy;
1922 old_value = ep->me_value;
1923 ep->me_value = NULL;
1924 mp->ma_used--;
1925 Py_DECREF(old_key);
1926 return old_value;
1927}
1928
1929static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001930dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001931{
Tim Peters9b10f7e2006-05-30 04:16:25 +00001932 Py_ssize_t i = 0;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001933 PyDictEntry *ep;
Guido van Rossumba6ab842000-12-12 22:02:18 +00001934 PyObject *res;
1935
Tim Petersf4b33f62001-06-02 05:42:29 +00001936 /* Allocate the result tuple before checking the size. Believe it
1937 * or not, this allocation could trigger a garbage collection which
1938 * could empty the dict, so if we checked the size first and that
1939 * happened, the result would be an infinite loop (searching for an
1940 * entry that no longer exists). Note that the usual popitem()
1941 * idiom is "while d: k, v = d.popitem()". so needing to throw the
Tim Peters9b10f7e2006-05-30 04:16:25 +00001942 * tuple away if the dict *is* empty isn't a significant
Tim Petersf4b33f62001-06-02 05:42:29 +00001943 * inefficiency -- possible, but unlikely in practice.
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001944 */
1945 res = PyTuple_New(2);
1946 if (res == NULL)
1947 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001948 if (mp->ma_used == 0) {
1949 Py_DECREF(res);
1950 PyErr_SetString(PyExc_KeyError,
1951 "popitem(): dictionary is empty");
1952 return NULL;
1953 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001954 /* Set ep to "the first" dict entry with a value. We abuse the hash
1955 * field of slot 0 to hold a search finger:
1956 * If slot 0 has a value, use slot 0.
1957 * Else slot 0 is being used to hold a search finger,
Neal Norwitzd3881b02006-05-30 04:25:05 +00001958 * and we use its hash value as the first index to look.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001959 */
1960 ep = &mp->ma_table[0];
1961 if (ep->me_value == NULL) {
Tim Peters9b10f7e2006-05-30 04:16:25 +00001962 i = ep->me_hash;
Tim Petersdea48ec2001-05-22 20:40:22 +00001963 /* The hash field may be a real hash value, or it may be a
1964 * legit search finger, or it may be a once-legit search
1965 * finger that's out of bounds now because it wrapped around
1966 * or the table shrunk -- simply make sure it's in bounds now.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001967 */
Tim Petersafb6ae82001-06-04 21:00:21 +00001968 if (i > mp->ma_mask || i < 1)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001969 i = 1; /* skip slot 0 */
1970 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1971 i++;
Tim Petersafb6ae82001-06-04 21:00:21 +00001972 if (i > mp->ma_mask)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001973 i = 1;
1974 }
1975 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001976 PyTuple_SET_ITEM(res, 0, ep->me_key);
1977 PyTuple_SET_ITEM(res, 1, ep->me_value);
1978 Py_INCREF(dummy);
1979 ep->me_key = dummy;
1980 ep->me_value = NULL;
1981 mp->ma_used--;
1982 assert(mp->ma_table[0].me_value == NULL);
1983 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001984 return res;
1985}
1986
Jeremy Hylton8caad492000-06-23 14:18:11 +00001987static int
1988dict_traverse(PyObject *op, visitproc visit, void *arg)
1989{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001990 Py_ssize_t i = 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001991 PyObject *pk;
1992 PyObject *pv;
1993
1994 while (PyDict_Next(op, &i, &pk, &pv)) {
Thomas Woutersc6e55062006-04-15 21:47:09 +00001995 Py_VISIT(pk);
1996 Py_VISIT(pv);
Jeremy Hylton8caad492000-06-23 14:18:11 +00001997 }
1998 return 0;
1999}
2000
2001static int
2002dict_tp_clear(PyObject *op)
2003{
2004 PyDict_Clear(op);
2005 return 0;
2006}
2007
Tim Petersf7f88b12000-12-13 23:18:45 +00002008
Raymond Hettinger019a1482004-03-18 02:41:19 +00002009extern PyTypeObject PyDictIterKey_Type; /* Forward */
2010extern PyTypeObject PyDictIterValue_Type; /* Forward */
2011extern PyTypeObject PyDictIterItem_Type; /* Forward */
Brett Cannon77ae87c2007-10-10 00:07:50 +00002012static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002013
2014static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002015dict_iterkeys(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002016{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002017 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002018}
2019
2020static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002021dict_itervalues(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002022{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002023 return dictiter_new(dict, &PyDictIterValue_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002024}
2025
2026static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002027dict_iteritems(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002028{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002029 return dictiter_new(dict, &PyDictIterItem_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002030}
2031
2032
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002033PyDoc_STRVAR(has_key__doc__,
Raymond Hettinger574aa322003-09-01 22:12:08 +00002034"D.has_key(k) -> True if D has a key k, else False");
Tim Petersf7f88b12000-12-13 23:18:45 +00002035
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002036PyDoc_STRVAR(contains__doc__,
2037"D.__contains__(k) -> True if D has a key k, else False");
2038
2039PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
2040
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002041PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002042"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002043
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002044PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002045"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 +00002046
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002047PyDoc_STRVAR(pop__doc__,
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002048"D.pop(k[,d]) -> v, remove specified key and return the corresponding value\n\
2049If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00002050
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002051PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00002052"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000020532-tuple; but raise KeyError if D is empty");
Tim Petersf7f88b12000-12-13 23:18:45 +00002054
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002055PyDoc_STRVAR(keys__doc__,
2056"D.keys() -> list of D's keys");
Tim Petersf7f88b12000-12-13 23:18:45 +00002057
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002058PyDoc_STRVAR(items__doc__,
2059"D.items() -> list of D's (key, value) pairs, as 2-tuples");
Tim Petersf7f88b12000-12-13 23:18:45 +00002060
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002061PyDoc_STRVAR(values__doc__,
2062"D.values() -> list of D's values");
Tim Petersf7f88b12000-12-13 23:18:45 +00002063
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002064PyDoc_STRVAR(update__doc__,
Walter Dörwaldd70ad8a2004-05-28 20:59:21 +00002065"D.update(E, **F) -> None. Update D from E and F: for k in E: D[k] = E[k]\n\
2066(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 +00002067
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002068PyDoc_STRVAR(fromkeys__doc__,
2069"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
2070v defaults to None.");
2071
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002072PyDoc_STRVAR(clear__doc__,
2073"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002074
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002075PyDoc_STRVAR(copy__doc__,
2076"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00002077
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002078PyDoc_STRVAR(iterkeys__doc__,
2079"D.iterkeys() -> an iterator over the keys of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00002080
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002081PyDoc_STRVAR(itervalues__doc__,
2082"D.itervalues() -> an iterator over the values of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00002083
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002084PyDoc_STRVAR(iteritems__doc__,
2085"D.iteritems() -> an iterator over the (key, value) items of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00002086
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002087static PyMethodDef mapp_methods[] = {
Neal Norwitzc7926292007-05-23 06:57:35 +00002088 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002089 contains__doc__},
Raymond Hettinger0c669672003-12-13 13:31:55 +00002090 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002091 getitem__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002092 {"has_key", (PyCFunction)dict_has_key, METH_O,
Tim Petersf7f88b12000-12-13 23:18:45 +00002093 has_key__doc__},
2094 {"get", (PyCFunction)dict_get, METH_VARARGS,
2095 get__doc__},
2096 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
2097 setdefault_doc__},
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002098 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
Just van Rossuma797d812002-11-23 09:45:04 +00002099 pop__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002100 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002101 popitem__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002102 {"keys", (PyCFunction)dict_keys, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002103 keys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002104 {"items", (PyCFunction)dict_items, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002105 items__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002106 {"values", (PyCFunction)dict_values, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002107 values__doc__},
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002108 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002109 update__doc__},
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002110 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
2111 fromkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002112 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002113 clear__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002114 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002115 copy__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002116 {"iterkeys", (PyCFunction)dict_iterkeys, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00002117 iterkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002118 {"itervalues", (PyCFunction)dict_itervalues, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00002119 itervalues__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002120 {"iteritems", (PyCFunction)dict_iteritems, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00002121 iteritems__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00002122 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002123};
2124
Tim Petersd770ebd2006-06-01 15:50:44 +00002125/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002126int
2127PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002128{
2129 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +00002130 PyDictObject *mp = (PyDictObject *)op;
2131 PyDictEntry *ep;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002132
Tim Peters0ab085c2001-09-14 00:25:33 +00002133 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00002134 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002135 hash = PyObject_Hash(key);
2136 if (hash == -1)
2137 return -1;
2138 }
Armin Rigo35f6d362006-06-01 13:19:12 +00002139 ep = (mp->ma_lookup)(mp, key, hash);
Tim Petersd770ebd2006-06-01 15:50:44 +00002140 return ep == NULL ? -1 : (ep->me_value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002141}
2142
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +00002143/* Internal version of PyDict_Contains used when the hash value is already known */
2144int
2145_PyDict_Contains(PyObject *op, PyObject *key, long hash)
2146{
Brett Cannon77ae87c2007-10-10 00:07:50 +00002147 PyDictObject *mp = (PyDictObject *)op;
2148 PyDictEntry *ep;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +00002149
2150 ep = (mp->ma_lookup)(mp, key, hash);
2151 return ep == NULL ? -1 : (ep->me_value != NULL);
2152}
2153
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002154/* Hack to implement "key in dict" */
2155static PySequenceMethods dict_as_sequence = {
Georg Brandl347b3002006-03-30 11:57:00 +00002156 0, /* sq_length */
2157 0, /* sq_concat */
2158 0, /* sq_repeat */
2159 0, /* sq_item */
2160 0, /* sq_slice */
2161 0, /* sq_ass_item */
2162 0, /* sq_ass_slice */
2163 PyDict_Contains, /* sq_contains */
2164 0, /* sq_inplace_concat */
2165 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002166};
2167
Guido van Rossum09e563a2001-05-01 12:10:21 +00002168static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002169dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2170{
2171 PyObject *self;
2172
2173 assert(type != NULL && type->tp_alloc != NULL);
2174 self = type->tp_alloc(type, 0);
2175 if (self != NULL) {
2176 PyDictObject *d = (PyDictObject *)self;
2177 /* It's guaranteed that tp->alloc zeroed out the struct. */
2178 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
2179 INIT_NONZERO_DICT_SLOTS(d);
2180 d->ma_lookup = lookdict_string;
2181#ifdef SHOW_CONVERSION_COUNTS
2182 ++created;
2183#endif
2184 }
2185 return self;
2186}
2187
Tim Peters25786c02001-09-02 08:22:48 +00002188static int
2189dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2190{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002191 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002192}
2193
Tim Peters6d6c1a32001-08-02 04:15:00 +00002194static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002195dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002196{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002197 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002198}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002199
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002200PyDoc_STRVAR(dictionary_doc,
Tim Petersa427a2b2001-10-29 22:25:45 +00002201"dict() -> new empty dictionary.\n"
2202"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Tim Peters1fc240e2001-10-26 05:06:50 +00002203" (key, value) pairs.\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002204"dict(seq) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002205" d = {}\n"
2206" for k, v in seq:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002207" d[k] = v\n"
2208"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2209" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002210
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002211PyTypeObject PyDict_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002212 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Tim Petersa427a2b2001-10-29 22:25:45 +00002213 "dict",
Brett Cannon77ae87c2007-10-10 00:07:50 +00002214 sizeof(PyDictObject),
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002215 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00002216 (destructor)dict_dealloc, /* tp_dealloc */
2217 (printfunc)dict_print, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002218 0, /* tp_getattr */
Guido van Rossumb9324202001-01-18 00:39:02 +00002219 0, /* tp_setattr */
Tim Peters4fa58bf2001-05-10 21:45:19 +00002220 (cmpfunc)dict_compare, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00002221 (reprfunc)dict_repr, /* tp_repr */
2222 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002223 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00002224 &dict_as_mapping, /* tp_as_mapping */
Guido van Rossum64c06e32007-11-22 00:55:51 +00002225 0, /* tp_hash */
Guido van Rossumb9324202001-01-18 00:39:02 +00002226 0, /* tp_call */
2227 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002228 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumb9324202001-01-18 00:39:02 +00002229 0, /* tp_setattro */
2230 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002231 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Neal Norwitzee3a1b52007-02-25 19:44:48 +00002232 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
Tim Peters25786c02001-09-02 08:22:48 +00002233 dictionary_doc, /* tp_doc */
Georg Brandl347b3002006-03-30 11:57:00 +00002234 dict_traverse, /* tp_traverse */
2235 dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00002236 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002237 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00002238 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002239 0, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002240 mapp_methods, /* tp_methods */
2241 0, /* tp_members */
2242 0, /* tp_getset */
2243 0, /* tp_base */
2244 0, /* tp_dict */
2245 0, /* tp_descr_get */
2246 0, /* tp_descr_set */
2247 0, /* tp_dictoffset */
Georg Brandl347b3002006-03-30 11:57:00 +00002248 dict_init, /* tp_init */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002249 PyType_GenericAlloc, /* tp_alloc */
2250 dict_new, /* tp_new */
Neil Schemenauer6189b892002-04-12 02:43:00 +00002251 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002252};
2253
Guido van Rossum3cca2451997-05-16 14:23:33 +00002254/* For backward compatibility with old dictionary interface */
2255
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002256PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002257PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002258{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002259 PyObject *kv, *rv;
2260 kv = PyString_FromString(key);
2261 if (kv == NULL)
2262 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002263 rv = PyDict_GetItem(v, kv);
2264 Py_DECREF(kv);
2265 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002266}
2267
2268int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002269PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002270{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002271 PyObject *kv;
2272 int err;
2273 kv = PyString_FromString(key);
2274 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002275 return -1;
Guido van Rossum4f3bf1e1997-09-29 23:31:11 +00002276 PyString_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00002277 err = PyDict_SetItem(v, kv, item);
2278 Py_DECREF(kv);
2279 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002280}
2281
2282int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002283PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002284{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002285 PyObject *kv;
2286 int err;
2287 kv = PyString_FromString(key);
2288 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002289 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002290 err = PyDict_DelItem(v, kv);
2291 Py_DECREF(kv);
2292 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002293}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002294
Raymond Hettinger019a1482004-03-18 02:41:19 +00002295/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002296
2297typedef struct {
2298 PyObject_HEAD
Brett Cannon77ae87c2007-10-10 00:07:50 +00002299 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
Tim Peters9b10f7e2006-05-30 04:16:25 +00002300 Py_ssize_t di_used;
2301 Py_ssize_t di_pos;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002302 PyObject* di_result; /* reusable result tuple for iteritems */
Tim Peters9b10f7e2006-05-30 04:16:25 +00002303 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002304} dictiterobject;
2305
2306static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002307dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002308{
2309 dictiterobject *di;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002310 di = PyObject_New(dictiterobject, itertype);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002311 if (di == NULL)
2312 return NULL;
2313 Py_INCREF(dict);
2314 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00002315 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002316 di->di_pos = 0;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002317 di->len = dict->ma_used;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002318 if (itertype == &PyDictIterItem_Type) {
2319 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2320 if (di->di_result == NULL) {
2321 Py_DECREF(di);
2322 return NULL;
2323 }
2324 }
2325 else
2326 di->di_result = NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002327 return (PyObject *)di;
2328}
2329
2330static void
2331dictiter_dealloc(dictiterobject *di)
2332{
Guido van Rossum2147df72002-07-16 20:30:22 +00002333 Py_XDECREF(di->di_dict);
Raymond Hettinger019a1482004-03-18 02:41:19 +00002334 Py_XDECREF(di->di_result);
Neil Schemenauer6189b892002-04-12 02:43:00 +00002335 PyObject_Del(di);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002336}
2337
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002338static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002339dictiter_len(dictiterobject *di)
2340{
Tim Peters9b10f7e2006-05-30 04:16:25 +00002341 Py_ssize_t len = 0;
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00002342 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002343 len = di->len;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002344 return PyInt_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002345}
2346
Armin Rigof5b3e362006-02-11 21:32:43 +00002347PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002348
2349static PyMethodDef dictiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +00002350 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002351 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002352};
2353
Raymond Hettinger019a1482004-03-18 02:41:19 +00002354static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002355{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002356 PyObject *key;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002357 register Py_ssize_t i, mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +00002358 register PyDictEntry *ep;
2359 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002360
Raymond Hettinger019a1482004-03-18 02:41:19 +00002361 if (d == NULL)
Guido van Rossum2147df72002-07-16 20:30:22 +00002362 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002363 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002364
Raymond Hettinger019a1482004-03-18 02:41:19 +00002365 if (di->di_used != d->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00002366 PyErr_SetString(PyExc_RuntimeError,
2367 "dictionary changed size during iteration");
Guido van Rossum2147df72002-07-16 20:30:22 +00002368 di->di_used = -1; /* Make this state sticky */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002369 return NULL;
2370 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002371
Raymond Hettinger019a1482004-03-18 02:41:19 +00002372 i = di->di_pos;
2373 if (i < 0)
2374 goto fail;
2375 ep = d->ma_table;
2376 mask = d->ma_mask;
2377 while (i <= mask && ep[i].me_value == NULL)
2378 i++;
2379 di->di_pos = i+1;
2380 if (i > mask)
2381 goto fail;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002382 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002383 key = ep[i].me_key;
2384 Py_INCREF(key);
2385 return key;
2386
2387fail:
2388 Py_DECREF(d);
Guido van Rossum2147df72002-07-16 20:30:22 +00002389 di->di_dict = NULL;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002390 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002391}
2392
Raymond Hettinger019a1482004-03-18 02:41:19 +00002393PyTypeObject PyDictIterKey_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002394 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002395 "dictionary-keyiterator", /* tp_name */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002396 sizeof(dictiterobject), /* tp_basicsize */
2397 0, /* tp_itemsize */
2398 /* methods */
2399 (destructor)dictiter_dealloc, /* tp_dealloc */
2400 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002401 0, /* tp_getattr */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002402 0, /* tp_setattr */
2403 0, /* tp_compare */
2404 0, /* tp_repr */
2405 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002406 0, /* tp_as_sequence */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002407 0, /* tp_as_mapping */
2408 0, /* tp_hash */
2409 0, /* tp_call */
2410 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002411 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002412 0, /* tp_setattro */
2413 0, /* tp_as_buffer */
2414 Py_TPFLAGS_DEFAULT, /* tp_flags */
2415 0, /* tp_doc */
2416 0, /* tp_traverse */
2417 0, /* tp_clear */
2418 0, /* tp_richcompare */
2419 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002420 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002421 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002422 dictiter_methods, /* tp_methods */
2423 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002424};
2425
2426static PyObject *dictiter_iternextvalue(dictiterobject *di)
2427{
2428 PyObject *value;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002429 register Py_ssize_t i, mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +00002430 register PyDictEntry *ep;
2431 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002432
2433 if (d == NULL)
2434 return NULL;
2435 assert (PyDict_Check(d));
2436
2437 if (di->di_used != d->ma_used) {
2438 PyErr_SetString(PyExc_RuntimeError,
2439 "dictionary changed size during iteration");
2440 di->di_used = -1; /* Make this state sticky */
2441 return NULL;
2442 }
2443
2444 i = di->di_pos;
Guido van Rossum09240f62004-03-20 19:11:58 +00002445 mask = d->ma_mask;
2446 if (i < 0 || i > mask)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002447 goto fail;
2448 ep = d->ma_table;
Guido van Rossum09240f62004-03-20 19:11:58 +00002449 while ((value=ep[i].me_value) == NULL) {
Raymond Hettinger019a1482004-03-18 02:41:19 +00002450 i++;
Guido van Rossum09240f62004-03-20 19:11:58 +00002451 if (i > mask)
2452 goto fail;
2453 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002454 di->di_pos = i+1;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002455 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002456 Py_INCREF(value);
2457 return value;
2458
2459fail:
2460 Py_DECREF(d);
2461 di->di_dict = NULL;
2462 return NULL;
2463}
2464
2465PyTypeObject PyDictIterValue_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002466 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002467 "dictionary-valueiterator", /* tp_name */
2468 sizeof(dictiterobject), /* tp_basicsize */
2469 0, /* tp_itemsize */
2470 /* methods */
2471 (destructor)dictiter_dealloc, /* tp_dealloc */
2472 0, /* tp_print */
2473 0, /* tp_getattr */
2474 0, /* tp_setattr */
2475 0, /* tp_compare */
2476 0, /* tp_repr */
2477 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002478 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002479 0, /* tp_as_mapping */
2480 0, /* tp_hash */
2481 0, /* tp_call */
2482 0, /* tp_str */
2483 PyObject_GenericGetAttr, /* tp_getattro */
2484 0, /* tp_setattro */
2485 0, /* tp_as_buffer */
2486 Py_TPFLAGS_DEFAULT, /* tp_flags */
2487 0, /* tp_doc */
2488 0, /* tp_traverse */
2489 0, /* tp_clear */
2490 0, /* tp_richcompare */
2491 0, /* tp_weaklistoffset */
2492 PyObject_SelfIter, /* tp_iter */
2493 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002494 dictiter_methods, /* tp_methods */
2495 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002496};
2497
2498static PyObject *dictiter_iternextitem(dictiterobject *di)
2499{
2500 PyObject *key, *value, *result = di->di_result;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002501 register Py_ssize_t i, mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +00002502 register PyDictEntry *ep;
2503 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002504
2505 if (d == NULL)
2506 return NULL;
2507 assert (PyDict_Check(d));
2508
2509 if (di->di_used != d->ma_used) {
2510 PyErr_SetString(PyExc_RuntimeError,
2511 "dictionary changed size during iteration");
2512 di->di_used = -1; /* Make this state sticky */
2513 return NULL;
2514 }
2515
2516 i = di->di_pos;
2517 if (i < 0)
2518 goto fail;
2519 ep = d->ma_table;
2520 mask = d->ma_mask;
2521 while (i <= mask && ep[i].me_value == NULL)
2522 i++;
2523 di->di_pos = i+1;
2524 if (i > mask)
2525 goto fail;
2526
2527 if (result->ob_refcnt == 1) {
2528 Py_INCREF(result);
2529 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2530 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2531 } else {
2532 result = PyTuple_New(2);
Tim Peters60b29962006-01-01 01:19:23 +00002533 if (result == NULL)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002534 return NULL;
2535 }
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002536 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002537 key = ep[i].me_key;
2538 value = ep[i].me_value;
2539 Py_INCREF(key);
2540 Py_INCREF(value);
2541 PyTuple_SET_ITEM(result, 0, key);
2542 PyTuple_SET_ITEM(result, 1, value);
2543 return result;
2544
2545fail:
2546 Py_DECREF(d);
2547 di->di_dict = NULL;
2548 return NULL;
2549}
2550
2551PyTypeObject PyDictIterItem_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002552 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002553 "dictionary-itemiterator", /* tp_name */
2554 sizeof(dictiterobject), /* tp_basicsize */
2555 0, /* tp_itemsize */
2556 /* methods */
2557 (destructor)dictiter_dealloc, /* tp_dealloc */
2558 0, /* tp_print */
2559 0, /* tp_getattr */
2560 0, /* tp_setattr */
2561 0, /* tp_compare */
2562 0, /* tp_repr */
2563 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002564 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002565 0, /* tp_as_mapping */
2566 0, /* tp_hash */
2567 0, /* tp_call */
2568 0, /* tp_str */
2569 PyObject_GenericGetAttr, /* tp_getattro */
2570 0, /* tp_setattro */
2571 0, /* tp_as_buffer */
2572 Py_TPFLAGS_DEFAULT, /* tp_flags */
2573 0, /* tp_doc */
2574 0, /* tp_traverse */
2575 0, /* tp_clear */
2576 0, /* tp_richcompare */
2577 0, /* tp_weaklistoffset */
2578 PyObject_SelfIter, /* tp_iter */
2579 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002580 dictiter_methods, /* tp_methods */
2581 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002582};