blob: e04ab2b92f444fc99b9e32314ff51e75abb84086 [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
R David Murray537ad7a2016-07-10 12:33:18 -040091important hash functions (for ints) are very regular in common
Tim Peterseb28ef22001-06-02 05:27:19 +000092cases:
Tim Peters15d49292001-05-27 07:39:22 +000093
R David Murray537ad7a2016-07-10 12:33:18 -040094 >>>[hash(i) for i in range(4)]
Guido van Rossumdc5f6b22006-08-24 21:29:26 +000095 [0, 1, 2, 3]
Tim Peters15d49292001-05-27 07:39:22 +000096
Tim Peterseb28ef22001-06-02 05:27:19 +000097This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
98the low-order i bits as the initial table index is extremely fast, and there
R David Murray537ad7a2016-07-10 12:33:18 -040099are no collisions at all for dicts indexed by a contiguous range of ints. So
100this gives better-than-random behavior in common cases, and that's very
101desirable.
Tim Peters15d49292001-05-27 07:39:22 +0000102
Tim Peterseb28ef22001-06-02 05:27:19 +0000103OTOH, when collisions occur, the tendency to fill contiguous slices of the
104hash table makes a good collision resolution strategy crucial. Taking only
105the last i bits of the hash code is also vulnerable: for example, consider
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106the list [i << 16 for i in range(20000)] as a set of keys. Since ints are
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000107their own hash codes, and this fits in a dict of size 2**15, the last 15 bits
108 of every hash code are all 0: they *all* map to the same table index.
Tim Peters15d49292001-05-27 07:39:22 +0000109
Tim Peterseb28ef22001-06-02 05:27:19 +0000110But catering to unusual cases should not slow the usual ones, so we just take
111the last i bits anyway. It's up to collision resolution to do the rest. If
112we *usually* find the key we're looking for on the first try (and, it turns
113out, we usually do -- the table load factor is kept under 2/3, so the odds
114are solidly in our favor), then it makes best sense to keep the initial index
115computation dirt cheap.
Tim Peters15d49292001-05-27 07:39:22 +0000116
Tim Peterseb28ef22001-06-02 05:27:19 +0000117The first half of collision resolution is to visit table indices via this
118recurrence:
Tim Peters15d49292001-05-27 07:39:22 +0000119
Tim Peterseb28ef22001-06-02 05:27:19 +0000120 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +0000121
Tim Peterseb28ef22001-06-02 05:27:19 +0000122For any initial j in range(2**i), repeating that 2**i times generates each
123int in range(2**i) exactly once (see any text on random-number generation for
124proof). By itself, this doesn't help much: like linear probing (setting
125j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
126order. This would be bad, except that's not the only thing we do, and it's
127actually *good* in the common cases where hash keys are consecutive. In an
128example that's really too small to make this entirely clear, for a table of
129size 2**3 the order of indices is:
Tim Peters15d49292001-05-27 07:39:22 +0000130
Tim Peterseb28ef22001-06-02 05:27:19 +0000131 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
132
133If two things come in at index 5, the first place we look after is index 2,
134not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
135Linear probing is deadly in this case because there the fixed probe order
136is the *same* as the order consecutive keys are likely to arrive. But it's
137extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
138and certain that consecutive hash codes do not.
139
140The other half of the strategy is to get the other bits of the hash code
141into play. This is done by initializing a (unsigned) vrbl "perturb" to the
142full hash code, and changing the recurrence to:
143
144 j = (5*j) + 1 + perturb;
145 perturb >>= PERTURB_SHIFT;
146 use j % 2**i as the next table index;
147
148Now the probe sequence depends (eventually) on every bit in the hash code,
149and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
150because it quickly magnifies small differences in the bits that didn't affect
151the initial index. Note that because perturb is unsigned, if the recurrence
152is executed often enough perturb eventually becomes and remains 0. At that
153point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
154that's certain to find an empty slot eventually (since it generates every int
155in range(2**i), and we make sure there's always at least one empty slot).
156
157Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
158small so that the high bits of the hash code continue to affect the probe
159sequence across iterations; but you want it large so that in really bad cases
160the high-order hash bits have an effect on early iterations. 5 was "the
161best" in minimizing total collisions across experiments Tim Peters ran (on
162both normal and pathological cases), but 4 and 6 weren't significantly worse.
163
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000164Historical: Reimer Behrends contributed the idea of using a polynomial-based
Tim Peterseb28ef22001-06-02 05:27:19 +0000165approach, using repeated multiplication by x in GF(2**n) where an irreducible
166polynomial for each table size was chosen such that x was a primitive root.
167Christian Tismer later extended that to use division by x instead, as an
168efficient way to get the high bits of the hash code into play. This scheme
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000169also gave excellent collision statistics, but was more expensive: two
170if-tests were required inside the loop; computing "the next" index took about
171the same number of operations but without as much potential parallelism
172(e.g., computing 5*j can go on at the same time as computing 1+perturb in the
173above, and then shifting perturb can be done while the table index is being
174masked); and the PyDictObject struct required a member to hold the table's
175polynomial. In Tim's experiments the current scheme ran faster, produced
176equally good collision statistics, needed less code & used less memory.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000177
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000178*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000179
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400180/* Object used as dummy key to fill deleted entries
181 * This could be any unique object,
182 * use a custom type in order to minimise coupling.
183*/
184static PyObject _dummy_struct;
185
186#define dummy (&_dummy_struct)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000187
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000188#ifdef Py_REF_DEBUG
189PyObject *
190_PyDict_Dummy(void)
191{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 return dummy;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000193}
194#endif
195
Fred Drake1bff34a2000-08-31 19:31:38 +0000196/* forward declarations */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400197static PyDictKeyEntry *lookdict(PyDictObject *mp, PyObject *key,
198 Py_hash_t hash, PyObject ***value_addr);
199static PyDictKeyEntry *lookdict_unicode(PyDictObject *mp, PyObject *key,
200 Py_hash_t hash, PyObject ***value_addr);
201static PyDictKeyEntry *
202lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
203 Py_hash_t hash, PyObject ***value_addr);
204static PyDictKeyEntry *lookdict_split(PyDictObject *mp, PyObject *key,
205 Py_hash_t hash, PyObject ***value_addr);
Fred Drake1bff34a2000-08-31 19:31:38 +0000206
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400207static int dictresize(PyDictObject *mp, Py_ssize_t minused);
Tim Petersdea48ec2001-05-22 20:40:22 +0000208
Raymond Hettinger43442782004-03-17 21:55:03 +0000209/* Dictionary reuse scheme to save calls to malloc, free, and memset */
Christian Heimes2202f872008-02-06 14:31:34 +0000210#ifndef PyDict_MAXFREELIST
211#define PyDict_MAXFREELIST 80
212#endif
213static PyDictObject *free_list[PyDict_MAXFREELIST];
214static int numfree = 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000215
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300216#include "clinic/dictobject.c.h"
217
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100218int
219PyDict_ClearFreeList(void)
Christian Heimes77c02eb2008-02-09 02:18:51 +0000220{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 PyDictObject *op;
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100222 int ret = numfree;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 while (numfree) {
224 op = free_list[--numfree];
225 assert(PyDict_CheckExact(op));
226 PyObject_GC_Del(op);
227 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100228 return ret;
229}
230
David Malcolm49526f42012-06-22 14:55:41 -0400231/* Print summary info about the state of the optimized allocator */
232void
233_PyDict_DebugMallocStats(FILE *out)
234{
235 _PyDebugAllocatorStats(out,
236 "free PyDictObject", numfree, sizeof(PyDictObject));
237}
238
239
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100240void
241PyDict_Fini(void)
242{
243 PyDict_ClearFreeList();
Christian Heimes77c02eb2008-02-09 02:18:51 +0000244}
245
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200246#define DK_DEBUG_INCREF _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA
247#define DK_DEBUG_DECREF _Py_DEC_REFTOTAL _Py_REF_DEBUG_COMMA
248
249#define DK_INCREF(dk) (DK_DEBUG_INCREF ++(dk)->dk_refcnt)
250#define DK_DECREF(dk) if (DK_DEBUG_DECREF (--(dk)->dk_refcnt) == 0) free_keys_object(dk)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400251#define DK_SIZE(dk) ((dk)->dk_size)
252#define DK_MASK(dk) (((dk)->dk_size)-1)
253#define IS_POWER_OF_2(x) (((x) & (x-1)) == 0)
254
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200255/* USABLE_FRACTION is the maximum dictionary load.
256 * Currently set to (2n+1)/3. Increasing this ratio makes dictionaries more
257 * dense resulting in more collisions. Decreasing it improves sparseness
258 * at the expense of spreading entries over more cache lines and at the
259 * cost of total memory consumed.
260 *
261 * USABLE_FRACTION must obey the following:
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400262 * (0 < USABLE_FRACTION(n) < n) for all n >= 2
263 *
264 * USABLE_FRACTION should be very quick to calculate.
265 * Fractions around 5/8 to 2/3 seem to work well in practice.
266 */
267
268/* Use (2n+1)/3 rather than 2n+3 because: it makes no difference for
269 * combined tables (the two fractions round to the same number n < ),
270 * but 2*4/3 is 2 whereas (2*4+1)/3 is 3 which potentially saves quite
271 * a lot of space for small, split tables */
272#define USABLE_FRACTION(n) ((((n) << 1)+1)/3)
273
274/* Alternative fraction that is otherwise close enough to (2n+1)/3 to make
275 * little difference. 8 * 2/3 == 8 * 5/8 == 5. 16 * 2/3 == 16 * 5/8 == 10.
276 * 32 * 2/3 = 21, 32 * 5/8 = 20.
277 * Its advantage is that it is faster to compute on machines with slow division.
278 * #define USABLE_FRACTION(n) (((n) >> 1) + ((n) >> 2) - ((n) >> 3))
279*/
280
Victor Stinnera9f61a52013-07-16 22:17:26 +0200281/* GROWTH_RATE. Growth rate upon hitting maximum load.
282 * Currently set to used*2 + capacity/2.
283 * This means that dicts double in size when growing without deletions,
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700284 * but have more head room when the number of deletions is on a par with the
285 * number of insertions.
286 * Raising this to used*4 doubles memory consumption depending on the size of
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200287 * the dictionary, but results in half the number of resizes, less effort to
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700288 * resize.
289 * GROWTH_RATE was set to used*4 up to version 3.2.
290 * GROWTH_RATE was set to used*2 in version 3.3.0
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200291 */
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700292#define GROWTH_RATE(d) (((d)->ma_used*2)+((d)->ma_keys->dk_size>>1))
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400293
294#define ENSURE_ALLOWS_DELETIONS(d) \
295 if ((d)->ma_keys->dk_lookup == lookdict_unicode_nodummy) { \
296 (d)->ma_keys->dk_lookup = lookdict_unicode; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400298
299/* This immutable, empty PyDictKeysObject is used for PyDict_Clear()
300 * (which cannot fail and thus can do no allocation).
301 */
302static PyDictKeysObject empty_keys_struct = {
303 2, /* dk_refcnt 1 for this struct, 1 for dummy_struct */
304 1, /* dk_size */
305 lookdict_split, /* dk_lookup */
306 0, /* dk_usable (immutable) */
307 {
308 { 0, 0, 0 } /* dk_entries (empty) */
309 }
310};
311
312static PyObject *empty_values[1] = { NULL };
313
314#define Py_EMPTY_KEYS &empty_keys_struct
315
316static PyDictKeysObject *new_keys_object(Py_ssize_t size)
317{
318 PyDictKeysObject *dk;
319 Py_ssize_t i;
320 PyDictKeyEntry *ep0;
321
322 assert(size >= PyDict_MINSIZE_SPLIT);
323 assert(IS_POWER_OF_2(size));
324 dk = PyMem_MALLOC(sizeof(PyDictKeysObject) +
325 sizeof(PyDictKeyEntry) * (size-1));
326 if (dk == NULL) {
327 PyErr_NoMemory();
328 return NULL;
329 }
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200330 DK_DEBUG_INCREF dk->dk_refcnt = 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400331 dk->dk_size = size;
332 dk->dk_usable = USABLE_FRACTION(size);
333 ep0 = &dk->dk_entries[0];
334 /* Hash value of slot 0 is used by popitem, so it must be initialized */
335 ep0->me_hash = 0;
336 for (i = 0; i < size; i++) {
337 ep0[i].me_key = NULL;
338 ep0[i].me_value = NULL;
339 }
340 dk->dk_lookup = lookdict_unicode_nodummy;
341 return dk;
342}
343
344static void
345free_keys_object(PyDictKeysObject *keys)
346{
347 PyDictKeyEntry *entries = &keys->dk_entries[0];
348 Py_ssize_t i, n;
349 for (i = 0, n = DK_SIZE(keys); i < n; i++) {
350 Py_XDECREF(entries[i].me_key);
351 Py_XDECREF(entries[i].me_value);
352 }
353 PyMem_FREE(keys);
354}
355
356#define new_values(size) PyMem_NEW(PyObject *, size)
357
358#define free_values(values) PyMem_FREE(values)
359
360/* Consumes a reference to the keys object */
361static PyObject *
362new_dict(PyDictKeysObject *keys, PyObject **values)
363{
364 PyDictObject *mp;
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200365 assert(keys != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 if (numfree) {
367 mp = free_list[--numfree];
368 assert (mp != NULL);
369 assert (Py_TYPE(mp) == &PyDict_Type);
370 _Py_NewReference((PyObject *)mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400372 else {
373 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
374 if (mp == NULL) {
375 DK_DECREF(keys);
376 free_values(values);
377 return NULL;
378 }
379 }
380 mp->ma_keys = keys;
381 mp->ma_values = values;
382 mp->ma_used = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000384}
385
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400386/* Consumes a reference to the keys object */
387static PyObject *
388new_dict_with_shared_keys(PyDictKeysObject *keys)
389{
390 PyObject **values;
391 Py_ssize_t i, size;
392
393 size = DK_SIZE(keys);
394 values = new_values(size);
395 if (values == NULL) {
396 DK_DECREF(keys);
397 return PyErr_NoMemory();
398 }
399 for (i = 0; i < size; i++) {
400 values[i] = NULL;
401 }
402 return new_dict(keys, values);
403}
404
405PyObject *
406PyDict_New(void)
407{
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200408 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE_COMBINED);
409 if (keys == NULL)
410 return NULL;
411 return new_dict(keys, NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400412}
413
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000414/*
415The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000416This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000417Open addressing is preferred over chaining since the link overhead for
418chaining would be substantial (100% with typical malloc overhead).
419
Tim Peterseb28ef22001-06-02 05:27:19 +0000420The initial probe index is computed as hash mod the table size. Subsequent
421probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000422
423All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000424
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000425The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000426contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000427Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000428
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000429lookdict() is general-purpose, and may return NULL if (and only if) a
430comparison raises an exception (this was new in Python 2.5).
Guido van Rossum89d8c602007-09-18 17:26:56 +0000431lookdict_unicode() below is specialized to string keys, comparison of which can
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400432never raise an exception; that function can never return NULL.
433lookdict_unicode_nodummy is further specialized for string keys that cannot be
434the <dummy> value.
435For both, when the key isn't found a PyDictEntry* is returned
436where the key would have been found, *value_addr points to the matching value
437slot.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000438*/
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400439static PyDictKeyEntry *
440lookdict(PyDictObject *mp, PyObject *key,
441 Py_hash_t hash, PyObject ***value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000442{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200443 size_t i;
444 size_t perturb;
445 PyDictKeyEntry *freeslot;
446 size_t mask;
Antoine Pitrou9a234902012-05-13 20:48:01 +0200447 PyDictKeyEntry *ep0;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200448 PyDictKeyEntry *ep;
449 int cmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000451
Antoine Pitrou9a234902012-05-13 20:48:01 +0200452top:
453 mask = DK_MASK(mp->ma_keys);
454 ep0 = &mp->ma_keys->dk_entries[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 i = (size_t)hash & mask;
456 ep = &ep0[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400457 if (ep->me_key == NULL || ep->me_key == key) {
458 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400460 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 if (ep->me_key == dummy)
462 freeslot = ep;
463 else {
464 if (ep->me_hash == hash) {
465 startkey = ep->me_key;
466 Py_INCREF(startkey);
467 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
468 Py_DECREF(startkey);
469 if (cmp < 0)
470 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400471 if (ep0 == mp->ma_keys->dk_entries && ep->me_key == startkey) {
472 if (cmp > 0) {
473 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400475 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 }
477 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200478 /* The dict was mutated, restart */
479 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 }
481 }
482 freeslot = NULL;
483 }
Tim Peters15d49292001-05-27 07:39:22 +0000484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 /* In the loop, me_key == dummy is by far (factor of 100s) the
486 least likely outcome, so test for that last. */
487 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
488 i = (i << 2) + i + perturb + 1;
489 ep = &ep0[i & mask];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400490 if (ep->me_key == NULL) {
491 if (freeslot == NULL) {
492 *value_addr = &ep->me_value;
493 return ep;
494 } else {
495 *value_addr = &freeslot->me_value;
496 return freeslot;
497 }
498 }
499 if (ep->me_key == key) {
500 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400502 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 if (ep->me_hash == hash && ep->me_key != dummy) {
504 startkey = ep->me_key;
505 Py_INCREF(startkey);
506 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
507 Py_DECREF(startkey);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400508 if (cmp < 0) {
509 *value_addr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400511 }
512 if (ep0 == mp->ma_keys->dk_entries && ep->me_key == startkey) {
513 if (cmp > 0) {
514 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400516 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 }
518 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200519 /* The dict was mutated, restart */
520 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 }
522 }
523 else if (ep->me_key == dummy && freeslot == NULL)
524 freeslot = ep;
525 }
526 assert(0); /* NOT REACHED */
527 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000528}
529
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400530/* Specialized version for string-only keys */
531static PyDictKeyEntry *
532lookdict_unicode(PyDictObject *mp, PyObject *key,
533 Py_hash_t hash, PyObject ***value_addr)
Fred Drake1bff34a2000-08-31 19:31:38 +0000534{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200535 size_t i;
536 size_t perturb;
537 PyDictKeyEntry *freeslot;
538 size_t mask = DK_MASK(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400539 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200540 PyDictKeyEntry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 /* Make sure this function doesn't have to handle non-unicode keys,
543 including subclasses of str; e.g., one reason to subclass
544 unicodes is to override __eq__, and for speed we don't cater to
545 that here. */
546 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400547 mp->ma_keys->dk_lookup = lookdict;
548 return lookdict(mp, key, hash, value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 }
Mark Dickinson57e683e2011-09-24 18:18:40 +0100550 i = (size_t)hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 ep = &ep0[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400552 if (ep->me_key == NULL || ep->me_key == key) {
553 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400555 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 if (ep->me_key == dummy)
557 freeslot = ep;
558 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400559 if (ep->me_hash == hash && unicode_eq(ep->me_key, key)) {
560 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400562 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 freeslot = NULL;
564 }
Tim Peters15d49292001-05-27 07:39:22 +0000565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 /* In the loop, me_key == dummy is by far (factor of 100s) the
567 least likely outcome, so test for that last. */
568 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
569 i = (i << 2) + i + perturb + 1;
570 ep = &ep0[i & mask];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400571 if (ep->me_key == NULL) {
572 if (freeslot == NULL) {
573 *value_addr = &ep->me_value;
574 return ep;
575 } else {
576 *value_addr = &freeslot->me_value;
577 return freeslot;
578 }
579 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 if (ep->me_key == key
581 || (ep->me_hash == hash
582 && ep->me_key != dummy
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400583 && unicode_eq(ep->me_key, key))) {
584 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400586 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 if (ep->me_key == dummy && freeslot == NULL)
588 freeslot = ep;
589 }
590 assert(0); /* NOT REACHED */
591 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000592}
593
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400594/* Faster version of lookdict_unicode when it is known that no <dummy> keys
595 * will be present. */
596static PyDictKeyEntry *
597lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
598 Py_hash_t hash, PyObject ***value_addr)
599{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200600 size_t i;
601 size_t perturb;
602 size_t mask = DK_MASK(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400603 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200604 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400605
606 /* Make sure this function doesn't have to handle non-unicode keys,
607 including subclasses of str; e.g., one reason to subclass
608 unicodes is to override __eq__, and for speed we don't cater to
609 that here. */
610 if (!PyUnicode_CheckExact(key)) {
611 mp->ma_keys->dk_lookup = lookdict;
612 return lookdict(mp, key, hash, value_addr);
613 }
614 i = (size_t)hash & mask;
615 ep = &ep0[i];
616 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
617 if (ep->me_key == NULL || ep->me_key == key ||
618 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
619 *value_addr = &ep->me_value;
620 return ep;
621 }
622 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
623 i = (i << 2) + i + perturb + 1;
624 ep = &ep0[i & mask];
625 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
626 if (ep->me_key == NULL || ep->me_key == key ||
627 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
628 *value_addr = &ep->me_value;
629 return ep;
630 }
631 }
632 assert(0); /* NOT REACHED */
633 return 0;
634}
635
636/* Version of lookdict for split tables.
637 * All split tables and only split tables use this lookup function.
638 * Split tables only contain unicode keys and no dummy keys,
639 * so algorithm is the same as lookdict_unicode_nodummy.
640 */
641static PyDictKeyEntry *
642lookdict_split(PyDictObject *mp, PyObject *key,
643 Py_hash_t hash, PyObject ***value_addr)
644{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200645 size_t i;
646 size_t perturb;
647 size_t mask = DK_MASK(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400648 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200649 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400650
651 if (!PyUnicode_CheckExact(key)) {
Benjamin Petersondb780d02012-04-23 13:44:32 -0400652 ep = lookdict(mp, key, hash, value_addr);
653 /* lookdict expects a combined-table, so fix value_addr */
654 i = ep - ep0;
655 *value_addr = &mp->ma_values[i];
656 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400657 }
658 i = (size_t)hash & mask;
659 ep = &ep0[i];
660 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
661 if (ep->me_key == NULL || ep->me_key == key ||
662 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
663 *value_addr = &mp->ma_values[i];
664 return ep;
665 }
666 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
667 i = (i << 2) + i + perturb + 1;
668 ep = &ep0[i & mask];
669 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
670 if (ep->me_key == NULL || ep->me_key == key ||
671 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
672 *value_addr = &mp->ma_values[i & mask];
673 return ep;
674 }
675 }
676 assert(0); /* NOT REACHED */
677 return 0;
678}
679
Benjamin Petersonfb886362010-04-24 18:21:17 +0000680int
681_PyDict_HasOnlyStringKeys(PyObject *dict)
682{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 Py_ssize_t pos = 0;
684 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000685 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400687 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 return 1;
689 while (PyDict_Next(dict, &pos, &key, &value))
690 if (!PyUnicode_Check(key))
691 return 0;
692 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000693}
694
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000695#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 do { \
697 if (!_PyObject_GC_IS_TRACKED(mp)) { \
698 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
699 _PyObject_GC_MAY_BE_TRACKED(value)) { \
700 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 } \
702 } \
703 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000704
705void
706_PyDict_MaybeUntrack(PyObject *op)
707{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 PyDictObject *mp;
709 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400710 Py_ssize_t i, size;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
713 return;
714
715 mp = (PyDictObject *) op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400716 size = DK_SIZE(mp->ma_keys);
717 if (_PyDict_HasSplitTable(mp)) {
718 for (i = 0; i < size; i++) {
719 if ((value = mp->ma_values[i]) == NULL)
720 continue;
721 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
722 assert(!_PyObject_GC_MAY_BE_TRACKED(
723 mp->ma_keys->dk_entries[i].me_key));
724 return;
725 }
726 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400728 else {
729 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
730 for (i = 0; i < size; i++) {
731 if ((value = ep0[i].me_value) == NULL)
732 continue;
733 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
734 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
735 return;
736 }
737 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000739}
740
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400741/* Internal function to find slot for an item from its hash
742 * when it is known that the key is not present in the dict.
743 */
744static PyDictKeyEntry *
745find_empty_slot(PyDictObject *mp, PyObject *key, Py_hash_t hash,
746 PyObject ***value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000747{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400748 size_t i;
749 size_t perturb;
750 size_t mask = DK_MASK(mp->ma_keys);
751 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
752 PyDictKeyEntry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000753
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400754 assert(key != NULL);
755 if (!PyUnicode_CheckExact(key))
756 mp->ma_keys->dk_lookup = lookdict;
757 i = hash & mask;
758 ep = &ep0[i];
759 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
760 i = (i << 2) + i + perturb + 1;
761 ep = &ep0[i & mask];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400763 assert(ep->me_value == NULL);
764 if (mp->ma_values)
765 *value_addr = &mp->ma_values[i & mask];
766 else
767 *value_addr = &ep->me_value;
768 return ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000769}
770
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400771static int
772insertion_resize(PyDictObject *mp)
773{
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700774 return dictresize(mp, GROWTH_RATE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400775}
Antoine Pitroue965d972012-02-27 00:45:12 +0100776
777/*
778Internal routine to insert a new item into the table.
779Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +0100780Returns -1 if an error occurred, or 0 on success.
781*/
782static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400783insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +0100784{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400785 PyObject *old_value;
786 PyObject **value_addr;
787 PyDictKeyEntry *ep;
788 assert(key != dummy);
Antoine Pitroue965d972012-02-27 00:45:12 +0100789
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400790 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
791 if (insertion_resize(mp) < 0)
792 return -1;
793 }
794
795 ep = mp->ma_keys->dk_lookup(mp, key, hash, &value_addr);
Antoine Pitroue965d972012-02-27 00:45:12 +0100796 if (ep == NULL) {
Antoine Pitroue965d972012-02-27 00:45:12 +0100797 return -1;
798 }
Antoine Pitroud6967322014-10-18 00:35:00 +0200799 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
Benjamin Petersona6f195e2012-04-30 10:23:40 -0400800 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400801 MAINTAIN_TRACKING(mp, key, value);
802 old_value = *value_addr;
803 if (old_value != NULL) {
804 assert(ep->me_key != NULL && ep->me_key != dummy);
805 *value_addr = value;
Antoine Pitroud6967322014-10-18 00:35:00 +0200806 Py_DECREF(old_value); /* which **CAN** re-enter (see issue #22653) */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400807 }
808 else {
809 if (ep->me_key == NULL) {
Benjamin Petersona6f195e2012-04-30 10:23:40 -0400810 Py_INCREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400811 if (mp->ma_keys->dk_usable <= 0) {
812 /* Need to resize. */
813 if (insertion_resize(mp) < 0) {
814 Py_DECREF(key);
815 Py_DECREF(value);
816 return -1;
817 }
818 ep = find_empty_slot(mp, key, hash, &value_addr);
819 }
820 mp->ma_keys->dk_usable--;
821 assert(mp->ma_keys->dk_usable >= 0);
822 ep->me_key = key;
823 ep->me_hash = hash;
824 }
825 else {
826 if (ep->me_key == dummy) {
Benjamin Petersona6f195e2012-04-30 10:23:40 -0400827 Py_INCREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400828 ep->me_key = key;
829 ep->me_hash = hash;
830 Py_DECREF(dummy);
831 } else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400832 assert(_PyDict_HasSplitTable(mp));
833 }
834 }
835 mp->ma_used++;
836 *value_addr = value;
Antoine Pitroud6967322014-10-18 00:35:00 +0200837 assert(ep->me_key != NULL && ep->me_key != dummy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400838 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400839 return 0;
Antoine Pitroue965d972012-02-27 00:45:12 +0100840}
841
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000842/*
843Internal routine used by dictresize() to insert an item which is
844known to be absent from the dict. This routine also assumes that
845the dict contains no deleted entries. Besides the performance benefit,
846using insertdict() in dictresize() is dangerous (SF bug #1456209).
847Note that no refcounts are changed by this routine; if needed, the caller
848is responsible for incref'ing `key` and `value`.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400849Neither mp->ma_used nor k->dk_usable are modified by this routine; the caller
850must set them correctly
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000851*/
852static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400853insertdict_clean(PyDictObject *mp, PyObject *key, Py_hash_t hash,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 PyObject *value)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000855{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400856 size_t i;
857 size_t perturb;
858 PyDictKeysObject *k = mp->ma_keys;
859 size_t mask = (size_t)DK_SIZE(k)-1;
860 PyDictKeyEntry *ep0 = &k->dk_entries[0];
861 PyDictKeyEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000862
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400863 assert(k->dk_lookup != NULL);
864 assert(value != NULL);
865 assert(key != NULL);
866 assert(key != dummy);
867 assert(PyUnicode_CheckExact(key) || k->dk_lookup == lookdict);
868 i = hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 ep = &ep0[i];
870 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
871 i = (i << 2) + i + perturb + 1;
872 ep = &ep0[i & mask];
873 }
874 assert(ep->me_value == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 ep->me_key = key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000876 ep->me_hash = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000878}
879
880/*
881Restructure the table by allocating a new table and reinserting all
882items again. When entries have been deleted, the new table may
883actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400884If a table is split (its keys and hashes are shared, its values are not),
885then the values are temporarily copied into the table, it is resized as
886a combined table, then the me_value slots in the old table are NULLed out.
887After resizing a table is always combined,
888but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000889*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000890static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000891dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000892{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 Py_ssize_t newsize;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400894 PyDictKeysObject *oldkeys;
895 PyObject **oldvalues;
896 Py_ssize_t i, oldsize;
Tim Peters91a364d2001-05-19 07:04:38 +0000897
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400898/* Find the smallest table size > minused. */
899 for (newsize = PyDict_MINSIZE_COMBINED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 newsize <= minused && newsize > 0;
901 newsize <<= 1)
902 ;
903 if (newsize <= 0) {
904 PyErr_NoMemory();
905 return -1;
906 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400907 oldkeys = mp->ma_keys;
908 oldvalues = mp->ma_values;
909 /* Allocate a new table. */
910 mp->ma_keys = new_keys_object(newsize);
911 if (mp->ma_keys == NULL) {
912 mp->ma_keys = oldkeys;
913 return -1;
914 }
915 if (oldkeys->dk_lookup == lookdict)
916 mp->ma_keys->dk_lookup = lookdict;
917 oldsize = DK_SIZE(oldkeys);
918 mp->ma_values = NULL;
919 /* If empty then nothing to copy so just return */
920 if (oldsize == 1) {
921 assert(oldkeys == Py_EMPTY_KEYS);
922 DK_DECREF(oldkeys);
923 return 0;
924 }
925 /* Main loop below assumes we can transfer refcount to new keys
926 * and that value is stored in me_value.
927 * Increment ref-counts and copy values here to compensate
928 * This (resizing a split table) should be relatively rare */
929 if (oldvalues != NULL) {
930 for (i = 0; i < oldsize; i++) {
931 if (oldvalues[i] != NULL) {
932 Py_INCREF(oldkeys->dk_entries[i].me_key);
933 oldkeys->dk_entries[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 }
936 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400937 /* Main loop */
938 for (i = 0; i < oldsize; i++) {
939 PyDictKeyEntry *ep = &oldkeys->dk_entries[i];
940 if (ep->me_value != NULL) {
941 assert(ep->me_key != dummy);
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000942 insertdict_clean(mp, ep->me_key, ep->me_hash, ep->me_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400945 mp->ma_keys->dk_usable -= mp->ma_used;
946 if (oldvalues != NULL) {
947 /* NULL out me_value slot in oldkeys, in case it was shared */
948 for (i = 0; i < oldsize; i++)
949 oldkeys->dk_entries[i].me_value = NULL;
950 assert(oldvalues != empty_values);
951 free_values(oldvalues);
952 DK_DECREF(oldkeys);
953 }
954 else {
955 assert(oldkeys->dk_lookup != lookdict_split);
956 if (oldkeys->dk_lookup != lookdict_unicode_nodummy) {
957 PyDictKeyEntry *ep0 = &oldkeys->dk_entries[0];
958 for (i = 0; i < oldsize; i++) {
959 if (ep0[i].me_key == dummy)
960 Py_DECREF(dummy);
961 }
962 }
963 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200964 DK_DEBUG_DECREF PyMem_FREE(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400965 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000967}
968
Benjamin Peterson15ee8212012-04-24 14:44:18 -0400969/* Returns NULL if unable to split table.
970 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400971static PyDictKeysObject *
972make_keys_shared(PyObject *op)
973{
974 Py_ssize_t i;
975 Py_ssize_t size;
976 PyDictObject *mp = (PyDictObject *)op;
977
Benjamin Peterson15ee8212012-04-24 14:44:18 -0400978 if (!PyDict_CheckExact(op))
979 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400980 if (!_PyDict_HasSplitTable(mp)) {
981 PyDictKeyEntry *ep0;
982 PyObject **values;
983 assert(mp->ma_keys->dk_refcnt == 1);
984 if (mp->ma_keys->dk_lookup == lookdict) {
985 return NULL;
986 }
987 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
988 /* Remove dummy keys */
989 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
990 return NULL;
991 }
992 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
993 /* Copy values into a new array */
994 ep0 = &mp->ma_keys->dk_entries[0];
995 size = DK_SIZE(mp->ma_keys);
996 values = new_values(size);
997 if (values == NULL) {
998 PyErr_SetString(PyExc_MemoryError,
999 "Not enough memory to allocate new values array");
1000 return NULL;
1001 }
1002 for (i = 0; i < size; i++) {
1003 values[i] = ep0[i].me_value;
1004 ep0[i].me_value = NULL;
1005 }
1006 mp->ma_keys->dk_lookup = lookdict_split;
1007 mp->ma_values = values;
1008 }
1009 DK_INCREF(mp->ma_keys);
1010 return mp->ma_keys;
1011}
Christian Heimes99170a52007-12-19 02:07:34 +00001012
1013PyObject *
1014_PyDict_NewPresized(Py_ssize_t minused)
1015{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001016 Py_ssize_t newsize;
1017 PyDictKeysObject *new_keys;
1018 for (newsize = PyDict_MINSIZE_COMBINED;
1019 newsize <= minused && newsize > 0;
1020 newsize <<= 1)
1021 ;
1022 new_keys = new_keys_object(newsize);
1023 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001025 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001026}
1027
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001028/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1029 * that may occur (originally dicts supported only string keys, and exceptions
1030 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001031 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001032 * (suppressed) occurred while computing the key's hash, or that some error
1033 * (suppressed) occurred when comparing keys in the dict's internal probe
1034 * sequence. A nasty example of the latter is when a Python-coded comparison
1035 * function hits a stack-depth error, which can cause this to return NULL
1036 * even if the key is present.
1037 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001038PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001039PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001040{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001041 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001043 PyDictKeyEntry *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 PyThreadState *tstate;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001045 PyObject **value_addr;
1046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 if (!PyDict_Check(op))
1048 return NULL;
1049 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001050 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 {
1052 hash = PyObject_Hash(key);
1053 if (hash == -1) {
1054 PyErr_Clear();
1055 return NULL;
1056 }
1057 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 /* We can arrive here with a NULL tstate during initialization: try
1060 running "python -Wi" for an example related to string interning.
1061 Let's just hope that no exception occurs then... This must be
1062 _PyThreadState_Current and not PyThreadState_GET() because in debug
1063 mode, the latter complains if tstate is NULL. */
Victor Stinnerbfd316e2016-01-20 11:12:38 +01001064 tstate = _PyThreadState_UncheckedGet();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 if (tstate != NULL && tstate->curexc_type != NULL) {
1066 /* preserve the existing exception */
1067 PyObject *err_type, *err_value, *err_tb;
1068 PyErr_Fetch(&err_type, &err_value, &err_tb);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001069 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 /* ignore errors */
1071 PyErr_Restore(err_type, err_value, err_tb);
1072 if (ep == NULL)
1073 return NULL;
1074 }
1075 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001076 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 if (ep == NULL) {
1078 PyErr_Clear();
1079 return NULL;
1080 }
1081 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001082 return *value_addr;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001083}
1084
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001085PyObject *
1086_PyDict_GetItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1087{
1088 PyDictObject *mp = (PyDictObject *)op;
1089 PyDictKeyEntry *ep;
1090 PyThreadState *tstate;
1091 PyObject **value_addr;
1092
1093 if (!PyDict_Check(op))
1094 return NULL;
1095
1096 /* We can arrive here with a NULL tstate during initialization: try
1097 running "python -Wi" for an example related to string interning.
1098 Let's just hope that no exception occurs then... This must be
1099 _PyThreadState_Current and not PyThreadState_GET() because in debug
1100 mode, the latter complains if tstate is NULL. */
Victor Stinnerbfd316e2016-01-20 11:12:38 +01001101 tstate = _PyThreadState_UncheckedGet();
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001102 if (tstate != NULL && tstate->curexc_type != NULL) {
1103 /* preserve the existing exception */
1104 PyObject *err_type, *err_value, *err_tb;
1105 PyErr_Fetch(&err_type, &err_value, &err_tb);
1106 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
1107 /* ignore errors */
1108 PyErr_Restore(err_type, err_value, err_tb);
1109 if (ep == NULL)
1110 return NULL;
1111 }
1112 else {
1113 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
1114 if (ep == NULL) {
1115 PyErr_Clear();
1116 return NULL;
1117 }
1118 }
1119 return *value_addr;
1120}
1121
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001122/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1123 This returns NULL *with* an exception set if an exception occurred.
1124 It returns NULL *without* an exception set if the key wasn't present.
1125*/
1126PyObject *
1127PyDict_GetItemWithError(PyObject *op, PyObject *key)
1128{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001129 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 PyDictObject*mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001131 PyDictKeyEntry *ep;
1132 PyObject **value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001133
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 if (!PyDict_Check(op)) {
1135 PyErr_BadInternalCall();
1136 return NULL;
1137 }
1138 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001139 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 {
1141 hash = PyObject_Hash(key);
1142 if (hash == -1) {
1143 return NULL;
1144 }
1145 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001146
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001147 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 if (ep == NULL)
1149 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001150 return *value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001151}
1152
Brett Cannonfd074152012-04-14 14:10:13 -04001153PyObject *
1154_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1155{
1156 PyObject *kv;
1157 kv = _PyUnicode_FromId(key); /* borrowed */
1158 if (kv == NULL)
1159 return NULL;
1160 return PyDict_GetItemWithError(dp, kv);
1161}
1162
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001163/* Fast version of global value lookup.
1164 * Lookup in globals, then builtins.
1165 */
1166PyObject *
1167_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001168{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001169 PyObject *x;
1170 if (PyUnicode_CheckExact(key)) {
1171 PyObject **value_addr;
1172 Py_hash_t hash = ((PyASCIIObject *)key)->hash;
1173 if (hash != -1) {
1174 PyDictKeyEntry *e;
1175 e = globals->ma_keys->dk_lookup(globals, key, hash, &value_addr);
1176 if (e == NULL) {
1177 return NULL;
1178 }
1179 x = *value_addr;
1180 if (x != NULL)
1181 return x;
1182 e = builtins->ma_keys->dk_lookup(builtins, key, hash, &value_addr);
1183 if (e == NULL) {
1184 return NULL;
1185 }
1186 x = *value_addr;
1187 return x;
1188 }
Antoine Pitroue965d972012-02-27 00:45:12 +01001189 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001190 x = PyDict_GetItemWithError((PyObject *)globals, key);
1191 if (x != NULL)
1192 return x;
1193 if (PyErr_Occurred())
1194 return NULL;
1195 return PyDict_GetItemWithError((PyObject *)builtins, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001196}
1197
Antoine Pitroue965d972012-02-27 00:45:12 +01001198/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1199 * dictionary if it's merely replacing the value for an existing key.
1200 * This means that it's safe to loop over a dictionary with PyDict_Next()
1201 * and occasionally replace a value -- but you can't insert new keys or
1202 * remove them.
1203 */
1204int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001205PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001206{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001207 PyDictObject *mp;
1208 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001209 if (!PyDict_Check(op)) {
1210 PyErr_BadInternalCall();
1211 return -1;
1212 }
1213 assert(key);
1214 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001215 mp = (PyDictObject *)op;
1216 if (!PyUnicode_CheckExact(key) ||
1217 (hash = ((PyASCIIObject *) key)->hash) == -1)
1218 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001219 hash = PyObject_Hash(key);
1220 if (hash == -1)
1221 return -1;
1222 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001223
1224 /* insertdict() handles any resizing that might be necessary */
1225 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001226}
1227
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001228int
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001229_PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
1230 Py_hash_t hash)
1231{
1232 PyDictObject *mp;
1233
1234 if (!PyDict_Check(op)) {
1235 PyErr_BadInternalCall();
1236 return -1;
1237 }
1238 assert(key);
1239 assert(value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001240 assert(hash != -1);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001241 mp = (PyDictObject *)op;
1242
1243 /* insertdict() handles any resizing that might be necessary */
1244 return insertdict(mp, key, hash, value);
1245}
1246
1247int
Tim Peters1f5871e2000-07-04 17:44:48 +00001248PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001249{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001250 PyDictObject *mp;
1251 Py_hash_t hash;
1252 PyDictKeyEntry *ep;
1253 PyObject *old_key, *old_value;
1254 PyObject **value_addr;
Guido van Rossumd7047b31995-01-02 19:07:15 +00001255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 if (!PyDict_Check(op)) {
1257 PyErr_BadInternalCall();
1258 return -1;
1259 }
1260 assert(key);
1261 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001262 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 hash = PyObject_Hash(key);
1264 if (hash == -1)
1265 return -1;
1266 }
1267 mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001268 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 if (ep == NULL)
1270 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001271 if (*value_addr == NULL) {
Raymond Hettinger69492da2013-09-02 15:59:26 -07001272 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 return -1;
1274 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001275 old_value = *value_addr;
1276 *value_addr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001278 if (!_PyDict_HasSplitTable(mp)) {
1279 ENSURE_ALLOWS_DELETIONS(mp);
1280 old_key = ep->me_key;
1281 Py_INCREF(dummy);
1282 ep->me_key = dummy;
1283 Py_DECREF(old_key);
1284 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 Py_DECREF(old_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001287}
1288
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001289int
1290_PyDict_DelItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1291{
1292 PyDictObject *mp;
1293 PyDictKeyEntry *ep;
1294 PyObject *old_key, *old_value;
1295 PyObject **value_addr;
1296
1297 if (!PyDict_Check(op)) {
1298 PyErr_BadInternalCall();
1299 return -1;
1300 }
1301 assert(key);
1302 assert(hash != -1);
1303 mp = (PyDictObject *)op;
1304 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
1305 if (ep == NULL)
1306 return -1;
1307 if (*value_addr == NULL) {
1308 _PyErr_SetKeyError(key);
1309 return -1;
1310 }
1311 old_value = *value_addr;
1312 *value_addr = NULL;
1313 mp->ma_used--;
1314 if (!_PyDict_HasSplitTable(mp)) {
1315 ENSURE_ALLOWS_DELETIONS(mp);
1316 old_key = ep->me_key;
1317 Py_INCREF(dummy);
1318 ep->me_key = dummy;
1319 Py_DECREF(old_key);
1320 }
1321 Py_DECREF(old_value);
1322 return 0;
1323}
1324
Guido van Rossum25831651993-05-19 14:50:45 +00001325void
Tim Peters1f5871e2000-07-04 17:44:48 +00001326PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001327{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001329 PyDictKeysObject *oldkeys;
1330 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 if (!PyDict_Check(op))
1334 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001335 mp = ((PyDictObject *)op);
1336 oldkeys = mp->ma_keys;
1337 oldvalues = mp->ma_values;
1338 if (oldvalues == empty_values)
1339 return;
1340 /* Empty the dict... */
1341 DK_INCREF(Py_EMPTY_KEYS);
1342 mp->ma_keys = Py_EMPTY_KEYS;
1343 mp->ma_values = empty_values;
1344 mp->ma_used = 0;
1345 /* ...then clear the keys and values */
1346 if (oldvalues != NULL) {
1347 n = DK_SIZE(oldkeys);
1348 for (i = 0; i < n; i++)
1349 Py_CLEAR(oldvalues[i]);
1350 free_values(oldvalues);
1351 DK_DECREF(oldkeys);
1352 }
1353 else {
1354 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001355 DK_DECREF(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001356 }
1357}
1358
1359/* Returns -1 if no more items (or op is not a dict),
1360 * index of item otherwise. Stores value in pvalue
1361 */
1362Py_LOCAL_INLINE(Py_ssize_t)
1363dict_next(PyObject *op, Py_ssize_t i, PyObject **pvalue)
1364{
1365 Py_ssize_t mask, offset;
1366 PyDictObject *mp;
1367 PyObject **value_ptr;
1368
1369
1370 if (!PyDict_Check(op))
1371 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001373 if (i < 0)
1374 return -1;
1375 if (mp->ma_values) {
1376 value_ptr = &mp->ma_values[i];
1377 offset = sizeof(PyObject *);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001379 else {
1380 value_ptr = &mp->ma_keys->dk_entries[i].me_value;
1381 offset = sizeof(PyDictKeyEntry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001383 mask = DK_MASK(mp->ma_keys);
1384 while (i <= mask && *value_ptr == NULL) {
1385 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1386 i++;
1387 }
1388 if (i > mask)
1389 return -1;
1390 if (pvalue)
1391 *pvalue = *value_ptr;
1392 return i;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001393}
1394
Tim Peters080c88b2003-02-15 03:01:11 +00001395/*
1396 * Iterate over a dict. Use like so:
1397 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001398 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001399 * PyObject *key, *value;
1400 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001401 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +00001402 * Refer to borrowed references in key and value.
1403 * }
1404 *
1405 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001406 * mutates the dict. One exception: it is safe if the loop merely changes
1407 * the values associated with the keys (but doesn't insert new keys or
1408 * delete keys), via PyDict_SetItem().
1409 */
Guido van Rossum25831651993-05-19 14:50:45 +00001410int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001411PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001412{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001413 PyDictObject *mp;
1414 Py_ssize_t i = dict_next(op, *ppos, pvalue);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 if (i < 0)
1416 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001417 mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 *ppos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 if (pkey)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001420 *pkey = mp->ma_keys->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001422}
1423
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001424/* Internal version of PyDict_Next that returns a hash value in addition
1425 * to the key and value.
1426 */
Thomas Wouterscf297e42007-02-23 15:07:44 +00001427int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001428_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1429 PyObject **pvalue, Py_hash_t *phash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00001430{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001431 PyDictObject *mp;
1432 Py_ssize_t i = dict_next(op, *ppos, pvalue);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 if (i < 0)
1434 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001435 mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 *ppos = i+1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001437 *phash = mp->ma_keys->dk_entries[i].me_hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 if (pkey)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001439 *pkey = mp->ma_keys->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 return 1;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001441}
1442
Eric Snow96c6af92015-05-29 22:21:39 -06001443/* Internal version of dict.pop(). */
1444PyObject *
1445_PyDict_Pop(PyDictObject *mp, PyObject *key, PyObject *deflt)
1446{
1447 Py_hash_t hash;
1448 PyObject *old_value, *old_key;
1449 PyDictKeyEntry *ep;
1450 PyObject **value_addr;
1451
1452 if (mp->ma_used == 0) {
1453 if (deflt) {
1454 Py_INCREF(deflt);
1455 return deflt;
1456 }
1457 _PyErr_SetKeyError(key);
1458 return NULL;
1459 }
1460 if (!PyUnicode_CheckExact(key) ||
1461 (hash = ((PyASCIIObject *) key)->hash) == -1) {
1462 hash = PyObject_Hash(key);
1463 if (hash == -1)
1464 return NULL;
1465 }
1466 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
1467 if (ep == NULL)
1468 return NULL;
1469 old_value = *value_addr;
1470 if (old_value == NULL) {
1471 if (deflt) {
1472 Py_INCREF(deflt);
1473 return deflt;
1474 }
1475 _PyErr_SetKeyError(key);
1476 return NULL;
1477 }
1478 *value_addr = NULL;
1479 mp->ma_used--;
1480 if (!_PyDict_HasSplitTable(mp)) {
1481 ENSURE_ALLOWS_DELETIONS(mp);
1482 old_key = ep->me_key;
1483 Py_INCREF(dummy);
1484 ep->me_key = dummy;
1485 Py_DECREF(old_key);
1486 }
1487 return old_value;
1488}
1489
1490/* Internal version of dict.from_keys(). It is subclass-friendly. */
1491PyObject *
1492_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
1493{
1494 PyObject *it; /* iter(iterable) */
1495 PyObject *key;
1496 PyObject *d;
1497 int status;
1498
1499 d = PyObject_CallObject(cls, NULL);
1500 if (d == NULL)
1501 return NULL;
1502
1503 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
1504 if (PyDict_CheckExact(iterable)) {
1505 PyDictObject *mp = (PyDictObject *)d;
1506 PyObject *oldvalue;
1507 Py_ssize_t pos = 0;
1508 PyObject *key;
1509 Py_hash_t hash;
1510
1511 if (dictresize(mp, Py_SIZE(iterable))) {
1512 Py_DECREF(d);
1513 return NULL;
1514 }
1515
1516 while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
1517 if (insertdict(mp, key, hash, value)) {
1518 Py_DECREF(d);
1519 return NULL;
1520 }
1521 }
1522 return d;
1523 }
1524 if (PyAnySet_CheckExact(iterable)) {
1525 PyDictObject *mp = (PyDictObject *)d;
1526 Py_ssize_t pos = 0;
1527 PyObject *key;
1528 Py_hash_t hash;
1529
1530 if (dictresize(mp, PySet_GET_SIZE(iterable))) {
1531 Py_DECREF(d);
1532 return NULL;
1533 }
1534
1535 while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
1536 if (insertdict(mp, key, hash, value)) {
1537 Py_DECREF(d);
1538 return NULL;
1539 }
1540 }
1541 return d;
1542 }
1543 }
1544
1545 it = PyObject_GetIter(iterable);
1546 if (it == NULL){
1547 Py_DECREF(d);
1548 return NULL;
1549 }
1550
1551 if (PyDict_CheckExact(d)) {
1552 while ((key = PyIter_Next(it)) != NULL) {
1553 status = PyDict_SetItem(d, key, value);
1554 Py_DECREF(key);
1555 if (status < 0)
1556 goto Fail;
1557 }
1558 } else {
1559 while ((key = PyIter_Next(it)) != NULL) {
1560 status = PyObject_SetItem(d, key, value);
1561 Py_DECREF(key);
1562 if (status < 0)
1563 goto Fail;
1564 }
1565 }
1566
1567 if (PyErr_Occurred())
1568 goto Fail;
1569 Py_DECREF(it);
1570 return d;
1571
1572Fail:
1573 Py_DECREF(it);
1574 Py_DECREF(d);
1575 return NULL;
1576}
1577
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001578/* Methods */
1579
1580static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001581dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001582{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001583 PyObject **values = mp->ma_values;
1584 PyDictKeysObject *keys = mp->ma_keys;
1585 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586 PyObject_GC_UnTrack(mp);
1587 Py_TRASHCAN_SAFE_BEGIN(mp)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001588 if (values != NULL) {
1589 if (values != empty_values) {
1590 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
1591 Py_XDECREF(values[i]);
1592 }
1593 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001595 DK_DECREF(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02001597 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001598 assert(keys->dk_refcnt == 1);
1599 DK_DECREF(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001600 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1602 free_list[numfree++] = mp;
1603 else
1604 Py_TYPE(mp)->tp_free((PyObject *)mp);
1605 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001606}
1607
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001608
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001609static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001610dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001611{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001613 PyObject *key = NULL, *value = NULL;
1614 _PyUnicodeWriter writer;
1615 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00001616
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 i = Py_ReprEnter((PyObject *)mp);
1618 if (i != 0) {
1619 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
1620 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001623 Py_ReprLeave((PyObject *)mp);
1624 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 }
Tim Petersa7259592001-06-16 05:11:17 +00001626
Victor Stinnerf91929b2013-11-19 13:07:38 +01001627 _PyUnicodeWriter_Init(&writer);
1628 writer.overallocate = 1;
1629 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
1630 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00001631
Victor Stinnerf91929b2013-11-19 13:07:38 +01001632 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
1633 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001635 /* Do repr() on each key+value pair, and insert ": " between them.
1636 Note that repr may mutate the dict. */
1637 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001638 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001640 PyObject *s;
1641 int res;
1642
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001643 /* Prevent repr from deleting key or value during key format. */
1644 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02001646
Victor Stinnerf91929b2013-11-19 13:07:38 +01001647 if (!first) {
1648 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
1649 goto error;
1650 }
1651 first = 0;
1652
1653 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01001655 goto error;
1656 res = _PyUnicodeWriter_WriteStr(&writer, s);
1657 Py_DECREF(s);
1658 if (res < 0)
1659 goto error;
1660
1661 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
1662 goto error;
1663
1664 s = PyObject_Repr(value);
1665 if (s == NULL)
1666 goto error;
1667 res = _PyUnicodeWriter_WriteStr(&writer, s);
1668 Py_DECREF(s);
1669 if (res < 0)
1670 goto error;
1671
1672 Py_CLEAR(key);
1673 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 }
Tim Petersa7259592001-06-16 05:11:17 +00001675
Victor Stinnerf91929b2013-11-19 13:07:38 +01001676 writer.overallocate = 0;
1677 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
1678 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01001681
1682 return _PyUnicodeWriter_Finish(&writer);
1683
1684error:
1685 Py_ReprLeave((PyObject *)mp);
1686 _PyUnicodeWriter_Dealloc(&writer);
1687 Py_XDECREF(key);
1688 Py_XDECREF(value);
1689 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001690}
1691
Martin v. Löwis18e16552006-02-15 17:27:45 +00001692static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001693dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001694{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001696}
1697
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001698static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001699dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001700{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 PyObject *v;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001702 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001703 PyDictKeyEntry *ep;
1704 PyObject **value_addr;
1705
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001706 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001707 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708 hash = PyObject_Hash(key);
1709 if (hash == -1)
1710 return NULL;
1711 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001712 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001713 if (ep == NULL)
1714 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001715 v = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001716 if (v == NULL) {
1717 if (!PyDict_CheckExact(mp)) {
1718 /* Look up __missing__ method if we're a subclass. */
1719 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05001720 _Py_IDENTIFIER(__missing__);
1721 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722 if (missing != NULL) {
1723 res = PyObject_CallFunctionObjArgs(missing,
1724 key, NULL);
1725 Py_DECREF(missing);
1726 return res;
1727 }
1728 else if (PyErr_Occurred())
1729 return NULL;
1730 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07001731 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 return NULL;
1733 }
1734 else
1735 Py_INCREF(v);
1736 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001737}
1738
1739static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001740dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001741{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 if (w == NULL)
1743 return PyDict_DelItem((PyObject *)mp, v);
1744 else
1745 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001746}
1747
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001748static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 (lenfunc)dict_length, /*mp_length*/
1750 (binaryfunc)dict_subscript, /*mp_subscript*/
1751 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001752};
1753
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001754static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001755dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001756{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001757 PyObject *v;
1758 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001759 PyDictKeyEntry *ep;
1760 Py_ssize_t size, n, offset;
1761 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001762
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001763 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001764 n = mp->ma_used;
1765 v = PyList_New(n);
1766 if (v == NULL)
1767 return NULL;
1768 if (n != mp->ma_used) {
1769 /* Durnit. The allocations caused the dict to resize.
1770 * Just start over, this shouldn't normally happen.
1771 */
1772 Py_DECREF(v);
1773 goto again;
1774 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001775 ep = &mp->ma_keys->dk_entries[0];
1776 size = DK_SIZE(mp->ma_keys);
1777 if (mp->ma_values) {
1778 value_ptr = mp->ma_values;
1779 offset = sizeof(PyObject *);
1780 }
1781 else {
1782 value_ptr = &ep[0].me_value;
1783 offset = sizeof(PyDictKeyEntry);
1784 }
1785 for (i = 0, j = 0; i < size; i++) {
1786 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 PyObject *key = ep[i].me_key;
1788 Py_INCREF(key);
1789 PyList_SET_ITEM(v, j, key);
1790 j++;
1791 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001792 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001793 }
1794 assert(j == n);
1795 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001796}
1797
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001798static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001799dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001800{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001801 PyObject *v;
1802 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001803 Py_ssize_t size, n, offset;
1804 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001805
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001806 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 n = mp->ma_used;
1808 v = PyList_New(n);
1809 if (v == NULL)
1810 return NULL;
1811 if (n != mp->ma_used) {
1812 /* Durnit. The allocations caused the dict to resize.
1813 * Just start over, this shouldn't normally happen.
1814 */
1815 Py_DECREF(v);
1816 goto again;
1817 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001818 size = DK_SIZE(mp->ma_keys);
1819 if (mp->ma_values) {
1820 value_ptr = mp->ma_values;
1821 offset = sizeof(PyObject *);
1822 }
1823 else {
1824 value_ptr = &mp->ma_keys->dk_entries[0].me_value;
1825 offset = sizeof(PyDictKeyEntry);
1826 }
1827 for (i = 0, j = 0; i < size; i++) {
1828 PyObject *value = *value_ptr;
1829 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1830 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001831 Py_INCREF(value);
1832 PyList_SET_ITEM(v, j, value);
1833 j++;
1834 }
1835 }
1836 assert(j == n);
1837 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001838}
1839
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001840static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001841dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001842{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001843 PyObject *v;
1844 Py_ssize_t i, j, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001845 Py_ssize_t size, offset;
1846 PyObject *item, *key;
1847 PyDictKeyEntry *ep;
1848 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001850 /* Preallocate the list of tuples, to avoid allocations during
1851 * the loop over the items, which could trigger GC, which
1852 * could resize the dict. :-(
1853 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001854 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855 n = mp->ma_used;
1856 v = PyList_New(n);
1857 if (v == NULL)
1858 return NULL;
1859 for (i = 0; i < n; i++) {
1860 item = PyTuple_New(2);
1861 if (item == NULL) {
1862 Py_DECREF(v);
1863 return NULL;
1864 }
1865 PyList_SET_ITEM(v, i, item);
1866 }
1867 if (n != mp->ma_used) {
1868 /* Durnit. The allocations caused the dict to resize.
1869 * Just start over, this shouldn't normally happen.
1870 */
1871 Py_DECREF(v);
1872 goto again;
1873 }
1874 /* Nothing we do below makes any function calls. */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001875 ep = mp->ma_keys->dk_entries;
1876 size = DK_SIZE(mp->ma_keys);
1877 if (mp->ma_values) {
1878 value_ptr = mp->ma_values;
1879 offset = sizeof(PyObject *);
1880 }
1881 else {
1882 value_ptr = &ep[0].me_value;
1883 offset = sizeof(PyDictKeyEntry);
1884 }
1885 for (i = 0, j = 0; i < size; i++) {
1886 PyObject *value = *value_ptr;
1887 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1888 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889 key = ep[i].me_key;
1890 item = PyList_GET_ITEM(v, j);
1891 Py_INCREF(key);
1892 PyTuple_SET_ITEM(item, 0, key);
1893 Py_INCREF(value);
1894 PyTuple_SET_ITEM(item, 1, value);
1895 j++;
1896 }
1897 }
1898 assert(j == n);
1899 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001900}
1901
Larry Hastings5c661892014-01-24 06:17:25 -08001902/*[clinic input]
1903@classmethod
1904dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08001905 iterable: object
1906 value: object=None
1907 /
1908
1909Returns a new dict with keys from iterable and values equal to value.
1910[clinic start generated code]*/
1911
Larry Hastings5c661892014-01-24 06:17:25 -08001912static PyObject *
1913dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001914/*[clinic end generated code: output=8fb98e4b10384999 input=b85a667f9bf4669d]*/
Larry Hastings5c661892014-01-24 06:17:25 -08001915{
Eric Snow96c6af92015-05-29 22:21:39 -06001916 return _PyDict_FromKeys((PyObject *)type, iterable, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001917}
1918
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001919static int
1920dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001921{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 PyObject *arg = NULL;
1923 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001925 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1926 result = -1;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001928 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001929 _Py_IDENTIFIER(keys);
1930 if (_PyObject_HasAttrId(arg, &PyId_keys))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 result = PyDict_Merge(self, arg, 1);
1932 else
1933 result = PyDict_MergeFromSeq2(self, arg, 1);
1934 }
1935 if (result == 0 && kwds != NULL) {
1936 if (PyArg_ValidateKeywordArguments(kwds))
1937 result = PyDict_Merge(self, kwds, 1);
1938 else
1939 result = -1;
1940 }
1941 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001942}
1943
1944static PyObject *
1945dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1946{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001947 if (dict_update_common(self, args, kwds, "update") != -1)
1948 Py_RETURN_NONE;
1949 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001950}
1951
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001952/* Update unconditionally replaces existing items.
1953 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001954 otherwise it leaves existing items unchanged.
1955
1956 PyDict_{Update,Merge} update/merge from a mapping object.
1957
Tim Petersf582b822001-12-11 18:51:08 +00001958 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001959 producing iterable objects of length 2.
1960*/
1961
Tim Petersf582b822001-12-11 18:51:08 +00001962int
Tim Peters1fc240e2001-10-26 05:06:50 +00001963PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1964{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001965 PyObject *it; /* iter(seq2) */
1966 Py_ssize_t i; /* index into seq2 of current element */
1967 PyObject *item; /* seq2[i] */
1968 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00001969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001970 assert(d != NULL);
1971 assert(PyDict_Check(d));
1972 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00001973
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974 it = PyObject_GetIter(seq2);
1975 if (it == NULL)
1976 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001977
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001978 for (i = 0; ; ++i) {
1979 PyObject *key, *value;
1980 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001982 fast = NULL;
1983 item = PyIter_Next(it);
1984 if (item == NULL) {
1985 if (PyErr_Occurred())
1986 goto Fail;
1987 break;
1988 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001990 /* Convert item to sequence, and verify length 2. */
1991 fast = PySequence_Fast(item, "");
1992 if (fast == NULL) {
1993 if (PyErr_ExceptionMatches(PyExc_TypeError))
1994 PyErr_Format(PyExc_TypeError,
1995 "cannot convert dictionary update "
1996 "sequence element #%zd to a sequence",
1997 i);
1998 goto Fail;
1999 }
2000 n = PySequence_Fast_GET_SIZE(fast);
2001 if (n != 2) {
2002 PyErr_Format(PyExc_ValueError,
2003 "dictionary update sequence element #%zd "
2004 "has length %zd; 2 is required",
2005 i, n);
2006 goto Fail;
2007 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002009 /* Update/merge with this (key, value) pair. */
2010 key = PySequence_Fast_GET_ITEM(fast, 0);
2011 value = PySequence_Fast_GET_ITEM(fast, 1);
2012 if (override || PyDict_GetItem(d, key) == NULL) {
2013 int status = PyDict_SetItem(d, key, value);
2014 if (status < 0)
2015 goto Fail;
2016 }
2017 Py_DECREF(fast);
2018 Py_DECREF(item);
2019 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002020
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002021 i = 0;
2022 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00002023Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002024 Py_XDECREF(item);
2025 Py_XDECREF(fast);
2026 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002027Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002028 Py_DECREF(it);
2029 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00002030}
2031
Tim Peters6d6c1a32001-08-02 04:15:00 +00002032int
2033PyDict_Update(PyObject *a, PyObject *b)
2034{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002035 return PyDict_Merge(a, b, 1);
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002036}
2037
2038int
2039PyDict_Merge(PyObject *a, PyObject *b, int override)
2040{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002041 PyDictObject *mp, *other;
2042 Py_ssize_t i, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002043 PyDictKeyEntry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002045 /* We accept for the argument either a concrete dictionary object,
2046 * or an abstract "mapping" object. For the former, we can do
2047 * things quite efficiently. For the latter, we only require that
2048 * PyMapping_Keys() and PyObject_GetItem() be supported.
2049 */
2050 if (a == NULL || !PyDict_Check(a) || b == NULL) {
2051 PyErr_BadInternalCall();
2052 return -1;
2053 }
2054 mp = (PyDictObject*)a;
2055 if (PyDict_Check(b)) {
2056 other = (PyDictObject*)b;
2057 if (other == mp || other->ma_used == 0)
2058 /* a.update(a) or a.update({}); nothing to do */
2059 return 0;
2060 if (mp->ma_used == 0)
2061 /* Since the target dict is empty, PyDict_GetItem()
2062 * always returns NULL. Setting override to 1
2063 * skips the unnecessary test.
2064 */
2065 override = 1;
2066 /* Do one big resize at the start, rather than
2067 * incrementally resizing as we insert new items. Expect
2068 * that there will be no (or few) overlapping keys.
2069 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002070 if (mp->ma_keys->dk_usable * 3 < other->ma_used * 2)
2071 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002072 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002073 for (i = 0, n = DK_SIZE(other->ma_keys); i < n; i++) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002074 PyObject *key, *value;
2075 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002076 entry = &other->ma_keys->dk_entries[i];
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002077 key = entry->me_key;
2078 hash = entry->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002079 if (other->ma_values)
2080 value = other->ma_values[i];
2081 else
2082 value = entry->me_value;
2083
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002084 if (value != NULL) {
2085 int err = 0;
2086 Py_INCREF(key);
2087 Py_INCREF(value);
2088 if (override || PyDict_GetItem(a, key) == NULL)
2089 err = insertdict(mp, key, hash, value);
2090 Py_DECREF(value);
2091 Py_DECREF(key);
2092 if (err != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002093 return -1;
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002094
2095 if (n != DK_SIZE(other->ma_keys)) {
2096 PyErr_SetString(PyExc_RuntimeError,
2097 "dict mutated during update");
2098 return -1;
2099 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002100 }
2101 }
2102 }
2103 else {
2104 /* Do it the generic, slower way */
2105 PyObject *keys = PyMapping_Keys(b);
2106 PyObject *iter;
2107 PyObject *key, *value;
2108 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002110 if (keys == NULL)
2111 /* Docstring says this is equivalent to E.keys() so
2112 * if E doesn't have a .keys() method we want
2113 * AttributeError to percolate up. Might as well
2114 * do the same for any other error.
2115 */
2116 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002118 iter = PyObject_GetIter(keys);
2119 Py_DECREF(keys);
2120 if (iter == NULL)
2121 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002123 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
2124 if (!override && PyDict_GetItem(a, key) != NULL) {
2125 Py_DECREF(key);
2126 continue;
2127 }
2128 value = PyObject_GetItem(b, key);
2129 if (value == NULL) {
2130 Py_DECREF(iter);
2131 Py_DECREF(key);
2132 return -1;
2133 }
2134 status = PyDict_SetItem(a, key, value);
2135 Py_DECREF(key);
2136 Py_DECREF(value);
2137 if (status < 0) {
2138 Py_DECREF(iter);
2139 return -1;
2140 }
2141 }
2142 Py_DECREF(iter);
2143 if (PyErr_Occurred())
2144 /* Iterator completed, via error */
2145 return -1;
2146 }
2147 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002148}
2149
2150static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002151dict_copy(PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002152{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002153 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002154}
2155
2156PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002157PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002158{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002159 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002160 PyDictObject *mp;
2161 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002162
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002163 if (o == NULL || !PyDict_Check(o)) {
2164 PyErr_BadInternalCall();
2165 return NULL;
2166 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002167 mp = (PyDictObject *)o;
2168 if (_PyDict_HasSplitTable(mp)) {
2169 PyDictObject *split_copy;
2170 PyObject **newvalues = new_values(DK_SIZE(mp->ma_keys));
2171 if (newvalues == NULL)
2172 return PyErr_NoMemory();
2173 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2174 if (split_copy == NULL) {
2175 free_values(newvalues);
2176 return NULL;
2177 }
2178 split_copy->ma_values = newvalues;
2179 split_copy->ma_keys = mp->ma_keys;
2180 split_copy->ma_used = mp->ma_used;
2181 DK_INCREF(mp->ma_keys);
2182 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2183 PyObject *value = mp->ma_values[i];
2184 Py_XINCREF(value);
2185 split_copy->ma_values[i] = value;
2186 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002187 if (_PyObject_GC_IS_TRACKED(mp))
2188 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002189 return (PyObject *)split_copy;
2190 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002191 copy = PyDict_New();
2192 if (copy == NULL)
2193 return NULL;
2194 if (PyDict_Merge(copy, o, 1) == 0)
2195 return copy;
2196 Py_DECREF(copy);
2197 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002198}
2199
Martin v. Löwis18e16552006-02-15 17:27:45 +00002200Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002201PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002202{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002203 if (mp == NULL || !PyDict_Check(mp)) {
2204 PyErr_BadInternalCall();
2205 return -1;
2206 }
2207 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002208}
2209
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002210PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002211PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002212{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002213 if (mp == NULL || !PyDict_Check(mp)) {
2214 PyErr_BadInternalCall();
2215 return NULL;
2216 }
2217 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002218}
2219
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002220PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002221PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002222{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002223 if (mp == NULL || !PyDict_Check(mp)) {
2224 PyErr_BadInternalCall();
2225 return NULL;
2226 }
2227 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002228}
2229
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002230PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002231PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002232{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002233 if (mp == NULL || !PyDict_Check(mp)) {
2234 PyErr_BadInternalCall();
2235 return NULL;
2236 }
2237 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002238}
2239
Tim Peterse63415e2001-05-08 04:38:29 +00002240/* Return 1 if dicts equal, 0 if not, -1 if error.
2241 * Gets out as soon as any difference is detected.
2242 * Uses only Py_EQ comparison.
2243 */
2244static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002245dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002246{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002247 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002249 if (a->ma_used != b->ma_used)
2250 /* can't be equal if # of entries differ */
2251 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002252 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002253 for (i = 0; i < DK_SIZE(a->ma_keys); i++) {
2254 PyDictKeyEntry *ep = &a->ma_keys->dk_entries[i];
2255 PyObject *aval;
2256 if (a->ma_values)
2257 aval = a->ma_values[i];
2258 else
2259 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002260 if (aval != NULL) {
2261 int cmp;
2262 PyObject *bval;
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002263 PyObject **vaddr;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002264 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002265 /* temporarily bump aval's refcount to ensure it stays
2266 alive until we're done with it */
2267 Py_INCREF(aval);
2268 /* ditto for key */
2269 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002270 /* reuse the known hash value */
2271 if ((b->ma_keys->dk_lookup)(b, key, ep->me_hash, &vaddr) == NULL)
2272 bval = NULL;
2273 else
2274 bval = *vaddr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002275 Py_DECREF(key);
2276 if (bval == NULL) {
2277 Py_DECREF(aval);
2278 if (PyErr_Occurred())
2279 return -1;
2280 return 0;
2281 }
2282 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
2283 Py_DECREF(aval);
2284 if (cmp <= 0) /* error or not equal */
2285 return cmp;
2286 }
2287 }
2288 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002289}
Tim Peterse63415e2001-05-08 04:38:29 +00002290
2291static PyObject *
2292dict_richcompare(PyObject *v, PyObject *w, int op)
2293{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 int cmp;
2295 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002297 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2298 res = Py_NotImplemented;
2299 }
2300 else if (op == Py_EQ || op == Py_NE) {
2301 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2302 if (cmp < 0)
2303 return NULL;
2304 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2305 }
2306 else
2307 res = Py_NotImplemented;
2308 Py_INCREF(res);
2309 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002310}
Tim Peterse63415e2001-05-08 04:38:29 +00002311
Larry Hastings61272b72014-01-07 12:41:53 -08002312/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002313
2314@coexist
2315dict.__contains__
2316
2317 key: object
2318 /
2319
Meador Ingee02de8c2014-01-14 16:48:31 -06002320True if D has a key k, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002321[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002322
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002323static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002324dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002325/*[clinic end generated code: output=a3d03db709ed6e6b input=b852b2a19b51ab24]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002326{
Larry Hastingsc2047262014-01-25 20:43:29 -08002327 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002328 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002329 PyDictKeyEntry *ep;
2330 PyObject **value_addr;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002332 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002333 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002334 hash = PyObject_Hash(key);
2335 if (hash == -1)
2336 return NULL;
2337 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002338 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002339 if (ep == NULL)
2340 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002341 return PyBool_FromLong(*value_addr != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002342}
2343
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002344static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002345dict_get(PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00002346{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002347 PyObject *key;
2348 PyObject *failobj = Py_None;
2349 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002350 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002351 PyDictKeyEntry *ep;
2352 PyObject **value_addr;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
2355 return NULL;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002358 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002359 hash = PyObject_Hash(key);
2360 if (hash == -1)
2361 return NULL;
2362 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002363 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 if (ep == NULL)
2365 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002366 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002367 if (val == NULL)
2368 val = failobj;
2369 Py_INCREF(val);
2370 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002371}
2372
Benjamin Peterson00e98862013-03-07 22:16:29 -05002373PyObject *
2374PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002375{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002376 PyDictObject *mp = (PyDictObject *)d;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002377 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002378 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002379 PyDictKeyEntry *ep;
2380 PyObject **value_addr;
Guido van Rossum164452c2000-08-08 16:12:54 +00002381
Benjamin Peterson00e98862013-03-07 22:16:29 -05002382 if (!PyDict_Check(d)) {
2383 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002384 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002385 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002387 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388 hash = PyObject_Hash(key);
2389 if (hash == -1)
2390 return NULL;
2391 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002392 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393 if (ep == NULL)
2394 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002395 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002396 if (val == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002397 if (mp->ma_keys->dk_usable <= 0) {
2398 /* Need to resize. */
2399 if (insertion_resize(mp) < 0)
2400 return NULL;
2401 ep = find_empty_slot(mp, key, hash, &value_addr);
2402 }
Benjamin Peterson00e98862013-03-07 22:16:29 -05002403 Py_INCREF(defaultobj);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002404 Py_INCREF(key);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002405 MAINTAIN_TRACKING(mp, key, defaultobj);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002406 ep->me_key = key;
2407 ep->me_hash = hash;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002408 *value_addr = defaultobj;
2409 val = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002410 mp->ma_keys->dk_usable--;
2411 mp->ma_used++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002412 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002413 return val;
Guido van Rossum164452c2000-08-08 16:12:54 +00002414}
2415
Benjamin Peterson00e98862013-03-07 22:16:29 -05002416static PyObject *
2417dict_setdefault(PyDictObject *mp, PyObject *args)
2418{
2419 PyObject *key, *val;
2420 PyObject *defaultobj = Py_None;
2421
2422 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &defaultobj))
2423 return NULL;
2424
Benjamin Peterson55898502013-03-08 08:36:49 -05002425 val = PyDict_SetDefault((PyObject *)mp, key, defaultobj);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002426 Py_XINCREF(val);
2427 return val;
2428}
Guido van Rossum164452c2000-08-08 16:12:54 +00002429
2430static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002431dict_clear(PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002432{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002433 PyDict_Clear((PyObject *)mp);
2434 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002435}
2436
Guido van Rossumba6ab842000-12-12 22:02:18 +00002437static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002438dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002439{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002440 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00002441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002442 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2443 return NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06002444
2445 return _PyDict_Pop(mp, key, deflt);
Guido van Rossume027d982002-04-12 15:11:59 +00002446}
2447
2448static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002449dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00002450{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002451 Py_hash_t i = 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002452 PyDictKeyEntry *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002453 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002454
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002456 /* Allocate the result tuple before checking the size. Believe it
2457 * or not, this allocation could trigger a garbage collection which
2458 * could empty the dict, so if we checked the size first and that
2459 * happened, the result would be an infinite loop (searching for an
2460 * entry that no longer exists). Note that the usual popitem()
2461 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2462 * tuple away if the dict *is* empty isn't a significant
2463 * inefficiency -- possible, but unlikely in practice.
2464 */
2465 res = PyTuple_New(2);
2466 if (res == NULL)
2467 return NULL;
2468 if (mp->ma_used == 0) {
2469 Py_DECREF(res);
2470 PyErr_SetString(PyExc_KeyError,
2471 "popitem(): dictionary is empty");
2472 return NULL;
2473 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002474 /* Convert split table to combined table */
2475 if (mp->ma_keys->dk_lookup == lookdict_split) {
2476 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
2477 Py_DECREF(res);
2478 return NULL;
2479 }
2480 }
2481 ENSURE_ALLOWS_DELETIONS(mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 /* Set ep to "the first" dict entry with a value. We abuse the hash
2483 * field of slot 0 to hold a search finger:
2484 * If slot 0 has a value, use slot 0.
2485 * Else slot 0 is being used to hold a search finger,
2486 * and we use its hash value as the first index to look.
2487 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002488 ep = &mp->ma_keys->dk_entries[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002489 if (ep->me_value == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002490 Py_ssize_t mask = DK_MASK(mp->ma_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002491 i = ep->me_hash;
2492 /* The hash field may be a real hash value, or it may be a
2493 * legit search finger, or it may be a once-legit search
2494 * finger that's out of bounds now because it wrapped around
2495 * or the table shrunk -- simply make sure it's in bounds now.
2496 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002497 if (i > mask || i < 1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002498 i = 1; /* skip slot 0 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002499 while ((ep = &mp->ma_keys->dk_entries[i])->me_value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002500 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002501 if (i > mask)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002502 i = 1;
2503 }
2504 }
2505 PyTuple_SET_ITEM(res, 0, ep->me_key);
2506 PyTuple_SET_ITEM(res, 1, ep->me_value);
2507 Py_INCREF(dummy);
2508 ep->me_key = dummy;
2509 ep->me_value = NULL;
2510 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002511 assert(mp->ma_keys->dk_entries[0].me_value == NULL);
2512 mp->ma_keys->dk_entries[0].me_hash = i + 1; /* next place to start */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002513 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002514}
2515
Jeremy Hylton8caad492000-06-23 14:18:11 +00002516static int
2517dict_traverse(PyObject *op, visitproc visit, void *arg)
2518{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002519 Py_ssize_t i, n;
2520 PyDictObject *mp = (PyDictObject *)op;
2521 if (mp->ma_keys->dk_lookup == lookdict) {
2522 for (i = 0; i < DK_SIZE(mp->ma_keys); i++) {
2523 if (mp->ma_keys->dk_entries[i].me_value != NULL) {
2524 Py_VISIT(mp->ma_keys->dk_entries[i].me_value);
2525 Py_VISIT(mp->ma_keys->dk_entries[i].me_key);
2526 }
2527 }
2528 } else {
2529 if (mp->ma_values != NULL) {
2530 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2531 Py_VISIT(mp->ma_values[i]);
2532 }
2533 }
2534 else {
2535 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2536 Py_VISIT(mp->ma_keys->dk_entries[i].me_value);
2537 }
2538 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002539 }
2540 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002541}
2542
2543static int
2544dict_tp_clear(PyObject *op)
2545{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002546 PyDict_Clear(op);
2547 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002548}
2549
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002550static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002551
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002552Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06002553_PyDict_SizeOf(PyDictObject *mp)
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002554{
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002555 Py_ssize_t size, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002556
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002557 size = DK_SIZE(mp->ma_keys);
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002558 res = _PyObject_SIZE(Py_TYPE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002559 if (mp->ma_values)
2560 res += size * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002561 /* If the dictionary is split, the keys portion is accounted-for
2562 in the type object. */
2563 if (mp->ma_keys->dk_refcnt == 1)
2564 res += sizeof(PyDictKeysObject) + (size-1) * sizeof(PyDictKeyEntry);
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002565 return res;
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002566}
2567
2568Py_ssize_t
2569_PyDict_KeysSize(PyDictKeysObject *keys)
2570{
2571 return sizeof(PyDictKeysObject) + (DK_SIZE(keys)-1) * sizeof(PyDictKeyEntry);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002572}
2573
doko@ubuntu.com17210f52016-01-14 14:04:59 +01002574static PyObject *
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002575dict_sizeof(PyDictObject *mp)
2576{
2577 return PyLong_FromSsize_t(_PyDict_SizeOf(mp));
2578}
2579
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002580PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
2581
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002582PyDoc_STRVAR(sizeof__doc__,
2583"D.__sizeof__() -> size of D in memory, in bytes");
2584
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002585PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002586"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002587
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002588PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002589"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 +00002590
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002591PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002592"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002593If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00002594
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002595PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00002596"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000025972-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002598
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002599PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04002600"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
2601If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
2602If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
2603In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00002604
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002605PyDoc_STRVAR(clear__doc__,
2606"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002607
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002608PyDoc_STRVAR(copy__doc__,
2609"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00002610
Guido van Rossumb90c8482007-02-10 01:11:45 +00002611/* Forward */
2612static PyObject *dictkeys_new(PyObject *);
2613static PyObject *dictitems_new(PyObject *);
2614static PyObject *dictvalues_new(PyObject *);
2615
Guido van Rossum45c85d12007-07-27 16:31:40 +00002616PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002617 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002618PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002619 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002620PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002621 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00002622
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002623static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07002624 DICT___CONTAINS___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002625 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
2626 getitem__doc__},
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002627 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002628 sizeof__doc__},
2629 {"get", (PyCFunction)dict_get, METH_VARARGS,
2630 get__doc__},
2631 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
2632 setdefault_doc__},
2633 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
2634 pop__doc__},
2635 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
2636 popitem__doc__},
2637 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
2638 keys__doc__},
2639 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
2640 items__doc__},
2641 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
2642 values__doc__},
2643 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
2644 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08002645 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002646 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
2647 clear__doc__},
2648 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
2649 copy__doc__},
2650 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002651};
2652
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002653/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002654int
2655PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002656{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002657 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002658 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002659 PyDictKeyEntry *ep;
2660 PyObject **value_addr;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002662 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002663 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002664 hash = PyObject_Hash(key);
2665 if (hash == -1)
2666 return -1;
2667 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002668 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
2669 return (ep == NULL) ? -1 : (*value_addr != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002670}
2671
Thomas Wouterscf297e42007-02-23 15:07:44 +00002672/* Internal version of PyDict_Contains used when the hash value is already known */
2673int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002674_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00002675{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002676 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002677 PyDictKeyEntry *ep;
2678 PyObject **value_addr;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002679
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002680 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
2681 return (ep == NULL) ? -1 : (*value_addr != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00002682}
2683
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002684/* Hack to implement "key in dict" */
2685static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002686 0, /* sq_length */
2687 0, /* sq_concat */
2688 0, /* sq_repeat */
2689 0, /* sq_item */
2690 0, /* sq_slice */
2691 0, /* sq_ass_item */
2692 0, /* sq_ass_slice */
2693 PyDict_Contains, /* sq_contains */
2694 0, /* sq_inplace_concat */
2695 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002696};
2697
Guido van Rossum09e563a2001-05-01 12:10:21 +00002698static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002699dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2700{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002701 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02002702 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002704 assert(type != NULL && type->tp_alloc != NULL);
2705 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02002706 if (self == NULL)
2707 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02002708 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02002709
Victor Stinnera9f61a52013-07-16 22:17:26 +02002710 /* The object has been implicitly tracked by tp_alloc */
2711 if (type == &PyDict_Type)
2712 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02002713
2714 d->ma_used = 0;
2715 d->ma_keys = new_keys_object(PyDict_MINSIZE_COMBINED);
2716 if (d->ma_keys == NULL) {
2717 Py_DECREF(self);
2718 return NULL;
2719 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002720 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002721}
2722
Tim Peters25786c02001-09-02 08:22:48 +00002723static int
2724dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2725{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002726 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002727}
2728
Tim Peters6d6c1a32001-08-02 04:15:00 +00002729static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002730dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002731{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002732 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002733}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002734
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002735PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002736"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002737"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002738" (key, value) pairs\n"
2739"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002740" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002741" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002742" d[k] = v\n"
2743"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2744" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002745
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002746PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002747 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2748 "dict",
2749 sizeof(PyDictObject),
2750 0,
2751 (destructor)dict_dealloc, /* tp_dealloc */
2752 0, /* tp_print */
2753 0, /* tp_getattr */
2754 0, /* tp_setattr */
2755 0, /* tp_reserved */
2756 (reprfunc)dict_repr, /* tp_repr */
2757 0, /* tp_as_number */
2758 &dict_as_sequence, /* tp_as_sequence */
2759 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002760 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002761 0, /* tp_call */
2762 0, /* tp_str */
2763 PyObject_GenericGetAttr, /* tp_getattro */
2764 0, /* tp_setattro */
2765 0, /* tp_as_buffer */
2766 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2767 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
2768 dictionary_doc, /* tp_doc */
2769 dict_traverse, /* tp_traverse */
2770 dict_tp_clear, /* tp_clear */
2771 dict_richcompare, /* tp_richcompare */
2772 0, /* tp_weaklistoffset */
2773 (getiterfunc)dict_iter, /* tp_iter */
2774 0, /* tp_iternext */
2775 mapp_methods, /* tp_methods */
2776 0, /* tp_members */
2777 0, /* tp_getset */
2778 0, /* tp_base */
2779 0, /* tp_dict */
2780 0, /* tp_descr_get */
2781 0, /* tp_descr_set */
2782 0, /* tp_dictoffset */
2783 dict_init, /* tp_init */
2784 PyType_GenericAlloc, /* tp_alloc */
2785 dict_new, /* tp_new */
2786 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002787};
2788
Victor Stinner3c1e4812012-03-26 22:10:51 +02002789PyObject *
2790_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
2791{
2792 PyObject *kv;
2793 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02002794 if (kv == NULL) {
2795 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002796 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02002797 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02002798 return PyDict_GetItem(dp, kv);
2799}
2800
Guido van Rossum3cca2451997-05-16 14:23:33 +00002801/* For backward compatibility with old dictionary interface */
2802
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002803PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002804PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002805{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002806 PyObject *kv, *rv;
2807 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02002808 if (kv == NULL) {
2809 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002810 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02002811 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002812 rv = PyDict_GetItem(v, kv);
2813 Py_DECREF(kv);
2814 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002815}
2816
2817int
Victor Stinner3c1e4812012-03-26 22:10:51 +02002818_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
2819{
2820 PyObject *kv;
2821 kv = _PyUnicode_FromId(key); /* borrowed */
2822 if (kv == NULL)
2823 return -1;
2824 return PyDict_SetItem(v, kv, item);
2825}
2826
2827int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002828PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002829{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002830 PyObject *kv;
2831 int err;
2832 kv = PyUnicode_FromString(key);
2833 if (kv == NULL)
2834 return -1;
2835 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
2836 err = PyDict_SetItem(v, kv, item);
2837 Py_DECREF(kv);
2838 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002839}
2840
2841int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01002842_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
2843{
2844 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
2845 if (kv == NULL)
2846 return -1;
2847 return PyDict_DelItem(v, kv);
2848}
2849
2850int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002851PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002852{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002853 PyObject *kv;
2854 int err;
2855 kv = PyUnicode_FromString(key);
2856 if (kv == NULL)
2857 return -1;
2858 err = PyDict_DelItem(v, kv);
2859 Py_DECREF(kv);
2860 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002861}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002862
Raymond Hettinger019a1482004-03-18 02:41:19 +00002863/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002864
2865typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002866 PyObject_HEAD
2867 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
2868 Py_ssize_t di_used;
2869 Py_ssize_t di_pos;
2870 PyObject* di_result; /* reusable result tuple for iteritems */
2871 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002872} dictiterobject;
2873
2874static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002875dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002876{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002877 dictiterobject *di;
2878 di = PyObject_GC_New(dictiterobject, itertype);
2879 if (di == NULL)
2880 return NULL;
2881 Py_INCREF(dict);
2882 di->di_dict = dict;
2883 di->di_used = dict->ma_used;
2884 di->di_pos = 0;
2885 di->len = dict->ma_used;
2886 if (itertype == &PyDictIterItem_Type) {
2887 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2888 if (di->di_result == NULL) {
2889 Py_DECREF(di);
2890 return NULL;
2891 }
2892 }
2893 else
2894 di->di_result = NULL;
2895 _PyObject_GC_TRACK(di);
2896 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002897}
2898
2899static void
2900dictiter_dealloc(dictiterobject *di)
2901{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002902 Py_XDECREF(di->di_dict);
2903 Py_XDECREF(di->di_result);
2904 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002905}
2906
2907static int
2908dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
2909{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002910 Py_VISIT(di->di_dict);
2911 Py_VISIT(di->di_result);
2912 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002913}
2914
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002915static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002916dictiter_len(dictiterobject *di)
2917{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002918 Py_ssize_t len = 0;
2919 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
2920 len = di->len;
2921 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002922}
2923
Guido van Rossumb90c8482007-02-10 01:11:45 +00002924PyDoc_STRVAR(length_hint_doc,
2925 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002926
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002927static PyObject *
2928dictiter_reduce(dictiterobject *di);
2929
2930PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
2931
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002932static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002933 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
2934 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002935 {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS,
2936 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002937 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002938};
2939
Raymond Hettinger019a1482004-03-18 02:41:19 +00002940static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002941{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002942 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002943 Py_ssize_t i, mask, offset;
2944 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002945 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002946 PyObject **value_ptr;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002947
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002948 if (d == NULL)
2949 return NULL;
2950 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002952 if (di->di_used != d->ma_used) {
2953 PyErr_SetString(PyExc_RuntimeError,
2954 "dictionary changed size during iteration");
2955 di->di_used = -1; /* Make this state sticky */
2956 return NULL;
2957 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002958
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002959 i = di->di_pos;
2960 if (i < 0)
2961 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002962 k = d->ma_keys;
2963 if (d->ma_values) {
2964 value_ptr = &d->ma_values[i];
2965 offset = sizeof(PyObject *);
2966 }
2967 else {
2968 value_ptr = &k->dk_entries[i].me_value;
2969 offset = sizeof(PyDictKeyEntry);
2970 }
2971 mask = DK_SIZE(k)-1;
2972 while (i <= mask && *value_ptr == NULL) {
2973 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002974 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002975 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002976 di->di_pos = i+1;
2977 if (i > mask)
2978 goto fail;
2979 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002980 key = k->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002981 Py_INCREF(key);
2982 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002983
2984fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002985 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03002986 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002987 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002988}
2989
Raymond Hettinger019a1482004-03-18 02:41:19 +00002990PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002991 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2992 "dict_keyiterator", /* tp_name */
2993 sizeof(dictiterobject), /* tp_basicsize */
2994 0, /* tp_itemsize */
2995 /* methods */
2996 (destructor)dictiter_dealloc, /* tp_dealloc */
2997 0, /* tp_print */
2998 0, /* tp_getattr */
2999 0, /* tp_setattr */
3000 0, /* tp_reserved */
3001 0, /* tp_repr */
3002 0, /* tp_as_number */
3003 0, /* tp_as_sequence */
3004 0, /* tp_as_mapping */
3005 0, /* tp_hash */
3006 0, /* tp_call */
3007 0, /* tp_str */
3008 PyObject_GenericGetAttr, /* tp_getattro */
3009 0, /* tp_setattro */
3010 0, /* tp_as_buffer */
3011 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3012 0, /* tp_doc */
3013 (traverseproc)dictiter_traverse, /* tp_traverse */
3014 0, /* tp_clear */
3015 0, /* tp_richcompare */
3016 0, /* tp_weaklistoffset */
3017 PyObject_SelfIter, /* tp_iter */
3018 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
3019 dictiter_methods, /* tp_methods */
3020 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003021};
3022
3023static PyObject *dictiter_iternextvalue(dictiterobject *di)
3024{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003025 PyObject *value;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003026 Py_ssize_t i, mask, offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003027 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003028 PyObject **value_ptr;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003030 if (d == NULL)
3031 return NULL;
3032 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003033
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003034 if (di->di_used != d->ma_used) {
3035 PyErr_SetString(PyExc_RuntimeError,
3036 "dictionary changed size during iteration");
3037 di->di_used = -1; /* Make this state sticky */
3038 return NULL;
3039 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003041 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003042 mask = DK_SIZE(d->ma_keys)-1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003043 if (i < 0 || i > mask)
3044 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003045 if (d->ma_values) {
3046 value_ptr = &d->ma_values[i];
3047 offset = sizeof(PyObject *);
3048 }
3049 else {
3050 value_ptr = &d->ma_keys->dk_entries[i].me_value;
3051 offset = sizeof(PyDictKeyEntry);
3052 }
3053 while (i <= mask && *value_ptr == NULL) {
3054 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003055 i++;
3056 if (i > mask)
3057 goto fail;
3058 }
3059 di->di_pos = i+1;
3060 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003061 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003062 Py_INCREF(value);
3063 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003064
3065fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003066 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003067 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003068 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003069}
3070
3071PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003072 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3073 "dict_valueiterator", /* tp_name */
3074 sizeof(dictiterobject), /* tp_basicsize */
3075 0, /* tp_itemsize */
3076 /* methods */
3077 (destructor)dictiter_dealloc, /* tp_dealloc */
3078 0, /* tp_print */
3079 0, /* tp_getattr */
3080 0, /* tp_setattr */
3081 0, /* tp_reserved */
3082 0, /* tp_repr */
3083 0, /* tp_as_number */
3084 0, /* tp_as_sequence */
3085 0, /* tp_as_mapping */
3086 0, /* tp_hash */
3087 0, /* tp_call */
3088 0, /* tp_str */
3089 PyObject_GenericGetAttr, /* tp_getattro */
3090 0, /* tp_setattro */
3091 0, /* tp_as_buffer */
3092 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3093 0, /* tp_doc */
3094 (traverseproc)dictiter_traverse, /* tp_traverse */
3095 0, /* tp_clear */
3096 0, /* tp_richcompare */
3097 0, /* tp_weaklistoffset */
3098 PyObject_SelfIter, /* tp_iter */
3099 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3100 dictiter_methods, /* tp_methods */
3101 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003102};
3103
3104static PyObject *dictiter_iternextitem(dictiterobject *di)
3105{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003106 PyObject *key, *value, *result = di->di_result;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003107 Py_ssize_t i, mask, offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003108 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003109 PyObject **value_ptr;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003110
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003111 if (d == NULL)
3112 return NULL;
3113 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003115 if (di->di_used != d->ma_used) {
3116 PyErr_SetString(PyExc_RuntimeError,
3117 "dictionary changed size during iteration");
3118 di->di_used = -1; /* Make this state sticky */
3119 return NULL;
3120 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003122 i = di->di_pos;
3123 if (i < 0)
3124 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003125 mask = DK_SIZE(d->ma_keys)-1;
3126 if (d->ma_values) {
3127 value_ptr = &d->ma_values[i];
3128 offset = sizeof(PyObject *);
3129 }
3130 else {
3131 value_ptr = &d->ma_keys->dk_entries[i].me_value;
3132 offset = sizeof(PyDictKeyEntry);
3133 }
3134 while (i <= mask && *value_ptr == NULL) {
3135 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003136 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003137 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003138 di->di_pos = i+1;
3139 if (i > mask)
3140 goto fail;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003142 if (result->ob_refcnt == 1) {
3143 Py_INCREF(result);
3144 Py_DECREF(PyTuple_GET_ITEM(result, 0));
3145 Py_DECREF(PyTuple_GET_ITEM(result, 1));
3146 } else {
3147 result = PyTuple_New(2);
3148 if (result == NULL)
3149 return NULL;
3150 }
3151 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003152 key = d->ma_keys->dk_entries[i].me_key;
3153 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003154 Py_INCREF(key);
3155 Py_INCREF(value);
Eric Snow96c6af92015-05-29 22:21:39 -06003156 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3157 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003158 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003159
3160fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003161 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003162 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003163 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003164}
3165
3166PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003167 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3168 "dict_itemiterator", /* tp_name */
3169 sizeof(dictiterobject), /* tp_basicsize */
3170 0, /* tp_itemsize */
3171 /* methods */
3172 (destructor)dictiter_dealloc, /* tp_dealloc */
3173 0, /* tp_print */
3174 0, /* tp_getattr */
3175 0, /* tp_setattr */
3176 0, /* tp_reserved */
3177 0, /* tp_repr */
3178 0, /* tp_as_number */
3179 0, /* tp_as_sequence */
3180 0, /* tp_as_mapping */
3181 0, /* tp_hash */
3182 0, /* tp_call */
3183 0, /* tp_str */
3184 PyObject_GenericGetAttr, /* tp_getattro */
3185 0, /* tp_setattro */
3186 0, /* tp_as_buffer */
3187 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3188 0, /* tp_doc */
3189 (traverseproc)dictiter_traverse, /* tp_traverse */
3190 0, /* tp_clear */
3191 0, /* tp_richcompare */
3192 0, /* tp_weaklistoffset */
3193 PyObject_SelfIter, /* tp_iter */
3194 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3195 dictiter_methods, /* tp_methods */
3196 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003197};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003198
3199
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003200static PyObject *
3201dictiter_reduce(dictiterobject *di)
3202{
3203 PyObject *list;
3204 dictiterobject tmp;
3205
3206 list = PyList_New(0);
3207 if (!list)
3208 return NULL;
3209
3210 /* copy the itertor state */
3211 tmp = *di;
3212 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003213
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003214 /* iterate the temporary into a list */
3215 for(;;) {
3216 PyObject *element = 0;
3217 if (Py_TYPE(di) == &PyDictIterItem_Type)
3218 element = dictiter_iternextitem(&tmp);
3219 else if (Py_TYPE(di) == &PyDictIterKey_Type)
3220 element = dictiter_iternextkey(&tmp);
3221 else if (Py_TYPE(di) == &PyDictIterValue_Type)
3222 element = dictiter_iternextvalue(&tmp);
3223 else
3224 assert(0);
3225 if (element) {
3226 if (PyList_Append(list, element)) {
3227 Py_DECREF(element);
3228 Py_DECREF(list);
3229 Py_XDECREF(tmp.di_dict);
3230 return NULL;
3231 }
3232 Py_DECREF(element);
3233 } else
3234 break;
3235 }
3236 Py_XDECREF(tmp.di_dict);
3237 /* check for error */
3238 if (tmp.di_dict != NULL) {
3239 /* we have an error */
3240 Py_DECREF(list);
3241 return NULL;
3242 }
Antoine Pitroua7013882012-04-05 00:04:20 +02003243 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003244}
3245
Guido van Rossum3ac67412007-02-10 18:55:06 +00003246/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003247/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003248/***********************************************/
3249
Guido van Rossumb90c8482007-02-10 01:11:45 +00003250/* The instance lay-out is the same for all three; but the type differs. */
3251
Guido van Rossumb90c8482007-02-10 01:11:45 +00003252static void
Eric Snow96c6af92015-05-29 22:21:39 -06003253dictview_dealloc(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003254{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003255 Py_XDECREF(dv->dv_dict);
3256 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003257}
3258
3259static int
Eric Snow96c6af92015-05-29 22:21:39 -06003260dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003261{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003262 Py_VISIT(dv->dv_dict);
3263 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003264}
3265
Guido van Rossum83825ac2007-02-10 04:54:19 +00003266static Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003267dictview_len(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003268{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003269 Py_ssize_t len = 0;
3270 if (dv->dv_dict != NULL)
3271 len = dv->dv_dict->ma_used;
3272 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003273}
3274
Eric Snow96c6af92015-05-29 22:21:39 -06003275PyObject *
3276_PyDictView_New(PyObject *dict, PyTypeObject *type)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003277{
Eric Snow96c6af92015-05-29 22:21:39 -06003278 _PyDictViewObject *dv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003279 if (dict == NULL) {
3280 PyErr_BadInternalCall();
3281 return NULL;
3282 }
3283 if (!PyDict_Check(dict)) {
3284 /* XXX Get rid of this restriction later */
3285 PyErr_Format(PyExc_TypeError,
3286 "%s() requires a dict argument, not '%s'",
3287 type->tp_name, dict->ob_type->tp_name);
3288 return NULL;
3289 }
Eric Snow96c6af92015-05-29 22:21:39 -06003290 dv = PyObject_GC_New(_PyDictViewObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003291 if (dv == NULL)
3292 return NULL;
3293 Py_INCREF(dict);
3294 dv->dv_dict = (PyDictObject *)dict;
3295 _PyObject_GC_TRACK(dv);
3296 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003297}
3298
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003299/* TODO(guido): The views objects are not complete:
3300
3301 * support more set operations
3302 * support arbitrary mappings?
3303 - either these should be static or exported in dictobject.h
3304 - if public then they should probably be in builtins
3305*/
3306
Guido van Rossumaac530c2007-08-24 22:33:45 +00003307/* Return 1 if self is a subset of other, iterating over self;
3308 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003309static int
3310all_contained_in(PyObject *self, PyObject *other)
3311{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003312 PyObject *iter = PyObject_GetIter(self);
3313 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003315 if (iter == NULL)
3316 return -1;
3317 for (;;) {
3318 PyObject *next = PyIter_Next(iter);
3319 if (next == NULL) {
3320 if (PyErr_Occurred())
3321 ok = -1;
3322 break;
3323 }
3324 ok = PySequence_Contains(other, next);
3325 Py_DECREF(next);
3326 if (ok <= 0)
3327 break;
3328 }
3329 Py_DECREF(iter);
3330 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003331}
3332
3333static PyObject *
3334dictview_richcompare(PyObject *self, PyObject *other, int op)
3335{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003336 Py_ssize_t len_self, len_other;
3337 int ok;
3338 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003340 assert(self != NULL);
3341 assert(PyDictViewSet_Check(self));
3342 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00003343
Brian Curtindfc80e32011-08-10 20:28:54 -05003344 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
3345 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003347 len_self = PyObject_Size(self);
3348 if (len_self < 0)
3349 return NULL;
3350 len_other = PyObject_Size(other);
3351 if (len_other < 0)
3352 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003354 ok = 0;
3355 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00003356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003357 case Py_NE:
3358 case Py_EQ:
3359 if (len_self == len_other)
3360 ok = all_contained_in(self, other);
3361 if (op == Py_NE && ok >= 0)
3362 ok = !ok;
3363 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003365 case Py_LT:
3366 if (len_self < len_other)
3367 ok = all_contained_in(self, other);
3368 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003370 case Py_LE:
3371 if (len_self <= len_other)
3372 ok = all_contained_in(self, other);
3373 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003375 case Py_GT:
3376 if (len_self > len_other)
3377 ok = all_contained_in(other, self);
3378 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003380 case Py_GE:
3381 if (len_self >= len_other)
3382 ok = all_contained_in(other, self);
3383 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003385 }
3386 if (ok < 0)
3387 return NULL;
3388 result = ok ? Py_True : Py_False;
3389 Py_INCREF(result);
3390 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003391}
3392
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003393static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003394dictview_repr(_PyDictViewObject *dv)
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003395{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003396 PyObject *seq;
3397 PyObject *result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003399 seq = PySequence_List((PyObject *)dv);
3400 if (seq == NULL)
3401 return NULL;
3402
3403 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
3404 Py_DECREF(seq);
3405 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003406}
3407
Guido van Rossum3ac67412007-02-10 18:55:06 +00003408/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003409
3410static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003411dictkeys_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003412{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003413 if (dv->dv_dict == NULL) {
3414 Py_RETURN_NONE;
3415 }
3416 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003417}
3418
3419static int
Eric Snow96c6af92015-05-29 22:21:39 -06003420dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00003421{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003422 if (dv->dv_dict == NULL)
3423 return 0;
3424 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003425}
3426
Guido van Rossum83825ac2007-02-10 04:54:19 +00003427static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003428 (lenfunc)dictview_len, /* sq_length */
3429 0, /* sq_concat */
3430 0, /* sq_repeat */
3431 0, /* sq_item */
3432 0, /* sq_slice */
3433 0, /* sq_ass_item */
3434 0, /* sq_ass_slice */
3435 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003436};
3437
Guido van Rossum523259b2007-08-24 23:41:22 +00003438static PyObject*
3439dictviews_sub(PyObject* self, PyObject *other)
3440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003441 PyObject *result = PySet_New(self);
3442 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003443 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003445 if (result == NULL)
3446 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003447
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003448 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003449 if (tmp == NULL) {
3450 Py_DECREF(result);
3451 return NULL;
3452 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003454 Py_DECREF(tmp);
3455 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003456}
3457
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003458PyObject*
3459_PyDictView_Intersect(PyObject* self, PyObject *other)
Guido van Rossum523259b2007-08-24 23:41:22 +00003460{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003461 PyObject *result = PySet_New(self);
3462 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003463 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003465 if (result == NULL)
3466 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003467
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003468 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_intersection_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003469 if (tmp == NULL) {
3470 Py_DECREF(result);
3471 return NULL;
3472 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003474 Py_DECREF(tmp);
3475 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003476}
3477
3478static PyObject*
3479dictviews_or(PyObject* self, PyObject *other)
3480{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003481 PyObject *result = PySet_New(self);
3482 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003483 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003485 if (result == NULL)
3486 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003487
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003488 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003489 if (tmp == NULL) {
3490 Py_DECREF(result);
3491 return NULL;
3492 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003494 Py_DECREF(tmp);
3495 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003496}
3497
3498static PyObject*
3499dictviews_xor(PyObject* self, PyObject *other)
3500{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003501 PyObject *result = PySet_New(self);
3502 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003503 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003505 if (result == NULL)
3506 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003507
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003508 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_symmetric_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003509 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