blob: ae400b6ef011f4f40c01d70651cc8cdc15a5dcb9 [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;
Guido van Rossum2fd4f372007-11-29 18:43:05 +0000273 Py_INCREF(startkey);
Tim Peters453163d2001-06-03 04:54:32 +0000274 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Guido van Rossum2fd4f372007-11-29 18:43:05 +0000275 Py_DECREF(startkey);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000276 if (cmp < 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000277 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000278 if (ep0 == mp->ma_table && ep->me_key == startkey) {
279 if (cmp > 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000280 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000281 }
282 else {
283 /* The compare did major nasty stuff to the
284 * dict: start over.
285 * XXX A clever adversary could prevent this
286 * XXX from terminating.
287 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000288 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000289 }
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000290 }
291 freeslot = NULL;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000292 }
Tim Peters15d49292001-05-27 07:39:22 +0000293
Tim Peters342c65e2001-05-13 06:43:53 +0000294 /* In the loop, me_key == dummy is by far (factor of 100s) the
295 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000296 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
297 i = (i << 2) + i + perturb + 1;
298 ep = &ep0[i & mask];
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000299 if (ep->me_key == NULL)
300 return freeslot == NULL ? ep : freeslot;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000301 if (ep->me_key == key)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000302 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000303 if (ep->me_hash == hash && ep->me_key != dummy) {
Tim Peters453163d2001-06-03 04:54:32 +0000304 startkey = ep->me_key;
Guido van Rossum2fd4f372007-11-29 18:43:05 +0000305 Py_INCREF(startkey);
Tim Peters453163d2001-06-03 04:54:32 +0000306 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Guido van Rossum2fd4f372007-11-29 18:43:05 +0000307 Py_DECREF(startkey);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000308 if (cmp < 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000309 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000310 if (ep0 == mp->ma_table && ep->me_key == startkey) {
311 if (cmp > 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000312 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000313 }
314 else {
315 /* The compare did major nasty stuff to the
316 * dict: start over.
317 * XXX A clever adversary could prevent this
318 * XXX from terminating.
319 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000320 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000321 }
Guido van Rossum16e93a81997-01-28 00:00:11 +0000322 }
Tim Peters342c65e2001-05-13 06:43:53 +0000323 else if (ep->me_key == dummy && freeslot == NULL)
324 freeslot = ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000325 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000326 assert(0); /* NOT REACHED */
327 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000328}
329
Guido van Rossum89d8c602007-09-18 17:26:56 +0000330/* Return 1 if two unicode objects are equal, 0 if not. */
331static int
332unicode_eq(PyObject *aa, PyObject *bb)
333{
334 PyUnicodeObject *a = (PyUnicodeObject *)aa;
335 PyUnicodeObject *b = (PyUnicodeObject *)bb;
336
337 if (a->length != b->length)
338 return 0;
339 if (a->length == 0)
340 return 1;
341 if (a->str[0] != b->str[0])
342 return 0;
343 if (a->length == 1)
344 return 1;
Guido van Rossume4a9e782007-09-18 18:39:50 +0000345 return memcmp(a->str, b->str, a->length * sizeof(Py_UNICODE)) == 0;
Guido van Rossum89d8c602007-09-18 17:26:56 +0000346}
347
348
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000349/*
Guido van Rossum89d8c602007-09-18 17:26:56 +0000350 * Hacked up version of lookdict which can assume keys are always
351 * unicodes; this assumption allows testing for errors during
352 * PyObject_RichCompareBool() to be dropped; unicode-unicode
353 * comparisons never raise exceptions. This also means we don't need
354 * to go through PyObject_RichCompareBool(); we can always use
355 * unicode_eq() directly.
Fred Drake1bff34a2000-08-31 19:31:38 +0000356 *
Guido van Rossum89d8c602007-09-18 17:26:56 +0000357 * This is valuable because dicts with only unicode keys are very common.
Fred Drake1bff34a2000-08-31 19:31:38 +0000358 */
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000359static PyDictEntry *
360lookdict_unicode(PyDictObject *mp, PyObject *key, register long hash)
Fred Drake1bff34a2000-08-31 19:31:38 +0000361{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000362 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000363 register size_t perturb;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000364 register PyDictEntry *freeslot;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000365 register size_t mask = (size_t)mp->ma_mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000366 PyDictEntry *ep0 = mp->ma_table;
367 register PyDictEntry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000368
Guido van Rossum89d8c602007-09-18 17:26:56 +0000369 /* Make sure this function doesn't have to handle non-unicode keys,
Tim Peters0ab085c2001-09-14 00:25:33 +0000370 including subclasses of str; e.g., one reason to subclass
Guido van Rossum89d8c602007-09-18 17:26:56 +0000371 unicodes is to override __eq__, and for speed we don't cater to
Tim Peters0ab085c2001-09-14 00:25:33 +0000372 that here. */
Guido van Rossum89d8c602007-09-18 17:26:56 +0000373 if (!PyUnicode_CheckExact(key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000374#ifdef SHOW_CONVERSION_COUNTS
375 ++converted;
376#endif
377 mp->ma_lookup = lookdict;
378 return lookdict(mp, key, hash);
379 }
Tim Peters2f228e72001-05-13 00:19:31 +0000380 i = hash & mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000381 ep = &ep0[i];
382 if (ep->me_key == NULL || ep->me_key == key)
383 return ep;
384 if (ep->me_key == dummy)
385 freeslot = ep;
386 else {
Guido van Rossum89d8c602007-09-18 17:26:56 +0000387 if (ep->me_hash == hash && unicode_eq(ep->me_key, key))
Fred Drake1bff34a2000-08-31 19:31:38 +0000388 return ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000389 freeslot = NULL;
390 }
Tim Peters15d49292001-05-27 07:39:22 +0000391
Tim Peters342c65e2001-05-13 06:43:53 +0000392 /* In the loop, me_key == dummy is by far (factor of 100s) the
393 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000394 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
395 i = (i << 2) + i + perturb + 1;
396 ep = &ep0[i & mask];
Tim Peters342c65e2001-05-13 06:43:53 +0000397 if (ep->me_key == NULL)
398 return freeslot == NULL ? ep : freeslot;
399 if (ep->me_key == key
400 || (ep->me_hash == hash
401 && ep->me_key != dummy
Guido van Rossum89d8c602007-09-18 17:26:56 +0000402 && unicode_eq(ep->me_key, key)))
Fred Drake1bff34a2000-08-31 19:31:38 +0000403 return ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000404 if (ep->me_key == dummy && freeslot == NULL)
Tim Peters342c65e2001-05-13 06:43:53 +0000405 freeslot = ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000406 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000407 assert(0); /* NOT REACHED */
408 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000409}
410
411/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000412Internal routine to insert a new item into the table.
413Used both by the internal resize routine and by the public insert routine.
414Eats a reference to key and one to value.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000415Returns -1 if an error occurred, or 0 on success.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000416*/
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000417static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000418insertdict(register PyDictObject *mp, PyObject *key, long hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000419{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000420 PyObject *old_value;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000421 register PyDictEntry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000422 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
423
424 assert(mp->ma_lookup != NULL);
425 ep = mp->ma_lookup(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000426 if (ep == NULL) {
427 Py_DECREF(key);
428 Py_DECREF(value);
429 return -1;
430 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000431 if (ep->me_value != NULL) {
Guido van Rossumd7047b31995-01-02 19:07:15 +0000432 old_value = ep->me_value;
433 ep->me_value = value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000434 Py_DECREF(old_value); /* which **CAN** re-enter */
435 Py_DECREF(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000436 }
437 else {
438 if (ep->me_key == NULL)
439 mp->ma_fill++;
Raymond Hettinger07ead172005-02-05 23:42:57 +0000440 else {
441 assert(ep->me_key == dummy);
442 Py_DECREF(dummy);
443 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000444 ep->me_key = key;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000445 ep->me_hash = (Py_ssize_t)hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000446 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000447 mp->ma_used++;
448 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000449 return 0;
450}
451
452/*
453Internal routine used by dictresize() to insert an item which is
454known to be absent from the dict. This routine also assumes that
455the dict contains no deleted entries. Besides the performance benefit,
456using insertdict() in dictresize() is dangerous (SF bug #1456209).
457Note that no refcounts are changed by this routine; if needed, the caller
458is responsible for incref'ing `key` and `value`.
459*/
460static void
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000461insertdict_clean(register PyDictObject *mp, PyObject *key, long hash,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000462 PyObject *value)
463{
464 register size_t i;
465 register size_t perturb;
466 register size_t mask = (size_t)mp->ma_mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000467 PyDictEntry *ep0 = mp->ma_table;
468 register PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000469
470 i = hash & mask;
471 ep = &ep0[i];
472 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
473 i = (i << 2) + i + perturb + 1;
474 ep = &ep0[i & mask];
475 }
476 assert(ep->me_value == NULL);
477 mp->ma_fill++;
478 ep->me_key = key;
479 ep->me_hash = (Py_ssize_t)hash;
480 ep->me_value = value;
481 mp->ma_used++;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000482}
483
484/*
485Restructure the table by allocating a new table and reinserting all
486items again. When entries have been deleted, the new table may
487actually be smaller than the old one.
488*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000489static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000490dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000491{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000492 Py_ssize_t newsize;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000493 PyDictEntry *oldtable, *newtable, *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000494 Py_ssize_t i;
Tim Peters0c6010b2001-05-23 23:33:57 +0000495 int is_oldtable_malloced;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000496 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000497
498 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000499
Tim Peterseb28ef22001-06-02 05:27:19 +0000500 /* Find the smallest table size > minused. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000501 for (newsize = PyDict_MINSIZE;
Tim Peterseb28ef22001-06-02 05:27:19 +0000502 newsize <= minused && newsize > 0;
503 newsize <<= 1)
504 ;
505 if (newsize <= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000506 PyErr_NoMemory();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000507 return -1;
508 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000509
510 /* Get space for a new table. */
511 oldtable = mp->ma_table;
512 assert(oldtable != NULL);
513 is_oldtable_malloced = oldtable != mp->ma_smalltable;
514
Tim Peters6d6c1a32001-08-02 04:15:00 +0000515 if (newsize == PyDict_MINSIZE) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000516 /* A large table is shrinking, or we can't get any smaller. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000517 newtable = mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000518 if (newtable == oldtable) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000519 if (mp->ma_fill == mp->ma_used) {
520 /* No dummies, so no point doing anything. */
Tim Peters0c6010b2001-05-23 23:33:57 +0000521 return 0;
Tim Petersf8a548c2001-05-24 16:26:40 +0000522 }
523 /* We're not going to resize it, but rebuild the
524 table anyway to purge old dummy entries.
525 Subtle: This is *necessary* if fill==size,
526 as lookdict needs at least one virgin slot to
527 terminate failing searches. If fill < size, it's
528 merely desirable, as dummies slow searches. */
529 assert(mp->ma_fill > mp->ma_used);
Tim Peters0c6010b2001-05-23 23:33:57 +0000530 memcpy(small_copy, oldtable, sizeof(small_copy));
531 oldtable = small_copy;
532 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000533 }
534 else {
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000535 newtable = PyMem_NEW(PyDictEntry, newsize);
Tim Petersdea48ec2001-05-22 20:40:22 +0000536 if (newtable == NULL) {
537 PyErr_NoMemory();
538 return -1;
539 }
540 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000541
542 /* Make the dict empty, using the new table. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000543 assert(newtable != oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000544 mp->ma_table = newtable;
Tim Petersafb6ae82001-06-04 21:00:21 +0000545 mp->ma_mask = newsize - 1;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000546 memset(newtable, 0, sizeof(PyDictEntry) * newsize);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000547 mp->ma_used = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000548 i = mp->ma_fill;
549 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000550
Tim Peters19283142001-05-17 22:25:34 +0000551 /* Copy the data over; this is refcount-neutral for active entries;
552 dummy entries aren't copied over, of course */
Tim Petersdea48ec2001-05-22 20:40:22 +0000553 for (ep = oldtable; i > 0; ep++) {
554 if (ep->me_value != NULL) { /* active entry */
555 --i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000556 insertdict_clean(mp, ep->me_key, (long)ep->me_hash,
557 ep->me_value);
Tim Petersdea48ec2001-05-22 20:40:22 +0000558 }
Tim Peters19283142001-05-17 22:25:34 +0000559 else if (ep->me_key != NULL) { /* dummy entry */
Tim Petersdea48ec2001-05-22 20:40:22 +0000560 --i;
Tim Peters19283142001-05-17 22:25:34 +0000561 assert(ep->me_key == dummy);
562 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000563 }
Tim Peters19283142001-05-17 22:25:34 +0000564 /* else key == value == NULL: nothing to do */
Guido van Rossumd7047b31995-01-02 19:07:15 +0000565 }
566
Tim Peters0c6010b2001-05-23 23:33:57 +0000567 if (is_oldtable_malloced)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000568 PyMem_DEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000569 return 0;
570}
571
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000572/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
573 * that may occur (originally dicts supported only string keys, and exceptions
574 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000575 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000576 * (suppressed) occurred while computing the key's hash, or that some error
577 * (suppressed) occurred when comparing keys in the dict's internal probe
578 * sequence. A nasty example of the latter is when a Python-coded comparison
579 * function hits a stack-depth error, which can cause this to return NULL
580 * even if the key is present.
581 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000582PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000583PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000584{
585 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000586 PyDictObject *mp = (PyDictObject *)op;
587 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000588 PyThreadState *tstate;
589 if (!PyDict_Check(op))
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000590 return NULL;
Guido van Rossum4d027722007-09-18 04:30:42 +0000591 if (!PyUnicode_CheckExact(key) ||
592 (hash = ((PyUnicodeObject *) key)->hash) == -1)
Guido van Rossumca756f21997-01-23 19:39:29 +0000593 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000594 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000595 if (hash == -1) {
596 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000597 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000598 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000599 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000600
601 /* We can arrive here with a NULL tstate during initialization:
602 try running "python -Wi" for an example related to string
603 interning. Let's just hope that no exception occurs then... */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000604 tstate = _PyThreadState_Current;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000605 if (tstate != NULL && tstate->curexc_type != NULL) {
606 /* preserve the existing exception */
607 PyObject *err_type, *err_value, *err_tb;
608 PyErr_Fetch(&err_type, &err_value, &err_tb);
609 ep = (mp->ma_lookup)(mp, key, hash);
610 /* ignore errors */
611 PyErr_Restore(err_type, err_value, err_tb);
612 if (ep == NULL)
613 return NULL;
614 }
615 else {
616 ep = (mp->ma_lookup)(mp, key, hash);
617 if (ep == NULL) {
618 PyErr_Clear();
619 return NULL;
620 }
621 }
622 return ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000623}
624
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000625/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
626 This returns NULL *with* an exception set if an exception occurred.
627 It returns NULL *without* an exception set if the key wasn't present.
628*/
629PyObject *
630PyDict_GetItemWithError(PyObject *op, PyObject *key)
631{
632 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000633 PyDictObject*mp = (PyDictObject *)op;
634 PyDictEntry *ep;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000635
636 if (!PyDict_Check(op)) {
637 PyErr_BadInternalCall();
638 return NULL;
639 }
Guido van Rossum89d8c602007-09-18 17:26:56 +0000640 if (!PyUnicode_CheckExact(key) ||
641 (hash = ((PyUnicodeObject *) key)->hash) == -1)
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000642 {
643 hash = PyObject_Hash(key);
644 if (hash == -1) {
645 return NULL;
646 }
647 }
648
649 ep = (mp->ma_lookup)(mp, key, hash);
650 if (ep == NULL)
651 return NULL;
652 return ep->me_value;
653}
654
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000655/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
Tim Peters60b29962006-01-01 01:19:23 +0000656 * dictionary if it's merely replacing the value for an existing key.
657 * This means that it's safe to loop over a dictionary with PyDict_Next()
658 * and occasionally replace a value -- but you can't insert new keys or
659 * remove them.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000660 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000661int
Tim Peters1f5871e2000-07-04 17:44:48 +0000662PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000663{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000664 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000665 register long hash;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000666 register Py_ssize_t n_used;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000667
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000668 if (!PyDict_Check(op)) {
669 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000670 return -1;
671 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000672 assert(key);
673 assert(value);
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000674 mp = (PyDictObject *)op;
Guido van Rossum4d027722007-09-18 04:30:42 +0000675 if (!PyUnicode_CheckExact(key) ||
676 (hash = ((PyUnicodeObject *) key)->hash) == -1)
677 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000678 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000679 if (hash == -1)
680 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000681 }
Tim Petersafb6ae82001-06-04 21:00:21 +0000682 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000683 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000684 Py_INCREF(value);
685 Py_INCREF(key);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000686 if (insertdict(mp, key, hash, value) != 0)
687 return -1;
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000688 /* If we added a key, we can safely resize. Otherwise just return!
689 * If fill >= 2/3 size, adjust size. Normally, this doubles or
690 * quaduples the size, but it's also possible for the dict to shrink
Tim Peters60b29962006-01-01 01:19:23 +0000691 * (if ma_fill is much larger than ma_used, meaning a lot of dict
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000692 * keys have been * deleted).
Tim Peters60b29962006-01-01 01:19:23 +0000693 *
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000694 * Quadrupling the size improves average dictionary sparseness
695 * (reducing collisions) at the cost of some memory and iteration
696 * speed (which loops over every possible entry). It also halves
697 * the number of expensive resize operations in a growing dictionary.
Tim Peters60b29962006-01-01 01:19:23 +0000698 *
699 * Very large dictionaries (over 50K items) use doubling instead.
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000700 * This may help applications with severe memory constraints.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000701 */
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000702 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
703 return 0;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000704 return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000705}
706
707int
Tim Peters1f5871e2000-07-04 17:44:48 +0000708PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000709{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000710 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000711 register long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000712 register PyDictEntry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000713 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000714
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000715 if (!PyDict_Check(op)) {
716 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000717 return -1;
718 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000719 assert(key);
Guido van Rossum89d8c602007-09-18 17:26:56 +0000720 if (!PyUnicode_CheckExact(key) ||
721 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000722 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000723 if (hash == -1)
724 return -1;
725 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000726 mp = (PyDictObject *)op;
Fred Drake1bff34a2000-08-31 19:31:38 +0000727 ep = (mp->ma_lookup)(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000728 if (ep == NULL)
729 return -1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000730 if (ep->me_value == NULL) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000731 set_key_error(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000732 return -1;
733 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000734 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000735 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000736 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000737 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000738 ep->me_value = NULL;
739 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000740 Py_DECREF(old_value);
741 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000742 return 0;
743}
744
Guido van Rossum25831651993-05-19 14:50:45 +0000745void
Tim Peters1f5871e2000-07-04 17:44:48 +0000746PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000747{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000748 PyDictObject *mp;
749 PyDictEntry *ep, *table;
Tim Petersdea48ec2001-05-22 20:40:22 +0000750 int table_is_malloced;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000751 Py_ssize_t fill;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000752 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Petersdea48ec2001-05-22 20:40:22 +0000753#ifdef Py_DEBUG
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000754 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +0000755#endif
756
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000757 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000758 return;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000759 mp = (PyDictObject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000760#ifdef Py_DEBUG
Tim Petersafb6ae82001-06-04 21:00:21 +0000761 n = mp->ma_mask + 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000762 i = 0;
763#endif
764
765 table = mp->ma_table;
766 assert(table != NULL);
767 table_is_malloced = table != mp->ma_smalltable;
768
769 /* This is delicate. During the process of clearing the dict,
770 * decrefs can cause the dict to mutate. To avoid fatal confusion
771 * (voice of experience), we have to make the dict empty before
772 * clearing the slots, and never refer to anything via mp->xxx while
773 * clearing.
774 */
775 fill = mp->ma_fill;
776 if (table_is_malloced)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000777 EMPTY_TO_MINSIZE(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000778
779 else if (fill > 0) {
780 /* It's a small table with something that needs to be cleared.
781 * Afraid the only safe way is to copy the dict entries into
782 * another small table first.
783 */
784 memcpy(small_copy, table, sizeof(small_copy));
785 table = small_copy;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000786 EMPTY_TO_MINSIZE(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000787 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000788 /* else it's a small table that's already empty */
789
790 /* Now we can finally clear things. If C had refcounts, we could
791 * assert that the refcount on table is 1 now, i.e. that this function
792 * has unique access to it, so decref side-effects can't alter it.
793 */
794 for (ep = table; fill > 0; ++ep) {
795#ifdef Py_DEBUG
796 assert(i < n);
797 ++i;
798#endif
799 if (ep->me_key) {
800 --fill;
801 Py_DECREF(ep->me_key);
802 Py_XDECREF(ep->me_value);
803 }
804#ifdef Py_DEBUG
805 else
806 assert(ep->me_value == NULL);
807#endif
808 }
809
810 if (table_is_malloced)
811 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000812}
813
Tim Peters080c88b2003-02-15 03:01:11 +0000814/*
815 * Iterate over a dict. Use like so:
816 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000817 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +0000818 * PyObject *key, *value;
819 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +0000820 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +0000821 * Refer to borrowed references in key and value.
822 * }
823 *
824 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +0000825 * mutates the dict. One exception: it is safe if the loop merely changes
826 * the values associated with the keys (but doesn't insert new keys or
827 * delete keys), via PyDict_SetItem().
828 */
Guido van Rossum25831651993-05-19 14:50:45 +0000829int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000830PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000831{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000832 register Py_ssize_t i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000833 register Py_ssize_t mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000834 register PyDictEntry *ep;
Raymond Hettinger43442782004-03-17 21:55:03 +0000835
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000836 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000837 return 0;
Guido van Rossum25831651993-05-19 14:50:45 +0000838 i = *ppos;
839 if (i < 0)
840 return 0;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000841 ep = ((PyDictObject *)op)->ma_table;
842 mask = ((PyDictObject *)op)->ma_mask;
Raymond Hettinger43442782004-03-17 21:55:03 +0000843 while (i <= mask && ep[i].me_value == NULL)
Guido van Rossum25831651993-05-19 14:50:45 +0000844 i++;
845 *ppos = i+1;
Raymond Hettinger43442782004-03-17 21:55:03 +0000846 if (i > mask)
Guido van Rossum25831651993-05-19 14:50:45 +0000847 return 0;
848 if (pkey)
Raymond Hettinger43442782004-03-17 21:55:03 +0000849 *pkey = ep[i].me_key;
Guido van Rossum25831651993-05-19 14:50:45 +0000850 if (pvalue)
Raymond Hettinger43442782004-03-17 21:55:03 +0000851 *pvalue = ep[i].me_value;
Guido van Rossum25831651993-05-19 14:50:45 +0000852 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000853}
854
Thomas Wouterscf297e42007-02-23 15:07:44 +0000855/* Internal version of PyDict_Next that returns a hash value in addition to the key and value.*/
856int
857_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue, long *phash)
858{
859 register Py_ssize_t i;
860 register Py_ssize_t mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000861 register PyDictEntry *ep;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000862
863 if (!PyDict_Check(op))
864 return 0;
865 i = *ppos;
866 if (i < 0)
867 return 0;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000868 ep = ((PyDictObject *)op)->ma_table;
869 mask = ((PyDictObject *)op)->ma_mask;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000870 while (i <= mask && ep[i].me_value == NULL)
871 i++;
872 *ppos = i+1;
873 if (i > mask)
874 return 0;
875 *phash = (long)(ep[i].me_hash);
876 if (pkey)
877 *pkey = ep[i].me_key;
878 if (pvalue)
879 *pvalue = ep[i].me_value;
880 return 1;
881}
882
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000883/* Methods */
884
885static void
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000886dict_dealloc(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000887{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000888 register PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000889 Py_ssize_t fill = mp->ma_fill;
Guido van Rossumff413af2002-03-28 20:34:59 +0000890 PyObject_GC_UnTrack(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000891 Py_TRASHCAN_SAFE_BEGIN(mp)
Tim Petersdea48ec2001-05-22 20:40:22 +0000892 for (ep = mp->ma_table; fill > 0; ep++) {
893 if (ep->me_key) {
894 --fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000895 Py_DECREF(ep->me_key);
Tim Petersdea48ec2001-05-22 20:40:22 +0000896 Py_XDECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000897 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000898 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000899 if (mp->ma_table != mp->ma_smalltable)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000900 PyMem_DEL(mp->ma_table);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000901 if (num_free_dicts < MAXFREEDICTS && Py_Type(mp) == &PyDict_Type)
Raymond Hettinger43442782004-03-17 21:55:03 +0000902 free_dicts[num_free_dicts++] = mp;
Tim Peters60b29962006-01-01 01:19:23 +0000903 else
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000904 Py_Type(mp)->tp_free((PyObject *)mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000905 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000906}
907
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000908static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000909dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000910{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000911 Py_ssize_t i;
Tim Petersa7259592001-06-16 05:11:17 +0000912 PyObject *s, *temp, *colon = NULL;
913 PyObject *pieces = NULL, *result = NULL;
914 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +0000915
Tim Petersa7259592001-06-16 05:11:17 +0000916 i = Py_ReprEnter((PyObject *)mp);
Guido van Rossum255443b1998-04-10 22:47:14 +0000917 if (i != 0) {
Walter Dörwald1ab83302007-05-18 17:15:44 +0000918 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
Guido van Rossum255443b1998-04-10 22:47:14 +0000919 }
920
Tim Petersa7259592001-06-16 05:11:17 +0000921 if (mp->ma_used == 0) {
Walter Dörwald1ab83302007-05-18 17:15:44 +0000922 result = PyUnicode_FromString("{}");
Tim Petersa7259592001-06-16 05:11:17 +0000923 goto Done;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000924 }
Tim Petersa7259592001-06-16 05:11:17 +0000925
926 pieces = PyList_New(0);
927 if (pieces == NULL)
928 goto Done;
929
Walter Dörwald1ab83302007-05-18 17:15:44 +0000930 colon = PyUnicode_FromString(": ");
Tim Petersa7259592001-06-16 05:11:17 +0000931 if (colon == NULL)
932 goto Done;
933
934 /* Do repr() on each key+value pair, and insert ": " between them.
935 Note that repr may mutate the dict. */
Tim Petersc6057842001-06-16 07:52:53 +0000936 i = 0;
937 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Tim Petersa7259592001-06-16 05:11:17 +0000938 int status;
939 /* Prevent repr from deleting value during key format. */
940 Py_INCREF(value);
941 s = PyObject_Repr(key);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000942 PyUnicode_Append(&s, colon);
943 PyUnicode_AppendAndDel(&s, PyObject_Repr(value));
Tim Petersa7259592001-06-16 05:11:17 +0000944 Py_DECREF(value);
945 if (s == NULL)
946 goto Done;
947 status = PyList_Append(pieces, s);
948 Py_DECREF(s); /* append created a new ref */
949 if (status < 0)
950 goto Done;
951 }
952
953 /* Add "{}" decorations to the first and last items. */
954 assert(PyList_GET_SIZE(pieces) > 0);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000955 s = PyUnicode_FromString("{");
Tim Petersa7259592001-06-16 05:11:17 +0000956 if (s == NULL)
957 goto Done;
958 temp = PyList_GET_ITEM(pieces, 0);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000959 PyUnicode_AppendAndDel(&s, temp);
Tim Petersa7259592001-06-16 05:11:17 +0000960 PyList_SET_ITEM(pieces, 0, s);
961 if (s == NULL)
962 goto Done;
963
Walter Dörwald1ab83302007-05-18 17:15:44 +0000964 s = PyUnicode_FromString("}");
Tim Petersa7259592001-06-16 05:11:17 +0000965 if (s == NULL)
966 goto Done;
967 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000968 PyUnicode_AppendAndDel(&temp, s);
Tim Petersa7259592001-06-16 05:11:17 +0000969 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
970 if (temp == NULL)
971 goto Done;
972
973 /* Paste them all together with ", " between. */
Walter Dörwald1ab83302007-05-18 17:15:44 +0000974 s = PyUnicode_FromString(", ");
Tim Petersa7259592001-06-16 05:11:17 +0000975 if (s == NULL)
976 goto Done;
Walter Dörwald1ab83302007-05-18 17:15:44 +0000977 result = PyUnicode_Join(s, pieces);
Tim Peters0ab085c2001-09-14 00:25:33 +0000978 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +0000979
980Done:
981 Py_XDECREF(pieces);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000982 Py_XDECREF(colon);
Tim Petersa7259592001-06-16 05:11:17 +0000983 Py_ReprLeave((PyObject *)mp);
984 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000985}
986
Martin v. Löwis18e16552006-02-15 17:27:45 +0000987static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000988dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000989{
990 return mp->ma_used;
991}
992
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000993static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000994dict_subscript(PyDictObject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000995{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000996 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000997 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000998 PyDictEntry *ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000999 assert(mp->ma_table != NULL);
Guido van Rossum89d8c602007-09-18 17:26:56 +00001000 if (!PyUnicode_CheckExact(key) ||
1001 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001002 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001003 if (hash == -1)
1004 return NULL;
1005 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001006 ep = (mp->ma_lookup)(mp, key, hash);
1007 if (ep == NULL)
1008 return NULL;
1009 v = ep->me_value;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001010 if (v == NULL) {
1011 if (!PyDict_CheckExact(mp)) {
1012 /* Look up __missing__ method if we're a subclass. */
Guido van Rossum4b92a822006-02-25 23:32:30 +00001013 PyObject *missing;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001014 static PyObject *missing_str = NULL;
1015 if (missing_str == NULL)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001016 missing_str =
Martin v. Löwis5b222132007-06-10 09:51:05 +00001017 PyUnicode_InternFromString("__missing__");
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001018 missing = _PyType_Lookup(Py_Type(mp), missing_str);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001019 if (missing != NULL)
1020 return PyObject_CallFunctionObjArgs(missing,
1021 (PyObject *)mp, key, NULL);
1022 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001023 set_key_error(key);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001024 return NULL;
1025 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001026 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001027 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001028 return v;
1029}
1030
1031static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001032dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001033{
1034 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001035 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001036 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001037 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001038}
1039
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001040static PyMappingMethods dict_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001041 (lenfunc)dict_length, /*mp_length*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001042 (binaryfunc)dict_subscript, /*mp_subscript*/
1043 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001044};
1045
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001046static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001047dict_keys(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001048{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001049 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001050 register Py_ssize_t i, j;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001051 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001052 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001053
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001054 again:
1055 n = mp->ma_used;
1056 v = PyList_New(n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001057 if (v == NULL)
1058 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001059 if (n != mp->ma_used) {
1060 /* Durnit. The allocations caused the dict to resize.
1061 * Just start over, this shouldn't normally happen.
1062 */
1063 Py_DECREF(v);
1064 goto again;
1065 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001066 ep = mp->ma_table;
1067 mask = mp->ma_mask;
1068 for (i = 0, j = 0; i <= mask; i++) {
1069 if (ep[i].me_value != NULL) {
1070 PyObject *key = ep[i].me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001071 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001072 PyList_SET_ITEM(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001073 j++;
1074 }
1075 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001076 assert(j == n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001077 return v;
1078}
1079
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001080static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001081dict_values(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001082{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001083 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001084 register Py_ssize_t i, j;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001085 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001086 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001087
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001088 again:
1089 n = mp->ma_used;
1090 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001091 if (v == NULL)
1092 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001093 if (n != mp->ma_used) {
1094 /* Durnit. The allocations caused the dict to resize.
1095 * Just start over, this shouldn't normally happen.
1096 */
1097 Py_DECREF(v);
1098 goto again;
1099 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001100 ep = mp->ma_table;
1101 mask = mp->ma_mask;
1102 for (i = 0, j = 0; i <= mask; i++) {
1103 if (ep[i].me_value != NULL) {
1104 PyObject *value = ep[i].me_value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001105 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001106 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001107 j++;
1108 }
1109 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001110 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001111 return v;
1112}
1113
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001114static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001115dict_items(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001116{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001117 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001118 register Py_ssize_t i, j, n;
1119 Py_ssize_t mask;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001120 PyObject *item, *key, *value;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001121 PyDictEntry *ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001122
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001123 /* Preallocate the list of tuples, to avoid allocations during
1124 * the loop over the items, which could trigger GC, which
1125 * could resize the dict. :-(
1126 */
1127 again:
1128 n = mp->ma_used;
1129 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001130 if (v == NULL)
1131 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001132 for (i = 0; i < n; i++) {
1133 item = PyTuple_New(2);
1134 if (item == NULL) {
1135 Py_DECREF(v);
1136 return NULL;
1137 }
1138 PyList_SET_ITEM(v, i, item);
1139 }
1140 if (n != mp->ma_used) {
1141 /* Durnit. The allocations caused the dict to resize.
1142 * Just start over, this shouldn't normally happen.
1143 */
1144 Py_DECREF(v);
1145 goto again;
1146 }
1147 /* Nothing we do below makes any function calls. */
Raymond Hettinger43442782004-03-17 21:55:03 +00001148 ep = mp->ma_table;
1149 mask = mp->ma_mask;
1150 for (i = 0, j = 0; i <= mask; i++) {
Raymond Hettinger06905122004-03-19 10:30:00 +00001151 if ((value=ep[i].me_value) != NULL) {
Raymond Hettinger43442782004-03-17 21:55:03 +00001152 key = ep[i].me_key;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001153 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001154 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001155 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001156 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001157 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001158 j++;
1159 }
1160 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001161 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001162 return v;
1163}
1164
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001165static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001166dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001167{
1168 PyObject *seq;
1169 PyObject *value = Py_None;
1170 PyObject *it; /* iter(seq) */
1171 PyObject *key;
1172 PyObject *d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001173 int status;
1174
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001175 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001176 return NULL;
1177
1178 d = PyObject_CallObject(cls, NULL);
1179 if (d == NULL)
1180 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001181
Guido van Rossum58da9312007-11-10 23:39:45 +00001182 if (PyDict_CheckExact(d) && PyDict_CheckExact(seq)) {
1183 PyDictObject *mp = (PyDictObject *)d;
1184 PyObject *oldvalue;
1185 Py_ssize_t pos = 0;
1186 PyObject *key;
1187 long hash;
1188
1189 if (dictresize(mp, PySet_GET_SIZE(seq)))
1190 return NULL;
1191
1192 while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
1193 Py_INCREF(key);
1194 Py_INCREF(value);
1195 if (insertdict(mp, key, hash, value))
1196 return NULL;
1197 }
1198 return d;
1199 }
1200
Guido van Rossumd8faa362007-04-27 19:54:29 +00001201 if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) {
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001202 PyDictObject *mp = (PyDictObject *)d;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001203 Py_ssize_t pos = 0;
1204 PyObject *key;
1205 long hash;
1206
1207 if (dictresize(mp, PySet_GET_SIZE(seq)))
1208 return NULL;
1209
1210 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
1211 Py_INCREF(key);
1212 Py_INCREF(value);
1213 if (insertdict(mp, key, hash, value))
1214 return NULL;
1215 }
1216 return d;
1217 }
1218
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001219 it = PyObject_GetIter(seq);
1220 if (it == NULL){
1221 Py_DECREF(d);
1222 return NULL;
1223 }
1224
Guido van Rossum58da9312007-11-10 23:39:45 +00001225 if (PyDict_CheckExact(d)) {
1226 while ((key = PyIter_Next(it)) != NULL) {
1227 status = PyDict_SetItem(d, key, value);
1228 Py_DECREF(key);
1229 if (status < 0)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001230 goto Fail;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001231 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001232 } else {
1233 while ((key = PyIter_Next(it)) != NULL) {
1234 status = PyObject_SetItem(d, key, value);
1235 Py_DECREF(key);
1236 if (status < 0)
1237 goto Fail;
1238 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001239 }
1240
Guido van Rossum58da9312007-11-10 23:39:45 +00001241 if (PyErr_Occurred())
1242 goto Fail;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001243 Py_DECREF(it);
1244 return d;
1245
1246Fail:
1247 Py_DECREF(it);
1248 Py_DECREF(d);
1249 return NULL;
1250}
1251
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001252static int
1253dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001254{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001255 PyObject *arg = NULL;
1256 int result = 0;
1257
1258 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1259 result = -1;
1260
1261 else if (arg != NULL) {
1262 if (PyObject_HasAttrString(arg, "keys"))
1263 result = PyDict_Merge(self, arg, 1);
1264 else
1265 result = PyDict_MergeFromSeq2(self, arg, 1);
1266 }
1267 if (result == 0 && kwds != NULL)
1268 result = PyDict_Merge(self, kwds, 1);
1269 return result;
1270}
1271
1272static PyObject *
1273dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1274{
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001275 if (dict_update_common(self, args, kwds, "update") != -1)
1276 Py_RETURN_NONE;
1277 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001278}
1279
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001280/* Update unconditionally replaces existing items.
1281 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001282 otherwise it leaves existing items unchanged.
1283
1284 PyDict_{Update,Merge} update/merge from a mapping object.
1285
Tim Petersf582b822001-12-11 18:51:08 +00001286 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001287 producing iterable objects of length 2.
1288*/
1289
Tim Petersf582b822001-12-11 18:51:08 +00001290int
Tim Peters1fc240e2001-10-26 05:06:50 +00001291PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1292{
1293 PyObject *it; /* iter(seq2) */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001294 Py_ssize_t i; /* index into seq2 of current element */
Tim Peters1fc240e2001-10-26 05:06:50 +00001295 PyObject *item; /* seq2[i] */
1296 PyObject *fast; /* item as a 2-tuple or 2-list */
1297
1298 assert(d != NULL);
1299 assert(PyDict_Check(d));
1300 assert(seq2 != NULL);
1301
1302 it = PyObject_GetIter(seq2);
1303 if (it == NULL)
1304 return -1;
1305
1306 for (i = 0; ; ++i) {
1307 PyObject *key, *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001308 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001309
1310 fast = NULL;
1311 item = PyIter_Next(it);
1312 if (item == NULL) {
1313 if (PyErr_Occurred())
1314 goto Fail;
1315 break;
1316 }
1317
1318 /* Convert item to sequence, and verify length 2. */
1319 fast = PySequence_Fast(item, "");
1320 if (fast == NULL) {
1321 if (PyErr_ExceptionMatches(PyExc_TypeError))
1322 PyErr_Format(PyExc_TypeError,
1323 "cannot convert dictionary update "
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001324 "sequence element #%zd to a sequence",
Tim Peters1fc240e2001-10-26 05:06:50 +00001325 i);
1326 goto Fail;
1327 }
1328 n = PySequence_Fast_GET_SIZE(fast);
1329 if (n != 2) {
1330 PyErr_Format(PyExc_ValueError,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001331 "dictionary update sequence element #%zd "
Martin v. Löwis2c95cc62006-02-16 06:54:25 +00001332 "has length %zd; 2 is required",
Martin v. Löwise0e89f72006-02-16 06:59:22 +00001333 i, n);
Tim Peters1fc240e2001-10-26 05:06:50 +00001334 goto Fail;
1335 }
1336
1337 /* Update/merge with this (key, value) pair. */
1338 key = PySequence_Fast_GET_ITEM(fast, 0);
1339 value = PySequence_Fast_GET_ITEM(fast, 1);
1340 if (override || PyDict_GetItem(d, key) == NULL) {
1341 int status = PyDict_SetItem(d, key, value);
1342 if (status < 0)
1343 goto Fail;
1344 }
1345 Py_DECREF(fast);
1346 Py_DECREF(item);
1347 }
1348
1349 i = 0;
1350 goto Return;
1351Fail:
1352 Py_XDECREF(item);
1353 Py_XDECREF(fast);
1354 i = -1;
1355Return:
1356 Py_DECREF(it);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001357 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001358}
1359
Tim Peters6d6c1a32001-08-02 04:15:00 +00001360int
1361PyDict_Update(PyObject *a, PyObject *b)
1362{
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001363 return PyDict_Merge(a, b, 1);
1364}
1365
1366int
1367PyDict_Merge(PyObject *a, PyObject *b, int override)
1368{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001369 register PyDictObject *mp, *other;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001370 register Py_ssize_t i;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001371 PyDictEntry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001372
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001373 /* We accept for the argument either a concrete dictionary object,
1374 * or an abstract "mapping" object. For the former, we can do
1375 * things quite efficiently. For the latter, we only require that
1376 * PyMapping_Keys() and PyObject_GetItem() be supported.
1377 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001378 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1379 PyErr_BadInternalCall();
1380 return -1;
1381 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001382 mp = (PyDictObject*)a;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001383 if (PyDict_CheckExact(b)) {
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001384 other = (PyDictObject*)b;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001385 if (other == mp || other->ma_used == 0)
1386 /* a.update(a) or a.update({}); nothing to do */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001387 return 0;
Raymond Hettinger186e7392005-05-14 18:08:25 +00001388 if (mp->ma_used == 0)
1389 /* Since the target dict is empty, PyDict_GetItem()
1390 * always returns NULL. Setting override to 1
1391 * skips the unnecessary test.
1392 */
1393 override = 1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001394 /* Do one big resize at the start, rather than
1395 * incrementally resizing as we insert new items. Expect
1396 * that there will be no (or few) overlapping keys.
1397 */
1398 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
Raymond Hettingerc8d22902003-05-07 00:49:40 +00001399 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001400 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001401 }
1402 for (i = 0; i <= other->ma_mask; i++) {
1403 entry = &other->ma_table[i];
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001404 if (entry->me_value != NULL &&
1405 (override ||
1406 PyDict_GetItem(a, entry->me_key) == NULL)) {
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001407 Py_INCREF(entry->me_key);
1408 Py_INCREF(entry->me_value);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001409 if (insertdict(mp, entry->me_key,
1410 (long)entry->me_hash,
1411 entry->me_value) != 0)
1412 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001413 }
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001414 }
1415 }
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001416 else {
1417 /* Do it the generic, slower way */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001418 PyObject *keys = PyMapping_Keys(b);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001419 PyObject *iter;
1420 PyObject *key, *value;
1421 int status;
1422
1423 if (keys == NULL)
1424 /* Docstring says this is equivalent to E.keys() so
1425 * if E doesn't have a .keys() method we want
1426 * AttributeError to percolate up. Might as well
1427 * do the same for any other error.
1428 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001429 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001430
1431 iter = PyObject_GetIter(keys);
1432 Py_DECREF(keys);
1433 if (iter == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001434 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001435
1436 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001437 if (!override && PyDict_GetItem(a, key) != NULL) {
1438 Py_DECREF(key);
1439 continue;
1440 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001441 value = PyObject_GetItem(b, key);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001442 if (value == NULL) {
1443 Py_DECREF(iter);
1444 Py_DECREF(key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001445 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001446 }
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001447 status = PyDict_SetItem(a, key, value);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001448 Py_DECREF(key);
1449 Py_DECREF(value);
1450 if (status < 0) {
1451 Py_DECREF(iter);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001452 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001453 }
1454 }
1455 Py_DECREF(iter);
1456 if (PyErr_Occurred())
1457 /* Iterator completed, via error */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001458 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001459 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001460 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001461}
1462
1463static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001464dict_copy(register PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001465{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001466 return PyDict_Copy((PyObject*)mp);
1467}
1468
1469PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001470PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001471{
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001472 PyObject *copy;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001473
1474 if (o == NULL || !PyDict_Check(o)) {
1475 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001476 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001477 }
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001478 copy = PyDict_New();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001479 if (copy == NULL)
1480 return NULL;
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001481 if (PyDict_Merge(copy, o, 1) == 0)
1482 return copy;
1483 Py_DECREF(copy);
Raymond Hettinger1356f782005-04-15 15:58:42 +00001484 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001485}
1486
Martin v. Löwis18e16552006-02-15 17:27:45 +00001487Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00001488PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001489{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001490 if (mp == NULL || !PyDict_Check(mp)) {
1491 PyErr_BadInternalCall();
Jeremy Hylton7083bb72004-02-17 20:10:11 +00001492 return -1;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001493 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001494 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001495}
1496
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001497PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001498PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001499{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001500 if (mp == NULL || !PyDict_Check(mp)) {
1501 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001502 return NULL;
1503 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001504 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001505}
1506
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001507PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001508PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001509{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001510 if (mp == NULL || !PyDict_Check(mp)) {
1511 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001512 return NULL;
1513 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001514 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001515}
1516
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001517PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001518PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001519{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001520 if (mp == NULL || !PyDict_Check(mp)) {
1521 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001522 return NULL;
1523 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001524 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001525}
1526
Tim Peterse63415e2001-05-08 04:38:29 +00001527/* Return 1 if dicts equal, 0 if not, -1 if error.
1528 * Gets out as soon as any difference is detected.
1529 * Uses only Py_EQ comparison.
1530 */
1531static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001532dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00001533{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001534 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00001535
1536 if (a->ma_used != b->ma_used)
1537 /* can't be equal if # of entries differ */
1538 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001539
Tim Peterse63415e2001-05-08 04:38:29 +00001540 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Tim Petersafb6ae82001-06-04 21:00:21 +00001541 for (i = 0; i <= a->ma_mask; i++) {
Tim Peterse63415e2001-05-08 04:38:29 +00001542 PyObject *aval = a->ma_table[i].me_value;
1543 if (aval != NULL) {
1544 int cmp;
1545 PyObject *bval;
1546 PyObject *key = a->ma_table[i].me_key;
1547 /* temporarily bump aval's refcount to ensure it stays
1548 alive until we're done with it */
1549 Py_INCREF(aval);
Guido van Rossumdc5f6b22006-08-24 21:29:26 +00001550 /* ditto for key */
1551 Py_INCREF(key);
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001552 bval = PyDict_GetItemWithError((PyObject *)b, key);
Guido van Rossumdc5f6b22006-08-24 21:29:26 +00001553 Py_DECREF(key);
Tim Peterse63415e2001-05-08 04:38:29 +00001554 if (bval == NULL) {
1555 Py_DECREF(aval);
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001556 if (PyErr_Occurred())
1557 return -1;
Tim Peterse63415e2001-05-08 04:38:29 +00001558 return 0;
1559 }
1560 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1561 Py_DECREF(aval);
1562 if (cmp <= 0) /* error or not equal */
1563 return cmp;
1564 }
1565 }
1566 return 1;
1567 }
1568
1569static PyObject *
1570dict_richcompare(PyObject *v, PyObject *w, int op)
1571{
1572 int cmp;
1573 PyObject *res;
1574
1575 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1576 res = Py_NotImplemented;
1577 }
1578 else if (op == Py_EQ || op == Py_NE) {
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001579 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
Tim Peterse63415e2001-05-08 04:38:29 +00001580 if (cmp < 0)
1581 return NULL;
1582 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1583 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001584 else
1585 res = Py_NotImplemented;
Tim Peterse63415e2001-05-08 04:38:29 +00001586 Py_INCREF(res);
1587 return res;
1588 }
1589
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001590static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001591dict_contains(register PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001592{
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001593 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001594 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001595
Guido van Rossum89d8c602007-09-18 17:26:56 +00001596 if (!PyUnicode_CheckExact(key) ||
1597 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001598 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001599 if (hash == -1)
1600 return NULL;
1601 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001602 ep = (mp->ma_lookup)(mp, key, hash);
1603 if (ep == NULL)
1604 return NULL;
1605 return PyBool_FromLong(ep->me_value != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001606}
1607
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001608static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001609dict_get(register PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001610{
1611 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001612 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001613 PyObject *val = NULL;
1614 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001615 PyDictEntry *ep;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001616
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001617 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001618 return NULL;
1619
Guido van Rossum89d8c602007-09-18 17:26:56 +00001620 if (!PyUnicode_CheckExact(key) ||
1621 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Barry Warsawc38c5da1997-10-06 17:49:20 +00001622 hash = PyObject_Hash(key);
1623 if (hash == -1)
1624 return NULL;
1625 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001626 ep = (mp->ma_lookup)(mp, key, hash);
1627 if (ep == NULL)
1628 return NULL;
1629 val = ep->me_value;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001630 if (val == NULL)
1631 val = failobj;
1632 Py_INCREF(val);
1633 return val;
1634}
1635
1636
1637static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001638dict_setdefault(register PyDictObject *mp, PyObject *args)
Guido van Rossum164452c2000-08-08 16:12:54 +00001639{
1640 PyObject *key;
1641 PyObject *failobj = Py_None;
1642 PyObject *val = NULL;
1643 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001644 PyDictEntry *ep;
Guido van Rossum164452c2000-08-08 16:12:54 +00001645
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001646 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
Guido van Rossum164452c2000-08-08 16:12:54 +00001647 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001648
Guido van Rossum89d8c602007-09-18 17:26:56 +00001649 if (!PyUnicode_CheckExact(key) ||
1650 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossum164452c2000-08-08 16:12:54 +00001651 hash = PyObject_Hash(key);
1652 if (hash == -1)
1653 return NULL;
1654 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001655 ep = (mp->ma_lookup)(mp, key, hash);
1656 if (ep == NULL)
1657 return NULL;
1658 val = ep->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001659 if (val == NULL) {
1660 val = failobj;
1661 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1662 val = NULL;
1663 }
1664 Py_XINCREF(val);
1665 return val;
1666}
1667
1668
1669static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001670dict_clear(register PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001671{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001672 PyDict_Clear((PyObject *)mp);
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001673 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001674}
1675
Guido van Rossumba6ab842000-12-12 22:02:18 +00001676static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001677dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00001678{
1679 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001680 PyDictEntry *ep;
Guido van Rossume027d982002-04-12 15:11:59 +00001681 PyObject *old_value, *old_key;
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001682 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001683
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001684 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1685 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001686 if (mp->ma_used == 0) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001687 if (deflt) {
1688 Py_INCREF(deflt);
1689 return deflt;
1690 }
Guido van Rossume027d982002-04-12 15:11:59 +00001691 PyErr_SetString(PyExc_KeyError,
1692 "pop(): dictionary is empty");
1693 return NULL;
1694 }
Guido van Rossum89d8c602007-09-18 17:26:56 +00001695 if (!PyUnicode_CheckExact(key) ||
1696 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossume027d982002-04-12 15:11:59 +00001697 hash = PyObject_Hash(key);
1698 if (hash == -1)
1699 return NULL;
1700 }
1701 ep = (mp->ma_lookup)(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001702 if (ep == NULL)
1703 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001704 if (ep->me_value == NULL) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001705 if (deflt) {
1706 Py_INCREF(deflt);
1707 return deflt;
1708 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001709 set_key_error(key);
Guido van Rossume027d982002-04-12 15:11:59 +00001710 return NULL;
1711 }
1712 old_key = ep->me_key;
1713 Py_INCREF(dummy);
1714 ep->me_key = dummy;
1715 old_value = ep->me_value;
1716 ep->me_value = NULL;
1717 mp->ma_used--;
1718 Py_DECREF(old_key);
1719 return old_value;
1720}
1721
1722static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001723dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001724{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001725 Py_ssize_t i = 0;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001726 PyDictEntry *ep;
Guido van Rossumba6ab842000-12-12 22:02:18 +00001727 PyObject *res;
1728
Tim Petersf4b33f62001-06-02 05:42:29 +00001729 /* Allocate the result tuple before checking the size. Believe it
1730 * or not, this allocation could trigger a garbage collection which
1731 * could empty the dict, so if we checked the size first and that
1732 * happened, the result would be an infinite loop (searching for an
1733 * entry that no longer exists). Note that the usual popitem()
1734 * idiom is "while d: k, v = d.popitem()". so needing to throw the
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001735 * tuple away if the dict *is* empty isn't a significant
Tim Petersf4b33f62001-06-02 05:42:29 +00001736 * inefficiency -- possible, but unlikely in practice.
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001737 */
1738 res = PyTuple_New(2);
1739 if (res == NULL)
1740 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001741 if (mp->ma_used == 0) {
1742 Py_DECREF(res);
1743 PyErr_SetString(PyExc_KeyError,
1744 "popitem(): dictionary is empty");
1745 return NULL;
1746 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001747 /* Set ep to "the first" dict entry with a value. We abuse the hash
1748 * field of slot 0 to hold a search finger:
1749 * If slot 0 has a value, use slot 0.
1750 * Else slot 0 is being used to hold a search finger,
1751 * and we use its hash value as the first index to look.
1752 */
1753 ep = &mp->ma_table[0];
1754 if (ep->me_value == NULL) {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001755 i = ep->me_hash;
Tim Petersdea48ec2001-05-22 20:40:22 +00001756 /* The hash field may be a real hash value, or it may be a
1757 * legit search finger, or it may be a once-legit search
1758 * finger that's out of bounds now because it wrapped around
1759 * or the table shrunk -- simply make sure it's in bounds now.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001760 */
Tim Petersafb6ae82001-06-04 21:00:21 +00001761 if (i > mp->ma_mask || i < 1)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001762 i = 1; /* skip slot 0 */
1763 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1764 i++;
Tim Petersafb6ae82001-06-04 21:00:21 +00001765 if (i > mp->ma_mask)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001766 i = 1;
1767 }
1768 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001769 PyTuple_SET_ITEM(res, 0, ep->me_key);
1770 PyTuple_SET_ITEM(res, 1, ep->me_value);
1771 Py_INCREF(dummy);
1772 ep->me_key = dummy;
1773 ep->me_value = NULL;
1774 mp->ma_used--;
1775 assert(mp->ma_table[0].me_value == NULL);
1776 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001777 return res;
1778}
1779
Jeremy Hylton8caad492000-06-23 14:18:11 +00001780static int
1781dict_traverse(PyObject *op, visitproc visit, void *arg)
1782{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001783 Py_ssize_t i = 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001784 PyObject *pk;
1785 PyObject *pv;
1786
1787 while (PyDict_Next(op, &i, &pk, &pv)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001788 Py_VISIT(pk);
1789 Py_VISIT(pv);
Jeremy Hylton8caad492000-06-23 14:18:11 +00001790 }
1791 return 0;
1792}
1793
1794static int
1795dict_tp_clear(PyObject *op)
1796{
1797 PyDict_Clear(op);
1798 return 0;
1799}
1800
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001801static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001802
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001803PyDoc_STRVAR(contains__doc__,
1804"D.__contains__(k) -> True if D has a key k, else False");
1805
1806PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
1807
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001808PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001809"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001810
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001811PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001812"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 +00001813
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001814PyDoc_STRVAR(pop__doc__,
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001815"D.pop(k[,d]) -> v, remove specified key and return the corresponding value\n\
1816If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00001817
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001818PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00001819"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000018202-tuple; but raise KeyError if D is empty");
Tim Petersf7f88b12000-12-13 23:18:45 +00001821
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001822PyDoc_STRVAR(update__doc__,
Guido van Rossumb90c8482007-02-10 01:11:45 +00001823"D.update(E, **F) -> None. Update D from E and F: for k in E: D[k] = E[k]\
1824\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 +00001825
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001826PyDoc_STRVAR(fromkeys__doc__,
1827"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
1828v defaults to None.");
1829
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001830PyDoc_STRVAR(clear__doc__,
1831"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001832
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001833PyDoc_STRVAR(copy__doc__,
1834"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00001835
Guido van Rossumb90c8482007-02-10 01:11:45 +00001836/* Forward */
1837static PyObject *dictkeys_new(PyObject *);
1838static PyObject *dictitems_new(PyObject *);
1839static PyObject *dictvalues_new(PyObject *);
1840
Guido van Rossum45c85d12007-07-27 16:31:40 +00001841PyDoc_STRVAR(keys__doc__,
1842 "D.keys() -> a set-like object providing a view on D's keys");
1843PyDoc_STRVAR(items__doc__,
1844 "D.items() -> a set-like object providing a view on D's items");
1845PyDoc_STRVAR(values__doc__,
1846 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00001847
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001848static PyMethodDef mapp_methods[] = {
Guido van Rossume2b70bc2006-08-18 22:13:04 +00001849 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001850 contains__doc__},
Raymond Hettinger0c669672003-12-13 13:31:55 +00001851 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001852 getitem__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00001853 {"get", (PyCFunction)dict_get, METH_VARARGS,
1854 get__doc__},
1855 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
1856 setdefault_doc__},
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001857 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
Just van Rossuma797d812002-11-23 09:45:04 +00001858 pop__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001859 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001860 popitem__doc__},
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001861 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
Guido van Rossumeff072c2007-02-27 05:47:18 +00001862 keys__doc__},
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001863 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
Guido van Rossumeff072c2007-02-27 05:47:18 +00001864 items__doc__},
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001865 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
Guido van Rossumeff072c2007-02-27 05:47:18 +00001866 values__doc__},
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001867 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001868 update__doc__},
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001869 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
1870 fromkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001871 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001872 clear__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001873 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001874 copy__doc__},
1875 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001876};
1877
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001878/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001879int
1880PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001881{
1882 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001883 PyDictObject *mp = (PyDictObject *)op;
1884 PyDictEntry *ep;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001885
Guido van Rossum89d8c602007-09-18 17:26:56 +00001886 if (!PyUnicode_CheckExact(key) ||
1887 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001888 hash = PyObject_Hash(key);
1889 if (hash == -1)
1890 return -1;
1891 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001892 ep = (mp->ma_lookup)(mp, key, hash);
1893 return ep == NULL ? -1 : (ep->me_value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001894}
1895
Thomas Wouterscf297e42007-02-23 15:07:44 +00001896/* Internal version of PyDict_Contains used when the hash value is already known */
1897int
1898_PyDict_Contains(PyObject *op, PyObject *key, long hash)
1899{
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001900 PyDictObject *mp = (PyDictObject *)op;
1901 PyDictEntry *ep;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001902
1903 ep = (mp->ma_lookup)(mp, key, hash);
1904 return ep == NULL ? -1 : (ep->me_value != NULL);
1905}
1906
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001907/* Hack to implement "key in dict" */
1908static PySequenceMethods dict_as_sequence = {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001909 0, /* sq_length */
1910 0, /* sq_concat */
1911 0, /* sq_repeat */
1912 0, /* sq_item */
1913 0, /* sq_slice */
1914 0, /* sq_ass_item */
1915 0, /* sq_ass_slice */
1916 PyDict_Contains, /* sq_contains */
1917 0, /* sq_inplace_concat */
1918 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001919};
1920
Guido van Rossum09e563a2001-05-01 12:10:21 +00001921static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001922dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1923{
1924 PyObject *self;
1925
1926 assert(type != NULL && type->tp_alloc != NULL);
1927 self = type->tp_alloc(type, 0);
1928 if (self != NULL) {
1929 PyDictObject *d = (PyDictObject *)self;
1930 /* It's guaranteed that tp->alloc zeroed out the struct. */
1931 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
1932 INIT_NONZERO_DICT_SLOTS(d);
Guido van Rossum89d8c602007-09-18 17:26:56 +00001933 d->ma_lookup = lookdict_unicode;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001934#ifdef SHOW_CONVERSION_COUNTS
1935 ++created;
1936#endif
1937 }
1938 return self;
1939}
1940
Tim Peters25786c02001-09-02 08:22:48 +00001941static int
1942dict_init(PyObject *self, PyObject *args, PyObject *kwds)
1943{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001944 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00001945}
1946
Tim Peters6d6c1a32001-08-02 04:15:00 +00001947static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001948dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001949{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001950 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001951}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001952
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001953PyDoc_STRVAR(dictionary_doc,
Tim Petersa427a2b2001-10-29 22:25:45 +00001954"dict() -> new empty dictionary.\n"
1955"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Tim Peters1fc240e2001-10-26 05:06:50 +00001956" (key, value) pairs.\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00001957"dict(seq) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00001958" d = {}\n"
1959" for k, v in seq:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00001960" d[k] = v\n"
1961"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
1962" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00001963
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001964PyTypeObject PyDict_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001965 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Tim Petersa427a2b2001-10-29 22:25:45 +00001966 "dict",
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001967 sizeof(PyDictObject),
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001968 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00001969 (destructor)dict_dealloc, /* tp_dealloc */
Guido van Rossum04dbf3b2007-08-07 19:51:00 +00001970 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001971 0, /* tp_getattr */
Guido van Rossumb9324202001-01-18 00:39:02 +00001972 0, /* tp_setattr */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001973 0, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00001974 (reprfunc)dict_repr, /* tp_repr */
1975 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001976 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00001977 &dict_as_mapping, /* tp_as_mapping */
Guido van Rossum50e9fb92006-08-17 05:42:55 +00001978 0, /* tp_hash */
Guido van Rossumb9324202001-01-18 00:39:02 +00001979 0, /* tp_call */
1980 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001981 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumb9324202001-01-18 00:39:02 +00001982 0, /* tp_setattro */
1983 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00001984 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Thomas Wouters27d517b2007-02-25 20:39:11 +00001985 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
Tim Peters25786c02001-09-02 08:22:48 +00001986 dictionary_doc, /* tp_doc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001987 dict_traverse, /* tp_traverse */
1988 dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00001989 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001990 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00001991 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001992 0, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001993 mapp_methods, /* tp_methods */
1994 0, /* tp_members */
1995 0, /* tp_getset */
1996 0, /* tp_base */
1997 0, /* tp_dict */
1998 0, /* tp_descr_get */
1999 0, /* tp_descr_set */
2000 0, /* tp_dictoffset */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002001 dict_init, /* tp_init */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002002 PyType_GenericAlloc, /* tp_alloc */
2003 dict_new, /* tp_new */
Neil Schemenauer6189b892002-04-12 02:43:00 +00002004 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002005};
2006
Guido van Rossum3cca2451997-05-16 14:23:33 +00002007/* For backward compatibility with old dictionary interface */
2008
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002009PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002010PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002011{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002012 PyObject *kv, *rv;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002013 kv = PyUnicode_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00002014 if (kv == NULL)
2015 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002016 rv = PyDict_GetItem(v, kv);
2017 Py_DECREF(kv);
2018 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002019}
2020
2021int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002022PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002023{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002024 PyObject *kv;
2025 int err;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002026 kv = PyUnicode_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00002027 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002028 return -1;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002029 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00002030 err = PyDict_SetItem(v, kv, item);
2031 Py_DECREF(kv);
2032 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002033}
2034
2035int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002036PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002037{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002038 PyObject *kv;
2039 int err;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002040 kv = PyUnicode_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00002041 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002042 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002043 err = PyDict_DelItem(v, kv);
2044 Py_DECREF(kv);
2045 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002046}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002047
Raymond Hettinger019a1482004-03-18 02:41:19 +00002048/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002049
2050typedef struct {
2051 PyObject_HEAD
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002052 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002053 Py_ssize_t di_used;
2054 Py_ssize_t di_pos;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002055 PyObject* di_result; /* reusable result tuple for iteritems */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002056 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002057} dictiterobject;
2058
2059static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002060dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002061{
2062 dictiterobject *di;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002063 di = PyObject_New(dictiterobject, itertype);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002064 if (di == NULL)
2065 return NULL;
2066 Py_INCREF(dict);
2067 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00002068 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002069 di->di_pos = 0;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002070 di->len = dict->ma_used;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002071 if (itertype == &PyDictIterItem_Type) {
2072 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2073 if (di->di_result == NULL) {
2074 Py_DECREF(di);
2075 return NULL;
2076 }
2077 }
2078 else
2079 di->di_result = NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002080 return (PyObject *)di;
2081}
2082
2083static void
2084dictiter_dealloc(dictiterobject *di)
2085{
Guido van Rossum2147df72002-07-16 20:30:22 +00002086 Py_XDECREF(di->di_dict);
Raymond Hettinger019a1482004-03-18 02:41:19 +00002087 Py_XDECREF(di->di_result);
Neil Schemenauer6189b892002-04-12 02:43:00 +00002088 PyObject_Del(di);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002089}
2090
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002091static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002092dictiter_len(dictiterobject *di)
2093{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002094 Py_ssize_t len = 0;
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00002095 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002096 len = di->len;
Christian Heimes217cfd12007-12-02 14:31:20 +00002097 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002098}
2099
Guido van Rossumb90c8482007-02-10 01:11:45 +00002100PyDoc_STRVAR(length_hint_doc,
2101 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002102
2103static PyMethodDef dictiter_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002104 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
2105 length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002106 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002107};
2108
Raymond Hettinger019a1482004-03-18 02:41:19 +00002109static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002110{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002111 PyObject *key;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002112 register Py_ssize_t i, mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002113 register PyDictEntry *ep;
2114 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002115
Raymond Hettinger019a1482004-03-18 02:41:19 +00002116 if (d == NULL)
Guido van Rossum2147df72002-07-16 20:30:22 +00002117 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002118 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002119
Raymond Hettinger019a1482004-03-18 02:41:19 +00002120 if (di->di_used != d->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00002121 PyErr_SetString(PyExc_RuntimeError,
2122 "dictionary changed size during iteration");
Guido van Rossum2147df72002-07-16 20:30:22 +00002123 di->di_used = -1; /* Make this state sticky */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002124 return NULL;
2125 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002126
Raymond Hettinger019a1482004-03-18 02:41:19 +00002127 i = di->di_pos;
2128 if (i < 0)
2129 goto fail;
2130 ep = d->ma_table;
2131 mask = d->ma_mask;
2132 while (i <= mask && ep[i].me_value == NULL)
2133 i++;
2134 di->di_pos = i+1;
2135 if (i > mask)
2136 goto fail;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002137 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002138 key = ep[i].me_key;
2139 Py_INCREF(key);
2140 return key;
2141
2142fail:
2143 Py_DECREF(d);
Guido van Rossum2147df72002-07-16 20:30:22 +00002144 di->di_dict = NULL;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002145 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002146}
2147
Raymond Hettinger019a1482004-03-18 02:41:19 +00002148PyTypeObject PyDictIterKey_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002149 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Christian Heimesf83be4e2007-11-28 09:44:38 +00002150 "dict_keyiterator", /* tp_name */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002151 sizeof(dictiterobject), /* tp_basicsize */
2152 0, /* tp_itemsize */
2153 /* methods */
2154 (destructor)dictiter_dealloc, /* tp_dealloc */
2155 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002156 0, /* tp_getattr */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002157 0, /* tp_setattr */
2158 0, /* tp_compare */
2159 0, /* tp_repr */
2160 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002161 0, /* tp_as_sequence */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002162 0, /* tp_as_mapping */
2163 0, /* tp_hash */
2164 0, /* tp_call */
2165 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002166 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002167 0, /* tp_setattro */
2168 0, /* tp_as_buffer */
2169 Py_TPFLAGS_DEFAULT, /* tp_flags */
2170 0, /* tp_doc */
2171 0, /* tp_traverse */
2172 0, /* tp_clear */
2173 0, /* tp_richcompare */
2174 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002175 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002176 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002177 dictiter_methods, /* tp_methods */
2178 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002179};
2180
2181static PyObject *dictiter_iternextvalue(dictiterobject *di)
2182{
2183 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002184 register Py_ssize_t i, mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002185 register PyDictEntry *ep;
2186 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002187
2188 if (d == NULL)
2189 return NULL;
2190 assert (PyDict_Check(d));
2191
2192 if (di->di_used != d->ma_used) {
2193 PyErr_SetString(PyExc_RuntimeError,
2194 "dictionary changed size during iteration");
2195 di->di_used = -1; /* Make this state sticky */
2196 return NULL;
2197 }
2198
2199 i = di->di_pos;
Guido van Rossum09240f62004-03-20 19:11:58 +00002200 mask = d->ma_mask;
2201 if (i < 0 || i > mask)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002202 goto fail;
2203 ep = d->ma_table;
Guido van Rossum09240f62004-03-20 19:11:58 +00002204 while ((value=ep[i].me_value) == NULL) {
Raymond Hettinger019a1482004-03-18 02:41:19 +00002205 i++;
Guido van Rossum09240f62004-03-20 19:11:58 +00002206 if (i > mask)
2207 goto fail;
2208 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002209 di->di_pos = i+1;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002210 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002211 Py_INCREF(value);
2212 return value;
2213
2214fail:
2215 Py_DECREF(d);
2216 di->di_dict = NULL;
2217 return NULL;
2218}
2219
2220PyTypeObject PyDictIterValue_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002221 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Christian Heimesf83be4e2007-11-28 09:44:38 +00002222 "dict_valueiterator", /* tp_name */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002223 sizeof(dictiterobject), /* tp_basicsize */
2224 0, /* tp_itemsize */
2225 /* methods */
2226 (destructor)dictiter_dealloc, /* tp_dealloc */
2227 0, /* tp_print */
2228 0, /* tp_getattr */
2229 0, /* tp_setattr */
2230 0, /* tp_compare */
2231 0, /* tp_repr */
2232 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002233 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002234 0, /* tp_as_mapping */
2235 0, /* tp_hash */
2236 0, /* tp_call */
2237 0, /* tp_str */
2238 PyObject_GenericGetAttr, /* tp_getattro */
2239 0, /* tp_setattro */
2240 0, /* tp_as_buffer */
2241 Py_TPFLAGS_DEFAULT, /* tp_flags */
2242 0, /* tp_doc */
2243 0, /* tp_traverse */
2244 0, /* tp_clear */
2245 0, /* tp_richcompare */
2246 0, /* tp_weaklistoffset */
2247 PyObject_SelfIter, /* tp_iter */
2248 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002249 dictiter_methods, /* tp_methods */
2250 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002251};
2252
2253static PyObject *dictiter_iternextitem(dictiterobject *di)
2254{
2255 PyObject *key, *value, *result = di->di_result;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002256 register Py_ssize_t i, mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002257 register PyDictEntry *ep;
2258 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002259
2260 if (d == NULL)
2261 return NULL;
2262 assert (PyDict_Check(d));
2263
2264 if (di->di_used != d->ma_used) {
2265 PyErr_SetString(PyExc_RuntimeError,
2266 "dictionary changed size during iteration");
2267 di->di_used = -1; /* Make this state sticky */
2268 return NULL;
2269 }
2270
2271 i = di->di_pos;
2272 if (i < 0)
2273 goto fail;
2274 ep = d->ma_table;
2275 mask = d->ma_mask;
2276 while (i <= mask && ep[i].me_value == NULL)
2277 i++;
2278 di->di_pos = i+1;
2279 if (i > mask)
2280 goto fail;
2281
2282 if (result->ob_refcnt == 1) {
2283 Py_INCREF(result);
2284 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2285 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2286 } else {
2287 result = PyTuple_New(2);
Tim Peters60b29962006-01-01 01:19:23 +00002288 if (result == NULL)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002289 return NULL;
2290 }
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002291 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002292 key = ep[i].me_key;
2293 value = ep[i].me_value;
2294 Py_INCREF(key);
2295 Py_INCREF(value);
2296 PyTuple_SET_ITEM(result, 0, key);
2297 PyTuple_SET_ITEM(result, 1, value);
2298 return result;
2299
2300fail:
2301 Py_DECREF(d);
2302 di->di_dict = NULL;
2303 return NULL;
2304}
2305
2306PyTypeObject PyDictIterItem_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002307 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Christian Heimesf83be4e2007-11-28 09:44:38 +00002308 "dict_itemiterator", /* tp_name */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002309 sizeof(dictiterobject), /* tp_basicsize */
2310 0, /* tp_itemsize */
2311 /* methods */
2312 (destructor)dictiter_dealloc, /* tp_dealloc */
2313 0, /* tp_print */
2314 0, /* tp_getattr */
2315 0, /* tp_setattr */
2316 0, /* tp_compare */
2317 0, /* tp_repr */
2318 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002319 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002320 0, /* tp_as_mapping */
2321 0, /* tp_hash */
2322 0, /* tp_call */
2323 0, /* tp_str */
2324 PyObject_GenericGetAttr, /* tp_getattro */
2325 0, /* tp_setattro */
2326 0, /* tp_as_buffer */
2327 Py_TPFLAGS_DEFAULT, /* tp_flags */
2328 0, /* tp_doc */
2329 0, /* tp_traverse */
2330 0, /* tp_clear */
2331 0, /* tp_richcompare */
2332 0, /* tp_weaklistoffset */
2333 PyObject_SelfIter, /* tp_iter */
2334 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002335 dictiter_methods, /* tp_methods */
2336 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002337};
Guido van Rossumb90c8482007-02-10 01:11:45 +00002338
2339
Guido van Rossum3ac67412007-02-10 18:55:06 +00002340/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002341/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002342/***********************************************/
2343
Guido van Rossumb90c8482007-02-10 01:11:45 +00002344/* The instance lay-out is the same for all three; but the type differs. */
2345
2346typedef struct {
2347 PyObject_HEAD
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002348 PyDictObject *dv_dict;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002349} dictviewobject;
2350
2351
2352static void
Guido van Rossum3ac67412007-02-10 18:55:06 +00002353dictview_dealloc(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002354{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002355 Py_XDECREF(dv->dv_dict);
2356 PyObject_Del(dv);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002357}
2358
Guido van Rossum83825ac2007-02-10 04:54:19 +00002359static Py_ssize_t
Guido van Rossum3ac67412007-02-10 18:55:06 +00002360dictview_len(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002361{
2362 Py_ssize_t len = 0;
Guido van Rossum3ac67412007-02-10 18:55:06 +00002363 if (dv->dv_dict != NULL)
2364 len = dv->dv_dict->ma_used;
Guido van Rossum83825ac2007-02-10 04:54:19 +00002365 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002366}
2367
2368static PyObject *
2369dictview_new(PyObject *dict, PyTypeObject *type)
2370{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002371 dictviewobject *dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002372 if (dict == NULL) {
2373 PyErr_BadInternalCall();
2374 return NULL;
2375 }
2376 if (!PyDict_Check(dict)) {
2377 /* XXX Get rid of this restriction later */
2378 PyErr_Format(PyExc_TypeError,
2379 "%s() requires a dict argument, not '%s'",
2380 type->tp_name, dict->ob_type->tp_name);
2381 return NULL;
2382 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002383 dv = PyObject_New(dictviewobject, type);
2384 if (dv == NULL)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002385 return NULL;
2386 Py_INCREF(dict);
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002387 dv->dv_dict = (PyDictObject *)dict;
Guido van Rossum3ac67412007-02-10 18:55:06 +00002388 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002389}
2390
Neal Norwitze36f2ba2007-02-26 23:12:28 +00002391/* TODO(guido): The views objects are not complete:
2392
2393 * support more set operations
2394 * support arbitrary mappings?
2395 - either these should be static or exported in dictobject.h
2396 - if public then they should probably be in builtins
2397*/
2398
Guido van Rossumaac530c2007-08-24 22:33:45 +00002399/* Return 1 if self is a subset of other, iterating over self;
2400 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002401static int
2402all_contained_in(PyObject *self, PyObject *other)
2403{
2404 PyObject *iter = PyObject_GetIter(self);
2405 int ok = 1;
2406
2407 if (iter == NULL)
2408 return -1;
2409 for (;;) {
2410 PyObject *next = PyIter_Next(iter);
2411 if (next == NULL) {
2412 if (PyErr_Occurred())
2413 ok = -1;
2414 break;
2415 }
2416 ok = PySequence_Contains(other, next);
2417 Py_DECREF(next);
2418 if (ok <= 0)
2419 break;
2420 }
2421 Py_DECREF(iter);
2422 return ok;
2423}
2424
2425static PyObject *
2426dictview_richcompare(PyObject *self, PyObject *other, int op)
2427{
Guido van Rossumaac530c2007-08-24 22:33:45 +00002428 Py_ssize_t len_self, len_other;
2429 int ok;
2430 PyObject *result;
2431
Guido van Rossumd9214d12007-02-12 02:23:40 +00002432 assert(self != NULL);
2433 assert(PyDictViewSet_Check(self));
2434 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00002435
Guido van Rossumaac530c2007-08-24 22:33:45 +00002436 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other)) {
Guido van Rossumd9214d12007-02-12 02:23:40 +00002437 Py_INCREF(Py_NotImplemented);
2438 return Py_NotImplemented;
2439 }
Guido van Rossumaac530c2007-08-24 22:33:45 +00002440
2441 len_self = PyObject_Size(self);
2442 if (len_self < 0)
2443 return NULL;
2444 len_other = PyObject_Size(other);
2445 if (len_other < 0)
2446 return NULL;
2447
2448 ok = 0;
2449 switch(op) {
2450
2451 case Py_NE:
2452 case Py_EQ:
2453 if (len_self == len_other)
2454 ok = all_contained_in(self, other);
2455 if (op == Py_NE && ok >= 0)
2456 ok = !ok;
2457 break;
2458
2459 case Py_LT:
2460 if (len_self < len_other)
2461 ok = all_contained_in(self, other);
2462 break;
2463
2464 case Py_LE:
2465 if (len_self <= len_other)
2466 ok = all_contained_in(self, other);
2467 break;
2468
2469 case Py_GT:
2470 if (len_self > len_other)
2471 ok = all_contained_in(other, self);
2472 break;
2473
2474 case Py_GE:
2475 if (len_self >= len_other)
2476 ok = all_contained_in(other, self);
2477 break;
2478
2479 }
2480 if (ok < 0)
2481 return NULL;
2482 result = ok ? Py_True : Py_False;
2483 Py_INCREF(result);
2484 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00002485}
2486
Guido van Rossum3ac67412007-02-10 18:55:06 +00002487/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002488
2489static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002490dictkeys_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002491{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002492 if (dv->dv_dict == NULL) {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002493 Py_RETURN_NONE;
2494 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002495 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
2496}
2497
2498static int
2499dictkeys_contains(dictviewobject *dv, PyObject *obj)
2500{
2501 if (dv->dv_dict == NULL)
2502 return 0;
2503 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002504}
2505
Guido van Rossum83825ac2007-02-10 04:54:19 +00002506static PySequenceMethods dictkeys_as_sequence = {
2507 (lenfunc)dictview_len, /* sq_length */
2508 0, /* sq_concat */
2509 0, /* sq_repeat */
2510 0, /* sq_item */
2511 0, /* sq_slice */
2512 0, /* sq_ass_item */
2513 0, /* sq_ass_slice */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002514 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002515};
2516
Guido van Rossum523259b2007-08-24 23:41:22 +00002517static PyObject*
2518dictviews_sub(PyObject* self, PyObject *other)
2519{
2520 PyObject *result = PySet_New(self);
2521 PyObject *tmp;
2522 if (result == NULL)
2523 return NULL;
2524
2525 tmp = PyObject_CallMethod(result, "difference_update", "O", other);
2526 if (tmp == NULL) {
2527 Py_DECREF(result);
2528 return NULL;
2529 }
2530
2531 Py_DECREF(tmp);
2532 return result;
2533}
2534
2535static PyObject*
2536dictviews_and(PyObject* self, PyObject *other)
2537{
2538 PyObject *result = PySet_New(self);
2539 PyObject *tmp;
2540 if (result == NULL)
2541 return NULL;
2542
2543 tmp = PyObject_CallMethod(result, "intersection_update", "O", other);
2544 if (tmp == NULL) {
2545 Py_DECREF(result);
2546 return NULL;
2547 }
2548
2549 Py_DECREF(tmp);
2550 return result;
2551}
2552
2553static PyObject*
2554dictviews_or(PyObject* self, PyObject *other)
2555{
2556 PyObject *result = PySet_New(self);
2557 PyObject *tmp;
2558 if (result == NULL)
2559 return NULL;
2560
2561 tmp = PyObject_CallMethod(result, "update", "O", other);
2562 if (tmp == NULL) {
2563 Py_DECREF(result);
2564 return NULL;
2565 }
2566
2567 Py_DECREF(tmp);
2568 return result;
2569}
2570
2571static PyObject*
2572dictviews_xor(PyObject* self, PyObject *other)
2573{
2574 PyObject *result = PySet_New(self);
2575 PyObject *tmp;
2576 if (result == NULL)
2577 return NULL;
2578
2579 tmp = PyObject_CallMethod(result, "symmetric_difference_update", "O",
2580 other);
2581 if (tmp == NULL) {
2582 Py_DECREF(result);
2583 return NULL;
2584 }
2585
2586 Py_DECREF(tmp);
2587 return result;
2588}
2589
2590static PyNumberMethods dictviews_as_number = {
2591 0, /*nb_add*/
2592 (binaryfunc)dictviews_sub, /*nb_subtract*/
2593 0, /*nb_multiply*/
2594 0, /*nb_remainder*/
2595 0, /*nb_divmod*/
2596 0, /*nb_power*/
2597 0, /*nb_negative*/
2598 0, /*nb_positive*/
2599 0, /*nb_absolute*/
2600 0, /*nb_bool*/
2601 0, /*nb_invert*/
2602 0, /*nb_lshift*/
2603 0, /*nb_rshift*/
2604 (binaryfunc)dictviews_and, /*nb_and*/
2605 (binaryfunc)dictviews_xor, /*nb_xor*/
2606 (binaryfunc)dictviews_or, /*nb_or*/
2607};
2608
Guido van Rossumb90c8482007-02-10 01:11:45 +00002609static PyMethodDef dictkeys_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002610 {NULL, NULL} /* sentinel */
2611};
2612
2613PyTypeObject PyDictKeys_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002614 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002615 "dict_keys", /* tp_name */
2616 sizeof(dictviewobject), /* tp_basicsize */
2617 0, /* tp_itemsize */
2618 /* methods */
2619 (destructor)dictview_dealloc, /* tp_dealloc */
2620 0, /* tp_print */
2621 0, /* tp_getattr */
2622 0, /* tp_setattr */
2623 0, /* tp_compare */
2624 0, /* tp_repr */
Guido van Rossum523259b2007-08-24 23:41:22 +00002625 &dictviews_as_number, /* tp_as_number */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002626 &dictkeys_as_sequence, /* tp_as_sequence */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002627 0, /* tp_as_mapping */
2628 0, /* tp_hash */
2629 0, /* tp_call */
2630 0, /* tp_str */
2631 PyObject_GenericGetAttr, /* tp_getattro */
2632 0, /* tp_setattro */
2633 0, /* tp_as_buffer */
2634 Py_TPFLAGS_DEFAULT, /* tp_flags */
2635 0, /* tp_doc */
2636 0, /* tp_traverse */
2637 0, /* tp_clear */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002638 dictview_richcompare, /* tp_richcompare */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002639 0, /* tp_weaklistoffset */
2640 (getiterfunc)dictkeys_iter, /* tp_iter */
2641 0, /* tp_iternext */
2642 dictkeys_methods, /* tp_methods */
2643 0,
2644};
2645
2646static PyObject *
2647dictkeys_new(PyObject *dict)
2648{
2649 return dictview_new(dict, &PyDictKeys_Type);
2650}
2651
Guido van Rossum3ac67412007-02-10 18:55:06 +00002652/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002653
2654static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002655dictitems_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002656{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002657 if (dv->dv_dict == NULL) {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002658 Py_RETURN_NONE;
2659 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002660 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
2661}
2662
2663static int
2664dictitems_contains(dictviewobject *dv, PyObject *obj)
2665{
2666 PyObject *key, *value, *found;
2667 if (dv->dv_dict == NULL)
2668 return 0;
2669 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
2670 return 0;
2671 key = PyTuple_GET_ITEM(obj, 0);
2672 value = PyTuple_GET_ITEM(obj, 1);
2673 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
2674 if (found == NULL) {
2675 if (PyErr_Occurred())
2676 return -1;
2677 return 0;
2678 }
2679 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002680}
2681
Guido van Rossum83825ac2007-02-10 04:54:19 +00002682static PySequenceMethods dictitems_as_sequence = {
2683 (lenfunc)dictview_len, /* sq_length */
2684 0, /* sq_concat */
2685 0, /* sq_repeat */
2686 0, /* sq_item */
2687 0, /* sq_slice */
2688 0, /* sq_ass_item */
2689 0, /* sq_ass_slice */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002690 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002691};
2692
Guido van Rossumb90c8482007-02-10 01:11:45 +00002693static PyMethodDef dictitems_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002694 {NULL, NULL} /* sentinel */
2695};
2696
2697PyTypeObject PyDictItems_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002698 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002699 "dict_items", /* tp_name */
2700 sizeof(dictviewobject), /* tp_basicsize */
2701 0, /* tp_itemsize */
2702 /* methods */
2703 (destructor)dictview_dealloc, /* tp_dealloc */
2704 0, /* tp_print */
2705 0, /* tp_getattr */
2706 0, /* tp_setattr */
2707 0, /* tp_compare */
2708 0, /* tp_repr */
Guido van Rossum523259b2007-08-24 23:41:22 +00002709 &dictviews_as_number, /* tp_as_number */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002710 &dictitems_as_sequence, /* tp_as_sequence */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002711 0, /* tp_as_mapping */
2712 0, /* tp_hash */
2713 0, /* tp_call */
2714 0, /* tp_str */
2715 PyObject_GenericGetAttr, /* tp_getattro */
2716 0, /* tp_setattro */
2717 0, /* tp_as_buffer */
2718 Py_TPFLAGS_DEFAULT, /* tp_flags */
2719 0, /* tp_doc */
2720 0, /* tp_traverse */
2721 0, /* tp_clear */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002722 dictview_richcompare, /* tp_richcompare */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002723 0, /* tp_weaklistoffset */
2724 (getiterfunc)dictitems_iter, /* tp_iter */
2725 0, /* tp_iternext */
2726 dictitems_methods, /* tp_methods */
2727 0,
2728};
2729
2730static PyObject *
2731dictitems_new(PyObject *dict)
2732{
2733 return dictview_new(dict, &PyDictItems_Type);
2734}
2735
Guido van Rossum3ac67412007-02-10 18:55:06 +00002736/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002737
2738static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002739dictvalues_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002740{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002741 if (dv->dv_dict == NULL) {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002742 Py_RETURN_NONE;
2743 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002744 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002745}
2746
Guido van Rossum83825ac2007-02-10 04:54:19 +00002747static PySequenceMethods dictvalues_as_sequence = {
2748 (lenfunc)dictview_len, /* sq_length */
2749 0, /* sq_concat */
2750 0, /* sq_repeat */
2751 0, /* sq_item */
2752 0, /* sq_slice */
2753 0, /* sq_ass_item */
2754 0, /* sq_ass_slice */
2755 (objobjproc)0, /* sq_contains */
2756};
2757
Guido van Rossumb90c8482007-02-10 01:11:45 +00002758static PyMethodDef dictvalues_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002759 {NULL, NULL} /* sentinel */
2760};
2761
2762PyTypeObject PyDictValues_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002763 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002764 "dict_values", /* tp_name */
2765 sizeof(dictviewobject), /* tp_basicsize */
2766 0, /* tp_itemsize */
2767 /* methods */
2768 (destructor)dictview_dealloc, /* tp_dealloc */
2769 0, /* tp_print */
2770 0, /* tp_getattr */
2771 0, /* tp_setattr */
2772 0, /* tp_compare */
2773 0, /* tp_repr */
2774 0, /* tp_as_number */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002775 &dictvalues_as_sequence, /* tp_as_sequence */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002776 0, /* tp_as_mapping */
2777 0, /* tp_hash */
2778 0, /* tp_call */
2779 0, /* tp_str */
2780 PyObject_GenericGetAttr, /* tp_getattro */
2781 0, /* tp_setattro */
2782 0, /* tp_as_buffer */
2783 Py_TPFLAGS_DEFAULT, /* tp_flags */
2784 0, /* tp_doc */
2785 0, /* tp_traverse */
2786 0, /* tp_clear */
2787 0, /* tp_richcompare */
2788 0, /* tp_weaklistoffset */
2789 (getiterfunc)dictvalues_iter, /* tp_iter */
2790 0, /* tp_iternext */
2791 dictvalues_methods, /* tp_methods */
2792 0,
2793};
2794
2795static PyObject *
2796dictvalues_new(PyObject *dict)
2797{
2798 return dictview_new(dict, &PyDictValues_Type);
2799}