blob: 3e6e112ba0431529452a694111ed9327a756bc9a [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);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001245 assert(hash != -1);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001246 mp = (PyDictObject *)op;
1247
1248 /* insertdict() handles any resizing that might be necessary */
1249 return insertdict(mp, key, hash, value);
1250}
1251
1252int
Tim Peters1f5871e2000-07-04 17:44:48 +00001253PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001254{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001255 PyDictObject *mp;
1256 Py_hash_t hash;
1257 PyDictKeyEntry *ep;
1258 PyObject *old_key, *old_value;
1259 PyObject **value_addr;
Guido van Rossumd7047b31995-01-02 19:07:15 +00001260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 if (!PyDict_Check(op)) {
1262 PyErr_BadInternalCall();
1263 return -1;
1264 }
1265 assert(key);
1266 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001267 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 hash = PyObject_Hash(key);
1269 if (hash == -1)
1270 return -1;
1271 }
1272 mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001273 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 if (ep == NULL)
1275 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001276 if (*value_addr == NULL) {
Raymond Hettinger69492da2013-09-02 15:59:26 -07001277 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 return -1;
1279 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001280 old_value = *value_addr;
1281 *value_addr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001283 if (!_PyDict_HasSplitTable(mp)) {
1284 ENSURE_ALLOWS_DELETIONS(mp);
1285 old_key = ep->me_key;
1286 Py_INCREF(dummy);
1287 ep->me_key = dummy;
1288 Py_DECREF(old_key);
1289 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 Py_DECREF(old_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001292}
1293
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001294int
1295_PyDict_DelItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1296{
1297 PyDictObject *mp;
1298 PyDictKeyEntry *ep;
1299 PyObject *old_key, *old_value;
1300 PyObject **value_addr;
1301
1302 if (!PyDict_Check(op)) {
1303 PyErr_BadInternalCall();
1304 return -1;
1305 }
1306 assert(key);
1307 assert(hash != -1);
1308 mp = (PyDictObject *)op;
1309 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
1310 if (ep == NULL)
1311 return -1;
1312 if (*value_addr == NULL) {
1313 _PyErr_SetKeyError(key);
1314 return -1;
1315 }
1316 old_value = *value_addr;
1317 *value_addr = NULL;
1318 mp->ma_used--;
1319 if (!_PyDict_HasSplitTable(mp)) {
1320 ENSURE_ALLOWS_DELETIONS(mp);
1321 old_key = ep->me_key;
1322 Py_INCREF(dummy);
1323 ep->me_key = dummy;
1324 Py_DECREF(old_key);
1325 }
1326 Py_DECREF(old_value);
1327 return 0;
1328}
1329
Guido van Rossum25831651993-05-19 14:50:45 +00001330void
Tim Peters1f5871e2000-07-04 17:44:48 +00001331PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001332{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001334 PyDictKeysObject *oldkeys;
1335 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 if (!PyDict_Check(op))
1339 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001340 mp = ((PyDictObject *)op);
1341 oldkeys = mp->ma_keys;
1342 oldvalues = mp->ma_values;
1343 if (oldvalues == empty_values)
1344 return;
1345 /* Empty the dict... */
1346 DK_INCREF(Py_EMPTY_KEYS);
1347 mp->ma_keys = Py_EMPTY_KEYS;
1348 mp->ma_values = empty_values;
1349 mp->ma_used = 0;
1350 /* ...then clear the keys and values */
1351 if (oldvalues != NULL) {
1352 n = DK_SIZE(oldkeys);
1353 for (i = 0; i < n; i++)
1354 Py_CLEAR(oldvalues[i]);
1355 free_values(oldvalues);
1356 DK_DECREF(oldkeys);
1357 }
1358 else {
1359 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001360 DK_DECREF(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001361 }
1362}
1363
1364/* Returns -1 if no more items (or op is not a dict),
1365 * index of item otherwise. Stores value in pvalue
1366 */
1367Py_LOCAL_INLINE(Py_ssize_t)
1368dict_next(PyObject *op, Py_ssize_t i, PyObject **pvalue)
1369{
1370 Py_ssize_t mask, offset;
1371 PyDictObject *mp;
1372 PyObject **value_ptr;
1373
1374
1375 if (!PyDict_Check(op))
1376 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001378 if (i < 0)
1379 return -1;
1380 if (mp->ma_values) {
1381 value_ptr = &mp->ma_values[i];
1382 offset = sizeof(PyObject *);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001384 else {
1385 value_ptr = &mp->ma_keys->dk_entries[i].me_value;
1386 offset = sizeof(PyDictKeyEntry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001388 mask = DK_MASK(mp->ma_keys);
1389 while (i <= mask && *value_ptr == NULL) {
1390 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1391 i++;
1392 }
1393 if (i > mask)
1394 return -1;
1395 if (pvalue)
1396 *pvalue = *value_ptr;
1397 return i;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001398}
1399
Tim Peters080c88b2003-02-15 03:01:11 +00001400/*
1401 * Iterate over a dict. Use like so:
1402 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001403 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001404 * PyObject *key, *value;
1405 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001406 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +00001407 * Refer to borrowed references in key and value.
1408 * }
1409 *
1410 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001411 * mutates the dict. One exception: it is safe if the loop merely changes
1412 * the values associated with the keys (but doesn't insert new keys or
1413 * delete keys), via PyDict_SetItem().
1414 */
Guido van Rossum25831651993-05-19 14:50:45 +00001415int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001416PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001417{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001418 PyDictObject *mp;
1419 Py_ssize_t i = dict_next(op, *ppos, pvalue);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 if (i < 0)
1421 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001422 mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 *ppos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 if (pkey)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001425 *pkey = mp->ma_keys->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001427}
1428
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001429/* Internal version of PyDict_Next that returns a hash value in addition
1430 * to the key and value.
1431 */
Thomas Wouterscf297e42007-02-23 15:07:44 +00001432int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001433_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1434 PyObject **pvalue, Py_hash_t *phash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00001435{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001436 PyDictObject *mp;
1437 Py_ssize_t i = dict_next(op, *ppos, pvalue);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 if (i < 0)
1439 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001440 mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 *ppos = i+1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001442 *phash = mp->ma_keys->dk_entries[i].me_hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 if (pkey)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001444 *pkey = mp->ma_keys->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001445 return 1;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001446}
1447
Eric Snow96c6af92015-05-29 22:21:39 -06001448/* Internal version of dict.pop(). */
1449PyObject *
1450_PyDict_Pop(PyDictObject *mp, PyObject *key, PyObject *deflt)
1451{
1452 Py_hash_t hash;
1453 PyObject *old_value, *old_key;
1454 PyDictKeyEntry *ep;
1455 PyObject **value_addr;
1456
1457 if (mp->ma_used == 0) {
1458 if (deflt) {
1459 Py_INCREF(deflt);
1460 return deflt;
1461 }
1462 _PyErr_SetKeyError(key);
1463 return NULL;
1464 }
1465 if (!PyUnicode_CheckExact(key) ||
1466 (hash = ((PyASCIIObject *) key)->hash) == -1) {
1467 hash = PyObject_Hash(key);
1468 if (hash == -1)
1469 return NULL;
1470 }
1471 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
1472 if (ep == NULL)
1473 return NULL;
1474 old_value = *value_addr;
1475 if (old_value == NULL) {
1476 if (deflt) {
1477 Py_INCREF(deflt);
1478 return deflt;
1479 }
1480 _PyErr_SetKeyError(key);
1481 return NULL;
1482 }
1483 *value_addr = NULL;
1484 mp->ma_used--;
1485 if (!_PyDict_HasSplitTable(mp)) {
1486 ENSURE_ALLOWS_DELETIONS(mp);
1487 old_key = ep->me_key;
1488 Py_INCREF(dummy);
1489 ep->me_key = dummy;
1490 Py_DECREF(old_key);
1491 }
1492 return old_value;
1493}
1494
1495/* Internal version of dict.from_keys(). It is subclass-friendly. */
1496PyObject *
1497_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
1498{
1499 PyObject *it; /* iter(iterable) */
1500 PyObject *key;
1501 PyObject *d;
1502 int status;
1503
1504 d = PyObject_CallObject(cls, NULL);
1505 if (d == NULL)
1506 return NULL;
1507
1508 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
1509 if (PyDict_CheckExact(iterable)) {
1510 PyDictObject *mp = (PyDictObject *)d;
1511 PyObject *oldvalue;
1512 Py_ssize_t pos = 0;
1513 PyObject *key;
1514 Py_hash_t hash;
1515
1516 if (dictresize(mp, Py_SIZE(iterable))) {
1517 Py_DECREF(d);
1518 return NULL;
1519 }
1520
1521 while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
1522 if (insertdict(mp, key, hash, value)) {
1523 Py_DECREF(d);
1524 return NULL;
1525 }
1526 }
1527 return d;
1528 }
1529 if (PyAnySet_CheckExact(iterable)) {
1530 PyDictObject *mp = (PyDictObject *)d;
1531 Py_ssize_t pos = 0;
1532 PyObject *key;
1533 Py_hash_t hash;
1534
1535 if (dictresize(mp, PySet_GET_SIZE(iterable))) {
1536 Py_DECREF(d);
1537 return NULL;
1538 }
1539
1540 while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
1541 if (insertdict(mp, key, hash, value)) {
1542 Py_DECREF(d);
1543 return NULL;
1544 }
1545 }
1546 return d;
1547 }
1548 }
1549
1550 it = PyObject_GetIter(iterable);
1551 if (it == NULL){
1552 Py_DECREF(d);
1553 return NULL;
1554 }
1555
1556 if (PyDict_CheckExact(d)) {
1557 while ((key = PyIter_Next(it)) != NULL) {
1558 status = PyDict_SetItem(d, key, value);
1559 Py_DECREF(key);
1560 if (status < 0)
1561 goto Fail;
1562 }
1563 } else {
1564 while ((key = PyIter_Next(it)) != NULL) {
1565 status = PyObject_SetItem(d, key, value);
1566 Py_DECREF(key);
1567 if (status < 0)
1568 goto Fail;
1569 }
1570 }
1571
1572 if (PyErr_Occurred())
1573 goto Fail;
1574 Py_DECREF(it);
1575 return d;
1576
1577Fail:
1578 Py_DECREF(it);
1579 Py_DECREF(d);
1580 return NULL;
1581}
1582
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001583/* Methods */
1584
1585static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001586dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001587{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001588 PyObject **values = mp->ma_values;
1589 PyDictKeysObject *keys = mp->ma_keys;
1590 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 PyObject_GC_UnTrack(mp);
1592 Py_TRASHCAN_SAFE_BEGIN(mp)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001593 if (values != NULL) {
1594 if (values != empty_values) {
1595 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
1596 Py_XDECREF(values[i]);
1597 }
1598 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001600 DK_DECREF(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02001602 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001603 assert(keys->dk_refcnt == 1);
1604 DK_DECREF(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001605 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1607 free_list[numfree++] = mp;
1608 else
1609 Py_TYPE(mp)->tp_free((PyObject *)mp);
1610 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001611}
1612
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001613
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001614static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001615dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001616{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001618 PyObject *key = NULL, *value = NULL;
1619 _PyUnicodeWriter writer;
1620 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00001621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 i = Py_ReprEnter((PyObject *)mp);
1623 if (i != 0) {
1624 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
1625 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001628 Py_ReprLeave((PyObject *)mp);
1629 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001630 }
Tim Petersa7259592001-06-16 05:11:17 +00001631
Victor Stinnerf91929b2013-11-19 13:07:38 +01001632 _PyUnicodeWriter_Init(&writer);
1633 writer.overallocate = 1;
1634 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
1635 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00001636
Victor Stinnerf91929b2013-11-19 13:07:38 +01001637 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
1638 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 /* Do repr() on each key+value pair, and insert ": " between them.
1641 Note that repr may mutate the dict. */
1642 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001643 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001645 PyObject *s;
1646 int res;
1647
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001648 /* Prevent repr from deleting key or value during key format. */
1649 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001650 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02001651
Victor Stinnerf91929b2013-11-19 13:07:38 +01001652 if (!first) {
1653 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
1654 goto error;
1655 }
1656 first = 0;
1657
1658 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01001660 goto error;
1661 res = _PyUnicodeWriter_WriteStr(&writer, s);
1662 Py_DECREF(s);
1663 if (res < 0)
1664 goto error;
1665
1666 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
1667 goto error;
1668
1669 s = PyObject_Repr(value);
1670 if (s == NULL)
1671 goto error;
1672 res = _PyUnicodeWriter_WriteStr(&writer, s);
1673 Py_DECREF(s);
1674 if (res < 0)
1675 goto error;
1676
1677 Py_CLEAR(key);
1678 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 }
Tim Petersa7259592001-06-16 05:11:17 +00001680
Victor Stinnerf91929b2013-11-19 13:07:38 +01001681 writer.overallocate = 0;
1682 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
1683 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01001686
1687 return _PyUnicodeWriter_Finish(&writer);
1688
1689error:
1690 Py_ReprLeave((PyObject *)mp);
1691 _PyUnicodeWriter_Dealloc(&writer);
1692 Py_XDECREF(key);
1693 Py_XDECREF(value);
1694 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001695}
1696
Martin v. Löwis18e16552006-02-15 17:27:45 +00001697static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001698dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001699{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001701}
1702
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001703static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001704dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001705{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001706 PyObject *v;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001707 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001708 PyDictKeyEntry *ep;
1709 PyObject **value_addr;
1710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001712 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001713 hash = PyObject_Hash(key);
1714 if (hash == -1)
1715 return NULL;
1716 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001717 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 if (ep == NULL)
1719 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001720 v = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001721 if (v == NULL) {
1722 if (!PyDict_CheckExact(mp)) {
1723 /* Look up __missing__ method if we're a subclass. */
1724 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05001725 _Py_IDENTIFIER(__missing__);
1726 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 if (missing != NULL) {
1728 res = PyObject_CallFunctionObjArgs(missing,
1729 key, NULL);
1730 Py_DECREF(missing);
1731 return res;
1732 }
1733 else if (PyErr_Occurred())
1734 return NULL;
1735 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07001736 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 return NULL;
1738 }
1739 else
1740 Py_INCREF(v);
1741 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001742}
1743
1744static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001745dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001746{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001747 if (w == NULL)
1748 return PyDict_DelItem((PyObject *)mp, v);
1749 else
1750 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001751}
1752
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001753static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001754 (lenfunc)dict_length, /*mp_length*/
1755 (binaryfunc)dict_subscript, /*mp_subscript*/
1756 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001757};
1758
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001759static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001760dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001761{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001762 PyObject *v;
1763 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001764 PyDictKeyEntry *ep;
1765 Py_ssize_t size, n, offset;
1766 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001767
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001768 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 n = mp->ma_used;
1770 v = PyList_New(n);
1771 if (v == NULL)
1772 return NULL;
1773 if (n != mp->ma_used) {
1774 /* Durnit. The allocations caused the dict to resize.
1775 * Just start over, this shouldn't normally happen.
1776 */
1777 Py_DECREF(v);
1778 goto again;
1779 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001780 ep = &mp->ma_keys->dk_entries[0];
1781 size = DK_SIZE(mp->ma_keys);
1782 if (mp->ma_values) {
1783 value_ptr = mp->ma_values;
1784 offset = sizeof(PyObject *);
1785 }
1786 else {
1787 value_ptr = &ep[0].me_value;
1788 offset = sizeof(PyDictKeyEntry);
1789 }
1790 for (i = 0, j = 0; i < size; i++) {
1791 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001792 PyObject *key = ep[i].me_key;
1793 Py_INCREF(key);
1794 PyList_SET_ITEM(v, j, key);
1795 j++;
1796 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001797 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001798 }
1799 assert(j == n);
1800 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001801}
1802
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001803static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001804dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001805{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001806 PyObject *v;
1807 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001808 Py_ssize_t size, n, offset;
1809 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001810
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001811 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001812 n = mp->ma_used;
1813 v = PyList_New(n);
1814 if (v == NULL)
1815 return NULL;
1816 if (n != mp->ma_used) {
1817 /* Durnit. The allocations caused the dict to resize.
1818 * Just start over, this shouldn't normally happen.
1819 */
1820 Py_DECREF(v);
1821 goto again;
1822 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001823 size = DK_SIZE(mp->ma_keys);
1824 if (mp->ma_values) {
1825 value_ptr = mp->ma_values;
1826 offset = sizeof(PyObject *);
1827 }
1828 else {
1829 value_ptr = &mp->ma_keys->dk_entries[0].me_value;
1830 offset = sizeof(PyDictKeyEntry);
1831 }
1832 for (i = 0, j = 0; i < size; i++) {
1833 PyObject *value = *value_ptr;
1834 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1835 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 Py_INCREF(value);
1837 PyList_SET_ITEM(v, j, value);
1838 j++;
1839 }
1840 }
1841 assert(j == n);
1842 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001843}
1844
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001845static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001846dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001847{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001848 PyObject *v;
1849 Py_ssize_t i, j, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001850 Py_ssize_t size, offset;
1851 PyObject *item, *key;
1852 PyDictKeyEntry *ep;
1853 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001854
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855 /* Preallocate the list of tuples, to avoid allocations during
1856 * the loop over the items, which could trigger GC, which
1857 * could resize the dict. :-(
1858 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001859 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001860 n = mp->ma_used;
1861 v = PyList_New(n);
1862 if (v == NULL)
1863 return NULL;
1864 for (i = 0; i < n; i++) {
1865 item = PyTuple_New(2);
1866 if (item == NULL) {
1867 Py_DECREF(v);
1868 return NULL;
1869 }
1870 PyList_SET_ITEM(v, i, item);
1871 }
1872 if (n != mp->ma_used) {
1873 /* Durnit. The allocations caused the dict to resize.
1874 * Just start over, this shouldn't normally happen.
1875 */
1876 Py_DECREF(v);
1877 goto again;
1878 }
1879 /* Nothing we do below makes any function calls. */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001880 ep = mp->ma_keys->dk_entries;
1881 size = DK_SIZE(mp->ma_keys);
1882 if (mp->ma_values) {
1883 value_ptr = mp->ma_values;
1884 offset = sizeof(PyObject *);
1885 }
1886 else {
1887 value_ptr = &ep[0].me_value;
1888 offset = sizeof(PyDictKeyEntry);
1889 }
1890 for (i = 0, j = 0; i < size; i++) {
1891 PyObject *value = *value_ptr;
1892 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1893 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 key = ep[i].me_key;
1895 item = PyList_GET_ITEM(v, j);
1896 Py_INCREF(key);
1897 PyTuple_SET_ITEM(item, 0, key);
1898 Py_INCREF(value);
1899 PyTuple_SET_ITEM(item, 1, value);
1900 j++;
1901 }
1902 }
1903 assert(j == n);
1904 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001905}
1906
Larry Hastings5c661892014-01-24 06:17:25 -08001907/*[clinic input]
1908@classmethod
1909dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08001910 iterable: object
1911 value: object=None
1912 /
1913
1914Returns a new dict with keys from iterable and values equal to value.
1915[clinic start generated code]*/
1916
Larry Hastings5c661892014-01-24 06:17:25 -08001917static PyObject *
1918dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001919/*[clinic end generated code: output=8fb98e4b10384999 input=b85a667f9bf4669d]*/
Larry Hastings5c661892014-01-24 06:17:25 -08001920{
Eric Snow96c6af92015-05-29 22:21:39 -06001921 return _PyDict_FromKeys((PyObject *)type, iterable, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001922}
1923
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001924static int
1925dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001926{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001927 PyObject *arg = NULL;
1928 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001929
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1931 result = -1;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001933 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001934 _Py_IDENTIFIER(keys);
1935 if (_PyObject_HasAttrId(arg, &PyId_keys))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001936 result = PyDict_Merge(self, arg, 1);
1937 else
1938 result = PyDict_MergeFromSeq2(self, arg, 1);
1939 }
1940 if (result == 0 && kwds != NULL) {
1941 if (PyArg_ValidateKeywordArguments(kwds))
1942 result = PyDict_Merge(self, kwds, 1);
1943 else
1944 result = -1;
1945 }
1946 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001947}
1948
1949static PyObject *
1950dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1951{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 if (dict_update_common(self, args, kwds, "update") != -1)
1953 Py_RETURN_NONE;
1954 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001955}
1956
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001957/* Update unconditionally replaces existing items.
1958 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001959 otherwise it leaves existing items unchanged.
1960
1961 PyDict_{Update,Merge} update/merge from a mapping object.
1962
Tim Petersf582b822001-12-11 18:51:08 +00001963 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001964 producing iterable objects of length 2.
1965*/
1966
Tim Petersf582b822001-12-11 18:51:08 +00001967int
Tim Peters1fc240e2001-10-26 05:06:50 +00001968PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1969{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001970 PyObject *it; /* iter(seq2) */
1971 Py_ssize_t i; /* index into seq2 of current element */
1972 PyObject *item; /* seq2[i] */
1973 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00001974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 assert(d != NULL);
1976 assert(PyDict_Check(d));
1977 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00001978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001979 it = PyObject_GetIter(seq2);
1980 if (it == NULL)
1981 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001982
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001983 for (i = 0; ; ++i) {
1984 PyObject *key, *value;
1985 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001987 fast = NULL;
1988 item = PyIter_Next(it);
1989 if (item == NULL) {
1990 if (PyErr_Occurred())
1991 goto Fail;
1992 break;
1993 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001995 /* Convert item to sequence, and verify length 2. */
1996 fast = PySequence_Fast(item, "");
1997 if (fast == NULL) {
1998 if (PyErr_ExceptionMatches(PyExc_TypeError))
1999 PyErr_Format(PyExc_TypeError,
2000 "cannot convert dictionary update "
2001 "sequence element #%zd to a sequence",
2002 i);
2003 goto Fail;
2004 }
2005 n = PySequence_Fast_GET_SIZE(fast);
2006 if (n != 2) {
2007 PyErr_Format(PyExc_ValueError,
2008 "dictionary update sequence element #%zd "
2009 "has length %zd; 2 is required",
2010 i, n);
2011 goto Fail;
2012 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002014 /* Update/merge with this (key, value) pair. */
2015 key = PySequence_Fast_GET_ITEM(fast, 0);
2016 value = PySequence_Fast_GET_ITEM(fast, 1);
2017 if (override || PyDict_GetItem(d, key) == NULL) {
2018 int status = PyDict_SetItem(d, key, value);
2019 if (status < 0)
2020 goto Fail;
2021 }
2022 Py_DECREF(fast);
2023 Py_DECREF(item);
2024 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002026 i = 0;
2027 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00002028Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002029 Py_XDECREF(item);
2030 Py_XDECREF(fast);
2031 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002032Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002033 Py_DECREF(it);
2034 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00002035}
2036
Tim Peters6d6c1a32001-08-02 04:15:00 +00002037int
2038PyDict_Update(PyObject *a, PyObject *b)
2039{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002040 return PyDict_Merge(a, b, 1);
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002041}
2042
2043int
2044PyDict_Merge(PyObject *a, PyObject *b, int override)
2045{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002046 PyDictObject *mp, *other;
2047 Py_ssize_t i, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002048 PyDictKeyEntry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002050 /* We accept for the argument either a concrete dictionary object,
2051 * or an abstract "mapping" object. For the former, we can do
2052 * things quite efficiently. For the latter, we only require that
2053 * PyMapping_Keys() and PyObject_GetItem() be supported.
2054 */
2055 if (a == NULL || !PyDict_Check(a) || b == NULL) {
2056 PyErr_BadInternalCall();
2057 return -1;
2058 }
2059 mp = (PyDictObject*)a;
2060 if (PyDict_Check(b)) {
2061 other = (PyDictObject*)b;
2062 if (other == mp || other->ma_used == 0)
2063 /* a.update(a) or a.update({}); nothing to do */
2064 return 0;
2065 if (mp->ma_used == 0)
2066 /* Since the target dict is empty, PyDict_GetItem()
2067 * always returns NULL. Setting override to 1
2068 * skips the unnecessary test.
2069 */
2070 override = 1;
2071 /* Do one big resize at the start, rather than
2072 * incrementally resizing as we insert new items. Expect
2073 * that there will be no (or few) overlapping keys.
2074 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002075 if (mp->ma_keys->dk_usable * 3 < other->ma_used * 2)
2076 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002077 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002078 for (i = 0, n = DK_SIZE(other->ma_keys); i < n; i++) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002079 PyObject *key, *value;
2080 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002081 entry = &other->ma_keys->dk_entries[i];
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002082 key = entry->me_key;
2083 hash = entry->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002084 if (other->ma_values)
2085 value = other->ma_values[i];
2086 else
2087 value = entry->me_value;
2088
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002089 if (value != NULL) {
2090 int err = 0;
2091 Py_INCREF(key);
2092 Py_INCREF(value);
2093 if (override || PyDict_GetItem(a, key) == NULL)
2094 err = insertdict(mp, key, hash, value);
2095 Py_DECREF(value);
2096 Py_DECREF(key);
2097 if (err != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002098 return -1;
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002099
2100 if (n != DK_SIZE(other->ma_keys)) {
2101 PyErr_SetString(PyExc_RuntimeError,
2102 "dict mutated during update");
2103 return -1;
2104 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002105 }
2106 }
2107 }
2108 else {
2109 /* Do it the generic, slower way */
2110 PyObject *keys = PyMapping_Keys(b);
2111 PyObject *iter;
2112 PyObject *key, *value;
2113 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002115 if (keys == NULL)
2116 /* Docstring says this is equivalent to E.keys() so
2117 * if E doesn't have a .keys() method we want
2118 * AttributeError to percolate up. Might as well
2119 * do the same for any other error.
2120 */
2121 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002123 iter = PyObject_GetIter(keys);
2124 Py_DECREF(keys);
2125 if (iter == NULL)
2126 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002128 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
2129 if (!override && PyDict_GetItem(a, key) != NULL) {
2130 Py_DECREF(key);
2131 continue;
2132 }
2133 value = PyObject_GetItem(b, key);
2134 if (value == NULL) {
2135 Py_DECREF(iter);
2136 Py_DECREF(key);
2137 return -1;
2138 }
2139 status = PyDict_SetItem(a, key, value);
2140 Py_DECREF(key);
2141 Py_DECREF(value);
2142 if (status < 0) {
2143 Py_DECREF(iter);
2144 return -1;
2145 }
2146 }
2147 Py_DECREF(iter);
2148 if (PyErr_Occurred())
2149 /* Iterator completed, via error */
2150 return -1;
2151 }
2152 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002153}
2154
2155static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002156dict_copy(PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002157{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002158 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002159}
2160
2161PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002162PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002163{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002164 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002165 PyDictObject *mp;
2166 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002168 if (o == NULL || !PyDict_Check(o)) {
2169 PyErr_BadInternalCall();
2170 return NULL;
2171 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002172 mp = (PyDictObject *)o;
2173 if (_PyDict_HasSplitTable(mp)) {
2174 PyDictObject *split_copy;
2175 PyObject **newvalues = new_values(DK_SIZE(mp->ma_keys));
2176 if (newvalues == NULL)
2177 return PyErr_NoMemory();
2178 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2179 if (split_copy == NULL) {
2180 free_values(newvalues);
2181 return NULL;
2182 }
2183 split_copy->ma_values = newvalues;
2184 split_copy->ma_keys = mp->ma_keys;
2185 split_copy->ma_used = mp->ma_used;
2186 DK_INCREF(mp->ma_keys);
2187 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2188 PyObject *value = mp->ma_values[i];
2189 Py_XINCREF(value);
2190 split_copy->ma_values[i] = value;
2191 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002192 if (_PyObject_GC_IS_TRACKED(mp))
2193 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002194 return (PyObject *)split_copy;
2195 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002196 copy = PyDict_New();
2197 if (copy == NULL)
2198 return NULL;
2199 if (PyDict_Merge(copy, o, 1) == 0)
2200 return copy;
2201 Py_DECREF(copy);
2202 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002203}
2204
Martin v. Löwis18e16552006-02-15 17:27:45 +00002205Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002206PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002207{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002208 if (mp == NULL || !PyDict_Check(mp)) {
2209 PyErr_BadInternalCall();
2210 return -1;
2211 }
2212 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002213}
2214
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002215PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002216PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002217{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002218 if (mp == NULL || !PyDict_Check(mp)) {
2219 PyErr_BadInternalCall();
2220 return NULL;
2221 }
2222 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002223}
2224
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002225PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002226PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002227{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228 if (mp == NULL || !PyDict_Check(mp)) {
2229 PyErr_BadInternalCall();
2230 return NULL;
2231 }
2232 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002233}
2234
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002235PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002236PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002237{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002238 if (mp == NULL || !PyDict_Check(mp)) {
2239 PyErr_BadInternalCall();
2240 return NULL;
2241 }
2242 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002243}
2244
Tim Peterse63415e2001-05-08 04:38:29 +00002245/* Return 1 if dicts equal, 0 if not, -1 if error.
2246 * Gets out as soon as any difference is detected.
2247 * Uses only Py_EQ comparison.
2248 */
2249static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002250dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002251{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002252 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002254 if (a->ma_used != b->ma_used)
2255 /* can't be equal if # of entries differ */
2256 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002257 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002258 for (i = 0; i < DK_SIZE(a->ma_keys); i++) {
2259 PyDictKeyEntry *ep = &a->ma_keys->dk_entries[i];
2260 PyObject *aval;
2261 if (a->ma_values)
2262 aval = a->ma_values[i];
2263 else
2264 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002265 if (aval != NULL) {
2266 int cmp;
2267 PyObject *bval;
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002268 PyObject **vaddr;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002269 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002270 /* temporarily bump aval's refcount to ensure it stays
2271 alive until we're done with it */
2272 Py_INCREF(aval);
2273 /* ditto for key */
2274 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002275 /* reuse the known hash value */
2276 if ((b->ma_keys->dk_lookup)(b, key, ep->me_hash, &vaddr) == NULL)
2277 bval = NULL;
2278 else
2279 bval = *vaddr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002280 Py_DECREF(key);
2281 if (bval == NULL) {
2282 Py_DECREF(aval);
2283 if (PyErr_Occurred())
2284 return -1;
2285 return 0;
2286 }
2287 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
2288 Py_DECREF(aval);
2289 if (cmp <= 0) /* error or not equal */
2290 return cmp;
2291 }
2292 }
2293 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002294}
Tim Peterse63415e2001-05-08 04:38:29 +00002295
2296static PyObject *
2297dict_richcompare(PyObject *v, PyObject *w, int op)
2298{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002299 int cmp;
2300 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002302 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2303 res = Py_NotImplemented;
2304 }
2305 else if (op == Py_EQ || op == Py_NE) {
2306 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2307 if (cmp < 0)
2308 return NULL;
2309 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2310 }
2311 else
2312 res = Py_NotImplemented;
2313 Py_INCREF(res);
2314 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002315}
Tim Peterse63415e2001-05-08 04:38:29 +00002316
Larry Hastings61272b72014-01-07 12:41:53 -08002317/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002318
2319@coexist
2320dict.__contains__
2321
2322 key: object
2323 /
2324
Meador Ingee02de8c2014-01-14 16:48:31 -06002325True if D has a key k, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002326[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002327
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002328static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002329dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002330/*[clinic end generated code: output=a3d03db709ed6e6b input=b852b2a19b51ab24]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002331{
Larry Hastingsc2047262014-01-25 20:43:29 -08002332 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002333 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002334 PyDictKeyEntry *ep;
2335 PyObject **value_addr;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002337 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002338 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002339 hash = PyObject_Hash(key);
2340 if (hash == -1)
2341 return NULL;
2342 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002343 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002344 if (ep == NULL)
2345 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002346 return PyBool_FromLong(*value_addr != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002347}
2348
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002349static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002350dict_get(PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00002351{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002352 PyObject *key;
2353 PyObject *failobj = Py_None;
2354 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002355 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002356 PyDictKeyEntry *ep;
2357 PyObject **value_addr;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002359 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
2360 return NULL;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002362 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002363 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 hash = PyObject_Hash(key);
2365 if (hash == -1)
2366 return NULL;
2367 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002368 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 if (ep == NULL)
2370 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002371 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002372 if (val == NULL)
2373 val = failobj;
2374 Py_INCREF(val);
2375 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002376}
2377
Benjamin Peterson00e98862013-03-07 22:16:29 -05002378PyObject *
2379PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002380{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002381 PyDictObject *mp = (PyDictObject *)d;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002382 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002383 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002384 PyDictKeyEntry *ep;
2385 PyObject **value_addr;
Guido van Rossum164452c2000-08-08 16:12:54 +00002386
Benjamin Peterson00e98862013-03-07 22:16:29 -05002387 if (!PyDict_Check(d)) {
2388 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002390 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002392 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393 hash = PyObject_Hash(key);
2394 if (hash == -1)
2395 return NULL;
2396 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002397 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398 if (ep == NULL)
2399 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002400 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 if (val == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002402 if (mp->ma_keys->dk_usable <= 0) {
2403 /* Need to resize. */
2404 if (insertion_resize(mp) < 0)
2405 return NULL;
2406 ep = find_empty_slot(mp, key, hash, &value_addr);
2407 }
Benjamin Peterson00e98862013-03-07 22:16:29 -05002408 Py_INCREF(defaultobj);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002409 Py_INCREF(key);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002410 MAINTAIN_TRACKING(mp, key, defaultobj);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002411 ep->me_key = key;
2412 ep->me_hash = hash;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002413 *value_addr = defaultobj;
2414 val = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002415 mp->ma_keys->dk_usable--;
2416 mp->ma_used++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002417 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002418 return val;
Guido van Rossum164452c2000-08-08 16:12:54 +00002419}
2420
Benjamin Peterson00e98862013-03-07 22:16:29 -05002421static PyObject *
2422dict_setdefault(PyDictObject *mp, PyObject *args)
2423{
2424 PyObject *key, *val;
2425 PyObject *defaultobj = Py_None;
2426
2427 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &defaultobj))
2428 return NULL;
2429
Benjamin Peterson55898502013-03-08 08:36:49 -05002430 val = PyDict_SetDefault((PyObject *)mp, key, defaultobj);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002431 Py_XINCREF(val);
2432 return val;
2433}
Guido van Rossum164452c2000-08-08 16:12:54 +00002434
2435static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002436dict_clear(PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002437{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002438 PyDict_Clear((PyObject *)mp);
2439 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002440}
2441
Guido van Rossumba6ab842000-12-12 22:02:18 +00002442static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002443dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002444{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002445 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00002446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002447 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2448 return NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06002449
2450 return _PyDict_Pop(mp, key, deflt);
Guido van Rossume027d982002-04-12 15:11:59 +00002451}
2452
2453static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002454dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00002455{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002456 Py_hash_t i = 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002457 PyDictKeyEntry *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002458 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002459
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002461 /* Allocate the result tuple before checking the size. Believe it
2462 * or not, this allocation could trigger a garbage collection which
2463 * could empty the dict, so if we checked the size first and that
2464 * happened, the result would be an infinite loop (searching for an
2465 * entry that no longer exists). Note that the usual popitem()
2466 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2467 * tuple away if the dict *is* empty isn't a significant
2468 * inefficiency -- possible, but unlikely in practice.
2469 */
2470 res = PyTuple_New(2);
2471 if (res == NULL)
2472 return NULL;
2473 if (mp->ma_used == 0) {
2474 Py_DECREF(res);
2475 PyErr_SetString(PyExc_KeyError,
2476 "popitem(): dictionary is empty");
2477 return NULL;
2478 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002479 /* Convert split table to combined table */
2480 if (mp->ma_keys->dk_lookup == lookdict_split) {
2481 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
2482 Py_DECREF(res);
2483 return NULL;
2484 }
2485 }
2486 ENSURE_ALLOWS_DELETIONS(mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002487 /* Set ep to "the first" dict entry with a value. We abuse the hash
2488 * field of slot 0 to hold a search finger:
2489 * If slot 0 has a value, use slot 0.
2490 * Else slot 0 is being used to hold a search finger,
2491 * and we use its hash value as the first index to look.
2492 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002493 ep = &mp->ma_keys->dk_entries[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002494 if (ep->me_value == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002495 Py_ssize_t mask = DK_MASK(mp->ma_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002496 i = ep->me_hash;
2497 /* The hash field may be a real hash value, or it may be a
2498 * legit search finger, or it may be a once-legit search
2499 * finger that's out of bounds now because it wrapped around
2500 * or the table shrunk -- simply make sure it's in bounds now.
2501 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002502 if (i > mask || i < 1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002503 i = 1; /* skip slot 0 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002504 while ((ep = &mp->ma_keys->dk_entries[i])->me_value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002505 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002506 if (i > mask)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002507 i = 1;
2508 }
2509 }
2510 PyTuple_SET_ITEM(res, 0, ep->me_key);
2511 PyTuple_SET_ITEM(res, 1, ep->me_value);
2512 Py_INCREF(dummy);
2513 ep->me_key = dummy;
2514 ep->me_value = NULL;
2515 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002516 assert(mp->ma_keys->dk_entries[0].me_value == NULL);
2517 mp->ma_keys->dk_entries[0].me_hash = i + 1; /* next place to start */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002518 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002519}
2520
Jeremy Hylton8caad492000-06-23 14:18:11 +00002521static int
2522dict_traverse(PyObject *op, visitproc visit, void *arg)
2523{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002524 Py_ssize_t i, n;
2525 PyDictObject *mp = (PyDictObject *)op;
2526 if (mp->ma_keys->dk_lookup == lookdict) {
2527 for (i = 0; i < DK_SIZE(mp->ma_keys); i++) {
2528 if (mp->ma_keys->dk_entries[i].me_value != NULL) {
2529 Py_VISIT(mp->ma_keys->dk_entries[i].me_value);
2530 Py_VISIT(mp->ma_keys->dk_entries[i].me_key);
2531 }
2532 }
2533 } else {
2534 if (mp->ma_values != NULL) {
2535 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2536 Py_VISIT(mp->ma_values[i]);
2537 }
2538 }
2539 else {
2540 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2541 Py_VISIT(mp->ma_keys->dk_entries[i].me_value);
2542 }
2543 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002544 }
2545 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002546}
2547
2548static int
2549dict_tp_clear(PyObject *op)
2550{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002551 PyDict_Clear(op);
2552 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002553}
2554
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002555static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002556
Eric Snow96c6af92015-05-29 22:21:39 -06002557PyObject *
2558_PyDict_SizeOf(PyDictObject *mp)
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002559{
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002560 Py_ssize_t size, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002561
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002562 size = DK_SIZE(mp->ma_keys);
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002563 res = _PyObject_SIZE(Py_TYPE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002564 if (mp->ma_values)
2565 res += size * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002566 /* If the dictionary is split, the keys portion is accounted-for
2567 in the type object. */
2568 if (mp->ma_keys->dk_refcnt == 1)
2569 res += sizeof(PyDictKeysObject) + (size-1) * sizeof(PyDictKeyEntry);
2570 return PyLong_FromSsize_t(res);
2571}
2572
2573Py_ssize_t
2574_PyDict_KeysSize(PyDictKeysObject *keys)
2575{
2576 return sizeof(PyDictKeysObject) + (DK_SIZE(keys)-1) * sizeof(PyDictKeyEntry);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002577}
2578
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002579PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
2580
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002581PyDoc_STRVAR(sizeof__doc__,
2582"D.__sizeof__() -> size of D in memory, in bytes");
2583
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002584PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002585"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002586
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002587PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002588"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 +00002589
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002590PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002591"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002592If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00002593
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002594PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00002595"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000025962-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002597
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002598PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04002599"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
2600If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
2601If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
2602In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00002603
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002604PyDoc_STRVAR(clear__doc__,
2605"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002606
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002607PyDoc_STRVAR(copy__doc__,
2608"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00002609
Guido van Rossumb90c8482007-02-10 01:11:45 +00002610/* Forward */
2611static PyObject *dictkeys_new(PyObject *);
2612static PyObject *dictitems_new(PyObject *);
2613static PyObject *dictvalues_new(PyObject *);
2614
Guido van Rossum45c85d12007-07-27 16:31:40 +00002615PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002616 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002617PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002618 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002619PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002620 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00002621
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002622static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07002623 DICT___CONTAINS___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002624 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
2625 getitem__doc__},
Eric Snow96c6af92015-05-29 22:21:39 -06002626 {"__sizeof__", (PyCFunction)_PyDict_SizeOf, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002627 sizeof__doc__},
2628 {"get", (PyCFunction)dict_get, METH_VARARGS,
2629 get__doc__},
2630 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
2631 setdefault_doc__},
2632 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
2633 pop__doc__},
2634 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
2635 popitem__doc__},
2636 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
2637 keys__doc__},
2638 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
2639 items__doc__},
2640 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
2641 values__doc__},
2642 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
2643 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08002644 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002645 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
2646 clear__doc__},
2647 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
2648 copy__doc__},
2649 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002650};
2651
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002652/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002653int
2654PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002655{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002656 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002657 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002658 PyDictKeyEntry *ep;
2659 PyObject **value_addr;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002661 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002662 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002663 hash = PyObject_Hash(key);
2664 if (hash == -1)
2665 return -1;
2666 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002667 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
2668 return (ep == NULL) ? -1 : (*value_addr != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002669}
2670
Thomas Wouterscf297e42007-02-23 15:07:44 +00002671/* Internal version of PyDict_Contains used when the hash value is already known */
2672int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002673_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00002674{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002675 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002676 PyDictKeyEntry *ep;
2677 PyObject **value_addr;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002678
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002679 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
2680 return (ep == NULL) ? -1 : (*value_addr != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00002681}
2682
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002683/* Hack to implement "key in dict" */
2684static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002685 0, /* sq_length */
2686 0, /* sq_concat */
2687 0, /* sq_repeat */
2688 0, /* sq_item */
2689 0, /* sq_slice */
2690 0, /* sq_ass_item */
2691 0, /* sq_ass_slice */
2692 PyDict_Contains, /* sq_contains */
2693 0, /* sq_inplace_concat */
2694 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002695};
2696
Guido van Rossum09e563a2001-05-01 12:10:21 +00002697static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002698dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2699{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002700 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02002701 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002703 assert(type != NULL && type->tp_alloc != NULL);
2704 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02002705 if (self == NULL)
2706 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02002707 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02002708
Victor Stinnera9f61a52013-07-16 22:17:26 +02002709 /* The object has been implicitly tracked by tp_alloc */
2710 if (type == &PyDict_Type)
2711 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02002712
2713 d->ma_used = 0;
2714 d->ma_keys = new_keys_object(PyDict_MINSIZE_COMBINED);
2715 if (d->ma_keys == NULL) {
2716 Py_DECREF(self);
2717 return NULL;
2718 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002719 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002720}
2721
Tim Peters25786c02001-09-02 08:22:48 +00002722static int
2723dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2724{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002725 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002726}
2727
Tim Peters6d6c1a32001-08-02 04:15:00 +00002728static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002729dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002730{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002731 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002732}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002733
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002734PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002735"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002736"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002737" (key, value) pairs\n"
2738"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002739" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002740" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002741" d[k] = v\n"
2742"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2743" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002744
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002745PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002746 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2747 "dict",
2748 sizeof(PyDictObject),
2749 0,
2750 (destructor)dict_dealloc, /* tp_dealloc */
2751 0, /* tp_print */
2752 0, /* tp_getattr */
2753 0, /* tp_setattr */
2754 0, /* tp_reserved */
2755 (reprfunc)dict_repr, /* tp_repr */
2756 0, /* tp_as_number */
2757 &dict_as_sequence, /* tp_as_sequence */
2758 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002759 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002760 0, /* tp_call */
2761 0, /* tp_str */
2762 PyObject_GenericGetAttr, /* tp_getattro */
2763 0, /* tp_setattro */
2764 0, /* tp_as_buffer */
2765 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2766 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
2767 dictionary_doc, /* tp_doc */
2768 dict_traverse, /* tp_traverse */
2769 dict_tp_clear, /* tp_clear */
2770 dict_richcompare, /* tp_richcompare */
2771 0, /* tp_weaklistoffset */
2772 (getiterfunc)dict_iter, /* tp_iter */
2773 0, /* tp_iternext */
2774 mapp_methods, /* tp_methods */
2775 0, /* tp_members */
2776 0, /* tp_getset */
2777 0, /* tp_base */
2778 0, /* tp_dict */
2779 0, /* tp_descr_get */
2780 0, /* tp_descr_set */
2781 0, /* tp_dictoffset */
2782 dict_init, /* tp_init */
2783 PyType_GenericAlloc, /* tp_alloc */
2784 dict_new, /* tp_new */
2785 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002786};
2787
Victor Stinner3c1e4812012-03-26 22:10:51 +02002788PyObject *
2789_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
2790{
2791 PyObject *kv;
2792 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02002793 if (kv == NULL) {
2794 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002795 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02002796 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02002797 return PyDict_GetItem(dp, kv);
2798}
2799
Guido van Rossum3cca2451997-05-16 14:23:33 +00002800/* For backward compatibility with old dictionary interface */
2801
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002802PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002803PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002804{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002805 PyObject *kv, *rv;
2806 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02002807 if (kv == NULL) {
2808 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002809 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02002810 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002811 rv = PyDict_GetItem(v, kv);
2812 Py_DECREF(kv);
2813 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002814}
2815
2816int
Victor Stinner3c1e4812012-03-26 22:10:51 +02002817_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
2818{
2819 PyObject *kv;
2820 kv = _PyUnicode_FromId(key); /* borrowed */
2821 if (kv == NULL)
2822 return -1;
2823 return PyDict_SetItem(v, kv, item);
2824}
2825
2826int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002827PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002828{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002829 PyObject *kv;
2830 int err;
2831 kv = PyUnicode_FromString(key);
2832 if (kv == NULL)
2833 return -1;
2834 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
2835 err = PyDict_SetItem(v, kv, item);
2836 Py_DECREF(kv);
2837 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002838}
2839
2840int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01002841_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
2842{
2843 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
2844 if (kv == NULL)
2845 return -1;
2846 return PyDict_DelItem(v, kv);
2847}
2848
2849int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002850PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002851{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002852 PyObject *kv;
2853 int err;
2854 kv = PyUnicode_FromString(key);
2855 if (kv == NULL)
2856 return -1;
2857 err = PyDict_DelItem(v, kv);
2858 Py_DECREF(kv);
2859 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002860}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002861
Raymond Hettinger019a1482004-03-18 02:41:19 +00002862/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002863
2864typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002865 PyObject_HEAD
2866 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
2867 Py_ssize_t di_used;
2868 Py_ssize_t di_pos;
2869 PyObject* di_result; /* reusable result tuple for iteritems */
2870 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002871} dictiterobject;
2872
2873static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002874dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002875{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 dictiterobject *di;
2877 di = PyObject_GC_New(dictiterobject, itertype);
2878 if (di == NULL)
2879 return NULL;
2880 Py_INCREF(dict);
2881 di->di_dict = dict;
2882 di->di_used = dict->ma_used;
2883 di->di_pos = 0;
2884 di->len = dict->ma_used;
2885 if (itertype == &PyDictIterItem_Type) {
2886 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2887 if (di->di_result == NULL) {
2888 Py_DECREF(di);
2889 return NULL;
2890 }
2891 }
2892 else
2893 di->di_result = NULL;
2894 _PyObject_GC_TRACK(di);
2895 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002896}
2897
2898static void
2899dictiter_dealloc(dictiterobject *di)
2900{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 Py_XDECREF(di->di_dict);
2902 Py_XDECREF(di->di_result);
2903 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002904}
2905
2906static int
2907dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
2908{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002909 Py_VISIT(di->di_dict);
2910 Py_VISIT(di->di_result);
2911 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002912}
2913
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002914static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002915dictiter_len(dictiterobject *di)
2916{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 Py_ssize_t len = 0;
2918 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
2919 len = di->len;
2920 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002921}
2922
Guido van Rossumb90c8482007-02-10 01:11:45 +00002923PyDoc_STRVAR(length_hint_doc,
2924 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002925
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002926static PyObject *
2927dictiter_reduce(dictiterobject *di);
2928
2929PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
2930
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002931static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002932 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
2933 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002934 {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS,
2935 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002936 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002937};
2938
Raymond Hettinger019a1482004-03-18 02:41:19 +00002939static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002940{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002941 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002942 Py_ssize_t i, mask, offset;
2943 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002944 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002945 PyObject **value_ptr;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002946
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002947 if (d == NULL)
2948 return NULL;
2949 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002951 if (di->di_used != d->ma_used) {
2952 PyErr_SetString(PyExc_RuntimeError,
2953 "dictionary changed size during iteration");
2954 di->di_used = -1; /* Make this state sticky */
2955 return NULL;
2956 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002958 i = di->di_pos;
2959 if (i < 0)
2960 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002961 k = d->ma_keys;
2962 if (d->ma_values) {
2963 value_ptr = &d->ma_values[i];
2964 offset = sizeof(PyObject *);
2965 }
2966 else {
2967 value_ptr = &k->dk_entries[i].me_value;
2968 offset = sizeof(PyDictKeyEntry);
2969 }
2970 mask = DK_SIZE(k)-1;
2971 while (i <= mask && *value_ptr == NULL) {
2972 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002973 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002974 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002975 di->di_pos = i+1;
2976 if (i > mask)
2977 goto fail;
2978 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002979 key = k->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002980 Py_INCREF(key);
2981 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002982
2983fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002984 Py_DECREF(d);
2985 di->di_dict = NULL;
2986 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002987}
2988
Raymond Hettinger019a1482004-03-18 02:41:19 +00002989PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002990 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2991 "dict_keyiterator", /* tp_name */
2992 sizeof(dictiterobject), /* tp_basicsize */
2993 0, /* tp_itemsize */
2994 /* methods */
2995 (destructor)dictiter_dealloc, /* tp_dealloc */
2996 0, /* tp_print */
2997 0, /* tp_getattr */
2998 0, /* tp_setattr */
2999 0, /* tp_reserved */
3000 0, /* tp_repr */
3001 0, /* tp_as_number */
3002 0, /* tp_as_sequence */
3003 0, /* tp_as_mapping */
3004 0, /* tp_hash */
3005 0, /* tp_call */
3006 0, /* tp_str */
3007 PyObject_GenericGetAttr, /* tp_getattro */
3008 0, /* tp_setattro */
3009 0, /* tp_as_buffer */
3010 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3011 0, /* tp_doc */
3012 (traverseproc)dictiter_traverse, /* tp_traverse */
3013 0, /* tp_clear */
3014 0, /* tp_richcompare */
3015 0, /* tp_weaklistoffset */
3016 PyObject_SelfIter, /* tp_iter */
3017 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
3018 dictiter_methods, /* tp_methods */
3019 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003020};
3021
3022static PyObject *dictiter_iternextvalue(dictiterobject *di)
3023{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003024 PyObject *value;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003025 Py_ssize_t i, mask, offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003026 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003027 PyObject **value_ptr;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003029 if (d == NULL)
3030 return NULL;
3031 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003032
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003033 if (di->di_used != d->ma_used) {
3034 PyErr_SetString(PyExc_RuntimeError,
3035 "dictionary changed size during iteration");
3036 di->di_used = -1; /* Make this state sticky */
3037 return NULL;
3038 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003040 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003041 mask = DK_SIZE(d->ma_keys)-1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003042 if (i < 0 || i > mask)
3043 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003044 if (d->ma_values) {
3045 value_ptr = &d->ma_values[i];
3046 offset = sizeof(PyObject *);
3047 }
3048 else {
3049 value_ptr = &d->ma_keys->dk_entries[i].me_value;
3050 offset = sizeof(PyDictKeyEntry);
3051 }
3052 while (i <= mask && *value_ptr == NULL) {
3053 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003054 i++;
3055 if (i > mask)
3056 goto fail;
3057 }
3058 di->di_pos = i+1;
3059 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003060 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003061 Py_INCREF(value);
3062 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003063
3064fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003065 Py_DECREF(d);
3066 di->di_dict = NULL;
3067 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003068}
3069
3070PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003071 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3072 "dict_valueiterator", /* tp_name */
3073 sizeof(dictiterobject), /* tp_basicsize */
3074 0, /* tp_itemsize */
3075 /* methods */
3076 (destructor)dictiter_dealloc, /* tp_dealloc */
3077 0, /* tp_print */
3078 0, /* tp_getattr */
3079 0, /* tp_setattr */
3080 0, /* tp_reserved */
3081 0, /* tp_repr */
3082 0, /* tp_as_number */
3083 0, /* tp_as_sequence */
3084 0, /* tp_as_mapping */
3085 0, /* tp_hash */
3086 0, /* tp_call */
3087 0, /* tp_str */
3088 PyObject_GenericGetAttr, /* tp_getattro */
3089 0, /* tp_setattro */
3090 0, /* tp_as_buffer */
3091 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3092 0, /* tp_doc */
3093 (traverseproc)dictiter_traverse, /* tp_traverse */
3094 0, /* tp_clear */
3095 0, /* tp_richcompare */
3096 0, /* tp_weaklistoffset */
3097 PyObject_SelfIter, /* tp_iter */
3098 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3099 dictiter_methods, /* tp_methods */
3100 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003101};
3102
3103static PyObject *dictiter_iternextitem(dictiterobject *di)
3104{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003105 PyObject *key, *value, *result = di->di_result;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003106 Py_ssize_t i, mask, offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003107 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003108 PyObject **value_ptr;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003110 if (d == NULL)
3111 return NULL;
3112 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003114 if (di->di_used != d->ma_used) {
3115 PyErr_SetString(PyExc_RuntimeError,
3116 "dictionary changed size during iteration");
3117 di->di_used = -1; /* Make this state sticky */
3118 return NULL;
3119 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003121 i = di->di_pos;
3122 if (i < 0)
3123 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003124 mask = DK_SIZE(d->ma_keys)-1;
3125 if (d->ma_values) {
3126 value_ptr = &d->ma_values[i];
3127 offset = sizeof(PyObject *);
3128 }
3129 else {
3130 value_ptr = &d->ma_keys->dk_entries[i].me_value;
3131 offset = sizeof(PyDictKeyEntry);
3132 }
3133 while (i <= mask && *value_ptr == NULL) {
3134 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003135 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003136 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003137 di->di_pos = i+1;
3138 if (i > mask)
3139 goto fail;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003141 if (result->ob_refcnt == 1) {
3142 Py_INCREF(result);
3143 Py_DECREF(PyTuple_GET_ITEM(result, 0));
3144 Py_DECREF(PyTuple_GET_ITEM(result, 1));
3145 } else {
3146 result = PyTuple_New(2);
3147 if (result == NULL)
3148 return NULL;
3149 }
3150 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003151 key = d->ma_keys->dk_entries[i].me_key;
3152 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003153 Py_INCREF(key);
3154 Py_INCREF(value);
Eric Snow96c6af92015-05-29 22:21:39 -06003155 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3156 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003157 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003158
3159fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003160 Py_DECREF(d);
3161 di->di_dict = NULL;
3162 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003163}
3164
3165PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003166 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3167 "dict_itemiterator", /* tp_name */
3168 sizeof(dictiterobject), /* tp_basicsize */
3169 0, /* tp_itemsize */
3170 /* methods */
3171 (destructor)dictiter_dealloc, /* tp_dealloc */
3172 0, /* tp_print */
3173 0, /* tp_getattr */
3174 0, /* tp_setattr */
3175 0, /* tp_reserved */
3176 0, /* tp_repr */
3177 0, /* tp_as_number */
3178 0, /* tp_as_sequence */
3179 0, /* tp_as_mapping */
3180 0, /* tp_hash */
3181 0, /* tp_call */
3182 0, /* tp_str */
3183 PyObject_GenericGetAttr, /* tp_getattro */
3184 0, /* tp_setattro */
3185 0, /* tp_as_buffer */
3186 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3187 0, /* tp_doc */
3188 (traverseproc)dictiter_traverse, /* tp_traverse */
3189 0, /* tp_clear */
3190 0, /* tp_richcompare */
3191 0, /* tp_weaklistoffset */
3192 PyObject_SelfIter, /* tp_iter */
3193 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3194 dictiter_methods, /* tp_methods */
3195 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003196};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003197
3198
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003199static PyObject *
3200dictiter_reduce(dictiterobject *di)
3201{
3202 PyObject *list;
3203 dictiterobject tmp;
3204
3205 list = PyList_New(0);
3206 if (!list)
3207 return NULL;
3208
3209 /* copy the itertor state */
3210 tmp = *di;
3211 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003212
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003213 /* iterate the temporary into a list */
3214 for(;;) {
3215 PyObject *element = 0;
3216 if (Py_TYPE(di) == &PyDictIterItem_Type)
3217 element = dictiter_iternextitem(&tmp);
3218 else if (Py_TYPE(di) == &PyDictIterKey_Type)
3219 element = dictiter_iternextkey(&tmp);
3220 else if (Py_TYPE(di) == &PyDictIterValue_Type)
3221 element = dictiter_iternextvalue(&tmp);
3222 else
3223 assert(0);
3224 if (element) {
3225 if (PyList_Append(list, element)) {
3226 Py_DECREF(element);
3227 Py_DECREF(list);
3228 Py_XDECREF(tmp.di_dict);
3229 return NULL;
3230 }
3231 Py_DECREF(element);
3232 } else
3233 break;
3234 }
3235 Py_XDECREF(tmp.di_dict);
3236 /* check for error */
3237 if (tmp.di_dict != NULL) {
3238 /* we have an error */
3239 Py_DECREF(list);
3240 return NULL;
3241 }
Antoine Pitroua7013882012-04-05 00:04:20 +02003242 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003243}
3244
Guido van Rossum3ac67412007-02-10 18:55:06 +00003245/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003246/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003247/***********************************************/
3248
Guido van Rossumb90c8482007-02-10 01:11:45 +00003249/* The instance lay-out is the same for all three; but the type differs. */
3250
Guido van Rossumb90c8482007-02-10 01:11:45 +00003251static void
Eric Snow96c6af92015-05-29 22:21:39 -06003252dictview_dealloc(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003253{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003254 Py_XDECREF(dv->dv_dict);
3255 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003256}
3257
3258static int
Eric Snow96c6af92015-05-29 22:21:39 -06003259dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003260{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003261 Py_VISIT(dv->dv_dict);
3262 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003263}
3264
Guido van Rossum83825ac2007-02-10 04:54:19 +00003265static Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003266dictview_len(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003267{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003268 Py_ssize_t len = 0;
3269 if (dv->dv_dict != NULL)
3270 len = dv->dv_dict->ma_used;
3271 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003272}
3273
Eric Snow96c6af92015-05-29 22:21:39 -06003274PyObject *
3275_PyDictView_New(PyObject *dict, PyTypeObject *type)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003276{
Eric Snow96c6af92015-05-29 22:21:39 -06003277 _PyDictViewObject *dv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003278 if (dict == NULL) {
3279 PyErr_BadInternalCall();
3280 return NULL;
3281 }
3282 if (!PyDict_Check(dict)) {
3283 /* XXX Get rid of this restriction later */
3284 PyErr_Format(PyExc_TypeError,
3285 "%s() requires a dict argument, not '%s'",
3286 type->tp_name, dict->ob_type->tp_name);
3287 return NULL;
3288 }
Eric Snow96c6af92015-05-29 22:21:39 -06003289 dv = PyObject_GC_New(_PyDictViewObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003290 if (dv == NULL)
3291 return NULL;
3292 Py_INCREF(dict);
3293 dv->dv_dict = (PyDictObject *)dict;
3294 _PyObject_GC_TRACK(dv);
3295 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003296}
3297
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003298/* TODO(guido): The views objects are not complete:
3299
3300 * support more set operations
3301 * support arbitrary mappings?
3302 - either these should be static or exported in dictobject.h
3303 - if public then they should probably be in builtins
3304*/
3305
Guido van Rossumaac530c2007-08-24 22:33:45 +00003306/* Return 1 if self is a subset of other, iterating over self;
3307 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003308static int
3309all_contained_in(PyObject *self, PyObject *other)
3310{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003311 PyObject *iter = PyObject_GetIter(self);
3312 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003314 if (iter == NULL)
3315 return -1;
3316 for (;;) {
3317 PyObject *next = PyIter_Next(iter);
3318 if (next == NULL) {
3319 if (PyErr_Occurred())
3320 ok = -1;
3321 break;
3322 }
3323 ok = PySequence_Contains(other, next);
3324 Py_DECREF(next);
3325 if (ok <= 0)
3326 break;
3327 }
3328 Py_DECREF(iter);
3329 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003330}
3331
3332static PyObject *
3333dictview_richcompare(PyObject *self, PyObject *other, int op)
3334{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003335 Py_ssize_t len_self, len_other;
3336 int ok;
3337 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003339 assert(self != NULL);
3340 assert(PyDictViewSet_Check(self));
3341 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00003342
Brian Curtindfc80e32011-08-10 20:28:54 -05003343 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
3344 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003346 len_self = PyObject_Size(self);
3347 if (len_self < 0)
3348 return NULL;
3349 len_other = PyObject_Size(other);
3350 if (len_other < 0)
3351 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003353 ok = 0;
3354 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00003355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003356 case Py_NE:
3357 case Py_EQ:
3358 if (len_self == len_other)
3359 ok = all_contained_in(self, other);
3360 if (op == Py_NE && ok >= 0)
3361 ok = !ok;
3362 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003364 case Py_LT:
3365 if (len_self < len_other)
3366 ok = all_contained_in(self, other);
3367 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003369 case Py_LE:
3370 if (len_self <= len_other)
3371 ok = all_contained_in(self, other);
3372 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003374 case Py_GT:
3375 if (len_self > len_other)
3376 ok = all_contained_in(other, self);
3377 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003379 case Py_GE:
3380 if (len_self >= len_other)
3381 ok = all_contained_in(other, self);
3382 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003384 }
3385 if (ok < 0)
3386 return NULL;
3387 result = ok ? Py_True : Py_False;
3388 Py_INCREF(result);
3389 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003390}
3391
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003392static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003393dictview_repr(_PyDictViewObject *dv)
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003394{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003395 PyObject *seq;
3396 PyObject *result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003398 seq = PySequence_List((PyObject *)dv);
3399 if (seq == NULL)
3400 return NULL;
3401
3402 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
3403 Py_DECREF(seq);
3404 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003405}
3406
Guido van Rossum3ac67412007-02-10 18:55:06 +00003407/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003408
3409static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003410dictkeys_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003411{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003412 if (dv->dv_dict == NULL) {
3413 Py_RETURN_NONE;
3414 }
3415 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003416}
3417
3418static int
Eric Snow96c6af92015-05-29 22:21:39 -06003419dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00003420{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003421 if (dv->dv_dict == NULL)
3422 return 0;
3423 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003424}
3425
Guido van Rossum83825ac2007-02-10 04:54:19 +00003426static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003427 (lenfunc)dictview_len, /* sq_length */
3428 0, /* sq_concat */
3429 0, /* sq_repeat */
3430 0, /* sq_item */
3431 0, /* sq_slice */
3432 0, /* sq_ass_item */
3433 0, /* sq_ass_slice */
3434 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003435};
3436
Guido van Rossum523259b2007-08-24 23:41:22 +00003437static PyObject*
3438dictviews_sub(PyObject* self, PyObject *other)
3439{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003440 PyObject *result = PySet_New(self);
3441 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003442 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003443
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003444 if (result == NULL)
3445 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003446
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003447 tmp = _PyObject_CallMethodId(result, &PyId_difference_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003448 if (tmp == NULL) {
3449 Py_DECREF(result);
3450 return NULL;
3451 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003453 Py_DECREF(tmp);
3454 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003455}
3456
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003457PyObject*
3458_PyDictView_Intersect(PyObject* self, PyObject *other)
Guido van Rossum523259b2007-08-24 23:41:22 +00003459{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003460 PyObject *result = PySet_New(self);
3461 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003462 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003464 if (result == NULL)
3465 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003466
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003467 tmp = _PyObject_CallMethodId(result, &PyId_intersection_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003468 if (tmp == NULL) {
3469 Py_DECREF(result);
3470 return NULL;
3471 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003473 Py_DECREF(tmp);
3474 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003475}
3476
3477static PyObject*
3478dictviews_or(PyObject* self, PyObject *other)
3479{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003480 PyObject *result = PySet_New(self);
3481 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003482 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003484 if (result == NULL)
3485 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003486
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003487 tmp = _PyObject_CallMethodId(result, &PyId_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003488 if (tmp == NULL) {
3489 Py_DECREF(result);
3490 return NULL;
3491 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003493 Py_DECREF(tmp);
3494 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003495}
3496
3497static PyObject*
3498dictviews_xor(PyObject* self, PyObject *other)
3499{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003500 PyObject *result = PySet_New(self);
3501 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003502 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003504 if (result == NULL)
3505 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003506
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003507 tmp = _PyObject_CallMethodId(result, &PyId_symmetric_difference_update, "O",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003508 other);
3509 if (tmp == NULL) {
3510 Py_DECREF(result);
3511 return NULL;
3512 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003514 Py_DECREF(tmp);
3515 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003516}
3517
3518static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003519 0, /*nb_add*/
3520 (binaryfunc)dictviews_sub, /*nb_subtract*/
3521 0, /*nb_multiply*/
3522 0, /*nb_remainder*/
3523 0, /*nb_divmod*/
3524 0, /*nb_power*/
3525 0, /*nb_negative*/
3526 0, /*nb_positive*/
3527 0, /*nb_absolute*/
3528 0, /*nb_bool*/
3529 0, /*nb_invert*/
3530 0, /*nb_lshift*/
3531 0, /*nb_rshift*/
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003532 (binaryfunc)_PyDictView_Intersect, /*nb_and*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003533 (binaryfunc)dictviews_xor, /*nb_xor*/
3534 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00003535};
3536
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003537static PyObject*
3538dictviews_isdisjoint(PyObject *self, PyObject *other)
3539{
3540 PyObject *it;
3541 PyObject *item = NULL;
3542
3543 if (self == other) {
Eric Snow96c6af92015-05-29 22:21:39 -06003544 if (dictview_len((_PyDictViewObject *)self) == 0)
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003545 Py_RETURN_TRUE;
3546 else
3547 Py_RETURN_FALSE;
3548 }
3549
3550 /* Iterate over the shorter object (only if other is a set,
3551 * because PySequence_Contains may be expensive otherwise): */
3552 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
Eric Snow96c6af92015-05-29 22:21:39 -06003553 Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self);
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003554 Py_ssize_t len_other = PyObject_Size(other);
3555 if (len_other == -1)
3556 return NULL;
3557
3558 if ((len_other > len_self)) {
3559 PyObject *tmp = other;
3560 other = self;
3561 self = tmp;
3562 }
3563 }
3564
3565 it = PyObject_GetIter(other);
3566 if (it == NULL)
3567 return NULL;
3568
3569 while ((item = PyIter_Next(it)) != NULL) {
3570 int contains = PySequence_Contains(self, item);
3571 Py_DECREF(item);
3572 if (contains == -1) {
3573 Py_DECREF(it);
3574 return NULL;
3575 }
3576
3577 if (contains) {
3578 Py_DECREF(it);
3579 Py_RETURN_FALSE;
3580 }
3581 }
3582 Py_DECREF(it);
3583 if (PyErr_Occurred())
3584 return NULL; /* PyIter_Next raised an exception. */
3585 Py_RETURN_TRUE;
3586}
3587
3588PyDoc_STRVAR(isdisjoint_doc,
3589"Return True if the view and the given iterable have a null intersection.");
3590
Guido van Rossumb90c8482007-02-10 01:11:45 +00003591static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003592 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3593 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003594 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003595};
3596
3597PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003598 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3599 "dict_keys", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06003600 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003601 0, /* tp_itemsize */
3602 /* methods */
3603 (destructor)dictview_dealloc, /* tp_dealloc */
3604 0, /* tp_print */
3605 0, /* tp_getattr */
3606 0, /* tp_setattr */
3607 0, /* tp_reserved */
3608 (reprfunc)dictview_repr, /* tp_repr */
3609 &dictviews_as_number, /* tp_as_number */
3610 &dictkeys_as_sequence, /* tp_as_sequence */
3611 0, /* tp_as_mapping */
3612 0, /* tp_hash */
3613 0, /* tp_call */
3614 0, /* tp_str */
3615 PyObject_GenericGetAttr, /* tp_getattro */
3616 0, /* tp_setattro */
3617 0, /* tp_as_buffer */
3618 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3619 0, /* tp_doc */
3620 (traverseproc)dictview_traverse, /* tp_traverse */
3621 0, /* tp_clear */
3622 dictview_richcompare, /* tp_richcompare */
3623 0, /* tp_weaklistoffset */
3624 (getiterfunc)dictkeys_iter, /* tp_iter */
3625 0, /* tp_iternext */
3626 dictkeys_methods, /* tp_methods */
3627 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003628};
3629
3630static PyObject *
3631dictkeys_new(PyObject *dict)
3632{
Eric Snow96c6af92015-05-29 22:21:39 -06003633 return _PyDictView_New(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003634}
3635
Guido van Rossum3ac67412007-02-10 18:55:06 +00003636/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003637
3638static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003639dictitems_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003640{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003641 if (dv->dv_dict == NULL) {
3642 Py_RETURN_NONE;
3643 }
3644 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003645}
3646
3647static int
Eric Snow96c6af92015-05-29 22:21:39 -06003648dictitems_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00003649{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003650 PyObject *key, *value, *found;
3651 if (dv->dv_dict == NULL)
3652 return 0;
3653 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
3654 return 0;
3655 key = PyTuple_GET_ITEM(obj, 0);
3656 value = PyTuple_GET_ITEM(obj, 1);
3657 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
3658 if (found == NULL) {
3659 if (PyErr_Occurred())
3660 return -1;
3661 return 0;
3662 }
3663 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003664}
3665
Guido van Rossum83825ac2007-02-10 04:54:19 +00003666static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003667 (lenfunc)dictview_len, /* sq_length */
3668 0, /* sq_concat */
3669 0, /* sq_repeat */
3670 0, /* sq_item */
3671 0, /* sq_slice */
3672 0, /* sq_ass_item */
3673 0, /* sq_ass_slice */
3674 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003675};
3676
Guido van Rossumb90c8482007-02-10 01:11:45 +00003677static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003678 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3679 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003680 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003681};
3682
3683PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003684 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3685 "dict_items", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06003686 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003687 0, /* tp_itemsize */
3688 /* methods */
3689 (destructor)dictview_dealloc, /* tp_dealloc */
3690 0, /* tp_print */
3691 0, /* tp_getattr */
3692 0, /* tp_setattr */
3693 0, /* tp_reserved */
3694 (reprfunc)dictview_repr, /* tp_repr */
3695 &dictviews_as_number, /* tp_as_number */
3696 &dictitems_as_sequence, /* tp_as_sequence */
3697 0, /* tp_as_mapping */
3698 0, /* tp_hash */
3699 0, /* tp_call */
3700 0, /* tp_str */
3701 PyObject_GenericGetAttr, /* tp_getattro */
3702 0, /* tp_setattro */
3703 0, /* tp_as_buffer */
3704 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3705 0, /* tp_doc */
3706 (traverseproc)dictview_traverse, /* tp_traverse */
3707 0, /* tp_clear */
3708 dictview_richcompare, /* tp_richcompare */
3709 0, /* tp_weaklistoffset */
3710 (getiterfunc)dictitems_iter, /* tp_iter */
3711 0, /* tp_iternext */
3712 dictitems_methods, /* tp_methods */
3713 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003714};
3715
3716static PyObject *
3717dictitems_new(PyObject *dict)
3718{
Eric Snow96c6af92015-05-29 22:21:39 -06003719 return _PyDictView_New(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003720}
3721
Guido van Rossum3ac67412007-02-10 18:55:06 +00003722/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003723
3724static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003725dictvalues_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003726{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003727 if (dv->dv_dict == NULL) {
3728 Py_RETURN_NONE;
3729 }
3730 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003731}
3732
Guido van Rossum83825ac2007-02-10 04:54:19 +00003733static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003734 (lenfunc)dictview_len, /* sq_length */
3735 0, /* sq_concat */
3736 0, /* sq_repeat */
3737 0, /* sq_item */
3738 0, /* sq_slice */
3739 0, /* sq_ass_item */
3740 0, /* sq_ass_slice */
3741 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003742};
3743
Guido van Rossumb90c8482007-02-10 01:11:45 +00003744static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003745 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003746};
3747
3748PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003749 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3750 "dict_values", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06003751 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003752 0, /* tp_itemsize */
3753 /* methods */
3754 (destructor)dictview_dealloc, /* tp_dealloc */
3755 0, /* tp_print */
3756 0, /* tp_getattr */
3757 0, /* tp_setattr */
3758 0, /* tp_reserved */
3759 (reprfunc)dictview_repr, /* tp_repr */
3760 0, /* tp_as_number */
3761 &dictvalues_as_sequence, /* tp_as_sequence */
3762 0, /* tp_as_mapping */
3763 0, /* tp_hash */
3764 0, /* tp_call */
3765 0, /* tp_str */
3766 PyObject_GenericGetAttr, /* tp_getattro */
3767 0, /* tp_setattro */
3768 0, /* tp_as_buffer */
3769 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3770 0, /* tp_doc */
3771 (traverseproc)dictview_traverse, /* tp_traverse */
3772 0, /* tp_clear */
3773 0, /* tp_richcompare */
3774 0, /* tp_weaklistoffset */
3775 (getiterfunc)dictvalues_iter, /* tp_iter */
3776 0, /* tp_iternext */
3777 dictvalues_methods, /* tp_methods */
3778 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003779};
3780
3781static PyObject *
3782dictvalues_new(PyObject *dict)
3783{
Eric Snow96c6af92015-05-29 22:21:39 -06003784 return _PyDictView_New(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003785}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003786
3787/* Returns NULL if cannot allocate a new PyDictKeysObject,
3788 but does not set an error */
3789PyDictKeysObject *
3790_PyDict_NewKeysForClass(void)
3791{
3792 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE_SPLIT);
3793 if (keys == NULL)
3794 PyErr_Clear();
3795 else
3796 keys->dk_lookup = lookdict_split;
3797 return keys;
3798}
3799
3800#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
3801
3802PyObject *
3803PyObject_GenericGetDict(PyObject *obj, void *context)
3804{
3805 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
3806 if (dictptr == NULL) {
3807 PyErr_SetString(PyExc_AttributeError,
3808 "This object has no __dict__");
3809 return NULL;
3810 }
3811 dict = *dictptr;
3812 if (dict == NULL) {
3813 PyTypeObject *tp = Py_TYPE(obj);
3814 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
3815 DK_INCREF(CACHED_KEYS(tp));
3816 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
3817 }
3818 else {
3819 *dictptr = dict = PyDict_New();
3820 }
3821 }
3822 Py_XINCREF(dict);
3823 return dict;
3824}
3825
3826int
3827_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
3828 PyObject *key, PyObject *value)
3829{
3830 PyObject *dict;
3831 int res;
3832 PyDictKeysObject *cached;
3833
3834 assert(dictptr != NULL);
3835 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
3836 assert(dictptr != NULL);
3837 dict = *dictptr;
3838 if (dict == NULL) {
3839 DK_INCREF(cached);
3840 dict = new_dict_with_shared_keys(cached);
3841 if (dict == NULL)
3842 return -1;
3843 *dictptr = dict;
3844 }
3845 if (value == NULL) {
3846 res = PyDict_DelItem(dict, key);
3847 if (cached != ((PyDictObject *)dict)->ma_keys) {
3848 CACHED_KEYS(tp) = NULL;
3849 DK_DECREF(cached);
3850 }
3851 } else {
3852 res = PyDict_SetItem(dict, key, value);
3853 if (cached != ((PyDictObject *)dict)->ma_keys) {
3854 /* Either update tp->ht_cached_keys or delete it */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04003855 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003856 CACHED_KEYS(tp) = make_keys_shared(dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003857 } else {
3858 CACHED_KEYS(tp) = NULL;
3859 }
3860 DK_DECREF(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04003861 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
3862 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003863 }
3864 }
3865 } else {
3866 dict = *dictptr;
3867 if (dict == NULL) {
3868 dict = PyDict_New();
3869 if (dict == NULL)
3870 return -1;
3871 *dictptr = dict;
3872 }
3873 if (value == NULL) {
3874 res = PyDict_DelItem(dict, key);
3875 } else {
3876 res = PyDict_SetItem(dict, key, value);
3877 }
3878 }
3879 return res;
3880}
3881
3882void
3883_PyDictKeys_DecRef(PyDictKeysObject *keys)
3884{
3885 DK_DECREF(keys);
3886}
3887
3888
3889/* ARGSUSED */
3890static PyObject *
3891dummy_repr(PyObject *op)
3892{
3893 return PyUnicode_FromString("<dummy key>");
3894}
3895
3896/* ARGUSED */
3897static void
3898dummy_dealloc(PyObject* ignore)
3899{
3900 /* This should never get called, but we also don't want to SEGV if
3901 * we accidentally decref dummy-key out of existence.
3902 */
3903 Py_FatalError("deallocating <dummy key>");
3904}
3905
3906static PyTypeObject PyDictDummy_Type = {
3907 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3908 "<dummy key> type",
3909 0,
3910 0,
3911 dummy_dealloc, /*tp_dealloc*/ /*never called*/
3912 0, /*tp_print*/
3913 0, /*tp_getattr*/
3914 0, /*tp_setattr*/
3915 0, /*tp_reserved*/
3916 dummy_repr, /*tp_repr*/
3917 0, /*tp_as_number*/
3918 0, /*tp_as_sequence*/
3919 0, /*tp_as_mapping*/
3920 0, /*tp_hash */
3921 0, /*tp_call */
3922 0, /*tp_str */
3923 0, /*tp_getattro */
3924 0, /*tp_setattro */
3925 0, /*tp_as_buffer */
3926 Py_TPFLAGS_DEFAULT, /*tp_flags */
3927};
3928
3929static PyObject _dummy_struct = {
3930 _PyObject_EXTRA_INIT
3931 2, &PyDictDummy_Type
3932};
3933