blob: 2673817b561b2eb91afb1bf53a2574e57ca89f4b [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
Larry Hastings61272b72014-01-07 12:41:53 -080072/*[clinic input]
Larry Hastingsc2047262014-01-25 20:43:29 -080073class dict "PyDictObject *" "&PyDict_Type"
Larry Hastings61272b72014-01-07 12:41:53 -080074[clinic start generated code]*/
Larry Hastings581ee362014-01-28 05:00:08 -080075/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f157a5a0ce9589d6]*/
Larry Hastings44e2eaa2013-11-23 15:37:55 -080076
Benjamin Peterson7d95e402012-04-23 11:24:50 -040077typedef struct {
78 /* Cached hash code of me_key. */
79 Py_hash_t me_hash;
80 PyObject *me_key;
81 PyObject *me_value; /* This field is only meaningful for combined tables */
82} PyDictKeyEntry;
83
84typedef PyDictKeyEntry *(*dict_lookup_func)
85(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject ***value_addr);
86
87struct _dictkeysobject {
88 Py_ssize_t dk_refcnt;
89 Py_ssize_t dk_size;
90 dict_lookup_func dk_lookup;
91 Py_ssize_t dk_usable;
92 PyDictKeyEntry dk_entries[1];
93};
94
95
96/*
97To ensure the lookup algorithm terminates, there must be at least one Unused
98slot (NULL key) in the table.
99To avoid slowing down lookups on a near-full table, we resize the table when
100it's USABLE_FRACTION (currently two-thirds) full.
101*/
Guido van Rossum16e93a81997-01-28 00:00:11 +0000102
Tim Peterseb28ef22001-06-02 05:27:19 +0000103#define PERTURB_SHIFT 5
104
Guido van Rossum16e93a81997-01-28 00:00:11 +0000105/*
Tim Peterseb28ef22001-06-02 05:27:19 +0000106Major subtleties ahead: Most hash schemes depend on having a "good" hash
107function, in the sense of simulating randomness. Python doesn't: its most
108important hash functions (for strings and ints) are very regular in common
109cases:
Tim Peters15d49292001-05-27 07:39:22 +0000110
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000111 >>> map(hash, (0, 1, 2, 3))
112 [0, 1, 2, 3]
113 >>> map(hash, ("namea", "nameb", "namec", "named"))
114 [-1658398457, -1658398460, -1658398459, -1658398462]
115 >>>
Tim Peters15d49292001-05-27 07:39:22 +0000116
Tim Peterseb28ef22001-06-02 05:27:19 +0000117This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
118the low-order i bits as the initial table index is extremely fast, and there
119are no collisions at all for dicts indexed by a contiguous range of ints.
120The same is approximately true when keys are "consecutive" strings. So this
121gives better-than-random behavior in common cases, and that's very desirable.
Tim Peters15d49292001-05-27 07:39:22 +0000122
Tim Peterseb28ef22001-06-02 05:27:19 +0000123OTOH, when collisions occur, the tendency to fill contiguous slices of the
124hash table makes a good collision resolution strategy crucial. Taking only
125the last i bits of the hash code is also vulnerable: for example, consider
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126the list [i << 16 for i in range(20000)] as a set of keys. Since ints are
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000127their own hash codes, and this fits in a dict of size 2**15, the last 15 bits
128 of every hash code are all 0: they *all* map to the same table index.
Tim Peters15d49292001-05-27 07:39:22 +0000129
Tim Peterseb28ef22001-06-02 05:27:19 +0000130But catering to unusual cases should not slow the usual ones, so we just take
131the last i bits anyway. It's up to collision resolution to do the rest. If
132we *usually* find the key we're looking for on the first try (and, it turns
133out, we usually do -- the table load factor is kept under 2/3, so the odds
134are solidly in our favor), then it makes best sense to keep the initial index
135computation dirt cheap.
Tim Peters15d49292001-05-27 07:39:22 +0000136
Tim Peterseb28ef22001-06-02 05:27:19 +0000137The first half of collision resolution is to visit table indices via this
138recurrence:
Tim Peters15d49292001-05-27 07:39:22 +0000139
Tim Peterseb28ef22001-06-02 05:27:19 +0000140 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +0000141
Tim Peterseb28ef22001-06-02 05:27:19 +0000142For any initial j in range(2**i), repeating that 2**i times generates each
143int in range(2**i) exactly once (see any text on random-number generation for
144proof). By itself, this doesn't help much: like linear probing (setting
145j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
146order. This would be bad, except that's not the only thing we do, and it's
147actually *good* in the common cases where hash keys are consecutive. In an
148example that's really too small to make this entirely clear, for a table of
149size 2**3 the order of indices is:
Tim Peters15d49292001-05-27 07:39:22 +0000150
Tim Peterseb28ef22001-06-02 05:27:19 +0000151 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
152
153If two things come in at index 5, the first place we look after is index 2,
154not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
155Linear probing is deadly in this case because there the fixed probe order
156is the *same* as the order consecutive keys are likely to arrive. But it's
157extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
158and certain that consecutive hash codes do not.
159
160The other half of the strategy is to get the other bits of the hash code
161into play. This is done by initializing a (unsigned) vrbl "perturb" to the
162full hash code, and changing the recurrence to:
163
164 j = (5*j) + 1 + perturb;
165 perturb >>= PERTURB_SHIFT;
166 use j % 2**i as the next table index;
167
168Now the probe sequence depends (eventually) on every bit in the hash code,
169and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
170because it quickly magnifies small differences in the bits that didn't affect
171the initial index. Note that because perturb is unsigned, if the recurrence
172is executed often enough perturb eventually becomes and remains 0. At that
173point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
174that's certain to find an empty slot eventually (since it generates every int
175in range(2**i), and we make sure there's always at least one empty slot).
176
177Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
178small so that the high bits of the hash code continue to affect the probe
179sequence across iterations; but you want it large so that in really bad cases
180the high-order hash bits have an effect on early iterations. 5 was "the
181best" in minimizing total collisions across experiments Tim Peters ran (on
182both normal and pathological cases), but 4 and 6 weren't significantly worse.
183
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000184Historical: Reimer Behrends contributed the idea of using a polynomial-based
Tim Peterseb28ef22001-06-02 05:27:19 +0000185approach, using repeated multiplication by x in GF(2**n) where an irreducible
186polynomial for each table size was chosen such that x was a primitive root.
187Christian Tismer later extended that to use division by x instead, as an
188efficient way to get the high bits of the hash code into play. This scheme
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000189also gave excellent collision statistics, but was more expensive: two
190if-tests were required inside the loop; computing "the next" index took about
191the same number of operations but without as much potential parallelism
192(e.g., computing 5*j can go on at the same time as computing 1+perturb in the
193above, and then shifting perturb can be done while the table index is being
194masked); and the PyDictObject struct required a member to hold the table's
195polynomial. In Tim's experiments the current scheme ran faster, produced
196equally good collision statistics, needed less code & used less memory.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000197
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000198*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000199
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400200/* Object used as dummy key to fill deleted entries
201 * This could be any unique object,
202 * use a custom type in order to minimise coupling.
203*/
204static PyObject _dummy_struct;
205
206#define dummy (&_dummy_struct)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000207
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000208#ifdef Py_REF_DEBUG
209PyObject *
210_PyDict_Dummy(void)
211{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 return dummy;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000213}
214#endif
215
Fred Drake1bff34a2000-08-31 19:31:38 +0000216/* forward declarations */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400217static PyDictKeyEntry *lookdict(PyDictObject *mp, PyObject *key,
218 Py_hash_t hash, PyObject ***value_addr);
219static PyDictKeyEntry *lookdict_unicode(PyDictObject *mp, PyObject *key,
220 Py_hash_t hash, PyObject ***value_addr);
221static PyDictKeyEntry *
222lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
223 Py_hash_t hash, PyObject ***value_addr);
224static PyDictKeyEntry *lookdict_split(PyDictObject *mp, PyObject *key,
225 Py_hash_t hash, PyObject ***value_addr);
Fred Drake1bff34a2000-08-31 19:31:38 +0000226
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400227static int dictresize(PyDictObject *mp, Py_ssize_t minused);
Tim Petersdea48ec2001-05-22 20:40:22 +0000228
Raymond Hettinger43442782004-03-17 21:55:03 +0000229/* Dictionary reuse scheme to save calls to malloc, free, and memset */
Christian Heimes2202f872008-02-06 14:31:34 +0000230#ifndef PyDict_MAXFREELIST
231#define PyDict_MAXFREELIST 80
232#endif
233static PyDictObject *free_list[PyDict_MAXFREELIST];
234static int numfree = 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000235
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100236int
237PyDict_ClearFreeList(void)
Christian Heimes77c02eb2008-02-09 02:18:51 +0000238{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 PyDictObject *op;
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100240 int ret = numfree;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 while (numfree) {
242 op = free_list[--numfree];
243 assert(PyDict_CheckExact(op));
244 PyObject_GC_Del(op);
245 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100246 return ret;
247}
248
David Malcolm49526f42012-06-22 14:55:41 -0400249/* Print summary info about the state of the optimized allocator */
250void
251_PyDict_DebugMallocStats(FILE *out)
252{
253 _PyDebugAllocatorStats(out,
254 "free PyDictObject", numfree, sizeof(PyDictObject));
255}
256
257
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100258void
259PyDict_Fini(void)
260{
261 PyDict_ClearFreeList();
Christian Heimes77c02eb2008-02-09 02:18:51 +0000262}
263
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200264#define DK_DEBUG_INCREF _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA
265#define DK_DEBUG_DECREF _Py_DEC_REFTOTAL _Py_REF_DEBUG_COMMA
266
267#define DK_INCREF(dk) (DK_DEBUG_INCREF ++(dk)->dk_refcnt)
268#define DK_DECREF(dk) if (DK_DEBUG_DECREF (--(dk)->dk_refcnt) == 0) free_keys_object(dk)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400269#define DK_SIZE(dk) ((dk)->dk_size)
270#define DK_MASK(dk) (((dk)->dk_size)-1)
271#define IS_POWER_OF_2(x) (((x) & (x-1)) == 0)
272
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200273/* USABLE_FRACTION is the maximum dictionary load.
274 * Currently set to (2n+1)/3. Increasing this ratio makes dictionaries more
275 * dense resulting in more collisions. Decreasing it improves sparseness
276 * at the expense of spreading entries over more cache lines and at the
277 * cost of total memory consumed.
278 *
279 * USABLE_FRACTION must obey the following:
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400280 * (0 < USABLE_FRACTION(n) < n) for all n >= 2
281 *
282 * USABLE_FRACTION should be very quick to calculate.
283 * Fractions around 5/8 to 2/3 seem to work well in practice.
284 */
285
286/* Use (2n+1)/3 rather than 2n+3 because: it makes no difference for
287 * combined tables (the two fractions round to the same number n < ),
288 * but 2*4/3 is 2 whereas (2*4+1)/3 is 3 which potentially saves quite
289 * a lot of space for small, split tables */
290#define USABLE_FRACTION(n) ((((n) << 1)+1)/3)
291
292/* Alternative fraction that is otherwise close enough to (2n+1)/3 to make
293 * little difference. 8 * 2/3 == 8 * 5/8 == 5. 16 * 2/3 == 16 * 5/8 == 10.
294 * 32 * 2/3 = 21, 32 * 5/8 = 20.
295 * Its advantage is that it is faster to compute on machines with slow division.
296 * #define USABLE_FRACTION(n) (((n) >> 1) + ((n) >> 2) - ((n) >> 3))
297*/
298
Victor Stinnera9f61a52013-07-16 22:17:26 +0200299/* GROWTH_RATE. Growth rate upon hitting maximum load.
300 * Currently set to used*2 + capacity/2.
301 * This means that dicts double in size when growing without deletions,
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700302 * but have more head room when the number of deletions is on a par with the
303 * number of insertions.
304 * Raising this to used*4 doubles memory consumption depending on the size of
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200305 * the dictionary, but results in half the number of resizes, less effort to
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700306 * resize.
307 * GROWTH_RATE was set to used*4 up to version 3.2.
308 * GROWTH_RATE was set to used*2 in version 3.3.0
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200309 */
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700310#define GROWTH_RATE(d) (((d)->ma_used*2)+((d)->ma_keys->dk_size>>1))
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400311
312#define ENSURE_ALLOWS_DELETIONS(d) \
313 if ((d)->ma_keys->dk_lookup == lookdict_unicode_nodummy) { \
314 (d)->ma_keys->dk_lookup = lookdict_unicode; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400316
317/* This immutable, empty PyDictKeysObject is used for PyDict_Clear()
318 * (which cannot fail and thus can do no allocation).
319 */
320static PyDictKeysObject empty_keys_struct = {
321 2, /* dk_refcnt 1 for this struct, 1 for dummy_struct */
322 1, /* dk_size */
323 lookdict_split, /* dk_lookup */
324 0, /* dk_usable (immutable) */
325 {
326 { 0, 0, 0 } /* dk_entries (empty) */
327 }
328};
329
330static PyObject *empty_values[1] = { NULL };
331
332#define Py_EMPTY_KEYS &empty_keys_struct
333
334static PyDictKeysObject *new_keys_object(Py_ssize_t size)
335{
336 PyDictKeysObject *dk;
337 Py_ssize_t i;
338 PyDictKeyEntry *ep0;
339
340 assert(size >= PyDict_MINSIZE_SPLIT);
341 assert(IS_POWER_OF_2(size));
342 dk = PyMem_MALLOC(sizeof(PyDictKeysObject) +
343 sizeof(PyDictKeyEntry) * (size-1));
344 if (dk == NULL) {
345 PyErr_NoMemory();
346 return NULL;
347 }
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200348 DK_DEBUG_INCREF dk->dk_refcnt = 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400349 dk->dk_size = size;
350 dk->dk_usable = USABLE_FRACTION(size);
351 ep0 = &dk->dk_entries[0];
352 /* Hash value of slot 0 is used by popitem, so it must be initialized */
353 ep0->me_hash = 0;
354 for (i = 0; i < size; i++) {
355 ep0[i].me_key = NULL;
356 ep0[i].me_value = NULL;
357 }
358 dk->dk_lookup = lookdict_unicode_nodummy;
359 return dk;
360}
361
362static void
363free_keys_object(PyDictKeysObject *keys)
364{
365 PyDictKeyEntry *entries = &keys->dk_entries[0];
366 Py_ssize_t i, n;
367 for (i = 0, n = DK_SIZE(keys); i < n; i++) {
368 Py_XDECREF(entries[i].me_key);
369 Py_XDECREF(entries[i].me_value);
370 }
371 PyMem_FREE(keys);
372}
373
374#define new_values(size) PyMem_NEW(PyObject *, size)
375
376#define free_values(values) PyMem_FREE(values)
377
378/* Consumes a reference to the keys object */
379static PyObject *
380new_dict(PyDictKeysObject *keys, PyObject **values)
381{
382 PyDictObject *mp;
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200383 assert(keys != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 if (numfree) {
385 mp = free_list[--numfree];
386 assert (mp != NULL);
387 assert (Py_TYPE(mp) == &PyDict_Type);
388 _Py_NewReference((PyObject *)mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400390 else {
391 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
392 if (mp == NULL) {
393 DK_DECREF(keys);
394 free_values(values);
395 return NULL;
396 }
397 }
398 mp->ma_keys = keys;
399 mp->ma_values = values;
400 mp->ma_used = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000402}
403
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400404/* Consumes a reference to the keys object */
405static PyObject *
406new_dict_with_shared_keys(PyDictKeysObject *keys)
407{
408 PyObject **values;
409 Py_ssize_t i, size;
410
411 size = DK_SIZE(keys);
412 values = new_values(size);
413 if (values == NULL) {
414 DK_DECREF(keys);
415 return PyErr_NoMemory();
416 }
417 for (i = 0; i < size; i++) {
418 values[i] = NULL;
419 }
420 return new_dict(keys, values);
421}
422
423PyObject *
424PyDict_New(void)
425{
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200426 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE_COMBINED);
427 if (keys == NULL)
428 return NULL;
429 return new_dict(keys, NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400430}
431
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000432/*
433The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000434This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000435Open addressing is preferred over chaining since the link overhead for
436chaining would be substantial (100% with typical malloc overhead).
437
Tim Peterseb28ef22001-06-02 05:27:19 +0000438The initial probe index is computed as hash mod the table size. Subsequent
439probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000440
441All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000442
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000443The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000444contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000445Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000446
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000447lookdict() is general-purpose, and may return NULL if (and only if) a
448comparison raises an exception (this was new in Python 2.5).
Guido van Rossum89d8c602007-09-18 17:26:56 +0000449lookdict_unicode() below is specialized to string keys, comparison of which can
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400450never raise an exception; that function can never return NULL.
451lookdict_unicode_nodummy is further specialized for string keys that cannot be
452the <dummy> value.
453For both, when the key isn't found a PyDictEntry* is returned
454where the key would have been found, *value_addr points to the matching value
455slot.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000456*/
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400457static PyDictKeyEntry *
458lookdict(PyDictObject *mp, PyObject *key,
459 Py_hash_t hash, PyObject ***value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000460{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200461 size_t i;
462 size_t perturb;
463 PyDictKeyEntry *freeslot;
464 size_t mask;
Antoine Pitrou9a234902012-05-13 20:48:01 +0200465 PyDictKeyEntry *ep0;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200466 PyDictKeyEntry *ep;
467 int cmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000469
Antoine Pitrou9a234902012-05-13 20:48:01 +0200470top:
471 mask = DK_MASK(mp->ma_keys);
472 ep0 = &mp->ma_keys->dk_entries[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 i = (size_t)hash & mask;
474 ep = &ep0[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400475 if (ep->me_key == NULL || ep->me_key == key) {
476 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400478 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 if (ep->me_key == dummy)
480 freeslot = ep;
481 else {
482 if (ep->me_hash == hash) {
483 startkey = ep->me_key;
484 Py_INCREF(startkey);
485 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
486 Py_DECREF(startkey);
487 if (cmp < 0)
488 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400489 if (ep0 == mp->ma_keys->dk_entries && ep->me_key == startkey) {
490 if (cmp > 0) {
491 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400493 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 }
495 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200496 /* The dict was mutated, restart */
497 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 }
499 }
500 freeslot = NULL;
501 }
Tim Peters15d49292001-05-27 07:39:22 +0000502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 /* In the loop, me_key == dummy is by far (factor of 100s) the
504 least likely outcome, so test for that last. */
505 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
506 i = (i << 2) + i + perturb + 1;
507 ep = &ep0[i & mask];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400508 if (ep->me_key == NULL) {
509 if (freeslot == NULL) {
510 *value_addr = &ep->me_value;
511 return ep;
512 } else {
513 *value_addr = &freeslot->me_value;
514 return freeslot;
515 }
516 }
517 if (ep->me_key == key) {
518 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400520 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 if (ep->me_hash == hash && ep->me_key != dummy) {
522 startkey = ep->me_key;
523 Py_INCREF(startkey);
524 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
525 Py_DECREF(startkey);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400526 if (cmp < 0) {
527 *value_addr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400529 }
530 if (ep0 == mp->ma_keys->dk_entries && ep->me_key == startkey) {
531 if (cmp > 0) {
532 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400534 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 }
536 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200537 /* The dict was mutated, restart */
538 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 }
540 }
541 else if (ep->me_key == dummy && freeslot == NULL)
542 freeslot = ep;
543 }
544 assert(0); /* NOT REACHED */
545 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000546}
547
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400548/* Specialized version for string-only keys */
549static PyDictKeyEntry *
550lookdict_unicode(PyDictObject *mp, PyObject *key,
551 Py_hash_t hash, PyObject ***value_addr)
Fred Drake1bff34a2000-08-31 19:31:38 +0000552{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200553 size_t i;
554 size_t perturb;
555 PyDictKeyEntry *freeslot;
556 size_t mask = DK_MASK(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400557 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200558 PyDictKeyEntry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 /* Make sure this function doesn't have to handle non-unicode keys,
561 including subclasses of str; e.g., one reason to subclass
562 unicodes is to override __eq__, and for speed we don't cater to
563 that here. */
564 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400565 mp->ma_keys->dk_lookup = lookdict;
566 return lookdict(mp, key, hash, value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 }
Mark Dickinson57e683e2011-09-24 18:18:40 +0100568 i = (size_t)hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 ep = &ep0[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400570 if (ep->me_key == NULL || ep->me_key == key) {
571 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400573 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 if (ep->me_key == dummy)
575 freeslot = ep;
576 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400577 if (ep->me_hash == hash && unicode_eq(ep->me_key, key)) {
578 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400580 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 freeslot = NULL;
582 }
Tim Peters15d49292001-05-27 07:39:22 +0000583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 /* In the loop, me_key == dummy is by far (factor of 100s) the
585 least likely outcome, so test for that last. */
586 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
587 i = (i << 2) + i + perturb + 1;
588 ep = &ep0[i & mask];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400589 if (ep->me_key == NULL) {
590 if (freeslot == NULL) {
591 *value_addr = &ep->me_value;
592 return ep;
593 } else {
594 *value_addr = &freeslot->me_value;
595 return freeslot;
596 }
597 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 if (ep->me_key == key
599 || (ep->me_hash == hash
600 && ep->me_key != dummy
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400601 && unicode_eq(ep->me_key, key))) {
602 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400604 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 if (ep->me_key == dummy && freeslot == NULL)
606 freeslot = ep;
607 }
608 assert(0); /* NOT REACHED */
609 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000610}
611
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400612/* Faster version of lookdict_unicode when it is known that no <dummy> keys
613 * will be present. */
614static PyDictKeyEntry *
615lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
616 Py_hash_t hash, PyObject ***value_addr)
617{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200618 size_t i;
619 size_t perturb;
620 size_t mask = DK_MASK(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400621 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200622 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400623
624 /* Make sure this function doesn't have to handle non-unicode keys,
625 including subclasses of str; e.g., one reason to subclass
626 unicodes is to override __eq__, and for speed we don't cater to
627 that here. */
628 if (!PyUnicode_CheckExact(key)) {
629 mp->ma_keys->dk_lookup = lookdict;
630 return lookdict(mp, key, hash, value_addr);
631 }
632 i = (size_t)hash & mask;
633 ep = &ep0[i];
634 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
635 if (ep->me_key == NULL || ep->me_key == key ||
636 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
637 *value_addr = &ep->me_value;
638 return ep;
639 }
640 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
641 i = (i << 2) + i + perturb + 1;
642 ep = &ep0[i & mask];
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 }
650 assert(0); /* NOT REACHED */
651 return 0;
652}
653
654/* Version of lookdict for split tables.
655 * All split tables and only split tables use this lookup function.
656 * Split tables only contain unicode keys and no dummy keys,
657 * so algorithm is the same as lookdict_unicode_nodummy.
658 */
659static PyDictKeyEntry *
660lookdict_split(PyDictObject *mp, PyObject *key,
661 Py_hash_t hash, PyObject ***value_addr)
662{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200663 size_t i;
664 size_t perturb;
665 size_t mask = DK_MASK(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400666 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200667 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400668
669 if (!PyUnicode_CheckExact(key)) {
Benjamin Petersondb780d02012-04-23 13:44:32 -0400670 ep = lookdict(mp, key, hash, value_addr);
671 /* lookdict expects a combined-table, so fix value_addr */
672 i = ep - ep0;
673 *value_addr = &mp->ma_values[i];
674 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400675 }
676 i = (size_t)hash & mask;
677 ep = &ep0[i];
678 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
679 if (ep->me_key == NULL || ep->me_key == key ||
680 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
681 *value_addr = &mp->ma_values[i];
682 return ep;
683 }
684 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
685 i = (i << 2) + i + perturb + 1;
686 ep = &ep0[i & mask];
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 & mask];
691 return ep;
692 }
693 }
694 assert(0); /* NOT REACHED */
695 return 0;
696}
697
Benjamin Petersonfb886362010-04-24 18:21:17 +0000698int
699_PyDict_HasOnlyStringKeys(PyObject *dict)
700{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 Py_ssize_t pos = 0;
702 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000703 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400705 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 return 1;
707 while (PyDict_Next(dict, &pos, &key, &value))
708 if (!PyUnicode_Check(key))
709 return 0;
710 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000711}
712
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000713#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 do { \
715 if (!_PyObject_GC_IS_TRACKED(mp)) { \
716 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
717 _PyObject_GC_MAY_BE_TRACKED(value)) { \
718 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 } \
720 } \
721 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000722
723void
724_PyDict_MaybeUntrack(PyObject *op)
725{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 PyDictObject *mp;
727 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400728 Py_ssize_t i, size;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
731 return;
732
733 mp = (PyDictObject *) op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400734 size = DK_SIZE(mp->ma_keys);
735 if (_PyDict_HasSplitTable(mp)) {
736 for (i = 0; i < size; i++) {
737 if ((value = mp->ma_values[i]) == NULL)
738 continue;
739 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
740 assert(!_PyObject_GC_MAY_BE_TRACKED(
741 mp->ma_keys->dk_entries[i].me_key));
742 return;
743 }
744 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400746 else {
747 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
748 for (i = 0; i < size; i++) {
749 if ((value = ep0[i].me_value) == NULL)
750 continue;
751 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
752 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
753 return;
754 }
755 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000757}
758
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400759/* Internal function to find slot for an item from its hash
760 * when it is known that the key is not present in the dict.
761 */
762static PyDictKeyEntry *
763find_empty_slot(PyDictObject *mp, PyObject *key, Py_hash_t hash,
764 PyObject ***value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000765{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400766 size_t i;
767 size_t perturb;
768 size_t mask = DK_MASK(mp->ma_keys);
769 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
770 PyDictKeyEntry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000771
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400772 assert(key != NULL);
773 if (!PyUnicode_CheckExact(key))
774 mp->ma_keys->dk_lookup = lookdict;
775 i = hash & mask;
776 ep = &ep0[i];
777 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
778 i = (i << 2) + i + perturb + 1;
779 ep = &ep0[i & mask];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400781 assert(ep->me_value == NULL);
782 if (mp->ma_values)
783 *value_addr = &mp->ma_values[i & mask];
784 else
785 *value_addr = &ep->me_value;
786 return ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000787}
788
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400789static int
790insertion_resize(PyDictObject *mp)
791{
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700792 return dictresize(mp, GROWTH_RATE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400793}
Antoine Pitroue965d972012-02-27 00:45:12 +0100794
795/*
796Internal routine to insert a new item into the table.
797Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +0100798Returns -1 if an error occurred, or 0 on success.
799*/
800static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400801insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +0100802{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400803 PyObject *old_value;
804 PyObject **value_addr;
805 PyDictKeyEntry *ep;
806 assert(key != dummy);
Antoine Pitroue965d972012-02-27 00:45:12 +0100807
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400808 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
809 if (insertion_resize(mp) < 0)
810 return -1;
811 }
812
813 ep = mp->ma_keys->dk_lookup(mp, key, hash, &value_addr);
Antoine Pitroue965d972012-02-27 00:45:12 +0100814 if (ep == NULL) {
Antoine Pitroue965d972012-02-27 00:45:12 +0100815 return -1;
816 }
Benjamin Petersona6f195e2012-04-30 10:23:40 -0400817 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400818 MAINTAIN_TRACKING(mp, key, value);
819 old_value = *value_addr;
820 if (old_value != NULL) {
821 assert(ep->me_key != NULL && ep->me_key != dummy);
822 *value_addr = value;
823 Py_DECREF(old_value); /* which **CAN** re-enter */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400824 }
825 else {
826 if (ep->me_key == NULL) {
Benjamin Petersona6f195e2012-04-30 10:23:40 -0400827 Py_INCREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400828 if (mp->ma_keys->dk_usable <= 0) {
829 /* Need to resize. */
830 if (insertion_resize(mp) < 0) {
831 Py_DECREF(key);
832 Py_DECREF(value);
833 return -1;
834 }
835 ep = find_empty_slot(mp, key, hash, &value_addr);
836 }
837 mp->ma_keys->dk_usable--;
838 assert(mp->ma_keys->dk_usable >= 0);
839 ep->me_key = key;
840 ep->me_hash = hash;
841 }
842 else {
843 if (ep->me_key == dummy) {
Benjamin Petersona6f195e2012-04-30 10:23:40 -0400844 Py_INCREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400845 ep->me_key = key;
846 ep->me_hash = hash;
847 Py_DECREF(dummy);
848 } else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400849 assert(_PyDict_HasSplitTable(mp));
850 }
851 }
852 mp->ma_used++;
853 *value_addr = value;
854 }
855 assert(ep->me_key != NULL && ep->me_key != dummy);
856 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
857 return 0;
Antoine Pitroue965d972012-02-27 00:45:12 +0100858}
859
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000860/*
861Internal routine used by dictresize() to insert an item which is
862known to be absent from the dict. This routine also assumes that
863the dict contains no deleted entries. Besides the performance benefit,
864using insertdict() in dictresize() is dangerous (SF bug #1456209).
865Note that no refcounts are changed by this routine; if needed, the caller
866is responsible for incref'ing `key` and `value`.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400867Neither mp->ma_used nor k->dk_usable are modified by this routine; the caller
868must set them correctly
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000869*/
870static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400871insertdict_clean(PyDictObject *mp, PyObject *key, Py_hash_t hash,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 PyObject *value)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000873{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400874 size_t i;
875 size_t perturb;
876 PyDictKeysObject *k = mp->ma_keys;
877 size_t mask = (size_t)DK_SIZE(k)-1;
878 PyDictKeyEntry *ep0 = &k->dk_entries[0];
879 PyDictKeyEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000880
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400881 assert(k->dk_lookup != NULL);
882 assert(value != NULL);
883 assert(key != NULL);
884 assert(key != dummy);
885 assert(PyUnicode_CheckExact(key) || k->dk_lookup == lookdict);
886 i = hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 ep = &ep0[i];
888 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
889 i = (i << 2) + i + perturb + 1;
890 ep = &ep0[i & mask];
891 }
892 assert(ep->me_value == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 ep->me_key = key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000894 ep->me_hash = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000896}
897
898/*
899Restructure the table by allocating a new table and reinserting all
900items again. When entries have been deleted, the new table may
901actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400902If a table is split (its keys and hashes are shared, its values are not),
903then the values are temporarily copied into the table, it is resized as
904a combined table, then the me_value slots in the old table are NULLed out.
905After resizing a table is always combined,
906but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000907*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000908static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000909dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000910{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 Py_ssize_t newsize;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400912 PyDictKeysObject *oldkeys;
913 PyObject **oldvalues;
914 Py_ssize_t i, oldsize;
Tim Peters91a364d2001-05-19 07:04:38 +0000915
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400916/* Find the smallest table size > minused. */
917 for (newsize = PyDict_MINSIZE_COMBINED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 newsize <= minused && newsize > 0;
919 newsize <<= 1)
920 ;
921 if (newsize <= 0) {
922 PyErr_NoMemory();
923 return -1;
924 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400925 oldkeys = mp->ma_keys;
926 oldvalues = mp->ma_values;
927 /* Allocate a new table. */
928 mp->ma_keys = new_keys_object(newsize);
929 if (mp->ma_keys == NULL) {
930 mp->ma_keys = oldkeys;
931 return -1;
932 }
933 if (oldkeys->dk_lookup == lookdict)
934 mp->ma_keys->dk_lookup = lookdict;
935 oldsize = DK_SIZE(oldkeys);
936 mp->ma_values = NULL;
937 /* If empty then nothing to copy so just return */
938 if (oldsize == 1) {
939 assert(oldkeys == Py_EMPTY_KEYS);
940 DK_DECREF(oldkeys);
941 return 0;
942 }
943 /* Main loop below assumes we can transfer refcount to new keys
944 * and that value is stored in me_value.
945 * Increment ref-counts and copy values here to compensate
946 * This (resizing a split table) should be relatively rare */
947 if (oldvalues != NULL) {
948 for (i = 0; i < oldsize; i++) {
949 if (oldvalues[i] != NULL) {
950 Py_INCREF(oldkeys->dk_entries[i].me_key);
951 oldkeys->dk_entries[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 }
954 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400955 /* Main loop */
956 for (i = 0; i < oldsize; i++) {
957 PyDictKeyEntry *ep = &oldkeys->dk_entries[i];
958 if (ep->me_value != NULL) {
959 assert(ep->me_key != dummy);
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000960 insertdict_clean(mp, ep->me_key, ep->me_hash, ep->me_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400963 mp->ma_keys->dk_usable -= mp->ma_used;
964 if (oldvalues != NULL) {
965 /* NULL out me_value slot in oldkeys, in case it was shared */
966 for (i = 0; i < oldsize; i++)
967 oldkeys->dk_entries[i].me_value = NULL;
968 assert(oldvalues != empty_values);
969 free_values(oldvalues);
970 DK_DECREF(oldkeys);
971 }
972 else {
973 assert(oldkeys->dk_lookup != lookdict_split);
974 if (oldkeys->dk_lookup != lookdict_unicode_nodummy) {
975 PyDictKeyEntry *ep0 = &oldkeys->dk_entries[0];
976 for (i = 0; i < oldsize; i++) {
977 if (ep0[i].me_key == dummy)
978 Py_DECREF(dummy);
979 }
980 }
981 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200982 DK_DEBUG_DECREF PyMem_FREE(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400983 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000985}
986
Benjamin Peterson15ee8212012-04-24 14:44:18 -0400987/* Returns NULL if unable to split table.
988 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400989static PyDictKeysObject *
990make_keys_shared(PyObject *op)
991{
992 Py_ssize_t i;
993 Py_ssize_t size;
994 PyDictObject *mp = (PyDictObject *)op;
995
Benjamin Peterson15ee8212012-04-24 14:44:18 -0400996 if (!PyDict_CheckExact(op))
997 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400998 if (!_PyDict_HasSplitTable(mp)) {
999 PyDictKeyEntry *ep0;
1000 PyObject **values;
1001 assert(mp->ma_keys->dk_refcnt == 1);
1002 if (mp->ma_keys->dk_lookup == lookdict) {
1003 return NULL;
1004 }
1005 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
1006 /* Remove dummy keys */
1007 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
1008 return NULL;
1009 }
1010 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
1011 /* Copy values into a new array */
1012 ep0 = &mp->ma_keys->dk_entries[0];
1013 size = DK_SIZE(mp->ma_keys);
1014 values = new_values(size);
1015 if (values == NULL) {
1016 PyErr_SetString(PyExc_MemoryError,
1017 "Not enough memory to allocate new values array");
1018 return NULL;
1019 }
1020 for (i = 0; i < size; i++) {
1021 values[i] = ep0[i].me_value;
1022 ep0[i].me_value = NULL;
1023 }
1024 mp->ma_keys->dk_lookup = lookdict_split;
1025 mp->ma_values = values;
1026 }
1027 DK_INCREF(mp->ma_keys);
1028 return mp->ma_keys;
1029}
Christian Heimes99170a52007-12-19 02:07:34 +00001030
1031PyObject *
1032_PyDict_NewPresized(Py_ssize_t minused)
1033{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001034 Py_ssize_t newsize;
1035 PyDictKeysObject *new_keys;
1036 for (newsize = PyDict_MINSIZE_COMBINED;
1037 newsize <= minused && newsize > 0;
1038 newsize <<= 1)
1039 ;
1040 new_keys = new_keys_object(newsize);
1041 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001043 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001044}
1045
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001046/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1047 * that may occur (originally dicts supported only string keys, and exceptions
1048 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001049 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001050 * (suppressed) occurred while computing the key's hash, or that some error
1051 * (suppressed) occurred when comparing keys in the dict's internal probe
1052 * sequence. A nasty example of the latter is when a Python-coded comparison
1053 * function hits a stack-depth error, which can cause this to return NULL
1054 * even if the key is present.
1055 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001056PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001057PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001058{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001059 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001061 PyDictKeyEntry *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 PyThreadState *tstate;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001063 PyObject **value_addr;
1064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 if (!PyDict_Check(op))
1066 return NULL;
1067 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001068 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 {
1070 hash = PyObject_Hash(key);
1071 if (hash == -1) {
1072 PyErr_Clear();
1073 return NULL;
1074 }
1075 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 /* We can arrive here with a NULL tstate during initialization: try
1078 running "python -Wi" for an example related to string interning.
1079 Let's just hope that no exception occurs then... This must be
1080 _PyThreadState_Current and not PyThreadState_GET() because in debug
1081 mode, the latter complains if tstate is NULL. */
1082 tstate = (PyThreadState*)_Py_atomic_load_relaxed(
1083 &_PyThreadState_Current);
1084 if (tstate != NULL && tstate->curexc_type != NULL) {
1085 /* preserve the existing exception */
1086 PyObject *err_type, *err_value, *err_tb;
1087 PyErr_Fetch(&err_type, &err_value, &err_tb);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001088 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 /* ignore errors */
1090 PyErr_Restore(err_type, err_value, err_tb);
1091 if (ep == NULL)
1092 return NULL;
1093 }
1094 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001095 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 if (ep == NULL) {
1097 PyErr_Clear();
1098 return NULL;
1099 }
1100 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001101 return *value_addr;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001102}
1103
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001104/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1105 This returns NULL *with* an exception set if an exception occurred.
1106 It returns NULL *without* an exception set if the key wasn't present.
1107*/
1108PyObject *
1109PyDict_GetItemWithError(PyObject *op, PyObject *key)
1110{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001111 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 PyDictObject*mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001113 PyDictKeyEntry *ep;
1114 PyObject **value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 if (!PyDict_Check(op)) {
1117 PyErr_BadInternalCall();
1118 return NULL;
1119 }
1120 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001121 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 {
1123 hash = PyObject_Hash(key);
1124 if (hash == -1) {
1125 return NULL;
1126 }
1127 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001128
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001129 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 if (ep == NULL)
1131 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001132 return *value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001133}
1134
Brett Cannonfd074152012-04-14 14:10:13 -04001135PyObject *
1136_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1137{
1138 PyObject *kv;
1139 kv = _PyUnicode_FromId(key); /* borrowed */
1140 if (kv == NULL)
1141 return NULL;
1142 return PyDict_GetItemWithError(dp, kv);
1143}
1144
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001145/* Fast version of global value lookup.
1146 * Lookup in globals, then builtins.
1147 */
1148PyObject *
1149_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001150{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001151 PyObject *x;
1152 if (PyUnicode_CheckExact(key)) {
1153 PyObject **value_addr;
1154 Py_hash_t hash = ((PyASCIIObject *)key)->hash;
1155 if (hash != -1) {
1156 PyDictKeyEntry *e;
1157 e = globals->ma_keys->dk_lookup(globals, key, hash, &value_addr);
1158 if (e == NULL) {
1159 return NULL;
1160 }
1161 x = *value_addr;
1162 if (x != NULL)
1163 return x;
1164 e = builtins->ma_keys->dk_lookup(builtins, key, hash, &value_addr);
1165 if (e == NULL) {
1166 return NULL;
1167 }
1168 x = *value_addr;
1169 return x;
1170 }
Antoine Pitroue965d972012-02-27 00:45:12 +01001171 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001172 x = PyDict_GetItemWithError((PyObject *)globals, key);
1173 if (x != NULL)
1174 return x;
1175 if (PyErr_Occurred())
1176 return NULL;
1177 return PyDict_GetItemWithError((PyObject *)builtins, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001178}
1179
Antoine Pitroue965d972012-02-27 00:45:12 +01001180/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1181 * dictionary if it's merely replacing the value for an existing key.
1182 * This means that it's safe to loop over a dictionary with PyDict_Next()
1183 * and occasionally replace a value -- but you can't insert new keys or
1184 * remove them.
1185 */
1186int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001187PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001188{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001189 PyDictObject *mp;
1190 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001191 if (!PyDict_Check(op)) {
1192 PyErr_BadInternalCall();
1193 return -1;
1194 }
1195 assert(key);
1196 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001197 mp = (PyDictObject *)op;
1198 if (!PyUnicode_CheckExact(key) ||
1199 (hash = ((PyASCIIObject *) key)->hash) == -1)
1200 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001201 hash = PyObject_Hash(key);
1202 if (hash == -1)
1203 return -1;
1204 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001205
1206 /* insertdict() handles any resizing that might be necessary */
1207 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001208}
1209
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001210int
Tim Peters1f5871e2000-07-04 17:44:48 +00001211PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001212{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001213 PyDictObject *mp;
1214 Py_hash_t hash;
1215 PyDictKeyEntry *ep;
1216 PyObject *old_key, *old_value;
1217 PyObject **value_addr;
Guido van Rossumd7047b31995-01-02 19:07:15 +00001218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 if (!PyDict_Check(op)) {
1220 PyErr_BadInternalCall();
1221 return -1;
1222 }
1223 assert(key);
1224 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001225 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 hash = PyObject_Hash(key);
1227 if (hash == -1)
1228 return -1;
1229 }
1230 mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001231 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 if (ep == NULL)
1233 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001234 if (*value_addr == NULL) {
Raymond Hettinger69492da2013-09-02 15:59:26 -07001235 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 return -1;
1237 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001238 old_value = *value_addr;
1239 *value_addr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001241 if (!_PyDict_HasSplitTable(mp)) {
1242 ENSURE_ALLOWS_DELETIONS(mp);
1243 old_key = ep->me_key;
1244 Py_INCREF(dummy);
1245 ep->me_key = dummy;
1246 Py_DECREF(old_key);
1247 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 Py_DECREF(old_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001250}
1251
Guido van Rossum25831651993-05-19 14:50:45 +00001252void
Tim Peters1f5871e2000-07-04 17:44:48 +00001253PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001254{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001256 PyDictKeysObject *oldkeys;
1257 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 if (!PyDict_Check(op))
1261 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001262 mp = ((PyDictObject *)op);
1263 oldkeys = mp->ma_keys;
1264 oldvalues = mp->ma_values;
1265 if (oldvalues == empty_values)
1266 return;
1267 /* Empty the dict... */
1268 DK_INCREF(Py_EMPTY_KEYS);
1269 mp->ma_keys = Py_EMPTY_KEYS;
1270 mp->ma_values = empty_values;
1271 mp->ma_used = 0;
1272 /* ...then clear the keys and values */
1273 if (oldvalues != NULL) {
1274 n = DK_SIZE(oldkeys);
1275 for (i = 0; i < n; i++)
1276 Py_CLEAR(oldvalues[i]);
1277 free_values(oldvalues);
1278 DK_DECREF(oldkeys);
1279 }
1280 else {
1281 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001282 DK_DECREF(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001283 }
1284}
1285
1286/* Returns -1 if no more items (or op is not a dict),
1287 * index of item otherwise. Stores value in pvalue
1288 */
1289Py_LOCAL_INLINE(Py_ssize_t)
1290dict_next(PyObject *op, Py_ssize_t i, PyObject **pvalue)
1291{
1292 Py_ssize_t mask, offset;
1293 PyDictObject *mp;
1294 PyObject **value_ptr;
1295
1296
1297 if (!PyDict_Check(op))
1298 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001300 if (i < 0)
1301 return -1;
1302 if (mp->ma_values) {
1303 value_ptr = &mp->ma_values[i];
1304 offset = sizeof(PyObject *);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001306 else {
1307 value_ptr = &mp->ma_keys->dk_entries[i].me_value;
1308 offset = sizeof(PyDictKeyEntry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001310 mask = DK_MASK(mp->ma_keys);
1311 while (i <= mask && *value_ptr == NULL) {
1312 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1313 i++;
1314 }
1315 if (i > mask)
1316 return -1;
1317 if (pvalue)
1318 *pvalue = *value_ptr;
1319 return i;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001320}
1321
Tim Peters080c88b2003-02-15 03:01:11 +00001322/*
1323 * Iterate over a dict. Use like so:
1324 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001325 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001326 * PyObject *key, *value;
1327 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001328 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +00001329 * Refer to borrowed references in key and value.
1330 * }
1331 *
1332 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001333 * mutates the dict. One exception: it is safe if the loop merely changes
1334 * the values associated with the keys (but doesn't insert new keys or
1335 * delete keys), via PyDict_SetItem().
1336 */
Guido van Rossum25831651993-05-19 14:50:45 +00001337int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001338PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001339{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001340 PyDictObject *mp;
1341 Py_ssize_t i = dict_next(op, *ppos, pvalue);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 if (i < 0)
1343 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001344 mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 *ppos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 if (pkey)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001347 *pkey = mp->ma_keys->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001349}
1350
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001351/* Internal version of PyDict_Next that returns a hash value in addition
1352 * to the key and value.
1353 */
Thomas Wouterscf297e42007-02-23 15:07:44 +00001354int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001355_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1356 PyObject **pvalue, Py_hash_t *phash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00001357{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001358 PyDictObject *mp;
1359 Py_ssize_t i = dict_next(op, *ppos, pvalue);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 if (i < 0)
1361 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001362 mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 *ppos = i+1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001364 *phash = mp->ma_keys->dk_entries[i].me_hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 if (pkey)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001366 *pkey = mp->ma_keys->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 return 1;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001368}
1369
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001370/* Methods */
1371
1372static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001373dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001374{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001375 PyObject **values = mp->ma_values;
1376 PyDictKeysObject *keys = mp->ma_keys;
1377 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 PyObject_GC_UnTrack(mp);
1379 Py_TRASHCAN_SAFE_BEGIN(mp)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001380 if (values != NULL) {
1381 if (values != empty_values) {
1382 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
1383 Py_XDECREF(values[i]);
1384 }
1385 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001387 DK_DECREF(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02001389 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001390 assert(keys->dk_refcnt == 1);
1391 DK_DECREF(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001392 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1394 free_list[numfree++] = mp;
1395 else
1396 Py_TYPE(mp)->tp_free((PyObject *)mp);
1397 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001398}
1399
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001400
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001401static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001402dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001403{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001405 PyObject *key = NULL, *value = NULL;
1406 _PyUnicodeWriter writer;
1407 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00001408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 i = Py_ReprEnter((PyObject *)mp);
1410 if (i != 0) {
1411 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
1412 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001414 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001415 Py_ReprLeave((PyObject *)mp);
1416 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 }
Tim Petersa7259592001-06-16 05:11:17 +00001418
Victor Stinnerf91929b2013-11-19 13:07:38 +01001419 _PyUnicodeWriter_Init(&writer);
1420 writer.overallocate = 1;
1421 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
1422 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00001423
Victor Stinnerf91929b2013-11-19 13:07:38 +01001424 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
1425 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 /* Do repr() on each key+value pair, and insert ": " between them.
1428 Note that repr may mutate the dict. */
1429 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001430 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001431 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001432 PyObject *s;
1433 int res;
1434
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001435 /* Prevent repr from deleting key or value during key format. */
1436 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02001438
Victor Stinnerf91929b2013-11-19 13:07:38 +01001439 if (!first) {
1440 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
1441 goto error;
1442 }
1443 first = 0;
1444
1445 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01001447 goto error;
1448 res = _PyUnicodeWriter_WriteStr(&writer, s);
1449 Py_DECREF(s);
1450 if (res < 0)
1451 goto error;
1452
1453 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
1454 goto error;
1455
1456 s = PyObject_Repr(value);
1457 if (s == NULL)
1458 goto error;
1459 res = _PyUnicodeWriter_WriteStr(&writer, s);
1460 Py_DECREF(s);
1461 if (res < 0)
1462 goto error;
1463
1464 Py_CLEAR(key);
1465 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001466 }
Tim Petersa7259592001-06-16 05:11:17 +00001467
Victor Stinnerf91929b2013-11-19 13:07:38 +01001468 writer.overallocate = 0;
1469 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
1470 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01001473
1474 return _PyUnicodeWriter_Finish(&writer);
1475
1476error:
1477 Py_ReprLeave((PyObject *)mp);
1478 _PyUnicodeWriter_Dealloc(&writer);
1479 Py_XDECREF(key);
1480 Py_XDECREF(value);
1481 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001482}
1483
Martin v. Löwis18e16552006-02-15 17:27:45 +00001484static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001485dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001486{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001488}
1489
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001490static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001491dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001492{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001493 PyObject *v;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001494 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001495 PyDictKeyEntry *ep;
1496 PyObject **value_addr;
1497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001498 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001499 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001500 hash = PyObject_Hash(key);
1501 if (hash == -1)
1502 return NULL;
1503 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001504 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 if (ep == NULL)
1506 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001507 v = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001508 if (v == NULL) {
1509 if (!PyDict_CheckExact(mp)) {
1510 /* Look up __missing__ method if we're a subclass. */
1511 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05001512 _Py_IDENTIFIER(__missing__);
1513 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 if (missing != NULL) {
1515 res = PyObject_CallFunctionObjArgs(missing,
1516 key, NULL);
1517 Py_DECREF(missing);
1518 return res;
1519 }
1520 else if (PyErr_Occurred())
1521 return NULL;
1522 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07001523 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 return NULL;
1525 }
1526 else
1527 Py_INCREF(v);
1528 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001529}
1530
1531static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001532dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001533{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534 if (w == NULL)
1535 return PyDict_DelItem((PyObject *)mp, v);
1536 else
1537 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001538}
1539
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001540static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001541 (lenfunc)dict_length, /*mp_length*/
1542 (binaryfunc)dict_subscript, /*mp_subscript*/
1543 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001544};
1545
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001546static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001547dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001548{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001549 PyObject *v;
1550 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001551 PyDictKeyEntry *ep;
1552 Py_ssize_t size, n, offset;
1553 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001554
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001555 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 n = mp->ma_used;
1557 v = PyList_New(n);
1558 if (v == NULL)
1559 return NULL;
1560 if (n != mp->ma_used) {
1561 /* Durnit. The allocations caused the dict to resize.
1562 * Just start over, this shouldn't normally happen.
1563 */
1564 Py_DECREF(v);
1565 goto again;
1566 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001567 ep = &mp->ma_keys->dk_entries[0];
1568 size = DK_SIZE(mp->ma_keys);
1569 if (mp->ma_values) {
1570 value_ptr = mp->ma_values;
1571 offset = sizeof(PyObject *);
1572 }
1573 else {
1574 value_ptr = &ep[0].me_value;
1575 offset = sizeof(PyDictKeyEntry);
1576 }
1577 for (i = 0, j = 0; i < size; i++) {
1578 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 PyObject *key = ep[i].me_key;
1580 Py_INCREF(key);
1581 PyList_SET_ITEM(v, j, key);
1582 j++;
1583 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001584 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 }
1586 assert(j == n);
1587 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001588}
1589
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001590static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001591dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001592{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001593 PyObject *v;
1594 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001595 Py_ssize_t size, n, offset;
1596 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001597
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001598 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 n = mp->ma_used;
1600 v = PyList_New(n);
1601 if (v == NULL)
1602 return NULL;
1603 if (n != mp->ma_used) {
1604 /* Durnit. The allocations caused the dict to resize.
1605 * Just start over, this shouldn't normally happen.
1606 */
1607 Py_DECREF(v);
1608 goto again;
1609 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001610 size = DK_SIZE(mp->ma_keys);
1611 if (mp->ma_values) {
1612 value_ptr = mp->ma_values;
1613 offset = sizeof(PyObject *);
1614 }
1615 else {
1616 value_ptr = &mp->ma_keys->dk_entries[0].me_value;
1617 offset = sizeof(PyDictKeyEntry);
1618 }
1619 for (i = 0, j = 0; i < size; i++) {
1620 PyObject *value = *value_ptr;
1621 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1622 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 Py_INCREF(value);
1624 PyList_SET_ITEM(v, j, value);
1625 j++;
1626 }
1627 }
1628 assert(j == n);
1629 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001630}
1631
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001632static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001633dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001634{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001635 PyObject *v;
1636 Py_ssize_t i, j, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001637 Py_ssize_t size, offset;
1638 PyObject *item, *key;
1639 PyDictKeyEntry *ep;
1640 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 /* Preallocate the list of tuples, to avoid allocations during
1643 * the loop over the items, which could trigger GC, which
1644 * could resize the dict. :-(
1645 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001646 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 n = mp->ma_used;
1648 v = PyList_New(n);
1649 if (v == NULL)
1650 return NULL;
1651 for (i = 0; i < n; i++) {
1652 item = PyTuple_New(2);
1653 if (item == NULL) {
1654 Py_DECREF(v);
1655 return NULL;
1656 }
1657 PyList_SET_ITEM(v, i, item);
1658 }
1659 if (n != mp->ma_used) {
1660 /* Durnit. The allocations caused the dict to resize.
1661 * Just start over, this shouldn't normally happen.
1662 */
1663 Py_DECREF(v);
1664 goto again;
1665 }
1666 /* Nothing we do below makes any function calls. */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001667 ep = mp->ma_keys->dk_entries;
1668 size = DK_SIZE(mp->ma_keys);
1669 if (mp->ma_values) {
1670 value_ptr = mp->ma_values;
1671 offset = sizeof(PyObject *);
1672 }
1673 else {
1674 value_ptr = &ep[0].me_value;
1675 offset = sizeof(PyDictKeyEntry);
1676 }
1677 for (i = 0, j = 0; i < size; i++) {
1678 PyObject *value = *value_ptr;
1679 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1680 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001681 key = ep[i].me_key;
1682 item = PyList_GET_ITEM(v, j);
1683 Py_INCREF(key);
1684 PyTuple_SET_ITEM(item, 0, key);
1685 Py_INCREF(value);
1686 PyTuple_SET_ITEM(item, 1, value);
1687 j++;
1688 }
1689 }
1690 assert(j == n);
1691 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001692}
1693
Larry Hastings5c661892014-01-24 06:17:25 -08001694/*[clinic input]
1695@classmethod
1696dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08001697 iterable: object
1698 value: object=None
1699 /
1700
1701Returns a new dict with keys from iterable and values equal to value.
1702[clinic start generated code]*/
1703
1704PyDoc_STRVAR(dict_fromkeys__doc__,
Larry Hastings581ee362014-01-28 05:00:08 -08001705"sig=($type, iterable, value=None)\n"
Larry Hastings5c661892014-01-24 06:17:25 -08001706"Returns a new dict with keys from iterable and values equal to value.");
1707
1708#define DICT_FROMKEYS_METHODDEF \
1709 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS|METH_CLASS, dict_fromkeys__doc__},
1710
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001711static PyObject *
Larry Hastings5c661892014-01-24 06:17:25 -08001712dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value);
1713
1714static PyObject *
1715dict_fromkeys(PyTypeObject *type, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001716{
Larry Hastings5c661892014-01-24 06:17:25 -08001717 PyObject *return_value = NULL;
1718 PyObject *iterable;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 PyObject *value = Py_None;
Larry Hastings5c661892014-01-24 06:17:25 -08001720
1721 if (!PyArg_UnpackTuple(args, "fromkeys",
1722 1, 2,
1723 &iterable, &value))
1724 goto exit;
1725 return_value = dict_fromkeys_impl(type, iterable, value);
1726
1727exit:
1728 return return_value;
1729}
1730
1731static PyObject *
1732dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Larry Hastings581ee362014-01-28 05:00:08 -08001733/*[clinic end generated code: output=aff6e583703dbeba input=b85a667f9bf4669d]*/
Larry Hastings5c661892014-01-24 06:17:25 -08001734{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001735 PyObject *it; /* iter(seq) */
1736 PyObject *key;
1737 PyObject *d;
1738 int status;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001739
Larry Hastings5c661892014-01-24 06:17:25 -08001740 d = PyObject_CallObject((PyObject *)type, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 if (d == NULL)
1742 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001743
Benjamin Peterson9892f522012-10-31 14:22:12 -04001744 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
Larry Hastings5c661892014-01-24 06:17:25 -08001745 if (PyDict_CheckExact(iterable)) {
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001746 PyDictObject *mp = (PyDictObject *)d;
1747 PyObject *oldvalue;
1748 Py_ssize_t pos = 0;
1749 PyObject *key;
1750 Py_hash_t hash;
Guido van Rossum58da9312007-11-10 23:39:45 +00001751
Larry Hastings5c661892014-01-24 06:17:25 -08001752 if (dictresize(mp, Py_SIZE(iterable))) {
Petri Lehtinena94200e2011-10-24 21:12:58 +03001753 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001754 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001755 }
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001756
Larry Hastings5c661892014-01-24 06:17:25 -08001757 while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001758 if (insertdict(mp, key, hash, value)) {
1759 Py_DECREF(d);
1760 return NULL;
1761 }
1762 }
1763 return d;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001764 }
Larry Hastings5c661892014-01-24 06:17:25 -08001765 if (PyAnySet_CheckExact(iterable)) {
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001766 PyDictObject *mp = (PyDictObject *)d;
1767 Py_ssize_t pos = 0;
1768 PyObject *key;
1769 Py_hash_t hash;
Guido van Rossum58da9312007-11-10 23:39:45 +00001770
Larry Hastings5c661892014-01-24 06:17:25 -08001771 if (dictresize(mp, PySet_GET_SIZE(iterable))) {
Petri Lehtinena94200e2011-10-24 21:12:58 +03001772 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001774 }
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001775
Larry Hastings5c661892014-01-24 06:17:25 -08001776 while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001777 if (insertdict(mp, key, hash, value)) {
1778 Py_DECREF(d);
1779 return NULL;
1780 }
1781 }
1782 return d;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001784 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001785
Larry Hastings5c661892014-01-24 06:17:25 -08001786 it = PyObject_GetIter(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 if (it == NULL){
1788 Py_DECREF(d);
1789 return NULL;
1790 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001792 if (PyDict_CheckExact(d)) {
1793 while ((key = PyIter_Next(it)) != NULL) {
1794 status = PyDict_SetItem(d, key, value);
1795 Py_DECREF(key);
1796 if (status < 0)
1797 goto Fail;
1798 }
1799 } else {
1800 while ((key = PyIter_Next(it)) != NULL) {
1801 status = PyObject_SetItem(d, key, value);
1802 Py_DECREF(key);
1803 if (status < 0)
1804 goto Fail;
1805 }
1806 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 if (PyErr_Occurred())
1809 goto Fail;
1810 Py_DECREF(it);
1811 return d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001812
1813Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001814 Py_DECREF(it);
1815 Py_DECREF(d);
1816 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001817}
1818
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001819static int
1820dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001821{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 PyObject *arg = NULL;
1823 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1826 result = -1;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001827
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001828 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001829 _Py_IDENTIFIER(keys);
1830 if (_PyObject_HasAttrId(arg, &PyId_keys))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001831 result = PyDict_Merge(self, arg, 1);
1832 else
1833 result = PyDict_MergeFromSeq2(self, arg, 1);
1834 }
1835 if (result == 0 && kwds != NULL) {
1836 if (PyArg_ValidateKeywordArguments(kwds))
1837 result = PyDict_Merge(self, kwds, 1);
1838 else
1839 result = -1;
1840 }
1841 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001842}
1843
1844static PyObject *
1845dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1846{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 if (dict_update_common(self, args, kwds, "update") != -1)
1848 Py_RETURN_NONE;
1849 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001850}
1851
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001852/* Update unconditionally replaces existing items.
1853 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001854 otherwise it leaves existing items unchanged.
1855
1856 PyDict_{Update,Merge} update/merge from a mapping object.
1857
Tim Petersf582b822001-12-11 18:51:08 +00001858 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001859 producing iterable objects of length 2.
1860*/
1861
Tim Petersf582b822001-12-11 18:51:08 +00001862int
Tim Peters1fc240e2001-10-26 05:06:50 +00001863PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1864{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 PyObject *it; /* iter(seq2) */
1866 Py_ssize_t i; /* index into seq2 of current element */
1867 PyObject *item; /* seq2[i] */
1868 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00001869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001870 assert(d != NULL);
1871 assert(PyDict_Check(d));
1872 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00001873
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001874 it = PyObject_GetIter(seq2);
1875 if (it == NULL)
1876 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001878 for (i = 0; ; ++i) {
1879 PyObject *key, *value;
1880 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001882 fast = NULL;
1883 item = PyIter_Next(it);
1884 if (item == NULL) {
1885 if (PyErr_Occurred())
1886 goto Fail;
1887 break;
1888 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001889
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890 /* Convert item to sequence, and verify length 2. */
1891 fast = PySequence_Fast(item, "");
1892 if (fast == NULL) {
1893 if (PyErr_ExceptionMatches(PyExc_TypeError))
1894 PyErr_Format(PyExc_TypeError,
1895 "cannot convert dictionary update "
1896 "sequence element #%zd to a sequence",
1897 i);
1898 goto Fail;
1899 }
1900 n = PySequence_Fast_GET_SIZE(fast);
1901 if (n != 2) {
1902 PyErr_Format(PyExc_ValueError,
1903 "dictionary update sequence element #%zd "
1904 "has length %zd; 2 is required",
1905 i, n);
1906 goto Fail;
1907 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909 /* Update/merge with this (key, value) pair. */
1910 key = PySequence_Fast_GET_ITEM(fast, 0);
1911 value = PySequence_Fast_GET_ITEM(fast, 1);
1912 if (override || PyDict_GetItem(d, key) == NULL) {
1913 int status = PyDict_SetItem(d, key, value);
1914 if (status < 0)
1915 goto Fail;
1916 }
1917 Py_DECREF(fast);
1918 Py_DECREF(item);
1919 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 i = 0;
1922 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00001923Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 Py_XDECREF(item);
1925 Py_XDECREF(fast);
1926 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001927Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001928 Py_DECREF(it);
1929 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001930}
1931
Tim Peters6d6c1a32001-08-02 04:15:00 +00001932int
1933PyDict_Update(PyObject *a, PyObject *b)
1934{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001935 return PyDict_Merge(a, b, 1);
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001936}
1937
1938int
1939PyDict_Merge(PyObject *a, PyObject *b, int override)
1940{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001941 PyDictObject *mp, *other;
1942 Py_ssize_t i, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001943 PyDictKeyEntry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001944
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001945 /* We accept for the argument either a concrete dictionary object,
1946 * or an abstract "mapping" object. For the former, we can do
1947 * things quite efficiently. For the latter, we only require that
1948 * PyMapping_Keys() and PyObject_GetItem() be supported.
1949 */
1950 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1951 PyErr_BadInternalCall();
1952 return -1;
1953 }
1954 mp = (PyDictObject*)a;
1955 if (PyDict_Check(b)) {
1956 other = (PyDictObject*)b;
1957 if (other == mp || other->ma_used == 0)
1958 /* a.update(a) or a.update({}); nothing to do */
1959 return 0;
1960 if (mp->ma_used == 0)
1961 /* Since the target dict is empty, PyDict_GetItem()
1962 * always returns NULL. Setting override to 1
1963 * skips the unnecessary test.
1964 */
1965 override = 1;
1966 /* Do one big resize at the start, rather than
1967 * incrementally resizing as we insert new items. Expect
1968 * that there will be no (or few) overlapping keys.
1969 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001970 if (mp->ma_keys->dk_usable * 3 < other->ma_used * 2)
1971 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001972 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001973 for (i = 0, n = DK_SIZE(other->ma_keys); i < n; i++) {
1974 PyObject *value;
1975 entry = &other->ma_keys->dk_entries[i];
1976 if (other->ma_values)
1977 value = other->ma_values[i];
1978 else
1979 value = entry->me_value;
1980
1981 if (value != NULL &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001982 (override ||
1983 PyDict_GetItem(a, entry->me_key) == NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984 if (insertdict(mp, entry->me_key,
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001985 entry->me_hash,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001986 value) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001987 return -1;
1988 }
1989 }
1990 }
1991 else {
1992 /* Do it the generic, slower way */
1993 PyObject *keys = PyMapping_Keys(b);
1994 PyObject *iter;
1995 PyObject *key, *value;
1996 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001998 if (keys == NULL)
1999 /* Docstring says this is equivalent to E.keys() so
2000 * if E doesn't have a .keys() method we want
2001 * AttributeError to percolate up. Might as well
2002 * do the same for any other error.
2003 */
2004 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002006 iter = PyObject_GetIter(keys);
2007 Py_DECREF(keys);
2008 if (iter == NULL)
2009 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002011 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
2012 if (!override && PyDict_GetItem(a, key) != NULL) {
2013 Py_DECREF(key);
2014 continue;
2015 }
2016 value = PyObject_GetItem(b, key);
2017 if (value == NULL) {
2018 Py_DECREF(iter);
2019 Py_DECREF(key);
2020 return -1;
2021 }
2022 status = PyDict_SetItem(a, key, value);
2023 Py_DECREF(key);
2024 Py_DECREF(value);
2025 if (status < 0) {
2026 Py_DECREF(iter);
2027 return -1;
2028 }
2029 }
2030 Py_DECREF(iter);
2031 if (PyErr_Occurred())
2032 /* Iterator completed, via error */
2033 return -1;
2034 }
2035 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002036}
2037
2038static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002039dict_copy(PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002040{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002041 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002042}
2043
2044PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002045PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002046{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002047 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002048 PyDictObject *mp;
2049 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002050
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002051 if (o == NULL || !PyDict_Check(o)) {
2052 PyErr_BadInternalCall();
2053 return NULL;
2054 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002055 mp = (PyDictObject *)o;
2056 if (_PyDict_HasSplitTable(mp)) {
2057 PyDictObject *split_copy;
2058 PyObject **newvalues = new_values(DK_SIZE(mp->ma_keys));
2059 if (newvalues == NULL)
2060 return PyErr_NoMemory();
2061 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2062 if (split_copy == NULL) {
2063 free_values(newvalues);
2064 return NULL;
2065 }
2066 split_copy->ma_values = newvalues;
2067 split_copy->ma_keys = mp->ma_keys;
2068 split_copy->ma_used = mp->ma_used;
2069 DK_INCREF(mp->ma_keys);
2070 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2071 PyObject *value = mp->ma_values[i];
2072 Py_XINCREF(value);
2073 split_copy->ma_values[i] = value;
2074 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002075 if (_PyObject_GC_IS_TRACKED(mp))
2076 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002077 return (PyObject *)split_copy;
2078 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002079 copy = PyDict_New();
2080 if (copy == NULL)
2081 return NULL;
2082 if (PyDict_Merge(copy, o, 1) == 0)
2083 return copy;
2084 Py_DECREF(copy);
2085 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002086}
2087
Martin v. Löwis18e16552006-02-15 17:27:45 +00002088Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002089PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002090{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002091 if (mp == NULL || !PyDict_Check(mp)) {
2092 PyErr_BadInternalCall();
2093 return -1;
2094 }
2095 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002096}
2097
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002098PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002099PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002100{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002101 if (mp == NULL || !PyDict_Check(mp)) {
2102 PyErr_BadInternalCall();
2103 return NULL;
2104 }
2105 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002106}
2107
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002108PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002109PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002110{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002111 if (mp == NULL || !PyDict_Check(mp)) {
2112 PyErr_BadInternalCall();
2113 return NULL;
2114 }
2115 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002116}
2117
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002118PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002119PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002120{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002121 if (mp == NULL || !PyDict_Check(mp)) {
2122 PyErr_BadInternalCall();
2123 return NULL;
2124 }
2125 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002126}
2127
Tim Peterse63415e2001-05-08 04:38:29 +00002128/* Return 1 if dicts equal, 0 if not, -1 if error.
2129 * Gets out as soon as any difference is detected.
2130 * Uses only Py_EQ comparison.
2131 */
2132static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002133dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002134{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002135 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002137 if (a->ma_used != b->ma_used)
2138 /* can't be equal if # of entries differ */
2139 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002140 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002141 for (i = 0; i < DK_SIZE(a->ma_keys); i++) {
2142 PyDictKeyEntry *ep = &a->ma_keys->dk_entries[i];
2143 PyObject *aval;
2144 if (a->ma_values)
2145 aval = a->ma_values[i];
2146 else
2147 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002148 if (aval != NULL) {
2149 int cmp;
2150 PyObject *bval;
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002151 PyObject **vaddr;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002152 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002153 /* temporarily bump aval's refcount to ensure it stays
2154 alive until we're done with it */
2155 Py_INCREF(aval);
2156 /* ditto for key */
2157 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002158 /* reuse the known hash value */
2159 if ((b->ma_keys->dk_lookup)(b, key, ep->me_hash, &vaddr) == NULL)
2160 bval = NULL;
2161 else
2162 bval = *vaddr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002163 Py_DECREF(key);
2164 if (bval == NULL) {
2165 Py_DECREF(aval);
2166 if (PyErr_Occurred())
2167 return -1;
2168 return 0;
2169 }
2170 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
2171 Py_DECREF(aval);
2172 if (cmp <= 0) /* error or not equal */
2173 return cmp;
2174 }
2175 }
2176 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002177}
Tim Peterse63415e2001-05-08 04:38:29 +00002178
2179static PyObject *
2180dict_richcompare(PyObject *v, PyObject *w, int op)
2181{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002182 int cmp;
2183 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002184
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002185 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2186 res = Py_NotImplemented;
2187 }
2188 else if (op == Py_EQ || op == Py_NE) {
2189 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2190 if (cmp < 0)
2191 return NULL;
2192 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2193 }
2194 else
2195 res = Py_NotImplemented;
2196 Py_INCREF(res);
2197 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002198}
Tim Peterse63415e2001-05-08 04:38:29 +00002199
Larry Hastings61272b72014-01-07 12:41:53 -08002200/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002201
2202@coexist
2203dict.__contains__
2204
2205 key: object
2206 /
2207
Meador Ingee02de8c2014-01-14 16:48:31 -06002208True if D has a key k, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002209[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002210
2211PyDoc_STRVAR(dict___contains____doc__,
Larry Hastings581ee362014-01-28 05:00:08 -08002212"sig=($self, key)\n"
Meador Ingee02de8c2014-01-14 16:48:31 -06002213"True if D has a key k, else False.");
Larry Hastings31826802013-10-19 00:09:25 -07002214
2215#define DICT___CONTAINS___METHODDEF \
2216 {"__contains__", (PyCFunction)dict___contains__, METH_O|METH_COEXIST, dict___contains____doc__},
2217
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002218static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002219dict___contains__(PyDictObject *self, PyObject *key)
Larry Hastings581ee362014-01-28 05:00:08 -08002220/*[clinic end generated code: output=c654684a6d880281 input=b852b2a19b51ab24]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002221{
Larry Hastingsc2047262014-01-25 20:43:29 -08002222 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002223 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002224 PyDictKeyEntry *ep;
2225 PyObject **value_addr;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002227 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002228 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002229 hash = PyObject_Hash(key);
2230 if (hash == -1)
2231 return NULL;
2232 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002233 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002234 if (ep == NULL)
2235 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002236 return PyBool_FromLong(*value_addr != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002237}
2238
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002239static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002240dict_get(PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00002241{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002242 PyObject *key;
2243 PyObject *failobj = Py_None;
2244 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002245 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002246 PyDictKeyEntry *ep;
2247 PyObject **value_addr;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002249 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
2250 return NULL;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002252 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002253 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002254 hash = PyObject_Hash(key);
2255 if (hash == -1)
2256 return NULL;
2257 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002258 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002259 if (ep == NULL)
2260 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002261 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002262 if (val == NULL)
2263 val = failobj;
2264 Py_INCREF(val);
2265 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002266}
2267
Benjamin Peterson00e98862013-03-07 22:16:29 -05002268PyObject *
2269PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002270{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002271 PyDictObject *mp = (PyDictObject *)d;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002272 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002273 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002274 PyDictKeyEntry *ep;
2275 PyObject **value_addr;
Guido van Rossum164452c2000-08-08 16:12:54 +00002276
Benjamin Peterson00e98862013-03-07 22:16:29 -05002277 if (!PyDict_Check(d)) {
2278 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002279 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002280 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002282 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002283 hash = PyObject_Hash(key);
2284 if (hash == -1)
2285 return NULL;
2286 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002287 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002288 if (ep == NULL)
2289 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002290 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002291 if (val == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002292 if (mp->ma_keys->dk_usable <= 0) {
2293 /* Need to resize. */
2294 if (insertion_resize(mp) < 0)
2295 return NULL;
2296 ep = find_empty_slot(mp, key, hash, &value_addr);
2297 }
Benjamin Peterson00e98862013-03-07 22:16:29 -05002298 Py_INCREF(defaultobj);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002299 Py_INCREF(key);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002300 MAINTAIN_TRACKING(mp, key, defaultobj);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002301 ep->me_key = key;
2302 ep->me_hash = hash;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002303 *value_addr = defaultobj;
2304 val = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002305 mp->ma_keys->dk_usable--;
2306 mp->ma_used++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002308 return val;
Guido van Rossum164452c2000-08-08 16:12:54 +00002309}
2310
Benjamin Peterson00e98862013-03-07 22:16:29 -05002311static PyObject *
2312dict_setdefault(PyDictObject *mp, PyObject *args)
2313{
2314 PyObject *key, *val;
2315 PyObject *defaultobj = Py_None;
2316
2317 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &defaultobj))
2318 return NULL;
2319
Benjamin Peterson55898502013-03-08 08:36:49 -05002320 val = PyDict_SetDefault((PyObject *)mp, key, defaultobj);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002321 Py_XINCREF(val);
2322 return val;
2323}
Guido van Rossum164452c2000-08-08 16:12:54 +00002324
2325static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002326dict_clear(PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002327{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002328 PyDict_Clear((PyObject *)mp);
2329 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002330}
2331
Guido van Rossumba6ab842000-12-12 22:02:18 +00002332static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002333dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002334{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002335 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002336 PyObject *old_value, *old_key;
2337 PyObject *key, *deflt = NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002338 PyDictKeyEntry *ep;
2339 PyObject **value_addr;
Guido van Rossume027d982002-04-12 15:11:59 +00002340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002341 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2342 return NULL;
2343 if (mp->ma_used == 0) {
2344 if (deflt) {
2345 Py_INCREF(deflt);
2346 return deflt;
2347 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07002348 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002349 return NULL;
2350 }
2351 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002352 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002353 hash = PyObject_Hash(key);
2354 if (hash == -1)
2355 return NULL;
2356 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002357 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 if (ep == NULL)
2359 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002360 old_value = *value_addr;
2361 if (old_value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002362 if (deflt) {
2363 Py_INCREF(deflt);
2364 return deflt;
2365 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07002366 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002367 return NULL;
2368 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002369 *value_addr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002370 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002371 if (!_PyDict_HasSplitTable(mp)) {
2372 ENSURE_ALLOWS_DELETIONS(mp);
2373 old_key = ep->me_key;
2374 Py_INCREF(dummy);
2375 ep->me_key = dummy;
2376 Py_DECREF(old_key);
2377 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002378 return old_value;
Guido van Rossume027d982002-04-12 15:11:59 +00002379}
2380
2381static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002382dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00002383{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002384 Py_hash_t i = 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002385 PyDictKeyEntry *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002387
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 /* Allocate the result tuple before checking the size. Believe it
2390 * or not, this allocation could trigger a garbage collection which
2391 * could empty the dict, so if we checked the size first and that
2392 * happened, the result would be an infinite loop (searching for an
2393 * entry that no longer exists). Note that the usual popitem()
2394 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2395 * tuple away if the dict *is* empty isn't a significant
2396 * inefficiency -- possible, but unlikely in practice.
2397 */
2398 res = PyTuple_New(2);
2399 if (res == NULL)
2400 return NULL;
2401 if (mp->ma_used == 0) {
2402 Py_DECREF(res);
2403 PyErr_SetString(PyExc_KeyError,
2404 "popitem(): dictionary is empty");
2405 return NULL;
2406 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002407 /* Convert split table to combined table */
2408 if (mp->ma_keys->dk_lookup == lookdict_split) {
2409 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
2410 Py_DECREF(res);
2411 return NULL;
2412 }
2413 }
2414 ENSURE_ALLOWS_DELETIONS(mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002415 /* Set ep to "the first" dict entry with a value. We abuse the hash
2416 * field of slot 0 to hold a search finger:
2417 * If slot 0 has a value, use slot 0.
2418 * Else slot 0 is being used to hold a search finger,
2419 * and we use its hash value as the first index to look.
2420 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002421 ep = &mp->ma_keys->dk_entries[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002422 if (ep->me_value == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002423 Py_ssize_t mask = DK_MASK(mp->ma_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002424 i = ep->me_hash;
2425 /* The hash field may be a real hash value, or it may be a
2426 * legit search finger, or it may be a once-legit search
2427 * finger that's out of bounds now because it wrapped around
2428 * or the table shrunk -- simply make sure it's in bounds now.
2429 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002430 if (i > mask || i < 1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002431 i = 1; /* skip slot 0 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002432 while ((ep = &mp->ma_keys->dk_entries[i])->me_value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002433 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002434 if (i > mask)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002435 i = 1;
2436 }
2437 }
2438 PyTuple_SET_ITEM(res, 0, ep->me_key);
2439 PyTuple_SET_ITEM(res, 1, ep->me_value);
2440 Py_INCREF(dummy);
2441 ep->me_key = dummy;
2442 ep->me_value = NULL;
2443 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002444 assert(mp->ma_keys->dk_entries[0].me_value == NULL);
2445 mp->ma_keys->dk_entries[0].me_hash = i + 1; /* next place to start */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002446 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002447}
2448
Jeremy Hylton8caad492000-06-23 14:18:11 +00002449static int
2450dict_traverse(PyObject *op, visitproc visit, void *arg)
2451{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002452 Py_ssize_t i, n;
2453 PyDictObject *mp = (PyDictObject *)op;
2454 if (mp->ma_keys->dk_lookup == lookdict) {
2455 for (i = 0; i < DK_SIZE(mp->ma_keys); i++) {
2456 if (mp->ma_keys->dk_entries[i].me_value != NULL) {
2457 Py_VISIT(mp->ma_keys->dk_entries[i].me_value);
2458 Py_VISIT(mp->ma_keys->dk_entries[i].me_key);
2459 }
2460 }
2461 } else {
2462 if (mp->ma_values != NULL) {
2463 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2464 Py_VISIT(mp->ma_values[i]);
2465 }
2466 }
2467 else {
2468 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2469 Py_VISIT(mp->ma_keys->dk_entries[i].me_value);
2470 }
2471 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002472 }
2473 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002474}
2475
2476static int
2477dict_tp_clear(PyObject *op)
2478{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002479 PyDict_Clear(op);
2480 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002481}
2482
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002483static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002484
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002485static PyObject *
2486dict_sizeof(PyDictObject *mp)
2487{
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002488 Py_ssize_t size, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002489
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002490 size = DK_SIZE(mp->ma_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002491 res = sizeof(PyDictObject);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002492 if (mp->ma_values)
2493 res += size * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002494 /* If the dictionary is split, the keys portion is accounted-for
2495 in the type object. */
2496 if (mp->ma_keys->dk_refcnt == 1)
2497 res += sizeof(PyDictKeysObject) + (size-1) * sizeof(PyDictKeyEntry);
2498 return PyLong_FromSsize_t(res);
2499}
2500
2501Py_ssize_t
2502_PyDict_KeysSize(PyDictKeysObject *keys)
2503{
2504 return sizeof(PyDictKeysObject) + (DK_SIZE(keys)-1) * sizeof(PyDictKeyEntry);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002505}
2506
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002507PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
2508
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002509PyDoc_STRVAR(sizeof__doc__,
2510"D.__sizeof__() -> size of D in memory, in bytes");
2511
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002512PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002513"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002514
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002515PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002516"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 +00002517
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002518PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002519"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002520If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00002521
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002522PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00002523"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000025242-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002525
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002526PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04002527"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
2528If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
2529If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
2530In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00002531
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002532PyDoc_STRVAR(clear__doc__,
2533"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002534
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002535PyDoc_STRVAR(copy__doc__,
2536"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00002537
Guido van Rossumb90c8482007-02-10 01:11:45 +00002538/* Forward */
2539static PyObject *dictkeys_new(PyObject *);
2540static PyObject *dictitems_new(PyObject *);
2541static PyObject *dictvalues_new(PyObject *);
2542
Guido van Rossum45c85d12007-07-27 16:31:40 +00002543PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002544 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002545PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002546 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002547PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002548 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00002549
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002550static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07002551 DICT___CONTAINS___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002552 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
2553 getitem__doc__},
2554 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
2555 sizeof__doc__},
2556 {"get", (PyCFunction)dict_get, METH_VARARGS,
2557 get__doc__},
2558 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
2559 setdefault_doc__},
2560 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
2561 pop__doc__},
2562 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
2563 popitem__doc__},
2564 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
2565 keys__doc__},
2566 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
2567 items__doc__},
2568 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
2569 values__doc__},
2570 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
2571 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08002572 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002573 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
2574 clear__doc__},
2575 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
2576 copy__doc__},
2577 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002578};
2579
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002580/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002581int
2582PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002583{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002584 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002585 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002586 PyDictKeyEntry *ep;
2587 PyObject **value_addr;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002589 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002590 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002591 hash = PyObject_Hash(key);
2592 if (hash == -1)
2593 return -1;
2594 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002595 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
2596 return (ep == NULL) ? -1 : (*value_addr != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002597}
2598
Thomas Wouterscf297e42007-02-23 15:07:44 +00002599/* Internal version of PyDict_Contains used when the hash value is already known */
2600int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002601_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00002602{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002603 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002604 PyDictKeyEntry *ep;
2605 PyObject **value_addr;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002606
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002607 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
2608 return (ep == NULL) ? -1 : (*value_addr != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00002609}
2610
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002611/* Hack to implement "key in dict" */
2612static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002613 0, /* sq_length */
2614 0, /* sq_concat */
2615 0, /* sq_repeat */
2616 0, /* sq_item */
2617 0, /* sq_slice */
2618 0, /* sq_ass_item */
2619 0, /* sq_ass_slice */
2620 PyDict_Contains, /* sq_contains */
2621 0, /* sq_inplace_concat */
2622 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002623};
2624
Guido van Rossum09e563a2001-05-01 12:10:21 +00002625static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002626dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2627{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002628 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02002629 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002631 assert(type != NULL && type->tp_alloc != NULL);
2632 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02002633 if (self == NULL)
2634 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02002635 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02002636
Victor Stinnera9f61a52013-07-16 22:17:26 +02002637 /* The object has been implicitly tracked by tp_alloc */
2638 if (type == &PyDict_Type)
2639 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02002640
2641 d->ma_used = 0;
2642 d->ma_keys = new_keys_object(PyDict_MINSIZE_COMBINED);
2643 if (d->ma_keys == NULL) {
2644 Py_DECREF(self);
2645 return NULL;
2646 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002647 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002648}
2649
Tim Peters25786c02001-09-02 08:22:48 +00002650static int
2651dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2652{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002653 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002654}
2655
Tim Peters6d6c1a32001-08-02 04:15:00 +00002656static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002657dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002658{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002659 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002660}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002661
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002662PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002663"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002664"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002665" (key, value) pairs\n"
2666"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002667" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002668" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002669" d[k] = v\n"
2670"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2671" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002672
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002673PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002674 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2675 "dict",
2676 sizeof(PyDictObject),
2677 0,
2678 (destructor)dict_dealloc, /* tp_dealloc */
2679 0, /* tp_print */
2680 0, /* tp_getattr */
2681 0, /* tp_setattr */
2682 0, /* tp_reserved */
2683 (reprfunc)dict_repr, /* tp_repr */
2684 0, /* tp_as_number */
2685 &dict_as_sequence, /* tp_as_sequence */
2686 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002687 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002688 0, /* tp_call */
2689 0, /* tp_str */
2690 PyObject_GenericGetAttr, /* tp_getattro */
2691 0, /* tp_setattro */
2692 0, /* tp_as_buffer */
2693 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2694 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
2695 dictionary_doc, /* tp_doc */
2696 dict_traverse, /* tp_traverse */
2697 dict_tp_clear, /* tp_clear */
2698 dict_richcompare, /* tp_richcompare */
2699 0, /* tp_weaklistoffset */
2700 (getiterfunc)dict_iter, /* tp_iter */
2701 0, /* tp_iternext */
2702 mapp_methods, /* tp_methods */
2703 0, /* tp_members */
2704 0, /* tp_getset */
2705 0, /* tp_base */
2706 0, /* tp_dict */
2707 0, /* tp_descr_get */
2708 0, /* tp_descr_set */
2709 0, /* tp_dictoffset */
2710 dict_init, /* tp_init */
2711 PyType_GenericAlloc, /* tp_alloc */
2712 dict_new, /* tp_new */
2713 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002714};
2715
Victor Stinner3c1e4812012-03-26 22:10:51 +02002716PyObject *
2717_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
2718{
2719 PyObject *kv;
2720 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02002721 if (kv == NULL) {
2722 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002723 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02002724 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02002725 return PyDict_GetItem(dp, kv);
2726}
2727
Guido van Rossum3cca2451997-05-16 14:23:33 +00002728/* For backward compatibility with old dictionary interface */
2729
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002730PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002731PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002732{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002733 PyObject *kv, *rv;
2734 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02002735 if (kv == NULL) {
2736 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002737 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02002738 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002739 rv = PyDict_GetItem(v, kv);
2740 Py_DECREF(kv);
2741 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002742}
2743
2744int
Victor Stinner3c1e4812012-03-26 22:10:51 +02002745_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
2746{
2747 PyObject *kv;
2748 kv = _PyUnicode_FromId(key); /* borrowed */
2749 if (kv == NULL)
2750 return -1;
2751 return PyDict_SetItem(v, kv, item);
2752}
2753
2754int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002755PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002756{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 PyObject *kv;
2758 int err;
2759 kv = PyUnicode_FromString(key);
2760 if (kv == NULL)
2761 return -1;
2762 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
2763 err = PyDict_SetItem(v, kv, item);
2764 Py_DECREF(kv);
2765 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002766}
2767
2768int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01002769_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
2770{
2771 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
2772 if (kv == NULL)
2773 return -1;
2774 return PyDict_DelItem(v, kv);
2775}
2776
2777int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002778PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002779{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002780 PyObject *kv;
2781 int err;
2782 kv = PyUnicode_FromString(key);
2783 if (kv == NULL)
2784 return -1;
2785 err = PyDict_DelItem(v, kv);
2786 Py_DECREF(kv);
2787 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002788}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002789
Raymond Hettinger019a1482004-03-18 02:41:19 +00002790/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002791
2792typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002793 PyObject_HEAD
2794 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
2795 Py_ssize_t di_used;
2796 Py_ssize_t di_pos;
2797 PyObject* di_result; /* reusable result tuple for iteritems */
2798 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002799} dictiterobject;
2800
2801static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002802dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002803{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002804 dictiterobject *di;
2805 di = PyObject_GC_New(dictiterobject, itertype);
2806 if (di == NULL)
2807 return NULL;
2808 Py_INCREF(dict);
2809 di->di_dict = dict;
2810 di->di_used = dict->ma_used;
2811 di->di_pos = 0;
2812 di->len = dict->ma_used;
2813 if (itertype == &PyDictIterItem_Type) {
2814 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2815 if (di->di_result == NULL) {
2816 Py_DECREF(di);
2817 return NULL;
2818 }
2819 }
2820 else
2821 di->di_result = NULL;
2822 _PyObject_GC_TRACK(di);
2823 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002824}
2825
2826static void
2827dictiter_dealloc(dictiterobject *di)
2828{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002829 Py_XDECREF(di->di_dict);
2830 Py_XDECREF(di->di_result);
2831 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002832}
2833
2834static int
2835dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
2836{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002837 Py_VISIT(di->di_dict);
2838 Py_VISIT(di->di_result);
2839 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002840}
2841
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002842static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002843dictiter_len(dictiterobject *di)
2844{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002845 Py_ssize_t len = 0;
2846 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
2847 len = di->len;
2848 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002849}
2850
Guido van Rossumb90c8482007-02-10 01:11:45 +00002851PyDoc_STRVAR(length_hint_doc,
2852 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002853
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002854static PyObject *
2855dictiter_reduce(dictiterobject *di);
2856
2857PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
2858
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002859static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002860 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
2861 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002862 {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS,
2863 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002864 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002865};
2866
Raymond Hettinger019a1482004-03-18 02:41:19 +00002867static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002868{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002869 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002870 Py_ssize_t i, mask, offset;
2871 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002872 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002873 PyObject **value_ptr;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002875 if (d == NULL)
2876 return NULL;
2877 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 if (di->di_used != d->ma_used) {
2880 PyErr_SetString(PyExc_RuntimeError,
2881 "dictionary changed size during iteration");
2882 di->di_used = -1; /* Make this state sticky */
2883 return NULL;
2884 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002886 i = di->di_pos;
2887 if (i < 0)
2888 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002889 k = d->ma_keys;
2890 if (d->ma_values) {
2891 value_ptr = &d->ma_values[i];
2892 offset = sizeof(PyObject *);
2893 }
2894 else {
2895 value_ptr = &k->dk_entries[i].me_value;
2896 offset = sizeof(PyDictKeyEntry);
2897 }
2898 mask = DK_SIZE(k)-1;
2899 while (i <= mask && *value_ptr == NULL) {
2900 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002902 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002903 di->di_pos = i+1;
2904 if (i > mask)
2905 goto fail;
2906 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002907 key = k->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002908 Py_INCREF(key);
2909 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002910
2911fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002912 Py_DECREF(d);
2913 di->di_dict = NULL;
2914 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002915}
2916
Raymond Hettinger019a1482004-03-18 02:41:19 +00002917PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002918 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2919 "dict_keyiterator", /* tp_name */
2920 sizeof(dictiterobject), /* tp_basicsize */
2921 0, /* tp_itemsize */
2922 /* methods */
2923 (destructor)dictiter_dealloc, /* tp_dealloc */
2924 0, /* tp_print */
2925 0, /* tp_getattr */
2926 0, /* tp_setattr */
2927 0, /* tp_reserved */
2928 0, /* tp_repr */
2929 0, /* tp_as_number */
2930 0, /* tp_as_sequence */
2931 0, /* tp_as_mapping */
2932 0, /* tp_hash */
2933 0, /* tp_call */
2934 0, /* tp_str */
2935 PyObject_GenericGetAttr, /* tp_getattro */
2936 0, /* tp_setattro */
2937 0, /* tp_as_buffer */
2938 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2939 0, /* tp_doc */
2940 (traverseproc)dictiter_traverse, /* tp_traverse */
2941 0, /* tp_clear */
2942 0, /* tp_richcompare */
2943 0, /* tp_weaklistoffset */
2944 PyObject_SelfIter, /* tp_iter */
2945 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
2946 dictiter_methods, /* tp_methods */
2947 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002948};
2949
2950static PyObject *dictiter_iternextvalue(dictiterobject *di)
2951{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002952 PyObject *value;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002953 Py_ssize_t i, mask, offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002954 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002955 PyObject **value_ptr;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002957 if (d == NULL)
2958 return NULL;
2959 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00002960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002961 if (di->di_used != d->ma_used) {
2962 PyErr_SetString(PyExc_RuntimeError,
2963 "dictionary changed size during iteration");
2964 di->di_used = -1; /* Make this state sticky */
2965 return NULL;
2966 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002968 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002969 mask = DK_SIZE(d->ma_keys)-1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002970 if (i < 0 || i > mask)
2971 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002972 if (d->ma_values) {
2973 value_ptr = &d->ma_values[i];
2974 offset = sizeof(PyObject *);
2975 }
2976 else {
2977 value_ptr = &d->ma_keys->dk_entries[i].me_value;
2978 offset = sizeof(PyDictKeyEntry);
2979 }
2980 while (i <= mask && *value_ptr == NULL) {
2981 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002982 i++;
2983 if (i > mask)
2984 goto fail;
2985 }
2986 di->di_pos = i+1;
2987 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002988 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002989 Py_INCREF(value);
2990 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002991
2992fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002993 Py_DECREF(d);
2994 di->di_dict = NULL;
2995 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002996}
2997
2998PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002999 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3000 "dict_valueiterator", /* tp_name */
3001 sizeof(dictiterobject), /* tp_basicsize */
3002 0, /* tp_itemsize */
3003 /* methods */
3004 (destructor)dictiter_dealloc, /* tp_dealloc */
3005 0, /* tp_print */
3006 0, /* tp_getattr */
3007 0, /* tp_setattr */
3008 0, /* tp_reserved */
3009 0, /* tp_repr */
3010 0, /* tp_as_number */
3011 0, /* tp_as_sequence */
3012 0, /* tp_as_mapping */
3013 0, /* tp_hash */
3014 0, /* tp_call */
3015 0, /* tp_str */
3016 PyObject_GenericGetAttr, /* tp_getattro */
3017 0, /* tp_setattro */
3018 0, /* tp_as_buffer */
3019 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3020 0, /* tp_doc */
3021 (traverseproc)dictiter_traverse, /* tp_traverse */
3022 0, /* tp_clear */
3023 0, /* tp_richcompare */
3024 0, /* tp_weaklistoffset */
3025 PyObject_SelfIter, /* tp_iter */
3026 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3027 dictiter_methods, /* tp_methods */
3028 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003029};
3030
3031static PyObject *dictiter_iternextitem(dictiterobject *di)
3032{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003033 PyObject *key, *value, *result = di->di_result;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003034 Py_ssize_t i, mask, offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003035 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003036 PyObject **value_ptr;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003038 if (d == NULL)
3039 return NULL;
3040 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003042 if (di->di_used != d->ma_used) {
3043 PyErr_SetString(PyExc_RuntimeError,
3044 "dictionary changed size during iteration");
3045 di->di_used = -1; /* Make this state sticky */
3046 return NULL;
3047 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003049 i = di->di_pos;
3050 if (i < 0)
3051 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003052 mask = DK_SIZE(d->ma_keys)-1;
3053 if (d->ma_values) {
3054 value_ptr = &d->ma_values[i];
3055 offset = sizeof(PyObject *);
3056 }
3057 else {
3058 value_ptr = &d->ma_keys->dk_entries[i].me_value;
3059 offset = sizeof(PyDictKeyEntry);
3060 }
3061 while (i <= mask && *value_ptr == NULL) {
3062 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003063 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003064 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003065 di->di_pos = i+1;
3066 if (i > mask)
3067 goto fail;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003068
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003069 if (result->ob_refcnt == 1) {
3070 Py_INCREF(result);
3071 Py_DECREF(PyTuple_GET_ITEM(result, 0));
3072 Py_DECREF(PyTuple_GET_ITEM(result, 1));
3073 } else {
3074 result = PyTuple_New(2);
3075 if (result == NULL)
3076 return NULL;
3077 }
3078 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003079 key = d->ma_keys->dk_entries[i].me_key;
3080 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003081 Py_INCREF(key);
3082 Py_INCREF(value);
3083 PyTuple_SET_ITEM(result, 0, key);
3084 PyTuple_SET_ITEM(result, 1, value);
3085 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003086
3087fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003088 Py_DECREF(d);
3089 di->di_dict = NULL;
3090 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003091}
3092
3093PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003094 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3095 "dict_itemiterator", /* tp_name */
3096 sizeof(dictiterobject), /* tp_basicsize */
3097 0, /* tp_itemsize */
3098 /* methods */
3099 (destructor)dictiter_dealloc, /* tp_dealloc */
3100 0, /* tp_print */
3101 0, /* tp_getattr */
3102 0, /* tp_setattr */
3103 0, /* tp_reserved */
3104 0, /* tp_repr */
3105 0, /* tp_as_number */
3106 0, /* tp_as_sequence */
3107 0, /* tp_as_mapping */
3108 0, /* tp_hash */
3109 0, /* tp_call */
3110 0, /* tp_str */
3111 PyObject_GenericGetAttr, /* tp_getattro */
3112 0, /* tp_setattro */
3113 0, /* tp_as_buffer */
3114 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3115 0, /* tp_doc */
3116 (traverseproc)dictiter_traverse, /* tp_traverse */
3117 0, /* tp_clear */
3118 0, /* tp_richcompare */
3119 0, /* tp_weaklistoffset */
3120 PyObject_SelfIter, /* tp_iter */
3121 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3122 dictiter_methods, /* tp_methods */
3123 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003124};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003125
3126
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003127static PyObject *
3128dictiter_reduce(dictiterobject *di)
3129{
3130 PyObject *list;
3131 dictiterobject tmp;
3132
3133 list = PyList_New(0);
3134 if (!list)
3135 return NULL;
3136
3137 /* copy the itertor state */
3138 tmp = *di;
3139 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003140
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003141 /* iterate the temporary into a list */
3142 for(;;) {
3143 PyObject *element = 0;
3144 if (Py_TYPE(di) == &PyDictIterItem_Type)
3145 element = dictiter_iternextitem(&tmp);
3146 else if (Py_TYPE(di) == &PyDictIterKey_Type)
3147 element = dictiter_iternextkey(&tmp);
3148 else if (Py_TYPE(di) == &PyDictIterValue_Type)
3149 element = dictiter_iternextvalue(&tmp);
3150 else
3151 assert(0);
3152 if (element) {
3153 if (PyList_Append(list, element)) {
3154 Py_DECREF(element);
3155 Py_DECREF(list);
3156 Py_XDECREF(tmp.di_dict);
3157 return NULL;
3158 }
3159 Py_DECREF(element);
3160 } else
3161 break;
3162 }
3163 Py_XDECREF(tmp.di_dict);
3164 /* check for error */
3165 if (tmp.di_dict != NULL) {
3166 /* we have an error */
3167 Py_DECREF(list);
3168 return NULL;
3169 }
Antoine Pitroua7013882012-04-05 00:04:20 +02003170 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003171}
3172
Guido van Rossum3ac67412007-02-10 18:55:06 +00003173/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003174/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003175/***********************************************/
3176
Guido van Rossumb90c8482007-02-10 01:11:45 +00003177/* The instance lay-out is the same for all three; but the type differs. */
3178
3179typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003180 PyObject_HEAD
3181 PyDictObject *dv_dict;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003182} dictviewobject;
3183
3184
3185static void
Guido van Rossum3ac67412007-02-10 18:55:06 +00003186dictview_dealloc(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003187{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003188 Py_XDECREF(dv->dv_dict);
3189 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003190}
3191
3192static int
3193dictview_traverse(dictviewobject *dv, visitproc visit, void *arg)
3194{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003195 Py_VISIT(dv->dv_dict);
3196 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003197}
3198
Guido van Rossum83825ac2007-02-10 04:54:19 +00003199static Py_ssize_t
Guido van Rossum3ac67412007-02-10 18:55:06 +00003200dictview_len(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003202 Py_ssize_t len = 0;
3203 if (dv->dv_dict != NULL)
3204 len = dv->dv_dict->ma_used;
3205 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003206}
3207
3208static PyObject *
3209dictview_new(PyObject *dict, PyTypeObject *type)
3210{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003211 dictviewobject *dv;
3212 if (dict == NULL) {
3213 PyErr_BadInternalCall();
3214 return NULL;
3215 }
3216 if (!PyDict_Check(dict)) {
3217 /* XXX Get rid of this restriction later */
3218 PyErr_Format(PyExc_TypeError,
3219 "%s() requires a dict argument, not '%s'",
3220 type->tp_name, dict->ob_type->tp_name);
3221 return NULL;
3222 }
3223 dv = PyObject_GC_New(dictviewobject, type);
3224 if (dv == NULL)
3225 return NULL;
3226 Py_INCREF(dict);
3227 dv->dv_dict = (PyDictObject *)dict;
3228 _PyObject_GC_TRACK(dv);
3229 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003230}
3231
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003232/* TODO(guido): The views objects are not complete:
3233
3234 * support more set operations
3235 * support arbitrary mappings?
3236 - either these should be static or exported in dictobject.h
3237 - if public then they should probably be in builtins
3238*/
3239
Guido van Rossumaac530c2007-08-24 22:33:45 +00003240/* Return 1 if self is a subset of other, iterating over self;
3241 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003242static int
3243all_contained_in(PyObject *self, PyObject *other)
3244{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003245 PyObject *iter = PyObject_GetIter(self);
3246 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003248 if (iter == NULL)
3249 return -1;
3250 for (;;) {
3251 PyObject *next = PyIter_Next(iter);
3252 if (next == NULL) {
3253 if (PyErr_Occurred())
3254 ok = -1;
3255 break;
3256 }
3257 ok = PySequence_Contains(other, next);
3258 Py_DECREF(next);
3259 if (ok <= 0)
3260 break;
3261 }
3262 Py_DECREF(iter);
3263 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003264}
3265
3266static PyObject *
3267dictview_richcompare(PyObject *self, PyObject *other, int op)
3268{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003269 Py_ssize_t len_self, len_other;
3270 int ok;
3271 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003273 assert(self != NULL);
3274 assert(PyDictViewSet_Check(self));
3275 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00003276
Brian Curtindfc80e32011-08-10 20:28:54 -05003277 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
3278 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003280 len_self = PyObject_Size(self);
3281 if (len_self < 0)
3282 return NULL;
3283 len_other = PyObject_Size(other);
3284 if (len_other < 0)
3285 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003287 ok = 0;
3288 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00003289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003290 case Py_NE:
3291 case Py_EQ:
3292 if (len_self == len_other)
3293 ok = all_contained_in(self, other);
3294 if (op == Py_NE && ok >= 0)
3295 ok = !ok;
3296 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003298 case Py_LT:
3299 if (len_self < len_other)
3300 ok = all_contained_in(self, other);
3301 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003303 case Py_LE:
3304 if (len_self <= len_other)
3305 ok = all_contained_in(self, other);
3306 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003308 case Py_GT:
3309 if (len_self > len_other)
3310 ok = all_contained_in(other, self);
3311 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003313 case Py_GE:
3314 if (len_self >= len_other)
3315 ok = all_contained_in(other, self);
3316 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003318 }
3319 if (ok < 0)
3320 return NULL;
3321 result = ok ? Py_True : Py_False;
3322 Py_INCREF(result);
3323 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003324}
3325
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003326static PyObject *
3327dictview_repr(dictviewobject *dv)
3328{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003329 PyObject *seq;
3330 PyObject *result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003332 seq = PySequence_List((PyObject *)dv);
3333 if (seq == NULL)
3334 return NULL;
3335
3336 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
3337 Py_DECREF(seq);
3338 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003339}
3340
Guido van Rossum3ac67412007-02-10 18:55:06 +00003341/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003342
3343static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003344dictkeys_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003345{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003346 if (dv->dv_dict == NULL) {
3347 Py_RETURN_NONE;
3348 }
3349 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003350}
3351
3352static int
3353dictkeys_contains(dictviewobject *dv, PyObject *obj)
3354{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003355 if (dv->dv_dict == NULL)
3356 return 0;
3357 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003358}
3359
Guido van Rossum83825ac2007-02-10 04:54:19 +00003360static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003361 (lenfunc)dictview_len, /* sq_length */
3362 0, /* sq_concat */
3363 0, /* sq_repeat */
3364 0, /* sq_item */
3365 0, /* sq_slice */
3366 0, /* sq_ass_item */
3367 0, /* sq_ass_slice */
3368 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003369};
3370
Guido van Rossum523259b2007-08-24 23:41:22 +00003371static PyObject*
3372dictviews_sub(PyObject* self, PyObject *other)
3373{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003374 PyObject *result = PySet_New(self);
3375 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003376 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003378 if (result == NULL)
3379 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003380
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003381 tmp = _PyObject_CallMethodId(result, &PyId_difference_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003382 if (tmp == NULL) {
3383 Py_DECREF(result);
3384 return NULL;
3385 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003387 Py_DECREF(tmp);
3388 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003389}
3390
3391static PyObject*
3392dictviews_and(PyObject* self, PyObject *other)
3393{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003394 PyObject *result = PySet_New(self);
3395 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003396 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003398 if (result == NULL)
3399 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003400
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003401 tmp = _PyObject_CallMethodId(result, &PyId_intersection_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003402 if (tmp == NULL) {
3403 Py_DECREF(result);
3404 return NULL;
3405 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003407 Py_DECREF(tmp);
3408 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003409}
3410
3411static PyObject*
3412dictviews_or(PyObject* self, PyObject *other)
3413{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003414 PyObject *result = PySet_New(self);
3415 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003416 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003418 if (result == NULL)
3419 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003420
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003421 tmp = _PyObject_CallMethodId(result, &PyId_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003422 if (tmp == NULL) {
3423 Py_DECREF(result);
3424 return NULL;
3425 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003427 Py_DECREF(tmp);
3428 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003429}
3430
3431static PyObject*
3432dictviews_xor(PyObject* self, PyObject *other)
3433{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003434 PyObject *result = PySet_New(self);
3435 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003436 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003438 if (result == NULL)
3439 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003440
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003441 tmp = _PyObject_CallMethodId(result, &PyId_symmetric_difference_update, "O",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003442 other);
3443 if (tmp == NULL) {
3444 Py_DECREF(result);
3445 return NULL;
3446 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003448 Py_DECREF(tmp);
3449 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003450}
3451
3452static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003453 0, /*nb_add*/
3454 (binaryfunc)dictviews_sub, /*nb_subtract*/
3455 0, /*nb_multiply*/
3456 0, /*nb_remainder*/
3457 0, /*nb_divmod*/
3458 0, /*nb_power*/
3459 0, /*nb_negative*/
3460 0, /*nb_positive*/
3461 0, /*nb_absolute*/
3462 0, /*nb_bool*/
3463 0, /*nb_invert*/
3464 0, /*nb_lshift*/
3465 0, /*nb_rshift*/
3466 (binaryfunc)dictviews_and, /*nb_and*/
3467 (binaryfunc)dictviews_xor, /*nb_xor*/
3468 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00003469};
3470
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003471static PyObject*
3472dictviews_isdisjoint(PyObject *self, PyObject *other)
3473{
3474 PyObject *it;
3475 PyObject *item = NULL;
3476
3477 if (self == other) {
3478 if (dictview_len((dictviewobject *)self) == 0)
3479 Py_RETURN_TRUE;
3480 else
3481 Py_RETURN_FALSE;
3482 }
3483
3484 /* Iterate over the shorter object (only if other is a set,
3485 * because PySequence_Contains may be expensive otherwise): */
3486 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
3487 Py_ssize_t len_self = dictview_len((dictviewobject *)self);
3488 Py_ssize_t len_other = PyObject_Size(other);
3489 if (len_other == -1)
3490 return NULL;
3491
3492 if ((len_other > len_self)) {
3493 PyObject *tmp = other;
3494 other = self;
3495 self = tmp;
3496 }
3497 }
3498
3499 it = PyObject_GetIter(other);
3500 if (it == NULL)
3501 return NULL;
3502
3503 while ((item = PyIter_Next(it)) != NULL) {
3504 int contains = PySequence_Contains(self, item);
3505 Py_DECREF(item);
3506 if (contains == -1) {
3507 Py_DECREF(it);
3508 return NULL;
3509 }
3510
3511 if (contains) {
3512 Py_DECREF(it);
3513 Py_RETURN_FALSE;
3514 }
3515 }
3516 Py_DECREF(it);
3517 if (PyErr_Occurred())
3518 return NULL; /* PyIter_Next raised an exception. */
3519 Py_RETURN_TRUE;
3520}
3521
3522PyDoc_STRVAR(isdisjoint_doc,
3523"Return True if the view and the given iterable have a null intersection.");
3524
Guido van Rossumb90c8482007-02-10 01:11:45 +00003525static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003526 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3527 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003528 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003529};
3530
3531PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003532 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3533 "dict_keys", /* tp_name */
3534 sizeof(dictviewobject), /* tp_basicsize */
3535 0, /* tp_itemsize */
3536 /* methods */
3537 (destructor)dictview_dealloc, /* tp_dealloc */
3538 0, /* tp_print */
3539 0, /* tp_getattr */
3540 0, /* tp_setattr */
3541 0, /* tp_reserved */
3542 (reprfunc)dictview_repr, /* tp_repr */
3543 &dictviews_as_number, /* tp_as_number */
3544 &dictkeys_as_sequence, /* tp_as_sequence */
3545 0, /* tp_as_mapping */
3546 0, /* tp_hash */
3547 0, /* tp_call */
3548 0, /* tp_str */
3549 PyObject_GenericGetAttr, /* tp_getattro */
3550 0, /* tp_setattro */
3551 0, /* tp_as_buffer */
3552 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3553 0, /* tp_doc */
3554 (traverseproc)dictview_traverse, /* tp_traverse */
3555 0, /* tp_clear */
3556 dictview_richcompare, /* tp_richcompare */
3557 0, /* tp_weaklistoffset */
3558 (getiterfunc)dictkeys_iter, /* tp_iter */
3559 0, /* tp_iternext */
3560 dictkeys_methods, /* tp_methods */
3561 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003562};
3563
3564static PyObject *
3565dictkeys_new(PyObject *dict)
3566{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003567 return dictview_new(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003568}
3569
Guido van Rossum3ac67412007-02-10 18:55:06 +00003570/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003571
3572static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003573dictitems_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003574{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003575 if (dv->dv_dict == NULL) {
3576 Py_RETURN_NONE;
3577 }
3578 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003579}
3580
3581static int
3582dictitems_contains(dictviewobject *dv, PyObject *obj)
3583{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003584 PyObject *key, *value, *found;
3585 if (dv->dv_dict == NULL)
3586 return 0;
3587 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
3588 return 0;
3589 key = PyTuple_GET_ITEM(obj, 0);
3590 value = PyTuple_GET_ITEM(obj, 1);
3591 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
3592 if (found == NULL) {
3593 if (PyErr_Occurred())
3594 return -1;
3595 return 0;
3596 }
3597 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003598}
3599
Guido van Rossum83825ac2007-02-10 04:54:19 +00003600static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003601 (lenfunc)dictview_len, /* sq_length */
3602 0, /* sq_concat */
3603 0, /* sq_repeat */
3604 0, /* sq_item */
3605 0, /* sq_slice */
3606 0, /* sq_ass_item */
3607 0, /* sq_ass_slice */
3608 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003609};
3610
Guido van Rossumb90c8482007-02-10 01:11:45 +00003611static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003612 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3613 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003614 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003615};
3616
3617PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003618 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3619 "dict_items", /* tp_name */
3620 sizeof(dictviewobject), /* tp_basicsize */
3621 0, /* tp_itemsize */
3622 /* methods */
3623 (destructor)dictview_dealloc, /* tp_dealloc */
3624 0, /* tp_print */
3625 0, /* tp_getattr */
3626 0, /* tp_setattr */
3627 0, /* tp_reserved */
3628 (reprfunc)dictview_repr, /* tp_repr */
3629 &dictviews_as_number, /* tp_as_number */
3630 &dictitems_as_sequence, /* tp_as_sequence */
3631 0, /* tp_as_mapping */
3632 0, /* tp_hash */
3633 0, /* tp_call */
3634 0, /* tp_str */
3635 PyObject_GenericGetAttr, /* tp_getattro */
3636 0, /* tp_setattro */
3637 0, /* tp_as_buffer */
3638 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3639 0, /* tp_doc */
3640 (traverseproc)dictview_traverse, /* tp_traverse */
3641 0, /* tp_clear */
3642 dictview_richcompare, /* tp_richcompare */
3643 0, /* tp_weaklistoffset */
3644 (getiterfunc)dictitems_iter, /* tp_iter */
3645 0, /* tp_iternext */
3646 dictitems_methods, /* tp_methods */
3647 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003648};
3649
3650static PyObject *
3651dictitems_new(PyObject *dict)
3652{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003653 return dictview_new(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003654}
3655
Guido van Rossum3ac67412007-02-10 18:55:06 +00003656/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003657
3658static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003659dictvalues_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003660{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003661 if (dv->dv_dict == NULL) {
3662 Py_RETURN_NONE;
3663 }
3664 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003665}
3666
Guido van Rossum83825ac2007-02-10 04:54:19 +00003667static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003668 (lenfunc)dictview_len, /* sq_length */
3669 0, /* sq_concat */
3670 0, /* sq_repeat */
3671 0, /* sq_item */
3672 0, /* sq_slice */
3673 0, /* sq_ass_item */
3674 0, /* sq_ass_slice */
3675 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003676};
3677
Guido van Rossumb90c8482007-02-10 01:11:45 +00003678static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003679 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003680};
3681
3682PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003683 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3684 "dict_values", /* tp_name */
3685 sizeof(dictviewobject), /* tp_basicsize */
3686 0, /* tp_itemsize */
3687 /* methods */
3688 (destructor)dictview_dealloc, /* tp_dealloc */
3689 0, /* tp_print */
3690 0, /* tp_getattr */
3691 0, /* tp_setattr */
3692 0, /* tp_reserved */
3693 (reprfunc)dictview_repr, /* tp_repr */
3694 0, /* tp_as_number */
3695 &dictvalues_as_sequence, /* tp_as_sequence */
3696 0, /* tp_as_mapping */
3697 0, /* tp_hash */
3698 0, /* tp_call */
3699 0, /* tp_str */
3700 PyObject_GenericGetAttr, /* tp_getattro */
3701 0, /* tp_setattro */
3702 0, /* tp_as_buffer */
3703 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3704 0, /* tp_doc */
3705 (traverseproc)dictview_traverse, /* tp_traverse */
3706 0, /* tp_clear */
3707 0, /* tp_richcompare */
3708 0, /* tp_weaklistoffset */
3709 (getiterfunc)dictvalues_iter, /* tp_iter */
3710 0, /* tp_iternext */
3711 dictvalues_methods, /* tp_methods */
3712 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003713};
3714
3715static PyObject *
3716dictvalues_new(PyObject *dict)
3717{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003718 return dictview_new(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003719}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003720
3721/* Returns NULL if cannot allocate a new PyDictKeysObject,
3722 but does not set an error */
3723PyDictKeysObject *
3724_PyDict_NewKeysForClass(void)
3725{
3726 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE_SPLIT);
3727 if (keys == NULL)
3728 PyErr_Clear();
3729 else
3730 keys->dk_lookup = lookdict_split;
3731 return keys;
3732}
3733
3734#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
3735
3736PyObject *
3737PyObject_GenericGetDict(PyObject *obj, void *context)
3738{
3739 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
3740 if (dictptr == NULL) {
3741 PyErr_SetString(PyExc_AttributeError,
3742 "This object has no __dict__");
3743 return NULL;
3744 }
3745 dict = *dictptr;
3746 if (dict == NULL) {
3747 PyTypeObject *tp = Py_TYPE(obj);
3748 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
3749 DK_INCREF(CACHED_KEYS(tp));
3750 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
3751 }
3752 else {
3753 *dictptr = dict = PyDict_New();
3754 }
3755 }
3756 Py_XINCREF(dict);
3757 return dict;
3758}
3759
3760int
3761_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
3762 PyObject *key, PyObject *value)
3763{
3764 PyObject *dict;
3765 int res;
3766 PyDictKeysObject *cached;
3767
3768 assert(dictptr != NULL);
3769 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
3770 assert(dictptr != NULL);
3771 dict = *dictptr;
3772 if (dict == NULL) {
3773 DK_INCREF(cached);
3774 dict = new_dict_with_shared_keys(cached);
3775 if (dict == NULL)
3776 return -1;
3777 *dictptr = dict;
3778 }
3779 if (value == NULL) {
3780 res = PyDict_DelItem(dict, key);
3781 if (cached != ((PyDictObject *)dict)->ma_keys) {
3782 CACHED_KEYS(tp) = NULL;
3783 DK_DECREF(cached);
3784 }
3785 } else {
3786 res = PyDict_SetItem(dict, key, value);
3787 if (cached != ((PyDictObject *)dict)->ma_keys) {
3788 /* Either update tp->ht_cached_keys or delete it */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04003789 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003790 CACHED_KEYS(tp) = make_keys_shared(dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003791 } else {
3792 CACHED_KEYS(tp) = NULL;
3793 }
3794 DK_DECREF(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04003795 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
3796 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003797 }
3798 }
3799 } else {
3800 dict = *dictptr;
3801 if (dict == NULL) {
3802 dict = PyDict_New();
3803 if (dict == NULL)
3804 return -1;
3805 *dictptr = dict;
3806 }
3807 if (value == NULL) {
3808 res = PyDict_DelItem(dict, key);
3809 } else {
3810 res = PyDict_SetItem(dict, key, value);
3811 }
3812 }
3813 return res;
3814}
3815
3816void
3817_PyDictKeys_DecRef(PyDictKeysObject *keys)
3818{
3819 DK_DECREF(keys);
3820}
3821
3822
3823/* ARGSUSED */
3824static PyObject *
3825dummy_repr(PyObject *op)
3826{
3827 return PyUnicode_FromString("<dummy key>");
3828}
3829
3830/* ARGUSED */
3831static void
3832dummy_dealloc(PyObject* ignore)
3833{
3834 /* This should never get called, but we also don't want to SEGV if
3835 * we accidentally decref dummy-key out of existence.
3836 */
3837 Py_FatalError("deallocating <dummy key>");
3838}
3839
3840static PyTypeObject PyDictDummy_Type = {
3841 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3842 "<dummy key> type",
3843 0,
3844 0,
3845 dummy_dealloc, /*tp_dealloc*/ /*never called*/
3846 0, /*tp_print*/
3847 0, /*tp_getattr*/
3848 0, /*tp_setattr*/
3849 0, /*tp_reserved*/
3850 dummy_repr, /*tp_repr*/
3851 0, /*tp_as_number*/
3852 0, /*tp_as_sequence*/
3853 0, /*tp_as_mapping*/
3854 0, /*tp_hash */
3855 0, /*tp_call */
3856 0, /*tp_str */
3857 0, /*tp_getattro */
3858 0, /*tp_setattro */
3859 0, /*tp_as_buffer */
3860 Py_TPFLAGS_DEFAULT, /*tp_flags */
3861};
3862
3863static PyObject _dummy_struct = {
3864 _PyObject_EXTRA_INIT
3865 2, &PyDictDummy_Type
3866};
3867