blob: d55209890dfe51f901d35c08296915309b795c69 [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 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +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);
244 }
245 assert (mp->ma_used == 0);
246 assert (mp->ma_table == mp->ma_smalltable);
247 assert (mp->ma_mask == PyDict_MINSIZE - 1);
Christian Heimesb4ee4a12008-02-07 17:15:30 +0000248#ifdef SHOW_ALLOC_COUNT
249 count_reuse++;
250#endif
Raymond Hettinger43442782004-03-17 21:55:03 +0000251 } else {
Brett Cannon77ae87c2007-10-10 00:07:50 +0000252 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
Raymond Hettinger43442782004-03-17 21:55:03 +0000253 if (mp == NULL)
254 return NULL;
255 EMPTY_TO_MINSIZE(mp);
Christian Heimesb4ee4a12008-02-07 17:15:30 +0000256#ifdef SHOW_ALLOC_COUNT
257 count_alloc++;
258#endif
Raymond Hettinger43442782004-03-17 21:55:03 +0000259 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000260 mp->ma_lookup = lookdict_string;
261#ifdef SHOW_CONVERSION_COUNTS
262 ++created;
263#endif
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000264 _PyObject_GC_TRACK(mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000265 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000266}
267
268/*
269The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000270This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000271Open addressing is preferred over chaining since the link overhead for
272chaining would be substantial (100% with typical malloc overhead).
273
Tim Peterseb28ef22001-06-02 05:27:19 +0000274The initial probe index is computed as hash mod the table size. Subsequent
275probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000276
277All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000278
Tim Peterseb28ef22001-06-02 05:27:19 +0000279(The details in this version are due to Tim Peters, building on many past
280contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
281Christian Tismer).
Fred Drake1bff34a2000-08-31 19:31:38 +0000282
Tim Petersd770ebd2006-06-01 15:50:44 +0000283lookdict() is general-purpose, and may return NULL if (and only if) a
284comparison raises an exception (this was new in Python 2.5).
285lookdict_string() below is specialized to string keys, comparison of which can
286never raise an exception; that function can never return NULL. For both, when
Brett Cannon77ae87c2007-10-10 00:07:50 +0000287the key isn't found a PyDictEntry* is returned for which the me_value field is
Tim Petersd770ebd2006-06-01 15:50:44 +0000288NULL; this is the slot in the dict at which the key would have been found, and
289the caller can (if it wishes) add the <key, value> pair to the returned
Brett Cannon77ae87c2007-10-10 00:07:50 +0000290PyDictEntry*.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000291*/
Brett Cannon77ae87c2007-10-10 00:07:50 +0000292static PyDictEntry *
293lookdict(PyDictObject *mp, PyObject *key, register long hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000294{
Tim Petersd770ebd2006-06-01 15:50:44 +0000295 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000296 register size_t perturb;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000297 register PyDictEntry *freeslot;
Tim Petersd770ebd2006-06-01 15:50:44 +0000298 register size_t mask = (size_t)mp->ma_mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000299 PyDictEntry *ep0 = mp->ma_table;
300 register PyDictEntry *ep;
Fred Drakec88b99c2000-08-31 19:04:07 +0000301 register int cmp;
Tim Peters453163d2001-06-03 04:54:32 +0000302 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000303
Tim Petersd770ebd2006-06-01 15:50:44 +0000304 i = (size_t)hash & mask;
Guido van Rossumefb46091997-01-29 15:53:56 +0000305 ep = &ep0[i];
Guido van Rossumc1c7b1a1998-10-06 16:01:14 +0000306 if (ep->me_key == NULL || ep->me_key == key)
Guido van Rossum7d181591997-01-16 21:06:45 +0000307 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000308
Guido van Rossum16e93a81997-01-28 00:00:11 +0000309 if (ep->me_key == dummy)
Guido van Rossum7d181591997-01-16 21:06:45 +0000310 freeslot = ep;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000311 else {
Fred Drakec88b99c2000-08-31 19:04:07 +0000312 if (ep->me_hash == hash) {
Tim Peters453163d2001-06-03 04:54:32 +0000313 startkey = ep->me_key;
Georg Brandlede3a322007-11-29 18:33:01 +0000314 Py_INCREF(startkey);
Tim Peters453163d2001-06-03 04:54:32 +0000315 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Georg Brandlede3a322007-11-29 18:33:01 +0000316 Py_DECREF(startkey);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000317 if (cmp < 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000318 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000319 if (ep0 == mp->ma_table && ep->me_key == startkey) {
320 if (cmp > 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000321 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000322 }
323 else {
324 /* The compare did major nasty stuff to the
325 * dict: start over.
326 * XXX A clever adversary could prevent this
327 * XXX from terminating.
328 */
Armin Rigo35f6d362006-06-01 13:19:12 +0000329 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000330 }
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000331 }
332 freeslot = NULL;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000333 }
Tim Peters15d49292001-05-27 07:39:22 +0000334
Tim Peters342c65e2001-05-13 06:43:53 +0000335 /* In the loop, me_key == dummy is by far (factor of 100s) the
336 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000337 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
338 i = (i << 2) + i + perturb + 1;
339 ep = &ep0[i & mask];
Armin Rigo35f6d362006-06-01 13:19:12 +0000340 if (ep->me_key == NULL)
341 return freeslot == NULL ? ep : freeslot;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000342 if (ep->me_key == key)
Armin Rigo35f6d362006-06-01 13:19:12 +0000343 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000344 if (ep->me_hash == hash && ep->me_key != dummy) {
Tim Peters453163d2001-06-03 04:54:32 +0000345 startkey = ep->me_key;
Georg Brandlede3a322007-11-29 18:33:01 +0000346 Py_INCREF(startkey);
Tim Peters453163d2001-06-03 04:54:32 +0000347 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Georg Brandlede3a322007-11-29 18:33:01 +0000348 Py_DECREF(startkey);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000349 if (cmp < 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000350 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000351 if (ep0 == mp->ma_table && ep->me_key == startkey) {
352 if (cmp > 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000353 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000354 }
355 else {
356 /* The compare did major nasty stuff to the
357 * dict: start over.
358 * XXX A clever adversary could prevent this
359 * XXX from terminating.
360 */
Armin Rigo35f6d362006-06-01 13:19:12 +0000361 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000362 }
Guido van Rossum16e93a81997-01-28 00:00:11 +0000363 }
Tim Peters342c65e2001-05-13 06:43:53 +0000364 else if (ep->me_key == dummy && freeslot == NULL)
365 freeslot = ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000366 }
Neal Norwitza5ccda92006-10-28 21:16:54 +0000367 assert(0); /* NOT REACHED */
368 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000369}
370
371/*
Fred Drake1bff34a2000-08-31 19:31:38 +0000372 * Hacked up version of lookdict which can assume keys are always strings;
Tim Petersd770ebd2006-06-01 15:50:44 +0000373 * this assumption allows testing for errors during PyObject_RichCompareBool()
374 * to be dropped; string-string comparisons never raise exceptions. This also
375 * means we don't need to go through PyObject_RichCompareBool(); we can always
376 * use _PyString_Eq() directly.
Fred Drake1bff34a2000-08-31 19:31:38 +0000377 *
Tim Petersd770ebd2006-06-01 15:50:44 +0000378 * This is valuable because dicts with only string keys are very common.
Fred Drake1bff34a2000-08-31 19:31:38 +0000379 */
Brett Cannon77ae87c2007-10-10 00:07:50 +0000380static PyDictEntry *
381lookdict_string(PyDictObject *mp, PyObject *key, register long hash)
Fred Drake1bff34a2000-08-31 19:31:38 +0000382{
Tim Petersd770ebd2006-06-01 15:50:44 +0000383 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000384 register size_t perturb;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000385 register PyDictEntry *freeslot;
Tim Petersd770ebd2006-06-01 15:50:44 +0000386 register size_t mask = (size_t)mp->ma_mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000387 PyDictEntry *ep0 = mp->ma_table;
388 register PyDictEntry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000389
Tim Peters0ab085c2001-09-14 00:25:33 +0000390 /* Make sure this function doesn't have to handle non-string keys,
391 including subclasses of str; e.g., one reason to subclass
392 strings is to override __eq__, and for speed we don't cater to
393 that here. */
394 if (!PyString_CheckExact(key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000395#ifdef SHOW_CONVERSION_COUNTS
396 ++converted;
397#endif
398 mp->ma_lookup = lookdict;
399 return lookdict(mp, key, hash);
400 }
Tim Peters2f228e72001-05-13 00:19:31 +0000401 i = hash & mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000402 ep = &ep0[i];
403 if (ep->me_key == NULL || ep->me_key == key)
404 return ep;
405 if (ep->me_key == dummy)
406 freeslot = ep;
407 else {
Tim Petersd770ebd2006-06-01 15:50:44 +0000408 if (ep->me_hash == hash && _PyString_Eq(ep->me_key, key))
Fred Drake1bff34a2000-08-31 19:31:38 +0000409 return ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000410 freeslot = NULL;
411 }
Tim Peters15d49292001-05-27 07:39:22 +0000412
Tim Peters342c65e2001-05-13 06:43:53 +0000413 /* In the loop, me_key == dummy is by far (factor of 100s) the
414 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000415 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
416 i = (i << 2) + i + perturb + 1;
417 ep = &ep0[i & mask];
Tim Peters342c65e2001-05-13 06:43:53 +0000418 if (ep->me_key == NULL)
419 return freeslot == NULL ? ep : freeslot;
420 if (ep->me_key == key
421 || (ep->me_hash == hash
422 && ep->me_key != dummy
Martin v. Löwiscd353062001-05-24 16:56:35 +0000423 && _PyString_Eq(ep->me_key, key)))
Fred Drake1bff34a2000-08-31 19:31:38 +0000424 return ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000425 if (ep->me_key == dummy && freeslot == NULL)
Tim Peters342c65e2001-05-13 06:43:53 +0000426 freeslot = ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000427 }
Neal Norwitza5ccda92006-10-28 21:16:54 +0000428 assert(0); /* NOT REACHED */
429 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000430}
431
432/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000433Internal routine to insert a new item into the table.
434Used both by the internal resize routine and by the public insert routine.
435Eats a reference to key and one to value.
Tim Petersd770ebd2006-06-01 15:50:44 +0000436Returns -1 if an error occurred, or 0 on success.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000437*/
Armin Rigo35f6d362006-06-01 13:19:12 +0000438static int
Brett Cannon77ae87c2007-10-10 00:07:50 +0000439insertdict(register PyDictObject *mp, PyObject *key, long hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000440{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000441 PyObject *old_value;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000442 register PyDictEntry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000443 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
444
445 assert(mp->ma_lookup != NULL);
446 ep = mp->ma_lookup(mp, key, hash);
Armin Rigo35f6d362006-06-01 13:19:12 +0000447 if (ep == NULL) {
448 Py_DECREF(key);
449 Py_DECREF(value);
450 return -1;
451 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000452 if (ep->me_value != NULL) {
Guido van Rossumd7047b31995-01-02 19:07:15 +0000453 old_value = ep->me_value;
454 ep->me_value = value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000455 Py_DECREF(old_value); /* which **CAN** re-enter */
456 Py_DECREF(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000457 }
458 else {
459 if (ep->me_key == NULL)
460 mp->ma_fill++;
Raymond Hettinger07ead172005-02-05 23:42:57 +0000461 else {
462 assert(ep->me_key == dummy);
463 Py_DECREF(dummy);
464 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000465 ep->me_key = key;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000466 ep->me_hash = (Py_ssize_t)hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000467 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000468 mp->ma_used++;
469 }
Armin Rigo35f6d362006-06-01 13:19:12 +0000470 return 0;
471}
472
473/*
474Internal routine used by dictresize() to insert an item which is
475known to be absent from the dict. This routine also assumes that
476the dict contains no deleted entries. Besides the performance benefit,
477using insertdict() in dictresize() is dangerous (SF bug #1456209).
Tim Petersd770ebd2006-06-01 15:50:44 +0000478Note that no refcounts are changed by this routine; if needed, the caller
479is responsible for incref'ing `key` and `value`.
Armin Rigo35f6d362006-06-01 13:19:12 +0000480*/
481static void
Brett Cannon77ae87c2007-10-10 00:07:50 +0000482insertdict_clean(register PyDictObject *mp, PyObject *key, long hash,
Armin Rigo35f6d362006-06-01 13:19:12 +0000483 PyObject *value)
484{
Tim Petersd770ebd2006-06-01 15:50:44 +0000485 register size_t i;
Armin Rigo35f6d362006-06-01 13:19:12 +0000486 register size_t perturb;
Tim Petersd770ebd2006-06-01 15:50:44 +0000487 register size_t mask = (size_t)mp->ma_mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000488 PyDictEntry *ep0 = mp->ma_table;
489 register PyDictEntry *ep;
Armin Rigo35f6d362006-06-01 13:19:12 +0000490
491 i = hash & mask;
492 ep = &ep0[i];
493 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
494 i = (i << 2) + i + perturb + 1;
495 ep = &ep0[i & mask];
496 }
Tim Petersd770ebd2006-06-01 15:50:44 +0000497 assert(ep->me_value == NULL);
Armin Rigo35f6d362006-06-01 13:19:12 +0000498 mp->ma_fill++;
499 ep->me_key = key;
500 ep->me_hash = (Py_ssize_t)hash;
501 ep->me_value = value;
502 mp->ma_used++;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000503}
504
505/*
506Restructure the table by allocating a new table and reinserting all
507items again. When entries have been deleted, the new table may
508actually be smaller than the old one.
509*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000510static int
Brett Cannon77ae87c2007-10-10 00:07:50 +0000511dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000512{
Tim Peters9b10f7e2006-05-30 04:16:25 +0000513 Py_ssize_t newsize;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000514 PyDictEntry *oldtable, *newtable, *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000515 Py_ssize_t i;
Tim Peters0c6010b2001-05-23 23:33:57 +0000516 int is_oldtable_malloced;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000517 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000518
519 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000520
Tim Peterseb28ef22001-06-02 05:27:19 +0000521 /* Find the smallest table size > minused. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000522 for (newsize = PyDict_MINSIZE;
Tim Peterseb28ef22001-06-02 05:27:19 +0000523 newsize <= minused && newsize > 0;
524 newsize <<= 1)
525 ;
526 if (newsize <= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000527 PyErr_NoMemory();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000528 return -1;
529 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000530
531 /* Get space for a new table. */
532 oldtable = mp->ma_table;
533 assert(oldtable != NULL);
534 is_oldtable_malloced = oldtable != mp->ma_smalltable;
535
Tim Peters6d6c1a32001-08-02 04:15:00 +0000536 if (newsize == PyDict_MINSIZE) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000537 /* A large table is shrinking, or we can't get any smaller. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000538 newtable = mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000539 if (newtable == oldtable) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000540 if (mp->ma_fill == mp->ma_used) {
541 /* No dummies, so no point doing anything. */
Tim Peters0c6010b2001-05-23 23:33:57 +0000542 return 0;
Tim Petersf8a548c2001-05-24 16:26:40 +0000543 }
544 /* We're not going to resize it, but rebuild the
545 table anyway to purge old dummy entries.
546 Subtle: This is *necessary* if fill==size,
547 as lookdict needs at least one virgin slot to
548 terminate failing searches. If fill < size, it's
549 merely desirable, as dummies slow searches. */
550 assert(mp->ma_fill > mp->ma_used);
Tim Peters0c6010b2001-05-23 23:33:57 +0000551 memcpy(small_copy, oldtable, sizeof(small_copy));
552 oldtable = small_copy;
553 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000554 }
555 else {
Brett Cannon77ae87c2007-10-10 00:07:50 +0000556 newtable = PyMem_NEW(PyDictEntry, newsize);
Tim Petersdea48ec2001-05-22 20:40:22 +0000557 if (newtable == NULL) {
558 PyErr_NoMemory();
559 return -1;
560 }
561 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000562
563 /* Make the dict empty, using the new table. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000564 assert(newtable != oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000565 mp->ma_table = newtable;
Tim Petersafb6ae82001-06-04 21:00:21 +0000566 mp->ma_mask = newsize - 1;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000567 memset(newtable, 0, sizeof(PyDictEntry) * newsize);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000568 mp->ma_used = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000569 i = mp->ma_fill;
570 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000571
Tim Peters19283142001-05-17 22:25:34 +0000572 /* Copy the data over; this is refcount-neutral for active entries;
573 dummy entries aren't copied over, of course */
Tim Petersdea48ec2001-05-22 20:40:22 +0000574 for (ep = oldtable; i > 0; ep++) {
575 if (ep->me_value != NULL) { /* active entry */
576 --i;
Armin Rigo35f6d362006-06-01 13:19:12 +0000577 insertdict_clean(mp, ep->me_key, (long)ep->me_hash,
578 ep->me_value);
Tim Petersdea48ec2001-05-22 20:40:22 +0000579 }
Tim Peters19283142001-05-17 22:25:34 +0000580 else if (ep->me_key != NULL) { /* dummy entry */
Tim Petersdea48ec2001-05-22 20:40:22 +0000581 --i;
Tim Peters19283142001-05-17 22:25:34 +0000582 assert(ep->me_key == dummy);
583 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000584 }
Tim Peters19283142001-05-17 22:25:34 +0000585 /* else key == value == NULL: nothing to do */
Guido van Rossumd7047b31995-01-02 19:07:15 +0000586 }
587
Tim Peters0c6010b2001-05-23 23:33:57 +0000588 if (is_oldtable_malloced)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000589 PyMem_DEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000590 return 0;
591}
592
Raymond Hettingerfd7ed402007-12-18 21:24:09 +0000593/* Create a new dictionary pre-sized to hold an estimated number of elements.
594 Underestimates are okay because the dictionary will resize as necessary.
595 Overestimates just mean the dictionary will be more sparse than usual.
596*/
597
598PyObject *
599_PyDict_NewPresized(Py_ssize_t minused)
600{
601 PyObject *op = PyDict_New();
602
603 if (minused>5 && op != NULL && dictresize((PyDictObject *)op, minused) == -1) {
604 Py_DECREF(op);
605 return NULL;
606 }
607 return op;
608}
609
Tim Petersd770ebd2006-06-01 15:50:44 +0000610/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
611 * that may occur (originally dicts supported only string keys, and exceptions
612 * weren't possible). So, while the original intent was that a NULL return
Andrew M. Kuchling0067b5f2006-08-04 20:37:43 +0000613 * meant the key wasn't present, in reality it can mean that, or that an error
Tim Petersd770ebd2006-06-01 15:50:44 +0000614 * (suppressed) occurred while computing the key's hash, or that some error
615 * (suppressed) occurred when comparing keys in the dict's internal probe
616 * sequence. A nasty example of the latter is when a Python-coded comparison
617 * function hits a stack-depth error, which can cause this to return NULL
618 * even if the key is present.
619 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000620PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000621PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000622{
623 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000624 PyDictObject *mp = (PyDictObject *)op;
625 PyDictEntry *ep;
Armin Rigo35f6d362006-06-01 13:19:12 +0000626 PyThreadState *tstate;
Tim Petersd770ebd2006-06-01 15:50:44 +0000627 if (!PyDict_Check(op))
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000628 return NULL;
Tim Peters0ab085c2001-09-14 00:25:33 +0000629 if (!PyString_CheckExact(key) ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000630 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Guido van Rossumca756f21997-01-23 19:39:29 +0000631 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000632 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000633 if (hash == -1) {
634 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000635 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000636 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000637 }
Armin Rigo35f6d362006-06-01 13:19:12 +0000638
639 /* We can arrive here with a NULL tstate during initialization:
640 try running "python -Wi" for an example related to string
641 interning. Let's just hope that no exception occurs then... */
Armin Rigoacd0d6d2006-06-10 10:57:40 +0000642 tstate = _PyThreadState_Current;
Armin Rigo35f6d362006-06-01 13:19:12 +0000643 if (tstate != NULL && tstate->curexc_type != NULL) {
644 /* preserve the existing exception */
645 PyObject *err_type, *err_value, *err_tb;
646 PyErr_Fetch(&err_type, &err_value, &err_tb);
647 ep = (mp->ma_lookup)(mp, key, hash);
648 /* ignore errors */
649 PyErr_Restore(err_type, err_value, err_tb);
650 if (ep == NULL)
651 return NULL;
652 }
653 else {
654 ep = (mp->ma_lookup)(mp, key, hash);
655 if (ep == NULL) {
656 PyErr_Clear();
657 return NULL;
658 }
659 }
660 return ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000661}
662
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000663/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
Tim Peters60b29962006-01-01 01:19:23 +0000664 * dictionary if it's merely replacing the value for an existing key.
665 * This means that it's safe to loop over a dictionary with PyDict_Next()
666 * and occasionally replace a value -- but you can't insert new keys or
667 * remove them.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000668 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000669int
Tim Peters1f5871e2000-07-04 17:44:48 +0000670PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000671{
Brett Cannon77ae87c2007-10-10 00:07:50 +0000672 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000673 register long hash;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000674 register Py_ssize_t n_used;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000675
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000676 if (!PyDict_Check(op)) {
677 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000678 return -1;
679 }
Neal Norwitz48808a12006-07-21 05:29:58 +0000680 assert(key);
681 assert(value);
Brett Cannon77ae87c2007-10-10 00:07:50 +0000682 mp = (PyDictObject *)op;
Tim Peters0ab085c2001-09-14 00:25:33 +0000683 if (PyString_CheckExact(key)) {
Guido van Rossum45ec02a2002-08-19 21:43:18 +0000684 hash = ((PyStringObject *)key)->ob_shash;
685 if (hash == -1)
686 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000687 }
Tim Peters1f7df352002-03-29 03:29:08 +0000688 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000689 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000690 if (hash == -1)
691 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000692 }
Tim Petersafb6ae82001-06-04 21:00:21 +0000693 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000694 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000695 Py_INCREF(value);
696 Py_INCREF(key);
Armin Rigo35f6d362006-06-01 13:19:12 +0000697 if (insertdict(mp, key, hash, value) != 0)
698 return -1;
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000699 /* If we added a key, we can safely resize. Otherwise just return!
700 * If fill >= 2/3 size, adjust size. Normally, this doubles or
701 * quaduples the size, but it's also possible for the dict to shrink
Tim Peters60b29962006-01-01 01:19:23 +0000702 * (if ma_fill is much larger than ma_used, meaning a lot of dict
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000703 * keys have been * deleted).
Tim Peters60b29962006-01-01 01:19:23 +0000704 *
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000705 * Quadrupling the size improves average dictionary sparseness
706 * (reducing collisions) at the cost of some memory and iteration
707 * speed (which loops over every possible entry). It also halves
Neal Norwitz80af59c2006-05-30 04:19:21 +0000708 * the number of expensive resize operations in a growing dictionary.
Tim Peters60b29962006-01-01 01:19:23 +0000709 *
710 * Very large dictionaries (over 50K items) use doubling instead.
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000711 * This may help applications with severe memory constraints.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000712 */
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000713 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
714 return 0;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000715 return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000716}
717
718int
Tim Peters1f5871e2000-07-04 17:44:48 +0000719PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000720{
Brett Cannon77ae87c2007-10-10 00:07:50 +0000721 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000722 register long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000723 register PyDictEntry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000724 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000725
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000726 if (!PyDict_Check(op)) {
727 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000728 return -1;
729 }
Neal Norwitz48808a12006-07-21 05:29:58 +0000730 assert(key);
Tim Peters0ab085c2001-09-14 00:25:33 +0000731 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +0000732 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000733 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000734 if (hash == -1)
735 return -1;
736 }
Brett Cannon77ae87c2007-10-10 00:07:50 +0000737 mp = (PyDictObject *)op;
Fred Drake1bff34a2000-08-31 19:31:38 +0000738 ep = (mp->ma_lookup)(mp, key, hash);
Armin Rigo35f6d362006-06-01 13:19:12 +0000739 if (ep == NULL)
740 return -1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000741 if (ep->me_value == NULL) {
Georg Brandlb9f4ad32006-10-29 18:31:42 +0000742 set_key_error(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000743 return -1;
744 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000745 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000746 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000747 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000748 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000749 ep->me_value = NULL;
750 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000751 Py_DECREF(old_value);
752 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000753 return 0;
754}
755
Guido van Rossum25831651993-05-19 14:50:45 +0000756void
Tim Peters1f5871e2000-07-04 17:44:48 +0000757PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000758{
Brett Cannon77ae87c2007-10-10 00:07:50 +0000759 PyDictObject *mp;
760 PyDictEntry *ep, *table;
Tim Petersdea48ec2001-05-22 20:40:22 +0000761 int table_is_malloced;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000762 Py_ssize_t fill;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000763 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Petersdea48ec2001-05-22 20:40:22 +0000764#ifdef Py_DEBUG
Tim Peters9b10f7e2006-05-30 04:16:25 +0000765 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +0000766#endif
767
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000768 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000769 return;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000770 mp = (PyDictObject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000771#ifdef Py_DEBUG
Tim Petersafb6ae82001-06-04 21:00:21 +0000772 n = mp->ma_mask + 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000773 i = 0;
774#endif
775
776 table = mp->ma_table;
777 assert(table != NULL);
778 table_is_malloced = table != mp->ma_smalltable;
779
780 /* This is delicate. During the process of clearing the dict,
781 * decrefs can cause the dict to mutate. To avoid fatal confusion
782 * (voice of experience), we have to make the dict empty before
783 * clearing the slots, and never refer to anything via mp->xxx while
784 * clearing.
785 */
786 fill = mp->ma_fill;
787 if (table_is_malloced)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000788 EMPTY_TO_MINSIZE(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000789
790 else if (fill > 0) {
791 /* It's a small table with something that needs to be cleared.
792 * Afraid the only safe way is to copy the dict entries into
793 * another small table first.
794 */
795 memcpy(small_copy, table, sizeof(small_copy));
796 table = small_copy;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000797 EMPTY_TO_MINSIZE(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000798 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000799 /* else it's a small table that's already empty */
800
801 /* Now we can finally clear things. If C had refcounts, we could
802 * assert that the refcount on table is 1 now, i.e. that this function
803 * has unique access to it, so decref side-effects can't alter it.
804 */
805 for (ep = table; fill > 0; ++ep) {
806#ifdef Py_DEBUG
807 assert(i < n);
808 ++i;
809#endif
810 if (ep->me_key) {
811 --fill;
812 Py_DECREF(ep->me_key);
813 Py_XDECREF(ep->me_value);
814 }
815#ifdef Py_DEBUG
816 else
817 assert(ep->me_value == NULL);
818#endif
819 }
820
821 if (table_is_malloced)
822 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000823}
824
Tim Peters080c88b2003-02-15 03:01:11 +0000825/*
826 * Iterate over a dict. Use like so:
827 *
Tim Peters9b10f7e2006-05-30 04:16:25 +0000828 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +0000829 * PyObject *key, *value;
830 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +0000831 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +0000832 * Refer to borrowed references in key and value.
833 * }
834 *
835 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +0000836 * mutates the dict. One exception: it is safe if the loop merely changes
837 * the values associated with the keys (but doesn't insert new keys or
838 * delete keys), via PyDict_SetItem().
839 */
Guido van Rossum25831651993-05-19 14:50:45 +0000840int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000841PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000842{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000843 register Py_ssize_t i;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000844 register Py_ssize_t mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000845 register PyDictEntry *ep;
Raymond Hettinger43442782004-03-17 21:55:03 +0000846
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000847 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000848 return 0;
Guido van Rossum25831651993-05-19 14:50:45 +0000849 i = *ppos;
850 if (i < 0)
851 return 0;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000852 ep = ((PyDictObject *)op)->ma_table;
853 mask = ((PyDictObject *)op)->ma_mask;
Raymond Hettinger43442782004-03-17 21:55:03 +0000854 while (i <= mask && ep[i].me_value == NULL)
Guido van Rossum25831651993-05-19 14:50:45 +0000855 i++;
856 *ppos = i+1;
Raymond Hettinger43442782004-03-17 21:55:03 +0000857 if (i > mask)
Guido van Rossum25831651993-05-19 14:50:45 +0000858 return 0;
859 if (pkey)
Raymond Hettinger43442782004-03-17 21:55:03 +0000860 *pkey = ep[i].me_key;
Guido van Rossum25831651993-05-19 14:50:45 +0000861 if (pvalue)
Raymond Hettinger43442782004-03-17 21:55:03 +0000862 *pvalue = ep[i].me_value;
Guido van Rossum25831651993-05-19 14:50:45 +0000863 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000864}
865
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000866/* Internal version of PyDict_Next that returns a hash value in addition to the key and value.*/
867int
868_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue, long *phash)
869{
870 register Py_ssize_t i;
871 register Py_ssize_t mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000872 register PyDictEntry *ep;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000873
874 if (!PyDict_Check(op))
875 return 0;
876 i = *ppos;
877 if (i < 0)
878 return 0;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000879 ep = ((PyDictObject *)op)->ma_table;
880 mask = ((PyDictObject *)op)->ma_mask;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000881 while (i <= mask && ep[i].me_value == NULL)
882 i++;
883 *ppos = i+1;
884 if (i > mask)
885 return 0;
886 *phash = (long)(ep[i].me_hash);
887 if (pkey)
888 *pkey = ep[i].me_key;
889 if (pvalue)
890 *pvalue = ep[i].me_value;
891 return 1;
892}
893
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000894/* Methods */
895
896static void
Brett Cannon77ae87c2007-10-10 00:07:50 +0000897dict_dealloc(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000898{
Brett Cannon77ae87c2007-10-10 00:07:50 +0000899 register PyDictEntry *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000900 Py_ssize_t fill = mp->ma_fill;
Guido van Rossumff413af2002-03-28 20:34:59 +0000901 PyObject_GC_UnTrack(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000902 Py_TRASHCAN_SAFE_BEGIN(mp)
Tim Petersdea48ec2001-05-22 20:40:22 +0000903 for (ep = mp->ma_table; fill > 0; ep++) {
904 if (ep->me_key) {
905 --fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000906 Py_DECREF(ep->me_key);
Tim Petersdea48ec2001-05-22 20:40:22 +0000907 Py_XDECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000908 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000909 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000910 if (mp->ma_table != mp->ma_smalltable)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000911 PyMem_DEL(mp->ma_table);
Christian Heimes5b970ad2008-02-06 13:33:44 +0000912 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
913 free_list[numfree++] = mp;
Tim Peters60b29962006-01-01 01:19:23 +0000914 else
Christian Heimese93237d2007-12-19 02:37:44 +0000915 Py_TYPE(mp)->tp_free((PyObject *)mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000916 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000917}
918
919static int
Brett Cannon77ae87c2007-10-10 00:07:50 +0000920dict_print(register PyDictObject *mp, register FILE *fp, register int flags)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000921{
Tim Peters9b10f7e2006-05-30 04:16:25 +0000922 register Py_ssize_t i;
923 register Py_ssize_t any;
Tim Peters33f4a6a2006-05-30 05:23:59 +0000924 int status;
Guido van Rossum255443b1998-04-10 22:47:14 +0000925
Tim Peters33f4a6a2006-05-30 05:23:59 +0000926 status = Py_ReprEnter((PyObject*)mp);
927 if (status != 0) {
928 if (status < 0)
929 return status;
Brett Cannon01531592007-09-17 03:28:34 +0000930 Py_BEGIN_ALLOW_THREADS
Guido van Rossum255443b1998-04-10 22:47:14 +0000931 fprintf(fp, "{...}");
Brett Cannon01531592007-09-17 03:28:34 +0000932 Py_END_ALLOW_THREADS
Guido van Rossum255443b1998-04-10 22:47:14 +0000933 return 0;
934 }
935
Brett Cannon01531592007-09-17 03:28:34 +0000936 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000937 fprintf(fp, "{");
Brett Cannon01531592007-09-17 03:28:34 +0000938 Py_END_ALLOW_THREADS
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000939 any = 0;
Tim Petersafb6ae82001-06-04 21:00:21 +0000940 for (i = 0; i <= mp->ma_mask; i++) {
Brett Cannon77ae87c2007-10-10 00:07:50 +0000941 PyDictEntry *ep = mp->ma_table + i;
Tim Peters23cf6be2001-06-02 08:02:56 +0000942 PyObject *pvalue = ep->me_value;
943 if (pvalue != NULL) {
944 /* Prevent PyObject_Repr from deleting value during
945 key format */
946 Py_INCREF(pvalue);
Brett Cannon01531592007-09-17 03:28:34 +0000947 if (any++ > 0) {
948 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000949 fprintf(fp, ", ");
Brett Cannon01531592007-09-17 03:28:34 +0000950 Py_END_ALLOW_THREADS
951 }
Guido van Rossum255443b1998-04-10 22:47:14 +0000952 if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000953 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000954 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000955 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000956 }
Brett Cannon01531592007-09-17 03:28:34 +0000957 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000958 fprintf(fp, ": ");
Brett Cannon01531592007-09-17 03:28:34 +0000959 Py_END_ALLOW_THREADS
Tim Peters19b77cf2001-06-02 08:27:39 +0000960 if (PyObject_Print(pvalue, fp, 0) != 0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000961 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000962 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000963 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000964 }
Tim Peters23cf6be2001-06-02 08:02:56 +0000965 Py_DECREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000966 }
967 }
Brett Cannon01531592007-09-17 03:28:34 +0000968 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000969 fprintf(fp, "}");
Brett Cannon01531592007-09-17 03:28:34 +0000970 Py_END_ALLOW_THREADS
Guido van Rossum255443b1998-04-10 22:47:14 +0000971 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000972 return 0;
973}
974
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000975static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +0000976dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000977{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000978 Py_ssize_t i;
Tim Petersa7259592001-06-16 05:11:17 +0000979 PyObject *s, *temp, *colon = NULL;
980 PyObject *pieces = NULL, *result = NULL;
981 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +0000982
Tim Petersa7259592001-06-16 05:11:17 +0000983 i = Py_ReprEnter((PyObject *)mp);
Guido van Rossum255443b1998-04-10 22:47:14 +0000984 if (i != 0) {
Tim Petersa7259592001-06-16 05:11:17 +0000985 return i > 0 ? PyString_FromString("{...}") : NULL;
Guido van Rossum255443b1998-04-10 22:47:14 +0000986 }
987
Tim Petersa7259592001-06-16 05:11:17 +0000988 if (mp->ma_used == 0) {
989 result = PyString_FromString("{}");
990 goto Done;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000991 }
Tim Petersa7259592001-06-16 05:11:17 +0000992
993 pieces = PyList_New(0);
994 if (pieces == NULL)
995 goto Done;
996
997 colon = PyString_FromString(": ");
998 if (colon == NULL)
999 goto Done;
1000
1001 /* Do repr() on each key+value pair, and insert ": " between them.
1002 Note that repr may mutate the dict. */
Tim Petersc6057842001-06-16 07:52:53 +00001003 i = 0;
1004 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Tim Petersa7259592001-06-16 05:11:17 +00001005 int status;
1006 /* Prevent repr from deleting value during key format. */
1007 Py_INCREF(value);
1008 s = PyObject_Repr(key);
1009 PyString_Concat(&s, colon);
1010 PyString_ConcatAndDel(&s, PyObject_Repr(value));
1011 Py_DECREF(value);
1012 if (s == NULL)
1013 goto Done;
1014 status = PyList_Append(pieces, s);
1015 Py_DECREF(s); /* append created a new ref */
1016 if (status < 0)
1017 goto Done;
1018 }
1019
1020 /* Add "{}" decorations to the first and last items. */
1021 assert(PyList_GET_SIZE(pieces) > 0);
1022 s = PyString_FromString("{");
1023 if (s == NULL)
1024 goto Done;
1025 temp = PyList_GET_ITEM(pieces, 0);
1026 PyString_ConcatAndDel(&s, temp);
1027 PyList_SET_ITEM(pieces, 0, s);
1028 if (s == NULL)
1029 goto Done;
1030
1031 s = PyString_FromString("}");
1032 if (s == NULL)
1033 goto Done;
1034 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
1035 PyString_ConcatAndDel(&temp, s);
1036 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
1037 if (temp == NULL)
1038 goto Done;
1039
1040 /* Paste them all together with ", " between. */
1041 s = PyString_FromString(", ");
1042 if (s == NULL)
1043 goto Done;
1044 result = _PyString_Join(s, pieces);
Tim Peters0ab085c2001-09-14 00:25:33 +00001045 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +00001046
1047Done:
1048 Py_XDECREF(pieces);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001049 Py_XDECREF(colon);
Tim Petersa7259592001-06-16 05:11:17 +00001050 Py_ReprLeave((PyObject *)mp);
1051 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001052}
1053
Martin v. Löwis18e16552006-02-15 17:27:45 +00001054static Py_ssize_t
Brett Cannon77ae87c2007-10-10 00:07:50 +00001055dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001056{
1057 return mp->ma_used;
1058}
1059
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001060static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001061dict_subscript(PyDictObject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001062{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001063 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001064 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001065 PyDictEntry *ep;
Tim Petersdea48ec2001-05-22 20:40:22 +00001066 assert(mp->ma_table != NULL);
Tim Peters0ab085c2001-09-14 00:25:33 +00001067 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001068 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001069 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001070 if (hash == -1)
1071 return NULL;
1072 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001073 ep = (mp->ma_lookup)(mp, key, hash);
1074 if (ep == NULL)
1075 return NULL;
1076 v = ep->me_value;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001077 if (v == NULL) {
1078 if (!PyDict_CheckExact(mp)) {
1079 /* Look up __missing__ method if we're a subclass. */
Guido van Rossum4b92a822006-02-25 23:32:30 +00001080 PyObject *missing;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001081 static PyObject *missing_str = NULL;
1082 if (missing_str == NULL)
Tim Peters9b10f7e2006-05-30 04:16:25 +00001083 missing_str =
Guido van Rossum1968ad32006-02-25 22:38:04 +00001084 PyString_InternFromString("__missing__");
Christian Heimese93237d2007-12-19 02:37:44 +00001085 missing = _PyType_Lookup(Py_TYPE(mp), missing_str);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001086 if (missing != NULL)
1087 return PyObject_CallFunctionObjArgs(missing,
1088 (PyObject *)mp, key, NULL);
1089 }
Georg Brandlb9f4ad32006-10-29 18:31:42 +00001090 set_key_error(key);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001091 return NULL;
1092 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001093 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001094 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001095 return v;
1096}
1097
1098static int
Brett Cannon77ae87c2007-10-10 00:07:50 +00001099dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001100{
1101 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001102 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001103 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001104 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001105}
1106
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001107static PyMappingMethods dict_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001108 (lenfunc)dict_length, /*mp_length*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001109 (binaryfunc)dict_subscript, /*mp_subscript*/
1110 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001111};
1112
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001113static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001114dict_keys(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001115{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001116 register PyObject *v;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001117 register Py_ssize_t i, j;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001118 PyDictEntry *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001119 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001120
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001121 again:
1122 n = mp->ma_used;
1123 v = PyList_New(n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001124 if (v == NULL)
1125 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001126 if (n != mp->ma_used) {
1127 /* Durnit. The allocations caused the dict to resize.
1128 * Just start over, this shouldn't normally happen.
1129 */
1130 Py_DECREF(v);
1131 goto again;
1132 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001133 ep = mp->ma_table;
1134 mask = mp->ma_mask;
1135 for (i = 0, j = 0; i <= mask; i++) {
1136 if (ep[i].me_value != NULL) {
1137 PyObject *key = ep[i].me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001138 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001139 PyList_SET_ITEM(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001140 j++;
1141 }
1142 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001143 assert(j == n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001144 return v;
1145}
1146
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001147static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001148dict_values(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001149{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001150 register PyObject *v;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001151 register Py_ssize_t i, j;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001152 PyDictEntry *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001153 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001154
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001155 again:
1156 n = mp->ma_used;
1157 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001158 if (v == NULL)
1159 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001160 if (n != mp->ma_used) {
1161 /* Durnit. The allocations caused the dict to resize.
1162 * Just start over, this shouldn't normally happen.
1163 */
1164 Py_DECREF(v);
1165 goto again;
1166 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001167 ep = mp->ma_table;
1168 mask = mp->ma_mask;
1169 for (i = 0, j = 0; i <= mask; i++) {
1170 if (ep[i].me_value != NULL) {
1171 PyObject *value = ep[i].me_value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001172 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001173 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001174 j++;
1175 }
1176 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001177 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001178 return v;
1179}
1180
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001181static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001182dict_items(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001183{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001184 register PyObject *v;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001185 register Py_ssize_t i, j, n;
1186 Py_ssize_t mask;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001187 PyObject *item, *key, *value;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001188 PyDictEntry *ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001189
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001190 /* Preallocate the list of tuples, to avoid allocations during
1191 * the loop over the items, which could trigger GC, which
1192 * could resize the dict. :-(
1193 */
1194 again:
1195 n = mp->ma_used;
1196 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001197 if (v == NULL)
1198 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001199 for (i = 0; i < n; i++) {
1200 item = PyTuple_New(2);
1201 if (item == NULL) {
1202 Py_DECREF(v);
1203 return NULL;
1204 }
1205 PyList_SET_ITEM(v, i, item);
1206 }
1207 if (n != mp->ma_used) {
1208 /* Durnit. The allocations caused the dict to resize.
1209 * Just start over, this shouldn't normally happen.
1210 */
1211 Py_DECREF(v);
1212 goto again;
1213 }
1214 /* Nothing we do below makes any function calls. */
Raymond Hettinger43442782004-03-17 21:55:03 +00001215 ep = mp->ma_table;
1216 mask = mp->ma_mask;
1217 for (i = 0, j = 0; i <= mask; i++) {
Raymond Hettinger06905122004-03-19 10:30:00 +00001218 if ((value=ep[i].me_value) != NULL) {
Raymond Hettinger43442782004-03-17 21:55:03 +00001219 key = ep[i].me_key;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001220 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001221 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001222 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001223 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001224 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001225 j++;
1226 }
1227 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001228 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001229 return v;
1230}
1231
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001232static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001233dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001234{
1235 PyObject *seq;
1236 PyObject *value = Py_None;
1237 PyObject *it; /* iter(seq) */
1238 PyObject *key;
1239 PyObject *d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001240 int status;
1241
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001242 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001243 return NULL;
1244
1245 d = PyObject_CallObject(cls, NULL);
1246 if (d == NULL)
1247 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001248
Raymond Hettingercdcf8872007-11-07 02:26:17 +00001249 if (PyDict_CheckExact(d) && PyDict_CheckExact(seq)) {
1250 PyDictObject *mp = (PyDictObject *)d;
1251 PyObject *oldvalue;
1252 Py_ssize_t pos = 0;
1253 PyObject *key;
1254 long hash;
1255
Raymond Hettinger34448792007-11-09 23:14:44 +00001256 if (dictresize(mp, PySet_GET_SIZE(seq)))
Raymond Hettingercdcf8872007-11-07 02:26:17 +00001257 return NULL;
1258
1259 while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
1260 Py_INCREF(key);
1261 Py_INCREF(value);
1262 if (insertdict(mp, key, hash, value))
1263 return NULL;
1264 }
1265 return d;
1266 }
1267
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00001268 if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) {
Brett Cannon77ae87c2007-10-10 00:07:50 +00001269 PyDictObject *mp = (PyDictObject *)d;
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00001270 Py_ssize_t pos = 0;
1271 PyObject *key;
1272 long hash;
1273
1274 if (dictresize(mp, PySet_GET_SIZE(seq)))
1275 return NULL;
1276
1277 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
1278 Py_INCREF(key);
Raymond Hettingere3146f52007-03-21 20:33:57 +00001279 Py_INCREF(value);
1280 if (insertdict(mp, key, hash, value))
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00001281 return NULL;
1282 }
1283 return d;
1284 }
1285
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001286 it = PyObject_GetIter(seq);
1287 if (it == NULL){
1288 Py_DECREF(d);
1289 return NULL;
1290 }
1291
Raymond Hettinger34448792007-11-09 23:14:44 +00001292 if (PyDict_CheckExact(d)) {
1293 while ((key = PyIter_Next(it)) != NULL) {
1294 status = PyDict_SetItem(d, key, value);
1295 Py_DECREF(key);
1296 if (status < 0)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001297 goto Fail;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001298 }
Raymond Hettinger34448792007-11-09 23:14:44 +00001299 } else {
1300 while ((key = PyIter_Next(it)) != NULL) {
1301 status = PyObject_SetItem(d, key, value);
1302 Py_DECREF(key);
1303 if (status < 0)
1304 goto Fail;
1305 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001306 }
1307
Raymond Hettinger34448792007-11-09 23:14:44 +00001308 if (PyErr_Occurred())
1309 goto Fail;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001310 Py_DECREF(it);
1311 return d;
1312
1313Fail:
1314 Py_DECREF(it);
1315 Py_DECREF(d);
1316 return NULL;
1317}
1318
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001319static int
1320dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001321{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001322 PyObject *arg = NULL;
1323 int result = 0;
1324
1325 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1326 result = -1;
1327
1328 else if (arg != NULL) {
1329 if (PyObject_HasAttrString(arg, "keys"))
1330 result = PyDict_Merge(self, arg, 1);
1331 else
1332 result = PyDict_MergeFromSeq2(self, arg, 1);
1333 }
1334 if (result == 0 && kwds != NULL)
1335 result = PyDict_Merge(self, kwds, 1);
1336 return result;
1337}
1338
1339static PyObject *
1340dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1341{
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001342 if (dict_update_common(self, args, kwds, "update") != -1)
1343 Py_RETURN_NONE;
1344 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001345}
1346
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001347/* Update unconditionally replaces existing items.
1348 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001349 otherwise it leaves existing items unchanged.
1350
1351 PyDict_{Update,Merge} update/merge from a mapping object.
1352
Tim Petersf582b822001-12-11 18:51:08 +00001353 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001354 producing iterable objects of length 2.
1355*/
1356
Tim Petersf582b822001-12-11 18:51:08 +00001357int
Tim Peters1fc240e2001-10-26 05:06:50 +00001358PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1359{
1360 PyObject *it; /* iter(seq2) */
Tim Peters9b10f7e2006-05-30 04:16:25 +00001361 Py_ssize_t i; /* index into seq2 of current element */
Tim Peters1fc240e2001-10-26 05:06:50 +00001362 PyObject *item; /* seq2[i] */
1363 PyObject *fast; /* item as a 2-tuple or 2-list */
1364
1365 assert(d != NULL);
1366 assert(PyDict_Check(d));
1367 assert(seq2 != NULL);
1368
1369 it = PyObject_GetIter(seq2);
1370 if (it == NULL)
1371 return -1;
1372
1373 for (i = 0; ; ++i) {
1374 PyObject *key, *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001375 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001376
1377 fast = NULL;
1378 item = PyIter_Next(it);
1379 if (item == NULL) {
1380 if (PyErr_Occurred())
1381 goto Fail;
1382 break;
1383 }
1384
1385 /* Convert item to sequence, and verify length 2. */
1386 fast = PySequence_Fast(item, "");
1387 if (fast == NULL) {
1388 if (PyErr_ExceptionMatches(PyExc_TypeError))
1389 PyErr_Format(PyExc_TypeError,
1390 "cannot convert dictionary update "
Neal Norwitzd3881b02006-05-30 04:25:05 +00001391 "sequence element #%zd to a sequence",
Tim Peters1fc240e2001-10-26 05:06:50 +00001392 i);
1393 goto Fail;
1394 }
1395 n = PySequence_Fast_GET_SIZE(fast);
1396 if (n != 2) {
1397 PyErr_Format(PyExc_ValueError,
Neal Norwitzd3881b02006-05-30 04:25:05 +00001398 "dictionary update sequence element #%zd "
Martin v. Löwis2c95cc62006-02-16 06:54:25 +00001399 "has length %zd; 2 is required",
Martin v. Löwise0e89f72006-02-16 06:59:22 +00001400 i, n);
Tim Peters1fc240e2001-10-26 05:06:50 +00001401 goto Fail;
1402 }
1403
1404 /* Update/merge with this (key, value) pair. */
1405 key = PySequence_Fast_GET_ITEM(fast, 0);
1406 value = PySequence_Fast_GET_ITEM(fast, 1);
1407 if (override || PyDict_GetItem(d, key) == NULL) {
1408 int status = PyDict_SetItem(d, key, value);
1409 if (status < 0)
1410 goto Fail;
1411 }
1412 Py_DECREF(fast);
1413 Py_DECREF(item);
1414 }
1415
1416 i = 0;
1417 goto Return;
1418Fail:
1419 Py_XDECREF(item);
1420 Py_XDECREF(fast);
1421 i = -1;
1422Return:
1423 Py_DECREF(it);
Neal Norwitzd3881b02006-05-30 04:25:05 +00001424 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001425}
1426
Tim Peters6d6c1a32001-08-02 04:15:00 +00001427int
1428PyDict_Update(PyObject *a, PyObject *b)
1429{
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001430 return PyDict_Merge(a, b, 1);
1431}
1432
1433int
1434PyDict_Merge(PyObject *a, PyObject *b, int override)
1435{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001436 register PyDictObject *mp, *other;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001437 register Py_ssize_t i;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001438 PyDictEntry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001439
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001440 /* We accept for the argument either a concrete dictionary object,
1441 * or an abstract "mapping" object. For the former, we can do
1442 * things quite efficiently. For the latter, we only require that
1443 * PyMapping_Keys() and PyObject_GetItem() be supported.
1444 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001445 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1446 PyErr_BadInternalCall();
1447 return -1;
1448 }
Brett Cannon77ae87c2007-10-10 00:07:50 +00001449 mp = (PyDictObject*)a;
Raymond Hettinger3dbd4c52008-01-25 19:24:46 +00001450 if (PyDict_Check(b)) {
Brett Cannon77ae87c2007-10-10 00:07:50 +00001451 other = (PyDictObject*)b;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001452 if (other == mp || other->ma_used == 0)
1453 /* a.update(a) or a.update({}); nothing to do */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001454 return 0;
Raymond Hettinger186e7392005-05-14 18:08:25 +00001455 if (mp->ma_used == 0)
1456 /* Since the target dict is empty, PyDict_GetItem()
1457 * always returns NULL. Setting override to 1
1458 * skips the unnecessary test.
1459 */
1460 override = 1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001461 /* Do one big resize at the start, rather than
1462 * incrementally resizing as we insert new items. Expect
1463 * that there will be no (or few) overlapping keys.
1464 */
1465 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
Raymond Hettingerc8d22902003-05-07 00:49:40 +00001466 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001467 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001468 }
1469 for (i = 0; i <= other->ma_mask; i++) {
1470 entry = &other->ma_table[i];
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001471 if (entry->me_value != NULL &&
1472 (override ||
1473 PyDict_GetItem(a, entry->me_key) == NULL)) {
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001474 Py_INCREF(entry->me_key);
1475 Py_INCREF(entry->me_value);
Armin Rigo35f6d362006-06-01 13:19:12 +00001476 if (insertdict(mp, entry->me_key,
1477 (long)entry->me_hash,
1478 entry->me_value) != 0)
1479 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001480 }
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001481 }
1482 }
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001483 else {
1484 /* Do it the generic, slower way */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001485 PyObject *keys = PyMapping_Keys(b);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001486 PyObject *iter;
1487 PyObject *key, *value;
1488 int status;
1489
1490 if (keys == NULL)
1491 /* Docstring says this is equivalent to E.keys() so
1492 * if E doesn't have a .keys() method we want
1493 * AttributeError to percolate up. Might as well
1494 * do the same for any other error.
1495 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001496 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001497
1498 iter = PyObject_GetIter(keys);
1499 Py_DECREF(keys);
1500 if (iter == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001501 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001502
1503 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001504 if (!override && PyDict_GetItem(a, key) != NULL) {
1505 Py_DECREF(key);
1506 continue;
1507 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001508 value = PyObject_GetItem(b, key);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001509 if (value == NULL) {
1510 Py_DECREF(iter);
1511 Py_DECREF(key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001512 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001513 }
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001514 status = PyDict_SetItem(a, key, value);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001515 Py_DECREF(key);
1516 Py_DECREF(value);
1517 if (status < 0) {
1518 Py_DECREF(iter);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001519 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001520 }
1521 }
1522 Py_DECREF(iter);
1523 if (PyErr_Occurred())
1524 /* Iterator completed, via error */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001525 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001526 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001527 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001528}
1529
1530static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001531dict_copy(register PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001532{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001533 return PyDict_Copy((PyObject*)mp);
1534}
1535
1536PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001537PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001538{
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001539 PyObject *copy;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001540
1541 if (o == NULL || !PyDict_Check(o)) {
1542 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001543 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001544 }
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001545 copy = PyDict_New();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001546 if (copy == NULL)
1547 return NULL;
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001548 if (PyDict_Merge(copy, o, 1) == 0)
1549 return copy;
1550 Py_DECREF(copy);
Raymond Hettinger1356f782005-04-15 15:58:42 +00001551 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001552}
1553
Martin v. Löwis18e16552006-02-15 17:27:45 +00001554Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00001555PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001556{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001557 if (mp == NULL || !PyDict_Check(mp)) {
1558 PyErr_BadInternalCall();
Jeremy Hylton7083bb72004-02-17 20:10:11 +00001559 return -1;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001560 }
Brett Cannon77ae87c2007-10-10 00:07:50 +00001561 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001562}
1563
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001564PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001565PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001566{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001567 if (mp == NULL || !PyDict_Check(mp)) {
1568 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001569 return NULL;
1570 }
Brett Cannon77ae87c2007-10-10 00:07:50 +00001571 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001572}
1573
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001574PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001575PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001576{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001577 if (mp == NULL || !PyDict_Check(mp)) {
1578 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001579 return NULL;
1580 }
Brett Cannon77ae87c2007-10-10 00:07:50 +00001581 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001582}
1583
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001584PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001585PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001586{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001587 if (mp == NULL || !PyDict_Check(mp)) {
1588 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001589 return NULL;
1590 }
Brett Cannon77ae87c2007-10-10 00:07:50 +00001591 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001592}
1593
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001594/* Subroutine which returns the smallest key in a for which b's value
1595 is different or absent. The value is returned too, through the
Tim Peters95bf9392001-05-10 08:32:44 +00001596 pval argument. Both are NULL if no key in a is found for which b's status
1597 differs. The refcounts on (and only on) non-NULL *pval and function return
1598 values must be decremented by the caller (characterize() increments them
1599 to ensure that mutating comparison and PyDict_GetItem calls can't delete
1600 them before the caller is done looking at them). */
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001601
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001602static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001603characterize(PyDictObject *a, PyDictObject *b, PyObject **pval)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001604{
Tim Peters95bf9392001-05-10 08:32:44 +00001605 PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */
1606 PyObject *aval = NULL; /* a[akey] */
Tim Peters9b10f7e2006-05-30 04:16:25 +00001607 Py_ssize_t i;
1608 int cmp;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001609
Tim Petersafb6ae82001-06-04 21:00:21 +00001610 for (i = 0; i <= a->ma_mask; i++) {
Tim Peters95bf9392001-05-10 08:32:44 +00001611 PyObject *thiskey, *thisaval, *thisbval;
1612 if (a->ma_table[i].me_value == NULL)
1613 continue;
1614 thiskey = a->ma_table[i].me_key;
1615 Py_INCREF(thiskey); /* keep alive across compares */
1616 if (akey != NULL) {
1617 cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT);
1618 if (cmp < 0) {
1619 Py_DECREF(thiskey);
1620 goto Fail;
1621 }
1622 if (cmp > 0 ||
Tim Petersafb6ae82001-06-04 21:00:21 +00001623 i > a->ma_mask ||
Tim Peters95bf9392001-05-10 08:32:44 +00001624 a->ma_table[i].me_value == NULL)
1625 {
1626 /* Not the *smallest* a key; or maybe it is
1627 * but the compare shrunk the dict so we can't
1628 * find its associated value anymore; or
1629 * maybe it is but the compare deleted the
1630 * a[thiskey] entry.
Neal Norwitzd3881b02006-05-30 04:25:05 +00001631 */
Tim Peters95bf9392001-05-10 08:32:44 +00001632 Py_DECREF(thiskey);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001633 continue;
Guido van Rossumb9324202001-01-18 00:39:02 +00001634 }
Tim Peters95bf9392001-05-10 08:32:44 +00001635 }
1636
1637 /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */
1638 thisaval = a->ma_table[i].me_value;
1639 assert(thisaval);
1640 Py_INCREF(thisaval); /* keep alive */
1641 thisbval = PyDict_GetItem((PyObject *)b, thiskey);
1642 if (thisbval == NULL)
1643 cmp = 0;
1644 else {
1645 /* both dicts have thiskey: same values? */
1646 cmp = PyObject_RichCompareBool(
1647 thisaval, thisbval, Py_EQ);
1648 if (cmp < 0) {
1649 Py_DECREF(thiskey);
1650 Py_DECREF(thisaval);
1651 goto Fail;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001652 }
1653 }
Tim Peters95bf9392001-05-10 08:32:44 +00001654 if (cmp == 0) {
1655 /* New winner. */
1656 Py_XDECREF(akey);
1657 Py_XDECREF(aval);
1658 akey = thiskey;
1659 aval = thisaval;
1660 }
1661 else {
1662 Py_DECREF(thiskey);
1663 Py_DECREF(thisaval);
1664 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001665 }
Tim Peters95bf9392001-05-10 08:32:44 +00001666 *pval = aval;
1667 return akey;
1668
1669Fail:
1670 Py_XDECREF(akey);
1671 Py_XDECREF(aval);
1672 *pval = NULL;
1673 return NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001674}
1675
1676static int
Brett Cannon77ae87c2007-10-10 00:07:50 +00001677dict_compare(PyDictObject *a, PyDictObject *b)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001678{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001679 PyObject *adiff, *bdiff, *aval, *bval;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001680 int res;
1681
1682 /* Compare lengths first */
1683 if (a->ma_used < b->ma_used)
1684 return -1; /* a is shorter */
1685 else if (a->ma_used > b->ma_used)
1686 return 1; /* b is shorter */
Tim Peters95bf9392001-05-10 08:32:44 +00001687
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001688 /* Same length -- check all keys */
Tim Peters95bf9392001-05-10 08:32:44 +00001689 bdiff = bval = NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001690 adiff = characterize(a, b, &aval);
Tim Peters95bf9392001-05-10 08:32:44 +00001691 if (adiff == NULL) {
1692 assert(!aval);
Tim Peters3918fb22001-05-10 18:58:31 +00001693 /* Either an error, or a is a subset with the same length so
Tim Peters95bf9392001-05-10 08:32:44 +00001694 * must be equal.
1695 */
1696 res = PyErr_Occurred() ? -1 : 0;
1697 goto Finished;
1698 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001699 bdiff = characterize(b, a, &bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001700 if (bdiff == NULL && PyErr_Occurred()) {
1701 assert(!bval);
1702 res = -1;
1703 goto Finished;
1704 }
1705 res = 0;
1706 if (bdiff) {
1707 /* bdiff == NULL "should be" impossible now, but perhaps
1708 * the last comparison done by the characterize() on a had
1709 * the side effect of making the dicts equal!
1710 */
1711 res = PyObject_Compare(adiff, bdiff);
1712 }
1713 if (res == 0 && bval != NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001714 res = PyObject_Compare(aval, bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001715
1716Finished:
1717 Py_XDECREF(adiff);
1718 Py_XDECREF(bdiff);
1719 Py_XDECREF(aval);
1720 Py_XDECREF(bval);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001721 return res;
1722}
1723
Tim Peterse63415e2001-05-08 04:38:29 +00001724/* Return 1 if dicts equal, 0 if not, -1 if error.
1725 * Gets out as soon as any difference is detected.
1726 * Uses only Py_EQ comparison.
1727 */
1728static int
Brett Cannon77ae87c2007-10-10 00:07:50 +00001729dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00001730{
Tim Peters9b10f7e2006-05-30 04:16:25 +00001731 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00001732
1733 if (a->ma_used != b->ma_used)
1734 /* can't be equal if # of entries differ */
1735 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001736
Tim Peterse63415e2001-05-08 04:38:29 +00001737 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Tim Petersafb6ae82001-06-04 21:00:21 +00001738 for (i = 0; i <= a->ma_mask; i++) {
Tim Peterse63415e2001-05-08 04:38:29 +00001739 PyObject *aval = a->ma_table[i].me_value;
1740 if (aval != NULL) {
1741 int cmp;
1742 PyObject *bval;
1743 PyObject *key = a->ma_table[i].me_key;
1744 /* temporarily bump aval's refcount to ensure it stays
1745 alive until we're done with it */
1746 Py_INCREF(aval);
Neal Norwitza22975f2006-09-05 02:24:03 +00001747 /* ditto for key */
1748 Py_INCREF(key);
Tim Peterse63415e2001-05-08 04:38:29 +00001749 bval = PyDict_GetItem((PyObject *)b, key);
Neal Norwitza22975f2006-09-05 02:24:03 +00001750 Py_DECREF(key);
Tim Peterse63415e2001-05-08 04:38:29 +00001751 if (bval == NULL) {
1752 Py_DECREF(aval);
1753 return 0;
1754 }
1755 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1756 Py_DECREF(aval);
1757 if (cmp <= 0) /* error or not equal */
1758 return cmp;
1759 }
1760 }
1761 return 1;
1762 }
1763
1764static PyObject *
1765dict_richcompare(PyObject *v, PyObject *w, int op)
1766{
1767 int cmp;
1768 PyObject *res;
1769
1770 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1771 res = Py_NotImplemented;
1772 }
1773 else if (op == Py_EQ || op == Py_NE) {
Brett Cannon77ae87c2007-10-10 00:07:50 +00001774 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
Tim Peterse63415e2001-05-08 04:38:29 +00001775 if (cmp < 0)
1776 return NULL;
1777 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1778 }
Steven Bethardae42f332008-03-18 17:26:10 +00001779 else {
1780 /* Py3K warning if comparison isn't == or != */
Georg Brandld5b635f2008-03-25 08:29:14 +00001781 if (Py_Py3kWarningFlag &&
Georg Brandld65ab952008-03-25 08:31:32 +00001782 PyErr_Warn(PyExc_DeprecationWarning,
Georg Brandld5b635f2008-03-25 08:29:14 +00001783 "dict inequality comparisons not supported "
1784 "in 3.x") < 0) {
Steven Bethardae42f332008-03-18 17:26:10 +00001785 return NULL;
1786 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001787 res = Py_NotImplemented;
Steven Bethardae42f332008-03-18 17:26:10 +00001788 }
Tim Peterse63415e2001-05-08 04:38:29 +00001789 Py_INCREF(res);
1790 return res;
1791 }
1792
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001793static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001794dict_contains(register PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001795{
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001796 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001797 PyDictEntry *ep;
Tim Petersd770ebd2006-06-01 15:50:44 +00001798
Tim Peters0ab085c2001-09-14 00:25:33 +00001799 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001800 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001801 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001802 if (hash == -1)
1803 return NULL;
1804 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001805 ep = (mp->ma_lookup)(mp, key, hash);
1806 if (ep == NULL)
1807 return NULL;
Tim Petersd770ebd2006-06-01 15:50:44 +00001808 return PyBool_FromLong(ep->me_value != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001809}
1810
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001811static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001812dict_has_key(register PyDictObject *mp, PyObject *key)
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +00001813{
1814 if (Py_Py3kWarningFlag &&
1815 PyErr_Warn(PyExc_DeprecationWarning,
Georg Brandld5b635f2008-03-25 08:29:14 +00001816 "dict.has_key() not supported in 3.x; "
1817 "use the in operator") < 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
Tim Peters0ab085c2001-09-14 00:25:33 +00001834 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001835 (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
Tim Peters0ab085c2001-09-14 00:25:33 +00001863 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001864 (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 }
1909 if (!PyString_CheckExact(key) ||
1910 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1911 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
2039
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002040PyDoc_STRVAR(has_key__doc__,
Raymond Hettinger574aa322003-09-01 22:12:08 +00002041"D.has_key(k) -> True if D has a key k, else False");
Tim Petersf7f88b12000-12-13 23:18:45 +00002042
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002043PyDoc_STRVAR(contains__doc__,
2044"D.__contains__(k) -> True if D has a key k, else False");
2045
2046PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
2047
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002048PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002049"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002050
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002051PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002052"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 +00002053
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002054PyDoc_STRVAR(pop__doc__,
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002055"D.pop(k[,d]) -> v, remove specified key and return the corresponding value\n\
2056If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00002057
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002058PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00002059"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000020602-tuple; but raise KeyError if D is empty");
Tim Petersf7f88b12000-12-13 23:18:45 +00002061
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002062PyDoc_STRVAR(keys__doc__,
2063"D.keys() -> list of D's keys");
Tim Petersf7f88b12000-12-13 23:18:45 +00002064
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002065PyDoc_STRVAR(items__doc__,
2066"D.items() -> list of D's (key, value) pairs, as 2-tuples");
Tim Petersf7f88b12000-12-13 23:18:45 +00002067
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002068PyDoc_STRVAR(values__doc__,
2069"D.values() -> list of D's values");
Tim Petersf7f88b12000-12-13 23:18:45 +00002070
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002071PyDoc_STRVAR(update__doc__,
Walter Dörwaldd70ad8a2004-05-28 20:59:21 +00002072"D.update(E, **F) -> None. Update D from E and F: for k in E: D[k] = E[k]\n\
2073(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 +00002074
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002075PyDoc_STRVAR(fromkeys__doc__,
2076"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
2077v defaults to None.");
2078
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002079PyDoc_STRVAR(clear__doc__,
2080"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002081
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002082PyDoc_STRVAR(copy__doc__,
2083"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00002084
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002085PyDoc_STRVAR(iterkeys__doc__,
2086"D.iterkeys() -> an iterator over the keys of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00002087
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002088PyDoc_STRVAR(itervalues__doc__,
2089"D.itervalues() -> an iterator over the values of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00002090
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002091PyDoc_STRVAR(iteritems__doc__,
2092"D.iteritems() -> an iterator over the (key, value) items of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00002093
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002094static PyMethodDef mapp_methods[] = {
Neal Norwitzc7926292007-05-23 06:57:35 +00002095 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002096 contains__doc__},
Raymond Hettinger0c669672003-12-13 13:31:55 +00002097 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002098 getitem__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002099 {"has_key", (PyCFunction)dict_has_key, METH_O,
Tim Petersf7f88b12000-12-13 23:18:45 +00002100 has_key__doc__},
2101 {"get", (PyCFunction)dict_get, METH_VARARGS,
2102 get__doc__},
2103 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
2104 setdefault_doc__},
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002105 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
Just van Rossuma797d812002-11-23 09:45:04 +00002106 pop__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002107 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002108 popitem__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002109 {"keys", (PyCFunction)dict_keys, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002110 keys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002111 {"items", (PyCFunction)dict_items, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002112 items__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002113 {"values", (PyCFunction)dict_values, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002114 values__doc__},
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002115 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002116 update__doc__},
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002117 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
2118 fromkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002119 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002120 clear__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002121 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002122 copy__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002123 {"iterkeys", (PyCFunction)dict_iterkeys, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00002124 iterkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002125 {"itervalues", (PyCFunction)dict_itervalues, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00002126 itervalues__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002127 {"iteritems", (PyCFunction)dict_iteritems, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00002128 iteritems__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00002129 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002130};
2131
Tim Petersd770ebd2006-06-01 15:50:44 +00002132/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002133int
2134PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002135{
2136 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +00002137 PyDictObject *mp = (PyDictObject *)op;
2138 PyDictEntry *ep;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002139
Tim Peters0ab085c2001-09-14 00:25:33 +00002140 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00002141 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002142 hash = PyObject_Hash(key);
2143 if (hash == -1)
2144 return -1;
2145 }
Armin Rigo35f6d362006-06-01 13:19:12 +00002146 ep = (mp->ma_lookup)(mp, key, hash);
Tim Petersd770ebd2006-06-01 15:50:44 +00002147 return ep == NULL ? -1 : (ep->me_value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002148}
2149
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +00002150/* Internal version of PyDict_Contains used when the hash value is already known */
2151int
2152_PyDict_Contains(PyObject *op, PyObject *key, long hash)
2153{
Brett Cannon77ae87c2007-10-10 00:07:50 +00002154 PyDictObject *mp = (PyDictObject *)op;
2155 PyDictEntry *ep;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +00002156
2157 ep = (mp->ma_lookup)(mp, key, hash);
2158 return ep == NULL ? -1 : (ep->me_value != NULL);
2159}
2160
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002161/* Hack to implement "key in dict" */
2162static PySequenceMethods dict_as_sequence = {
Georg Brandl347b3002006-03-30 11:57:00 +00002163 0, /* sq_length */
2164 0, /* sq_concat */
2165 0, /* sq_repeat */
2166 0, /* sq_item */
2167 0, /* sq_slice */
2168 0, /* sq_ass_item */
2169 0, /* sq_ass_slice */
2170 PyDict_Contains, /* sq_contains */
2171 0, /* sq_inplace_concat */
2172 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002173};
2174
Guido van Rossum09e563a2001-05-01 12:10:21 +00002175static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002176dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2177{
2178 PyObject *self;
2179
2180 assert(type != NULL && type->tp_alloc != NULL);
2181 self = type->tp_alloc(type, 0);
2182 if (self != NULL) {
2183 PyDictObject *d = (PyDictObject *)self;
2184 /* It's guaranteed that tp->alloc zeroed out the struct. */
2185 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
2186 INIT_NONZERO_DICT_SLOTS(d);
2187 d->ma_lookup = lookdict_string;
2188#ifdef SHOW_CONVERSION_COUNTS
2189 ++created;
2190#endif
2191 }
2192 return self;
2193}
2194
Tim Peters25786c02001-09-02 08:22:48 +00002195static int
2196dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2197{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002198 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002199}
2200
Tim Peters6d6c1a32001-08-02 04:15:00 +00002201static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002202dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002203{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002204 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002205}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002206
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002207PyDoc_STRVAR(dictionary_doc,
Tim Petersa427a2b2001-10-29 22:25:45 +00002208"dict() -> new empty dictionary.\n"
2209"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Tim Peters1fc240e2001-10-26 05:06:50 +00002210" (key, value) pairs.\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002211"dict(seq) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002212" d = {}\n"
2213" for k, v in seq:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002214" d[k] = v\n"
2215"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2216" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002217
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002218PyTypeObject PyDict_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002219 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Tim Petersa427a2b2001-10-29 22:25:45 +00002220 "dict",
Brett Cannon77ae87c2007-10-10 00:07:50 +00002221 sizeof(PyDictObject),
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002222 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00002223 (destructor)dict_dealloc, /* tp_dealloc */
2224 (printfunc)dict_print, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002225 0, /* tp_getattr */
Guido van Rossumb9324202001-01-18 00:39:02 +00002226 0, /* tp_setattr */
Tim Peters4fa58bf2001-05-10 21:45:19 +00002227 (cmpfunc)dict_compare, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00002228 (reprfunc)dict_repr, /* tp_repr */
2229 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002230 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00002231 &dict_as_mapping, /* tp_as_mapping */
Guido van Rossum64c06e32007-11-22 00:55:51 +00002232 0, /* tp_hash */
Guido van Rossumb9324202001-01-18 00:39:02 +00002233 0, /* tp_call */
2234 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002235 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumb9324202001-01-18 00:39:02 +00002236 0, /* tp_setattro */
2237 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002238 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Neal Norwitzee3a1b52007-02-25 19:44:48 +00002239 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
Tim Peters25786c02001-09-02 08:22:48 +00002240 dictionary_doc, /* tp_doc */
Georg Brandl347b3002006-03-30 11:57:00 +00002241 dict_traverse, /* tp_traverse */
2242 dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00002243 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002244 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00002245 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002246 0, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002247 mapp_methods, /* tp_methods */
2248 0, /* tp_members */
2249 0, /* tp_getset */
2250 0, /* tp_base */
2251 0, /* tp_dict */
2252 0, /* tp_descr_get */
2253 0, /* tp_descr_set */
2254 0, /* tp_dictoffset */
Georg Brandl347b3002006-03-30 11:57:00 +00002255 dict_init, /* tp_init */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002256 PyType_GenericAlloc, /* tp_alloc */
2257 dict_new, /* tp_new */
Neil Schemenauer6189b892002-04-12 02:43:00 +00002258 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002259};
2260
Guido van Rossum3cca2451997-05-16 14:23:33 +00002261/* For backward compatibility with old dictionary interface */
2262
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002263PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002264PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002265{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002266 PyObject *kv, *rv;
2267 kv = PyString_FromString(key);
2268 if (kv == NULL)
2269 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002270 rv = PyDict_GetItem(v, kv);
2271 Py_DECREF(kv);
2272 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002273}
2274
2275int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002276PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002277{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002278 PyObject *kv;
2279 int err;
2280 kv = PyString_FromString(key);
2281 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002282 return -1;
Guido van Rossum4f3bf1e1997-09-29 23:31:11 +00002283 PyString_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00002284 err = PyDict_SetItem(v, kv, item);
2285 Py_DECREF(kv);
2286 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002287}
2288
2289int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002290PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002291{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002292 PyObject *kv;
2293 int err;
2294 kv = PyString_FromString(key);
2295 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002296 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002297 err = PyDict_DelItem(v, kv);
2298 Py_DECREF(kv);
2299 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002300}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002301
Raymond Hettinger019a1482004-03-18 02:41:19 +00002302/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002303
2304typedef struct {
2305 PyObject_HEAD
Brett Cannon77ae87c2007-10-10 00:07:50 +00002306 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
Tim Peters9b10f7e2006-05-30 04:16:25 +00002307 Py_ssize_t di_used;
2308 Py_ssize_t di_pos;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002309 PyObject* di_result; /* reusable result tuple for iteritems */
Tim Peters9b10f7e2006-05-30 04:16:25 +00002310 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002311} dictiterobject;
2312
2313static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002314dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002315{
2316 dictiterobject *di;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002317 di = PyObject_New(dictiterobject, itertype);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002318 if (di == NULL)
2319 return NULL;
2320 Py_INCREF(dict);
2321 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00002322 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002323 di->di_pos = 0;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002324 di->len = dict->ma_used;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002325 if (itertype == &PyDictIterItem_Type) {
2326 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2327 if (di->di_result == NULL) {
2328 Py_DECREF(di);
2329 return NULL;
2330 }
2331 }
2332 else
2333 di->di_result = NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002334 return (PyObject *)di;
2335}
2336
2337static void
2338dictiter_dealloc(dictiterobject *di)
2339{
Guido van Rossum2147df72002-07-16 20:30:22 +00002340 Py_XDECREF(di->di_dict);
Raymond Hettinger019a1482004-03-18 02:41:19 +00002341 Py_XDECREF(di->di_result);
Neil Schemenauer6189b892002-04-12 02:43:00 +00002342 PyObject_Del(di);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002343}
2344
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002345static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002346dictiter_len(dictiterobject *di)
2347{
Tim Peters9b10f7e2006-05-30 04:16:25 +00002348 Py_ssize_t len = 0;
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00002349 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002350 len = di->len;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002351 return PyInt_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002352}
2353
Armin Rigof5b3e362006-02-11 21:32:43 +00002354PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002355
2356static PyMethodDef dictiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +00002357 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002358 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002359};
2360
Raymond Hettinger019a1482004-03-18 02:41:19 +00002361static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002362{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002363 PyObject *key;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002364 register Py_ssize_t i, mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +00002365 register PyDictEntry *ep;
2366 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002367
Raymond Hettinger019a1482004-03-18 02:41:19 +00002368 if (d == NULL)
Guido van Rossum2147df72002-07-16 20:30:22 +00002369 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002370 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002371
Raymond Hettinger019a1482004-03-18 02:41:19 +00002372 if (di->di_used != d->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00002373 PyErr_SetString(PyExc_RuntimeError,
2374 "dictionary changed size during iteration");
Guido van Rossum2147df72002-07-16 20:30:22 +00002375 di->di_used = -1; /* Make this state sticky */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002376 return NULL;
2377 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002378
Raymond Hettinger019a1482004-03-18 02:41:19 +00002379 i = di->di_pos;
2380 if (i < 0)
2381 goto fail;
2382 ep = d->ma_table;
2383 mask = d->ma_mask;
2384 while (i <= mask && ep[i].me_value == NULL)
2385 i++;
2386 di->di_pos = i+1;
2387 if (i > mask)
2388 goto fail;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002389 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002390 key = ep[i].me_key;
2391 Py_INCREF(key);
2392 return key;
2393
2394fail:
2395 Py_DECREF(d);
Guido van Rossum2147df72002-07-16 20:30:22 +00002396 di->di_dict = NULL;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002397 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002398}
2399
Raymond Hettinger019a1482004-03-18 02:41:19 +00002400PyTypeObject PyDictIterKey_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002401 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002402 "dictionary-keyiterator", /* tp_name */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002403 sizeof(dictiterobject), /* tp_basicsize */
2404 0, /* tp_itemsize */
2405 /* methods */
2406 (destructor)dictiter_dealloc, /* tp_dealloc */
2407 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002408 0, /* tp_getattr */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002409 0, /* tp_setattr */
2410 0, /* tp_compare */
2411 0, /* tp_repr */
2412 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002413 0, /* tp_as_sequence */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002414 0, /* tp_as_mapping */
2415 0, /* tp_hash */
2416 0, /* tp_call */
2417 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002418 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002419 0, /* tp_setattro */
2420 0, /* tp_as_buffer */
2421 Py_TPFLAGS_DEFAULT, /* tp_flags */
2422 0, /* tp_doc */
2423 0, /* tp_traverse */
2424 0, /* tp_clear */
2425 0, /* tp_richcompare */
2426 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002427 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002428 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002429 dictiter_methods, /* tp_methods */
2430 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002431};
2432
2433static PyObject *dictiter_iternextvalue(dictiterobject *di)
2434{
2435 PyObject *value;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002436 register Py_ssize_t i, mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +00002437 register PyDictEntry *ep;
2438 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002439
2440 if (d == NULL)
2441 return NULL;
2442 assert (PyDict_Check(d));
2443
2444 if (di->di_used != d->ma_used) {
2445 PyErr_SetString(PyExc_RuntimeError,
2446 "dictionary changed size during iteration");
2447 di->di_used = -1; /* Make this state sticky */
2448 return NULL;
2449 }
2450
2451 i = di->di_pos;
Guido van Rossum09240f62004-03-20 19:11:58 +00002452 mask = d->ma_mask;
2453 if (i < 0 || i > mask)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002454 goto fail;
2455 ep = d->ma_table;
Guido van Rossum09240f62004-03-20 19:11:58 +00002456 while ((value=ep[i].me_value) == NULL) {
Raymond Hettinger019a1482004-03-18 02:41:19 +00002457 i++;
Guido van Rossum09240f62004-03-20 19:11:58 +00002458 if (i > mask)
2459 goto fail;
2460 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002461 di->di_pos = i+1;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002462 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002463 Py_INCREF(value);
2464 return value;
2465
2466fail:
2467 Py_DECREF(d);
2468 di->di_dict = NULL;
2469 return NULL;
2470}
2471
2472PyTypeObject PyDictIterValue_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002473 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002474 "dictionary-valueiterator", /* tp_name */
2475 sizeof(dictiterobject), /* tp_basicsize */
2476 0, /* tp_itemsize */
2477 /* methods */
2478 (destructor)dictiter_dealloc, /* tp_dealloc */
2479 0, /* tp_print */
2480 0, /* tp_getattr */
2481 0, /* tp_setattr */
2482 0, /* tp_compare */
2483 0, /* tp_repr */
2484 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002485 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002486 0, /* tp_as_mapping */
2487 0, /* tp_hash */
2488 0, /* tp_call */
2489 0, /* tp_str */
2490 PyObject_GenericGetAttr, /* tp_getattro */
2491 0, /* tp_setattro */
2492 0, /* tp_as_buffer */
2493 Py_TPFLAGS_DEFAULT, /* tp_flags */
2494 0, /* tp_doc */
2495 0, /* tp_traverse */
2496 0, /* tp_clear */
2497 0, /* tp_richcompare */
2498 0, /* tp_weaklistoffset */
2499 PyObject_SelfIter, /* tp_iter */
2500 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002501 dictiter_methods, /* tp_methods */
2502 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002503};
2504
2505static PyObject *dictiter_iternextitem(dictiterobject *di)
2506{
2507 PyObject *key, *value, *result = di->di_result;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002508 register Py_ssize_t i, mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +00002509 register PyDictEntry *ep;
2510 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002511
2512 if (d == NULL)
2513 return NULL;
2514 assert (PyDict_Check(d));
2515
2516 if (di->di_used != d->ma_used) {
2517 PyErr_SetString(PyExc_RuntimeError,
2518 "dictionary changed size during iteration");
2519 di->di_used = -1; /* Make this state sticky */
2520 return NULL;
2521 }
2522
2523 i = di->di_pos;
2524 if (i < 0)
2525 goto fail;
2526 ep = d->ma_table;
2527 mask = d->ma_mask;
2528 while (i <= mask && ep[i].me_value == NULL)
2529 i++;
2530 di->di_pos = i+1;
2531 if (i > mask)
2532 goto fail;
2533
2534 if (result->ob_refcnt == 1) {
2535 Py_INCREF(result);
2536 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2537 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2538 } else {
2539 result = PyTuple_New(2);
Tim Peters60b29962006-01-01 01:19:23 +00002540 if (result == NULL)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002541 return NULL;
2542 }
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002543 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002544 key = ep[i].me_key;
2545 value = ep[i].me_value;
2546 Py_INCREF(key);
2547 Py_INCREF(value);
2548 PyTuple_SET_ITEM(result, 0, key);
2549 PyTuple_SET_ITEM(result, 1, value);
2550 return result;
2551
2552fail:
2553 Py_DECREF(d);
2554 di->di_dict = NULL;
2555 return NULL;
2556}
2557
2558PyTypeObject PyDictIterItem_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002559 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002560 "dictionary-itemiterator", /* tp_name */
2561 sizeof(dictiterobject), /* tp_basicsize */
2562 0, /* tp_itemsize */
2563 /* methods */
2564 (destructor)dictiter_dealloc, /* tp_dealloc */
2565 0, /* tp_print */
2566 0, /* tp_getattr */
2567 0, /* tp_setattr */
2568 0, /* tp_compare */
2569 0, /* tp_repr */
2570 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002571 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002572 0, /* tp_as_mapping */
2573 0, /* tp_hash */
2574 0, /* tp_call */
2575 0, /* tp_str */
2576 PyObject_GenericGetAttr, /* tp_getattro */
2577 0, /* tp_setattro */
2578 0, /* tp_as_buffer */
2579 Py_TPFLAGS_DEFAULT, /* tp_flags */
2580 0, /* tp_doc */
2581 0, /* tp_traverse */
2582 0, /* tp_clear */
2583 0, /* tp_richcompare */
2584 0, /* tp_weaklistoffset */
2585 PyObject_SelfIter, /* tp_iter */
2586 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002587 dictiter_methods, /* tp_methods */
2588 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002589};