blob: 5a7de9e539061c719a4418ae4fe454f4cb6e6a1b [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"
Eric Snow96c6af92015-05-29 22:21:39 -060070#include "dict-common.h"
Christian Heimes0ded5b52007-12-10 15:50:56 +000071#include "stringlib/eq.h"
Guido van Rossum4b1302b1993-03-27 18:11:32 +000072
Larry Hastings61272b72014-01-07 12:41:53 -080073/*[clinic input]
Larry Hastingsc2047262014-01-25 20:43:29 -080074class dict "PyDictObject *" "&PyDict_Type"
Larry Hastings61272b72014-01-07 12:41:53 -080075[clinic start generated code]*/
Larry Hastings581ee362014-01-28 05:00:08 -080076/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f157a5a0ce9589d6]*/
Larry Hastings44e2eaa2013-11-23 15:37:55 -080077
Benjamin Peterson7d95e402012-04-23 11:24:50 -040078
79/*
80To ensure the lookup algorithm terminates, there must be at least one Unused
81slot (NULL key) in the table.
82To avoid slowing down lookups on a near-full table, we resize the table when
83it's USABLE_FRACTION (currently two-thirds) full.
84*/
Guido van Rossum16e93a81997-01-28 00:00:11 +000085
Tim Peterseb28ef22001-06-02 05:27:19 +000086#define PERTURB_SHIFT 5
87
Guido van Rossum16e93a81997-01-28 00:00:11 +000088/*
Tim Peterseb28ef22001-06-02 05:27:19 +000089Major subtleties ahead: Most hash schemes depend on having a "good" hash
90function, in the sense of simulating randomness. Python doesn't: its most
91important hash functions (for strings and ints) are very regular in common
92cases:
Tim Peters15d49292001-05-27 07:39:22 +000093
Guido van Rossumdc5f6b22006-08-24 21:29:26 +000094 >>> map(hash, (0, 1, 2, 3))
95 [0, 1, 2, 3]
96 >>> map(hash, ("namea", "nameb", "namec", "named"))
97 [-1658398457, -1658398460, -1658398459, -1658398462]
98 >>>
Tim Peters15d49292001-05-27 07:39:22 +000099
Tim Peterseb28ef22001-06-02 05:27:19 +0000100This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
101the low-order i bits as the initial table index is extremely fast, and there
102are no collisions at all for dicts indexed by a contiguous range of ints.
103The same is approximately true when keys are "consecutive" strings. So this
104gives better-than-random behavior in common cases, and that's very desirable.
Tim Peters15d49292001-05-27 07:39:22 +0000105
Tim Peterseb28ef22001-06-02 05:27:19 +0000106OTOH, when collisions occur, the tendency to fill contiguous slices of the
107hash table makes a good collision resolution strategy crucial. Taking only
108the last i bits of the hash code is also vulnerable: for example, consider
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109the list [i << 16 for i in range(20000)] as a set of keys. Since ints are
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000110their own hash codes, and this fits in a dict of size 2**15, the last 15 bits
111 of every hash code are all 0: they *all* map to the same table index.
Tim Peters15d49292001-05-27 07:39:22 +0000112
Tim Peterseb28ef22001-06-02 05:27:19 +0000113But catering to unusual cases should not slow the usual ones, so we just take
114the last i bits anyway. It's up to collision resolution to do the rest. If
115we *usually* find the key we're looking for on the first try (and, it turns
116out, we usually do -- the table load factor is kept under 2/3, so the odds
117are solidly in our favor), then it makes best sense to keep the initial index
118computation dirt cheap.
Tim Peters15d49292001-05-27 07:39:22 +0000119
Tim Peterseb28ef22001-06-02 05:27:19 +0000120The first half of collision resolution is to visit table indices via this
121recurrence:
Tim Peters15d49292001-05-27 07:39:22 +0000122
Tim Peterseb28ef22001-06-02 05:27:19 +0000123 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +0000124
Tim Peterseb28ef22001-06-02 05:27:19 +0000125For any initial j in range(2**i), repeating that 2**i times generates each
126int in range(2**i) exactly once (see any text on random-number generation for
127proof). By itself, this doesn't help much: like linear probing (setting
128j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
129order. This would be bad, except that's not the only thing we do, and it's
130actually *good* in the common cases where hash keys are consecutive. In an
131example that's really too small to make this entirely clear, for a table of
132size 2**3 the order of indices is:
Tim Peters15d49292001-05-27 07:39:22 +0000133
Tim Peterseb28ef22001-06-02 05:27:19 +0000134 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
135
136If two things come in at index 5, the first place we look after is index 2,
137not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
138Linear probing is deadly in this case because there the fixed probe order
139is the *same* as the order consecutive keys are likely to arrive. But it's
140extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
141and certain that consecutive hash codes do not.
142
143The other half of the strategy is to get the other bits of the hash code
144into play. This is done by initializing a (unsigned) vrbl "perturb" to the
145full hash code, and changing the recurrence to:
146
147 j = (5*j) + 1 + perturb;
148 perturb >>= PERTURB_SHIFT;
149 use j % 2**i as the next table index;
150
151Now the probe sequence depends (eventually) on every bit in the hash code,
152and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
153because it quickly magnifies small differences in the bits that didn't affect
154the initial index. Note that because perturb is unsigned, if the recurrence
155is executed often enough perturb eventually becomes and remains 0. At that
156point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
157that's certain to find an empty slot eventually (since it generates every int
158in range(2**i), and we make sure there's always at least one empty slot).
159
160Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
161small so that the high bits of the hash code continue to affect the probe
162sequence across iterations; but you want it large so that in really bad cases
163the high-order hash bits have an effect on early iterations. 5 was "the
164best" in minimizing total collisions across experiments Tim Peters ran (on
165both normal and pathological cases), but 4 and 6 weren't significantly worse.
166
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000167Historical: Reimer Behrends contributed the idea of using a polynomial-based
Tim Peterseb28ef22001-06-02 05:27:19 +0000168approach, using repeated multiplication by x in GF(2**n) where an irreducible
169polynomial for each table size was chosen such that x was a primitive root.
170Christian Tismer later extended that to use division by x instead, as an
171efficient way to get the high bits of the hash code into play. This scheme
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000172also gave excellent collision statistics, but was more expensive: two
173if-tests were required inside the loop; computing "the next" index took about
174the same number of operations but without as much potential parallelism
175(e.g., computing 5*j can go on at the same time as computing 1+perturb in the
176above, and then shifting perturb can be done while the table index is being
177masked); and the PyDictObject struct required a member to hold the table's
178polynomial. In Tim's experiments the current scheme ran faster, produced
179equally good collision statistics, needed less code & used less memory.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000180
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000181*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000182
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400183/* Object used as dummy key to fill deleted entries
184 * This could be any unique object,
185 * use a custom type in order to minimise coupling.
186*/
187static PyObject _dummy_struct;
188
189#define dummy (&_dummy_struct)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000190
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000191#ifdef Py_REF_DEBUG
192PyObject *
193_PyDict_Dummy(void)
194{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 return dummy;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000196}
197#endif
198
Fred Drake1bff34a2000-08-31 19:31:38 +0000199/* forward declarations */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400200static PyDictKeyEntry *lookdict(PyDictObject *mp, PyObject *key,
201 Py_hash_t hash, PyObject ***value_addr);
202static PyDictKeyEntry *lookdict_unicode(PyDictObject *mp, PyObject *key,
203 Py_hash_t hash, PyObject ***value_addr);
204static PyDictKeyEntry *
205lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
206 Py_hash_t hash, PyObject ***value_addr);
207static PyDictKeyEntry *lookdict_split(PyDictObject *mp, PyObject *key,
208 Py_hash_t hash, PyObject ***value_addr);
Fred Drake1bff34a2000-08-31 19:31:38 +0000209
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400210static int dictresize(PyDictObject *mp, Py_ssize_t minused);
Tim Petersdea48ec2001-05-22 20:40:22 +0000211
Raymond Hettinger43442782004-03-17 21:55:03 +0000212/* Dictionary reuse scheme to save calls to malloc, free, and memset */
Christian Heimes2202f872008-02-06 14:31:34 +0000213#ifndef PyDict_MAXFREELIST
214#define PyDict_MAXFREELIST 80
215#endif
216static PyDictObject *free_list[PyDict_MAXFREELIST];
217static int numfree = 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000218
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300219#include "clinic/dictobject.c.h"
220
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100221int
222PyDict_ClearFreeList(void)
Christian Heimes77c02eb2008-02-09 02:18:51 +0000223{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 PyDictObject *op;
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100225 int ret = numfree;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 while (numfree) {
227 op = free_list[--numfree];
228 assert(PyDict_CheckExact(op));
229 PyObject_GC_Del(op);
230 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100231 return ret;
232}
233
David Malcolm49526f42012-06-22 14:55:41 -0400234/* Print summary info about the state of the optimized allocator */
235void
236_PyDict_DebugMallocStats(FILE *out)
237{
238 _PyDebugAllocatorStats(out,
239 "free PyDictObject", numfree, sizeof(PyDictObject));
240}
241
242
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100243void
244PyDict_Fini(void)
245{
246 PyDict_ClearFreeList();
Christian Heimes77c02eb2008-02-09 02:18:51 +0000247}
248
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200249#define DK_DEBUG_INCREF _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA
250#define DK_DEBUG_DECREF _Py_DEC_REFTOTAL _Py_REF_DEBUG_COMMA
251
252#define DK_INCREF(dk) (DK_DEBUG_INCREF ++(dk)->dk_refcnt)
253#define DK_DECREF(dk) if (DK_DEBUG_DECREF (--(dk)->dk_refcnt) == 0) free_keys_object(dk)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400254#define DK_SIZE(dk) ((dk)->dk_size)
255#define DK_MASK(dk) (((dk)->dk_size)-1)
256#define IS_POWER_OF_2(x) (((x) & (x-1)) == 0)
257
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200258/* USABLE_FRACTION is the maximum dictionary load.
259 * Currently set to (2n+1)/3. Increasing this ratio makes dictionaries more
260 * dense resulting in more collisions. Decreasing it improves sparseness
261 * at the expense of spreading entries over more cache lines and at the
262 * cost of total memory consumed.
263 *
264 * USABLE_FRACTION must obey the following:
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400265 * (0 < USABLE_FRACTION(n) < n) for all n >= 2
266 *
267 * USABLE_FRACTION should be very quick to calculate.
268 * Fractions around 5/8 to 2/3 seem to work well in practice.
269 */
270
271/* Use (2n+1)/3 rather than 2n+3 because: it makes no difference for
272 * combined tables (the two fractions round to the same number n < ),
273 * but 2*4/3 is 2 whereas (2*4+1)/3 is 3 which potentially saves quite
274 * a lot of space for small, split tables */
275#define USABLE_FRACTION(n) ((((n) << 1)+1)/3)
276
277/* Alternative fraction that is otherwise close enough to (2n+1)/3 to make
278 * little difference. 8 * 2/3 == 8 * 5/8 == 5. 16 * 2/3 == 16 * 5/8 == 10.
279 * 32 * 2/3 = 21, 32 * 5/8 = 20.
280 * Its advantage is that it is faster to compute on machines with slow division.
281 * #define USABLE_FRACTION(n) (((n) >> 1) + ((n) >> 2) - ((n) >> 3))
282*/
283
Victor Stinnera9f61a52013-07-16 22:17:26 +0200284/* GROWTH_RATE. Growth rate upon hitting maximum load.
285 * Currently set to used*2 + capacity/2.
286 * This means that dicts double in size when growing without deletions,
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700287 * but have more head room when the number of deletions is on a par with the
288 * number of insertions.
289 * Raising this to used*4 doubles memory consumption depending on the size of
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200290 * the dictionary, but results in half the number of resizes, less effort to
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700291 * resize.
292 * GROWTH_RATE was set to used*4 up to version 3.2.
293 * GROWTH_RATE was set to used*2 in version 3.3.0
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200294 */
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700295#define GROWTH_RATE(d) (((d)->ma_used*2)+((d)->ma_keys->dk_size>>1))
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400296
297#define ENSURE_ALLOWS_DELETIONS(d) \
298 if ((d)->ma_keys->dk_lookup == lookdict_unicode_nodummy) { \
299 (d)->ma_keys->dk_lookup = lookdict_unicode; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400301
302/* This immutable, empty PyDictKeysObject is used for PyDict_Clear()
303 * (which cannot fail and thus can do no allocation).
304 */
305static PyDictKeysObject empty_keys_struct = {
306 2, /* dk_refcnt 1 for this struct, 1 for dummy_struct */
307 1, /* dk_size */
308 lookdict_split, /* dk_lookup */
309 0, /* dk_usable (immutable) */
310 {
311 { 0, 0, 0 } /* dk_entries (empty) */
312 }
313};
314
315static PyObject *empty_values[1] = { NULL };
316
317#define Py_EMPTY_KEYS &empty_keys_struct
318
319static PyDictKeysObject *new_keys_object(Py_ssize_t size)
320{
321 PyDictKeysObject *dk;
322 Py_ssize_t i;
323 PyDictKeyEntry *ep0;
324
325 assert(size >= PyDict_MINSIZE_SPLIT);
326 assert(IS_POWER_OF_2(size));
327 dk = PyMem_MALLOC(sizeof(PyDictKeysObject) +
328 sizeof(PyDictKeyEntry) * (size-1));
329 if (dk == NULL) {
330 PyErr_NoMemory();
331 return NULL;
332 }
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200333 DK_DEBUG_INCREF dk->dk_refcnt = 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400334 dk->dk_size = size;
335 dk->dk_usable = USABLE_FRACTION(size);
336 ep0 = &dk->dk_entries[0];
337 /* Hash value of slot 0 is used by popitem, so it must be initialized */
338 ep0->me_hash = 0;
339 for (i = 0; i < size; i++) {
340 ep0[i].me_key = NULL;
341 ep0[i].me_value = NULL;
342 }
343 dk->dk_lookup = lookdict_unicode_nodummy;
344 return dk;
345}
346
347static void
348free_keys_object(PyDictKeysObject *keys)
349{
350 PyDictKeyEntry *entries = &keys->dk_entries[0];
351 Py_ssize_t i, n;
352 for (i = 0, n = DK_SIZE(keys); i < n; i++) {
353 Py_XDECREF(entries[i].me_key);
354 Py_XDECREF(entries[i].me_value);
355 }
356 PyMem_FREE(keys);
357}
358
359#define new_values(size) PyMem_NEW(PyObject *, size)
360
361#define free_values(values) PyMem_FREE(values)
362
363/* Consumes a reference to the keys object */
364static PyObject *
365new_dict(PyDictKeysObject *keys, PyObject **values)
366{
367 PyDictObject *mp;
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200368 assert(keys != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 if (numfree) {
370 mp = free_list[--numfree];
371 assert (mp != NULL);
372 assert (Py_TYPE(mp) == &PyDict_Type);
373 _Py_NewReference((PyObject *)mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400375 else {
376 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
377 if (mp == NULL) {
378 DK_DECREF(keys);
379 free_values(values);
380 return NULL;
381 }
382 }
383 mp->ma_keys = keys;
384 mp->ma_values = values;
385 mp->ma_used = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000387}
388
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400389/* Consumes a reference to the keys object */
390static PyObject *
391new_dict_with_shared_keys(PyDictKeysObject *keys)
392{
393 PyObject **values;
394 Py_ssize_t i, size;
395
396 size = DK_SIZE(keys);
397 values = new_values(size);
398 if (values == NULL) {
399 DK_DECREF(keys);
400 return PyErr_NoMemory();
401 }
402 for (i = 0; i < size; i++) {
403 values[i] = NULL;
404 }
405 return new_dict(keys, values);
406}
407
408PyObject *
409PyDict_New(void)
410{
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200411 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE_COMBINED);
412 if (keys == NULL)
413 return NULL;
414 return new_dict(keys, NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400415}
416
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000417/*
418The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000419This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000420Open addressing is preferred over chaining since the link overhead for
421chaining would be substantial (100% with typical malloc overhead).
422
Tim Peterseb28ef22001-06-02 05:27:19 +0000423The initial probe index is computed as hash mod the table size. Subsequent
424probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000425
426All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000427
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000428The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000429contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000430Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000431
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000432lookdict() is general-purpose, and may return NULL if (and only if) a
433comparison raises an exception (this was new in Python 2.5).
Guido van Rossum89d8c602007-09-18 17:26:56 +0000434lookdict_unicode() below is specialized to string keys, comparison of which can
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400435never raise an exception; that function can never return NULL.
436lookdict_unicode_nodummy is further specialized for string keys that cannot be
437the <dummy> value.
438For both, when the key isn't found a PyDictEntry* is returned
439where the key would have been found, *value_addr points to the matching value
440slot.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000441*/
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400442static PyDictKeyEntry *
443lookdict(PyDictObject *mp, PyObject *key,
444 Py_hash_t hash, PyObject ***value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000445{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200446 size_t i;
447 size_t perturb;
448 PyDictKeyEntry *freeslot;
449 size_t mask;
Antoine Pitrou9a234902012-05-13 20:48:01 +0200450 PyDictKeyEntry *ep0;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200451 PyDictKeyEntry *ep;
452 int cmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000454
Antoine Pitrou9a234902012-05-13 20:48:01 +0200455top:
456 mask = DK_MASK(mp->ma_keys);
457 ep0 = &mp->ma_keys->dk_entries[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 i = (size_t)hash & mask;
459 ep = &ep0[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400460 if (ep->me_key == NULL || ep->me_key == key) {
461 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400463 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 if (ep->me_key == dummy)
465 freeslot = ep;
466 else {
467 if (ep->me_hash == hash) {
468 startkey = ep->me_key;
469 Py_INCREF(startkey);
470 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
471 Py_DECREF(startkey);
472 if (cmp < 0)
473 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400474 if (ep0 == mp->ma_keys->dk_entries && ep->me_key == startkey) {
475 if (cmp > 0) {
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 }
480 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200481 /* The dict was mutated, restart */
482 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 }
484 }
485 freeslot = NULL;
486 }
Tim Peters15d49292001-05-27 07:39:22 +0000487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 /* In the loop, me_key == dummy is by far (factor of 100s) the
489 least likely outcome, so test for that last. */
490 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
491 i = (i << 2) + i + perturb + 1;
492 ep = &ep0[i & mask];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400493 if (ep->me_key == NULL) {
494 if (freeslot == NULL) {
495 *value_addr = &ep->me_value;
496 return ep;
497 } else {
498 *value_addr = &freeslot->me_value;
499 return freeslot;
500 }
501 }
502 if (ep->me_key == key) {
503 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400505 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 if (ep->me_hash == hash && ep->me_key != dummy) {
507 startkey = ep->me_key;
508 Py_INCREF(startkey);
509 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
510 Py_DECREF(startkey);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400511 if (cmp < 0) {
512 *value_addr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400514 }
515 if (ep0 == mp->ma_keys->dk_entries && ep->me_key == startkey) {
516 if (cmp > 0) {
517 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400519 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 }
521 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200522 /* The dict was mutated, restart */
523 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 }
525 }
526 else if (ep->me_key == dummy && freeslot == NULL)
527 freeslot = ep;
528 }
529 assert(0); /* NOT REACHED */
530 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000531}
532
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400533/* Specialized version for string-only keys */
534static PyDictKeyEntry *
535lookdict_unicode(PyDictObject *mp, PyObject *key,
536 Py_hash_t hash, PyObject ***value_addr)
Fred Drake1bff34a2000-08-31 19:31:38 +0000537{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200538 size_t i;
539 size_t perturb;
540 PyDictKeyEntry *freeslot;
541 size_t mask = DK_MASK(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400542 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200543 PyDictKeyEntry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 /* Make sure this function doesn't have to handle non-unicode keys,
546 including subclasses of str; e.g., one reason to subclass
547 unicodes is to override __eq__, and for speed we don't cater to
548 that here. */
549 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400550 mp->ma_keys->dk_lookup = lookdict;
551 return lookdict(mp, key, hash, value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 }
Mark Dickinson57e683e2011-09-24 18:18:40 +0100553 i = (size_t)hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 ep = &ep0[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400555 if (ep->me_key == NULL || ep->me_key == key) {
556 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400558 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 if (ep->me_key == dummy)
560 freeslot = ep;
561 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400562 if (ep->me_hash == hash && unicode_eq(ep->me_key, key)) {
563 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400565 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 freeslot = NULL;
567 }
Tim Peters15d49292001-05-27 07:39:22 +0000568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 /* In the loop, me_key == dummy is by far (factor of 100s) the
570 least likely outcome, so test for that last. */
571 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
572 i = (i << 2) + i + perturb + 1;
573 ep = &ep0[i & mask];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400574 if (ep->me_key == NULL) {
575 if (freeslot == NULL) {
576 *value_addr = &ep->me_value;
577 return ep;
578 } else {
579 *value_addr = &freeslot->me_value;
580 return freeslot;
581 }
582 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 if (ep->me_key == key
584 || (ep->me_hash == hash
585 && ep->me_key != dummy
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400586 && unicode_eq(ep->me_key, key))) {
587 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400589 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 if (ep->me_key == dummy && freeslot == NULL)
591 freeslot = ep;
592 }
593 assert(0); /* NOT REACHED */
594 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000595}
596
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400597/* Faster version of lookdict_unicode when it is known that no <dummy> keys
598 * will be present. */
599static PyDictKeyEntry *
600lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
601 Py_hash_t hash, PyObject ***value_addr)
602{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200603 size_t i;
604 size_t perturb;
605 size_t mask = DK_MASK(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400606 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200607 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400608
609 /* Make sure this function doesn't have to handle non-unicode keys,
610 including subclasses of str; e.g., one reason to subclass
611 unicodes is to override __eq__, and for speed we don't cater to
612 that here. */
613 if (!PyUnicode_CheckExact(key)) {
614 mp->ma_keys->dk_lookup = lookdict;
615 return lookdict(mp, key, hash, value_addr);
616 }
617 i = (size_t)hash & mask;
618 ep = &ep0[i];
619 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
620 if (ep->me_key == NULL || ep->me_key == key ||
621 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
622 *value_addr = &ep->me_value;
623 return ep;
624 }
625 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
626 i = (i << 2) + i + perturb + 1;
627 ep = &ep0[i & mask];
628 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
629 if (ep->me_key == NULL || ep->me_key == key ||
630 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
631 *value_addr = &ep->me_value;
632 return ep;
633 }
634 }
635 assert(0); /* NOT REACHED */
636 return 0;
637}
638
639/* Version of lookdict for split tables.
640 * All split tables and only split tables use this lookup function.
641 * Split tables only contain unicode keys and no dummy keys,
642 * so algorithm is the same as lookdict_unicode_nodummy.
643 */
644static PyDictKeyEntry *
645lookdict_split(PyDictObject *mp, PyObject *key,
646 Py_hash_t hash, PyObject ***value_addr)
647{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200648 size_t i;
649 size_t perturb;
650 size_t mask = DK_MASK(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400651 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200652 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400653
654 if (!PyUnicode_CheckExact(key)) {
Benjamin Petersondb780d02012-04-23 13:44:32 -0400655 ep = lookdict(mp, key, hash, value_addr);
656 /* lookdict expects a combined-table, so fix value_addr */
657 i = ep - ep0;
658 *value_addr = &mp->ma_values[i];
659 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400660 }
661 i = (size_t)hash & mask;
662 ep = &ep0[i];
663 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
664 if (ep->me_key == NULL || ep->me_key == key ||
665 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
666 *value_addr = &mp->ma_values[i];
667 return ep;
668 }
669 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
670 i = (i << 2) + i + perturb + 1;
671 ep = &ep0[i & mask];
672 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
673 if (ep->me_key == NULL || ep->me_key == key ||
674 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
675 *value_addr = &mp->ma_values[i & mask];
676 return ep;
677 }
678 }
679 assert(0); /* NOT REACHED */
680 return 0;
681}
682
Benjamin Petersonfb886362010-04-24 18:21:17 +0000683int
684_PyDict_HasOnlyStringKeys(PyObject *dict)
685{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 Py_ssize_t pos = 0;
687 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000688 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400690 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 return 1;
692 while (PyDict_Next(dict, &pos, &key, &value))
693 if (!PyUnicode_Check(key))
694 return 0;
695 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000696}
697
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000698#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 do { \
700 if (!_PyObject_GC_IS_TRACKED(mp)) { \
701 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
702 _PyObject_GC_MAY_BE_TRACKED(value)) { \
703 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 } \
705 } \
706 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000707
708void
709_PyDict_MaybeUntrack(PyObject *op)
710{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 PyDictObject *mp;
712 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400713 Py_ssize_t i, size;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
716 return;
717
718 mp = (PyDictObject *) op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400719 size = DK_SIZE(mp->ma_keys);
720 if (_PyDict_HasSplitTable(mp)) {
721 for (i = 0; i < size; i++) {
722 if ((value = mp->ma_values[i]) == NULL)
723 continue;
724 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
725 assert(!_PyObject_GC_MAY_BE_TRACKED(
726 mp->ma_keys->dk_entries[i].me_key));
727 return;
728 }
729 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400731 else {
732 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
733 for (i = 0; i < size; i++) {
734 if ((value = ep0[i].me_value) == NULL)
735 continue;
736 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
737 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
738 return;
739 }
740 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000742}
743
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400744/* Internal function to find slot for an item from its hash
745 * when it is known that the key is not present in the dict.
746 */
747static PyDictKeyEntry *
748find_empty_slot(PyDictObject *mp, PyObject *key, Py_hash_t hash,
749 PyObject ***value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000750{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400751 size_t i;
752 size_t perturb;
753 size_t mask = DK_MASK(mp->ma_keys);
754 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
755 PyDictKeyEntry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000756
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400757 assert(key != NULL);
758 if (!PyUnicode_CheckExact(key))
759 mp->ma_keys->dk_lookup = lookdict;
760 i = hash & mask;
761 ep = &ep0[i];
762 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
763 i = (i << 2) + i + perturb + 1;
764 ep = &ep0[i & mask];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400766 assert(ep->me_value == NULL);
767 if (mp->ma_values)
768 *value_addr = &mp->ma_values[i & mask];
769 else
770 *value_addr = &ep->me_value;
771 return ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000772}
773
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400774static int
775insertion_resize(PyDictObject *mp)
776{
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700777 return dictresize(mp, GROWTH_RATE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400778}
Antoine Pitroue965d972012-02-27 00:45:12 +0100779
780/*
781Internal routine to insert a new item into the table.
782Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +0100783Returns -1 if an error occurred, or 0 on success.
784*/
785static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400786insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +0100787{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400788 PyObject *old_value;
789 PyObject **value_addr;
790 PyDictKeyEntry *ep;
791 assert(key != dummy);
Antoine Pitroue965d972012-02-27 00:45:12 +0100792
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400793 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
794 if (insertion_resize(mp) < 0)
795 return -1;
796 }
797
798 ep = mp->ma_keys->dk_lookup(mp, key, hash, &value_addr);
Antoine Pitroue965d972012-02-27 00:45:12 +0100799 if (ep == NULL) {
Antoine Pitroue965d972012-02-27 00:45:12 +0100800 return -1;
801 }
Antoine Pitroud6967322014-10-18 00:35:00 +0200802 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
Benjamin Petersona6f195e2012-04-30 10:23:40 -0400803 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400804 MAINTAIN_TRACKING(mp, key, value);
805 old_value = *value_addr;
806 if (old_value != NULL) {
807 assert(ep->me_key != NULL && ep->me_key != dummy);
808 *value_addr = value;
Antoine Pitroud6967322014-10-18 00:35:00 +0200809 Py_DECREF(old_value); /* which **CAN** re-enter (see issue #22653) */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400810 }
811 else {
812 if (ep->me_key == NULL) {
Benjamin Petersona6f195e2012-04-30 10:23:40 -0400813 Py_INCREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400814 if (mp->ma_keys->dk_usable <= 0) {
815 /* Need to resize. */
816 if (insertion_resize(mp) < 0) {
817 Py_DECREF(key);
818 Py_DECREF(value);
819 return -1;
820 }
821 ep = find_empty_slot(mp, key, hash, &value_addr);
822 }
823 mp->ma_keys->dk_usable--;
824 assert(mp->ma_keys->dk_usable >= 0);
825 ep->me_key = key;
826 ep->me_hash = hash;
827 }
828 else {
829 if (ep->me_key == dummy) {
Benjamin Petersona6f195e2012-04-30 10:23:40 -0400830 Py_INCREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400831 ep->me_key = key;
832 ep->me_hash = hash;
833 Py_DECREF(dummy);
834 } else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400835 assert(_PyDict_HasSplitTable(mp));
836 }
837 }
838 mp->ma_used++;
839 *value_addr = value;
Antoine Pitroud6967322014-10-18 00:35:00 +0200840 assert(ep->me_key != NULL && ep->me_key != dummy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400841 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400842 return 0;
Antoine Pitroue965d972012-02-27 00:45:12 +0100843}
844
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000845/*
846Internal routine used by dictresize() to insert an item which is
847known to be absent from the dict. This routine also assumes that
848the dict contains no deleted entries. Besides the performance benefit,
849using insertdict() in dictresize() is dangerous (SF bug #1456209).
850Note that no refcounts are changed by this routine; if needed, the caller
851is responsible for incref'ing `key` and `value`.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400852Neither mp->ma_used nor k->dk_usable are modified by this routine; the caller
853must set them correctly
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000854*/
855static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400856insertdict_clean(PyDictObject *mp, PyObject *key, Py_hash_t hash,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 PyObject *value)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000858{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400859 size_t i;
860 size_t perturb;
861 PyDictKeysObject *k = mp->ma_keys;
862 size_t mask = (size_t)DK_SIZE(k)-1;
863 PyDictKeyEntry *ep0 = &k->dk_entries[0];
864 PyDictKeyEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000865
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400866 assert(k->dk_lookup != NULL);
867 assert(value != NULL);
868 assert(key != NULL);
869 assert(key != dummy);
870 assert(PyUnicode_CheckExact(key) || k->dk_lookup == lookdict);
871 i = hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 ep = &ep0[i];
873 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
874 i = (i << 2) + i + perturb + 1;
875 ep = &ep0[i & mask];
876 }
877 assert(ep->me_value == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 ep->me_key = key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000879 ep->me_hash = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000881}
882
883/*
884Restructure the table by allocating a new table and reinserting all
885items again. When entries have been deleted, the new table may
886actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400887If a table is split (its keys and hashes are shared, its values are not),
888then the values are temporarily copied into the table, it is resized as
889a combined table, then the me_value slots in the old table are NULLed out.
890After resizing a table is always combined,
891but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000892*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000893static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000894dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000895{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 Py_ssize_t newsize;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400897 PyDictKeysObject *oldkeys;
898 PyObject **oldvalues;
899 Py_ssize_t i, oldsize;
Tim Peters91a364d2001-05-19 07:04:38 +0000900
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400901/* Find the smallest table size > minused. */
902 for (newsize = PyDict_MINSIZE_COMBINED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 newsize <= minused && newsize > 0;
904 newsize <<= 1)
905 ;
906 if (newsize <= 0) {
907 PyErr_NoMemory();
908 return -1;
909 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400910 oldkeys = mp->ma_keys;
911 oldvalues = mp->ma_values;
912 /* Allocate a new table. */
913 mp->ma_keys = new_keys_object(newsize);
914 if (mp->ma_keys == NULL) {
915 mp->ma_keys = oldkeys;
916 return -1;
917 }
918 if (oldkeys->dk_lookup == lookdict)
919 mp->ma_keys->dk_lookup = lookdict;
920 oldsize = DK_SIZE(oldkeys);
921 mp->ma_values = NULL;
922 /* If empty then nothing to copy so just return */
923 if (oldsize == 1) {
924 assert(oldkeys == Py_EMPTY_KEYS);
925 DK_DECREF(oldkeys);
926 return 0;
927 }
928 /* Main loop below assumes we can transfer refcount to new keys
929 * and that value is stored in me_value.
930 * Increment ref-counts and copy values here to compensate
931 * This (resizing a split table) should be relatively rare */
932 if (oldvalues != NULL) {
933 for (i = 0; i < oldsize; i++) {
934 if (oldvalues[i] != NULL) {
935 Py_INCREF(oldkeys->dk_entries[i].me_key);
936 oldkeys->dk_entries[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 }
939 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400940 /* Main loop */
941 for (i = 0; i < oldsize; i++) {
942 PyDictKeyEntry *ep = &oldkeys->dk_entries[i];
943 if (ep->me_value != NULL) {
944 assert(ep->me_key != dummy);
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000945 insertdict_clean(mp, ep->me_key, ep->me_hash, ep->me_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400948 mp->ma_keys->dk_usable -= mp->ma_used;
949 if (oldvalues != NULL) {
950 /* NULL out me_value slot in oldkeys, in case it was shared */
951 for (i = 0; i < oldsize; i++)
952 oldkeys->dk_entries[i].me_value = NULL;
953 assert(oldvalues != empty_values);
954 free_values(oldvalues);
955 DK_DECREF(oldkeys);
956 }
957 else {
958 assert(oldkeys->dk_lookup != lookdict_split);
959 if (oldkeys->dk_lookup != lookdict_unicode_nodummy) {
960 PyDictKeyEntry *ep0 = &oldkeys->dk_entries[0];
961 for (i = 0; i < oldsize; i++) {
962 if (ep0[i].me_key == dummy)
963 Py_DECREF(dummy);
964 }
965 }
966 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200967 DK_DEBUG_DECREF PyMem_FREE(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400968 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000970}
971
Benjamin Peterson15ee8212012-04-24 14:44:18 -0400972/* Returns NULL if unable to split table.
973 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400974static PyDictKeysObject *
975make_keys_shared(PyObject *op)
976{
977 Py_ssize_t i;
978 Py_ssize_t size;
979 PyDictObject *mp = (PyDictObject *)op;
980
Benjamin Peterson15ee8212012-04-24 14:44:18 -0400981 if (!PyDict_CheckExact(op))
982 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400983 if (!_PyDict_HasSplitTable(mp)) {
984 PyDictKeyEntry *ep0;
985 PyObject **values;
986 assert(mp->ma_keys->dk_refcnt == 1);
987 if (mp->ma_keys->dk_lookup == lookdict) {
988 return NULL;
989 }
990 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
991 /* Remove dummy keys */
992 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
993 return NULL;
994 }
995 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
996 /* Copy values into a new array */
997 ep0 = &mp->ma_keys->dk_entries[0];
998 size = DK_SIZE(mp->ma_keys);
999 values = new_values(size);
1000 if (values == NULL) {
1001 PyErr_SetString(PyExc_MemoryError,
1002 "Not enough memory to allocate new values array");
1003 return NULL;
1004 }
1005 for (i = 0; i < size; i++) {
1006 values[i] = ep0[i].me_value;
1007 ep0[i].me_value = NULL;
1008 }
1009 mp->ma_keys->dk_lookup = lookdict_split;
1010 mp->ma_values = values;
1011 }
1012 DK_INCREF(mp->ma_keys);
1013 return mp->ma_keys;
1014}
Christian Heimes99170a52007-12-19 02:07:34 +00001015
1016PyObject *
1017_PyDict_NewPresized(Py_ssize_t minused)
1018{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001019 Py_ssize_t newsize;
1020 PyDictKeysObject *new_keys;
1021 for (newsize = PyDict_MINSIZE_COMBINED;
1022 newsize <= minused && newsize > 0;
1023 newsize <<= 1)
1024 ;
1025 new_keys = new_keys_object(newsize);
1026 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001028 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001029}
1030
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001031/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1032 * that may occur (originally dicts supported only string keys, and exceptions
1033 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001034 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001035 * (suppressed) occurred while computing the key's hash, or that some error
1036 * (suppressed) occurred when comparing keys in the dict's internal probe
1037 * sequence. A nasty example of the latter is when a Python-coded comparison
1038 * function hits a stack-depth error, which can cause this to return NULL
1039 * even if the key is present.
1040 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001041PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001042PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001043{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001044 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001046 PyDictKeyEntry *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 PyThreadState *tstate;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001048 PyObject **value_addr;
1049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 if (!PyDict_Check(op))
1051 return NULL;
1052 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001053 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 {
1055 hash = PyObject_Hash(key);
1056 if (hash == -1) {
1057 PyErr_Clear();
1058 return NULL;
1059 }
1060 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 /* We can arrive here with a NULL tstate during initialization: try
1063 running "python -Wi" for an example related to string interning.
1064 Let's just hope that no exception occurs then... This must be
1065 _PyThreadState_Current and not PyThreadState_GET() because in debug
1066 mode, the latter complains if tstate is NULL. */
1067 tstate = (PyThreadState*)_Py_atomic_load_relaxed(
1068 &_PyThreadState_Current);
1069 if (tstate != NULL && tstate->curexc_type != NULL) {
1070 /* preserve the existing exception */
1071 PyObject *err_type, *err_value, *err_tb;
1072 PyErr_Fetch(&err_type, &err_value, &err_tb);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001073 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 /* ignore errors */
1075 PyErr_Restore(err_type, err_value, err_tb);
1076 if (ep == NULL)
1077 return NULL;
1078 }
1079 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001080 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 if (ep == NULL) {
1082 PyErr_Clear();
1083 return NULL;
1084 }
1085 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001086 return *value_addr;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001087}
1088
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001089PyObject *
1090_PyDict_GetItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1091{
1092 PyDictObject *mp = (PyDictObject *)op;
1093 PyDictKeyEntry *ep;
1094 PyThreadState *tstate;
1095 PyObject **value_addr;
1096
1097 if (!PyDict_Check(op))
1098 return NULL;
1099
1100 /* We can arrive here with a NULL tstate during initialization: try
1101 running "python -Wi" for an example related to string interning.
1102 Let's just hope that no exception occurs then... This must be
1103 _PyThreadState_Current and not PyThreadState_GET() because in debug
1104 mode, the latter complains if tstate is NULL. */
1105 tstate = (PyThreadState*)_Py_atomic_load_relaxed(
1106 &_PyThreadState_Current);
1107 if (tstate != NULL && tstate->curexc_type != NULL) {
1108 /* preserve the existing exception */
1109 PyObject *err_type, *err_value, *err_tb;
1110 PyErr_Fetch(&err_type, &err_value, &err_tb);
1111 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
1112 /* ignore errors */
1113 PyErr_Restore(err_type, err_value, err_tb);
1114 if (ep == NULL)
1115 return NULL;
1116 }
1117 else {
1118 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
1119 if (ep == NULL) {
1120 PyErr_Clear();
1121 return NULL;
1122 }
1123 }
1124 return *value_addr;
1125}
1126
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001127/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1128 This returns NULL *with* an exception set if an exception occurred.
1129 It returns NULL *without* an exception set if the key wasn't present.
1130*/
1131PyObject *
1132PyDict_GetItemWithError(PyObject *op, PyObject *key)
1133{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001134 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 PyDictObject*mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001136 PyDictKeyEntry *ep;
1137 PyObject **value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 if (!PyDict_Check(op)) {
1140 PyErr_BadInternalCall();
1141 return NULL;
1142 }
1143 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001144 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 {
1146 hash = PyObject_Hash(key);
1147 if (hash == -1) {
1148 return NULL;
1149 }
1150 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001151
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001152 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 if (ep == NULL)
1154 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001155 return *value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001156}
1157
Brett Cannonfd074152012-04-14 14:10:13 -04001158PyObject *
1159_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1160{
1161 PyObject *kv;
1162 kv = _PyUnicode_FromId(key); /* borrowed */
1163 if (kv == NULL)
1164 return NULL;
1165 return PyDict_GetItemWithError(dp, kv);
1166}
1167
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001168/* Fast version of global value lookup.
1169 * Lookup in globals, then builtins.
1170 */
1171PyObject *
1172_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001173{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001174 PyObject *x;
1175 if (PyUnicode_CheckExact(key)) {
1176 PyObject **value_addr;
1177 Py_hash_t hash = ((PyASCIIObject *)key)->hash;
1178 if (hash != -1) {
1179 PyDictKeyEntry *e;
1180 e = globals->ma_keys->dk_lookup(globals, key, hash, &value_addr);
1181 if (e == NULL) {
1182 return NULL;
1183 }
1184 x = *value_addr;
1185 if (x != NULL)
1186 return x;
1187 e = builtins->ma_keys->dk_lookup(builtins, key, hash, &value_addr);
1188 if (e == NULL) {
1189 return NULL;
1190 }
1191 x = *value_addr;
1192 return x;
1193 }
Antoine Pitroue965d972012-02-27 00:45:12 +01001194 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001195 x = PyDict_GetItemWithError((PyObject *)globals, key);
1196 if (x != NULL)
1197 return x;
1198 if (PyErr_Occurred())
1199 return NULL;
1200 return PyDict_GetItemWithError((PyObject *)builtins, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001201}
1202
Antoine Pitroue965d972012-02-27 00:45:12 +01001203/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1204 * dictionary if it's merely replacing the value for an existing key.
1205 * This means that it's safe to loop over a dictionary with PyDict_Next()
1206 * and occasionally replace a value -- but you can't insert new keys or
1207 * remove them.
1208 */
1209int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001210PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001211{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001212 PyDictObject *mp;
1213 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001214 if (!PyDict_Check(op)) {
1215 PyErr_BadInternalCall();
1216 return -1;
1217 }
1218 assert(key);
1219 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001220 mp = (PyDictObject *)op;
1221 if (!PyUnicode_CheckExact(key) ||
1222 (hash = ((PyASCIIObject *) key)->hash) == -1)
1223 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001224 hash = PyObject_Hash(key);
1225 if (hash == -1)
1226 return -1;
1227 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001228
1229 /* insertdict() handles any resizing that might be necessary */
1230 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001231}
1232
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001233int
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001234_PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
1235 Py_hash_t hash)
1236{
1237 PyDictObject *mp;
1238
1239 if (!PyDict_Check(op)) {
1240 PyErr_BadInternalCall();
1241 return -1;
1242 }
1243 assert(key);
1244 assert(value);
1245 mp = (PyDictObject *)op;
1246
1247 /* insertdict() handles any resizing that might be necessary */
1248 return insertdict(mp, key, hash, value);
1249}
1250
1251int
Tim Peters1f5871e2000-07-04 17:44:48 +00001252PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001253{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001254 PyDictObject *mp;
1255 Py_hash_t hash;
1256 PyDictKeyEntry *ep;
1257 PyObject *old_key, *old_value;
1258 PyObject **value_addr;
Guido van Rossumd7047b31995-01-02 19:07:15 +00001259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 if (!PyDict_Check(op)) {
1261 PyErr_BadInternalCall();
1262 return -1;
1263 }
1264 assert(key);
1265 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001266 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 hash = PyObject_Hash(key);
1268 if (hash == -1)
1269 return -1;
1270 }
1271 mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001272 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 if (ep == NULL)
1274 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001275 if (*value_addr == NULL) {
Raymond Hettinger69492da2013-09-02 15:59:26 -07001276 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 return -1;
1278 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001279 old_value = *value_addr;
1280 *value_addr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001282 if (!_PyDict_HasSplitTable(mp)) {
1283 ENSURE_ALLOWS_DELETIONS(mp);
1284 old_key = ep->me_key;
1285 Py_INCREF(dummy);
1286 ep->me_key = dummy;
1287 Py_DECREF(old_key);
1288 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 Py_DECREF(old_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001291}
1292
Guido van Rossum25831651993-05-19 14:50:45 +00001293void
Tim Peters1f5871e2000-07-04 17:44:48 +00001294PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001295{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001297 PyDictKeysObject *oldkeys;
1298 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 if (!PyDict_Check(op))
1302 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001303 mp = ((PyDictObject *)op);
1304 oldkeys = mp->ma_keys;
1305 oldvalues = mp->ma_values;
1306 if (oldvalues == empty_values)
1307 return;
1308 /* Empty the dict... */
1309 DK_INCREF(Py_EMPTY_KEYS);
1310 mp->ma_keys = Py_EMPTY_KEYS;
1311 mp->ma_values = empty_values;
1312 mp->ma_used = 0;
1313 /* ...then clear the keys and values */
1314 if (oldvalues != NULL) {
1315 n = DK_SIZE(oldkeys);
1316 for (i = 0; i < n; i++)
1317 Py_CLEAR(oldvalues[i]);
1318 free_values(oldvalues);
1319 DK_DECREF(oldkeys);
1320 }
1321 else {
1322 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001323 DK_DECREF(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001324 }
1325}
1326
1327/* Returns -1 if no more items (or op is not a dict),
1328 * index of item otherwise. Stores value in pvalue
1329 */
1330Py_LOCAL_INLINE(Py_ssize_t)
1331dict_next(PyObject *op, Py_ssize_t i, PyObject **pvalue)
1332{
1333 Py_ssize_t mask, offset;
1334 PyDictObject *mp;
1335 PyObject **value_ptr;
1336
1337
1338 if (!PyDict_Check(op))
1339 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001341 if (i < 0)
1342 return -1;
1343 if (mp->ma_values) {
1344 value_ptr = &mp->ma_values[i];
1345 offset = sizeof(PyObject *);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001347 else {
1348 value_ptr = &mp->ma_keys->dk_entries[i].me_value;
1349 offset = sizeof(PyDictKeyEntry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001351 mask = DK_MASK(mp->ma_keys);
1352 while (i <= mask && *value_ptr == NULL) {
1353 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1354 i++;
1355 }
1356 if (i > mask)
1357 return -1;
1358 if (pvalue)
1359 *pvalue = *value_ptr;
1360 return i;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001361}
1362
Tim Peters080c88b2003-02-15 03:01:11 +00001363/*
1364 * Iterate over a dict. Use like so:
1365 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001366 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001367 * PyObject *key, *value;
1368 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001369 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +00001370 * Refer to borrowed references in key and value.
1371 * }
1372 *
1373 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001374 * mutates the dict. One exception: it is safe if the loop merely changes
1375 * the values associated with the keys (but doesn't insert new keys or
1376 * delete keys), via PyDict_SetItem().
1377 */
Guido van Rossum25831651993-05-19 14:50:45 +00001378int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001379PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001380{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001381 PyDictObject *mp;
1382 Py_ssize_t i = dict_next(op, *ppos, pvalue);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 if (i < 0)
1384 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001385 mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 *ppos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 if (pkey)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001388 *pkey = mp->ma_keys->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001390}
1391
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001392/* Internal version of PyDict_Next that returns a hash value in addition
1393 * to the key and value.
1394 */
Thomas Wouterscf297e42007-02-23 15:07:44 +00001395int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001396_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1397 PyObject **pvalue, Py_hash_t *phash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00001398{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001399 PyDictObject *mp;
1400 Py_ssize_t i = dict_next(op, *ppos, pvalue);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 if (i < 0)
1402 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001403 mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 *ppos = i+1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001405 *phash = mp->ma_keys->dk_entries[i].me_hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 if (pkey)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001407 *pkey = mp->ma_keys->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 return 1;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001409}
1410
Eric Snow96c6af92015-05-29 22:21:39 -06001411/* Internal version of dict.pop(). */
1412PyObject *
1413_PyDict_Pop(PyDictObject *mp, PyObject *key, PyObject *deflt)
1414{
1415 Py_hash_t hash;
1416 PyObject *old_value, *old_key;
1417 PyDictKeyEntry *ep;
1418 PyObject **value_addr;
1419
1420 if (mp->ma_used == 0) {
1421 if (deflt) {
1422 Py_INCREF(deflt);
1423 return deflt;
1424 }
1425 _PyErr_SetKeyError(key);
1426 return NULL;
1427 }
1428 if (!PyUnicode_CheckExact(key) ||
1429 (hash = ((PyASCIIObject *) key)->hash) == -1) {
1430 hash = PyObject_Hash(key);
1431 if (hash == -1)
1432 return NULL;
1433 }
1434 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
1435 if (ep == NULL)
1436 return NULL;
1437 old_value = *value_addr;
1438 if (old_value == NULL) {
1439 if (deflt) {
1440 Py_INCREF(deflt);
1441 return deflt;
1442 }
1443 _PyErr_SetKeyError(key);
1444 return NULL;
1445 }
1446 *value_addr = NULL;
1447 mp->ma_used--;
1448 if (!_PyDict_HasSplitTable(mp)) {
1449 ENSURE_ALLOWS_DELETIONS(mp);
1450 old_key = ep->me_key;
1451 Py_INCREF(dummy);
1452 ep->me_key = dummy;
1453 Py_DECREF(old_key);
1454 }
1455 return old_value;
1456}
1457
1458/* Internal version of dict.from_keys(). It is subclass-friendly. */
1459PyObject *
1460_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
1461{
1462 PyObject *it; /* iter(iterable) */
1463 PyObject *key;
1464 PyObject *d;
1465 int status;
1466
1467 d = PyObject_CallObject(cls, NULL);
1468 if (d == NULL)
1469 return NULL;
1470
1471 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
1472 if (PyDict_CheckExact(iterable)) {
1473 PyDictObject *mp = (PyDictObject *)d;
1474 PyObject *oldvalue;
1475 Py_ssize_t pos = 0;
1476 PyObject *key;
1477 Py_hash_t hash;
1478
1479 if (dictresize(mp, Py_SIZE(iterable))) {
1480 Py_DECREF(d);
1481 return NULL;
1482 }
1483
1484 while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
1485 if (insertdict(mp, key, hash, value)) {
1486 Py_DECREF(d);
1487 return NULL;
1488 }
1489 }
1490 return d;
1491 }
1492 if (PyAnySet_CheckExact(iterable)) {
1493 PyDictObject *mp = (PyDictObject *)d;
1494 Py_ssize_t pos = 0;
1495 PyObject *key;
1496 Py_hash_t hash;
1497
1498 if (dictresize(mp, PySet_GET_SIZE(iterable))) {
1499 Py_DECREF(d);
1500 return NULL;
1501 }
1502
1503 while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
1504 if (insertdict(mp, key, hash, value)) {
1505 Py_DECREF(d);
1506 return NULL;
1507 }
1508 }
1509 return d;
1510 }
1511 }
1512
1513 it = PyObject_GetIter(iterable);
1514 if (it == NULL){
1515 Py_DECREF(d);
1516 return NULL;
1517 }
1518
1519 if (PyDict_CheckExact(d)) {
1520 while ((key = PyIter_Next(it)) != NULL) {
1521 status = PyDict_SetItem(d, key, value);
1522 Py_DECREF(key);
1523 if (status < 0)
1524 goto Fail;
1525 }
1526 } else {
1527 while ((key = PyIter_Next(it)) != NULL) {
1528 status = PyObject_SetItem(d, key, value);
1529 Py_DECREF(key);
1530 if (status < 0)
1531 goto Fail;
1532 }
1533 }
1534
1535 if (PyErr_Occurred())
1536 goto Fail;
1537 Py_DECREF(it);
1538 return d;
1539
1540Fail:
1541 Py_DECREF(it);
1542 Py_DECREF(d);
1543 return NULL;
1544}
1545
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001546/* Methods */
1547
1548static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001549dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001550{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001551 PyObject **values = mp->ma_values;
1552 PyDictKeysObject *keys = mp->ma_keys;
1553 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554 PyObject_GC_UnTrack(mp);
1555 Py_TRASHCAN_SAFE_BEGIN(mp)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001556 if (values != NULL) {
1557 if (values != empty_values) {
1558 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
1559 Py_XDECREF(values[i]);
1560 }
1561 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001563 DK_DECREF(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02001565 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001566 assert(keys->dk_refcnt == 1);
1567 DK_DECREF(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001568 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1570 free_list[numfree++] = mp;
1571 else
1572 Py_TYPE(mp)->tp_free((PyObject *)mp);
1573 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001574}
1575
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001576
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001577static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001578dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001579{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001580 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001581 PyObject *key = NULL, *value = NULL;
1582 _PyUnicodeWriter writer;
1583 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00001584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 i = Py_ReprEnter((PyObject *)mp);
1586 if (i != 0) {
1587 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
1588 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001591 Py_ReprLeave((PyObject *)mp);
1592 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 }
Tim Petersa7259592001-06-16 05:11:17 +00001594
Victor Stinnerf91929b2013-11-19 13:07:38 +01001595 _PyUnicodeWriter_Init(&writer);
1596 writer.overallocate = 1;
1597 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
1598 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00001599
Victor Stinnerf91929b2013-11-19 13:07:38 +01001600 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
1601 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 /* Do repr() on each key+value pair, and insert ": " between them.
1604 Note that repr may mutate the dict. */
1605 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001606 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001608 PyObject *s;
1609 int res;
1610
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001611 /* Prevent repr from deleting key or value during key format. */
1612 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02001614
Victor Stinnerf91929b2013-11-19 13:07:38 +01001615 if (!first) {
1616 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
1617 goto error;
1618 }
1619 first = 0;
1620
1621 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01001623 goto error;
1624 res = _PyUnicodeWriter_WriteStr(&writer, s);
1625 Py_DECREF(s);
1626 if (res < 0)
1627 goto error;
1628
1629 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
1630 goto error;
1631
1632 s = PyObject_Repr(value);
1633 if (s == NULL)
1634 goto error;
1635 res = _PyUnicodeWriter_WriteStr(&writer, s);
1636 Py_DECREF(s);
1637 if (res < 0)
1638 goto error;
1639
1640 Py_CLEAR(key);
1641 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 }
Tim Petersa7259592001-06-16 05:11:17 +00001643
Victor Stinnerf91929b2013-11-19 13:07:38 +01001644 writer.overallocate = 0;
1645 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
1646 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01001649
1650 return _PyUnicodeWriter_Finish(&writer);
1651
1652error:
1653 Py_ReprLeave((PyObject *)mp);
1654 _PyUnicodeWriter_Dealloc(&writer);
1655 Py_XDECREF(key);
1656 Py_XDECREF(value);
1657 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001658}
1659
Martin v. Löwis18e16552006-02-15 17:27:45 +00001660static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001661dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001662{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001664}
1665
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001666static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001667dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001668{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 PyObject *v;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001670 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001671 PyDictKeyEntry *ep;
1672 PyObject **value_addr;
1673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001675 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 hash = PyObject_Hash(key);
1677 if (hash == -1)
1678 return NULL;
1679 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001680 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001681 if (ep == NULL)
1682 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001683 v = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 if (v == NULL) {
1685 if (!PyDict_CheckExact(mp)) {
1686 /* Look up __missing__ method if we're a subclass. */
1687 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05001688 _Py_IDENTIFIER(__missing__);
1689 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001690 if (missing != NULL) {
1691 res = PyObject_CallFunctionObjArgs(missing,
1692 key, NULL);
1693 Py_DECREF(missing);
1694 return res;
1695 }
1696 else if (PyErr_Occurred())
1697 return NULL;
1698 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07001699 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 return NULL;
1701 }
1702 else
1703 Py_INCREF(v);
1704 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001705}
1706
1707static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001708dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001709{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001710 if (w == NULL)
1711 return PyDict_DelItem((PyObject *)mp, v);
1712 else
1713 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001714}
1715
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001716static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717 (lenfunc)dict_length, /*mp_length*/
1718 (binaryfunc)dict_subscript, /*mp_subscript*/
1719 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001720};
1721
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001722static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001723dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001724{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001725 PyObject *v;
1726 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001727 PyDictKeyEntry *ep;
1728 Py_ssize_t size, n, offset;
1729 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001730
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001731 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 n = mp->ma_used;
1733 v = PyList_New(n);
1734 if (v == NULL)
1735 return NULL;
1736 if (n != mp->ma_used) {
1737 /* Durnit. The allocations caused the dict to resize.
1738 * Just start over, this shouldn't normally happen.
1739 */
1740 Py_DECREF(v);
1741 goto again;
1742 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001743 ep = &mp->ma_keys->dk_entries[0];
1744 size = DK_SIZE(mp->ma_keys);
1745 if (mp->ma_values) {
1746 value_ptr = mp->ma_values;
1747 offset = sizeof(PyObject *);
1748 }
1749 else {
1750 value_ptr = &ep[0].me_value;
1751 offset = sizeof(PyDictKeyEntry);
1752 }
1753 for (i = 0, j = 0; i < size; i++) {
1754 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001755 PyObject *key = ep[i].me_key;
1756 Py_INCREF(key);
1757 PyList_SET_ITEM(v, j, key);
1758 j++;
1759 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001760 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001761 }
1762 assert(j == n);
1763 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001764}
1765
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001766static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001767dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001768{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001769 PyObject *v;
1770 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001771 Py_ssize_t size, n, offset;
1772 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001773
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001774 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001775 n = mp->ma_used;
1776 v = PyList_New(n);
1777 if (v == NULL)
1778 return NULL;
1779 if (n != mp->ma_used) {
1780 /* Durnit. The allocations caused the dict to resize.
1781 * Just start over, this shouldn't normally happen.
1782 */
1783 Py_DECREF(v);
1784 goto again;
1785 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001786 size = DK_SIZE(mp->ma_keys);
1787 if (mp->ma_values) {
1788 value_ptr = mp->ma_values;
1789 offset = sizeof(PyObject *);
1790 }
1791 else {
1792 value_ptr = &mp->ma_keys->dk_entries[0].me_value;
1793 offset = sizeof(PyDictKeyEntry);
1794 }
1795 for (i = 0, j = 0; i < size; i++) {
1796 PyObject *value = *value_ptr;
1797 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1798 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 Py_INCREF(value);
1800 PyList_SET_ITEM(v, j, value);
1801 j++;
1802 }
1803 }
1804 assert(j == n);
1805 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001806}
1807
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001808static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001809dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001810{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001811 PyObject *v;
1812 Py_ssize_t i, j, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001813 Py_ssize_t size, offset;
1814 PyObject *item, *key;
1815 PyDictKeyEntry *ep;
1816 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 /* Preallocate the list of tuples, to avoid allocations during
1819 * the loop over the items, which could trigger GC, which
1820 * could resize the dict. :-(
1821 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001822 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001823 n = mp->ma_used;
1824 v = PyList_New(n);
1825 if (v == NULL)
1826 return NULL;
1827 for (i = 0; i < n; i++) {
1828 item = PyTuple_New(2);
1829 if (item == NULL) {
1830 Py_DECREF(v);
1831 return NULL;
1832 }
1833 PyList_SET_ITEM(v, i, item);
1834 }
1835 if (n != mp->ma_used) {
1836 /* Durnit. The allocations caused the dict to resize.
1837 * Just start over, this shouldn't normally happen.
1838 */
1839 Py_DECREF(v);
1840 goto again;
1841 }
1842 /* Nothing we do below makes any function calls. */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001843 ep = mp->ma_keys->dk_entries;
1844 size = DK_SIZE(mp->ma_keys);
1845 if (mp->ma_values) {
1846 value_ptr = mp->ma_values;
1847 offset = sizeof(PyObject *);
1848 }
1849 else {
1850 value_ptr = &ep[0].me_value;
1851 offset = sizeof(PyDictKeyEntry);
1852 }
1853 for (i = 0, j = 0; i < size; i++) {
1854 PyObject *value = *value_ptr;
1855 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1856 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 key = ep[i].me_key;
1858 item = PyList_GET_ITEM(v, j);
1859 Py_INCREF(key);
1860 PyTuple_SET_ITEM(item, 0, key);
1861 Py_INCREF(value);
1862 PyTuple_SET_ITEM(item, 1, value);
1863 j++;
1864 }
1865 }
1866 assert(j == n);
1867 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001868}
1869
Larry Hastings5c661892014-01-24 06:17:25 -08001870/*[clinic input]
1871@classmethod
1872dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08001873 iterable: object
1874 value: object=None
1875 /
1876
1877Returns a new dict with keys from iterable and values equal to value.
1878[clinic start generated code]*/
1879
Larry Hastings5c661892014-01-24 06:17:25 -08001880static PyObject *
1881dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001882/*[clinic end generated code: output=8fb98e4b10384999 input=b85a667f9bf4669d]*/
Larry Hastings5c661892014-01-24 06:17:25 -08001883{
Eric Snow96c6af92015-05-29 22:21:39 -06001884 return _PyDict_FromKeys((PyObject *)type, iterable, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001885}
1886
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001887static int
1888dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001889{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890 PyObject *arg = NULL;
1891 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1894 result = -1;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001897 _Py_IDENTIFIER(keys);
1898 if (_PyObject_HasAttrId(arg, &PyId_keys))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899 result = PyDict_Merge(self, arg, 1);
1900 else
1901 result = PyDict_MergeFromSeq2(self, arg, 1);
1902 }
1903 if (result == 0 && kwds != NULL) {
1904 if (PyArg_ValidateKeywordArguments(kwds))
1905 result = PyDict_Merge(self, kwds, 1);
1906 else
1907 result = -1;
1908 }
1909 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001910}
1911
1912static PyObject *
1913dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1914{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915 if (dict_update_common(self, args, kwds, "update") != -1)
1916 Py_RETURN_NONE;
1917 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001918}
1919
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001920/* Update unconditionally replaces existing items.
1921 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001922 otherwise it leaves existing items unchanged.
1923
1924 PyDict_{Update,Merge} update/merge from a mapping object.
1925
Tim Petersf582b822001-12-11 18:51:08 +00001926 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001927 producing iterable objects of length 2.
1928*/
1929
Tim Petersf582b822001-12-11 18:51:08 +00001930int
Tim Peters1fc240e2001-10-26 05:06:50 +00001931PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1932{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001933 PyObject *it; /* iter(seq2) */
1934 Py_ssize_t i; /* index into seq2 of current element */
1935 PyObject *item; /* seq2[i] */
1936 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00001937
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938 assert(d != NULL);
1939 assert(PyDict_Check(d));
1940 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00001941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001942 it = PyObject_GetIter(seq2);
1943 if (it == NULL)
1944 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001946 for (i = 0; ; ++i) {
1947 PyObject *key, *value;
1948 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001950 fast = NULL;
1951 item = PyIter_Next(it);
1952 if (item == NULL) {
1953 if (PyErr_Occurred())
1954 goto Fail;
1955 break;
1956 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001958 /* Convert item to sequence, and verify length 2. */
1959 fast = PySequence_Fast(item, "");
1960 if (fast == NULL) {
1961 if (PyErr_ExceptionMatches(PyExc_TypeError))
1962 PyErr_Format(PyExc_TypeError,
1963 "cannot convert dictionary update "
1964 "sequence element #%zd to a sequence",
1965 i);
1966 goto Fail;
1967 }
1968 n = PySequence_Fast_GET_SIZE(fast);
1969 if (n != 2) {
1970 PyErr_Format(PyExc_ValueError,
1971 "dictionary update sequence element #%zd "
1972 "has length %zd; 2 is required",
1973 i, n);
1974 goto Fail;
1975 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001977 /* Update/merge with this (key, value) pair. */
1978 key = PySequence_Fast_GET_ITEM(fast, 0);
1979 value = PySequence_Fast_GET_ITEM(fast, 1);
1980 if (override || PyDict_GetItem(d, key) == NULL) {
1981 int status = PyDict_SetItem(d, key, value);
1982 if (status < 0)
1983 goto Fail;
1984 }
1985 Py_DECREF(fast);
1986 Py_DECREF(item);
1987 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989 i = 0;
1990 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00001991Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001992 Py_XDECREF(item);
1993 Py_XDECREF(fast);
1994 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001995Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001996 Py_DECREF(it);
1997 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001998}
1999
Tim Peters6d6c1a32001-08-02 04:15:00 +00002000int
2001PyDict_Update(PyObject *a, PyObject *b)
2002{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003 return PyDict_Merge(a, b, 1);
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002004}
2005
2006int
2007PyDict_Merge(PyObject *a, PyObject *b, int override)
2008{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002009 PyDictObject *mp, *other;
2010 Py_ssize_t i, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002011 PyDictKeyEntry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002012
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002013 /* We accept for the argument either a concrete dictionary object,
2014 * or an abstract "mapping" object. For the former, we can do
2015 * things quite efficiently. For the latter, we only require that
2016 * PyMapping_Keys() and PyObject_GetItem() be supported.
2017 */
2018 if (a == NULL || !PyDict_Check(a) || b == NULL) {
2019 PyErr_BadInternalCall();
2020 return -1;
2021 }
2022 mp = (PyDictObject*)a;
2023 if (PyDict_Check(b)) {
2024 other = (PyDictObject*)b;
2025 if (other == mp || other->ma_used == 0)
2026 /* a.update(a) or a.update({}); nothing to do */
2027 return 0;
2028 if (mp->ma_used == 0)
2029 /* Since the target dict is empty, PyDict_GetItem()
2030 * always returns NULL. Setting override to 1
2031 * skips the unnecessary test.
2032 */
2033 override = 1;
2034 /* Do one big resize at the start, rather than
2035 * incrementally resizing as we insert new items. Expect
2036 * that there will be no (or few) overlapping keys.
2037 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002038 if (mp->ma_keys->dk_usable * 3 < other->ma_used * 2)
2039 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002040 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002041 for (i = 0, n = DK_SIZE(other->ma_keys); i < n; i++) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002042 PyObject *key, *value;
2043 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002044 entry = &other->ma_keys->dk_entries[i];
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002045 key = entry->me_key;
2046 hash = entry->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002047 if (other->ma_values)
2048 value = other->ma_values[i];
2049 else
2050 value = entry->me_value;
2051
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002052 if (value != NULL) {
2053 int err = 0;
2054 Py_INCREF(key);
2055 Py_INCREF(value);
2056 if (override || PyDict_GetItem(a, key) == NULL)
2057 err = insertdict(mp, key, hash, value);
2058 Py_DECREF(value);
2059 Py_DECREF(key);
2060 if (err != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061 return -1;
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002062
2063 if (n != DK_SIZE(other->ma_keys)) {
2064 PyErr_SetString(PyExc_RuntimeError,
2065 "dict mutated during update");
2066 return -1;
2067 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002068 }
2069 }
2070 }
2071 else {
2072 /* Do it the generic, slower way */
2073 PyObject *keys = PyMapping_Keys(b);
2074 PyObject *iter;
2075 PyObject *key, *value;
2076 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002078 if (keys == NULL)
2079 /* Docstring says this is equivalent to E.keys() so
2080 * if E doesn't have a .keys() method we want
2081 * AttributeError to percolate up. Might as well
2082 * do the same for any other error.
2083 */
2084 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002085
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002086 iter = PyObject_GetIter(keys);
2087 Py_DECREF(keys);
2088 if (iter == NULL)
2089 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002090
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002091 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
2092 if (!override && PyDict_GetItem(a, key) != NULL) {
2093 Py_DECREF(key);
2094 continue;
2095 }
2096 value = PyObject_GetItem(b, key);
2097 if (value == NULL) {
2098 Py_DECREF(iter);
2099 Py_DECREF(key);
2100 return -1;
2101 }
2102 status = PyDict_SetItem(a, key, value);
2103 Py_DECREF(key);
2104 Py_DECREF(value);
2105 if (status < 0) {
2106 Py_DECREF(iter);
2107 return -1;
2108 }
2109 }
2110 Py_DECREF(iter);
2111 if (PyErr_Occurred())
2112 /* Iterator completed, via error */
2113 return -1;
2114 }
2115 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002116}
2117
2118static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002119dict_copy(PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002120{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002121 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002122}
2123
2124PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002125PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002126{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002127 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002128 PyDictObject *mp;
2129 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002130
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002131 if (o == NULL || !PyDict_Check(o)) {
2132 PyErr_BadInternalCall();
2133 return NULL;
2134 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002135 mp = (PyDictObject *)o;
2136 if (_PyDict_HasSplitTable(mp)) {
2137 PyDictObject *split_copy;
2138 PyObject **newvalues = new_values(DK_SIZE(mp->ma_keys));
2139 if (newvalues == NULL)
2140 return PyErr_NoMemory();
2141 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2142 if (split_copy == NULL) {
2143 free_values(newvalues);
2144 return NULL;
2145 }
2146 split_copy->ma_values = newvalues;
2147 split_copy->ma_keys = mp->ma_keys;
2148 split_copy->ma_used = mp->ma_used;
2149 DK_INCREF(mp->ma_keys);
2150 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2151 PyObject *value = mp->ma_values[i];
2152 Py_XINCREF(value);
2153 split_copy->ma_values[i] = value;
2154 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002155 if (_PyObject_GC_IS_TRACKED(mp))
2156 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002157 return (PyObject *)split_copy;
2158 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002159 copy = PyDict_New();
2160 if (copy == NULL)
2161 return NULL;
2162 if (PyDict_Merge(copy, o, 1) == 0)
2163 return copy;
2164 Py_DECREF(copy);
2165 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002166}
2167
Martin v. Löwis18e16552006-02-15 17:27:45 +00002168Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002169PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002170{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002171 if (mp == NULL || !PyDict_Check(mp)) {
2172 PyErr_BadInternalCall();
2173 return -1;
2174 }
2175 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002176}
2177
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002178PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002179PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002180{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002181 if (mp == NULL || !PyDict_Check(mp)) {
2182 PyErr_BadInternalCall();
2183 return NULL;
2184 }
2185 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002186}
2187
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002188PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002189PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002190{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002191 if (mp == NULL || !PyDict_Check(mp)) {
2192 PyErr_BadInternalCall();
2193 return NULL;
2194 }
2195 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002196}
2197
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002198PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002199PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002200{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002201 if (mp == NULL || !PyDict_Check(mp)) {
2202 PyErr_BadInternalCall();
2203 return NULL;
2204 }
2205 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002206}
2207
Tim Peterse63415e2001-05-08 04:38:29 +00002208/* Return 1 if dicts equal, 0 if not, -1 if error.
2209 * Gets out as soon as any difference is detected.
2210 * Uses only Py_EQ comparison.
2211 */
2212static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002213dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002214{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002215 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002217 if (a->ma_used != b->ma_used)
2218 /* can't be equal if # of entries differ */
2219 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002220 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002221 for (i = 0; i < DK_SIZE(a->ma_keys); i++) {
2222 PyDictKeyEntry *ep = &a->ma_keys->dk_entries[i];
2223 PyObject *aval;
2224 if (a->ma_values)
2225 aval = a->ma_values[i];
2226 else
2227 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228 if (aval != NULL) {
2229 int cmp;
2230 PyObject *bval;
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002231 PyObject **vaddr;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002232 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002233 /* temporarily bump aval's refcount to ensure it stays
2234 alive until we're done with it */
2235 Py_INCREF(aval);
2236 /* ditto for key */
2237 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002238 /* reuse the known hash value */
2239 if ((b->ma_keys->dk_lookup)(b, key, ep->me_hash, &vaddr) == NULL)
2240 bval = NULL;
2241 else
2242 bval = *vaddr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002243 Py_DECREF(key);
2244 if (bval == NULL) {
2245 Py_DECREF(aval);
2246 if (PyErr_Occurred())
2247 return -1;
2248 return 0;
2249 }
2250 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
2251 Py_DECREF(aval);
2252 if (cmp <= 0) /* error or not equal */
2253 return cmp;
2254 }
2255 }
2256 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002257}
Tim Peterse63415e2001-05-08 04:38:29 +00002258
2259static PyObject *
2260dict_richcompare(PyObject *v, PyObject *w, int op)
2261{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002262 int cmp;
2263 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002265 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2266 res = Py_NotImplemented;
2267 }
2268 else if (op == Py_EQ || op == Py_NE) {
2269 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2270 if (cmp < 0)
2271 return NULL;
2272 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2273 }
2274 else
2275 res = Py_NotImplemented;
2276 Py_INCREF(res);
2277 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002278}
Tim Peterse63415e2001-05-08 04:38:29 +00002279
Larry Hastings61272b72014-01-07 12:41:53 -08002280/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002281
2282@coexist
2283dict.__contains__
2284
2285 key: object
2286 /
2287
Meador Ingee02de8c2014-01-14 16:48:31 -06002288True if D has a key k, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002289[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002290
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002291static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002292dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002293/*[clinic end generated code: output=a3d03db709ed6e6b input=b852b2a19b51ab24]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002294{
Larry Hastingsc2047262014-01-25 20:43:29 -08002295 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002296 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002297 PyDictKeyEntry *ep;
2298 PyObject **value_addr;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002300 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002301 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002302 hash = PyObject_Hash(key);
2303 if (hash == -1)
2304 return NULL;
2305 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002306 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 if (ep == NULL)
2308 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002309 return PyBool_FromLong(*value_addr != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002310}
2311
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002312static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002313dict_get(PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00002314{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002315 PyObject *key;
2316 PyObject *failobj = Py_None;
2317 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002318 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002319 PyDictKeyEntry *ep;
2320 PyObject **value_addr;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002322 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
2323 return NULL;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002325 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002326 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002327 hash = PyObject_Hash(key);
2328 if (hash == -1)
2329 return NULL;
2330 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002331 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002332 if (ep == NULL)
2333 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002334 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002335 if (val == NULL)
2336 val = failobj;
2337 Py_INCREF(val);
2338 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002339}
2340
Benjamin Peterson00e98862013-03-07 22:16:29 -05002341PyObject *
2342PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002343{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002344 PyDictObject *mp = (PyDictObject *)d;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002345 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002346 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002347 PyDictKeyEntry *ep;
2348 PyObject **value_addr;
Guido van Rossum164452c2000-08-08 16:12:54 +00002349
Benjamin Peterson00e98862013-03-07 22:16:29 -05002350 if (!PyDict_Check(d)) {
2351 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002352 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002353 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002355 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002356 hash = PyObject_Hash(key);
2357 if (hash == -1)
2358 return NULL;
2359 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002360 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 if (ep == NULL)
2362 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002363 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 if (val == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002365 if (mp->ma_keys->dk_usable <= 0) {
2366 /* Need to resize. */
2367 if (insertion_resize(mp) < 0)
2368 return NULL;
2369 ep = find_empty_slot(mp, key, hash, &value_addr);
2370 }
Benjamin Peterson00e98862013-03-07 22:16:29 -05002371 Py_INCREF(defaultobj);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002372 Py_INCREF(key);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002373 MAINTAIN_TRACKING(mp, key, defaultobj);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002374 ep->me_key = key;
2375 ep->me_hash = hash;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002376 *value_addr = defaultobj;
2377 val = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002378 mp->ma_keys->dk_usable--;
2379 mp->ma_used++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002380 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002381 return val;
Guido van Rossum164452c2000-08-08 16:12:54 +00002382}
2383
Benjamin Peterson00e98862013-03-07 22:16:29 -05002384static PyObject *
2385dict_setdefault(PyDictObject *mp, PyObject *args)
2386{
2387 PyObject *key, *val;
2388 PyObject *defaultobj = Py_None;
2389
2390 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &defaultobj))
2391 return NULL;
2392
Benjamin Peterson55898502013-03-08 08:36:49 -05002393 val = PyDict_SetDefault((PyObject *)mp, key, defaultobj);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002394 Py_XINCREF(val);
2395 return val;
2396}
Guido van Rossum164452c2000-08-08 16:12:54 +00002397
2398static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002399dict_clear(PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002400{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 PyDict_Clear((PyObject *)mp);
2402 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002403}
2404
Guido van Rossumba6ab842000-12-12 22:02:18 +00002405static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002406dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002407{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002408 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00002409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002410 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2411 return NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06002412
2413 return _PyDict_Pop(mp, key, deflt);
Guido van Rossume027d982002-04-12 15:11:59 +00002414}
2415
2416static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002417dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00002418{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002419 Py_hash_t i = 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002420 PyDictKeyEntry *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002421 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002422
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002424 /* Allocate the result tuple before checking the size. Believe it
2425 * or not, this allocation could trigger a garbage collection which
2426 * could empty the dict, so if we checked the size first and that
2427 * happened, the result would be an infinite loop (searching for an
2428 * entry that no longer exists). Note that the usual popitem()
2429 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2430 * tuple away if the dict *is* empty isn't a significant
2431 * inefficiency -- possible, but unlikely in practice.
2432 */
2433 res = PyTuple_New(2);
2434 if (res == NULL)
2435 return NULL;
2436 if (mp->ma_used == 0) {
2437 Py_DECREF(res);
2438 PyErr_SetString(PyExc_KeyError,
2439 "popitem(): dictionary is empty");
2440 return NULL;
2441 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002442 /* Convert split table to combined table */
2443 if (mp->ma_keys->dk_lookup == lookdict_split) {
2444 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
2445 Py_DECREF(res);
2446 return NULL;
2447 }
2448 }
2449 ENSURE_ALLOWS_DELETIONS(mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002450 /* Set ep to "the first" dict entry with a value. We abuse the hash
2451 * field of slot 0 to hold a search finger:
2452 * If slot 0 has a value, use slot 0.
2453 * Else slot 0 is being used to hold a search finger,
2454 * and we use its hash value as the first index to look.
2455 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002456 ep = &mp->ma_keys->dk_entries[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002457 if (ep->me_value == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002458 Py_ssize_t mask = DK_MASK(mp->ma_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002459 i = ep->me_hash;
2460 /* The hash field may be a real hash value, or it may be a
2461 * legit search finger, or it may be a once-legit search
2462 * finger that's out of bounds now because it wrapped around
2463 * or the table shrunk -- simply make sure it's in bounds now.
2464 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002465 if (i > mask || i < 1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002466 i = 1; /* skip slot 0 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002467 while ((ep = &mp->ma_keys->dk_entries[i])->me_value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002468 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002469 if (i > mask)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002470 i = 1;
2471 }
2472 }
2473 PyTuple_SET_ITEM(res, 0, ep->me_key);
2474 PyTuple_SET_ITEM(res, 1, ep->me_value);
2475 Py_INCREF(dummy);
2476 ep->me_key = dummy;
2477 ep->me_value = NULL;
2478 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002479 assert(mp->ma_keys->dk_entries[0].me_value == NULL);
2480 mp->ma_keys->dk_entries[0].me_hash = i + 1; /* next place to start */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002481 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002482}
2483
Jeremy Hylton8caad492000-06-23 14:18:11 +00002484static int
2485dict_traverse(PyObject *op, visitproc visit, void *arg)
2486{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002487 Py_ssize_t i, n;
2488 PyDictObject *mp = (PyDictObject *)op;
2489 if (mp->ma_keys->dk_lookup == lookdict) {
2490 for (i = 0; i < DK_SIZE(mp->ma_keys); i++) {
2491 if (mp->ma_keys->dk_entries[i].me_value != NULL) {
2492 Py_VISIT(mp->ma_keys->dk_entries[i].me_value);
2493 Py_VISIT(mp->ma_keys->dk_entries[i].me_key);
2494 }
2495 }
2496 } else {
2497 if (mp->ma_values != NULL) {
2498 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2499 Py_VISIT(mp->ma_values[i]);
2500 }
2501 }
2502 else {
2503 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2504 Py_VISIT(mp->ma_keys->dk_entries[i].me_value);
2505 }
2506 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002507 }
2508 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002509}
2510
2511static int
2512dict_tp_clear(PyObject *op)
2513{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002514 PyDict_Clear(op);
2515 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002516}
2517
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002518static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002519
Eric Snow96c6af92015-05-29 22:21:39 -06002520PyObject *
2521_PyDict_SizeOf(PyDictObject *mp)
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002522{
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002523 Py_ssize_t size, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002524
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002525 size = DK_SIZE(mp->ma_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002526 res = sizeof(PyDictObject);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002527 if (mp->ma_values)
2528 res += size * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002529 /* If the dictionary is split, the keys portion is accounted-for
2530 in the type object. */
2531 if (mp->ma_keys->dk_refcnt == 1)
2532 res += sizeof(PyDictKeysObject) + (size-1) * sizeof(PyDictKeyEntry);
2533 return PyLong_FromSsize_t(res);
2534}
2535
2536Py_ssize_t
2537_PyDict_KeysSize(PyDictKeysObject *keys)
2538{
2539 return sizeof(PyDictKeysObject) + (DK_SIZE(keys)-1) * sizeof(PyDictKeyEntry);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002540}
2541
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002542PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
2543
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002544PyDoc_STRVAR(sizeof__doc__,
2545"D.__sizeof__() -> size of D in memory, in bytes");
2546
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002547PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002548"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002549
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002550PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002551"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 +00002552
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002553PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002554"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002555If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00002556
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002557PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00002558"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000025592-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002560
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002561PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04002562"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
2563If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
2564If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
2565In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00002566
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002567PyDoc_STRVAR(clear__doc__,
2568"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002569
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002570PyDoc_STRVAR(copy__doc__,
2571"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00002572
Guido van Rossumb90c8482007-02-10 01:11:45 +00002573/* Forward */
2574static PyObject *dictkeys_new(PyObject *);
2575static PyObject *dictitems_new(PyObject *);
2576static PyObject *dictvalues_new(PyObject *);
2577
Guido van Rossum45c85d12007-07-27 16:31:40 +00002578PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002579 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002580PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002581 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002582PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002583 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00002584
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002585static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07002586 DICT___CONTAINS___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002587 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
2588 getitem__doc__},
Eric Snow96c6af92015-05-29 22:21:39 -06002589 {"__sizeof__", (PyCFunction)_PyDict_SizeOf, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002590 sizeof__doc__},
2591 {"get", (PyCFunction)dict_get, METH_VARARGS,
2592 get__doc__},
2593 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
2594 setdefault_doc__},
2595 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
2596 pop__doc__},
2597 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
2598 popitem__doc__},
2599 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
2600 keys__doc__},
2601 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
2602 items__doc__},
2603 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
2604 values__doc__},
2605 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
2606 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08002607 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002608 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
2609 clear__doc__},
2610 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
2611 copy__doc__},
2612 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002613};
2614
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002615/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002616int
2617PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002618{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002619 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002620 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002621 PyDictKeyEntry *ep;
2622 PyObject **value_addr;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002624 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002625 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002626 hash = PyObject_Hash(key);
2627 if (hash == -1)
2628 return -1;
2629 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002630 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
2631 return (ep == NULL) ? -1 : (*value_addr != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002632}
2633
Thomas Wouterscf297e42007-02-23 15:07:44 +00002634/* Internal version of PyDict_Contains used when the hash value is already known */
2635int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002636_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00002637{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002638 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002639 PyDictKeyEntry *ep;
2640 PyObject **value_addr;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002641
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002642 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
2643 return (ep == NULL) ? -1 : (*value_addr != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00002644}
2645
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002646/* Hack to implement "key in dict" */
2647static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002648 0, /* sq_length */
2649 0, /* sq_concat */
2650 0, /* sq_repeat */
2651 0, /* sq_item */
2652 0, /* sq_slice */
2653 0, /* sq_ass_item */
2654 0, /* sq_ass_slice */
2655 PyDict_Contains, /* sq_contains */
2656 0, /* sq_inplace_concat */
2657 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002658};
2659
Guido van Rossum09e563a2001-05-01 12:10:21 +00002660static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002661dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2662{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002663 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02002664 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002665
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002666 assert(type != NULL && type->tp_alloc != NULL);
2667 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02002668 if (self == NULL)
2669 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02002670 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02002671
Victor Stinnera9f61a52013-07-16 22:17:26 +02002672 /* The object has been implicitly tracked by tp_alloc */
2673 if (type == &PyDict_Type)
2674 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02002675
2676 d->ma_used = 0;
2677 d->ma_keys = new_keys_object(PyDict_MINSIZE_COMBINED);
2678 if (d->ma_keys == NULL) {
2679 Py_DECREF(self);
2680 return NULL;
2681 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002682 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002683}
2684
Tim Peters25786c02001-09-02 08:22:48 +00002685static int
2686dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2687{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002688 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002689}
2690
Tim Peters6d6c1a32001-08-02 04:15:00 +00002691static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002692dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002693{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002694 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002695}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002696
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002697PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002698"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002699"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002700" (key, value) pairs\n"
2701"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002702" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002703" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002704" d[k] = v\n"
2705"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2706" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002707
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002708PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002709 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2710 "dict",
2711 sizeof(PyDictObject),
2712 0,
2713 (destructor)dict_dealloc, /* tp_dealloc */
2714 0, /* tp_print */
2715 0, /* tp_getattr */
2716 0, /* tp_setattr */
2717 0, /* tp_reserved */
2718 (reprfunc)dict_repr, /* tp_repr */
2719 0, /* tp_as_number */
2720 &dict_as_sequence, /* tp_as_sequence */
2721 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002722 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002723 0, /* tp_call */
2724 0, /* tp_str */
2725 PyObject_GenericGetAttr, /* tp_getattro */
2726 0, /* tp_setattro */
2727 0, /* tp_as_buffer */
2728 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2729 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
2730 dictionary_doc, /* tp_doc */
2731 dict_traverse, /* tp_traverse */
2732 dict_tp_clear, /* tp_clear */
2733 dict_richcompare, /* tp_richcompare */
2734 0, /* tp_weaklistoffset */
2735 (getiterfunc)dict_iter, /* tp_iter */
2736 0, /* tp_iternext */
2737 mapp_methods, /* tp_methods */
2738 0, /* tp_members */
2739 0, /* tp_getset */
2740 0, /* tp_base */
2741 0, /* tp_dict */
2742 0, /* tp_descr_get */
2743 0, /* tp_descr_set */
2744 0, /* tp_dictoffset */
2745 dict_init, /* tp_init */
2746 PyType_GenericAlloc, /* tp_alloc */
2747 dict_new, /* tp_new */
2748 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002749};
2750
Victor Stinner3c1e4812012-03-26 22:10:51 +02002751PyObject *
2752_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
2753{
2754 PyObject *kv;
2755 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02002756 if (kv == NULL) {
2757 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002758 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02002759 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02002760 return PyDict_GetItem(dp, kv);
2761}
2762
Guido van Rossum3cca2451997-05-16 14:23:33 +00002763/* For backward compatibility with old dictionary interface */
2764
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002765PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002766PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002767{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002768 PyObject *kv, *rv;
2769 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02002770 if (kv == NULL) {
2771 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002772 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02002773 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002774 rv = PyDict_GetItem(v, kv);
2775 Py_DECREF(kv);
2776 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002777}
2778
2779int
Victor Stinner3c1e4812012-03-26 22:10:51 +02002780_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
2781{
2782 PyObject *kv;
2783 kv = _PyUnicode_FromId(key); /* borrowed */
2784 if (kv == NULL)
2785 return -1;
2786 return PyDict_SetItem(v, kv, item);
2787}
2788
2789int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002790PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002791{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002792 PyObject *kv;
2793 int err;
2794 kv = PyUnicode_FromString(key);
2795 if (kv == NULL)
2796 return -1;
2797 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
2798 err = PyDict_SetItem(v, kv, item);
2799 Py_DECREF(kv);
2800 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002801}
2802
2803int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01002804_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
2805{
2806 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
2807 if (kv == NULL)
2808 return -1;
2809 return PyDict_DelItem(v, kv);
2810}
2811
2812int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002813PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002814{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002815 PyObject *kv;
2816 int err;
2817 kv = PyUnicode_FromString(key);
2818 if (kv == NULL)
2819 return -1;
2820 err = PyDict_DelItem(v, kv);
2821 Py_DECREF(kv);
2822 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002823}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002824
Raymond Hettinger019a1482004-03-18 02:41:19 +00002825/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002826
2827typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002828 PyObject_HEAD
2829 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
2830 Py_ssize_t di_used;
2831 Py_ssize_t di_pos;
2832 PyObject* di_result; /* reusable result tuple for iteritems */
2833 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002834} dictiterobject;
2835
2836static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002837dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002838{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002839 dictiterobject *di;
2840 di = PyObject_GC_New(dictiterobject, itertype);
2841 if (di == NULL)
2842 return NULL;
2843 Py_INCREF(dict);
2844 di->di_dict = dict;
2845 di->di_used = dict->ma_used;
2846 di->di_pos = 0;
2847 di->len = dict->ma_used;
2848 if (itertype == &PyDictIterItem_Type) {
2849 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2850 if (di->di_result == NULL) {
2851 Py_DECREF(di);
2852 return NULL;
2853 }
2854 }
2855 else
2856 di->di_result = NULL;
2857 _PyObject_GC_TRACK(di);
2858 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002859}
2860
2861static void
2862dictiter_dealloc(dictiterobject *di)
2863{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002864 Py_XDECREF(di->di_dict);
2865 Py_XDECREF(di->di_result);
2866 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002867}
2868
2869static int
2870dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
2871{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002872 Py_VISIT(di->di_dict);
2873 Py_VISIT(di->di_result);
2874 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002875}
2876
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002877static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002878dictiter_len(dictiterobject *di)
2879{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002880 Py_ssize_t len = 0;
2881 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
2882 len = di->len;
2883 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002884}
2885
Guido van Rossumb90c8482007-02-10 01:11:45 +00002886PyDoc_STRVAR(length_hint_doc,
2887 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002888
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002889static PyObject *
2890dictiter_reduce(dictiterobject *di);
2891
2892PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
2893
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002894static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002895 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
2896 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002897 {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS,
2898 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002899 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002900};
2901
Raymond Hettinger019a1482004-03-18 02:41:19 +00002902static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002903{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002904 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002905 Py_ssize_t i, mask, offset;
2906 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002907 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002908 PyObject **value_ptr;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002909
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002910 if (d == NULL)
2911 return NULL;
2912 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002913
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002914 if (di->di_used != d->ma_used) {
2915 PyErr_SetString(PyExc_RuntimeError,
2916 "dictionary changed size during iteration");
2917 di->di_used = -1; /* Make this state sticky */
2918 return NULL;
2919 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002921 i = di->di_pos;
2922 if (i < 0)
2923 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002924 k = d->ma_keys;
2925 if (d->ma_values) {
2926 value_ptr = &d->ma_values[i];
2927 offset = sizeof(PyObject *);
2928 }
2929 else {
2930 value_ptr = &k->dk_entries[i].me_value;
2931 offset = sizeof(PyDictKeyEntry);
2932 }
2933 mask = DK_SIZE(k)-1;
2934 while (i <= mask && *value_ptr == NULL) {
2935 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002936 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002937 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002938 di->di_pos = i+1;
2939 if (i > mask)
2940 goto fail;
2941 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002942 key = k->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002943 Py_INCREF(key);
2944 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002945
2946fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002947 Py_DECREF(d);
2948 di->di_dict = NULL;
2949 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002950}
2951
Raymond Hettinger019a1482004-03-18 02:41:19 +00002952PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002953 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2954 "dict_keyiterator", /* tp_name */
2955 sizeof(dictiterobject), /* tp_basicsize */
2956 0, /* tp_itemsize */
2957 /* methods */
2958 (destructor)dictiter_dealloc, /* tp_dealloc */
2959 0, /* tp_print */
2960 0, /* tp_getattr */
2961 0, /* tp_setattr */
2962 0, /* tp_reserved */
2963 0, /* tp_repr */
2964 0, /* tp_as_number */
2965 0, /* tp_as_sequence */
2966 0, /* tp_as_mapping */
2967 0, /* tp_hash */
2968 0, /* tp_call */
2969 0, /* tp_str */
2970 PyObject_GenericGetAttr, /* tp_getattro */
2971 0, /* tp_setattro */
2972 0, /* tp_as_buffer */
2973 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2974 0, /* tp_doc */
2975 (traverseproc)dictiter_traverse, /* tp_traverse */
2976 0, /* tp_clear */
2977 0, /* tp_richcompare */
2978 0, /* tp_weaklistoffset */
2979 PyObject_SelfIter, /* tp_iter */
2980 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
2981 dictiter_methods, /* tp_methods */
2982 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002983};
2984
2985static PyObject *dictiter_iternextvalue(dictiterobject *di)
2986{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002987 PyObject *value;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002988 Py_ssize_t i, mask, offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002989 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002990 PyObject **value_ptr;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002992 if (d == NULL)
2993 return NULL;
2994 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00002995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002996 if (di->di_used != d->ma_used) {
2997 PyErr_SetString(PyExc_RuntimeError,
2998 "dictionary changed size during iteration");
2999 di->di_used = -1; /* Make this state sticky */
3000 return NULL;
3001 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003003 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003004 mask = DK_SIZE(d->ma_keys)-1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003005 if (i < 0 || i > mask)
3006 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003007 if (d->ma_values) {
3008 value_ptr = &d->ma_values[i];
3009 offset = sizeof(PyObject *);
3010 }
3011 else {
3012 value_ptr = &d->ma_keys->dk_entries[i].me_value;
3013 offset = sizeof(PyDictKeyEntry);
3014 }
3015 while (i <= mask && *value_ptr == NULL) {
3016 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003017 i++;
3018 if (i > mask)
3019 goto fail;
3020 }
3021 di->di_pos = i+1;
3022 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003023 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003024 Py_INCREF(value);
3025 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003026
3027fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003028 Py_DECREF(d);
3029 di->di_dict = NULL;
3030 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003031}
3032
3033PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003034 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3035 "dict_valueiterator", /* tp_name */
3036 sizeof(dictiterobject), /* tp_basicsize */
3037 0, /* tp_itemsize */
3038 /* methods */
3039 (destructor)dictiter_dealloc, /* tp_dealloc */
3040 0, /* tp_print */
3041 0, /* tp_getattr */
3042 0, /* tp_setattr */
3043 0, /* tp_reserved */
3044 0, /* tp_repr */
3045 0, /* tp_as_number */
3046 0, /* tp_as_sequence */
3047 0, /* tp_as_mapping */
3048 0, /* tp_hash */
3049 0, /* tp_call */
3050 0, /* tp_str */
3051 PyObject_GenericGetAttr, /* tp_getattro */
3052 0, /* tp_setattro */
3053 0, /* tp_as_buffer */
3054 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3055 0, /* tp_doc */
3056 (traverseproc)dictiter_traverse, /* tp_traverse */
3057 0, /* tp_clear */
3058 0, /* tp_richcompare */
3059 0, /* tp_weaklistoffset */
3060 PyObject_SelfIter, /* tp_iter */
3061 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3062 dictiter_methods, /* tp_methods */
3063 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003064};
3065
3066static PyObject *dictiter_iternextitem(dictiterobject *di)
3067{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003068 PyObject *key, *value, *result = di->di_result;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003069 Py_ssize_t i, mask, offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003070 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003071 PyObject **value_ptr;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003073 if (d == NULL)
3074 return NULL;
3075 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003077 if (di->di_used != d->ma_used) {
3078 PyErr_SetString(PyExc_RuntimeError,
3079 "dictionary changed size during iteration");
3080 di->di_used = -1; /* Make this state sticky */
3081 return NULL;
3082 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003083
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003084 i = di->di_pos;
3085 if (i < 0)
3086 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003087 mask = DK_SIZE(d->ma_keys)-1;
3088 if (d->ma_values) {
3089 value_ptr = &d->ma_values[i];
3090 offset = sizeof(PyObject *);
3091 }
3092 else {
3093 value_ptr = &d->ma_keys->dk_entries[i].me_value;
3094 offset = sizeof(PyDictKeyEntry);
3095 }
3096 while (i <= mask && *value_ptr == NULL) {
3097 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003098 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003099 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003100 di->di_pos = i+1;
3101 if (i > mask)
3102 goto fail;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003104 if (result->ob_refcnt == 1) {
3105 Py_INCREF(result);
3106 Py_DECREF(PyTuple_GET_ITEM(result, 0));
3107 Py_DECREF(PyTuple_GET_ITEM(result, 1));
3108 } else {
3109 result = PyTuple_New(2);
3110 if (result == NULL)
3111 return NULL;
3112 }
3113 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003114 key = d->ma_keys->dk_entries[i].me_key;
3115 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003116 Py_INCREF(key);
3117 Py_INCREF(value);
Eric Snow96c6af92015-05-29 22:21:39 -06003118 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3119 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003120 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003121
3122fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003123 Py_DECREF(d);
3124 di->di_dict = NULL;
3125 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003126}
3127
3128PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003129 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3130 "dict_itemiterator", /* tp_name */
3131 sizeof(dictiterobject), /* tp_basicsize */
3132 0, /* tp_itemsize */
3133 /* methods */
3134 (destructor)dictiter_dealloc, /* tp_dealloc */
3135 0, /* tp_print */
3136 0, /* tp_getattr */
3137 0, /* tp_setattr */
3138 0, /* tp_reserved */
3139 0, /* tp_repr */
3140 0, /* tp_as_number */
3141 0, /* tp_as_sequence */
3142 0, /* tp_as_mapping */
3143 0, /* tp_hash */
3144 0, /* tp_call */
3145 0, /* tp_str */
3146 PyObject_GenericGetAttr, /* tp_getattro */
3147 0, /* tp_setattro */
3148 0, /* tp_as_buffer */
3149 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3150 0, /* tp_doc */
3151 (traverseproc)dictiter_traverse, /* tp_traverse */
3152 0, /* tp_clear */
3153 0, /* tp_richcompare */
3154 0, /* tp_weaklistoffset */
3155 PyObject_SelfIter, /* tp_iter */
3156 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3157 dictiter_methods, /* tp_methods */
3158 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003159};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003160
3161
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003162static PyObject *
3163dictiter_reduce(dictiterobject *di)
3164{
3165 PyObject *list;
3166 dictiterobject tmp;
3167
3168 list = PyList_New(0);
3169 if (!list)
3170 return NULL;
3171
3172 /* copy the itertor state */
3173 tmp = *di;
3174 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003175
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003176 /* iterate the temporary into a list */
3177 for(;;) {
3178 PyObject *element = 0;
3179 if (Py_TYPE(di) == &PyDictIterItem_Type)
3180 element = dictiter_iternextitem(&tmp);
3181 else if (Py_TYPE(di) == &PyDictIterKey_Type)
3182 element = dictiter_iternextkey(&tmp);
3183 else if (Py_TYPE(di) == &PyDictIterValue_Type)
3184 element = dictiter_iternextvalue(&tmp);
3185 else
3186 assert(0);
3187 if (element) {
3188 if (PyList_Append(list, element)) {
3189 Py_DECREF(element);
3190 Py_DECREF(list);
3191 Py_XDECREF(tmp.di_dict);
3192 return NULL;
3193 }
3194 Py_DECREF(element);
3195 } else
3196 break;
3197 }
3198 Py_XDECREF(tmp.di_dict);
3199 /* check for error */
3200 if (tmp.di_dict != NULL) {
3201 /* we have an error */
3202 Py_DECREF(list);
3203 return NULL;
3204 }
Antoine Pitroua7013882012-04-05 00:04:20 +02003205 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003206}
3207
Guido van Rossum3ac67412007-02-10 18:55:06 +00003208/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003209/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003210/***********************************************/
3211
Guido van Rossumb90c8482007-02-10 01:11:45 +00003212/* The instance lay-out is the same for all three; but the type differs. */
3213
Guido van Rossumb90c8482007-02-10 01:11:45 +00003214static void
Eric Snow96c6af92015-05-29 22:21:39 -06003215dictview_dealloc(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003216{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003217 Py_XDECREF(dv->dv_dict);
3218 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003219}
3220
3221static int
Eric Snow96c6af92015-05-29 22:21:39 -06003222dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003223{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003224 Py_VISIT(dv->dv_dict);
3225 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003226}
3227
Guido van Rossum83825ac2007-02-10 04:54:19 +00003228static Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003229dictview_len(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003230{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003231 Py_ssize_t len = 0;
3232 if (dv->dv_dict != NULL)
3233 len = dv->dv_dict->ma_used;
3234 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003235}
3236
Eric Snow96c6af92015-05-29 22:21:39 -06003237PyObject *
3238_PyDictView_New(PyObject *dict, PyTypeObject *type)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003239{
Eric Snow96c6af92015-05-29 22:21:39 -06003240 _PyDictViewObject *dv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003241 if (dict == NULL) {
3242 PyErr_BadInternalCall();
3243 return NULL;
3244 }
3245 if (!PyDict_Check(dict)) {
3246 /* XXX Get rid of this restriction later */
3247 PyErr_Format(PyExc_TypeError,
3248 "%s() requires a dict argument, not '%s'",
3249 type->tp_name, dict->ob_type->tp_name);
3250 return NULL;
3251 }
Eric Snow96c6af92015-05-29 22:21:39 -06003252 dv = PyObject_GC_New(_PyDictViewObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003253 if (dv == NULL)
3254 return NULL;
3255 Py_INCREF(dict);
3256 dv->dv_dict = (PyDictObject *)dict;
3257 _PyObject_GC_TRACK(dv);
3258 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003259}
3260
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003261/* TODO(guido): The views objects are not complete:
3262
3263 * support more set operations
3264 * support arbitrary mappings?
3265 - either these should be static or exported in dictobject.h
3266 - if public then they should probably be in builtins
3267*/
3268
Guido van Rossumaac530c2007-08-24 22:33:45 +00003269/* Return 1 if self is a subset of other, iterating over self;
3270 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003271static int
3272all_contained_in(PyObject *self, PyObject *other)
3273{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003274 PyObject *iter = PyObject_GetIter(self);
3275 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003277 if (iter == NULL)
3278 return -1;
3279 for (;;) {
3280 PyObject *next = PyIter_Next(iter);
3281 if (next == NULL) {
3282 if (PyErr_Occurred())
3283 ok = -1;
3284 break;
3285 }
3286 ok = PySequence_Contains(other, next);
3287 Py_DECREF(next);
3288 if (ok <= 0)
3289 break;
3290 }
3291 Py_DECREF(iter);
3292 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003293}
3294
3295static PyObject *
3296dictview_richcompare(PyObject *self, PyObject *other, int op)
3297{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003298 Py_ssize_t len_self, len_other;
3299 int ok;
3300 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003302 assert(self != NULL);
3303 assert(PyDictViewSet_Check(self));
3304 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00003305
Brian Curtindfc80e32011-08-10 20:28:54 -05003306 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
3307 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003309 len_self = PyObject_Size(self);
3310 if (len_self < 0)
3311 return NULL;
3312 len_other = PyObject_Size(other);
3313 if (len_other < 0)
3314 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003316 ok = 0;
3317 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00003318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003319 case Py_NE:
3320 case Py_EQ:
3321 if (len_self == len_other)
3322 ok = all_contained_in(self, other);
3323 if (op == Py_NE && ok >= 0)
3324 ok = !ok;
3325 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003327 case Py_LT:
3328 if (len_self < len_other)
3329 ok = all_contained_in(self, other);
3330 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003332 case Py_LE:
3333 if (len_self <= len_other)
3334 ok = all_contained_in(self, other);
3335 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003337 case Py_GT:
3338 if (len_self > len_other)
3339 ok = all_contained_in(other, self);
3340 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003342 case Py_GE:
3343 if (len_self >= len_other)
3344 ok = all_contained_in(other, self);
3345 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003347 }
3348 if (ok < 0)
3349 return NULL;
3350 result = ok ? Py_True : Py_False;
3351 Py_INCREF(result);
3352 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003353}
3354
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003355static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003356dictview_repr(_PyDictViewObject *dv)
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003357{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003358 PyObject *seq;
3359 PyObject *result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003361 seq = PySequence_List((PyObject *)dv);
3362 if (seq == NULL)
3363 return NULL;
3364
3365 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
3366 Py_DECREF(seq);
3367 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003368}
3369
Guido van Rossum3ac67412007-02-10 18:55:06 +00003370/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003371
3372static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003373dictkeys_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003374{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003375 if (dv->dv_dict == NULL) {
3376 Py_RETURN_NONE;
3377 }
3378 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003379}
3380
3381static int
Eric Snow96c6af92015-05-29 22:21:39 -06003382dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00003383{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003384 if (dv->dv_dict == NULL)
3385 return 0;
3386 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003387}
3388
Guido van Rossum83825ac2007-02-10 04:54:19 +00003389static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003390 (lenfunc)dictview_len, /* sq_length */
3391 0, /* sq_concat */
3392 0, /* sq_repeat */
3393 0, /* sq_item */
3394 0, /* sq_slice */
3395 0, /* sq_ass_item */
3396 0, /* sq_ass_slice */
3397 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003398};
3399
Guido van Rossum523259b2007-08-24 23:41:22 +00003400static PyObject*
3401dictviews_sub(PyObject* self, PyObject *other)
3402{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003403 PyObject *result = PySet_New(self);
3404 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003405 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003407 if (result == NULL)
3408 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003409
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003410 tmp = _PyObject_CallMethodId(result, &PyId_difference_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003411 if (tmp == NULL) {
3412 Py_DECREF(result);
3413 return NULL;
3414 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003416 Py_DECREF(tmp);
3417 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003418}
3419
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003420PyObject*
3421_PyDictView_Intersect(PyObject* self, PyObject *other)
Guido van Rossum523259b2007-08-24 23:41:22 +00003422{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003423 PyObject *result = PySet_New(self);
3424 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003425 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003427 if (result == NULL)
3428 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003429
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003430 tmp = _PyObject_CallMethodId(result, &PyId_intersection_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003431 if (tmp == NULL) {
3432 Py_DECREF(result);
3433 return NULL;
3434 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003436 Py_DECREF(tmp);
3437 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003438}
3439
3440static PyObject*
3441dictviews_or(PyObject* self, PyObject *other)
3442{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003443 PyObject *result = PySet_New(self);
3444 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003445 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003447 if (result == NULL)
3448 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003449
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003450 tmp = _PyObject_CallMethodId(result, &PyId_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003451 if (tmp == NULL) {
3452 Py_DECREF(result);
3453 return NULL;
3454 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003456 Py_DECREF(tmp);
3457 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003458}
3459
3460static PyObject*
3461dictviews_xor(PyObject* self, PyObject *other)
3462{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003463 PyObject *result = PySet_New(self);
3464 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003465 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003466
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003467 if (result == NULL)
3468 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003469
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003470 tmp = _PyObject_CallMethodId(result, &PyId_symmetric_difference_update, "O",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003471 other);
3472 if (tmp == NULL) {
3473 Py_DECREF(result);
3474 return NULL;
3475 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003477 Py_DECREF(tmp);
3478 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003479}
3480
3481static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003482 0, /*nb_add*/
3483 (binaryfunc)dictviews_sub, /*nb_subtract*/
3484 0, /*nb_multiply*/
3485 0, /*nb_remainder*/
3486 0, /*nb_divmod*/
3487 0, /*nb_power*/
3488 0, /*nb_negative*/
3489 0, /*nb_positive*/
3490 0, /*nb_absolute*/
3491 0, /*nb_bool*/
3492 0, /*nb_invert*/
3493 0, /*nb_lshift*/
3494 0, /*nb_rshift*/
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003495 (binaryfunc)_PyDictView_Intersect, /*nb_and*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003496 (binaryfunc)dictviews_xor, /*nb_xor*/
3497 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00003498};
3499
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003500static PyObject*
3501dictviews_isdisjoint(PyObject *self, PyObject *other)
3502{
3503 PyObject *it;
3504 PyObject *item = NULL;
3505
3506 if (self == other) {
Eric Snow96c6af92015-05-29 22:21:39 -06003507 if (dictview_len((_PyDictViewObject *)self) == 0)
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003508 Py_RETURN_TRUE;
3509 else
3510 Py_RETURN_FALSE;
3511 }
3512
3513 /* Iterate over the shorter object (only if other is a set,
3514 * because PySequence_Contains may be expensive otherwise): */
3515 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
Eric Snow96c6af92015-05-29 22:21:39 -06003516 Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self);
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003517 Py_ssize_t len_other = PyObject_Size(other);
3518 if (len_other == -1)
3519 return NULL;
3520
3521 if ((len_other > len_self)) {
3522 PyObject *tmp = other;
3523 other = self;
3524 self = tmp;
3525 }
3526 }
3527
3528 it = PyObject_GetIter(other);
3529 if (it == NULL)
3530 return NULL;
3531
3532 while ((item = PyIter_Next(it)) != NULL) {
3533 int contains = PySequence_Contains(self, item);
3534 Py_DECREF(item);
3535 if (contains == -1) {
3536 Py_DECREF(it);
3537 return NULL;
3538 }
3539
3540 if (contains) {
3541 Py_DECREF(it);
3542 Py_RETURN_FALSE;
3543 }
3544 }
3545 Py_DECREF(it);
3546 if (PyErr_Occurred())
3547 return NULL; /* PyIter_Next raised an exception. */
3548 Py_RETURN_TRUE;
3549}
3550
3551PyDoc_STRVAR(isdisjoint_doc,
3552"Return True if the view and the given iterable have a null intersection.");
3553
Guido van Rossumb90c8482007-02-10 01:11:45 +00003554static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003555 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3556 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003557 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003558};
3559
3560PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003561 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3562 "dict_keys", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06003563 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003564 0, /* tp_itemsize */
3565 /* methods */
3566 (destructor)dictview_dealloc, /* tp_dealloc */
3567 0, /* tp_print */
3568 0, /* tp_getattr */
3569 0, /* tp_setattr */
3570 0, /* tp_reserved */
3571 (reprfunc)dictview_repr, /* tp_repr */
3572 &dictviews_as_number, /* tp_as_number */
3573 &dictkeys_as_sequence, /* tp_as_sequence */
3574 0, /* tp_as_mapping */
3575 0, /* tp_hash */
3576 0, /* tp_call */
3577 0, /* tp_str */
3578 PyObject_GenericGetAttr, /* tp_getattro */
3579 0, /* tp_setattro */
3580 0, /* tp_as_buffer */
3581 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3582 0, /* tp_doc */
3583 (traverseproc)dictview_traverse, /* tp_traverse */
3584 0, /* tp_clear */
3585 dictview_richcompare, /* tp_richcompare */
3586 0, /* tp_weaklistoffset */
3587 (getiterfunc)dictkeys_iter, /* tp_iter */
3588 0, /* tp_iternext */
3589 dictkeys_methods, /* tp_methods */
3590 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003591};
3592
3593static PyObject *
3594dictkeys_new(PyObject *dict)
3595{
Eric Snow96c6af92015-05-29 22:21:39 -06003596 return _PyDictView_New(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003597}
3598
Guido van Rossum3ac67412007-02-10 18:55:06 +00003599/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003600
3601static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003602dictitems_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003603{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003604 if (dv->dv_dict == NULL) {
3605 Py_RETURN_NONE;
3606 }
3607 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003608}
3609
3610static int
Eric Snow96c6af92015-05-29 22:21:39 -06003611dictitems_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00003612{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003613 PyObject *key, *value, *found;
3614 if (dv->dv_dict == NULL)
3615 return 0;
3616 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
3617 return 0;
3618 key = PyTuple_GET_ITEM(obj, 0);
3619 value = PyTuple_GET_ITEM(obj, 1);
3620 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
3621 if (found == NULL) {
3622 if (PyErr_Occurred())
3623 return -1;
3624 return 0;
3625 }
3626 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003627}
3628
Guido van Rossum83825ac2007-02-10 04:54:19 +00003629static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003630 (lenfunc)dictview_len, /* sq_length */
3631 0, /* sq_concat */
3632 0, /* sq_repeat */
3633 0, /* sq_item */
3634 0, /* sq_slice */
3635 0, /* sq_ass_item */
3636 0, /* sq_ass_slice */
3637 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003638};
3639
Guido van Rossumb90c8482007-02-10 01:11:45 +00003640static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003641 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3642 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003643 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003644};
3645
3646PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003647 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3648 "dict_items", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06003649 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003650 0, /* tp_itemsize */
3651 /* methods */
3652 (destructor)dictview_dealloc, /* tp_dealloc */
3653 0, /* tp_print */
3654 0, /* tp_getattr */
3655 0, /* tp_setattr */
3656 0, /* tp_reserved */
3657 (reprfunc)dictview_repr, /* tp_repr */
3658 &dictviews_as_number, /* tp_as_number */
3659 &dictitems_as_sequence, /* tp_as_sequence */
3660 0, /* tp_as_mapping */
3661 0, /* tp_hash */
3662 0, /* tp_call */
3663 0, /* tp_str */
3664 PyObject_GenericGetAttr, /* tp_getattro */
3665 0, /* tp_setattro */
3666 0, /* tp_as_buffer */
3667 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3668 0, /* tp_doc */
3669 (traverseproc)dictview_traverse, /* tp_traverse */
3670 0, /* tp_clear */
3671 dictview_richcompare, /* tp_richcompare */
3672 0, /* tp_weaklistoffset */
3673 (getiterfunc)dictitems_iter, /* tp_iter */
3674 0, /* tp_iternext */
3675 dictitems_methods, /* tp_methods */
3676 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003677};
3678
3679static PyObject *
3680dictitems_new(PyObject *dict)
3681{
Eric Snow96c6af92015-05-29 22:21:39 -06003682 return _PyDictView_New(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003683}
3684
Guido van Rossum3ac67412007-02-10 18:55:06 +00003685/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003686
3687static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003688dictvalues_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003689{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003690 if (dv->dv_dict == NULL) {
3691 Py_RETURN_NONE;
3692 }
3693 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003694}
3695
Guido van Rossum83825ac2007-02-10 04:54:19 +00003696static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003697 (lenfunc)dictview_len, /* sq_length */
3698 0, /* sq_concat */
3699 0, /* sq_repeat */
3700 0, /* sq_item */
3701 0, /* sq_slice */
3702 0, /* sq_ass_item */
3703 0, /* sq_ass_slice */
3704 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003705};
3706
Guido van Rossumb90c8482007-02-10 01:11:45 +00003707static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003708 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003709};
3710
3711PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003712 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3713 "dict_values", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06003714 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003715 0, /* tp_itemsize */
3716 /* methods */
3717 (destructor)dictview_dealloc, /* tp_dealloc */
3718 0, /* tp_print */
3719 0, /* tp_getattr */
3720 0, /* tp_setattr */
3721 0, /* tp_reserved */
3722 (reprfunc)dictview_repr, /* tp_repr */
3723 0, /* tp_as_number */
3724 &dictvalues_as_sequence, /* tp_as_sequence */
3725 0, /* tp_as_mapping */
3726 0, /* tp_hash */
3727 0, /* tp_call */
3728 0, /* tp_str */
3729 PyObject_GenericGetAttr, /* tp_getattro */
3730 0, /* tp_setattro */
3731 0, /* tp_as_buffer */
3732 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3733 0, /* tp_doc */
3734 (traverseproc)dictview_traverse, /* tp_traverse */
3735 0, /* tp_clear */
3736 0, /* tp_richcompare */
3737 0, /* tp_weaklistoffset */
3738 (getiterfunc)dictvalues_iter, /* tp_iter */
3739 0, /* tp_iternext */
3740 dictvalues_methods, /* tp_methods */
3741 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003742};
3743
3744static PyObject *
3745dictvalues_new(PyObject *dict)
3746{
Eric Snow96c6af92015-05-29 22:21:39 -06003747 return _PyDictView_New(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003748}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003749
3750/* Returns NULL if cannot allocate a new PyDictKeysObject,
3751 but does not set an error */
3752PyDictKeysObject *
3753_PyDict_NewKeysForClass(void)
3754{
3755 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE_SPLIT);
3756 if (keys == NULL)
3757 PyErr_Clear();
3758 else
3759 keys->dk_lookup = lookdict_split;
3760 return keys;
3761}
3762
3763#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
3764
3765PyObject *
3766PyObject_GenericGetDict(PyObject *obj, void *context)
3767{
3768 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
3769 if (dictptr == NULL) {
3770 PyErr_SetString(PyExc_AttributeError,
3771 "This object has no __dict__");
3772 return NULL;
3773 }
3774 dict = *dictptr;
3775 if (dict == NULL) {
3776 PyTypeObject *tp = Py_TYPE(obj);
3777 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
3778 DK_INCREF(CACHED_KEYS(tp));
3779 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
3780 }
3781 else {
3782 *dictptr = dict = PyDict_New();
3783 }
3784 }
3785 Py_XINCREF(dict);
3786 return dict;
3787}
3788
3789int
3790_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
3791 PyObject *key, PyObject *value)
3792{
3793 PyObject *dict;
3794 int res;
3795 PyDictKeysObject *cached;
3796
3797 assert(dictptr != NULL);
3798 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
3799 assert(dictptr != NULL);
3800 dict = *dictptr;
3801 if (dict == NULL) {
3802 DK_INCREF(cached);
3803 dict = new_dict_with_shared_keys(cached);
3804 if (dict == NULL)
3805 return -1;
3806 *dictptr = dict;
3807 }
3808 if (value == NULL) {
3809 res = PyDict_DelItem(dict, key);
3810 if (cached != ((PyDictObject *)dict)->ma_keys) {
3811 CACHED_KEYS(tp) = NULL;
3812 DK_DECREF(cached);
3813 }
3814 } else {
3815 res = PyDict_SetItem(dict, key, value);
3816 if (cached != ((PyDictObject *)dict)->ma_keys) {
3817 /* Either update tp->ht_cached_keys or delete it */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04003818 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003819 CACHED_KEYS(tp) = make_keys_shared(dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003820 } else {
3821 CACHED_KEYS(tp) = NULL;
3822 }
3823 DK_DECREF(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04003824 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
3825 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003826 }
3827 }
3828 } else {
3829 dict = *dictptr;
3830 if (dict == NULL) {
3831 dict = PyDict_New();
3832 if (dict == NULL)
3833 return -1;
3834 *dictptr = dict;
3835 }
3836 if (value == NULL) {
3837 res = PyDict_DelItem(dict, key);
3838 } else {
3839 res = PyDict_SetItem(dict, key, value);
3840 }
3841 }
3842 return res;
3843}
3844
3845void
3846_PyDictKeys_DecRef(PyDictKeysObject *keys)
3847{
3848 DK_DECREF(keys);
3849}
3850
3851
3852/* ARGSUSED */
3853static PyObject *
3854dummy_repr(PyObject *op)
3855{
3856 return PyUnicode_FromString("<dummy key>");
3857}
3858
3859/* ARGUSED */
3860static void
3861dummy_dealloc(PyObject* ignore)
3862{
3863 /* This should never get called, but we also don't want to SEGV if
3864 * we accidentally decref dummy-key out of existence.
3865 */
3866 Py_FatalError("deallocating <dummy key>");
3867}
3868
3869static PyTypeObject PyDictDummy_Type = {
3870 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3871 "<dummy key> type",
3872 0,
3873 0,
3874 dummy_dealloc, /*tp_dealloc*/ /*never called*/
3875 0, /*tp_print*/
3876 0, /*tp_getattr*/
3877 0, /*tp_setattr*/
3878 0, /*tp_reserved*/
3879 dummy_repr, /*tp_repr*/
3880 0, /*tp_as_number*/
3881 0, /*tp_as_sequence*/
3882 0, /*tp_as_mapping*/
3883 0, /*tp_hash */
3884 0, /*tp_call */
3885 0, /*tp_str */
3886 0, /*tp_getattro */
3887 0, /*tp_setattro */
3888 0, /*tp_as_buffer */
3889 Py_TPFLAGS_DEFAULT, /*tp_flags */
3890};
3891
3892static PyObject _dummy_struct = {
3893 _PyObject_EXTRA_INIT
3894 2, &PyDictDummy_Type
3895};
3896