blob: 7bff7d8153ecf7f0d5e1c83f27666b84305778c4 [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
Thomas Wouters89f507f2006-12-13 04:49:30 +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);
24 Py_DECREF(tup);
25}
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
Guido van Rossumdc5f6b22006-08-24 21:29:26 +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
Guido van Rossumdc5f6b22006-08-24 21:29:26 +000054the list [i << 16 for i in range(20000)] as a set of keys. Since ints are
55their own hash codes, and this fits in a dict of size 2**15, the last 15 bits
56 of every hash 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
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000112Historical: Reimer Behrends contributed the idea of using a polynomial-based
Tim Peterseb28ef22001-06-02 05:27:19 +0000113approach, 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
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000117also 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
122masked); and the PyDictObject struct required a member to hold the table's
123polynomial. In Tim's experiments the current scheme ran faster, produced
124equally good collision statistics, needed less code & used less memory.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +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 */
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000138static PyObject *dummy = NULL; /* Initialized by first call to newPyDictObject() */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000139
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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 */
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000149static PyDictEntry *
150lookdict_unicode(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
Tim Peters6d6c1a32001-08-02 04:15:00 +0000165/* Initialization macros.
166 There are two ways to create a dict: PyDict_New() is the main C API
167 function, and the tp_new slot maps to dict_new(). In the latter case we
168 can save a little time over what PyDict_New does because it's guaranteed
169 that the PyDictObject struct is already zeroed out.
170 Everyone except dict_new() should use EMPTY_TO_MINSIZE (unless they have
171 an excellent reason not to).
172*/
173
174#define INIT_NONZERO_DICT_SLOTS(mp) do { \
Tim Petersdea48ec2001-05-22 20:40:22 +0000175 (mp)->ma_table = (mp)->ma_smalltable; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000176 (mp)->ma_mask = PyDict_MINSIZE - 1; \
177 } while(0)
178
179#define EMPTY_TO_MINSIZE(mp) do { \
180 memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000181 (mp)->ma_used = (mp)->ma_fill = 0; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000182 INIT_NONZERO_DICT_SLOTS(mp); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000183 } while(0)
184
Raymond Hettinger43442782004-03-17 21:55:03 +0000185/* Dictionary reuse scheme to save calls to malloc, free, and memset */
186#define MAXFREEDICTS 80
187static PyDictObject *free_dicts[MAXFREEDICTS];
188static int num_free_dicts = 0;
189
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000190PyObject *
Thomas Wouters78890102000-07-22 19:25:51 +0000191PyDict_New(void)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000192{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000193 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000194 if (dummy == NULL) { /* Auto-initialize dummy */
Neal Norwitz53cbdaa2007-08-23 21:42:55 +0000195 dummy = PyUnicode_FromString("<dummy key>");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000196 if (dummy == NULL)
197 return NULL;
Fred Drake1bff34a2000-08-31 19:31:38 +0000198#ifdef SHOW_CONVERSION_COUNTS
199 Py_AtExit(show_counts);
200#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000201 }
Raymond Hettinger43442782004-03-17 21:55:03 +0000202 if (num_free_dicts) {
203 mp = free_dicts[--num_free_dicts];
204 assert (mp != NULL);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000205 assert (Py_Type(mp) == &PyDict_Type);
Raymond Hettinger43442782004-03-17 21:55:03 +0000206 _Py_NewReference((PyObject *)mp);
207 if (mp->ma_fill) {
208 EMPTY_TO_MINSIZE(mp);
209 }
210 assert (mp->ma_used == 0);
211 assert (mp->ma_table == mp->ma_smalltable);
212 assert (mp->ma_mask == PyDict_MINSIZE - 1);
213 } else {
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000214 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
Raymond Hettinger43442782004-03-17 21:55:03 +0000215 if (mp == NULL)
216 return NULL;
217 EMPTY_TO_MINSIZE(mp);
218 }
Guido van Rossum89d8c602007-09-18 17:26:56 +0000219 mp->ma_lookup = lookdict_unicode;
Fred Drake1bff34a2000-08-31 19:31:38 +0000220#ifdef SHOW_CONVERSION_COUNTS
221 ++created;
222#endif
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000223 _PyObject_GC_TRACK(mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000224 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000225}
226
227/*
228The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000229This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000230Open addressing is preferred over chaining since the link overhead for
231chaining would be substantial (100% with typical malloc overhead).
232
Tim Peterseb28ef22001-06-02 05:27:19 +0000233The initial probe index is computed as hash mod the table size. Subsequent
234probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000235
236All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000237
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000238The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000239contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000240Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000241
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000242lookdict() is general-purpose, and may return NULL if (and only if) a
243comparison raises an exception (this was new in Python 2.5).
Guido van Rossum89d8c602007-09-18 17:26:56 +0000244lookdict_unicode() below is specialized to string keys, comparison of which can
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000245never raise an exception; that function can never return NULL. For both, when
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000246the key isn't found a PyDictEntry* is returned for which the me_value field is
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000247NULL; this is the slot in the dict at which the key would have been found, and
248the caller can (if it wishes) add the <key, value> pair to the returned
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000249PyDictEntry*.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000250*/
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000251static PyDictEntry *
252lookdict(PyDictObject *mp, PyObject *key, register long hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000253{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000254 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000255 register size_t perturb;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000256 register PyDictEntry *freeslot;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000257 register size_t mask = (size_t)mp->ma_mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000258 PyDictEntry *ep0 = mp->ma_table;
259 register PyDictEntry *ep;
Fred Drakec88b99c2000-08-31 19:04:07 +0000260 register int cmp;
Tim Peters453163d2001-06-03 04:54:32 +0000261 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000262
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000263 i = (size_t)hash & mask;
Guido van Rossumefb46091997-01-29 15:53:56 +0000264 ep = &ep0[i];
Guido van Rossumc1c7b1a1998-10-06 16:01:14 +0000265 if (ep->me_key == NULL || ep->me_key == key)
Guido van Rossum7d181591997-01-16 21:06:45 +0000266 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000267
Guido van Rossum16e93a81997-01-28 00:00:11 +0000268 if (ep->me_key == dummy)
Guido van Rossum7d181591997-01-16 21:06:45 +0000269 freeslot = ep;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000270 else {
Fred Drakec88b99c2000-08-31 19:04:07 +0000271 if (ep->me_hash == hash) {
Tim Peters453163d2001-06-03 04:54:32 +0000272 startkey = ep->me_key;
273 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000274 if (cmp < 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000275 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000276 if (ep0 == mp->ma_table && ep->me_key == startkey) {
277 if (cmp > 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000278 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000279 }
280 else {
281 /* The compare did major nasty stuff to the
282 * dict: start over.
283 * XXX A clever adversary could prevent this
284 * XXX from terminating.
285 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000286 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000287 }
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000288 }
289 freeslot = NULL;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000290 }
Tim Peters15d49292001-05-27 07:39:22 +0000291
Tim Peters342c65e2001-05-13 06:43:53 +0000292 /* In the loop, me_key == dummy is by far (factor of 100s) the
293 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000294 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
295 i = (i << 2) + i + perturb + 1;
296 ep = &ep0[i & mask];
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000297 if (ep->me_key == NULL)
298 return freeslot == NULL ? ep : freeslot;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000299 if (ep->me_key == key)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000300 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000301 if (ep->me_hash == hash && ep->me_key != dummy) {
Tim Peters453163d2001-06-03 04:54:32 +0000302 startkey = ep->me_key;
303 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000304 if (cmp < 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000305 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000306 if (ep0 == mp->ma_table && ep->me_key == startkey) {
307 if (cmp > 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000308 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000309 }
310 else {
311 /* The compare did major nasty stuff to the
312 * dict: start over.
313 * XXX A clever adversary could prevent this
314 * XXX from terminating.
315 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000316 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000317 }
Guido van Rossum16e93a81997-01-28 00:00:11 +0000318 }
Tim Peters342c65e2001-05-13 06:43:53 +0000319 else if (ep->me_key == dummy && freeslot == NULL)
320 freeslot = ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000321 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000322 assert(0); /* NOT REACHED */
323 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000324}
325
Guido van Rossum89d8c602007-09-18 17:26:56 +0000326/* Return 1 if two unicode objects are equal, 0 if not. */
327static int
328unicode_eq(PyObject *aa, PyObject *bb)
329{
330 PyUnicodeObject *a = (PyUnicodeObject *)aa;
331 PyUnicodeObject *b = (PyUnicodeObject *)bb;
332
333 if (a->length != b->length)
334 return 0;
335 if (a->length == 0)
336 return 1;
337 if (a->str[0] != b->str[0])
338 return 0;
339 if (a->length == 1)
340 return 1;
Guido van Rossume4a9e782007-09-18 18:39:50 +0000341 return memcmp(a->str, b->str, a->length * sizeof(Py_UNICODE)) == 0;
Guido van Rossum89d8c602007-09-18 17:26:56 +0000342}
343
344
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000345/*
Guido van Rossum89d8c602007-09-18 17:26:56 +0000346 * Hacked up version of lookdict which can assume keys are always
347 * unicodes; this assumption allows testing for errors during
348 * PyObject_RichCompareBool() to be dropped; unicode-unicode
349 * comparisons never raise exceptions. This also means we don't need
350 * to go through PyObject_RichCompareBool(); we can always use
351 * unicode_eq() directly.
Fred Drake1bff34a2000-08-31 19:31:38 +0000352 *
Guido van Rossum89d8c602007-09-18 17:26:56 +0000353 * This is valuable because dicts with only unicode keys are very common.
Fred Drake1bff34a2000-08-31 19:31:38 +0000354 */
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000355static PyDictEntry *
356lookdict_unicode(PyDictObject *mp, PyObject *key, register long hash)
Fred Drake1bff34a2000-08-31 19:31:38 +0000357{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000358 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000359 register size_t perturb;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000360 register PyDictEntry *freeslot;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000361 register size_t mask = (size_t)mp->ma_mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000362 PyDictEntry *ep0 = mp->ma_table;
363 register PyDictEntry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000364
Guido van Rossum89d8c602007-09-18 17:26:56 +0000365 /* Make sure this function doesn't have to handle non-unicode keys,
Tim Peters0ab085c2001-09-14 00:25:33 +0000366 including subclasses of str; e.g., one reason to subclass
Guido van Rossum89d8c602007-09-18 17:26:56 +0000367 unicodes is to override __eq__, and for speed we don't cater to
Tim Peters0ab085c2001-09-14 00:25:33 +0000368 that here. */
Guido van Rossum89d8c602007-09-18 17:26:56 +0000369 if (!PyUnicode_CheckExact(key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000370#ifdef SHOW_CONVERSION_COUNTS
371 ++converted;
372#endif
373 mp->ma_lookup = lookdict;
374 return lookdict(mp, key, hash);
375 }
Tim Peters2f228e72001-05-13 00:19:31 +0000376 i = hash & mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000377 ep = &ep0[i];
378 if (ep->me_key == NULL || ep->me_key == key)
379 return ep;
380 if (ep->me_key == dummy)
381 freeslot = ep;
382 else {
Guido van Rossum89d8c602007-09-18 17:26:56 +0000383 if (ep->me_hash == hash && unicode_eq(ep->me_key, key))
Fred Drake1bff34a2000-08-31 19:31:38 +0000384 return ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000385 freeslot = NULL;
386 }
Tim Peters15d49292001-05-27 07:39:22 +0000387
Tim Peters342c65e2001-05-13 06:43:53 +0000388 /* In the loop, me_key == dummy is by far (factor of 100s) the
389 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000390 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
391 i = (i << 2) + i + perturb + 1;
392 ep = &ep0[i & mask];
Tim Peters342c65e2001-05-13 06:43:53 +0000393 if (ep->me_key == NULL)
394 return freeslot == NULL ? ep : freeslot;
395 if (ep->me_key == key
396 || (ep->me_hash == hash
397 && ep->me_key != dummy
Guido van Rossum89d8c602007-09-18 17:26:56 +0000398 && unicode_eq(ep->me_key, key)))
Fred Drake1bff34a2000-08-31 19:31:38 +0000399 return ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000400 if (ep->me_key == dummy && freeslot == NULL)
Tim Peters342c65e2001-05-13 06:43:53 +0000401 freeslot = ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000402 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000403 assert(0); /* NOT REACHED */
404 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000405}
406
407/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000408Internal routine to insert a new item into the table.
409Used both by the internal resize routine and by the public insert routine.
410Eats a reference to key and one to value.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000411Returns -1 if an error occurred, or 0 on success.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000412*/
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000413static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000414insertdict(register PyDictObject *mp, PyObject *key, long hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000415{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000416 PyObject *old_value;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000417 register PyDictEntry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000418 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
419
420 assert(mp->ma_lookup != NULL);
421 ep = mp->ma_lookup(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000422 if (ep == NULL) {
423 Py_DECREF(key);
424 Py_DECREF(value);
425 return -1;
426 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000427 if (ep->me_value != NULL) {
Guido van Rossumd7047b31995-01-02 19:07:15 +0000428 old_value = ep->me_value;
429 ep->me_value = value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000430 Py_DECREF(old_value); /* which **CAN** re-enter */
431 Py_DECREF(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000432 }
433 else {
434 if (ep->me_key == NULL)
435 mp->ma_fill++;
Raymond Hettinger07ead172005-02-05 23:42:57 +0000436 else {
437 assert(ep->me_key == dummy);
438 Py_DECREF(dummy);
439 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000440 ep->me_key = key;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000441 ep->me_hash = (Py_ssize_t)hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000442 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000443 mp->ma_used++;
444 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000445 return 0;
446}
447
448/*
449Internal routine used by dictresize() to insert an item which is
450known to be absent from the dict. This routine also assumes that
451the dict contains no deleted entries. Besides the performance benefit,
452using insertdict() in dictresize() is dangerous (SF bug #1456209).
453Note that no refcounts are changed by this routine; if needed, the caller
454is responsible for incref'ing `key` and `value`.
455*/
456static void
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000457insertdict_clean(register PyDictObject *mp, PyObject *key, long hash,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000458 PyObject *value)
459{
460 register size_t i;
461 register size_t perturb;
462 register size_t mask = (size_t)mp->ma_mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000463 PyDictEntry *ep0 = mp->ma_table;
464 register PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000465
466 i = hash & mask;
467 ep = &ep0[i];
468 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
469 i = (i << 2) + i + perturb + 1;
470 ep = &ep0[i & mask];
471 }
472 assert(ep->me_value == NULL);
473 mp->ma_fill++;
474 ep->me_key = key;
475 ep->me_hash = (Py_ssize_t)hash;
476 ep->me_value = value;
477 mp->ma_used++;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000478}
479
480/*
481Restructure the table by allocating a new table and reinserting all
482items again. When entries have been deleted, the new table may
483actually be smaller than the old one.
484*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000485static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000486dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000487{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000488 Py_ssize_t newsize;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000489 PyDictEntry *oldtable, *newtable, *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000490 Py_ssize_t i;
Tim Peters0c6010b2001-05-23 23:33:57 +0000491 int is_oldtable_malloced;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000492 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000493
494 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000495
Tim Peterseb28ef22001-06-02 05:27:19 +0000496 /* Find the smallest table size > minused. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000497 for (newsize = PyDict_MINSIZE;
Tim Peterseb28ef22001-06-02 05:27:19 +0000498 newsize <= minused && newsize > 0;
499 newsize <<= 1)
500 ;
501 if (newsize <= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000502 PyErr_NoMemory();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000503 return -1;
504 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000505
506 /* Get space for a new table. */
507 oldtable = mp->ma_table;
508 assert(oldtable != NULL);
509 is_oldtable_malloced = oldtable != mp->ma_smalltable;
510
Tim Peters6d6c1a32001-08-02 04:15:00 +0000511 if (newsize == PyDict_MINSIZE) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000512 /* A large table is shrinking, or we can't get any smaller. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000513 newtable = mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000514 if (newtable == oldtable) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000515 if (mp->ma_fill == mp->ma_used) {
516 /* No dummies, so no point doing anything. */
Tim Peters0c6010b2001-05-23 23:33:57 +0000517 return 0;
Tim Petersf8a548c2001-05-24 16:26:40 +0000518 }
519 /* We're not going to resize it, but rebuild the
520 table anyway to purge old dummy entries.
521 Subtle: This is *necessary* if fill==size,
522 as lookdict needs at least one virgin slot to
523 terminate failing searches. If fill < size, it's
524 merely desirable, as dummies slow searches. */
525 assert(mp->ma_fill > mp->ma_used);
Tim Peters0c6010b2001-05-23 23:33:57 +0000526 memcpy(small_copy, oldtable, sizeof(small_copy));
527 oldtable = small_copy;
528 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000529 }
530 else {
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000531 newtable = PyMem_NEW(PyDictEntry, newsize);
Tim Petersdea48ec2001-05-22 20:40:22 +0000532 if (newtable == NULL) {
533 PyErr_NoMemory();
534 return -1;
535 }
536 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000537
538 /* Make the dict empty, using the new table. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000539 assert(newtable != oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000540 mp->ma_table = newtable;
Tim Petersafb6ae82001-06-04 21:00:21 +0000541 mp->ma_mask = newsize - 1;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000542 memset(newtable, 0, sizeof(PyDictEntry) * newsize);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000543 mp->ma_used = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000544 i = mp->ma_fill;
545 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000546
Tim Peters19283142001-05-17 22:25:34 +0000547 /* Copy the data over; this is refcount-neutral for active entries;
548 dummy entries aren't copied over, of course */
Tim Petersdea48ec2001-05-22 20:40:22 +0000549 for (ep = oldtable; i > 0; ep++) {
550 if (ep->me_value != NULL) { /* active entry */
551 --i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000552 insertdict_clean(mp, ep->me_key, (long)ep->me_hash,
553 ep->me_value);
Tim Petersdea48ec2001-05-22 20:40:22 +0000554 }
Tim Peters19283142001-05-17 22:25:34 +0000555 else if (ep->me_key != NULL) { /* dummy entry */
Tim Petersdea48ec2001-05-22 20:40:22 +0000556 --i;
Tim Peters19283142001-05-17 22:25:34 +0000557 assert(ep->me_key == dummy);
558 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000559 }
Tim Peters19283142001-05-17 22:25:34 +0000560 /* else key == value == NULL: nothing to do */
Guido van Rossumd7047b31995-01-02 19:07:15 +0000561 }
562
Tim Peters0c6010b2001-05-23 23:33:57 +0000563 if (is_oldtable_malloced)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000564 PyMem_DEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000565 return 0;
566}
567
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000568/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
569 * that may occur (originally dicts supported only string keys, and exceptions
570 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000571 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000572 * (suppressed) occurred while computing the key's hash, or that some error
573 * (suppressed) occurred when comparing keys in the dict's internal probe
574 * sequence. A nasty example of the latter is when a Python-coded comparison
575 * function hits a stack-depth error, which can cause this to return NULL
576 * even if the key is present.
577 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000578PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000579PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000580{
581 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000582 PyDictObject *mp = (PyDictObject *)op;
583 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000584 PyThreadState *tstate;
585 if (!PyDict_Check(op))
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000586 return NULL;
Guido van Rossum4d027722007-09-18 04:30:42 +0000587 if (!PyUnicode_CheckExact(key) ||
588 (hash = ((PyUnicodeObject *) key)->hash) == -1)
Guido van Rossumca756f21997-01-23 19:39:29 +0000589 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000590 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000591 if (hash == -1) {
592 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000593 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000594 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000595 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000596
597 /* We can arrive here with a NULL tstate during initialization:
598 try running "python -Wi" for an example related to string
599 interning. Let's just hope that no exception occurs then... */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000600 tstate = _PyThreadState_Current;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000601 if (tstate != NULL && tstate->curexc_type != NULL) {
602 /* preserve the existing exception */
603 PyObject *err_type, *err_value, *err_tb;
604 PyErr_Fetch(&err_type, &err_value, &err_tb);
605 ep = (mp->ma_lookup)(mp, key, hash);
606 /* ignore errors */
607 PyErr_Restore(err_type, err_value, err_tb);
608 if (ep == NULL)
609 return NULL;
610 }
611 else {
612 ep = (mp->ma_lookup)(mp, key, hash);
613 if (ep == NULL) {
614 PyErr_Clear();
615 return NULL;
616 }
617 }
618 return ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000619}
620
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000621/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
622 This returns NULL *with* an exception set if an exception occurred.
623 It returns NULL *without* an exception set if the key wasn't present.
624*/
625PyObject *
626PyDict_GetItemWithError(PyObject *op, PyObject *key)
627{
628 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000629 PyDictObject*mp = (PyDictObject *)op;
630 PyDictEntry *ep;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000631
632 if (!PyDict_Check(op)) {
633 PyErr_BadInternalCall();
634 return NULL;
635 }
Guido van Rossum89d8c602007-09-18 17:26:56 +0000636 if (!PyUnicode_CheckExact(key) ||
637 (hash = ((PyUnicodeObject *) key)->hash) == -1)
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000638 {
639 hash = PyObject_Hash(key);
640 if (hash == -1) {
641 return NULL;
642 }
643 }
644
645 ep = (mp->ma_lookup)(mp, key, hash);
646 if (ep == NULL)
647 return NULL;
648 return ep->me_value;
649}
650
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000651/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
Tim Peters60b29962006-01-01 01:19:23 +0000652 * dictionary if it's merely replacing the value for an existing key.
653 * This means that it's safe to loop over a dictionary with PyDict_Next()
654 * and occasionally replace a value -- but you can't insert new keys or
655 * remove them.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000656 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000657int
Tim Peters1f5871e2000-07-04 17:44:48 +0000658PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000659{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000660 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000661 register long hash;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000662 register Py_ssize_t n_used;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000663
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000664 if (!PyDict_Check(op)) {
665 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000666 return -1;
667 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000668 assert(key);
669 assert(value);
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000670 mp = (PyDictObject *)op;
Guido van Rossum4d027722007-09-18 04:30:42 +0000671 if (!PyUnicode_CheckExact(key) ||
672 (hash = ((PyUnicodeObject *) key)->hash) == -1)
673 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000674 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000675 if (hash == -1)
676 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000677 }
Tim Petersafb6ae82001-06-04 21:00:21 +0000678 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000679 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000680 Py_INCREF(value);
681 Py_INCREF(key);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000682 if (insertdict(mp, key, hash, value) != 0)
683 return -1;
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000684 /* If we added a key, we can safely resize. Otherwise just return!
685 * If fill >= 2/3 size, adjust size. Normally, this doubles or
686 * quaduples the size, but it's also possible for the dict to shrink
Tim Peters60b29962006-01-01 01:19:23 +0000687 * (if ma_fill is much larger than ma_used, meaning a lot of dict
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000688 * keys have been * deleted).
Tim Peters60b29962006-01-01 01:19:23 +0000689 *
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000690 * Quadrupling the size improves average dictionary sparseness
691 * (reducing collisions) at the cost of some memory and iteration
692 * speed (which loops over every possible entry). It also halves
693 * the number of expensive resize operations in a growing dictionary.
Tim Peters60b29962006-01-01 01:19:23 +0000694 *
695 * Very large dictionaries (over 50K items) use doubling instead.
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000696 * This may help applications with severe memory constraints.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000697 */
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000698 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
699 return 0;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000700 return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000701}
702
703int
Tim Peters1f5871e2000-07-04 17:44:48 +0000704PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000705{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000706 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000707 register long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000708 register PyDictEntry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000709 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000710
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000711 if (!PyDict_Check(op)) {
712 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000713 return -1;
714 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000715 assert(key);
Guido van Rossum89d8c602007-09-18 17:26:56 +0000716 if (!PyUnicode_CheckExact(key) ||
717 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000718 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000719 if (hash == -1)
720 return -1;
721 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000722 mp = (PyDictObject *)op;
Fred Drake1bff34a2000-08-31 19:31:38 +0000723 ep = (mp->ma_lookup)(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000724 if (ep == NULL)
725 return -1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000726 if (ep->me_value == NULL) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000727 set_key_error(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000728 return -1;
729 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000730 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000731 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000732 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000733 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000734 ep->me_value = NULL;
735 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000736 Py_DECREF(old_value);
737 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000738 return 0;
739}
740
Guido van Rossum25831651993-05-19 14:50:45 +0000741void
Tim Peters1f5871e2000-07-04 17:44:48 +0000742PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000743{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000744 PyDictObject *mp;
745 PyDictEntry *ep, *table;
Tim Petersdea48ec2001-05-22 20:40:22 +0000746 int table_is_malloced;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000747 Py_ssize_t fill;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000748 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Petersdea48ec2001-05-22 20:40:22 +0000749#ifdef Py_DEBUG
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000750 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +0000751#endif
752
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000753 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000754 return;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000755 mp = (PyDictObject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000756#ifdef Py_DEBUG
Tim Petersafb6ae82001-06-04 21:00:21 +0000757 n = mp->ma_mask + 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000758 i = 0;
759#endif
760
761 table = mp->ma_table;
762 assert(table != NULL);
763 table_is_malloced = table != mp->ma_smalltable;
764
765 /* This is delicate. During the process of clearing the dict,
766 * decrefs can cause the dict to mutate. To avoid fatal confusion
767 * (voice of experience), we have to make the dict empty before
768 * clearing the slots, and never refer to anything via mp->xxx while
769 * clearing.
770 */
771 fill = mp->ma_fill;
772 if (table_is_malloced)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000773 EMPTY_TO_MINSIZE(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000774
775 else if (fill > 0) {
776 /* It's a small table with something that needs to be cleared.
777 * Afraid the only safe way is to copy the dict entries into
778 * another small table first.
779 */
780 memcpy(small_copy, table, sizeof(small_copy));
781 table = small_copy;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000782 EMPTY_TO_MINSIZE(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000783 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000784 /* else it's a small table that's already empty */
785
786 /* Now we can finally clear things. If C had refcounts, we could
787 * assert that the refcount on table is 1 now, i.e. that this function
788 * has unique access to it, so decref side-effects can't alter it.
789 */
790 for (ep = table; fill > 0; ++ep) {
791#ifdef Py_DEBUG
792 assert(i < n);
793 ++i;
794#endif
795 if (ep->me_key) {
796 --fill;
797 Py_DECREF(ep->me_key);
798 Py_XDECREF(ep->me_value);
799 }
800#ifdef Py_DEBUG
801 else
802 assert(ep->me_value == NULL);
803#endif
804 }
805
806 if (table_is_malloced)
807 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000808}
809
Tim Peters080c88b2003-02-15 03:01:11 +0000810/*
811 * Iterate over a dict. Use like so:
812 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000813 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +0000814 * PyObject *key, *value;
815 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +0000816 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +0000817 * Refer to borrowed references in key and value.
818 * }
819 *
820 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +0000821 * mutates the dict. One exception: it is safe if the loop merely changes
822 * the values associated with the keys (but doesn't insert new keys or
823 * delete keys), via PyDict_SetItem().
824 */
Guido van Rossum25831651993-05-19 14:50:45 +0000825int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000826PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000827{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000828 register Py_ssize_t i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000829 register Py_ssize_t mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000830 register PyDictEntry *ep;
Raymond Hettinger43442782004-03-17 21:55:03 +0000831
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000832 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000833 return 0;
Guido van Rossum25831651993-05-19 14:50:45 +0000834 i = *ppos;
835 if (i < 0)
836 return 0;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000837 ep = ((PyDictObject *)op)->ma_table;
838 mask = ((PyDictObject *)op)->ma_mask;
Raymond Hettinger43442782004-03-17 21:55:03 +0000839 while (i <= mask && ep[i].me_value == NULL)
Guido van Rossum25831651993-05-19 14:50:45 +0000840 i++;
841 *ppos = i+1;
Raymond Hettinger43442782004-03-17 21:55:03 +0000842 if (i > mask)
Guido van Rossum25831651993-05-19 14:50:45 +0000843 return 0;
844 if (pkey)
Raymond Hettinger43442782004-03-17 21:55:03 +0000845 *pkey = ep[i].me_key;
Guido van Rossum25831651993-05-19 14:50:45 +0000846 if (pvalue)
Raymond Hettinger43442782004-03-17 21:55:03 +0000847 *pvalue = ep[i].me_value;
Guido van Rossum25831651993-05-19 14:50:45 +0000848 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000849}
850
Thomas Wouterscf297e42007-02-23 15:07:44 +0000851/* Internal version of PyDict_Next that returns a hash value in addition to the key and value.*/
852int
853_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue, long *phash)
854{
855 register Py_ssize_t i;
856 register Py_ssize_t mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000857 register PyDictEntry *ep;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000858
859 if (!PyDict_Check(op))
860 return 0;
861 i = *ppos;
862 if (i < 0)
863 return 0;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000864 ep = ((PyDictObject *)op)->ma_table;
865 mask = ((PyDictObject *)op)->ma_mask;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000866 while (i <= mask && ep[i].me_value == NULL)
867 i++;
868 *ppos = i+1;
869 if (i > mask)
870 return 0;
871 *phash = (long)(ep[i].me_hash);
872 if (pkey)
873 *pkey = ep[i].me_key;
874 if (pvalue)
875 *pvalue = ep[i].me_value;
876 return 1;
877}
878
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000879/* Methods */
880
881static void
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000882dict_dealloc(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000883{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000884 register PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000885 Py_ssize_t fill = mp->ma_fill;
Guido van Rossumff413af2002-03-28 20:34:59 +0000886 PyObject_GC_UnTrack(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000887 Py_TRASHCAN_SAFE_BEGIN(mp)
Tim Petersdea48ec2001-05-22 20:40:22 +0000888 for (ep = mp->ma_table; fill > 0; ep++) {
889 if (ep->me_key) {
890 --fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000891 Py_DECREF(ep->me_key);
Tim Petersdea48ec2001-05-22 20:40:22 +0000892 Py_XDECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000893 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000894 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000895 if (mp->ma_table != mp->ma_smalltable)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000896 PyMem_DEL(mp->ma_table);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000897 if (num_free_dicts < MAXFREEDICTS && Py_Type(mp) == &PyDict_Type)
Raymond Hettinger43442782004-03-17 21:55:03 +0000898 free_dicts[num_free_dicts++] = mp;
Tim Peters60b29962006-01-01 01:19:23 +0000899 else
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000900 Py_Type(mp)->tp_free((PyObject *)mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000901 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000902}
903
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000904static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000905dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000906{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000907 Py_ssize_t i;
Tim Petersa7259592001-06-16 05:11:17 +0000908 PyObject *s, *temp, *colon = NULL;
909 PyObject *pieces = NULL, *result = NULL;
910 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +0000911
Tim Petersa7259592001-06-16 05:11:17 +0000912 i = Py_ReprEnter((PyObject *)mp);
Guido van Rossum255443b1998-04-10 22:47:14 +0000913 if (i != 0) {
Walter Dörwald1ab83302007-05-18 17:15:44 +0000914 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
Guido van Rossum255443b1998-04-10 22:47:14 +0000915 }
916
Tim Petersa7259592001-06-16 05:11:17 +0000917 if (mp->ma_used == 0) {
Walter Dörwald1ab83302007-05-18 17:15:44 +0000918 result = PyUnicode_FromString("{}");
Tim Petersa7259592001-06-16 05:11:17 +0000919 goto Done;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000920 }
Tim Petersa7259592001-06-16 05:11:17 +0000921
922 pieces = PyList_New(0);
923 if (pieces == NULL)
924 goto Done;
925
Walter Dörwald1ab83302007-05-18 17:15:44 +0000926 colon = PyUnicode_FromString(": ");
Tim Petersa7259592001-06-16 05:11:17 +0000927 if (colon == NULL)
928 goto Done;
929
930 /* Do repr() on each key+value pair, and insert ": " between them.
931 Note that repr may mutate the dict. */
Tim Petersc6057842001-06-16 07:52:53 +0000932 i = 0;
933 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Tim Petersa7259592001-06-16 05:11:17 +0000934 int status;
935 /* Prevent repr from deleting value during key format. */
936 Py_INCREF(value);
937 s = PyObject_Repr(key);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000938 PyUnicode_Append(&s, colon);
939 PyUnicode_AppendAndDel(&s, PyObject_Repr(value));
Tim Petersa7259592001-06-16 05:11:17 +0000940 Py_DECREF(value);
941 if (s == NULL)
942 goto Done;
943 status = PyList_Append(pieces, s);
944 Py_DECREF(s); /* append created a new ref */
945 if (status < 0)
946 goto Done;
947 }
948
949 /* Add "{}" decorations to the first and last items. */
950 assert(PyList_GET_SIZE(pieces) > 0);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000951 s = PyUnicode_FromString("{");
Tim Petersa7259592001-06-16 05:11:17 +0000952 if (s == NULL)
953 goto Done;
954 temp = PyList_GET_ITEM(pieces, 0);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000955 PyUnicode_AppendAndDel(&s, temp);
Tim Petersa7259592001-06-16 05:11:17 +0000956 PyList_SET_ITEM(pieces, 0, s);
957 if (s == NULL)
958 goto Done;
959
Walter Dörwald1ab83302007-05-18 17:15:44 +0000960 s = PyUnicode_FromString("}");
Tim Petersa7259592001-06-16 05:11:17 +0000961 if (s == NULL)
962 goto Done;
963 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000964 PyUnicode_AppendAndDel(&temp, s);
Tim Petersa7259592001-06-16 05:11:17 +0000965 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
966 if (temp == NULL)
967 goto Done;
968
969 /* Paste them all together with ", " between. */
Walter Dörwald1ab83302007-05-18 17:15:44 +0000970 s = PyUnicode_FromString(", ");
Tim Petersa7259592001-06-16 05:11:17 +0000971 if (s == NULL)
972 goto Done;
Walter Dörwald1ab83302007-05-18 17:15:44 +0000973 result = PyUnicode_Join(s, pieces);
Tim Peters0ab085c2001-09-14 00:25:33 +0000974 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +0000975
976Done:
977 Py_XDECREF(pieces);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000978 Py_XDECREF(colon);
Tim Petersa7259592001-06-16 05:11:17 +0000979 Py_ReprLeave((PyObject *)mp);
980 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000981}
982
Martin v. Löwis18e16552006-02-15 17:27:45 +0000983static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000984dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000985{
986 return mp->ma_used;
987}
988
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000989static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000990dict_subscript(PyDictObject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000991{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000992 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000993 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000994 PyDictEntry *ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000995 assert(mp->ma_table != NULL);
Guido van Rossum89d8c602007-09-18 17:26:56 +0000996 if (!PyUnicode_CheckExact(key) ||
997 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000998 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000999 if (hash == -1)
1000 return NULL;
1001 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001002 ep = (mp->ma_lookup)(mp, key, hash);
1003 if (ep == NULL)
1004 return NULL;
1005 v = ep->me_value;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001006 if (v == NULL) {
1007 if (!PyDict_CheckExact(mp)) {
1008 /* Look up __missing__ method if we're a subclass. */
Guido van Rossum4b92a822006-02-25 23:32:30 +00001009 PyObject *missing;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001010 static PyObject *missing_str = NULL;
1011 if (missing_str == NULL)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001012 missing_str =
Martin v. Löwis5b222132007-06-10 09:51:05 +00001013 PyUnicode_InternFromString("__missing__");
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001014 missing = _PyType_Lookup(Py_Type(mp), missing_str);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001015 if (missing != NULL)
1016 return PyObject_CallFunctionObjArgs(missing,
1017 (PyObject *)mp, key, NULL);
1018 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001019 set_key_error(key);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001020 return NULL;
1021 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001022 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001023 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001024 return v;
1025}
1026
1027static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001028dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001029{
1030 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001031 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001032 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001033 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001034}
1035
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001036static PyMappingMethods dict_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001037 (lenfunc)dict_length, /*mp_length*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001038 (binaryfunc)dict_subscript, /*mp_subscript*/
1039 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001040};
1041
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001042static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001043dict_keys(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001044{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001045 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001046 register Py_ssize_t i, j;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001047 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001048 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001049
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001050 again:
1051 n = mp->ma_used;
1052 v = PyList_New(n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001053 if (v == NULL)
1054 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001055 if (n != mp->ma_used) {
1056 /* Durnit. The allocations caused the dict to resize.
1057 * Just start over, this shouldn't normally happen.
1058 */
1059 Py_DECREF(v);
1060 goto again;
1061 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001062 ep = mp->ma_table;
1063 mask = mp->ma_mask;
1064 for (i = 0, j = 0; i <= mask; i++) {
1065 if (ep[i].me_value != NULL) {
1066 PyObject *key = ep[i].me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001067 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001068 PyList_SET_ITEM(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001069 j++;
1070 }
1071 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001072 assert(j == n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001073 return v;
1074}
1075
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001076static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001077dict_values(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001078{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001079 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001080 register Py_ssize_t i, j;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001081 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001082 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001083
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001084 again:
1085 n = mp->ma_used;
1086 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001087 if (v == NULL)
1088 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001089 if (n != mp->ma_used) {
1090 /* Durnit. The allocations caused the dict to resize.
1091 * Just start over, this shouldn't normally happen.
1092 */
1093 Py_DECREF(v);
1094 goto again;
1095 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001096 ep = mp->ma_table;
1097 mask = mp->ma_mask;
1098 for (i = 0, j = 0; i <= mask; i++) {
1099 if (ep[i].me_value != NULL) {
1100 PyObject *value = ep[i].me_value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001101 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001102 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001103 j++;
1104 }
1105 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001106 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001107 return v;
1108}
1109
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001110static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001111dict_items(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001112{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001113 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001114 register Py_ssize_t i, j, n;
1115 Py_ssize_t mask;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001116 PyObject *item, *key, *value;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001117 PyDictEntry *ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001118
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001119 /* Preallocate the list of tuples, to avoid allocations during
1120 * the loop over the items, which could trigger GC, which
1121 * could resize the dict. :-(
1122 */
1123 again:
1124 n = mp->ma_used;
1125 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001126 if (v == NULL)
1127 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001128 for (i = 0; i < n; i++) {
1129 item = PyTuple_New(2);
1130 if (item == NULL) {
1131 Py_DECREF(v);
1132 return NULL;
1133 }
1134 PyList_SET_ITEM(v, i, item);
1135 }
1136 if (n != mp->ma_used) {
1137 /* Durnit. The allocations caused the dict to resize.
1138 * Just start over, this shouldn't normally happen.
1139 */
1140 Py_DECREF(v);
1141 goto again;
1142 }
1143 /* Nothing we do below makes any function calls. */
Raymond Hettinger43442782004-03-17 21:55:03 +00001144 ep = mp->ma_table;
1145 mask = mp->ma_mask;
1146 for (i = 0, j = 0; i <= mask; i++) {
Raymond Hettinger06905122004-03-19 10:30:00 +00001147 if ((value=ep[i].me_value) != NULL) {
Raymond Hettinger43442782004-03-17 21:55:03 +00001148 key = ep[i].me_key;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001149 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001150 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001151 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001152 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001153 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001154 j++;
1155 }
1156 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001157 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001158 return v;
1159}
1160
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001161static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001162dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001163{
1164 PyObject *seq;
1165 PyObject *value = Py_None;
1166 PyObject *it; /* iter(seq) */
1167 PyObject *key;
1168 PyObject *d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001169 int status;
1170
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001171 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001172 return NULL;
1173
1174 d = PyObject_CallObject(cls, NULL);
1175 if (d == NULL)
1176 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001177
Guido van Rossum58da9312007-11-10 23:39:45 +00001178 if (PyDict_CheckExact(d) && PyDict_CheckExact(seq)) {
1179 PyDictObject *mp = (PyDictObject *)d;
1180 PyObject *oldvalue;
1181 Py_ssize_t pos = 0;
1182 PyObject *key;
1183 long hash;
1184
1185 if (dictresize(mp, PySet_GET_SIZE(seq)))
1186 return NULL;
1187
1188 while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
1189 Py_INCREF(key);
1190 Py_INCREF(value);
1191 if (insertdict(mp, key, hash, value))
1192 return NULL;
1193 }
1194 return d;
1195 }
1196
Guido van Rossumd8faa362007-04-27 19:54:29 +00001197 if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) {
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001198 PyDictObject *mp = (PyDictObject *)d;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001199 Py_ssize_t pos = 0;
1200 PyObject *key;
1201 long hash;
1202
1203 if (dictresize(mp, PySet_GET_SIZE(seq)))
1204 return NULL;
1205
1206 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
1207 Py_INCREF(key);
1208 Py_INCREF(value);
1209 if (insertdict(mp, key, hash, value))
1210 return NULL;
1211 }
1212 return d;
1213 }
1214
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001215 it = PyObject_GetIter(seq);
1216 if (it == NULL){
1217 Py_DECREF(d);
1218 return NULL;
1219 }
1220
Guido van Rossum58da9312007-11-10 23:39:45 +00001221 if (PyDict_CheckExact(d)) {
1222 while ((key = PyIter_Next(it)) != NULL) {
1223 status = PyDict_SetItem(d, key, value);
1224 Py_DECREF(key);
1225 if (status < 0)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001226 goto Fail;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001227 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001228 } else {
1229 while ((key = PyIter_Next(it)) != NULL) {
1230 status = PyObject_SetItem(d, key, value);
1231 Py_DECREF(key);
1232 if (status < 0)
1233 goto Fail;
1234 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001235 }
1236
Guido van Rossum58da9312007-11-10 23:39:45 +00001237 if (PyErr_Occurred())
1238 goto Fail;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001239 Py_DECREF(it);
1240 return d;
1241
1242Fail:
1243 Py_DECREF(it);
1244 Py_DECREF(d);
1245 return NULL;
1246}
1247
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001248static int
1249dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001250{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001251 PyObject *arg = NULL;
1252 int result = 0;
1253
1254 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1255 result = -1;
1256
1257 else if (arg != NULL) {
1258 if (PyObject_HasAttrString(arg, "keys"))
1259 result = PyDict_Merge(self, arg, 1);
1260 else
1261 result = PyDict_MergeFromSeq2(self, arg, 1);
1262 }
1263 if (result == 0 && kwds != NULL)
1264 result = PyDict_Merge(self, kwds, 1);
1265 return result;
1266}
1267
1268static PyObject *
1269dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1270{
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001271 if (dict_update_common(self, args, kwds, "update") != -1)
1272 Py_RETURN_NONE;
1273 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001274}
1275
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001276/* Update unconditionally replaces existing items.
1277 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001278 otherwise it leaves existing items unchanged.
1279
1280 PyDict_{Update,Merge} update/merge from a mapping object.
1281
Tim Petersf582b822001-12-11 18:51:08 +00001282 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001283 producing iterable objects of length 2.
1284*/
1285
Tim Petersf582b822001-12-11 18:51:08 +00001286int
Tim Peters1fc240e2001-10-26 05:06:50 +00001287PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1288{
1289 PyObject *it; /* iter(seq2) */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001290 Py_ssize_t i; /* index into seq2 of current element */
Tim Peters1fc240e2001-10-26 05:06:50 +00001291 PyObject *item; /* seq2[i] */
1292 PyObject *fast; /* item as a 2-tuple or 2-list */
1293
1294 assert(d != NULL);
1295 assert(PyDict_Check(d));
1296 assert(seq2 != NULL);
1297
1298 it = PyObject_GetIter(seq2);
1299 if (it == NULL)
1300 return -1;
1301
1302 for (i = 0; ; ++i) {
1303 PyObject *key, *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001304 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001305
1306 fast = NULL;
1307 item = PyIter_Next(it);
1308 if (item == NULL) {
1309 if (PyErr_Occurred())
1310 goto Fail;
1311 break;
1312 }
1313
1314 /* Convert item to sequence, and verify length 2. */
1315 fast = PySequence_Fast(item, "");
1316 if (fast == NULL) {
1317 if (PyErr_ExceptionMatches(PyExc_TypeError))
1318 PyErr_Format(PyExc_TypeError,
1319 "cannot convert dictionary update "
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001320 "sequence element #%zd to a sequence",
Tim Peters1fc240e2001-10-26 05:06:50 +00001321 i);
1322 goto Fail;
1323 }
1324 n = PySequence_Fast_GET_SIZE(fast);
1325 if (n != 2) {
1326 PyErr_Format(PyExc_ValueError,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001327 "dictionary update sequence element #%zd "
Martin v. Löwis2c95cc62006-02-16 06:54:25 +00001328 "has length %zd; 2 is required",
Martin v. Löwise0e89f72006-02-16 06:59:22 +00001329 i, n);
Tim Peters1fc240e2001-10-26 05:06:50 +00001330 goto Fail;
1331 }
1332
1333 /* Update/merge with this (key, value) pair. */
1334 key = PySequence_Fast_GET_ITEM(fast, 0);
1335 value = PySequence_Fast_GET_ITEM(fast, 1);
1336 if (override || PyDict_GetItem(d, key) == NULL) {
1337 int status = PyDict_SetItem(d, key, value);
1338 if (status < 0)
1339 goto Fail;
1340 }
1341 Py_DECREF(fast);
1342 Py_DECREF(item);
1343 }
1344
1345 i = 0;
1346 goto Return;
1347Fail:
1348 Py_XDECREF(item);
1349 Py_XDECREF(fast);
1350 i = -1;
1351Return:
1352 Py_DECREF(it);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001353 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001354}
1355
Tim Peters6d6c1a32001-08-02 04:15:00 +00001356int
1357PyDict_Update(PyObject *a, PyObject *b)
1358{
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001359 return PyDict_Merge(a, b, 1);
1360}
1361
1362int
1363PyDict_Merge(PyObject *a, PyObject *b, int override)
1364{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001365 register PyDictObject *mp, *other;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001366 register Py_ssize_t i;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001367 PyDictEntry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001368
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001369 /* We accept for the argument either a concrete dictionary object,
1370 * or an abstract "mapping" object. For the former, we can do
1371 * things quite efficiently. For the latter, we only require that
1372 * PyMapping_Keys() and PyObject_GetItem() be supported.
1373 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001374 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1375 PyErr_BadInternalCall();
1376 return -1;
1377 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001378 mp = (PyDictObject*)a;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001379 if (PyDict_CheckExact(b)) {
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001380 other = (PyDictObject*)b;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001381 if (other == mp || other->ma_used == 0)
1382 /* a.update(a) or a.update({}); nothing to do */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001383 return 0;
Raymond Hettinger186e7392005-05-14 18:08:25 +00001384 if (mp->ma_used == 0)
1385 /* Since the target dict is empty, PyDict_GetItem()
1386 * always returns NULL. Setting override to 1
1387 * skips the unnecessary test.
1388 */
1389 override = 1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001390 /* Do one big resize at the start, rather than
1391 * incrementally resizing as we insert new items. Expect
1392 * that there will be no (or few) overlapping keys.
1393 */
1394 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
Raymond Hettingerc8d22902003-05-07 00:49:40 +00001395 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001396 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001397 }
1398 for (i = 0; i <= other->ma_mask; i++) {
1399 entry = &other->ma_table[i];
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001400 if (entry->me_value != NULL &&
1401 (override ||
1402 PyDict_GetItem(a, entry->me_key) == NULL)) {
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001403 Py_INCREF(entry->me_key);
1404 Py_INCREF(entry->me_value);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001405 if (insertdict(mp, entry->me_key,
1406 (long)entry->me_hash,
1407 entry->me_value) != 0)
1408 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001409 }
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001410 }
1411 }
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001412 else {
1413 /* Do it the generic, slower way */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001414 PyObject *keys = PyMapping_Keys(b);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001415 PyObject *iter;
1416 PyObject *key, *value;
1417 int status;
1418
1419 if (keys == NULL)
1420 /* Docstring says this is equivalent to E.keys() so
1421 * if E doesn't have a .keys() method we want
1422 * AttributeError to percolate up. Might as well
1423 * do the same for any other error.
1424 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001425 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001426
1427 iter = PyObject_GetIter(keys);
1428 Py_DECREF(keys);
1429 if (iter == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001430 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001431
1432 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001433 if (!override && PyDict_GetItem(a, key) != NULL) {
1434 Py_DECREF(key);
1435 continue;
1436 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001437 value = PyObject_GetItem(b, key);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001438 if (value == NULL) {
1439 Py_DECREF(iter);
1440 Py_DECREF(key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001441 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001442 }
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001443 status = PyDict_SetItem(a, key, value);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001444 Py_DECREF(key);
1445 Py_DECREF(value);
1446 if (status < 0) {
1447 Py_DECREF(iter);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001448 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001449 }
1450 }
1451 Py_DECREF(iter);
1452 if (PyErr_Occurred())
1453 /* Iterator completed, via error */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001454 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001455 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001456 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001457}
1458
1459static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001460dict_copy(register PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001461{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001462 return PyDict_Copy((PyObject*)mp);
1463}
1464
1465PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001466PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001467{
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001468 PyObject *copy;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001469
1470 if (o == NULL || !PyDict_Check(o)) {
1471 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001472 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001473 }
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001474 copy = PyDict_New();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001475 if (copy == NULL)
1476 return NULL;
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001477 if (PyDict_Merge(copy, o, 1) == 0)
1478 return copy;
1479 Py_DECREF(copy);
Raymond Hettinger1356f782005-04-15 15:58:42 +00001480 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001481}
1482
Martin v. Löwis18e16552006-02-15 17:27:45 +00001483Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00001484PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001485{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001486 if (mp == NULL || !PyDict_Check(mp)) {
1487 PyErr_BadInternalCall();
Jeremy Hylton7083bb72004-02-17 20:10:11 +00001488 return -1;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001489 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001490 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001491}
1492
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001493PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001494PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001495{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001496 if (mp == NULL || !PyDict_Check(mp)) {
1497 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001498 return NULL;
1499 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001500 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001501}
1502
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001503PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001504PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001505{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001506 if (mp == NULL || !PyDict_Check(mp)) {
1507 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001508 return NULL;
1509 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001510 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001511}
1512
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001513PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001514PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001515{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001516 if (mp == NULL || !PyDict_Check(mp)) {
1517 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001518 return NULL;
1519 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001520 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001521}
1522
Tim Peterse63415e2001-05-08 04:38:29 +00001523/* Return 1 if dicts equal, 0 if not, -1 if error.
1524 * Gets out as soon as any difference is detected.
1525 * Uses only Py_EQ comparison.
1526 */
1527static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001528dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00001529{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001530 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00001531
1532 if (a->ma_used != b->ma_used)
1533 /* can't be equal if # of entries differ */
1534 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001535
Tim Peterse63415e2001-05-08 04:38:29 +00001536 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Tim Petersafb6ae82001-06-04 21:00:21 +00001537 for (i = 0; i <= a->ma_mask; i++) {
Tim Peterse63415e2001-05-08 04:38:29 +00001538 PyObject *aval = a->ma_table[i].me_value;
1539 if (aval != NULL) {
1540 int cmp;
1541 PyObject *bval;
1542 PyObject *key = a->ma_table[i].me_key;
1543 /* temporarily bump aval's refcount to ensure it stays
1544 alive until we're done with it */
1545 Py_INCREF(aval);
Guido van Rossumdc5f6b22006-08-24 21:29:26 +00001546 /* ditto for key */
1547 Py_INCREF(key);
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001548 bval = PyDict_GetItemWithError((PyObject *)b, key);
Guido van Rossumdc5f6b22006-08-24 21:29:26 +00001549 Py_DECREF(key);
Tim Peterse63415e2001-05-08 04:38:29 +00001550 if (bval == NULL) {
1551 Py_DECREF(aval);
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001552 if (PyErr_Occurred())
1553 return -1;
Tim Peterse63415e2001-05-08 04:38:29 +00001554 return 0;
1555 }
1556 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1557 Py_DECREF(aval);
1558 if (cmp <= 0) /* error or not equal */
1559 return cmp;
1560 }
1561 }
1562 return 1;
1563 }
1564
1565static PyObject *
1566dict_richcompare(PyObject *v, PyObject *w, int op)
1567{
1568 int cmp;
1569 PyObject *res;
1570
1571 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1572 res = Py_NotImplemented;
1573 }
1574 else if (op == Py_EQ || op == Py_NE) {
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001575 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
Tim Peterse63415e2001-05-08 04:38:29 +00001576 if (cmp < 0)
1577 return NULL;
1578 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1579 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001580 else
1581 res = Py_NotImplemented;
Tim Peterse63415e2001-05-08 04:38:29 +00001582 Py_INCREF(res);
1583 return res;
1584 }
1585
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001586static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001587dict_contains(register PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001588{
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001589 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001590 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001591
Guido van Rossum89d8c602007-09-18 17:26:56 +00001592 if (!PyUnicode_CheckExact(key) ||
1593 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001594 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001595 if (hash == -1)
1596 return NULL;
1597 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001598 ep = (mp->ma_lookup)(mp, key, hash);
1599 if (ep == NULL)
1600 return NULL;
1601 return PyBool_FromLong(ep->me_value != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001602}
1603
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001604static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001605dict_get(register PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001606{
1607 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001608 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001609 PyObject *val = NULL;
1610 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001611 PyDictEntry *ep;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001612
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001613 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001614 return NULL;
1615
Guido van Rossum89d8c602007-09-18 17:26:56 +00001616 if (!PyUnicode_CheckExact(key) ||
1617 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Barry Warsawc38c5da1997-10-06 17:49:20 +00001618 hash = PyObject_Hash(key);
1619 if (hash == -1)
1620 return NULL;
1621 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001622 ep = (mp->ma_lookup)(mp, key, hash);
1623 if (ep == NULL)
1624 return NULL;
1625 val = ep->me_value;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001626 if (val == NULL)
1627 val = failobj;
1628 Py_INCREF(val);
1629 return val;
1630}
1631
1632
1633static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001634dict_setdefault(register PyDictObject *mp, PyObject *args)
Guido van Rossum164452c2000-08-08 16:12:54 +00001635{
1636 PyObject *key;
1637 PyObject *failobj = Py_None;
1638 PyObject *val = NULL;
1639 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001640 PyDictEntry *ep;
Guido van Rossum164452c2000-08-08 16:12:54 +00001641
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001642 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
Guido van Rossum164452c2000-08-08 16:12:54 +00001643 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001644
Guido van Rossum89d8c602007-09-18 17:26:56 +00001645 if (!PyUnicode_CheckExact(key) ||
1646 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossum164452c2000-08-08 16:12:54 +00001647 hash = PyObject_Hash(key);
1648 if (hash == -1)
1649 return NULL;
1650 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001651 ep = (mp->ma_lookup)(mp, key, hash);
1652 if (ep == NULL)
1653 return NULL;
1654 val = ep->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001655 if (val == NULL) {
1656 val = failobj;
1657 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1658 val = NULL;
1659 }
1660 Py_XINCREF(val);
1661 return val;
1662}
1663
1664
1665static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001666dict_clear(register PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001667{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001668 PyDict_Clear((PyObject *)mp);
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001669 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001670}
1671
Guido van Rossumba6ab842000-12-12 22:02:18 +00001672static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001673dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00001674{
1675 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001676 PyDictEntry *ep;
Guido van Rossume027d982002-04-12 15:11:59 +00001677 PyObject *old_value, *old_key;
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001678 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001679
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001680 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1681 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001682 if (mp->ma_used == 0) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001683 if (deflt) {
1684 Py_INCREF(deflt);
1685 return deflt;
1686 }
Guido van Rossume027d982002-04-12 15:11:59 +00001687 PyErr_SetString(PyExc_KeyError,
1688 "pop(): dictionary is empty");
1689 return NULL;
1690 }
Guido van Rossum89d8c602007-09-18 17:26:56 +00001691 if (!PyUnicode_CheckExact(key) ||
1692 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossume027d982002-04-12 15:11:59 +00001693 hash = PyObject_Hash(key);
1694 if (hash == -1)
1695 return NULL;
1696 }
1697 ep = (mp->ma_lookup)(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001698 if (ep == NULL)
1699 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001700 if (ep->me_value == NULL) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001701 if (deflt) {
1702 Py_INCREF(deflt);
1703 return deflt;
1704 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001705 set_key_error(key);
Guido van Rossume027d982002-04-12 15:11:59 +00001706 return NULL;
1707 }
1708 old_key = ep->me_key;
1709 Py_INCREF(dummy);
1710 ep->me_key = dummy;
1711 old_value = ep->me_value;
1712 ep->me_value = NULL;
1713 mp->ma_used--;
1714 Py_DECREF(old_key);
1715 return old_value;
1716}
1717
1718static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001719dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001720{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001721 Py_ssize_t i = 0;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001722 PyDictEntry *ep;
Guido van Rossumba6ab842000-12-12 22:02:18 +00001723 PyObject *res;
1724
Tim Petersf4b33f62001-06-02 05:42:29 +00001725 /* Allocate the result tuple before checking the size. Believe it
1726 * or not, this allocation could trigger a garbage collection which
1727 * could empty the dict, so if we checked the size first and that
1728 * happened, the result would be an infinite loop (searching for an
1729 * entry that no longer exists). Note that the usual popitem()
1730 * idiom is "while d: k, v = d.popitem()". so needing to throw the
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001731 * tuple away if the dict *is* empty isn't a significant
Tim Petersf4b33f62001-06-02 05:42:29 +00001732 * inefficiency -- possible, but unlikely in practice.
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001733 */
1734 res = PyTuple_New(2);
1735 if (res == NULL)
1736 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001737 if (mp->ma_used == 0) {
1738 Py_DECREF(res);
1739 PyErr_SetString(PyExc_KeyError,
1740 "popitem(): dictionary is empty");
1741 return NULL;
1742 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001743 /* Set ep to "the first" dict entry with a value. We abuse the hash
1744 * field of slot 0 to hold a search finger:
1745 * If slot 0 has a value, use slot 0.
1746 * Else slot 0 is being used to hold a search finger,
1747 * and we use its hash value as the first index to look.
1748 */
1749 ep = &mp->ma_table[0];
1750 if (ep->me_value == NULL) {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001751 i = ep->me_hash;
Tim Petersdea48ec2001-05-22 20:40:22 +00001752 /* The hash field may be a real hash value, or it may be a
1753 * legit search finger, or it may be a once-legit search
1754 * finger that's out of bounds now because it wrapped around
1755 * or the table shrunk -- simply make sure it's in bounds now.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001756 */
Tim Petersafb6ae82001-06-04 21:00:21 +00001757 if (i > mp->ma_mask || i < 1)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001758 i = 1; /* skip slot 0 */
1759 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1760 i++;
Tim Petersafb6ae82001-06-04 21:00:21 +00001761 if (i > mp->ma_mask)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001762 i = 1;
1763 }
1764 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001765 PyTuple_SET_ITEM(res, 0, ep->me_key);
1766 PyTuple_SET_ITEM(res, 1, ep->me_value);
1767 Py_INCREF(dummy);
1768 ep->me_key = dummy;
1769 ep->me_value = NULL;
1770 mp->ma_used--;
1771 assert(mp->ma_table[0].me_value == NULL);
1772 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001773 return res;
1774}
1775
Jeremy Hylton8caad492000-06-23 14:18:11 +00001776static int
1777dict_traverse(PyObject *op, visitproc visit, void *arg)
1778{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001779 Py_ssize_t i = 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001780 PyObject *pk;
1781 PyObject *pv;
1782
1783 while (PyDict_Next(op, &i, &pk, &pv)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001784 Py_VISIT(pk);
1785 Py_VISIT(pv);
Jeremy Hylton8caad492000-06-23 14:18:11 +00001786 }
1787 return 0;
1788}
1789
1790static int
1791dict_tp_clear(PyObject *op)
1792{
1793 PyDict_Clear(op);
1794 return 0;
1795}
1796
Tim Petersf7f88b12000-12-13 23:18:45 +00001797
Raymond Hettinger019a1482004-03-18 02:41:19 +00001798extern PyTypeObject PyDictIterKey_Type; /* Forward */
1799extern PyTypeObject PyDictIterValue_Type; /* Forward */
1800extern PyTypeObject PyDictIterItem_Type; /* Forward */
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001801static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001802
Guido van Rossum09e563a2001-05-01 12:10:21 +00001803
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001804PyDoc_STRVAR(contains__doc__,
1805"D.__contains__(k) -> True if D has a key k, else False");
1806
1807PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
1808
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001809PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001810"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001811
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001812PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001813"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 +00001814
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001815PyDoc_STRVAR(pop__doc__,
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001816"D.pop(k[,d]) -> v, remove specified key and return the corresponding value\n\
1817If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00001818
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001819PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00001820"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000018212-tuple; but raise KeyError if D is empty");
Tim Petersf7f88b12000-12-13 23:18:45 +00001822
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001823PyDoc_STRVAR(update__doc__,
Guido van Rossumb90c8482007-02-10 01:11:45 +00001824"D.update(E, **F) -> None. Update D from E and F: for k in E: D[k] = E[k]\
1825\n(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 +00001826
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001827PyDoc_STRVAR(fromkeys__doc__,
1828"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
1829v defaults to None.");
1830
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001831PyDoc_STRVAR(clear__doc__,
1832"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001833
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001834PyDoc_STRVAR(copy__doc__,
1835"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00001836
Guido van Rossumb90c8482007-02-10 01:11:45 +00001837/* Forward */
1838static PyObject *dictkeys_new(PyObject *);
1839static PyObject *dictitems_new(PyObject *);
1840static PyObject *dictvalues_new(PyObject *);
1841
Guido van Rossum45c85d12007-07-27 16:31:40 +00001842PyDoc_STRVAR(keys__doc__,
1843 "D.keys() -> a set-like object providing a view on D's keys");
1844PyDoc_STRVAR(items__doc__,
1845 "D.items() -> a set-like object providing a view on D's items");
1846PyDoc_STRVAR(values__doc__,
1847 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00001848
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001849static PyMethodDef mapp_methods[] = {
Guido van Rossume2b70bc2006-08-18 22:13:04 +00001850 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001851 contains__doc__},
Raymond Hettinger0c669672003-12-13 13:31:55 +00001852 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001853 getitem__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00001854 {"get", (PyCFunction)dict_get, METH_VARARGS,
1855 get__doc__},
1856 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
1857 setdefault_doc__},
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001858 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
Just van Rossuma797d812002-11-23 09:45:04 +00001859 pop__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001860 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001861 popitem__doc__},
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001862 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
Guido van Rossumeff072c2007-02-27 05:47:18 +00001863 keys__doc__},
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001864 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
Guido van Rossumeff072c2007-02-27 05:47:18 +00001865 items__doc__},
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001866 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
Guido van Rossumeff072c2007-02-27 05:47:18 +00001867 values__doc__},
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001868 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001869 update__doc__},
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001870 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
1871 fromkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001872 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001873 clear__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001874 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001875 copy__doc__},
1876 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001877};
1878
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001879/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001880int
1881PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001882{
1883 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001884 PyDictObject *mp = (PyDictObject *)op;
1885 PyDictEntry *ep;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001886
Guido van Rossum89d8c602007-09-18 17:26:56 +00001887 if (!PyUnicode_CheckExact(key) ||
1888 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001889 hash = PyObject_Hash(key);
1890 if (hash == -1)
1891 return -1;
1892 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001893 ep = (mp->ma_lookup)(mp, key, hash);
1894 return ep == NULL ? -1 : (ep->me_value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001895}
1896
Thomas Wouterscf297e42007-02-23 15:07:44 +00001897/* Internal version of PyDict_Contains used when the hash value is already known */
1898int
1899_PyDict_Contains(PyObject *op, PyObject *key, long hash)
1900{
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001901 PyDictObject *mp = (PyDictObject *)op;
1902 PyDictEntry *ep;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001903
1904 ep = (mp->ma_lookup)(mp, key, hash);
1905 return ep == NULL ? -1 : (ep->me_value != NULL);
1906}
1907
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001908/* Hack to implement "key in dict" */
1909static PySequenceMethods dict_as_sequence = {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001910 0, /* sq_length */
1911 0, /* sq_concat */
1912 0, /* sq_repeat */
1913 0, /* sq_item */
1914 0, /* sq_slice */
1915 0, /* sq_ass_item */
1916 0, /* sq_ass_slice */
1917 PyDict_Contains, /* sq_contains */
1918 0, /* sq_inplace_concat */
1919 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001920};
1921
Guido van Rossum09e563a2001-05-01 12:10:21 +00001922static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001923dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1924{
1925 PyObject *self;
1926
1927 assert(type != NULL && type->tp_alloc != NULL);
1928 self = type->tp_alloc(type, 0);
1929 if (self != NULL) {
1930 PyDictObject *d = (PyDictObject *)self;
1931 /* It's guaranteed that tp->alloc zeroed out the struct. */
1932 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
1933 INIT_NONZERO_DICT_SLOTS(d);
Guido van Rossum89d8c602007-09-18 17:26:56 +00001934 d->ma_lookup = lookdict_unicode;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001935#ifdef SHOW_CONVERSION_COUNTS
1936 ++created;
1937#endif
1938 }
1939 return self;
1940}
1941
Tim Peters25786c02001-09-02 08:22:48 +00001942static int
1943dict_init(PyObject *self, PyObject *args, PyObject *kwds)
1944{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001945 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00001946}
1947
Tim Peters6d6c1a32001-08-02 04:15:00 +00001948static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001949dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001950{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001951 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001952}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001953
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001954PyDoc_STRVAR(dictionary_doc,
Tim Petersa427a2b2001-10-29 22:25:45 +00001955"dict() -> new empty dictionary.\n"
1956"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Tim Peters1fc240e2001-10-26 05:06:50 +00001957" (key, value) pairs.\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00001958"dict(seq) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00001959" d = {}\n"
1960" for k, v in seq:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00001961" d[k] = v\n"
1962"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
1963" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00001964
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001965PyTypeObject PyDict_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001966 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Tim Petersa427a2b2001-10-29 22:25:45 +00001967 "dict",
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001968 sizeof(PyDictObject),
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001969 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00001970 (destructor)dict_dealloc, /* tp_dealloc */
Guido van Rossum04dbf3b2007-08-07 19:51:00 +00001971 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001972 0, /* tp_getattr */
Guido van Rossumb9324202001-01-18 00:39:02 +00001973 0, /* tp_setattr */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001974 0, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00001975 (reprfunc)dict_repr, /* tp_repr */
1976 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001977 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00001978 &dict_as_mapping, /* tp_as_mapping */
Guido van Rossum50e9fb92006-08-17 05:42:55 +00001979 0, /* tp_hash */
Guido van Rossumb9324202001-01-18 00:39:02 +00001980 0, /* tp_call */
1981 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001982 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumb9324202001-01-18 00:39:02 +00001983 0, /* tp_setattro */
1984 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00001985 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Thomas Wouters27d517b2007-02-25 20:39:11 +00001986 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
Tim Peters25786c02001-09-02 08:22:48 +00001987 dictionary_doc, /* tp_doc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001988 dict_traverse, /* tp_traverse */
1989 dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00001990 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001991 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00001992 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001993 0, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001994 mapp_methods, /* tp_methods */
1995 0, /* tp_members */
1996 0, /* tp_getset */
1997 0, /* tp_base */
1998 0, /* tp_dict */
1999 0, /* tp_descr_get */
2000 0, /* tp_descr_set */
2001 0, /* tp_dictoffset */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002002 dict_init, /* tp_init */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002003 PyType_GenericAlloc, /* tp_alloc */
2004 dict_new, /* tp_new */
Neil Schemenauer6189b892002-04-12 02:43:00 +00002005 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002006};
2007
Guido van Rossum3cca2451997-05-16 14:23:33 +00002008/* For backward compatibility with old dictionary interface */
2009
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002010PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002011PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002012{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002013 PyObject *kv, *rv;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002014 kv = PyUnicode_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00002015 if (kv == NULL)
2016 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002017 rv = PyDict_GetItem(v, kv);
2018 Py_DECREF(kv);
2019 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002020}
2021
2022int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002023PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002024{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002025 PyObject *kv;
2026 int err;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002027 kv = PyUnicode_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00002028 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002029 return -1;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002030 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00002031 err = PyDict_SetItem(v, kv, item);
2032 Py_DECREF(kv);
2033 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002034}
2035
2036int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002037PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002038{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002039 PyObject *kv;
2040 int err;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002041 kv = PyUnicode_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00002042 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002043 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002044 err = PyDict_DelItem(v, kv);
2045 Py_DECREF(kv);
2046 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002047}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002048
Raymond Hettinger019a1482004-03-18 02:41:19 +00002049/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002050
2051typedef struct {
2052 PyObject_HEAD
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002053 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002054 Py_ssize_t di_used;
2055 Py_ssize_t di_pos;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002056 PyObject* di_result; /* reusable result tuple for iteritems */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002057 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002058} dictiterobject;
2059
2060static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002061dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002062{
2063 dictiterobject *di;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002064 di = PyObject_New(dictiterobject, itertype);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002065 if (di == NULL)
2066 return NULL;
2067 Py_INCREF(dict);
2068 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00002069 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002070 di->di_pos = 0;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002071 di->len = dict->ma_used;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002072 if (itertype == &PyDictIterItem_Type) {
2073 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2074 if (di->di_result == NULL) {
2075 Py_DECREF(di);
2076 return NULL;
2077 }
2078 }
2079 else
2080 di->di_result = NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002081 return (PyObject *)di;
2082}
2083
2084static void
2085dictiter_dealloc(dictiterobject *di)
2086{
Guido van Rossum2147df72002-07-16 20:30:22 +00002087 Py_XDECREF(di->di_dict);
Raymond Hettinger019a1482004-03-18 02:41:19 +00002088 Py_XDECREF(di->di_result);
Neil Schemenauer6189b892002-04-12 02:43:00 +00002089 PyObject_Del(di);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002090}
2091
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002092static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002093dictiter_len(dictiterobject *di)
2094{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002095 Py_ssize_t len = 0;
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00002096 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002097 len = di->len;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002098 return PyInt_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002099}
2100
Guido van Rossumb90c8482007-02-10 01:11:45 +00002101PyDoc_STRVAR(length_hint_doc,
2102 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002103
2104static PyMethodDef dictiter_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002105 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
2106 length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002107 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002108};
2109
Raymond Hettinger019a1482004-03-18 02:41:19 +00002110static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002111{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002112 PyObject *key;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002113 register Py_ssize_t i, mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002114 register PyDictEntry *ep;
2115 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002116
Raymond Hettinger019a1482004-03-18 02:41:19 +00002117 if (d == NULL)
Guido van Rossum2147df72002-07-16 20:30:22 +00002118 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002119 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002120
Raymond Hettinger019a1482004-03-18 02:41:19 +00002121 if (di->di_used != d->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00002122 PyErr_SetString(PyExc_RuntimeError,
2123 "dictionary changed size during iteration");
Guido van Rossum2147df72002-07-16 20:30:22 +00002124 di->di_used = -1; /* Make this state sticky */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002125 return NULL;
2126 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002127
Raymond Hettinger019a1482004-03-18 02:41:19 +00002128 i = di->di_pos;
2129 if (i < 0)
2130 goto fail;
2131 ep = d->ma_table;
2132 mask = d->ma_mask;
2133 while (i <= mask && ep[i].me_value == NULL)
2134 i++;
2135 di->di_pos = i+1;
2136 if (i > mask)
2137 goto fail;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002138 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002139 key = ep[i].me_key;
2140 Py_INCREF(key);
2141 return key;
2142
2143fail:
2144 Py_DECREF(d);
Guido van Rossum2147df72002-07-16 20:30:22 +00002145 di->di_dict = NULL;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002146 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002147}
2148
Raymond Hettinger019a1482004-03-18 02:41:19 +00002149PyTypeObject PyDictIterKey_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002150 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002151 "dictionary-keyiterator", /* tp_name */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002152 sizeof(dictiterobject), /* tp_basicsize */
2153 0, /* tp_itemsize */
2154 /* methods */
2155 (destructor)dictiter_dealloc, /* tp_dealloc */
2156 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002157 0, /* tp_getattr */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002158 0, /* tp_setattr */
2159 0, /* tp_compare */
2160 0, /* tp_repr */
2161 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002162 0, /* tp_as_sequence */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002163 0, /* tp_as_mapping */
2164 0, /* tp_hash */
2165 0, /* tp_call */
2166 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002167 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002168 0, /* tp_setattro */
2169 0, /* tp_as_buffer */
2170 Py_TPFLAGS_DEFAULT, /* tp_flags */
2171 0, /* tp_doc */
2172 0, /* tp_traverse */
2173 0, /* tp_clear */
2174 0, /* tp_richcompare */
2175 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002176 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002177 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002178 dictiter_methods, /* tp_methods */
2179 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002180};
2181
2182static PyObject *dictiter_iternextvalue(dictiterobject *di)
2183{
2184 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002185 register Py_ssize_t i, mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002186 register PyDictEntry *ep;
2187 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002188
2189 if (d == NULL)
2190 return NULL;
2191 assert (PyDict_Check(d));
2192
2193 if (di->di_used != d->ma_used) {
2194 PyErr_SetString(PyExc_RuntimeError,
2195 "dictionary changed size during iteration");
2196 di->di_used = -1; /* Make this state sticky */
2197 return NULL;
2198 }
2199
2200 i = di->di_pos;
Guido van Rossum09240f62004-03-20 19:11:58 +00002201 mask = d->ma_mask;
2202 if (i < 0 || i > mask)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002203 goto fail;
2204 ep = d->ma_table;
Guido van Rossum09240f62004-03-20 19:11:58 +00002205 while ((value=ep[i].me_value) == NULL) {
Raymond Hettinger019a1482004-03-18 02:41:19 +00002206 i++;
Guido van Rossum09240f62004-03-20 19:11:58 +00002207 if (i > mask)
2208 goto fail;
2209 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002210 di->di_pos = i+1;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002211 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002212 Py_INCREF(value);
2213 return value;
2214
2215fail:
2216 Py_DECREF(d);
2217 di->di_dict = NULL;
2218 return NULL;
2219}
2220
2221PyTypeObject PyDictIterValue_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002222 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002223 "dictionary-valueiterator", /* tp_name */
2224 sizeof(dictiterobject), /* tp_basicsize */
2225 0, /* tp_itemsize */
2226 /* methods */
2227 (destructor)dictiter_dealloc, /* tp_dealloc */
2228 0, /* tp_print */
2229 0, /* tp_getattr */
2230 0, /* tp_setattr */
2231 0, /* tp_compare */
2232 0, /* tp_repr */
2233 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002234 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002235 0, /* tp_as_mapping */
2236 0, /* tp_hash */
2237 0, /* tp_call */
2238 0, /* tp_str */
2239 PyObject_GenericGetAttr, /* tp_getattro */
2240 0, /* tp_setattro */
2241 0, /* tp_as_buffer */
2242 Py_TPFLAGS_DEFAULT, /* tp_flags */
2243 0, /* tp_doc */
2244 0, /* tp_traverse */
2245 0, /* tp_clear */
2246 0, /* tp_richcompare */
2247 0, /* tp_weaklistoffset */
2248 PyObject_SelfIter, /* tp_iter */
2249 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002250 dictiter_methods, /* tp_methods */
2251 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002252};
2253
2254static PyObject *dictiter_iternextitem(dictiterobject *di)
2255{
2256 PyObject *key, *value, *result = di->di_result;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002257 register Py_ssize_t i, mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002258 register PyDictEntry *ep;
2259 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002260
2261 if (d == NULL)
2262 return NULL;
2263 assert (PyDict_Check(d));
2264
2265 if (di->di_used != d->ma_used) {
2266 PyErr_SetString(PyExc_RuntimeError,
2267 "dictionary changed size during iteration");
2268 di->di_used = -1; /* Make this state sticky */
2269 return NULL;
2270 }
2271
2272 i = di->di_pos;
2273 if (i < 0)
2274 goto fail;
2275 ep = d->ma_table;
2276 mask = d->ma_mask;
2277 while (i <= mask && ep[i].me_value == NULL)
2278 i++;
2279 di->di_pos = i+1;
2280 if (i > mask)
2281 goto fail;
2282
2283 if (result->ob_refcnt == 1) {
2284 Py_INCREF(result);
2285 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2286 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2287 } else {
2288 result = PyTuple_New(2);
Tim Peters60b29962006-01-01 01:19:23 +00002289 if (result == NULL)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002290 return NULL;
2291 }
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002292 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002293 key = ep[i].me_key;
2294 value = ep[i].me_value;
2295 Py_INCREF(key);
2296 Py_INCREF(value);
2297 PyTuple_SET_ITEM(result, 0, key);
2298 PyTuple_SET_ITEM(result, 1, value);
2299 return result;
2300
2301fail:
2302 Py_DECREF(d);
2303 di->di_dict = NULL;
2304 return NULL;
2305}
2306
2307PyTypeObject PyDictIterItem_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002308 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002309 "dictionary-itemiterator", /* tp_name */
2310 sizeof(dictiterobject), /* tp_basicsize */
2311 0, /* tp_itemsize */
2312 /* methods */
2313 (destructor)dictiter_dealloc, /* tp_dealloc */
2314 0, /* tp_print */
2315 0, /* tp_getattr */
2316 0, /* tp_setattr */
2317 0, /* tp_compare */
2318 0, /* tp_repr */
2319 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002320 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002321 0, /* tp_as_mapping */
2322 0, /* tp_hash */
2323 0, /* tp_call */
2324 0, /* tp_str */
2325 PyObject_GenericGetAttr, /* tp_getattro */
2326 0, /* tp_setattro */
2327 0, /* tp_as_buffer */
2328 Py_TPFLAGS_DEFAULT, /* tp_flags */
2329 0, /* tp_doc */
2330 0, /* tp_traverse */
2331 0, /* tp_clear */
2332 0, /* tp_richcompare */
2333 0, /* tp_weaklistoffset */
2334 PyObject_SelfIter, /* tp_iter */
2335 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002336 dictiter_methods, /* tp_methods */
2337 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002338};
Guido van Rossumb90c8482007-02-10 01:11:45 +00002339
2340
Guido van Rossum3ac67412007-02-10 18:55:06 +00002341/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002342/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002343/***********************************************/
2344
Guido van Rossumb90c8482007-02-10 01:11:45 +00002345/* The instance lay-out is the same for all three; but the type differs. */
2346
2347typedef struct {
2348 PyObject_HEAD
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002349 PyDictObject *dv_dict;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002350} dictviewobject;
2351
2352
2353static void
Guido van Rossum3ac67412007-02-10 18:55:06 +00002354dictview_dealloc(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002355{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002356 Py_XDECREF(dv->dv_dict);
2357 PyObject_Del(dv);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002358}
2359
Guido van Rossum83825ac2007-02-10 04:54:19 +00002360static Py_ssize_t
Guido van Rossum3ac67412007-02-10 18:55:06 +00002361dictview_len(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002362{
2363 Py_ssize_t len = 0;
Guido van Rossum3ac67412007-02-10 18:55:06 +00002364 if (dv->dv_dict != NULL)
2365 len = dv->dv_dict->ma_used;
Guido van Rossum83825ac2007-02-10 04:54:19 +00002366 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002367}
2368
2369static PyObject *
2370dictview_new(PyObject *dict, PyTypeObject *type)
2371{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002372 dictviewobject *dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002373 if (dict == NULL) {
2374 PyErr_BadInternalCall();
2375 return NULL;
2376 }
2377 if (!PyDict_Check(dict)) {
2378 /* XXX Get rid of this restriction later */
2379 PyErr_Format(PyExc_TypeError,
2380 "%s() requires a dict argument, not '%s'",
2381 type->tp_name, dict->ob_type->tp_name);
2382 return NULL;
2383 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002384 dv = PyObject_New(dictviewobject, type);
2385 if (dv == NULL)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002386 return NULL;
2387 Py_INCREF(dict);
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002388 dv->dv_dict = (PyDictObject *)dict;
Guido van Rossum3ac67412007-02-10 18:55:06 +00002389 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002390}
2391
Neal Norwitze36f2ba2007-02-26 23:12:28 +00002392/* TODO(guido): The views objects are not complete:
2393
2394 * support more set operations
2395 * support arbitrary mappings?
2396 - either these should be static or exported in dictobject.h
2397 - if public then they should probably be in builtins
2398*/
2399
Guido van Rossumd9214d12007-02-12 02:23:40 +00002400/* Forward */
2401PyTypeObject PyDictKeys_Type;
2402PyTypeObject PyDictItems_Type;
2403PyTypeObject PyDictValues_Type;
2404
2405#define PyDictKeys_Check(obj) ((obj)->ob_type == &PyDictKeys_Type)
2406#define PyDictItems_Check(obj) ((obj)->ob_type == &PyDictItems_Type)
2407#define PyDictValues_Check(obj) ((obj)->ob_type == &PyDictValues_Type)
2408
2409/* This excludes Values, since they are not sets. */
2410# define PyDictViewSet_Check(obj) \
2411 (PyDictKeys_Check(obj) || PyDictItems_Check(obj))
2412
Guido van Rossumaac530c2007-08-24 22:33:45 +00002413/* Return 1 if self is a subset of other, iterating over self;
2414 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002415static int
2416all_contained_in(PyObject *self, PyObject *other)
2417{
2418 PyObject *iter = PyObject_GetIter(self);
2419 int ok = 1;
2420
2421 if (iter == NULL)
2422 return -1;
2423 for (;;) {
2424 PyObject *next = PyIter_Next(iter);
2425 if (next == NULL) {
2426 if (PyErr_Occurred())
2427 ok = -1;
2428 break;
2429 }
2430 ok = PySequence_Contains(other, next);
2431 Py_DECREF(next);
2432 if (ok <= 0)
2433 break;
2434 }
2435 Py_DECREF(iter);
2436 return ok;
2437}
2438
2439static PyObject *
2440dictview_richcompare(PyObject *self, PyObject *other, int op)
2441{
Guido van Rossumaac530c2007-08-24 22:33:45 +00002442 Py_ssize_t len_self, len_other;
2443 int ok;
2444 PyObject *result;
2445
Guido van Rossumd9214d12007-02-12 02:23:40 +00002446 assert(self != NULL);
2447 assert(PyDictViewSet_Check(self));
2448 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00002449
Guido van Rossumaac530c2007-08-24 22:33:45 +00002450 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other)) {
Guido van Rossumd9214d12007-02-12 02:23:40 +00002451 Py_INCREF(Py_NotImplemented);
2452 return Py_NotImplemented;
2453 }
Guido van Rossumaac530c2007-08-24 22:33:45 +00002454
2455 len_self = PyObject_Size(self);
2456 if (len_self < 0)
2457 return NULL;
2458 len_other = PyObject_Size(other);
2459 if (len_other < 0)
2460 return NULL;
2461
2462 ok = 0;
2463 switch(op) {
2464
2465 case Py_NE:
2466 case Py_EQ:
2467 if (len_self == len_other)
2468 ok = all_contained_in(self, other);
2469 if (op == Py_NE && ok >= 0)
2470 ok = !ok;
2471 break;
2472
2473 case Py_LT:
2474 if (len_self < len_other)
2475 ok = all_contained_in(self, other);
2476 break;
2477
2478 case Py_LE:
2479 if (len_self <= len_other)
2480 ok = all_contained_in(self, other);
2481 break;
2482
2483 case Py_GT:
2484 if (len_self > len_other)
2485 ok = all_contained_in(other, self);
2486 break;
2487
2488 case Py_GE:
2489 if (len_self >= len_other)
2490 ok = all_contained_in(other, self);
2491 break;
2492
2493 }
2494 if (ok < 0)
2495 return NULL;
2496 result = ok ? Py_True : Py_False;
2497 Py_INCREF(result);
2498 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00002499}
2500
Guido van Rossum3ac67412007-02-10 18:55:06 +00002501/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002502
2503static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002504dictkeys_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002505{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002506 if (dv->dv_dict == NULL) {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002507 Py_RETURN_NONE;
2508 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002509 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
2510}
2511
2512static int
2513dictkeys_contains(dictviewobject *dv, PyObject *obj)
2514{
2515 if (dv->dv_dict == NULL)
2516 return 0;
2517 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002518}
2519
Guido van Rossum83825ac2007-02-10 04:54:19 +00002520static PySequenceMethods dictkeys_as_sequence = {
2521 (lenfunc)dictview_len, /* sq_length */
2522 0, /* sq_concat */
2523 0, /* sq_repeat */
2524 0, /* sq_item */
2525 0, /* sq_slice */
2526 0, /* sq_ass_item */
2527 0, /* sq_ass_slice */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002528 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002529};
2530
Guido van Rossum523259b2007-08-24 23:41:22 +00002531static PyObject*
2532dictviews_sub(PyObject* self, PyObject *other)
2533{
2534 PyObject *result = PySet_New(self);
2535 PyObject *tmp;
2536 if (result == NULL)
2537 return NULL;
2538
2539 tmp = PyObject_CallMethod(result, "difference_update", "O", other);
2540 if (tmp == NULL) {
2541 Py_DECREF(result);
2542 return NULL;
2543 }
2544
2545 Py_DECREF(tmp);
2546 return result;
2547}
2548
2549static PyObject*
2550dictviews_and(PyObject* self, PyObject *other)
2551{
2552 PyObject *result = PySet_New(self);
2553 PyObject *tmp;
2554 if (result == NULL)
2555 return NULL;
2556
2557 tmp = PyObject_CallMethod(result, "intersection_update", "O", other);
2558 if (tmp == NULL) {
2559 Py_DECREF(result);
2560 return NULL;
2561 }
2562
2563 Py_DECREF(tmp);
2564 return result;
2565}
2566
2567static PyObject*
2568dictviews_or(PyObject* self, PyObject *other)
2569{
2570 PyObject *result = PySet_New(self);
2571 PyObject *tmp;
2572 if (result == NULL)
2573 return NULL;
2574
2575 tmp = PyObject_CallMethod(result, "update", "O", other);
2576 if (tmp == NULL) {
2577 Py_DECREF(result);
2578 return NULL;
2579 }
2580
2581 Py_DECREF(tmp);
2582 return result;
2583}
2584
2585static PyObject*
2586dictviews_xor(PyObject* self, PyObject *other)
2587{
2588 PyObject *result = PySet_New(self);
2589 PyObject *tmp;
2590 if (result == NULL)
2591 return NULL;
2592
2593 tmp = PyObject_CallMethod(result, "symmetric_difference_update", "O",
2594 other);
2595 if (tmp == NULL) {
2596 Py_DECREF(result);
2597 return NULL;
2598 }
2599
2600 Py_DECREF(tmp);
2601 return result;
2602}
2603
2604static PyNumberMethods dictviews_as_number = {
2605 0, /*nb_add*/
2606 (binaryfunc)dictviews_sub, /*nb_subtract*/
2607 0, /*nb_multiply*/
2608 0, /*nb_remainder*/
2609 0, /*nb_divmod*/
2610 0, /*nb_power*/
2611 0, /*nb_negative*/
2612 0, /*nb_positive*/
2613 0, /*nb_absolute*/
2614 0, /*nb_bool*/
2615 0, /*nb_invert*/
2616 0, /*nb_lshift*/
2617 0, /*nb_rshift*/
2618 (binaryfunc)dictviews_and, /*nb_and*/
2619 (binaryfunc)dictviews_xor, /*nb_xor*/
2620 (binaryfunc)dictviews_or, /*nb_or*/
2621};
2622
Guido van Rossumb90c8482007-02-10 01:11:45 +00002623static PyMethodDef dictkeys_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002624 {NULL, NULL} /* sentinel */
2625};
2626
2627PyTypeObject PyDictKeys_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002628 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002629 "dict_keys", /* tp_name */
2630 sizeof(dictviewobject), /* tp_basicsize */
2631 0, /* tp_itemsize */
2632 /* methods */
2633 (destructor)dictview_dealloc, /* tp_dealloc */
2634 0, /* tp_print */
2635 0, /* tp_getattr */
2636 0, /* tp_setattr */
2637 0, /* tp_compare */
2638 0, /* tp_repr */
Guido van Rossum523259b2007-08-24 23:41:22 +00002639 &dictviews_as_number, /* tp_as_number */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002640 &dictkeys_as_sequence, /* tp_as_sequence */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002641 0, /* tp_as_mapping */
2642 0, /* tp_hash */
2643 0, /* tp_call */
2644 0, /* tp_str */
2645 PyObject_GenericGetAttr, /* tp_getattro */
2646 0, /* tp_setattro */
2647 0, /* tp_as_buffer */
2648 Py_TPFLAGS_DEFAULT, /* tp_flags */
2649 0, /* tp_doc */
2650 0, /* tp_traverse */
2651 0, /* tp_clear */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002652 dictview_richcompare, /* tp_richcompare */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002653 0, /* tp_weaklistoffset */
2654 (getiterfunc)dictkeys_iter, /* tp_iter */
2655 0, /* tp_iternext */
2656 dictkeys_methods, /* tp_methods */
2657 0,
2658};
2659
2660static PyObject *
2661dictkeys_new(PyObject *dict)
2662{
2663 return dictview_new(dict, &PyDictKeys_Type);
2664}
2665
Guido van Rossum3ac67412007-02-10 18:55:06 +00002666/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002667
2668static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002669dictitems_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002670{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002671 if (dv->dv_dict == NULL) {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002672 Py_RETURN_NONE;
2673 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002674 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
2675}
2676
2677static int
2678dictitems_contains(dictviewobject *dv, PyObject *obj)
2679{
2680 PyObject *key, *value, *found;
2681 if (dv->dv_dict == NULL)
2682 return 0;
2683 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
2684 return 0;
2685 key = PyTuple_GET_ITEM(obj, 0);
2686 value = PyTuple_GET_ITEM(obj, 1);
2687 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
2688 if (found == NULL) {
2689 if (PyErr_Occurred())
2690 return -1;
2691 return 0;
2692 }
2693 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002694}
2695
Guido van Rossum83825ac2007-02-10 04:54:19 +00002696static PySequenceMethods dictitems_as_sequence = {
2697 (lenfunc)dictview_len, /* sq_length */
2698 0, /* sq_concat */
2699 0, /* sq_repeat */
2700 0, /* sq_item */
2701 0, /* sq_slice */
2702 0, /* sq_ass_item */
2703 0, /* sq_ass_slice */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002704 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002705};
2706
Guido van Rossumb90c8482007-02-10 01:11:45 +00002707static PyMethodDef dictitems_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002708 {NULL, NULL} /* sentinel */
2709};
2710
2711PyTypeObject PyDictItems_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002712 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002713 "dict_items", /* tp_name */
2714 sizeof(dictviewobject), /* tp_basicsize */
2715 0, /* tp_itemsize */
2716 /* methods */
2717 (destructor)dictview_dealloc, /* tp_dealloc */
2718 0, /* tp_print */
2719 0, /* tp_getattr */
2720 0, /* tp_setattr */
2721 0, /* tp_compare */
2722 0, /* tp_repr */
Guido van Rossum523259b2007-08-24 23:41:22 +00002723 &dictviews_as_number, /* tp_as_number */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002724 &dictitems_as_sequence, /* tp_as_sequence */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002725 0, /* tp_as_mapping */
2726 0, /* tp_hash */
2727 0, /* tp_call */
2728 0, /* tp_str */
2729 PyObject_GenericGetAttr, /* tp_getattro */
2730 0, /* tp_setattro */
2731 0, /* tp_as_buffer */
2732 Py_TPFLAGS_DEFAULT, /* tp_flags */
2733 0, /* tp_doc */
2734 0, /* tp_traverse */
2735 0, /* tp_clear */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002736 dictview_richcompare, /* tp_richcompare */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002737 0, /* tp_weaklistoffset */
2738 (getiterfunc)dictitems_iter, /* tp_iter */
2739 0, /* tp_iternext */
2740 dictitems_methods, /* tp_methods */
2741 0,
2742};
2743
2744static PyObject *
2745dictitems_new(PyObject *dict)
2746{
2747 return dictview_new(dict, &PyDictItems_Type);
2748}
2749
Guido van Rossum3ac67412007-02-10 18:55:06 +00002750/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002751
2752static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002753dictvalues_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002754{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002755 if (dv->dv_dict == NULL) {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002756 Py_RETURN_NONE;
2757 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002758 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002759}
2760
Guido van Rossum83825ac2007-02-10 04:54:19 +00002761static PySequenceMethods dictvalues_as_sequence = {
2762 (lenfunc)dictview_len, /* sq_length */
2763 0, /* sq_concat */
2764 0, /* sq_repeat */
2765 0, /* sq_item */
2766 0, /* sq_slice */
2767 0, /* sq_ass_item */
2768 0, /* sq_ass_slice */
2769 (objobjproc)0, /* sq_contains */
2770};
2771
Guido van Rossumb90c8482007-02-10 01:11:45 +00002772static PyMethodDef dictvalues_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002773 {NULL, NULL} /* sentinel */
2774};
2775
2776PyTypeObject PyDictValues_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002777 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002778 "dict_values", /* tp_name */
2779 sizeof(dictviewobject), /* tp_basicsize */
2780 0, /* tp_itemsize */
2781 /* methods */
2782 (destructor)dictview_dealloc, /* tp_dealloc */
2783 0, /* tp_print */
2784 0, /* tp_getattr */
2785 0, /* tp_setattr */
2786 0, /* tp_compare */
2787 0, /* tp_repr */
2788 0, /* tp_as_number */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002789 &dictvalues_as_sequence, /* tp_as_sequence */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002790 0, /* tp_as_mapping */
2791 0, /* tp_hash */
2792 0, /* tp_call */
2793 0, /* tp_str */
2794 PyObject_GenericGetAttr, /* tp_getattro */
2795 0, /* tp_setattro */
2796 0, /* tp_as_buffer */
2797 Py_TPFLAGS_DEFAULT, /* tp_flags */
2798 0, /* tp_doc */
2799 0, /* tp_traverse */
2800 0, /* tp_clear */
2801 0, /* tp_richcompare */
2802 0, /* tp_weaklistoffset */
2803 (getiterfunc)dictvalues_iter, /* tp_iter */
2804 0, /* tp_iternext */
2805 dictvalues_methods, /* tp_methods */
2806 0,
2807};
2808
2809static PyObject *
2810dictvalues_new(PyObject *dict)
2811{
2812 return dictview_new(dict, &PyDictValues_Type);
2813}