blob: 423012581f6949c1f59c8a3d73518166b390f95b [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 Rossumd8faa362007-04-27 19:54:29 +00001178 if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) {
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001179 PyDictObject *mp = (PyDictObject *)d;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001180 Py_ssize_t pos = 0;
1181 PyObject *key;
1182 long hash;
1183
1184 if (dictresize(mp, PySet_GET_SIZE(seq)))
1185 return NULL;
1186
1187 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
1188 Py_INCREF(key);
1189 Py_INCREF(value);
1190 if (insertdict(mp, key, hash, value))
1191 return NULL;
1192 }
1193 return d;
1194 }
1195
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001196 it = PyObject_GetIter(seq);
1197 if (it == NULL){
1198 Py_DECREF(d);
1199 return NULL;
1200 }
1201
1202 for (;;) {
1203 key = PyIter_Next(it);
1204 if (key == NULL) {
1205 if (PyErr_Occurred())
1206 goto Fail;
1207 break;
1208 }
Raymond Hettingere03e5b12002-12-07 08:10:51 +00001209 status = PyObject_SetItem(d, key, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001210 Py_DECREF(key);
1211 if (status < 0)
1212 goto Fail;
1213 }
1214
1215 Py_DECREF(it);
1216 return d;
1217
1218Fail:
1219 Py_DECREF(it);
1220 Py_DECREF(d);
1221 return NULL;
1222}
1223
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001224static int
1225dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001226{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001227 PyObject *arg = NULL;
1228 int result = 0;
1229
1230 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1231 result = -1;
1232
1233 else if (arg != NULL) {
1234 if (PyObject_HasAttrString(arg, "keys"))
1235 result = PyDict_Merge(self, arg, 1);
1236 else
1237 result = PyDict_MergeFromSeq2(self, arg, 1);
1238 }
1239 if (result == 0 && kwds != NULL)
1240 result = PyDict_Merge(self, kwds, 1);
1241 return result;
1242}
1243
1244static PyObject *
1245dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1246{
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001247 if (dict_update_common(self, args, kwds, "update") != -1)
1248 Py_RETURN_NONE;
1249 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001250}
1251
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001252/* Update unconditionally replaces existing items.
1253 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001254 otherwise it leaves existing items unchanged.
1255
1256 PyDict_{Update,Merge} update/merge from a mapping object.
1257
Tim Petersf582b822001-12-11 18:51:08 +00001258 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001259 producing iterable objects of length 2.
1260*/
1261
Tim Petersf582b822001-12-11 18:51:08 +00001262int
Tim Peters1fc240e2001-10-26 05:06:50 +00001263PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1264{
1265 PyObject *it; /* iter(seq2) */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001266 Py_ssize_t i; /* index into seq2 of current element */
Tim Peters1fc240e2001-10-26 05:06:50 +00001267 PyObject *item; /* seq2[i] */
1268 PyObject *fast; /* item as a 2-tuple or 2-list */
1269
1270 assert(d != NULL);
1271 assert(PyDict_Check(d));
1272 assert(seq2 != NULL);
1273
1274 it = PyObject_GetIter(seq2);
1275 if (it == NULL)
1276 return -1;
1277
1278 for (i = 0; ; ++i) {
1279 PyObject *key, *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001280 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001281
1282 fast = NULL;
1283 item = PyIter_Next(it);
1284 if (item == NULL) {
1285 if (PyErr_Occurred())
1286 goto Fail;
1287 break;
1288 }
1289
1290 /* Convert item to sequence, and verify length 2. */
1291 fast = PySequence_Fast(item, "");
1292 if (fast == NULL) {
1293 if (PyErr_ExceptionMatches(PyExc_TypeError))
1294 PyErr_Format(PyExc_TypeError,
1295 "cannot convert dictionary update "
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001296 "sequence element #%zd to a sequence",
Tim Peters1fc240e2001-10-26 05:06:50 +00001297 i);
1298 goto Fail;
1299 }
1300 n = PySequence_Fast_GET_SIZE(fast);
1301 if (n != 2) {
1302 PyErr_Format(PyExc_ValueError,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001303 "dictionary update sequence element #%zd "
Martin v. Löwis2c95cc62006-02-16 06:54:25 +00001304 "has length %zd; 2 is required",
Martin v. Löwise0e89f72006-02-16 06:59:22 +00001305 i, n);
Tim Peters1fc240e2001-10-26 05:06:50 +00001306 goto Fail;
1307 }
1308
1309 /* Update/merge with this (key, value) pair. */
1310 key = PySequence_Fast_GET_ITEM(fast, 0);
1311 value = PySequence_Fast_GET_ITEM(fast, 1);
1312 if (override || PyDict_GetItem(d, key) == NULL) {
1313 int status = PyDict_SetItem(d, key, value);
1314 if (status < 0)
1315 goto Fail;
1316 }
1317 Py_DECREF(fast);
1318 Py_DECREF(item);
1319 }
1320
1321 i = 0;
1322 goto Return;
1323Fail:
1324 Py_XDECREF(item);
1325 Py_XDECREF(fast);
1326 i = -1;
1327Return:
1328 Py_DECREF(it);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001329 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001330}
1331
Tim Peters6d6c1a32001-08-02 04:15:00 +00001332int
1333PyDict_Update(PyObject *a, PyObject *b)
1334{
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001335 return PyDict_Merge(a, b, 1);
1336}
1337
1338int
1339PyDict_Merge(PyObject *a, PyObject *b, int override)
1340{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001341 register PyDictObject *mp, *other;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001342 register Py_ssize_t i;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001343 PyDictEntry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001344
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001345 /* We accept for the argument either a concrete dictionary object,
1346 * or an abstract "mapping" object. For the former, we can do
1347 * things quite efficiently. For the latter, we only require that
1348 * PyMapping_Keys() and PyObject_GetItem() be supported.
1349 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001350 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1351 PyErr_BadInternalCall();
1352 return -1;
1353 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001354 mp = (PyDictObject*)a;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001355 if (PyDict_CheckExact(b)) {
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001356 other = (PyDictObject*)b;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001357 if (other == mp || other->ma_used == 0)
1358 /* a.update(a) or a.update({}); nothing to do */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001359 return 0;
Raymond Hettinger186e7392005-05-14 18:08:25 +00001360 if (mp->ma_used == 0)
1361 /* Since the target dict is empty, PyDict_GetItem()
1362 * always returns NULL. Setting override to 1
1363 * skips the unnecessary test.
1364 */
1365 override = 1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001366 /* Do one big resize at the start, rather than
1367 * incrementally resizing as we insert new items. Expect
1368 * that there will be no (or few) overlapping keys.
1369 */
1370 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
Raymond Hettingerc8d22902003-05-07 00:49:40 +00001371 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001372 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001373 }
1374 for (i = 0; i <= other->ma_mask; i++) {
1375 entry = &other->ma_table[i];
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001376 if (entry->me_value != NULL &&
1377 (override ||
1378 PyDict_GetItem(a, entry->me_key) == NULL)) {
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001379 Py_INCREF(entry->me_key);
1380 Py_INCREF(entry->me_value);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001381 if (insertdict(mp, entry->me_key,
1382 (long)entry->me_hash,
1383 entry->me_value) != 0)
1384 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001385 }
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001386 }
1387 }
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001388 else {
1389 /* Do it the generic, slower way */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001390 PyObject *keys = PyMapping_Keys(b);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001391 PyObject *iter;
1392 PyObject *key, *value;
1393 int status;
1394
1395 if (keys == NULL)
1396 /* Docstring says this is equivalent to E.keys() so
1397 * if E doesn't have a .keys() method we want
1398 * AttributeError to percolate up. Might as well
1399 * do the same for any other error.
1400 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001401 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001402
1403 iter = PyObject_GetIter(keys);
1404 Py_DECREF(keys);
1405 if (iter == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001406 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001407
1408 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001409 if (!override && PyDict_GetItem(a, key) != NULL) {
1410 Py_DECREF(key);
1411 continue;
1412 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001413 value = PyObject_GetItem(b, key);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001414 if (value == NULL) {
1415 Py_DECREF(iter);
1416 Py_DECREF(key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001417 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001418 }
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001419 status = PyDict_SetItem(a, key, value);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001420 Py_DECREF(key);
1421 Py_DECREF(value);
1422 if (status < 0) {
1423 Py_DECREF(iter);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001424 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001425 }
1426 }
1427 Py_DECREF(iter);
1428 if (PyErr_Occurred())
1429 /* Iterator completed, via error */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001430 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001431 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001432 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001433}
1434
1435static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001436dict_copy(register PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001437{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001438 return PyDict_Copy((PyObject*)mp);
1439}
1440
1441PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001442PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001443{
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001444 PyObject *copy;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001445
1446 if (o == NULL || !PyDict_Check(o)) {
1447 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001448 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001449 }
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001450 copy = PyDict_New();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001451 if (copy == NULL)
1452 return NULL;
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001453 if (PyDict_Merge(copy, o, 1) == 0)
1454 return copy;
1455 Py_DECREF(copy);
Raymond Hettinger1356f782005-04-15 15:58:42 +00001456 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001457}
1458
Martin v. Löwis18e16552006-02-15 17:27:45 +00001459Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00001460PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001461{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001462 if (mp == NULL || !PyDict_Check(mp)) {
1463 PyErr_BadInternalCall();
Jeremy Hylton7083bb72004-02-17 20:10:11 +00001464 return -1;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001465 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001466 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001467}
1468
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001469PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001470PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001471{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001472 if (mp == NULL || !PyDict_Check(mp)) {
1473 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001474 return NULL;
1475 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001476 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001477}
1478
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001479PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001480PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001481{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001482 if (mp == NULL || !PyDict_Check(mp)) {
1483 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001484 return NULL;
1485 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001486 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001487}
1488
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001489PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001490PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001491{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001492 if (mp == NULL || !PyDict_Check(mp)) {
1493 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001494 return NULL;
1495 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001496 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001497}
1498
Tim Peterse63415e2001-05-08 04:38:29 +00001499/* Return 1 if dicts equal, 0 if not, -1 if error.
1500 * Gets out as soon as any difference is detected.
1501 * Uses only Py_EQ comparison.
1502 */
1503static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001504dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00001505{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001506 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00001507
1508 if (a->ma_used != b->ma_used)
1509 /* can't be equal if # of entries differ */
1510 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001511
Tim Peterse63415e2001-05-08 04:38:29 +00001512 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Tim Petersafb6ae82001-06-04 21:00:21 +00001513 for (i = 0; i <= a->ma_mask; i++) {
Tim Peterse63415e2001-05-08 04:38:29 +00001514 PyObject *aval = a->ma_table[i].me_value;
1515 if (aval != NULL) {
1516 int cmp;
1517 PyObject *bval;
1518 PyObject *key = a->ma_table[i].me_key;
1519 /* temporarily bump aval's refcount to ensure it stays
1520 alive until we're done with it */
1521 Py_INCREF(aval);
Guido van Rossumdc5f6b22006-08-24 21:29:26 +00001522 /* ditto for key */
1523 Py_INCREF(key);
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001524 bval = PyDict_GetItemWithError((PyObject *)b, key);
Guido van Rossumdc5f6b22006-08-24 21:29:26 +00001525 Py_DECREF(key);
Tim Peterse63415e2001-05-08 04:38:29 +00001526 if (bval == NULL) {
1527 Py_DECREF(aval);
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001528 if (PyErr_Occurred())
1529 return -1;
Tim Peterse63415e2001-05-08 04:38:29 +00001530 return 0;
1531 }
1532 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1533 Py_DECREF(aval);
1534 if (cmp <= 0) /* error or not equal */
1535 return cmp;
1536 }
1537 }
1538 return 1;
1539 }
1540
1541static PyObject *
1542dict_richcompare(PyObject *v, PyObject *w, int op)
1543{
1544 int cmp;
1545 PyObject *res;
1546
1547 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1548 res = Py_NotImplemented;
1549 }
1550 else if (op == Py_EQ || op == Py_NE) {
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001551 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
Tim Peterse63415e2001-05-08 04:38:29 +00001552 if (cmp < 0)
1553 return NULL;
1554 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1555 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001556 else
1557 res = Py_NotImplemented;
Tim Peterse63415e2001-05-08 04:38:29 +00001558 Py_INCREF(res);
1559 return res;
1560 }
1561
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001562static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001563dict_contains(register PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001564{
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001565 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001566 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001567
Guido van Rossum89d8c602007-09-18 17:26:56 +00001568 if (!PyUnicode_CheckExact(key) ||
1569 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001570 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001571 if (hash == -1)
1572 return NULL;
1573 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001574 ep = (mp->ma_lookup)(mp, key, hash);
1575 if (ep == NULL)
1576 return NULL;
1577 return PyBool_FromLong(ep->me_value != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001578}
1579
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001580static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001581dict_get(register PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001582{
1583 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001584 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001585 PyObject *val = NULL;
1586 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001587 PyDictEntry *ep;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001588
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001589 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001590 return NULL;
1591
Guido van Rossum89d8c602007-09-18 17:26:56 +00001592 if (!PyUnicode_CheckExact(key) ||
1593 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Barry Warsawc38c5da1997-10-06 17:49:20 +00001594 hash = PyObject_Hash(key);
1595 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 val = ep->me_value;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001602 if (val == NULL)
1603 val = failobj;
1604 Py_INCREF(val);
1605 return val;
1606}
1607
1608
1609static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001610dict_setdefault(register PyDictObject *mp, PyObject *args)
Guido van Rossum164452c2000-08-08 16:12:54 +00001611{
1612 PyObject *key;
1613 PyObject *failobj = Py_None;
1614 PyObject *val = NULL;
1615 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001616 PyDictEntry *ep;
Guido van Rossum164452c2000-08-08 16:12:54 +00001617
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001618 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
Guido van Rossum164452c2000-08-08 16:12:54 +00001619 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001620
Guido van Rossum89d8c602007-09-18 17:26:56 +00001621 if (!PyUnicode_CheckExact(key) ||
1622 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossum164452c2000-08-08 16:12:54 +00001623 hash = PyObject_Hash(key);
1624 if (hash == -1)
1625 return NULL;
1626 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001627 ep = (mp->ma_lookup)(mp, key, hash);
1628 if (ep == NULL)
1629 return NULL;
1630 val = ep->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001631 if (val == NULL) {
1632 val = failobj;
1633 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1634 val = NULL;
1635 }
1636 Py_XINCREF(val);
1637 return val;
1638}
1639
1640
1641static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001642dict_clear(register PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001643{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001644 PyDict_Clear((PyObject *)mp);
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001645 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001646}
1647
Guido van Rossumba6ab842000-12-12 22:02:18 +00001648static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001649dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00001650{
1651 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001652 PyDictEntry *ep;
Guido van Rossume027d982002-04-12 15:11:59 +00001653 PyObject *old_value, *old_key;
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001654 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001655
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001656 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1657 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001658 if (mp->ma_used == 0) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001659 if (deflt) {
1660 Py_INCREF(deflt);
1661 return deflt;
1662 }
Guido van Rossume027d982002-04-12 15:11:59 +00001663 PyErr_SetString(PyExc_KeyError,
1664 "pop(): dictionary is empty");
1665 return NULL;
1666 }
Guido van Rossum89d8c602007-09-18 17:26:56 +00001667 if (!PyUnicode_CheckExact(key) ||
1668 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossume027d982002-04-12 15:11:59 +00001669 hash = PyObject_Hash(key);
1670 if (hash == -1)
1671 return NULL;
1672 }
1673 ep = (mp->ma_lookup)(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001674 if (ep == NULL)
1675 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001676 if (ep->me_value == NULL) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001677 if (deflt) {
1678 Py_INCREF(deflt);
1679 return deflt;
1680 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001681 set_key_error(key);
Guido van Rossume027d982002-04-12 15:11:59 +00001682 return NULL;
1683 }
1684 old_key = ep->me_key;
1685 Py_INCREF(dummy);
1686 ep->me_key = dummy;
1687 old_value = ep->me_value;
1688 ep->me_value = NULL;
1689 mp->ma_used--;
1690 Py_DECREF(old_key);
1691 return old_value;
1692}
1693
1694static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001695dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001696{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001697 Py_ssize_t i = 0;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001698 PyDictEntry *ep;
Guido van Rossumba6ab842000-12-12 22:02:18 +00001699 PyObject *res;
1700
Tim Petersf4b33f62001-06-02 05:42:29 +00001701 /* Allocate the result tuple before checking the size. Believe it
1702 * or not, this allocation could trigger a garbage collection which
1703 * could empty the dict, so if we checked the size first and that
1704 * happened, the result would be an infinite loop (searching for an
1705 * entry that no longer exists). Note that the usual popitem()
1706 * idiom is "while d: k, v = d.popitem()". so needing to throw the
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001707 * tuple away if the dict *is* empty isn't a significant
Tim Petersf4b33f62001-06-02 05:42:29 +00001708 * inefficiency -- possible, but unlikely in practice.
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001709 */
1710 res = PyTuple_New(2);
1711 if (res == NULL)
1712 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001713 if (mp->ma_used == 0) {
1714 Py_DECREF(res);
1715 PyErr_SetString(PyExc_KeyError,
1716 "popitem(): dictionary is empty");
1717 return NULL;
1718 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001719 /* Set ep to "the first" dict entry with a value. We abuse the hash
1720 * field of slot 0 to hold a search finger:
1721 * If slot 0 has a value, use slot 0.
1722 * Else slot 0 is being used to hold a search finger,
1723 * and we use its hash value as the first index to look.
1724 */
1725 ep = &mp->ma_table[0];
1726 if (ep->me_value == NULL) {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001727 i = ep->me_hash;
Tim Petersdea48ec2001-05-22 20:40:22 +00001728 /* The hash field may be a real hash value, or it may be a
1729 * legit search finger, or it may be a once-legit search
1730 * finger that's out of bounds now because it wrapped around
1731 * or the table shrunk -- simply make sure it's in bounds now.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001732 */
Tim Petersafb6ae82001-06-04 21:00:21 +00001733 if (i > mp->ma_mask || i < 1)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001734 i = 1; /* skip slot 0 */
1735 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1736 i++;
Tim Petersafb6ae82001-06-04 21:00:21 +00001737 if (i > mp->ma_mask)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001738 i = 1;
1739 }
1740 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001741 PyTuple_SET_ITEM(res, 0, ep->me_key);
1742 PyTuple_SET_ITEM(res, 1, ep->me_value);
1743 Py_INCREF(dummy);
1744 ep->me_key = dummy;
1745 ep->me_value = NULL;
1746 mp->ma_used--;
1747 assert(mp->ma_table[0].me_value == NULL);
1748 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001749 return res;
1750}
1751
Jeremy Hylton8caad492000-06-23 14:18:11 +00001752static int
1753dict_traverse(PyObject *op, visitproc visit, void *arg)
1754{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001755 Py_ssize_t i = 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001756 PyObject *pk;
1757 PyObject *pv;
1758
1759 while (PyDict_Next(op, &i, &pk, &pv)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001760 Py_VISIT(pk);
1761 Py_VISIT(pv);
Jeremy Hylton8caad492000-06-23 14:18:11 +00001762 }
1763 return 0;
1764}
1765
1766static int
1767dict_tp_clear(PyObject *op)
1768{
1769 PyDict_Clear(op);
1770 return 0;
1771}
1772
Tim Petersf7f88b12000-12-13 23:18:45 +00001773
Raymond Hettinger019a1482004-03-18 02:41:19 +00001774extern PyTypeObject PyDictIterKey_Type; /* Forward */
1775extern PyTypeObject PyDictIterValue_Type; /* Forward */
1776extern PyTypeObject PyDictIterItem_Type; /* Forward */
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001777static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001778
Guido van Rossum09e563a2001-05-01 12:10:21 +00001779
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001780PyDoc_STRVAR(contains__doc__,
1781"D.__contains__(k) -> True if D has a key k, else False");
1782
1783PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
1784
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001785PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001786"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001787
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001788PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001789"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 +00001790
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001791PyDoc_STRVAR(pop__doc__,
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001792"D.pop(k[,d]) -> v, remove specified key and return the corresponding value\n\
1793If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00001794
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001795PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00001796"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000017972-tuple; but raise KeyError if D is empty");
Tim Petersf7f88b12000-12-13 23:18:45 +00001798
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001799PyDoc_STRVAR(update__doc__,
Guido van Rossumb90c8482007-02-10 01:11:45 +00001800"D.update(E, **F) -> None. Update D from E and F: for k in E: D[k] = E[k]\
1801\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 +00001802
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001803PyDoc_STRVAR(fromkeys__doc__,
1804"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
1805v defaults to None.");
1806
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001807PyDoc_STRVAR(clear__doc__,
1808"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001809
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001810PyDoc_STRVAR(copy__doc__,
1811"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00001812
Guido van Rossumb90c8482007-02-10 01:11:45 +00001813/* Forward */
1814static PyObject *dictkeys_new(PyObject *);
1815static PyObject *dictitems_new(PyObject *);
1816static PyObject *dictvalues_new(PyObject *);
1817
Guido van Rossum45c85d12007-07-27 16:31:40 +00001818PyDoc_STRVAR(keys__doc__,
1819 "D.keys() -> a set-like object providing a view on D's keys");
1820PyDoc_STRVAR(items__doc__,
1821 "D.items() -> a set-like object providing a view on D's items");
1822PyDoc_STRVAR(values__doc__,
1823 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00001824
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001825static PyMethodDef mapp_methods[] = {
Guido van Rossume2b70bc2006-08-18 22:13:04 +00001826 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001827 contains__doc__},
Raymond Hettinger0c669672003-12-13 13:31:55 +00001828 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001829 getitem__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00001830 {"get", (PyCFunction)dict_get, METH_VARARGS,
1831 get__doc__},
1832 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
1833 setdefault_doc__},
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001834 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
Just van Rossuma797d812002-11-23 09:45:04 +00001835 pop__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001836 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001837 popitem__doc__},
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001838 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
Guido van Rossumeff072c2007-02-27 05:47:18 +00001839 keys__doc__},
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001840 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
Guido van Rossumeff072c2007-02-27 05:47:18 +00001841 items__doc__},
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001842 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
Guido van Rossumeff072c2007-02-27 05:47:18 +00001843 values__doc__},
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001844 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001845 update__doc__},
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001846 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
1847 fromkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001848 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001849 clear__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001850 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001851 copy__doc__},
1852 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001853};
1854
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001855/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001856int
1857PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001858{
1859 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001860 PyDictObject *mp = (PyDictObject *)op;
1861 PyDictEntry *ep;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001862
Guido van Rossum89d8c602007-09-18 17:26:56 +00001863 if (!PyUnicode_CheckExact(key) ||
1864 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001865 hash = PyObject_Hash(key);
1866 if (hash == -1)
1867 return -1;
1868 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001869 ep = (mp->ma_lookup)(mp, key, hash);
1870 return ep == NULL ? -1 : (ep->me_value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001871}
1872
Thomas Wouterscf297e42007-02-23 15:07:44 +00001873/* Internal version of PyDict_Contains used when the hash value is already known */
1874int
1875_PyDict_Contains(PyObject *op, PyObject *key, long hash)
1876{
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001877 PyDictObject *mp = (PyDictObject *)op;
1878 PyDictEntry *ep;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001879
1880 ep = (mp->ma_lookup)(mp, key, hash);
1881 return ep == NULL ? -1 : (ep->me_value != NULL);
1882}
1883
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001884/* Hack to implement "key in dict" */
1885static PySequenceMethods dict_as_sequence = {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001886 0, /* sq_length */
1887 0, /* sq_concat */
1888 0, /* sq_repeat */
1889 0, /* sq_item */
1890 0, /* sq_slice */
1891 0, /* sq_ass_item */
1892 0, /* sq_ass_slice */
1893 PyDict_Contains, /* sq_contains */
1894 0, /* sq_inplace_concat */
1895 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001896};
1897
Guido van Rossum09e563a2001-05-01 12:10:21 +00001898static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001899dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1900{
1901 PyObject *self;
1902
1903 assert(type != NULL && type->tp_alloc != NULL);
1904 self = type->tp_alloc(type, 0);
1905 if (self != NULL) {
1906 PyDictObject *d = (PyDictObject *)self;
1907 /* It's guaranteed that tp->alloc zeroed out the struct. */
1908 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
1909 INIT_NONZERO_DICT_SLOTS(d);
Guido van Rossum89d8c602007-09-18 17:26:56 +00001910 d->ma_lookup = lookdict_unicode;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001911#ifdef SHOW_CONVERSION_COUNTS
1912 ++created;
1913#endif
1914 }
1915 return self;
1916}
1917
Tim Peters25786c02001-09-02 08:22:48 +00001918static int
1919dict_init(PyObject *self, PyObject *args, PyObject *kwds)
1920{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001921 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00001922}
1923
Tim Peters6d6c1a32001-08-02 04:15:00 +00001924static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001925dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001926{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001927 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001928}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001929
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001930PyDoc_STRVAR(dictionary_doc,
Tim Petersa427a2b2001-10-29 22:25:45 +00001931"dict() -> new empty dictionary.\n"
1932"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Tim Peters1fc240e2001-10-26 05:06:50 +00001933" (key, value) pairs.\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00001934"dict(seq) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00001935" d = {}\n"
1936" for k, v in seq:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00001937" d[k] = v\n"
1938"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
1939" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00001940
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001941PyTypeObject PyDict_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001942 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Tim Petersa427a2b2001-10-29 22:25:45 +00001943 "dict",
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001944 sizeof(PyDictObject),
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001945 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00001946 (destructor)dict_dealloc, /* tp_dealloc */
Guido van Rossum04dbf3b2007-08-07 19:51:00 +00001947 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001948 0, /* tp_getattr */
Guido van Rossumb9324202001-01-18 00:39:02 +00001949 0, /* tp_setattr */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001950 0, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00001951 (reprfunc)dict_repr, /* tp_repr */
1952 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001953 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00001954 &dict_as_mapping, /* tp_as_mapping */
Guido van Rossum50e9fb92006-08-17 05:42:55 +00001955 0, /* tp_hash */
Guido van Rossumb9324202001-01-18 00:39:02 +00001956 0, /* tp_call */
1957 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001958 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumb9324202001-01-18 00:39:02 +00001959 0, /* tp_setattro */
1960 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00001961 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Thomas Wouters27d517b2007-02-25 20:39:11 +00001962 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
Tim Peters25786c02001-09-02 08:22:48 +00001963 dictionary_doc, /* tp_doc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001964 dict_traverse, /* tp_traverse */
1965 dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00001966 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001967 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00001968 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001969 0, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001970 mapp_methods, /* tp_methods */
1971 0, /* tp_members */
1972 0, /* tp_getset */
1973 0, /* tp_base */
1974 0, /* tp_dict */
1975 0, /* tp_descr_get */
1976 0, /* tp_descr_set */
1977 0, /* tp_dictoffset */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001978 dict_init, /* tp_init */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001979 PyType_GenericAlloc, /* tp_alloc */
1980 dict_new, /* tp_new */
Neil Schemenauer6189b892002-04-12 02:43:00 +00001981 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001982};
1983
Guido van Rossum3cca2451997-05-16 14:23:33 +00001984/* For backward compatibility with old dictionary interface */
1985
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001986PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00001987PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001988{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001989 PyObject *kv, *rv;
Martin v. Löwis5b222132007-06-10 09:51:05 +00001990 kv = PyUnicode_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00001991 if (kv == NULL)
1992 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00001993 rv = PyDict_GetItem(v, kv);
1994 Py_DECREF(kv);
1995 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001996}
1997
1998int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00001999PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002000{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002001 PyObject *kv;
2002 int err;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002003 kv = PyUnicode_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00002004 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002005 return -1;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002006 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00002007 err = PyDict_SetItem(v, kv, item);
2008 Py_DECREF(kv);
2009 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002010}
2011
2012int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002013PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002014{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002015 PyObject *kv;
2016 int err;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002017 kv = PyUnicode_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00002018 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002019 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002020 err = PyDict_DelItem(v, kv);
2021 Py_DECREF(kv);
2022 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002023}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002024
Raymond Hettinger019a1482004-03-18 02:41:19 +00002025/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002026
2027typedef struct {
2028 PyObject_HEAD
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002029 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002030 Py_ssize_t di_used;
2031 Py_ssize_t di_pos;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002032 PyObject* di_result; /* reusable result tuple for iteritems */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002033 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002034} dictiterobject;
2035
2036static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002037dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002038{
2039 dictiterobject *di;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002040 di = PyObject_New(dictiterobject, itertype);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002041 if (di == NULL)
2042 return NULL;
2043 Py_INCREF(dict);
2044 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00002045 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002046 di->di_pos = 0;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002047 di->len = dict->ma_used;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002048 if (itertype == &PyDictIterItem_Type) {
2049 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2050 if (di->di_result == NULL) {
2051 Py_DECREF(di);
2052 return NULL;
2053 }
2054 }
2055 else
2056 di->di_result = NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002057 return (PyObject *)di;
2058}
2059
2060static void
2061dictiter_dealloc(dictiterobject *di)
2062{
Guido van Rossum2147df72002-07-16 20:30:22 +00002063 Py_XDECREF(di->di_dict);
Raymond Hettinger019a1482004-03-18 02:41:19 +00002064 Py_XDECREF(di->di_result);
Neil Schemenauer6189b892002-04-12 02:43:00 +00002065 PyObject_Del(di);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002066}
2067
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002068static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002069dictiter_len(dictiterobject *di)
2070{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002071 Py_ssize_t len = 0;
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00002072 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002073 len = di->len;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002074 return PyInt_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002075}
2076
Guido van Rossumb90c8482007-02-10 01:11:45 +00002077PyDoc_STRVAR(length_hint_doc,
2078 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002079
2080static PyMethodDef dictiter_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002081 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
2082 length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002083 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002084};
2085
Raymond Hettinger019a1482004-03-18 02:41:19 +00002086static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002087{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002088 PyObject *key;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002089 register Py_ssize_t i, mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002090 register PyDictEntry *ep;
2091 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002092
Raymond Hettinger019a1482004-03-18 02:41:19 +00002093 if (d == NULL)
Guido van Rossum2147df72002-07-16 20:30:22 +00002094 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002095 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002096
Raymond Hettinger019a1482004-03-18 02:41:19 +00002097 if (di->di_used != d->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00002098 PyErr_SetString(PyExc_RuntimeError,
2099 "dictionary changed size during iteration");
Guido van Rossum2147df72002-07-16 20:30:22 +00002100 di->di_used = -1; /* Make this state sticky */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002101 return NULL;
2102 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002103
Raymond Hettinger019a1482004-03-18 02:41:19 +00002104 i = di->di_pos;
2105 if (i < 0)
2106 goto fail;
2107 ep = d->ma_table;
2108 mask = d->ma_mask;
2109 while (i <= mask && ep[i].me_value == NULL)
2110 i++;
2111 di->di_pos = i+1;
2112 if (i > mask)
2113 goto fail;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002114 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002115 key = ep[i].me_key;
2116 Py_INCREF(key);
2117 return key;
2118
2119fail:
2120 Py_DECREF(d);
Guido van Rossum2147df72002-07-16 20:30:22 +00002121 di->di_dict = NULL;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002122 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002123}
2124
Raymond Hettinger019a1482004-03-18 02:41:19 +00002125PyTypeObject PyDictIterKey_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002126 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002127 "dictionary-keyiterator", /* tp_name */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002128 sizeof(dictiterobject), /* tp_basicsize */
2129 0, /* tp_itemsize */
2130 /* methods */
2131 (destructor)dictiter_dealloc, /* tp_dealloc */
2132 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002133 0, /* tp_getattr */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002134 0, /* tp_setattr */
2135 0, /* tp_compare */
2136 0, /* tp_repr */
2137 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002138 0, /* tp_as_sequence */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002139 0, /* tp_as_mapping */
2140 0, /* tp_hash */
2141 0, /* tp_call */
2142 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002143 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002144 0, /* tp_setattro */
2145 0, /* tp_as_buffer */
2146 Py_TPFLAGS_DEFAULT, /* tp_flags */
2147 0, /* tp_doc */
2148 0, /* tp_traverse */
2149 0, /* tp_clear */
2150 0, /* tp_richcompare */
2151 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002152 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002153 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002154 dictiter_methods, /* tp_methods */
2155 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002156};
2157
2158static PyObject *dictiter_iternextvalue(dictiterobject *di)
2159{
2160 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002161 register Py_ssize_t i, mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002162 register PyDictEntry *ep;
2163 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002164
2165 if (d == NULL)
2166 return NULL;
2167 assert (PyDict_Check(d));
2168
2169 if (di->di_used != d->ma_used) {
2170 PyErr_SetString(PyExc_RuntimeError,
2171 "dictionary changed size during iteration");
2172 di->di_used = -1; /* Make this state sticky */
2173 return NULL;
2174 }
2175
2176 i = di->di_pos;
Guido van Rossum09240f62004-03-20 19:11:58 +00002177 mask = d->ma_mask;
2178 if (i < 0 || i > mask)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002179 goto fail;
2180 ep = d->ma_table;
Guido van Rossum09240f62004-03-20 19:11:58 +00002181 while ((value=ep[i].me_value) == NULL) {
Raymond Hettinger019a1482004-03-18 02:41:19 +00002182 i++;
Guido van Rossum09240f62004-03-20 19:11:58 +00002183 if (i > mask)
2184 goto fail;
2185 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002186 di->di_pos = i+1;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002187 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002188 Py_INCREF(value);
2189 return value;
2190
2191fail:
2192 Py_DECREF(d);
2193 di->di_dict = NULL;
2194 return NULL;
2195}
2196
2197PyTypeObject PyDictIterValue_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002198 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002199 "dictionary-valueiterator", /* tp_name */
2200 sizeof(dictiterobject), /* tp_basicsize */
2201 0, /* tp_itemsize */
2202 /* methods */
2203 (destructor)dictiter_dealloc, /* tp_dealloc */
2204 0, /* tp_print */
2205 0, /* tp_getattr */
2206 0, /* tp_setattr */
2207 0, /* tp_compare */
2208 0, /* tp_repr */
2209 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002210 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002211 0, /* tp_as_mapping */
2212 0, /* tp_hash */
2213 0, /* tp_call */
2214 0, /* tp_str */
2215 PyObject_GenericGetAttr, /* tp_getattro */
2216 0, /* tp_setattro */
2217 0, /* tp_as_buffer */
2218 Py_TPFLAGS_DEFAULT, /* tp_flags */
2219 0, /* tp_doc */
2220 0, /* tp_traverse */
2221 0, /* tp_clear */
2222 0, /* tp_richcompare */
2223 0, /* tp_weaklistoffset */
2224 PyObject_SelfIter, /* tp_iter */
2225 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002226 dictiter_methods, /* tp_methods */
2227 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002228};
2229
2230static PyObject *dictiter_iternextitem(dictiterobject *di)
2231{
2232 PyObject *key, *value, *result = di->di_result;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002233 register Py_ssize_t i, mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002234 register PyDictEntry *ep;
2235 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002236
2237 if (d == NULL)
2238 return NULL;
2239 assert (PyDict_Check(d));
2240
2241 if (di->di_used != d->ma_used) {
2242 PyErr_SetString(PyExc_RuntimeError,
2243 "dictionary changed size during iteration");
2244 di->di_used = -1; /* Make this state sticky */
2245 return NULL;
2246 }
2247
2248 i = di->di_pos;
2249 if (i < 0)
2250 goto fail;
2251 ep = d->ma_table;
2252 mask = d->ma_mask;
2253 while (i <= mask && ep[i].me_value == NULL)
2254 i++;
2255 di->di_pos = i+1;
2256 if (i > mask)
2257 goto fail;
2258
2259 if (result->ob_refcnt == 1) {
2260 Py_INCREF(result);
2261 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2262 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2263 } else {
2264 result = PyTuple_New(2);
Tim Peters60b29962006-01-01 01:19:23 +00002265 if (result == NULL)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002266 return NULL;
2267 }
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002268 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002269 key = ep[i].me_key;
2270 value = ep[i].me_value;
2271 Py_INCREF(key);
2272 Py_INCREF(value);
2273 PyTuple_SET_ITEM(result, 0, key);
2274 PyTuple_SET_ITEM(result, 1, value);
2275 return result;
2276
2277fail:
2278 Py_DECREF(d);
2279 di->di_dict = NULL;
2280 return NULL;
2281}
2282
2283PyTypeObject PyDictIterItem_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002284 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002285 "dictionary-itemiterator", /* tp_name */
2286 sizeof(dictiterobject), /* tp_basicsize */
2287 0, /* tp_itemsize */
2288 /* methods */
2289 (destructor)dictiter_dealloc, /* tp_dealloc */
2290 0, /* tp_print */
2291 0, /* tp_getattr */
2292 0, /* tp_setattr */
2293 0, /* tp_compare */
2294 0, /* tp_repr */
2295 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002296 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002297 0, /* tp_as_mapping */
2298 0, /* tp_hash */
2299 0, /* tp_call */
2300 0, /* tp_str */
2301 PyObject_GenericGetAttr, /* tp_getattro */
2302 0, /* tp_setattro */
2303 0, /* tp_as_buffer */
2304 Py_TPFLAGS_DEFAULT, /* tp_flags */
2305 0, /* tp_doc */
2306 0, /* tp_traverse */
2307 0, /* tp_clear */
2308 0, /* tp_richcompare */
2309 0, /* tp_weaklistoffset */
2310 PyObject_SelfIter, /* tp_iter */
2311 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002312 dictiter_methods, /* tp_methods */
2313 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002314};
Guido van Rossumb90c8482007-02-10 01:11:45 +00002315
2316
Guido van Rossum3ac67412007-02-10 18:55:06 +00002317/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002318/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002319/***********************************************/
2320
Guido van Rossumb90c8482007-02-10 01:11:45 +00002321/* The instance lay-out is the same for all three; but the type differs. */
2322
2323typedef struct {
2324 PyObject_HEAD
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002325 PyDictObject *dv_dict;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002326} dictviewobject;
2327
2328
2329static void
Guido van Rossum3ac67412007-02-10 18:55:06 +00002330dictview_dealloc(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002331{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002332 Py_XDECREF(dv->dv_dict);
2333 PyObject_Del(dv);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002334}
2335
Guido van Rossum83825ac2007-02-10 04:54:19 +00002336static Py_ssize_t
Guido van Rossum3ac67412007-02-10 18:55:06 +00002337dictview_len(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002338{
2339 Py_ssize_t len = 0;
Guido van Rossum3ac67412007-02-10 18:55:06 +00002340 if (dv->dv_dict != NULL)
2341 len = dv->dv_dict->ma_used;
Guido van Rossum83825ac2007-02-10 04:54:19 +00002342 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002343}
2344
2345static PyObject *
2346dictview_new(PyObject *dict, PyTypeObject *type)
2347{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002348 dictviewobject *dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002349 if (dict == NULL) {
2350 PyErr_BadInternalCall();
2351 return NULL;
2352 }
2353 if (!PyDict_Check(dict)) {
2354 /* XXX Get rid of this restriction later */
2355 PyErr_Format(PyExc_TypeError,
2356 "%s() requires a dict argument, not '%s'",
2357 type->tp_name, dict->ob_type->tp_name);
2358 return NULL;
2359 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002360 dv = PyObject_New(dictviewobject, type);
2361 if (dv == NULL)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002362 return NULL;
2363 Py_INCREF(dict);
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002364 dv->dv_dict = (PyDictObject *)dict;
Guido van Rossum3ac67412007-02-10 18:55:06 +00002365 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002366}
2367
Neal Norwitze36f2ba2007-02-26 23:12:28 +00002368/* TODO(guido): The views objects are not complete:
2369
2370 * support more set operations
2371 * support arbitrary mappings?
2372 - either these should be static or exported in dictobject.h
2373 - if public then they should probably be in builtins
2374*/
2375
Guido van Rossumd9214d12007-02-12 02:23:40 +00002376/* Forward */
2377PyTypeObject PyDictKeys_Type;
2378PyTypeObject PyDictItems_Type;
2379PyTypeObject PyDictValues_Type;
2380
2381#define PyDictKeys_Check(obj) ((obj)->ob_type == &PyDictKeys_Type)
2382#define PyDictItems_Check(obj) ((obj)->ob_type == &PyDictItems_Type)
2383#define PyDictValues_Check(obj) ((obj)->ob_type == &PyDictValues_Type)
2384
2385/* This excludes Values, since they are not sets. */
2386# define PyDictViewSet_Check(obj) \
2387 (PyDictKeys_Check(obj) || PyDictItems_Check(obj))
2388
Guido van Rossumaac530c2007-08-24 22:33:45 +00002389/* Return 1 if self is a subset of other, iterating over self;
2390 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002391static int
2392all_contained_in(PyObject *self, PyObject *other)
2393{
2394 PyObject *iter = PyObject_GetIter(self);
2395 int ok = 1;
2396
2397 if (iter == NULL)
2398 return -1;
2399 for (;;) {
2400 PyObject *next = PyIter_Next(iter);
2401 if (next == NULL) {
2402 if (PyErr_Occurred())
2403 ok = -1;
2404 break;
2405 }
2406 ok = PySequence_Contains(other, next);
2407 Py_DECREF(next);
2408 if (ok <= 0)
2409 break;
2410 }
2411 Py_DECREF(iter);
2412 return ok;
2413}
2414
2415static PyObject *
2416dictview_richcompare(PyObject *self, PyObject *other, int op)
2417{
Guido van Rossumaac530c2007-08-24 22:33:45 +00002418 Py_ssize_t len_self, len_other;
2419 int ok;
2420 PyObject *result;
2421
Guido van Rossumd9214d12007-02-12 02:23:40 +00002422 assert(self != NULL);
2423 assert(PyDictViewSet_Check(self));
2424 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00002425
Guido van Rossumaac530c2007-08-24 22:33:45 +00002426 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other)) {
Guido van Rossumd9214d12007-02-12 02:23:40 +00002427 Py_INCREF(Py_NotImplemented);
2428 return Py_NotImplemented;
2429 }
Guido van Rossumaac530c2007-08-24 22:33:45 +00002430
2431 len_self = PyObject_Size(self);
2432 if (len_self < 0)
2433 return NULL;
2434 len_other = PyObject_Size(other);
2435 if (len_other < 0)
2436 return NULL;
2437
2438 ok = 0;
2439 switch(op) {
2440
2441 case Py_NE:
2442 case Py_EQ:
2443 if (len_self == len_other)
2444 ok = all_contained_in(self, other);
2445 if (op == Py_NE && ok >= 0)
2446 ok = !ok;
2447 break;
2448
2449 case Py_LT:
2450 if (len_self < len_other)
2451 ok = all_contained_in(self, other);
2452 break;
2453
2454 case Py_LE:
2455 if (len_self <= len_other)
2456 ok = all_contained_in(self, other);
2457 break;
2458
2459 case Py_GT:
2460 if (len_self > len_other)
2461 ok = all_contained_in(other, self);
2462 break;
2463
2464 case Py_GE:
2465 if (len_self >= len_other)
2466 ok = all_contained_in(other, self);
2467 break;
2468
2469 }
2470 if (ok < 0)
2471 return NULL;
2472 result = ok ? Py_True : Py_False;
2473 Py_INCREF(result);
2474 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00002475}
2476
Guido van Rossum3ac67412007-02-10 18:55:06 +00002477/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002478
2479static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002480dictkeys_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002481{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002482 if (dv->dv_dict == NULL) {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002483 Py_RETURN_NONE;
2484 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002485 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
2486}
2487
2488static int
2489dictkeys_contains(dictviewobject *dv, PyObject *obj)
2490{
2491 if (dv->dv_dict == NULL)
2492 return 0;
2493 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002494}
2495
Guido van Rossum83825ac2007-02-10 04:54:19 +00002496static PySequenceMethods dictkeys_as_sequence = {
2497 (lenfunc)dictview_len, /* sq_length */
2498 0, /* sq_concat */
2499 0, /* sq_repeat */
2500 0, /* sq_item */
2501 0, /* sq_slice */
2502 0, /* sq_ass_item */
2503 0, /* sq_ass_slice */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002504 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002505};
2506
Guido van Rossum523259b2007-08-24 23:41:22 +00002507static PyObject*
2508dictviews_sub(PyObject* self, PyObject *other)
2509{
2510 PyObject *result = PySet_New(self);
2511 PyObject *tmp;
2512 if (result == NULL)
2513 return NULL;
2514
2515 tmp = PyObject_CallMethod(result, "difference_update", "O", other);
2516 if (tmp == NULL) {
2517 Py_DECREF(result);
2518 return NULL;
2519 }
2520
2521 Py_DECREF(tmp);
2522 return result;
2523}
2524
2525static PyObject*
2526dictviews_and(PyObject* self, PyObject *other)
2527{
2528 PyObject *result = PySet_New(self);
2529 PyObject *tmp;
2530 if (result == NULL)
2531 return NULL;
2532
2533 tmp = PyObject_CallMethod(result, "intersection_update", "O", other);
2534 if (tmp == NULL) {
2535 Py_DECREF(result);
2536 return NULL;
2537 }
2538
2539 Py_DECREF(tmp);
2540 return result;
2541}
2542
2543static PyObject*
2544dictviews_or(PyObject* self, PyObject *other)
2545{
2546 PyObject *result = PySet_New(self);
2547 PyObject *tmp;
2548 if (result == NULL)
2549 return NULL;
2550
2551 tmp = PyObject_CallMethod(result, "update", "O", other);
2552 if (tmp == NULL) {
2553 Py_DECREF(result);
2554 return NULL;
2555 }
2556
2557 Py_DECREF(tmp);
2558 return result;
2559}
2560
2561static PyObject*
2562dictviews_xor(PyObject* self, PyObject *other)
2563{
2564 PyObject *result = PySet_New(self);
2565 PyObject *tmp;
2566 if (result == NULL)
2567 return NULL;
2568
2569 tmp = PyObject_CallMethod(result, "symmetric_difference_update", "O",
2570 other);
2571 if (tmp == NULL) {
2572 Py_DECREF(result);
2573 return NULL;
2574 }
2575
2576 Py_DECREF(tmp);
2577 return result;
2578}
2579
2580static PyNumberMethods dictviews_as_number = {
2581 0, /*nb_add*/
2582 (binaryfunc)dictviews_sub, /*nb_subtract*/
2583 0, /*nb_multiply*/
2584 0, /*nb_remainder*/
2585 0, /*nb_divmod*/
2586 0, /*nb_power*/
2587 0, /*nb_negative*/
2588 0, /*nb_positive*/
2589 0, /*nb_absolute*/
2590 0, /*nb_bool*/
2591 0, /*nb_invert*/
2592 0, /*nb_lshift*/
2593 0, /*nb_rshift*/
2594 (binaryfunc)dictviews_and, /*nb_and*/
2595 (binaryfunc)dictviews_xor, /*nb_xor*/
2596 (binaryfunc)dictviews_or, /*nb_or*/
2597};
2598
Guido van Rossumb90c8482007-02-10 01:11:45 +00002599static PyMethodDef dictkeys_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002600 {NULL, NULL} /* sentinel */
2601};
2602
2603PyTypeObject PyDictKeys_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002604 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002605 "dict_keys", /* tp_name */
2606 sizeof(dictviewobject), /* tp_basicsize */
2607 0, /* tp_itemsize */
2608 /* methods */
2609 (destructor)dictview_dealloc, /* tp_dealloc */
2610 0, /* tp_print */
2611 0, /* tp_getattr */
2612 0, /* tp_setattr */
2613 0, /* tp_compare */
2614 0, /* tp_repr */
Guido van Rossum523259b2007-08-24 23:41:22 +00002615 &dictviews_as_number, /* tp_as_number */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002616 &dictkeys_as_sequence, /* tp_as_sequence */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002617 0, /* tp_as_mapping */
2618 0, /* tp_hash */
2619 0, /* tp_call */
2620 0, /* tp_str */
2621 PyObject_GenericGetAttr, /* tp_getattro */
2622 0, /* tp_setattro */
2623 0, /* tp_as_buffer */
2624 Py_TPFLAGS_DEFAULT, /* tp_flags */
2625 0, /* tp_doc */
2626 0, /* tp_traverse */
2627 0, /* tp_clear */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002628 dictview_richcompare, /* tp_richcompare */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002629 0, /* tp_weaklistoffset */
2630 (getiterfunc)dictkeys_iter, /* tp_iter */
2631 0, /* tp_iternext */
2632 dictkeys_methods, /* tp_methods */
2633 0,
2634};
2635
2636static PyObject *
2637dictkeys_new(PyObject *dict)
2638{
2639 return dictview_new(dict, &PyDictKeys_Type);
2640}
2641
Guido van Rossum3ac67412007-02-10 18:55:06 +00002642/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002643
2644static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002645dictitems_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002646{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002647 if (dv->dv_dict == NULL) {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002648 Py_RETURN_NONE;
2649 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002650 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
2651}
2652
2653static int
2654dictitems_contains(dictviewobject *dv, PyObject *obj)
2655{
2656 PyObject *key, *value, *found;
2657 if (dv->dv_dict == NULL)
2658 return 0;
2659 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
2660 return 0;
2661 key = PyTuple_GET_ITEM(obj, 0);
2662 value = PyTuple_GET_ITEM(obj, 1);
2663 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
2664 if (found == NULL) {
2665 if (PyErr_Occurred())
2666 return -1;
2667 return 0;
2668 }
2669 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002670}
2671
Guido van Rossum83825ac2007-02-10 04:54:19 +00002672static PySequenceMethods dictitems_as_sequence = {
2673 (lenfunc)dictview_len, /* sq_length */
2674 0, /* sq_concat */
2675 0, /* sq_repeat */
2676 0, /* sq_item */
2677 0, /* sq_slice */
2678 0, /* sq_ass_item */
2679 0, /* sq_ass_slice */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002680 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002681};
2682
Guido van Rossumb90c8482007-02-10 01:11:45 +00002683static PyMethodDef dictitems_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002684 {NULL, NULL} /* sentinel */
2685};
2686
2687PyTypeObject PyDictItems_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002688 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002689 "dict_items", /* tp_name */
2690 sizeof(dictviewobject), /* tp_basicsize */
2691 0, /* tp_itemsize */
2692 /* methods */
2693 (destructor)dictview_dealloc, /* tp_dealloc */
2694 0, /* tp_print */
2695 0, /* tp_getattr */
2696 0, /* tp_setattr */
2697 0, /* tp_compare */
2698 0, /* tp_repr */
Guido van Rossum523259b2007-08-24 23:41:22 +00002699 &dictviews_as_number, /* tp_as_number */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002700 &dictitems_as_sequence, /* tp_as_sequence */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002701 0, /* tp_as_mapping */
2702 0, /* tp_hash */
2703 0, /* tp_call */
2704 0, /* tp_str */
2705 PyObject_GenericGetAttr, /* tp_getattro */
2706 0, /* tp_setattro */
2707 0, /* tp_as_buffer */
2708 Py_TPFLAGS_DEFAULT, /* tp_flags */
2709 0, /* tp_doc */
2710 0, /* tp_traverse */
2711 0, /* tp_clear */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002712 dictview_richcompare, /* tp_richcompare */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002713 0, /* tp_weaklistoffset */
2714 (getiterfunc)dictitems_iter, /* tp_iter */
2715 0, /* tp_iternext */
2716 dictitems_methods, /* tp_methods */
2717 0,
2718};
2719
2720static PyObject *
2721dictitems_new(PyObject *dict)
2722{
2723 return dictview_new(dict, &PyDictItems_Type);
2724}
2725
Guido van Rossum3ac67412007-02-10 18:55:06 +00002726/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002727
2728static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002729dictvalues_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002730{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002731 if (dv->dv_dict == NULL) {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002732 Py_RETURN_NONE;
2733 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002734 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002735}
2736
Guido van Rossum83825ac2007-02-10 04:54:19 +00002737static PySequenceMethods dictvalues_as_sequence = {
2738 (lenfunc)dictview_len, /* sq_length */
2739 0, /* sq_concat */
2740 0, /* sq_repeat */
2741 0, /* sq_item */
2742 0, /* sq_slice */
2743 0, /* sq_ass_item */
2744 0, /* sq_ass_slice */
2745 (objobjproc)0, /* sq_contains */
2746};
2747
Guido van Rossumb90c8482007-02-10 01:11:45 +00002748static PyMethodDef dictvalues_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002749 {NULL, NULL} /* sentinel */
2750};
2751
2752PyTypeObject PyDictValues_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002753 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002754 "dict_values", /* tp_name */
2755 sizeof(dictviewobject), /* tp_basicsize */
2756 0, /* tp_itemsize */
2757 /* methods */
2758 (destructor)dictview_dealloc, /* tp_dealloc */
2759 0, /* tp_print */
2760 0, /* tp_getattr */
2761 0, /* tp_setattr */
2762 0, /* tp_compare */
2763 0, /* tp_repr */
2764 0, /* tp_as_number */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002765 &dictvalues_as_sequence, /* tp_as_sequence */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002766 0, /* tp_as_mapping */
2767 0, /* tp_hash */
2768 0, /* tp_call */
2769 0, /* tp_str */
2770 PyObject_GenericGetAttr, /* tp_getattro */
2771 0, /* tp_setattro */
2772 0, /* tp_as_buffer */
2773 Py_TPFLAGS_DEFAULT, /* tp_flags */
2774 0, /* tp_doc */
2775 0, /* tp_traverse */
2776 0, /* tp_clear */
2777 0, /* tp_richcompare */
2778 0, /* tp_weaklistoffset */
2779 (getiterfunc)dictvalues_iter, /* tp_iter */
2780 0, /* tp_iternext */
2781 dictvalues_methods, /* tp_methods */
2782 0,
2783};
2784
2785static PyObject *
2786dictvalues_new(PyObject *dict)
2787{
2788 return dictview_new(dict, &PyDictValues_Type);
2789}