blob: a494d6b942594a2ef88a1308b58ccf7e6ea8a04e [file] [log] [blame]
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001
Guido van Rossum2bc13791999-03-24 19:06:42 +00002/* Dictionary object implementation using a hash table */
Guido van Rossum9bfef441993-03-29 10:43:31 +00003
Raymond Hettinger930427b2003-05-03 06:51:59 +00004/* The distribution includes a separate file, Objects/dictnotes.txt,
Tim Peters60b29962006-01-01 01:19:23 +00005 describing explorations into dictionary design and optimization.
Raymond Hettinger930427b2003-05-03 06:51:59 +00006 It covers typical dictionary use patterns, the parameters for
7 tuning dictionaries, and several ideas for possible optimizations.
8*/
9
Benjamin Peterson7d95e402012-04-23 11:24:50 -040010
11/*
12There are four kinds of slots in the table:
13
141. Unused. me_key == me_value == NULL
15 Does not hold an active (key, value) pair now and never did. Unused can
16 transition to Active upon key insertion. This is the only case in which
17 me_key is NULL, and is each slot's initial state.
18
192. Active. me_key != NULL and me_key != dummy and me_value != NULL
20 Holds an active (key, value) pair. Active can transition to Dummy or
21 Pending upon key deletion (for combined and split tables respectively).
22 This is the only case in which me_value != NULL.
23
243. Dummy. me_key == dummy and me_value == NULL
25 Previously held an active (key, value) pair, but that was deleted and an
26 active pair has not yet overwritten the slot. Dummy can transition to
27 Active upon key insertion. Dummy slots cannot be made Unused again
28 (cannot have me_key set to NULL), else the probe sequence in case of
29 collision would have no way to know they were once active.
30
314. Pending. Not yet inserted or deleted from a split-table.
32 key != NULL, key != dummy and value == NULL
33
34The DictObject can be in one of two forms.
35Either:
36 A combined table:
37 ma_values == NULL, dk_refcnt == 1.
38 Values are stored in the me_value field of the PyDictKeysObject.
39 Slot kind 4 is not allowed i.e.
40 key != NULL, key != dummy and value == NULL is illegal.
41Or:
42 A split table:
43 ma_values != NULL, dk_refcnt >= 1
44 Values are stored in the ma_values array.
45 Only string (unicode) keys are allowed, no <dummy> keys are present.
46
47Note: .popitem() abuses the me_hash field of an Unused or Dummy slot to
48hold a search finger. The me_hash field of Unused or Dummy slots has no
49meaning otherwise. As a consequence of this popitem always converts the dict
50to the combined-table form.
51*/
52
53/* PyDict_MINSIZE_SPLIT is the minimum size of a split dictionary.
54 * It must be a power of 2, and at least 4.
55 * Resizing of split dictionaries is very rare, so the saving memory is more
56 * important than the cost of resizing.
57 */
58#define PyDict_MINSIZE_SPLIT 4
59
60/* PyDict_MINSIZE_COMBINED is the starting size for any new, non-split dict.
61 * 8 allows dicts with no more than 5 active entries; experiments suggested
62 * this suffices for the majority of dicts (consisting mostly of usually-small
63 * dicts created to pass keyword arguments).
64 * Making this 8, rather than 4 reduces the number of resizes for most
65 * dictionaries, without any significant extra memory use.
66 */
67#define PyDict_MINSIZE_COMBINED 8
68
Guido van Rossumc0b618a1997-05-02 03:12:38 +000069#include "Python.h"
Christian Heimes0ded5b52007-12-10 15:50:56 +000070#include "stringlib/eq.h"
Guido van Rossum4b1302b1993-03-27 18:11:32 +000071
Larry Hastings61272b72014-01-07 12:41:53 -080072/*[clinic input]
Larry Hastingsc2047262014-01-25 20:43:29 -080073class dict "PyDictObject *" "&PyDict_Type"
Larry Hastings61272b72014-01-07 12:41:53 -080074[clinic start generated code]*/
Larry Hastings581ee362014-01-28 05:00:08 -080075/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f157a5a0ce9589d6]*/
Larry Hastings44e2eaa2013-11-23 15:37:55 -080076
Benjamin Peterson7d95e402012-04-23 11:24:50 -040077typedef struct {
78 /* Cached hash code of me_key. */
79 Py_hash_t me_hash;
80 PyObject *me_key;
81 PyObject *me_value; /* This field is only meaningful for combined tables */
82} PyDictKeyEntry;
83
84typedef PyDictKeyEntry *(*dict_lookup_func)
85(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject ***value_addr);
86
87struct _dictkeysobject {
88 Py_ssize_t dk_refcnt;
89 Py_ssize_t dk_size;
90 dict_lookup_func dk_lookup;
91 Py_ssize_t dk_usable;
92 PyDictKeyEntry dk_entries[1];
93};
94
95
96/*
97To ensure the lookup algorithm terminates, there must be at least one Unused
98slot (NULL key) in the table.
99To avoid slowing down lookups on a near-full table, we resize the table when
100it's USABLE_FRACTION (currently two-thirds) full.
101*/
Guido van Rossum16e93a81997-01-28 00:00:11 +0000102
Tim Peterseb28ef22001-06-02 05:27:19 +0000103#define PERTURB_SHIFT 5
104
Guido van Rossum16e93a81997-01-28 00:00:11 +0000105/*
Tim Peterseb28ef22001-06-02 05:27:19 +0000106Major subtleties ahead: Most hash schemes depend on having a "good" hash
107function, in the sense of simulating randomness. Python doesn't: its most
108important hash functions (for strings and ints) are very regular in common
109cases:
Tim Peters15d49292001-05-27 07:39:22 +0000110
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000111 >>> map(hash, (0, 1, 2, 3))
112 [0, 1, 2, 3]
113 >>> map(hash, ("namea", "nameb", "namec", "named"))
114 [-1658398457, -1658398460, -1658398459, -1658398462]
115 >>>
Tim Peters15d49292001-05-27 07:39:22 +0000116
Tim Peterseb28ef22001-06-02 05:27:19 +0000117This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
118the low-order i bits as the initial table index is extremely fast, and there
119are no collisions at all for dicts indexed by a contiguous range of ints.
120The same is approximately true when keys are "consecutive" strings. So this
121gives better-than-random behavior in common cases, and that's very desirable.
Tim Peters15d49292001-05-27 07:39:22 +0000122
Tim Peterseb28ef22001-06-02 05:27:19 +0000123OTOH, when collisions occur, the tendency to fill contiguous slices of the
124hash table makes a good collision resolution strategy crucial. Taking only
125the last i bits of the hash code is also vulnerable: for example, consider
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126the list [i << 16 for i in range(20000)] as a set of keys. Since ints are
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000127their own hash codes, and this fits in a dict of size 2**15, the last 15 bits
128 of every hash code are all 0: they *all* map to the same table index.
Tim Peters15d49292001-05-27 07:39:22 +0000129
Tim Peterseb28ef22001-06-02 05:27:19 +0000130But catering to unusual cases should not slow the usual ones, so we just take
131the last i bits anyway. It's up to collision resolution to do the rest. If
132we *usually* find the key we're looking for on the first try (and, it turns
133out, we usually do -- the table load factor is kept under 2/3, so the odds
134are solidly in our favor), then it makes best sense to keep the initial index
135computation dirt cheap.
Tim Peters15d49292001-05-27 07:39:22 +0000136
Tim Peterseb28ef22001-06-02 05:27:19 +0000137The first half of collision resolution is to visit table indices via this
138recurrence:
Tim Peters15d49292001-05-27 07:39:22 +0000139
Tim Peterseb28ef22001-06-02 05:27:19 +0000140 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +0000141
Tim Peterseb28ef22001-06-02 05:27:19 +0000142For any initial j in range(2**i), repeating that 2**i times generates each
143int in range(2**i) exactly once (see any text on random-number generation for
144proof). By itself, this doesn't help much: like linear probing (setting
145j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
146order. This would be bad, except that's not the only thing we do, and it's
147actually *good* in the common cases where hash keys are consecutive. In an
148example that's really too small to make this entirely clear, for a table of
149size 2**3 the order of indices is:
Tim Peters15d49292001-05-27 07:39:22 +0000150
Tim Peterseb28ef22001-06-02 05:27:19 +0000151 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
152
153If two things come in at index 5, the first place we look after is index 2,
154not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
155Linear probing is deadly in this case because there the fixed probe order
156is the *same* as the order consecutive keys are likely to arrive. But it's
157extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
158and certain that consecutive hash codes do not.
159
160The other half of the strategy is to get the other bits of the hash code
161into play. This is done by initializing a (unsigned) vrbl "perturb" to the
162full hash code, and changing the recurrence to:
163
164 j = (5*j) + 1 + perturb;
165 perturb >>= PERTURB_SHIFT;
166 use j % 2**i as the next table index;
167
168Now the probe sequence depends (eventually) on every bit in the hash code,
169and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
170because it quickly magnifies small differences in the bits that didn't affect
171the initial index. Note that because perturb is unsigned, if the recurrence
172is executed often enough perturb eventually becomes and remains 0. At that
173point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
174that's certain to find an empty slot eventually (since it generates every int
175in range(2**i), and we make sure there's always at least one empty slot).
176
177Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
178small so that the high bits of the hash code continue to affect the probe
179sequence across iterations; but you want it large so that in really bad cases
180the high-order hash bits have an effect on early iterations. 5 was "the
181best" in minimizing total collisions across experiments Tim Peters ran (on
182both normal and pathological cases), but 4 and 6 weren't significantly worse.
183
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000184Historical: Reimer Behrends contributed the idea of using a polynomial-based
Tim Peterseb28ef22001-06-02 05:27:19 +0000185approach, using repeated multiplication by x in GF(2**n) where an irreducible
186polynomial for each table size was chosen such that x was a primitive root.
187Christian Tismer later extended that to use division by x instead, as an
188efficient way to get the high bits of the hash code into play. This scheme
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000189also gave excellent collision statistics, but was more expensive: two
190if-tests were required inside the loop; computing "the next" index took about
191the same number of operations but without as much potential parallelism
192(e.g., computing 5*j can go on at the same time as computing 1+perturb in the
193above, and then shifting perturb can be done while the table index is being
194masked); and the PyDictObject struct required a member to hold the table's
195polynomial. In Tim's experiments the current scheme ran faster, produced
196equally good collision statistics, needed less code & used less memory.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000197
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000198*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000199
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400200/* Object used as dummy key to fill deleted entries
201 * This could be any unique object,
202 * use a custom type in order to minimise coupling.
203*/
204static PyObject _dummy_struct;
205
206#define dummy (&_dummy_struct)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000207
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000208#ifdef Py_REF_DEBUG
209PyObject *
210_PyDict_Dummy(void)
211{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 return dummy;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000213}
214#endif
215
Fred Drake1bff34a2000-08-31 19:31:38 +0000216/* forward declarations */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400217static PyDictKeyEntry *lookdict(PyDictObject *mp, PyObject *key,
218 Py_hash_t hash, PyObject ***value_addr);
219static PyDictKeyEntry *lookdict_unicode(PyDictObject *mp, PyObject *key,
220 Py_hash_t hash, PyObject ***value_addr);
221static PyDictKeyEntry *
222lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
223 Py_hash_t hash, PyObject ***value_addr);
224static PyDictKeyEntry *lookdict_split(PyDictObject *mp, PyObject *key,
225 Py_hash_t hash, PyObject ***value_addr);
Fred Drake1bff34a2000-08-31 19:31:38 +0000226
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400227static int dictresize(PyDictObject *mp, Py_ssize_t minused);
Tim Petersdea48ec2001-05-22 20:40:22 +0000228
Raymond Hettinger43442782004-03-17 21:55:03 +0000229/* Dictionary reuse scheme to save calls to malloc, free, and memset */
Christian Heimes2202f872008-02-06 14:31:34 +0000230#ifndef PyDict_MAXFREELIST
231#define PyDict_MAXFREELIST 80
232#endif
233static PyDictObject *free_list[PyDict_MAXFREELIST];
234static int numfree = 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000235
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100236int
237PyDict_ClearFreeList(void)
Christian Heimes77c02eb2008-02-09 02:18:51 +0000238{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 PyDictObject *op;
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100240 int ret = numfree;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 while (numfree) {
242 op = free_list[--numfree];
243 assert(PyDict_CheckExact(op));
244 PyObject_GC_Del(op);
245 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100246 return ret;
247}
248
David Malcolm49526f42012-06-22 14:55:41 -0400249/* Print summary info about the state of the optimized allocator */
250void
251_PyDict_DebugMallocStats(FILE *out)
252{
253 _PyDebugAllocatorStats(out,
254 "free PyDictObject", numfree, sizeof(PyDictObject));
255}
256
257
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100258void
259PyDict_Fini(void)
260{
261 PyDict_ClearFreeList();
Christian Heimes77c02eb2008-02-09 02:18:51 +0000262}
263
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200264#define DK_DEBUG_INCREF _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA
265#define DK_DEBUG_DECREF _Py_DEC_REFTOTAL _Py_REF_DEBUG_COMMA
266
267#define DK_INCREF(dk) (DK_DEBUG_INCREF ++(dk)->dk_refcnt)
268#define DK_DECREF(dk) if (DK_DEBUG_DECREF (--(dk)->dk_refcnt) == 0) free_keys_object(dk)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400269#define DK_SIZE(dk) ((dk)->dk_size)
270#define DK_MASK(dk) (((dk)->dk_size)-1)
271#define IS_POWER_OF_2(x) (((x) & (x-1)) == 0)
272
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200273/* USABLE_FRACTION is the maximum dictionary load.
274 * Currently set to (2n+1)/3. Increasing this ratio makes dictionaries more
275 * dense resulting in more collisions. Decreasing it improves sparseness
276 * at the expense of spreading entries over more cache lines and at the
277 * cost of total memory consumed.
278 *
279 * USABLE_FRACTION must obey the following:
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400280 * (0 < USABLE_FRACTION(n) < n) for all n >= 2
281 *
282 * USABLE_FRACTION should be very quick to calculate.
283 * Fractions around 5/8 to 2/3 seem to work well in practice.
284 */
285
286/* Use (2n+1)/3 rather than 2n+3 because: it makes no difference for
287 * combined tables (the two fractions round to the same number n < ),
288 * but 2*4/3 is 2 whereas (2*4+1)/3 is 3 which potentially saves quite
289 * a lot of space for small, split tables */
290#define USABLE_FRACTION(n) ((((n) << 1)+1)/3)
291
292/* Alternative fraction that is otherwise close enough to (2n+1)/3 to make
293 * little difference. 8 * 2/3 == 8 * 5/8 == 5. 16 * 2/3 == 16 * 5/8 == 10.
294 * 32 * 2/3 = 21, 32 * 5/8 = 20.
295 * Its advantage is that it is faster to compute on machines with slow division.
296 * #define USABLE_FRACTION(n) (((n) >> 1) + ((n) >> 2) - ((n) >> 3))
297*/
298
Victor Stinnera9f61a52013-07-16 22:17:26 +0200299/* GROWTH_RATE. Growth rate upon hitting maximum load.
300 * Currently set to used*2 + capacity/2.
301 * This means that dicts double in size when growing without deletions,
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700302 * but have more head room when the number of deletions is on a par with the
303 * number of insertions.
304 * Raising this to used*4 doubles memory consumption depending on the size of
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200305 * the dictionary, but results in half the number of resizes, less effort to
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700306 * resize.
307 * GROWTH_RATE was set to used*4 up to version 3.2.
308 * GROWTH_RATE was set to used*2 in version 3.3.0
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200309 */
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700310#define GROWTH_RATE(d) (((d)->ma_used*2)+((d)->ma_keys->dk_size>>1))
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400311
312#define ENSURE_ALLOWS_DELETIONS(d) \
313 if ((d)->ma_keys->dk_lookup == lookdict_unicode_nodummy) { \
314 (d)->ma_keys->dk_lookup = lookdict_unicode; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400316
317/* This immutable, empty PyDictKeysObject is used for PyDict_Clear()
318 * (which cannot fail and thus can do no allocation).
319 */
320static PyDictKeysObject empty_keys_struct = {
321 2, /* dk_refcnt 1 for this struct, 1 for dummy_struct */
322 1, /* dk_size */
323 lookdict_split, /* dk_lookup */
324 0, /* dk_usable (immutable) */
325 {
326 { 0, 0, 0 } /* dk_entries (empty) */
327 }
328};
329
330static PyObject *empty_values[1] = { NULL };
331
332#define Py_EMPTY_KEYS &empty_keys_struct
333
334static PyDictKeysObject *new_keys_object(Py_ssize_t size)
335{
336 PyDictKeysObject *dk;
337 Py_ssize_t i;
338 PyDictKeyEntry *ep0;
339
340 assert(size >= PyDict_MINSIZE_SPLIT);
341 assert(IS_POWER_OF_2(size));
342 dk = PyMem_MALLOC(sizeof(PyDictKeysObject) +
343 sizeof(PyDictKeyEntry) * (size-1));
344 if (dk == NULL) {
345 PyErr_NoMemory();
346 return NULL;
347 }
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200348 DK_DEBUG_INCREF dk->dk_refcnt = 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400349 dk->dk_size = size;
350 dk->dk_usable = USABLE_FRACTION(size);
351 ep0 = &dk->dk_entries[0];
352 /* Hash value of slot 0 is used by popitem, so it must be initialized */
353 ep0->me_hash = 0;
354 for (i = 0; i < size; i++) {
355 ep0[i].me_key = NULL;
356 ep0[i].me_value = NULL;
357 }
358 dk->dk_lookup = lookdict_unicode_nodummy;
359 return dk;
360}
361
362static void
363free_keys_object(PyDictKeysObject *keys)
364{
365 PyDictKeyEntry *entries = &keys->dk_entries[0];
366 Py_ssize_t i, n;
367 for (i = 0, n = DK_SIZE(keys); i < n; i++) {
368 Py_XDECREF(entries[i].me_key);
369 Py_XDECREF(entries[i].me_value);
370 }
371 PyMem_FREE(keys);
372}
373
374#define new_values(size) PyMem_NEW(PyObject *, size)
375
376#define free_values(values) PyMem_FREE(values)
377
378/* Consumes a reference to the keys object */
379static PyObject *
380new_dict(PyDictKeysObject *keys, PyObject **values)
381{
382 PyDictObject *mp;
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200383 assert(keys != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 if (numfree) {
385 mp = free_list[--numfree];
386 assert (mp != NULL);
387 assert (Py_TYPE(mp) == &PyDict_Type);
388 _Py_NewReference((PyObject *)mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400390 else {
391 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
392 if (mp == NULL) {
393 DK_DECREF(keys);
394 free_values(values);
395 return NULL;
396 }
397 }
398 mp->ma_keys = keys;
399 mp->ma_values = values;
400 mp->ma_used = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000402}
403
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400404/* Consumes a reference to the keys object */
405static PyObject *
406new_dict_with_shared_keys(PyDictKeysObject *keys)
407{
408 PyObject **values;
409 Py_ssize_t i, size;
410
411 size = DK_SIZE(keys);
412 values = new_values(size);
413 if (values == NULL) {
414 DK_DECREF(keys);
415 return PyErr_NoMemory();
416 }
417 for (i = 0; i < size; i++) {
418 values[i] = NULL;
419 }
420 return new_dict(keys, values);
421}
422
423PyObject *
424PyDict_New(void)
425{
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200426 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE_COMBINED);
427 if (keys == NULL)
428 return NULL;
429 return new_dict(keys, NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400430}
431
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000432/*
433The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000434This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000435Open addressing is preferred over chaining since the link overhead for
436chaining would be substantial (100% with typical malloc overhead).
437
Tim Peterseb28ef22001-06-02 05:27:19 +0000438The initial probe index is computed as hash mod the table size. Subsequent
439probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000440
441All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000442
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000443The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000444contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000445Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000446
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000447lookdict() is general-purpose, and may return NULL if (and only if) a
448comparison raises an exception (this was new in Python 2.5).
Guido van Rossum89d8c602007-09-18 17:26:56 +0000449lookdict_unicode() below is specialized to string keys, comparison of which can
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400450never raise an exception; that function can never return NULL.
451lookdict_unicode_nodummy is further specialized for string keys that cannot be
452the <dummy> value.
453For both, when the key isn't found a PyDictEntry* is returned
454where the key would have been found, *value_addr points to the matching value
455slot.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000456*/
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400457static PyDictKeyEntry *
458lookdict(PyDictObject *mp, PyObject *key,
459 Py_hash_t hash, PyObject ***value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000460{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200461 size_t i;
462 size_t perturb;
463 PyDictKeyEntry *freeslot;
464 size_t mask;
Antoine Pitrou9a234902012-05-13 20:48:01 +0200465 PyDictKeyEntry *ep0;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200466 PyDictKeyEntry *ep;
467 int cmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000469
Antoine Pitrou9a234902012-05-13 20:48:01 +0200470top:
471 mask = DK_MASK(mp->ma_keys);
472 ep0 = &mp->ma_keys->dk_entries[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 i = (size_t)hash & mask;
474 ep = &ep0[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400475 if (ep->me_key == NULL || ep->me_key == key) {
476 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400478 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 if (ep->me_key == dummy)
480 freeslot = ep;
481 else {
482 if (ep->me_hash == hash) {
483 startkey = ep->me_key;
484 Py_INCREF(startkey);
485 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
486 Py_DECREF(startkey);
487 if (cmp < 0)
488 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400489 if (ep0 == mp->ma_keys->dk_entries && ep->me_key == startkey) {
490 if (cmp > 0) {
491 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400493 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 }
495 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200496 /* The dict was mutated, restart */
497 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 }
499 }
500 freeslot = NULL;
501 }
Tim Peters15d49292001-05-27 07:39:22 +0000502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 /* In the loop, me_key == dummy is by far (factor of 100s) the
504 least likely outcome, so test for that last. */
505 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
506 i = (i << 2) + i + perturb + 1;
507 ep = &ep0[i & mask];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400508 if (ep->me_key == NULL) {
509 if (freeslot == NULL) {
510 *value_addr = &ep->me_value;
511 return ep;
512 } else {
513 *value_addr = &freeslot->me_value;
514 return freeslot;
515 }
516 }
517 if (ep->me_key == key) {
518 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400520 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 if (ep->me_hash == hash && ep->me_key != dummy) {
522 startkey = ep->me_key;
523 Py_INCREF(startkey);
524 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
525 Py_DECREF(startkey);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400526 if (cmp < 0) {
527 *value_addr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400529 }
530 if (ep0 == mp->ma_keys->dk_entries && ep->me_key == startkey) {
531 if (cmp > 0) {
532 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400534 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 }
536 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200537 /* The dict was mutated, restart */
538 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 }
540 }
541 else if (ep->me_key == dummy && freeslot == NULL)
542 freeslot = ep;
543 }
544 assert(0); /* NOT REACHED */
545 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000546}
547
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400548/* Specialized version for string-only keys */
549static PyDictKeyEntry *
550lookdict_unicode(PyDictObject *mp, PyObject *key,
551 Py_hash_t hash, PyObject ***value_addr)
Fred Drake1bff34a2000-08-31 19:31:38 +0000552{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200553 size_t i;
554 size_t perturb;
555 PyDictKeyEntry *freeslot;
556 size_t mask = DK_MASK(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400557 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200558 PyDictKeyEntry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 /* Make sure this function doesn't have to handle non-unicode keys,
561 including subclasses of str; e.g., one reason to subclass
562 unicodes is to override __eq__, and for speed we don't cater to
563 that here. */
564 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400565 mp->ma_keys->dk_lookup = lookdict;
566 return lookdict(mp, key, hash, value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 }
Mark Dickinson57e683e2011-09-24 18:18:40 +0100568 i = (size_t)hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 ep = &ep0[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400570 if (ep->me_key == NULL || ep->me_key == key) {
571 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400573 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 if (ep->me_key == dummy)
575 freeslot = ep;
576 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400577 if (ep->me_hash == hash && unicode_eq(ep->me_key, key)) {
578 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400580 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 freeslot = NULL;
582 }
Tim Peters15d49292001-05-27 07:39:22 +0000583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 /* In the loop, me_key == dummy is by far (factor of 100s) the
585 least likely outcome, so test for that last. */
586 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
587 i = (i << 2) + i + perturb + 1;
588 ep = &ep0[i & mask];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400589 if (ep->me_key == NULL) {
590 if (freeslot == NULL) {
591 *value_addr = &ep->me_value;
592 return ep;
593 } else {
594 *value_addr = &freeslot->me_value;
595 return freeslot;
596 }
597 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 if (ep->me_key == key
599 || (ep->me_hash == hash
600 && ep->me_key != dummy
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400601 && unicode_eq(ep->me_key, key))) {
602 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400604 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 if (ep->me_key == dummy && freeslot == NULL)
606 freeslot = ep;
607 }
608 assert(0); /* NOT REACHED */
609 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000610}
611
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400612/* Faster version of lookdict_unicode when it is known that no <dummy> keys
613 * will be present. */
614static PyDictKeyEntry *
615lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
616 Py_hash_t hash, PyObject ***value_addr)
617{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200618 size_t i;
619 size_t perturb;
620 size_t mask = DK_MASK(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400621 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200622 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400623
624 /* Make sure this function doesn't have to handle non-unicode keys,
625 including subclasses of str; e.g., one reason to subclass
626 unicodes is to override __eq__, and for speed we don't cater to
627 that here. */
628 if (!PyUnicode_CheckExact(key)) {
629 mp->ma_keys->dk_lookup = lookdict;
630 return lookdict(mp, key, hash, value_addr);
631 }
632 i = (size_t)hash & mask;
633 ep = &ep0[i];
634 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
635 if (ep->me_key == NULL || ep->me_key == key ||
636 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
637 *value_addr = &ep->me_value;
638 return ep;
639 }
640 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
641 i = (i << 2) + i + perturb + 1;
642 ep = &ep0[i & mask];
643 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
644 if (ep->me_key == NULL || ep->me_key == key ||
645 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
646 *value_addr = &ep->me_value;
647 return ep;
648 }
649 }
650 assert(0); /* NOT REACHED */
651 return 0;
652}
653
654/* Version of lookdict for split tables.
655 * All split tables and only split tables use this lookup function.
656 * Split tables only contain unicode keys and no dummy keys,
657 * so algorithm is the same as lookdict_unicode_nodummy.
658 */
659static PyDictKeyEntry *
660lookdict_split(PyDictObject *mp, PyObject *key,
661 Py_hash_t hash, PyObject ***value_addr)
662{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200663 size_t i;
664 size_t perturb;
665 size_t mask = DK_MASK(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400666 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200667 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400668
669 if (!PyUnicode_CheckExact(key)) {
Benjamin Petersondb780d02012-04-23 13:44:32 -0400670 ep = lookdict(mp, key, hash, value_addr);
671 /* lookdict expects a combined-table, so fix value_addr */
672 i = ep - ep0;
673 *value_addr = &mp->ma_values[i];
674 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400675 }
676 i = (size_t)hash & mask;
677 ep = &ep0[i];
678 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
679 if (ep->me_key == NULL || ep->me_key == key ||
680 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
681 *value_addr = &mp->ma_values[i];
682 return ep;
683 }
684 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
685 i = (i << 2) + i + perturb + 1;
686 ep = &ep0[i & mask];
687 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
688 if (ep->me_key == NULL || ep->me_key == key ||
689 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
690 *value_addr = &mp->ma_values[i & mask];
691 return ep;
692 }
693 }
694 assert(0); /* NOT REACHED */
695 return 0;
696}
697
Benjamin Petersonfb886362010-04-24 18:21:17 +0000698int
699_PyDict_HasOnlyStringKeys(PyObject *dict)
700{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 Py_ssize_t pos = 0;
702 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000703 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400705 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 return 1;
707 while (PyDict_Next(dict, &pos, &key, &value))
708 if (!PyUnicode_Check(key))
709 return 0;
710 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000711}
712
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000713#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 do { \
715 if (!_PyObject_GC_IS_TRACKED(mp)) { \
716 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
717 _PyObject_GC_MAY_BE_TRACKED(value)) { \
718 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 } \
720 } \
721 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000722
723void
724_PyDict_MaybeUntrack(PyObject *op)
725{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 PyDictObject *mp;
727 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400728 Py_ssize_t i, size;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
731 return;
732
733 mp = (PyDictObject *) op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400734 size = DK_SIZE(mp->ma_keys);
735 if (_PyDict_HasSplitTable(mp)) {
736 for (i = 0; i < size; i++) {
737 if ((value = mp->ma_values[i]) == NULL)
738 continue;
739 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
740 assert(!_PyObject_GC_MAY_BE_TRACKED(
741 mp->ma_keys->dk_entries[i].me_key));
742 return;
743 }
744 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400746 else {
747 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
748 for (i = 0; i < size; i++) {
749 if ((value = ep0[i].me_value) == NULL)
750 continue;
751 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
752 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
753 return;
754 }
755 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000757}
758
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400759/* Internal function to find slot for an item from its hash
760 * when it is known that the key is not present in the dict.
761 */
762static PyDictKeyEntry *
763find_empty_slot(PyDictObject *mp, PyObject *key, Py_hash_t hash,
764 PyObject ***value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000765{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400766 size_t i;
767 size_t perturb;
768 size_t mask = DK_MASK(mp->ma_keys);
769 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
770 PyDictKeyEntry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000771
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400772 assert(key != NULL);
773 if (!PyUnicode_CheckExact(key))
774 mp->ma_keys->dk_lookup = lookdict;
775 i = hash & mask;
776 ep = &ep0[i];
777 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
778 i = (i << 2) + i + perturb + 1;
779 ep = &ep0[i & mask];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400781 assert(ep->me_value == NULL);
782 if (mp->ma_values)
783 *value_addr = &mp->ma_values[i & mask];
784 else
785 *value_addr = &ep->me_value;
786 return ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000787}
788
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400789static int
790insertion_resize(PyDictObject *mp)
791{
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700792 return dictresize(mp, GROWTH_RATE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400793}
Antoine Pitroue965d972012-02-27 00:45:12 +0100794
795/*
796Internal routine to insert a new item into the table.
797Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +0100798Returns -1 if an error occurred, or 0 on success.
799*/
800static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400801insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +0100802{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400803 PyObject *old_value;
804 PyObject **value_addr;
805 PyDictKeyEntry *ep;
806 assert(key != dummy);
Antoine Pitroue965d972012-02-27 00:45:12 +0100807
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400808 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
809 if (insertion_resize(mp) < 0)
810 return -1;
811 }
812
813 ep = mp->ma_keys->dk_lookup(mp, key, hash, &value_addr);
Antoine Pitroue965d972012-02-27 00:45:12 +0100814 if (ep == NULL) {
Antoine Pitroue965d972012-02-27 00:45:12 +0100815 return -1;
816 }
Antoine Pitroud6967322014-10-18 00:35:00 +0200817 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
Benjamin Petersona6f195e2012-04-30 10:23:40 -0400818 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400819 MAINTAIN_TRACKING(mp, key, value);
820 old_value = *value_addr;
821 if (old_value != NULL) {
822 assert(ep->me_key != NULL && ep->me_key != dummy);
823 *value_addr = value;
Antoine Pitroud6967322014-10-18 00:35:00 +0200824 Py_DECREF(old_value); /* which **CAN** re-enter (see issue #22653) */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400825 }
826 else {
827 if (ep->me_key == NULL) {
Benjamin Petersona6f195e2012-04-30 10:23:40 -0400828 Py_INCREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400829 if (mp->ma_keys->dk_usable <= 0) {
830 /* Need to resize. */
831 if (insertion_resize(mp) < 0) {
832 Py_DECREF(key);
833 Py_DECREF(value);
834 return -1;
835 }
836 ep = find_empty_slot(mp, key, hash, &value_addr);
837 }
838 mp->ma_keys->dk_usable--;
839 assert(mp->ma_keys->dk_usable >= 0);
840 ep->me_key = key;
841 ep->me_hash = hash;
842 }
843 else {
844 if (ep->me_key == dummy) {
Benjamin Petersona6f195e2012-04-30 10:23:40 -0400845 Py_INCREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400846 ep->me_key = key;
847 ep->me_hash = hash;
848 Py_DECREF(dummy);
849 } else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400850 assert(_PyDict_HasSplitTable(mp));
851 }
852 }
853 mp->ma_used++;
854 *value_addr = value;
Antoine Pitroud6967322014-10-18 00:35:00 +0200855 assert(ep->me_key != NULL && ep->me_key != dummy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400856 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400857 return 0;
Antoine Pitroue965d972012-02-27 00:45:12 +0100858}
859
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000860/*
861Internal routine used by dictresize() to insert an item which is
862known to be absent from the dict. This routine also assumes that
863the dict contains no deleted entries. Besides the performance benefit,
864using insertdict() in dictresize() is dangerous (SF bug #1456209).
865Note that no refcounts are changed by this routine; if needed, the caller
866is responsible for incref'ing `key` and `value`.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400867Neither mp->ma_used nor k->dk_usable are modified by this routine; the caller
868must set them correctly
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000869*/
870static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400871insertdict_clean(PyDictObject *mp, PyObject *key, Py_hash_t hash,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 PyObject *value)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000873{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400874 size_t i;
875 size_t perturb;
876 PyDictKeysObject *k = mp->ma_keys;
877 size_t mask = (size_t)DK_SIZE(k)-1;
878 PyDictKeyEntry *ep0 = &k->dk_entries[0];
879 PyDictKeyEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000880
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400881 assert(k->dk_lookup != NULL);
882 assert(value != NULL);
883 assert(key != NULL);
884 assert(key != dummy);
885 assert(PyUnicode_CheckExact(key) || k->dk_lookup == lookdict);
886 i = hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 ep = &ep0[i];
888 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
889 i = (i << 2) + i + perturb + 1;
890 ep = &ep0[i & mask];
891 }
892 assert(ep->me_value == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 ep->me_key = key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000894 ep->me_hash = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000896}
897
898/*
899Restructure the table by allocating a new table and reinserting all
900items again. When entries have been deleted, the new table may
901actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400902If a table is split (its keys and hashes are shared, its values are not),
903then the values are temporarily copied into the table, it is resized as
904a combined table, then the me_value slots in the old table are NULLed out.
905After resizing a table is always combined,
906but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000907*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000908static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000909dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000910{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 Py_ssize_t newsize;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400912 PyDictKeysObject *oldkeys;
913 PyObject **oldvalues;
914 Py_ssize_t i, oldsize;
Tim Peters91a364d2001-05-19 07:04:38 +0000915
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400916/* Find the smallest table size > minused. */
917 for (newsize = PyDict_MINSIZE_COMBINED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 newsize <= minused && newsize > 0;
919 newsize <<= 1)
920 ;
921 if (newsize <= 0) {
922 PyErr_NoMemory();
923 return -1;
924 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400925 oldkeys = mp->ma_keys;
926 oldvalues = mp->ma_values;
927 /* Allocate a new table. */
928 mp->ma_keys = new_keys_object(newsize);
929 if (mp->ma_keys == NULL) {
930 mp->ma_keys = oldkeys;
931 return -1;
932 }
933 if (oldkeys->dk_lookup == lookdict)
934 mp->ma_keys->dk_lookup = lookdict;
935 oldsize = DK_SIZE(oldkeys);
936 mp->ma_values = NULL;
937 /* If empty then nothing to copy so just return */
938 if (oldsize == 1) {
939 assert(oldkeys == Py_EMPTY_KEYS);
940 DK_DECREF(oldkeys);
941 return 0;
942 }
943 /* Main loop below assumes we can transfer refcount to new keys
944 * and that value is stored in me_value.
945 * Increment ref-counts and copy values here to compensate
946 * This (resizing a split table) should be relatively rare */
947 if (oldvalues != NULL) {
948 for (i = 0; i < oldsize; i++) {
949 if (oldvalues[i] != NULL) {
950 Py_INCREF(oldkeys->dk_entries[i].me_key);
951 oldkeys->dk_entries[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 }
954 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400955 /* Main loop */
956 for (i = 0; i < oldsize; i++) {
957 PyDictKeyEntry *ep = &oldkeys->dk_entries[i];
958 if (ep->me_value != NULL) {
959 assert(ep->me_key != dummy);
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000960 insertdict_clean(mp, ep->me_key, ep->me_hash, ep->me_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400963 mp->ma_keys->dk_usable -= mp->ma_used;
964 if (oldvalues != NULL) {
965 /* NULL out me_value slot in oldkeys, in case it was shared */
966 for (i = 0; i < oldsize; i++)
967 oldkeys->dk_entries[i].me_value = NULL;
968 assert(oldvalues != empty_values);
969 free_values(oldvalues);
970 DK_DECREF(oldkeys);
971 }
972 else {
973 assert(oldkeys->dk_lookup != lookdict_split);
974 if (oldkeys->dk_lookup != lookdict_unicode_nodummy) {
975 PyDictKeyEntry *ep0 = &oldkeys->dk_entries[0];
976 for (i = 0; i < oldsize; i++) {
977 if (ep0[i].me_key == dummy)
978 Py_DECREF(dummy);
979 }
980 }
981 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200982 DK_DEBUG_DECREF PyMem_FREE(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400983 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000985}
986
Benjamin Peterson15ee8212012-04-24 14:44:18 -0400987/* Returns NULL if unable to split table.
988 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400989static PyDictKeysObject *
990make_keys_shared(PyObject *op)
991{
992 Py_ssize_t i;
993 Py_ssize_t size;
994 PyDictObject *mp = (PyDictObject *)op;
995
Benjamin Peterson15ee8212012-04-24 14:44:18 -0400996 if (!PyDict_CheckExact(op))
997 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400998 if (!_PyDict_HasSplitTable(mp)) {
999 PyDictKeyEntry *ep0;
1000 PyObject **values;
1001 assert(mp->ma_keys->dk_refcnt == 1);
1002 if (mp->ma_keys->dk_lookup == lookdict) {
1003 return NULL;
1004 }
1005 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
1006 /* Remove dummy keys */
1007 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
1008 return NULL;
1009 }
1010 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
1011 /* Copy values into a new array */
1012 ep0 = &mp->ma_keys->dk_entries[0];
1013 size = DK_SIZE(mp->ma_keys);
1014 values = new_values(size);
1015 if (values == NULL) {
1016 PyErr_SetString(PyExc_MemoryError,
1017 "Not enough memory to allocate new values array");
1018 return NULL;
1019 }
1020 for (i = 0; i < size; i++) {
1021 values[i] = ep0[i].me_value;
1022 ep0[i].me_value = NULL;
1023 }
1024 mp->ma_keys->dk_lookup = lookdict_split;
1025 mp->ma_values = values;
1026 }
1027 DK_INCREF(mp->ma_keys);
1028 return mp->ma_keys;
1029}
Christian Heimes99170a52007-12-19 02:07:34 +00001030
1031PyObject *
1032_PyDict_NewPresized(Py_ssize_t minused)
1033{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001034 Py_ssize_t newsize;
1035 PyDictKeysObject *new_keys;
1036 for (newsize = PyDict_MINSIZE_COMBINED;
1037 newsize <= minused && newsize > 0;
1038 newsize <<= 1)
1039 ;
1040 new_keys = new_keys_object(newsize);
1041 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001043 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001044}
1045
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001046/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1047 * that may occur (originally dicts supported only string keys, and exceptions
1048 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001049 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001050 * (suppressed) occurred while computing the key's hash, or that some error
1051 * (suppressed) occurred when comparing keys in the dict's internal probe
1052 * sequence. A nasty example of the latter is when a Python-coded comparison
1053 * function hits a stack-depth error, which can cause this to return NULL
1054 * even if the key is present.
1055 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001056PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001057PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001058{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001059 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001061 PyDictKeyEntry *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 PyThreadState *tstate;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001063 PyObject **value_addr;
1064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 if (!PyDict_Check(op))
1066 return NULL;
1067 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001068 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 {
1070 hash = PyObject_Hash(key);
1071 if (hash == -1) {
1072 PyErr_Clear();
1073 return NULL;
1074 }
1075 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 /* We can arrive here with a NULL tstate during initialization: try
1078 running "python -Wi" for an example related to string interning.
1079 Let's just hope that no exception occurs then... This must be
1080 _PyThreadState_Current and not PyThreadState_GET() because in debug
1081 mode, the latter complains if tstate is NULL. */
1082 tstate = (PyThreadState*)_Py_atomic_load_relaxed(
1083 &_PyThreadState_Current);
1084 if (tstate != NULL && tstate->curexc_type != NULL) {
1085 /* preserve the existing exception */
1086 PyObject *err_type, *err_value, *err_tb;
1087 PyErr_Fetch(&err_type, &err_value, &err_tb);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001088 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 /* ignore errors */
1090 PyErr_Restore(err_type, err_value, err_tb);
1091 if (ep == NULL)
1092 return NULL;
1093 }
1094 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001095 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 if (ep == NULL) {
1097 PyErr_Clear();
1098 return NULL;
1099 }
1100 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001101 return *value_addr;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001102}
1103
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001104/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1105 This returns NULL *with* an exception set if an exception occurred.
1106 It returns NULL *without* an exception set if the key wasn't present.
1107*/
1108PyObject *
1109PyDict_GetItemWithError(PyObject *op, PyObject *key)
1110{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001111 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 PyDictObject*mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001113 PyDictKeyEntry *ep;
1114 PyObject **value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 if (!PyDict_Check(op)) {
1117 PyErr_BadInternalCall();
1118 return NULL;
1119 }
1120 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001121 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 {
1123 hash = PyObject_Hash(key);
1124 if (hash == -1) {
1125 return NULL;
1126 }
1127 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001128
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001129 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 if (ep == NULL)
1131 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001132 return *value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001133}
1134
Brett Cannonfd074152012-04-14 14:10:13 -04001135PyObject *
1136_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1137{
1138 PyObject *kv;
1139 kv = _PyUnicode_FromId(key); /* borrowed */
1140 if (kv == NULL)
1141 return NULL;
1142 return PyDict_GetItemWithError(dp, kv);
1143}
1144
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001145/* Fast version of global value lookup.
1146 * Lookup in globals, then builtins.
1147 */
1148PyObject *
1149_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001150{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001151 PyObject *x;
1152 if (PyUnicode_CheckExact(key)) {
1153 PyObject **value_addr;
1154 Py_hash_t hash = ((PyASCIIObject *)key)->hash;
1155 if (hash != -1) {
1156 PyDictKeyEntry *e;
1157 e = globals->ma_keys->dk_lookup(globals, key, hash, &value_addr);
1158 if (e == NULL) {
1159 return NULL;
1160 }
1161 x = *value_addr;
1162 if (x != NULL)
1163 return x;
1164 e = builtins->ma_keys->dk_lookup(builtins, key, hash, &value_addr);
1165 if (e == NULL) {
1166 return NULL;
1167 }
1168 x = *value_addr;
1169 return x;
1170 }
Antoine Pitroue965d972012-02-27 00:45:12 +01001171 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001172 x = PyDict_GetItemWithError((PyObject *)globals, key);
1173 if (x != NULL)
1174 return x;
1175 if (PyErr_Occurred())
1176 return NULL;
1177 return PyDict_GetItemWithError((PyObject *)builtins, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001178}
1179
Antoine Pitroue965d972012-02-27 00:45:12 +01001180/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1181 * dictionary if it's merely replacing the value for an existing key.
1182 * This means that it's safe to loop over a dictionary with PyDict_Next()
1183 * and occasionally replace a value -- but you can't insert new keys or
1184 * remove them.
1185 */
1186int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001187PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001188{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001189 PyDictObject *mp;
1190 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001191 if (!PyDict_Check(op)) {
1192 PyErr_BadInternalCall();
1193 return -1;
1194 }
1195 assert(key);
1196 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001197 mp = (PyDictObject *)op;
1198 if (!PyUnicode_CheckExact(key) ||
1199 (hash = ((PyASCIIObject *) key)->hash) == -1)
1200 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001201 hash = PyObject_Hash(key);
1202 if (hash == -1)
1203 return -1;
1204 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001205
1206 /* insertdict() handles any resizing that might be necessary */
1207 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001208}
1209
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001210int
Tim Peters1f5871e2000-07-04 17:44:48 +00001211PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001212{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001213 PyDictObject *mp;
1214 Py_hash_t hash;
1215 PyDictKeyEntry *ep;
1216 PyObject *old_key, *old_value;
1217 PyObject **value_addr;
Guido van Rossumd7047b31995-01-02 19:07:15 +00001218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 if (!PyDict_Check(op)) {
1220 PyErr_BadInternalCall();
1221 return -1;
1222 }
1223 assert(key);
1224 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001225 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 hash = PyObject_Hash(key);
1227 if (hash == -1)
1228 return -1;
1229 }
1230 mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001231 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 if (ep == NULL)
1233 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001234 if (*value_addr == NULL) {
Raymond Hettinger69492da2013-09-02 15:59:26 -07001235 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 return -1;
1237 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001238 old_value = *value_addr;
1239 *value_addr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001241 if (!_PyDict_HasSplitTable(mp)) {
1242 ENSURE_ALLOWS_DELETIONS(mp);
1243 old_key = ep->me_key;
1244 Py_INCREF(dummy);
1245 ep->me_key = dummy;
1246 Py_DECREF(old_key);
1247 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 Py_DECREF(old_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001250}
1251
Guido van Rossum25831651993-05-19 14:50:45 +00001252void
Tim Peters1f5871e2000-07-04 17:44:48 +00001253PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001254{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001256 PyDictKeysObject *oldkeys;
1257 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 if (!PyDict_Check(op))
1261 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001262 mp = ((PyDictObject *)op);
1263 oldkeys = mp->ma_keys;
1264 oldvalues = mp->ma_values;
1265 if (oldvalues == empty_values)
1266 return;
1267 /* Empty the dict... */
1268 DK_INCREF(Py_EMPTY_KEYS);
1269 mp->ma_keys = Py_EMPTY_KEYS;
1270 mp->ma_values = empty_values;
1271 mp->ma_used = 0;
1272 /* ...then clear the keys and values */
1273 if (oldvalues != NULL) {
1274 n = DK_SIZE(oldkeys);
1275 for (i = 0; i < n; i++)
1276 Py_CLEAR(oldvalues[i]);
1277 free_values(oldvalues);
1278 DK_DECREF(oldkeys);
1279 }
1280 else {
1281 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001282 DK_DECREF(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001283 }
1284}
1285
1286/* Returns -1 if no more items (or op is not a dict),
1287 * index of item otherwise. Stores value in pvalue
1288 */
1289Py_LOCAL_INLINE(Py_ssize_t)
1290dict_next(PyObject *op, Py_ssize_t i, PyObject **pvalue)
1291{
1292 Py_ssize_t mask, offset;
1293 PyDictObject *mp;
1294 PyObject **value_ptr;
1295
1296
1297 if (!PyDict_Check(op))
1298 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001300 if (i < 0)
1301 return -1;
1302 if (mp->ma_values) {
1303 value_ptr = &mp->ma_values[i];
1304 offset = sizeof(PyObject *);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001306 else {
1307 value_ptr = &mp->ma_keys->dk_entries[i].me_value;
1308 offset = sizeof(PyDictKeyEntry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001310 mask = DK_MASK(mp->ma_keys);
1311 while (i <= mask && *value_ptr == NULL) {
1312 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1313 i++;
1314 }
1315 if (i > mask)
1316 return -1;
1317 if (pvalue)
1318 *pvalue = *value_ptr;
1319 return i;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001320}
1321
Tim Peters080c88b2003-02-15 03:01:11 +00001322/*
1323 * Iterate over a dict. Use like so:
1324 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001325 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001326 * PyObject *key, *value;
1327 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001328 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +00001329 * Refer to borrowed references in key and value.
1330 * }
1331 *
1332 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001333 * mutates the dict. One exception: it is safe if the loop merely changes
1334 * the values associated with the keys (but doesn't insert new keys or
1335 * delete keys), via PyDict_SetItem().
1336 */
Guido van Rossum25831651993-05-19 14:50:45 +00001337int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001338PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001339{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001340 PyDictObject *mp;
1341 Py_ssize_t i = dict_next(op, *ppos, pvalue);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 if (i < 0)
1343 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001344 mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 *ppos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 if (pkey)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001347 *pkey = mp->ma_keys->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001349}
1350
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001351/* Internal version of PyDict_Next that returns a hash value in addition
1352 * to the key and value.
1353 */
Thomas Wouterscf297e42007-02-23 15:07:44 +00001354int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001355_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1356 PyObject **pvalue, Py_hash_t *phash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00001357{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001358 PyDictObject *mp;
1359 Py_ssize_t i = dict_next(op, *ppos, pvalue);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 if (i < 0)
1361 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001362 mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 *ppos = i+1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001364 *phash = mp->ma_keys->dk_entries[i].me_hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 if (pkey)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001366 *pkey = mp->ma_keys->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 return 1;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001368}
1369
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001370/* Methods */
1371
1372static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001373dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001374{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001375 PyObject **values = mp->ma_values;
1376 PyDictKeysObject *keys = mp->ma_keys;
1377 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 PyObject_GC_UnTrack(mp);
1379 Py_TRASHCAN_SAFE_BEGIN(mp)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001380 if (values != NULL) {
1381 if (values != empty_values) {
1382 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
1383 Py_XDECREF(values[i]);
1384 }
1385 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001387 DK_DECREF(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02001389 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001390 assert(keys->dk_refcnt == 1);
1391 DK_DECREF(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001392 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1394 free_list[numfree++] = mp;
1395 else
1396 Py_TYPE(mp)->tp_free((PyObject *)mp);
1397 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001398}
1399
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001400
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001401static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001402dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001403{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001405 PyObject *key = NULL, *value = NULL;
1406 _PyUnicodeWriter writer;
1407 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00001408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 i = Py_ReprEnter((PyObject *)mp);
1410 if (i != 0) {
1411 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
1412 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001414 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001415 Py_ReprLeave((PyObject *)mp);
1416 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 }
Tim Petersa7259592001-06-16 05:11:17 +00001418
Victor Stinnerf91929b2013-11-19 13:07:38 +01001419 _PyUnicodeWriter_Init(&writer);
1420 writer.overallocate = 1;
1421 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
1422 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00001423
Victor Stinnerf91929b2013-11-19 13:07:38 +01001424 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
1425 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 /* Do repr() on each key+value pair, and insert ": " between them.
1428 Note that repr may mutate the dict. */
1429 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001430 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001431 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001432 PyObject *s;
1433 int res;
1434
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001435 /* Prevent repr from deleting key or value during key format. */
1436 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02001438
Victor Stinnerf91929b2013-11-19 13:07:38 +01001439 if (!first) {
1440 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
1441 goto error;
1442 }
1443 first = 0;
1444
1445 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01001447 goto error;
1448 res = _PyUnicodeWriter_WriteStr(&writer, s);
1449 Py_DECREF(s);
1450 if (res < 0)
1451 goto error;
1452
1453 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
1454 goto error;
1455
1456 s = PyObject_Repr(value);
1457 if (s == NULL)
1458 goto error;
1459 res = _PyUnicodeWriter_WriteStr(&writer, s);
1460 Py_DECREF(s);
1461 if (res < 0)
1462 goto error;
1463
1464 Py_CLEAR(key);
1465 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001466 }
Tim Petersa7259592001-06-16 05:11:17 +00001467
Victor Stinnerf91929b2013-11-19 13:07:38 +01001468 writer.overallocate = 0;
1469 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
1470 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01001473
1474 return _PyUnicodeWriter_Finish(&writer);
1475
1476error:
1477 Py_ReprLeave((PyObject *)mp);
1478 _PyUnicodeWriter_Dealloc(&writer);
1479 Py_XDECREF(key);
1480 Py_XDECREF(value);
1481 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001482}
1483
Martin v. Löwis18e16552006-02-15 17:27:45 +00001484static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001485dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001486{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001488}
1489
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001490static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001491dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001492{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001493 PyObject *v;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001494 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001495 PyDictKeyEntry *ep;
1496 PyObject **value_addr;
1497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001498 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001499 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001500 hash = PyObject_Hash(key);
1501 if (hash == -1)
1502 return NULL;
1503 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001504 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 if (ep == NULL)
1506 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001507 v = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001508 if (v == NULL) {
1509 if (!PyDict_CheckExact(mp)) {
1510 /* Look up __missing__ method if we're a subclass. */
1511 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05001512 _Py_IDENTIFIER(__missing__);
1513 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 if (missing != NULL) {
1515 res = PyObject_CallFunctionObjArgs(missing,
1516 key, NULL);
1517 Py_DECREF(missing);
1518 return res;
1519 }
1520 else if (PyErr_Occurred())
1521 return NULL;
1522 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07001523 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 return NULL;
1525 }
1526 else
1527 Py_INCREF(v);
1528 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001529}
1530
1531static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001532dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001533{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534 if (w == NULL)
1535 return PyDict_DelItem((PyObject *)mp, v);
1536 else
1537 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001538}
1539
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001540static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001541 (lenfunc)dict_length, /*mp_length*/
1542 (binaryfunc)dict_subscript, /*mp_subscript*/
1543 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001544};
1545
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001546static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001547dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001548{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001549 PyObject *v;
1550 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001551 PyDictKeyEntry *ep;
1552 Py_ssize_t size, n, offset;
1553 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001554
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001555 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 n = mp->ma_used;
1557 v = PyList_New(n);
1558 if (v == NULL)
1559 return NULL;
1560 if (n != mp->ma_used) {
1561 /* Durnit. The allocations caused the dict to resize.
1562 * Just start over, this shouldn't normally happen.
1563 */
1564 Py_DECREF(v);
1565 goto again;
1566 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001567 ep = &mp->ma_keys->dk_entries[0];
1568 size = DK_SIZE(mp->ma_keys);
1569 if (mp->ma_values) {
1570 value_ptr = mp->ma_values;
1571 offset = sizeof(PyObject *);
1572 }
1573 else {
1574 value_ptr = &ep[0].me_value;
1575 offset = sizeof(PyDictKeyEntry);
1576 }
1577 for (i = 0, j = 0; i < size; i++) {
1578 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 PyObject *key = ep[i].me_key;
1580 Py_INCREF(key);
1581 PyList_SET_ITEM(v, j, key);
1582 j++;
1583 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001584 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 }
1586 assert(j == n);
1587 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001588}
1589
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001590static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001591dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001592{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001593 PyObject *v;
1594 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001595 Py_ssize_t size, n, offset;
1596 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001597
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001598 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 n = mp->ma_used;
1600 v = PyList_New(n);
1601 if (v == NULL)
1602 return NULL;
1603 if (n != mp->ma_used) {
1604 /* Durnit. The allocations caused the dict to resize.
1605 * Just start over, this shouldn't normally happen.
1606 */
1607 Py_DECREF(v);
1608 goto again;
1609 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001610 size = DK_SIZE(mp->ma_keys);
1611 if (mp->ma_values) {
1612 value_ptr = mp->ma_values;
1613 offset = sizeof(PyObject *);
1614 }
1615 else {
1616 value_ptr = &mp->ma_keys->dk_entries[0].me_value;
1617 offset = sizeof(PyDictKeyEntry);
1618 }
1619 for (i = 0, j = 0; i < size; i++) {
1620 PyObject *value = *value_ptr;
1621 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1622 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 Py_INCREF(value);
1624 PyList_SET_ITEM(v, j, value);
1625 j++;
1626 }
1627 }
1628 assert(j == n);
1629 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001630}
1631
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001632static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001633dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001634{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001635 PyObject *v;
1636 Py_ssize_t i, j, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001637 Py_ssize_t size, offset;
1638 PyObject *item, *key;
1639 PyDictKeyEntry *ep;
1640 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 /* Preallocate the list of tuples, to avoid allocations during
1643 * the loop over the items, which could trigger GC, which
1644 * could resize the dict. :-(
1645 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001646 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 n = mp->ma_used;
1648 v = PyList_New(n);
1649 if (v == NULL)
1650 return NULL;
1651 for (i = 0; i < n; i++) {
1652 item = PyTuple_New(2);
1653 if (item == NULL) {
1654 Py_DECREF(v);
1655 return NULL;
1656 }
1657 PyList_SET_ITEM(v, i, item);
1658 }
1659 if (n != mp->ma_used) {
1660 /* Durnit. The allocations caused the dict to resize.
1661 * Just start over, this shouldn't normally happen.
1662 */
1663 Py_DECREF(v);
1664 goto again;
1665 }
1666 /* Nothing we do below makes any function calls. */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001667 ep = mp->ma_keys->dk_entries;
1668 size = DK_SIZE(mp->ma_keys);
1669 if (mp->ma_values) {
1670 value_ptr = mp->ma_values;
1671 offset = sizeof(PyObject *);
1672 }
1673 else {
1674 value_ptr = &ep[0].me_value;
1675 offset = sizeof(PyDictKeyEntry);
1676 }
1677 for (i = 0, j = 0; i < size; i++) {
1678 PyObject *value = *value_ptr;
1679 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1680 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001681 key = ep[i].me_key;
1682 item = PyList_GET_ITEM(v, j);
1683 Py_INCREF(key);
1684 PyTuple_SET_ITEM(item, 0, key);
1685 Py_INCREF(value);
1686 PyTuple_SET_ITEM(item, 1, value);
1687 j++;
1688 }
1689 }
1690 assert(j == n);
1691 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001692}
1693
Larry Hastings5c661892014-01-24 06:17:25 -08001694/*[clinic input]
1695@classmethod
1696dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08001697 iterable: object
1698 value: object=None
1699 /
1700
1701Returns a new dict with keys from iterable and values equal to value.
1702[clinic start generated code]*/
1703
1704PyDoc_STRVAR(dict_fromkeys__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -08001705"fromkeys($type, iterable, value=None, /)\n"
1706"--\n"
1707"\n"
Larry Hastings5c661892014-01-24 06:17:25 -08001708"Returns a new dict with keys from iterable and values equal to value.");
1709
1710#define DICT_FROMKEYS_METHODDEF \
1711 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS|METH_CLASS, dict_fromkeys__doc__},
1712
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001713static PyObject *
Larry Hastings5c661892014-01-24 06:17:25 -08001714dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value);
1715
1716static PyObject *
1717dict_fromkeys(PyTypeObject *type, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001718{
Larry Hastings5c661892014-01-24 06:17:25 -08001719 PyObject *return_value = NULL;
1720 PyObject *iterable;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001721 PyObject *value = Py_None;
Larry Hastings5c661892014-01-24 06:17:25 -08001722
1723 if (!PyArg_UnpackTuple(args, "fromkeys",
1724 1, 2,
1725 &iterable, &value))
1726 goto exit;
1727 return_value = dict_fromkeys_impl(type, iterable, value);
1728
1729exit:
1730 return return_value;
1731}
1732
1733static PyObject *
1734dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Larry Hastings2623c8c2014-02-08 22:15:29 -08001735/*[clinic end generated code: output=55f8dc0ffa87406f input=b85a667f9bf4669d]*/
Larry Hastings5c661892014-01-24 06:17:25 -08001736{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 PyObject *it; /* iter(seq) */
1738 PyObject *key;
1739 PyObject *d;
1740 int status;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001741
Larry Hastings5c661892014-01-24 06:17:25 -08001742 d = PyObject_CallObject((PyObject *)type, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 if (d == NULL)
1744 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001745
Benjamin Peterson9892f522012-10-31 14:22:12 -04001746 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
Larry Hastings5c661892014-01-24 06:17:25 -08001747 if (PyDict_CheckExact(iterable)) {
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001748 PyDictObject *mp = (PyDictObject *)d;
1749 PyObject *oldvalue;
1750 Py_ssize_t pos = 0;
1751 PyObject *key;
1752 Py_hash_t hash;
Guido van Rossum58da9312007-11-10 23:39:45 +00001753
Larry Hastings5c661892014-01-24 06:17:25 -08001754 if (dictresize(mp, Py_SIZE(iterable))) {
Petri Lehtinena94200e2011-10-24 21:12:58 +03001755 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001756 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001757 }
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001758
Larry Hastings5c661892014-01-24 06:17:25 -08001759 while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001760 if (insertdict(mp, key, hash, value)) {
1761 Py_DECREF(d);
1762 return NULL;
1763 }
1764 }
1765 return d;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766 }
Larry Hastings5c661892014-01-24 06:17:25 -08001767 if (PyAnySet_CheckExact(iterable)) {
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001768 PyDictObject *mp = (PyDictObject *)d;
1769 Py_ssize_t pos = 0;
1770 PyObject *key;
1771 Py_hash_t hash;
Guido van Rossum58da9312007-11-10 23:39:45 +00001772
Larry Hastings5c661892014-01-24 06:17:25 -08001773 if (dictresize(mp, PySet_GET_SIZE(iterable))) {
Petri Lehtinena94200e2011-10-24 21:12:58 +03001774 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001775 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001776 }
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001777
Larry Hastings5c661892014-01-24 06:17:25 -08001778 while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001779 if (insertdict(mp, key, hash, value)) {
1780 Py_DECREF(d);
1781 return NULL;
1782 }
1783 }
1784 return d;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001787
Larry Hastings5c661892014-01-24 06:17:25 -08001788 it = PyObject_GetIter(iterable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001789 if (it == NULL){
1790 Py_DECREF(d);
1791 return NULL;
1792 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001794 if (PyDict_CheckExact(d)) {
1795 while ((key = PyIter_Next(it)) != NULL) {
1796 status = PyDict_SetItem(d, key, value);
1797 Py_DECREF(key);
1798 if (status < 0)
1799 goto Fail;
1800 }
1801 } else {
1802 while ((key = PyIter_Next(it)) != NULL) {
1803 status = PyObject_SetItem(d, key, value);
1804 Py_DECREF(key);
1805 if (status < 0)
1806 goto Fail;
1807 }
1808 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001810 if (PyErr_Occurred())
1811 goto Fail;
1812 Py_DECREF(it);
1813 return d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001814
1815Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816 Py_DECREF(it);
1817 Py_DECREF(d);
1818 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001819}
1820
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001821static int
1822dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001823{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 PyObject *arg = NULL;
1825 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001826
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001827 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1828 result = -1;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001831 _Py_IDENTIFIER(keys);
1832 if (_PyObject_HasAttrId(arg, &PyId_keys))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001833 result = PyDict_Merge(self, arg, 1);
1834 else
1835 result = PyDict_MergeFromSeq2(self, arg, 1);
1836 }
1837 if (result == 0 && kwds != NULL) {
1838 if (PyArg_ValidateKeywordArguments(kwds))
1839 result = PyDict_Merge(self, kwds, 1);
1840 else
1841 result = -1;
1842 }
1843 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001844}
1845
1846static PyObject *
1847dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1848{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001849 if (dict_update_common(self, args, kwds, "update") != -1)
1850 Py_RETURN_NONE;
1851 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001852}
1853
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001854/* Update unconditionally replaces existing items.
1855 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001856 otherwise it leaves existing items unchanged.
1857
1858 PyDict_{Update,Merge} update/merge from a mapping object.
1859
Tim Petersf582b822001-12-11 18:51:08 +00001860 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001861 producing iterable objects of length 2.
1862*/
1863
Tim Petersf582b822001-12-11 18:51:08 +00001864int
Tim Peters1fc240e2001-10-26 05:06:50 +00001865PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1866{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001867 PyObject *it; /* iter(seq2) */
1868 Py_ssize_t i; /* index into seq2 of current element */
1869 PyObject *item; /* seq2[i] */
1870 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00001871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 assert(d != NULL);
1873 assert(PyDict_Check(d));
1874 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00001875
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 it = PyObject_GetIter(seq2);
1877 if (it == NULL)
1878 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001880 for (i = 0; ; ++i) {
1881 PyObject *key, *value;
1882 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001884 fast = NULL;
1885 item = PyIter_Next(it);
1886 if (item == NULL) {
1887 if (PyErr_Occurred())
1888 goto Fail;
1889 break;
1890 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 /* Convert item to sequence, and verify length 2. */
1893 fast = PySequence_Fast(item, "");
1894 if (fast == NULL) {
1895 if (PyErr_ExceptionMatches(PyExc_TypeError))
1896 PyErr_Format(PyExc_TypeError,
1897 "cannot convert dictionary update "
1898 "sequence element #%zd to a sequence",
1899 i);
1900 goto Fail;
1901 }
1902 n = PySequence_Fast_GET_SIZE(fast);
1903 if (n != 2) {
1904 PyErr_Format(PyExc_ValueError,
1905 "dictionary update sequence element #%zd "
1906 "has length %zd; 2 is required",
1907 i, n);
1908 goto Fail;
1909 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001911 /* Update/merge with this (key, value) pair. */
1912 key = PySequence_Fast_GET_ITEM(fast, 0);
1913 value = PySequence_Fast_GET_ITEM(fast, 1);
1914 if (override || PyDict_GetItem(d, key) == NULL) {
1915 int status = PyDict_SetItem(d, key, value);
1916 if (status < 0)
1917 goto Fail;
1918 }
1919 Py_DECREF(fast);
1920 Py_DECREF(item);
1921 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001923 i = 0;
1924 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00001925Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001926 Py_XDECREF(item);
1927 Py_XDECREF(fast);
1928 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001929Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 Py_DECREF(it);
1931 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001932}
1933
Tim Peters6d6c1a32001-08-02 04:15:00 +00001934int
1935PyDict_Update(PyObject *a, PyObject *b)
1936{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 return PyDict_Merge(a, b, 1);
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001938}
1939
1940int
1941PyDict_Merge(PyObject *a, PyObject *b, int override)
1942{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001943 PyDictObject *mp, *other;
1944 Py_ssize_t i, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001945 PyDictKeyEntry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001946
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001947 /* We accept for the argument either a concrete dictionary object,
1948 * or an abstract "mapping" object. For the former, we can do
1949 * things quite efficiently. For the latter, we only require that
1950 * PyMapping_Keys() and PyObject_GetItem() be supported.
1951 */
1952 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1953 PyErr_BadInternalCall();
1954 return -1;
1955 }
1956 mp = (PyDictObject*)a;
1957 if (PyDict_Check(b)) {
1958 other = (PyDictObject*)b;
1959 if (other == mp || other->ma_used == 0)
1960 /* a.update(a) or a.update({}); nothing to do */
1961 return 0;
1962 if (mp->ma_used == 0)
1963 /* Since the target dict is empty, PyDict_GetItem()
1964 * always returns NULL. Setting override to 1
1965 * skips the unnecessary test.
1966 */
1967 override = 1;
1968 /* Do one big resize at the start, rather than
1969 * incrementally resizing as we insert new items. Expect
1970 * that there will be no (or few) overlapping keys.
1971 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001972 if (mp->ma_keys->dk_usable * 3 < other->ma_used * 2)
1973 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001975 for (i = 0, n = DK_SIZE(other->ma_keys); i < n; i++) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05001976 PyObject *key, *value;
1977 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001978 entry = &other->ma_keys->dk_entries[i];
Benjamin Petersona82f77f2015-07-04 19:55:16 -05001979 key = entry->me_key;
1980 hash = entry->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001981 if (other->ma_values)
1982 value = other->ma_values[i];
1983 else
1984 value = entry->me_value;
1985
Benjamin Petersona82f77f2015-07-04 19:55:16 -05001986 if (value != NULL) {
1987 int err = 0;
1988 Py_INCREF(key);
1989 Py_INCREF(value);
1990 if (override || PyDict_GetItem(a, key) == NULL)
1991 err = insertdict(mp, key, hash, value);
1992 Py_DECREF(value);
1993 Py_DECREF(key);
1994 if (err != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001995 return -1;
Benjamin Petersona82f77f2015-07-04 19:55:16 -05001996
1997 if (n != DK_SIZE(other->ma_keys)) {
1998 PyErr_SetString(PyExc_RuntimeError,
1999 "dict mutated during update");
2000 return -1;
2001 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002002 }
2003 }
2004 }
2005 else {
2006 /* Do it the generic, slower way */
2007 PyObject *keys = PyMapping_Keys(b);
2008 PyObject *iter;
2009 PyObject *key, *value;
2010 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002011
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002012 if (keys == NULL)
2013 /* Docstring says this is equivalent to E.keys() so
2014 * if E doesn't have a .keys() method we want
2015 * AttributeError to percolate up. Might as well
2016 * do the same for any other error.
2017 */
2018 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002020 iter = PyObject_GetIter(keys);
2021 Py_DECREF(keys);
2022 if (iter == NULL)
2023 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002024
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
2026 if (!override && PyDict_GetItem(a, key) != NULL) {
2027 Py_DECREF(key);
2028 continue;
2029 }
2030 value = PyObject_GetItem(b, key);
2031 if (value == NULL) {
2032 Py_DECREF(iter);
2033 Py_DECREF(key);
2034 return -1;
2035 }
2036 status = PyDict_SetItem(a, key, value);
2037 Py_DECREF(key);
2038 Py_DECREF(value);
2039 if (status < 0) {
2040 Py_DECREF(iter);
2041 return -1;
2042 }
2043 }
2044 Py_DECREF(iter);
2045 if (PyErr_Occurred())
2046 /* Iterator completed, via error */
2047 return -1;
2048 }
2049 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002050}
2051
2052static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002053dict_copy(PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002054{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002055 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002056}
2057
2058PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002059PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002060{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002062 PyDictObject *mp;
2063 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002065 if (o == NULL || !PyDict_Check(o)) {
2066 PyErr_BadInternalCall();
2067 return NULL;
2068 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002069 mp = (PyDictObject *)o;
2070 if (_PyDict_HasSplitTable(mp)) {
2071 PyDictObject *split_copy;
2072 PyObject **newvalues = new_values(DK_SIZE(mp->ma_keys));
2073 if (newvalues == NULL)
2074 return PyErr_NoMemory();
2075 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2076 if (split_copy == NULL) {
2077 free_values(newvalues);
2078 return NULL;
2079 }
2080 split_copy->ma_values = newvalues;
2081 split_copy->ma_keys = mp->ma_keys;
2082 split_copy->ma_used = mp->ma_used;
2083 DK_INCREF(mp->ma_keys);
2084 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2085 PyObject *value = mp->ma_values[i];
2086 Py_XINCREF(value);
2087 split_copy->ma_values[i] = value;
2088 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002089 if (_PyObject_GC_IS_TRACKED(mp))
2090 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002091 return (PyObject *)split_copy;
2092 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002093 copy = PyDict_New();
2094 if (copy == NULL)
2095 return NULL;
2096 if (PyDict_Merge(copy, o, 1) == 0)
2097 return copy;
2098 Py_DECREF(copy);
2099 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002100}
2101
Martin v. Löwis18e16552006-02-15 17:27:45 +00002102Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002103PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002104{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002105 if (mp == NULL || !PyDict_Check(mp)) {
2106 PyErr_BadInternalCall();
2107 return -1;
2108 }
2109 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002110}
2111
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002112PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002113PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002114{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002115 if (mp == NULL || !PyDict_Check(mp)) {
2116 PyErr_BadInternalCall();
2117 return NULL;
2118 }
2119 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002120}
2121
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002122PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002123PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002124{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002125 if (mp == NULL || !PyDict_Check(mp)) {
2126 PyErr_BadInternalCall();
2127 return NULL;
2128 }
2129 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002130}
2131
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002132PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002133PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002134{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002135 if (mp == NULL || !PyDict_Check(mp)) {
2136 PyErr_BadInternalCall();
2137 return NULL;
2138 }
2139 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002140}
2141
Tim Peterse63415e2001-05-08 04:38:29 +00002142/* Return 1 if dicts equal, 0 if not, -1 if error.
2143 * Gets out as soon as any difference is detected.
2144 * Uses only Py_EQ comparison.
2145 */
2146static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002147dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002148{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002149 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002151 if (a->ma_used != b->ma_used)
2152 /* can't be equal if # of entries differ */
2153 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002154 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002155 for (i = 0; i < DK_SIZE(a->ma_keys); i++) {
2156 PyDictKeyEntry *ep = &a->ma_keys->dk_entries[i];
2157 PyObject *aval;
2158 if (a->ma_values)
2159 aval = a->ma_values[i];
2160 else
2161 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162 if (aval != NULL) {
2163 int cmp;
2164 PyObject *bval;
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002165 PyObject **vaddr;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002166 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002167 /* temporarily bump aval's refcount to ensure it stays
2168 alive until we're done with it */
2169 Py_INCREF(aval);
2170 /* ditto for key */
2171 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002172 /* reuse the known hash value */
2173 if ((b->ma_keys->dk_lookup)(b, key, ep->me_hash, &vaddr) == NULL)
2174 bval = NULL;
2175 else
2176 bval = *vaddr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002177 Py_DECREF(key);
2178 if (bval == NULL) {
2179 Py_DECREF(aval);
2180 if (PyErr_Occurred())
2181 return -1;
2182 return 0;
2183 }
2184 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
2185 Py_DECREF(aval);
2186 if (cmp <= 0) /* error or not equal */
2187 return cmp;
2188 }
2189 }
2190 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002191}
Tim Peterse63415e2001-05-08 04:38:29 +00002192
2193static PyObject *
2194dict_richcompare(PyObject *v, PyObject *w, int op)
2195{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002196 int cmp;
2197 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002199 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2200 res = Py_NotImplemented;
2201 }
2202 else if (op == Py_EQ || op == Py_NE) {
2203 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2204 if (cmp < 0)
2205 return NULL;
2206 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2207 }
2208 else
2209 res = Py_NotImplemented;
2210 Py_INCREF(res);
2211 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002212}
Tim Peterse63415e2001-05-08 04:38:29 +00002213
Larry Hastings61272b72014-01-07 12:41:53 -08002214/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002215
2216@coexist
2217dict.__contains__
2218
2219 key: object
2220 /
2221
Meador Ingee02de8c2014-01-14 16:48:31 -06002222True if D has a key k, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002223[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002224
2225PyDoc_STRVAR(dict___contains____doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -08002226"__contains__($self, key, /)\n"
2227"--\n"
2228"\n"
Meador Ingee02de8c2014-01-14 16:48:31 -06002229"True if D has a key k, else False.");
Larry Hastings31826802013-10-19 00:09:25 -07002230
2231#define DICT___CONTAINS___METHODDEF \
2232 {"__contains__", (PyCFunction)dict___contains__, METH_O|METH_COEXIST, dict___contains____doc__},
2233
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002234static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002235dict___contains__(PyDictObject *self, PyObject *key)
Larry Hastings2623c8c2014-02-08 22:15:29 -08002236/*[clinic end generated code: output=3cf3f8aaf2cc5cc3 input=b852b2a19b51ab24]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002237{
Larry Hastingsc2047262014-01-25 20:43:29 -08002238 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002239 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002240 PyDictKeyEntry *ep;
2241 PyObject **value_addr;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002243 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002244 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002245 hash = PyObject_Hash(key);
2246 if (hash == -1)
2247 return NULL;
2248 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002249 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002250 if (ep == NULL)
2251 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002252 return PyBool_FromLong(*value_addr != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002253}
2254
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002255static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002256dict_get(PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00002257{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002258 PyObject *key;
2259 PyObject *failobj = Py_None;
2260 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002261 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002262 PyDictKeyEntry *ep;
2263 PyObject **value_addr;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002265 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
2266 return NULL;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002268 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002269 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002270 hash = PyObject_Hash(key);
2271 if (hash == -1)
2272 return NULL;
2273 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002274 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002275 if (ep == NULL)
2276 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002277 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 if (val == NULL)
2279 val = failobj;
2280 Py_INCREF(val);
2281 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002282}
2283
Benjamin Peterson00e98862013-03-07 22:16:29 -05002284PyObject *
2285PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002286{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002287 PyDictObject *mp = (PyDictObject *)d;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002288 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002289 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002290 PyDictKeyEntry *ep;
2291 PyObject **value_addr;
Guido van Rossum164452c2000-08-08 16:12:54 +00002292
Benjamin Peterson00e98862013-03-07 22:16:29 -05002293 if (!PyDict_Check(d)) {
2294 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002295 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002296 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002297 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002298 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002299 hash = PyObject_Hash(key);
2300 if (hash == -1)
2301 return NULL;
2302 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002303 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002304 if (ep == NULL)
2305 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002306 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 if (val == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002308 if (mp->ma_keys->dk_usable <= 0) {
2309 /* Need to resize. */
2310 if (insertion_resize(mp) < 0)
2311 return NULL;
2312 ep = find_empty_slot(mp, key, hash, &value_addr);
2313 }
Benjamin Peterson00e98862013-03-07 22:16:29 -05002314 Py_INCREF(defaultobj);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002315 Py_INCREF(key);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002316 MAINTAIN_TRACKING(mp, key, defaultobj);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002317 ep->me_key = key;
2318 ep->me_hash = hash;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002319 *value_addr = defaultobj;
2320 val = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002321 mp->ma_keys->dk_usable--;
2322 mp->ma_used++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002323 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002324 return val;
Guido van Rossum164452c2000-08-08 16:12:54 +00002325}
2326
Benjamin Peterson00e98862013-03-07 22:16:29 -05002327static PyObject *
2328dict_setdefault(PyDictObject *mp, PyObject *args)
2329{
2330 PyObject *key, *val;
2331 PyObject *defaultobj = Py_None;
2332
2333 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &defaultobj))
2334 return NULL;
2335
Benjamin Peterson55898502013-03-08 08:36:49 -05002336 val = PyDict_SetDefault((PyObject *)mp, key, defaultobj);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002337 Py_XINCREF(val);
2338 return val;
2339}
Guido van Rossum164452c2000-08-08 16:12:54 +00002340
2341static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002342dict_clear(PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002343{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002344 PyDict_Clear((PyObject *)mp);
2345 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002346}
2347
Guido van Rossumba6ab842000-12-12 22:02:18 +00002348static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002349dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002350{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002351 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002352 PyObject *old_value, *old_key;
2353 PyObject *key, *deflt = NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002354 PyDictKeyEntry *ep;
2355 PyObject **value_addr;
Guido van Rossume027d982002-04-12 15:11:59 +00002356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2358 return NULL;
2359 if (mp->ma_used == 0) {
2360 if (deflt) {
2361 Py_INCREF(deflt);
2362 return deflt;
2363 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07002364 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002365 return NULL;
2366 }
2367 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002368 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 hash = PyObject_Hash(key);
2370 if (hash == -1)
2371 return NULL;
2372 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002373 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002374 if (ep == NULL)
2375 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002376 old_value = *value_addr;
2377 if (old_value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002378 if (deflt) {
2379 Py_INCREF(deflt);
2380 return deflt;
2381 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07002382 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 return NULL;
2384 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002385 *value_addr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002387 if (!_PyDict_HasSplitTable(mp)) {
2388 ENSURE_ALLOWS_DELETIONS(mp);
2389 old_key = ep->me_key;
2390 Py_INCREF(dummy);
2391 ep->me_key = dummy;
2392 Py_DECREF(old_key);
2393 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 return old_value;
Guido van Rossume027d982002-04-12 15:11:59 +00002395}
2396
2397static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002398dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00002399{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002400 Py_hash_t i = 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002401 PyDictKeyEntry *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002402 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002403
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002405 /* Allocate the result tuple before checking the size. Believe it
2406 * or not, this allocation could trigger a garbage collection which
2407 * could empty the dict, so if we checked the size first and that
2408 * happened, the result would be an infinite loop (searching for an
2409 * entry that no longer exists). Note that the usual popitem()
2410 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2411 * tuple away if the dict *is* empty isn't a significant
2412 * inefficiency -- possible, but unlikely in practice.
2413 */
2414 res = PyTuple_New(2);
2415 if (res == NULL)
2416 return NULL;
2417 if (mp->ma_used == 0) {
2418 Py_DECREF(res);
2419 PyErr_SetString(PyExc_KeyError,
2420 "popitem(): dictionary is empty");
2421 return NULL;
2422 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002423 /* Convert split table to combined table */
2424 if (mp->ma_keys->dk_lookup == lookdict_split) {
2425 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
2426 Py_DECREF(res);
2427 return NULL;
2428 }
2429 }
2430 ENSURE_ALLOWS_DELETIONS(mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002431 /* Set ep to "the first" dict entry with a value. We abuse the hash
2432 * field of slot 0 to hold a search finger:
2433 * If slot 0 has a value, use slot 0.
2434 * Else slot 0 is being used to hold a search finger,
2435 * and we use its hash value as the first index to look.
2436 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002437 ep = &mp->ma_keys->dk_entries[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002438 if (ep->me_value == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002439 Py_ssize_t mask = DK_MASK(mp->ma_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002440 i = ep->me_hash;
2441 /* The hash field may be a real hash value, or it may be a
2442 * legit search finger, or it may be a once-legit search
2443 * finger that's out of bounds now because it wrapped around
2444 * or the table shrunk -- simply make sure it's in bounds now.
2445 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002446 if (i > mask || i < 1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002447 i = 1; /* skip slot 0 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002448 while ((ep = &mp->ma_keys->dk_entries[i])->me_value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002449 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002450 if (i > mask)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002451 i = 1;
2452 }
2453 }
2454 PyTuple_SET_ITEM(res, 0, ep->me_key);
2455 PyTuple_SET_ITEM(res, 1, ep->me_value);
2456 Py_INCREF(dummy);
2457 ep->me_key = dummy;
2458 ep->me_value = NULL;
2459 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002460 assert(mp->ma_keys->dk_entries[0].me_value == NULL);
2461 mp->ma_keys->dk_entries[0].me_hash = i + 1; /* next place to start */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002462 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002463}
2464
Jeremy Hylton8caad492000-06-23 14:18:11 +00002465static int
2466dict_traverse(PyObject *op, visitproc visit, void *arg)
2467{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002468 Py_ssize_t i, n;
2469 PyDictObject *mp = (PyDictObject *)op;
2470 if (mp->ma_keys->dk_lookup == lookdict) {
2471 for (i = 0; i < DK_SIZE(mp->ma_keys); i++) {
2472 if (mp->ma_keys->dk_entries[i].me_value != NULL) {
2473 Py_VISIT(mp->ma_keys->dk_entries[i].me_value);
2474 Py_VISIT(mp->ma_keys->dk_entries[i].me_key);
2475 }
2476 }
2477 } else {
2478 if (mp->ma_values != NULL) {
2479 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2480 Py_VISIT(mp->ma_values[i]);
2481 }
2482 }
2483 else {
2484 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2485 Py_VISIT(mp->ma_keys->dk_entries[i].me_value);
2486 }
2487 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002488 }
2489 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002490}
2491
2492static int
2493dict_tp_clear(PyObject *op)
2494{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002495 PyDict_Clear(op);
2496 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002497}
2498
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002499static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002500
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002501static PyObject *
2502dict_sizeof(PyDictObject *mp)
2503{
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002504 Py_ssize_t size, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002505
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002506 size = DK_SIZE(mp->ma_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002507 res = sizeof(PyDictObject);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002508 if (mp->ma_values)
2509 res += size * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002510 /* If the dictionary is split, the keys portion is accounted-for
2511 in the type object. */
2512 if (mp->ma_keys->dk_refcnt == 1)
2513 res += sizeof(PyDictKeysObject) + (size-1) * sizeof(PyDictKeyEntry);
2514 return PyLong_FromSsize_t(res);
2515}
2516
2517Py_ssize_t
2518_PyDict_KeysSize(PyDictKeysObject *keys)
2519{
2520 return sizeof(PyDictKeysObject) + (DK_SIZE(keys)-1) * sizeof(PyDictKeyEntry);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002521}
2522
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002523PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
2524
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002525PyDoc_STRVAR(sizeof__doc__,
2526"D.__sizeof__() -> size of D in memory, in bytes");
2527
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002528PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002529"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002530
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002531PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002532"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 +00002533
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002534PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002535"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002536If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00002537
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002538PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00002539"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000025402-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002541
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002542PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04002543"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
2544If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
2545If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
2546In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00002547
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002548PyDoc_STRVAR(clear__doc__,
2549"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002550
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002551PyDoc_STRVAR(copy__doc__,
2552"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00002553
Guido van Rossumb90c8482007-02-10 01:11:45 +00002554/* Forward */
2555static PyObject *dictkeys_new(PyObject *);
2556static PyObject *dictitems_new(PyObject *);
2557static PyObject *dictvalues_new(PyObject *);
2558
Guido van Rossum45c85d12007-07-27 16:31:40 +00002559PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002560 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002561PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002562 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002563PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002564 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00002565
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002566static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07002567 DICT___CONTAINS___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002568 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
2569 getitem__doc__},
2570 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
2571 sizeof__doc__},
2572 {"get", (PyCFunction)dict_get, METH_VARARGS,
2573 get__doc__},
2574 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
2575 setdefault_doc__},
2576 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
2577 pop__doc__},
2578 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
2579 popitem__doc__},
2580 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
2581 keys__doc__},
2582 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
2583 items__doc__},
2584 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
2585 values__doc__},
2586 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
2587 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08002588 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002589 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
2590 clear__doc__},
2591 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
2592 copy__doc__},
2593 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002594};
2595
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002596/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002597int
2598PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002599{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002600 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002601 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002602 PyDictKeyEntry *ep;
2603 PyObject **value_addr;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002605 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002606 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002607 hash = PyObject_Hash(key);
2608 if (hash == -1)
2609 return -1;
2610 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002611 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
2612 return (ep == NULL) ? -1 : (*value_addr != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002613}
2614
Thomas Wouterscf297e42007-02-23 15:07:44 +00002615/* Internal version of PyDict_Contains used when the hash value is already known */
2616int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002617_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00002618{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002619 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002620 PyDictKeyEntry *ep;
2621 PyObject **value_addr;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002622
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002623 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
2624 return (ep == NULL) ? -1 : (*value_addr != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00002625}
2626
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002627/* Hack to implement "key in dict" */
2628static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002629 0, /* sq_length */
2630 0, /* sq_concat */
2631 0, /* sq_repeat */
2632 0, /* sq_item */
2633 0, /* sq_slice */
2634 0, /* sq_ass_item */
2635 0, /* sq_ass_slice */
2636 PyDict_Contains, /* sq_contains */
2637 0, /* sq_inplace_concat */
2638 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002639};
2640
Guido van Rossum09e563a2001-05-01 12:10:21 +00002641static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002642dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2643{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002644 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02002645 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002647 assert(type != NULL && type->tp_alloc != NULL);
2648 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02002649 if (self == NULL)
2650 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02002651 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02002652
Victor Stinnera9f61a52013-07-16 22:17:26 +02002653 /* The object has been implicitly tracked by tp_alloc */
2654 if (type == &PyDict_Type)
2655 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02002656
2657 d->ma_used = 0;
2658 d->ma_keys = new_keys_object(PyDict_MINSIZE_COMBINED);
2659 if (d->ma_keys == NULL) {
2660 Py_DECREF(self);
2661 return NULL;
2662 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002663 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002664}
2665
Tim Peters25786c02001-09-02 08:22:48 +00002666static int
2667dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2668{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002669 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002670}
2671
Tim Peters6d6c1a32001-08-02 04:15:00 +00002672static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002673dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002674{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002675 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002676}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002677
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002678PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002679"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002680"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002681" (key, value) pairs\n"
2682"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002683" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002684" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002685" d[k] = v\n"
2686"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2687" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002688
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002689PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002690 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2691 "dict",
2692 sizeof(PyDictObject),
2693 0,
2694 (destructor)dict_dealloc, /* tp_dealloc */
2695 0, /* tp_print */
2696 0, /* tp_getattr */
2697 0, /* tp_setattr */
2698 0, /* tp_reserved */
2699 (reprfunc)dict_repr, /* tp_repr */
2700 0, /* tp_as_number */
2701 &dict_as_sequence, /* tp_as_sequence */
2702 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002703 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002704 0, /* tp_call */
2705 0, /* tp_str */
2706 PyObject_GenericGetAttr, /* tp_getattro */
2707 0, /* tp_setattro */
2708 0, /* tp_as_buffer */
2709 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2710 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
2711 dictionary_doc, /* tp_doc */
2712 dict_traverse, /* tp_traverse */
2713 dict_tp_clear, /* tp_clear */
2714 dict_richcompare, /* tp_richcompare */
2715 0, /* tp_weaklistoffset */
2716 (getiterfunc)dict_iter, /* tp_iter */
2717 0, /* tp_iternext */
2718 mapp_methods, /* tp_methods */
2719 0, /* tp_members */
2720 0, /* tp_getset */
2721 0, /* tp_base */
2722 0, /* tp_dict */
2723 0, /* tp_descr_get */
2724 0, /* tp_descr_set */
2725 0, /* tp_dictoffset */
2726 dict_init, /* tp_init */
2727 PyType_GenericAlloc, /* tp_alloc */
2728 dict_new, /* tp_new */
2729 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002730};
2731
Victor Stinner3c1e4812012-03-26 22:10:51 +02002732PyObject *
2733_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
2734{
2735 PyObject *kv;
2736 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02002737 if (kv == NULL) {
2738 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002739 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02002740 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02002741 return PyDict_GetItem(dp, kv);
2742}
2743
Guido van Rossum3cca2451997-05-16 14:23:33 +00002744/* For backward compatibility with old dictionary interface */
2745
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002746PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002747PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002748{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002749 PyObject *kv, *rv;
2750 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02002751 if (kv == NULL) {
2752 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002753 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02002754 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002755 rv = PyDict_GetItem(v, kv);
2756 Py_DECREF(kv);
2757 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002758}
2759
2760int
Victor Stinner3c1e4812012-03-26 22:10:51 +02002761_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
2762{
2763 PyObject *kv;
2764 kv = _PyUnicode_FromId(key); /* borrowed */
2765 if (kv == NULL)
2766 return -1;
2767 return PyDict_SetItem(v, kv, item);
2768}
2769
2770int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002771PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002772{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002773 PyObject *kv;
2774 int err;
2775 kv = PyUnicode_FromString(key);
2776 if (kv == NULL)
2777 return -1;
2778 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
2779 err = PyDict_SetItem(v, kv, item);
2780 Py_DECREF(kv);
2781 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002782}
2783
2784int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01002785_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
2786{
2787 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
2788 if (kv == NULL)
2789 return -1;
2790 return PyDict_DelItem(v, kv);
2791}
2792
2793int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002794PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002795{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002796 PyObject *kv;
2797 int err;
2798 kv = PyUnicode_FromString(key);
2799 if (kv == NULL)
2800 return -1;
2801 err = PyDict_DelItem(v, kv);
2802 Py_DECREF(kv);
2803 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002804}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002805
Raymond Hettinger019a1482004-03-18 02:41:19 +00002806/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002807
2808typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002809 PyObject_HEAD
2810 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
2811 Py_ssize_t di_used;
2812 Py_ssize_t di_pos;
2813 PyObject* di_result; /* reusable result tuple for iteritems */
2814 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002815} dictiterobject;
2816
2817static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002818dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002819{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002820 dictiterobject *di;
2821 di = PyObject_GC_New(dictiterobject, itertype);
2822 if (di == NULL)
2823 return NULL;
2824 Py_INCREF(dict);
2825 di->di_dict = dict;
2826 di->di_used = dict->ma_used;
2827 di->di_pos = 0;
2828 di->len = dict->ma_used;
2829 if (itertype == &PyDictIterItem_Type) {
2830 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2831 if (di->di_result == NULL) {
2832 Py_DECREF(di);
2833 return NULL;
2834 }
2835 }
2836 else
2837 di->di_result = NULL;
2838 _PyObject_GC_TRACK(di);
2839 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002840}
2841
2842static void
2843dictiter_dealloc(dictiterobject *di)
2844{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002845 Py_XDECREF(di->di_dict);
2846 Py_XDECREF(di->di_result);
2847 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002848}
2849
2850static int
2851dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
2852{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002853 Py_VISIT(di->di_dict);
2854 Py_VISIT(di->di_result);
2855 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002856}
2857
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002858static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002859dictiter_len(dictiterobject *di)
2860{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002861 Py_ssize_t len = 0;
2862 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
2863 len = di->len;
2864 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002865}
2866
Guido van Rossumb90c8482007-02-10 01:11:45 +00002867PyDoc_STRVAR(length_hint_doc,
2868 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002869
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002870static PyObject *
2871dictiter_reduce(dictiterobject *di);
2872
2873PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
2874
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002875static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
2877 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002878 {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS,
2879 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002880 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002881};
2882
Raymond Hettinger019a1482004-03-18 02:41:19 +00002883static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002884{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002885 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002886 Py_ssize_t i, mask, offset;
2887 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002888 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002889 PyObject **value_ptr;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002891 if (d == NULL)
2892 return NULL;
2893 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002895 if (di->di_used != d->ma_used) {
2896 PyErr_SetString(PyExc_RuntimeError,
2897 "dictionary changed size during iteration");
2898 di->di_used = -1; /* Make this state sticky */
2899 return NULL;
2900 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002902 i = di->di_pos;
2903 if (i < 0)
2904 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002905 k = d->ma_keys;
2906 if (d->ma_values) {
2907 value_ptr = &d->ma_values[i];
2908 offset = sizeof(PyObject *);
2909 }
2910 else {
2911 value_ptr = &k->dk_entries[i].me_value;
2912 offset = sizeof(PyDictKeyEntry);
2913 }
2914 mask = DK_SIZE(k)-1;
2915 while (i <= mask && *value_ptr == NULL) {
2916 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002918 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002919 di->di_pos = i+1;
2920 if (i > mask)
2921 goto fail;
2922 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002923 key = k->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002924 Py_INCREF(key);
2925 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002926
2927fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002928 Py_DECREF(d);
2929 di->di_dict = NULL;
2930 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002931}
2932
Raymond Hettinger019a1482004-03-18 02:41:19 +00002933PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002934 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2935 "dict_keyiterator", /* tp_name */
2936 sizeof(dictiterobject), /* tp_basicsize */
2937 0, /* tp_itemsize */
2938 /* methods */
2939 (destructor)dictiter_dealloc, /* tp_dealloc */
2940 0, /* tp_print */
2941 0, /* tp_getattr */
2942 0, /* tp_setattr */
2943 0, /* tp_reserved */
2944 0, /* tp_repr */
2945 0, /* tp_as_number */
2946 0, /* tp_as_sequence */
2947 0, /* tp_as_mapping */
2948 0, /* tp_hash */
2949 0, /* tp_call */
2950 0, /* tp_str */
2951 PyObject_GenericGetAttr, /* tp_getattro */
2952 0, /* tp_setattro */
2953 0, /* tp_as_buffer */
2954 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2955 0, /* tp_doc */
2956 (traverseproc)dictiter_traverse, /* tp_traverse */
2957 0, /* tp_clear */
2958 0, /* tp_richcompare */
2959 0, /* tp_weaklistoffset */
2960 PyObject_SelfIter, /* tp_iter */
2961 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
2962 dictiter_methods, /* tp_methods */
2963 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002964};
2965
2966static PyObject *dictiter_iternextvalue(dictiterobject *di)
2967{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002968 PyObject *value;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002969 Py_ssize_t i, mask, offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002970 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002971 PyObject **value_ptr;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002973 if (d == NULL)
2974 return NULL;
2975 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00002976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002977 if (di->di_used != d->ma_used) {
2978 PyErr_SetString(PyExc_RuntimeError,
2979 "dictionary changed size during iteration");
2980 di->di_used = -1; /* Make this state sticky */
2981 return NULL;
2982 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002984 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002985 mask = DK_SIZE(d->ma_keys)-1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002986 if (i < 0 || i > mask)
2987 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002988 if (d->ma_values) {
2989 value_ptr = &d->ma_values[i];
2990 offset = sizeof(PyObject *);
2991 }
2992 else {
2993 value_ptr = &d->ma_keys->dk_entries[i].me_value;
2994 offset = sizeof(PyDictKeyEntry);
2995 }
2996 while (i <= mask && *value_ptr == NULL) {
2997 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002998 i++;
2999 if (i > mask)
3000 goto fail;
3001 }
3002 di->di_pos = i+1;
3003 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003004 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003005 Py_INCREF(value);
3006 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003007
3008fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003009 Py_DECREF(d);
3010 di->di_dict = NULL;
3011 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003012}
3013
3014PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003015 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3016 "dict_valueiterator", /* tp_name */
3017 sizeof(dictiterobject), /* tp_basicsize */
3018 0, /* tp_itemsize */
3019 /* methods */
3020 (destructor)dictiter_dealloc, /* tp_dealloc */
3021 0, /* tp_print */
3022 0, /* tp_getattr */
3023 0, /* tp_setattr */
3024 0, /* tp_reserved */
3025 0, /* tp_repr */
3026 0, /* tp_as_number */
3027 0, /* tp_as_sequence */
3028 0, /* tp_as_mapping */
3029 0, /* tp_hash */
3030 0, /* tp_call */
3031 0, /* tp_str */
3032 PyObject_GenericGetAttr, /* tp_getattro */
3033 0, /* tp_setattro */
3034 0, /* tp_as_buffer */
3035 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3036 0, /* tp_doc */
3037 (traverseproc)dictiter_traverse, /* tp_traverse */
3038 0, /* tp_clear */
3039 0, /* tp_richcompare */
3040 0, /* tp_weaklistoffset */
3041 PyObject_SelfIter, /* tp_iter */
3042 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3043 dictiter_methods, /* tp_methods */
3044 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003045};
3046
3047static PyObject *dictiter_iternextitem(dictiterobject *di)
3048{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003049 PyObject *key, *value, *result = di->di_result;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003050 Py_ssize_t i, mask, offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003051 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003052 PyObject **value_ptr;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003054 if (d == NULL)
3055 return NULL;
3056 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003058 if (di->di_used != d->ma_used) {
3059 PyErr_SetString(PyExc_RuntimeError,
3060 "dictionary changed size during iteration");
3061 di->di_used = -1; /* Make this state sticky */
3062 return NULL;
3063 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003065 i = di->di_pos;
3066 if (i < 0)
3067 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003068 mask = DK_SIZE(d->ma_keys)-1;
3069 if (d->ma_values) {
3070 value_ptr = &d->ma_values[i];
3071 offset = sizeof(PyObject *);
3072 }
3073 else {
3074 value_ptr = &d->ma_keys->dk_entries[i].me_value;
3075 offset = sizeof(PyDictKeyEntry);
3076 }
3077 while (i <= mask && *value_ptr == NULL) {
3078 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003079 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003080 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003081 di->di_pos = i+1;
3082 if (i > mask)
3083 goto fail;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003085 if (result->ob_refcnt == 1) {
3086 Py_INCREF(result);
3087 Py_DECREF(PyTuple_GET_ITEM(result, 0));
3088 Py_DECREF(PyTuple_GET_ITEM(result, 1));
3089 } else {
3090 result = PyTuple_New(2);
3091 if (result == NULL)
3092 return NULL;
3093 }
3094 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003095 key = d->ma_keys->dk_entries[i].me_key;
3096 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003097 Py_INCREF(key);
3098 Py_INCREF(value);
3099 PyTuple_SET_ITEM(result, 0, key);
3100 PyTuple_SET_ITEM(result, 1, value);
3101 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003102
3103fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003104 Py_DECREF(d);
3105 di->di_dict = NULL;
3106 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003107}
3108
3109PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003110 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3111 "dict_itemiterator", /* tp_name */
3112 sizeof(dictiterobject), /* tp_basicsize */
3113 0, /* tp_itemsize */
3114 /* methods */
3115 (destructor)dictiter_dealloc, /* tp_dealloc */
3116 0, /* tp_print */
3117 0, /* tp_getattr */
3118 0, /* tp_setattr */
3119 0, /* tp_reserved */
3120 0, /* tp_repr */
3121 0, /* tp_as_number */
3122 0, /* tp_as_sequence */
3123 0, /* tp_as_mapping */
3124 0, /* tp_hash */
3125 0, /* tp_call */
3126 0, /* tp_str */
3127 PyObject_GenericGetAttr, /* tp_getattro */
3128 0, /* tp_setattro */
3129 0, /* tp_as_buffer */
3130 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3131 0, /* tp_doc */
3132 (traverseproc)dictiter_traverse, /* tp_traverse */
3133 0, /* tp_clear */
3134 0, /* tp_richcompare */
3135 0, /* tp_weaklistoffset */
3136 PyObject_SelfIter, /* tp_iter */
3137 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3138 dictiter_methods, /* tp_methods */
3139 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003140};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003141
3142
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003143static PyObject *
3144dictiter_reduce(dictiterobject *di)
3145{
3146 PyObject *list;
3147 dictiterobject tmp;
3148
3149 list = PyList_New(0);
3150 if (!list)
3151 return NULL;
3152
3153 /* copy the itertor state */
3154 tmp = *di;
3155 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003156
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003157 /* iterate the temporary into a list */
3158 for(;;) {
3159 PyObject *element = 0;
3160 if (Py_TYPE(di) == &PyDictIterItem_Type)
3161 element = dictiter_iternextitem(&tmp);
3162 else if (Py_TYPE(di) == &PyDictIterKey_Type)
3163 element = dictiter_iternextkey(&tmp);
3164 else if (Py_TYPE(di) == &PyDictIterValue_Type)
3165 element = dictiter_iternextvalue(&tmp);
3166 else
3167 assert(0);
3168 if (element) {
3169 if (PyList_Append(list, element)) {
3170 Py_DECREF(element);
3171 Py_DECREF(list);
3172 Py_XDECREF(tmp.di_dict);
3173 return NULL;
3174 }
3175 Py_DECREF(element);
3176 } else
3177 break;
3178 }
3179 Py_XDECREF(tmp.di_dict);
3180 /* check for error */
3181 if (tmp.di_dict != NULL) {
3182 /* we have an error */
3183 Py_DECREF(list);
3184 return NULL;
3185 }
Antoine Pitroua7013882012-04-05 00:04:20 +02003186 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003187}
3188
Guido van Rossum3ac67412007-02-10 18:55:06 +00003189/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003190/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003191/***********************************************/
3192
Guido van Rossumb90c8482007-02-10 01:11:45 +00003193/* The instance lay-out is the same for all three; but the type differs. */
3194
3195typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003196 PyObject_HEAD
3197 PyDictObject *dv_dict;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003198} dictviewobject;
3199
3200
3201static void
Guido van Rossum3ac67412007-02-10 18:55:06 +00003202dictview_dealloc(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003203{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003204 Py_XDECREF(dv->dv_dict);
3205 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003206}
3207
3208static int
3209dictview_traverse(dictviewobject *dv, visitproc visit, void *arg)
3210{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003211 Py_VISIT(dv->dv_dict);
3212 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003213}
3214
Guido van Rossum83825ac2007-02-10 04:54:19 +00003215static Py_ssize_t
Guido van Rossum3ac67412007-02-10 18:55:06 +00003216dictview_len(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003217{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003218 Py_ssize_t len = 0;
3219 if (dv->dv_dict != NULL)
3220 len = dv->dv_dict->ma_used;
3221 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003222}
3223
3224static PyObject *
3225dictview_new(PyObject *dict, PyTypeObject *type)
3226{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003227 dictviewobject *dv;
3228 if (dict == NULL) {
3229 PyErr_BadInternalCall();
3230 return NULL;
3231 }
3232 if (!PyDict_Check(dict)) {
3233 /* XXX Get rid of this restriction later */
3234 PyErr_Format(PyExc_TypeError,
3235 "%s() requires a dict argument, not '%s'",
3236 type->tp_name, dict->ob_type->tp_name);
3237 return NULL;
3238 }
3239 dv = PyObject_GC_New(dictviewobject, type);
3240 if (dv == NULL)
3241 return NULL;
3242 Py_INCREF(dict);
3243 dv->dv_dict = (PyDictObject *)dict;
3244 _PyObject_GC_TRACK(dv);
3245 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003246}
3247
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003248/* TODO(guido): The views objects are not complete:
3249
3250 * support more set operations
3251 * support arbitrary mappings?
3252 - either these should be static or exported in dictobject.h
3253 - if public then they should probably be in builtins
3254*/
3255
Guido van Rossumaac530c2007-08-24 22:33:45 +00003256/* Return 1 if self is a subset of other, iterating over self;
3257 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003258static int
3259all_contained_in(PyObject *self, PyObject *other)
3260{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003261 PyObject *iter = PyObject_GetIter(self);
3262 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003264 if (iter == NULL)
3265 return -1;
3266 for (;;) {
3267 PyObject *next = PyIter_Next(iter);
3268 if (next == NULL) {
3269 if (PyErr_Occurred())
3270 ok = -1;
3271 break;
3272 }
3273 ok = PySequence_Contains(other, next);
3274 Py_DECREF(next);
3275 if (ok <= 0)
3276 break;
3277 }
3278 Py_DECREF(iter);
3279 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003280}
3281
3282static PyObject *
3283dictview_richcompare(PyObject *self, PyObject *other, int op)
3284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003285 Py_ssize_t len_self, len_other;
3286 int ok;
3287 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003289 assert(self != NULL);
3290 assert(PyDictViewSet_Check(self));
3291 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00003292
Brian Curtindfc80e32011-08-10 20:28:54 -05003293 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
3294 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003296 len_self = PyObject_Size(self);
3297 if (len_self < 0)
3298 return NULL;
3299 len_other = PyObject_Size(other);
3300 if (len_other < 0)
3301 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003303 ok = 0;
3304 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00003305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003306 case Py_NE:
3307 case Py_EQ:
3308 if (len_self == len_other)
3309 ok = all_contained_in(self, other);
3310 if (op == Py_NE && ok >= 0)
3311 ok = !ok;
3312 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003314 case Py_LT:
3315 if (len_self < len_other)
3316 ok = all_contained_in(self, other);
3317 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003319 case Py_LE:
3320 if (len_self <= len_other)
3321 ok = all_contained_in(self, other);
3322 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003324 case Py_GT:
3325 if (len_self > len_other)
3326 ok = all_contained_in(other, self);
3327 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003329 case Py_GE:
3330 if (len_self >= len_other)
3331 ok = all_contained_in(other, self);
3332 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003334 }
3335 if (ok < 0)
3336 return NULL;
3337 result = ok ? Py_True : Py_False;
3338 Py_INCREF(result);
3339 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003340}
3341
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003342static PyObject *
3343dictview_repr(dictviewobject *dv)
3344{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003345 PyObject *seq;
3346 PyObject *result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003348 seq = PySequence_List((PyObject *)dv);
3349 if (seq == NULL)
3350 return NULL;
3351
3352 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
3353 Py_DECREF(seq);
3354 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003355}
3356
Guido van Rossum3ac67412007-02-10 18:55:06 +00003357/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003358
3359static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003360dictkeys_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003361{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003362 if (dv->dv_dict == NULL) {
3363 Py_RETURN_NONE;
3364 }
3365 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003366}
3367
3368static int
3369dictkeys_contains(dictviewobject *dv, PyObject *obj)
3370{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003371 if (dv->dv_dict == NULL)
3372 return 0;
3373 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003374}
3375
Guido van Rossum83825ac2007-02-10 04:54:19 +00003376static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003377 (lenfunc)dictview_len, /* sq_length */
3378 0, /* sq_concat */
3379 0, /* sq_repeat */
3380 0, /* sq_item */
3381 0, /* sq_slice */
3382 0, /* sq_ass_item */
3383 0, /* sq_ass_slice */
3384 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003385};
3386
Guido van Rossum523259b2007-08-24 23:41:22 +00003387static PyObject*
3388dictviews_sub(PyObject* self, PyObject *other)
3389{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003390 PyObject *result = PySet_New(self);
3391 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003392 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003394 if (result == NULL)
3395 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003396
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003397 tmp = _PyObject_CallMethodId(result, &PyId_difference_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003398 if (tmp == NULL) {
3399 Py_DECREF(result);
3400 return NULL;
3401 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003403 Py_DECREF(tmp);
3404 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003405}
3406
3407static PyObject*
3408dictviews_and(PyObject* self, PyObject *other)
3409{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003410 PyObject *result = PySet_New(self);
3411 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003412 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003414 if (result == NULL)
3415 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003416
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003417 tmp = _PyObject_CallMethodId(result, &PyId_intersection_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003418 if (tmp == NULL) {
3419 Py_DECREF(result);
3420 return NULL;
3421 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003423 Py_DECREF(tmp);
3424 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003425}
3426
3427static PyObject*
3428dictviews_or(PyObject* self, PyObject *other)
3429{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003430 PyObject *result = PySet_New(self);
3431 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003432 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003434 if (result == NULL)
3435 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003436
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003437 tmp = _PyObject_CallMethodId(result, &PyId_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003438 if (tmp == NULL) {
3439 Py_DECREF(result);
3440 return NULL;
3441 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003443 Py_DECREF(tmp);
3444 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003445}
3446
3447static PyObject*
3448dictviews_xor(PyObject* self, PyObject *other)
3449{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003450 PyObject *result = PySet_New(self);
3451 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003452 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003454 if (result == NULL)
3455 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003456
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003457 tmp = _PyObject_CallMethodId(result, &PyId_symmetric_difference_update, "O",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003458 other);
3459 if (tmp == NULL) {
3460 Py_DECREF(result);
3461 return NULL;
3462 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003464 Py_DECREF(tmp);
3465 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003466}
3467
3468static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003469 0, /*nb_add*/
3470 (binaryfunc)dictviews_sub, /*nb_subtract*/
3471 0, /*nb_multiply*/
3472 0, /*nb_remainder*/
3473 0, /*nb_divmod*/
3474 0, /*nb_power*/
3475 0, /*nb_negative*/
3476 0, /*nb_positive*/
3477 0, /*nb_absolute*/
3478 0, /*nb_bool*/
3479 0, /*nb_invert*/
3480 0, /*nb_lshift*/
3481 0, /*nb_rshift*/
3482 (binaryfunc)dictviews_and, /*nb_and*/
3483 (binaryfunc)dictviews_xor, /*nb_xor*/
3484 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00003485};
3486
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003487static PyObject*
3488dictviews_isdisjoint(PyObject *self, PyObject *other)
3489{
3490 PyObject *it;
3491 PyObject *item = NULL;
3492
3493 if (self == other) {
3494 if (dictview_len((dictviewobject *)self) == 0)
3495 Py_RETURN_TRUE;
3496 else
3497 Py_RETURN_FALSE;
3498 }
3499
3500 /* Iterate over the shorter object (only if other is a set,
3501 * because PySequence_Contains may be expensive otherwise): */
3502 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
3503 Py_ssize_t len_self = dictview_len((dictviewobject *)self);
3504 Py_ssize_t len_other = PyObject_Size(other);
3505 if (len_other == -1)
3506 return NULL;
3507
3508 if ((len_other > len_self)) {
3509 PyObject *tmp = other;
3510 other = self;
3511 self = tmp;
3512 }
3513 }
3514
3515 it = PyObject_GetIter(other);
3516 if (it == NULL)
3517 return NULL;
3518
3519 while ((item = PyIter_Next(it)) != NULL) {
3520 int contains = PySequence_Contains(self, item);
3521 Py_DECREF(item);
3522 if (contains == -1) {
3523 Py_DECREF(it);
3524 return NULL;
3525 }
3526
3527 if (contains) {
3528 Py_DECREF(it);
3529 Py_RETURN_FALSE;
3530 }
3531 }
3532 Py_DECREF(it);
3533 if (PyErr_Occurred())
3534 return NULL; /* PyIter_Next raised an exception. */
3535 Py_RETURN_TRUE;
3536}
3537
3538PyDoc_STRVAR(isdisjoint_doc,
3539"Return True if the view and the given iterable have a null intersection.");
3540
Guido van Rossumb90c8482007-02-10 01:11:45 +00003541static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003542 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3543 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003544 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003545};
3546
3547PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003548 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3549 "dict_keys", /* tp_name */
3550 sizeof(dictviewobject), /* tp_basicsize */
3551 0, /* tp_itemsize */
3552 /* methods */
3553 (destructor)dictview_dealloc, /* tp_dealloc */
3554 0, /* tp_print */
3555 0, /* tp_getattr */
3556 0, /* tp_setattr */
3557 0, /* tp_reserved */
3558 (reprfunc)dictview_repr, /* tp_repr */
3559 &dictviews_as_number, /* tp_as_number */
3560 &dictkeys_as_sequence, /* tp_as_sequence */
3561 0, /* tp_as_mapping */
3562 0, /* tp_hash */
3563 0, /* tp_call */
3564 0, /* tp_str */
3565 PyObject_GenericGetAttr, /* tp_getattro */
3566 0, /* tp_setattro */
3567 0, /* tp_as_buffer */
3568 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3569 0, /* tp_doc */
3570 (traverseproc)dictview_traverse, /* tp_traverse */
3571 0, /* tp_clear */
3572 dictview_richcompare, /* tp_richcompare */
3573 0, /* tp_weaklistoffset */
3574 (getiterfunc)dictkeys_iter, /* tp_iter */
3575 0, /* tp_iternext */
3576 dictkeys_methods, /* tp_methods */
3577 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003578};
3579
3580static PyObject *
3581dictkeys_new(PyObject *dict)
3582{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003583 return dictview_new(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003584}
3585
Guido van Rossum3ac67412007-02-10 18:55:06 +00003586/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003587
3588static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003589dictitems_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003590{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003591 if (dv->dv_dict == NULL) {
3592 Py_RETURN_NONE;
3593 }
3594 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003595}
3596
3597static int
3598dictitems_contains(dictviewobject *dv, PyObject *obj)
3599{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003600 PyObject *key, *value, *found;
3601 if (dv->dv_dict == NULL)
3602 return 0;
3603 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
3604 return 0;
3605 key = PyTuple_GET_ITEM(obj, 0);
3606 value = PyTuple_GET_ITEM(obj, 1);
3607 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
3608 if (found == NULL) {
3609 if (PyErr_Occurred())
3610 return -1;
3611 return 0;
3612 }
3613 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003614}
3615
Guido van Rossum83825ac2007-02-10 04:54:19 +00003616static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003617 (lenfunc)dictview_len, /* sq_length */
3618 0, /* sq_concat */
3619 0, /* sq_repeat */
3620 0, /* sq_item */
3621 0, /* sq_slice */
3622 0, /* sq_ass_item */
3623 0, /* sq_ass_slice */
3624 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003625};
3626
Guido van Rossumb90c8482007-02-10 01:11:45 +00003627static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003628 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3629 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003630 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003631};
3632
3633PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003634 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3635 "dict_items", /* tp_name */
3636 sizeof(dictviewobject), /* tp_basicsize */
3637 0, /* tp_itemsize */
3638 /* methods */
3639 (destructor)dictview_dealloc, /* tp_dealloc */
3640 0, /* tp_print */
3641 0, /* tp_getattr */
3642 0, /* tp_setattr */
3643 0, /* tp_reserved */
3644 (reprfunc)dictview_repr, /* tp_repr */
3645 &dictviews_as_number, /* tp_as_number */
3646 &dictitems_as_sequence, /* tp_as_sequence */
3647 0, /* tp_as_mapping */
3648 0, /* tp_hash */
3649 0, /* tp_call */
3650 0, /* tp_str */
3651 PyObject_GenericGetAttr, /* tp_getattro */
3652 0, /* tp_setattro */
3653 0, /* tp_as_buffer */
3654 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3655 0, /* tp_doc */
3656 (traverseproc)dictview_traverse, /* tp_traverse */
3657 0, /* tp_clear */
3658 dictview_richcompare, /* tp_richcompare */
3659 0, /* tp_weaklistoffset */
3660 (getiterfunc)dictitems_iter, /* tp_iter */
3661 0, /* tp_iternext */
3662 dictitems_methods, /* tp_methods */
3663 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003664};
3665
3666static PyObject *
3667dictitems_new(PyObject *dict)
3668{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003669 return dictview_new(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003670}
3671
Guido van Rossum3ac67412007-02-10 18:55:06 +00003672/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003673
3674static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003675dictvalues_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003676{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003677 if (dv->dv_dict == NULL) {
3678 Py_RETURN_NONE;
3679 }
3680 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003681}
3682
Guido van Rossum83825ac2007-02-10 04:54:19 +00003683static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003684 (lenfunc)dictview_len, /* sq_length */
3685 0, /* sq_concat */
3686 0, /* sq_repeat */
3687 0, /* sq_item */
3688 0, /* sq_slice */
3689 0, /* sq_ass_item */
3690 0, /* sq_ass_slice */
3691 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003692};
3693
Guido van Rossumb90c8482007-02-10 01:11:45 +00003694static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003695 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003696};
3697
3698PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003699 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3700 "dict_values", /* tp_name */
3701 sizeof(dictviewobject), /* tp_basicsize */
3702 0, /* tp_itemsize */
3703 /* methods */
3704 (destructor)dictview_dealloc, /* tp_dealloc */
3705 0, /* tp_print */
3706 0, /* tp_getattr */
3707 0, /* tp_setattr */
3708 0, /* tp_reserved */
3709 (reprfunc)dictview_repr, /* tp_repr */
3710 0, /* tp_as_number */
3711 &dictvalues_as_sequence, /* tp_as_sequence */
3712 0, /* tp_as_mapping */
3713 0, /* tp_hash */
3714 0, /* tp_call */
3715 0, /* tp_str */
3716 PyObject_GenericGetAttr, /* tp_getattro */
3717 0, /* tp_setattro */
3718 0, /* tp_as_buffer */
3719 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3720 0, /* tp_doc */
3721 (traverseproc)dictview_traverse, /* tp_traverse */
3722 0, /* tp_clear */
3723 0, /* tp_richcompare */
3724 0, /* tp_weaklistoffset */
3725 (getiterfunc)dictvalues_iter, /* tp_iter */
3726 0, /* tp_iternext */
3727 dictvalues_methods, /* tp_methods */
3728 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003729};
3730
3731static PyObject *
3732dictvalues_new(PyObject *dict)
3733{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003734 return dictview_new(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003735}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003736
3737/* Returns NULL if cannot allocate a new PyDictKeysObject,
3738 but does not set an error */
3739PyDictKeysObject *
3740_PyDict_NewKeysForClass(void)
3741{
3742 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE_SPLIT);
3743 if (keys == NULL)
3744 PyErr_Clear();
3745 else
3746 keys->dk_lookup = lookdict_split;
3747 return keys;
3748}
3749
3750#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
3751
3752PyObject *
3753PyObject_GenericGetDict(PyObject *obj, void *context)
3754{
3755 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
3756 if (dictptr == NULL) {
3757 PyErr_SetString(PyExc_AttributeError,
3758 "This object has no __dict__");
3759 return NULL;
3760 }
3761 dict = *dictptr;
3762 if (dict == NULL) {
3763 PyTypeObject *tp = Py_TYPE(obj);
3764 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
3765 DK_INCREF(CACHED_KEYS(tp));
3766 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
3767 }
3768 else {
3769 *dictptr = dict = PyDict_New();
3770 }
3771 }
3772 Py_XINCREF(dict);
3773 return dict;
3774}
3775
3776int
3777_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
3778 PyObject *key, PyObject *value)
3779{
3780 PyObject *dict;
3781 int res;
3782 PyDictKeysObject *cached;
3783
3784 assert(dictptr != NULL);
3785 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
3786 assert(dictptr != NULL);
3787 dict = *dictptr;
3788 if (dict == NULL) {
3789 DK_INCREF(cached);
3790 dict = new_dict_with_shared_keys(cached);
3791 if (dict == NULL)
3792 return -1;
3793 *dictptr = dict;
3794 }
3795 if (value == NULL) {
3796 res = PyDict_DelItem(dict, key);
3797 if (cached != ((PyDictObject *)dict)->ma_keys) {
3798 CACHED_KEYS(tp) = NULL;
3799 DK_DECREF(cached);
3800 }
3801 } else {
3802 res = PyDict_SetItem(dict, key, value);
3803 if (cached != ((PyDictObject *)dict)->ma_keys) {
3804 /* Either update tp->ht_cached_keys or delete it */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04003805 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003806 CACHED_KEYS(tp) = make_keys_shared(dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003807 } else {
3808 CACHED_KEYS(tp) = NULL;
3809 }
3810 DK_DECREF(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04003811 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
3812 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003813 }
3814 }
3815 } else {
3816 dict = *dictptr;
3817 if (dict == NULL) {
3818 dict = PyDict_New();
3819 if (dict == NULL)
3820 return -1;
3821 *dictptr = dict;
3822 }
3823 if (value == NULL) {
3824 res = PyDict_DelItem(dict, key);
3825 } else {
3826 res = PyDict_SetItem(dict, key, value);
3827 }
3828 }
3829 return res;
3830}
3831
3832void
3833_PyDictKeys_DecRef(PyDictKeysObject *keys)
3834{
3835 DK_DECREF(keys);
3836}
3837
3838
3839/* ARGSUSED */
3840static PyObject *
3841dummy_repr(PyObject *op)
3842{
3843 return PyUnicode_FromString("<dummy key>");
3844}
3845
3846/* ARGUSED */
3847static void
3848dummy_dealloc(PyObject* ignore)
3849{
3850 /* This should never get called, but we also don't want to SEGV if
3851 * we accidentally decref dummy-key out of existence.
3852 */
3853 Py_FatalError("deallocating <dummy key>");
3854}
3855
3856static PyTypeObject PyDictDummy_Type = {
3857 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3858 "<dummy key> type",
3859 0,
3860 0,
3861 dummy_dealloc, /*tp_dealloc*/ /*never called*/
3862 0, /*tp_print*/
3863 0, /*tp_getattr*/
3864 0, /*tp_setattr*/
3865 0, /*tp_reserved*/
3866 dummy_repr, /*tp_repr*/
3867 0, /*tp_as_number*/
3868 0, /*tp_as_sequence*/
3869 0, /*tp_as_mapping*/
3870 0, /*tp_hash */
3871 0, /*tp_call */
3872 0, /*tp_str */
3873 0, /*tp_getattro */
3874 0, /*tp_setattro */
3875 0, /*tp_as_buffer */
3876 Py_TPFLAGS_DEFAULT, /*tp_flags */
3877};
3878
3879static PyObject _dummy_struct = {
3880 _PyObject_EXTRA_INIT
3881 2, &PyDictDummy_Type
3882};
3883