blob: 1bc7184439d3e25195974ad8d11cefd119818519 [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"
Christian Heimes0ded5b52007-12-10 15:50:56 +000011#include "stringlib/eq.h"
Guido van Rossum4b1302b1993-03-27 18:11:32 +000012
Guido van Rossum16e93a81997-01-28 00:00:11 +000013
Thomas Wouters89f507f2006-12-13 04:49:30 +000014/* Set a key error with the specified argument, wrapping it in a
15 * tuple automatically so that tuple keys are not unpacked as the
16 * exception arguments. */
17static void
18set_key_error(PyObject *arg)
19{
20 PyObject *tup;
21 tup = PyTuple_Pack(1, arg);
22 if (!tup)
23 return; /* caller will expect error to be set anyway */
24 PyErr_SetObject(PyExc_KeyError, tup);
25 Py_DECREF(tup);
26}
27
Tim Peterseb28ef22001-06-02 05:27:19 +000028/* Define this out if you don't want conversion statistics on exit. */
Fred Drake1bff34a2000-08-31 19:31:38 +000029#undef SHOW_CONVERSION_COUNTS
30
Tim Peterseb28ef22001-06-02 05:27:19 +000031/* See large comment block below. This must be >= 1. */
32#define PERTURB_SHIFT 5
33
Guido van Rossum16e93a81997-01-28 00:00:11 +000034/*
Tim Peterseb28ef22001-06-02 05:27:19 +000035Major subtleties ahead: Most hash schemes depend on having a "good" hash
36function, in the sense of simulating randomness. Python doesn't: its most
37important hash functions (for strings and ints) are very regular in common
38cases:
Tim Peters15d49292001-05-27 07:39:22 +000039
Guido van Rossumdc5f6b22006-08-24 21:29:26 +000040 >>> map(hash, (0, 1, 2, 3))
41 [0, 1, 2, 3]
42 >>> map(hash, ("namea", "nameb", "namec", "named"))
43 [-1658398457, -1658398460, -1658398459, -1658398462]
44 >>>
Tim Peters15d49292001-05-27 07:39:22 +000045
Tim Peterseb28ef22001-06-02 05:27:19 +000046This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
47the low-order i bits as the initial table index is extremely fast, and there
48are no collisions at all for dicts indexed by a contiguous range of ints.
49The same is approximately true when keys are "consecutive" strings. So this
50gives better-than-random behavior in common cases, and that's very desirable.
Tim Peters15d49292001-05-27 07:39:22 +000051
Tim Peterseb28ef22001-06-02 05:27:19 +000052OTOH, when collisions occur, the tendency to fill contiguous slices of the
53hash table makes a good collision resolution strategy crucial. Taking only
54the last i bits of the hash code is also vulnerable: for example, consider
Guido van Rossumdc5f6b22006-08-24 21:29:26 +000055the list [i << 16 for i in range(20000)] as a set of keys. Since ints are
56their own hash codes, and this fits in a dict of size 2**15, the last 15 bits
57 of every hash code are all 0: they *all* map to the same table index.
Tim Peters15d49292001-05-27 07:39:22 +000058
Tim Peterseb28ef22001-06-02 05:27:19 +000059But catering to unusual cases should not slow the usual ones, so we just take
60the last i bits anyway. It's up to collision resolution to do the rest. If
61we *usually* find the key we're looking for on the first try (and, it turns
62out, we usually do -- the table load factor is kept under 2/3, so the odds
63are solidly in our favor), then it makes best sense to keep the initial index
64computation dirt cheap.
Tim Peters15d49292001-05-27 07:39:22 +000065
Tim Peterseb28ef22001-06-02 05:27:19 +000066The first half of collision resolution is to visit table indices via this
67recurrence:
Tim Peters15d49292001-05-27 07:39:22 +000068
Tim Peterseb28ef22001-06-02 05:27:19 +000069 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +000070
Tim Peterseb28ef22001-06-02 05:27:19 +000071For any initial j in range(2**i), repeating that 2**i times generates each
72int in range(2**i) exactly once (see any text on random-number generation for
73proof). By itself, this doesn't help much: like linear probing (setting
74j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
75order. This would be bad, except that's not the only thing we do, and it's
76actually *good* in the common cases where hash keys are consecutive. In an
77example that's really too small to make this entirely clear, for a table of
78size 2**3 the order of indices is:
Tim Peters15d49292001-05-27 07:39:22 +000079
Tim Peterseb28ef22001-06-02 05:27:19 +000080 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
81
82If two things come in at index 5, the first place we look after is index 2,
83not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
84Linear probing is deadly in this case because there the fixed probe order
85is the *same* as the order consecutive keys are likely to arrive. But it's
86extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
87and certain that consecutive hash codes do not.
88
89The other half of the strategy is to get the other bits of the hash code
90into play. This is done by initializing a (unsigned) vrbl "perturb" to the
91full hash code, and changing the recurrence to:
92
93 j = (5*j) + 1 + perturb;
94 perturb >>= PERTURB_SHIFT;
95 use j % 2**i as the next table index;
96
97Now the probe sequence depends (eventually) on every bit in the hash code,
98and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
99because it quickly magnifies small differences in the bits that didn't affect
100the initial index. Note that because perturb is unsigned, if the recurrence
101is executed often enough perturb eventually becomes and remains 0. At that
102point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
103that's certain to find an empty slot eventually (since it generates every int
104in range(2**i), and we make sure there's always at least one empty slot).
105
106Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
107small so that the high bits of the hash code continue to affect the probe
108sequence across iterations; but you want it large so that in really bad cases
109the high-order hash bits have an effect on early iterations. 5 was "the
110best" in minimizing total collisions across experiments Tim Peters ran (on
111both normal and pathological cases), but 4 and 6 weren't significantly worse.
112
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000113Historical: Reimer Behrends contributed the idea of using a polynomial-based
Tim Peterseb28ef22001-06-02 05:27:19 +0000114approach, using repeated multiplication by x in GF(2**n) where an irreducible
115polynomial for each table size was chosen such that x was a primitive root.
116Christian Tismer later extended that to use division by x instead, as an
117efficient way to get the high bits of the hash code into play. This scheme
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000118also gave excellent collision statistics, but was more expensive: two
119if-tests were required inside the loop; computing "the next" index took about
120the same number of operations but without as much potential parallelism
121(e.g., computing 5*j can go on at the same time as computing 1+perturb in the
122above, and then shifting perturb can be done while the table index is being
123masked); and the PyDictObject struct required a member to hold the table's
124polynomial. In Tim's experiments the current scheme ran faster, produced
125equally good collision statistics, needed less code & used less memory.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000126
127Theoretical Python 2.5 headache: hash codes are only C "long", but
128sizeof(Py_ssize_t) > sizeof(long) may be possible. In that case, and if a
129dict is genuinely huge, then only the slots directly reachable via indexing
130by a C long can be the first slot in a probe sequence. The probe sequence
131will still eventually reach every slot in the table, but the collision rate
132on initial probes may be much higher than this scheme was designed for.
133Getting a hash code as fat as Py_ssize_t is the only real cure. But in
134practice, this probably won't make a lick of difference for many years (at
135which point everyone will have terabytes of RAM on 64-bit boxes).
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000136*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000137
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000138/* Object used as dummy key to fill deleted entries */
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000139static PyObject *dummy = NULL; /* Initialized by first call to newPyDictObject() */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000140
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000141#ifdef Py_REF_DEBUG
142PyObject *
143_PyDict_Dummy(void)
144{
145 return dummy;
146}
147#endif
148
Fred Drake1bff34a2000-08-31 19:31:38 +0000149/* forward declarations */
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000150static PyDictEntry *
151lookdict_unicode(PyDictObject *mp, PyObject *key, long hash);
Fred Drake1bff34a2000-08-31 19:31:38 +0000152
153#ifdef SHOW_CONVERSION_COUNTS
154static long created = 0L;
155static long converted = 0L;
156
157static void
158show_counts(void)
159{
160 fprintf(stderr, "created %ld string dicts\n", created);
161 fprintf(stderr, "converted %ld to normal dicts\n", converted);
162 fprintf(stderr, "%.2f%% conversion rate\n", (100.0*converted)/created);
163}
164#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000165
Tim Peters6d6c1a32001-08-02 04:15:00 +0000166/* Initialization macros.
167 There are two ways to create a dict: PyDict_New() is the main C API
168 function, and the tp_new slot maps to dict_new(). In the latter case we
169 can save a little time over what PyDict_New does because it's guaranteed
170 that the PyDictObject struct is already zeroed out.
171 Everyone except dict_new() should use EMPTY_TO_MINSIZE (unless they have
172 an excellent reason not to).
173*/
174
175#define INIT_NONZERO_DICT_SLOTS(mp) do { \
Tim Petersdea48ec2001-05-22 20:40:22 +0000176 (mp)->ma_table = (mp)->ma_smalltable; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000177 (mp)->ma_mask = PyDict_MINSIZE - 1; \
178 } while(0)
179
180#define EMPTY_TO_MINSIZE(mp) do { \
181 memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000182 (mp)->ma_used = (mp)->ma_fill = 0; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000183 INIT_NONZERO_DICT_SLOTS(mp); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000184 } while(0)
185
Raymond Hettinger43442782004-03-17 21:55:03 +0000186/* Dictionary reuse scheme to save calls to malloc, free, and memset */
Christian Heimes2202f872008-02-06 14:31:34 +0000187#ifndef PyDict_MAXFREELIST
188#define PyDict_MAXFREELIST 80
189#endif
190static PyDictObject *free_list[PyDict_MAXFREELIST];
191static int numfree = 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000192
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000193PyObject *
Thomas Wouters78890102000-07-22 19:25:51 +0000194PyDict_New(void)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000195{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000196 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000197 if (dummy == NULL) { /* Auto-initialize dummy */
Neal Norwitz53cbdaa2007-08-23 21:42:55 +0000198 dummy = PyUnicode_FromString("<dummy key>");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000199 if (dummy == NULL)
200 return NULL;
Fred Drake1bff34a2000-08-31 19:31:38 +0000201#ifdef SHOW_CONVERSION_COUNTS
202 Py_AtExit(show_counts);
203#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000204 }
Christian Heimes2202f872008-02-06 14:31:34 +0000205 if (numfree) {
206 mp = free_list[--numfree];
Raymond Hettinger43442782004-03-17 21:55:03 +0000207 assert (mp != NULL);
Christian Heimes90aa7642007-12-19 02:45:37 +0000208 assert (Py_TYPE(mp) == &PyDict_Type);
Raymond Hettinger43442782004-03-17 21:55:03 +0000209 _Py_NewReference((PyObject *)mp);
210 if (mp->ma_fill) {
211 EMPTY_TO_MINSIZE(mp);
212 }
213 assert (mp->ma_used == 0);
214 assert (mp->ma_table == mp->ma_smalltable);
215 assert (mp->ma_mask == PyDict_MINSIZE - 1);
216 } else {
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000217 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
Raymond Hettinger43442782004-03-17 21:55:03 +0000218 if (mp == NULL)
219 return NULL;
220 EMPTY_TO_MINSIZE(mp);
221 }
Guido van Rossum89d8c602007-09-18 17:26:56 +0000222 mp->ma_lookup = lookdict_unicode;
Fred Drake1bff34a2000-08-31 19:31:38 +0000223#ifdef SHOW_CONVERSION_COUNTS
224 ++created;
225#endif
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000226 _PyObject_GC_TRACK(mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000227 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000228}
229
230/*
231The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000232This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000233Open addressing is preferred over chaining since the link overhead for
234chaining would be substantial (100% with typical malloc overhead).
235
Tim Peterseb28ef22001-06-02 05:27:19 +0000236The initial probe index is computed as hash mod the table size. Subsequent
237probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000238
239All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000240
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000241The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000242contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000243Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000244
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000245lookdict() is general-purpose, and may return NULL if (and only if) a
246comparison raises an exception (this was new in Python 2.5).
Guido van Rossum89d8c602007-09-18 17:26:56 +0000247lookdict_unicode() below is specialized to string keys, comparison of which can
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000248never raise an exception; that function can never return NULL. For both, when
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000249the key isn't found a PyDictEntry* is returned for which the me_value field is
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000250NULL; this is the slot in the dict at which the key would have been found, and
251the caller can (if it wishes) add the <key, value> pair to the returned
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000252PyDictEntry*.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000253*/
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000254static PyDictEntry *
255lookdict(PyDictObject *mp, PyObject *key, register long hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000256{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000257 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000258 register size_t perturb;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000259 register PyDictEntry *freeslot;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000260 register size_t mask = (size_t)mp->ma_mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000261 PyDictEntry *ep0 = mp->ma_table;
262 register PyDictEntry *ep;
Fred Drakec88b99c2000-08-31 19:04:07 +0000263 register int cmp;
Tim Peters453163d2001-06-03 04:54:32 +0000264 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000265
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000266 i = (size_t)hash & mask;
Guido van Rossumefb46091997-01-29 15:53:56 +0000267 ep = &ep0[i];
Guido van Rossumc1c7b1a1998-10-06 16:01:14 +0000268 if (ep->me_key == NULL || ep->me_key == key)
Guido van Rossum7d181591997-01-16 21:06:45 +0000269 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000270
Guido van Rossum16e93a81997-01-28 00:00:11 +0000271 if (ep->me_key == dummy)
Guido van Rossum7d181591997-01-16 21:06:45 +0000272 freeslot = ep;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000273 else {
Fred Drakec88b99c2000-08-31 19:04:07 +0000274 if (ep->me_hash == hash) {
Tim Peters453163d2001-06-03 04:54:32 +0000275 startkey = ep->me_key;
Guido van Rossum2fd4f372007-11-29 18:43:05 +0000276 Py_INCREF(startkey);
Tim Peters453163d2001-06-03 04:54:32 +0000277 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Guido van Rossum2fd4f372007-11-29 18:43:05 +0000278 Py_DECREF(startkey);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000279 if (cmp < 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000280 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000281 if (ep0 == mp->ma_table && ep->me_key == startkey) {
282 if (cmp > 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000283 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000284 }
285 else {
286 /* The compare did major nasty stuff to the
287 * dict: start over.
288 * XXX A clever adversary could prevent this
289 * XXX from terminating.
290 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000291 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000292 }
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000293 }
294 freeslot = NULL;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000295 }
Tim Peters15d49292001-05-27 07:39:22 +0000296
Tim Peters342c65e2001-05-13 06:43:53 +0000297 /* In the loop, me_key == dummy is by far (factor of 100s) the
298 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000299 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
300 i = (i << 2) + i + perturb + 1;
301 ep = &ep0[i & mask];
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000302 if (ep->me_key == NULL)
303 return freeslot == NULL ? ep : freeslot;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000304 if (ep->me_key == key)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000305 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000306 if (ep->me_hash == hash && ep->me_key != dummy) {
Tim Peters453163d2001-06-03 04:54:32 +0000307 startkey = ep->me_key;
Guido van Rossum2fd4f372007-11-29 18:43:05 +0000308 Py_INCREF(startkey);
Tim Peters453163d2001-06-03 04:54:32 +0000309 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Guido van Rossum2fd4f372007-11-29 18:43:05 +0000310 Py_DECREF(startkey);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000311 if (cmp < 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000312 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000313 if (ep0 == mp->ma_table && ep->me_key == startkey) {
314 if (cmp > 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000315 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000316 }
317 else {
318 /* The compare did major nasty stuff to the
319 * dict: start over.
320 * XXX A clever adversary could prevent this
321 * XXX from terminating.
322 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000323 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000324 }
Guido van Rossum16e93a81997-01-28 00:00:11 +0000325 }
Tim Peters342c65e2001-05-13 06:43:53 +0000326 else if (ep->me_key == dummy && freeslot == NULL)
327 freeslot = ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000328 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000329 assert(0); /* NOT REACHED */
330 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000331}
332
333/*
Guido van Rossum89d8c602007-09-18 17:26:56 +0000334 * Hacked up version of lookdict which can assume keys are always
335 * unicodes; this assumption allows testing for errors during
336 * PyObject_RichCompareBool() to be dropped; unicode-unicode
337 * comparisons never raise exceptions. This also means we don't need
338 * to go through PyObject_RichCompareBool(); we can always use
339 * unicode_eq() directly.
Fred Drake1bff34a2000-08-31 19:31:38 +0000340 *
Guido van Rossum89d8c602007-09-18 17:26:56 +0000341 * This is valuable because dicts with only unicode keys are very common.
Fred Drake1bff34a2000-08-31 19:31:38 +0000342 */
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000343static PyDictEntry *
344lookdict_unicode(PyDictObject *mp, PyObject *key, register long hash)
Fred Drake1bff34a2000-08-31 19:31:38 +0000345{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000346 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000347 register size_t perturb;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000348 register PyDictEntry *freeslot;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000349 register size_t mask = (size_t)mp->ma_mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000350 PyDictEntry *ep0 = mp->ma_table;
351 register PyDictEntry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000352
Guido van Rossum89d8c602007-09-18 17:26:56 +0000353 /* Make sure this function doesn't have to handle non-unicode keys,
Tim Peters0ab085c2001-09-14 00:25:33 +0000354 including subclasses of str; e.g., one reason to subclass
Guido van Rossum89d8c602007-09-18 17:26:56 +0000355 unicodes is to override __eq__, and for speed we don't cater to
Tim Peters0ab085c2001-09-14 00:25:33 +0000356 that here. */
Guido van Rossum89d8c602007-09-18 17:26:56 +0000357 if (!PyUnicode_CheckExact(key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000358#ifdef SHOW_CONVERSION_COUNTS
359 ++converted;
360#endif
361 mp->ma_lookup = lookdict;
362 return lookdict(mp, key, hash);
363 }
Tim Peters2f228e72001-05-13 00:19:31 +0000364 i = hash & mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000365 ep = &ep0[i];
366 if (ep->me_key == NULL || ep->me_key == key)
367 return ep;
368 if (ep->me_key == dummy)
369 freeslot = ep;
370 else {
Guido van Rossum89d8c602007-09-18 17:26:56 +0000371 if (ep->me_hash == hash && unicode_eq(ep->me_key, key))
Fred Drake1bff34a2000-08-31 19:31:38 +0000372 return ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000373 freeslot = NULL;
374 }
Tim Peters15d49292001-05-27 07:39:22 +0000375
Tim Peters342c65e2001-05-13 06:43:53 +0000376 /* In the loop, me_key == dummy is by far (factor of 100s) the
377 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000378 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
379 i = (i << 2) + i + perturb + 1;
380 ep = &ep0[i & mask];
Tim Peters342c65e2001-05-13 06:43:53 +0000381 if (ep->me_key == NULL)
382 return freeslot == NULL ? ep : freeslot;
383 if (ep->me_key == key
384 || (ep->me_hash == hash
385 && ep->me_key != dummy
Guido van Rossum89d8c602007-09-18 17:26:56 +0000386 && unicode_eq(ep->me_key, key)))
Fred Drake1bff34a2000-08-31 19:31:38 +0000387 return ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000388 if (ep->me_key == dummy && freeslot == NULL)
Tim Peters342c65e2001-05-13 06:43:53 +0000389 freeslot = ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000390 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000391 assert(0); /* NOT REACHED */
392 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000393}
394
395/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000396Internal routine to insert a new item into the table.
397Used both by the internal resize routine and by the public insert routine.
398Eats a reference to key and one to value.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000399Returns -1 if an error occurred, or 0 on success.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000400*/
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000401static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000402insertdict(register PyDictObject *mp, PyObject *key, long hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000403{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000404 PyObject *old_value;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000405 register PyDictEntry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000406 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
407
408 assert(mp->ma_lookup != NULL);
409 ep = mp->ma_lookup(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000410 if (ep == NULL) {
411 Py_DECREF(key);
412 Py_DECREF(value);
413 return -1;
414 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000415 if (ep->me_value != NULL) {
Guido van Rossumd7047b31995-01-02 19:07:15 +0000416 old_value = ep->me_value;
417 ep->me_value = value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000418 Py_DECREF(old_value); /* which **CAN** re-enter */
419 Py_DECREF(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000420 }
421 else {
422 if (ep->me_key == NULL)
423 mp->ma_fill++;
Raymond Hettinger07ead172005-02-05 23:42:57 +0000424 else {
425 assert(ep->me_key == dummy);
426 Py_DECREF(dummy);
427 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000428 ep->me_key = key;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000429 ep->me_hash = (Py_ssize_t)hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000430 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000431 mp->ma_used++;
432 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000433 return 0;
434}
435
436/*
437Internal routine used by dictresize() to insert an item which is
438known to be absent from the dict. This routine also assumes that
439the dict contains no deleted entries. Besides the performance benefit,
440using insertdict() in dictresize() is dangerous (SF bug #1456209).
441Note that no refcounts are changed by this routine; if needed, the caller
442is responsible for incref'ing `key` and `value`.
443*/
444static void
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000445insertdict_clean(register PyDictObject *mp, PyObject *key, long hash,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000446 PyObject *value)
447{
448 register size_t i;
449 register size_t perturb;
450 register size_t mask = (size_t)mp->ma_mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000451 PyDictEntry *ep0 = mp->ma_table;
452 register PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000453
454 i = hash & mask;
455 ep = &ep0[i];
456 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
457 i = (i << 2) + i + perturb + 1;
458 ep = &ep0[i & mask];
459 }
460 assert(ep->me_value == NULL);
461 mp->ma_fill++;
462 ep->me_key = key;
463 ep->me_hash = (Py_ssize_t)hash;
464 ep->me_value = value;
465 mp->ma_used++;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000466}
467
468/*
469Restructure the table by allocating a new table and reinserting all
470items again. When entries have been deleted, the new table may
471actually be smaller than the old one.
472*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000473static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000474dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000475{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000476 Py_ssize_t newsize;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000477 PyDictEntry *oldtable, *newtable, *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000478 Py_ssize_t i;
Tim Peters0c6010b2001-05-23 23:33:57 +0000479 int is_oldtable_malloced;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000480 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000481
482 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000483
Tim Peterseb28ef22001-06-02 05:27:19 +0000484 /* Find the smallest table size > minused. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000485 for (newsize = PyDict_MINSIZE;
Tim Peterseb28ef22001-06-02 05:27:19 +0000486 newsize <= minused && newsize > 0;
487 newsize <<= 1)
488 ;
489 if (newsize <= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000490 PyErr_NoMemory();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000491 return -1;
492 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000493
494 /* Get space for a new table. */
495 oldtable = mp->ma_table;
496 assert(oldtable != NULL);
497 is_oldtable_malloced = oldtable != mp->ma_smalltable;
498
Tim Peters6d6c1a32001-08-02 04:15:00 +0000499 if (newsize == PyDict_MINSIZE) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000500 /* A large table is shrinking, or we can't get any smaller. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000501 newtable = mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000502 if (newtable == oldtable) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000503 if (mp->ma_fill == mp->ma_used) {
504 /* No dummies, so no point doing anything. */
Tim Peters0c6010b2001-05-23 23:33:57 +0000505 return 0;
Tim Petersf8a548c2001-05-24 16:26:40 +0000506 }
507 /* We're not going to resize it, but rebuild the
508 table anyway to purge old dummy entries.
509 Subtle: This is *necessary* if fill==size,
510 as lookdict needs at least one virgin slot to
511 terminate failing searches. If fill < size, it's
512 merely desirable, as dummies slow searches. */
513 assert(mp->ma_fill > mp->ma_used);
Tim Peters0c6010b2001-05-23 23:33:57 +0000514 memcpy(small_copy, oldtable, sizeof(small_copy));
515 oldtable = small_copy;
516 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000517 }
518 else {
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000519 newtable = PyMem_NEW(PyDictEntry, newsize);
Tim Petersdea48ec2001-05-22 20:40:22 +0000520 if (newtable == NULL) {
521 PyErr_NoMemory();
522 return -1;
523 }
524 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000525
526 /* Make the dict empty, using the new table. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000527 assert(newtable != oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000528 mp->ma_table = newtable;
Tim Petersafb6ae82001-06-04 21:00:21 +0000529 mp->ma_mask = newsize - 1;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000530 memset(newtable, 0, sizeof(PyDictEntry) * newsize);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000531 mp->ma_used = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000532 i = mp->ma_fill;
533 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000534
Tim Peters19283142001-05-17 22:25:34 +0000535 /* Copy the data over; this is refcount-neutral for active entries;
536 dummy entries aren't copied over, of course */
Tim Petersdea48ec2001-05-22 20:40:22 +0000537 for (ep = oldtable; i > 0; ep++) {
538 if (ep->me_value != NULL) { /* active entry */
539 --i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000540 insertdict_clean(mp, ep->me_key, (long)ep->me_hash,
541 ep->me_value);
Tim Petersdea48ec2001-05-22 20:40:22 +0000542 }
Tim Peters19283142001-05-17 22:25:34 +0000543 else if (ep->me_key != NULL) { /* dummy entry */
Tim Petersdea48ec2001-05-22 20:40:22 +0000544 --i;
Tim Peters19283142001-05-17 22:25:34 +0000545 assert(ep->me_key == dummy);
546 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000547 }
Tim Peters19283142001-05-17 22:25:34 +0000548 /* else key == value == NULL: nothing to do */
Guido van Rossumd7047b31995-01-02 19:07:15 +0000549 }
550
Tim Peters0c6010b2001-05-23 23:33:57 +0000551 if (is_oldtable_malloced)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000552 PyMem_DEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000553 return 0;
554}
555
Christian Heimes99170a52007-12-19 02:07:34 +0000556/* Create a new dictionary pre-sized to hold an estimated number of elements.
557 Underestimates are okay because the dictionary will resize as necessary.
558 Overestimates just mean the dictionary will be more sparse than usual.
559*/
560
561PyObject *
562_PyDict_NewPresized(Py_ssize_t minused)
563{
564 PyObject *op = PyDict_New();
565
566 if (minused>5 && op != NULL && dictresize((PyDictObject *)op, minused) == -1) {
567 Py_DECREF(op);
568 return NULL;
569 }
570 return op;
571}
572
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000573/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
574 * that may occur (originally dicts supported only string keys, and exceptions
575 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000576 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000577 * (suppressed) occurred while computing the key's hash, or that some error
578 * (suppressed) occurred when comparing keys in the dict's internal probe
579 * sequence. A nasty example of the latter is when a Python-coded comparison
580 * function hits a stack-depth error, which can cause this to return NULL
581 * even if the key is present.
582 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000583PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000584PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000585{
586 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000587 PyDictObject *mp = (PyDictObject *)op;
588 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000589 PyThreadState *tstate;
590 if (!PyDict_Check(op))
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000591 return NULL;
Guido van Rossum4d027722007-09-18 04:30:42 +0000592 if (!PyUnicode_CheckExact(key) ||
593 (hash = ((PyUnicodeObject *) key)->hash) == -1)
Guido van Rossumca756f21997-01-23 19:39:29 +0000594 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000595 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000596 if (hash == -1) {
597 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000598 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000599 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000600 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000601
602 /* We can arrive here with a NULL tstate during initialization:
603 try running "python -Wi" for an example related to string
604 interning. Let's just hope that no exception occurs then... */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000605 tstate = _PyThreadState_Current;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000606 if (tstate != NULL && tstate->curexc_type != NULL) {
607 /* preserve the existing exception */
608 PyObject *err_type, *err_value, *err_tb;
609 PyErr_Fetch(&err_type, &err_value, &err_tb);
610 ep = (mp->ma_lookup)(mp, key, hash);
611 /* ignore errors */
612 PyErr_Restore(err_type, err_value, err_tb);
613 if (ep == NULL)
614 return NULL;
615 }
616 else {
617 ep = (mp->ma_lookup)(mp, key, hash);
618 if (ep == NULL) {
619 PyErr_Clear();
620 return NULL;
621 }
622 }
623 return ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000624}
625
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000626/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
627 This returns NULL *with* an exception set if an exception occurred.
628 It returns NULL *without* an exception set if the key wasn't present.
629*/
630PyObject *
631PyDict_GetItemWithError(PyObject *op, PyObject *key)
632{
633 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000634 PyDictObject*mp = (PyDictObject *)op;
635 PyDictEntry *ep;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000636
637 if (!PyDict_Check(op)) {
638 PyErr_BadInternalCall();
639 return NULL;
640 }
Guido van Rossum89d8c602007-09-18 17:26:56 +0000641 if (!PyUnicode_CheckExact(key) ||
642 (hash = ((PyUnicodeObject *) key)->hash) == -1)
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000643 {
644 hash = PyObject_Hash(key);
645 if (hash == -1) {
646 return NULL;
647 }
648 }
649
650 ep = (mp->ma_lookup)(mp, key, hash);
651 if (ep == NULL)
652 return NULL;
653 return ep->me_value;
654}
655
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000656/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
Tim Peters60b29962006-01-01 01:19:23 +0000657 * dictionary if it's merely replacing the value for an existing key.
658 * This means that it's safe to loop over a dictionary with PyDict_Next()
659 * and occasionally replace a value -- but you can't insert new keys or
660 * remove them.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000661 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000662int
Tim Peters1f5871e2000-07-04 17:44:48 +0000663PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000664{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000665 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000666 register long hash;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000667 register Py_ssize_t n_used;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000668
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000669 if (!PyDict_Check(op)) {
670 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000671 return -1;
672 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000673 assert(key);
674 assert(value);
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000675 mp = (PyDictObject *)op;
Guido van Rossum4d027722007-09-18 04:30:42 +0000676 if (!PyUnicode_CheckExact(key) ||
677 (hash = ((PyUnicodeObject *) key)->hash) == -1)
678 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000679 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000680 if (hash == -1)
681 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000682 }
Tim Petersafb6ae82001-06-04 21:00:21 +0000683 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000684 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000685 Py_INCREF(value);
686 Py_INCREF(key);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000687 if (insertdict(mp, key, hash, value) != 0)
688 return -1;
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000689 /* If we added a key, we can safely resize. Otherwise just return!
690 * If fill >= 2/3 size, adjust size. Normally, this doubles or
691 * quaduples the size, but it's also possible for the dict to shrink
Tim Peters60b29962006-01-01 01:19:23 +0000692 * (if ma_fill is much larger than ma_used, meaning a lot of dict
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000693 * keys have been * deleted).
Tim Peters60b29962006-01-01 01:19:23 +0000694 *
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000695 * Quadrupling the size improves average dictionary sparseness
696 * (reducing collisions) at the cost of some memory and iteration
697 * speed (which loops over every possible entry). It also halves
698 * the number of expensive resize operations in a growing dictionary.
Tim Peters60b29962006-01-01 01:19:23 +0000699 *
700 * Very large dictionaries (over 50K items) use doubling instead.
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000701 * This may help applications with severe memory constraints.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000702 */
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000703 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
704 return 0;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000705 return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000706}
707
708int
Tim Peters1f5871e2000-07-04 17:44:48 +0000709PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000710{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000711 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000712 register long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000713 register PyDictEntry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000714 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000715
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000716 if (!PyDict_Check(op)) {
717 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000718 return -1;
719 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000720 assert(key);
Guido van Rossum89d8c602007-09-18 17:26:56 +0000721 if (!PyUnicode_CheckExact(key) ||
722 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000723 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000724 if (hash == -1)
725 return -1;
726 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000727 mp = (PyDictObject *)op;
Fred Drake1bff34a2000-08-31 19:31:38 +0000728 ep = (mp->ma_lookup)(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000729 if (ep == NULL)
730 return -1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000731 if (ep->me_value == NULL) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000732 set_key_error(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000733 return -1;
734 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000735 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000736 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000737 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000738 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000739 ep->me_value = NULL;
740 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000741 Py_DECREF(old_value);
742 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000743 return 0;
744}
745
Guido van Rossum25831651993-05-19 14:50:45 +0000746void
Tim Peters1f5871e2000-07-04 17:44:48 +0000747PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000748{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000749 PyDictObject *mp;
750 PyDictEntry *ep, *table;
Tim Petersdea48ec2001-05-22 20:40:22 +0000751 int table_is_malloced;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000752 Py_ssize_t fill;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000753 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Petersdea48ec2001-05-22 20:40:22 +0000754#ifdef Py_DEBUG
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000755 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +0000756#endif
757
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000758 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000759 return;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000760 mp = (PyDictObject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000761#ifdef Py_DEBUG
Tim Petersafb6ae82001-06-04 21:00:21 +0000762 n = mp->ma_mask + 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000763 i = 0;
764#endif
765
766 table = mp->ma_table;
767 assert(table != NULL);
768 table_is_malloced = table != mp->ma_smalltable;
769
770 /* This is delicate. During the process of clearing the dict,
771 * decrefs can cause the dict to mutate. To avoid fatal confusion
772 * (voice of experience), we have to make the dict empty before
773 * clearing the slots, and never refer to anything via mp->xxx while
774 * clearing.
775 */
776 fill = mp->ma_fill;
777 if (table_is_malloced)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000778 EMPTY_TO_MINSIZE(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000779
780 else if (fill > 0) {
781 /* It's a small table with something that needs to be cleared.
782 * Afraid the only safe way is to copy the dict entries into
783 * another small table first.
784 */
785 memcpy(small_copy, table, sizeof(small_copy));
786 table = small_copy;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000787 EMPTY_TO_MINSIZE(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000788 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000789 /* else it's a small table that's already empty */
790
791 /* Now we can finally clear things. If C had refcounts, we could
792 * assert that the refcount on table is 1 now, i.e. that this function
793 * has unique access to it, so decref side-effects can't alter it.
794 */
795 for (ep = table; fill > 0; ++ep) {
796#ifdef Py_DEBUG
797 assert(i < n);
798 ++i;
799#endif
800 if (ep->me_key) {
801 --fill;
802 Py_DECREF(ep->me_key);
803 Py_XDECREF(ep->me_value);
804 }
805#ifdef Py_DEBUG
806 else
807 assert(ep->me_value == NULL);
808#endif
809 }
810
811 if (table_is_malloced)
812 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000813}
814
Tim Peters080c88b2003-02-15 03:01:11 +0000815/*
816 * Iterate over a dict. Use like so:
817 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000818 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +0000819 * PyObject *key, *value;
820 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +0000821 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +0000822 * Refer to borrowed references in key and value.
823 * }
824 *
825 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +0000826 * mutates the dict. One exception: it is safe if the loop merely changes
827 * the values associated with the keys (but doesn't insert new keys or
828 * delete keys), via PyDict_SetItem().
829 */
Guido van Rossum25831651993-05-19 14:50:45 +0000830int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000831PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000832{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000833 register Py_ssize_t i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000834 register Py_ssize_t mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000835 register PyDictEntry *ep;
Raymond Hettinger43442782004-03-17 21:55:03 +0000836
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000837 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000838 return 0;
Guido van Rossum25831651993-05-19 14:50:45 +0000839 i = *ppos;
840 if (i < 0)
841 return 0;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000842 ep = ((PyDictObject *)op)->ma_table;
843 mask = ((PyDictObject *)op)->ma_mask;
Raymond Hettinger43442782004-03-17 21:55:03 +0000844 while (i <= mask && ep[i].me_value == NULL)
Guido van Rossum25831651993-05-19 14:50:45 +0000845 i++;
846 *ppos = i+1;
Raymond Hettinger43442782004-03-17 21:55:03 +0000847 if (i > mask)
Guido van Rossum25831651993-05-19 14:50:45 +0000848 return 0;
849 if (pkey)
Raymond Hettinger43442782004-03-17 21:55:03 +0000850 *pkey = ep[i].me_key;
Guido van Rossum25831651993-05-19 14:50:45 +0000851 if (pvalue)
Raymond Hettinger43442782004-03-17 21:55:03 +0000852 *pvalue = ep[i].me_value;
Guido van Rossum25831651993-05-19 14:50:45 +0000853 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000854}
855
Thomas Wouterscf297e42007-02-23 15:07:44 +0000856/* Internal version of PyDict_Next that returns a hash value in addition to the key and value.*/
857int
858_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue, long *phash)
859{
860 register Py_ssize_t i;
861 register Py_ssize_t mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000862 register PyDictEntry *ep;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000863
864 if (!PyDict_Check(op))
865 return 0;
866 i = *ppos;
867 if (i < 0)
868 return 0;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000869 ep = ((PyDictObject *)op)->ma_table;
870 mask = ((PyDictObject *)op)->ma_mask;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000871 while (i <= mask && ep[i].me_value == NULL)
872 i++;
873 *ppos = i+1;
874 if (i > mask)
875 return 0;
876 *phash = (long)(ep[i].me_hash);
877 if (pkey)
878 *pkey = ep[i].me_key;
879 if (pvalue)
880 *pvalue = ep[i].me_value;
881 return 1;
882}
883
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000884/* Methods */
885
886static void
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000887dict_dealloc(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000888{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000889 register PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000890 Py_ssize_t fill = mp->ma_fill;
Guido van Rossumff413af2002-03-28 20:34:59 +0000891 PyObject_GC_UnTrack(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000892 Py_TRASHCAN_SAFE_BEGIN(mp)
Tim Petersdea48ec2001-05-22 20:40:22 +0000893 for (ep = mp->ma_table; fill > 0; ep++) {
894 if (ep->me_key) {
895 --fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000896 Py_DECREF(ep->me_key);
Tim Petersdea48ec2001-05-22 20:40:22 +0000897 Py_XDECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000898 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000899 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000900 if (mp->ma_table != mp->ma_smalltable)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000901 PyMem_DEL(mp->ma_table);
Christian Heimes2202f872008-02-06 14:31:34 +0000902 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
903 free_list[numfree++] = mp;
Tim Peters60b29962006-01-01 01:19:23 +0000904 else
Christian Heimes90aa7642007-12-19 02:45:37 +0000905 Py_TYPE(mp)->tp_free((PyObject *)mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000906 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000907}
908
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000909static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000910dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000911{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000912 Py_ssize_t i;
Tim Petersa7259592001-06-16 05:11:17 +0000913 PyObject *s, *temp, *colon = NULL;
914 PyObject *pieces = NULL, *result = NULL;
915 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +0000916
Tim Petersa7259592001-06-16 05:11:17 +0000917 i = Py_ReprEnter((PyObject *)mp);
Guido van Rossum255443b1998-04-10 22:47:14 +0000918 if (i != 0) {
Walter Dörwald1ab83302007-05-18 17:15:44 +0000919 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
Guido van Rossum255443b1998-04-10 22:47:14 +0000920 }
921
Tim Petersa7259592001-06-16 05:11:17 +0000922 if (mp->ma_used == 0) {
Walter Dörwald1ab83302007-05-18 17:15:44 +0000923 result = PyUnicode_FromString("{}");
Tim Petersa7259592001-06-16 05:11:17 +0000924 goto Done;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000925 }
Tim Petersa7259592001-06-16 05:11:17 +0000926
927 pieces = PyList_New(0);
928 if (pieces == NULL)
929 goto Done;
930
Walter Dörwald1ab83302007-05-18 17:15:44 +0000931 colon = PyUnicode_FromString(": ");
Tim Petersa7259592001-06-16 05:11:17 +0000932 if (colon == NULL)
933 goto Done;
934
935 /* Do repr() on each key+value pair, and insert ": " between them.
936 Note that repr may mutate the dict. */
Tim Petersc6057842001-06-16 07:52:53 +0000937 i = 0;
938 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Tim Petersa7259592001-06-16 05:11:17 +0000939 int status;
940 /* Prevent repr from deleting value during key format. */
941 Py_INCREF(value);
942 s = PyObject_Repr(key);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000943 PyUnicode_Append(&s, colon);
944 PyUnicode_AppendAndDel(&s, PyObject_Repr(value));
Tim Petersa7259592001-06-16 05:11:17 +0000945 Py_DECREF(value);
946 if (s == NULL)
947 goto Done;
948 status = PyList_Append(pieces, s);
949 Py_DECREF(s); /* append created a new ref */
950 if (status < 0)
951 goto Done;
952 }
953
954 /* Add "{}" decorations to the first and last items. */
955 assert(PyList_GET_SIZE(pieces) > 0);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000956 s = PyUnicode_FromString("{");
Tim Petersa7259592001-06-16 05:11:17 +0000957 if (s == NULL)
958 goto Done;
959 temp = PyList_GET_ITEM(pieces, 0);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000960 PyUnicode_AppendAndDel(&s, temp);
Tim Petersa7259592001-06-16 05:11:17 +0000961 PyList_SET_ITEM(pieces, 0, s);
962 if (s == NULL)
963 goto Done;
964
Walter Dörwald1ab83302007-05-18 17:15:44 +0000965 s = PyUnicode_FromString("}");
Tim Petersa7259592001-06-16 05:11:17 +0000966 if (s == NULL)
967 goto Done;
968 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000969 PyUnicode_AppendAndDel(&temp, s);
Tim Petersa7259592001-06-16 05:11:17 +0000970 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
971 if (temp == NULL)
972 goto Done;
973
974 /* Paste them all together with ", " between. */
Walter Dörwald1ab83302007-05-18 17:15:44 +0000975 s = PyUnicode_FromString(", ");
Tim Petersa7259592001-06-16 05:11:17 +0000976 if (s == NULL)
977 goto Done;
Walter Dörwald1ab83302007-05-18 17:15:44 +0000978 result = PyUnicode_Join(s, pieces);
Tim Peters0ab085c2001-09-14 00:25:33 +0000979 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +0000980
981Done:
982 Py_XDECREF(pieces);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000983 Py_XDECREF(colon);
Tim Petersa7259592001-06-16 05:11:17 +0000984 Py_ReprLeave((PyObject *)mp);
985 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000986}
987
Martin v. Löwis18e16552006-02-15 17:27:45 +0000988static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000989dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000990{
991 return mp->ma_used;
992}
993
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000994static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000995dict_subscript(PyDictObject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000996{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000997 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000998 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000999 PyDictEntry *ep;
Tim Petersdea48ec2001-05-22 20:40:22 +00001000 assert(mp->ma_table != NULL);
Guido van Rossum89d8c602007-09-18 17:26:56 +00001001 if (!PyUnicode_CheckExact(key) ||
1002 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001003 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001004 if (hash == -1)
1005 return NULL;
1006 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001007 ep = (mp->ma_lookup)(mp, key, hash);
1008 if (ep == NULL)
1009 return NULL;
1010 v = ep->me_value;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001011 if (v == NULL) {
1012 if (!PyDict_CheckExact(mp)) {
1013 /* Look up __missing__ method if we're a subclass. */
Guido van Rossum4b92a822006-02-25 23:32:30 +00001014 PyObject *missing;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001015 static PyObject *missing_str = NULL;
1016 if (missing_str == NULL)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001017 missing_str =
Martin v. Löwis5b222132007-06-10 09:51:05 +00001018 PyUnicode_InternFromString("__missing__");
Christian Heimes90aa7642007-12-19 02:45:37 +00001019 missing = _PyType_Lookup(Py_TYPE(mp), missing_str);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001020 if (missing != NULL)
1021 return PyObject_CallFunctionObjArgs(missing,
1022 (PyObject *)mp, key, NULL);
1023 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001024 set_key_error(key);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001025 return NULL;
1026 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001027 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001028 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001029 return v;
1030}
1031
1032static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001033dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001034{
1035 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001036 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001037 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001038 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001039}
1040
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001041static PyMappingMethods dict_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001042 (lenfunc)dict_length, /*mp_length*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001043 (binaryfunc)dict_subscript, /*mp_subscript*/
1044 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001045};
1046
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001047static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001048dict_keys(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001049{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001050 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001051 register Py_ssize_t i, j;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001052 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001053 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001054
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001055 again:
1056 n = mp->ma_used;
1057 v = PyList_New(n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001058 if (v == NULL)
1059 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001060 if (n != mp->ma_used) {
1061 /* Durnit. The allocations caused the dict to resize.
1062 * Just start over, this shouldn't normally happen.
1063 */
1064 Py_DECREF(v);
1065 goto again;
1066 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001067 ep = mp->ma_table;
1068 mask = mp->ma_mask;
1069 for (i = 0, j = 0; i <= mask; i++) {
1070 if (ep[i].me_value != NULL) {
1071 PyObject *key = ep[i].me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001072 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001073 PyList_SET_ITEM(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001074 j++;
1075 }
1076 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001077 assert(j == n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001078 return v;
1079}
1080
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001081static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001082dict_values(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001083{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001084 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001085 register Py_ssize_t i, j;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001086 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001087 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001088
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001089 again:
1090 n = mp->ma_used;
1091 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001092 if (v == NULL)
1093 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001094 if (n != mp->ma_used) {
1095 /* Durnit. The allocations caused the dict to resize.
1096 * Just start over, this shouldn't normally happen.
1097 */
1098 Py_DECREF(v);
1099 goto again;
1100 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001101 ep = mp->ma_table;
1102 mask = mp->ma_mask;
1103 for (i = 0, j = 0; i <= mask; i++) {
1104 if (ep[i].me_value != NULL) {
1105 PyObject *value = ep[i].me_value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001106 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001107 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001108 j++;
1109 }
1110 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001111 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001112 return v;
1113}
1114
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001115static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001116dict_items(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001117{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001118 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001119 register Py_ssize_t i, j, n;
1120 Py_ssize_t mask;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001121 PyObject *item, *key, *value;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001122 PyDictEntry *ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001123
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001124 /* Preallocate the list of tuples, to avoid allocations during
1125 * the loop over the items, which could trigger GC, which
1126 * could resize the dict. :-(
1127 */
1128 again:
1129 n = mp->ma_used;
1130 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001131 if (v == NULL)
1132 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001133 for (i = 0; i < n; i++) {
1134 item = PyTuple_New(2);
1135 if (item == NULL) {
1136 Py_DECREF(v);
1137 return NULL;
1138 }
1139 PyList_SET_ITEM(v, i, item);
1140 }
1141 if (n != mp->ma_used) {
1142 /* Durnit. The allocations caused the dict to resize.
1143 * Just start over, this shouldn't normally happen.
1144 */
1145 Py_DECREF(v);
1146 goto again;
1147 }
1148 /* Nothing we do below makes any function calls. */
Raymond Hettinger43442782004-03-17 21:55:03 +00001149 ep = mp->ma_table;
1150 mask = mp->ma_mask;
1151 for (i = 0, j = 0; i <= mask; i++) {
Raymond Hettinger06905122004-03-19 10:30:00 +00001152 if ((value=ep[i].me_value) != NULL) {
Raymond Hettinger43442782004-03-17 21:55:03 +00001153 key = ep[i].me_key;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001154 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001155 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001156 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001157 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001158 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001159 j++;
1160 }
1161 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001162 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001163 return v;
1164}
1165
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001166static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001167dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001168{
1169 PyObject *seq;
1170 PyObject *value = Py_None;
1171 PyObject *it; /* iter(seq) */
1172 PyObject *key;
1173 PyObject *d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001174 int status;
1175
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001176 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001177 return NULL;
1178
1179 d = PyObject_CallObject(cls, NULL);
1180 if (d == NULL)
1181 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001182
Guido van Rossum58da9312007-11-10 23:39:45 +00001183 if (PyDict_CheckExact(d) && PyDict_CheckExact(seq)) {
1184 PyDictObject *mp = (PyDictObject *)d;
1185 PyObject *oldvalue;
1186 Py_ssize_t pos = 0;
1187 PyObject *key;
1188 long hash;
1189
1190 if (dictresize(mp, PySet_GET_SIZE(seq)))
1191 return NULL;
1192
1193 while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
1194 Py_INCREF(key);
1195 Py_INCREF(value);
1196 if (insertdict(mp, key, hash, value))
1197 return NULL;
1198 }
1199 return d;
1200 }
1201
Guido van Rossumd8faa362007-04-27 19:54:29 +00001202 if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) {
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001203 PyDictObject *mp = (PyDictObject *)d;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001204 Py_ssize_t pos = 0;
1205 PyObject *key;
1206 long hash;
1207
1208 if (dictresize(mp, PySet_GET_SIZE(seq)))
1209 return NULL;
1210
1211 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
1212 Py_INCREF(key);
1213 Py_INCREF(value);
1214 if (insertdict(mp, key, hash, value))
1215 return NULL;
1216 }
1217 return d;
1218 }
1219
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001220 it = PyObject_GetIter(seq);
1221 if (it == NULL){
1222 Py_DECREF(d);
1223 return NULL;
1224 }
1225
Guido van Rossum58da9312007-11-10 23:39:45 +00001226 if (PyDict_CheckExact(d)) {
1227 while ((key = PyIter_Next(it)) != NULL) {
1228 status = PyDict_SetItem(d, key, value);
1229 Py_DECREF(key);
1230 if (status < 0)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001231 goto Fail;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001232 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001233 } else {
1234 while ((key = PyIter_Next(it)) != NULL) {
1235 status = PyObject_SetItem(d, key, value);
1236 Py_DECREF(key);
1237 if (status < 0)
1238 goto Fail;
1239 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001240 }
1241
Guido van Rossum58da9312007-11-10 23:39:45 +00001242 if (PyErr_Occurred())
1243 goto Fail;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001244 Py_DECREF(it);
1245 return d;
1246
1247Fail:
1248 Py_DECREF(it);
1249 Py_DECREF(d);
1250 return NULL;
1251}
1252
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001253static int
1254dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001255{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001256 PyObject *arg = NULL;
1257 int result = 0;
1258
1259 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1260 result = -1;
1261
1262 else if (arg != NULL) {
1263 if (PyObject_HasAttrString(arg, "keys"))
1264 result = PyDict_Merge(self, arg, 1);
1265 else
1266 result = PyDict_MergeFromSeq2(self, arg, 1);
1267 }
1268 if (result == 0 && kwds != NULL)
1269 result = PyDict_Merge(self, kwds, 1);
1270 return result;
1271}
1272
1273static PyObject *
1274dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1275{
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001276 if (dict_update_common(self, args, kwds, "update") != -1)
1277 Py_RETURN_NONE;
1278 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001279}
1280
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001281/* Update unconditionally replaces existing items.
1282 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001283 otherwise it leaves existing items unchanged.
1284
1285 PyDict_{Update,Merge} update/merge from a mapping object.
1286
Tim Petersf582b822001-12-11 18:51:08 +00001287 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001288 producing iterable objects of length 2.
1289*/
1290
Tim Petersf582b822001-12-11 18:51:08 +00001291int
Tim Peters1fc240e2001-10-26 05:06:50 +00001292PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1293{
1294 PyObject *it; /* iter(seq2) */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001295 Py_ssize_t i; /* index into seq2 of current element */
Tim Peters1fc240e2001-10-26 05:06:50 +00001296 PyObject *item; /* seq2[i] */
1297 PyObject *fast; /* item as a 2-tuple or 2-list */
1298
1299 assert(d != NULL);
1300 assert(PyDict_Check(d));
1301 assert(seq2 != NULL);
1302
1303 it = PyObject_GetIter(seq2);
1304 if (it == NULL)
1305 return -1;
1306
1307 for (i = 0; ; ++i) {
1308 PyObject *key, *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001309 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001310
1311 fast = NULL;
1312 item = PyIter_Next(it);
1313 if (item == NULL) {
1314 if (PyErr_Occurred())
1315 goto Fail;
1316 break;
1317 }
1318
1319 /* Convert item to sequence, and verify length 2. */
1320 fast = PySequence_Fast(item, "");
1321 if (fast == NULL) {
1322 if (PyErr_ExceptionMatches(PyExc_TypeError))
1323 PyErr_Format(PyExc_TypeError,
1324 "cannot convert dictionary update "
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001325 "sequence element #%zd to a sequence",
Tim Peters1fc240e2001-10-26 05:06:50 +00001326 i);
1327 goto Fail;
1328 }
1329 n = PySequence_Fast_GET_SIZE(fast);
1330 if (n != 2) {
1331 PyErr_Format(PyExc_ValueError,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001332 "dictionary update sequence element #%zd "
Martin v. Löwis2c95cc62006-02-16 06:54:25 +00001333 "has length %zd; 2 is required",
Martin v. Löwise0e89f72006-02-16 06:59:22 +00001334 i, n);
Tim Peters1fc240e2001-10-26 05:06:50 +00001335 goto Fail;
1336 }
1337
1338 /* Update/merge with this (key, value) pair. */
1339 key = PySequence_Fast_GET_ITEM(fast, 0);
1340 value = PySequence_Fast_GET_ITEM(fast, 1);
1341 if (override || PyDict_GetItem(d, key) == NULL) {
1342 int status = PyDict_SetItem(d, key, value);
1343 if (status < 0)
1344 goto Fail;
1345 }
1346 Py_DECREF(fast);
1347 Py_DECREF(item);
1348 }
1349
1350 i = 0;
1351 goto Return;
1352Fail:
1353 Py_XDECREF(item);
1354 Py_XDECREF(fast);
1355 i = -1;
1356Return:
1357 Py_DECREF(it);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001358 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001359}
1360
Tim Peters6d6c1a32001-08-02 04:15:00 +00001361int
1362PyDict_Update(PyObject *a, PyObject *b)
1363{
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001364 return PyDict_Merge(a, b, 1);
1365}
1366
1367int
1368PyDict_Merge(PyObject *a, PyObject *b, int override)
1369{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001370 register PyDictObject *mp, *other;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001371 register Py_ssize_t i;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001372 PyDictEntry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001373
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001374 /* We accept for the argument either a concrete dictionary object,
1375 * or an abstract "mapping" object. For the former, we can do
1376 * things quite efficiently. For the latter, we only require that
1377 * PyMapping_Keys() and PyObject_GetItem() be supported.
1378 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001379 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1380 PyErr_BadInternalCall();
1381 return -1;
1382 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001383 mp = (PyDictObject*)a;
Christian Heimesaf98da12008-01-27 15:18:18 +00001384 if (PyDict_Check(b)) {
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001385 other = (PyDictObject*)b;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001386 if (other == mp || other->ma_used == 0)
1387 /* a.update(a) or a.update({}); nothing to do */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001388 return 0;
Raymond Hettinger186e7392005-05-14 18:08:25 +00001389 if (mp->ma_used == 0)
1390 /* Since the target dict is empty, PyDict_GetItem()
1391 * always returns NULL. Setting override to 1
1392 * skips the unnecessary test.
1393 */
1394 override = 1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001395 /* Do one big resize at the start, rather than
1396 * incrementally resizing as we insert new items. Expect
1397 * that there will be no (or few) overlapping keys.
1398 */
1399 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
Raymond Hettingerc8d22902003-05-07 00:49:40 +00001400 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001401 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001402 }
1403 for (i = 0; i <= other->ma_mask; i++) {
1404 entry = &other->ma_table[i];
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001405 if (entry->me_value != NULL &&
1406 (override ||
1407 PyDict_GetItem(a, entry->me_key) == NULL)) {
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001408 Py_INCREF(entry->me_key);
1409 Py_INCREF(entry->me_value);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001410 if (insertdict(mp, entry->me_key,
1411 (long)entry->me_hash,
1412 entry->me_value) != 0)
1413 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001414 }
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001415 }
1416 }
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001417 else {
1418 /* Do it the generic, slower way */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001419 PyObject *keys = PyMapping_Keys(b);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001420 PyObject *iter;
1421 PyObject *key, *value;
1422 int status;
1423
1424 if (keys == NULL)
1425 /* Docstring says this is equivalent to E.keys() so
1426 * if E doesn't have a .keys() method we want
1427 * AttributeError to percolate up. Might as well
1428 * do the same for any other error.
1429 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001430 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001431
1432 iter = PyObject_GetIter(keys);
1433 Py_DECREF(keys);
1434 if (iter == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001435 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001436
1437 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001438 if (!override && PyDict_GetItem(a, key) != NULL) {
1439 Py_DECREF(key);
1440 continue;
1441 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001442 value = PyObject_GetItem(b, key);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001443 if (value == NULL) {
1444 Py_DECREF(iter);
1445 Py_DECREF(key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001446 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001447 }
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001448 status = PyDict_SetItem(a, key, value);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001449 Py_DECREF(key);
1450 Py_DECREF(value);
1451 if (status < 0) {
1452 Py_DECREF(iter);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001453 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001454 }
1455 }
1456 Py_DECREF(iter);
1457 if (PyErr_Occurred())
1458 /* Iterator completed, via error */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001459 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001460 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001461 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001462}
1463
1464static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001465dict_copy(register PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001466{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001467 return PyDict_Copy((PyObject*)mp);
1468}
1469
1470PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001471PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001472{
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001473 PyObject *copy;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001474
1475 if (o == NULL || !PyDict_Check(o)) {
1476 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001477 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001478 }
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001479 copy = PyDict_New();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001480 if (copy == NULL)
1481 return NULL;
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001482 if (PyDict_Merge(copy, o, 1) == 0)
1483 return copy;
1484 Py_DECREF(copy);
Raymond Hettinger1356f782005-04-15 15:58:42 +00001485 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001486}
1487
Martin v. Löwis18e16552006-02-15 17:27:45 +00001488Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00001489PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001490{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001491 if (mp == NULL || !PyDict_Check(mp)) {
1492 PyErr_BadInternalCall();
Jeremy Hylton7083bb72004-02-17 20:10:11 +00001493 return -1;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001494 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001495 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001496}
1497
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001498PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001499PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001500{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001501 if (mp == NULL || !PyDict_Check(mp)) {
1502 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001503 return NULL;
1504 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001505 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001506}
1507
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001508PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001509PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001510{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001511 if (mp == NULL || !PyDict_Check(mp)) {
1512 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001513 return NULL;
1514 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001515 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001516}
1517
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001518PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001519PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001520{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001521 if (mp == NULL || !PyDict_Check(mp)) {
1522 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001523 return NULL;
1524 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001525 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001526}
1527
Tim Peterse63415e2001-05-08 04:38:29 +00001528/* Return 1 if dicts equal, 0 if not, -1 if error.
1529 * Gets out as soon as any difference is detected.
1530 * Uses only Py_EQ comparison.
1531 */
1532static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001533dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00001534{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001535 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00001536
1537 if (a->ma_used != b->ma_used)
1538 /* can't be equal if # of entries differ */
1539 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001540
Tim Peterse63415e2001-05-08 04:38:29 +00001541 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Tim Petersafb6ae82001-06-04 21:00:21 +00001542 for (i = 0; i <= a->ma_mask; i++) {
Tim Peterse63415e2001-05-08 04:38:29 +00001543 PyObject *aval = a->ma_table[i].me_value;
1544 if (aval != NULL) {
1545 int cmp;
1546 PyObject *bval;
1547 PyObject *key = a->ma_table[i].me_key;
1548 /* temporarily bump aval's refcount to ensure it stays
1549 alive until we're done with it */
1550 Py_INCREF(aval);
Guido van Rossumdc5f6b22006-08-24 21:29:26 +00001551 /* ditto for key */
1552 Py_INCREF(key);
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001553 bval = PyDict_GetItemWithError((PyObject *)b, key);
Guido van Rossumdc5f6b22006-08-24 21:29:26 +00001554 Py_DECREF(key);
Tim Peterse63415e2001-05-08 04:38:29 +00001555 if (bval == NULL) {
1556 Py_DECREF(aval);
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001557 if (PyErr_Occurred())
1558 return -1;
Tim Peterse63415e2001-05-08 04:38:29 +00001559 return 0;
1560 }
1561 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1562 Py_DECREF(aval);
1563 if (cmp <= 0) /* error or not equal */
1564 return cmp;
1565 }
1566 }
1567 return 1;
1568 }
1569
1570static PyObject *
1571dict_richcompare(PyObject *v, PyObject *w, int op)
1572{
1573 int cmp;
1574 PyObject *res;
1575
1576 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1577 res = Py_NotImplemented;
1578 }
1579 else if (op == Py_EQ || op == Py_NE) {
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001580 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
Tim Peterse63415e2001-05-08 04:38:29 +00001581 if (cmp < 0)
1582 return NULL;
1583 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1584 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001585 else
1586 res = Py_NotImplemented;
Tim Peterse63415e2001-05-08 04:38:29 +00001587 Py_INCREF(res);
1588 return res;
1589 }
1590
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001591static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001592dict_contains(register PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001593{
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001594 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001595 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001596
Guido van Rossum89d8c602007-09-18 17:26:56 +00001597 if (!PyUnicode_CheckExact(key) ||
1598 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001599 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001600 if (hash == -1)
1601 return NULL;
1602 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001603 ep = (mp->ma_lookup)(mp, key, hash);
1604 if (ep == NULL)
1605 return NULL;
1606 return PyBool_FromLong(ep->me_value != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001607}
1608
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001609static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001610dict_get(register PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001611{
1612 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001613 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001614 PyObject *val = NULL;
1615 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001616 PyDictEntry *ep;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001617
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001618 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001619 return NULL;
1620
Guido van Rossum89d8c602007-09-18 17:26:56 +00001621 if (!PyUnicode_CheckExact(key) ||
1622 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Barry Warsawc38c5da1997-10-06 17:49:20 +00001623 hash = PyObject_Hash(key);
1624 if (hash == -1)
1625 return NULL;
1626 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001627 ep = (mp->ma_lookup)(mp, key, hash);
1628 if (ep == NULL)
1629 return NULL;
1630 val = ep->me_value;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001631 if (val == NULL)
1632 val = failobj;
1633 Py_INCREF(val);
1634 return val;
1635}
1636
1637
1638static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001639dict_setdefault(register PyDictObject *mp, PyObject *args)
Guido van Rossum164452c2000-08-08 16:12:54 +00001640{
1641 PyObject *key;
1642 PyObject *failobj = Py_None;
1643 PyObject *val = NULL;
1644 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001645 PyDictEntry *ep;
Guido van Rossum164452c2000-08-08 16:12:54 +00001646
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001647 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
Guido van Rossum164452c2000-08-08 16:12:54 +00001648 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001649
Guido van Rossum89d8c602007-09-18 17:26:56 +00001650 if (!PyUnicode_CheckExact(key) ||
1651 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossum164452c2000-08-08 16:12:54 +00001652 hash = PyObject_Hash(key);
1653 if (hash == -1)
1654 return NULL;
1655 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001656 ep = (mp->ma_lookup)(mp, key, hash);
1657 if (ep == NULL)
1658 return NULL;
1659 val = ep->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001660 if (val == NULL) {
1661 val = failobj;
1662 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1663 val = NULL;
1664 }
1665 Py_XINCREF(val);
1666 return val;
1667}
1668
1669
1670static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001671dict_clear(register PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001672{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001673 PyDict_Clear((PyObject *)mp);
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001674 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001675}
1676
Guido van Rossumba6ab842000-12-12 22:02:18 +00001677static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001678dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00001679{
1680 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001681 PyDictEntry *ep;
Guido van Rossume027d982002-04-12 15:11:59 +00001682 PyObject *old_value, *old_key;
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001683 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001684
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001685 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1686 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001687 if (mp->ma_used == 0) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001688 if (deflt) {
1689 Py_INCREF(deflt);
1690 return deflt;
1691 }
Guido van Rossume027d982002-04-12 15:11:59 +00001692 PyErr_SetString(PyExc_KeyError,
1693 "pop(): dictionary is empty");
1694 return NULL;
1695 }
Guido van Rossum89d8c602007-09-18 17:26:56 +00001696 if (!PyUnicode_CheckExact(key) ||
1697 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossume027d982002-04-12 15:11:59 +00001698 hash = PyObject_Hash(key);
1699 if (hash == -1)
1700 return NULL;
1701 }
1702 ep = (mp->ma_lookup)(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001703 if (ep == NULL)
1704 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001705 if (ep->me_value == NULL) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001706 if (deflt) {
1707 Py_INCREF(deflt);
1708 return deflt;
1709 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001710 set_key_error(key);
Guido van Rossume027d982002-04-12 15:11:59 +00001711 return NULL;
1712 }
1713 old_key = ep->me_key;
1714 Py_INCREF(dummy);
1715 ep->me_key = dummy;
1716 old_value = ep->me_value;
1717 ep->me_value = NULL;
1718 mp->ma_used--;
1719 Py_DECREF(old_key);
1720 return old_value;
1721}
1722
1723static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001724dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001725{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001726 Py_ssize_t i = 0;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001727 PyDictEntry *ep;
Guido van Rossumba6ab842000-12-12 22:02:18 +00001728 PyObject *res;
1729
Tim Petersf4b33f62001-06-02 05:42:29 +00001730 /* Allocate the result tuple before checking the size. Believe it
1731 * or not, this allocation could trigger a garbage collection which
1732 * could empty the dict, so if we checked the size first and that
1733 * happened, the result would be an infinite loop (searching for an
1734 * entry that no longer exists). Note that the usual popitem()
1735 * idiom is "while d: k, v = d.popitem()". so needing to throw the
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001736 * tuple away if the dict *is* empty isn't a significant
Tim Petersf4b33f62001-06-02 05:42:29 +00001737 * inefficiency -- possible, but unlikely in practice.
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001738 */
1739 res = PyTuple_New(2);
1740 if (res == NULL)
1741 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001742 if (mp->ma_used == 0) {
1743 Py_DECREF(res);
1744 PyErr_SetString(PyExc_KeyError,
1745 "popitem(): dictionary is empty");
1746 return NULL;
1747 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001748 /* Set ep to "the first" dict entry with a value. We abuse the hash
1749 * field of slot 0 to hold a search finger:
1750 * If slot 0 has a value, use slot 0.
1751 * Else slot 0 is being used to hold a search finger,
1752 * and we use its hash value as the first index to look.
1753 */
1754 ep = &mp->ma_table[0];
1755 if (ep->me_value == NULL) {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001756 i = ep->me_hash;
Tim Petersdea48ec2001-05-22 20:40:22 +00001757 /* The hash field may be a real hash value, or it may be a
1758 * legit search finger, or it may be a once-legit search
1759 * finger that's out of bounds now because it wrapped around
1760 * or the table shrunk -- simply make sure it's in bounds now.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001761 */
Tim Petersafb6ae82001-06-04 21:00:21 +00001762 if (i > mp->ma_mask || i < 1)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001763 i = 1; /* skip slot 0 */
1764 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1765 i++;
Tim Petersafb6ae82001-06-04 21:00:21 +00001766 if (i > mp->ma_mask)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001767 i = 1;
1768 }
1769 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001770 PyTuple_SET_ITEM(res, 0, ep->me_key);
1771 PyTuple_SET_ITEM(res, 1, ep->me_value);
1772 Py_INCREF(dummy);
1773 ep->me_key = dummy;
1774 ep->me_value = NULL;
1775 mp->ma_used--;
1776 assert(mp->ma_table[0].me_value == NULL);
1777 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001778 return res;
1779}
1780
Jeremy Hylton8caad492000-06-23 14:18:11 +00001781static int
1782dict_traverse(PyObject *op, visitproc visit, void *arg)
1783{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001784 Py_ssize_t i = 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001785 PyObject *pk;
1786 PyObject *pv;
1787
1788 while (PyDict_Next(op, &i, &pk, &pv)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001789 Py_VISIT(pk);
1790 Py_VISIT(pv);
Jeremy Hylton8caad492000-06-23 14:18:11 +00001791 }
1792 return 0;
1793}
1794
1795static int
1796dict_tp_clear(PyObject *op)
1797{
1798 PyDict_Clear(op);
1799 return 0;
1800}
1801
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001802static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001803
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001804PyDoc_STRVAR(contains__doc__,
1805"D.__contains__(k) -> True if D has a key k, else False");
1806
1807PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
1808
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001809PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001810"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001811
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001812PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001813"D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D");
Tim Petersf7f88b12000-12-13 23:18:45 +00001814
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001815PyDoc_STRVAR(pop__doc__,
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001816"D.pop(k[,d]) -> v, remove specified key and return the corresponding value\n\
1817If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00001818
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001819PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00001820"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000018212-tuple; but raise KeyError if D is empty");
Tim Petersf7f88b12000-12-13 23:18:45 +00001822
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001823PyDoc_STRVAR(update__doc__,
Guido van Rossumb90c8482007-02-10 01:11:45 +00001824"D.update(E, **F) -> None. Update D from E and F: for k in E: D[k] = E[k]\
1825\n(if E has keys else: for (k, v) in E: D[k] = v) then: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00001826
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001827PyDoc_STRVAR(fromkeys__doc__,
1828"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
1829v defaults to None.");
1830
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001831PyDoc_STRVAR(clear__doc__,
1832"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001833
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001834PyDoc_STRVAR(copy__doc__,
1835"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00001836
Guido van Rossumb90c8482007-02-10 01:11:45 +00001837/* Forward */
1838static PyObject *dictkeys_new(PyObject *);
1839static PyObject *dictitems_new(PyObject *);
1840static PyObject *dictvalues_new(PyObject *);
1841
Guido van Rossum45c85d12007-07-27 16:31:40 +00001842PyDoc_STRVAR(keys__doc__,
1843 "D.keys() -> a set-like object providing a view on D's keys");
1844PyDoc_STRVAR(items__doc__,
1845 "D.items() -> a set-like object providing a view on D's items");
1846PyDoc_STRVAR(values__doc__,
1847 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00001848
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001849static PyMethodDef mapp_methods[] = {
Guido van Rossume2b70bc2006-08-18 22:13:04 +00001850 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001851 contains__doc__},
Raymond Hettinger0c669672003-12-13 13:31:55 +00001852 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001853 getitem__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00001854 {"get", (PyCFunction)dict_get, METH_VARARGS,
1855 get__doc__},
1856 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
1857 setdefault_doc__},
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001858 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
Just van Rossuma797d812002-11-23 09:45:04 +00001859 pop__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001860 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001861 popitem__doc__},
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001862 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
Guido van Rossumeff072c2007-02-27 05:47:18 +00001863 keys__doc__},
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001864 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
Guido van Rossumeff072c2007-02-27 05:47:18 +00001865 items__doc__},
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001866 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
Guido van Rossumeff072c2007-02-27 05:47:18 +00001867 values__doc__},
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001868 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001869 update__doc__},
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001870 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
1871 fromkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001872 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001873 clear__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001874 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001875 copy__doc__},
1876 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001877};
1878
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001879/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001880int
1881PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001882{
1883 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001884 PyDictObject *mp = (PyDictObject *)op;
1885 PyDictEntry *ep;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001886
Guido van Rossum89d8c602007-09-18 17:26:56 +00001887 if (!PyUnicode_CheckExact(key) ||
1888 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001889 hash = PyObject_Hash(key);
1890 if (hash == -1)
1891 return -1;
1892 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001893 ep = (mp->ma_lookup)(mp, key, hash);
1894 return ep == NULL ? -1 : (ep->me_value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001895}
1896
Thomas Wouterscf297e42007-02-23 15:07:44 +00001897/* Internal version of PyDict_Contains used when the hash value is already known */
1898int
1899_PyDict_Contains(PyObject *op, PyObject *key, long hash)
1900{
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001901 PyDictObject *mp = (PyDictObject *)op;
1902 PyDictEntry *ep;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001903
1904 ep = (mp->ma_lookup)(mp, key, hash);
1905 return ep == NULL ? -1 : (ep->me_value != NULL);
1906}
1907
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001908/* Hack to implement "key in dict" */
1909static PySequenceMethods dict_as_sequence = {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001910 0, /* sq_length */
1911 0, /* sq_concat */
1912 0, /* sq_repeat */
1913 0, /* sq_item */
1914 0, /* sq_slice */
1915 0, /* sq_ass_item */
1916 0, /* sq_ass_slice */
1917 PyDict_Contains, /* sq_contains */
1918 0, /* sq_inplace_concat */
1919 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001920};
1921
Guido van Rossum09e563a2001-05-01 12:10:21 +00001922static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001923dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1924{
1925 PyObject *self;
1926
1927 assert(type != NULL && type->tp_alloc != NULL);
1928 self = type->tp_alloc(type, 0);
1929 if (self != NULL) {
1930 PyDictObject *d = (PyDictObject *)self;
1931 /* It's guaranteed that tp->alloc zeroed out the struct. */
1932 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
1933 INIT_NONZERO_DICT_SLOTS(d);
Guido van Rossum89d8c602007-09-18 17:26:56 +00001934 d->ma_lookup = lookdict_unicode;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001935#ifdef SHOW_CONVERSION_COUNTS
1936 ++created;
1937#endif
1938 }
1939 return self;
1940}
1941
Tim Peters25786c02001-09-02 08:22:48 +00001942static int
1943dict_init(PyObject *self, PyObject *args, PyObject *kwds)
1944{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001945 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00001946}
1947
Tim Peters6d6c1a32001-08-02 04:15:00 +00001948static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001949dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001950{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001951 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001952}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001953
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001954PyDoc_STRVAR(dictionary_doc,
Tim Petersa427a2b2001-10-29 22:25:45 +00001955"dict() -> new empty dictionary.\n"
1956"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Tim Peters1fc240e2001-10-26 05:06:50 +00001957" (key, value) pairs.\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00001958"dict(seq) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00001959" d = {}\n"
1960" for k, v in seq:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00001961" d[k] = v\n"
1962"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
1963" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00001964
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001965PyTypeObject PyDict_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001966 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Tim Petersa427a2b2001-10-29 22:25:45 +00001967 "dict",
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001968 sizeof(PyDictObject),
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001969 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00001970 (destructor)dict_dealloc, /* tp_dealloc */
Guido van Rossum04dbf3b2007-08-07 19:51:00 +00001971 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001972 0, /* tp_getattr */
Guido van Rossumb9324202001-01-18 00:39:02 +00001973 0, /* tp_setattr */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001974 0, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00001975 (reprfunc)dict_repr, /* tp_repr */
1976 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001977 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00001978 &dict_as_mapping, /* tp_as_mapping */
Guido van Rossum50e9fb92006-08-17 05:42:55 +00001979 0, /* tp_hash */
Guido van Rossumb9324202001-01-18 00:39:02 +00001980 0, /* tp_call */
1981 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001982 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumb9324202001-01-18 00:39:02 +00001983 0, /* tp_setattro */
1984 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00001985 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Thomas Wouters27d517b2007-02-25 20:39:11 +00001986 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
Tim Peters25786c02001-09-02 08:22:48 +00001987 dictionary_doc, /* tp_doc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001988 dict_traverse, /* tp_traverse */
1989 dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00001990 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001991 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00001992 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001993 0, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001994 mapp_methods, /* tp_methods */
1995 0, /* tp_members */
1996 0, /* tp_getset */
1997 0, /* tp_base */
1998 0, /* tp_dict */
1999 0, /* tp_descr_get */
2000 0, /* tp_descr_set */
2001 0, /* tp_dictoffset */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002002 dict_init, /* tp_init */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002003 PyType_GenericAlloc, /* tp_alloc */
2004 dict_new, /* tp_new */
Neil Schemenauer6189b892002-04-12 02:43:00 +00002005 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002006};
2007
Guido van Rossum3cca2451997-05-16 14:23:33 +00002008/* For backward compatibility with old dictionary interface */
2009
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002010PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002011PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002012{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002013 PyObject *kv, *rv;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002014 kv = PyUnicode_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00002015 if (kv == NULL)
2016 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002017 rv = PyDict_GetItem(v, kv);
2018 Py_DECREF(kv);
2019 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002020}
2021
2022int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002023PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002024{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002025 PyObject *kv;
2026 int err;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002027 kv = PyUnicode_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00002028 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002029 return -1;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002030 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00002031 err = PyDict_SetItem(v, kv, item);
2032 Py_DECREF(kv);
2033 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002034}
2035
2036int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002037PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002038{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002039 PyObject *kv;
2040 int err;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002041 kv = PyUnicode_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00002042 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002043 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002044 err = PyDict_DelItem(v, kv);
2045 Py_DECREF(kv);
2046 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002047}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002048
Raymond Hettinger019a1482004-03-18 02:41:19 +00002049/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002050
2051typedef struct {
2052 PyObject_HEAD
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002053 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002054 Py_ssize_t di_used;
2055 Py_ssize_t di_pos;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002056 PyObject* di_result; /* reusable result tuple for iteritems */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002057 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002058} dictiterobject;
2059
2060static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002061dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002062{
2063 dictiterobject *di;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002064 di = PyObject_New(dictiterobject, itertype);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002065 if (di == NULL)
2066 return NULL;
2067 Py_INCREF(dict);
2068 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00002069 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002070 di->di_pos = 0;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002071 di->len = dict->ma_used;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002072 if (itertype == &PyDictIterItem_Type) {
2073 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2074 if (di->di_result == NULL) {
2075 Py_DECREF(di);
2076 return NULL;
2077 }
2078 }
2079 else
2080 di->di_result = NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002081 return (PyObject *)di;
2082}
2083
2084static void
2085dictiter_dealloc(dictiterobject *di)
2086{
Guido van Rossum2147df72002-07-16 20:30:22 +00002087 Py_XDECREF(di->di_dict);
Raymond Hettinger019a1482004-03-18 02:41:19 +00002088 Py_XDECREF(di->di_result);
Neil Schemenauer6189b892002-04-12 02:43:00 +00002089 PyObject_Del(di);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002090}
2091
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002092static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002093dictiter_len(dictiterobject *di)
2094{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002095 Py_ssize_t len = 0;
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00002096 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002097 len = di->len;
Christian Heimes217cfd12007-12-02 14:31:20 +00002098 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002099}
2100
Guido van Rossumb90c8482007-02-10 01:11:45 +00002101PyDoc_STRVAR(length_hint_doc,
2102 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002103
2104static PyMethodDef dictiter_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002105 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
2106 length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002107 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002108};
2109
Raymond Hettinger019a1482004-03-18 02:41:19 +00002110static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002111{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002112 PyObject *key;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002113 register Py_ssize_t i, mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002114 register PyDictEntry *ep;
2115 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002116
Raymond Hettinger019a1482004-03-18 02:41:19 +00002117 if (d == NULL)
Guido van Rossum2147df72002-07-16 20:30:22 +00002118 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002119 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002120
Raymond Hettinger019a1482004-03-18 02:41:19 +00002121 if (di->di_used != d->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00002122 PyErr_SetString(PyExc_RuntimeError,
2123 "dictionary changed size during iteration");
Guido van Rossum2147df72002-07-16 20:30:22 +00002124 di->di_used = -1; /* Make this state sticky */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002125 return NULL;
2126 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002127
Raymond Hettinger019a1482004-03-18 02:41:19 +00002128 i = di->di_pos;
2129 if (i < 0)
2130 goto fail;
2131 ep = d->ma_table;
2132 mask = d->ma_mask;
2133 while (i <= mask && ep[i].me_value == NULL)
2134 i++;
2135 di->di_pos = i+1;
2136 if (i > mask)
2137 goto fail;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002138 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002139 key = ep[i].me_key;
2140 Py_INCREF(key);
2141 return key;
2142
2143fail:
2144 Py_DECREF(d);
Guido van Rossum2147df72002-07-16 20:30:22 +00002145 di->di_dict = NULL;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002146 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002147}
2148
Raymond Hettinger019a1482004-03-18 02:41:19 +00002149PyTypeObject PyDictIterKey_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002150 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Christian Heimesf83be4e2007-11-28 09:44:38 +00002151 "dict_keyiterator", /* tp_name */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002152 sizeof(dictiterobject), /* tp_basicsize */
2153 0, /* tp_itemsize */
2154 /* methods */
2155 (destructor)dictiter_dealloc, /* tp_dealloc */
2156 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002157 0, /* tp_getattr */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002158 0, /* tp_setattr */
2159 0, /* tp_compare */
2160 0, /* tp_repr */
2161 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002162 0, /* tp_as_sequence */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002163 0, /* tp_as_mapping */
2164 0, /* tp_hash */
2165 0, /* tp_call */
2166 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002167 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002168 0, /* tp_setattro */
2169 0, /* tp_as_buffer */
2170 Py_TPFLAGS_DEFAULT, /* tp_flags */
2171 0, /* tp_doc */
2172 0, /* tp_traverse */
2173 0, /* tp_clear */
2174 0, /* tp_richcompare */
2175 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002176 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002177 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002178 dictiter_methods, /* tp_methods */
2179 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002180};
2181
2182static PyObject *dictiter_iternextvalue(dictiterobject *di)
2183{
2184 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002185 register Py_ssize_t i, mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002186 register PyDictEntry *ep;
2187 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002188
2189 if (d == NULL)
2190 return NULL;
2191 assert (PyDict_Check(d));
2192
2193 if (di->di_used != d->ma_used) {
2194 PyErr_SetString(PyExc_RuntimeError,
2195 "dictionary changed size during iteration");
2196 di->di_used = -1; /* Make this state sticky */
2197 return NULL;
2198 }
2199
2200 i = di->di_pos;
Guido van Rossum09240f62004-03-20 19:11:58 +00002201 mask = d->ma_mask;
2202 if (i < 0 || i > mask)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002203 goto fail;
2204 ep = d->ma_table;
Guido van Rossum09240f62004-03-20 19:11:58 +00002205 while ((value=ep[i].me_value) == NULL) {
Raymond Hettinger019a1482004-03-18 02:41:19 +00002206 i++;
Guido van Rossum09240f62004-03-20 19:11:58 +00002207 if (i > mask)
2208 goto fail;
2209 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002210 di->di_pos = i+1;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002211 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002212 Py_INCREF(value);
2213 return value;
2214
2215fail:
2216 Py_DECREF(d);
2217 di->di_dict = NULL;
2218 return NULL;
2219}
2220
2221PyTypeObject PyDictIterValue_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002222 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Christian Heimesf83be4e2007-11-28 09:44:38 +00002223 "dict_valueiterator", /* tp_name */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002224 sizeof(dictiterobject), /* tp_basicsize */
2225 0, /* tp_itemsize */
2226 /* methods */
2227 (destructor)dictiter_dealloc, /* tp_dealloc */
2228 0, /* tp_print */
2229 0, /* tp_getattr */
2230 0, /* tp_setattr */
2231 0, /* tp_compare */
2232 0, /* tp_repr */
2233 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002234 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002235 0, /* tp_as_mapping */
2236 0, /* tp_hash */
2237 0, /* tp_call */
2238 0, /* tp_str */
2239 PyObject_GenericGetAttr, /* tp_getattro */
2240 0, /* tp_setattro */
2241 0, /* tp_as_buffer */
2242 Py_TPFLAGS_DEFAULT, /* tp_flags */
2243 0, /* tp_doc */
2244 0, /* tp_traverse */
2245 0, /* tp_clear */
2246 0, /* tp_richcompare */
2247 0, /* tp_weaklistoffset */
2248 PyObject_SelfIter, /* tp_iter */
2249 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002250 dictiter_methods, /* tp_methods */
2251 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002252};
2253
2254static PyObject *dictiter_iternextitem(dictiterobject *di)
2255{
2256 PyObject *key, *value, *result = di->di_result;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002257 register Py_ssize_t i, mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002258 register PyDictEntry *ep;
2259 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002260
2261 if (d == NULL)
2262 return NULL;
2263 assert (PyDict_Check(d));
2264
2265 if (di->di_used != d->ma_used) {
2266 PyErr_SetString(PyExc_RuntimeError,
2267 "dictionary changed size during iteration");
2268 di->di_used = -1; /* Make this state sticky */
2269 return NULL;
2270 }
2271
2272 i = di->di_pos;
2273 if (i < 0)
2274 goto fail;
2275 ep = d->ma_table;
2276 mask = d->ma_mask;
2277 while (i <= mask && ep[i].me_value == NULL)
2278 i++;
2279 di->di_pos = i+1;
2280 if (i > mask)
2281 goto fail;
2282
2283 if (result->ob_refcnt == 1) {
2284 Py_INCREF(result);
2285 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2286 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2287 } else {
2288 result = PyTuple_New(2);
Tim Peters60b29962006-01-01 01:19:23 +00002289 if (result == NULL)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002290 return NULL;
2291 }
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002292 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002293 key = ep[i].me_key;
2294 value = ep[i].me_value;
2295 Py_INCREF(key);
2296 Py_INCREF(value);
2297 PyTuple_SET_ITEM(result, 0, key);
2298 PyTuple_SET_ITEM(result, 1, value);
2299 return result;
2300
2301fail:
2302 Py_DECREF(d);
2303 di->di_dict = NULL;
2304 return NULL;
2305}
2306
2307PyTypeObject PyDictIterItem_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002308 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Christian Heimesf83be4e2007-11-28 09:44:38 +00002309 "dict_itemiterator", /* tp_name */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002310 sizeof(dictiterobject), /* tp_basicsize */
2311 0, /* tp_itemsize */
2312 /* methods */
2313 (destructor)dictiter_dealloc, /* tp_dealloc */
2314 0, /* tp_print */
2315 0, /* tp_getattr */
2316 0, /* tp_setattr */
2317 0, /* tp_compare */
2318 0, /* tp_repr */
2319 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002320 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002321 0, /* tp_as_mapping */
2322 0, /* tp_hash */
2323 0, /* tp_call */
2324 0, /* tp_str */
2325 PyObject_GenericGetAttr, /* tp_getattro */
2326 0, /* tp_setattro */
2327 0, /* tp_as_buffer */
2328 Py_TPFLAGS_DEFAULT, /* tp_flags */
2329 0, /* tp_doc */
2330 0, /* tp_traverse */
2331 0, /* tp_clear */
2332 0, /* tp_richcompare */
2333 0, /* tp_weaklistoffset */
2334 PyObject_SelfIter, /* tp_iter */
2335 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002336 dictiter_methods, /* tp_methods */
2337 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002338};
Guido van Rossumb90c8482007-02-10 01:11:45 +00002339
2340
Guido van Rossum3ac67412007-02-10 18:55:06 +00002341/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002342/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002343/***********************************************/
2344
Guido van Rossumb90c8482007-02-10 01:11:45 +00002345/* The instance lay-out is the same for all three; but the type differs. */
2346
2347typedef struct {
2348 PyObject_HEAD
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002349 PyDictObject *dv_dict;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002350} dictviewobject;
2351
2352
2353static void
Guido van Rossum3ac67412007-02-10 18:55:06 +00002354dictview_dealloc(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002355{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002356 Py_XDECREF(dv->dv_dict);
2357 PyObject_Del(dv);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002358}
2359
Guido van Rossum83825ac2007-02-10 04:54:19 +00002360static Py_ssize_t
Guido van Rossum3ac67412007-02-10 18:55:06 +00002361dictview_len(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002362{
2363 Py_ssize_t len = 0;
Guido van Rossum3ac67412007-02-10 18:55:06 +00002364 if (dv->dv_dict != NULL)
2365 len = dv->dv_dict->ma_used;
Guido van Rossum83825ac2007-02-10 04:54:19 +00002366 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002367}
2368
2369static PyObject *
2370dictview_new(PyObject *dict, PyTypeObject *type)
2371{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002372 dictviewobject *dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002373 if (dict == NULL) {
2374 PyErr_BadInternalCall();
2375 return NULL;
2376 }
2377 if (!PyDict_Check(dict)) {
2378 /* XXX Get rid of this restriction later */
2379 PyErr_Format(PyExc_TypeError,
2380 "%s() requires a dict argument, not '%s'",
2381 type->tp_name, dict->ob_type->tp_name);
2382 return NULL;
2383 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002384 dv = PyObject_New(dictviewobject, type);
2385 if (dv == NULL)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002386 return NULL;
2387 Py_INCREF(dict);
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002388 dv->dv_dict = (PyDictObject *)dict;
Guido van Rossum3ac67412007-02-10 18:55:06 +00002389 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002390}
2391
Neal Norwitze36f2ba2007-02-26 23:12:28 +00002392/* TODO(guido): The views objects are not complete:
2393
2394 * support more set operations
2395 * support arbitrary mappings?
2396 - either these should be static or exported in dictobject.h
2397 - if public then they should probably be in builtins
2398*/
2399
Guido van Rossumaac530c2007-08-24 22:33:45 +00002400/* Return 1 if self is a subset of other, iterating over self;
2401 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002402static int
2403all_contained_in(PyObject *self, PyObject *other)
2404{
2405 PyObject *iter = PyObject_GetIter(self);
2406 int ok = 1;
2407
2408 if (iter == NULL)
2409 return -1;
2410 for (;;) {
2411 PyObject *next = PyIter_Next(iter);
2412 if (next == NULL) {
2413 if (PyErr_Occurred())
2414 ok = -1;
2415 break;
2416 }
2417 ok = PySequence_Contains(other, next);
2418 Py_DECREF(next);
2419 if (ok <= 0)
2420 break;
2421 }
2422 Py_DECREF(iter);
2423 return ok;
2424}
2425
2426static PyObject *
2427dictview_richcompare(PyObject *self, PyObject *other, int op)
2428{
Guido van Rossumaac530c2007-08-24 22:33:45 +00002429 Py_ssize_t len_self, len_other;
2430 int ok;
2431 PyObject *result;
2432
Guido van Rossumd9214d12007-02-12 02:23:40 +00002433 assert(self != NULL);
2434 assert(PyDictViewSet_Check(self));
2435 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00002436
Guido van Rossumaac530c2007-08-24 22:33:45 +00002437 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other)) {
Guido van Rossumd9214d12007-02-12 02:23:40 +00002438 Py_INCREF(Py_NotImplemented);
2439 return Py_NotImplemented;
2440 }
Guido van Rossumaac530c2007-08-24 22:33:45 +00002441
2442 len_self = PyObject_Size(self);
2443 if (len_self < 0)
2444 return NULL;
2445 len_other = PyObject_Size(other);
2446 if (len_other < 0)
2447 return NULL;
2448
2449 ok = 0;
2450 switch(op) {
2451
2452 case Py_NE:
2453 case Py_EQ:
2454 if (len_self == len_other)
2455 ok = all_contained_in(self, other);
2456 if (op == Py_NE && ok >= 0)
2457 ok = !ok;
2458 break;
2459
2460 case Py_LT:
2461 if (len_self < len_other)
2462 ok = all_contained_in(self, other);
2463 break;
2464
2465 case Py_LE:
2466 if (len_self <= len_other)
2467 ok = all_contained_in(self, other);
2468 break;
2469
2470 case Py_GT:
2471 if (len_self > len_other)
2472 ok = all_contained_in(other, self);
2473 break;
2474
2475 case Py_GE:
2476 if (len_self >= len_other)
2477 ok = all_contained_in(other, self);
2478 break;
2479
2480 }
2481 if (ok < 0)
2482 return NULL;
2483 result = ok ? Py_True : Py_False;
2484 Py_INCREF(result);
2485 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00002486}
2487
Guido van Rossum3ac67412007-02-10 18:55:06 +00002488/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002489
2490static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002491dictkeys_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002492{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002493 if (dv->dv_dict == NULL) {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002494 Py_RETURN_NONE;
2495 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002496 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
2497}
2498
2499static int
2500dictkeys_contains(dictviewobject *dv, PyObject *obj)
2501{
2502 if (dv->dv_dict == NULL)
2503 return 0;
2504 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002505}
2506
Guido van Rossum83825ac2007-02-10 04:54:19 +00002507static PySequenceMethods dictkeys_as_sequence = {
2508 (lenfunc)dictview_len, /* sq_length */
2509 0, /* sq_concat */
2510 0, /* sq_repeat */
2511 0, /* sq_item */
2512 0, /* sq_slice */
2513 0, /* sq_ass_item */
2514 0, /* sq_ass_slice */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002515 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002516};
2517
Guido van Rossum523259b2007-08-24 23:41:22 +00002518static PyObject*
2519dictviews_sub(PyObject* self, PyObject *other)
2520{
2521 PyObject *result = PySet_New(self);
2522 PyObject *tmp;
2523 if (result == NULL)
2524 return NULL;
2525
2526 tmp = PyObject_CallMethod(result, "difference_update", "O", other);
2527 if (tmp == NULL) {
2528 Py_DECREF(result);
2529 return NULL;
2530 }
2531
2532 Py_DECREF(tmp);
2533 return result;
2534}
2535
2536static PyObject*
2537dictviews_and(PyObject* self, PyObject *other)
2538{
2539 PyObject *result = PySet_New(self);
2540 PyObject *tmp;
2541 if (result == NULL)
2542 return NULL;
2543
2544 tmp = PyObject_CallMethod(result, "intersection_update", "O", other);
2545 if (tmp == NULL) {
2546 Py_DECREF(result);
2547 return NULL;
2548 }
2549
2550 Py_DECREF(tmp);
2551 return result;
2552}
2553
2554static PyObject*
2555dictviews_or(PyObject* self, PyObject *other)
2556{
2557 PyObject *result = PySet_New(self);
2558 PyObject *tmp;
2559 if (result == NULL)
2560 return NULL;
2561
2562 tmp = PyObject_CallMethod(result, "update", "O", other);
2563 if (tmp == NULL) {
2564 Py_DECREF(result);
2565 return NULL;
2566 }
2567
2568 Py_DECREF(tmp);
2569 return result;
2570}
2571
2572static PyObject*
2573dictviews_xor(PyObject* self, PyObject *other)
2574{
2575 PyObject *result = PySet_New(self);
2576 PyObject *tmp;
2577 if (result == NULL)
2578 return NULL;
2579
2580 tmp = PyObject_CallMethod(result, "symmetric_difference_update", "O",
2581 other);
2582 if (tmp == NULL) {
2583 Py_DECREF(result);
2584 return NULL;
2585 }
2586
2587 Py_DECREF(tmp);
2588 return result;
2589}
2590
2591static PyNumberMethods dictviews_as_number = {
2592 0, /*nb_add*/
2593 (binaryfunc)dictviews_sub, /*nb_subtract*/
2594 0, /*nb_multiply*/
2595 0, /*nb_remainder*/
2596 0, /*nb_divmod*/
2597 0, /*nb_power*/
2598 0, /*nb_negative*/
2599 0, /*nb_positive*/
2600 0, /*nb_absolute*/
2601 0, /*nb_bool*/
2602 0, /*nb_invert*/
2603 0, /*nb_lshift*/
2604 0, /*nb_rshift*/
2605 (binaryfunc)dictviews_and, /*nb_and*/
2606 (binaryfunc)dictviews_xor, /*nb_xor*/
2607 (binaryfunc)dictviews_or, /*nb_or*/
2608};
2609
Guido van Rossumb90c8482007-02-10 01:11:45 +00002610static PyMethodDef dictkeys_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002611 {NULL, NULL} /* sentinel */
2612};
2613
2614PyTypeObject PyDictKeys_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002615 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002616 "dict_keys", /* tp_name */
2617 sizeof(dictviewobject), /* tp_basicsize */
2618 0, /* tp_itemsize */
2619 /* methods */
2620 (destructor)dictview_dealloc, /* tp_dealloc */
2621 0, /* tp_print */
2622 0, /* tp_getattr */
2623 0, /* tp_setattr */
2624 0, /* tp_compare */
2625 0, /* tp_repr */
Guido van Rossum523259b2007-08-24 23:41:22 +00002626 &dictviews_as_number, /* tp_as_number */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002627 &dictkeys_as_sequence, /* tp_as_sequence */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002628 0, /* tp_as_mapping */
2629 0, /* tp_hash */
2630 0, /* tp_call */
2631 0, /* tp_str */
2632 PyObject_GenericGetAttr, /* tp_getattro */
2633 0, /* tp_setattro */
2634 0, /* tp_as_buffer */
2635 Py_TPFLAGS_DEFAULT, /* tp_flags */
2636 0, /* tp_doc */
2637 0, /* tp_traverse */
2638 0, /* tp_clear */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002639 dictview_richcompare, /* tp_richcompare */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002640 0, /* tp_weaklistoffset */
2641 (getiterfunc)dictkeys_iter, /* tp_iter */
2642 0, /* tp_iternext */
2643 dictkeys_methods, /* tp_methods */
2644 0,
2645};
2646
2647static PyObject *
2648dictkeys_new(PyObject *dict)
2649{
2650 return dictview_new(dict, &PyDictKeys_Type);
2651}
2652
Guido van Rossum3ac67412007-02-10 18:55:06 +00002653/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002654
2655static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002656dictitems_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002657{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002658 if (dv->dv_dict == NULL) {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002659 Py_RETURN_NONE;
2660 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002661 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
2662}
2663
2664static int
2665dictitems_contains(dictviewobject *dv, PyObject *obj)
2666{
2667 PyObject *key, *value, *found;
2668 if (dv->dv_dict == NULL)
2669 return 0;
2670 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
2671 return 0;
2672 key = PyTuple_GET_ITEM(obj, 0);
2673 value = PyTuple_GET_ITEM(obj, 1);
2674 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
2675 if (found == NULL) {
2676 if (PyErr_Occurred())
2677 return -1;
2678 return 0;
2679 }
2680 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002681}
2682
Guido van Rossum83825ac2007-02-10 04:54:19 +00002683static PySequenceMethods dictitems_as_sequence = {
2684 (lenfunc)dictview_len, /* sq_length */
2685 0, /* sq_concat */
2686 0, /* sq_repeat */
2687 0, /* sq_item */
2688 0, /* sq_slice */
2689 0, /* sq_ass_item */
2690 0, /* sq_ass_slice */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002691 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002692};
2693
Guido van Rossumb90c8482007-02-10 01:11:45 +00002694static PyMethodDef dictitems_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002695 {NULL, NULL} /* sentinel */
2696};
2697
2698PyTypeObject PyDictItems_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002699 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002700 "dict_items", /* tp_name */
2701 sizeof(dictviewobject), /* tp_basicsize */
2702 0, /* tp_itemsize */
2703 /* methods */
2704 (destructor)dictview_dealloc, /* tp_dealloc */
2705 0, /* tp_print */
2706 0, /* tp_getattr */
2707 0, /* tp_setattr */
2708 0, /* tp_compare */
2709 0, /* tp_repr */
Guido van Rossum523259b2007-08-24 23:41:22 +00002710 &dictviews_as_number, /* tp_as_number */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002711 &dictitems_as_sequence, /* tp_as_sequence */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002712 0, /* tp_as_mapping */
2713 0, /* tp_hash */
2714 0, /* tp_call */
2715 0, /* tp_str */
2716 PyObject_GenericGetAttr, /* tp_getattro */
2717 0, /* tp_setattro */
2718 0, /* tp_as_buffer */
2719 Py_TPFLAGS_DEFAULT, /* tp_flags */
2720 0, /* tp_doc */
2721 0, /* tp_traverse */
2722 0, /* tp_clear */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002723 dictview_richcompare, /* tp_richcompare */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002724 0, /* tp_weaklistoffset */
2725 (getiterfunc)dictitems_iter, /* tp_iter */
2726 0, /* tp_iternext */
2727 dictitems_methods, /* tp_methods */
2728 0,
2729};
2730
2731static PyObject *
2732dictitems_new(PyObject *dict)
2733{
2734 return dictview_new(dict, &PyDictItems_Type);
2735}
2736
Guido van Rossum3ac67412007-02-10 18:55:06 +00002737/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002738
2739static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002740dictvalues_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002741{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002742 if (dv->dv_dict == NULL) {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002743 Py_RETURN_NONE;
2744 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002745 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002746}
2747
Guido van Rossum83825ac2007-02-10 04:54:19 +00002748static PySequenceMethods dictvalues_as_sequence = {
2749 (lenfunc)dictview_len, /* sq_length */
2750 0, /* sq_concat */
2751 0, /* sq_repeat */
2752 0, /* sq_item */
2753 0, /* sq_slice */
2754 0, /* sq_ass_item */
2755 0, /* sq_ass_slice */
2756 (objobjproc)0, /* sq_contains */
2757};
2758
Guido van Rossumb90c8482007-02-10 01:11:45 +00002759static PyMethodDef dictvalues_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002760 {NULL, NULL} /* sentinel */
2761};
2762
2763PyTypeObject PyDictValues_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002764 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002765 "dict_values", /* tp_name */
2766 sizeof(dictviewobject), /* tp_basicsize */
2767 0, /* tp_itemsize */
2768 /* methods */
2769 (destructor)dictview_dealloc, /* tp_dealloc */
2770 0, /* tp_print */
2771 0, /* tp_getattr */
2772 0, /* tp_setattr */
2773 0, /* tp_compare */
2774 0, /* tp_repr */
2775 0, /* tp_as_number */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002776 &dictvalues_as_sequence, /* tp_as_sequence */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002777 0, /* tp_as_mapping */
2778 0, /* tp_hash */
2779 0, /* tp_call */
2780 0, /* tp_str */
2781 PyObject_GenericGetAttr, /* tp_getattro */
2782 0, /* tp_setattro */
2783 0, /* tp_as_buffer */
2784 Py_TPFLAGS_DEFAULT, /* tp_flags */
2785 0, /* tp_doc */
2786 0, /* tp_traverse */
2787 0, /* tp_clear */
2788 0, /* tp_richcompare */
2789 0, /* tp_weaklistoffset */
2790 (getiterfunc)dictvalues_iter, /* tp_iter */
2791 0, /* tp_iternext */
2792 dictvalues_methods, /* tp_methods */
2793 0,
2794};
2795
2796static PyObject *
2797dictvalues_new(PyObject *dict)
2798{
2799 return dictview_new(dict, &PyDictValues_Type);
2800}