blob: 3243061b68bd1a5a4a33eb5a66f7b233715fcd6e [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
Benjamin Peterson7d95e402012-04-23 11:24:50 -040010
11/*
12There are four kinds of slots in the table:
13
141. Unused. me_key == me_value == NULL
15 Does not hold an active (key, value) pair now and never did. Unused can
16 transition to Active upon key insertion. This is the only case in which
17 me_key is NULL, and is each slot's initial state.
18
192. Active. me_key != NULL and me_key != dummy and me_value != NULL
20 Holds an active (key, value) pair. Active can transition to Dummy or
21 Pending upon key deletion (for combined and split tables respectively).
22 This is the only case in which me_value != NULL.
23
243. Dummy. me_key == dummy and me_value == NULL
25 Previously held an active (key, value) pair, but that was deleted and an
26 active pair has not yet overwritten the slot. Dummy can transition to
27 Active upon key insertion. Dummy slots cannot be made Unused again
28 (cannot have me_key set to NULL), else the probe sequence in case of
29 collision would have no way to know they were once active.
30
314. Pending. Not yet inserted or deleted from a split-table.
32 key != NULL, key != dummy and value == NULL
33
34The DictObject can be in one of two forms.
35Either:
36 A combined table:
37 ma_values == NULL, dk_refcnt == 1.
38 Values are stored in the me_value field of the PyDictKeysObject.
39 Slot kind 4 is not allowed i.e.
40 key != NULL, key != dummy and value == NULL is illegal.
41Or:
42 A split table:
43 ma_values != NULL, dk_refcnt >= 1
44 Values are stored in the ma_values array.
45 Only string (unicode) keys are allowed, no <dummy> keys are present.
46
47Note: .popitem() abuses the me_hash field of an Unused or Dummy slot to
48hold a search finger. The me_hash field of Unused or Dummy slots has no
49meaning otherwise. As a consequence of this popitem always converts the dict
50to the combined-table form.
51*/
52
53/* PyDict_MINSIZE_SPLIT is the minimum size of a split dictionary.
54 * It must be a power of 2, and at least 4.
55 * Resizing of split dictionaries is very rare, so the saving memory is more
56 * important than the cost of resizing.
57 */
58#define PyDict_MINSIZE_SPLIT 4
59
60/* PyDict_MINSIZE_COMBINED is the starting size for any new, non-split dict.
61 * 8 allows dicts with no more than 5 active entries; experiments suggested
62 * this suffices for the majority of dicts (consisting mostly of usually-small
63 * dicts created to pass keyword arguments).
64 * Making this 8, rather than 4 reduces the number of resizes for most
65 * dictionaries, without any significant extra memory use.
66 */
67#define PyDict_MINSIZE_COMBINED 8
68
Guido van Rossumc0b618a1997-05-02 03:12:38 +000069#include "Python.h"
Christian Heimes0ded5b52007-12-10 15:50:56 +000070#include "stringlib/eq.h"
Guido van Rossum4b1302b1993-03-27 18:11:32 +000071
Benjamin Peterson7d95e402012-04-23 11:24:50 -040072typedef struct {
73 /* Cached hash code of me_key. */
74 Py_hash_t me_hash;
75 PyObject *me_key;
76 PyObject *me_value; /* This field is only meaningful for combined tables */
77} PyDictKeyEntry;
78
79typedef PyDictKeyEntry *(*dict_lookup_func)
80(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject ***value_addr);
81
82struct _dictkeysobject {
83 Py_ssize_t dk_refcnt;
84 Py_ssize_t dk_size;
85 dict_lookup_func dk_lookup;
86 Py_ssize_t dk_usable;
87 PyDictKeyEntry dk_entries[1];
88};
89
90
91/*
92To ensure the lookup algorithm terminates, there must be at least one Unused
93slot (NULL key) in the table.
94To avoid slowing down lookups on a near-full table, we resize the table when
95it's USABLE_FRACTION (currently two-thirds) full.
96*/
Guido van Rossum16e93a81997-01-28 00:00:11 +000097
Thomas Wouters89f507f2006-12-13 04:49:30 +000098/* Set a key error with the specified argument, wrapping it in a
99 * tuple automatically so that tuple keys are not unpacked as the
100 * exception arguments. */
101static void
102set_key_error(PyObject *arg)
103{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 PyObject *tup;
105 tup = PyTuple_Pack(1, arg);
106 if (!tup)
107 return; /* caller will expect error to be set anyway */
108 PyErr_SetObject(PyExc_KeyError, tup);
109 Py_DECREF(tup);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000110}
111
Tim Peterseb28ef22001-06-02 05:27:19 +0000112#define PERTURB_SHIFT 5
113
Guido van Rossum16e93a81997-01-28 00:00:11 +0000114/*
Tim Peterseb28ef22001-06-02 05:27:19 +0000115Major subtleties ahead: Most hash schemes depend on having a "good" hash
116function, in the sense of simulating randomness. Python doesn't: its most
117important hash functions (for strings and ints) are very regular in common
118cases:
Tim Peters15d49292001-05-27 07:39:22 +0000119
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000120 >>> map(hash, (0, 1, 2, 3))
121 [0, 1, 2, 3]
122 >>> map(hash, ("namea", "nameb", "namec", "named"))
123 [-1658398457, -1658398460, -1658398459, -1658398462]
124 >>>
Tim Peters15d49292001-05-27 07:39:22 +0000125
Tim Peterseb28ef22001-06-02 05:27:19 +0000126This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
127the low-order i bits as the initial table index is extremely fast, and there
128are no collisions at all for dicts indexed by a contiguous range of ints.
129The same is approximately true when keys are "consecutive" strings. So this
130gives better-than-random behavior in common cases, and that's very desirable.
Tim Peters15d49292001-05-27 07:39:22 +0000131
Tim Peterseb28ef22001-06-02 05:27:19 +0000132OTOH, when collisions occur, the tendency to fill contiguous slices of the
133hash table makes a good collision resolution strategy crucial. Taking only
134the last i bits of the hash code is also vulnerable: for example, consider
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135the list [i << 16 for i in range(20000)] as a set of keys. Since ints are
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000136their own hash codes, and this fits in a dict of size 2**15, the last 15 bits
137 of every hash code are all 0: they *all* map to the same table index.
Tim Peters15d49292001-05-27 07:39:22 +0000138
Tim Peterseb28ef22001-06-02 05:27:19 +0000139But catering to unusual cases should not slow the usual ones, so we just take
140the last i bits anyway. It's up to collision resolution to do the rest. If
141we *usually* find the key we're looking for on the first try (and, it turns
142out, we usually do -- the table load factor is kept under 2/3, so the odds
143are solidly in our favor), then it makes best sense to keep the initial index
144computation dirt cheap.
Tim Peters15d49292001-05-27 07:39:22 +0000145
Tim Peterseb28ef22001-06-02 05:27:19 +0000146The first half of collision resolution is to visit table indices via this
147recurrence:
Tim Peters15d49292001-05-27 07:39:22 +0000148
Tim Peterseb28ef22001-06-02 05:27:19 +0000149 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +0000150
Tim Peterseb28ef22001-06-02 05:27:19 +0000151For any initial j in range(2**i), repeating that 2**i times generates each
152int in range(2**i) exactly once (see any text on random-number generation for
153proof). By itself, this doesn't help much: like linear probing (setting
154j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
155order. This would be bad, except that's not the only thing we do, and it's
156actually *good* in the common cases where hash keys are consecutive. In an
157example that's really too small to make this entirely clear, for a table of
158size 2**3 the order of indices is:
Tim Peters15d49292001-05-27 07:39:22 +0000159
Tim Peterseb28ef22001-06-02 05:27:19 +0000160 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
161
162If two things come in at index 5, the first place we look after is index 2,
163not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
164Linear probing is deadly in this case because there the fixed probe order
165is the *same* as the order consecutive keys are likely to arrive. But it's
166extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
167and certain that consecutive hash codes do not.
168
169The other half of the strategy is to get the other bits of the hash code
170into play. This is done by initializing a (unsigned) vrbl "perturb" to the
171full hash code, and changing the recurrence to:
172
173 j = (5*j) + 1 + perturb;
174 perturb >>= PERTURB_SHIFT;
175 use j % 2**i as the next table index;
176
177Now the probe sequence depends (eventually) on every bit in the hash code,
178and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
179because it quickly magnifies small differences in the bits that didn't affect
180the initial index. Note that because perturb is unsigned, if the recurrence
181is executed often enough perturb eventually becomes and remains 0. At that
182point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
183that's certain to find an empty slot eventually (since it generates every int
184in range(2**i), and we make sure there's always at least one empty slot).
185
186Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
187small so that the high bits of the hash code continue to affect the probe
188sequence across iterations; but you want it large so that in really bad cases
189the high-order hash bits have an effect on early iterations. 5 was "the
190best" in minimizing total collisions across experiments Tim Peters ran (on
191both normal and pathological cases), but 4 and 6 weren't significantly worse.
192
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000193Historical: Reimer Behrends contributed the idea of using a polynomial-based
Tim Peterseb28ef22001-06-02 05:27:19 +0000194approach, using repeated multiplication by x in GF(2**n) where an irreducible
195polynomial for each table size was chosen such that x was a primitive root.
196Christian Tismer later extended that to use division by x instead, as an
197efficient way to get the high bits of the hash code into play. This scheme
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000198also gave excellent collision statistics, but was more expensive: two
199if-tests were required inside the loop; computing "the next" index took about
200the same number of operations but without as much potential parallelism
201(e.g., computing 5*j can go on at the same time as computing 1+perturb in the
202above, and then shifting perturb can be done while the table index is being
203masked); and the PyDictObject struct required a member to hold the table's
204polynomial. In Tim's experiments the current scheme ran faster, produced
205equally good collision statistics, needed less code & used less memory.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000206
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000207*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000208
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400209/* Object used as dummy key to fill deleted entries
210 * This could be any unique object,
211 * use a custom type in order to minimise coupling.
212*/
213static PyObject _dummy_struct;
214
215#define dummy (&_dummy_struct)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000216
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000217#ifdef Py_REF_DEBUG
218PyObject *
219_PyDict_Dummy(void)
220{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 return dummy;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000222}
223#endif
224
Fred Drake1bff34a2000-08-31 19:31:38 +0000225/* forward declarations */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400226static PyDictKeyEntry *lookdict(PyDictObject *mp, PyObject *key,
227 Py_hash_t hash, PyObject ***value_addr);
228static PyDictKeyEntry *lookdict_unicode(PyDictObject *mp, PyObject *key,
229 Py_hash_t hash, PyObject ***value_addr);
230static PyDictKeyEntry *
231lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
232 Py_hash_t hash, PyObject ***value_addr);
233static PyDictKeyEntry *lookdict_split(PyDictObject *mp, PyObject *key,
234 Py_hash_t hash, PyObject ***value_addr);
Fred Drake1bff34a2000-08-31 19:31:38 +0000235
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400236static int dictresize(PyDictObject *mp, Py_ssize_t minused);
Tim Petersdea48ec2001-05-22 20:40:22 +0000237
Raymond Hettinger43442782004-03-17 21:55:03 +0000238/* Dictionary reuse scheme to save calls to malloc, free, and memset */
Christian Heimes2202f872008-02-06 14:31:34 +0000239#ifndef PyDict_MAXFREELIST
240#define PyDict_MAXFREELIST 80
241#endif
242static PyDictObject *free_list[PyDict_MAXFREELIST];
243static int numfree = 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000244
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100245int
246PyDict_ClearFreeList(void)
Christian Heimes77c02eb2008-02-09 02:18:51 +0000247{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 PyDictObject *op;
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100249 int ret = numfree;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 while (numfree) {
251 op = free_list[--numfree];
252 assert(PyDict_CheckExact(op));
253 PyObject_GC_Del(op);
254 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100255 return ret;
256}
257
David Malcolm49526f42012-06-22 14:55:41 -0400258/* Print summary info about the state of the optimized allocator */
259void
260_PyDict_DebugMallocStats(FILE *out)
261{
262 _PyDebugAllocatorStats(out,
263 "free PyDictObject", numfree, sizeof(PyDictObject));
264}
265
266
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100267void
268PyDict_Fini(void)
269{
270 PyDict_ClearFreeList();
Christian Heimes77c02eb2008-02-09 02:18:51 +0000271}
272
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200273#define DK_DEBUG_INCREF _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA
274#define DK_DEBUG_DECREF _Py_DEC_REFTOTAL _Py_REF_DEBUG_COMMA
275
276#define DK_INCREF(dk) (DK_DEBUG_INCREF ++(dk)->dk_refcnt)
277#define DK_DECREF(dk) if (DK_DEBUG_DECREF (--(dk)->dk_refcnt) == 0) free_keys_object(dk)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400278#define DK_SIZE(dk) ((dk)->dk_size)
279#define DK_MASK(dk) (((dk)->dk_size)-1)
280#define IS_POWER_OF_2(x) (((x) & (x-1)) == 0)
281
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200282/* USABLE_FRACTION is the maximum dictionary load.
283 * Currently set to (2n+1)/3. Increasing this ratio makes dictionaries more
284 * dense resulting in more collisions. Decreasing it improves sparseness
285 * at the expense of spreading entries over more cache lines and at the
286 * cost of total memory consumed.
287 *
288 * USABLE_FRACTION must obey the following:
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400289 * (0 < USABLE_FRACTION(n) < n) for all n >= 2
290 *
291 * USABLE_FRACTION should be very quick to calculate.
292 * Fractions around 5/8 to 2/3 seem to work well in practice.
293 */
294
295/* Use (2n+1)/3 rather than 2n+3 because: it makes no difference for
296 * combined tables (the two fractions round to the same number n < ),
297 * but 2*4/3 is 2 whereas (2*4+1)/3 is 3 which potentially saves quite
298 * a lot of space for small, split tables */
299#define USABLE_FRACTION(n) ((((n) << 1)+1)/3)
300
301/* Alternative fraction that is otherwise close enough to (2n+1)/3 to make
302 * little difference. 8 * 2/3 == 8 * 5/8 == 5. 16 * 2/3 == 16 * 5/8 == 10.
303 * 32 * 2/3 = 21, 32 * 5/8 = 20.
304 * Its advantage is that it is faster to compute on machines with slow division.
305 * #define USABLE_FRACTION(n) (((n) >> 1) + ((n) >> 2) - ((n) >> 3))
306*/
307
Victor Stinnera9f61a52013-07-16 22:17:26 +0200308/* GROWTH_RATE. Growth rate upon hitting maximum load.
309 * Currently set to used*2 + capacity/2.
310 * This means that dicts double in size when growing without deletions,
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700311 * but have more head room when the number of deletions is on a par with the
312 * number of insertions.
313 * Raising this to used*4 doubles memory consumption depending on the size of
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200314 * the dictionary, but results in half the number of resizes, less effort to
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700315 * resize.
316 * GROWTH_RATE was set to used*4 up to version 3.2.
317 * GROWTH_RATE was set to used*2 in version 3.3.0
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200318 */
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700319#define GROWTH_RATE(d) (((d)->ma_used*2)+((d)->ma_keys->dk_size>>1))
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400320
321#define ENSURE_ALLOWS_DELETIONS(d) \
322 if ((d)->ma_keys->dk_lookup == lookdict_unicode_nodummy) { \
323 (d)->ma_keys->dk_lookup = lookdict_unicode; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400325
326/* This immutable, empty PyDictKeysObject is used for PyDict_Clear()
327 * (which cannot fail and thus can do no allocation).
328 */
329static PyDictKeysObject empty_keys_struct = {
330 2, /* dk_refcnt 1 for this struct, 1 for dummy_struct */
331 1, /* dk_size */
332 lookdict_split, /* dk_lookup */
333 0, /* dk_usable (immutable) */
334 {
335 { 0, 0, 0 } /* dk_entries (empty) */
336 }
337};
338
339static PyObject *empty_values[1] = { NULL };
340
341#define Py_EMPTY_KEYS &empty_keys_struct
342
343static PyDictKeysObject *new_keys_object(Py_ssize_t size)
344{
345 PyDictKeysObject *dk;
346 Py_ssize_t i;
347 PyDictKeyEntry *ep0;
348
349 assert(size >= PyDict_MINSIZE_SPLIT);
350 assert(IS_POWER_OF_2(size));
351 dk = PyMem_MALLOC(sizeof(PyDictKeysObject) +
352 sizeof(PyDictKeyEntry) * (size-1));
353 if (dk == NULL) {
354 PyErr_NoMemory();
355 return NULL;
356 }
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200357 DK_DEBUG_INCREF dk->dk_refcnt = 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400358 dk->dk_size = size;
359 dk->dk_usable = USABLE_FRACTION(size);
360 ep0 = &dk->dk_entries[0];
361 /* Hash value of slot 0 is used by popitem, so it must be initialized */
362 ep0->me_hash = 0;
363 for (i = 0; i < size; i++) {
364 ep0[i].me_key = NULL;
365 ep0[i].me_value = NULL;
366 }
367 dk->dk_lookup = lookdict_unicode_nodummy;
368 return dk;
369}
370
371static void
372free_keys_object(PyDictKeysObject *keys)
373{
374 PyDictKeyEntry *entries = &keys->dk_entries[0];
375 Py_ssize_t i, n;
376 for (i = 0, n = DK_SIZE(keys); i < n; i++) {
377 Py_XDECREF(entries[i].me_key);
378 Py_XDECREF(entries[i].me_value);
379 }
380 PyMem_FREE(keys);
381}
382
383#define new_values(size) PyMem_NEW(PyObject *, size)
384
385#define free_values(values) PyMem_FREE(values)
386
387/* Consumes a reference to the keys object */
388static PyObject *
389new_dict(PyDictKeysObject *keys, PyObject **values)
390{
391 PyDictObject *mp;
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200392 assert(keys != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 if (numfree) {
394 mp = free_list[--numfree];
395 assert (mp != NULL);
396 assert (Py_TYPE(mp) == &PyDict_Type);
397 _Py_NewReference((PyObject *)mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400399 else {
400 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
401 if (mp == NULL) {
402 DK_DECREF(keys);
403 free_values(values);
404 return NULL;
405 }
406 }
407 mp->ma_keys = keys;
408 mp->ma_values = values;
409 mp->ma_used = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000411}
412
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400413/* Consumes a reference to the keys object */
414static PyObject *
415new_dict_with_shared_keys(PyDictKeysObject *keys)
416{
417 PyObject **values;
418 Py_ssize_t i, size;
419
420 size = DK_SIZE(keys);
421 values = new_values(size);
422 if (values == NULL) {
423 DK_DECREF(keys);
424 return PyErr_NoMemory();
425 }
426 for (i = 0; i < size; i++) {
427 values[i] = NULL;
428 }
429 return new_dict(keys, values);
430}
431
432PyObject *
433PyDict_New(void)
434{
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200435 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE_COMBINED);
436 if (keys == NULL)
437 return NULL;
438 return new_dict(keys, NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400439}
440
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000441/*
442The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000443This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000444Open addressing is preferred over chaining since the link overhead for
445chaining would be substantial (100% with typical malloc overhead).
446
Tim Peterseb28ef22001-06-02 05:27:19 +0000447The initial probe index is computed as hash mod the table size. Subsequent
448probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000449
450All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000451
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000452The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000453contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000454Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000455
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000456lookdict() is general-purpose, and may return NULL if (and only if) a
457comparison raises an exception (this was new in Python 2.5).
Guido van Rossum89d8c602007-09-18 17:26:56 +0000458lookdict_unicode() below is specialized to string keys, comparison of which can
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400459never raise an exception; that function can never return NULL.
460lookdict_unicode_nodummy is further specialized for string keys that cannot be
461the <dummy> value.
462For both, when the key isn't found a PyDictEntry* is returned
463where the key would have been found, *value_addr points to the matching value
464slot.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000465*/
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400466static PyDictKeyEntry *
467lookdict(PyDictObject *mp, PyObject *key,
468 Py_hash_t hash, PyObject ***value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000469{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 register size_t i;
471 register size_t perturb;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400472 register PyDictKeyEntry *freeslot;
Antoine Pitrou9a234902012-05-13 20:48:01 +0200473 register size_t mask;
474 PyDictKeyEntry *ep0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400475 register PyDictKeyEntry *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 register int cmp;
477 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000478
Antoine Pitrou9a234902012-05-13 20:48:01 +0200479top:
480 mask = DK_MASK(mp->ma_keys);
481 ep0 = &mp->ma_keys->dk_entries[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 i = (size_t)hash & mask;
483 ep = &ep0[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400484 if (ep->me_key == NULL || ep->me_key == key) {
485 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400487 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 if (ep->me_key == dummy)
489 freeslot = ep;
490 else {
491 if (ep->me_hash == hash) {
492 startkey = ep->me_key;
493 Py_INCREF(startkey);
494 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
495 Py_DECREF(startkey);
496 if (cmp < 0)
497 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400498 if (ep0 == mp->ma_keys->dk_entries && ep->me_key == startkey) {
499 if (cmp > 0) {
500 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400502 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 }
504 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200505 /* The dict was mutated, restart */
506 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 }
508 }
509 freeslot = NULL;
510 }
Tim Peters15d49292001-05-27 07:39:22 +0000511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 /* In the loop, me_key == dummy is by far (factor of 100s) the
513 least likely outcome, so test for that last. */
514 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
515 i = (i << 2) + i + perturb + 1;
516 ep = &ep0[i & mask];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400517 if (ep->me_key == NULL) {
518 if (freeslot == NULL) {
519 *value_addr = &ep->me_value;
520 return ep;
521 } else {
522 *value_addr = &freeslot->me_value;
523 return freeslot;
524 }
525 }
526 if (ep->me_key == key) {
527 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400529 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 if (ep->me_hash == hash && ep->me_key != dummy) {
531 startkey = ep->me_key;
532 Py_INCREF(startkey);
533 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
534 Py_DECREF(startkey);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400535 if (cmp < 0) {
536 *value_addr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400538 }
539 if (ep0 == mp->ma_keys->dk_entries && ep->me_key == startkey) {
540 if (cmp > 0) {
541 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400543 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 }
545 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200546 /* The dict was mutated, restart */
547 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 }
549 }
550 else if (ep->me_key == dummy && freeslot == NULL)
551 freeslot = ep;
552 }
553 assert(0); /* NOT REACHED */
554 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000555}
556
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400557/* Specialized version for string-only keys */
558static PyDictKeyEntry *
559lookdict_unicode(PyDictObject *mp, PyObject *key,
560 Py_hash_t hash, PyObject ***value_addr)
Fred Drake1bff34a2000-08-31 19:31:38 +0000561{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 register size_t i;
563 register size_t perturb;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400564 register PyDictKeyEntry *freeslot;
565 register size_t mask = DK_MASK(mp->ma_keys);
566 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
567 register PyDictKeyEntry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 /* Make sure this function doesn't have to handle non-unicode keys,
570 including subclasses of str; e.g., one reason to subclass
571 unicodes is to override __eq__, and for speed we don't cater to
572 that here. */
573 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400574 mp->ma_keys->dk_lookup = lookdict;
575 return lookdict(mp, key, hash, value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 }
Mark Dickinson57e683e2011-09-24 18:18:40 +0100577 i = (size_t)hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 ep = &ep0[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400579 if (ep->me_key == NULL || ep->me_key == key) {
580 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400582 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 if (ep->me_key == dummy)
584 freeslot = ep;
585 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400586 if (ep->me_hash == hash && unicode_eq(ep->me_key, key)) {
587 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400589 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 freeslot = NULL;
591 }
Tim Peters15d49292001-05-27 07:39:22 +0000592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 /* In the loop, me_key == dummy is by far (factor of 100s) the
594 least likely outcome, so test for that last. */
595 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
596 i = (i << 2) + i + perturb + 1;
597 ep = &ep0[i & mask];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400598 if (ep->me_key == NULL) {
599 if (freeslot == NULL) {
600 *value_addr = &ep->me_value;
601 return ep;
602 } else {
603 *value_addr = &freeslot->me_value;
604 return freeslot;
605 }
606 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 if (ep->me_key == key
608 || (ep->me_hash == hash
609 && ep->me_key != dummy
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400610 && unicode_eq(ep->me_key, key))) {
611 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400613 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 if (ep->me_key == dummy && freeslot == NULL)
615 freeslot = ep;
616 }
617 assert(0); /* NOT REACHED */
618 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000619}
620
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400621/* Faster version of lookdict_unicode when it is known that no <dummy> keys
622 * will be present. */
623static PyDictKeyEntry *
624lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
625 Py_hash_t hash, PyObject ***value_addr)
626{
627 register size_t i;
628 register size_t perturb;
629 register size_t mask = DK_MASK(mp->ma_keys);
630 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
631 register PyDictKeyEntry *ep;
632
633 /* Make sure this function doesn't have to handle non-unicode keys,
634 including subclasses of str; e.g., one reason to subclass
635 unicodes is to override __eq__, and for speed we don't cater to
636 that here. */
637 if (!PyUnicode_CheckExact(key)) {
638 mp->ma_keys->dk_lookup = lookdict;
639 return lookdict(mp, key, hash, value_addr);
640 }
641 i = (size_t)hash & mask;
642 ep = &ep0[i];
643 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
644 if (ep->me_key == NULL || ep->me_key == key ||
645 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
646 *value_addr = &ep->me_value;
647 return ep;
648 }
649 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
650 i = (i << 2) + i + perturb + 1;
651 ep = &ep0[i & mask];
652 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
653 if (ep->me_key == NULL || ep->me_key == key ||
654 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
655 *value_addr = &ep->me_value;
656 return ep;
657 }
658 }
659 assert(0); /* NOT REACHED */
660 return 0;
661}
662
663/* Version of lookdict for split tables.
664 * All split tables and only split tables use this lookup function.
665 * Split tables only contain unicode keys and no dummy keys,
666 * so algorithm is the same as lookdict_unicode_nodummy.
667 */
668static PyDictKeyEntry *
669lookdict_split(PyDictObject *mp, PyObject *key,
670 Py_hash_t hash, PyObject ***value_addr)
671{
672 register size_t i;
673 register size_t perturb;
674 register size_t mask = DK_MASK(mp->ma_keys);
675 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
676 register PyDictKeyEntry *ep;
677
678 if (!PyUnicode_CheckExact(key)) {
Benjamin Petersondb780d02012-04-23 13:44:32 -0400679 ep = lookdict(mp, key, hash, value_addr);
680 /* lookdict expects a combined-table, so fix value_addr */
681 i = ep - ep0;
682 *value_addr = &mp->ma_values[i];
683 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400684 }
685 i = (size_t)hash & mask;
686 ep = &ep0[i];
687 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
688 if (ep->me_key == NULL || ep->me_key == key ||
689 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
690 *value_addr = &mp->ma_values[i];
691 return ep;
692 }
693 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
694 i = (i << 2) + i + perturb + 1;
695 ep = &ep0[i & mask];
696 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
697 if (ep->me_key == NULL || ep->me_key == key ||
698 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
699 *value_addr = &mp->ma_values[i & mask];
700 return ep;
701 }
702 }
703 assert(0); /* NOT REACHED */
704 return 0;
705}
706
Benjamin Petersonfb886362010-04-24 18:21:17 +0000707int
708_PyDict_HasOnlyStringKeys(PyObject *dict)
709{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 Py_ssize_t pos = 0;
711 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000712 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400714 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 return 1;
716 while (PyDict_Next(dict, &pos, &key, &value))
717 if (!PyUnicode_Check(key))
718 return 0;
719 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000720}
721
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000722#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 do { \
724 if (!_PyObject_GC_IS_TRACKED(mp)) { \
725 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
726 _PyObject_GC_MAY_BE_TRACKED(value)) { \
727 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 } \
729 } \
730 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000731
732void
733_PyDict_MaybeUntrack(PyObject *op)
734{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 PyDictObject *mp;
736 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400737 Py_ssize_t i, size;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
740 return;
741
742 mp = (PyDictObject *) op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400743 size = DK_SIZE(mp->ma_keys);
744 if (_PyDict_HasSplitTable(mp)) {
745 for (i = 0; i < size; i++) {
746 if ((value = mp->ma_values[i]) == NULL)
747 continue;
748 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
749 assert(!_PyObject_GC_MAY_BE_TRACKED(
750 mp->ma_keys->dk_entries[i].me_key));
751 return;
752 }
753 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400755 else {
756 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
757 for (i = 0; i < size; i++) {
758 if ((value = ep0[i].me_value) == NULL)
759 continue;
760 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
761 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
762 return;
763 }
764 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000766}
767
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400768/* Internal function to find slot for an item from its hash
769 * when it is known that the key is not present in the dict.
770 */
771static PyDictKeyEntry *
772find_empty_slot(PyDictObject *mp, PyObject *key, Py_hash_t hash,
773 PyObject ***value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000774{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400775 size_t i;
776 size_t perturb;
777 size_t mask = DK_MASK(mp->ma_keys);
778 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
779 PyDictKeyEntry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000780
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400781 assert(key != NULL);
782 if (!PyUnicode_CheckExact(key))
783 mp->ma_keys->dk_lookup = lookdict;
784 i = hash & mask;
785 ep = &ep0[i];
786 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
787 i = (i << 2) + i + perturb + 1;
788 ep = &ep0[i & mask];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400790 assert(ep->me_value == NULL);
791 if (mp->ma_values)
792 *value_addr = &mp->ma_values[i & mask];
793 else
794 *value_addr = &ep->me_value;
795 return ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000796}
797
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400798static int
799insertion_resize(PyDictObject *mp)
800{
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700801 return dictresize(mp, GROWTH_RATE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400802}
Antoine Pitroue965d972012-02-27 00:45:12 +0100803
804/*
805Internal routine to insert a new item into the table.
806Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +0100807Returns -1 if an error occurred, or 0 on success.
808*/
809static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400810insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +0100811{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400812 PyObject *old_value;
813 PyObject **value_addr;
814 PyDictKeyEntry *ep;
815 assert(key != dummy);
Antoine Pitroue965d972012-02-27 00:45:12 +0100816
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400817 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
818 if (insertion_resize(mp) < 0)
819 return -1;
820 }
821
822 ep = mp->ma_keys->dk_lookup(mp, key, hash, &value_addr);
Antoine Pitroue965d972012-02-27 00:45:12 +0100823 if (ep == NULL) {
Antoine Pitroue965d972012-02-27 00:45:12 +0100824 return -1;
825 }
Benjamin Petersona6f195e2012-04-30 10:23:40 -0400826 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400827 MAINTAIN_TRACKING(mp, key, value);
828 old_value = *value_addr;
829 if (old_value != NULL) {
830 assert(ep->me_key != NULL && ep->me_key != dummy);
831 *value_addr = value;
832 Py_DECREF(old_value); /* which **CAN** re-enter */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400833 }
834 else {
835 if (ep->me_key == NULL) {
Benjamin Petersona6f195e2012-04-30 10:23:40 -0400836 Py_INCREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400837 if (mp->ma_keys->dk_usable <= 0) {
838 /* Need to resize. */
839 if (insertion_resize(mp) < 0) {
840 Py_DECREF(key);
841 Py_DECREF(value);
842 return -1;
843 }
844 ep = find_empty_slot(mp, key, hash, &value_addr);
845 }
846 mp->ma_keys->dk_usable--;
847 assert(mp->ma_keys->dk_usable >= 0);
848 ep->me_key = key;
849 ep->me_hash = hash;
850 }
851 else {
852 if (ep->me_key == dummy) {
Benjamin Petersona6f195e2012-04-30 10:23:40 -0400853 Py_INCREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400854 ep->me_key = key;
855 ep->me_hash = hash;
856 Py_DECREF(dummy);
857 } else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400858 assert(_PyDict_HasSplitTable(mp));
859 }
860 }
861 mp->ma_used++;
862 *value_addr = value;
863 }
864 assert(ep->me_key != NULL && ep->me_key != dummy);
865 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
866 return 0;
Antoine Pitroue965d972012-02-27 00:45:12 +0100867}
868
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000869/*
870Internal routine used by dictresize() to insert an item which is
871known to be absent from the dict. This routine also assumes that
872the dict contains no deleted entries. Besides the performance benefit,
873using insertdict() in dictresize() is dangerous (SF bug #1456209).
874Note that no refcounts are changed by this routine; if needed, the caller
875is responsible for incref'ing `key` and `value`.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400876Neither mp->ma_used nor k->dk_usable are modified by this routine; the caller
877must set them correctly
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000878*/
879static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400880insertdict_clean(PyDictObject *mp, PyObject *key, Py_hash_t hash,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 PyObject *value)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000882{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400883 size_t i;
884 size_t perturb;
885 PyDictKeysObject *k = mp->ma_keys;
886 size_t mask = (size_t)DK_SIZE(k)-1;
887 PyDictKeyEntry *ep0 = &k->dk_entries[0];
888 PyDictKeyEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000889
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400890 assert(k->dk_lookup != NULL);
891 assert(value != NULL);
892 assert(key != NULL);
893 assert(key != dummy);
894 assert(PyUnicode_CheckExact(key) || k->dk_lookup == lookdict);
895 i = hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 ep = &ep0[i];
897 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
898 i = (i << 2) + i + perturb + 1;
899 ep = &ep0[i & mask];
900 }
901 assert(ep->me_value == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 ep->me_key = key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000903 ep->me_hash = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000905}
906
907/*
908Restructure the table by allocating a new table and reinserting all
909items again. When entries have been deleted, the new table may
910actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400911If a table is split (its keys and hashes are shared, its values are not),
912then the values are temporarily copied into the table, it is resized as
913a combined table, then the me_value slots in the old table are NULLed out.
914After resizing a table is always combined,
915but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000916*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000917static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000918dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000919{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 Py_ssize_t newsize;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400921 PyDictKeysObject *oldkeys;
922 PyObject **oldvalues;
923 Py_ssize_t i, oldsize;
Tim Peters91a364d2001-05-19 07:04:38 +0000924
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400925/* Find the smallest table size > minused. */
926 for (newsize = PyDict_MINSIZE_COMBINED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 newsize <= minused && newsize > 0;
928 newsize <<= 1)
929 ;
930 if (newsize <= 0) {
931 PyErr_NoMemory();
932 return -1;
933 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400934 oldkeys = mp->ma_keys;
935 oldvalues = mp->ma_values;
936 /* Allocate a new table. */
937 mp->ma_keys = new_keys_object(newsize);
938 if (mp->ma_keys == NULL) {
939 mp->ma_keys = oldkeys;
940 return -1;
941 }
942 if (oldkeys->dk_lookup == lookdict)
943 mp->ma_keys->dk_lookup = lookdict;
944 oldsize = DK_SIZE(oldkeys);
945 mp->ma_values = NULL;
946 /* If empty then nothing to copy so just return */
947 if (oldsize == 1) {
948 assert(oldkeys == Py_EMPTY_KEYS);
949 DK_DECREF(oldkeys);
950 return 0;
951 }
952 /* Main loop below assumes we can transfer refcount to new keys
953 * and that value is stored in me_value.
954 * Increment ref-counts and copy values here to compensate
955 * This (resizing a split table) should be relatively rare */
956 if (oldvalues != NULL) {
957 for (i = 0; i < oldsize; i++) {
958 if (oldvalues[i] != NULL) {
959 Py_INCREF(oldkeys->dk_entries[i].me_key);
960 oldkeys->dk_entries[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 }
963 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400964 /* Main loop */
965 for (i = 0; i < oldsize; i++) {
966 PyDictKeyEntry *ep = &oldkeys->dk_entries[i];
967 if (ep->me_value != NULL) {
968 assert(ep->me_key != dummy);
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000969 insertdict_clean(mp, ep->me_key, ep->me_hash, ep->me_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400972 mp->ma_keys->dk_usable -= mp->ma_used;
973 if (oldvalues != NULL) {
974 /* NULL out me_value slot in oldkeys, in case it was shared */
975 for (i = 0; i < oldsize; i++)
976 oldkeys->dk_entries[i].me_value = NULL;
977 assert(oldvalues != empty_values);
978 free_values(oldvalues);
979 DK_DECREF(oldkeys);
980 }
981 else {
982 assert(oldkeys->dk_lookup != lookdict_split);
983 if (oldkeys->dk_lookup != lookdict_unicode_nodummy) {
984 PyDictKeyEntry *ep0 = &oldkeys->dk_entries[0];
985 for (i = 0; i < oldsize; i++) {
986 if (ep0[i].me_key == dummy)
987 Py_DECREF(dummy);
988 }
989 }
990 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200991 DK_DEBUG_DECREF PyMem_FREE(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400992 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000994}
995
Benjamin Peterson15ee8212012-04-24 14:44:18 -0400996/* Returns NULL if unable to split table.
997 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400998static PyDictKeysObject *
999make_keys_shared(PyObject *op)
1000{
1001 Py_ssize_t i;
1002 Py_ssize_t size;
1003 PyDictObject *mp = (PyDictObject *)op;
1004
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001005 if (!PyDict_CheckExact(op))
1006 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001007 if (!_PyDict_HasSplitTable(mp)) {
1008 PyDictKeyEntry *ep0;
1009 PyObject **values;
1010 assert(mp->ma_keys->dk_refcnt == 1);
1011 if (mp->ma_keys->dk_lookup == lookdict) {
1012 return NULL;
1013 }
1014 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
1015 /* Remove dummy keys */
1016 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
1017 return NULL;
1018 }
1019 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
1020 /* Copy values into a new array */
1021 ep0 = &mp->ma_keys->dk_entries[0];
1022 size = DK_SIZE(mp->ma_keys);
1023 values = new_values(size);
1024 if (values == NULL) {
1025 PyErr_SetString(PyExc_MemoryError,
1026 "Not enough memory to allocate new values array");
1027 return NULL;
1028 }
1029 for (i = 0; i < size; i++) {
1030 values[i] = ep0[i].me_value;
1031 ep0[i].me_value = NULL;
1032 }
1033 mp->ma_keys->dk_lookup = lookdict_split;
1034 mp->ma_values = values;
1035 }
1036 DK_INCREF(mp->ma_keys);
1037 return mp->ma_keys;
1038}
Christian Heimes99170a52007-12-19 02:07:34 +00001039
1040PyObject *
1041_PyDict_NewPresized(Py_ssize_t minused)
1042{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001043 Py_ssize_t newsize;
1044 PyDictKeysObject *new_keys;
1045 for (newsize = PyDict_MINSIZE_COMBINED;
1046 newsize <= minused && newsize > 0;
1047 newsize <<= 1)
1048 ;
1049 new_keys = new_keys_object(newsize);
1050 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001052 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001053}
1054
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001055/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1056 * that may occur (originally dicts supported only string keys, and exceptions
1057 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001058 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001059 * (suppressed) occurred while computing the key's hash, or that some error
1060 * (suppressed) occurred when comparing keys in the dict's internal probe
1061 * sequence. A nasty example of the latter is when a Python-coded comparison
1062 * function hits a stack-depth error, which can cause this to return NULL
1063 * even if the key is present.
1064 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001065PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001066PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001067{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001068 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001070 PyDictKeyEntry *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 PyThreadState *tstate;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001072 PyObject **value_addr;
1073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 if (!PyDict_Check(op))
1075 return NULL;
1076 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001077 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 {
1079 hash = PyObject_Hash(key);
1080 if (hash == -1) {
1081 PyErr_Clear();
1082 return NULL;
1083 }
1084 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001085
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 /* We can arrive here with a NULL tstate during initialization: try
1087 running "python -Wi" for an example related to string interning.
1088 Let's just hope that no exception occurs then... This must be
1089 _PyThreadState_Current and not PyThreadState_GET() because in debug
1090 mode, the latter complains if tstate is NULL. */
1091 tstate = (PyThreadState*)_Py_atomic_load_relaxed(
1092 &_PyThreadState_Current);
1093 if (tstate != NULL && tstate->curexc_type != NULL) {
1094 /* preserve the existing exception */
1095 PyObject *err_type, *err_value, *err_tb;
1096 PyErr_Fetch(&err_type, &err_value, &err_tb);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001097 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 /* ignore errors */
1099 PyErr_Restore(err_type, err_value, err_tb);
1100 if (ep == NULL)
1101 return NULL;
1102 }
1103 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001104 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 if (ep == NULL) {
1106 PyErr_Clear();
1107 return NULL;
1108 }
1109 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001110 return *value_addr;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001111}
1112
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001113/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1114 This returns NULL *with* an exception set if an exception occurred.
1115 It returns NULL *without* an exception set if the key wasn't present.
1116*/
1117PyObject *
1118PyDict_GetItemWithError(PyObject *op, PyObject *key)
1119{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001120 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 PyDictObject*mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001122 PyDictKeyEntry *ep;
1123 PyObject **value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001124
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 if (!PyDict_Check(op)) {
1126 PyErr_BadInternalCall();
1127 return NULL;
1128 }
1129 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001130 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 {
1132 hash = PyObject_Hash(key);
1133 if (hash == -1) {
1134 return NULL;
1135 }
1136 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001137
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001138 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 if (ep == NULL)
1140 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001141 return *value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001142}
1143
Brett Cannonfd074152012-04-14 14:10:13 -04001144PyObject *
1145_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1146{
1147 PyObject *kv;
1148 kv = _PyUnicode_FromId(key); /* borrowed */
1149 if (kv == NULL)
1150 return NULL;
1151 return PyDict_GetItemWithError(dp, kv);
1152}
1153
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001154/* Fast version of global value lookup.
1155 * Lookup in globals, then builtins.
1156 */
1157PyObject *
1158_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001159{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001160 PyObject *x;
1161 if (PyUnicode_CheckExact(key)) {
1162 PyObject **value_addr;
1163 Py_hash_t hash = ((PyASCIIObject *)key)->hash;
1164 if (hash != -1) {
1165 PyDictKeyEntry *e;
1166 e = globals->ma_keys->dk_lookup(globals, key, hash, &value_addr);
1167 if (e == NULL) {
1168 return NULL;
1169 }
1170 x = *value_addr;
1171 if (x != NULL)
1172 return x;
1173 e = builtins->ma_keys->dk_lookup(builtins, key, hash, &value_addr);
1174 if (e == NULL) {
1175 return NULL;
1176 }
1177 x = *value_addr;
1178 return x;
1179 }
Antoine Pitroue965d972012-02-27 00:45:12 +01001180 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001181 x = PyDict_GetItemWithError((PyObject *)globals, key);
1182 if (x != NULL)
1183 return x;
1184 if (PyErr_Occurred())
1185 return NULL;
1186 return PyDict_GetItemWithError((PyObject *)builtins, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001187}
1188
Antoine Pitroue965d972012-02-27 00:45:12 +01001189/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1190 * dictionary if it's merely replacing the value for an existing key.
1191 * This means that it's safe to loop over a dictionary with PyDict_Next()
1192 * and occasionally replace a value -- but you can't insert new keys or
1193 * remove them.
1194 */
1195int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001196PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001197{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001198 PyDictObject *mp;
1199 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001200 if (!PyDict_Check(op)) {
1201 PyErr_BadInternalCall();
1202 return -1;
1203 }
1204 assert(key);
1205 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001206 mp = (PyDictObject *)op;
1207 if (!PyUnicode_CheckExact(key) ||
1208 (hash = ((PyASCIIObject *) key)->hash) == -1)
1209 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001210 hash = PyObject_Hash(key);
1211 if (hash == -1)
1212 return -1;
1213 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001214
1215 /* insertdict() handles any resizing that might be necessary */
1216 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001217}
1218
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001219int
Tim Peters1f5871e2000-07-04 17:44:48 +00001220PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001221{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001222 PyDictObject *mp;
1223 Py_hash_t hash;
1224 PyDictKeyEntry *ep;
1225 PyObject *old_key, *old_value;
1226 PyObject **value_addr;
Guido van Rossumd7047b31995-01-02 19:07:15 +00001227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 if (!PyDict_Check(op)) {
1229 PyErr_BadInternalCall();
1230 return -1;
1231 }
1232 assert(key);
1233 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001234 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 hash = PyObject_Hash(key);
1236 if (hash == -1)
1237 return -1;
1238 }
1239 mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001240 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 if (ep == NULL)
1242 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001243 if (*value_addr == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 set_key_error(key);
1245 return -1;
1246 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001247 old_value = *value_addr;
1248 *value_addr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001250 if (!_PyDict_HasSplitTable(mp)) {
1251 ENSURE_ALLOWS_DELETIONS(mp);
1252 old_key = ep->me_key;
1253 Py_INCREF(dummy);
1254 ep->me_key = dummy;
1255 Py_DECREF(old_key);
1256 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 Py_DECREF(old_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001259}
1260
Guido van Rossum25831651993-05-19 14:50:45 +00001261void
Tim Peters1f5871e2000-07-04 17:44:48 +00001262PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001265 PyDictKeysObject *oldkeys;
1266 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 if (!PyDict_Check(op))
1270 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001271 mp = ((PyDictObject *)op);
1272 oldkeys = mp->ma_keys;
1273 oldvalues = mp->ma_values;
1274 if (oldvalues == empty_values)
1275 return;
1276 /* Empty the dict... */
1277 DK_INCREF(Py_EMPTY_KEYS);
1278 mp->ma_keys = Py_EMPTY_KEYS;
1279 mp->ma_values = empty_values;
1280 mp->ma_used = 0;
1281 /* ...then clear the keys and values */
1282 if (oldvalues != NULL) {
1283 n = DK_SIZE(oldkeys);
1284 for (i = 0; i < n; i++)
1285 Py_CLEAR(oldvalues[i]);
1286 free_values(oldvalues);
1287 DK_DECREF(oldkeys);
1288 }
1289 else {
1290 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001291 DK_DECREF(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001292 }
1293}
1294
1295/* Returns -1 if no more items (or op is not a dict),
1296 * index of item otherwise. Stores value in pvalue
1297 */
1298Py_LOCAL_INLINE(Py_ssize_t)
1299dict_next(PyObject *op, Py_ssize_t i, PyObject **pvalue)
1300{
1301 Py_ssize_t mask, offset;
1302 PyDictObject *mp;
1303 PyObject **value_ptr;
1304
1305
1306 if (!PyDict_Check(op))
1307 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001309 if (i < 0)
1310 return -1;
1311 if (mp->ma_values) {
1312 value_ptr = &mp->ma_values[i];
1313 offset = sizeof(PyObject *);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001315 else {
1316 value_ptr = &mp->ma_keys->dk_entries[i].me_value;
1317 offset = sizeof(PyDictKeyEntry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001319 mask = DK_MASK(mp->ma_keys);
1320 while (i <= mask && *value_ptr == NULL) {
1321 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1322 i++;
1323 }
1324 if (i > mask)
1325 return -1;
1326 if (pvalue)
1327 *pvalue = *value_ptr;
1328 return i;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001329}
1330
Tim Peters080c88b2003-02-15 03:01:11 +00001331/*
1332 * Iterate over a dict. Use like so:
1333 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001334 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001335 * PyObject *key, *value;
1336 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001337 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +00001338 * Refer to borrowed references in key and value.
1339 * }
1340 *
1341 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001342 * mutates the dict. One exception: it is safe if the loop merely changes
1343 * the values associated with the keys (but doesn't insert new keys or
1344 * delete keys), via PyDict_SetItem().
1345 */
Guido van Rossum25831651993-05-19 14:50:45 +00001346int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001347PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001348{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001349 PyDictObject *mp;
1350 Py_ssize_t i = dict_next(op, *ppos, pvalue);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 if (i < 0)
1352 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001353 mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 *ppos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 if (pkey)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001356 *pkey = mp->ma_keys->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001358}
1359
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001360/* Internal version of PyDict_Next that returns a hash value in addition
1361 * to the key and value.
1362 */
Thomas Wouterscf297e42007-02-23 15:07:44 +00001363int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001364_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1365 PyObject **pvalue, Py_hash_t *phash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00001366{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001367 PyDictObject *mp;
1368 Py_ssize_t i = dict_next(op, *ppos, pvalue);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 if (i < 0)
1370 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001371 mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 *ppos = i+1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001373 *phash = mp->ma_keys->dk_entries[i].me_hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 if (pkey)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001375 *pkey = mp->ma_keys->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 return 1;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001377}
1378
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001379/* Methods */
1380
1381static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001382dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001383{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001384 PyObject **values = mp->ma_values;
1385 PyDictKeysObject *keys = mp->ma_keys;
1386 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 PyObject_GC_UnTrack(mp);
1388 Py_TRASHCAN_SAFE_BEGIN(mp)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001389 if (values != NULL) {
1390 if (values != empty_values) {
1391 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
1392 Py_XDECREF(values[i]);
1393 }
1394 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001396 DK_DECREF(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02001398 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001399 assert(keys->dk_refcnt == 1);
1400 DK_DECREF(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001401 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1403 free_list[numfree++] = mp;
1404 else
1405 Py_TYPE(mp)->tp_free((PyObject *)mp);
1406 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001407}
1408
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001409
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001410static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001411dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001412{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 Py_ssize_t i;
1414 PyObject *s, *temp, *colon = NULL;
1415 PyObject *pieces = NULL, *result = NULL;
1416 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +00001417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 i = Py_ReprEnter((PyObject *)mp);
1419 if (i != 0) {
1420 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
1421 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 if (mp->ma_used == 0) {
1424 result = PyUnicode_FromString("{}");
1425 goto Done;
1426 }
Tim Petersa7259592001-06-16 05:11:17 +00001427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 pieces = PyList_New(0);
1429 if (pieces == NULL)
1430 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 colon = PyUnicode_FromString(": ");
1433 if (colon == NULL)
1434 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 /* Do repr() on each key+value pair, and insert ": " between them.
1437 Note that repr may mutate the dict. */
1438 i = 0;
1439 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
1440 int status;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001441 /* Prevent repr from deleting key or value during key format. */
1442 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 Py_INCREF(value);
1444 s = PyObject_Repr(key);
1445 PyUnicode_Append(&s, colon);
1446 PyUnicode_AppendAndDel(&s, PyObject_Repr(value));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001447 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 Py_DECREF(value);
1449 if (s == NULL)
1450 goto Done;
1451 status = PyList_Append(pieces, s);
1452 Py_DECREF(s); /* append created a new ref */
1453 if (status < 0)
1454 goto Done;
1455 }
Tim Petersa7259592001-06-16 05:11:17 +00001456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 /* Add "{}" decorations to the first and last items. */
1458 assert(PyList_GET_SIZE(pieces) > 0);
1459 s = PyUnicode_FromString("{");
1460 if (s == NULL)
1461 goto Done;
1462 temp = PyList_GET_ITEM(pieces, 0);
1463 PyUnicode_AppendAndDel(&s, temp);
1464 PyList_SET_ITEM(pieces, 0, s);
1465 if (s == NULL)
1466 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001467
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001468 s = PyUnicode_FromString("}");
1469 if (s == NULL)
1470 goto Done;
1471 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
1472 PyUnicode_AppendAndDel(&temp, s);
1473 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
1474 if (temp == NULL)
1475 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 /* Paste them all together with ", " between. */
1478 s = PyUnicode_FromString(", ");
1479 if (s == NULL)
1480 goto Done;
1481 result = PyUnicode_Join(s, pieces);
1482 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +00001483
1484Done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 Py_XDECREF(pieces);
1486 Py_XDECREF(colon);
1487 Py_ReprLeave((PyObject *)mp);
1488 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001489}
1490
Martin v. Löwis18e16552006-02-15 17:27:45 +00001491static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001492dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001493{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001494 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001495}
1496
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001497static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001498dict_subscript(PyDictObject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001499{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001500 PyObject *v;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001501 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001502 PyDictKeyEntry *ep;
1503 PyObject **value_addr;
1504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001506 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 hash = PyObject_Hash(key);
1508 if (hash == -1)
1509 return NULL;
1510 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001511 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001512 if (ep == NULL)
1513 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001514 v = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 if (v == NULL) {
1516 if (!PyDict_CheckExact(mp)) {
1517 /* Look up __missing__ method if we're a subclass. */
1518 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05001519 _Py_IDENTIFIER(__missing__);
1520 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 if (missing != NULL) {
1522 res = PyObject_CallFunctionObjArgs(missing,
1523 key, NULL);
1524 Py_DECREF(missing);
1525 return res;
1526 }
1527 else if (PyErr_Occurred())
1528 return NULL;
1529 }
1530 set_key_error(key);
1531 return NULL;
1532 }
1533 else
1534 Py_INCREF(v);
1535 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001536}
1537
1538static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001539dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001540{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001541 if (w == NULL)
1542 return PyDict_DelItem((PyObject *)mp, v);
1543 else
1544 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001545}
1546
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001547static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548 (lenfunc)dict_length, /*mp_length*/
1549 (binaryfunc)dict_subscript, /*mp_subscript*/
1550 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001551};
1552
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001553static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001554dict_keys(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001555{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 register PyObject *v;
1557 register Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001558 PyDictKeyEntry *ep;
1559 Py_ssize_t size, n, offset;
1560 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001561
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001562 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 n = mp->ma_used;
1564 v = PyList_New(n);
1565 if (v == NULL)
1566 return NULL;
1567 if (n != mp->ma_used) {
1568 /* Durnit. The allocations caused the dict to resize.
1569 * Just start over, this shouldn't normally happen.
1570 */
1571 Py_DECREF(v);
1572 goto again;
1573 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001574 ep = &mp->ma_keys->dk_entries[0];
1575 size = DK_SIZE(mp->ma_keys);
1576 if (mp->ma_values) {
1577 value_ptr = mp->ma_values;
1578 offset = sizeof(PyObject *);
1579 }
1580 else {
1581 value_ptr = &ep[0].me_value;
1582 offset = sizeof(PyDictKeyEntry);
1583 }
1584 for (i = 0, j = 0; i < size; i++) {
1585 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586 PyObject *key = ep[i].me_key;
1587 Py_INCREF(key);
1588 PyList_SET_ITEM(v, j, key);
1589 j++;
1590 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001591 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592 }
1593 assert(j == n);
1594 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001595}
1596
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001597static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001598dict_values(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001599{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001600 register PyObject *v;
1601 register Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001602 Py_ssize_t size, n, offset;
1603 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001604
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001605 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 n = mp->ma_used;
1607 v = PyList_New(n);
1608 if (v == NULL)
1609 return NULL;
1610 if (n != mp->ma_used) {
1611 /* Durnit. The allocations caused the dict to resize.
1612 * Just start over, this shouldn't normally happen.
1613 */
1614 Py_DECREF(v);
1615 goto again;
1616 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001617 size = DK_SIZE(mp->ma_keys);
1618 if (mp->ma_values) {
1619 value_ptr = mp->ma_values;
1620 offset = sizeof(PyObject *);
1621 }
1622 else {
1623 value_ptr = &mp->ma_keys->dk_entries[0].me_value;
1624 offset = sizeof(PyDictKeyEntry);
1625 }
1626 for (i = 0, j = 0; i < size; i++) {
1627 PyObject *value = *value_ptr;
1628 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1629 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001630 Py_INCREF(value);
1631 PyList_SET_ITEM(v, j, value);
1632 j++;
1633 }
1634 }
1635 assert(j == n);
1636 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001637}
1638
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001639static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001640dict_items(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001641{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 register PyObject *v;
1643 register Py_ssize_t i, j, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001644 Py_ssize_t size, offset;
1645 PyObject *item, *key;
1646 PyDictKeyEntry *ep;
1647 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 /* Preallocate the list of tuples, to avoid allocations during
1650 * the loop over the items, which could trigger GC, which
1651 * could resize the dict. :-(
1652 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001653 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 n = mp->ma_used;
1655 v = PyList_New(n);
1656 if (v == NULL)
1657 return NULL;
1658 for (i = 0; i < n; i++) {
1659 item = PyTuple_New(2);
1660 if (item == NULL) {
1661 Py_DECREF(v);
1662 return NULL;
1663 }
1664 PyList_SET_ITEM(v, i, item);
1665 }
1666 if (n != mp->ma_used) {
1667 /* Durnit. The allocations caused the dict to resize.
1668 * Just start over, this shouldn't normally happen.
1669 */
1670 Py_DECREF(v);
1671 goto again;
1672 }
1673 /* Nothing we do below makes any function calls. */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001674 ep = mp->ma_keys->dk_entries;
1675 size = DK_SIZE(mp->ma_keys);
1676 if (mp->ma_values) {
1677 value_ptr = mp->ma_values;
1678 offset = sizeof(PyObject *);
1679 }
1680 else {
1681 value_ptr = &ep[0].me_value;
1682 offset = sizeof(PyDictKeyEntry);
1683 }
1684 for (i = 0, j = 0; i < size; i++) {
1685 PyObject *value = *value_ptr;
1686 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1687 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 key = ep[i].me_key;
1689 item = PyList_GET_ITEM(v, j);
1690 Py_INCREF(key);
1691 PyTuple_SET_ITEM(item, 0, key);
1692 Py_INCREF(value);
1693 PyTuple_SET_ITEM(item, 1, value);
1694 j++;
1695 }
1696 }
1697 assert(j == n);
1698 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001699}
1700
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001701static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001702dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001703{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 PyObject *seq;
1705 PyObject *value = Py_None;
1706 PyObject *it; /* iter(seq) */
1707 PyObject *key;
1708 PyObject *d;
1709 int status;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
1712 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 d = PyObject_CallObject(cls, NULL);
1715 if (d == NULL)
1716 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001717
Benjamin Peterson9892f522012-10-31 14:22:12 -04001718 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001719 if (PyDict_CheckExact(seq)) {
1720 PyDictObject *mp = (PyDictObject *)d;
1721 PyObject *oldvalue;
1722 Py_ssize_t pos = 0;
1723 PyObject *key;
1724 Py_hash_t hash;
Guido van Rossum58da9312007-11-10 23:39:45 +00001725
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001726 if (dictresize(mp, Py_SIZE(seq))) {
Petri Lehtinena94200e2011-10-24 21:12:58 +03001727 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001728 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001729 }
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001730
1731 while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001732 if (insertdict(mp, key, hash, value)) {
1733 Py_DECREF(d);
1734 return NULL;
1735 }
1736 }
1737 return d;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001738 }
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001739 if (PyAnySet_CheckExact(seq)) {
1740 PyDictObject *mp = (PyDictObject *)d;
1741 Py_ssize_t pos = 0;
1742 PyObject *key;
1743 Py_hash_t hash;
Guido van Rossum58da9312007-11-10 23:39:45 +00001744
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001745 if (dictresize(mp, PySet_GET_SIZE(seq))) {
Petri Lehtinena94200e2011-10-24 21:12:58 +03001746 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001747 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001748 }
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001749
1750 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001751 if (insertdict(mp, key, hash, value)) {
1752 Py_DECREF(d);
1753 return NULL;
1754 }
1755 }
1756 return d;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001757 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001758 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001760 it = PyObject_GetIter(seq);
1761 if (it == NULL){
1762 Py_DECREF(d);
1763 return NULL;
1764 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766 if (PyDict_CheckExact(d)) {
1767 while ((key = PyIter_Next(it)) != NULL) {
1768 status = PyDict_SetItem(d, key, value);
1769 Py_DECREF(key);
1770 if (status < 0)
1771 goto Fail;
1772 }
1773 } else {
1774 while ((key = PyIter_Next(it)) != NULL) {
1775 status = PyObject_SetItem(d, key, value);
1776 Py_DECREF(key);
1777 if (status < 0)
1778 goto Fail;
1779 }
1780 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001781
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001782 if (PyErr_Occurred())
1783 goto Fail;
1784 Py_DECREF(it);
1785 return d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001786
1787Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001788 Py_DECREF(it);
1789 Py_DECREF(d);
1790 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001791}
1792
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001793static int
1794dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001795{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001796 PyObject *arg = NULL;
1797 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1800 result = -1;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001802 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001803 _Py_IDENTIFIER(keys);
1804 if (_PyObject_HasAttrId(arg, &PyId_keys))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001805 result = PyDict_Merge(self, arg, 1);
1806 else
1807 result = PyDict_MergeFromSeq2(self, arg, 1);
1808 }
1809 if (result == 0 && kwds != NULL) {
1810 if (PyArg_ValidateKeywordArguments(kwds))
1811 result = PyDict_Merge(self, kwds, 1);
1812 else
1813 result = -1;
1814 }
1815 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001816}
1817
1818static PyObject *
1819dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1820{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001821 if (dict_update_common(self, args, kwds, "update") != -1)
1822 Py_RETURN_NONE;
1823 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001824}
1825
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001826/* Update unconditionally replaces existing items.
1827 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001828 otherwise it leaves existing items unchanged.
1829
1830 PyDict_{Update,Merge} update/merge from a mapping object.
1831
Tim Petersf582b822001-12-11 18:51:08 +00001832 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001833 producing iterable objects of length 2.
1834*/
1835
Tim Petersf582b822001-12-11 18:51:08 +00001836int
Tim Peters1fc240e2001-10-26 05:06:50 +00001837PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1838{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001839 PyObject *it; /* iter(seq2) */
1840 Py_ssize_t i; /* index into seq2 of current element */
1841 PyObject *item; /* seq2[i] */
1842 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00001843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844 assert(d != NULL);
1845 assert(PyDict_Check(d));
1846 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00001847
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001848 it = PyObject_GetIter(seq2);
1849 if (it == NULL)
1850 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001852 for (i = 0; ; ++i) {
1853 PyObject *key, *value;
1854 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856 fast = NULL;
1857 item = PyIter_Next(it);
1858 if (item == NULL) {
1859 if (PyErr_Occurred())
1860 goto Fail;
1861 break;
1862 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001863
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001864 /* Convert item to sequence, and verify length 2. */
1865 fast = PySequence_Fast(item, "");
1866 if (fast == NULL) {
1867 if (PyErr_ExceptionMatches(PyExc_TypeError))
1868 PyErr_Format(PyExc_TypeError,
1869 "cannot convert dictionary update "
1870 "sequence element #%zd to a sequence",
1871 i);
1872 goto Fail;
1873 }
1874 n = PySequence_Fast_GET_SIZE(fast);
1875 if (n != 2) {
1876 PyErr_Format(PyExc_ValueError,
1877 "dictionary update sequence element #%zd "
1878 "has length %zd; 2 is required",
1879 i, n);
1880 goto Fail;
1881 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001883 /* Update/merge with this (key, value) pair. */
1884 key = PySequence_Fast_GET_ITEM(fast, 0);
1885 value = PySequence_Fast_GET_ITEM(fast, 1);
1886 if (override || PyDict_GetItem(d, key) == NULL) {
1887 int status = PyDict_SetItem(d, key, value);
1888 if (status < 0)
1889 goto Fail;
1890 }
1891 Py_DECREF(fast);
1892 Py_DECREF(item);
1893 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 i = 0;
1896 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00001897Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 Py_XDECREF(item);
1899 Py_XDECREF(fast);
1900 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001901Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 Py_DECREF(it);
1903 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001904}
1905
Tim Peters6d6c1a32001-08-02 04:15:00 +00001906int
1907PyDict_Update(PyObject *a, PyObject *b)
1908{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909 return PyDict_Merge(a, b, 1);
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001910}
1911
1912int
1913PyDict_Merge(PyObject *a, PyObject *b, int override)
1914{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915 register PyDictObject *mp, *other;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001916 register Py_ssize_t i, n;
1917 PyDictKeyEntry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919 /* We accept for the argument either a concrete dictionary object,
1920 * or an abstract "mapping" object. For the former, we can do
1921 * things quite efficiently. For the latter, we only require that
1922 * PyMapping_Keys() and PyObject_GetItem() be supported.
1923 */
1924 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1925 PyErr_BadInternalCall();
1926 return -1;
1927 }
1928 mp = (PyDictObject*)a;
1929 if (PyDict_Check(b)) {
1930 other = (PyDictObject*)b;
1931 if (other == mp || other->ma_used == 0)
1932 /* a.update(a) or a.update({}); nothing to do */
1933 return 0;
1934 if (mp->ma_used == 0)
1935 /* Since the target dict is empty, PyDict_GetItem()
1936 * always returns NULL. Setting override to 1
1937 * skips the unnecessary test.
1938 */
1939 override = 1;
1940 /* Do one big resize at the start, rather than
1941 * incrementally resizing as we insert new items. Expect
1942 * that there will be no (or few) overlapping keys.
1943 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001944 if (mp->ma_keys->dk_usable * 3 < other->ma_used * 2)
1945 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001946 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001947 for (i = 0, n = DK_SIZE(other->ma_keys); i < n; i++) {
1948 PyObject *value;
1949 entry = &other->ma_keys->dk_entries[i];
1950 if (other->ma_values)
1951 value = other->ma_values[i];
1952 else
1953 value = entry->me_value;
1954
1955 if (value != NULL &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 (override ||
1957 PyDict_GetItem(a, entry->me_key) == NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001958 if (insertdict(mp, entry->me_key,
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001959 entry->me_hash,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001960 value) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 return -1;
1962 }
1963 }
1964 }
1965 else {
1966 /* Do it the generic, slower way */
1967 PyObject *keys = PyMapping_Keys(b);
1968 PyObject *iter;
1969 PyObject *key, *value;
1970 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001972 if (keys == NULL)
1973 /* Docstring says this is equivalent to E.keys() so
1974 * if E doesn't have a .keys() method we want
1975 * AttributeError to percolate up. Might as well
1976 * do the same for any other error.
1977 */
1978 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980 iter = PyObject_GetIter(keys);
1981 Py_DECREF(keys);
1982 if (iter == NULL)
1983 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001985 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
1986 if (!override && PyDict_GetItem(a, key) != NULL) {
1987 Py_DECREF(key);
1988 continue;
1989 }
1990 value = PyObject_GetItem(b, key);
1991 if (value == NULL) {
1992 Py_DECREF(iter);
1993 Py_DECREF(key);
1994 return -1;
1995 }
1996 status = PyDict_SetItem(a, key, value);
1997 Py_DECREF(key);
1998 Py_DECREF(value);
1999 if (status < 0) {
2000 Py_DECREF(iter);
2001 return -1;
2002 }
2003 }
2004 Py_DECREF(iter);
2005 if (PyErr_Occurred())
2006 /* Iterator completed, via error */
2007 return -1;
2008 }
2009 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002010}
2011
2012static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002013dict_copy(register PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002014{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002016}
2017
2018PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002019PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002020{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002021 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002022 PyDictObject *mp;
2023 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002024
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 if (o == NULL || !PyDict_Check(o)) {
2026 PyErr_BadInternalCall();
2027 return NULL;
2028 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002029 mp = (PyDictObject *)o;
2030 if (_PyDict_HasSplitTable(mp)) {
2031 PyDictObject *split_copy;
2032 PyObject **newvalues = new_values(DK_SIZE(mp->ma_keys));
2033 if (newvalues == NULL)
2034 return PyErr_NoMemory();
2035 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2036 if (split_copy == NULL) {
2037 free_values(newvalues);
2038 return NULL;
2039 }
2040 split_copy->ma_values = newvalues;
2041 split_copy->ma_keys = mp->ma_keys;
2042 split_copy->ma_used = mp->ma_used;
2043 DK_INCREF(mp->ma_keys);
2044 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2045 PyObject *value = mp->ma_values[i];
2046 Py_XINCREF(value);
2047 split_copy->ma_values[i] = value;
2048 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002049 if (_PyObject_GC_IS_TRACKED(mp))
2050 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002051 return (PyObject *)split_copy;
2052 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053 copy = PyDict_New();
2054 if (copy == NULL)
2055 return NULL;
2056 if (PyDict_Merge(copy, o, 1) == 0)
2057 return copy;
2058 Py_DECREF(copy);
2059 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002060}
2061
Martin v. Löwis18e16552006-02-15 17:27:45 +00002062Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002063PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002064{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002065 if (mp == NULL || !PyDict_Check(mp)) {
2066 PyErr_BadInternalCall();
2067 return -1;
2068 }
2069 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002070}
2071
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002072PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002073PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002074{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002075 if (mp == NULL || !PyDict_Check(mp)) {
2076 PyErr_BadInternalCall();
2077 return NULL;
2078 }
2079 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002080}
2081
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002082PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002083PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002084{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002085 if (mp == NULL || !PyDict_Check(mp)) {
2086 PyErr_BadInternalCall();
2087 return NULL;
2088 }
2089 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002090}
2091
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002092PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002093PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002094{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002095 if (mp == NULL || !PyDict_Check(mp)) {
2096 PyErr_BadInternalCall();
2097 return NULL;
2098 }
2099 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002100}
2101
Tim Peterse63415e2001-05-08 04:38:29 +00002102/* Return 1 if dicts equal, 0 if not, -1 if error.
2103 * Gets out as soon as any difference is detected.
2104 * Uses only Py_EQ comparison.
2105 */
2106static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002107dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002108{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002109 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002110
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002111 if (a->ma_used != b->ma_used)
2112 /* can't be equal if # of entries differ */
2113 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002114 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002115 for (i = 0; i < DK_SIZE(a->ma_keys); i++) {
2116 PyDictKeyEntry *ep = &a->ma_keys->dk_entries[i];
2117 PyObject *aval;
2118 if (a->ma_values)
2119 aval = a->ma_values[i];
2120 else
2121 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122 if (aval != NULL) {
2123 int cmp;
2124 PyObject *bval;
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002125 PyObject **vaddr;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002126 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002127 /* temporarily bump aval's refcount to ensure it stays
2128 alive until we're done with it */
2129 Py_INCREF(aval);
2130 /* ditto for key */
2131 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002132 /* reuse the known hash value */
2133 if ((b->ma_keys->dk_lookup)(b, key, ep->me_hash, &vaddr) == NULL)
2134 bval = NULL;
2135 else
2136 bval = *vaddr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002137 Py_DECREF(key);
2138 if (bval == NULL) {
2139 Py_DECREF(aval);
2140 if (PyErr_Occurred())
2141 return -1;
2142 return 0;
2143 }
2144 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
2145 Py_DECREF(aval);
2146 if (cmp <= 0) /* error or not equal */
2147 return cmp;
2148 }
2149 }
2150 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002151}
Tim Peterse63415e2001-05-08 04:38:29 +00002152
2153static PyObject *
2154dict_richcompare(PyObject *v, PyObject *w, int op)
2155{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002156 int cmp;
2157 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002159 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2160 res = Py_NotImplemented;
2161 }
2162 else if (op == Py_EQ || op == Py_NE) {
2163 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2164 if (cmp < 0)
2165 return NULL;
2166 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2167 }
2168 else
2169 res = Py_NotImplemented;
2170 Py_INCREF(res);
2171 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002172}
Tim Peterse63415e2001-05-08 04:38:29 +00002173
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002174static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002175dict_contains(register PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002176{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002177 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002178 PyDictKeyEntry *ep;
2179 PyObject **value_addr;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002181 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002182 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002183 hash = PyObject_Hash(key);
2184 if (hash == -1)
2185 return NULL;
2186 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002187 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002188 if (ep == NULL)
2189 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002190 return PyBool_FromLong(*value_addr != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002191}
2192
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002193static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002194dict_get(register PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00002195{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002196 PyObject *key;
2197 PyObject *failobj = Py_None;
2198 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002199 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002200 PyDictKeyEntry *ep;
2201 PyObject **value_addr;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002202
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002203 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
2204 return NULL;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002206 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002207 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002208 hash = PyObject_Hash(key);
2209 if (hash == -1)
2210 return NULL;
2211 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002212 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002213 if (ep == NULL)
2214 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002215 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002216 if (val == NULL)
2217 val = failobj;
2218 Py_INCREF(val);
2219 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002220}
2221
Benjamin Peterson00e98862013-03-07 22:16:29 -05002222PyObject *
2223PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002224{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002225 PyDictObject *mp = (PyDictObject *)d;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002226 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002227 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002228 PyDictKeyEntry *ep;
2229 PyObject **value_addr;
Guido van Rossum164452c2000-08-08 16:12:54 +00002230
Benjamin Peterson00e98862013-03-07 22:16:29 -05002231 if (!PyDict_Check(d)) {
2232 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002233 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002234 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002235 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002236 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002237 hash = PyObject_Hash(key);
2238 if (hash == -1)
2239 return NULL;
2240 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002241 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002242 if (ep == NULL)
2243 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002244 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002245 if (val == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002246 if (mp->ma_keys->dk_usable <= 0) {
2247 /* Need to resize. */
2248 if (insertion_resize(mp) < 0)
2249 return NULL;
2250 ep = find_empty_slot(mp, key, hash, &value_addr);
2251 }
Benjamin Peterson00e98862013-03-07 22:16:29 -05002252 Py_INCREF(defaultobj);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002253 Py_INCREF(key);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002254 MAINTAIN_TRACKING(mp, key, defaultobj);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002255 ep->me_key = key;
2256 ep->me_hash = hash;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002257 *value_addr = defaultobj;
2258 val = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002259 mp->ma_keys->dk_usable--;
2260 mp->ma_used++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002262 return val;
Guido van Rossum164452c2000-08-08 16:12:54 +00002263}
2264
Benjamin Peterson00e98862013-03-07 22:16:29 -05002265static PyObject *
2266dict_setdefault(PyDictObject *mp, PyObject *args)
2267{
2268 PyObject *key, *val;
2269 PyObject *defaultobj = Py_None;
2270
2271 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &defaultobj))
2272 return NULL;
2273
Benjamin Peterson55898502013-03-08 08:36:49 -05002274 val = PyDict_SetDefault((PyObject *)mp, key, defaultobj);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002275 Py_XINCREF(val);
2276 return val;
2277}
Guido van Rossum164452c2000-08-08 16:12:54 +00002278
2279static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002280dict_clear(register PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002281{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002282 PyDict_Clear((PyObject *)mp);
2283 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002284}
2285
Guido van Rossumba6ab842000-12-12 22:02:18 +00002286static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002287dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002288{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002289 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002290 PyObject *old_value, *old_key;
2291 PyObject *key, *deflt = NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002292 PyDictKeyEntry *ep;
2293 PyObject **value_addr;
Guido van Rossume027d982002-04-12 15:11:59 +00002294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002295 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2296 return NULL;
2297 if (mp->ma_used == 0) {
2298 if (deflt) {
2299 Py_INCREF(deflt);
2300 return deflt;
2301 }
Raymond Hettingerdd421542010-10-30 08:10:29 +00002302 set_key_error(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002303 return NULL;
2304 }
2305 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002306 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 hash = PyObject_Hash(key);
2308 if (hash == -1)
2309 return NULL;
2310 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002311 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002312 if (ep == NULL)
2313 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002314 old_value = *value_addr;
2315 if (old_value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002316 if (deflt) {
2317 Py_INCREF(deflt);
2318 return deflt;
2319 }
2320 set_key_error(key);
2321 return NULL;
2322 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002323 *value_addr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002324 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002325 if (!_PyDict_HasSplitTable(mp)) {
2326 ENSURE_ALLOWS_DELETIONS(mp);
2327 old_key = ep->me_key;
2328 Py_INCREF(dummy);
2329 ep->me_key = dummy;
2330 Py_DECREF(old_key);
2331 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002332 return old_value;
Guido van Rossume027d982002-04-12 15:11:59 +00002333}
2334
2335static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002336dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00002337{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002338 Py_hash_t i = 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002339 PyDictKeyEntry *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002340 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002341
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002343 /* Allocate the result tuple before checking the size. Believe it
2344 * or not, this allocation could trigger a garbage collection which
2345 * could empty the dict, so if we checked the size first and that
2346 * happened, the result would be an infinite loop (searching for an
2347 * entry that no longer exists). Note that the usual popitem()
2348 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2349 * tuple away if the dict *is* empty isn't a significant
2350 * inefficiency -- possible, but unlikely in practice.
2351 */
2352 res = PyTuple_New(2);
2353 if (res == NULL)
2354 return NULL;
2355 if (mp->ma_used == 0) {
2356 Py_DECREF(res);
2357 PyErr_SetString(PyExc_KeyError,
2358 "popitem(): dictionary is empty");
2359 return NULL;
2360 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002361 /* Convert split table to combined table */
2362 if (mp->ma_keys->dk_lookup == lookdict_split) {
2363 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
2364 Py_DECREF(res);
2365 return NULL;
2366 }
2367 }
2368 ENSURE_ALLOWS_DELETIONS(mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 /* Set ep to "the first" dict entry with a value. We abuse the hash
2370 * field of slot 0 to hold a search finger:
2371 * If slot 0 has a value, use slot 0.
2372 * Else slot 0 is being used to hold a search finger,
2373 * and we use its hash value as the first index to look.
2374 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002375 ep = &mp->ma_keys->dk_entries[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376 if (ep->me_value == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002377 Py_ssize_t mask = DK_MASK(mp->ma_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002378 i = ep->me_hash;
2379 /* The hash field may be a real hash value, or it may be a
2380 * legit search finger, or it may be a once-legit search
2381 * finger that's out of bounds now because it wrapped around
2382 * or the table shrunk -- simply make sure it's in bounds now.
2383 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002384 if (i > mask || i < 1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002385 i = 1; /* skip slot 0 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002386 while ((ep = &mp->ma_keys->dk_entries[i])->me_value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002387 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002388 if (i > mask)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 i = 1;
2390 }
2391 }
2392 PyTuple_SET_ITEM(res, 0, ep->me_key);
2393 PyTuple_SET_ITEM(res, 1, ep->me_value);
2394 Py_INCREF(dummy);
2395 ep->me_key = dummy;
2396 ep->me_value = NULL;
2397 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002398 assert(mp->ma_keys->dk_entries[0].me_value == NULL);
2399 mp->ma_keys->dk_entries[0].me_hash = i + 1; /* next place to start */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002400 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002401}
2402
Jeremy Hylton8caad492000-06-23 14:18:11 +00002403static int
2404dict_traverse(PyObject *op, visitproc visit, void *arg)
2405{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002406 Py_ssize_t i, n;
2407 PyDictObject *mp = (PyDictObject *)op;
2408 if (mp->ma_keys->dk_lookup == lookdict) {
2409 for (i = 0; i < DK_SIZE(mp->ma_keys); i++) {
2410 if (mp->ma_keys->dk_entries[i].me_value != NULL) {
2411 Py_VISIT(mp->ma_keys->dk_entries[i].me_value);
2412 Py_VISIT(mp->ma_keys->dk_entries[i].me_key);
2413 }
2414 }
2415 } else {
2416 if (mp->ma_values != NULL) {
2417 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2418 Py_VISIT(mp->ma_values[i]);
2419 }
2420 }
2421 else {
2422 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2423 Py_VISIT(mp->ma_keys->dk_entries[i].me_value);
2424 }
2425 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002426 }
2427 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002428}
2429
2430static int
2431dict_tp_clear(PyObject *op)
2432{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002433 PyDict_Clear(op);
2434 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002435}
2436
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002437static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002438
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002439static PyObject *
2440dict_sizeof(PyDictObject *mp)
2441{
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002442 Py_ssize_t size, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002443
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002444 size = DK_SIZE(mp->ma_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002445 res = sizeof(PyDictObject);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002446 if (mp->ma_values)
2447 res += size * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002448 /* If the dictionary is split, the keys portion is accounted-for
2449 in the type object. */
2450 if (mp->ma_keys->dk_refcnt == 1)
2451 res += sizeof(PyDictKeysObject) + (size-1) * sizeof(PyDictKeyEntry);
2452 return PyLong_FromSsize_t(res);
2453}
2454
2455Py_ssize_t
2456_PyDict_KeysSize(PyDictKeysObject *keys)
2457{
2458 return sizeof(PyDictKeysObject) + (DK_SIZE(keys)-1) * sizeof(PyDictKeyEntry);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002459}
2460
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002461PyDoc_STRVAR(contains__doc__,
2462"D.__contains__(k) -> True if D has a key k, else False");
2463
2464PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
2465
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002466PyDoc_STRVAR(sizeof__doc__,
2467"D.__sizeof__() -> size of D in memory, in bytes");
2468
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002469PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002470"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002471
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002472PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002473"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 +00002474
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002475PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002476"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002477If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00002478
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002479PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00002480"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000024812-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002482
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002483PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04002484"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
2485If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
2486If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
2487In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00002488
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002489PyDoc_STRVAR(fromkeys__doc__,
2490"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
2491v defaults to None.");
2492
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002493PyDoc_STRVAR(clear__doc__,
2494"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002495
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002496PyDoc_STRVAR(copy__doc__,
2497"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00002498
Guido van Rossumb90c8482007-02-10 01:11:45 +00002499/* Forward */
2500static PyObject *dictkeys_new(PyObject *);
2501static PyObject *dictitems_new(PyObject *);
2502static PyObject *dictvalues_new(PyObject *);
2503
Guido van Rossum45c85d12007-07-27 16:31:40 +00002504PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002505 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002506PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002507 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002508PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002509 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00002510
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002511static PyMethodDef mapp_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002512 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
2513 contains__doc__},
2514 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
2515 getitem__doc__},
2516 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
2517 sizeof__doc__},
2518 {"get", (PyCFunction)dict_get, METH_VARARGS,
2519 get__doc__},
2520 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
2521 setdefault_doc__},
2522 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
2523 pop__doc__},
2524 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
2525 popitem__doc__},
2526 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
2527 keys__doc__},
2528 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
2529 items__doc__},
2530 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
2531 values__doc__},
2532 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
2533 update__doc__},
2534 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
2535 fromkeys__doc__},
2536 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
2537 clear__doc__},
2538 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
2539 copy__doc__},
2540 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002541};
2542
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002543/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002544int
2545PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002546{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002547 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002548 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002549 PyDictKeyEntry *ep;
2550 PyObject **value_addr;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002552 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002553 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002554 hash = PyObject_Hash(key);
2555 if (hash == -1)
2556 return -1;
2557 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002558 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
2559 return (ep == NULL) ? -1 : (*value_addr != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002560}
2561
Thomas Wouterscf297e42007-02-23 15:07:44 +00002562/* Internal version of PyDict_Contains used when the hash value is already known */
2563int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002564_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00002565{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002566 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002567 PyDictKeyEntry *ep;
2568 PyObject **value_addr;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002569
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002570 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
2571 return (ep == NULL) ? -1 : (*value_addr != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00002572}
2573
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002574/* Hack to implement "key in dict" */
2575static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002576 0, /* sq_length */
2577 0, /* sq_concat */
2578 0, /* sq_repeat */
2579 0, /* sq_item */
2580 0, /* sq_slice */
2581 0, /* sq_ass_item */
2582 0, /* sq_ass_slice */
2583 PyDict_Contains, /* sq_contains */
2584 0, /* sq_inplace_concat */
2585 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002586};
2587
Guido van Rossum09e563a2001-05-01 12:10:21 +00002588static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002589dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2590{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002591 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02002592 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002594 assert(type != NULL && type->tp_alloc != NULL);
2595 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02002596 if (self == NULL)
2597 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02002598 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02002599
Victor Stinnera9f61a52013-07-16 22:17:26 +02002600 /* The object has been implicitly tracked by tp_alloc */
2601 if (type == &PyDict_Type)
2602 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02002603
2604 d->ma_used = 0;
2605 d->ma_keys = new_keys_object(PyDict_MINSIZE_COMBINED);
2606 if (d->ma_keys == NULL) {
2607 Py_DECREF(self);
2608 return NULL;
2609 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002610 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002611}
2612
Tim Peters25786c02001-09-02 08:22:48 +00002613static int
2614dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2615{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002616 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002617}
2618
Tim Peters6d6c1a32001-08-02 04:15:00 +00002619static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002620dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002621{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002622 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002623}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002624
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002625PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002626"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002627"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002628" (key, value) pairs\n"
2629"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002630" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002631" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002632" d[k] = v\n"
2633"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2634" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002635
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002636PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002637 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2638 "dict",
2639 sizeof(PyDictObject),
2640 0,
2641 (destructor)dict_dealloc, /* tp_dealloc */
2642 0, /* tp_print */
2643 0, /* tp_getattr */
2644 0, /* tp_setattr */
2645 0, /* tp_reserved */
2646 (reprfunc)dict_repr, /* tp_repr */
2647 0, /* tp_as_number */
2648 &dict_as_sequence, /* tp_as_sequence */
2649 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002650 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002651 0, /* tp_call */
2652 0, /* tp_str */
2653 PyObject_GenericGetAttr, /* tp_getattro */
2654 0, /* tp_setattro */
2655 0, /* tp_as_buffer */
2656 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2657 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
2658 dictionary_doc, /* tp_doc */
2659 dict_traverse, /* tp_traverse */
2660 dict_tp_clear, /* tp_clear */
2661 dict_richcompare, /* tp_richcompare */
2662 0, /* tp_weaklistoffset */
2663 (getiterfunc)dict_iter, /* tp_iter */
2664 0, /* tp_iternext */
2665 mapp_methods, /* tp_methods */
2666 0, /* tp_members */
2667 0, /* tp_getset */
2668 0, /* tp_base */
2669 0, /* tp_dict */
2670 0, /* tp_descr_get */
2671 0, /* tp_descr_set */
2672 0, /* tp_dictoffset */
2673 dict_init, /* tp_init */
2674 PyType_GenericAlloc, /* tp_alloc */
2675 dict_new, /* tp_new */
2676 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002677};
2678
Victor Stinner3c1e4812012-03-26 22:10:51 +02002679PyObject *
2680_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
2681{
2682 PyObject *kv;
2683 kv = _PyUnicode_FromId(key); /* borrowed */
2684 if (kv == NULL)
2685 return NULL;
2686 return PyDict_GetItem(dp, kv);
2687}
2688
Guido van Rossum3cca2451997-05-16 14:23:33 +00002689/* For backward compatibility with old dictionary interface */
2690
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002691PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002692PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002693{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002694 PyObject *kv, *rv;
2695 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02002696 if (kv == NULL) {
2697 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002698 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02002699 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002700 rv = PyDict_GetItem(v, kv);
2701 Py_DECREF(kv);
2702 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002703}
2704
2705int
Victor Stinner3c1e4812012-03-26 22:10:51 +02002706_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
2707{
2708 PyObject *kv;
2709 kv = _PyUnicode_FromId(key); /* borrowed */
2710 if (kv == NULL)
2711 return -1;
2712 return PyDict_SetItem(v, kv, item);
2713}
2714
2715int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002716PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002717{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002718 PyObject *kv;
2719 int err;
2720 kv = PyUnicode_FromString(key);
2721 if (kv == NULL)
2722 return -1;
2723 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
2724 err = PyDict_SetItem(v, kv, item);
2725 Py_DECREF(kv);
2726 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002727}
2728
2729int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002730PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002731{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002732 PyObject *kv;
2733 int err;
2734 kv = PyUnicode_FromString(key);
2735 if (kv == NULL)
2736 return -1;
2737 err = PyDict_DelItem(v, kv);
2738 Py_DECREF(kv);
2739 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002740}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002741
Raymond Hettinger019a1482004-03-18 02:41:19 +00002742/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002743
2744typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002745 PyObject_HEAD
2746 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
2747 Py_ssize_t di_used;
2748 Py_ssize_t di_pos;
2749 PyObject* di_result; /* reusable result tuple for iteritems */
2750 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002751} dictiterobject;
2752
2753static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002754dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002755{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002756 dictiterobject *di;
2757 di = PyObject_GC_New(dictiterobject, itertype);
2758 if (di == NULL)
2759 return NULL;
2760 Py_INCREF(dict);
2761 di->di_dict = dict;
2762 di->di_used = dict->ma_used;
2763 di->di_pos = 0;
2764 di->len = dict->ma_used;
2765 if (itertype == &PyDictIterItem_Type) {
2766 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2767 if (di->di_result == NULL) {
2768 Py_DECREF(di);
2769 return NULL;
2770 }
2771 }
2772 else
2773 di->di_result = NULL;
2774 _PyObject_GC_TRACK(di);
2775 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002776}
2777
2778static void
2779dictiter_dealloc(dictiterobject *di)
2780{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002781 Py_XDECREF(di->di_dict);
2782 Py_XDECREF(di->di_result);
2783 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002784}
2785
2786static int
2787dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
2788{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002789 Py_VISIT(di->di_dict);
2790 Py_VISIT(di->di_result);
2791 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002792}
2793
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002794static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002795dictiter_len(dictiterobject *di)
2796{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002797 Py_ssize_t len = 0;
2798 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
2799 len = di->len;
2800 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002801}
2802
Guido van Rossumb90c8482007-02-10 01:11:45 +00002803PyDoc_STRVAR(length_hint_doc,
2804 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002805
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002806static PyObject *
2807dictiter_reduce(dictiterobject *di);
2808
2809PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
2810
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002811static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002812 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
2813 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002814 {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS,
2815 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002816 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002817};
2818
Raymond Hettinger019a1482004-03-18 02:41:19 +00002819static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002820{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002821 PyObject *key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002822 register Py_ssize_t i, mask, offset;
2823 register PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002824 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002825 PyObject **value_ptr;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002826
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002827 if (d == NULL)
2828 return NULL;
2829 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002830
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002831 if (di->di_used != d->ma_used) {
2832 PyErr_SetString(PyExc_RuntimeError,
2833 "dictionary changed size during iteration");
2834 di->di_used = -1; /* Make this state sticky */
2835 return NULL;
2836 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002838 i = di->di_pos;
2839 if (i < 0)
2840 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002841 k = d->ma_keys;
2842 if (d->ma_values) {
2843 value_ptr = &d->ma_values[i];
2844 offset = sizeof(PyObject *);
2845 }
2846 else {
2847 value_ptr = &k->dk_entries[i].me_value;
2848 offset = sizeof(PyDictKeyEntry);
2849 }
2850 mask = DK_SIZE(k)-1;
2851 while (i <= mask && *value_ptr == NULL) {
2852 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002853 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002854 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002855 di->di_pos = i+1;
2856 if (i > mask)
2857 goto fail;
2858 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002859 key = k->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002860 Py_INCREF(key);
2861 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002862
2863fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002864 Py_DECREF(d);
2865 di->di_dict = NULL;
2866 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002867}
2868
Raymond Hettinger019a1482004-03-18 02:41:19 +00002869PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002870 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2871 "dict_keyiterator", /* tp_name */
2872 sizeof(dictiterobject), /* tp_basicsize */
2873 0, /* tp_itemsize */
2874 /* methods */
2875 (destructor)dictiter_dealloc, /* tp_dealloc */
2876 0, /* tp_print */
2877 0, /* tp_getattr */
2878 0, /* tp_setattr */
2879 0, /* tp_reserved */
2880 0, /* tp_repr */
2881 0, /* tp_as_number */
2882 0, /* tp_as_sequence */
2883 0, /* tp_as_mapping */
2884 0, /* tp_hash */
2885 0, /* tp_call */
2886 0, /* tp_str */
2887 PyObject_GenericGetAttr, /* tp_getattro */
2888 0, /* tp_setattro */
2889 0, /* tp_as_buffer */
2890 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2891 0, /* tp_doc */
2892 (traverseproc)dictiter_traverse, /* tp_traverse */
2893 0, /* tp_clear */
2894 0, /* tp_richcompare */
2895 0, /* tp_weaklistoffset */
2896 PyObject_SelfIter, /* tp_iter */
2897 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
2898 dictiter_methods, /* tp_methods */
2899 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002900};
2901
2902static PyObject *dictiter_iternextvalue(dictiterobject *di)
2903{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002904 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002905 register Py_ssize_t i, mask, offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002906 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002907 PyObject **value_ptr;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002909 if (d == NULL)
2910 return NULL;
2911 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00002912
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 if (di->di_used != d->ma_used) {
2914 PyErr_SetString(PyExc_RuntimeError,
2915 "dictionary changed size during iteration");
2916 di->di_used = -1; /* Make this state sticky */
2917 return NULL;
2918 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002920 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002921 mask = DK_SIZE(d->ma_keys)-1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002922 if (i < 0 || i > mask)
2923 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002924 if (d->ma_values) {
2925 value_ptr = &d->ma_values[i];
2926 offset = sizeof(PyObject *);
2927 }
2928 else {
2929 value_ptr = &d->ma_keys->dk_entries[i].me_value;
2930 offset = sizeof(PyDictKeyEntry);
2931 }
2932 while (i <= mask && *value_ptr == NULL) {
2933 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002934 i++;
2935 if (i > mask)
2936 goto fail;
2937 }
2938 di->di_pos = i+1;
2939 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002940 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002941 Py_INCREF(value);
2942 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002943
2944fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002945 Py_DECREF(d);
2946 di->di_dict = NULL;
2947 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002948}
2949
2950PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002951 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2952 "dict_valueiterator", /* tp_name */
2953 sizeof(dictiterobject), /* tp_basicsize */
2954 0, /* tp_itemsize */
2955 /* methods */
2956 (destructor)dictiter_dealloc, /* tp_dealloc */
2957 0, /* tp_print */
2958 0, /* tp_getattr */
2959 0, /* tp_setattr */
2960 0, /* tp_reserved */
2961 0, /* tp_repr */
2962 0, /* tp_as_number */
2963 0, /* tp_as_sequence */
2964 0, /* tp_as_mapping */
2965 0, /* tp_hash */
2966 0, /* tp_call */
2967 0, /* tp_str */
2968 PyObject_GenericGetAttr, /* tp_getattro */
2969 0, /* tp_setattro */
2970 0, /* tp_as_buffer */
2971 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2972 0, /* tp_doc */
2973 (traverseproc)dictiter_traverse, /* tp_traverse */
2974 0, /* tp_clear */
2975 0, /* tp_richcompare */
2976 0, /* tp_weaklistoffset */
2977 PyObject_SelfIter, /* tp_iter */
2978 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
2979 dictiter_methods, /* tp_methods */
2980 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002981};
2982
2983static PyObject *dictiter_iternextitem(dictiterobject *di)
2984{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002985 PyObject *key, *value, *result = di->di_result;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002986 register Py_ssize_t i, mask, offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002987 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002988 PyObject **value_ptr;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002990 if (d == NULL)
2991 return NULL;
2992 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00002993
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002994 if (di->di_used != d->ma_used) {
2995 PyErr_SetString(PyExc_RuntimeError,
2996 "dictionary changed size during iteration");
2997 di->di_used = -1; /* Make this state sticky */
2998 return NULL;
2999 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003001 i = di->di_pos;
3002 if (i < 0)
3003 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003004 mask = DK_SIZE(d->ma_keys)-1;
3005 if (d->ma_values) {
3006 value_ptr = &d->ma_values[i];
3007 offset = sizeof(PyObject *);
3008 }
3009 else {
3010 value_ptr = &d->ma_keys->dk_entries[i].me_value;
3011 offset = sizeof(PyDictKeyEntry);
3012 }
3013 while (i <= mask && *value_ptr == NULL) {
3014 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003015 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003016 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003017 di->di_pos = i+1;
3018 if (i > mask)
3019 goto fail;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003020
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003021 if (result->ob_refcnt == 1) {
3022 Py_INCREF(result);
3023 Py_DECREF(PyTuple_GET_ITEM(result, 0));
3024 Py_DECREF(PyTuple_GET_ITEM(result, 1));
3025 } else {
3026 result = PyTuple_New(2);
3027 if (result == NULL)
3028 return NULL;
3029 }
3030 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003031 key = d->ma_keys->dk_entries[i].me_key;
3032 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003033 Py_INCREF(key);
3034 Py_INCREF(value);
3035 PyTuple_SET_ITEM(result, 0, key);
3036 PyTuple_SET_ITEM(result, 1, value);
3037 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003038
3039fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003040 Py_DECREF(d);
3041 di->di_dict = NULL;
3042 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003043}
3044
3045PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003046 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3047 "dict_itemiterator", /* tp_name */
3048 sizeof(dictiterobject), /* tp_basicsize */
3049 0, /* tp_itemsize */
3050 /* methods */
3051 (destructor)dictiter_dealloc, /* tp_dealloc */
3052 0, /* tp_print */
3053 0, /* tp_getattr */
3054 0, /* tp_setattr */
3055 0, /* tp_reserved */
3056 0, /* tp_repr */
3057 0, /* tp_as_number */
3058 0, /* tp_as_sequence */
3059 0, /* tp_as_mapping */
3060 0, /* tp_hash */
3061 0, /* tp_call */
3062 0, /* tp_str */
3063 PyObject_GenericGetAttr, /* tp_getattro */
3064 0, /* tp_setattro */
3065 0, /* tp_as_buffer */
3066 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3067 0, /* tp_doc */
3068 (traverseproc)dictiter_traverse, /* tp_traverse */
3069 0, /* tp_clear */
3070 0, /* tp_richcompare */
3071 0, /* tp_weaklistoffset */
3072 PyObject_SelfIter, /* tp_iter */
3073 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3074 dictiter_methods, /* tp_methods */
3075 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003076};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003077
3078
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003079static PyObject *
3080dictiter_reduce(dictiterobject *di)
3081{
3082 PyObject *list;
3083 dictiterobject tmp;
3084
3085 list = PyList_New(0);
3086 if (!list)
3087 return NULL;
3088
3089 /* copy the itertor state */
3090 tmp = *di;
3091 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003092
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003093 /* iterate the temporary into a list */
3094 for(;;) {
3095 PyObject *element = 0;
3096 if (Py_TYPE(di) == &PyDictIterItem_Type)
3097 element = dictiter_iternextitem(&tmp);
3098 else if (Py_TYPE(di) == &PyDictIterKey_Type)
3099 element = dictiter_iternextkey(&tmp);
3100 else if (Py_TYPE(di) == &PyDictIterValue_Type)
3101 element = dictiter_iternextvalue(&tmp);
3102 else
3103 assert(0);
3104 if (element) {
3105 if (PyList_Append(list, element)) {
3106 Py_DECREF(element);
3107 Py_DECREF(list);
3108 Py_XDECREF(tmp.di_dict);
3109 return NULL;
3110 }
3111 Py_DECREF(element);
3112 } else
3113 break;
3114 }
3115 Py_XDECREF(tmp.di_dict);
3116 /* check for error */
3117 if (tmp.di_dict != NULL) {
3118 /* we have an error */
3119 Py_DECREF(list);
3120 return NULL;
3121 }
Antoine Pitroua7013882012-04-05 00:04:20 +02003122 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003123}
3124
Guido van Rossum3ac67412007-02-10 18:55:06 +00003125/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003126/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003127/***********************************************/
3128
Guido van Rossumb90c8482007-02-10 01:11:45 +00003129/* The instance lay-out is the same for all three; but the type differs. */
3130
3131typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003132 PyObject_HEAD
3133 PyDictObject *dv_dict;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003134} dictviewobject;
3135
3136
3137static void
Guido van Rossum3ac67412007-02-10 18:55:06 +00003138dictview_dealloc(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003139{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003140 Py_XDECREF(dv->dv_dict);
3141 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003142}
3143
3144static int
3145dictview_traverse(dictviewobject *dv, visitproc visit, void *arg)
3146{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003147 Py_VISIT(dv->dv_dict);
3148 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003149}
3150
Guido van Rossum83825ac2007-02-10 04:54:19 +00003151static Py_ssize_t
Guido van Rossum3ac67412007-02-10 18:55:06 +00003152dictview_len(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003153{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003154 Py_ssize_t len = 0;
3155 if (dv->dv_dict != NULL)
3156 len = dv->dv_dict->ma_used;
3157 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003158}
3159
3160static PyObject *
3161dictview_new(PyObject *dict, PyTypeObject *type)
3162{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003163 dictviewobject *dv;
3164 if (dict == NULL) {
3165 PyErr_BadInternalCall();
3166 return NULL;
3167 }
3168 if (!PyDict_Check(dict)) {
3169 /* XXX Get rid of this restriction later */
3170 PyErr_Format(PyExc_TypeError,
3171 "%s() requires a dict argument, not '%s'",
3172 type->tp_name, dict->ob_type->tp_name);
3173 return NULL;
3174 }
3175 dv = PyObject_GC_New(dictviewobject, type);
3176 if (dv == NULL)
3177 return NULL;
3178 Py_INCREF(dict);
3179 dv->dv_dict = (PyDictObject *)dict;
3180 _PyObject_GC_TRACK(dv);
3181 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003182}
3183
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003184/* TODO(guido): The views objects are not complete:
3185
3186 * support more set operations
3187 * support arbitrary mappings?
3188 - either these should be static or exported in dictobject.h
3189 - if public then they should probably be in builtins
3190*/
3191
Guido van Rossumaac530c2007-08-24 22:33:45 +00003192/* Return 1 if self is a subset of other, iterating over self;
3193 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003194static int
3195all_contained_in(PyObject *self, PyObject *other)
3196{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003197 PyObject *iter = PyObject_GetIter(self);
3198 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003200 if (iter == NULL)
3201 return -1;
3202 for (;;) {
3203 PyObject *next = PyIter_Next(iter);
3204 if (next == NULL) {
3205 if (PyErr_Occurred())
3206 ok = -1;
3207 break;
3208 }
3209 ok = PySequence_Contains(other, next);
3210 Py_DECREF(next);
3211 if (ok <= 0)
3212 break;
3213 }
3214 Py_DECREF(iter);
3215 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003216}
3217
3218static PyObject *
3219dictview_richcompare(PyObject *self, PyObject *other, int op)
3220{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003221 Py_ssize_t len_self, len_other;
3222 int ok;
3223 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003225 assert(self != NULL);
3226 assert(PyDictViewSet_Check(self));
3227 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00003228
Brian Curtindfc80e32011-08-10 20:28:54 -05003229 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
3230 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003232 len_self = PyObject_Size(self);
3233 if (len_self < 0)
3234 return NULL;
3235 len_other = PyObject_Size(other);
3236 if (len_other < 0)
3237 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003239 ok = 0;
3240 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00003241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003242 case Py_NE:
3243 case Py_EQ:
3244 if (len_self == len_other)
3245 ok = all_contained_in(self, other);
3246 if (op == Py_NE && ok >= 0)
3247 ok = !ok;
3248 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003250 case Py_LT:
3251 if (len_self < len_other)
3252 ok = all_contained_in(self, other);
3253 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003255 case Py_LE:
3256 if (len_self <= len_other)
3257 ok = all_contained_in(self, other);
3258 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003260 case Py_GT:
3261 if (len_self > len_other)
3262 ok = all_contained_in(other, self);
3263 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003265 case Py_GE:
3266 if (len_self >= len_other)
3267 ok = all_contained_in(other, self);
3268 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003270 }
3271 if (ok < 0)
3272 return NULL;
3273 result = ok ? Py_True : Py_False;
3274 Py_INCREF(result);
3275 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003276}
3277
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003278static PyObject *
3279dictview_repr(dictviewobject *dv)
3280{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003281 PyObject *seq;
3282 PyObject *result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003284 seq = PySequence_List((PyObject *)dv);
3285 if (seq == NULL)
3286 return NULL;
3287
3288 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
3289 Py_DECREF(seq);
3290 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003291}
3292
Guido van Rossum3ac67412007-02-10 18:55:06 +00003293/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003294
3295static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003296dictkeys_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003297{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003298 if (dv->dv_dict == NULL) {
3299 Py_RETURN_NONE;
3300 }
3301 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003302}
3303
3304static int
3305dictkeys_contains(dictviewobject *dv, PyObject *obj)
3306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003307 if (dv->dv_dict == NULL)
3308 return 0;
3309 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003310}
3311
Guido van Rossum83825ac2007-02-10 04:54:19 +00003312static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003313 (lenfunc)dictview_len, /* sq_length */
3314 0, /* sq_concat */
3315 0, /* sq_repeat */
3316 0, /* sq_item */
3317 0, /* sq_slice */
3318 0, /* sq_ass_item */
3319 0, /* sq_ass_slice */
3320 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003321};
3322
Guido van Rossum523259b2007-08-24 23:41:22 +00003323static PyObject*
3324dictviews_sub(PyObject* self, PyObject *other)
3325{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003326 PyObject *result = PySet_New(self);
3327 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003328 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003330 if (result == NULL)
3331 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003332
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003333 tmp = _PyObject_CallMethodId(result, &PyId_difference_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003334 if (tmp == NULL) {
3335 Py_DECREF(result);
3336 return NULL;
3337 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003339 Py_DECREF(tmp);
3340 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003341}
3342
3343static PyObject*
3344dictviews_and(PyObject* self, PyObject *other)
3345{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003346 PyObject *result = PySet_New(self);
3347 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003348 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003350 if (result == NULL)
3351 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003352
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003353 tmp = _PyObject_CallMethodId(result, &PyId_intersection_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003354 if (tmp == NULL) {
3355 Py_DECREF(result);
3356 return NULL;
3357 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003359 Py_DECREF(tmp);
3360 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003361}
3362
3363static PyObject*
3364dictviews_or(PyObject* self, PyObject *other)
3365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003366 PyObject *result = PySet_New(self);
3367 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003368 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003370 if (result == NULL)
3371 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003372
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003373 tmp = _PyObject_CallMethodId(result, &PyId_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003374 if (tmp == NULL) {
3375 Py_DECREF(result);
3376 return NULL;
3377 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003379 Py_DECREF(tmp);
3380 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003381}
3382
3383static PyObject*
3384dictviews_xor(PyObject* self, PyObject *other)
3385{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003386 PyObject *result = PySet_New(self);
3387 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003388 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003390 if (result == NULL)
3391 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003392
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003393 tmp = _PyObject_CallMethodId(result, &PyId_symmetric_difference_update, "O",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003394 other);
3395 if (tmp == NULL) {
3396 Py_DECREF(result);
3397 return NULL;
3398 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003400 Py_DECREF(tmp);
3401 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003402}
3403
3404static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003405 0, /*nb_add*/
3406 (binaryfunc)dictviews_sub, /*nb_subtract*/
3407 0, /*nb_multiply*/
3408 0, /*nb_remainder*/
3409 0, /*nb_divmod*/
3410 0, /*nb_power*/
3411 0, /*nb_negative*/
3412 0, /*nb_positive*/
3413 0, /*nb_absolute*/
3414 0, /*nb_bool*/
3415 0, /*nb_invert*/
3416 0, /*nb_lshift*/
3417 0, /*nb_rshift*/
3418 (binaryfunc)dictviews_and, /*nb_and*/
3419 (binaryfunc)dictviews_xor, /*nb_xor*/
3420 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00003421};
3422
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003423static PyObject*
3424dictviews_isdisjoint(PyObject *self, PyObject *other)
3425{
3426 PyObject *it;
3427 PyObject *item = NULL;
3428
3429 if (self == other) {
3430 if (dictview_len((dictviewobject *)self) == 0)
3431 Py_RETURN_TRUE;
3432 else
3433 Py_RETURN_FALSE;
3434 }
3435
3436 /* Iterate over the shorter object (only if other is a set,
3437 * because PySequence_Contains may be expensive otherwise): */
3438 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
3439 Py_ssize_t len_self = dictview_len((dictviewobject *)self);
3440 Py_ssize_t len_other = PyObject_Size(other);
3441 if (len_other == -1)
3442 return NULL;
3443
3444 if ((len_other > len_self)) {
3445 PyObject *tmp = other;
3446 other = self;
3447 self = tmp;
3448 }
3449 }
3450
3451 it = PyObject_GetIter(other);
3452 if (it == NULL)
3453 return NULL;
3454
3455 while ((item = PyIter_Next(it)) != NULL) {
3456 int contains = PySequence_Contains(self, item);
3457 Py_DECREF(item);
3458 if (contains == -1) {
3459 Py_DECREF(it);
3460 return NULL;
3461 }
3462
3463 if (contains) {
3464 Py_DECREF(it);
3465 Py_RETURN_FALSE;
3466 }
3467 }
3468 Py_DECREF(it);
3469 if (PyErr_Occurred())
3470 return NULL; /* PyIter_Next raised an exception. */
3471 Py_RETURN_TRUE;
3472}
3473
3474PyDoc_STRVAR(isdisjoint_doc,
3475"Return True if the view and the given iterable have a null intersection.");
3476
Guido van Rossumb90c8482007-02-10 01:11:45 +00003477static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003478 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3479 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003480 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003481};
3482
3483PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003484 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3485 "dict_keys", /* tp_name */
3486 sizeof(dictviewobject), /* tp_basicsize */
3487 0, /* tp_itemsize */
3488 /* methods */
3489 (destructor)dictview_dealloc, /* tp_dealloc */
3490 0, /* tp_print */
3491 0, /* tp_getattr */
3492 0, /* tp_setattr */
3493 0, /* tp_reserved */
3494 (reprfunc)dictview_repr, /* tp_repr */
3495 &dictviews_as_number, /* tp_as_number */
3496 &dictkeys_as_sequence, /* tp_as_sequence */
3497 0, /* tp_as_mapping */
3498 0, /* tp_hash */
3499 0, /* tp_call */
3500 0, /* tp_str */
3501 PyObject_GenericGetAttr, /* tp_getattro */
3502 0, /* tp_setattro */
3503 0, /* tp_as_buffer */
3504 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3505 0, /* tp_doc */
3506 (traverseproc)dictview_traverse, /* tp_traverse */
3507 0, /* tp_clear */
3508 dictview_richcompare, /* tp_richcompare */
3509 0, /* tp_weaklistoffset */
3510 (getiterfunc)dictkeys_iter, /* tp_iter */
3511 0, /* tp_iternext */
3512 dictkeys_methods, /* tp_methods */
3513 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003514};
3515
3516static PyObject *
3517dictkeys_new(PyObject *dict)
3518{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003519 return dictview_new(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003520}
3521
Guido van Rossum3ac67412007-02-10 18:55:06 +00003522/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003523
3524static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003525dictitems_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003526{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003527 if (dv->dv_dict == NULL) {
3528 Py_RETURN_NONE;
3529 }
3530 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003531}
3532
3533static int
3534dictitems_contains(dictviewobject *dv, PyObject *obj)
3535{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003536 PyObject *key, *value, *found;
3537 if (dv->dv_dict == NULL)
3538 return 0;
3539 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
3540 return 0;
3541 key = PyTuple_GET_ITEM(obj, 0);
3542 value = PyTuple_GET_ITEM(obj, 1);
3543 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
3544 if (found == NULL) {
3545 if (PyErr_Occurred())
3546 return -1;
3547 return 0;
3548 }
3549 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003550}
3551
Guido van Rossum83825ac2007-02-10 04:54:19 +00003552static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003553 (lenfunc)dictview_len, /* sq_length */
3554 0, /* sq_concat */
3555 0, /* sq_repeat */
3556 0, /* sq_item */
3557 0, /* sq_slice */
3558 0, /* sq_ass_item */
3559 0, /* sq_ass_slice */
3560 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003561};
3562
Guido van Rossumb90c8482007-02-10 01:11:45 +00003563static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003564 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3565 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003566 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003567};
3568
3569PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003570 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3571 "dict_items", /* tp_name */
3572 sizeof(dictviewobject), /* tp_basicsize */
3573 0, /* tp_itemsize */
3574 /* methods */
3575 (destructor)dictview_dealloc, /* tp_dealloc */
3576 0, /* tp_print */
3577 0, /* tp_getattr */
3578 0, /* tp_setattr */
3579 0, /* tp_reserved */
3580 (reprfunc)dictview_repr, /* tp_repr */
3581 &dictviews_as_number, /* tp_as_number */
3582 &dictitems_as_sequence, /* tp_as_sequence */
3583 0, /* tp_as_mapping */
3584 0, /* tp_hash */
3585 0, /* tp_call */
3586 0, /* tp_str */
3587 PyObject_GenericGetAttr, /* tp_getattro */
3588 0, /* tp_setattro */
3589 0, /* tp_as_buffer */
3590 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3591 0, /* tp_doc */
3592 (traverseproc)dictview_traverse, /* tp_traverse */
3593 0, /* tp_clear */
3594 dictview_richcompare, /* tp_richcompare */
3595 0, /* tp_weaklistoffset */
3596 (getiterfunc)dictitems_iter, /* tp_iter */
3597 0, /* tp_iternext */
3598 dictitems_methods, /* tp_methods */
3599 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003600};
3601
3602static PyObject *
3603dictitems_new(PyObject *dict)
3604{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003605 return dictview_new(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003606}
3607
Guido van Rossum3ac67412007-02-10 18:55:06 +00003608/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003609
3610static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003611dictvalues_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003612{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003613 if (dv->dv_dict == NULL) {
3614 Py_RETURN_NONE;
3615 }
3616 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003617}
3618
Guido van Rossum83825ac2007-02-10 04:54:19 +00003619static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003620 (lenfunc)dictview_len, /* sq_length */
3621 0, /* sq_concat */
3622 0, /* sq_repeat */
3623 0, /* sq_item */
3624 0, /* sq_slice */
3625 0, /* sq_ass_item */
3626 0, /* sq_ass_slice */
3627 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003628};
3629
Guido van Rossumb90c8482007-02-10 01:11:45 +00003630static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003631 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003632};
3633
3634PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003635 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3636 "dict_values", /* tp_name */
3637 sizeof(dictviewobject), /* tp_basicsize */
3638 0, /* tp_itemsize */
3639 /* methods */
3640 (destructor)dictview_dealloc, /* tp_dealloc */
3641 0, /* tp_print */
3642 0, /* tp_getattr */
3643 0, /* tp_setattr */
3644 0, /* tp_reserved */
3645 (reprfunc)dictview_repr, /* tp_repr */
3646 0, /* tp_as_number */
3647 &dictvalues_as_sequence, /* tp_as_sequence */
3648 0, /* tp_as_mapping */
3649 0, /* tp_hash */
3650 0, /* tp_call */
3651 0, /* tp_str */
3652 PyObject_GenericGetAttr, /* tp_getattro */
3653 0, /* tp_setattro */
3654 0, /* tp_as_buffer */
3655 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3656 0, /* tp_doc */
3657 (traverseproc)dictview_traverse, /* tp_traverse */
3658 0, /* tp_clear */
3659 0, /* tp_richcompare */
3660 0, /* tp_weaklistoffset */
3661 (getiterfunc)dictvalues_iter, /* tp_iter */
3662 0, /* tp_iternext */
3663 dictvalues_methods, /* tp_methods */
3664 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003665};
3666
3667static PyObject *
3668dictvalues_new(PyObject *dict)
3669{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003670 return dictview_new(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003671}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003672
3673/* Returns NULL if cannot allocate a new PyDictKeysObject,
3674 but does not set an error */
3675PyDictKeysObject *
3676_PyDict_NewKeysForClass(void)
3677{
3678 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE_SPLIT);
3679 if (keys == NULL)
3680 PyErr_Clear();
3681 else
3682 keys->dk_lookup = lookdict_split;
3683 return keys;
3684}
3685
3686#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
3687
3688PyObject *
3689PyObject_GenericGetDict(PyObject *obj, void *context)
3690{
3691 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
3692 if (dictptr == NULL) {
3693 PyErr_SetString(PyExc_AttributeError,
3694 "This object has no __dict__");
3695 return NULL;
3696 }
3697 dict = *dictptr;
3698 if (dict == NULL) {
3699 PyTypeObject *tp = Py_TYPE(obj);
3700 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
3701 DK_INCREF(CACHED_KEYS(tp));
3702 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
3703 }
3704 else {
3705 *dictptr = dict = PyDict_New();
3706 }
3707 }
3708 Py_XINCREF(dict);
3709 return dict;
3710}
3711
3712int
3713_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
3714 PyObject *key, PyObject *value)
3715{
3716 PyObject *dict;
3717 int res;
3718 PyDictKeysObject *cached;
3719
3720 assert(dictptr != NULL);
3721 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
3722 assert(dictptr != NULL);
3723 dict = *dictptr;
3724 if (dict == NULL) {
3725 DK_INCREF(cached);
3726 dict = new_dict_with_shared_keys(cached);
3727 if (dict == NULL)
3728 return -1;
3729 *dictptr = dict;
3730 }
3731 if (value == NULL) {
3732 res = PyDict_DelItem(dict, key);
3733 if (cached != ((PyDictObject *)dict)->ma_keys) {
3734 CACHED_KEYS(tp) = NULL;
3735 DK_DECREF(cached);
3736 }
3737 } else {
3738 res = PyDict_SetItem(dict, key, value);
3739 if (cached != ((PyDictObject *)dict)->ma_keys) {
3740 /* Either update tp->ht_cached_keys or delete it */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04003741 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003742 CACHED_KEYS(tp) = make_keys_shared(dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003743 } else {
3744 CACHED_KEYS(tp) = NULL;
3745 }
3746 DK_DECREF(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04003747 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
3748 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003749 }
3750 }
3751 } else {
3752 dict = *dictptr;
3753 if (dict == NULL) {
3754 dict = PyDict_New();
3755 if (dict == NULL)
3756 return -1;
3757 *dictptr = dict;
3758 }
3759 if (value == NULL) {
3760 res = PyDict_DelItem(dict, key);
3761 } else {
3762 res = PyDict_SetItem(dict, key, value);
3763 }
3764 }
3765 return res;
3766}
3767
3768void
3769_PyDictKeys_DecRef(PyDictKeysObject *keys)
3770{
3771 DK_DECREF(keys);
3772}
3773
3774
3775/* ARGSUSED */
3776static PyObject *
3777dummy_repr(PyObject *op)
3778{
3779 return PyUnicode_FromString("<dummy key>");
3780}
3781
3782/* ARGUSED */
3783static void
3784dummy_dealloc(PyObject* ignore)
3785{
3786 /* This should never get called, but we also don't want to SEGV if
3787 * we accidentally decref dummy-key out of existence.
3788 */
3789 Py_FatalError("deallocating <dummy key>");
3790}
3791
3792static PyTypeObject PyDictDummy_Type = {
3793 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3794 "<dummy key> type",
3795 0,
3796 0,
3797 dummy_dealloc, /*tp_dealloc*/ /*never called*/
3798 0, /*tp_print*/
3799 0, /*tp_getattr*/
3800 0, /*tp_setattr*/
3801 0, /*tp_reserved*/
3802 dummy_repr, /*tp_repr*/
3803 0, /*tp_as_number*/
3804 0, /*tp_as_sequence*/
3805 0, /*tp_as_mapping*/
3806 0, /*tp_hash */
3807 0, /*tp_call */
3808 0, /*tp_str */
3809 0, /*tp_getattro */
3810 0, /*tp_setattro */
3811 0, /*tp_as_buffer */
3812 Py_TPFLAGS_DEFAULT, /*tp_flags */
3813};
3814
3815static PyObject _dummy_struct = {
3816 _PyObject_EXTRA_INIT
3817 2, &PyDictDummy_Type
3818};
3819