blob: cb0d2a711281c416e2ce61570751517d489182c9 [file] [log] [blame]
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001
Guido van Rossum2bc13791999-03-24 19:06:42 +00002/* Dictionary object implementation using a hash table */
Guido van Rossum9bfef441993-03-29 10:43:31 +00003
Raymond Hettinger930427b2003-05-03 06:51:59 +00004/* The distribution includes a separate file, Objects/dictnotes.txt,
Tim Peters60b29962006-01-01 01:19:23 +00005 describing explorations into dictionary design and optimization.
Raymond Hettinger930427b2003-05-03 06:51:59 +00006 It covers typical dictionary use patterns, the parameters for
7 tuning dictionaries, and several ideas for possible optimizations.
8*/
9
Benjamin Peterson7d95e402012-04-23 11:24:50 -040010
11/*
12There are four kinds of slots in the table:
13
141. Unused. me_key == me_value == NULL
15 Does not hold an active (key, value) pair now and never did. Unused can
16 transition to Active upon key insertion. This is the only case in which
17 me_key is NULL, and is each slot's initial state.
18
192. Active. me_key != NULL and me_key != dummy and me_value != NULL
20 Holds an active (key, value) pair. Active can transition to Dummy or
21 Pending upon key deletion (for combined and split tables respectively).
22 This is the only case in which me_value != NULL.
23
243. Dummy. me_key == dummy and me_value == NULL
25 Previously held an active (key, value) pair, but that was deleted and an
26 active pair has not yet overwritten the slot. Dummy can transition to
27 Active upon key insertion. Dummy slots cannot be made Unused again
28 (cannot have me_key set to NULL), else the probe sequence in case of
29 collision would have no way to know they were once active.
30
314. Pending. Not yet inserted or deleted from a split-table.
32 key != NULL, key != dummy and value == NULL
33
34The DictObject can be in one of two forms.
35Either:
36 A combined table:
37 ma_values == NULL, dk_refcnt == 1.
38 Values are stored in the me_value field of the PyDictKeysObject.
39 Slot kind 4 is not allowed i.e.
40 key != NULL, key != dummy and value == NULL is illegal.
41Or:
42 A split table:
43 ma_values != NULL, dk_refcnt >= 1
44 Values are stored in the ma_values array.
45 Only string (unicode) keys are allowed, no <dummy> keys are present.
46
47Note: .popitem() abuses the me_hash field of an Unused or Dummy slot to
48hold a search finger. The me_hash field of Unused or Dummy slots has no
49meaning otherwise. As a consequence of this popitem always converts the dict
50to the combined-table form.
51*/
52
53/* PyDict_MINSIZE_SPLIT is the minimum size of a split dictionary.
54 * It must be a power of 2, and at least 4.
55 * Resizing of split dictionaries is very rare, so the saving memory is more
56 * important than the cost of resizing.
57 */
58#define PyDict_MINSIZE_SPLIT 4
59
60/* PyDict_MINSIZE_COMBINED is the starting size for any new, non-split dict.
61 * 8 allows dicts with no more than 5 active entries; experiments suggested
62 * this suffices for the majority of dicts (consisting mostly of usually-small
63 * dicts created to pass keyword arguments).
64 * Making this 8, rather than 4 reduces the number of resizes for most
65 * dictionaries, without any significant extra memory use.
66 */
67#define PyDict_MINSIZE_COMBINED 8
68
Guido van Rossumc0b618a1997-05-02 03:12:38 +000069#include "Python.h"
Christian Heimes0ded5b52007-12-10 15:50:56 +000070#include "stringlib/eq.h"
Guido van Rossum4b1302b1993-03-27 18:11:32 +000071
Larry Hastings61272b72014-01-07 12:41:53 -080072/*[clinic input]
Larry Hastingsc2047262014-01-25 20:43:29 -080073class dict "PyDictObject *" "&PyDict_Type"
Larry Hastings61272b72014-01-07 12:41:53 -080074[clinic start generated code]*/
Larry Hastings581ee362014-01-28 05:00:08 -080075/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f157a5a0ce9589d6]*/
Larry Hastings44e2eaa2013-11-23 15:37:55 -080076
Benjamin Peterson7d95e402012-04-23 11:24:50 -040077typedef struct {
78 /* Cached hash code of me_key. */
79 Py_hash_t me_hash;
80 PyObject *me_key;
81 PyObject *me_value; /* This field is only meaningful for combined tables */
82} PyDictKeyEntry;
83
84typedef PyDictKeyEntry *(*dict_lookup_func)
85(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject ***value_addr);
86
87struct _dictkeysobject {
88 Py_ssize_t dk_refcnt;
89 Py_ssize_t dk_size;
90 dict_lookup_func dk_lookup;
91 Py_ssize_t dk_usable;
92 PyDictKeyEntry dk_entries[1];
93};
94
95
96/*
97To ensure the lookup algorithm terminates, there must be at least one Unused
98slot (NULL key) in the table.
99To avoid slowing down lookups on a near-full table, we resize the table when
100it's USABLE_FRACTION (currently two-thirds) full.
101*/
Guido van Rossum16e93a81997-01-28 00:00:11 +0000102
Tim Peterseb28ef22001-06-02 05:27:19 +0000103#define PERTURB_SHIFT 5
104
Guido van Rossum16e93a81997-01-28 00:00:11 +0000105/*
Tim Peterseb28ef22001-06-02 05:27:19 +0000106Major subtleties ahead: Most hash schemes depend on having a "good" hash
107function, in the sense of simulating randomness. Python doesn't: its most
108important hash functions (for strings and ints) are very regular in common
109cases:
Tim Peters15d49292001-05-27 07:39:22 +0000110
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000111 >>> map(hash, (0, 1, 2, 3))
112 [0, 1, 2, 3]
113 >>> map(hash, ("namea", "nameb", "namec", "named"))
114 [-1658398457, -1658398460, -1658398459, -1658398462]
115 >>>
Tim Peters15d49292001-05-27 07:39:22 +0000116
Tim Peterseb28ef22001-06-02 05:27:19 +0000117This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
118the low-order i bits as the initial table index is extremely fast, and there
119are no collisions at all for dicts indexed by a contiguous range of ints.
120The same is approximately true when keys are "consecutive" strings. So this
121gives better-than-random behavior in common cases, and that's very desirable.
Tim Peters15d49292001-05-27 07:39:22 +0000122
Tim Peterseb28ef22001-06-02 05:27:19 +0000123OTOH, when collisions occur, the tendency to fill contiguous slices of the
124hash table makes a good collision resolution strategy crucial. Taking only
125the last i bits of the hash code is also vulnerable: for example, consider
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126the list [i << 16 for i in range(20000)] as a set of keys. Since ints are
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000127their own hash codes, and this fits in a dict of size 2**15, the last 15 bits
128 of every hash code are all 0: they *all* map to the same table index.
Tim Peters15d49292001-05-27 07:39:22 +0000129
Tim Peterseb28ef22001-06-02 05:27:19 +0000130But catering to unusual cases should not slow the usual ones, so we just take
131the last i bits anyway. It's up to collision resolution to do the rest. If
132we *usually* find the key we're looking for on the first try (and, it turns
133out, we usually do -- the table load factor is kept under 2/3, so the odds
134are solidly in our favor), then it makes best sense to keep the initial index
135computation dirt cheap.
Tim Peters15d49292001-05-27 07:39:22 +0000136
Tim Peterseb28ef22001-06-02 05:27:19 +0000137The first half of collision resolution is to visit table indices via this
138recurrence:
Tim Peters15d49292001-05-27 07:39:22 +0000139
Tim Peterseb28ef22001-06-02 05:27:19 +0000140 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +0000141
Tim Peterseb28ef22001-06-02 05:27:19 +0000142For any initial j in range(2**i), repeating that 2**i times generates each
143int in range(2**i) exactly once (see any text on random-number generation for
144proof). By itself, this doesn't help much: like linear probing (setting
145j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
146order. This would be bad, except that's not the only thing we do, and it's
147actually *good* in the common cases where hash keys are consecutive. In an
148example that's really too small to make this entirely clear, for a table of
149size 2**3 the order of indices is:
Tim Peters15d49292001-05-27 07:39:22 +0000150
Tim Peterseb28ef22001-06-02 05:27:19 +0000151 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
152
153If two things come in at index 5, the first place we look after is index 2,
154not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
155Linear probing is deadly in this case because there the fixed probe order
156is the *same* as the order consecutive keys are likely to arrive. But it's
157extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
158and certain that consecutive hash codes do not.
159
160The other half of the strategy is to get the other bits of the hash code
161into play. This is done by initializing a (unsigned) vrbl "perturb" to the
162full hash code, and changing the recurrence to:
163
164 j = (5*j) + 1 + perturb;
165 perturb >>= PERTURB_SHIFT;
166 use j % 2**i as the next table index;
167
168Now the probe sequence depends (eventually) on every bit in the hash code,
169and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
170because it quickly magnifies small differences in the bits that didn't affect
171the initial index. Note that because perturb is unsigned, if the recurrence
172is executed often enough perturb eventually becomes and remains 0. At that
173point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
174that's certain to find an empty slot eventually (since it generates every int
175in range(2**i), and we make sure there's always at least one empty slot).
176
177Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
178small so that the high bits of the hash code continue to affect the probe
179sequence across iterations; but you want it large so that in really bad cases
180the high-order hash bits have an effect on early iterations. 5 was "the
181best" in minimizing total collisions across experiments Tim Peters ran (on
182both normal and pathological cases), but 4 and 6 weren't significantly worse.
183
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000184Historical: Reimer Behrends contributed the idea of using a polynomial-based
Tim Peterseb28ef22001-06-02 05:27:19 +0000185approach, using repeated multiplication by x in GF(2**n) where an irreducible
186polynomial for each table size was chosen such that x was a primitive root.
187Christian Tismer later extended that to use division by x instead, as an
188efficient way to get the high bits of the hash code into play. This scheme
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000189also gave excellent collision statistics, but was more expensive: two
190if-tests were required inside the loop; computing "the next" index took about
191the same number of operations but without as much potential parallelism
192(e.g., computing 5*j can go on at the same time as computing 1+perturb in the
193above, and then shifting perturb can be done while the table index is being
194masked); and the PyDictObject struct required a member to hold the table's
195polynomial. In Tim's experiments the current scheme ran faster, produced
196equally good collision statistics, needed less code & used less memory.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000197
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000198*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000199
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400200/* Object used as dummy key to fill deleted entries
201 * This could be any unique object,
202 * use a custom type in order to minimise coupling.
203*/
204static PyObject _dummy_struct;
205
206#define dummy (&_dummy_struct)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000207
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000208#ifdef Py_REF_DEBUG
209PyObject *
210_PyDict_Dummy(void)
211{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 return dummy;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000213}
214#endif
215
Fred Drake1bff34a2000-08-31 19:31:38 +0000216/* forward declarations */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400217static PyDictKeyEntry *lookdict(PyDictObject *mp, PyObject *key,
218 Py_hash_t hash, PyObject ***value_addr);
219static PyDictKeyEntry *lookdict_unicode(PyDictObject *mp, PyObject *key,
220 Py_hash_t hash, PyObject ***value_addr);
221static PyDictKeyEntry *
222lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
223 Py_hash_t hash, PyObject ***value_addr);
224static PyDictKeyEntry *lookdict_split(PyDictObject *mp, PyObject *key,
225 Py_hash_t hash, PyObject ***value_addr);
Fred Drake1bff34a2000-08-31 19:31:38 +0000226
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400227static int dictresize(PyDictObject *mp, Py_ssize_t minused);
Tim Petersdea48ec2001-05-22 20:40:22 +0000228
Raymond Hettinger43442782004-03-17 21:55:03 +0000229/* Dictionary reuse scheme to save calls to malloc, free, and memset */
Christian Heimes2202f872008-02-06 14:31:34 +0000230#ifndef PyDict_MAXFREELIST
231#define PyDict_MAXFREELIST 80
232#endif
233static PyDictObject *free_list[PyDict_MAXFREELIST];
234static int numfree = 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000235
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300236#include "clinic/dictobject.c.h"
237
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100238int
239PyDict_ClearFreeList(void)
Christian Heimes77c02eb2008-02-09 02:18:51 +0000240{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 PyDictObject *op;
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100242 int ret = numfree;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 while (numfree) {
244 op = free_list[--numfree];
245 assert(PyDict_CheckExact(op));
246 PyObject_GC_Del(op);
247 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100248 return ret;
249}
250
David Malcolm49526f42012-06-22 14:55:41 -0400251/* Print summary info about the state of the optimized allocator */
252void
253_PyDict_DebugMallocStats(FILE *out)
254{
255 _PyDebugAllocatorStats(out,
256 "free PyDictObject", numfree, sizeof(PyDictObject));
257}
258
259
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100260void
261PyDict_Fini(void)
262{
263 PyDict_ClearFreeList();
Christian Heimes77c02eb2008-02-09 02:18:51 +0000264}
265
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200266#define DK_DEBUG_INCREF _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA
267#define DK_DEBUG_DECREF _Py_DEC_REFTOTAL _Py_REF_DEBUG_COMMA
268
269#define DK_INCREF(dk) (DK_DEBUG_INCREF ++(dk)->dk_refcnt)
270#define DK_DECREF(dk) if (DK_DEBUG_DECREF (--(dk)->dk_refcnt) == 0) free_keys_object(dk)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400271#define DK_SIZE(dk) ((dk)->dk_size)
272#define DK_MASK(dk) (((dk)->dk_size)-1)
273#define IS_POWER_OF_2(x) (((x) & (x-1)) == 0)
274
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200275/* USABLE_FRACTION is the maximum dictionary load.
276 * Currently set to (2n+1)/3. Increasing this ratio makes dictionaries more
277 * dense resulting in more collisions. Decreasing it improves sparseness
278 * at the expense of spreading entries over more cache lines and at the
279 * cost of total memory consumed.
280 *
281 * USABLE_FRACTION must obey the following:
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400282 * (0 < USABLE_FRACTION(n) < n) for all n >= 2
283 *
284 * USABLE_FRACTION should be very quick to calculate.
285 * Fractions around 5/8 to 2/3 seem to work well in practice.
286 */
287
288/* Use (2n+1)/3 rather than 2n+3 because: it makes no difference for
289 * combined tables (the two fractions round to the same number n < ),
290 * but 2*4/3 is 2 whereas (2*4+1)/3 is 3 which potentially saves quite
291 * a lot of space for small, split tables */
292#define USABLE_FRACTION(n) ((((n) << 1)+1)/3)
293
294/* Alternative fraction that is otherwise close enough to (2n+1)/3 to make
295 * little difference. 8 * 2/3 == 8 * 5/8 == 5. 16 * 2/3 == 16 * 5/8 == 10.
296 * 32 * 2/3 = 21, 32 * 5/8 = 20.
297 * Its advantage is that it is faster to compute on machines with slow division.
298 * #define USABLE_FRACTION(n) (((n) >> 1) + ((n) >> 2) - ((n) >> 3))
299*/
300
Victor Stinnera9f61a52013-07-16 22:17:26 +0200301/* GROWTH_RATE. Growth rate upon hitting maximum load.
302 * Currently set to used*2 + capacity/2.
303 * This means that dicts double in size when growing without deletions,
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700304 * but have more head room when the number of deletions is on a par with the
305 * number of insertions.
306 * Raising this to used*4 doubles memory consumption depending on the size of
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200307 * the dictionary, but results in half the number of resizes, less effort to
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700308 * resize.
309 * GROWTH_RATE was set to used*4 up to version 3.2.
310 * GROWTH_RATE was set to used*2 in version 3.3.0
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200311 */
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700312#define GROWTH_RATE(d) (((d)->ma_used*2)+((d)->ma_keys->dk_size>>1))
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400313
314#define ENSURE_ALLOWS_DELETIONS(d) \
315 if ((d)->ma_keys->dk_lookup == lookdict_unicode_nodummy) { \
316 (d)->ma_keys->dk_lookup = lookdict_unicode; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400318
319/* This immutable, empty PyDictKeysObject is used for PyDict_Clear()
320 * (which cannot fail and thus can do no allocation).
321 */
322static PyDictKeysObject empty_keys_struct = {
323 2, /* dk_refcnt 1 for this struct, 1 for dummy_struct */
324 1, /* dk_size */
325 lookdict_split, /* dk_lookup */
326 0, /* dk_usable (immutable) */
327 {
328 { 0, 0, 0 } /* dk_entries (empty) */
329 }
330};
331
332static PyObject *empty_values[1] = { NULL };
333
334#define Py_EMPTY_KEYS &empty_keys_struct
335
336static PyDictKeysObject *new_keys_object(Py_ssize_t size)
337{
338 PyDictKeysObject *dk;
339 Py_ssize_t i;
340 PyDictKeyEntry *ep0;
341
342 assert(size >= PyDict_MINSIZE_SPLIT);
343 assert(IS_POWER_OF_2(size));
344 dk = PyMem_MALLOC(sizeof(PyDictKeysObject) +
345 sizeof(PyDictKeyEntry) * (size-1));
346 if (dk == NULL) {
347 PyErr_NoMemory();
348 return NULL;
349 }
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200350 DK_DEBUG_INCREF dk->dk_refcnt = 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400351 dk->dk_size = size;
352 dk->dk_usable = USABLE_FRACTION(size);
353 ep0 = &dk->dk_entries[0];
354 /* Hash value of slot 0 is used by popitem, so it must be initialized */
355 ep0->me_hash = 0;
356 for (i = 0; i < size; i++) {
357 ep0[i].me_key = NULL;
358 ep0[i].me_value = NULL;
359 }
360 dk->dk_lookup = lookdict_unicode_nodummy;
361 return dk;
362}
363
364static void
365free_keys_object(PyDictKeysObject *keys)
366{
367 PyDictKeyEntry *entries = &keys->dk_entries[0];
368 Py_ssize_t i, n;
369 for (i = 0, n = DK_SIZE(keys); i < n; i++) {
370 Py_XDECREF(entries[i].me_key);
371 Py_XDECREF(entries[i].me_value);
372 }
373 PyMem_FREE(keys);
374}
375
376#define new_values(size) PyMem_NEW(PyObject *, size)
377
378#define free_values(values) PyMem_FREE(values)
379
380/* Consumes a reference to the keys object */
381static PyObject *
382new_dict(PyDictKeysObject *keys, PyObject **values)
383{
384 PyDictObject *mp;
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200385 assert(keys != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 if (numfree) {
387 mp = free_list[--numfree];
388 assert (mp != NULL);
389 assert (Py_TYPE(mp) == &PyDict_Type);
390 _Py_NewReference((PyObject *)mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400392 else {
393 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
394 if (mp == NULL) {
395 DK_DECREF(keys);
396 free_values(values);
397 return NULL;
398 }
399 }
400 mp->ma_keys = keys;
401 mp->ma_values = values;
402 mp->ma_used = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000404}
405
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400406/* Consumes a reference to the keys object */
407static PyObject *
408new_dict_with_shared_keys(PyDictKeysObject *keys)
409{
410 PyObject **values;
411 Py_ssize_t i, size;
412
413 size = DK_SIZE(keys);
414 values = new_values(size);
415 if (values == NULL) {
416 DK_DECREF(keys);
417 return PyErr_NoMemory();
418 }
419 for (i = 0; i < size; i++) {
420 values[i] = NULL;
421 }
422 return new_dict(keys, values);
423}
424
425PyObject *
426PyDict_New(void)
427{
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200428 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE_COMBINED);
429 if (keys == NULL)
430 return NULL;
431 return new_dict(keys, NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400432}
433
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000434/*
435The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000436This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000437Open addressing is preferred over chaining since the link overhead for
438chaining would be substantial (100% with typical malloc overhead).
439
Tim Peterseb28ef22001-06-02 05:27:19 +0000440The initial probe index is computed as hash mod the table size. Subsequent
441probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000442
443All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000444
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000445The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000446contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000447Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000448
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000449lookdict() is general-purpose, and may return NULL if (and only if) a
450comparison raises an exception (this was new in Python 2.5).
Guido van Rossum89d8c602007-09-18 17:26:56 +0000451lookdict_unicode() below is specialized to string keys, comparison of which can
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400452never raise an exception; that function can never return NULL.
453lookdict_unicode_nodummy is further specialized for string keys that cannot be
454the <dummy> value.
455For both, when the key isn't found a PyDictEntry* is returned
456where the key would have been found, *value_addr points to the matching value
457slot.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000458*/
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400459static PyDictKeyEntry *
460lookdict(PyDictObject *mp, PyObject *key,
461 Py_hash_t hash, PyObject ***value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000462{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200463 size_t i;
464 size_t perturb;
465 PyDictKeyEntry *freeslot;
466 size_t mask;
Antoine Pitrou9a234902012-05-13 20:48:01 +0200467 PyDictKeyEntry *ep0;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200468 PyDictKeyEntry *ep;
469 int cmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000471
Antoine Pitrou9a234902012-05-13 20:48:01 +0200472top:
473 mask = DK_MASK(mp->ma_keys);
474 ep0 = &mp->ma_keys->dk_entries[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 i = (size_t)hash & mask;
476 ep = &ep0[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400477 if (ep->me_key == NULL || ep->me_key == key) {
478 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400480 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 if (ep->me_key == dummy)
482 freeslot = ep;
483 else {
484 if (ep->me_hash == hash) {
485 startkey = ep->me_key;
486 Py_INCREF(startkey);
487 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
488 Py_DECREF(startkey);
489 if (cmp < 0)
490 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400491 if (ep0 == mp->ma_keys->dk_entries && ep->me_key == startkey) {
492 if (cmp > 0) {
493 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400495 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 }
497 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200498 /* The dict was mutated, restart */
499 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 }
501 }
502 freeslot = NULL;
503 }
Tim Peters15d49292001-05-27 07:39:22 +0000504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 /* In the loop, me_key == dummy is by far (factor of 100s) the
506 least likely outcome, so test for that last. */
507 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
508 i = (i << 2) + i + perturb + 1;
509 ep = &ep0[i & mask];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400510 if (ep->me_key == NULL) {
511 if (freeslot == NULL) {
512 *value_addr = &ep->me_value;
513 return ep;
514 } else {
515 *value_addr = &freeslot->me_value;
516 return freeslot;
517 }
518 }
519 if (ep->me_key == key) {
520 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400522 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 if (ep->me_hash == hash && ep->me_key != dummy) {
524 startkey = ep->me_key;
525 Py_INCREF(startkey);
526 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
527 Py_DECREF(startkey);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400528 if (cmp < 0) {
529 *value_addr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400531 }
532 if (ep0 == mp->ma_keys->dk_entries && ep->me_key == startkey) {
533 if (cmp > 0) {
534 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400536 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 }
538 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200539 /* The dict was mutated, restart */
540 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 }
542 }
543 else if (ep->me_key == dummy && freeslot == NULL)
544 freeslot = ep;
545 }
546 assert(0); /* NOT REACHED */
547 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000548}
549
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400550/* Specialized version for string-only keys */
551static PyDictKeyEntry *
552lookdict_unicode(PyDictObject *mp, PyObject *key,
553 Py_hash_t hash, PyObject ***value_addr)
Fred Drake1bff34a2000-08-31 19:31:38 +0000554{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200555 size_t i;
556 size_t perturb;
557 PyDictKeyEntry *freeslot;
558 size_t mask = DK_MASK(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400559 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200560 PyDictKeyEntry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 /* Make sure this function doesn't have to handle non-unicode keys,
563 including subclasses of str; e.g., one reason to subclass
564 unicodes is to override __eq__, and for speed we don't cater to
565 that here. */
566 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400567 mp->ma_keys->dk_lookup = lookdict;
568 return lookdict(mp, key, hash, value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 }
Mark Dickinson57e683e2011-09-24 18:18:40 +0100570 i = (size_t)hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 ep = &ep0[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400572 if (ep->me_key == NULL || ep->me_key == key) {
573 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400575 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 if (ep->me_key == dummy)
577 freeslot = ep;
578 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400579 if (ep->me_hash == hash && unicode_eq(ep->me_key, key)) {
580 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400582 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 freeslot = NULL;
584 }
Tim Peters15d49292001-05-27 07:39:22 +0000585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 /* In the loop, me_key == dummy is by far (factor of 100s) the
587 least likely outcome, so test for that last. */
588 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
589 i = (i << 2) + i + perturb + 1;
590 ep = &ep0[i & mask];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400591 if (ep->me_key == NULL) {
592 if (freeslot == NULL) {
593 *value_addr = &ep->me_value;
594 return ep;
595 } else {
596 *value_addr = &freeslot->me_value;
597 return freeslot;
598 }
599 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 if (ep->me_key == key
601 || (ep->me_hash == hash
602 && ep->me_key != dummy
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400603 && unicode_eq(ep->me_key, key))) {
604 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400606 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 if (ep->me_key == dummy && freeslot == NULL)
608 freeslot = ep;
609 }
610 assert(0); /* NOT REACHED */
611 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000612}
613
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400614/* Faster version of lookdict_unicode when it is known that no <dummy> keys
615 * will be present. */
616static PyDictKeyEntry *
617lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
618 Py_hash_t hash, PyObject ***value_addr)
619{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200620 size_t i;
621 size_t perturb;
622 size_t mask = DK_MASK(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400623 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200624 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400625
626 /* Make sure this function doesn't have to handle non-unicode keys,
627 including subclasses of str; e.g., one reason to subclass
628 unicodes is to override __eq__, and for speed we don't cater to
629 that here. */
630 if (!PyUnicode_CheckExact(key)) {
631 mp->ma_keys->dk_lookup = lookdict;
632 return lookdict(mp, key, hash, value_addr);
633 }
634 i = (size_t)hash & mask;
635 ep = &ep0[i];
636 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
637 if (ep->me_key == NULL || ep->me_key == key ||
638 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
639 *value_addr = &ep->me_value;
640 return ep;
641 }
642 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
643 i = (i << 2) + i + perturb + 1;
644 ep = &ep0[i & mask];
645 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
646 if (ep->me_key == NULL || ep->me_key == key ||
647 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
648 *value_addr = &ep->me_value;
649 return ep;
650 }
651 }
652 assert(0); /* NOT REACHED */
653 return 0;
654}
655
656/* Version of lookdict for split tables.
657 * All split tables and only split tables use this lookup function.
658 * Split tables only contain unicode keys and no dummy keys,
659 * so algorithm is the same as lookdict_unicode_nodummy.
660 */
661static PyDictKeyEntry *
662lookdict_split(PyDictObject *mp, PyObject *key,
663 Py_hash_t hash, PyObject ***value_addr)
664{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200665 size_t i;
666 size_t perturb;
667 size_t mask = DK_MASK(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400668 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200669 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400670
671 if (!PyUnicode_CheckExact(key)) {
Benjamin Petersondb780d02012-04-23 13:44:32 -0400672 ep = lookdict(mp, key, hash, value_addr);
673 /* lookdict expects a combined-table, so fix value_addr */
674 i = ep - ep0;
675 *value_addr = &mp->ma_values[i];
676 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400677 }
678 i = (size_t)hash & mask;
679 ep = &ep0[i];
680 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
681 if (ep->me_key == NULL || ep->me_key == key ||
682 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
683 *value_addr = &mp->ma_values[i];
684 return ep;
685 }
686 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
687 i = (i << 2) + i + perturb + 1;
688 ep = &ep0[i & mask];
689 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
690 if (ep->me_key == NULL || ep->me_key == key ||
691 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
692 *value_addr = &mp->ma_values[i & mask];
693 return ep;
694 }
695 }
696 assert(0); /* NOT REACHED */
697 return 0;
698}
699
Benjamin Petersonfb886362010-04-24 18:21:17 +0000700int
701_PyDict_HasOnlyStringKeys(PyObject *dict)
702{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 Py_ssize_t pos = 0;
704 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000705 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400707 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 return 1;
709 while (PyDict_Next(dict, &pos, &key, &value))
710 if (!PyUnicode_Check(key))
711 return 0;
712 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000713}
714
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000715#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 do { \
717 if (!_PyObject_GC_IS_TRACKED(mp)) { \
718 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
719 _PyObject_GC_MAY_BE_TRACKED(value)) { \
720 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 } \
722 } \
723 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000724
725void
726_PyDict_MaybeUntrack(PyObject *op)
727{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 PyDictObject *mp;
729 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400730 Py_ssize_t i, size;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
733 return;
734
735 mp = (PyDictObject *) op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400736 size = DK_SIZE(mp->ma_keys);
737 if (_PyDict_HasSplitTable(mp)) {
738 for (i = 0; i < size; i++) {
739 if ((value = mp->ma_values[i]) == NULL)
740 continue;
741 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
742 assert(!_PyObject_GC_MAY_BE_TRACKED(
743 mp->ma_keys->dk_entries[i].me_key));
744 return;
745 }
746 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400748 else {
749 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
750 for (i = 0; i < size; i++) {
751 if ((value = ep0[i].me_value) == NULL)
752 continue;
753 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
754 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
755 return;
756 }
757 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000759}
760
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400761/* Internal function to find slot for an item from its hash
762 * when it is known that the key is not present in the dict.
763 */
764static PyDictKeyEntry *
765find_empty_slot(PyDictObject *mp, PyObject *key, Py_hash_t hash,
766 PyObject ***value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000767{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400768 size_t i;
769 size_t perturb;
770 size_t mask = DK_MASK(mp->ma_keys);
771 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
772 PyDictKeyEntry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000773
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400774 assert(key != NULL);
775 if (!PyUnicode_CheckExact(key))
776 mp->ma_keys->dk_lookup = lookdict;
777 i = hash & mask;
778 ep = &ep0[i];
779 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
780 i = (i << 2) + i + perturb + 1;
781 ep = &ep0[i & mask];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400783 assert(ep->me_value == NULL);
784 if (mp->ma_values)
785 *value_addr = &mp->ma_values[i & mask];
786 else
787 *value_addr = &ep->me_value;
788 return ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000789}
790
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400791static int
792insertion_resize(PyDictObject *mp)
793{
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700794 return dictresize(mp, GROWTH_RATE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400795}
Antoine Pitroue965d972012-02-27 00:45:12 +0100796
797/*
798Internal routine to insert a new item into the table.
799Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +0100800Returns -1 if an error occurred, or 0 on success.
801*/
802static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400803insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +0100804{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400805 PyObject *old_value;
806 PyObject **value_addr;
807 PyDictKeyEntry *ep;
808 assert(key != dummy);
Antoine Pitroue965d972012-02-27 00:45:12 +0100809
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400810 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
811 if (insertion_resize(mp) < 0)
812 return -1;
813 }
814
815 ep = mp->ma_keys->dk_lookup(mp, key, hash, &value_addr);
Antoine Pitroue965d972012-02-27 00:45:12 +0100816 if (ep == NULL) {
Antoine Pitroue965d972012-02-27 00:45:12 +0100817 return -1;
818 }
Antoine Pitroud6967322014-10-18 00:35:00 +0200819 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
Benjamin Petersona6f195e2012-04-30 10:23:40 -0400820 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400821 MAINTAIN_TRACKING(mp, key, value);
822 old_value = *value_addr;
823 if (old_value != NULL) {
824 assert(ep->me_key != NULL && ep->me_key != dummy);
825 *value_addr = value;
Antoine Pitroud6967322014-10-18 00:35:00 +0200826 Py_DECREF(old_value); /* which **CAN** re-enter (see issue #22653) */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400827 }
828 else {
829 if (ep->me_key == NULL) {
Benjamin Petersona6f195e2012-04-30 10:23:40 -0400830 Py_INCREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400831 if (mp->ma_keys->dk_usable <= 0) {
832 /* Need to resize. */
833 if (insertion_resize(mp) < 0) {
834 Py_DECREF(key);
835 Py_DECREF(value);
836 return -1;
837 }
838 ep = find_empty_slot(mp, key, hash, &value_addr);
839 }
840 mp->ma_keys->dk_usable--;
841 assert(mp->ma_keys->dk_usable >= 0);
842 ep->me_key = key;
843 ep->me_hash = hash;
844 }
845 else {
846 if (ep->me_key == dummy) {
Benjamin Petersona6f195e2012-04-30 10:23:40 -0400847 Py_INCREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400848 ep->me_key = key;
849 ep->me_hash = hash;
850 Py_DECREF(dummy);
851 } else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400852 assert(_PyDict_HasSplitTable(mp));
853 }
854 }
855 mp->ma_used++;
856 *value_addr = value;
Antoine Pitroud6967322014-10-18 00:35:00 +0200857 assert(ep->me_key != NULL && ep->me_key != dummy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400858 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400859 return 0;
Antoine Pitroue965d972012-02-27 00:45:12 +0100860}
861
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000862/*
863Internal routine used by dictresize() to insert an item which is
864known to be absent from the dict. This routine also assumes that
865the dict contains no deleted entries. Besides the performance benefit,
866using insertdict() in dictresize() is dangerous (SF bug #1456209).
867Note that no refcounts are changed by this routine; if needed, the caller
868is responsible for incref'ing `key` and `value`.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400869Neither mp->ma_used nor k->dk_usable are modified by this routine; the caller
870must set them correctly
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000871*/
872static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400873insertdict_clean(PyDictObject *mp, PyObject *key, Py_hash_t hash,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 PyObject *value)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000875{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400876 size_t i;
877 size_t perturb;
878 PyDictKeysObject *k = mp->ma_keys;
879 size_t mask = (size_t)DK_SIZE(k)-1;
880 PyDictKeyEntry *ep0 = &k->dk_entries[0];
881 PyDictKeyEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000882
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400883 assert(k->dk_lookup != NULL);
884 assert(value != NULL);
885 assert(key != NULL);
886 assert(key != dummy);
887 assert(PyUnicode_CheckExact(key) || k->dk_lookup == lookdict);
888 i = hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 ep = &ep0[i];
890 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
891 i = (i << 2) + i + perturb + 1;
892 ep = &ep0[i & mask];
893 }
894 assert(ep->me_value == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 ep->me_key = key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000896 ep->me_hash = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000898}
899
900/*
901Restructure the table by allocating a new table and reinserting all
902items again. When entries have been deleted, the new table may
903actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400904If a table is split (its keys and hashes are shared, its values are not),
905then the values are temporarily copied into the table, it is resized as
906a combined table, then the me_value slots in the old table are NULLed out.
907After resizing a table is always combined,
908but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000909*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000910static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000911dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000912{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 Py_ssize_t newsize;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400914 PyDictKeysObject *oldkeys;
915 PyObject **oldvalues;
916 Py_ssize_t i, oldsize;
Tim Peters91a364d2001-05-19 07:04:38 +0000917
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400918/* Find the smallest table size > minused. */
919 for (newsize = PyDict_MINSIZE_COMBINED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 newsize <= minused && newsize > 0;
921 newsize <<= 1)
922 ;
923 if (newsize <= 0) {
924 PyErr_NoMemory();
925 return -1;
926 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400927 oldkeys = mp->ma_keys;
928 oldvalues = mp->ma_values;
929 /* Allocate a new table. */
930 mp->ma_keys = new_keys_object(newsize);
931 if (mp->ma_keys == NULL) {
932 mp->ma_keys = oldkeys;
933 return -1;
934 }
935 if (oldkeys->dk_lookup == lookdict)
936 mp->ma_keys->dk_lookup = lookdict;
937 oldsize = DK_SIZE(oldkeys);
938 mp->ma_values = NULL;
939 /* If empty then nothing to copy so just return */
940 if (oldsize == 1) {
941 assert(oldkeys == Py_EMPTY_KEYS);
942 DK_DECREF(oldkeys);
943 return 0;
944 }
945 /* Main loop below assumes we can transfer refcount to new keys
946 * and that value is stored in me_value.
947 * Increment ref-counts and copy values here to compensate
948 * This (resizing a split table) should be relatively rare */
949 if (oldvalues != NULL) {
950 for (i = 0; i < oldsize; i++) {
951 if (oldvalues[i] != NULL) {
952 Py_INCREF(oldkeys->dk_entries[i].me_key);
953 oldkeys->dk_entries[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 }
956 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400957 /* Main loop */
958 for (i = 0; i < oldsize; i++) {
959 PyDictKeyEntry *ep = &oldkeys->dk_entries[i];
960 if (ep->me_value != NULL) {
961 assert(ep->me_key != dummy);
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000962 insertdict_clean(mp, ep->me_key, ep->me_hash, ep->me_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400965 mp->ma_keys->dk_usable -= mp->ma_used;
966 if (oldvalues != NULL) {
967 /* NULL out me_value slot in oldkeys, in case it was shared */
968 for (i = 0; i < oldsize; i++)
969 oldkeys->dk_entries[i].me_value = NULL;
970 assert(oldvalues != empty_values);
971 free_values(oldvalues);
972 DK_DECREF(oldkeys);
973 }
974 else {
975 assert(oldkeys->dk_lookup != lookdict_split);
976 if (oldkeys->dk_lookup != lookdict_unicode_nodummy) {
977 PyDictKeyEntry *ep0 = &oldkeys->dk_entries[0];
978 for (i = 0; i < oldsize; i++) {
979 if (ep0[i].me_key == dummy)
980 Py_DECREF(dummy);
981 }
982 }
983 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200984 DK_DEBUG_DECREF PyMem_FREE(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400985 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000987}
988
Benjamin Peterson15ee8212012-04-24 14:44:18 -0400989/* Returns NULL if unable to split table.
990 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400991static PyDictKeysObject *
992make_keys_shared(PyObject *op)
993{
994 Py_ssize_t i;
995 Py_ssize_t size;
996 PyDictObject *mp = (PyDictObject *)op;
997
Benjamin Peterson15ee8212012-04-24 14:44:18 -0400998 if (!PyDict_CheckExact(op))
999 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001000 if (!_PyDict_HasSplitTable(mp)) {
1001 PyDictKeyEntry *ep0;
1002 PyObject **values;
1003 assert(mp->ma_keys->dk_refcnt == 1);
1004 if (mp->ma_keys->dk_lookup == lookdict) {
1005 return NULL;
1006 }
1007 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
1008 /* Remove dummy keys */
1009 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
1010 return NULL;
1011 }
1012 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
1013 /* Copy values into a new array */
1014 ep0 = &mp->ma_keys->dk_entries[0];
1015 size = DK_SIZE(mp->ma_keys);
1016 values = new_values(size);
1017 if (values == NULL) {
1018 PyErr_SetString(PyExc_MemoryError,
1019 "Not enough memory to allocate new values array");
1020 return NULL;
1021 }
1022 for (i = 0; i < size; i++) {
1023 values[i] = ep0[i].me_value;
1024 ep0[i].me_value = NULL;
1025 }
1026 mp->ma_keys->dk_lookup = lookdict_split;
1027 mp->ma_values = values;
1028 }
1029 DK_INCREF(mp->ma_keys);
1030 return mp->ma_keys;
1031}
Christian Heimes99170a52007-12-19 02:07:34 +00001032
1033PyObject *
1034_PyDict_NewPresized(Py_ssize_t minused)
1035{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001036 Py_ssize_t newsize;
1037 PyDictKeysObject *new_keys;
1038 for (newsize = PyDict_MINSIZE_COMBINED;
1039 newsize <= minused && newsize > 0;
1040 newsize <<= 1)
1041 ;
1042 new_keys = new_keys_object(newsize);
1043 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001045 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001046}
1047
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001048/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1049 * that may occur (originally dicts supported only string keys, and exceptions
1050 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001051 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001052 * (suppressed) occurred while computing the key's hash, or that some error
1053 * (suppressed) occurred when comparing keys in the dict's internal probe
1054 * sequence. A nasty example of the latter is when a Python-coded comparison
1055 * function hits a stack-depth error, which can cause this to return NULL
1056 * even if the key is present.
1057 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001058PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001059PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001060{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001061 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001063 PyDictKeyEntry *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 PyThreadState *tstate;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001065 PyObject **value_addr;
1066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 if (!PyDict_Check(op))
1068 return NULL;
1069 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001070 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 {
1072 hash = PyObject_Hash(key);
1073 if (hash == -1) {
1074 PyErr_Clear();
1075 return NULL;
1076 }
1077 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 /* We can arrive here with a NULL tstate during initialization: try
1080 running "python -Wi" for an example related to string interning.
1081 Let's just hope that no exception occurs then... This must be
1082 _PyThreadState_Current and not PyThreadState_GET() because in debug
1083 mode, the latter complains if tstate is NULL. */
1084 tstate = (PyThreadState*)_Py_atomic_load_relaxed(
1085 &_PyThreadState_Current);
1086 if (tstate != NULL && tstate->curexc_type != NULL) {
1087 /* preserve the existing exception */
1088 PyObject *err_type, *err_value, *err_tb;
1089 PyErr_Fetch(&err_type, &err_value, &err_tb);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001090 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 /* ignore errors */
1092 PyErr_Restore(err_type, err_value, err_tb);
1093 if (ep == NULL)
1094 return NULL;
1095 }
1096 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001097 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 if (ep == NULL) {
1099 PyErr_Clear();
1100 return NULL;
1101 }
1102 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001103 return *value_addr;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001104}
1105
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001106PyObject *
1107_PyDict_GetItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1108{
1109 PyDictObject *mp = (PyDictObject *)op;
1110 PyDictKeyEntry *ep;
1111 PyThreadState *tstate;
1112 PyObject **value_addr;
1113
1114 if (!PyDict_Check(op))
1115 return NULL;
1116
1117 /* We can arrive here with a NULL tstate during initialization: try
1118 running "python -Wi" for an example related to string interning.
1119 Let's just hope that no exception occurs then... This must be
1120 _PyThreadState_Current and not PyThreadState_GET() because in debug
1121 mode, the latter complains if tstate is NULL. */
1122 tstate = (PyThreadState*)_Py_atomic_load_relaxed(
1123 &_PyThreadState_Current);
1124 if (tstate != NULL && tstate->curexc_type != NULL) {
1125 /* preserve the existing exception */
1126 PyObject *err_type, *err_value, *err_tb;
1127 PyErr_Fetch(&err_type, &err_value, &err_tb);
1128 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
1129 /* ignore errors */
1130 PyErr_Restore(err_type, err_value, err_tb);
1131 if (ep == NULL)
1132 return NULL;
1133 }
1134 else {
1135 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
1136 if (ep == NULL) {
1137 PyErr_Clear();
1138 return NULL;
1139 }
1140 }
1141 return *value_addr;
1142}
1143
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001144/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1145 This returns NULL *with* an exception set if an exception occurred.
1146 It returns NULL *without* an exception set if the key wasn't present.
1147*/
1148PyObject *
1149PyDict_GetItemWithError(PyObject *op, PyObject *key)
1150{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001151 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 PyDictObject*mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001153 PyDictKeyEntry *ep;
1154 PyObject **value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 if (!PyDict_Check(op)) {
1157 PyErr_BadInternalCall();
1158 return NULL;
1159 }
1160 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001161 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 {
1163 hash = PyObject_Hash(key);
1164 if (hash == -1) {
1165 return NULL;
1166 }
1167 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001168
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001169 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 if (ep == NULL)
1171 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001172 return *value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001173}
1174
Brett Cannonfd074152012-04-14 14:10:13 -04001175PyObject *
1176_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1177{
1178 PyObject *kv;
1179 kv = _PyUnicode_FromId(key); /* borrowed */
1180 if (kv == NULL)
1181 return NULL;
1182 return PyDict_GetItemWithError(dp, kv);
1183}
1184
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001185/* Fast version of global value lookup.
1186 * Lookup in globals, then builtins.
1187 */
1188PyObject *
1189_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001190{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001191 PyObject *x;
1192 if (PyUnicode_CheckExact(key)) {
1193 PyObject **value_addr;
1194 Py_hash_t hash = ((PyASCIIObject *)key)->hash;
1195 if (hash != -1) {
1196 PyDictKeyEntry *e;
1197 e = globals->ma_keys->dk_lookup(globals, key, hash, &value_addr);
1198 if (e == NULL) {
1199 return NULL;
1200 }
1201 x = *value_addr;
1202 if (x != NULL)
1203 return x;
1204 e = builtins->ma_keys->dk_lookup(builtins, key, hash, &value_addr);
1205 if (e == NULL) {
1206 return NULL;
1207 }
1208 x = *value_addr;
1209 return x;
1210 }
Antoine Pitroue965d972012-02-27 00:45:12 +01001211 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001212 x = PyDict_GetItemWithError((PyObject *)globals, key);
1213 if (x != NULL)
1214 return x;
1215 if (PyErr_Occurred())
1216 return NULL;
1217 return PyDict_GetItemWithError((PyObject *)builtins, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001218}
1219
Antoine Pitroue965d972012-02-27 00:45:12 +01001220/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1221 * dictionary if it's merely replacing the value for an existing key.
1222 * This means that it's safe to loop over a dictionary with PyDict_Next()
1223 * and occasionally replace a value -- but you can't insert new keys or
1224 * remove them.
1225 */
1226int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001227PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001228{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001229 PyDictObject *mp;
1230 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001231 if (!PyDict_Check(op)) {
1232 PyErr_BadInternalCall();
1233 return -1;
1234 }
1235 assert(key);
1236 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001237 mp = (PyDictObject *)op;
1238 if (!PyUnicode_CheckExact(key) ||
1239 (hash = ((PyASCIIObject *) key)->hash) == -1)
1240 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001241 hash = PyObject_Hash(key);
1242 if (hash == -1)
1243 return -1;
1244 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001245
1246 /* insertdict() handles any resizing that might be necessary */
1247 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001248}
1249
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001250int
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001251_PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
1252 Py_hash_t hash)
1253{
1254 PyDictObject *mp;
1255
1256 if (!PyDict_Check(op)) {
1257 PyErr_BadInternalCall();
1258 return -1;
1259 }
1260 assert(key);
1261 assert(value);
1262 mp = (PyDictObject *)op;
1263
1264 /* insertdict() handles any resizing that might be necessary */
1265 return insertdict(mp, key, hash, value);
1266}
1267
1268int
Tim Peters1f5871e2000-07-04 17:44:48 +00001269PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001270{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001271 PyDictObject *mp;
1272 Py_hash_t hash;
1273 PyDictKeyEntry *ep;
1274 PyObject *old_key, *old_value;
1275 PyObject **value_addr;
Guido van Rossumd7047b31995-01-02 19:07:15 +00001276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 if (!PyDict_Check(op)) {
1278 PyErr_BadInternalCall();
1279 return -1;
1280 }
1281 assert(key);
1282 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001283 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 hash = PyObject_Hash(key);
1285 if (hash == -1)
1286 return -1;
1287 }
1288 mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001289 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 if (ep == NULL)
1291 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001292 if (*value_addr == NULL) {
Raymond Hettinger69492da2013-09-02 15:59:26 -07001293 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 return -1;
1295 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001296 old_value = *value_addr;
1297 *value_addr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001299 if (!_PyDict_HasSplitTable(mp)) {
1300 ENSURE_ALLOWS_DELETIONS(mp);
1301 old_key = ep->me_key;
1302 Py_INCREF(dummy);
1303 ep->me_key = dummy;
1304 Py_DECREF(old_key);
1305 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 Py_DECREF(old_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001308}
1309
Guido van Rossum25831651993-05-19 14:50:45 +00001310void
Tim Peters1f5871e2000-07-04 17:44:48 +00001311PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001312{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001314 PyDictKeysObject *oldkeys;
1315 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 if (!PyDict_Check(op))
1319 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001320 mp = ((PyDictObject *)op);
1321 oldkeys = mp->ma_keys;
1322 oldvalues = mp->ma_values;
1323 if (oldvalues == empty_values)
1324 return;
1325 /* Empty the dict... */
1326 DK_INCREF(Py_EMPTY_KEYS);
1327 mp->ma_keys = Py_EMPTY_KEYS;
1328 mp->ma_values = empty_values;
1329 mp->ma_used = 0;
1330 /* ...then clear the keys and values */
1331 if (oldvalues != NULL) {
1332 n = DK_SIZE(oldkeys);
1333 for (i = 0; i < n; i++)
1334 Py_CLEAR(oldvalues[i]);
1335 free_values(oldvalues);
1336 DK_DECREF(oldkeys);
1337 }
1338 else {
1339 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001340 DK_DECREF(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001341 }
1342}
1343
1344/* Returns -1 if no more items (or op is not a dict),
1345 * index of item otherwise. Stores value in pvalue
1346 */
1347Py_LOCAL_INLINE(Py_ssize_t)
1348dict_next(PyObject *op, Py_ssize_t i, PyObject **pvalue)
1349{
1350 Py_ssize_t mask, offset;
1351 PyDictObject *mp;
1352 PyObject **value_ptr;
1353
1354
1355 if (!PyDict_Check(op))
1356 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001358 if (i < 0)
1359 return -1;
1360 if (mp->ma_values) {
1361 value_ptr = &mp->ma_values[i];
1362 offset = sizeof(PyObject *);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001364 else {
1365 value_ptr = &mp->ma_keys->dk_entries[i].me_value;
1366 offset = sizeof(PyDictKeyEntry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001368 mask = DK_MASK(mp->ma_keys);
1369 while (i <= mask && *value_ptr == NULL) {
1370 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1371 i++;
1372 }
1373 if (i > mask)
1374 return -1;
1375 if (pvalue)
1376 *pvalue = *value_ptr;
1377 return i;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001378}
1379
Tim Peters080c88b2003-02-15 03:01:11 +00001380/*
1381 * Iterate over a dict. Use like so:
1382 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001383 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001384 * PyObject *key, *value;
1385 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001386 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +00001387 * Refer to borrowed references in key and value.
1388 * }
1389 *
1390 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001391 * mutates the dict. One exception: it is safe if the loop merely changes
1392 * the values associated with the keys (but doesn't insert new keys or
1393 * delete keys), via PyDict_SetItem().
1394 */
Guido van Rossum25831651993-05-19 14:50:45 +00001395int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001396PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001397{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001398 PyDictObject *mp;
1399 Py_ssize_t i = dict_next(op, *ppos, pvalue);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 if (i < 0)
1401 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001402 mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 *ppos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 if (pkey)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001405 *pkey = mp->ma_keys->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001407}
1408
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001409/* Internal version of PyDict_Next that returns a hash value in addition
1410 * to the key and value.
1411 */
Thomas Wouterscf297e42007-02-23 15:07:44 +00001412int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001413_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1414 PyObject **pvalue, Py_hash_t *phash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00001415{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001416 PyDictObject *mp;
1417 Py_ssize_t i = dict_next(op, *ppos, pvalue);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 if (i < 0)
1419 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001420 mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 *ppos = i+1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001422 *phash = mp->ma_keys->dk_entries[i].me_hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 if (pkey)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001424 *pkey = mp->ma_keys->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 return 1;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001426}
1427
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001428/* Methods */
1429
1430static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001431dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001432{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001433 PyObject **values = mp->ma_values;
1434 PyDictKeysObject *keys = mp->ma_keys;
1435 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 PyObject_GC_UnTrack(mp);
1437 Py_TRASHCAN_SAFE_BEGIN(mp)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001438 if (values != NULL) {
1439 if (values != empty_values) {
1440 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
1441 Py_XDECREF(values[i]);
1442 }
1443 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001445 DK_DECREF(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02001447 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001448 assert(keys->dk_refcnt == 1);
1449 DK_DECREF(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001450 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001451 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1452 free_list[numfree++] = mp;
1453 else
1454 Py_TYPE(mp)->tp_free((PyObject *)mp);
1455 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001456}
1457
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001458
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001459static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001460dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001461{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001463 PyObject *key = NULL, *value = NULL;
1464 _PyUnicodeWriter writer;
1465 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00001466
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001467 i = Py_ReprEnter((PyObject *)mp);
1468 if (i != 0) {
1469 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
1470 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001473 Py_ReprLeave((PyObject *)mp);
1474 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475 }
Tim Petersa7259592001-06-16 05:11:17 +00001476
Victor Stinnerf91929b2013-11-19 13:07:38 +01001477 _PyUnicodeWriter_Init(&writer);
1478 writer.overallocate = 1;
1479 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
1480 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00001481
Victor Stinnerf91929b2013-11-19 13:07:38 +01001482 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
1483 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 /* Do repr() on each key+value pair, and insert ": " between them.
1486 Note that repr may mutate the dict. */
1487 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001488 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001490 PyObject *s;
1491 int res;
1492
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001493 /* Prevent repr from deleting key or value during key format. */
1494 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001495 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02001496
Victor Stinnerf91929b2013-11-19 13:07:38 +01001497 if (!first) {
1498 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
1499 goto error;
1500 }
1501 first = 0;
1502
1503 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01001505 goto error;
1506 res = _PyUnicodeWriter_WriteStr(&writer, s);
1507 Py_DECREF(s);
1508 if (res < 0)
1509 goto error;
1510
1511 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
1512 goto error;
1513
1514 s = PyObject_Repr(value);
1515 if (s == NULL)
1516 goto error;
1517 res = _PyUnicodeWriter_WriteStr(&writer, s);
1518 Py_DECREF(s);
1519 if (res < 0)
1520 goto error;
1521
1522 Py_CLEAR(key);
1523 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 }
Tim Petersa7259592001-06-16 05:11:17 +00001525
Victor Stinnerf91929b2013-11-19 13:07:38 +01001526 writer.overallocate = 0;
1527 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
1528 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01001531
1532 return _PyUnicodeWriter_Finish(&writer);
1533
1534error:
1535 Py_ReprLeave((PyObject *)mp);
1536 _PyUnicodeWriter_Dealloc(&writer);
1537 Py_XDECREF(key);
1538 Py_XDECREF(value);
1539 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001540}
1541
Martin v. Löwis18e16552006-02-15 17:27:45 +00001542static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001543dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001544{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001545 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001546}
1547
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001548static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001549dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001550{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 PyObject *v;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001552 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001553 PyDictKeyEntry *ep;
1554 PyObject **value_addr;
1555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001557 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 hash = PyObject_Hash(key);
1559 if (hash == -1)
1560 return NULL;
1561 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001562 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 if (ep == NULL)
1564 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001565 v = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 if (v == NULL) {
1567 if (!PyDict_CheckExact(mp)) {
1568 /* Look up __missing__ method if we're a subclass. */
1569 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05001570 _Py_IDENTIFIER(__missing__);
1571 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 if (missing != NULL) {
1573 res = PyObject_CallFunctionObjArgs(missing,
1574 key, NULL);
1575 Py_DECREF(missing);
1576 return res;
1577 }
1578 else if (PyErr_Occurred())
1579 return NULL;
1580 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07001581 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 return NULL;
1583 }
1584 else
1585 Py_INCREF(v);
1586 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001587}
1588
1589static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001590dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001591{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592 if (w == NULL)
1593 return PyDict_DelItem((PyObject *)mp, v);
1594 else
1595 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001596}
1597
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001598static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 (lenfunc)dict_length, /*mp_length*/
1600 (binaryfunc)dict_subscript, /*mp_subscript*/
1601 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001602};
1603
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001604static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001605dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001606{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001607 PyObject *v;
1608 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001609 PyDictKeyEntry *ep;
1610 Py_ssize_t size, n, offset;
1611 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001612
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001613 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 n = mp->ma_used;
1615 v = PyList_New(n);
1616 if (v == NULL)
1617 return NULL;
1618 if (n != mp->ma_used) {
1619 /* Durnit. The allocations caused the dict to resize.
1620 * Just start over, this shouldn't normally happen.
1621 */
1622 Py_DECREF(v);
1623 goto again;
1624 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001625 ep = &mp->ma_keys->dk_entries[0];
1626 size = DK_SIZE(mp->ma_keys);
1627 if (mp->ma_values) {
1628 value_ptr = mp->ma_values;
1629 offset = sizeof(PyObject *);
1630 }
1631 else {
1632 value_ptr = &ep[0].me_value;
1633 offset = sizeof(PyDictKeyEntry);
1634 }
1635 for (i = 0, j = 0; i < size; i++) {
1636 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001637 PyObject *key = ep[i].me_key;
1638 Py_INCREF(key);
1639 PyList_SET_ITEM(v, j, key);
1640 j++;
1641 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001642 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001643 }
1644 assert(j == n);
1645 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001646}
1647
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001648static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001649dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001650{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001651 PyObject *v;
1652 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001653 Py_ssize_t size, n, offset;
1654 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001655
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001656 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 n = mp->ma_used;
1658 v = PyList_New(n);
1659 if (v == NULL)
1660 return NULL;
1661 if (n != mp->ma_used) {
1662 /* Durnit. The allocations caused the dict to resize.
1663 * Just start over, this shouldn't normally happen.
1664 */
1665 Py_DECREF(v);
1666 goto again;
1667 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001668 size = DK_SIZE(mp->ma_keys);
1669 if (mp->ma_values) {
1670 value_ptr = mp->ma_values;
1671 offset = sizeof(PyObject *);
1672 }
1673 else {
1674 value_ptr = &mp->ma_keys->dk_entries[0].me_value;
1675 offset = sizeof(PyDictKeyEntry);
1676 }
1677 for (i = 0, j = 0; i < size; i++) {
1678 PyObject *value = *value_ptr;
1679 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1680 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001681 Py_INCREF(value);
1682 PyList_SET_ITEM(v, j, value);
1683 j++;
1684 }
1685 }
1686 assert(j == n);
1687 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001688}
1689
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001690static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001691dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001692{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001693 PyObject *v;
1694 Py_ssize_t i, j, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001695 Py_ssize_t size, offset;
1696 PyObject *item, *key;
1697 PyDictKeyEntry *ep;
1698 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 /* Preallocate the list of tuples, to avoid allocations during
1701 * the loop over the items, which could trigger GC, which
1702 * could resize the dict. :-(
1703 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001704 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 n = mp->ma_used;
1706 v = PyList_New(n);
1707 if (v == NULL)
1708 return NULL;
1709 for (i = 0; i < n; i++) {
1710 item = PyTuple_New(2);
1711 if (item == NULL) {
1712 Py_DECREF(v);
1713 return NULL;
1714 }
1715 PyList_SET_ITEM(v, i, item);
1716 }
1717 if (n != mp->ma_used) {
1718 /* Durnit. The allocations caused the dict to resize.
1719 * Just start over, this shouldn't normally happen.
1720 */
1721 Py_DECREF(v);
1722 goto again;
1723 }
1724 /* Nothing we do below makes any function calls. */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001725 ep = mp->ma_keys->dk_entries;
1726 size = DK_SIZE(mp->ma_keys);
1727 if (mp->ma_values) {
1728 value_ptr = mp->ma_values;
1729 offset = sizeof(PyObject *);
1730 }
1731 else {
1732 value_ptr = &ep[0].me_value;
1733 offset = sizeof(PyDictKeyEntry);
1734 }
1735 for (i = 0, j = 0; i < size; i++) {
1736 PyObject *value = *value_ptr;
1737 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1738 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001739 key = ep[i].me_key;
1740 item = PyList_GET_ITEM(v, j);
1741 Py_INCREF(key);
1742 PyTuple_SET_ITEM(item, 0, key);
1743 Py_INCREF(value);
1744 PyTuple_SET_ITEM(item, 1, value);
1745 j++;
1746 }
1747 }
1748 assert(j == n);
1749 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001750}
1751
Larry Hastings5c661892014-01-24 06:17:25 -08001752/*[clinic input]
1753@classmethod
1754dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08001755 iterable: object
1756 value: object=None
1757 /
1758
1759Returns a new dict with keys from iterable and values equal to value.
1760[clinic start generated code]*/
1761
Larry Hastings5c661892014-01-24 06:17:25 -08001762static PyObject *
1763dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001764/*[clinic end generated code: output=8fb98e4b10384999 input=b85a667f9bf4669d]*/
Larry Hastings5c661892014-01-24 06:17:25 -08001765{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766 PyObject *it; /* iter(seq) */
1767 PyObject *key;
1768 PyObject *d;
1769 int status;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001770
Larry Hastings5c661892014-01-24 06:17:25 -08001771 d = PyObject_CallObject((PyObject *)type, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 if (d == NULL)
1773 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001774
Benjamin Peterson9892f522012-10-31 14:22:12 -04001775 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
Larry Hastings5c661892014-01-24 06:17:25 -08001776 if (PyDict_CheckExact(iterable)) {
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001777 PyDictObject *mp = (PyDictObject *)d;
1778 PyObject *oldvalue;
1779 Py_ssize_t pos = 0;
1780 PyObject *key;
1781 Py_hash_t hash;
Guido van Rossum58da9312007-11-10 23:39:45 +00001782
Larry Hastings5c661892014-01-24 06:17:25 -08001783 if (dictresize(mp, Py_SIZE(iterable))) {
Petri Lehtinena94200e2011-10-24 21:12:58 +03001784 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001786 }
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001787
Larry Hastings5c661892014-01-24 06:17:25 -08001788 while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001789 if (insertdict(mp, key, hash, value)) {
1790 Py_DECREF(d);
1791 return NULL;
1792 }
1793 }
1794 return d;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001795 }
Larry Hastings5c661892014-01-24 06:17:25 -08001796 if (PyAnySet_CheckExact(iterable)) {
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001797 PyDictObject *mp = (PyDictObject *)d;
1798 Py_ssize_t pos = 0;
1799 PyObject *key;
1800 Py_hash_t hash;
Guido van Rossum58da9312007-11-10 23:39:45 +00001801
Larry Hastings5c661892014-01-24 06:17:25 -08001802 if (dictresize(mp, PySet_GET_SIZE(iterable))) {
Petri Lehtinena94200e2011-10-24 21:12:58 +03001803 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001804 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001805 }
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001806
Larry Hastings5c661892014-01-24 06:17:25 -08001807 while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001808 if (insertdict(mp, key, hash, value)) {
1809 Py_DECREF(d);
1810 return NULL;
1811 }
1812 }
1813 return d;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001814 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001815 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001816
Larry Hastings5c661892014-01-24 06:17:25 -08001817 it = PyObject_GetIter(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 if (it == NULL){
1819 Py_DECREF(d);
1820 return NULL;
1821 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001823 if (PyDict_CheckExact(d)) {
1824 while ((key = PyIter_Next(it)) != NULL) {
1825 status = PyDict_SetItem(d, key, value);
1826 Py_DECREF(key);
1827 if (status < 0)
1828 goto Fail;
1829 }
1830 } else {
1831 while ((key = PyIter_Next(it)) != NULL) {
1832 status = PyObject_SetItem(d, key, value);
1833 Py_DECREF(key);
1834 if (status < 0)
1835 goto Fail;
1836 }
1837 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001839 if (PyErr_Occurred())
1840 goto Fail;
1841 Py_DECREF(it);
1842 return d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001843
1844Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001845 Py_DECREF(it);
1846 Py_DECREF(d);
1847 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001848}
1849
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001850static int
1851dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001852{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853 PyObject *arg = NULL;
1854 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1857 result = -1;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001860 _Py_IDENTIFIER(keys);
1861 if (_PyObject_HasAttrId(arg, &PyId_keys))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862 result = PyDict_Merge(self, arg, 1);
1863 else
1864 result = PyDict_MergeFromSeq2(self, arg, 1);
1865 }
1866 if (result == 0 && kwds != NULL) {
1867 if (PyArg_ValidateKeywordArguments(kwds))
1868 result = PyDict_Merge(self, kwds, 1);
1869 else
1870 result = -1;
1871 }
1872 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001873}
1874
1875static PyObject *
1876dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1877{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001878 if (dict_update_common(self, args, kwds, "update") != -1)
1879 Py_RETURN_NONE;
1880 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001881}
1882
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001883/* Update unconditionally replaces existing items.
1884 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001885 otherwise it leaves existing items unchanged.
1886
1887 PyDict_{Update,Merge} update/merge from a mapping object.
1888
Tim Petersf582b822001-12-11 18:51:08 +00001889 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001890 producing iterable objects of length 2.
1891*/
1892
Tim Petersf582b822001-12-11 18:51:08 +00001893int
Tim Peters1fc240e2001-10-26 05:06:50 +00001894PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1895{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 PyObject *it; /* iter(seq2) */
1897 Py_ssize_t i; /* index into seq2 of current element */
1898 PyObject *item; /* seq2[i] */
1899 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00001900
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001901 assert(d != NULL);
1902 assert(PyDict_Check(d));
1903 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00001904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 it = PyObject_GetIter(seq2);
1906 if (it == NULL)
1907 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909 for (i = 0; ; ++i) {
1910 PyObject *key, *value;
1911 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001912
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001913 fast = NULL;
1914 item = PyIter_Next(it);
1915 if (item == NULL) {
1916 if (PyErr_Occurred())
1917 goto Fail;
1918 break;
1919 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 /* Convert item to sequence, and verify length 2. */
1922 fast = PySequence_Fast(item, "");
1923 if (fast == NULL) {
1924 if (PyErr_ExceptionMatches(PyExc_TypeError))
1925 PyErr_Format(PyExc_TypeError,
1926 "cannot convert dictionary update "
1927 "sequence element #%zd to a sequence",
1928 i);
1929 goto Fail;
1930 }
1931 n = PySequence_Fast_GET_SIZE(fast);
1932 if (n != 2) {
1933 PyErr_Format(PyExc_ValueError,
1934 "dictionary update sequence element #%zd "
1935 "has length %zd; 2 is required",
1936 i, n);
1937 goto Fail;
1938 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 /* Update/merge with this (key, value) pair. */
1941 key = PySequence_Fast_GET_ITEM(fast, 0);
1942 value = PySequence_Fast_GET_ITEM(fast, 1);
1943 if (override || PyDict_GetItem(d, key) == NULL) {
1944 int status = PyDict_SetItem(d, key, value);
1945 if (status < 0)
1946 goto Fail;
1947 }
1948 Py_DECREF(fast);
1949 Py_DECREF(item);
1950 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 i = 0;
1953 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00001954Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 Py_XDECREF(item);
1956 Py_XDECREF(fast);
1957 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001958Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959 Py_DECREF(it);
1960 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001961}
1962
Tim Peters6d6c1a32001-08-02 04:15:00 +00001963int
1964PyDict_Update(PyObject *a, PyObject *b)
1965{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001966 return PyDict_Merge(a, b, 1);
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001967}
1968
1969int
1970PyDict_Merge(PyObject *a, PyObject *b, int override)
1971{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001972 PyDictObject *mp, *other;
1973 Py_ssize_t i, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001974 PyDictKeyEntry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001976 /* We accept for the argument either a concrete dictionary object,
1977 * or an abstract "mapping" object. For the former, we can do
1978 * things quite efficiently. For the latter, we only require that
1979 * PyMapping_Keys() and PyObject_GetItem() be supported.
1980 */
1981 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1982 PyErr_BadInternalCall();
1983 return -1;
1984 }
1985 mp = (PyDictObject*)a;
1986 if (PyDict_Check(b)) {
1987 other = (PyDictObject*)b;
1988 if (other == mp || other->ma_used == 0)
1989 /* a.update(a) or a.update({}); nothing to do */
1990 return 0;
1991 if (mp->ma_used == 0)
1992 /* Since the target dict is empty, PyDict_GetItem()
1993 * always returns NULL. Setting override to 1
1994 * skips the unnecessary test.
1995 */
1996 override = 1;
1997 /* Do one big resize at the start, rather than
1998 * incrementally resizing as we insert new items. Expect
1999 * that there will be no (or few) overlapping keys.
2000 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002001 if (mp->ma_keys->dk_usable * 3 < other->ma_used * 2)
2002 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002004 for (i = 0, n = DK_SIZE(other->ma_keys); i < n; i++) {
2005 PyObject *value;
2006 entry = &other->ma_keys->dk_entries[i];
2007 if (other->ma_values)
2008 value = other->ma_values[i];
2009 else
2010 value = entry->me_value;
2011
2012 if (value != NULL &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002013 (override ||
2014 PyDict_GetItem(a, entry->me_key) == NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015 if (insertdict(mp, entry->me_key,
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002016 entry->me_hash,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002017 value) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002018 return -1;
2019 }
2020 }
2021 }
2022 else {
2023 /* Do it the generic, slower way */
2024 PyObject *keys = PyMapping_Keys(b);
2025 PyObject *iter;
2026 PyObject *key, *value;
2027 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002029 if (keys == NULL)
2030 /* Docstring says this is equivalent to E.keys() so
2031 * if E doesn't have a .keys() method we want
2032 * AttributeError to percolate up. Might as well
2033 * do the same for any other error.
2034 */
2035 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002036
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002037 iter = PyObject_GetIter(keys);
2038 Py_DECREF(keys);
2039 if (iter == NULL)
2040 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002042 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
2043 if (!override && PyDict_GetItem(a, key) != NULL) {
2044 Py_DECREF(key);
2045 continue;
2046 }
2047 value = PyObject_GetItem(b, key);
2048 if (value == NULL) {
2049 Py_DECREF(iter);
2050 Py_DECREF(key);
2051 return -1;
2052 }
2053 status = PyDict_SetItem(a, key, value);
2054 Py_DECREF(key);
2055 Py_DECREF(value);
2056 if (status < 0) {
2057 Py_DECREF(iter);
2058 return -1;
2059 }
2060 }
2061 Py_DECREF(iter);
2062 if (PyErr_Occurred())
2063 /* Iterator completed, via error */
2064 return -1;
2065 }
2066 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002067}
2068
2069static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002070dict_copy(PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002071{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002072 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002073}
2074
2075PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002076PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002077{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002078 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002079 PyDictObject *mp;
2080 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002081
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002082 if (o == NULL || !PyDict_Check(o)) {
2083 PyErr_BadInternalCall();
2084 return NULL;
2085 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002086 mp = (PyDictObject *)o;
2087 if (_PyDict_HasSplitTable(mp)) {
2088 PyDictObject *split_copy;
2089 PyObject **newvalues = new_values(DK_SIZE(mp->ma_keys));
2090 if (newvalues == NULL)
2091 return PyErr_NoMemory();
2092 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2093 if (split_copy == NULL) {
2094 free_values(newvalues);
2095 return NULL;
2096 }
2097 split_copy->ma_values = newvalues;
2098 split_copy->ma_keys = mp->ma_keys;
2099 split_copy->ma_used = mp->ma_used;
2100 DK_INCREF(mp->ma_keys);
2101 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2102 PyObject *value = mp->ma_values[i];
2103 Py_XINCREF(value);
2104 split_copy->ma_values[i] = value;
2105 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002106 if (_PyObject_GC_IS_TRACKED(mp))
2107 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002108 return (PyObject *)split_copy;
2109 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002110 copy = PyDict_New();
2111 if (copy == NULL)
2112 return NULL;
2113 if (PyDict_Merge(copy, o, 1) == 0)
2114 return copy;
2115 Py_DECREF(copy);
2116 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002117}
2118
Martin v. Löwis18e16552006-02-15 17:27:45 +00002119Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002120PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002121{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122 if (mp == NULL || !PyDict_Check(mp)) {
2123 PyErr_BadInternalCall();
2124 return -1;
2125 }
2126 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002127}
2128
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002129PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002130PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002131{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002132 if (mp == NULL || !PyDict_Check(mp)) {
2133 PyErr_BadInternalCall();
2134 return NULL;
2135 }
2136 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002137}
2138
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002139PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002140PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002141{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002142 if (mp == NULL || !PyDict_Check(mp)) {
2143 PyErr_BadInternalCall();
2144 return NULL;
2145 }
2146 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002147}
2148
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002149PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002150PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002151{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002152 if (mp == NULL || !PyDict_Check(mp)) {
2153 PyErr_BadInternalCall();
2154 return NULL;
2155 }
2156 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002157}
2158
Tim Peterse63415e2001-05-08 04:38:29 +00002159/* Return 1 if dicts equal, 0 if not, -1 if error.
2160 * Gets out as soon as any difference is detected.
2161 * Uses only Py_EQ comparison.
2162 */
2163static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002164dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002165{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002166 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002168 if (a->ma_used != b->ma_used)
2169 /* can't be equal if # of entries differ */
2170 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002171 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002172 for (i = 0; i < DK_SIZE(a->ma_keys); i++) {
2173 PyDictKeyEntry *ep = &a->ma_keys->dk_entries[i];
2174 PyObject *aval;
2175 if (a->ma_values)
2176 aval = a->ma_values[i];
2177 else
2178 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002179 if (aval != NULL) {
2180 int cmp;
2181 PyObject *bval;
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002182 PyObject **vaddr;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002183 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002184 /* temporarily bump aval's refcount to ensure it stays
2185 alive until we're done with it */
2186 Py_INCREF(aval);
2187 /* ditto for key */
2188 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002189 /* reuse the known hash value */
2190 if ((b->ma_keys->dk_lookup)(b, key, ep->me_hash, &vaddr) == NULL)
2191 bval = NULL;
2192 else
2193 bval = *vaddr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002194 Py_DECREF(key);
2195 if (bval == NULL) {
2196 Py_DECREF(aval);
2197 if (PyErr_Occurred())
2198 return -1;
2199 return 0;
2200 }
2201 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
2202 Py_DECREF(aval);
2203 if (cmp <= 0) /* error or not equal */
2204 return cmp;
2205 }
2206 }
2207 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002208}
Tim Peterse63415e2001-05-08 04:38:29 +00002209
2210static PyObject *
2211dict_richcompare(PyObject *v, PyObject *w, int op)
2212{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002213 int cmp;
2214 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002216 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2217 res = Py_NotImplemented;
2218 }
2219 else if (op == Py_EQ || op == Py_NE) {
2220 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2221 if (cmp < 0)
2222 return NULL;
2223 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2224 }
2225 else
2226 res = Py_NotImplemented;
2227 Py_INCREF(res);
2228 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002229}
Tim Peterse63415e2001-05-08 04:38:29 +00002230
Larry Hastings61272b72014-01-07 12:41:53 -08002231/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002232
2233@coexist
2234dict.__contains__
2235
2236 key: object
2237 /
2238
Meador Ingee02de8c2014-01-14 16:48:31 -06002239True if D has a key k, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002240[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002241
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002242static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002243dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002244/*[clinic end generated code: output=a3d03db709ed6e6b input=b852b2a19b51ab24]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002245{
Larry Hastingsc2047262014-01-25 20:43:29 -08002246 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002247 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002248 PyDictKeyEntry *ep;
2249 PyObject **value_addr;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002251 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002252 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002253 hash = PyObject_Hash(key);
2254 if (hash == -1)
2255 return NULL;
2256 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002257 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002258 if (ep == NULL)
2259 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002260 return PyBool_FromLong(*value_addr != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002261}
2262
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002263static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002264dict_get(PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00002265{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002266 PyObject *key;
2267 PyObject *failobj = Py_None;
2268 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002269 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002270 PyDictKeyEntry *ep;
2271 PyObject **value_addr;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002273 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
2274 return NULL;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002276 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002277 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 hash = PyObject_Hash(key);
2279 if (hash == -1)
2280 return NULL;
2281 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002282 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002283 if (ep == NULL)
2284 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002285 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002286 if (val == NULL)
2287 val = failobj;
2288 Py_INCREF(val);
2289 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002290}
2291
Benjamin Peterson00e98862013-03-07 22:16:29 -05002292PyObject *
2293PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002294{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002295 PyDictObject *mp = (PyDictObject *)d;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002296 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002297 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002298 PyDictKeyEntry *ep;
2299 PyObject **value_addr;
Guido van Rossum164452c2000-08-08 16:12:54 +00002300
Benjamin Peterson00e98862013-03-07 22:16:29 -05002301 if (!PyDict_Check(d)) {
2302 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002303 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002304 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002305 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002306 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 hash = PyObject_Hash(key);
2308 if (hash == -1)
2309 return NULL;
2310 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002311 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002312 if (ep == NULL)
2313 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002314 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002315 if (val == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002316 if (mp->ma_keys->dk_usable <= 0) {
2317 /* Need to resize. */
2318 if (insertion_resize(mp) < 0)
2319 return NULL;
2320 ep = find_empty_slot(mp, key, hash, &value_addr);
2321 }
Benjamin Peterson00e98862013-03-07 22:16:29 -05002322 Py_INCREF(defaultobj);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002323 Py_INCREF(key);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002324 MAINTAIN_TRACKING(mp, key, defaultobj);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002325 ep->me_key = key;
2326 ep->me_hash = hash;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002327 *value_addr = defaultobj;
2328 val = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002329 mp->ma_keys->dk_usable--;
2330 mp->ma_used++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002331 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002332 return val;
Guido van Rossum164452c2000-08-08 16:12:54 +00002333}
2334
Benjamin Peterson00e98862013-03-07 22:16:29 -05002335static PyObject *
2336dict_setdefault(PyDictObject *mp, PyObject *args)
2337{
2338 PyObject *key, *val;
2339 PyObject *defaultobj = Py_None;
2340
2341 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &defaultobj))
2342 return NULL;
2343
Benjamin Peterson55898502013-03-08 08:36:49 -05002344 val = PyDict_SetDefault((PyObject *)mp, key, defaultobj);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002345 Py_XINCREF(val);
2346 return val;
2347}
Guido van Rossum164452c2000-08-08 16:12:54 +00002348
2349static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002350dict_clear(PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002351{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002352 PyDict_Clear((PyObject *)mp);
2353 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002354}
2355
Guido van Rossumba6ab842000-12-12 22:02:18 +00002356static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002357dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002358{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002359 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360 PyObject *old_value, *old_key;
2361 PyObject *key, *deflt = NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002362 PyDictKeyEntry *ep;
2363 PyObject **value_addr;
Guido van Rossume027d982002-04-12 15:11:59 +00002364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002365 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2366 return NULL;
2367 if (mp->ma_used == 0) {
2368 if (deflt) {
2369 Py_INCREF(deflt);
2370 return deflt;
2371 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07002372 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002373 return NULL;
2374 }
2375 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002376 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002377 hash = PyObject_Hash(key);
2378 if (hash == -1)
2379 return NULL;
2380 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002381 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002382 if (ep == NULL)
2383 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002384 old_value = *value_addr;
2385 if (old_value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386 if (deflt) {
2387 Py_INCREF(deflt);
2388 return deflt;
2389 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07002390 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 return NULL;
2392 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002393 *value_addr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002395 if (!_PyDict_HasSplitTable(mp)) {
2396 ENSURE_ALLOWS_DELETIONS(mp);
2397 old_key = ep->me_key;
2398 Py_INCREF(dummy);
2399 ep->me_key = dummy;
2400 Py_DECREF(old_key);
2401 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002402 return old_value;
Guido van Rossume027d982002-04-12 15:11:59 +00002403}
2404
2405static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002406dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00002407{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002408 Py_hash_t i = 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002409 PyDictKeyEntry *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002410 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002411
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002413 /* Allocate the result tuple before checking the size. Believe it
2414 * or not, this allocation could trigger a garbage collection which
2415 * could empty the dict, so if we checked the size first and that
2416 * happened, the result would be an infinite loop (searching for an
2417 * entry that no longer exists). Note that the usual popitem()
2418 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2419 * tuple away if the dict *is* empty isn't a significant
2420 * inefficiency -- possible, but unlikely in practice.
2421 */
2422 res = PyTuple_New(2);
2423 if (res == NULL)
2424 return NULL;
2425 if (mp->ma_used == 0) {
2426 Py_DECREF(res);
2427 PyErr_SetString(PyExc_KeyError,
2428 "popitem(): dictionary is empty");
2429 return NULL;
2430 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002431 /* Convert split table to combined table */
2432 if (mp->ma_keys->dk_lookup == lookdict_split) {
2433 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
2434 Py_DECREF(res);
2435 return NULL;
2436 }
2437 }
2438 ENSURE_ALLOWS_DELETIONS(mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002439 /* Set ep to "the first" dict entry with a value. We abuse the hash
2440 * field of slot 0 to hold a search finger:
2441 * If slot 0 has a value, use slot 0.
2442 * Else slot 0 is being used to hold a search finger,
2443 * and we use its hash value as the first index to look.
2444 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002445 ep = &mp->ma_keys->dk_entries[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002446 if (ep->me_value == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002447 Py_ssize_t mask = DK_MASK(mp->ma_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002448 i = ep->me_hash;
2449 /* The hash field may be a real hash value, or it may be a
2450 * legit search finger, or it may be a once-legit search
2451 * finger that's out of bounds now because it wrapped around
2452 * or the table shrunk -- simply make sure it's in bounds now.
2453 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002454 if (i > mask || i < 1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002455 i = 1; /* skip slot 0 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002456 while ((ep = &mp->ma_keys->dk_entries[i])->me_value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002457 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002458 if (i > mask)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002459 i = 1;
2460 }
2461 }
2462 PyTuple_SET_ITEM(res, 0, ep->me_key);
2463 PyTuple_SET_ITEM(res, 1, ep->me_value);
2464 Py_INCREF(dummy);
2465 ep->me_key = dummy;
2466 ep->me_value = NULL;
2467 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002468 assert(mp->ma_keys->dk_entries[0].me_value == NULL);
2469 mp->ma_keys->dk_entries[0].me_hash = i + 1; /* next place to start */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002470 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002471}
2472
Jeremy Hylton8caad492000-06-23 14:18:11 +00002473static int
2474dict_traverse(PyObject *op, visitproc visit, void *arg)
2475{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002476 Py_ssize_t i, n;
2477 PyDictObject *mp = (PyDictObject *)op;
2478 if (mp->ma_keys->dk_lookup == lookdict) {
2479 for (i = 0; i < DK_SIZE(mp->ma_keys); i++) {
2480 if (mp->ma_keys->dk_entries[i].me_value != NULL) {
2481 Py_VISIT(mp->ma_keys->dk_entries[i].me_value);
2482 Py_VISIT(mp->ma_keys->dk_entries[i].me_key);
2483 }
2484 }
2485 } else {
2486 if (mp->ma_values != NULL) {
2487 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2488 Py_VISIT(mp->ma_values[i]);
2489 }
2490 }
2491 else {
2492 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2493 Py_VISIT(mp->ma_keys->dk_entries[i].me_value);
2494 }
2495 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002496 }
2497 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002498}
2499
2500static int
2501dict_tp_clear(PyObject *op)
2502{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002503 PyDict_Clear(op);
2504 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002505}
2506
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002507static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002508
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002509static PyObject *
2510dict_sizeof(PyDictObject *mp)
2511{
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002512 Py_ssize_t size, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002513
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002514 size = DK_SIZE(mp->ma_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002515 res = sizeof(PyDictObject);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002516 if (mp->ma_values)
2517 res += size * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002518 /* If the dictionary is split, the keys portion is accounted-for
2519 in the type object. */
2520 if (mp->ma_keys->dk_refcnt == 1)
2521 res += sizeof(PyDictKeysObject) + (size-1) * sizeof(PyDictKeyEntry);
2522 return PyLong_FromSsize_t(res);
2523}
2524
2525Py_ssize_t
2526_PyDict_KeysSize(PyDictKeysObject *keys)
2527{
2528 return sizeof(PyDictKeysObject) + (DK_SIZE(keys)-1) * sizeof(PyDictKeyEntry);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002529}
2530
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002531PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
2532
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002533PyDoc_STRVAR(sizeof__doc__,
2534"D.__sizeof__() -> size of D in memory, in bytes");
2535
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002536PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002537"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002538
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002539PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002540"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 +00002541
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002542PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002543"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002544If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00002545
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002546PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00002547"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000025482-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002549
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002550PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04002551"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
2552If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
2553If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
2554In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00002555
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002556PyDoc_STRVAR(clear__doc__,
2557"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002558
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002559PyDoc_STRVAR(copy__doc__,
2560"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00002561
Guido van Rossumb90c8482007-02-10 01:11:45 +00002562/* Forward */
2563static PyObject *dictkeys_new(PyObject *);
2564static PyObject *dictitems_new(PyObject *);
2565static PyObject *dictvalues_new(PyObject *);
2566
Guido van Rossum45c85d12007-07-27 16:31:40 +00002567PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002568 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002569PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002570 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002571PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002572 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00002573
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002574static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07002575 DICT___CONTAINS___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002576 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
2577 getitem__doc__},
2578 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
2579 sizeof__doc__},
2580 {"get", (PyCFunction)dict_get, METH_VARARGS,
2581 get__doc__},
2582 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
2583 setdefault_doc__},
2584 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
2585 pop__doc__},
2586 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
2587 popitem__doc__},
2588 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
2589 keys__doc__},
2590 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
2591 items__doc__},
2592 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
2593 values__doc__},
2594 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
2595 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08002596 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002597 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
2598 clear__doc__},
2599 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
2600 copy__doc__},
2601 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002602};
2603
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002604/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002605int
2606PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002607{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002608 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002609 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002610 PyDictKeyEntry *ep;
2611 PyObject **value_addr;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002613 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002614 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002615 hash = PyObject_Hash(key);
2616 if (hash == -1)
2617 return -1;
2618 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002619 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
2620 return (ep == NULL) ? -1 : (*value_addr != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002621}
2622
Thomas Wouterscf297e42007-02-23 15:07:44 +00002623/* Internal version of PyDict_Contains used when the hash value is already known */
2624int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002625_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00002626{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002627 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002628 PyDictKeyEntry *ep;
2629 PyObject **value_addr;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002630
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002631 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
2632 return (ep == NULL) ? -1 : (*value_addr != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00002633}
2634
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002635/* Hack to implement "key in dict" */
2636static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002637 0, /* sq_length */
2638 0, /* sq_concat */
2639 0, /* sq_repeat */
2640 0, /* sq_item */
2641 0, /* sq_slice */
2642 0, /* sq_ass_item */
2643 0, /* sq_ass_slice */
2644 PyDict_Contains, /* sq_contains */
2645 0, /* sq_inplace_concat */
2646 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002647};
2648
Guido van Rossum09e563a2001-05-01 12:10:21 +00002649static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002650dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2651{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002652 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02002653 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002655 assert(type != NULL && type->tp_alloc != NULL);
2656 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02002657 if (self == NULL)
2658 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02002659 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02002660
Victor Stinnera9f61a52013-07-16 22:17:26 +02002661 /* The object has been implicitly tracked by tp_alloc */
2662 if (type == &PyDict_Type)
2663 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02002664
2665 d->ma_used = 0;
2666 d->ma_keys = new_keys_object(PyDict_MINSIZE_COMBINED);
2667 if (d->ma_keys == NULL) {
2668 Py_DECREF(self);
2669 return NULL;
2670 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002671 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002672}
2673
Tim Peters25786c02001-09-02 08:22:48 +00002674static int
2675dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2676{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002677 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002678}
2679
Tim Peters6d6c1a32001-08-02 04:15:00 +00002680static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002681dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002682{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002683 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002684}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002685
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002686PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002687"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002688"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002689" (key, value) pairs\n"
2690"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002691" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002692" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002693" d[k] = v\n"
2694"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2695" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002696
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002697PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002698 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2699 "dict",
2700 sizeof(PyDictObject),
2701 0,
2702 (destructor)dict_dealloc, /* tp_dealloc */
2703 0, /* tp_print */
2704 0, /* tp_getattr */
2705 0, /* tp_setattr */
2706 0, /* tp_reserved */
2707 (reprfunc)dict_repr, /* tp_repr */
2708 0, /* tp_as_number */
2709 &dict_as_sequence, /* tp_as_sequence */
2710 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002711 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002712 0, /* tp_call */
2713 0, /* tp_str */
2714 PyObject_GenericGetAttr, /* tp_getattro */
2715 0, /* tp_setattro */
2716 0, /* tp_as_buffer */
2717 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2718 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
2719 dictionary_doc, /* tp_doc */
2720 dict_traverse, /* tp_traverse */
2721 dict_tp_clear, /* tp_clear */
2722 dict_richcompare, /* tp_richcompare */
2723 0, /* tp_weaklistoffset */
2724 (getiterfunc)dict_iter, /* tp_iter */
2725 0, /* tp_iternext */
2726 mapp_methods, /* tp_methods */
2727 0, /* tp_members */
2728 0, /* tp_getset */
2729 0, /* tp_base */
2730 0, /* tp_dict */
2731 0, /* tp_descr_get */
2732 0, /* tp_descr_set */
2733 0, /* tp_dictoffset */
2734 dict_init, /* tp_init */
2735 PyType_GenericAlloc, /* tp_alloc */
2736 dict_new, /* tp_new */
2737 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002738};
2739
Victor Stinner3c1e4812012-03-26 22:10:51 +02002740PyObject *
2741_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
2742{
2743 PyObject *kv;
2744 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02002745 if (kv == NULL) {
2746 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002747 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02002748 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02002749 return PyDict_GetItem(dp, kv);
2750}
2751
Guido van Rossum3cca2451997-05-16 14:23:33 +00002752/* For backward compatibility with old dictionary interface */
2753
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002754PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002755PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002756{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 PyObject *kv, *rv;
2758 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02002759 if (kv == NULL) {
2760 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002761 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02002762 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002763 rv = PyDict_GetItem(v, kv);
2764 Py_DECREF(kv);
2765 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002766}
2767
2768int
Victor Stinner3c1e4812012-03-26 22:10:51 +02002769_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
2770{
2771 PyObject *kv;
2772 kv = _PyUnicode_FromId(key); /* borrowed */
2773 if (kv == NULL)
2774 return -1;
2775 return PyDict_SetItem(v, kv, item);
2776}
2777
2778int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002779PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002780{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002781 PyObject *kv;
2782 int err;
2783 kv = PyUnicode_FromString(key);
2784 if (kv == NULL)
2785 return -1;
2786 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
2787 err = PyDict_SetItem(v, kv, item);
2788 Py_DECREF(kv);
2789 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002790}
2791
2792int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01002793_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
2794{
2795 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
2796 if (kv == NULL)
2797 return -1;
2798 return PyDict_DelItem(v, kv);
2799}
2800
2801int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002802PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002803{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002804 PyObject *kv;
2805 int err;
2806 kv = PyUnicode_FromString(key);
2807 if (kv == NULL)
2808 return -1;
2809 err = PyDict_DelItem(v, kv);
2810 Py_DECREF(kv);
2811 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002812}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002813
Raymond Hettinger019a1482004-03-18 02:41:19 +00002814/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002815
2816typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002817 PyObject_HEAD
2818 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
2819 Py_ssize_t di_used;
2820 Py_ssize_t di_pos;
2821 PyObject* di_result; /* reusable result tuple for iteritems */
2822 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002823} dictiterobject;
2824
2825static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002826dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002827{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002828 dictiterobject *di;
2829 di = PyObject_GC_New(dictiterobject, itertype);
2830 if (di == NULL)
2831 return NULL;
2832 Py_INCREF(dict);
2833 di->di_dict = dict;
2834 di->di_used = dict->ma_used;
2835 di->di_pos = 0;
2836 di->len = dict->ma_used;
2837 if (itertype == &PyDictIterItem_Type) {
2838 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2839 if (di->di_result == NULL) {
2840 Py_DECREF(di);
2841 return NULL;
2842 }
2843 }
2844 else
2845 di->di_result = NULL;
2846 _PyObject_GC_TRACK(di);
2847 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002848}
2849
2850static void
2851dictiter_dealloc(dictiterobject *di)
2852{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002853 Py_XDECREF(di->di_dict);
2854 Py_XDECREF(di->di_result);
2855 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002856}
2857
2858static int
2859dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
2860{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002861 Py_VISIT(di->di_dict);
2862 Py_VISIT(di->di_result);
2863 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002864}
2865
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002866static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002867dictiter_len(dictiterobject *di)
2868{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002869 Py_ssize_t len = 0;
2870 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
2871 len = di->len;
2872 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002873}
2874
Guido van Rossumb90c8482007-02-10 01:11:45 +00002875PyDoc_STRVAR(length_hint_doc,
2876 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002877
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002878static PyObject *
2879dictiter_reduce(dictiterobject *di);
2880
2881PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
2882
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002883static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002884 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
2885 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002886 {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS,
2887 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002888 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002889};
2890
Raymond Hettinger019a1482004-03-18 02:41:19 +00002891static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002892{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002893 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002894 Py_ssize_t i, mask, offset;
2895 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002896 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002897 PyObject **value_ptr;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002899 if (d == NULL)
2900 return NULL;
2901 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002903 if (di->di_used != d->ma_used) {
2904 PyErr_SetString(PyExc_RuntimeError,
2905 "dictionary changed size during iteration");
2906 di->di_used = -1; /* Make this state sticky */
2907 return NULL;
2908 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002909
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002910 i = di->di_pos;
2911 if (i < 0)
2912 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002913 k = d->ma_keys;
2914 if (d->ma_values) {
2915 value_ptr = &d->ma_values[i];
2916 offset = sizeof(PyObject *);
2917 }
2918 else {
2919 value_ptr = &k->dk_entries[i].me_value;
2920 offset = sizeof(PyDictKeyEntry);
2921 }
2922 mask = DK_SIZE(k)-1;
2923 while (i <= mask && *value_ptr == NULL) {
2924 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002925 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002926 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002927 di->di_pos = i+1;
2928 if (i > mask)
2929 goto fail;
2930 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002931 key = k->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002932 Py_INCREF(key);
2933 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002934
2935fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002936 Py_DECREF(d);
2937 di->di_dict = NULL;
2938 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002939}
2940
Raymond Hettinger019a1482004-03-18 02:41:19 +00002941PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002942 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2943 "dict_keyiterator", /* tp_name */
2944 sizeof(dictiterobject), /* tp_basicsize */
2945 0, /* tp_itemsize */
2946 /* methods */
2947 (destructor)dictiter_dealloc, /* tp_dealloc */
2948 0, /* tp_print */
2949 0, /* tp_getattr */
2950 0, /* tp_setattr */
2951 0, /* tp_reserved */
2952 0, /* tp_repr */
2953 0, /* tp_as_number */
2954 0, /* tp_as_sequence */
2955 0, /* tp_as_mapping */
2956 0, /* tp_hash */
2957 0, /* tp_call */
2958 0, /* tp_str */
2959 PyObject_GenericGetAttr, /* tp_getattro */
2960 0, /* tp_setattro */
2961 0, /* tp_as_buffer */
2962 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2963 0, /* tp_doc */
2964 (traverseproc)dictiter_traverse, /* tp_traverse */
2965 0, /* tp_clear */
2966 0, /* tp_richcompare */
2967 0, /* tp_weaklistoffset */
2968 PyObject_SelfIter, /* tp_iter */
2969 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
2970 dictiter_methods, /* tp_methods */
2971 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002972};
2973
2974static PyObject *dictiter_iternextvalue(dictiterobject *di)
2975{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002976 PyObject *value;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002977 Py_ssize_t i, mask, offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002978 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002979 PyObject **value_ptr;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002981 if (d == NULL)
2982 return NULL;
2983 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00002984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002985 if (di->di_used != d->ma_used) {
2986 PyErr_SetString(PyExc_RuntimeError,
2987 "dictionary changed size during iteration");
2988 di->di_used = -1; /* Make this state sticky */
2989 return NULL;
2990 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002992 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002993 mask = DK_SIZE(d->ma_keys)-1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002994 if (i < 0 || i > mask)
2995 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002996 if (d->ma_values) {
2997 value_ptr = &d->ma_values[i];
2998 offset = sizeof(PyObject *);
2999 }
3000 else {
3001 value_ptr = &d->ma_keys->dk_entries[i].me_value;
3002 offset = sizeof(PyDictKeyEntry);
3003 }
3004 while (i <= mask && *value_ptr == NULL) {
3005 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003006 i++;
3007 if (i > mask)
3008 goto fail;
3009 }
3010 di->di_pos = i+1;
3011 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003012 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003013 Py_INCREF(value);
3014 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003015
3016fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003017 Py_DECREF(d);
3018 di->di_dict = NULL;
3019 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003020}
3021
3022PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003023 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3024 "dict_valueiterator", /* tp_name */
3025 sizeof(dictiterobject), /* tp_basicsize */
3026 0, /* tp_itemsize */
3027 /* methods */
3028 (destructor)dictiter_dealloc, /* tp_dealloc */
3029 0, /* tp_print */
3030 0, /* tp_getattr */
3031 0, /* tp_setattr */
3032 0, /* tp_reserved */
3033 0, /* tp_repr */
3034 0, /* tp_as_number */
3035 0, /* tp_as_sequence */
3036 0, /* tp_as_mapping */
3037 0, /* tp_hash */
3038 0, /* tp_call */
3039 0, /* tp_str */
3040 PyObject_GenericGetAttr, /* tp_getattro */
3041 0, /* tp_setattro */
3042 0, /* tp_as_buffer */
3043 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3044 0, /* tp_doc */
3045 (traverseproc)dictiter_traverse, /* tp_traverse */
3046 0, /* tp_clear */
3047 0, /* tp_richcompare */
3048 0, /* tp_weaklistoffset */
3049 PyObject_SelfIter, /* tp_iter */
3050 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3051 dictiter_methods, /* tp_methods */
3052 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003053};
3054
3055static PyObject *dictiter_iternextitem(dictiterobject *di)
3056{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003057 PyObject *key, *value, *result = di->di_result;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003058 Py_ssize_t i, mask, offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003059 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003060 PyObject **value_ptr;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003062 if (d == NULL)
3063 return NULL;
3064 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003066 if (di->di_used != d->ma_used) {
3067 PyErr_SetString(PyExc_RuntimeError,
3068 "dictionary changed size during iteration");
3069 di->di_used = -1; /* Make this state sticky */
3070 return NULL;
3071 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003073 i = di->di_pos;
3074 if (i < 0)
3075 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003076 mask = DK_SIZE(d->ma_keys)-1;
3077 if (d->ma_values) {
3078 value_ptr = &d->ma_values[i];
3079 offset = sizeof(PyObject *);
3080 }
3081 else {
3082 value_ptr = &d->ma_keys->dk_entries[i].me_value;
3083 offset = sizeof(PyDictKeyEntry);
3084 }
3085 while (i <= mask && *value_ptr == NULL) {
3086 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003087 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003088 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003089 di->di_pos = i+1;
3090 if (i > mask)
3091 goto fail;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003092
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003093 if (result->ob_refcnt == 1) {
3094 Py_INCREF(result);
3095 Py_DECREF(PyTuple_GET_ITEM(result, 0));
3096 Py_DECREF(PyTuple_GET_ITEM(result, 1));
3097 } else {
3098 result = PyTuple_New(2);
3099 if (result == NULL)
3100 return NULL;
3101 }
3102 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003103 key = d->ma_keys->dk_entries[i].me_key;
3104 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003105 Py_INCREF(key);
3106 Py_INCREF(value);
3107 PyTuple_SET_ITEM(result, 0, key);
3108 PyTuple_SET_ITEM(result, 1, value);
3109 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003110
3111fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003112 Py_DECREF(d);
3113 di->di_dict = NULL;
3114 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003115}
3116
3117PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003118 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3119 "dict_itemiterator", /* tp_name */
3120 sizeof(dictiterobject), /* tp_basicsize */
3121 0, /* tp_itemsize */
3122 /* methods */
3123 (destructor)dictiter_dealloc, /* tp_dealloc */
3124 0, /* tp_print */
3125 0, /* tp_getattr */
3126 0, /* tp_setattr */
3127 0, /* tp_reserved */
3128 0, /* tp_repr */
3129 0, /* tp_as_number */
3130 0, /* tp_as_sequence */
3131 0, /* tp_as_mapping */
3132 0, /* tp_hash */
3133 0, /* tp_call */
3134 0, /* tp_str */
3135 PyObject_GenericGetAttr, /* tp_getattro */
3136 0, /* tp_setattro */
3137 0, /* tp_as_buffer */
3138 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3139 0, /* tp_doc */
3140 (traverseproc)dictiter_traverse, /* tp_traverse */
3141 0, /* tp_clear */
3142 0, /* tp_richcompare */
3143 0, /* tp_weaklistoffset */
3144 PyObject_SelfIter, /* tp_iter */
3145 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3146 dictiter_methods, /* tp_methods */
3147 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003148};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003149
3150
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003151static PyObject *
3152dictiter_reduce(dictiterobject *di)
3153{
3154 PyObject *list;
3155 dictiterobject tmp;
3156
3157 list = PyList_New(0);
3158 if (!list)
3159 return NULL;
3160
3161 /* copy the itertor state */
3162 tmp = *di;
3163 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003164
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003165 /* iterate the temporary into a list */
3166 for(;;) {
3167 PyObject *element = 0;
3168 if (Py_TYPE(di) == &PyDictIterItem_Type)
3169 element = dictiter_iternextitem(&tmp);
3170 else if (Py_TYPE(di) == &PyDictIterKey_Type)
3171 element = dictiter_iternextkey(&tmp);
3172 else if (Py_TYPE(di) == &PyDictIterValue_Type)
3173 element = dictiter_iternextvalue(&tmp);
3174 else
3175 assert(0);
3176 if (element) {
3177 if (PyList_Append(list, element)) {
3178 Py_DECREF(element);
3179 Py_DECREF(list);
3180 Py_XDECREF(tmp.di_dict);
3181 return NULL;
3182 }
3183 Py_DECREF(element);
3184 } else
3185 break;
3186 }
3187 Py_XDECREF(tmp.di_dict);
3188 /* check for error */
3189 if (tmp.di_dict != NULL) {
3190 /* we have an error */
3191 Py_DECREF(list);
3192 return NULL;
3193 }
Antoine Pitroua7013882012-04-05 00:04:20 +02003194 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003195}
3196
Guido van Rossum3ac67412007-02-10 18:55:06 +00003197/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003198/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003199/***********************************************/
3200
Guido van Rossumb90c8482007-02-10 01:11:45 +00003201/* The instance lay-out is the same for all three; but the type differs. */
3202
3203typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003204 PyObject_HEAD
3205 PyDictObject *dv_dict;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003206} dictviewobject;
3207
3208
3209static void
Guido van Rossum3ac67412007-02-10 18:55:06 +00003210dictview_dealloc(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003211{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003212 Py_XDECREF(dv->dv_dict);
3213 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003214}
3215
3216static int
3217dictview_traverse(dictviewobject *dv, visitproc visit, void *arg)
3218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003219 Py_VISIT(dv->dv_dict);
3220 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003221}
3222
Guido van Rossum83825ac2007-02-10 04:54:19 +00003223static Py_ssize_t
Guido van Rossum3ac67412007-02-10 18:55:06 +00003224dictview_len(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003225{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003226 Py_ssize_t len = 0;
3227 if (dv->dv_dict != NULL)
3228 len = dv->dv_dict->ma_used;
3229 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003230}
3231
3232static PyObject *
3233dictview_new(PyObject *dict, PyTypeObject *type)
3234{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003235 dictviewobject *dv;
3236 if (dict == NULL) {
3237 PyErr_BadInternalCall();
3238 return NULL;
3239 }
3240 if (!PyDict_Check(dict)) {
3241 /* XXX Get rid of this restriction later */
3242 PyErr_Format(PyExc_TypeError,
3243 "%s() requires a dict argument, not '%s'",
3244 type->tp_name, dict->ob_type->tp_name);
3245 return NULL;
3246 }
3247 dv = PyObject_GC_New(dictviewobject, type);
3248 if (dv == NULL)
3249 return NULL;
3250 Py_INCREF(dict);
3251 dv->dv_dict = (PyDictObject *)dict;
3252 _PyObject_GC_TRACK(dv);
3253 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003254}
3255
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003256/* TODO(guido): The views objects are not complete:
3257
3258 * support more set operations
3259 * support arbitrary mappings?
3260 - either these should be static or exported in dictobject.h
3261 - if public then they should probably be in builtins
3262*/
3263
Guido van Rossumaac530c2007-08-24 22:33:45 +00003264/* Return 1 if self is a subset of other, iterating over self;
3265 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003266static int
3267all_contained_in(PyObject *self, PyObject *other)
3268{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003269 PyObject *iter = PyObject_GetIter(self);
3270 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003272 if (iter == NULL)
3273 return -1;
3274 for (;;) {
3275 PyObject *next = PyIter_Next(iter);
3276 if (next == NULL) {
3277 if (PyErr_Occurred())
3278 ok = -1;
3279 break;
3280 }
3281 ok = PySequence_Contains(other, next);
3282 Py_DECREF(next);
3283 if (ok <= 0)
3284 break;
3285 }
3286 Py_DECREF(iter);
3287 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003288}
3289
3290static PyObject *
3291dictview_richcompare(PyObject *self, PyObject *other, int op)
3292{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003293 Py_ssize_t len_self, len_other;
3294 int ok;
3295 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003297 assert(self != NULL);
3298 assert(PyDictViewSet_Check(self));
3299 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00003300
Brian Curtindfc80e32011-08-10 20:28:54 -05003301 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
3302 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003304 len_self = PyObject_Size(self);
3305 if (len_self < 0)
3306 return NULL;
3307 len_other = PyObject_Size(other);
3308 if (len_other < 0)
3309 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003311 ok = 0;
3312 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00003313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003314 case Py_NE:
3315 case Py_EQ:
3316 if (len_self == len_other)
3317 ok = all_contained_in(self, other);
3318 if (op == Py_NE && ok >= 0)
3319 ok = !ok;
3320 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003322 case Py_LT:
3323 if (len_self < len_other)
3324 ok = all_contained_in(self, other);
3325 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003327 case Py_LE:
3328 if (len_self <= len_other)
3329 ok = all_contained_in(self, other);
3330 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003332 case Py_GT:
3333 if (len_self > len_other)
3334 ok = all_contained_in(other, self);
3335 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003337 case Py_GE:
3338 if (len_self >= len_other)
3339 ok = all_contained_in(other, self);
3340 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003342 }
3343 if (ok < 0)
3344 return NULL;
3345 result = ok ? Py_True : Py_False;
3346 Py_INCREF(result);
3347 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003348}
3349
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003350static PyObject *
3351dictview_repr(dictviewobject *dv)
3352{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003353 PyObject *seq;
3354 PyObject *result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003356 seq = PySequence_List((PyObject *)dv);
3357 if (seq == NULL)
3358 return NULL;
3359
3360 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
3361 Py_DECREF(seq);
3362 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003363}
3364
Guido van Rossum3ac67412007-02-10 18:55:06 +00003365/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003366
3367static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003368dictkeys_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003369{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003370 if (dv->dv_dict == NULL) {
3371 Py_RETURN_NONE;
3372 }
3373 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003374}
3375
3376static int
3377dictkeys_contains(dictviewobject *dv, PyObject *obj)
3378{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003379 if (dv->dv_dict == NULL)
3380 return 0;
3381 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003382}
3383
Guido van Rossum83825ac2007-02-10 04:54:19 +00003384static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003385 (lenfunc)dictview_len, /* sq_length */
3386 0, /* sq_concat */
3387 0, /* sq_repeat */
3388 0, /* sq_item */
3389 0, /* sq_slice */
3390 0, /* sq_ass_item */
3391 0, /* sq_ass_slice */
3392 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003393};
3394
Guido van Rossum523259b2007-08-24 23:41:22 +00003395static PyObject*
3396dictviews_sub(PyObject* self, PyObject *other)
3397{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003398 PyObject *result = PySet_New(self);
3399 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003400 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003402 if (result == NULL)
3403 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003404
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003405 tmp = _PyObject_CallMethodId(result, &PyId_difference_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003406 if (tmp == NULL) {
3407 Py_DECREF(result);
3408 return NULL;
3409 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003411 Py_DECREF(tmp);
3412 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003413}
3414
3415static PyObject*
3416dictviews_and(PyObject* self, PyObject *other)
3417{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003418 PyObject *result = PySet_New(self);
3419 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003420 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003422 if (result == NULL)
3423 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003424
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003425 tmp = _PyObject_CallMethodId(result, &PyId_intersection_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003426 if (tmp == NULL) {
3427 Py_DECREF(result);
3428 return NULL;
3429 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003431 Py_DECREF(tmp);
3432 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003433}
3434
3435static PyObject*
3436dictviews_or(PyObject* self, PyObject *other)
3437{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003438 PyObject *result = PySet_New(self);
3439 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003440 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003442 if (result == NULL)
3443 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003444
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003445 tmp = _PyObject_CallMethodId(result, &PyId_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003446 if (tmp == NULL) {
3447 Py_DECREF(result);
3448 return NULL;
3449 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003451 Py_DECREF(tmp);
3452 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003453}
3454
3455static PyObject*
3456dictviews_xor(PyObject* self, PyObject *other)
3457{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003458 PyObject *result = PySet_New(self);
3459 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003460 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003462 if (result == NULL)
3463 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003464
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003465 tmp = _PyObject_CallMethodId(result, &PyId_symmetric_difference_update, "O",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003466 other);
3467 if (tmp == NULL) {
3468 Py_DECREF(result);
3469 return NULL;
3470 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003472 Py_DECREF(tmp);
3473 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003474}
3475
3476static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003477 0, /*nb_add*/
3478 (binaryfunc)dictviews_sub, /*nb_subtract*/
3479 0, /*nb_multiply*/
3480 0, /*nb_remainder*/
3481 0, /*nb_divmod*/
3482 0, /*nb_power*/
3483 0, /*nb_negative*/
3484 0, /*nb_positive*/
3485 0, /*nb_absolute*/
3486 0, /*nb_bool*/
3487 0, /*nb_invert*/
3488 0, /*nb_lshift*/
3489 0, /*nb_rshift*/
3490 (binaryfunc)dictviews_and, /*nb_and*/
3491 (binaryfunc)dictviews_xor, /*nb_xor*/
3492 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00003493};
3494
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003495static PyObject*
3496dictviews_isdisjoint(PyObject *self, PyObject *other)
3497{
3498 PyObject *it;
3499 PyObject *item = NULL;
3500
3501 if (self == other) {
3502 if (dictview_len((dictviewobject *)self) == 0)
3503 Py_RETURN_TRUE;
3504 else
3505 Py_RETURN_FALSE;
3506 }
3507
3508 /* Iterate over the shorter object (only if other is a set,
3509 * because PySequence_Contains may be expensive otherwise): */
3510 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
3511 Py_ssize_t len_self = dictview_len((dictviewobject *)self);
3512 Py_ssize_t len_other = PyObject_Size(other);
3513 if (len_other == -1)
3514 return NULL;
3515
3516 if ((len_other > len_self)) {
3517 PyObject *tmp = other;
3518 other = self;
3519 self = tmp;
3520 }
3521 }
3522
3523 it = PyObject_GetIter(other);
3524 if (it == NULL)
3525 return NULL;
3526
3527 while ((item = PyIter_Next(it)) != NULL) {
3528 int contains = PySequence_Contains(self, item);
3529 Py_DECREF(item);
3530 if (contains == -1) {
3531 Py_DECREF(it);
3532 return NULL;
3533 }
3534
3535 if (contains) {
3536 Py_DECREF(it);
3537 Py_RETURN_FALSE;
3538 }
3539 }
3540 Py_DECREF(it);
3541 if (PyErr_Occurred())
3542 return NULL; /* PyIter_Next raised an exception. */
3543 Py_RETURN_TRUE;
3544}
3545
3546PyDoc_STRVAR(isdisjoint_doc,
3547"Return True if the view and the given iterable have a null intersection.");
3548
Guido van Rossumb90c8482007-02-10 01:11:45 +00003549static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003550 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3551 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003552 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003553};
3554
3555PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003556 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3557 "dict_keys", /* tp_name */
3558 sizeof(dictviewobject), /* tp_basicsize */
3559 0, /* tp_itemsize */
3560 /* methods */
3561 (destructor)dictview_dealloc, /* tp_dealloc */
3562 0, /* tp_print */
3563 0, /* tp_getattr */
3564 0, /* tp_setattr */
3565 0, /* tp_reserved */
3566 (reprfunc)dictview_repr, /* tp_repr */
3567 &dictviews_as_number, /* tp_as_number */
3568 &dictkeys_as_sequence, /* tp_as_sequence */
3569 0, /* tp_as_mapping */
3570 0, /* tp_hash */
3571 0, /* tp_call */
3572 0, /* tp_str */
3573 PyObject_GenericGetAttr, /* tp_getattro */
3574 0, /* tp_setattro */
3575 0, /* tp_as_buffer */
3576 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3577 0, /* tp_doc */
3578 (traverseproc)dictview_traverse, /* tp_traverse */
3579 0, /* tp_clear */
3580 dictview_richcompare, /* tp_richcompare */
3581 0, /* tp_weaklistoffset */
3582 (getiterfunc)dictkeys_iter, /* tp_iter */
3583 0, /* tp_iternext */
3584 dictkeys_methods, /* tp_methods */
3585 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003586};
3587
3588static PyObject *
3589dictkeys_new(PyObject *dict)
3590{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003591 return dictview_new(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003592}
3593
Guido van Rossum3ac67412007-02-10 18:55:06 +00003594/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003595
3596static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003597dictitems_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003598{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003599 if (dv->dv_dict == NULL) {
3600 Py_RETURN_NONE;
3601 }
3602 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003603}
3604
3605static int
3606dictitems_contains(dictviewobject *dv, PyObject *obj)
3607{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003608 PyObject *key, *value, *found;
3609 if (dv->dv_dict == NULL)
3610 return 0;
3611 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
3612 return 0;
3613 key = PyTuple_GET_ITEM(obj, 0);
3614 value = PyTuple_GET_ITEM(obj, 1);
3615 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
3616 if (found == NULL) {
3617 if (PyErr_Occurred())
3618 return -1;
3619 return 0;
3620 }
3621 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003622}
3623
Guido van Rossum83825ac2007-02-10 04:54:19 +00003624static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003625 (lenfunc)dictview_len, /* sq_length */
3626 0, /* sq_concat */
3627 0, /* sq_repeat */
3628 0, /* sq_item */
3629 0, /* sq_slice */
3630 0, /* sq_ass_item */
3631 0, /* sq_ass_slice */
3632 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003633};
3634
Guido van Rossumb90c8482007-02-10 01:11:45 +00003635static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003636 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3637 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003638 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003639};
3640
3641PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003642 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3643 "dict_items", /* tp_name */
3644 sizeof(dictviewobject), /* tp_basicsize */
3645 0, /* tp_itemsize */
3646 /* methods */
3647 (destructor)dictview_dealloc, /* tp_dealloc */
3648 0, /* tp_print */
3649 0, /* tp_getattr */
3650 0, /* tp_setattr */
3651 0, /* tp_reserved */
3652 (reprfunc)dictview_repr, /* tp_repr */
3653 &dictviews_as_number, /* tp_as_number */
3654 &dictitems_as_sequence, /* tp_as_sequence */
3655 0, /* tp_as_mapping */
3656 0, /* tp_hash */
3657 0, /* tp_call */
3658 0, /* tp_str */
3659 PyObject_GenericGetAttr, /* tp_getattro */
3660 0, /* tp_setattro */
3661 0, /* tp_as_buffer */
3662 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3663 0, /* tp_doc */
3664 (traverseproc)dictview_traverse, /* tp_traverse */
3665 0, /* tp_clear */
3666 dictview_richcompare, /* tp_richcompare */
3667 0, /* tp_weaklistoffset */
3668 (getiterfunc)dictitems_iter, /* tp_iter */
3669 0, /* tp_iternext */
3670 dictitems_methods, /* tp_methods */
3671 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003672};
3673
3674static PyObject *
3675dictitems_new(PyObject *dict)
3676{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003677 return dictview_new(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003678}
3679
Guido van Rossum3ac67412007-02-10 18:55:06 +00003680/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003681
3682static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003683dictvalues_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003684{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003685 if (dv->dv_dict == NULL) {
3686 Py_RETURN_NONE;
3687 }
3688 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003689}
3690
Guido van Rossum83825ac2007-02-10 04:54:19 +00003691static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003692 (lenfunc)dictview_len, /* sq_length */
3693 0, /* sq_concat */
3694 0, /* sq_repeat */
3695 0, /* sq_item */
3696 0, /* sq_slice */
3697 0, /* sq_ass_item */
3698 0, /* sq_ass_slice */
3699 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003700};
3701
Guido van Rossumb90c8482007-02-10 01:11:45 +00003702static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003703 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003704};
3705
3706PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003707 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3708 "dict_values", /* tp_name */
3709 sizeof(dictviewobject), /* tp_basicsize */
3710 0, /* tp_itemsize */
3711 /* methods */
3712 (destructor)dictview_dealloc, /* tp_dealloc */
3713 0, /* tp_print */
3714 0, /* tp_getattr */
3715 0, /* tp_setattr */
3716 0, /* tp_reserved */
3717 (reprfunc)dictview_repr, /* tp_repr */
3718 0, /* tp_as_number */
3719 &dictvalues_as_sequence, /* tp_as_sequence */
3720 0, /* tp_as_mapping */
3721 0, /* tp_hash */
3722 0, /* tp_call */
3723 0, /* tp_str */
3724 PyObject_GenericGetAttr, /* tp_getattro */
3725 0, /* tp_setattro */
3726 0, /* tp_as_buffer */
3727 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3728 0, /* tp_doc */
3729 (traverseproc)dictview_traverse, /* tp_traverse */
3730 0, /* tp_clear */
3731 0, /* tp_richcompare */
3732 0, /* tp_weaklistoffset */
3733 (getiterfunc)dictvalues_iter, /* tp_iter */
3734 0, /* tp_iternext */
3735 dictvalues_methods, /* tp_methods */
3736 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003737};
3738
3739static PyObject *
3740dictvalues_new(PyObject *dict)
3741{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003742 return dictview_new(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003743}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003744
3745/* Returns NULL if cannot allocate a new PyDictKeysObject,
3746 but does not set an error */
3747PyDictKeysObject *
3748_PyDict_NewKeysForClass(void)
3749{
3750 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE_SPLIT);
3751 if (keys == NULL)
3752 PyErr_Clear();
3753 else
3754 keys->dk_lookup = lookdict_split;
3755 return keys;
3756}
3757
3758#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
3759
3760PyObject *
3761PyObject_GenericGetDict(PyObject *obj, void *context)
3762{
3763 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
3764 if (dictptr == NULL) {
3765 PyErr_SetString(PyExc_AttributeError,
3766 "This object has no __dict__");
3767 return NULL;
3768 }
3769 dict = *dictptr;
3770 if (dict == NULL) {
3771 PyTypeObject *tp = Py_TYPE(obj);
3772 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
3773 DK_INCREF(CACHED_KEYS(tp));
3774 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
3775 }
3776 else {
3777 *dictptr = dict = PyDict_New();
3778 }
3779 }
3780 Py_XINCREF(dict);
3781 return dict;
3782}
3783
3784int
3785_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
3786 PyObject *key, PyObject *value)
3787{
3788 PyObject *dict;
3789 int res;
3790 PyDictKeysObject *cached;
3791
3792 assert(dictptr != NULL);
3793 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
3794 assert(dictptr != NULL);
3795 dict = *dictptr;
3796 if (dict == NULL) {
3797 DK_INCREF(cached);
3798 dict = new_dict_with_shared_keys(cached);
3799 if (dict == NULL)
3800 return -1;
3801 *dictptr = dict;
3802 }
3803 if (value == NULL) {
3804 res = PyDict_DelItem(dict, key);
3805 if (cached != ((PyDictObject *)dict)->ma_keys) {
3806 CACHED_KEYS(tp) = NULL;
3807 DK_DECREF(cached);
3808 }
3809 } else {
3810 res = PyDict_SetItem(dict, key, value);
3811 if (cached != ((PyDictObject *)dict)->ma_keys) {
3812 /* Either update tp->ht_cached_keys or delete it */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04003813 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003814 CACHED_KEYS(tp) = make_keys_shared(dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003815 } else {
3816 CACHED_KEYS(tp) = NULL;
3817 }
3818 DK_DECREF(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04003819 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
3820 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003821 }
3822 }
3823 } else {
3824 dict = *dictptr;
3825 if (dict == NULL) {
3826 dict = PyDict_New();
3827 if (dict == NULL)
3828 return -1;
3829 *dictptr = dict;
3830 }
3831 if (value == NULL) {
3832 res = PyDict_DelItem(dict, key);
3833 } else {
3834 res = PyDict_SetItem(dict, key, value);
3835 }
3836 }
3837 return res;
3838}
3839
3840void
3841_PyDictKeys_DecRef(PyDictKeysObject *keys)
3842{
3843 DK_DECREF(keys);
3844}
3845
3846
3847/* ARGSUSED */
3848static PyObject *
3849dummy_repr(PyObject *op)
3850{
3851 return PyUnicode_FromString("<dummy key>");
3852}
3853
3854/* ARGUSED */
3855static void
3856dummy_dealloc(PyObject* ignore)
3857{
3858 /* This should never get called, but we also don't want to SEGV if
3859 * we accidentally decref dummy-key out of existence.
3860 */
3861 Py_FatalError("deallocating <dummy key>");
3862}
3863
3864static PyTypeObject PyDictDummy_Type = {
3865 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3866 "<dummy key> type",
3867 0,
3868 0,
3869 dummy_dealloc, /*tp_dealloc*/ /*never called*/
3870 0, /*tp_print*/
3871 0, /*tp_getattr*/
3872 0, /*tp_setattr*/
3873 0, /*tp_reserved*/
3874 dummy_repr, /*tp_repr*/
3875 0, /*tp_as_number*/
3876 0, /*tp_as_sequence*/
3877 0, /*tp_as_mapping*/
3878 0, /*tp_hash */
3879 0, /*tp_call */
3880 0, /*tp_str */
3881 0, /*tp_getattro */
3882 0, /*tp_setattro */
3883 0, /*tp_as_buffer */
3884 Py_TPFLAGS_DEFAULT, /*tp_flags */
3885};
3886
3887static PyObject _dummy_struct = {
3888 _PyObject_EXTRA_INIT
3889 2, &PyDictDummy_Type
3890};
3891