blob: 3d7ba559ad6f97fefb1de61918e2a126fe65b003 [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 */
187#define MAXFREEDICTS 80
188static PyDictObject *free_dicts[MAXFREEDICTS];
189static int num_free_dicts = 0;
190
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000191PyObject *
Thomas Wouters78890102000-07-22 19:25:51 +0000192PyDict_New(void)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000193{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000194 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000195 if (dummy == NULL) { /* Auto-initialize dummy */
Neal Norwitz53cbdaa2007-08-23 21:42:55 +0000196 dummy = PyUnicode_FromString("<dummy key>");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000197 if (dummy == NULL)
198 return NULL;
Fred Drake1bff34a2000-08-31 19:31:38 +0000199#ifdef SHOW_CONVERSION_COUNTS
200 Py_AtExit(show_counts);
201#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000202 }
Raymond Hettinger43442782004-03-17 21:55:03 +0000203 if (num_free_dicts) {
204 mp = free_dicts[--num_free_dicts];
205 assert (mp != NULL);
Christian Heimes90aa7642007-12-19 02:45:37 +0000206 assert (Py_TYPE(mp) == &PyDict_Type);
Raymond Hettinger43442782004-03-17 21:55:03 +0000207 _Py_NewReference((PyObject *)mp);
208 if (mp->ma_fill) {
209 EMPTY_TO_MINSIZE(mp);
210 }
211 assert (mp->ma_used == 0);
212 assert (mp->ma_table == mp->ma_smalltable);
213 assert (mp->ma_mask == PyDict_MINSIZE - 1);
214 } else {
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000215 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
Raymond Hettinger43442782004-03-17 21:55:03 +0000216 if (mp == NULL)
217 return NULL;
218 EMPTY_TO_MINSIZE(mp);
219 }
Guido van Rossum89d8c602007-09-18 17:26:56 +0000220 mp->ma_lookup = lookdict_unicode;
Fred Drake1bff34a2000-08-31 19:31:38 +0000221#ifdef SHOW_CONVERSION_COUNTS
222 ++created;
223#endif
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000224 _PyObject_GC_TRACK(mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000225 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000226}
227
228/*
229The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000230This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000231Open addressing is preferred over chaining since the link overhead for
232chaining would be substantial (100% with typical malloc overhead).
233
Tim Peterseb28ef22001-06-02 05:27:19 +0000234The initial probe index is computed as hash mod the table size. Subsequent
235probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000236
237All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000238
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000239The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000240contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000241Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000242
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000243lookdict() is general-purpose, and may return NULL if (and only if) a
244comparison raises an exception (this was new in Python 2.5).
Guido van Rossum89d8c602007-09-18 17:26:56 +0000245lookdict_unicode() below is specialized to string keys, comparison of which can
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000246never raise an exception; that function can never return NULL. For both, when
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000247the key isn't found a PyDictEntry* is returned for which the me_value field is
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000248NULL; this is the slot in the dict at which the key would have been found, and
249the caller can (if it wishes) add the <key, value> pair to the returned
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000250PyDictEntry*.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000251*/
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000252static PyDictEntry *
253lookdict(PyDictObject *mp, PyObject *key, register long hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000254{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000255 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000256 register size_t perturb;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000257 register PyDictEntry *freeslot;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000258 register size_t mask = (size_t)mp->ma_mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000259 PyDictEntry *ep0 = mp->ma_table;
260 register PyDictEntry *ep;
Fred Drakec88b99c2000-08-31 19:04:07 +0000261 register int cmp;
Tim Peters453163d2001-06-03 04:54:32 +0000262 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000263
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000264 i = (size_t)hash & mask;
Guido van Rossumefb46091997-01-29 15:53:56 +0000265 ep = &ep0[i];
Guido van Rossumc1c7b1a1998-10-06 16:01:14 +0000266 if (ep->me_key == NULL || ep->me_key == key)
Guido van Rossum7d181591997-01-16 21:06:45 +0000267 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000268
Guido van Rossum16e93a81997-01-28 00:00:11 +0000269 if (ep->me_key == dummy)
Guido van Rossum7d181591997-01-16 21:06:45 +0000270 freeslot = ep;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000271 else {
Fred Drakec88b99c2000-08-31 19:04:07 +0000272 if (ep->me_hash == hash) {
Tim Peters453163d2001-06-03 04:54:32 +0000273 startkey = ep->me_key;
Guido van Rossum2fd4f372007-11-29 18:43:05 +0000274 Py_INCREF(startkey);
Tim Peters453163d2001-06-03 04:54:32 +0000275 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Guido van Rossum2fd4f372007-11-29 18:43:05 +0000276 Py_DECREF(startkey);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000277 if (cmp < 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000278 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000279 if (ep0 == mp->ma_table && ep->me_key == startkey) {
280 if (cmp > 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000281 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000282 }
283 else {
284 /* The compare did major nasty stuff to the
285 * dict: start over.
286 * XXX A clever adversary could prevent this
287 * XXX from terminating.
288 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000289 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000290 }
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000291 }
292 freeslot = NULL;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000293 }
Tim Peters15d49292001-05-27 07:39:22 +0000294
Tim Peters342c65e2001-05-13 06:43:53 +0000295 /* In the loop, me_key == dummy is by far (factor of 100s) the
296 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000297 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
298 i = (i << 2) + i + perturb + 1;
299 ep = &ep0[i & mask];
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000300 if (ep->me_key == NULL)
301 return freeslot == NULL ? ep : freeslot;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000302 if (ep->me_key == key)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000303 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000304 if (ep->me_hash == hash && ep->me_key != dummy) {
Tim Peters453163d2001-06-03 04:54:32 +0000305 startkey = ep->me_key;
Guido van Rossum2fd4f372007-11-29 18:43:05 +0000306 Py_INCREF(startkey);
Tim Peters453163d2001-06-03 04:54:32 +0000307 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Guido van Rossum2fd4f372007-11-29 18:43:05 +0000308 Py_DECREF(startkey);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000309 if (cmp < 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000310 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000311 if (ep0 == mp->ma_table && ep->me_key == startkey) {
312 if (cmp > 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000313 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000314 }
315 else {
316 /* The compare did major nasty stuff to the
317 * dict: start over.
318 * XXX A clever adversary could prevent this
319 * XXX from terminating.
320 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000321 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000322 }
Guido van Rossum16e93a81997-01-28 00:00:11 +0000323 }
Tim Peters342c65e2001-05-13 06:43:53 +0000324 else if (ep->me_key == dummy && freeslot == NULL)
325 freeslot = ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000326 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000327 assert(0); /* NOT REACHED */
328 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000329}
330
331/*
Guido van Rossum89d8c602007-09-18 17:26:56 +0000332 * Hacked up version of lookdict which can assume keys are always
333 * unicodes; this assumption allows testing for errors during
334 * PyObject_RichCompareBool() to be dropped; unicode-unicode
335 * comparisons never raise exceptions. This also means we don't need
336 * to go through PyObject_RichCompareBool(); we can always use
337 * unicode_eq() directly.
Fred Drake1bff34a2000-08-31 19:31:38 +0000338 *
Guido van Rossum89d8c602007-09-18 17:26:56 +0000339 * This is valuable because dicts with only unicode keys are very common.
Fred Drake1bff34a2000-08-31 19:31:38 +0000340 */
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000341static PyDictEntry *
342lookdict_unicode(PyDictObject *mp, PyObject *key, register long hash)
Fred Drake1bff34a2000-08-31 19:31:38 +0000343{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000344 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000345 register size_t perturb;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000346 register PyDictEntry *freeslot;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000347 register size_t mask = (size_t)mp->ma_mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000348 PyDictEntry *ep0 = mp->ma_table;
349 register PyDictEntry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000350
Guido van Rossum89d8c602007-09-18 17:26:56 +0000351 /* Make sure this function doesn't have to handle non-unicode keys,
Tim Peters0ab085c2001-09-14 00:25:33 +0000352 including subclasses of str; e.g., one reason to subclass
Guido van Rossum89d8c602007-09-18 17:26:56 +0000353 unicodes is to override __eq__, and for speed we don't cater to
Tim Peters0ab085c2001-09-14 00:25:33 +0000354 that here. */
Guido van Rossum89d8c602007-09-18 17:26:56 +0000355 if (!PyUnicode_CheckExact(key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000356#ifdef SHOW_CONVERSION_COUNTS
357 ++converted;
358#endif
359 mp->ma_lookup = lookdict;
360 return lookdict(mp, key, hash);
361 }
Tim Peters2f228e72001-05-13 00:19:31 +0000362 i = hash & mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000363 ep = &ep0[i];
364 if (ep->me_key == NULL || ep->me_key == key)
365 return ep;
366 if (ep->me_key == dummy)
367 freeslot = ep;
368 else {
Guido van Rossum89d8c602007-09-18 17:26:56 +0000369 if (ep->me_hash == hash && unicode_eq(ep->me_key, key))
Fred Drake1bff34a2000-08-31 19:31:38 +0000370 return ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000371 freeslot = NULL;
372 }
Tim Peters15d49292001-05-27 07:39:22 +0000373
Tim Peters342c65e2001-05-13 06:43:53 +0000374 /* In the loop, me_key == dummy is by far (factor of 100s) the
375 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000376 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
377 i = (i << 2) + i + perturb + 1;
378 ep = &ep0[i & mask];
Tim Peters342c65e2001-05-13 06:43:53 +0000379 if (ep->me_key == NULL)
380 return freeslot == NULL ? ep : freeslot;
381 if (ep->me_key == key
382 || (ep->me_hash == hash
383 && ep->me_key != dummy
Guido van Rossum89d8c602007-09-18 17:26:56 +0000384 && unicode_eq(ep->me_key, key)))
Fred Drake1bff34a2000-08-31 19:31:38 +0000385 return ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000386 if (ep->me_key == dummy && freeslot == NULL)
Tim Peters342c65e2001-05-13 06:43:53 +0000387 freeslot = ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000388 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000389 assert(0); /* NOT REACHED */
390 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000391}
392
393/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000394Internal routine to insert a new item into the table.
395Used both by the internal resize routine and by the public insert routine.
396Eats a reference to key and one to value.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000397Returns -1 if an error occurred, or 0 on success.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000398*/
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000399static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000400insertdict(register PyDictObject *mp, PyObject *key, long hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000401{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000402 PyObject *old_value;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000403 register PyDictEntry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000404 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
405
406 assert(mp->ma_lookup != NULL);
407 ep = mp->ma_lookup(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000408 if (ep == NULL) {
409 Py_DECREF(key);
410 Py_DECREF(value);
411 return -1;
412 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000413 if (ep->me_value != NULL) {
Guido van Rossumd7047b31995-01-02 19:07:15 +0000414 old_value = ep->me_value;
415 ep->me_value = value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000416 Py_DECREF(old_value); /* which **CAN** re-enter */
417 Py_DECREF(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000418 }
419 else {
420 if (ep->me_key == NULL)
421 mp->ma_fill++;
Raymond Hettinger07ead172005-02-05 23:42:57 +0000422 else {
423 assert(ep->me_key == dummy);
424 Py_DECREF(dummy);
425 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000426 ep->me_key = key;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000427 ep->me_hash = (Py_ssize_t)hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000428 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000429 mp->ma_used++;
430 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000431 return 0;
432}
433
434/*
435Internal routine used by dictresize() to insert an item which is
436known to be absent from the dict. This routine also assumes that
437the dict contains no deleted entries. Besides the performance benefit,
438using insertdict() in dictresize() is dangerous (SF bug #1456209).
439Note that no refcounts are changed by this routine; if needed, the caller
440is responsible for incref'ing `key` and `value`.
441*/
442static void
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000443insertdict_clean(register PyDictObject *mp, PyObject *key, long hash,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000444 PyObject *value)
445{
446 register size_t i;
447 register size_t perturb;
448 register size_t mask = (size_t)mp->ma_mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000449 PyDictEntry *ep0 = mp->ma_table;
450 register PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000451
452 i = hash & mask;
453 ep = &ep0[i];
454 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
455 i = (i << 2) + i + perturb + 1;
456 ep = &ep0[i & mask];
457 }
458 assert(ep->me_value == NULL);
459 mp->ma_fill++;
460 ep->me_key = key;
461 ep->me_hash = (Py_ssize_t)hash;
462 ep->me_value = value;
463 mp->ma_used++;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000464}
465
466/*
467Restructure the table by allocating a new table and reinserting all
468items again. When entries have been deleted, the new table may
469actually be smaller than the old one.
470*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000471static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000472dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000473{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000474 Py_ssize_t newsize;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000475 PyDictEntry *oldtable, *newtable, *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000476 Py_ssize_t i;
Tim Peters0c6010b2001-05-23 23:33:57 +0000477 int is_oldtable_malloced;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000478 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000479
480 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000481
Tim Peterseb28ef22001-06-02 05:27:19 +0000482 /* Find the smallest table size > minused. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000483 for (newsize = PyDict_MINSIZE;
Tim Peterseb28ef22001-06-02 05:27:19 +0000484 newsize <= minused && newsize > 0;
485 newsize <<= 1)
486 ;
487 if (newsize <= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000488 PyErr_NoMemory();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000489 return -1;
490 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000491
492 /* Get space for a new table. */
493 oldtable = mp->ma_table;
494 assert(oldtable != NULL);
495 is_oldtable_malloced = oldtable != mp->ma_smalltable;
496
Tim Peters6d6c1a32001-08-02 04:15:00 +0000497 if (newsize == PyDict_MINSIZE) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000498 /* A large table is shrinking, or we can't get any smaller. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000499 newtable = mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000500 if (newtable == oldtable) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000501 if (mp->ma_fill == mp->ma_used) {
502 /* No dummies, so no point doing anything. */
Tim Peters0c6010b2001-05-23 23:33:57 +0000503 return 0;
Tim Petersf8a548c2001-05-24 16:26:40 +0000504 }
505 /* We're not going to resize it, but rebuild the
506 table anyway to purge old dummy entries.
507 Subtle: This is *necessary* if fill==size,
508 as lookdict needs at least one virgin slot to
509 terminate failing searches. If fill < size, it's
510 merely desirable, as dummies slow searches. */
511 assert(mp->ma_fill > mp->ma_used);
Tim Peters0c6010b2001-05-23 23:33:57 +0000512 memcpy(small_copy, oldtable, sizeof(small_copy));
513 oldtable = small_copy;
514 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000515 }
516 else {
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000517 newtable = PyMem_NEW(PyDictEntry, newsize);
Tim Petersdea48ec2001-05-22 20:40:22 +0000518 if (newtable == NULL) {
519 PyErr_NoMemory();
520 return -1;
521 }
522 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000523
524 /* Make the dict empty, using the new table. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000525 assert(newtable != oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000526 mp->ma_table = newtable;
Tim Petersafb6ae82001-06-04 21:00:21 +0000527 mp->ma_mask = newsize - 1;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000528 memset(newtable, 0, sizeof(PyDictEntry) * newsize);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000529 mp->ma_used = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000530 i = mp->ma_fill;
531 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000532
Tim Peters19283142001-05-17 22:25:34 +0000533 /* Copy the data over; this is refcount-neutral for active entries;
534 dummy entries aren't copied over, of course */
Tim Petersdea48ec2001-05-22 20:40:22 +0000535 for (ep = oldtable; i > 0; ep++) {
536 if (ep->me_value != NULL) { /* active entry */
537 --i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000538 insertdict_clean(mp, ep->me_key, (long)ep->me_hash,
539 ep->me_value);
Tim Petersdea48ec2001-05-22 20:40:22 +0000540 }
Tim Peters19283142001-05-17 22:25:34 +0000541 else if (ep->me_key != NULL) { /* dummy entry */
Tim Petersdea48ec2001-05-22 20:40:22 +0000542 --i;
Tim Peters19283142001-05-17 22:25:34 +0000543 assert(ep->me_key == dummy);
544 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000545 }
Tim Peters19283142001-05-17 22:25:34 +0000546 /* else key == value == NULL: nothing to do */
Guido van Rossumd7047b31995-01-02 19:07:15 +0000547 }
548
Tim Peters0c6010b2001-05-23 23:33:57 +0000549 if (is_oldtable_malloced)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000550 PyMem_DEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000551 return 0;
552}
553
Christian Heimes99170a52007-12-19 02:07:34 +0000554/* Create a new dictionary pre-sized to hold an estimated number of elements.
555 Underestimates are okay because the dictionary will resize as necessary.
556 Overestimates just mean the dictionary will be more sparse than usual.
557*/
558
559PyObject *
560_PyDict_NewPresized(Py_ssize_t minused)
561{
562 PyObject *op = PyDict_New();
563
564 if (minused>5 && op != NULL && dictresize((PyDictObject *)op, minused) == -1) {
565 Py_DECREF(op);
566 return NULL;
567 }
568 return op;
569}
570
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000571/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
572 * that may occur (originally dicts supported only string keys, and exceptions
573 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000574 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000575 * (suppressed) occurred while computing the key's hash, or that some error
576 * (suppressed) occurred when comparing keys in the dict's internal probe
577 * sequence. A nasty example of the latter is when a Python-coded comparison
578 * function hits a stack-depth error, which can cause this to return NULL
579 * even if the key is present.
580 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000581PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000582PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000583{
584 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000585 PyDictObject *mp = (PyDictObject *)op;
586 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000587 PyThreadState *tstate;
588 if (!PyDict_Check(op))
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000589 return NULL;
Guido van Rossum4d027722007-09-18 04:30:42 +0000590 if (!PyUnicode_CheckExact(key) ||
591 (hash = ((PyUnicodeObject *) key)->hash) == -1)
Guido van Rossumca756f21997-01-23 19:39:29 +0000592 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000593 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000594 if (hash == -1) {
595 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000596 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000597 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000598 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000599
600 /* We can arrive here with a NULL tstate during initialization:
601 try running "python -Wi" for an example related to string
602 interning. Let's just hope that no exception occurs then... */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000603 tstate = _PyThreadState_Current;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000604 if (tstate != NULL && tstate->curexc_type != NULL) {
605 /* preserve the existing exception */
606 PyObject *err_type, *err_value, *err_tb;
607 PyErr_Fetch(&err_type, &err_value, &err_tb);
608 ep = (mp->ma_lookup)(mp, key, hash);
609 /* ignore errors */
610 PyErr_Restore(err_type, err_value, err_tb);
611 if (ep == NULL)
612 return NULL;
613 }
614 else {
615 ep = (mp->ma_lookup)(mp, key, hash);
616 if (ep == NULL) {
617 PyErr_Clear();
618 return NULL;
619 }
620 }
621 return ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000622}
623
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000624/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
625 This returns NULL *with* an exception set if an exception occurred.
626 It returns NULL *without* an exception set if the key wasn't present.
627*/
628PyObject *
629PyDict_GetItemWithError(PyObject *op, PyObject *key)
630{
631 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000632 PyDictObject*mp = (PyDictObject *)op;
633 PyDictEntry *ep;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000634
635 if (!PyDict_Check(op)) {
636 PyErr_BadInternalCall();
637 return NULL;
638 }
Guido van Rossum89d8c602007-09-18 17:26:56 +0000639 if (!PyUnicode_CheckExact(key) ||
640 (hash = ((PyUnicodeObject *) key)->hash) == -1)
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000641 {
642 hash = PyObject_Hash(key);
643 if (hash == -1) {
644 return NULL;
645 }
646 }
647
648 ep = (mp->ma_lookup)(mp, key, hash);
649 if (ep == NULL)
650 return NULL;
651 return ep->me_value;
652}
653
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000654/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
Tim Peters60b29962006-01-01 01:19:23 +0000655 * dictionary if it's merely replacing the value for an existing key.
656 * This means that it's safe to loop over a dictionary with PyDict_Next()
657 * and occasionally replace a value -- but you can't insert new keys or
658 * remove them.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000659 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000660int
Tim Peters1f5871e2000-07-04 17:44:48 +0000661PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000662{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000663 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000664 register long hash;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000665 register Py_ssize_t n_used;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000666
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000667 if (!PyDict_Check(op)) {
668 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000669 return -1;
670 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000671 assert(key);
672 assert(value);
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000673 mp = (PyDictObject *)op;
Guido van Rossum4d027722007-09-18 04:30:42 +0000674 if (!PyUnicode_CheckExact(key) ||
675 (hash = ((PyUnicodeObject *) key)->hash) == -1)
676 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000677 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000678 if (hash == -1)
679 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000680 }
Tim Petersafb6ae82001-06-04 21:00:21 +0000681 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000682 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000683 Py_INCREF(value);
684 Py_INCREF(key);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000685 if (insertdict(mp, key, hash, value) != 0)
686 return -1;
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000687 /* If we added a key, we can safely resize. Otherwise just return!
688 * If fill >= 2/3 size, adjust size. Normally, this doubles or
689 * quaduples the size, but it's also possible for the dict to shrink
Tim Peters60b29962006-01-01 01:19:23 +0000690 * (if ma_fill is much larger than ma_used, meaning a lot of dict
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000691 * keys have been * deleted).
Tim Peters60b29962006-01-01 01:19:23 +0000692 *
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000693 * Quadrupling the size improves average dictionary sparseness
694 * (reducing collisions) at the cost of some memory and iteration
695 * speed (which loops over every possible entry). It also halves
696 * the number of expensive resize operations in a growing dictionary.
Tim Peters60b29962006-01-01 01:19:23 +0000697 *
698 * Very large dictionaries (over 50K items) use doubling instead.
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000699 * This may help applications with severe memory constraints.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000700 */
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000701 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
702 return 0;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000703 return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000704}
705
706int
Tim Peters1f5871e2000-07-04 17:44:48 +0000707PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000708{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000709 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000710 register long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000711 register PyDictEntry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000712 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000713
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000714 if (!PyDict_Check(op)) {
715 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000716 return -1;
717 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000718 assert(key);
Guido van Rossum89d8c602007-09-18 17:26:56 +0000719 if (!PyUnicode_CheckExact(key) ||
720 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000721 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000722 if (hash == -1)
723 return -1;
724 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000725 mp = (PyDictObject *)op;
Fred Drake1bff34a2000-08-31 19:31:38 +0000726 ep = (mp->ma_lookup)(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000727 if (ep == NULL)
728 return -1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000729 if (ep->me_value == NULL) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000730 set_key_error(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000731 return -1;
732 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000733 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000734 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000735 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000736 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000737 ep->me_value = NULL;
738 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000739 Py_DECREF(old_value);
740 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000741 return 0;
742}
743
Guido van Rossum25831651993-05-19 14:50:45 +0000744void
Tim Peters1f5871e2000-07-04 17:44:48 +0000745PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000746{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000747 PyDictObject *mp;
748 PyDictEntry *ep, *table;
Tim Petersdea48ec2001-05-22 20:40:22 +0000749 int table_is_malloced;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000750 Py_ssize_t fill;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000751 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Petersdea48ec2001-05-22 20:40:22 +0000752#ifdef Py_DEBUG
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000753 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +0000754#endif
755
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000756 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000757 return;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000758 mp = (PyDictObject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000759#ifdef Py_DEBUG
Tim Petersafb6ae82001-06-04 21:00:21 +0000760 n = mp->ma_mask + 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000761 i = 0;
762#endif
763
764 table = mp->ma_table;
765 assert(table != NULL);
766 table_is_malloced = table != mp->ma_smalltable;
767
768 /* This is delicate. During the process of clearing the dict,
769 * decrefs can cause the dict to mutate. To avoid fatal confusion
770 * (voice of experience), we have to make the dict empty before
771 * clearing the slots, and never refer to anything via mp->xxx while
772 * clearing.
773 */
774 fill = mp->ma_fill;
775 if (table_is_malloced)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000776 EMPTY_TO_MINSIZE(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000777
778 else if (fill > 0) {
779 /* It's a small table with something that needs to be cleared.
780 * Afraid the only safe way is to copy the dict entries into
781 * another small table first.
782 */
783 memcpy(small_copy, table, sizeof(small_copy));
784 table = small_copy;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000785 EMPTY_TO_MINSIZE(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000786 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000787 /* else it's a small table that's already empty */
788
789 /* Now we can finally clear things. If C had refcounts, we could
790 * assert that the refcount on table is 1 now, i.e. that this function
791 * has unique access to it, so decref side-effects can't alter it.
792 */
793 for (ep = table; fill > 0; ++ep) {
794#ifdef Py_DEBUG
795 assert(i < n);
796 ++i;
797#endif
798 if (ep->me_key) {
799 --fill;
800 Py_DECREF(ep->me_key);
801 Py_XDECREF(ep->me_value);
802 }
803#ifdef Py_DEBUG
804 else
805 assert(ep->me_value == NULL);
806#endif
807 }
808
809 if (table_is_malloced)
810 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000811}
812
Tim Peters080c88b2003-02-15 03:01:11 +0000813/*
814 * Iterate over a dict. Use like so:
815 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000816 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +0000817 * PyObject *key, *value;
818 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +0000819 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +0000820 * Refer to borrowed references in key and value.
821 * }
822 *
823 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +0000824 * mutates the dict. One exception: it is safe if the loop merely changes
825 * the values associated with the keys (but doesn't insert new keys or
826 * delete keys), via PyDict_SetItem().
827 */
Guido van Rossum25831651993-05-19 14:50:45 +0000828int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000829PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000830{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000831 register Py_ssize_t i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000832 register Py_ssize_t mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000833 register PyDictEntry *ep;
Raymond Hettinger43442782004-03-17 21:55:03 +0000834
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000835 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000836 return 0;
Guido van Rossum25831651993-05-19 14:50:45 +0000837 i = *ppos;
838 if (i < 0)
839 return 0;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000840 ep = ((PyDictObject *)op)->ma_table;
841 mask = ((PyDictObject *)op)->ma_mask;
Raymond Hettinger43442782004-03-17 21:55:03 +0000842 while (i <= mask && ep[i].me_value == NULL)
Guido van Rossum25831651993-05-19 14:50:45 +0000843 i++;
844 *ppos = i+1;
Raymond Hettinger43442782004-03-17 21:55:03 +0000845 if (i > mask)
Guido van Rossum25831651993-05-19 14:50:45 +0000846 return 0;
847 if (pkey)
Raymond Hettinger43442782004-03-17 21:55:03 +0000848 *pkey = ep[i].me_key;
Guido van Rossum25831651993-05-19 14:50:45 +0000849 if (pvalue)
Raymond Hettinger43442782004-03-17 21:55:03 +0000850 *pvalue = ep[i].me_value;
Guido van Rossum25831651993-05-19 14:50:45 +0000851 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000852}
853
Thomas Wouterscf297e42007-02-23 15:07:44 +0000854/* Internal version of PyDict_Next that returns a hash value in addition to the key and value.*/
855int
856_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue, long *phash)
857{
858 register Py_ssize_t i;
859 register Py_ssize_t mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000860 register PyDictEntry *ep;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000861
862 if (!PyDict_Check(op))
863 return 0;
864 i = *ppos;
865 if (i < 0)
866 return 0;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000867 ep = ((PyDictObject *)op)->ma_table;
868 mask = ((PyDictObject *)op)->ma_mask;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000869 while (i <= mask && ep[i].me_value == NULL)
870 i++;
871 *ppos = i+1;
872 if (i > mask)
873 return 0;
874 *phash = (long)(ep[i].me_hash);
875 if (pkey)
876 *pkey = ep[i].me_key;
877 if (pvalue)
878 *pvalue = ep[i].me_value;
879 return 1;
880}
881
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000882/* Methods */
883
884static void
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000885dict_dealloc(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000886{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000887 register PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000888 Py_ssize_t fill = mp->ma_fill;
Guido van Rossumff413af2002-03-28 20:34:59 +0000889 PyObject_GC_UnTrack(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000890 Py_TRASHCAN_SAFE_BEGIN(mp)
Tim Petersdea48ec2001-05-22 20:40:22 +0000891 for (ep = mp->ma_table; fill > 0; ep++) {
892 if (ep->me_key) {
893 --fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000894 Py_DECREF(ep->me_key);
Tim Petersdea48ec2001-05-22 20:40:22 +0000895 Py_XDECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000896 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000897 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000898 if (mp->ma_table != mp->ma_smalltable)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000899 PyMem_DEL(mp->ma_table);
Christian Heimes90aa7642007-12-19 02:45:37 +0000900 if (num_free_dicts < MAXFREEDICTS && Py_TYPE(mp) == &PyDict_Type)
Raymond Hettinger43442782004-03-17 21:55:03 +0000901 free_dicts[num_free_dicts++] = mp;
Tim Peters60b29962006-01-01 01:19:23 +0000902 else
Christian Heimes90aa7642007-12-19 02:45:37 +0000903 Py_TYPE(mp)->tp_free((PyObject *)mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000904 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000905}
906
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000907static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000908dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000909{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000910 Py_ssize_t i;
Tim Petersa7259592001-06-16 05:11:17 +0000911 PyObject *s, *temp, *colon = NULL;
912 PyObject *pieces = NULL, *result = NULL;
913 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +0000914
Tim Petersa7259592001-06-16 05:11:17 +0000915 i = Py_ReprEnter((PyObject *)mp);
Guido van Rossum255443b1998-04-10 22:47:14 +0000916 if (i != 0) {
Walter Dörwald1ab83302007-05-18 17:15:44 +0000917 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
Guido van Rossum255443b1998-04-10 22:47:14 +0000918 }
919
Tim Petersa7259592001-06-16 05:11:17 +0000920 if (mp->ma_used == 0) {
Walter Dörwald1ab83302007-05-18 17:15:44 +0000921 result = PyUnicode_FromString("{}");
Tim Petersa7259592001-06-16 05:11:17 +0000922 goto Done;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000923 }
Tim Petersa7259592001-06-16 05:11:17 +0000924
925 pieces = PyList_New(0);
926 if (pieces == NULL)
927 goto Done;
928
Walter Dörwald1ab83302007-05-18 17:15:44 +0000929 colon = PyUnicode_FromString(": ");
Tim Petersa7259592001-06-16 05:11:17 +0000930 if (colon == NULL)
931 goto Done;
932
933 /* Do repr() on each key+value pair, and insert ": " between them.
934 Note that repr may mutate the dict. */
Tim Petersc6057842001-06-16 07:52:53 +0000935 i = 0;
936 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Tim Petersa7259592001-06-16 05:11:17 +0000937 int status;
938 /* Prevent repr from deleting value during key format. */
939 Py_INCREF(value);
940 s = PyObject_Repr(key);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000941 PyUnicode_Append(&s, colon);
942 PyUnicode_AppendAndDel(&s, PyObject_Repr(value));
Tim Petersa7259592001-06-16 05:11:17 +0000943 Py_DECREF(value);
944 if (s == NULL)
945 goto Done;
946 status = PyList_Append(pieces, s);
947 Py_DECREF(s); /* append created a new ref */
948 if (status < 0)
949 goto Done;
950 }
951
952 /* Add "{}" decorations to the first and last items. */
953 assert(PyList_GET_SIZE(pieces) > 0);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000954 s = PyUnicode_FromString("{");
Tim Petersa7259592001-06-16 05:11:17 +0000955 if (s == NULL)
956 goto Done;
957 temp = PyList_GET_ITEM(pieces, 0);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000958 PyUnicode_AppendAndDel(&s, temp);
Tim Petersa7259592001-06-16 05:11:17 +0000959 PyList_SET_ITEM(pieces, 0, s);
960 if (s == NULL)
961 goto Done;
962
Walter Dörwald1ab83302007-05-18 17:15:44 +0000963 s = PyUnicode_FromString("}");
Tim Petersa7259592001-06-16 05:11:17 +0000964 if (s == NULL)
965 goto Done;
966 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000967 PyUnicode_AppendAndDel(&temp, s);
Tim Petersa7259592001-06-16 05:11:17 +0000968 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
969 if (temp == NULL)
970 goto Done;
971
972 /* Paste them all together with ", " between. */
Walter Dörwald1ab83302007-05-18 17:15:44 +0000973 s = PyUnicode_FromString(", ");
Tim Petersa7259592001-06-16 05:11:17 +0000974 if (s == NULL)
975 goto Done;
Walter Dörwald1ab83302007-05-18 17:15:44 +0000976 result = PyUnicode_Join(s, pieces);
Tim Peters0ab085c2001-09-14 00:25:33 +0000977 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +0000978
979Done:
980 Py_XDECREF(pieces);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000981 Py_XDECREF(colon);
Tim Petersa7259592001-06-16 05:11:17 +0000982 Py_ReprLeave((PyObject *)mp);
983 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000984}
985
Martin v. Löwis18e16552006-02-15 17:27:45 +0000986static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000987dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000988{
989 return mp->ma_used;
990}
991
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000992static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000993dict_subscript(PyDictObject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000994{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000995 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000996 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000997 PyDictEntry *ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000998 assert(mp->ma_table != NULL);
Guido van Rossum89d8c602007-09-18 17:26:56 +0000999 if (!PyUnicode_CheckExact(key) ||
1000 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001001 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001002 if (hash == -1)
1003 return NULL;
1004 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001005 ep = (mp->ma_lookup)(mp, key, hash);
1006 if (ep == NULL)
1007 return NULL;
1008 v = ep->me_value;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001009 if (v == NULL) {
1010 if (!PyDict_CheckExact(mp)) {
1011 /* Look up __missing__ method if we're a subclass. */
Guido van Rossum4b92a822006-02-25 23:32:30 +00001012 PyObject *missing;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001013 static PyObject *missing_str = NULL;
1014 if (missing_str == NULL)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001015 missing_str =
Martin v. Löwis5b222132007-06-10 09:51:05 +00001016 PyUnicode_InternFromString("__missing__");
Christian Heimes90aa7642007-12-19 02:45:37 +00001017 missing = _PyType_Lookup(Py_TYPE(mp), missing_str);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001018 if (missing != NULL)
1019 return PyObject_CallFunctionObjArgs(missing,
1020 (PyObject *)mp, key, NULL);
1021 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001022 set_key_error(key);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001023 return NULL;
1024 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001025 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001026 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001027 return v;
1028}
1029
1030static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001031dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001032{
1033 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001034 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001035 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001036 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001037}
1038
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001039static PyMappingMethods dict_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001040 (lenfunc)dict_length, /*mp_length*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001041 (binaryfunc)dict_subscript, /*mp_subscript*/
1042 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001043};
1044
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001045static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001046dict_keys(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001047{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001048 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001049 register Py_ssize_t i, j;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001050 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001051 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001052
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001053 again:
1054 n = mp->ma_used;
1055 v = PyList_New(n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001056 if (v == NULL)
1057 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001058 if (n != mp->ma_used) {
1059 /* Durnit. The allocations caused the dict to resize.
1060 * Just start over, this shouldn't normally happen.
1061 */
1062 Py_DECREF(v);
1063 goto again;
1064 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001065 ep = mp->ma_table;
1066 mask = mp->ma_mask;
1067 for (i = 0, j = 0; i <= mask; i++) {
1068 if (ep[i].me_value != NULL) {
1069 PyObject *key = ep[i].me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001070 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001071 PyList_SET_ITEM(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001072 j++;
1073 }
1074 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001075 assert(j == n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001076 return v;
1077}
1078
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001079static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001080dict_values(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001081{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001082 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001083 register Py_ssize_t i, j;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001084 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001085 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001086
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001087 again:
1088 n = mp->ma_used;
1089 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001090 if (v == NULL)
1091 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001092 if (n != mp->ma_used) {
1093 /* Durnit. The allocations caused the dict to resize.
1094 * Just start over, this shouldn't normally happen.
1095 */
1096 Py_DECREF(v);
1097 goto again;
1098 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001099 ep = mp->ma_table;
1100 mask = mp->ma_mask;
1101 for (i = 0, j = 0; i <= mask; i++) {
1102 if (ep[i].me_value != NULL) {
1103 PyObject *value = ep[i].me_value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001104 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001105 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001106 j++;
1107 }
1108 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001109 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001110 return v;
1111}
1112
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001113static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001114dict_items(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001115{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001116 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001117 register Py_ssize_t i, j, n;
1118 Py_ssize_t mask;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001119 PyObject *item, *key, *value;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001120 PyDictEntry *ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001121
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001122 /* Preallocate the list of tuples, to avoid allocations during
1123 * the loop over the items, which could trigger GC, which
1124 * could resize the dict. :-(
1125 */
1126 again:
1127 n = mp->ma_used;
1128 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001129 if (v == NULL)
1130 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001131 for (i = 0; i < n; i++) {
1132 item = PyTuple_New(2);
1133 if (item == NULL) {
1134 Py_DECREF(v);
1135 return NULL;
1136 }
1137 PyList_SET_ITEM(v, i, item);
1138 }
1139 if (n != mp->ma_used) {
1140 /* Durnit. The allocations caused the dict to resize.
1141 * Just start over, this shouldn't normally happen.
1142 */
1143 Py_DECREF(v);
1144 goto again;
1145 }
1146 /* Nothing we do below makes any function calls. */
Raymond Hettinger43442782004-03-17 21:55:03 +00001147 ep = mp->ma_table;
1148 mask = mp->ma_mask;
1149 for (i = 0, j = 0; i <= mask; i++) {
Raymond Hettinger06905122004-03-19 10:30:00 +00001150 if ((value=ep[i].me_value) != NULL) {
Raymond Hettinger43442782004-03-17 21:55:03 +00001151 key = ep[i].me_key;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001152 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001153 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001154 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001155 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001156 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001157 j++;
1158 }
1159 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001160 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001161 return v;
1162}
1163
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001164static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001165dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001166{
1167 PyObject *seq;
1168 PyObject *value = Py_None;
1169 PyObject *it; /* iter(seq) */
1170 PyObject *key;
1171 PyObject *d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001172 int status;
1173
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001174 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001175 return NULL;
1176
1177 d = PyObject_CallObject(cls, NULL);
1178 if (d == NULL)
1179 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001180
Guido van Rossum58da9312007-11-10 23:39:45 +00001181 if (PyDict_CheckExact(d) && PyDict_CheckExact(seq)) {
1182 PyDictObject *mp = (PyDictObject *)d;
1183 PyObject *oldvalue;
1184 Py_ssize_t pos = 0;
1185 PyObject *key;
1186 long hash;
1187
1188 if (dictresize(mp, PySet_GET_SIZE(seq)))
1189 return NULL;
1190
1191 while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
1192 Py_INCREF(key);
1193 Py_INCREF(value);
1194 if (insertdict(mp, key, hash, value))
1195 return NULL;
1196 }
1197 return d;
1198 }
1199
Guido van Rossumd8faa362007-04-27 19:54:29 +00001200 if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) {
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001201 PyDictObject *mp = (PyDictObject *)d;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001202 Py_ssize_t pos = 0;
1203 PyObject *key;
1204 long hash;
1205
1206 if (dictresize(mp, PySet_GET_SIZE(seq)))
1207 return NULL;
1208
1209 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
1210 Py_INCREF(key);
1211 Py_INCREF(value);
1212 if (insertdict(mp, key, hash, value))
1213 return NULL;
1214 }
1215 return d;
1216 }
1217
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001218 it = PyObject_GetIter(seq);
1219 if (it == NULL){
1220 Py_DECREF(d);
1221 return NULL;
1222 }
1223
Guido van Rossum58da9312007-11-10 23:39:45 +00001224 if (PyDict_CheckExact(d)) {
1225 while ((key = PyIter_Next(it)) != NULL) {
1226 status = PyDict_SetItem(d, key, value);
1227 Py_DECREF(key);
1228 if (status < 0)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001229 goto Fail;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001230 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001231 } else {
1232 while ((key = PyIter_Next(it)) != NULL) {
1233 status = PyObject_SetItem(d, key, value);
1234 Py_DECREF(key);
1235 if (status < 0)
1236 goto Fail;
1237 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001238 }
1239
Guido van Rossum58da9312007-11-10 23:39:45 +00001240 if (PyErr_Occurred())
1241 goto Fail;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001242 Py_DECREF(it);
1243 return d;
1244
1245Fail:
1246 Py_DECREF(it);
1247 Py_DECREF(d);
1248 return NULL;
1249}
1250
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001251static int
1252dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001253{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001254 PyObject *arg = NULL;
1255 int result = 0;
1256
1257 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1258 result = -1;
1259
1260 else if (arg != NULL) {
1261 if (PyObject_HasAttrString(arg, "keys"))
1262 result = PyDict_Merge(self, arg, 1);
1263 else
1264 result = PyDict_MergeFromSeq2(self, arg, 1);
1265 }
1266 if (result == 0 && kwds != NULL)
1267 result = PyDict_Merge(self, kwds, 1);
1268 return result;
1269}
1270
1271static PyObject *
1272dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1273{
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001274 if (dict_update_common(self, args, kwds, "update") != -1)
1275 Py_RETURN_NONE;
1276 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001277}
1278
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001279/* Update unconditionally replaces existing items.
1280 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001281 otherwise it leaves existing items unchanged.
1282
1283 PyDict_{Update,Merge} update/merge from a mapping object.
1284
Tim Petersf582b822001-12-11 18:51:08 +00001285 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001286 producing iterable objects of length 2.
1287*/
1288
Tim Petersf582b822001-12-11 18:51:08 +00001289int
Tim Peters1fc240e2001-10-26 05:06:50 +00001290PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1291{
1292 PyObject *it; /* iter(seq2) */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001293 Py_ssize_t i; /* index into seq2 of current element */
Tim Peters1fc240e2001-10-26 05:06:50 +00001294 PyObject *item; /* seq2[i] */
1295 PyObject *fast; /* item as a 2-tuple or 2-list */
1296
1297 assert(d != NULL);
1298 assert(PyDict_Check(d));
1299 assert(seq2 != NULL);
1300
1301 it = PyObject_GetIter(seq2);
1302 if (it == NULL)
1303 return -1;
1304
1305 for (i = 0; ; ++i) {
1306 PyObject *key, *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001307 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001308
1309 fast = NULL;
1310 item = PyIter_Next(it);
1311 if (item == NULL) {
1312 if (PyErr_Occurred())
1313 goto Fail;
1314 break;
1315 }
1316
1317 /* Convert item to sequence, and verify length 2. */
1318 fast = PySequence_Fast(item, "");
1319 if (fast == NULL) {
1320 if (PyErr_ExceptionMatches(PyExc_TypeError))
1321 PyErr_Format(PyExc_TypeError,
1322 "cannot convert dictionary update "
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001323 "sequence element #%zd to a sequence",
Tim Peters1fc240e2001-10-26 05:06:50 +00001324 i);
1325 goto Fail;
1326 }
1327 n = PySequence_Fast_GET_SIZE(fast);
1328 if (n != 2) {
1329 PyErr_Format(PyExc_ValueError,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001330 "dictionary update sequence element #%zd "
Martin v. Löwis2c95cc62006-02-16 06:54:25 +00001331 "has length %zd; 2 is required",
Martin v. Löwise0e89f72006-02-16 06:59:22 +00001332 i, n);
Tim Peters1fc240e2001-10-26 05:06:50 +00001333 goto Fail;
1334 }
1335
1336 /* Update/merge with this (key, value) pair. */
1337 key = PySequence_Fast_GET_ITEM(fast, 0);
1338 value = PySequence_Fast_GET_ITEM(fast, 1);
1339 if (override || PyDict_GetItem(d, key) == NULL) {
1340 int status = PyDict_SetItem(d, key, value);
1341 if (status < 0)
1342 goto Fail;
1343 }
1344 Py_DECREF(fast);
1345 Py_DECREF(item);
1346 }
1347
1348 i = 0;
1349 goto Return;
1350Fail:
1351 Py_XDECREF(item);
1352 Py_XDECREF(fast);
1353 i = -1;
1354Return:
1355 Py_DECREF(it);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001356 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001357}
1358
Tim Peters6d6c1a32001-08-02 04:15:00 +00001359int
1360PyDict_Update(PyObject *a, PyObject *b)
1361{
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001362 return PyDict_Merge(a, b, 1);
1363}
1364
1365int
1366PyDict_Merge(PyObject *a, PyObject *b, int override)
1367{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001368 register PyDictObject *mp, *other;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001369 register Py_ssize_t i;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001370 PyDictEntry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001371
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001372 /* We accept for the argument either a concrete dictionary object,
1373 * or an abstract "mapping" object. For the former, we can do
1374 * things quite efficiently. For the latter, we only require that
1375 * PyMapping_Keys() and PyObject_GetItem() be supported.
1376 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001377 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1378 PyErr_BadInternalCall();
1379 return -1;
1380 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001381 mp = (PyDictObject*)a;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001382 if (PyDict_CheckExact(b)) {
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001383 other = (PyDictObject*)b;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001384 if (other == mp || other->ma_used == 0)
1385 /* a.update(a) or a.update({}); nothing to do */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001386 return 0;
Raymond Hettinger186e7392005-05-14 18:08:25 +00001387 if (mp->ma_used == 0)
1388 /* Since the target dict is empty, PyDict_GetItem()
1389 * always returns NULL. Setting override to 1
1390 * skips the unnecessary test.
1391 */
1392 override = 1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001393 /* Do one big resize at the start, rather than
1394 * incrementally resizing as we insert new items. Expect
1395 * that there will be no (or few) overlapping keys.
1396 */
1397 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
Raymond Hettingerc8d22902003-05-07 00:49:40 +00001398 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001399 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001400 }
1401 for (i = 0; i <= other->ma_mask; i++) {
1402 entry = &other->ma_table[i];
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001403 if (entry->me_value != NULL &&
1404 (override ||
1405 PyDict_GetItem(a, entry->me_key) == NULL)) {
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001406 Py_INCREF(entry->me_key);
1407 Py_INCREF(entry->me_value);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001408 if (insertdict(mp, entry->me_key,
1409 (long)entry->me_hash,
1410 entry->me_value) != 0)
1411 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001412 }
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001413 }
1414 }
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001415 else {
1416 /* Do it the generic, slower way */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001417 PyObject *keys = PyMapping_Keys(b);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001418 PyObject *iter;
1419 PyObject *key, *value;
1420 int status;
1421
1422 if (keys == NULL)
1423 /* Docstring says this is equivalent to E.keys() so
1424 * if E doesn't have a .keys() method we want
1425 * AttributeError to percolate up. Might as well
1426 * do the same for any other error.
1427 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001428 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001429
1430 iter = PyObject_GetIter(keys);
1431 Py_DECREF(keys);
1432 if (iter == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001433 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001434
1435 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001436 if (!override && PyDict_GetItem(a, key) != NULL) {
1437 Py_DECREF(key);
1438 continue;
1439 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001440 value = PyObject_GetItem(b, key);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001441 if (value == NULL) {
1442 Py_DECREF(iter);
1443 Py_DECREF(key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001444 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001445 }
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001446 status = PyDict_SetItem(a, key, value);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001447 Py_DECREF(key);
1448 Py_DECREF(value);
1449 if (status < 0) {
1450 Py_DECREF(iter);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001451 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001452 }
1453 }
1454 Py_DECREF(iter);
1455 if (PyErr_Occurred())
1456 /* Iterator completed, via error */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001457 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001458 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001459 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001460}
1461
1462static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001463dict_copy(register PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001464{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001465 return PyDict_Copy((PyObject*)mp);
1466}
1467
1468PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001469PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001470{
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001471 PyObject *copy;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001472
1473 if (o == NULL || !PyDict_Check(o)) {
1474 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001475 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001476 }
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001477 copy = PyDict_New();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001478 if (copy == NULL)
1479 return NULL;
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001480 if (PyDict_Merge(copy, o, 1) == 0)
1481 return copy;
1482 Py_DECREF(copy);
Raymond Hettinger1356f782005-04-15 15:58:42 +00001483 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001484}
1485
Martin v. Löwis18e16552006-02-15 17:27:45 +00001486Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00001487PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001488{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001489 if (mp == NULL || !PyDict_Check(mp)) {
1490 PyErr_BadInternalCall();
Jeremy Hylton7083bb72004-02-17 20:10:11 +00001491 return -1;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001492 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001493 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001494}
1495
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001496PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001497PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001498{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001499 if (mp == NULL || !PyDict_Check(mp)) {
1500 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001501 return NULL;
1502 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001503 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001504}
1505
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001506PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001507PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001508{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001509 if (mp == NULL || !PyDict_Check(mp)) {
1510 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001511 return NULL;
1512 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001513 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001514}
1515
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001516PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001517PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001518{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001519 if (mp == NULL || !PyDict_Check(mp)) {
1520 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001521 return NULL;
1522 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001523 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001524}
1525
Tim Peterse63415e2001-05-08 04:38:29 +00001526/* Return 1 if dicts equal, 0 if not, -1 if error.
1527 * Gets out as soon as any difference is detected.
1528 * Uses only Py_EQ comparison.
1529 */
1530static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001531dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00001532{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001533 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00001534
1535 if (a->ma_used != b->ma_used)
1536 /* can't be equal if # of entries differ */
1537 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001538
Tim Peterse63415e2001-05-08 04:38:29 +00001539 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Tim Petersafb6ae82001-06-04 21:00:21 +00001540 for (i = 0; i <= a->ma_mask; i++) {
Tim Peterse63415e2001-05-08 04:38:29 +00001541 PyObject *aval = a->ma_table[i].me_value;
1542 if (aval != NULL) {
1543 int cmp;
1544 PyObject *bval;
1545 PyObject *key = a->ma_table[i].me_key;
1546 /* temporarily bump aval's refcount to ensure it stays
1547 alive until we're done with it */
1548 Py_INCREF(aval);
Guido van Rossumdc5f6b22006-08-24 21:29:26 +00001549 /* ditto for key */
1550 Py_INCREF(key);
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001551 bval = PyDict_GetItemWithError((PyObject *)b, key);
Guido van Rossumdc5f6b22006-08-24 21:29:26 +00001552 Py_DECREF(key);
Tim Peterse63415e2001-05-08 04:38:29 +00001553 if (bval == NULL) {
1554 Py_DECREF(aval);
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001555 if (PyErr_Occurred())
1556 return -1;
Tim Peterse63415e2001-05-08 04:38:29 +00001557 return 0;
1558 }
1559 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1560 Py_DECREF(aval);
1561 if (cmp <= 0) /* error or not equal */
1562 return cmp;
1563 }
1564 }
1565 return 1;
1566 }
1567
1568static PyObject *
1569dict_richcompare(PyObject *v, PyObject *w, int op)
1570{
1571 int cmp;
1572 PyObject *res;
1573
1574 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1575 res = Py_NotImplemented;
1576 }
1577 else if (op == Py_EQ || op == Py_NE) {
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001578 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
Tim Peterse63415e2001-05-08 04:38:29 +00001579 if (cmp < 0)
1580 return NULL;
1581 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1582 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001583 else
1584 res = Py_NotImplemented;
Tim Peterse63415e2001-05-08 04:38:29 +00001585 Py_INCREF(res);
1586 return res;
1587 }
1588
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001589static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001590dict_contains(register PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001591{
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001592 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001593 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001594
Guido van Rossum89d8c602007-09-18 17:26:56 +00001595 if (!PyUnicode_CheckExact(key) ||
1596 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001597 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001598 if (hash == -1)
1599 return NULL;
1600 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001601 ep = (mp->ma_lookup)(mp, key, hash);
1602 if (ep == NULL)
1603 return NULL;
1604 return PyBool_FromLong(ep->me_value != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001605}
1606
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001607static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001608dict_get(register PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001609{
1610 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001611 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001612 PyObject *val = NULL;
1613 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001614 PyDictEntry *ep;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001615
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001616 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001617 return NULL;
1618
Guido van Rossum89d8c602007-09-18 17:26:56 +00001619 if (!PyUnicode_CheckExact(key) ||
1620 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Barry Warsawc38c5da1997-10-06 17:49:20 +00001621 hash = PyObject_Hash(key);
1622 if (hash == -1)
1623 return NULL;
1624 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001625 ep = (mp->ma_lookup)(mp, key, hash);
1626 if (ep == NULL)
1627 return NULL;
1628 val = ep->me_value;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001629 if (val == NULL)
1630 val = failobj;
1631 Py_INCREF(val);
1632 return val;
1633}
1634
1635
1636static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001637dict_setdefault(register PyDictObject *mp, PyObject *args)
Guido van Rossum164452c2000-08-08 16:12:54 +00001638{
1639 PyObject *key;
1640 PyObject *failobj = Py_None;
1641 PyObject *val = NULL;
1642 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001643 PyDictEntry *ep;
Guido van Rossum164452c2000-08-08 16:12:54 +00001644
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001645 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
Guido van Rossum164452c2000-08-08 16:12:54 +00001646 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001647
Guido van Rossum89d8c602007-09-18 17:26:56 +00001648 if (!PyUnicode_CheckExact(key) ||
1649 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossum164452c2000-08-08 16:12:54 +00001650 hash = PyObject_Hash(key);
1651 if (hash == -1)
1652 return NULL;
1653 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001654 ep = (mp->ma_lookup)(mp, key, hash);
1655 if (ep == NULL)
1656 return NULL;
1657 val = ep->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001658 if (val == NULL) {
1659 val = failobj;
1660 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1661 val = NULL;
1662 }
1663 Py_XINCREF(val);
1664 return val;
1665}
1666
1667
1668static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001669dict_clear(register PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001670{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001671 PyDict_Clear((PyObject *)mp);
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001672 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001673}
1674
Guido van Rossumba6ab842000-12-12 22:02:18 +00001675static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001676dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00001677{
1678 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001679 PyDictEntry *ep;
Guido van Rossume027d982002-04-12 15:11:59 +00001680 PyObject *old_value, *old_key;
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001681 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001682
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001683 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1684 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001685 if (mp->ma_used == 0) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001686 if (deflt) {
1687 Py_INCREF(deflt);
1688 return deflt;
1689 }
Guido van Rossume027d982002-04-12 15:11:59 +00001690 PyErr_SetString(PyExc_KeyError,
1691 "pop(): dictionary is empty");
1692 return NULL;
1693 }
Guido van Rossum89d8c602007-09-18 17:26:56 +00001694 if (!PyUnicode_CheckExact(key) ||
1695 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossume027d982002-04-12 15:11:59 +00001696 hash = PyObject_Hash(key);
1697 if (hash == -1)
1698 return NULL;
1699 }
1700 ep = (mp->ma_lookup)(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001701 if (ep == NULL)
1702 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001703 if (ep->me_value == NULL) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001704 if (deflt) {
1705 Py_INCREF(deflt);
1706 return deflt;
1707 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001708 set_key_error(key);
Guido van Rossume027d982002-04-12 15:11:59 +00001709 return NULL;
1710 }
1711 old_key = ep->me_key;
1712 Py_INCREF(dummy);
1713 ep->me_key = dummy;
1714 old_value = ep->me_value;
1715 ep->me_value = NULL;
1716 mp->ma_used--;
1717 Py_DECREF(old_key);
1718 return old_value;
1719}
1720
1721static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001722dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001723{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001724 Py_ssize_t i = 0;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001725 PyDictEntry *ep;
Guido van Rossumba6ab842000-12-12 22:02:18 +00001726 PyObject *res;
1727
Tim Petersf4b33f62001-06-02 05:42:29 +00001728 /* Allocate the result tuple before checking the size. Believe it
1729 * or not, this allocation could trigger a garbage collection which
1730 * could empty the dict, so if we checked the size first and that
1731 * happened, the result would be an infinite loop (searching for an
1732 * entry that no longer exists). Note that the usual popitem()
1733 * idiom is "while d: k, v = d.popitem()". so needing to throw the
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001734 * tuple away if the dict *is* empty isn't a significant
Tim Petersf4b33f62001-06-02 05:42:29 +00001735 * inefficiency -- possible, but unlikely in practice.
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001736 */
1737 res = PyTuple_New(2);
1738 if (res == NULL)
1739 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001740 if (mp->ma_used == 0) {
1741 Py_DECREF(res);
1742 PyErr_SetString(PyExc_KeyError,
1743 "popitem(): dictionary is empty");
1744 return NULL;
1745 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001746 /* Set ep to "the first" dict entry with a value. We abuse the hash
1747 * field of slot 0 to hold a search finger:
1748 * If slot 0 has a value, use slot 0.
1749 * Else slot 0 is being used to hold a search finger,
1750 * and we use its hash value as the first index to look.
1751 */
1752 ep = &mp->ma_table[0];
1753 if (ep->me_value == NULL) {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001754 i = ep->me_hash;
Tim Petersdea48ec2001-05-22 20:40:22 +00001755 /* The hash field may be a real hash value, or it may be a
1756 * legit search finger, or it may be a once-legit search
1757 * finger that's out of bounds now because it wrapped around
1758 * or the table shrunk -- simply make sure it's in bounds now.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001759 */
Tim Petersafb6ae82001-06-04 21:00:21 +00001760 if (i > mp->ma_mask || i < 1)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001761 i = 1; /* skip slot 0 */
1762 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1763 i++;
Tim Petersafb6ae82001-06-04 21:00:21 +00001764 if (i > mp->ma_mask)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001765 i = 1;
1766 }
1767 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001768 PyTuple_SET_ITEM(res, 0, ep->me_key);
1769 PyTuple_SET_ITEM(res, 1, ep->me_value);
1770 Py_INCREF(dummy);
1771 ep->me_key = dummy;
1772 ep->me_value = NULL;
1773 mp->ma_used--;
1774 assert(mp->ma_table[0].me_value == NULL);
1775 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001776 return res;
1777}
1778
Jeremy Hylton8caad492000-06-23 14:18:11 +00001779static int
1780dict_traverse(PyObject *op, visitproc visit, void *arg)
1781{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001782 Py_ssize_t i = 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001783 PyObject *pk;
1784 PyObject *pv;
1785
1786 while (PyDict_Next(op, &i, &pk, &pv)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001787 Py_VISIT(pk);
1788 Py_VISIT(pv);
Jeremy Hylton8caad492000-06-23 14:18:11 +00001789 }
1790 return 0;
1791}
1792
1793static int
1794dict_tp_clear(PyObject *op)
1795{
1796 PyDict_Clear(op);
1797 return 0;
1798}
1799
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001800static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001801
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001802PyDoc_STRVAR(contains__doc__,
1803"D.__contains__(k) -> True if D has a key k, else False");
1804
1805PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
1806
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001807PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001808"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001809
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001810PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001811"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 +00001812
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001813PyDoc_STRVAR(pop__doc__,
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001814"D.pop(k[,d]) -> v, remove specified key and return the corresponding value\n\
1815If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00001816
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001817PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00001818"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000018192-tuple; but raise KeyError if D is empty");
Tim Petersf7f88b12000-12-13 23:18:45 +00001820
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001821PyDoc_STRVAR(update__doc__,
Guido van Rossumb90c8482007-02-10 01:11:45 +00001822"D.update(E, **F) -> None. Update D from E and F: for k in E: D[k] = E[k]\
1823\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 +00001824
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001825PyDoc_STRVAR(fromkeys__doc__,
1826"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
1827v defaults to None.");
1828
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001829PyDoc_STRVAR(clear__doc__,
1830"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001831
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001832PyDoc_STRVAR(copy__doc__,
1833"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00001834
Guido van Rossumb90c8482007-02-10 01:11:45 +00001835/* Forward */
1836static PyObject *dictkeys_new(PyObject *);
1837static PyObject *dictitems_new(PyObject *);
1838static PyObject *dictvalues_new(PyObject *);
1839
Guido van Rossum45c85d12007-07-27 16:31:40 +00001840PyDoc_STRVAR(keys__doc__,
1841 "D.keys() -> a set-like object providing a view on D's keys");
1842PyDoc_STRVAR(items__doc__,
1843 "D.items() -> a set-like object providing a view on D's items");
1844PyDoc_STRVAR(values__doc__,
1845 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00001846
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001847static PyMethodDef mapp_methods[] = {
Guido van Rossume2b70bc2006-08-18 22:13:04 +00001848 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001849 contains__doc__},
Raymond Hettinger0c669672003-12-13 13:31:55 +00001850 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001851 getitem__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00001852 {"get", (PyCFunction)dict_get, METH_VARARGS,
1853 get__doc__},
1854 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
1855 setdefault_doc__},
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001856 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
Just van Rossuma797d812002-11-23 09:45:04 +00001857 pop__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001858 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001859 popitem__doc__},
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001860 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
Guido van Rossumeff072c2007-02-27 05:47:18 +00001861 keys__doc__},
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001862 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
Guido van Rossumeff072c2007-02-27 05:47:18 +00001863 items__doc__},
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001864 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
Guido van Rossumeff072c2007-02-27 05:47:18 +00001865 values__doc__},
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001866 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001867 update__doc__},
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001868 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
1869 fromkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001870 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001871 clear__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001872 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001873 copy__doc__},
1874 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001875};
1876
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001877/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001878int
1879PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001880{
1881 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001882 PyDictObject *mp = (PyDictObject *)op;
1883 PyDictEntry *ep;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001884
Guido van Rossum89d8c602007-09-18 17:26:56 +00001885 if (!PyUnicode_CheckExact(key) ||
1886 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001887 hash = PyObject_Hash(key);
1888 if (hash == -1)
1889 return -1;
1890 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001891 ep = (mp->ma_lookup)(mp, key, hash);
1892 return ep == NULL ? -1 : (ep->me_value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001893}
1894
Thomas Wouterscf297e42007-02-23 15:07:44 +00001895/* Internal version of PyDict_Contains used when the hash value is already known */
1896int
1897_PyDict_Contains(PyObject *op, PyObject *key, long hash)
1898{
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001899 PyDictObject *mp = (PyDictObject *)op;
1900 PyDictEntry *ep;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001901
1902 ep = (mp->ma_lookup)(mp, key, hash);
1903 return ep == NULL ? -1 : (ep->me_value != NULL);
1904}
1905
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001906/* Hack to implement "key in dict" */
1907static PySequenceMethods dict_as_sequence = {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001908 0, /* sq_length */
1909 0, /* sq_concat */
1910 0, /* sq_repeat */
1911 0, /* sq_item */
1912 0, /* sq_slice */
1913 0, /* sq_ass_item */
1914 0, /* sq_ass_slice */
1915 PyDict_Contains, /* sq_contains */
1916 0, /* sq_inplace_concat */
1917 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001918};
1919
Guido van Rossum09e563a2001-05-01 12:10:21 +00001920static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001921dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1922{
1923 PyObject *self;
1924
1925 assert(type != NULL && type->tp_alloc != NULL);
1926 self = type->tp_alloc(type, 0);
1927 if (self != NULL) {
1928 PyDictObject *d = (PyDictObject *)self;
1929 /* It's guaranteed that tp->alloc zeroed out the struct. */
1930 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
1931 INIT_NONZERO_DICT_SLOTS(d);
Guido van Rossum89d8c602007-09-18 17:26:56 +00001932 d->ma_lookup = lookdict_unicode;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001933#ifdef SHOW_CONVERSION_COUNTS
1934 ++created;
1935#endif
1936 }
1937 return self;
1938}
1939
Tim Peters25786c02001-09-02 08:22:48 +00001940static int
1941dict_init(PyObject *self, PyObject *args, PyObject *kwds)
1942{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001943 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00001944}
1945
Tim Peters6d6c1a32001-08-02 04:15:00 +00001946static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001947dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001948{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001949 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001950}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001951
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001952PyDoc_STRVAR(dictionary_doc,
Tim Petersa427a2b2001-10-29 22:25:45 +00001953"dict() -> new empty dictionary.\n"
1954"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Tim Peters1fc240e2001-10-26 05:06:50 +00001955" (key, value) pairs.\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00001956"dict(seq) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00001957" d = {}\n"
1958" for k, v in seq:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00001959" d[k] = v\n"
1960"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
1961" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00001962
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001963PyTypeObject PyDict_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001964 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Tim Petersa427a2b2001-10-29 22:25:45 +00001965 "dict",
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001966 sizeof(PyDictObject),
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001967 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00001968 (destructor)dict_dealloc, /* tp_dealloc */
Guido van Rossum04dbf3b2007-08-07 19:51:00 +00001969 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001970 0, /* tp_getattr */
Guido van Rossumb9324202001-01-18 00:39:02 +00001971 0, /* tp_setattr */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001972 0, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00001973 (reprfunc)dict_repr, /* tp_repr */
1974 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001975 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00001976 &dict_as_mapping, /* tp_as_mapping */
Guido van Rossum50e9fb92006-08-17 05:42:55 +00001977 0, /* tp_hash */
Guido van Rossumb9324202001-01-18 00:39:02 +00001978 0, /* tp_call */
1979 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001980 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumb9324202001-01-18 00:39:02 +00001981 0, /* tp_setattro */
1982 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00001983 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Thomas Wouters27d517b2007-02-25 20:39:11 +00001984 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
Tim Peters25786c02001-09-02 08:22:48 +00001985 dictionary_doc, /* tp_doc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001986 dict_traverse, /* tp_traverse */
1987 dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00001988 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001989 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00001990 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001991 0, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001992 mapp_methods, /* tp_methods */
1993 0, /* tp_members */
1994 0, /* tp_getset */
1995 0, /* tp_base */
1996 0, /* tp_dict */
1997 0, /* tp_descr_get */
1998 0, /* tp_descr_set */
1999 0, /* tp_dictoffset */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002000 dict_init, /* tp_init */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002001 PyType_GenericAlloc, /* tp_alloc */
2002 dict_new, /* tp_new */
Neil Schemenauer6189b892002-04-12 02:43:00 +00002003 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002004};
2005
Guido van Rossum3cca2451997-05-16 14:23:33 +00002006/* For backward compatibility with old dictionary interface */
2007
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002008PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002009PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002010{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002011 PyObject *kv, *rv;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002012 kv = PyUnicode_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00002013 if (kv == NULL)
2014 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002015 rv = PyDict_GetItem(v, kv);
2016 Py_DECREF(kv);
2017 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002018}
2019
2020int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002021PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002022{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002023 PyObject *kv;
2024 int err;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002025 kv = PyUnicode_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00002026 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002027 return -1;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002028 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00002029 err = PyDict_SetItem(v, kv, item);
2030 Py_DECREF(kv);
2031 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002032}
2033
2034int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002035PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002036{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002037 PyObject *kv;
2038 int err;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002039 kv = PyUnicode_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00002040 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002041 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002042 err = PyDict_DelItem(v, kv);
2043 Py_DECREF(kv);
2044 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002045}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002046
Raymond Hettinger019a1482004-03-18 02:41:19 +00002047/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002048
2049typedef struct {
2050 PyObject_HEAD
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002051 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002052 Py_ssize_t di_used;
2053 Py_ssize_t di_pos;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002054 PyObject* di_result; /* reusable result tuple for iteritems */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002055 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002056} dictiterobject;
2057
2058static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002059dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002060{
2061 dictiterobject *di;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002062 di = PyObject_New(dictiterobject, itertype);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002063 if (di == NULL)
2064 return NULL;
2065 Py_INCREF(dict);
2066 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00002067 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002068 di->di_pos = 0;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002069 di->len = dict->ma_used;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002070 if (itertype == &PyDictIterItem_Type) {
2071 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2072 if (di->di_result == NULL) {
2073 Py_DECREF(di);
2074 return NULL;
2075 }
2076 }
2077 else
2078 di->di_result = NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002079 return (PyObject *)di;
2080}
2081
2082static void
2083dictiter_dealloc(dictiterobject *di)
2084{
Guido van Rossum2147df72002-07-16 20:30:22 +00002085 Py_XDECREF(di->di_dict);
Raymond Hettinger019a1482004-03-18 02:41:19 +00002086 Py_XDECREF(di->di_result);
Neil Schemenauer6189b892002-04-12 02:43:00 +00002087 PyObject_Del(di);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002088}
2089
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002090static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002091dictiter_len(dictiterobject *di)
2092{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002093 Py_ssize_t len = 0;
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00002094 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002095 len = di->len;
Christian Heimes217cfd12007-12-02 14:31:20 +00002096 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002097}
2098
Guido van Rossumb90c8482007-02-10 01:11:45 +00002099PyDoc_STRVAR(length_hint_doc,
2100 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002101
2102static PyMethodDef dictiter_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002103 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
2104 length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002105 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002106};
2107
Raymond Hettinger019a1482004-03-18 02:41:19 +00002108static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002109{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002110 PyObject *key;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002111 register Py_ssize_t i, mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002112 register PyDictEntry *ep;
2113 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002114
Raymond Hettinger019a1482004-03-18 02:41:19 +00002115 if (d == NULL)
Guido van Rossum2147df72002-07-16 20:30:22 +00002116 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002117 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002118
Raymond Hettinger019a1482004-03-18 02:41:19 +00002119 if (di->di_used != d->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00002120 PyErr_SetString(PyExc_RuntimeError,
2121 "dictionary changed size during iteration");
Guido van Rossum2147df72002-07-16 20:30:22 +00002122 di->di_used = -1; /* Make this state sticky */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002123 return NULL;
2124 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002125
Raymond Hettinger019a1482004-03-18 02:41:19 +00002126 i = di->di_pos;
2127 if (i < 0)
2128 goto fail;
2129 ep = d->ma_table;
2130 mask = d->ma_mask;
2131 while (i <= mask && ep[i].me_value == NULL)
2132 i++;
2133 di->di_pos = i+1;
2134 if (i > mask)
2135 goto fail;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002136 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002137 key = ep[i].me_key;
2138 Py_INCREF(key);
2139 return key;
2140
2141fail:
2142 Py_DECREF(d);
Guido van Rossum2147df72002-07-16 20:30:22 +00002143 di->di_dict = NULL;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002144 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002145}
2146
Raymond Hettinger019a1482004-03-18 02:41:19 +00002147PyTypeObject PyDictIterKey_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002148 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Christian Heimesf83be4e2007-11-28 09:44:38 +00002149 "dict_keyiterator", /* tp_name */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002150 sizeof(dictiterobject), /* tp_basicsize */
2151 0, /* tp_itemsize */
2152 /* methods */
2153 (destructor)dictiter_dealloc, /* tp_dealloc */
2154 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002155 0, /* tp_getattr */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002156 0, /* tp_setattr */
2157 0, /* tp_compare */
2158 0, /* tp_repr */
2159 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002160 0, /* tp_as_sequence */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002161 0, /* tp_as_mapping */
2162 0, /* tp_hash */
2163 0, /* tp_call */
2164 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002165 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002166 0, /* tp_setattro */
2167 0, /* tp_as_buffer */
2168 Py_TPFLAGS_DEFAULT, /* tp_flags */
2169 0, /* tp_doc */
2170 0, /* tp_traverse */
2171 0, /* tp_clear */
2172 0, /* tp_richcompare */
2173 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002174 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002175 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002176 dictiter_methods, /* tp_methods */
2177 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002178};
2179
2180static PyObject *dictiter_iternextvalue(dictiterobject *di)
2181{
2182 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002183 register Py_ssize_t i, mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002184 register PyDictEntry *ep;
2185 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002186
2187 if (d == NULL)
2188 return NULL;
2189 assert (PyDict_Check(d));
2190
2191 if (di->di_used != d->ma_used) {
2192 PyErr_SetString(PyExc_RuntimeError,
2193 "dictionary changed size during iteration");
2194 di->di_used = -1; /* Make this state sticky */
2195 return NULL;
2196 }
2197
2198 i = di->di_pos;
Guido van Rossum09240f62004-03-20 19:11:58 +00002199 mask = d->ma_mask;
2200 if (i < 0 || i > mask)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002201 goto fail;
2202 ep = d->ma_table;
Guido van Rossum09240f62004-03-20 19:11:58 +00002203 while ((value=ep[i].me_value) == NULL) {
Raymond Hettinger019a1482004-03-18 02:41:19 +00002204 i++;
Guido van Rossum09240f62004-03-20 19:11:58 +00002205 if (i > mask)
2206 goto fail;
2207 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002208 di->di_pos = i+1;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002209 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002210 Py_INCREF(value);
2211 return value;
2212
2213fail:
2214 Py_DECREF(d);
2215 di->di_dict = NULL;
2216 return NULL;
2217}
2218
2219PyTypeObject PyDictIterValue_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002220 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Christian Heimesf83be4e2007-11-28 09:44:38 +00002221 "dict_valueiterator", /* tp_name */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002222 sizeof(dictiterobject), /* tp_basicsize */
2223 0, /* tp_itemsize */
2224 /* methods */
2225 (destructor)dictiter_dealloc, /* tp_dealloc */
2226 0, /* tp_print */
2227 0, /* tp_getattr */
2228 0, /* tp_setattr */
2229 0, /* tp_compare */
2230 0, /* tp_repr */
2231 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002232 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002233 0, /* tp_as_mapping */
2234 0, /* tp_hash */
2235 0, /* tp_call */
2236 0, /* tp_str */
2237 PyObject_GenericGetAttr, /* tp_getattro */
2238 0, /* tp_setattro */
2239 0, /* tp_as_buffer */
2240 Py_TPFLAGS_DEFAULT, /* tp_flags */
2241 0, /* tp_doc */
2242 0, /* tp_traverse */
2243 0, /* tp_clear */
2244 0, /* tp_richcompare */
2245 0, /* tp_weaklistoffset */
2246 PyObject_SelfIter, /* tp_iter */
2247 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002248 dictiter_methods, /* tp_methods */
2249 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002250};
2251
2252static PyObject *dictiter_iternextitem(dictiterobject *di)
2253{
2254 PyObject *key, *value, *result = di->di_result;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002255 register Py_ssize_t i, mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002256 register PyDictEntry *ep;
2257 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002258
2259 if (d == NULL)
2260 return NULL;
2261 assert (PyDict_Check(d));
2262
2263 if (di->di_used != d->ma_used) {
2264 PyErr_SetString(PyExc_RuntimeError,
2265 "dictionary changed size during iteration");
2266 di->di_used = -1; /* Make this state sticky */
2267 return NULL;
2268 }
2269
2270 i = di->di_pos;
2271 if (i < 0)
2272 goto fail;
2273 ep = d->ma_table;
2274 mask = d->ma_mask;
2275 while (i <= mask && ep[i].me_value == NULL)
2276 i++;
2277 di->di_pos = i+1;
2278 if (i > mask)
2279 goto fail;
2280
2281 if (result->ob_refcnt == 1) {
2282 Py_INCREF(result);
2283 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2284 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2285 } else {
2286 result = PyTuple_New(2);
Tim Peters60b29962006-01-01 01:19:23 +00002287 if (result == NULL)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002288 return NULL;
2289 }
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002290 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002291 key = ep[i].me_key;
2292 value = ep[i].me_value;
2293 Py_INCREF(key);
2294 Py_INCREF(value);
2295 PyTuple_SET_ITEM(result, 0, key);
2296 PyTuple_SET_ITEM(result, 1, value);
2297 return result;
2298
2299fail:
2300 Py_DECREF(d);
2301 di->di_dict = NULL;
2302 return NULL;
2303}
2304
2305PyTypeObject PyDictIterItem_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002306 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Christian Heimesf83be4e2007-11-28 09:44:38 +00002307 "dict_itemiterator", /* tp_name */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002308 sizeof(dictiterobject), /* tp_basicsize */
2309 0, /* tp_itemsize */
2310 /* methods */
2311 (destructor)dictiter_dealloc, /* tp_dealloc */
2312 0, /* tp_print */
2313 0, /* tp_getattr */
2314 0, /* tp_setattr */
2315 0, /* tp_compare */
2316 0, /* tp_repr */
2317 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002318 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002319 0, /* tp_as_mapping */
2320 0, /* tp_hash */
2321 0, /* tp_call */
2322 0, /* tp_str */
2323 PyObject_GenericGetAttr, /* tp_getattro */
2324 0, /* tp_setattro */
2325 0, /* tp_as_buffer */
2326 Py_TPFLAGS_DEFAULT, /* tp_flags */
2327 0, /* tp_doc */
2328 0, /* tp_traverse */
2329 0, /* tp_clear */
2330 0, /* tp_richcompare */
2331 0, /* tp_weaklistoffset */
2332 PyObject_SelfIter, /* tp_iter */
2333 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002334 dictiter_methods, /* tp_methods */
2335 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002336};
Guido van Rossumb90c8482007-02-10 01:11:45 +00002337
2338
Guido van Rossum3ac67412007-02-10 18:55:06 +00002339/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002340/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002341/***********************************************/
2342
Guido van Rossumb90c8482007-02-10 01:11:45 +00002343/* The instance lay-out is the same for all three; but the type differs. */
2344
2345typedef struct {
2346 PyObject_HEAD
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002347 PyDictObject *dv_dict;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002348} dictviewobject;
2349
2350
2351static void
Guido van Rossum3ac67412007-02-10 18:55:06 +00002352dictview_dealloc(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002353{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002354 Py_XDECREF(dv->dv_dict);
2355 PyObject_Del(dv);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002356}
2357
Guido van Rossum83825ac2007-02-10 04:54:19 +00002358static Py_ssize_t
Guido van Rossum3ac67412007-02-10 18:55:06 +00002359dictview_len(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002360{
2361 Py_ssize_t len = 0;
Guido van Rossum3ac67412007-02-10 18:55:06 +00002362 if (dv->dv_dict != NULL)
2363 len = dv->dv_dict->ma_used;
Guido van Rossum83825ac2007-02-10 04:54:19 +00002364 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002365}
2366
2367static PyObject *
2368dictview_new(PyObject *dict, PyTypeObject *type)
2369{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002370 dictviewobject *dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002371 if (dict == NULL) {
2372 PyErr_BadInternalCall();
2373 return NULL;
2374 }
2375 if (!PyDict_Check(dict)) {
2376 /* XXX Get rid of this restriction later */
2377 PyErr_Format(PyExc_TypeError,
2378 "%s() requires a dict argument, not '%s'",
2379 type->tp_name, dict->ob_type->tp_name);
2380 return NULL;
2381 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002382 dv = PyObject_New(dictviewobject, type);
2383 if (dv == NULL)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002384 return NULL;
2385 Py_INCREF(dict);
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002386 dv->dv_dict = (PyDictObject *)dict;
Guido van Rossum3ac67412007-02-10 18:55:06 +00002387 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002388}
2389
Neal Norwitze36f2ba2007-02-26 23:12:28 +00002390/* TODO(guido): The views objects are not complete:
2391
2392 * support more set operations
2393 * support arbitrary mappings?
2394 - either these should be static or exported in dictobject.h
2395 - if public then they should probably be in builtins
2396*/
2397
Guido van Rossumaac530c2007-08-24 22:33:45 +00002398/* Return 1 if self is a subset of other, iterating over self;
2399 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002400static int
2401all_contained_in(PyObject *self, PyObject *other)
2402{
2403 PyObject *iter = PyObject_GetIter(self);
2404 int ok = 1;
2405
2406 if (iter == NULL)
2407 return -1;
2408 for (;;) {
2409 PyObject *next = PyIter_Next(iter);
2410 if (next == NULL) {
2411 if (PyErr_Occurred())
2412 ok = -1;
2413 break;
2414 }
2415 ok = PySequence_Contains(other, next);
2416 Py_DECREF(next);
2417 if (ok <= 0)
2418 break;
2419 }
2420 Py_DECREF(iter);
2421 return ok;
2422}
2423
2424static PyObject *
2425dictview_richcompare(PyObject *self, PyObject *other, int op)
2426{
Guido van Rossumaac530c2007-08-24 22:33:45 +00002427 Py_ssize_t len_self, len_other;
2428 int ok;
2429 PyObject *result;
2430
Guido van Rossumd9214d12007-02-12 02:23:40 +00002431 assert(self != NULL);
2432 assert(PyDictViewSet_Check(self));
2433 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00002434
Guido van Rossumaac530c2007-08-24 22:33:45 +00002435 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other)) {
Guido van Rossumd9214d12007-02-12 02:23:40 +00002436 Py_INCREF(Py_NotImplemented);
2437 return Py_NotImplemented;
2438 }
Guido van Rossumaac530c2007-08-24 22:33:45 +00002439
2440 len_self = PyObject_Size(self);
2441 if (len_self < 0)
2442 return NULL;
2443 len_other = PyObject_Size(other);
2444 if (len_other < 0)
2445 return NULL;
2446
2447 ok = 0;
2448 switch(op) {
2449
2450 case Py_NE:
2451 case Py_EQ:
2452 if (len_self == len_other)
2453 ok = all_contained_in(self, other);
2454 if (op == Py_NE && ok >= 0)
2455 ok = !ok;
2456 break;
2457
2458 case Py_LT:
2459 if (len_self < len_other)
2460 ok = all_contained_in(self, other);
2461 break;
2462
2463 case Py_LE:
2464 if (len_self <= len_other)
2465 ok = all_contained_in(self, other);
2466 break;
2467
2468 case Py_GT:
2469 if (len_self > len_other)
2470 ok = all_contained_in(other, self);
2471 break;
2472
2473 case Py_GE:
2474 if (len_self >= len_other)
2475 ok = all_contained_in(other, self);
2476 break;
2477
2478 }
2479 if (ok < 0)
2480 return NULL;
2481 result = ok ? Py_True : Py_False;
2482 Py_INCREF(result);
2483 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00002484}
2485
Guido van Rossum3ac67412007-02-10 18:55:06 +00002486/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002487
2488static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002489dictkeys_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002490{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002491 if (dv->dv_dict == NULL) {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002492 Py_RETURN_NONE;
2493 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002494 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
2495}
2496
2497static int
2498dictkeys_contains(dictviewobject *dv, PyObject *obj)
2499{
2500 if (dv->dv_dict == NULL)
2501 return 0;
2502 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002503}
2504
Guido van Rossum83825ac2007-02-10 04:54:19 +00002505static PySequenceMethods dictkeys_as_sequence = {
2506 (lenfunc)dictview_len, /* sq_length */
2507 0, /* sq_concat */
2508 0, /* sq_repeat */
2509 0, /* sq_item */
2510 0, /* sq_slice */
2511 0, /* sq_ass_item */
2512 0, /* sq_ass_slice */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002513 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002514};
2515
Guido van Rossum523259b2007-08-24 23:41:22 +00002516static PyObject*
2517dictviews_sub(PyObject* self, PyObject *other)
2518{
2519 PyObject *result = PySet_New(self);
2520 PyObject *tmp;
2521 if (result == NULL)
2522 return NULL;
2523
2524 tmp = PyObject_CallMethod(result, "difference_update", "O", other);
2525 if (tmp == NULL) {
2526 Py_DECREF(result);
2527 return NULL;
2528 }
2529
2530 Py_DECREF(tmp);
2531 return result;
2532}
2533
2534static PyObject*
2535dictviews_and(PyObject* self, PyObject *other)
2536{
2537 PyObject *result = PySet_New(self);
2538 PyObject *tmp;
2539 if (result == NULL)
2540 return NULL;
2541
2542 tmp = PyObject_CallMethod(result, "intersection_update", "O", other);
2543 if (tmp == NULL) {
2544 Py_DECREF(result);
2545 return NULL;
2546 }
2547
2548 Py_DECREF(tmp);
2549 return result;
2550}
2551
2552static PyObject*
2553dictviews_or(PyObject* self, PyObject *other)
2554{
2555 PyObject *result = PySet_New(self);
2556 PyObject *tmp;
2557 if (result == NULL)
2558 return NULL;
2559
2560 tmp = PyObject_CallMethod(result, "update", "O", other);
2561 if (tmp == NULL) {
2562 Py_DECREF(result);
2563 return NULL;
2564 }
2565
2566 Py_DECREF(tmp);
2567 return result;
2568}
2569
2570static PyObject*
2571dictviews_xor(PyObject* self, PyObject *other)
2572{
2573 PyObject *result = PySet_New(self);
2574 PyObject *tmp;
2575 if (result == NULL)
2576 return NULL;
2577
2578 tmp = PyObject_CallMethod(result, "symmetric_difference_update", "O",
2579 other);
2580 if (tmp == NULL) {
2581 Py_DECREF(result);
2582 return NULL;
2583 }
2584
2585 Py_DECREF(tmp);
2586 return result;
2587}
2588
2589static PyNumberMethods dictviews_as_number = {
2590 0, /*nb_add*/
2591 (binaryfunc)dictviews_sub, /*nb_subtract*/
2592 0, /*nb_multiply*/
2593 0, /*nb_remainder*/
2594 0, /*nb_divmod*/
2595 0, /*nb_power*/
2596 0, /*nb_negative*/
2597 0, /*nb_positive*/
2598 0, /*nb_absolute*/
2599 0, /*nb_bool*/
2600 0, /*nb_invert*/
2601 0, /*nb_lshift*/
2602 0, /*nb_rshift*/
2603 (binaryfunc)dictviews_and, /*nb_and*/
2604 (binaryfunc)dictviews_xor, /*nb_xor*/
2605 (binaryfunc)dictviews_or, /*nb_or*/
2606};
2607
Guido van Rossumb90c8482007-02-10 01:11:45 +00002608static PyMethodDef dictkeys_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002609 {NULL, NULL} /* sentinel */
2610};
2611
2612PyTypeObject PyDictKeys_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002613 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002614 "dict_keys", /* tp_name */
2615 sizeof(dictviewobject), /* tp_basicsize */
2616 0, /* tp_itemsize */
2617 /* methods */
2618 (destructor)dictview_dealloc, /* tp_dealloc */
2619 0, /* tp_print */
2620 0, /* tp_getattr */
2621 0, /* tp_setattr */
2622 0, /* tp_compare */
2623 0, /* tp_repr */
Guido van Rossum523259b2007-08-24 23:41:22 +00002624 &dictviews_as_number, /* tp_as_number */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002625 &dictkeys_as_sequence, /* tp_as_sequence */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002626 0, /* tp_as_mapping */
2627 0, /* tp_hash */
2628 0, /* tp_call */
2629 0, /* tp_str */
2630 PyObject_GenericGetAttr, /* tp_getattro */
2631 0, /* tp_setattro */
2632 0, /* tp_as_buffer */
2633 Py_TPFLAGS_DEFAULT, /* tp_flags */
2634 0, /* tp_doc */
2635 0, /* tp_traverse */
2636 0, /* tp_clear */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002637 dictview_richcompare, /* tp_richcompare */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002638 0, /* tp_weaklistoffset */
2639 (getiterfunc)dictkeys_iter, /* tp_iter */
2640 0, /* tp_iternext */
2641 dictkeys_methods, /* tp_methods */
2642 0,
2643};
2644
2645static PyObject *
2646dictkeys_new(PyObject *dict)
2647{
2648 return dictview_new(dict, &PyDictKeys_Type);
2649}
2650
Guido van Rossum3ac67412007-02-10 18:55:06 +00002651/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002652
2653static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002654dictitems_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002655{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002656 if (dv->dv_dict == NULL) {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002657 Py_RETURN_NONE;
2658 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002659 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
2660}
2661
2662static int
2663dictitems_contains(dictviewobject *dv, PyObject *obj)
2664{
2665 PyObject *key, *value, *found;
2666 if (dv->dv_dict == NULL)
2667 return 0;
2668 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
2669 return 0;
2670 key = PyTuple_GET_ITEM(obj, 0);
2671 value = PyTuple_GET_ITEM(obj, 1);
2672 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
2673 if (found == NULL) {
2674 if (PyErr_Occurred())
2675 return -1;
2676 return 0;
2677 }
2678 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002679}
2680
Guido van Rossum83825ac2007-02-10 04:54:19 +00002681static PySequenceMethods dictitems_as_sequence = {
2682 (lenfunc)dictview_len, /* sq_length */
2683 0, /* sq_concat */
2684 0, /* sq_repeat */
2685 0, /* sq_item */
2686 0, /* sq_slice */
2687 0, /* sq_ass_item */
2688 0, /* sq_ass_slice */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002689 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002690};
2691
Guido van Rossumb90c8482007-02-10 01:11:45 +00002692static PyMethodDef dictitems_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002693 {NULL, NULL} /* sentinel */
2694};
2695
2696PyTypeObject PyDictItems_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002697 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002698 "dict_items", /* tp_name */
2699 sizeof(dictviewobject), /* tp_basicsize */
2700 0, /* tp_itemsize */
2701 /* methods */
2702 (destructor)dictview_dealloc, /* tp_dealloc */
2703 0, /* tp_print */
2704 0, /* tp_getattr */
2705 0, /* tp_setattr */
2706 0, /* tp_compare */
2707 0, /* tp_repr */
Guido van Rossum523259b2007-08-24 23:41:22 +00002708 &dictviews_as_number, /* tp_as_number */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002709 &dictitems_as_sequence, /* tp_as_sequence */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002710 0, /* tp_as_mapping */
2711 0, /* tp_hash */
2712 0, /* tp_call */
2713 0, /* tp_str */
2714 PyObject_GenericGetAttr, /* tp_getattro */
2715 0, /* tp_setattro */
2716 0, /* tp_as_buffer */
2717 Py_TPFLAGS_DEFAULT, /* tp_flags */
2718 0, /* tp_doc */
2719 0, /* tp_traverse */
2720 0, /* tp_clear */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002721 dictview_richcompare, /* tp_richcompare */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002722 0, /* tp_weaklistoffset */
2723 (getiterfunc)dictitems_iter, /* tp_iter */
2724 0, /* tp_iternext */
2725 dictitems_methods, /* tp_methods */
2726 0,
2727};
2728
2729static PyObject *
2730dictitems_new(PyObject *dict)
2731{
2732 return dictview_new(dict, &PyDictItems_Type);
2733}
2734
Guido van Rossum3ac67412007-02-10 18:55:06 +00002735/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002736
2737static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002738dictvalues_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002739{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002740 if (dv->dv_dict == NULL) {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002741 Py_RETURN_NONE;
2742 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002743 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002744}
2745
Guido van Rossum83825ac2007-02-10 04:54:19 +00002746static PySequenceMethods dictvalues_as_sequence = {
2747 (lenfunc)dictview_len, /* sq_length */
2748 0, /* sq_concat */
2749 0, /* sq_repeat */
2750 0, /* sq_item */
2751 0, /* sq_slice */
2752 0, /* sq_ass_item */
2753 0, /* sq_ass_slice */
2754 (objobjproc)0, /* sq_contains */
2755};
2756
Guido van Rossumb90c8482007-02-10 01:11:45 +00002757static PyMethodDef dictvalues_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002758 {NULL, NULL} /* sentinel */
2759};
2760
2761PyTypeObject PyDictValues_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002762 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002763 "dict_values", /* tp_name */
2764 sizeof(dictviewobject), /* tp_basicsize */
2765 0, /* tp_itemsize */
2766 /* methods */
2767 (destructor)dictview_dealloc, /* tp_dealloc */
2768 0, /* tp_print */
2769 0, /* tp_getattr */
2770 0, /* tp_setattr */
2771 0, /* tp_compare */
2772 0, /* tp_repr */
2773 0, /* tp_as_number */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002774 &dictvalues_as_sequence, /* tp_as_sequence */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002775 0, /* tp_as_mapping */
2776 0, /* tp_hash */
2777 0, /* tp_call */
2778 0, /* tp_str */
2779 PyObject_GenericGetAttr, /* tp_getattro */
2780 0, /* tp_setattro */
2781 0, /* tp_as_buffer */
2782 Py_TPFLAGS_DEFAULT, /* tp_flags */
2783 0, /* tp_doc */
2784 0, /* tp_traverse */
2785 0, /* tp_clear */
2786 0, /* tp_richcompare */
2787 0, /* tp_weaklistoffset */
2788 (getiterfunc)dictvalues_iter, /* tp_iter */
2789 0, /* tp_iternext */
2790 dictvalues_methods, /* tp_methods */
2791 0,
2792};
2793
2794static PyObject *
2795dictvalues_new(PyObject *dict)
2796{
2797 return dictview_new(dict, &PyDictValues_Type);
2798}