blob: cc083f171674134d713753b81ab9c321c0fee326 [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{
Christian Heimes09bde042008-02-24 12:26:16 +0000174 fprintf(stderr, "Dict allocations: %" PY_FORMAT_SIZE_T "d\n",
175 count_alloc);
176 fprintf(stderr, "Dict reuse through freelist: %" PY_FORMAT_SIZE_T
177 "d\n", count_reuse);
Christian Heimesb4ee4a12008-02-07 17:15:30 +0000178 fprintf(stderr, "%.2f%% reuse rate\n\n",
179 (100.0*count_reuse/(count_alloc+count_reuse)));
180}
181#endif
182
Tim Peters6d6c1a32001-08-02 04:15:00 +0000183/* Initialization macros.
184 There are two ways to create a dict: PyDict_New() is the main C API
185 function, and the tp_new slot maps to dict_new(). In the latter case we
186 can save a little time over what PyDict_New does because it's guaranteed
187 that the PyDictObject struct is already zeroed out.
188 Everyone except dict_new() should use EMPTY_TO_MINSIZE (unless they have
189 an excellent reason not to).
190*/
191
192#define INIT_NONZERO_DICT_SLOTS(mp) do { \
Tim Petersdea48ec2001-05-22 20:40:22 +0000193 (mp)->ma_table = (mp)->ma_smalltable; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000194 (mp)->ma_mask = PyDict_MINSIZE - 1; \
195 } while(0)
196
197#define EMPTY_TO_MINSIZE(mp) do { \
198 memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000199 (mp)->ma_used = (mp)->ma_fill = 0; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000200 INIT_NONZERO_DICT_SLOTS(mp); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000201 } while(0)
202
Raymond Hettinger43442782004-03-17 21:55:03 +0000203/* Dictionary reuse scheme to save calls to malloc, free, and memset */
Christian Heimes5b970ad2008-02-06 13:33:44 +0000204#ifndef PyDict_MAXFREELIST
205#define PyDict_MAXFREELIST 80
206#endif
207static PyDictObject *free_list[PyDict_MAXFREELIST];
208static int numfree = 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000209
Christian Heimesf75dbef2008-02-08 00:11:31 +0000210void
211PyDict_Fini(void)
212{
Christian Heimes48397d62008-02-08 00:14:34 +0000213 PyDictObject *op;
Christian Heimesf75dbef2008-02-08 00:11:31 +0000214
215 while (numfree) {
Christian Heimes48397d62008-02-08 00:14:34 +0000216 op = free_list[--numfree];
Christian Heimesf75dbef2008-02-08 00:11:31 +0000217 assert(PyDict_CheckExact(op));
218 PyObject_GC_Del(op);
219 }
220}
221
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000222PyObject *
Thomas Wouters78890102000-07-22 19:25:51 +0000223PyDict_New(void)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000224{
Brett Cannon77ae87c2007-10-10 00:07:50 +0000225 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000226 if (dummy == NULL) { /* Auto-initialize dummy */
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000227 dummy = PyString_FromString("<dummy key>");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000228 if (dummy == NULL)
229 return NULL;
Fred Drake1bff34a2000-08-31 19:31:38 +0000230#ifdef SHOW_CONVERSION_COUNTS
231 Py_AtExit(show_counts);
232#endif
Christian Heimesb4ee4a12008-02-07 17:15:30 +0000233#ifdef SHOW_ALLOC_COUNT
234 Py_AtExit(show_alloc);
235#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000236 }
Christian Heimes5b970ad2008-02-06 13:33:44 +0000237 if (numfree) {
238 mp = free_list[--numfree];
Raymond Hettinger43442782004-03-17 21:55:03 +0000239 assert (mp != NULL);
Christian Heimese93237d2007-12-19 02:37:44 +0000240 assert (Py_TYPE(mp) == &PyDict_Type);
Raymond Hettinger43442782004-03-17 21:55:03 +0000241 _Py_NewReference((PyObject *)mp);
242 if (mp->ma_fill) {
243 EMPTY_TO_MINSIZE(mp);
Georg Brandl1e13ea92008-08-11 09:07:59 +0000244 } else {
245 /* At least set ma_table and ma_mask; these are wrong
246 if an empty but presized dict is added to freelist */
247 INIT_NONZERO_DICT_SLOTS(mp);
Raymond Hettinger43442782004-03-17 21:55:03 +0000248 }
249 assert (mp->ma_used == 0);
250 assert (mp->ma_table == mp->ma_smalltable);
251 assert (mp->ma_mask == PyDict_MINSIZE - 1);
Christian Heimesb4ee4a12008-02-07 17:15:30 +0000252#ifdef SHOW_ALLOC_COUNT
253 count_reuse++;
254#endif
Raymond Hettinger43442782004-03-17 21:55:03 +0000255 } else {
Brett Cannon77ae87c2007-10-10 00:07:50 +0000256 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
Raymond Hettinger43442782004-03-17 21:55:03 +0000257 if (mp == NULL)
258 return NULL;
259 EMPTY_TO_MINSIZE(mp);
Christian Heimesb4ee4a12008-02-07 17:15:30 +0000260#ifdef SHOW_ALLOC_COUNT
261 count_alloc++;
262#endif
Raymond Hettinger43442782004-03-17 21:55:03 +0000263 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000264 mp->ma_lookup = lookdict_string;
265#ifdef SHOW_CONVERSION_COUNTS
266 ++created;
267#endif
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000268 _PyObject_GC_TRACK(mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000269 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000270}
271
272/*
273The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000274This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000275Open addressing is preferred over chaining since the link overhead for
276chaining would be substantial (100% with typical malloc overhead).
277
Tim Peterseb28ef22001-06-02 05:27:19 +0000278The initial probe index is computed as hash mod the table size. Subsequent
279probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000280
281All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000282
Tim Peterseb28ef22001-06-02 05:27:19 +0000283(The details in this version are due to Tim Peters, building on many past
284contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
285Christian Tismer).
Fred Drake1bff34a2000-08-31 19:31:38 +0000286
Tim Petersd770ebd2006-06-01 15:50:44 +0000287lookdict() is general-purpose, and may return NULL if (and only if) a
288comparison raises an exception (this was new in Python 2.5).
289lookdict_string() below is specialized to string keys, comparison of which can
290never raise an exception; that function can never return NULL. For both, when
Brett Cannon77ae87c2007-10-10 00:07:50 +0000291the key isn't found a PyDictEntry* is returned for which the me_value field is
Tim Petersd770ebd2006-06-01 15:50:44 +0000292NULL; this is the slot in the dict at which the key would have been found, and
293the caller can (if it wishes) add the <key, value> pair to the returned
Brett Cannon77ae87c2007-10-10 00:07:50 +0000294PyDictEntry*.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000295*/
Brett Cannon77ae87c2007-10-10 00:07:50 +0000296static PyDictEntry *
297lookdict(PyDictObject *mp, PyObject *key, register long hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000298{
Tim Petersd770ebd2006-06-01 15:50:44 +0000299 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000300 register size_t perturb;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000301 register PyDictEntry *freeslot;
Tim Petersd770ebd2006-06-01 15:50:44 +0000302 register size_t mask = (size_t)mp->ma_mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000303 PyDictEntry *ep0 = mp->ma_table;
304 register PyDictEntry *ep;
Fred Drakec88b99c2000-08-31 19:04:07 +0000305 register int cmp;
Tim Peters453163d2001-06-03 04:54:32 +0000306 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000307
Tim Petersd770ebd2006-06-01 15:50:44 +0000308 i = (size_t)hash & mask;
Guido van Rossumefb46091997-01-29 15:53:56 +0000309 ep = &ep0[i];
Guido van Rossumc1c7b1a1998-10-06 16:01:14 +0000310 if (ep->me_key == NULL || ep->me_key == key)
Guido van Rossum7d181591997-01-16 21:06:45 +0000311 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000312
Guido van Rossum16e93a81997-01-28 00:00:11 +0000313 if (ep->me_key == dummy)
Guido van Rossum7d181591997-01-16 21:06:45 +0000314 freeslot = ep;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000315 else {
Fred Drakec88b99c2000-08-31 19:04:07 +0000316 if (ep->me_hash == hash) {
Tim Peters453163d2001-06-03 04:54:32 +0000317 startkey = ep->me_key;
Georg Brandlede3a322007-11-29 18:33:01 +0000318 Py_INCREF(startkey);
Tim Peters453163d2001-06-03 04:54:32 +0000319 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Georg Brandlede3a322007-11-29 18:33:01 +0000320 Py_DECREF(startkey);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000321 if (cmp < 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000322 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000323 if (ep0 == mp->ma_table && ep->me_key == startkey) {
324 if (cmp > 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000325 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000326 }
327 else {
328 /* The compare did major nasty stuff to the
329 * dict: start over.
330 * XXX A clever adversary could prevent this
331 * XXX from terminating.
332 */
Armin Rigo35f6d362006-06-01 13:19:12 +0000333 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000334 }
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000335 }
336 freeslot = NULL;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000337 }
Tim Peters15d49292001-05-27 07:39:22 +0000338
Tim Peters342c65e2001-05-13 06:43:53 +0000339 /* In the loop, me_key == dummy is by far (factor of 100s) the
340 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000341 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
342 i = (i << 2) + i + perturb + 1;
343 ep = &ep0[i & mask];
Armin Rigo35f6d362006-06-01 13:19:12 +0000344 if (ep->me_key == NULL)
345 return freeslot == NULL ? ep : freeslot;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000346 if (ep->me_key == key)
Armin Rigo35f6d362006-06-01 13:19:12 +0000347 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000348 if (ep->me_hash == hash && ep->me_key != dummy) {
Tim Peters453163d2001-06-03 04:54:32 +0000349 startkey = ep->me_key;
Georg Brandlede3a322007-11-29 18:33:01 +0000350 Py_INCREF(startkey);
Tim Peters453163d2001-06-03 04:54:32 +0000351 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Georg Brandlede3a322007-11-29 18:33:01 +0000352 Py_DECREF(startkey);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000353 if (cmp < 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000354 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000355 if (ep0 == mp->ma_table && ep->me_key == startkey) {
356 if (cmp > 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000357 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000358 }
359 else {
360 /* The compare did major nasty stuff to the
361 * dict: start over.
362 * XXX A clever adversary could prevent this
363 * XXX from terminating.
364 */
Armin Rigo35f6d362006-06-01 13:19:12 +0000365 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000366 }
Guido van Rossum16e93a81997-01-28 00:00:11 +0000367 }
Tim Peters342c65e2001-05-13 06:43:53 +0000368 else if (ep->me_key == dummy && freeslot == NULL)
369 freeslot = ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000370 }
Neal Norwitza5ccda92006-10-28 21:16:54 +0000371 assert(0); /* NOT REACHED */
372 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000373}
374
375/*
Fred Drake1bff34a2000-08-31 19:31:38 +0000376 * Hacked up version of lookdict which can assume keys are always strings;
Tim Petersd770ebd2006-06-01 15:50:44 +0000377 * this assumption allows testing for errors during PyObject_RichCompareBool()
378 * to be dropped; string-string comparisons never raise exceptions. This also
379 * means we don't need to go through PyObject_RichCompareBool(); we can always
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000380 * use _PyString_Eq() directly.
Fred Drake1bff34a2000-08-31 19:31:38 +0000381 *
Tim Petersd770ebd2006-06-01 15:50:44 +0000382 * This is valuable because dicts with only string keys are very common.
Fred Drake1bff34a2000-08-31 19:31:38 +0000383 */
Brett Cannon77ae87c2007-10-10 00:07:50 +0000384static PyDictEntry *
385lookdict_string(PyDictObject *mp, PyObject *key, register long hash)
Fred Drake1bff34a2000-08-31 19:31:38 +0000386{
Tim Petersd770ebd2006-06-01 15:50:44 +0000387 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000388 register size_t perturb;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000389 register PyDictEntry *freeslot;
Tim Petersd770ebd2006-06-01 15:50:44 +0000390 register size_t mask = (size_t)mp->ma_mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000391 PyDictEntry *ep0 = mp->ma_table;
392 register PyDictEntry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000393
Tim Peters0ab085c2001-09-14 00:25:33 +0000394 /* Make sure this function doesn't have to handle non-string keys,
395 including subclasses of str; e.g., one reason to subclass
396 strings is to override __eq__, and for speed we don't cater to
397 that here. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000398 if (!PyString_CheckExact(key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000399#ifdef SHOW_CONVERSION_COUNTS
400 ++converted;
401#endif
402 mp->ma_lookup = lookdict;
403 return lookdict(mp, key, hash);
404 }
Tim Peters2f228e72001-05-13 00:19:31 +0000405 i = hash & mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000406 ep = &ep0[i];
407 if (ep->me_key == NULL || ep->me_key == key)
408 return ep;
409 if (ep->me_key == dummy)
410 freeslot = ep;
411 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000412 if (ep->me_hash == hash && _PyString_Eq(ep->me_key, key))
Fred Drake1bff34a2000-08-31 19:31:38 +0000413 return ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000414 freeslot = NULL;
415 }
Tim Peters15d49292001-05-27 07:39:22 +0000416
Tim Peters342c65e2001-05-13 06:43:53 +0000417 /* In the loop, me_key == dummy is by far (factor of 100s) the
418 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000419 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
420 i = (i << 2) + i + perturb + 1;
421 ep = &ep0[i & mask];
Tim Peters342c65e2001-05-13 06:43:53 +0000422 if (ep->me_key == NULL)
423 return freeslot == NULL ? ep : freeslot;
424 if (ep->me_key == key
425 || (ep->me_hash == hash
426 && ep->me_key != dummy
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000427 && _PyString_Eq(ep->me_key, key)))
Fred Drake1bff34a2000-08-31 19:31:38 +0000428 return ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000429 if (ep->me_key == dummy && freeslot == NULL)
Tim Peters342c65e2001-05-13 06:43:53 +0000430 freeslot = ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000431 }
Neal Norwitza5ccda92006-10-28 21:16:54 +0000432 assert(0); /* NOT REACHED */
433 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000434}
435
436/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000437Internal routine to insert a new item into the table.
438Used both by the internal resize routine and by the public insert routine.
439Eats a reference to key and one to value.
Tim Petersd770ebd2006-06-01 15:50:44 +0000440Returns -1 if an error occurred, or 0 on success.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000441*/
Armin Rigo35f6d362006-06-01 13:19:12 +0000442static int
Brett Cannon77ae87c2007-10-10 00:07:50 +0000443insertdict(register PyDictObject *mp, PyObject *key, long hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000444{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000445 PyObject *old_value;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000446 register PyDictEntry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000447 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
448
449 assert(mp->ma_lookup != NULL);
450 ep = mp->ma_lookup(mp, key, hash);
Armin Rigo35f6d362006-06-01 13:19:12 +0000451 if (ep == NULL) {
452 Py_DECREF(key);
453 Py_DECREF(value);
454 return -1;
455 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000456 if (ep->me_value != NULL) {
Guido van Rossumd7047b31995-01-02 19:07:15 +0000457 old_value = ep->me_value;
458 ep->me_value = value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000459 Py_DECREF(old_value); /* which **CAN** re-enter */
460 Py_DECREF(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000461 }
462 else {
463 if (ep->me_key == NULL)
464 mp->ma_fill++;
Raymond Hettinger07ead172005-02-05 23:42:57 +0000465 else {
466 assert(ep->me_key == dummy);
467 Py_DECREF(dummy);
468 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000469 ep->me_key = key;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000470 ep->me_hash = (Py_ssize_t)hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000471 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000472 mp->ma_used++;
473 }
Armin Rigo35f6d362006-06-01 13:19:12 +0000474 return 0;
475}
476
477/*
478Internal routine used by dictresize() to insert an item which is
479known to be absent from the dict. This routine also assumes that
480the dict contains no deleted entries. Besides the performance benefit,
481using insertdict() in dictresize() is dangerous (SF bug #1456209).
Tim Petersd770ebd2006-06-01 15:50:44 +0000482Note that no refcounts are changed by this routine; if needed, the caller
483is responsible for incref'ing `key` and `value`.
Armin Rigo35f6d362006-06-01 13:19:12 +0000484*/
485static void
Brett Cannon77ae87c2007-10-10 00:07:50 +0000486insertdict_clean(register PyDictObject *mp, PyObject *key, long hash,
Armin Rigo35f6d362006-06-01 13:19:12 +0000487 PyObject *value)
488{
Tim Petersd770ebd2006-06-01 15:50:44 +0000489 register size_t i;
Armin Rigo35f6d362006-06-01 13:19:12 +0000490 register size_t perturb;
Tim Petersd770ebd2006-06-01 15:50:44 +0000491 register size_t mask = (size_t)mp->ma_mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000492 PyDictEntry *ep0 = mp->ma_table;
493 register PyDictEntry *ep;
Armin Rigo35f6d362006-06-01 13:19:12 +0000494
495 i = hash & mask;
496 ep = &ep0[i];
497 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
498 i = (i << 2) + i + perturb + 1;
499 ep = &ep0[i & mask];
500 }
Tim Petersd770ebd2006-06-01 15:50:44 +0000501 assert(ep->me_value == NULL);
Armin Rigo35f6d362006-06-01 13:19:12 +0000502 mp->ma_fill++;
503 ep->me_key = key;
504 ep->me_hash = (Py_ssize_t)hash;
505 ep->me_value = value;
506 mp->ma_used++;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000507}
508
509/*
510Restructure the table by allocating a new table and reinserting all
511items again. When entries have been deleted, the new table may
512actually be smaller than the old one.
513*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000514static int
Brett Cannon77ae87c2007-10-10 00:07:50 +0000515dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000516{
Tim Peters9b10f7e2006-05-30 04:16:25 +0000517 Py_ssize_t newsize;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000518 PyDictEntry *oldtable, *newtable, *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000519 Py_ssize_t i;
Tim Peters0c6010b2001-05-23 23:33:57 +0000520 int is_oldtable_malloced;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000521 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000522
523 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000524
Tim Peterseb28ef22001-06-02 05:27:19 +0000525 /* Find the smallest table size > minused. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000526 for (newsize = PyDict_MINSIZE;
Tim Peterseb28ef22001-06-02 05:27:19 +0000527 newsize <= minused && newsize > 0;
528 newsize <<= 1)
529 ;
530 if (newsize <= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000531 PyErr_NoMemory();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000532 return -1;
533 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000534
535 /* Get space for a new table. */
536 oldtable = mp->ma_table;
537 assert(oldtable != NULL);
538 is_oldtable_malloced = oldtable != mp->ma_smalltable;
539
Tim Peters6d6c1a32001-08-02 04:15:00 +0000540 if (newsize == PyDict_MINSIZE) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000541 /* A large table is shrinking, or we can't get any smaller. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000542 newtable = mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000543 if (newtable == oldtable) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000544 if (mp->ma_fill == mp->ma_used) {
545 /* No dummies, so no point doing anything. */
Tim Peters0c6010b2001-05-23 23:33:57 +0000546 return 0;
Tim Petersf8a548c2001-05-24 16:26:40 +0000547 }
548 /* We're not going to resize it, but rebuild the
549 table anyway to purge old dummy entries.
550 Subtle: This is *necessary* if fill==size,
551 as lookdict needs at least one virgin slot to
552 terminate failing searches. If fill < size, it's
553 merely desirable, as dummies slow searches. */
554 assert(mp->ma_fill > mp->ma_used);
Tim Peters0c6010b2001-05-23 23:33:57 +0000555 memcpy(small_copy, oldtable, sizeof(small_copy));
556 oldtable = small_copy;
557 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000558 }
559 else {
Brett Cannon77ae87c2007-10-10 00:07:50 +0000560 newtable = PyMem_NEW(PyDictEntry, newsize);
Tim Petersdea48ec2001-05-22 20:40:22 +0000561 if (newtable == NULL) {
562 PyErr_NoMemory();
563 return -1;
564 }
565 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000566
567 /* Make the dict empty, using the new table. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000568 assert(newtable != oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000569 mp->ma_table = newtable;
Tim Petersafb6ae82001-06-04 21:00:21 +0000570 mp->ma_mask = newsize - 1;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000571 memset(newtable, 0, sizeof(PyDictEntry) * newsize);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000572 mp->ma_used = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000573 i = mp->ma_fill;
574 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000575
Tim Peters19283142001-05-17 22:25:34 +0000576 /* Copy the data over; this is refcount-neutral for active entries;
577 dummy entries aren't copied over, of course */
Tim Petersdea48ec2001-05-22 20:40:22 +0000578 for (ep = oldtable; i > 0; ep++) {
579 if (ep->me_value != NULL) { /* active entry */
580 --i;
Armin Rigo35f6d362006-06-01 13:19:12 +0000581 insertdict_clean(mp, ep->me_key, (long)ep->me_hash,
582 ep->me_value);
Tim Petersdea48ec2001-05-22 20:40:22 +0000583 }
Tim Peters19283142001-05-17 22:25:34 +0000584 else if (ep->me_key != NULL) { /* dummy entry */
Tim Petersdea48ec2001-05-22 20:40:22 +0000585 --i;
Tim Peters19283142001-05-17 22:25:34 +0000586 assert(ep->me_key == dummy);
587 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000588 }
Tim Peters19283142001-05-17 22:25:34 +0000589 /* else key == value == NULL: nothing to do */
Guido van Rossumd7047b31995-01-02 19:07:15 +0000590 }
591
Tim Peters0c6010b2001-05-23 23:33:57 +0000592 if (is_oldtable_malloced)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000593 PyMem_DEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000594 return 0;
595}
596
Raymond Hettingerfd7ed402007-12-18 21:24:09 +0000597/* Create a new dictionary pre-sized to hold an estimated number of elements.
598 Underestimates are okay because the dictionary will resize as necessary.
599 Overestimates just mean the dictionary will be more sparse than usual.
600*/
601
602PyObject *
603_PyDict_NewPresized(Py_ssize_t minused)
604{
605 PyObject *op = PyDict_New();
606
607 if (minused>5 && op != NULL && dictresize((PyDictObject *)op, minused) == -1) {
608 Py_DECREF(op);
609 return NULL;
610 }
611 return op;
612}
613
Tim Petersd770ebd2006-06-01 15:50:44 +0000614/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
615 * that may occur (originally dicts supported only string keys, and exceptions
616 * weren't possible). So, while the original intent was that a NULL return
Andrew M. Kuchling0067b5f2006-08-04 20:37:43 +0000617 * meant the key wasn't present, in reality it can mean that, or that an error
Tim Petersd770ebd2006-06-01 15:50:44 +0000618 * (suppressed) occurred while computing the key's hash, or that some error
619 * (suppressed) occurred when comparing keys in the dict's internal probe
620 * sequence. A nasty example of the latter is when a Python-coded comparison
621 * function hits a stack-depth error, which can cause this to return NULL
622 * even if the key is present.
623 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000624PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000625PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000626{
627 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000628 PyDictObject *mp = (PyDictObject *)op;
629 PyDictEntry *ep;
Armin Rigo35f6d362006-06-01 13:19:12 +0000630 PyThreadState *tstate;
Tim Petersd770ebd2006-06-01 15:50:44 +0000631 if (!PyDict_Check(op))
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000632 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000633 if (!PyString_CheckExact(key) ||
634 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Guido van Rossumca756f21997-01-23 19:39:29 +0000635 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000636 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000637 if (hash == -1) {
638 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000639 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000640 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000641 }
Armin Rigo35f6d362006-06-01 13:19:12 +0000642
643 /* We can arrive here with a NULL tstate during initialization:
644 try running "python -Wi" for an example related to string
645 interning. Let's just hope that no exception occurs then... */
Armin Rigoacd0d6d2006-06-10 10:57:40 +0000646 tstate = _PyThreadState_Current;
Armin Rigo35f6d362006-06-01 13:19:12 +0000647 if (tstate != NULL && tstate->curexc_type != NULL) {
648 /* preserve the existing exception */
649 PyObject *err_type, *err_value, *err_tb;
650 PyErr_Fetch(&err_type, &err_value, &err_tb);
651 ep = (mp->ma_lookup)(mp, key, hash);
652 /* ignore errors */
653 PyErr_Restore(err_type, err_value, err_tb);
654 if (ep == NULL)
655 return NULL;
656 }
657 else {
658 ep = (mp->ma_lookup)(mp, key, hash);
659 if (ep == NULL) {
660 PyErr_Clear();
661 return NULL;
662 }
663 }
664 return ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000665}
666
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000667/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
Tim Peters60b29962006-01-01 01:19:23 +0000668 * dictionary if it's merely replacing the value for an existing key.
669 * This means that it's safe to loop over a dictionary with PyDict_Next()
670 * and occasionally replace a value -- but you can't insert new keys or
671 * remove them.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000672 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000673int
Tim Peters1f5871e2000-07-04 17:44:48 +0000674PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000675{
Brett Cannon77ae87c2007-10-10 00:07:50 +0000676 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000677 register long hash;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000678 register Py_ssize_t n_used;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000679
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000680 if (!PyDict_Check(op)) {
681 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000682 return -1;
683 }
Neal Norwitz48808a12006-07-21 05:29:58 +0000684 assert(key);
685 assert(value);
Brett Cannon77ae87c2007-10-10 00:07:50 +0000686 mp = (PyDictObject *)op;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000687 if (PyString_CheckExact(key)) {
688 hash = ((PyStringObject *)key)->ob_shash;
Guido van Rossum45ec02a2002-08-19 21:43:18 +0000689 if (hash == -1)
690 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000691 }
Tim Peters1f7df352002-03-29 03:29:08 +0000692 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000693 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000694 if (hash == -1)
695 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000696 }
Tim Petersafb6ae82001-06-04 21:00:21 +0000697 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000698 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000699 Py_INCREF(value);
700 Py_INCREF(key);
Armin Rigo35f6d362006-06-01 13:19:12 +0000701 if (insertdict(mp, key, hash, value) != 0)
702 return -1;
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000703 /* If we added a key, we can safely resize. Otherwise just return!
704 * If fill >= 2/3 size, adjust size. Normally, this doubles or
705 * quaduples the size, but it's also possible for the dict to shrink
Tim Peters60b29962006-01-01 01:19:23 +0000706 * (if ma_fill is much larger than ma_used, meaning a lot of dict
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000707 * keys have been * deleted).
Tim Peters60b29962006-01-01 01:19:23 +0000708 *
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000709 * Quadrupling the size improves average dictionary sparseness
710 * (reducing collisions) at the cost of some memory and iteration
711 * speed (which loops over every possible entry). It also halves
Neal Norwitz80af59c2006-05-30 04:19:21 +0000712 * the number of expensive resize operations in a growing dictionary.
Tim Peters60b29962006-01-01 01:19:23 +0000713 *
714 * Very large dictionaries (over 50K items) use doubling instead.
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000715 * This may help applications with severe memory constraints.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000716 */
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000717 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
718 return 0;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000719 return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000720}
721
722int
Tim Peters1f5871e2000-07-04 17:44:48 +0000723PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000724{
Brett Cannon77ae87c2007-10-10 00:07:50 +0000725 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000726 register long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000727 register PyDictEntry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000728 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000729
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000730 if (!PyDict_Check(op)) {
731 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000732 return -1;
733 }
Neal Norwitz48808a12006-07-21 05:29:58 +0000734 assert(key);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000735 if (!PyString_CheckExact(key) ||
736 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000737 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000738 if (hash == -1)
739 return -1;
740 }
Brett Cannon77ae87c2007-10-10 00:07:50 +0000741 mp = (PyDictObject *)op;
Fred Drake1bff34a2000-08-31 19:31:38 +0000742 ep = (mp->ma_lookup)(mp, key, hash);
Armin Rigo35f6d362006-06-01 13:19:12 +0000743 if (ep == NULL)
744 return -1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000745 if (ep->me_value == NULL) {
Georg Brandlb9f4ad32006-10-29 18:31:42 +0000746 set_key_error(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000747 return -1;
748 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000749 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000750 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000751 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000752 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000753 ep->me_value = NULL;
754 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000755 Py_DECREF(old_value);
756 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000757 return 0;
758}
759
Guido van Rossum25831651993-05-19 14:50:45 +0000760void
Tim Peters1f5871e2000-07-04 17:44:48 +0000761PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000762{
Brett Cannon77ae87c2007-10-10 00:07:50 +0000763 PyDictObject *mp;
764 PyDictEntry *ep, *table;
Tim Petersdea48ec2001-05-22 20:40:22 +0000765 int table_is_malloced;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000766 Py_ssize_t fill;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000767 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Petersdea48ec2001-05-22 20:40:22 +0000768#ifdef Py_DEBUG
Tim Peters9b10f7e2006-05-30 04:16:25 +0000769 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +0000770#endif
771
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000772 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000773 return;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000774 mp = (PyDictObject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000775#ifdef Py_DEBUG
Tim Petersafb6ae82001-06-04 21:00:21 +0000776 n = mp->ma_mask + 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000777 i = 0;
778#endif
779
780 table = mp->ma_table;
781 assert(table != NULL);
782 table_is_malloced = table != mp->ma_smalltable;
783
784 /* This is delicate. During the process of clearing the dict,
785 * decrefs can cause the dict to mutate. To avoid fatal confusion
786 * (voice of experience), we have to make the dict empty before
787 * clearing the slots, and never refer to anything via mp->xxx while
788 * clearing.
789 */
790 fill = mp->ma_fill;
791 if (table_is_malloced)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000792 EMPTY_TO_MINSIZE(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000793
794 else if (fill > 0) {
795 /* It's a small table with something that needs to be cleared.
796 * Afraid the only safe way is to copy the dict entries into
797 * another small table first.
798 */
799 memcpy(small_copy, table, sizeof(small_copy));
800 table = small_copy;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000801 EMPTY_TO_MINSIZE(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000802 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000803 /* else it's a small table that's already empty */
804
805 /* Now we can finally clear things. If C had refcounts, we could
806 * assert that the refcount on table is 1 now, i.e. that this function
807 * has unique access to it, so decref side-effects can't alter it.
808 */
809 for (ep = table; fill > 0; ++ep) {
810#ifdef Py_DEBUG
811 assert(i < n);
812 ++i;
813#endif
814 if (ep->me_key) {
815 --fill;
816 Py_DECREF(ep->me_key);
817 Py_XDECREF(ep->me_value);
818 }
819#ifdef Py_DEBUG
820 else
821 assert(ep->me_value == NULL);
822#endif
823 }
824
825 if (table_is_malloced)
826 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000827}
828
Tim Peters080c88b2003-02-15 03:01:11 +0000829/*
830 * Iterate over a dict. Use like so:
831 *
Tim Peters9b10f7e2006-05-30 04:16:25 +0000832 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +0000833 * PyObject *key, *value;
834 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +0000835 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +0000836 * Refer to borrowed references in key and value.
837 * }
838 *
839 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +0000840 * mutates the dict. One exception: it is safe if the loop merely changes
841 * the values associated with the keys (but doesn't insert new keys or
842 * delete keys), via PyDict_SetItem().
843 */
Guido van Rossum25831651993-05-19 14:50:45 +0000844int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000845PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000846{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000847 register Py_ssize_t i;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000848 register Py_ssize_t mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000849 register PyDictEntry *ep;
Raymond Hettinger43442782004-03-17 21:55:03 +0000850
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000851 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000852 return 0;
Guido van Rossum25831651993-05-19 14:50:45 +0000853 i = *ppos;
854 if (i < 0)
855 return 0;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000856 ep = ((PyDictObject *)op)->ma_table;
857 mask = ((PyDictObject *)op)->ma_mask;
Raymond Hettinger43442782004-03-17 21:55:03 +0000858 while (i <= mask && ep[i].me_value == NULL)
Guido van Rossum25831651993-05-19 14:50:45 +0000859 i++;
860 *ppos = i+1;
Raymond Hettinger43442782004-03-17 21:55:03 +0000861 if (i > mask)
Guido van Rossum25831651993-05-19 14:50:45 +0000862 return 0;
863 if (pkey)
Raymond Hettinger43442782004-03-17 21:55:03 +0000864 *pkey = ep[i].me_key;
Guido van Rossum25831651993-05-19 14:50:45 +0000865 if (pvalue)
Raymond Hettinger43442782004-03-17 21:55:03 +0000866 *pvalue = ep[i].me_value;
Guido van Rossum25831651993-05-19 14:50:45 +0000867 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000868}
869
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000870/* Internal version of PyDict_Next that returns a hash value in addition to the key and value.*/
871int
872_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue, long *phash)
873{
874 register Py_ssize_t i;
875 register Py_ssize_t mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000876 register PyDictEntry *ep;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000877
878 if (!PyDict_Check(op))
879 return 0;
880 i = *ppos;
881 if (i < 0)
882 return 0;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000883 ep = ((PyDictObject *)op)->ma_table;
884 mask = ((PyDictObject *)op)->ma_mask;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000885 while (i <= mask && ep[i].me_value == NULL)
886 i++;
887 *ppos = i+1;
888 if (i > mask)
889 return 0;
890 *phash = (long)(ep[i].me_hash);
891 if (pkey)
892 *pkey = ep[i].me_key;
893 if (pvalue)
894 *pvalue = ep[i].me_value;
895 return 1;
896}
897
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000898/* Methods */
899
900static void
Brett Cannon77ae87c2007-10-10 00:07:50 +0000901dict_dealloc(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000902{
Brett Cannon77ae87c2007-10-10 00:07:50 +0000903 register PyDictEntry *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000904 Py_ssize_t fill = mp->ma_fill;
Guido van Rossumff413af2002-03-28 20:34:59 +0000905 PyObject_GC_UnTrack(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000906 Py_TRASHCAN_SAFE_BEGIN(mp)
Tim Petersdea48ec2001-05-22 20:40:22 +0000907 for (ep = mp->ma_table; fill > 0; ep++) {
908 if (ep->me_key) {
909 --fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000910 Py_DECREF(ep->me_key);
Tim Petersdea48ec2001-05-22 20:40:22 +0000911 Py_XDECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000912 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000913 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000914 if (mp->ma_table != mp->ma_smalltable)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000915 PyMem_DEL(mp->ma_table);
Christian Heimes5b970ad2008-02-06 13:33:44 +0000916 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
917 free_list[numfree++] = mp;
Tim Peters60b29962006-01-01 01:19:23 +0000918 else
Christian Heimese93237d2007-12-19 02:37:44 +0000919 Py_TYPE(mp)->tp_free((PyObject *)mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000920 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000921}
922
923static int
Brett Cannon77ae87c2007-10-10 00:07:50 +0000924dict_print(register PyDictObject *mp, register FILE *fp, register int flags)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000925{
Tim Peters9b10f7e2006-05-30 04:16:25 +0000926 register Py_ssize_t i;
927 register Py_ssize_t any;
Tim Peters33f4a6a2006-05-30 05:23:59 +0000928 int status;
Guido van Rossum255443b1998-04-10 22:47:14 +0000929
Tim Peters33f4a6a2006-05-30 05:23:59 +0000930 status = Py_ReprEnter((PyObject*)mp);
931 if (status != 0) {
932 if (status < 0)
933 return status;
Brett Cannon01531592007-09-17 03:28:34 +0000934 Py_BEGIN_ALLOW_THREADS
Guido van Rossum255443b1998-04-10 22:47:14 +0000935 fprintf(fp, "{...}");
Brett Cannon01531592007-09-17 03:28:34 +0000936 Py_END_ALLOW_THREADS
Guido van Rossum255443b1998-04-10 22:47:14 +0000937 return 0;
938 }
939
Brett Cannon01531592007-09-17 03:28:34 +0000940 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000941 fprintf(fp, "{");
Brett Cannon01531592007-09-17 03:28:34 +0000942 Py_END_ALLOW_THREADS
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000943 any = 0;
Tim Petersafb6ae82001-06-04 21:00:21 +0000944 for (i = 0; i <= mp->ma_mask; i++) {
Brett Cannon77ae87c2007-10-10 00:07:50 +0000945 PyDictEntry *ep = mp->ma_table + i;
Tim Peters23cf6be2001-06-02 08:02:56 +0000946 PyObject *pvalue = ep->me_value;
947 if (pvalue != NULL) {
948 /* Prevent PyObject_Repr from deleting value during
949 key format */
950 Py_INCREF(pvalue);
Brett Cannon01531592007-09-17 03:28:34 +0000951 if (any++ > 0) {
952 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000953 fprintf(fp, ", ");
Brett Cannon01531592007-09-17 03:28:34 +0000954 Py_END_ALLOW_THREADS
955 }
Guido van Rossum255443b1998-04-10 22:47:14 +0000956 if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000957 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000958 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000959 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000960 }
Brett Cannon01531592007-09-17 03:28:34 +0000961 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000962 fprintf(fp, ": ");
Brett Cannon01531592007-09-17 03:28:34 +0000963 Py_END_ALLOW_THREADS
Tim Peters19b77cf2001-06-02 08:27:39 +0000964 if (PyObject_Print(pvalue, fp, 0) != 0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000965 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000966 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000967 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000968 }
Tim Peters23cf6be2001-06-02 08:02:56 +0000969 Py_DECREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000970 }
971 }
Brett Cannon01531592007-09-17 03:28:34 +0000972 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000973 fprintf(fp, "}");
Brett Cannon01531592007-09-17 03:28:34 +0000974 Py_END_ALLOW_THREADS
Guido van Rossum255443b1998-04-10 22:47:14 +0000975 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000976 return 0;
977}
978
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000979static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +0000980dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000981{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000982 Py_ssize_t i;
Tim Petersa7259592001-06-16 05:11:17 +0000983 PyObject *s, *temp, *colon = NULL;
984 PyObject *pieces = NULL, *result = NULL;
985 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +0000986
Tim Petersa7259592001-06-16 05:11:17 +0000987 i = Py_ReprEnter((PyObject *)mp);
Guido van Rossum255443b1998-04-10 22:47:14 +0000988 if (i != 0) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000989 return i > 0 ? PyString_FromString("{...}") : NULL;
Guido van Rossum255443b1998-04-10 22:47:14 +0000990 }
991
Tim Petersa7259592001-06-16 05:11:17 +0000992 if (mp->ma_used == 0) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000993 result = PyString_FromString("{}");
Tim Petersa7259592001-06-16 05:11:17 +0000994 goto Done;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000995 }
Tim Petersa7259592001-06-16 05:11:17 +0000996
997 pieces = PyList_New(0);
998 if (pieces == NULL)
999 goto Done;
1000
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001001 colon = PyString_FromString(": ");
Tim Petersa7259592001-06-16 05:11:17 +00001002 if (colon == NULL)
1003 goto Done;
1004
1005 /* Do repr() on each key+value pair, and insert ": " between them.
1006 Note that repr may mutate the dict. */
Tim Petersc6057842001-06-16 07:52:53 +00001007 i = 0;
1008 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Tim Petersa7259592001-06-16 05:11:17 +00001009 int status;
1010 /* Prevent repr from deleting value during key format. */
1011 Py_INCREF(value);
1012 s = PyObject_Repr(key);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001013 PyString_Concat(&s, colon);
1014 PyString_ConcatAndDel(&s, PyObject_Repr(value));
Tim Petersa7259592001-06-16 05:11:17 +00001015 Py_DECREF(value);
1016 if (s == NULL)
1017 goto Done;
1018 status = PyList_Append(pieces, s);
1019 Py_DECREF(s); /* append created a new ref */
1020 if (status < 0)
1021 goto Done;
1022 }
1023
1024 /* Add "{}" decorations to the first and last items. */
1025 assert(PyList_GET_SIZE(pieces) > 0);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001026 s = PyString_FromString("{");
Tim Petersa7259592001-06-16 05:11:17 +00001027 if (s == NULL)
1028 goto Done;
1029 temp = PyList_GET_ITEM(pieces, 0);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001030 PyString_ConcatAndDel(&s, temp);
Tim Petersa7259592001-06-16 05:11:17 +00001031 PyList_SET_ITEM(pieces, 0, s);
1032 if (s == NULL)
1033 goto Done;
1034
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001035 s = PyString_FromString("}");
Tim Petersa7259592001-06-16 05:11:17 +00001036 if (s == NULL)
1037 goto Done;
1038 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001039 PyString_ConcatAndDel(&temp, s);
Tim Petersa7259592001-06-16 05:11:17 +00001040 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
1041 if (temp == NULL)
1042 goto Done;
1043
1044 /* Paste them all together with ", " between. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001045 s = PyString_FromString(", ");
Tim Petersa7259592001-06-16 05:11:17 +00001046 if (s == NULL)
1047 goto Done;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001048 result = _PyString_Join(s, pieces);
Tim Peters0ab085c2001-09-14 00:25:33 +00001049 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +00001050
1051Done:
1052 Py_XDECREF(pieces);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001053 Py_XDECREF(colon);
Tim Petersa7259592001-06-16 05:11:17 +00001054 Py_ReprLeave((PyObject *)mp);
1055 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001056}
1057
Martin v. Löwis18e16552006-02-15 17:27:45 +00001058static Py_ssize_t
Brett Cannon77ae87c2007-10-10 00:07:50 +00001059dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001060{
1061 return mp->ma_used;
1062}
1063
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001064static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001065dict_subscript(PyDictObject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001066{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001067 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001068 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001069 PyDictEntry *ep;
Tim Petersdea48ec2001-05-22 20:40:22 +00001070 assert(mp->ma_table != NULL);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001071 if (!PyString_CheckExact(key) ||
1072 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001073 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001074 if (hash == -1)
1075 return NULL;
1076 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001077 ep = (mp->ma_lookup)(mp, key, hash);
1078 if (ep == NULL)
1079 return NULL;
1080 v = ep->me_value;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001081 if (v == NULL) {
1082 if (!PyDict_CheckExact(mp)) {
1083 /* Look up __missing__ method if we're a subclass. */
Guido van Rossum4b92a822006-02-25 23:32:30 +00001084 PyObject *missing;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001085 static PyObject *missing_str = NULL;
1086 if (missing_str == NULL)
Tim Peters9b10f7e2006-05-30 04:16:25 +00001087 missing_str =
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001088 PyString_InternFromString("__missing__");
Christian Heimese93237d2007-12-19 02:37:44 +00001089 missing = _PyType_Lookup(Py_TYPE(mp), missing_str);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001090 if (missing != NULL)
1091 return PyObject_CallFunctionObjArgs(missing,
1092 (PyObject *)mp, key, NULL);
1093 }
Georg Brandlb9f4ad32006-10-29 18:31:42 +00001094 set_key_error(key);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001095 return NULL;
1096 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001097 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001098 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001099 return v;
1100}
1101
1102static int
Brett Cannon77ae87c2007-10-10 00:07:50 +00001103dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001104{
1105 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001106 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001107 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001108 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001109}
1110
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001111static PyMappingMethods dict_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001112 (lenfunc)dict_length, /*mp_length*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001113 (binaryfunc)dict_subscript, /*mp_subscript*/
1114 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001115};
1116
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001117static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001118dict_keys(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001119{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001120 register PyObject *v;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001121 register Py_ssize_t i, j;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001122 PyDictEntry *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001123 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001124
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001125 again:
1126 n = mp->ma_used;
1127 v = PyList_New(n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001128 if (v == NULL)
1129 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001130 if (n != mp->ma_used) {
1131 /* Durnit. The allocations caused the dict to resize.
1132 * Just start over, this shouldn't normally happen.
1133 */
1134 Py_DECREF(v);
1135 goto again;
1136 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001137 ep = mp->ma_table;
1138 mask = mp->ma_mask;
1139 for (i = 0, j = 0; i <= mask; i++) {
1140 if (ep[i].me_value != NULL) {
1141 PyObject *key = ep[i].me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001142 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001143 PyList_SET_ITEM(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001144 j++;
1145 }
1146 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001147 assert(j == n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001148 return v;
1149}
1150
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001151static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001152dict_values(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001153{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001154 register PyObject *v;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001155 register Py_ssize_t i, j;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001156 PyDictEntry *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001157 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001158
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001159 again:
1160 n = mp->ma_used;
1161 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001162 if (v == NULL)
1163 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001164 if (n != mp->ma_used) {
1165 /* Durnit. The allocations caused the dict to resize.
1166 * Just start over, this shouldn't normally happen.
1167 */
1168 Py_DECREF(v);
1169 goto again;
1170 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001171 ep = mp->ma_table;
1172 mask = mp->ma_mask;
1173 for (i = 0, j = 0; i <= mask; i++) {
1174 if (ep[i].me_value != NULL) {
1175 PyObject *value = ep[i].me_value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001176 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001177 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001178 j++;
1179 }
1180 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001181 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001182 return v;
1183}
1184
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001185static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001186dict_items(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001187{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001188 register PyObject *v;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001189 register Py_ssize_t i, j, n;
1190 Py_ssize_t mask;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001191 PyObject *item, *key, *value;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001192 PyDictEntry *ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001193
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001194 /* Preallocate the list of tuples, to avoid allocations during
1195 * the loop over the items, which could trigger GC, which
1196 * could resize the dict. :-(
1197 */
1198 again:
1199 n = mp->ma_used;
1200 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001201 if (v == NULL)
1202 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001203 for (i = 0; i < n; i++) {
1204 item = PyTuple_New(2);
1205 if (item == NULL) {
1206 Py_DECREF(v);
1207 return NULL;
1208 }
1209 PyList_SET_ITEM(v, i, item);
1210 }
1211 if (n != mp->ma_used) {
1212 /* Durnit. The allocations caused the dict to resize.
1213 * Just start over, this shouldn't normally happen.
1214 */
1215 Py_DECREF(v);
1216 goto again;
1217 }
1218 /* Nothing we do below makes any function calls. */
Raymond Hettinger43442782004-03-17 21:55:03 +00001219 ep = mp->ma_table;
1220 mask = mp->ma_mask;
1221 for (i = 0, j = 0; i <= mask; i++) {
Raymond Hettinger06905122004-03-19 10:30:00 +00001222 if ((value=ep[i].me_value) != NULL) {
Raymond Hettinger43442782004-03-17 21:55:03 +00001223 key = ep[i].me_key;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001224 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001225 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001226 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001227 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001228 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001229 j++;
1230 }
1231 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001232 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001233 return v;
1234}
1235
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001236static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001237dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001238{
1239 PyObject *seq;
1240 PyObject *value = Py_None;
1241 PyObject *it; /* iter(seq) */
1242 PyObject *key;
1243 PyObject *d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001244 int status;
1245
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001246 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001247 return NULL;
1248
1249 d = PyObject_CallObject(cls, NULL);
1250 if (d == NULL)
1251 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001252
Raymond Hettingercdcf8872007-11-07 02:26:17 +00001253 if (PyDict_CheckExact(d) && PyDict_CheckExact(seq)) {
1254 PyDictObject *mp = (PyDictObject *)d;
1255 PyObject *oldvalue;
1256 Py_ssize_t pos = 0;
1257 PyObject *key;
1258 long hash;
1259
Raymond Hettingera27474c2008-06-28 22:16:53 +00001260 if (dictresize(mp, Py_SIZE(seq)))
Raymond Hettingercdcf8872007-11-07 02:26:17 +00001261 return NULL;
1262
1263 while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
1264 Py_INCREF(key);
1265 Py_INCREF(value);
1266 if (insertdict(mp, key, hash, value))
1267 return NULL;
1268 }
1269 return d;
1270 }
1271
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00001272 if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) {
Brett Cannon77ae87c2007-10-10 00:07:50 +00001273 PyDictObject *mp = (PyDictObject *)d;
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00001274 Py_ssize_t pos = 0;
1275 PyObject *key;
1276 long hash;
1277
1278 if (dictresize(mp, PySet_GET_SIZE(seq)))
1279 return NULL;
1280
1281 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
1282 Py_INCREF(key);
Raymond Hettingere3146f52007-03-21 20:33:57 +00001283 Py_INCREF(value);
1284 if (insertdict(mp, key, hash, value))
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00001285 return NULL;
1286 }
1287 return d;
1288 }
1289
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001290 it = PyObject_GetIter(seq);
1291 if (it == NULL){
1292 Py_DECREF(d);
1293 return NULL;
1294 }
1295
Raymond Hettinger34448792007-11-09 23:14:44 +00001296 if (PyDict_CheckExact(d)) {
1297 while ((key = PyIter_Next(it)) != NULL) {
1298 status = PyDict_SetItem(d, key, value);
1299 Py_DECREF(key);
1300 if (status < 0)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001301 goto Fail;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001302 }
Raymond Hettinger34448792007-11-09 23:14:44 +00001303 } else {
1304 while ((key = PyIter_Next(it)) != NULL) {
1305 status = PyObject_SetItem(d, key, value);
1306 Py_DECREF(key);
1307 if (status < 0)
1308 goto Fail;
1309 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001310 }
1311
Raymond Hettinger34448792007-11-09 23:14:44 +00001312 if (PyErr_Occurred())
1313 goto Fail;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001314 Py_DECREF(it);
1315 return d;
1316
1317Fail:
1318 Py_DECREF(it);
1319 Py_DECREF(d);
1320 return NULL;
1321}
1322
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001323static int
1324dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001325{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001326 PyObject *arg = NULL;
1327 int result = 0;
1328
1329 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1330 result = -1;
1331
1332 else if (arg != NULL) {
1333 if (PyObject_HasAttrString(arg, "keys"))
1334 result = PyDict_Merge(self, arg, 1);
1335 else
1336 result = PyDict_MergeFromSeq2(self, arg, 1);
1337 }
1338 if (result == 0 && kwds != NULL)
1339 result = PyDict_Merge(self, kwds, 1);
1340 return result;
1341}
1342
1343static PyObject *
1344dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1345{
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001346 if (dict_update_common(self, args, kwds, "update") != -1)
1347 Py_RETURN_NONE;
1348 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001349}
1350
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001351/* Update unconditionally replaces existing items.
1352 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001353 otherwise it leaves existing items unchanged.
1354
1355 PyDict_{Update,Merge} update/merge from a mapping object.
1356
Tim Petersf582b822001-12-11 18:51:08 +00001357 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001358 producing iterable objects of length 2.
1359*/
1360
Tim Petersf582b822001-12-11 18:51:08 +00001361int
Tim Peters1fc240e2001-10-26 05:06:50 +00001362PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1363{
1364 PyObject *it; /* iter(seq2) */
Tim Peters9b10f7e2006-05-30 04:16:25 +00001365 Py_ssize_t i; /* index into seq2 of current element */
Tim Peters1fc240e2001-10-26 05:06:50 +00001366 PyObject *item; /* seq2[i] */
1367 PyObject *fast; /* item as a 2-tuple or 2-list */
1368
1369 assert(d != NULL);
1370 assert(PyDict_Check(d));
1371 assert(seq2 != NULL);
1372
1373 it = PyObject_GetIter(seq2);
1374 if (it == NULL)
1375 return -1;
1376
1377 for (i = 0; ; ++i) {
1378 PyObject *key, *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001379 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001380
1381 fast = NULL;
1382 item = PyIter_Next(it);
1383 if (item == NULL) {
1384 if (PyErr_Occurred())
1385 goto Fail;
1386 break;
1387 }
1388
1389 /* Convert item to sequence, and verify length 2. */
1390 fast = PySequence_Fast(item, "");
1391 if (fast == NULL) {
1392 if (PyErr_ExceptionMatches(PyExc_TypeError))
1393 PyErr_Format(PyExc_TypeError,
1394 "cannot convert dictionary update "
Neal Norwitzd3881b02006-05-30 04:25:05 +00001395 "sequence element #%zd to a sequence",
Tim Peters1fc240e2001-10-26 05:06:50 +00001396 i);
1397 goto Fail;
1398 }
1399 n = PySequence_Fast_GET_SIZE(fast);
1400 if (n != 2) {
1401 PyErr_Format(PyExc_ValueError,
Neal Norwitzd3881b02006-05-30 04:25:05 +00001402 "dictionary update sequence element #%zd "
Martin v. Löwis2c95cc62006-02-16 06:54:25 +00001403 "has length %zd; 2 is required",
Martin v. Löwise0e89f72006-02-16 06:59:22 +00001404 i, n);
Tim Peters1fc240e2001-10-26 05:06:50 +00001405 goto Fail;
1406 }
1407
1408 /* Update/merge with this (key, value) pair. */
1409 key = PySequence_Fast_GET_ITEM(fast, 0);
1410 value = PySequence_Fast_GET_ITEM(fast, 1);
1411 if (override || PyDict_GetItem(d, key) == NULL) {
1412 int status = PyDict_SetItem(d, key, value);
1413 if (status < 0)
1414 goto Fail;
1415 }
1416 Py_DECREF(fast);
1417 Py_DECREF(item);
1418 }
1419
1420 i = 0;
1421 goto Return;
1422Fail:
1423 Py_XDECREF(item);
1424 Py_XDECREF(fast);
1425 i = -1;
1426Return:
1427 Py_DECREF(it);
Neal Norwitzd3881b02006-05-30 04:25:05 +00001428 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001429}
1430
Tim Peters6d6c1a32001-08-02 04:15:00 +00001431int
1432PyDict_Update(PyObject *a, PyObject *b)
1433{
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001434 return PyDict_Merge(a, b, 1);
1435}
1436
1437int
1438PyDict_Merge(PyObject *a, PyObject *b, int override)
1439{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001440 register PyDictObject *mp, *other;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001441 register Py_ssize_t i;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001442 PyDictEntry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001443
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001444 /* We accept for the argument either a concrete dictionary object,
1445 * or an abstract "mapping" object. For the former, we can do
1446 * things quite efficiently. For the latter, we only require that
1447 * PyMapping_Keys() and PyObject_GetItem() be supported.
1448 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001449 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1450 PyErr_BadInternalCall();
1451 return -1;
1452 }
Brett Cannon77ae87c2007-10-10 00:07:50 +00001453 mp = (PyDictObject*)a;
Raymond Hettinger3dbd4c52008-01-25 19:24:46 +00001454 if (PyDict_Check(b)) {
Brett Cannon77ae87c2007-10-10 00:07:50 +00001455 other = (PyDictObject*)b;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001456 if (other == mp || other->ma_used == 0)
1457 /* a.update(a) or a.update({}); nothing to do */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001458 return 0;
Raymond Hettinger186e7392005-05-14 18:08:25 +00001459 if (mp->ma_used == 0)
1460 /* Since the target dict is empty, PyDict_GetItem()
1461 * always returns NULL. Setting override to 1
1462 * skips the unnecessary test.
1463 */
1464 override = 1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001465 /* Do one big resize at the start, rather than
1466 * incrementally resizing as we insert new items. Expect
1467 * that there will be no (or few) overlapping keys.
1468 */
1469 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
Raymond Hettingerc8d22902003-05-07 00:49:40 +00001470 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001471 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001472 }
1473 for (i = 0; i <= other->ma_mask; i++) {
1474 entry = &other->ma_table[i];
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001475 if (entry->me_value != NULL &&
1476 (override ||
1477 PyDict_GetItem(a, entry->me_key) == NULL)) {
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001478 Py_INCREF(entry->me_key);
1479 Py_INCREF(entry->me_value);
Armin Rigo35f6d362006-06-01 13:19:12 +00001480 if (insertdict(mp, entry->me_key,
1481 (long)entry->me_hash,
1482 entry->me_value) != 0)
1483 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001484 }
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001485 }
1486 }
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001487 else {
1488 /* Do it the generic, slower way */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001489 PyObject *keys = PyMapping_Keys(b);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001490 PyObject *iter;
1491 PyObject *key, *value;
1492 int status;
1493
1494 if (keys == NULL)
1495 /* Docstring says this is equivalent to E.keys() so
1496 * if E doesn't have a .keys() method we want
1497 * AttributeError to percolate up. Might as well
1498 * do the same for any other error.
1499 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001500 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001501
1502 iter = PyObject_GetIter(keys);
1503 Py_DECREF(keys);
1504 if (iter == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001505 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001506
1507 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001508 if (!override && PyDict_GetItem(a, key) != NULL) {
1509 Py_DECREF(key);
1510 continue;
1511 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001512 value = PyObject_GetItem(b, key);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001513 if (value == NULL) {
1514 Py_DECREF(iter);
1515 Py_DECREF(key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001516 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001517 }
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001518 status = PyDict_SetItem(a, key, value);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001519 Py_DECREF(key);
1520 Py_DECREF(value);
1521 if (status < 0) {
1522 Py_DECREF(iter);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001523 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001524 }
1525 }
1526 Py_DECREF(iter);
1527 if (PyErr_Occurred())
1528 /* Iterator completed, via error */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001529 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001530 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001531 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001532}
1533
1534static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001535dict_copy(register PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001536{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001537 return PyDict_Copy((PyObject*)mp);
1538}
1539
1540PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001541PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001542{
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001543 PyObject *copy;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001544
1545 if (o == NULL || !PyDict_Check(o)) {
1546 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001547 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001548 }
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001549 copy = PyDict_New();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001550 if (copy == NULL)
1551 return NULL;
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001552 if (PyDict_Merge(copy, o, 1) == 0)
1553 return copy;
1554 Py_DECREF(copy);
Raymond Hettinger1356f782005-04-15 15:58:42 +00001555 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001556}
1557
Martin v. Löwis18e16552006-02-15 17:27:45 +00001558Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00001559PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001560{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001561 if (mp == NULL || !PyDict_Check(mp)) {
1562 PyErr_BadInternalCall();
Jeremy Hylton7083bb72004-02-17 20:10:11 +00001563 return -1;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001564 }
Brett Cannon77ae87c2007-10-10 00:07:50 +00001565 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001566}
1567
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001568PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001569PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001570{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001571 if (mp == NULL || !PyDict_Check(mp)) {
1572 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001573 return NULL;
1574 }
Brett Cannon77ae87c2007-10-10 00:07:50 +00001575 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001576}
1577
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001578PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001579PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001580{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001581 if (mp == NULL || !PyDict_Check(mp)) {
1582 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001583 return NULL;
1584 }
Brett Cannon77ae87c2007-10-10 00:07:50 +00001585 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001586}
1587
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001588PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001589PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001590{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001591 if (mp == NULL || !PyDict_Check(mp)) {
1592 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001593 return NULL;
1594 }
Brett Cannon77ae87c2007-10-10 00:07:50 +00001595 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001596}
1597
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001598/* Subroutine which returns the smallest key in a for which b's value
1599 is different or absent. The value is returned too, through the
Tim Peters95bf9392001-05-10 08:32:44 +00001600 pval argument. Both are NULL if no key in a is found for which b's status
1601 differs. The refcounts on (and only on) non-NULL *pval and function return
1602 values must be decremented by the caller (characterize() increments them
1603 to ensure that mutating comparison and PyDict_GetItem calls can't delete
1604 them before the caller is done looking at them). */
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001605
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001606static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001607characterize(PyDictObject *a, PyDictObject *b, PyObject **pval)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001608{
Tim Peters95bf9392001-05-10 08:32:44 +00001609 PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */
1610 PyObject *aval = NULL; /* a[akey] */
Tim Peters9b10f7e2006-05-30 04:16:25 +00001611 Py_ssize_t i;
1612 int cmp;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001613
Tim Petersafb6ae82001-06-04 21:00:21 +00001614 for (i = 0; i <= a->ma_mask; i++) {
Tim Peters95bf9392001-05-10 08:32:44 +00001615 PyObject *thiskey, *thisaval, *thisbval;
1616 if (a->ma_table[i].me_value == NULL)
1617 continue;
1618 thiskey = a->ma_table[i].me_key;
1619 Py_INCREF(thiskey); /* keep alive across compares */
1620 if (akey != NULL) {
1621 cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT);
1622 if (cmp < 0) {
1623 Py_DECREF(thiskey);
1624 goto Fail;
1625 }
1626 if (cmp > 0 ||
Tim Petersafb6ae82001-06-04 21:00:21 +00001627 i > a->ma_mask ||
Tim Peters95bf9392001-05-10 08:32:44 +00001628 a->ma_table[i].me_value == NULL)
1629 {
1630 /* Not the *smallest* a key; or maybe it is
1631 * but the compare shrunk the dict so we can't
1632 * find its associated value anymore; or
1633 * maybe it is but the compare deleted the
1634 * a[thiskey] entry.
Neal Norwitzd3881b02006-05-30 04:25:05 +00001635 */
Tim Peters95bf9392001-05-10 08:32:44 +00001636 Py_DECREF(thiskey);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001637 continue;
Guido van Rossumb9324202001-01-18 00:39:02 +00001638 }
Tim Peters95bf9392001-05-10 08:32:44 +00001639 }
1640
1641 /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */
1642 thisaval = a->ma_table[i].me_value;
1643 assert(thisaval);
1644 Py_INCREF(thisaval); /* keep alive */
1645 thisbval = PyDict_GetItem((PyObject *)b, thiskey);
1646 if (thisbval == NULL)
1647 cmp = 0;
1648 else {
1649 /* both dicts have thiskey: same values? */
1650 cmp = PyObject_RichCompareBool(
1651 thisaval, thisbval, Py_EQ);
1652 if (cmp < 0) {
1653 Py_DECREF(thiskey);
1654 Py_DECREF(thisaval);
1655 goto Fail;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001656 }
1657 }
Tim Peters95bf9392001-05-10 08:32:44 +00001658 if (cmp == 0) {
1659 /* New winner. */
1660 Py_XDECREF(akey);
1661 Py_XDECREF(aval);
1662 akey = thiskey;
1663 aval = thisaval;
1664 }
1665 else {
1666 Py_DECREF(thiskey);
1667 Py_DECREF(thisaval);
1668 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001669 }
Tim Peters95bf9392001-05-10 08:32:44 +00001670 *pval = aval;
1671 return akey;
1672
1673Fail:
1674 Py_XDECREF(akey);
1675 Py_XDECREF(aval);
1676 *pval = NULL;
1677 return NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001678}
1679
1680static int
Brett Cannon77ae87c2007-10-10 00:07:50 +00001681dict_compare(PyDictObject *a, PyDictObject *b)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001682{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001683 PyObject *adiff, *bdiff, *aval, *bval;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001684 int res;
1685
1686 /* Compare lengths first */
1687 if (a->ma_used < b->ma_used)
1688 return -1; /* a is shorter */
1689 else if (a->ma_used > b->ma_used)
1690 return 1; /* b is shorter */
Tim Peters95bf9392001-05-10 08:32:44 +00001691
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001692 /* Same length -- check all keys */
Tim Peters95bf9392001-05-10 08:32:44 +00001693 bdiff = bval = NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001694 adiff = characterize(a, b, &aval);
Tim Peters95bf9392001-05-10 08:32:44 +00001695 if (adiff == NULL) {
1696 assert(!aval);
Tim Peters3918fb22001-05-10 18:58:31 +00001697 /* Either an error, or a is a subset with the same length so
Tim Peters95bf9392001-05-10 08:32:44 +00001698 * must be equal.
1699 */
1700 res = PyErr_Occurred() ? -1 : 0;
1701 goto Finished;
1702 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001703 bdiff = characterize(b, a, &bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001704 if (bdiff == NULL && PyErr_Occurred()) {
1705 assert(!bval);
1706 res = -1;
1707 goto Finished;
1708 }
1709 res = 0;
1710 if (bdiff) {
1711 /* bdiff == NULL "should be" impossible now, but perhaps
1712 * the last comparison done by the characterize() on a had
1713 * the side effect of making the dicts equal!
1714 */
1715 res = PyObject_Compare(adiff, bdiff);
1716 }
1717 if (res == 0 && bval != NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001718 res = PyObject_Compare(aval, bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001719
1720Finished:
1721 Py_XDECREF(adiff);
1722 Py_XDECREF(bdiff);
1723 Py_XDECREF(aval);
1724 Py_XDECREF(bval);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001725 return res;
1726}
1727
Tim Peterse63415e2001-05-08 04:38:29 +00001728/* Return 1 if dicts equal, 0 if not, -1 if error.
1729 * Gets out as soon as any difference is detected.
1730 * Uses only Py_EQ comparison.
1731 */
1732static int
Brett Cannon77ae87c2007-10-10 00:07:50 +00001733dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00001734{
Tim Peters9b10f7e2006-05-30 04:16:25 +00001735 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00001736
1737 if (a->ma_used != b->ma_used)
1738 /* can't be equal if # of entries differ */
1739 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001740
Tim Peterse63415e2001-05-08 04:38:29 +00001741 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Tim Petersafb6ae82001-06-04 21:00:21 +00001742 for (i = 0; i <= a->ma_mask; i++) {
Tim Peterse63415e2001-05-08 04:38:29 +00001743 PyObject *aval = a->ma_table[i].me_value;
1744 if (aval != NULL) {
1745 int cmp;
1746 PyObject *bval;
1747 PyObject *key = a->ma_table[i].me_key;
1748 /* temporarily bump aval's refcount to ensure it stays
1749 alive until we're done with it */
1750 Py_INCREF(aval);
Neal Norwitza22975f2006-09-05 02:24:03 +00001751 /* ditto for key */
1752 Py_INCREF(key);
Tim Peterse63415e2001-05-08 04:38:29 +00001753 bval = PyDict_GetItem((PyObject *)b, key);
Neal Norwitza22975f2006-09-05 02:24:03 +00001754 Py_DECREF(key);
Tim Peterse63415e2001-05-08 04:38:29 +00001755 if (bval == NULL) {
1756 Py_DECREF(aval);
1757 return 0;
1758 }
1759 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1760 Py_DECREF(aval);
1761 if (cmp <= 0) /* error or not equal */
1762 return cmp;
1763 }
1764 }
1765 return 1;
1766 }
1767
1768static PyObject *
1769dict_richcompare(PyObject *v, PyObject *w, int op)
1770{
1771 int cmp;
1772 PyObject *res;
1773
1774 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1775 res = Py_NotImplemented;
1776 }
1777 else if (op == Py_EQ || op == Py_NE) {
Brett Cannon77ae87c2007-10-10 00:07:50 +00001778 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
Tim Peterse63415e2001-05-08 04:38:29 +00001779 if (cmp < 0)
1780 return NULL;
1781 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1782 }
Steven Bethardae42f332008-03-18 17:26:10 +00001783 else {
1784 /* Py3K warning if comparison isn't == or != */
Benjamin Peterson9f4f4812008-04-27 03:01:45 +00001785 if (PyErr_WarnPy3k("dict inequality comparisons not supported "
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00001786 "in 3.x", 1) < 0) {
Steven Bethardae42f332008-03-18 17:26:10 +00001787 return NULL;
1788 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001789 res = Py_NotImplemented;
Steven Bethardae42f332008-03-18 17:26:10 +00001790 }
Tim Peterse63415e2001-05-08 04:38:29 +00001791 Py_INCREF(res);
1792 return res;
1793 }
1794
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001795static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001796dict_contains(register PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001797{
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001798 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001799 PyDictEntry *ep;
Tim Petersd770ebd2006-06-01 15:50:44 +00001800
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001801 if (!PyString_CheckExact(key) ||
1802 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001803 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001804 if (hash == -1)
1805 return NULL;
1806 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001807 ep = (mp->ma_lookup)(mp, key, hash);
1808 if (ep == NULL)
1809 return NULL;
Tim Petersd770ebd2006-06-01 15:50:44 +00001810 return PyBool_FromLong(ep->me_value != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001811}
1812
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001813static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001814dict_has_key(register PyDictObject *mp, PyObject *key)
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +00001815{
Benjamin Peterson9f4f4812008-04-27 03:01:45 +00001816 if (PyErr_WarnPy3k("dict.has_key() not supported in 3.x; "
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00001817 "use the in operator", 1) < 0)
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +00001818 return NULL;
1819 return dict_contains(mp, key);
1820}
1821
1822static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001823dict_get(register PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001824{
1825 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001826 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001827 PyObject *val = NULL;
1828 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001829 PyDictEntry *ep;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001830
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001831 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001832 return NULL;
1833
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001834 if (!PyString_CheckExact(key) ||
1835 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Barry Warsawc38c5da1997-10-06 17:49:20 +00001836 hash = PyObject_Hash(key);
1837 if (hash == -1)
1838 return NULL;
1839 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001840 ep = (mp->ma_lookup)(mp, key, hash);
1841 if (ep == NULL)
1842 return NULL;
1843 val = ep->me_value;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001844 if (val == NULL)
1845 val = failobj;
1846 Py_INCREF(val);
1847 return val;
1848}
1849
1850
1851static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001852dict_setdefault(register PyDictObject *mp, PyObject *args)
Guido van Rossum164452c2000-08-08 16:12:54 +00001853{
1854 PyObject *key;
1855 PyObject *failobj = Py_None;
1856 PyObject *val = NULL;
1857 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001858 PyDictEntry *ep;
Guido van Rossum164452c2000-08-08 16:12:54 +00001859
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001860 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
Guido van Rossum164452c2000-08-08 16:12:54 +00001861 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001862
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001863 if (!PyString_CheckExact(key) ||
1864 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossum164452c2000-08-08 16:12:54 +00001865 hash = PyObject_Hash(key);
1866 if (hash == -1)
1867 return NULL;
1868 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001869 ep = (mp->ma_lookup)(mp, key, hash);
1870 if (ep == NULL)
1871 return NULL;
1872 val = ep->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001873 if (val == NULL) {
1874 val = failobj;
1875 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1876 val = NULL;
1877 }
1878 Py_XINCREF(val);
1879 return val;
1880}
1881
1882
1883static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001884dict_clear(register PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001885{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001886 PyDict_Clear((PyObject *)mp);
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001887 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001888}
1889
Guido van Rossumba6ab842000-12-12 22:02:18 +00001890static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001891dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00001892{
1893 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001894 PyDictEntry *ep;
Guido van Rossume027d982002-04-12 15:11:59 +00001895 PyObject *old_value, *old_key;
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001896 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001897
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001898 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1899 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001900 if (mp->ma_used == 0) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001901 if (deflt) {
1902 Py_INCREF(deflt);
1903 return deflt;
1904 }
Guido van Rossume027d982002-04-12 15:11:59 +00001905 PyErr_SetString(PyExc_KeyError,
1906 "pop(): dictionary is empty");
1907 return NULL;
1908 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001909 if (!PyString_CheckExact(key) ||
1910 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossume027d982002-04-12 15:11:59 +00001911 hash = PyObject_Hash(key);
1912 if (hash == -1)
1913 return NULL;
1914 }
1915 ep = (mp->ma_lookup)(mp, key, hash);
Armin Rigo35f6d362006-06-01 13:19:12 +00001916 if (ep == NULL)
1917 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001918 if (ep->me_value == NULL) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001919 if (deflt) {
1920 Py_INCREF(deflt);
1921 return deflt;
1922 }
Georg Brandlb9f4ad32006-10-29 18:31:42 +00001923 set_key_error(key);
Guido van Rossume027d982002-04-12 15:11:59 +00001924 return NULL;
1925 }
1926 old_key = ep->me_key;
1927 Py_INCREF(dummy);
1928 ep->me_key = dummy;
1929 old_value = ep->me_value;
1930 ep->me_value = NULL;
1931 mp->ma_used--;
1932 Py_DECREF(old_key);
1933 return old_value;
1934}
1935
1936static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001937dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001938{
Tim Peters9b10f7e2006-05-30 04:16:25 +00001939 Py_ssize_t i = 0;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001940 PyDictEntry *ep;
Guido van Rossumba6ab842000-12-12 22:02:18 +00001941 PyObject *res;
1942
Tim Petersf4b33f62001-06-02 05:42:29 +00001943 /* Allocate the result tuple before checking the size. Believe it
1944 * or not, this allocation could trigger a garbage collection which
1945 * could empty the dict, so if we checked the size first and that
1946 * happened, the result would be an infinite loop (searching for an
1947 * entry that no longer exists). Note that the usual popitem()
1948 * idiom is "while d: k, v = d.popitem()". so needing to throw the
Tim Peters9b10f7e2006-05-30 04:16:25 +00001949 * tuple away if the dict *is* empty isn't a significant
Tim Petersf4b33f62001-06-02 05:42:29 +00001950 * inefficiency -- possible, but unlikely in practice.
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001951 */
1952 res = PyTuple_New(2);
1953 if (res == NULL)
1954 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001955 if (mp->ma_used == 0) {
1956 Py_DECREF(res);
1957 PyErr_SetString(PyExc_KeyError,
1958 "popitem(): dictionary is empty");
1959 return NULL;
1960 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001961 /* Set ep to "the first" dict entry with a value. We abuse the hash
1962 * field of slot 0 to hold a search finger:
1963 * If slot 0 has a value, use slot 0.
1964 * Else slot 0 is being used to hold a search finger,
Neal Norwitzd3881b02006-05-30 04:25:05 +00001965 * and we use its hash value as the first index to look.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001966 */
1967 ep = &mp->ma_table[0];
1968 if (ep->me_value == NULL) {
Tim Peters9b10f7e2006-05-30 04:16:25 +00001969 i = ep->me_hash;
Tim Petersdea48ec2001-05-22 20:40:22 +00001970 /* The hash field may be a real hash value, or it may be a
1971 * legit search finger, or it may be a once-legit search
1972 * finger that's out of bounds now because it wrapped around
1973 * or the table shrunk -- simply make sure it's in bounds now.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001974 */
Tim Petersafb6ae82001-06-04 21:00:21 +00001975 if (i > mp->ma_mask || i < 1)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001976 i = 1; /* skip slot 0 */
1977 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1978 i++;
Tim Petersafb6ae82001-06-04 21:00:21 +00001979 if (i > mp->ma_mask)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001980 i = 1;
1981 }
1982 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001983 PyTuple_SET_ITEM(res, 0, ep->me_key);
1984 PyTuple_SET_ITEM(res, 1, ep->me_value);
1985 Py_INCREF(dummy);
1986 ep->me_key = dummy;
1987 ep->me_value = NULL;
1988 mp->ma_used--;
1989 assert(mp->ma_table[0].me_value == NULL);
1990 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001991 return res;
1992}
1993
Jeremy Hylton8caad492000-06-23 14:18:11 +00001994static int
1995dict_traverse(PyObject *op, visitproc visit, void *arg)
1996{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001997 Py_ssize_t i = 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001998 PyObject *pk;
1999 PyObject *pv;
2000
2001 while (PyDict_Next(op, &i, &pk, &pv)) {
Thomas Woutersc6e55062006-04-15 21:47:09 +00002002 Py_VISIT(pk);
2003 Py_VISIT(pv);
Jeremy Hylton8caad492000-06-23 14:18:11 +00002004 }
2005 return 0;
2006}
2007
2008static int
2009dict_tp_clear(PyObject *op)
2010{
2011 PyDict_Clear(op);
2012 return 0;
2013}
2014
Tim Petersf7f88b12000-12-13 23:18:45 +00002015
Raymond Hettinger019a1482004-03-18 02:41:19 +00002016extern PyTypeObject PyDictIterKey_Type; /* Forward */
2017extern PyTypeObject PyDictIterValue_Type; /* Forward */
2018extern PyTypeObject PyDictIterItem_Type; /* Forward */
Brett Cannon77ae87c2007-10-10 00:07:50 +00002019static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002020
2021static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002022dict_iterkeys(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002023{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002024 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002025}
2026
2027static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002028dict_itervalues(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002029{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002030 return dictiter_new(dict, &PyDictIterValue_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002031}
2032
2033static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002034dict_iteritems(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002035{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002036 return dictiter_new(dict, &PyDictIterItem_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002037}
2038
Robert Schuppenies51df0642008-06-01 16:16:17 +00002039static PyObject *
2040dict_sizeof(PyDictObject *mp)
2041{
2042 Py_ssize_t res;
2043
Robert Schuppenies161b9212008-06-26 15:20:35 +00002044 res = sizeof(PyDictObject);
Robert Schuppenies51df0642008-06-01 16:16:17 +00002045 if (mp->ma_table != mp->ma_smalltable)
2046 res = res + (mp->ma_mask + 1) * sizeof(PyDictEntry);
2047 return PyInt_FromSsize_t(res);
2048}
Guido van Rossum09e563a2001-05-01 12:10:21 +00002049
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002050PyDoc_STRVAR(has_key__doc__,
Raymond Hettinger574aa322003-09-01 22:12:08 +00002051"D.has_key(k) -> True if D has a key k, else False");
Tim Petersf7f88b12000-12-13 23:18:45 +00002052
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002053PyDoc_STRVAR(contains__doc__,
2054"D.__contains__(k) -> True if D has a key k, else False");
2055
2056PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
2057
Robert Schuppenies51df0642008-06-01 16:16:17 +00002058PyDoc_STRVAR(sizeof__doc__,
Georg Brandl7a6de8b2008-06-01 16:42:16 +00002059"D.__sizeof__() -> size of D in memory, in bytes");
Robert Schuppenies51df0642008-06-01 16:16:17 +00002060
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002061PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002062"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002063
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002064PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002065"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 +00002066
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002067PyDoc_STRVAR(pop__doc__,
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002068"D.pop(k[,d]) -> v, remove specified key and return the corresponding value\n\
2069If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00002070
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002071PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00002072"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000020732-tuple; but raise KeyError if D is empty");
Tim Petersf7f88b12000-12-13 23:18:45 +00002074
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002075PyDoc_STRVAR(keys__doc__,
2076"D.keys() -> list of D's keys");
Tim Petersf7f88b12000-12-13 23:18:45 +00002077
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002078PyDoc_STRVAR(items__doc__,
2079"D.items() -> list of D's (key, value) pairs, as 2-tuples");
Tim Petersf7f88b12000-12-13 23:18:45 +00002080
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002081PyDoc_STRVAR(values__doc__,
2082"D.values() -> list of D's values");
Tim Petersf7f88b12000-12-13 23:18:45 +00002083
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002084PyDoc_STRVAR(update__doc__,
Walter Dörwaldd70ad8a2004-05-28 20:59:21 +00002085"D.update(E, **F) -> None. Update D from E and F: for k in E: D[k] = E[k]\n\
2086(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 +00002087
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002088PyDoc_STRVAR(fromkeys__doc__,
2089"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
2090v defaults to None.");
2091
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002092PyDoc_STRVAR(clear__doc__,
2093"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002094
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002095PyDoc_STRVAR(copy__doc__,
2096"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00002097
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002098PyDoc_STRVAR(iterkeys__doc__,
2099"D.iterkeys() -> an iterator over the keys of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00002100
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002101PyDoc_STRVAR(itervalues__doc__,
2102"D.itervalues() -> an iterator over the values of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00002103
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002104PyDoc_STRVAR(iteritems__doc__,
2105"D.iteritems() -> an iterator over the (key, value) items of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00002106
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002107static PyMethodDef mapp_methods[] = {
Neal Norwitzc7926292007-05-23 06:57:35 +00002108 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002109 contains__doc__},
Raymond Hettinger0c669672003-12-13 13:31:55 +00002110 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002111 getitem__doc__},
Robert Schuppenies51df0642008-06-01 16:16:17 +00002112 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
2113 sizeof__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002114 {"has_key", (PyCFunction)dict_has_key, METH_O,
Tim Petersf7f88b12000-12-13 23:18:45 +00002115 has_key__doc__},
2116 {"get", (PyCFunction)dict_get, METH_VARARGS,
2117 get__doc__},
2118 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
2119 setdefault_doc__},
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002120 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
Just van Rossuma797d812002-11-23 09:45:04 +00002121 pop__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002122 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002123 popitem__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002124 {"keys", (PyCFunction)dict_keys, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002125 keys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002126 {"items", (PyCFunction)dict_items, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002127 items__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002128 {"values", (PyCFunction)dict_values, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002129 values__doc__},
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002130 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002131 update__doc__},
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002132 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
2133 fromkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002134 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002135 clear__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002136 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002137 copy__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002138 {"iterkeys", (PyCFunction)dict_iterkeys, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00002139 iterkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002140 {"itervalues", (PyCFunction)dict_itervalues, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00002141 itervalues__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002142 {"iteritems", (PyCFunction)dict_iteritems, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00002143 iteritems__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00002144 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002145};
2146
Tim Petersd770ebd2006-06-01 15:50:44 +00002147/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002148int
2149PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002150{
2151 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +00002152 PyDictObject *mp = (PyDictObject *)op;
2153 PyDictEntry *ep;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002154
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002155 if (!PyString_CheckExact(key) ||
2156 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002157 hash = PyObject_Hash(key);
2158 if (hash == -1)
2159 return -1;
2160 }
Armin Rigo35f6d362006-06-01 13:19:12 +00002161 ep = (mp->ma_lookup)(mp, key, hash);
Tim Petersd770ebd2006-06-01 15:50:44 +00002162 return ep == NULL ? -1 : (ep->me_value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002163}
2164
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +00002165/* Internal version of PyDict_Contains used when the hash value is already known */
2166int
2167_PyDict_Contains(PyObject *op, PyObject *key, long hash)
2168{
Brett Cannon77ae87c2007-10-10 00:07:50 +00002169 PyDictObject *mp = (PyDictObject *)op;
2170 PyDictEntry *ep;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +00002171
2172 ep = (mp->ma_lookup)(mp, key, hash);
2173 return ep == NULL ? -1 : (ep->me_value != NULL);
2174}
2175
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002176/* Hack to implement "key in dict" */
2177static PySequenceMethods dict_as_sequence = {
Georg Brandl347b3002006-03-30 11:57:00 +00002178 0, /* sq_length */
2179 0, /* sq_concat */
2180 0, /* sq_repeat */
2181 0, /* sq_item */
2182 0, /* sq_slice */
2183 0, /* sq_ass_item */
2184 0, /* sq_ass_slice */
2185 PyDict_Contains, /* sq_contains */
2186 0, /* sq_inplace_concat */
2187 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002188};
2189
Guido van Rossum09e563a2001-05-01 12:10:21 +00002190static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002191dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2192{
2193 PyObject *self;
2194
2195 assert(type != NULL && type->tp_alloc != NULL);
2196 self = type->tp_alloc(type, 0);
2197 if (self != NULL) {
2198 PyDictObject *d = (PyDictObject *)self;
2199 /* It's guaranteed that tp->alloc zeroed out the struct. */
2200 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
2201 INIT_NONZERO_DICT_SLOTS(d);
2202 d->ma_lookup = lookdict_string;
2203#ifdef SHOW_CONVERSION_COUNTS
2204 ++created;
2205#endif
2206 }
2207 return self;
2208}
2209
Tim Peters25786c02001-09-02 08:22:48 +00002210static int
2211dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2212{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002213 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002214}
2215
Tim Peters6d6c1a32001-08-02 04:15:00 +00002216static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002217dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002218{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002219 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002220}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002221
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002222PyDoc_STRVAR(dictionary_doc,
Tim Petersa427a2b2001-10-29 22:25:45 +00002223"dict() -> new empty dictionary.\n"
2224"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Tim Peters1fc240e2001-10-26 05:06:50 +00002225" (key, value) pairs.\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002226"dict(seq) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002227" d = {}\n"
2228" for k, v in seq:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002229" d[k] = v\n"
2230"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2231" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002232
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002233PyTypeObject PyDict_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002234 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Tim Petersa427a2b2001-10-29 22:25:45 +00002235 "dict",
Brett Cannon77ae87c2007-10-10 00:07:50 +00002236 sizeof(PyDictObject),
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002237 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00002238 (destructor)dict_dealloc, /* tp_dealloc */
2239 (printfunc)dict_print, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002240 0, /* tp_getattr */
Guido van Rossumb9324202001-01-18 00:39:02 +00002241 0, /* tp_setattr */
Tim Peters4fa58bf2001-05-10 21:45:19 +00002242 (cmpfunc)dict_compare, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00002243 (reprfunc)dict_repr, /* tp_repr */
2244 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002245 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00002246 &dict_as_mapping, /* tp_as_mapping */
Nick Coghlan53663a62008-07-15 14:27:37 +00002247 (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
Guido van Rossumb9324202001-01-18 00:39:02 +00002248 0, /* tp_call */
2249 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002250 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumb9324202001-01-18 00:39:02 +00002251 0, /* tp_setattro */
2252 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002253 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Neal Norwitzee3a1b52007-02-25 19:44:48 +00002254 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
Tim Peters25786c02001-09-02 08:22:48 +00002255 dictionary_doc, /* tp_doc */
Georg Brandl347b3002006-03-30 11:57:00 +00002256 dict_traverse, /* tp_traverse */
2257 dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00002258 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002259 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00002260 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002261 0, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002262 mapp_methods, /* tp_methods */
2263 0, /* tp_members */
2264 0, /* tp_getset */
2265 0, /* tp_base */
2266 0, /* tp_dict */
2267 0, /* tp_descr_get */
2268 0, /* tp_descr_set */
2269 0, /* tp_dictoffset */
Georg Brandl347b3002006-03-30 11:57:00 +00002270 dict_init, /* tp_init */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002271 PyType_GenericAlloc, /* tp_alloc */
2272 dict_new, /* tp_new */
Neil Schemenauer6189b892002-04-12 02:43:00 +00002273 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002274};
2275
Guido van Rossum3cca2451997-05-16 14:23:33 +00002276/* For backward compatibility with old dictionary interface */
2277
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002278PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002279PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002280{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002281 PyObject *kv, *rv;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002282 kv = PyString_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00002283 if (kv == NULL)
2284 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002285 rv = PyDict_GetItem(v, kv);
2286 Py_DECREF(kv);
2287 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002288}
2289
2290int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002291PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002292{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002293 PyObject *kv;
2294 int err;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002295 kv = PyString_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00002296 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002297 return -1;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002298 PyString_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00002299 err = PyDict_SetItem(v, kv, item);
2300 Py_DECREF(kv);
2301 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002302}
2303
2304int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002305PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002306{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002307 PyObject *kv;
2308 int err;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002309 kv = PyString_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00002310 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002311 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002312 err = PyDict_DelItem(v, kv);
2313 Py_DECREF(kv);
2314 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002315}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002316
Raymond Hettinger019a1482004-03-18 02:41:19 +00002317/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002318
2319typedef struct {
2320 PyObject_HEAD
Brett Cannon77ae87c2007-10-10 00:07:50 +00002321 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
Tim Peters9b10f7e2006-05-30 04:16:25 +00002322 Py_ssize_t di_used;
2323 Py_ssize_t di_pos;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002324 PyObject* di_result; /* reusable result tuple for iteritems */
Tim Peters9b10f7e2006-05-30 04:16:25 +00002325 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002326} dictiterobject;
2327
2328static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002329dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002330{
2331 dictiterobject *di;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002332 di = PyObject_New(dictiterobject, itertype);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002333 if (di == NULL)
2334 return NULL;
2335 Py_INCREF(dict);
2336 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00002337 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002338 di->di_pos = 0;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002339 di->len = dict->ma_used;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002340 if (itertype == &PyDictIterItem_Type) {
2341 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2342 if (di->di_result == NULL) {
2343 Py_DECREF(di);
2344 return NULL;
2345 }
2346 }
2347 else
2348 di->di_result = NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002349 return (PyObject *)di;
2350}
2351
2352static void
2353dictiter_dealloc(dictiterobject *di)
2354{
Guido van Rossum2147df72002-07-16 20:30:22 +00002355 Py_XDECREF(di->di_dict);
Raymond Hettinger019a1482004-03-18 02:41:19 +00002356 Py_XDECREF(di->di_result);
Neil Schemenauer6189b892002-04-12 02:43:00 +00002357 PyObject_Del(di);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002358}
2359
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002360static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002361dictiter_len(dictiterobject *di)
2362{
Tim Peters9b10f7e2006-05-30 04:16:25 +00002363 Py_ssize_t len = 0;
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00002364 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002365 len = di->len;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002366 return PyInt_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002367}
2368
Armin Rigof5b3e362006-02-11 21:32:43 +00002369PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002370
2371static PyMethodDef dictiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +00002372 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002373 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002374};
2375
Raymond Hettinger019a1482004-03-18 02:41:19 +00002376static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002377{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002378 PyObject *key;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002379 register Py_ssize_t i, mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +00002380 register PyDictEntry *ep;
2381 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002382
Raymond Hettinger019a1482004-03-18 02:41:19 +00002383 if (d == NULL)
Guido van Rossum2147df72002-07-16 20:30:22 +00002384 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002385 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002386
Raymond Hettinger019a1482004-03-18 02:41:19 +00002387 if (di->di_used != d->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00002388 PyErr_SetString(PyExc_RuntimeError,
2389 "dictionary changed size during iteration");
Guido van Rossum2147df72002-07-16 20:30:22 +00002390 di->di_used = -1; /* Make this state sticky */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002391 return NULL;
2392 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002393
Raymond Hettinger019a1482004-03-18 02:41:19 +00002394 i = di->di_pos;
2395 if (i < 0)
2396 goto fail;
2397 ep = d->ma_table;
2398 mask = d->ma_mask;
2399 while (i <= mask && ep[i].me_value == NULL)
2400 i++;
2401 di->di_pos = i+1;
2402 if (i > mask)
2403 goto fail;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002404 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002405 key = ep[i].me_key;
2406 Py_INCREF(key);
2407 return key;
2408
2409fail:
2410 Py_DECREF(d);
Guido van Rossum2147df72002-07-16 20:30:22 +00002411 di->di_dict = NULL;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002412 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002413}
2414
Raymond Hettinger019a1482004-03-18 02:41:19 +00002415PyTypeObject PyDictIterKey_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002416 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002417 "dictionary-keyiterator", /* tp_name */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002418 sizeof(dictiterobject), /* tp_basicsize */
2419 0, /* tp_itemsize */
2420 /* methods */
2421 (destructor)dictiter_dealloc, /* tp_dealloc */
2422 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002423 0, /* tp_getattr */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002424 0, /* tp_setattr */
2425 0, /* tp_compare */
2426 0, /* tp_repr */
2427 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002428 0, /* tp_as_sequence */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002429 0, /* tp_as_mapping */
2430 0, /* tp_hash */
2431 0, /* tp_call */
2432 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002433 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002434 0, /* tp_setattro */
2435 0, /* tp_as_buffer */
2436 Py_TPFLAGS_DEFAULT, /* tp_flags */
2437 0, /* tp_doc */
2438 0, /* tp_traverse */
2439 0, /* tp_clear */
2440 0, /* tp_richcompare */
2441 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002442 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002443 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002444 dictiter_methods, /* tp_methods */
2445 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002446};
2447
2448static PyObject *dictiter_iternextvalue(dictiterobject *di)
2449{
2450 PyObject *value;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002451 register Py_ssize_t i, mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +00002452 register PyDictEntry *ep;
2453 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002454
2455 if (d == NULL)
2456 return NULL;
2457 assert (PyDict_Check(d));
2458
2459 if (di->di_used != d->ma_used) {
2460 PyErr_SetString(PyExc_RuntimeError,
2461 "dictionary changed size during iteration");
2462 di->di_used = -1; /* Make this state sticky */
2463 return NULL;
2464 }
2465
2466 i = di->di_pos;
Guido van Rossum09240f62004-03-20 19:11:58 +00002467 mask = d->ma_mask;
2468 if (i < 0 || i > mask)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002469 goto fail;
2470 ep = d->ma_table;
Guido van Rossum09240f62004-03-20 19:11:58 +00002471 while ((value=ep[i].me_value) == NULL) {
Raymond Hettinger019a1482004-03-18 02:41:19 +00002472 i++;
Guido van Rossum09240f62004-03-20 19:11:58 +00002473 if (i > mask)
2474 goto fail;
2475 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002476 di->di_pos = i+1;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002477 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002478 Py_INCREF(value);
2479 return value;
2480
2481fail:
2482 Py_DECREF(d);
2483 di->di_dict = NULL;
2484 return NULL;
2485}
2486
2487PyTypeObject PyDictIterValue_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002488 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002489 "dictionary-valueiterator", /* tp_name */
2490 sizeof(dictiterobject), /* tp_basicsize */
2491 0, /* tp_itemsize */
2492 /* methods */
2493 (destructor)dictiter_dealloc, /* tp_dealloc */
2494 0, /* tp_print */
2495 0, /* tp_getattr */
2496 0, /* tp_setattr */
2497 0, /* tp_compare */
2498 0, /* tp_repr */
2499 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002500 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002501 0, /* tp_as_mapping */
2502 0, /* tp_hash */
2503 0, /* tp_call */
2504 0, /* tp_str */
2505 PyObject_GenericGetAttr, /* tp_getattro */
2506 0, /* tp_setattro */
2507 0, /* tp_as_buffer */
2508 Py_TPFLAGS_DEFAULT, /* tp_flags */
2509 0, /* tp_doc */
2510 0, /* tp_traverse */
2511 0, /* tp_clear */
2512 0, /* tp_richcompare */
2513 0, /* tp_weaklistoffset */
2514 PyObject_SelfIter, /* tp_iter */
2515 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002516 dictiter_methods, /* tp_methods */
2517 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002518};
2519
2520static PyObject *dictiter_iternextitem(dictiterobject *di)
2521{
2522 PyObject *key, *value, *result = di->di_result;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002523 register Py_ssize_t i, mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +00002524 register PyDictEntry *ep;
2525 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002526
2527 if (d == NULL)
2528 return NULL;
2529 assert (PyDict_Check(d));
2530
2531 if (di->di_used != d->ma_used) {
2532 PyErr_SetString(PyExc_RuntimeError,
2533 "dictionary changed size during iteration");
2534 di->di_used = -1; /* Make this state sticky */
2535 return NULL;
2536 }
2537
2538 i = di->di_pos;
2539 if (i < 0)
2540 goto fail;
2541 ep = d->ma_table;
2542 mask = d->ma_mask;
2543 while (i <= mask && ep[i].me_value == NULL)
2544 i++;
2545 di->di_pos = i+1;
2546 if (i > mask)
2547 goto fail;
2548
2549 if (result->ob_refcnt == 1) {
2550 Py_INCREF(result);
2551 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2552 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2553 } else {
2554 result = PyTuple_New(2);
Tim Peters60b29962006-01-01 01:19:23 +00002555 if (result == NULL)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002556 return NULL;
2557 }
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002558 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002559 key = ep[i].me_key;
2560 value = ep[i].me_value;
2561 Py_INCREF(key);
2562 Py_INCREF(value);
2563 PyTuple_SET_ITEM(result, 0, key);
2564 PyTuple_SET_ITEM(result, 1, value);
2565 return result;
2566
2567fail:
2568 Py_DECREF(d);
2569 di->di_dict = NULL;
2570 return NULL;
2571}
2572
2573PyTypeObject PyDictIterItem_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002574 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002575 "dictionary-itemiterator", /* tp_name */
2576 sizeof(dictiterobject), /* tp_basicsize */
2577 0, /* tp_itemsize */
2578 /* methods */
2579 (destructor)dictiter_dealloc, /* tp_dealloc */
2580 0, /* tp_print */
2581 0, /* tp_getattr */
2582 0, /* tp_setattr */
2583 0, /* tp_compare */
2584 0, /* tp_repr */
2585 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002586 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002587 0, /* tp_as_mapping */
2588 0, /* tp_hash */
2589 0, /* tp_call */
2590 0, /* tp_str */
2591 PyObject_GenericGetAttr, /* tp_getattro */
2592 0, /* tp_setattro */
2593 0, /* tp_as_buffer */
2594 Py_TPFLAGS_DEFAULT, /* tp_flags */
2595 0, /* tp_doc */
2596 0, /* tp_traverse */
2597 0, /* tp_clear */
2598 0, /* tp_richcompare */
2599 0, /* tp_weaklistoffset */
2600 PyObject_SelfIter, /* tp_iter */
2601 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002602 dictiter_methods, /* tp_methods */
2603 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002604};