blob: bbee1a61a3ab4ff86ddb8d02f2b33dff50751fcf [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
Benjamin Peterson7d95e402012-04-23 11:24:50 -040072typedef struct {
73 /* Cached hash code of me_key. */
74 Py_hash_t me_hash;
75 PyObject *me_key;
76 PyObject *me_value; /* This field is only meaningful for combined tables */
77} PyDictKeyEntry;
78
79typedef PyDictKeyEntry *(*dict_lookup_func)
80(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject ***value_addr);
81
82struct _dictkeysobject {
83 Py_ssize_t dk_refcnt;
84 Py_ssize_t dk_size;
85 dict_lookup_func dk_lookup;
86 Py_ssize_t dk_usable;
87 PyDictKeyEntry dk_entries[1];
88};
89
90
91/*
92To ensure the lookup algorithm terminates, there must be at least one Unused
93slot (NULL key) in the table.
94To avoid slowing down lookups on a near-full table, we resize the table when
95it's USABLE_FRACTION (currently two-thirds) full.
96*/
Guido van Rossum16e93a81997-01-28 00:00:11 +000097
Tim Peterseb28ef22001-06-02 05:27:19 +000098#define PERTURB_SHIFT 5
99
Guido van Rossum16e93a81997-01-28 00:00:11 +0000100/*
Tim Peterseb28ef22001-06-02 05:27:19 +0000101Major subtleties ahead: Most hash schemes depend on having a "good" hash
102function, in the sense of simulating randomness. Python doesn't: its most
103important hash functions (for strings and ints) are very regular in common
104cases:
Tim Peters15d49292001-05-27 07:39:22 +0000105
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000106 >>> map(hash, (0, 1, 2, 3))
107 [0, 1, 2, 3]
108 >>> map(hash, ("namea", "nameb", "namec", "named"))
109 [-1658398457, -1658398460, -1658398459, -1658398462]
110 >>>
Tim Peters15d49292001-05-27 07:39:22 +0000111
Tim Peterseb28ef22001-06-02 05:27:19 +0000112This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
113the low-order i bits as the initial table index is extremely fast, and there
114are no collisions at all for dicts indexed by a contiguous range of ints.
115The same is approximately true when keys are "consecutive" strings. So this
116gives better-than-random behavior in common cases, and that's very desirable.
Tim Peters15d49292001-05-27 07:39:22 +0000117
Tim Peterseb28ef22001-06-02 05:27:19 +0000118OTOH, when collisions occur, the tendency to fill contiguous slices of the
119hash table makes a good collision resolution strategy crucial. Taking only
120the last i bits of the hash code is also vulnerable: for example, consider
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121the list [i << 16 for i in range(20000)] as a set of keys. Since ints are
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000122their own hash codes, and this fits in a dict of size 2**15, the last 15 bits
123 of every hash code are all 0: they *all* map to the same table index.
Tim Peters15d49292001-05-27 07:39:22 +0000124
Tim Peterseb28ef22001-06-02 05:27:19 +0000125But catering to unusual cases should not slow the usual ones, so we just take
126the last i bits anyway. It's up to collision resolution to do the rest. If
127we *usually* find the key we're looking for on the first try (and, it turns
128out, we usually do -- the table load factor is kept under 2/3, so the odds
129are solidly in our favor), then it makes best sense to keep the initial index
130computation dirt cheap.
Tim Peters15d49292001-05-27 07:39:22 +0000131
Tim Peterseb28ef22001-06-02 05:27:19 +0000132The first half of collision resolution is to visit table indices via this
133recurrence:
Tim Peters15d49292001-05-27 07:39:22 +0000134
Tim Peterseb28ef22001-06-02 05:27:19 +0000135 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +0000136
Tim Peterseb28ef22001-06-02 05:27:19 +0000137For any initial j in range(2**i), repeating that 2**i times generates each
138int in range(2**i) exactly once (see any text on random-number generation for
139proof). By itself, this doesn't help much: like linear probing (setting
140j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
141order. This would be bad, except that's not the only thing we do, and it's
142actually *good* in the common cases where hash keys are consecutive. In an
143example that's really too small to make this entirely clear, for a table of
144size 2**3 the order of indices is:
Tim Peters15d49292001-05-27 07:39:22 +0000145
Tim Peterseb28ef22001-06-02 05:27:19 +0000146 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
147
148If two things come in at index 5, the first place we look after is index 2,
149not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
150Linear probing is deadly in this case because there the fixed probe order
151is the *same* as the order consecutive keys are likely to arrive. But it's
152extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
153and certain that consecutive hash codes do not.
154
155The other half of the strategy is to get the other bits of the hash code
156into play. This is done by initializing a (unsigned) vrbl "perturb" to the
157full hash code, and changing the recurrence to:
158
159 j = (5*j) + 1 + perturb;
160 perturb >>= PERTURB_SHIFT;
161 use j % 2**i as the next table index;
162
163Now the probe sequence depends (eventually) on every bit in the hash code,
164and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
165because it quickly magnifies small differences in the bits that didn't affect
166the initial index. Note that because perturb is unsigned, if the recurrence
167is executed often enough perturb eventually becomes and remains 0. At that
168point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
169that's certain to find an empty slot eventually (since it generates every int
170in range(2**i), and we make sure there's always at least one empty slot).
171
172Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
173small so that the high bits of the hash code continue to affect the probe
174sequence across iterations; but you want it large so that in really bad cases
175the high-order hash bits have an effect on early iterations. 5 was "the
176best" in minimizing total collisions across experiments Tim Peters ran (on
177both normal and pathological cases), but 4 and 6 weren't significantly worse.
178
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000179Historical: Reimer Behrends contributed the idea of using a polynomial-based
Tim Peterseb28ef22001-06-02 05:27:19 +0000180approach, using repeated multiplication by x in GF(2**n) where an irreducible
181polynomial for each table size was chosen such that x was a primitive root.
182Christian Tismer later extended that to use division by x instead, as an
183efficient way to get the high bits of the hash code into play. This scheme
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000184also gave excellent collision statistics, but was more expensive: two
185if-tests were required inside the loop; computing "the next" index took about
186the same number of operations but without as much potential parallelism
187(e.g., computing 5*j can go on at the same time as computing 1+perturb in the
188above, and then shifting perturb can be done while the table index is being
189masked); and the PyDictObject struct required a member to hold the table's
190polynomial. In Tim's experiments the current scheme ran faster, produced
191equally good collision statistics, needed less code & used less memory.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000192
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000193*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000194
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400195/* Object used as dummy key to fill deleted entries
196 * This could be any unique object,
197 * use a custom type in order to minimise coupling.
198*/
199static PyObject _dummy_struct;
200
201#define dummy (&_dummy_struct)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000202
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000203#ifdef Py_REF_DEBUG
204PyObject *
205_PyDict_Dummy(void)
206{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 return dummy;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000208}
209#endif
210
Fred Drake1bff34a2000-08-31 19:31:38 +0000211/* forward declarations */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400212static PyDictKeyEntry *lookdict(PyDictObject *mp, PyObject *key,
213 Py_hash_t hash, PyObject ***value_addr);
214static PyDictKeyEntry *lookdict_unicode(PyDictObject *mp, PyObject *key,
215 Py_hash_t hash, PyObject ***value_addr);
216static PyDictKeyEntry *
217lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
218 Py_hash_t hash, PyObject ***value_addr);
219static PyDictKeyEntry *lookdict_split(PyDictObject *mp, PyObject *key,
220 Py_hash_t hash, PyObject ***value_addr);
Fred Drake1bff34a2000-08-31 19:31:38 +0000221
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400222static int dictresize(PyDictObject *mp, Py_ssize_t minused);
Tim Petersdea48ec2001-05-22 20:40:22 +0000223
Raymond Hettinger43442782004-03-17 21:55:03 +0000224/* Dictionary reuse scheme to save calls to malloc, free, and memset */
Christian Heimes2202f872008-02-06 14:31:34 +0000225#ifndef PyDict_MAXFREELIST
226#define PyDict_MAXFREELIST 80
227#endif
228static PyDictObject *free_list[PyDict_MAXFREELIST];
229static int numfree = 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000230
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100231int
232PyDict_ClearFreeList(void)
Christian Heimes77c02eb2008-02-09 02:18:51 +0000233{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000234 PyDictObject *op;
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100235 int ret = numfree;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 while (numfree) {
237 op = free_list[--numfree];
238 assert(PyDict_CheckExact(op));
239 PyObject_GC_Del(op);
240 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100241 return ret;
242}
243
David Malcolm49526f42012-06-22 14:55:41 -0400244/* Print summary info about the state of the optimized allocator */
245void
246_PyDict_DebugMallocStats(FILE *out)
247{
248 _PyDebugAllocatorStats(out,
249 "free PyDictObject", numfree, sizeof(PyDictObject));
250}
251
252
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100253void
254PyDict_Fini(void)
255{
256 PyDict_ClearFreeList();
Christian Heimes77c02eb2008-02-09 02:18:51 +0000257}
258
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200259#define DK_DEBUG_INCREF _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA
260#define DK_DEBUG_DECREF _Py_DEC_REFTOTAL _Py_REF_DEBUG_COMMA
261
262#define DK_INCREF(dk) (DK_DEBUG_INCREF ++(dk)->dk_refcnt)
263#define DK_DECREF(dk) if (DK_DEBUG_DECREF (--(dk)->dk_refcnt) == 0) free_keys_object(dk)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400264#define DK_SIZE(dk) ((dk)->dk_size)
265#define DK_MASK(dk) (((dk)->dk_size)-1)
266#define IS_POWER_OF_2(x) (((x) & (x-1)) == 0)
267
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200268/* USABLE_FRACTION is the maximum dictionary load.
269 * Currently set to (2n+1)/3. Increasing this ratio makes dictionaries more
270 * dense resulting in more collisions. Decreasing it improves sparseness
271 * at the expense of spreading entries over more cache lines and at the
272 * cost of total memory consumed.
273 *
274 * USABLE_FRACTION must obey the following:
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400275 * (0 < USABLE_FRACTION(n) < n) for all n >= 2
276 *
277 * USABLE_FRACTION should be very quick to calculate.
278 * Fractions around 5/8 to 2/3 seem to work well in practice.
279 */
280
281/* Use (2n+1)/3 rather than 2n+3 because: it makes no difference for
282 * combined tables (the two fractions round to the same number n < ),
283 * but 2*4/3 is 2 whereas (2*4+1)/3 is 3 which potentially saves quite
284 * a lot of space for small, split tables */
285#define USABLE_FRACTION(n) ((((n) << 1)+1)/3)
286
287/* Alternative fraction that is otherwise close enough to (2n+1)/3 to make
288 * little difference. 8 * 2/3 == 8 * 5/8 == 5. 16 * 2/3 == 16 * 5/8 == 10.
289 * 32 * 2/3 = 21, 32 * 5/8 = 20.
290 * Its advantage is that it is faster to compute on machines with slow division.
291 * #define USABLE_FRACTION(n) (((n) >> 1) + ((n) >> 2) - ((n) >> 3))
292*/
293
Victor Stinnera9f61a52013-07-16 22:17:26 +0200294/* GROWTH_RATE. Growth rate upon hitting maximum load.
295 * Currently set to used*2 + capacity/2.
296 * This means that dicts double in size when growing without deletions,
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700297 * but have more head room when the number of deletions is on a par with the
298 * number of insertions.
299 * Raising this to used*4 doubles memory consumption depending on the size of
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200300 * the dictionary, but results in half the number of resizes, less effort to
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700301 * resize.
302 * GROWTH_RATE was set to used*4 up to version 3.2.
303 * GROWTH_RATE was set to used*2 in version 3.3.0
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200304 */
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700305#define GROWTH_RATE(d) (((d)->ma_used*2)+((d)->ma_keys->dk_size>>1))
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400306
307#define ENSURE_ALLOWS_DELETIONS(d) \
308 if ((d)->ma_keys->dk_lookup == lookdict_unicode_nodummy) { \
309 (d)->ma_keys->dk_lookup = lookdict_unicode; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400311
312/* This immutable, empty PyDictKeysObject is used for PyDict_Clear()
313 * (which cannot fail and thus can do no allocation).
314 */
315static PyDictKeysObject empty_keys_struct = {
316 2, /* dk_refcnt 1 for this struct, 1 for dummy_struct */
317 1, /* dk_size */
318 lookdict_split, /* dk_lookup */
319 0, /* dk_usable (immutable) */
320 {
321 { 0, 0, 0 } /* dk_entries (empty) */
322 }
323};
324
325static PyObject *empty_values[1] = { NULL };
326
327#define Py_EMPTY_KEYS &empty_keys_struct
328
329static PyDictKeysObject *new_keys_object(Py_ssize_t size)
330{
331 PyDictKeysObject *dk;
332 Py_ssize_t i;
333 PyDictKeyEntry *ep0;
334
335 assert(size >= PyDict_MINSIZE_SPLIT);
336 assert(IS_POWER_OF_2(size));
337 dk = PyMem_MALLOC(sizeof(PyDictKeysObject) +
338 sizeof(PyDictKeyEntry) * (size-1));
339 if (dk == NULL) {
340 PyErr_NoMemory();
341 return NULL;
342 }
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200343 DK_DEBUG_INCREF dk->dk_refcnt = 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400344 dk->dk_size = size;
345 dk->dk_usable = USABLE_FRACTION(size);
346 ep0 = &dk->dk_entries[0];
347 /* Hash value of slot 0 is used by popitem, so it must be initialized */
348 ep0->me_hash = 0;
349 for (i = 0; i < size; i++) {
350 ep0[i].me_key = NULL;
351 ep0[i].me_value = NULL;
352 }
353 dk->dk_lookup = lookdict_unicode_nodummy;
354 return dk;
355}
356
357static void
358free_keys_object(PyDictKeysObject *keys)
359{
360 PyDictKeyEntry *entries = &keys->dk_entries[0];
361 Py_ssize_t i, n;
362 for (i = 0, n = DK_SIZE(keys); i < n; i++) {
363 Py_XDECREF(entries[i].me_key);
364 Py_XDECREF(entries[i].me_value);
365 }
366 PyMem_FREE(keys);
367}
368
369#define new_values(size) PyMem_NEW(PyObject *, size)
370
371#define free_values(values) PyMem_FREE(values)
372
373/* Consumes a reference to the keys object */
374static PyObject *
375new_dict(PyDictKeysObject *keys, PyObject **values)
376{
377 PyDictObject *mp;
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200378 assert(keys != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 if (numfree) {
380 mp = free_list[--numfree];
381 assert (mp != NULL);
382 assert (Py_TYPE(mp) == &PyDict_Type);
383 _Py_NewReference((PyObject *)mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400385 else {
386 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
387 if (mp == NULL) {
388 DK_DECREF(keys);
389 free_values(values);
390 return NULL;
391 }
392 }
393 mp->ma_keys = keys;
394 mp->ma_values = values;
395 mp->ma_used = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000397}
398
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400399/* Consumes a reference to the keys object */
400static PyObject *
401new_dict_with_shared_keys(PyDictKeysObject *keys)
402{
403 PyObject **values;
404 Py_ssize_t i, size;
405
406 size = DK_SIZE(keys);
407 values = new_values(size);
408 if (values == NULL) {
409 DK_DECREF(keys);
410 return PyErr_NoMemory();
411 }
412 for (i = 0; i < size; i++) {
413 values[i] = NULL;
414 }
415 return new_dict(keys, values);
416}
417
418PyObject *
419PyDict_New(void)
420{
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200421 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE_COMBINED);
422 if (keys == NULL)
423 return NULL;
424 return new_dict(keys, NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400425}
426
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000427/*
428The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000429This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000430Open addressing is preferred over chaining since the link overhead for
431chaining would be substantial (100% with typical malloc overhead).
432
Tim Peterseb28ef22001-06-02 05:27:19 +0000433The initial probe index is computed as hash mod the table size. Subsequent
434probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000435
436All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000437
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000438The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000439contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000440Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000441
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000442lookdict() is general-purpose, and may return NULL if (and only if) a
443comparison raises an exception (this was new in Python 2.5).
Guido van Rossum89d8c602007-09-18 17:26:56 +0000444lookdict_unicode() below is specialized to string keys, comparison of which can
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400445never raise an exception; that function can never return NULL.
446lookdict_unicode_nodummy is further specialized for string keys that cannot be
447the <dummy> value.
448For both, when the key isn't found a PyDictEntry* is returned
449where the key would have been found, *value_addr points to the matching value
450slot.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000451*/
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400452static PyDictKeyEntry *
453lookdict(PyDictObject *mp, PyObject *key,
454 Py_hash_t hash, PyObject ***value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000455{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200456 size_t i;
457 size_t perturb;
458 PyDictKeyEntry *freeslot;
459 size_t mask;
Antoine Pitrou9a234902012-05-13 20:48:01 +0200460 PyDictKeyEntry *ep0;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200461 PyDictKeyEntry *ep;
462 int cmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000464
Antoine Pitrou9a234902012-05-13 20:48:01 +0200465top:
466 mask = DK_MASK(mp->ma_keys);
467 ep0 = &mp->ma_keys->dk_entries[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 i = (size_t)hash & mask;
469 ep = &ep0[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400470 if (ep->me_key == NULL || ep->me_key == key) {
471 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400473 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 if (ep->me_key == dummy)
475 freeslot = ep;
476 else {
477 if (ep->me_hash == hash) {
478 startkey = ep->me_key;
479 Py_INCREF(startkey);
480 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
481 Py_DECREF(startkey);
482 if (cmp < 0)
483 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400484 if (ep0 == mp->ma_keys->dk_entries && ep->me_key == startkey) {
485 if (cmp > 0) {
486 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400488 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 }
490 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200491 /* The dict was mutated, restart */
492 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 }
494 }
495 freeslot = NULL;
496 }
Tim Peters15d49292001-05-27 07:39:22 +0000497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 /* In the loop, me_key == dummy is by far (factor of 100s) the
499 least likely outcome, so test for that last. */
500 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
501 i = (i << 2) + i + perturb + 1;
502 ep = &ep0[i & mask];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400503 if (ep->me_key == NULL) {
504 if (freeslot == NULL) {
505 *value_addr = &ep->me_value;
506 return ep;
507 } else {
508 *value_addr = &freeslot->me_value;
509 return freeslot;
510 }
511 }
512 if (ep->me_key == key) {
513 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400515 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 if (ep->me_hash == hash && ep->me_key != dummy) {
517 startkey = ep->me_key;
518 Py_INCREF(startkey);
519 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
520 Py_DECREF(startkey);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400521 if (cmp < 0) {
522 *value_addr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400524 }
525 if (ep0 == mp->ma_keys->dk_entries && ep->me_key == startkey) {
526 if (cmp > 0) {
527 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400529 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 }
531 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200532 /* The dict was mutated, restart */
533 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 }
535 }
536 else if (ep->me_key == dummy && freeslot == NULL)
537 freeslot = ep;
538 }
539 assert(0); /* NOT REACHED */
540 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000541}
542
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400543/* Specialized version for string-only keys */
544static PyDictKeyEntry *
545lookdict_unicode(PyDictObject *mp, PyObject *key,
546 Py_hash_t hash, PyObject ***value_addr)
Fred Drake1bff34a2000-08-31 19:31:38 +0000547{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200548 size_t i;
549 size_t perturb;
550 PyDictKeyEntry *freeslot;
551 size_t mask = DK_MASK(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400552 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200553 PyDictKeyEntry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 /* Make sure this function doesn't have to handle non-unicode keys,
556 including subclasses of str; e.g., one reason to subclass
557 unicodes is to override __eq__, and for speed we don't cater to
558 that here. */
559 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400560 mp->ma_keys->dk_lookup = lookdict;
561 return lookdict(mp, key, hash, value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 }
Mark Dickinson57e683e2011-09-24 18:18:40 +0100563 i = (size_t)hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 ep = &ep0[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400565 if (ep->me_key == NULL || ep->me_key == key) {
566 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400568 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 if (ep->me_key == dummy)
570 freeslot = ep;
571 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400572 if (ep->me_hash == hash && unicode_eq(ep->me_key, key)) {
573 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400575 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 freeslot = NULL;
577 }
Tim Peters15d49292001-05-27 07:39:22 +0000578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 /* In the loop, me_key == dummy is by far (factor of 100s) the
580 least likely outcome, so test for that last. */
581 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
582 i = (i << 2) + i + perturb + 1;
583 ep = &ep0[i & mask];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400584 if (ep->me_key == NULL) {
585 if (freeslot == NULL) {
586 *value_addr = &ep->me_value;
587 return ep;
588 } else {
589 *value_addr = &freeslot->me_value;
590 return freeslot;
591 }
592 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 if (ep->me_key == key
594 || (ep->me_hash == hash
595 && ep->me_key != dummy
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400596 && unicode_eq(ep->me_key, key))) {
597 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400599 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 if (ep->me_key == dummy && freeslot == NULL)
601 freeslot = ep;
602 }
603 assert(0); /* NOT REACHED */
604 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000605}
606
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400607/* Faster version of lookdict_unicode when it is known that no <dummy> keys
608 * will be present. */
609static PyDictKeyEntry *
610lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
611 Py_hash_t hash, PyObject ***value_addr)
612{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200613 size_t i;
614 size_t perturb;
615 size_t mask = DK_MASK(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400616 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200617 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400618
619 /* Make sure this function doesn't have to handle non-unicode keys,
620 including subclasses of str; e.g., one reason to subclass
621 unicodes is to override __eq__, and for speed we don't cater to
622 that here. */
623 if (!PyUnicode_CheckExact(key)) {
624 mp->ma_keys->dk_lookup = lookdict;
625 return lookdict(mp, key, hash, value_addr);
626 }
627 i = (size_t)hash & mask;
628 ep = &ep0[i];
629 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
630 if (ep->me_key == NULL || ep->me_key == key ||
631 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
632 *value_addr = &ep->me_value;
633 return ep;
634 }
635 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
636 i = (i << 2) + i + perturb + 1;
637 ep = &ep0[i & mask];
638 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
639 if (ep->me_key == NULL || ep->me_key == key ||
640 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
641 *value_addr = &ep->me_value;
642 return ep;
643 }
644 }
645 assert(0); /* NOT REACHED */
646 return 0;
647}
648
649/* Version of lookdict for split tables.
650 * All split tables and only split tables use this lookup function.
651 * Split tables only contain unicode keys and no dummy keys,
652 * so algorithm is the same as lookdict_unicode_nodummy.
653 */
654static PyDictKeyEntry *
655lookdict_split(PyDictObject *mp, PyObject *key,
656 Py_hash_t hash, PyObject ***value_addr)
657{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200658 size_t i;
659 size_t perturb;
660 size_t mask = DK_MASK(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400661 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200662 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400663
664 if (!PyUnicode_CheckExact(key)) {
Benjamin Petersondb780d02012-04-23 13:44:32 -0400665 ep = lookdict(mp, key, hash, value_addr);
666 /* lookdict expects a combined-table, so fix value_addr */
667 i = ep - ep0;
668 *value_addr = &mp->ma_values[i];
669 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400670 }
671 i = (size_t)hash & mask;
672 ep = &ep0[i];
673 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
674 if (ep->me_key == NULL || ep->me_key == key ||
675 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
676 *value_addr = &mp->ma_values[i];
677 return ep;
678 }
679 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
680 i = (i << 2) + i + perturb + 1;
681 ep = &ep0[i & mask];
682 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
683 if (ep->me_key == NULL || ep->me_key == key ||
684 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
685 *value_addr = &mp->ma_values[i & mask];
686 return ep;
687 }
688 }
689 assert(0); /* NOT REACHED */
690 return 0;
691}
692
Benjamin Petersonfb886362010-04-24 18:21:17 +0000693int
694_PyDict_HasOnlyStringKeys(PyObject *dict)
695{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 Py_ssize_t pos = 0;
697 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000698 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400700 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 return 1;
702 while (PyDict_Next(dict, &pos, &key, &value))
703 if (!PyUnicode_Check(key))
704 return 0;
705 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000706}
707
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000708#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 do { \
710 if (!_PyObject_GC_IS_TRACKED(mp)) { \
711 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
712 _PyObject_GC_MAY_BE_TRACKED(value)) { \
713 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 } \
715 } \
716 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000717
718void
719_PyDict_MaybeUntrack(PyObject *op)
720{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 PyDictObject *mp;
722 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400723 Py_ssize_t i, size;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000724
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
726 return;
727
728 mp = (PyDictObject *) op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400729 size = DK_SIZE(mp->ma_keys);
730 if (_PyDict_HasSplitTable(mp)) {
731 for (i = 0; i < size; i++) {
732 if ((value = mp->ma_values[i]) == NULL)
733 continue;
734 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
735 assert(!_PyObject_GC_MAY_BE_TRACKED(
736 mp->ma_keys->dk_entries[i].me_key));
737 return;
738 }
739 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400741 else {
742 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
743 for (i = 0; i < size; i++) {
744 if ((value = ep0[i].me_value) == NULL)
745 continue;
746 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
747 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
748 return;
749 }
750 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000752}
753
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400754/* Internal function to find slot for an item from its hash
755 * when it is known that the key is not present in the dict.
756 */
757static PyDictKeyEntry *
758find_empty_slot(PyDictObject *mp, PyObject *key, Py_hash_t hash,
759 PyObject ***value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000760{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400761 size_t i;
762 size_t perturb;
763 size_t mask = DK_MASK(mp->ma_keys);
764 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
765 PyDictKeyEntry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000766
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400767 assert(key != NULL);
768 if (!PyUnicode_CheckExact(key))
769 mp->ma_keys->dk_lookup = lookdict;
770 i = hash & mask;
771 ep = &ep0[i];
772 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
773 i = (i << 2) + i + perturb + 1;
774 ep = &ep0[i & mask];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400776 assert(ep->me_value == NULL);
777 if (mp->ma_values)
778 *value_addr = &mp->ma_values[i & mask];
779 else
780 *value_addr = &ep->me_value;
781 return ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000782}
783
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400784static int
785insertion_resize(PyDictObject *mp)
786{
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700787 return dictresize(mp, GROWTH_RATE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400788}
Antoine Pitroue965d972012-02-27 00:45:12 +0100789
790/*
791Internal routine to insert a new item into the table.
792Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +0100793Returns -1 if an error occurred, or 0 on success.
794*/
795static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400796insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +0100797{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400798 PyObject *old_value;
799 PyObject **value_addr;
800 PyDictKeyEntry *ep;
801 assert(key != dummy);
Antoine Pitroue965d972012-02-27 00:45:12 +0100802
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400803 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
804 if (insertion_resize(mp) < 0)
805 return -1;
806 }
807
808 ep = mp->ma_keys->dk_lookup(mp, key, hash, &value_addr);
Antoine Pitroue965d972012-02-27 00:45:12 +0100809 if (ep == NULL) {
Antoine Pitroue965d972012-02-27 00:45:12 +0100810 return -1;
811 }
Benjamin Petersona6f195e2012-04-30 10:23:40 -0400812 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400813 MAINTAIN_TRACKING(mp, key, value);
814 old_value = *value_addr;
815 if (old_value != NULL) {
816 assert(ep->me_key != NULL && ep->me_key != dummy);
817 *value_addr = value;
818 Py_DECREF(old_value); /* which **CAN** re-enter */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400819 }
820 else {
821 if (ep->me_key == NULL) {
Benjamin Petersona6f195e2012-04-30 10:23:40 -0400822 Py_INCREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400823 if (mp->ma_keys->dk_usable <= 0) {
824 /* Need to resize. */
825 if (insertion_resize(mp) < 0) {
826 Py_DECREF(key);
827 Py_DECREF(value);
828 return -1;
829 }
830 ep = find_empty_slot(mp, key, hash, &value_addr);
831 }
832 mp->ma_keys->dk_usable--;
833 assert(mp->ma_keys->dk_usable >= 0);
834 ep->me_key = key;
835 ep->me_hash = hash;
836 }
837 else {
838 if (ep->me_key == dummy) {
Benjamin Petersona6f195e2012-04-30 10:23:40 -0400839 Py_INCREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400840 ep->me_key = key;
841 ep->me_hash = hash;
842 Py_DECREF(dummy);
843 } else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400844 assert(_PyDict_HasSplitTable(mp));
845 }
846 }
847 mp->ma_used++;
848 *value_addr = value;
849 }
850 assert(ep->me_key != NULL && ep->me_key != dummy);
851 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
852 return 0;
Antoine Pitroue965d972012-02-27 00:45:12 +0100853}
854
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000855/*
856Internal routine used by dictresize() to insert an item which is
857known to be absent from the dict. This routine also assumes that
858the dict contains no deleted entries. Besides the performance benefit,
859using insertdict() in dictresize() is dangerous (SF bug #1456209).
860Note that no refcounts are changed by this routine; if needed, the caller
861is responsible for incref'ing `key` and `value`.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400862Neither mp->ma_used nor k->dk_usable are modified by this routine; the caller
863must set them correctly
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000864*/
865static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400866insertdict_clean(PyDictObject *mp, PyObject *key, Py_hash_t hash,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 PyObject *value)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000868{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400869 size_t i;
870 size_t perturb;
871 PyDictKeysObject *k = mp->ma_keys;
872 size_t mask = (size_t)DK_SIZE(k)-1;
873 PyDictKeyEntry *ep0 = &k->dk_entries[0];
874 PyDictKeyEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000875
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400876 assert(k->dk_lookup != NULL);
877 assert(value != NULL);
878 assert(key != NULL);
879 assert(key != dummy);
880 assert(PyUnicode_CheckExact(key) || k->dk_lookup == lookdict);
881 i = hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 ep = &ep0[i];
883 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
884 i = (i << 2) + i + perturb + 1;
885 ep = &ep0[i & mask];
886 }
887 assert(ep->me_value == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 ep->me_key = key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000889 ep->me_hash = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000891}
892
893/*
894Restructure the table by allocating a new table and reinserting all
895items again. When entries have been deleted, the new table may
896actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400897If a table is split (its keys and hashes are shared, its values are not),
898then the values are temporarily copied into the table, it is resized as
899a combined table, then the me_value slots in the old table are NULLed out.
900After resizing a table is always combined,
901but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000902*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000903static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000904dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000905{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 Py_ssize_t newsize;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400907 PyDictKeysObject *oldkeys;
908 PyObject **oldvalues;
909 Py_ssize_t i, oldsize;
Tim Peters91a364d2001-05-19 07:04:38 +0000910
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400911/* Find the smallest table size > minused. */
912 for (newsize = PyDict_MINSIZE_COMBINED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 newsize <= minused && newsize > 0;
914 newsize <<= 1)
915 ;
916 if (newsize <= 0) {
917 PyErr_NoMemory();
918 return -1;
919 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400920 oldkeys = mp->ma_keys;
921 oldvalues = mp->ma_values;
922 /* Allocate a new table. */
923 mp->ma_keys = new_keys_object(newsize);
924 if (mp->ma_keys == NULL) {
925 mp->ma_keys = oldkeys;
926 return -1;
927 }
928 if (oldkeys->dk_lookup == lookdict)
929 mp->ma_keys->dk_lookup = lookdict;
930 oldsize = DK_SIZE(oldkeys);
931 mp->ma_values = NULL;
932 /* If empty then nothing to copy so just return */
933 if (oldsize == 1) {
934 assert(oldkeys == Py_EMPTY_KEYS);
935 DK_DECREF(oldkeys);
936 return 0;
937 }
938 /* Main loop below assumes we can transfer refcount to new keys
939 * and that value is stored in me_value.
940 * Increment ref-counts and copy values here to compensate
941 * This (resizing a split table) should be relatively rare */
942 if (oldvalues != NULL) {
943 for (i = 0; i < oldsize; i++) {
944 if (oldvalues[i] != NULL) {
945 Py_INCREF(oldkeys->dk_entries[i].me_key);
946 oldkeys->dk_entries[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 }
949 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400950 /* Main loop */
951 for (i = 0; i < oldsize; i++) {
952 PyDictKeyEntry *ep = &oldkeys->dk_entries[i];
953 if (ep->me_value != NULL) {
954 assert(ep->me_key != dummy);
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000955 insertdict_clean(mp, ep->me_key, ep->me_hash, ep->me_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400958 mp->ma_keys->dk_usable -= mp->ma_used;
959 if (oldvalues != NULL) {
960 /* NULL out me_value slot in oldkeys, in case it was shared */
961 for (i = 0; i < oldsize; i++)
962 oldkeys->dk_entries[i].me_value = NULL;
963 assert(oldvalues != empty_values);
964 free_values(oldvalues);
965 DK_DECREF(oldkeys);
966 }
967 else {
968 assert(oldkeys->dk_lookup != lookdict_split);
969 if (oldkeys->dk_lookup != lookdict_unicode_nodummy) {
970 PyDictKeyEntry *ep0 = &oldkeys->dk_entries[0];
971 for (i = 0; i < oldsize; i++) {
972 if (ep0[i].me_key == dummy)
973 Py_DECREF(dummy);
974 }
975 }
976 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200977 DK_DEBUG_DECREF PyMem_FREE(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400978 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000980}
981
Benjamin Peterson15ee8212012-04-24 14:44:18 -0400982/* Returns NULL if unable to split table.
983 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400984static PyDictKeysObject *
985make_keys_shared(PyObject *op)
986{
987 Py_ssize_t i;
988 Py_ssize_t size;
989 PyDictObject *mp = (PyDictObject *)op;
990
Benjamin Peterson15ee8212012-04-24 14:44:18 -0400991 if (!PyDict_CheckExact(op))
992 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400993 if (!_PyDict_HasSplitTable(mp)) {
994 PyDictKeyEntry *ep0;
995 PyObject **values;
996 assert(mp->ma_keys->dk_refcnt == 1);
997 if (mp->ma_keys->dk_lookup == lookdict) {
998 return NULL;
999 }
1000 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
1001 /* Remove dummy keys */
1002 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
1003 return NULL;
1004 }
1005 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
1006 /* Copy values into a new array */
1007 ep0 = &mp->ma_keys->dk_entries[0];
1008 size = DK_SIZE(mp->ma_keys);
1009 values = new_values(size);
1010 if (values == NULL) {
1011 PyErr_SetString(PyExc_MemoryError,
1012 "Not enough memory to allocate new values array");
1013 return NULL;
1014 }
1015 for (i = 0; i < size; i++) {
1016 values[i] = ep0[i].me_value;
1017 ep0[i].me_value = NULL;
1018 }
1019 mp->ma_keys->dk_lookup = lookdict_split;
1020 mp->ma_values = values;
1021 }
1022 DK_INCREF(mp->ma_keys);
1023 return mp->ma_keys;
1024}
Christian Heimes99170a52007-12-19 02:07:34 +00001025
1026PyObject *
1027_PyDict_NewPresized(Py_ssize_t minused)
1028{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001029 Py_ssize_t newsize;
1030 PyDictKeysObject *new_keys;
1031 for (newsize = PyDict_MINSIZE_COMBINED;
1032 newsize <= minused && newsize > 0;
1033 newsize <<= 1)
1034 ;
1035 new_keys = new_keys_object(newsize);
1036 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001038 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001039}
1040
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001041/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1042 * that may occur (originally dicts supported only string keys, and exceptions
1043 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001044 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001045 * (suppressed) occurred while computing the key's hash, or that some error
1046 * (suppressed) occurred when comparing keys in the dict's internal probe
1047 * sequence. A nasty example of the latter is when a Python-coded comparison
1048 * function hits a stack-depth error, which can cause this to return NULL
1049 * even if the key is present.
1050 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001051PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001052PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001053{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001054 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001056 PyDictKeyEntry *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 PyThreadState *tstate;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001058 PyObject **value_addr;
1059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 if (!PyDict_Check(op))
1061 return NULL;
1062 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001063 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 {
1065 hash = PyObject_Hash(key);
1066 if (hash == -1) {
1067 PyErr_Clear();
1068 return NULL;
1069 }
1070 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001071
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 /* We can arrive here with a NULL tstate during initialization: try
1073 running "python -Wi" for an example related to string interning.
1074 Let's just hope that no exception occurs then... This must be
1075 _PyThreadState_Current and not PyThreadState_GET() because in debug
1076 mode, the latter complains if tstate is NULL. */
1077 tstate = (PyThreadState*)_Py_atomic_load_relaxed(
1078 &_PyThreadState_Current);
1079 if (tstate != NULL && tstate->curexc_type != NULL) {
1080 /* preserve the existing exception */
1081 PyObject *err_type, *err_value, *err_tb;
1082 PyErr_Fetch(&err_type, &err_value, &err_tb);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001083 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 /* ignore errors */
1085 PyErr_Restore(err_type, err_value, err_tb);
1086 if (ep == NULL)
1087 return NULL;
1088 }
1089 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001090 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 if (ep == NULL) {
1092 PyErr_Clear();
1093 return NULL;
1094 }
1095 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001096 return *value_addr;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001097}
1098
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001099/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1100 This returns NULL *with* an exception set if an exception occurred.
1101 It returns NULL *without* an exception set if the key wasn't present.
1102*/
1103PyObject *
1104PyDict_GetItemWithError(PyObject *op, PyObject *key)
1105{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001106 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 PyDictObject*mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001108 PyDictKeyEntry *ep;
1109 PyObject **value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001110
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 if (!PyDict_Check(op)) {
1112 PyErr_BadInternalCall();
1113 return NULL;
1114 }
1115 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001116 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 {
1118 hash = PyObject_Hash(key);
1119 if (hash == -1) {
1120 return NULL;
1121 }
1122 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001123
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001124 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 if (ep == NULL)
1126 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001127 return *value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001128}
1129
Brett Cannonfd074152012-04-14 14:10:13 -04001130PyObject *
1131_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1132{
1133 PyObject *kv;
1134 kv = _PyUnicode_FromId(key); /* borrowed */
1135 if (kv == NULL)
1136 return NULL;
1137 return PyDict_GetItemWithError(dp, kv);
1138}
1139
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001140/* Fast version of global value lookup.
1141 * Lookup in globals, then builtins.
1142 */
1143PyObject *
1144_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001145{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001146 PyObject *x;
1147 if (PyUnicode_CheckExact(key)) {
1148 PyObject **value_addr;
1149 Py_hash_t hash = ((PyASCIIObject *)key)->hash;
1150 if (hash != -1) {
1151 PyDictKeyEntry *e;
1152 e = globals->ma_keys->dk_lookup(globals, key, hash, &value_addr);
1153 if (e == NULL) {
1154 return NULL;
1155 }
1156 x = *value_addr;
1157 if (x != NULL)
1158 return x;
1159 e = builtins->ma_keys->dk_lookup(builtins, key, hash, &value_addr);
1160 if (e == NULL) {
1161 return NULL;
1162 }
1163 x = *value_addr;
1164 return x;
1165 }
Antoine Pitroue965d972012-02-27 00:45:12 +01001166 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001167 x = PyDict_GetItemWithError((PyObject *)globals, key);
1168 if (x != NULL)
1169 return x;
1170 if (PyErr_Occurred())
1171 return NULL;
1172 return PyDict_GetItemWithError((PyObject *)builtins, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001173}
1174
Antoine Pitroue965d972012-02-27 00:45:12 +01001175/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1176 * dictionary if it's merely replacing the value for an existing key.
1177 * This means that it's safe to loop over a dictionary with PyDict_Next()
1178 * and occasionally replace a value -- but you can't insert new keys or
1179 * remove them.
1180 */
1181int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001182PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001183{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001184 PyDictObject *mp;
1185 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001186 if (!PyDict_Check(op)) {
1187 PyErr_BadInternalCall();
1188 return -1;
1189 }
1190 assert(key);
1191 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001192 mp = (PyDictObject *)op;
1193 if (!PyUnicode_CheckExact(key) ||
1194 (hash = ((PyASCIIObject *) key)->hash) == -1)
1195 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001196 hash = PyObject_Hash(key);
1197 if (hash == -1)
1198 return -1;
1199 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001200
1201 /* insertdict() handles any resizing that might be necessary */
1202 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001203}
1204
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001205int
Tim Peters1f5871e2000-07-04 17:44:48 +00001206PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001207{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001208 PyDictObject *mp;
1209 Py_hash_t hash;
1210 PyDictKeyEntry *ep;
1211 PyObject *old_key, *old_value;
1212 PyObject **value_addr;
Guido van Rossumd7047b31995-01-02 19:07:15 +00001213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 if (!PyDict_Check(op)) {
1215 PyErr_BadInternalCall();
1216 return -1;
1217 }
1218 assert(key);
1219 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001220 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 hash = PyObject_Hash(key);
1222 if (hash == -1)
1223 return -1;
1224 }
1225 mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001226 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 if (ep == NULL)
1228 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001229 if (*value_addr == NULL) {
Raymond Hettinger69492da2013-09-02 15:59:26 -07001230 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 return -1;
1232 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001233 old_value = *value_addr;
1234 *value_addr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001236 if (!_PyDict_HasSplitTable(mp)) {
1237 ENSURE_ALLOWS_DELETIONS(mp);
1238 old_key = ep->me_key;
1239 Py_INCREF(dummy);
1240 ep->me_key = dummy;
1241 Py_DECREF(old_key);
1242 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 Py_DECREF(old_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001245}
1246
Guido van Rossum25831651993-05-19 14:50:45 +00001247void
Tim Peters1f5871e2000-07-04 17:44:48 +00001248PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001249{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001251 PyDictKeysObject *oldkeys;
1252 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 if (!PyDict_Check(op))
1256 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001257 mp = ((PyDictObject *)op);
1258 oldkeys = mp->ma_keys;
1259 oldvalues = mp->ma_values;
1260 if (oldvalues == empty_values)
1261 return;
1262 /* Empty the dict... */
1263 DK_INCREF(Py_EMPTY_KEYS);
1264 mp->ma_keys = Py_EMPTY_KEYS;
1265 mp->ma_values = empty_values;
1266 mp->ma_used = 0;
1267 /* ...then clear the keys and values */
1268 if (oldvalues != NULL) {
1269 n = DK_SIZE(oldkeys);
1270 for (i = 0; i < n; i++)
1271 Py_CLEAR(oldvalues[i]);
1272 free_values(oldvalues);
1273 DK_DECREF(oldkeys);
1274 }
1275 else {
1276 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001277 DK_DECREF(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001278 }
1279}
1280
1281/* Returns -1 if no more items (or op is not a dict),
1282 * index of item otherwise. Stores value in pvalue
1283 */
1284Py_LOCAL_INLINE(Py_ssize_t)
1285dict_next(PyObject *op, Py_ssize_t i, PyObject **pvalue)
1286{
1287 Py_ssize_t mask, offset;
1288 PyDictObject *mp;
1289 PyObject **value_ptr;
1290
1291
1292 if (!PyDict_Check(op))
1293 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001295 if (i < 0)
1296 return -1;
1297 if (mp->ma_values) {
1298 value_ptr = &mp->ma_values[i];
1299 offset = sizeof(PyObject *);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001301 else {
1302 value_ptr = &mp->ma_keys->dk_entries[i].me_value;
1303 offset = sizeof(PyDictKeyEntry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001305 mask = DK_MASK(mp->ma_keys);
1306 while (i <= mask && *value_ptr == NULL) {
1307 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1308 i++;
1309 }
1310 if (i > mask)
1311 return -1;
1312 if (pvalue)
1313 *pvalue = *value_ptr;
1314 return i;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001315}
1316
Tim Peters080c88b2003-02-15 03:01:11 +00001317/*
1318 * Iterate over a dict. Use like so:
1319 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001320 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001321 * PyObject *key, *value;
1322 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001323 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +00001324 * Refer to borrowed references in key and value.
1325 * }
1326 *
1327 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001328 * mutates the dict. One exception: it is safe if the loop merely changes
1329 * the values associated with the keys (but doesn't insert new keys or
1330 * delete keys), via PyDict_SetItem().
1331 */
Guido van Rossum25831651993-05-19 14:50:45 +00001332int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001333PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001334{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001335 PyDictObject *mp;
1336 Py_ssize_t i = dict_next(op, *ppos, pvalue);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 if (i < 0)
1338 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001339 mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 *ppos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 if (pkey)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001342 *pkey = mp->ma_keys->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001344}
1345
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001346/* Internal version of PyDict_Next that returns a hash value in addition
1347 * to the key and value.
1348 */
Thomas Wouterscf297e42007-02-23 15:07:44 +00001349int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001350_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1351 PyObject **pvalue, Py_hash_t *phash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00001352{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001353 PyDictObject *mp;
1354 Py_ssize_t i = dict_next(op, *ppos, pvalue);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 if (i < 0)
1356 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001357 mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001358 *ppos = i+1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001359 *phash = mp->ma_keys->dk_entries[i].me_hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 if (pkey)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001361 *pkey = mp->ma_keys->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 return 1;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001363}
1364
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001365/* Methods */
1366
1367static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001368dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001369{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001370 PyObject **values = mp->ma_values;
1371 PyDictKeysObject *keys = mp->ma_keys;
1372 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 PyObject_GC_UnTrack(mp);
1374 Py_TRASHCAN_SAFE_BEGIN(mp)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001375 if (values != NULL) {
1376 if (values != empty_values) {
1377 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
1378 Py_XDECREF(values[i]);
1379 }
1380 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001382 DK_DECREF(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02001384 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001385 assert(keys->dk_refcnt == 1);
1386 DK_DECREF(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001387 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1389 free_list[numfree++] = mp;
1390 else
1391 Py_TYPE(mp)->tp_free((PyObject *)mp);
1392 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001393}
1394
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001395
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001396static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001397dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001398{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 Py_ssize_t i;
1400 PyObject *s, *temp, *colon = NULL;
1401 PyObject *pieces = NULL, *result = NULL;
1402 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +00001403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 i = Py_ReprEnter((PyObject *)mp);
1405 if (i != 0) {
1406 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
1407 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 if (mp->ma_used == 0) {
1410 result = PyUnicode_FromString("{}");
1411 goto Done;
1412 }
Tim Petersa7259592001-06-16 05:11:17 +00001413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001414 pieces = PyList_New(0);
1415 if (pieces == NULL)
1416 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 colon = PyUnicode_FromString(": ");
1419 if (colon == NULL)
1420 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 /* Do repr() on each key+value pair, and insert ": " between them.
1423 Note that repr may mutate the dict. */
1424 i = 0;
1425 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
1426 int status;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001427 /* Prevent repr from deleting key or value during key format. */
1428 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 Py_INCREF(value);
1430 s = PyObject_Repr(key);
1431 PyUnicode_Append(&s, colon);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02001432 if (s == NULL)
1433 goto Done;
1434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 PyUnicode_AppendAndDel(&s, PyObject_Repr(value));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001436 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 Py_DECREF(value);
1438 if (s == NULL)
1439 goto Done;
1440 status = PyList_Append(pieces, s);
1441 Py_DECREF(s); /* append created a new ref */
1442 if (status < 0)
1443 goto Done;
1444 }
Tim Petersa7259592001-06-16 05:11:17 +00001445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 /* Add "{}" decorations to the first and last items. */
1447 assert(PyList_GET_SIZE(pieces) > 0);
1448 s = PyUnicode_FromString("{");
1449 if (s == NULL)
1450 goto Done;
1451 temp = PyList_GET_ITEM(pieces, 0);
1452 PyUnicode_AppendAndDel(&s, temp);
1453 PyList_SET_ITEM(pieces, 0, s);
1454 if (s == NULL)
1455 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 s = PyUnicode_FromString("}");
1458 if (s == NULL)
1459 goto Done;
1460 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
1461 PyUnicode_AppendAndDel(&temp, s);
1462 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
1463 if (temp == NULL)
1464 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001466 /* Paste them all together with ", " between. */
1467 s = PyUnicode_FromString(", ");
1468 if (s == NULL)
1469 goto Done;
1470 result = PyUnicode_Join(s, pieces);
1471 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +00001472
1473Done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 Py_XDECREF(pieces);
1475 Py_XDECREF(colon);
1476 Py_ReprLeave((PyObject *)mp);
1477 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001478}
1479
Martin v. Löwis18e16552006-02-15 17:27:45 +00001480static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001481dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001482{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001484}
1485
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001486static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001487dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001488{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489 PyObject *v;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001490 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001491 PyDictKeyEntry *ep;
1492 PyObject **value_addr;
1493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001494 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001495 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 hash = PyObject_Hash(key);
1497 if (hash == -1)
1498 return NULL;
1499 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001500 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 if (ep == NULL)
1502 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001503 v = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 if (v == NULL) {
1505 if (!PyDict_CheckExact(mp)) {
1506 /* Look up __missing__ method if we're a subclass. */
1507 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05001508 _Py_IDENTIFIER(__missing__);
1509 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 if (missing != NULL) {
1511 res = PyObject_CallFunctionObjArgs(missing,
1512 key, NULL);
1513 Py_DECREF(missing);
1514 return res;
1515 }
1516 else if (PyErr_Occurred())
1517 return NULL;
1518 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07001519 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 return NULL;
1521 }
1522 else
1523 Py_INCREF(v);
1524 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001525}
1526
1527static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001528dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001529{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 if (w == NULL)
1531 return PyDict_DelItem((PyObject *)mp, v);
1532 else
1533 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001534}
1535
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001536static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537 (lenfunc)dict_length, /*mp_length*/
1538 (binaryfunc)dict_subscript, /*mp_subscript*/
1539 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001540};
1541
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001542static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001543dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001544{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001545 PyObject *v;
1546 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001547 PyDictKeyEntry *ep;
1548 Py_ssize_t size, n, offset;
1549 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001550
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001551 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 n = mp->ma_used;
1553 v = PyList_New(n);
1554 if (v == NULL)
1555 return NULL;
1556 if (n != mp->ma_used) {
1557 /* Durnit. The allocations caused the dict to resize.
1558 * Just start over, this shouldn't normally happen.
1559 */
1560 Py_DECREF(v);
1561 goto again;
1562 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001563 ep = &mp->ma_keys->dk_entries[0];
1564 size = DK_SIZE(mp->ma_keys);
1565 if (mp->ma_values) {
1566 value_ptr = mp->ma_values;
1567 offset = sizeof(PyObject *);
1568 }
1569 else {
1570 value_ptr = &ep[0].me_value;
1571 offset = sizeof(PyDictKeyEntry);
1572 }
1573 for (i = 0, j = 0; i < size; i++) {
1574 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001575 PyObject *key = ep[i].me_key;
1576 Py_INCREF(key);
1577 PyList_SET_ITEM(v, j, key);
1578 j++;
1579 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001580 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 }
1582 assert(j == n);
1583 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001584}
1585
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001586static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001587dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001588{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001589 PyObject *v;
1590 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001591 Py_ssize_t size, n, offset;
1592 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001593
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001594 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 n = mp->ma_used;
1596 v = PyList_New(n);
1597 if (v == NULL)
1598 return NULL;
1599 if (n != mp->ma_used) {
1600 /* Durnit. The allocations caused the dict to resize.
1601 * Just start over, this shouldn't normally happen.
1602 */
1603 Py_DECREF(v);
1604 goto again;
1605 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001606 size = DK_SIZE(mp->ma_keys);
1607 if (mp->ma_values) {
1608 value_ptr = mp->ma_values;
1609 offset = sizeof(PyObject *);
1610 }
1611 else {
1612 value_ptr = &mp->ma_keys->dk_entries[0].me_value;
1613 offset = sizeof(PyDictKeyEntry);
1614 }
1615 for (i = 0, j = 0; i < size; i++) {
1616 PyObject *value = *value_ptr;
1617 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1618 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619 Py_INCREF(value);
1620 PyList_SET_ITEM(v, j, value);
1621 j++;
1622 }
1623 }
1624 assert(j == n);
1625 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001626}
1627
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001628static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001629dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001630{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001631 PyObject *v;
1632 Py_ssize_t i, j, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001633 Py_ssize_t size, offset;
1634 PyObject *item, *key;
1635 PyDictKeyEntry *ep;
1636 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 /* Preallocate the list of tuples, to avoid allocations during
1639 * the loop over the items, which could trigger GC, which
1640 * could resize the dict. :-(
1641 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001642 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001643 n = mp->ma_used;
1644 v = PyList_New(n);
1645 if (v == NULL)
1646 return NULL;
1647 for (i = 0; i < n; i++) {
1648 item = PyTuple_New(2);
1649 if (item == NULL) {
1650 Py_DECREF(v);
1651 return NULL;
1652 }
1653 PyList_SET_ITEM(v, i, item);
1654 }
1655 if (n != mp->ma_used) {
1656 /* Durnit. The allocations caused the dict to resize.
1657 * Just start over, this shouldn't normally happen.
1658 */
1659 Py_DECREF(v);
1660 goto again;
1661 }
1662 /* Nothing we do below makes any function calls. */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001663 ep = mp->ma_keys->dk_entries;
1664 size = DK_SIZE(mp->ma_keys);
1665 if (mp->ma_values) {
1666 value_ptr = mp->ma_values;
1667 offset = sizeof(PyObject *);
1668 }
1669 else {
1670 value_ptr = &ep[0].me_value;
1671 offset = sizeof(PyDictKeyEntry);
1672 }
1673 for (i = 0, j = 0; i < size; i++) {
1674 PyObject *value = *value_ptr;
1675 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1676 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 key = ep[i].me_key;
1678 item = PyList_GET_ITEM(v, j);
1679 Py_INCREF(key);
1680 PyTuple_SET_ITEM(item, 0, key);
1681 Py_INCREF(value);
1682 PyTuple_SET_ITEM(item, 1, value);
1683 j++;
1684 }
1685 }
1686 assert(j == n);
1687 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001688}
1689
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001690static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001691dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001692{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 PyObject *seq;
1694 PyObject *value = Py_None;
1695 PyObject *it; /* iter(seq) */
1696 PyObject *key;
1697 PyObject *d;
1698 int status;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
1701 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 d = PyObject_CallObject(cls, NULL);
1704 if (d == NULL)
1705 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001706
Benjamin Peterson9892f522012-10-31 14:22:12 -04001707 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001708 if (PyDict_CheckExact(seq)) {
1709 PyDictObject *mp = (PyDictObject *)d;
1710 PyObject *oldvalue;
1711 Py_ssize_t pos = 0;
1712 PyObject *key;
1713 Py_hash_t hash;
Guido van Rossum58da9312007-11-10 23:39:45 +00001714
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001715 if (dictresize(mp, Py_SIZE(seq))) {
Petri Lehtinena94200e2011-10-24 21:12:58 +03001716 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001718 }
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001719
1720 while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001721 if (insertdict(mp, key, hash, value)) {
1722 Py_DECREF(d);
1723 return NULL;
1724 }
1725 }
1726 return d;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 }
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001728 if (PyAnySet_CheckExact(seq)) {
1729 PyDictObject *mp = (PyDictObject *)d;
1730 Py_ssize_t pos = 0;
1731 PyObject *key;
1732 Py_hash_t hash;
Guido van Rossum58da9312007-11-10 23:39:45 +00001733
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001734 if (dictresize(mp, PySet_GET_SIZE(seq))) {
Petri Lehtinena94200e2011-10-24 21:12:58 +03001735 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001736 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001737 }
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001738
1739 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001740 if (insertdict(mp, key, hash, value)) {
1741 Py_DECREF(d);
1742 return NULL;
1743 }
1744 }
1745 return d;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001747 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001748
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 it = PyObject_GetIter(seq);
1750 if (it == NULL){
1751 Py_DECREF(d);
1752 return NULL;
1753 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001755 if (PyDict_CheckExact(d)) {
1756 while ((key = PyIter_Next(it)) != NULL) {
1757 status = PyDict_SetItem(d, key, value);
1758 Py_DECREF(key);
1759 if (status < 0)
1760 goto Fail;
1761 }
1762 } else {
1763 while ((key = PyIter_Next(it)) != NULL) {
1764 status = PyObject_SetItem(d, key, value);
1765 Py_DECREF(key);
1766 if (status < 0)
1767 goto Fail;
1768 }
1769 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001771 if (PyErr_Occurred())
1772 goto Fail;
1773 Py_DECREF(it);
1774 return d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001775
1776Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001777 Py_DECREF(it);
1778 Py_DECREF(d);
1779 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001780}
1781
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001782static int
1783dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001784{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 PyObject *arg = NULL;
1786 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001788 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1789 result = -1;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001792 _Py_IDENTIFIER(keys);
1793 if (_PyObject_HasAttrId(arg, &PyId_keys))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001794 result = PyDict_Merge(self, arg, 1);
1795 else
1796 result = PyDict_MergeFromSeq2(self, arg, 1);
1797 }
1798 if (result == 0 && kwds != NULL) {
1799 if (PyArg_ValidateKeywordArguments(kwds))
1800 result = PyDict_Merge(self, kwds, 1);
1801 else
1802 result = -1;
1803 }
1804 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001805}
1806
1807static PyObject *
1808dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1809{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001810 if (dict_update_common(self, args, kwds, "update") != -1)
1811 Py_RETURN_NONE;
1812 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001813}
1814
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001815/* Update unconditionally replaces existing items.
1816 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001817 otherwise it leaves existing items unchanged.
1818
1819 PyDict_{Update,Merge} update/merge from a mapping object.
1820
Tim Petersf582b822001-12-11 18:51:08 +00001821 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001822 producing iterable objects of length 2.
1823*/
1824
Tim Petersf582b822001-12-11 18:51:08 +00001825int
Tim Peters1fc240e2001-10-26 05:06:50 +00001826PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1827{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001828 PyObject *it; /* iter(seq2) */
1829 Py_ssize_t i; /* index into seq2 of current element */
1830 PyObject *item; /* seq2[i] */
1831 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00001832
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001833 assert(d != NULL);
1834 assert(PyDict_Check(d));
1835 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00001836
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837 it = PyObject_GetIter(seq2);
1838 if (it == NULL)
1839 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001840
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001841 for (i = 0; ; ++i) {
1842 PyObject *key, *value;
1843 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001844
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001845 fast = NULL;
1846 item = PyIter_Next(it);
1847 if (item == NULL) {
1848 if (PyErr_Occurred())
1849 goto Fail;
1850 break;
1851 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853 /* Convert item to sequence, and verify length 2. */
1854 fast = PySequence_Fast(item, "");
1855 if (fast == NULL) {
1856 if (PyErr_ExceptionMatches(PyExc_TypeError))
1857 PyErr_Format(PyExc_TypeError,
1858 "cannot convert dictionary update "
1859 "sequence element #%zd to a sequence",
1860 i);
1861 goto Fail;
1862 }
1863 n = PySequence_Fast_GET_SIZE(fast);
1864 if (n != 2) {
1865 PyErr_Format(PyExc_ValueError,
1866 "dictionary update sequence element #%zd "
1867 "has length %zd; 2 is required",
1868 i, n);
1869 goto Fail;
1870 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 /* Update/merge with this (key, value) pair. */
1873 key = PySequence_Fast_GET_ITEM(fast, 0);
1874 value = PySequence_Fast_GET_ITEM(fast, 1);
1875 if (override || PyDict_GetItem(d, key) == NULL) {
1876 int status = PyDict_SetItem(d, key, value);
1877 if (status < 0)
1878 goto Fail;
1879 }
1880 Py_DECREF(fast);
1881 Py_DECREF(item);
1882 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001884 i = 0;
1885 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00001886Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887 Py_XDECREF(item);
1888 Py_XDECREF(fast);
1889 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001890Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 Py_DECREF(it);
1892 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001893}
1894
Tim Peters6d6c1a32001-08-02 04:15:00 +00001895int
1896PyDict_Update(PyObject *a, PyObject *b)
1897{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 return PyDict_Merge(a, b, 1);
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001899}
1900
1901int
1902PyDict_Merge(PyObject *a, PyObject *b, int override)
1903{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001904 PyDictObject *mp, *other;
1905 Py_ssize_t i, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001906 PyDictKeyEntry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001908 /* We accept for the argument either a concrete dictionary object,
1909 * or an abstract "mapping" object. For the former, we can do
1910 * things quite efficiently. For the latter, we only require that
1911 * PyMapping_Keys() and PyObject_GetItem() be supported.
1912 */
1913 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1914 PyErr_BadInternalCall();
1915 return -1;
1916 }
1917 mp = (PyDictObject*)a;
1918 if (PyDict_Check(b)) {
1919 other = (PyDictObject*)b;
1920 if (other == mp || other->ma_used == 0)
1921 /* a.update(a) or a.update({}); nothing to do */
1922 return 0;
1923 if (mp->ma_used == 0)
1924 /* Since the target dict is empty, PyDict_GetItem()
1925 * always returns NULL. Setting override to 1
1926 * skips the unnecessary test.
1927 */
1928 override = 1;
1929 /* Do one big resize at the start, rather than
1930 * incrementally resizing as we insert new items. Expect
1931 * that there will be no (or few) overlapping keys.
1932 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001933 if (mp->ma_keys->dk_usable * 3 < other->ma_used * 2)
1934 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001935 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001936 for (i = 0, n = DK_SIZE(other->ma_keys); i < n; i++) {
1937 PyObject *value;
1938 entry = &other->ma_keys->dk_entries[i];
1939 if (other->ma_values)
1940 value = other->ma_values[i];
1941 else
1942 value = entry->me_value;
1943
1944 if (value != NULL &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001945 (override ||
1946 PyDict_GetItem(a, entry->me_key) == NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001947 if (insertdict(mp, entry->me_key,
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001948 entry->me_hash,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001949 value) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001950 return -1;
1951 }
1952 }
1953 }
1954 else {
1955 /* Do it the generic, slower way */
1956 PyObject *keys = PyMapping_Keys(b);
1957 PyObject *iter;
1958 PyObject *key, *value;
1959 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 if (keys == NULL)
1962 /* Docstring says this is equivalent to E.keys() so
1963 * if E doesn't have a .keys() method we want
1964 * AttributeError to percolate up. Might as well
1965 * do the same for any other error.
1966 */
1967 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001968
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001969 iter = PyObject_GetIter(keys);
1970 Py_DECREF(keys);
1971 if (iter == NULL)
1972 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001973
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
1975 if (!override && PyDict_GetItem(a, key) != NULL) {
1976 Py_DECREF(key);
1977 continue;
1978 }
1979 value = PyObject_GetItem(b, key);
1980 if (value == NULL) {
1981 Py_DECREF(iter);
1982 Py_DECREF(key);
1983 return -1;
1984 }
1985 status = PyDict_SetItem(a, key, value);
1986 Py_DECREF(key);
1987 Py_DECREF(value);
1988 if (status < 0) {
1989 Py_DECREF(iter);
1990 return -1;
1991 }
1992 }
1993 Py_DECREF(iter);
1994 if (PyErr_Occurred())
1995 /* Iterator completed, via error */
1996 return -1;
1997 }
1998 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001999}
2000
2001static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002002dict_copy(PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002003{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002005}
2006
2007PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002008PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002009{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002010 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002011 PyDictObject *mp;
2012 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002014 if (o == NULL || !PyDict_Check(o)) {
2015 PyErr_BadInternalCall();
2016 return NULL;
2017 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002018 mp = (PyDictObject *)o;
2019 if (_PyDict_HasSplitTable(mp)) {
2020 PyDictObject *split_copy;
2021 PyObject **newvalues = new_values(DK_SIZE(mp->ma_keys));
2022 if (newvalues == NULL)
2023 return PyErr_NoMemory();
2024 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2025 if (split_copy == NULL) {
2026 free_values(newvalues);
2027 return NULL;
2028 }
2029 split_copy->ma_values = newvalues;
2030 split_copy->ma_keys = mp->ma_keys;
2031 split_copy->ma_used = mp->ma_used;
2032 DK_INCREF(mp->ma_keys);
2033 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2034 PyObject *value = mp->ma_values[i];
2035 Py_XINCREF(value);
2036 split_copy->ma_values[i] = value;
2037 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002038 if (_PyObject_GC_IS_TRACKED(mp))
2039 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002040 return (PyObject *)split_copy;
2041 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002042 copy = PyDict_New();
2043 if (copy == NULL)
2044 return NULL;
2045 if (PyDict_Merge(copy, o, 1) == 0)
2046 return copy;
2047 Py_DECREF(copy);
2048 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002049}
2050
Martin v. Löwis18e16552006-02-15 17:27:45 +00002051Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002052PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002053{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002054 if (mp == NULL || !PyDict_Check(mp)) {
2055 PyErr_BadInternalCall();
2056 return -1;
2057 }
2058 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002059}
2060
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002061PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002062PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002063{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002064 if (mp == NULL || !PyDict_Check(mp)) {
2065 PyErr_BadInternalCall();
2066 return NULL;
2067 }
2068 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002069}
2070
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002071PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002072PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002073{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002074 if (mp == NULL || !PyDict_Check(mp)) {
2075 PyErr_BadInternalCall();
2076 return NULL;
2077 }
2078 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002079}
2080
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002081PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002082PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002083{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002084 if (mp == NULL || !PyDict_Check(mp)) {
2085 PyErr_BadInternalCall();
2086 return NULL;
2087 }
2088 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002089}
2090
Tim Peterse63415e2001-05-08 04:38:29 +00002091/* Return 1 if dicts equal, 0 if not, -1 if error.
2092 * Gets out as soon as any difference is detected.
2093 * Uses only Py_EQ comparison.
2094 */
2095static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002096dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002097{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002098 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002100 if (a->ma_used != b->ma_used)
2101 /* can't be equal if # of entries differ */
2102 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002103 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002104 for (i = 0; i < DK_SIZE(a->ma_keys); i++) {
2105 PyDictKeyEntry *ep = &a->ma_keys->dk_entries[i];
2106 PyObject *aval;
2107 if (a->ma_values)
2108 aval = a->ma_values[i];
2109 else
2110 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002111 if (aval != NULL) {
2112 int cmp;
2113 PyObject *bval;
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002114 PyObject **vaddr;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002115 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002116 /* temporarily bump aval's refcount to ensure it stays
2117 alive until we're done with it */
2118 Py_INCREF(aval);
2119 /* ditto for key */
2120 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002121 /* reuse the known hash value */
2122 if ((b->ma_keys->dk_lookup)(b, key, ep->me_hash, &vaddr) == NULL)
2123 bval = NULL;
2124 else
2125 bval = *vaddr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002126 Py_DECREF(key);
2127 if (bval == NULL) {
2128 Py_DECREF(aval);
2129 if (PyErr_Occurred())
2130 return -1;
2131 return 0;
2132 }
2133 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
2134 Py_DECREF(aval);
2135 if (cmp <= 0) /* error or not equal */
2136 return cmp;
2137 }
2138 }
2139 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002140}
Tim Peterse63415e2001-05-08 04:38:29 +00002141
2142static PyObject *
2143dict_richcompare(PyObject *v, PyObject *w, int op)
2144{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002145 int cmp;
2146 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002148 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2149 res = Py_NotImplemented;
2150 }
2151 else if (op == Py_EQ || op == Py_NE) {
2152 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2153 if (cmp < 0)
2154 return NULL;
2155 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2156 }
2157 else
2158 res = Py_NotImplemented;
2159 Py_INCREF(res);
2160 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002161}
Tim Peterse63415e2001-05-08 04:38:29 +00002162
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002163static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002164dict_contains(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002165{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002166 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002167 PyDictKeyEntry *ep;
2168 PyObject **value_addr;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002170 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002171 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002172 hash = PyObject_Hash(key);
2173 if (hash == -1)
2174 return NULL;
2175 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002176 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002177 if (ep == NULL)
2178 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002179 return PyBool_FromLong(*value_addr != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002180}
2181
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002182static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002183dict_get(PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00002184{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002185 PyObject *key;
2186 PyObject *failobj = Py_None;
2187 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002188 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002189 PyDictKeyEntry *ep;
2190 PyObject **value_addr;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
2193 return NULL;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002195 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002196 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 hash = PyObject_Hash(key);
2198 if (hash == -1)
2199 return NULL;
2200 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002201 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002202 if (ep == NULL)
2203 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002204 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002205 if (val == NULL)
2206 val = failobj;
2207 Py_INCREF(val);
2208 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002209}
2210
Benjamin Peterson00e98862013-03-07 22:16:29 -05002211PyObject *
2212PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002213{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002214 PyDictObject *mp = (PyDictObject *)d;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002215 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002216 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002217 PyDictKeyEntry *ep;
2218 PyObject **value_addr;
Guido van Rossum164452c2000-08-08 16:12:54 +00002219
Benjamin Peterson00e98862013-03-07 22:16:29 -05002220 if (!PyDict_Check(d)) {
2221 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002222 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002223 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002224 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002225 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002226 hash = PyObject_Hash(key);
2227 if (hash == -1)
2228 return NULL;
2229 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002230 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002231 if (ep == NULL)
2232 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002233 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002234 if (val == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002235 if (mp->ma_keys->dk_usable <= 0) {
2236 /* Need to resize. */
2237 if (insertion_resize(mp) < 0)
2238 return NULL;
2239 ep = find_empty_slot(mp, key, hash, &value_addr);
2240 }
Benjamin Peterson00e98862013-03-07 22:16:29 -05002241 Py_INCREF(defaultobj);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002242 Py_INCREF(key);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002243 MAINTAIN_TRACKING(mp, key, defaultobj);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002244 ep->me_key = key;
2245 ep->me_hash = hash;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002246 *value_addr = defaultobj;
2247 val = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002248 mp->ma_keys->dk_usable--;
2249 mp->ma_used++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002250 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002251 return val;
Guido van Rossum164452c2000-08-08 16:12:54 +00002252}
2253
Benjamin Peterson00e98862013-03-07 22:16:29 -05002254static PyObject *
2255dict_setdefault(PyDictObject *mp, PyObject *args)
2256{
2257 PyObject *key, *val;
2258 PyObject *defaultobj = Py_None;
2259
2260 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &defaultobj))
2261 return NULL;
2262
Benjamin Peterson55898502013-03-08 08:36:49 -05002263 val = PyDict_SetDefault((PyObject *)mp, key, defaultobj);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002264 Py_XINCREF(val);
2265 return val;
2266}
Guido van Rossum164452c2000-08-08 16:12:54 +00002267
2268static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002269dict_clear(PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002270{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002271 PyDict_Clear((PyObject *)mp);
2272 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002273}
2274
Guido van Rossumba6ab842000-12-12 22:02:18 +00002275static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002276dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002277{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002278 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002279 PyObject *old_value, *old_key;
2280 PyObject *key, *deflt = NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002281 PyDictKeyEntry *ep;
2282 PyObject **value_addr;
Guido van Rossume027d982002-04-12 15:11:59 +00002283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002284 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2285 return NULL;
2286 if (mp->ma_used == 0) {
2287 if (deflt) {
2288 Py_INCREF(deflt);
2289 return deflt;
2290 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07002291 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002292 return NULL;
2293 }
2294 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002295 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002296 hash = PyObject_Hash(key);
2297 if (hash == -1)
2298 return NULL;
2299 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002300 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002301 if (ep == NULL)
2302 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002303 old_value = *value_addr;
2304 if (old_value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002305 if (deflt) {
2306 Py_INCREF(deflt);
2307 return deflt;
2308 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07002309 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 return NULL;
2311 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002312 *value_addr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002313 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002314 if (!_PyDict_HasSplitTable(mp)) {
2315 ENSURE_ALLOWS_DELETIONS(mp);
2316 old_key = ep->me_key;
2317 Py_INCREF(dummy);
2318 ep->me_key = dummy;
2319 Py_DECREF(old_key);
2320 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002321 return old_value;
Guido van Rossume027d982002-04-12 15:11:59 +00002322}
2323
2324static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002325dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00002326{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002327 Py_hash_t i = 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002328 PyDictKeyEntry *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002329 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002330
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002332 /* Allocate the result tuple before checking the size. Believe it
2333 * or not, this allocation could trigger a garbage collection which
2334 * could empty the dict, so if we checked the size first and that
2335 * happened, the result would be an infinite loop (searching for an
2336 * entry that no longer exists). Note that the usual popitem()
2337 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2338 * tuple away if the dict *is* empty isn't a significant
2339 * inefficiency -- possible, but unlikely in practice.
2340 */
2341 res = PyTuple_New(2);
2342 if (res == NULL)
2343 return NULL;
2344 if (mp->ma_used == 0) {
2345 Py_DECREF(res);
2346 PyErr_SetString(PyExc_KeyError,
2347 "popitem(): dictionary is empty");
2348 return NULL;
2349 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002350 /* Convert split table to combined table */
2351 if (mp->ma_keys->dk_lookup == lookdict_split) {
2352 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
2353 Py_DECREF(res);
2354 return NULL;
2355 }
2356 }
2357 ENSURE_ALLOWS_DELETIONS(mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 /* Set ep to "the first" dict entry with a value. We abuse the hash
2359 * field of slot 0 to hold a search finger:
2360 * If slot 0 has a value, use slot 0.
2361 * Else slot 0 is being used to hold a search finger,
2362 * and we use its hash value as the first index to look.
2363 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002364 ep = &mp->ma_keys->dk_entries[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002365 if (ep->me_value == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002366 Py_ssize_t mask = DK_MASK(mp->ma_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002367 i = ep->me_hash;
2368 /* The hash field may be a real hash value, or it may be a
2369 * legit search finger, or it may be a once-legit search
2370 * finger that's out of bounds now because it wrapped around
2371 * or the table shrunk -- simply make sure it's in bounds now.
2372 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002373 if (i > mask || i < 1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002374 i = 1; /* skip slot 0 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002375 while ((ep = &mp->ma_keys->dk_entries[i])->me_value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002377 if (i > mask)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002378 i = 1;
2379 }
2380 }
2381 PyTuple_SET_ITEM(res, 0, ep->me_key);
2382 PyTuple_SET_ITEM(res, 1, ep->me_value);
2383 Py_INCREF(dummy);
2384 ep->me_key = dummy;
2385 ep->me_value = NULL;
2386 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002387 assert(mp->ma_keys->dk_entries[0].me_value == NULL);
2388 mp->ma_keys->dk_entries[0].me_hash = i + 1; /* next place to start */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002390}
2391
Jeremy Hylton8caad492000-06-23 14:18:11 +00002392static int
2393dict_traverse(PyObject *op, visitproc visit, void *arg)
2394{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002395 Py_ssize_t i, n;
2396 PyDictObject *mp = (PyDictObject *)op;
2397 if (mp->ma_keys->dk_lookup == lookdict) {
2398 for (i = 0; i < DK_SIZE(mp->ma_keys); i++) {
2399 if (mp->ma_keys->dk_entries[i].me_value != NULL) {
2400 Py_VISIT(mp->ma_keys->dk_entries[i].me_value);
2401 Py_VISIT(mp->ma_keys->dk_entries[i].me_key);
2402 }
2403 }
2404 } else {
2405 if (mp->ma_values != NULL) {
2406 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2407 Py_VISIT(mp->ma_values[i]);
2408 }
2409 }
2410 else {
2411 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2412 Py_VISIT(mp->ma_keys->dk_entries[i].me_value);
2413 }
2414 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002415 }
2416 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002417}
2418
2419static int
2420dict_tp_clear(PyObject *op)
2421{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002422 PyDict_Clear(op);
2423 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002424}
2425
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002426static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002427
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002428static PyObject *
2429dict_sizeof(PyDictObject *mp)
2430{
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002431 Py_ssize_t size, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002432
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002433 size = DK_SIZE(mp->ma_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002434 res = sizeof(PyDictObject);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002435 if (mp->ma_values)
2436 res += size * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002437 /* If the dictionary is split, the keys portion is accounted-for
2438 in the type object. */
2439 if (mp->ma_keys->dk_refcnt == 1)
2440 res += sizeof(PyDictKeysObject) + (size-1) * sizeof(PyDictKeyEntry);
2441 return PyLong_FromSsize_t(res);
2442}
2443
2444Py_ssize_t
2445_PyDict_KeysSize(PyDictKeysObject *keys)
2446{
2447 return sizeof(PyDictKeysObject) + (DK_SIZE(keys)-1) * sizeof(PyDictKeyEntry);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002448}
2449
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002450PyDoc_STRVAR(contains__doc__,
2451"D.__contains__(k) -> True if D has a key k, else False");
2452
2453PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
2454
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002455PyDoc_STRVAR(sizeof__doc__,
2456"D.__sizeof__() -> size of D in memory, in bytes");
2457
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002458PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002459"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002460
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002461PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002462"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 +00002463
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002464PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002465"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002466If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00002467
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002468PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00002469"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000024702-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002471
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002472PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04002473"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
2474If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
2475If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
2476In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00002477
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002478PyDoc_STRVAR(fromkeys__doc__,
2479"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
2480v defaults to None.");
2481
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002482PyDoc_STRVAR(clear__doc__,
2483"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002484
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002485PyDoc_STRVAR(copy__doc__,
2486"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00002487
Guido van Rossumb90c8482007-02-10 01:11:45 +00002488/* Forward */
2489static PyObject *dictkeys_new(PyObject *);
2490static PyObject *dictitems_new(PyObject *);
2491static PyObject *dictvalues_new(PyObject *);
2492
Guido van Rossum45c85d12007-07-27 16:31:40 +00002493PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002494 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002495PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002496 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002497PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002498 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00002499
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002500static PyMethodDef mapp_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002501 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
2502 contains__doc__},
2503 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
2504 getitem__doc__},
2505 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
2506 sizeof__doc__},
2507 {"get", (PyCFunction)dict_get, METH_VARARGS,
2508 get__doc__},
2509 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
2510 setdefault_doc__},
2511 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
2512 pop__doc__},
2513 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
2514 popitem__doc__},
2515 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
2516 keys__doc__},
2517 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
2518 items__doc__},
2519 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
2520 values__doc__},
2521 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
2522 update__doc__},
2523 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
2524 fromkeys__doc__},
2525 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
2526 clear__doc__},
2527 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
2528 copy__doc__},
2529 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002530};
2531
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002532/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002533int
2534PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002535{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002536 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002537 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002538 PyDictKeyEntry *ep;
2539 PyObject **value_addr;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002541 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002542 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002543 hash = PyObject_Hash(key);
2544 if (hash == -1)
2545 return -1;
2546 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002547 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
2548 return (ep == NULL) ? -1 : (*value_addr != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002549}
2550
Thomas Wouterscf297e42007-02-23 15:07:44 +00002551/* Internal version of PyDict_Contains used when the hash value is already known */
2552int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002553_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00002554{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002555 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002556 PyDictKeyEntry *ep;
2557 PyObject **value_addr;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002558
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002559 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
2560 return (ep == NULL) ? -1 : (*value_addr != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00002561}
2562
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002563/* Hack to implement "key in dict" */
2564static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002565 0, /* sq_length */
2566 0, /* sq_concat */
2567 0, /* sq_repeat */
2568 0, /* sq_item */
2569 0, /* sq_slice */
2570 0, /* sq_ass_item */
2571 0, /* sq_ass_slice */
2572 PyDict_Contains, /* sq_contains */
2573 0, /* sq_inplace_concat */
2574 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002575};
2576
Guido van Rossum09e563a2001-05-01 12:10:21 +00002577static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002578dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2579{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002580 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02002581 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002583 assert(type != NULL && type->tp_alloc != NULL);
2584 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02002585 if (self == NULL)
2586 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02002587 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02002588
Victor Stinnera9f61a52013-07-16 22:17:26 +02002589 /* The object has been implicitly tracked by tp_alloc */
2590 if (type == &PyDict_Type)
2591 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02002592
2593 d->ma_used = 0;
2594 d->ma_keys = new_keys_object(PyDict_MINSIZE_COMBINED);
2595 if (d->ma_keys == NULL) {
2596 Py_DECREF(self);
2597 return NULL;
2598 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002599 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002600}
2601
Tim Peters25786c02001-09-02 08:22:48 +00002602static int
2603dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2604{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002605 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002606}
2607
Tim Peters6d6c1a32001-08-02 04:15:00 +00002608static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002609dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002610{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002611 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002612}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002613
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002614PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002615"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002616"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002617" (key, value) pairs\n"
2618"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002619" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002620" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002621" d[k] = v\n"
2622"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2623" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002624
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002625PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002626 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2627 "dict",
2628 sizeof(PyDictObject),
2629 0,
2630 (destructor)dict_dealloc, /* tp_dealloc */
2631 0, /* tp_print */
2632 0, /* tp_getattr */
2633 0, /* tp_setattr */
2634 0, /* tp_reserved */
2635 (reprfunc)dict_repr, /* tp_repr */
2636 0, /* tp_as_number */
2637 &dict_as_sequence, /* tp_as_sequence */
2638 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002639 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002640 0, /* tp_call */
2641 0, /* tp_str */
2642 PyObject_GenericGetAttr, /* tp_getattro */
2643 0, /* tp_setattro */
2644 0, /* tp_as_buffer */
2645 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2646 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
2647 dictionary_doc, /* tp_doc */
2648 dict_traverse, /* tp_traverse */
2649 dict_tp_clear, /* tp_clear */
2650 dict_richcompare, /* tp_richcompare */
2651 0, /* tp_weaklistoffset */
2652 (getiterfunc)dict_iter, /* tp_iter */
2653 0, /* tp_iternext */
2654 mapp_methods, /* tp_methods */
2655 0, /* tp_members */
2656 0, /* tp_getset */
2657 0, /* tp_base */
2658 0, /* tp_dict */
2659 0, /* tp_descr_get */
2660 0, /* tp_descr_set */
2661 0, /* tp_dictoffset */
2662 dict_init, /* tp_init */
2663 PyType_GenericAlloc, /* tp_alloc */
2664 dict_new, /* tp_new */
2665 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002666};
2667
Victor Stinner3c1e4812012-03-26 22:10:51 +02002668PyObject *
2669_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
2670{
2671 PyObject *kv;
2672 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02002673 if (kv == NULL) {
2674 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002675 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02002676 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02002677 return PyDict_GetItem(dp, kv);
2678}
2679
Guido van Rossum3cca2451997-05-16 14:23:33 +00002680/* For backward compatibility with old dictionary interface */
2681
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002682PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002683PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002684{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002685 PyObject *kv, *rv;
2686 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02002687 if (kv == NULL) {
2688 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002689 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02002690 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002691 rv = PyDict_GetItem(v, kv);
2692 Py_DECREF(kv);
2693 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002694}
2695
2696int
Victor Stinner3c1e4812012-03-26 22:10:51 +02002697_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
2698{
2699 PyObject *kv;
2700 kv = _PyUnicode_FromId(key); /* borrowed */
2701 if (kv == NULL)
2702 return -1;
2703 return PyDict_SetItem(v, kv, item);
2704}
2705
2706int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002707PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002708{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002709 PyObject *kv;
2710 int err;
2711 kv = PyUnicode_FromString(key);
2712 if (kv == NULL)
2713 return -1;
2714 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
2715 err = PyDict_SetItem(v, kv, item);
2716 Py_DECREF(kv);
2717 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002718}
2719
2720int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002721PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002722{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002723 PyObject *kv;
2724 int err;
2725 kv = PyUnicode_FromString(key);
2726 if (kv == NULL)
2727 return -1;
2728 err = PyDict_DelItem(v, kv);
2729 Py_DECREF(kv);
2730 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002731}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002732
Raymond Hettinger019a1482004-03-18 02:41:19 +00002733/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002734
2735typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002736 PyObject_HEAD
2737 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
2738 Py_ssize_t di_used;
2739 Py_ssize_t di_pos;
2740 PyObject* di_result; /* reusable result tuple for iteritems */
2741 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002742} dictiterobject;
2743
2744static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002745dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002746{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002747 dictiterobject *di;
2748 di = PyObject_GC_New(dictiterobject, itertype);
2749 if (di == NULL)
2750 return NULL;
2751 Py_INCREF(dict);
2752 di->di_dict = dict;
2753 di->di_used = dict->ma_used;
2754 di->di_pos = 0;
2755 di->len = dict->ma_used;
2756 if (itertype == &PyDictIterItem_Type) {
2757 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2758 if (di->di_result == NULL) {
2759 Py_DECREF(di);
2760 return NULL;
2761 }
2762 }
2763 else
2764 di->di_result = NULL;
2765 _PyObject_GC_TRACK(di);
2766 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002767}
2768
2769static void
2770dictiter_dealloc(dictiterobject *di)
2771{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002772 Py_XDECREF(di->di_dict);
2773 Py_XDECREF(di->di_result);
2774 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002775}
2776
2777static int
2778dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
2779{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002780 Py_VISIT(di->di_dict);
2781 Py_VISIT(di->di_result);
2782 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002783}
2784
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002785static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002786dictiter_len(dictiterobject *di)
2787{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002788 Py_ssize_t len = 0;
2789 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
2790 len = di->len;
2791 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002792}
2793
Guido van Rossumb90c8482007-02-10 01:11:45 +00002794PyDoc_STRVAR(length_hint_doc,
2795 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002796
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002797static PyObject *
2798dictiter_reduce(dictiterobject *di);
2799
2800PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
2801
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002802static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002803 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
2804 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002805 {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS,
2806 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002807 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002808};
2809
Raymond Hettinger019a1482004-03-18 02:41:19 +00002810static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002811{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002812 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002813 Py_ssize_t i, mask, offset;
2814 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002815 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002816 PyObject **value_ptr;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002818 if (d == NULL)
2819 return NULL;
2820 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002822 if (di->di_used != d->ma_used) {
2823 PyErr_SetString(PyExc_RuntimeError,
2824 "dictionary changed size during iteration");
2825 di->di_used = -1; /* Make this state sticky */
2826 return NULL;
2827 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002829 i = di->di_pos;
2830 if (i < 0)
2831 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002832 k = d->ma_keys;
2833 if (d->ma_values) {
2834 value_ptr = &d->ma_values[i];
2835 offset = sizeof(PyObject *);
2836 }
2837 else {
2838 value_ptr = &k->dk_entries[i].me_value;
2839 offset = sizeof(PyDictKeyEntry);
2840 }
2841 mask = DK_SIZE(k)-1;
2842 while (i <= mask && *value_ptr == NULL) {
2843 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002844 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002845 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002846 di->di_pos = i+1;
2847 if (i > mask)
2848 goto fail;
2849 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002850 key = k->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002851 Py_INCREF(key);
2852 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002853
2854fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002855 Py_DECREF(d);
2856 di->di_dict = NULL;
2857 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002858}
2859
Raymond Hettinger019a1482004-03-18 02:41:19 +00002860PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002861 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2862 "dict_keyiterator", /* tp_name */
2863 sizeof(dictiterobject), /* tp_basicsize */
2864 0, /* tp_itemsize */
2865 /* methods */
2866 (destructor)dictiter_dealloc, /* tp_dealloc */
2867 0, /* tp_print */
2868 0, /* tp_getattr */
2869 0, /* tp_setattr */
2870 0, /* tp_reserved */
2871 0, /* tp_repr */
2872 0, /* tp_as_number */
2873 0, /* tp_as_sequence */
2874 0, /* tp_as_mapping */
2875 0, /* tp_hash */
2876 0, /* tp_call */
2877 0, /* tp_str */
2878 PyObject_GenericGetAttr, /* tp_getattro */
2879 0, /* tp_setattro */
2880 0, /* tp_as_buffer */
2881 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2882 0, /* tp_doc */
2883 (traverseproc)dictiter_traverse, /* tp_traverse */
2884 0, /* tp_clear */
2885 0, /* tp_richcompare */
2886 0, /* tp_weaklistoffset */
2887 PyObject_SelfIter, /* tp_iter */
2888 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
2889 dictiter_methods, /* tp_methods */
2890 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002891};
2892
2893static PyObject *dictiter_iternextvalue(dictiterobject *di)
2894{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002895 PyObject *value;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002896 Py_ssize_t i, mask, offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002897 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002898 PyObject **value_ptr;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002900 if (d == NULL)
2901 return NULL;
2902 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00002903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002904 if (di->di_used != d->ma_used) {
2905 PyErr_SetString(PyExc_RuntimeError,
2906 "dictionary changed size during iteration");
2907 di->di_used = -1; /* Make this state sticky */
2908 return NULL;
2909 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002911 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002912 mask = DK_SIZE(d->ma_keys)-1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 if (i < 0 || i > mask)
2914 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002915 if (d->ma_values) {
2916 value_ptr = &d->ma_values[i];
2917 offset = sizeof(PyObject *);
2918 }
2919 else {
2920 value_ptr = &d->ma_keys->dk_entries[i].me_value;
2921 offset = sizeof(PyDictKeyEntry);
2922 }
2923 while (i <= mask && *value_ptr == NULL) {
2924 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002925 i++;
2926 if (i > mask)
2927 goto fail;
2928 }
2929 di->di_pos = i+1;
2930 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002931 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002932 Py_INCREF(value);
2933 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002934
2935fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002936 Py_DECREF(d);
2937 di->di_dict = NULL;
2938 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002939}
2940
2941PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002942 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2943 "dict_valueiterator", /* tp_name */
2944 sizeof(dictiterobject), /* tp_basicsize */
2945 0, /* tp_itemsize */
2946 /* methods */
2947 (destructor)dictiter_dealloc, /* tp_dealloc */
2948 0, /* tp_print */
2949 0, /* tp_getattr */
2950 0, /* tp_setattr */
2951 0, /* tp_reserved */
2952 0, /* tp_repr */
2953 0, /* tp_as_number */
2954 0, /* tp_as_sequence */
2955 0, /* tp_as_mapping */
2956 0, /* tp_hash */
2957 0, /* tp_call */
2958 0, /* tp_str */
2959 PyObject_GenericGetAttr, /* tp_getattro */
2960 0, /* tp_setattro */
2961 0, /* tp_as_buffer */
2962 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2963 0, /* tp_doc */
2964 (traverseproc)dictiter_traverse, /* tp_traverse */
2965 0, /* tp_clear */
2966 0, /* tp_richcompare */
2967 0, /* tp_weaklistoffset */
2968 PyObject_SelfIter, /* tp_iter */
2969 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
2970 dictiter_methods, /* tp_methods */
2971 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002972};
2973
2974static PyObject *dictiter_iternextitem(dictiterobject *di)
2975{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002976 PyObject *key, *value, *result = di->di_result;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002977 Py_ssize_t i, mask, offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002978 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002979 PyObject **value_ptr;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002981 if (d == NULL)
2982 return NULL;
2983 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00002984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002985 if (di->di_used != d->ma_used) {
2986 PyErr_SetString(PyExc_RuntimeError,
2987 "dictionary changed size during iteration");
2988 di->di_used = -1; /* Make this state sticky */
2989 return NULL;
2990 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002992 i = di->di_pos;
2993 if (i < 0)
2994 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002995 mask = DK_SIZE(d->ma_keys)-1;
2996 if (d->ma_values) {
2997 value_ptr = &d->ma_values[i];
2998 offset = sizeof(PyObject *);
2999 }
3000 else {
3001 value_ptr = &d->ma_keys->dk_entries[i].me_value;
3002 offset = sizeof(PyDictKeyEntry);
3003 }
3004 while (i <= mask && *value_ptr == NULL) {
3005 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003006 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003007 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003008 di->di_pos = i+1;
3009 if (i > mask)
3010 goto fail;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003011
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003012 if (result->ob_refcnt == 1) {
3013 Py_INCREF(result);
3014 Py_DECREF(PyTuple_GET_ITEM(result, 0));
3015 Py_DECREF(PyTuple_GET_ITEM(result, 1));
3016 } else {
3017 result = PyTuple_New(2);
3018 if (result == NULL)
3019 return NULL;
3020 }
3021 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003022 key = d->ma_keys->dk_entries[i].me_key;
3023 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003024 Py_INCREF(key);
3025 Py_INCREF(value);
3026 PyTuple_SET_ITEM(result, 0, key);
3027 PyTuple_SET_ITEM(result, 1, value);
3028 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003029
3030fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003031 Py_DECREF(d);
3032 di->di_dict = NULL;
3033 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003034}
3035
3036PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003037 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3038 "dict_itemiterator", /* tp_name */
3039 sizeof(dictiterobject), /* tp_basicsize */
3040 0, /* tp_itemsize */
3041 /* methods */
3042 (destructor)dictiter_dealloc, /* tp_dealloc */
3043 0, /* tp_print */
3044 0, /* tp_getattr */
3045 0, /* tp_setattr */
3046 0, /* tp_reserved */
3047 0, /* tp_repr */
3048 0, /* tp_as_number */
3049 0, /* tp_as_sequence */
3050 0, /* tp_as_mapping */
3051 0, /* tp_hash */
3052 0, /* tp_call */
3053 0, /* tp_str */
3054 PyObject_GenericGetAttr, /* tp_getattro */
3055 0, /* tp_setattro */
3056 0, /* tp_as_buffer */
3057 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3058 0, /* tp_doc */
3059 (traverseproc)dictiter_traverse, /* tp_traverse */
3060 0, /* tp_clear */
3061 0, /* tp_richcompare */
3062 0, /* tp_weaklistoffset */
3063 PyObject_SelfIter, /* tp_iter */
3064 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3065 dictiter_methods, /* tp_methods */
3066 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003067};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003068
3069
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003070static PyObject *
3071dictiter_reduce(dictiterobject *di)
3072{
3073 PyObject *list;
3074 dictiterobject tmp;
3075
3076 list = PyList_New(0);
3077 if (!list)
3078 return NULL;
3079
3080 /* copy the itertor state */
3081 tmp = *di;
3082 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003083
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003084 /* iterate the temporary into a list */
3085 for(;;) {
3086 PyObject *element = 0;
3087 if (Py_TYPE(di) == &PyDictIterItem_Type)
3088 element = dictiter_iternextitem(&tmp);
3089 else if (Py_TYPE(di) == &PyDictIterKey_Type)
3090 element = dictiter_iternextkey(&tmp);
3091 else if (Py_TYPE(di) == &PyDictIterValue_Type)
3092 element = dictiter_iternextvalue(&tmp);
3093 else
3094 assert(0);
3095 if (element) {
3096 if (PyList_Append(list, element)) {
3097 Py_DECREF(element);
3098 Py_DECREF(list);
3099 Py_XDECREF(tmp.di_dict);
3100 return NULL;
3101 }
3102 Py_DECREF(element);
3103 } else
3104 break;
3105 }
3106 Py_XDECREF(tmp.di_dict);
3107 /* check for error */
3108 if (tmp.di_dict != NULL) {
3109 /* we have an error */
3110 Py_DECREF(list);
3111 return NULL;
3112 }
Antoine Pitroua7013882012-04-05 00:04:20 +02003113 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003114}
3115
Guido van Rossum3ac67412007-02-10 18:55:06 +00003116/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003117/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003118/***********************************************/
3119
Guido van Rossumb90c8482007-02-10 01:11:45 +00003120/* The instance lay-out is the same for all three; but the type differs. */
3121
3122typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003123 PyObject_HEAD
3124 PyDictObject *dv_dict;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003125} dictviewobject;
3126
3127
3128static void
Guido van Rossum3ac67412007-02-10 18:55:06 +00003129dictview_dealloc(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003130{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003131 Py_XDECREF(dv->dv_dict);
3132 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003133}
3134
3135static int
3136dictview_traverse(dictviewobject *dv, visitproc visit, void *arg)
3137{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003138 Py_VISIT(dv->dv_dict);
3139 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003140}
3141
Guido van Rossum83825ac2007-02-10 04:54:19 +00003142static Py_ssize_t
Guido van Rossum3ac67412007-02-10 18:55:06 +00003143dictview_len(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003144{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003145 Py_ssize_t len = 0;
3146 if (dv->dv_dict != NULL)
3147 len = dv->dv_dict->ma_used;
3148 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003149}
3150
3151static PyObject *
3152dictview_new(PyObject *dict, PyTypeObject *type)
3153{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003154 dictviewobject *dv;
3155 if (dict == NULL) {
3156 PyErr_BadInternalCall();
3157 return NULL;
3158 }
3159 if (!PyDict_Check(dict)) {
3160 /* XXX Get rid of this restriction later */
3161 PyErr_Format(PyExc_TypeError,
3162 "%s() requires a dict argument, not '%s'",
3163 type->tp_name, dict->ob_type->tp_name);
3164 return NULL;
3165 }
3166 dv = PyObject_GC_New(dictviewobject, type);
3167 if (dv == NULL)
3168 return NULL;
3169 Py_INCREF(dict);
3170 dv->dv_dict = (PyDictObject *)dict;
3171 _PyObject_GC_TRACK(dv);
3172 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003173}
3174
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003175/* TODO(guido): The views objects are not complete:
3176
3177 * support more set operations
3178 * support arbitrary mappings?
3179 - either these should be static or exported in dictobject.h
3180 - if public then they should probably be in builtins
3181*/
3182
Guido van Rossumaac530c2007-08-24 22:33:45 +00003183/* Return 1 if self is a subset of other, iterating over self;
3184 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003185static int
3186all_contained_in(PyObject *self, PyObject *other)
3187{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003188 PyObject *iter = PyObject_GetIter(self);
3189 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003191 if (iter == NULL)
3192 return -1;
3193 for (;;) {
3194 PyObject *next = PyIter_Next(iter);
3195 if (next == NULL) {
3196 if (PyErr_Occurred())
3197 ok = -1;
3198 break;
3199 }
3200 ok = PySequence_Contains(other, next);
3201 Py_DECREF(next);
3202 if (ok <= 0)
3203 break;
3204 }
3205 Py_DECREF(iter);
3206 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003207}
3208
3209static PyObject *
3210dictview_richcompare(PyObject *self, PyObject *other, int op)
3211{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003212 Py_ssize_t len_self, len_other;
3213 int ok;
3214 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003216 assert(self != NULL);
3217 assert(PyDictViewSet_Check(self));
3218 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00003219
Brian Curtindfc80e32011-08-10 20:28:54 -05003220 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
3221 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003223 len_self = PyObject_Size(self);
3224 if (len_self < 0)
3225 return NULL;
3226 len_other = PyObject_Size(other);
3227 if (len_other < 0)
3228 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003230 ok = 0;
3231 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00003232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003233 case Py_NE:
3234 case Py_EQ:
3235 if (len_self == len_other)
3236 ok = all_contained_in(self, other);
3237 if (op == Py_NE && ok >= 0)
3238 ok = !ok;
3239 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003241 case Py_LT:
3242 if (len_self < len_other)
3243 ok = all_contained_in(self, other);
3244 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003246 case Py_LE:
3247 if (len_self <= len_other)
3248 ok = all_contained_in(self, other);
3249 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003251 case Py_GT:
3252 if (len_self > len_other)
3253 ok = all_contained_in(other, self);
3254 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003256 case Py_GE:
3257 if (len_self >= len_other)
3258 ok = all_contained_in(other, self);
3259 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003261 }
3262 if (ok < 0)
3263 return NULL;
3264 result = ok ? Py_True : Py_False;
3265 Py_INCREF(result);
3266 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003267}
3268
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003269static PyObject *
3270dictview_repr(dictviewobject *dv)
3271{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003272 PyObject *seq;
3273 PyObject *result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003275 seq = PySequence_List((PyObject *)dv);
3276 if (seq == NULL)
3277 return NULL;
3278
3279 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
3280 Py_DECREF(seq);
3281 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003282}
3283
Guido van Rossum3ac67412007-02-10 18:55:06 +00003284/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003285
3286static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003287dictkeys_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003288{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003289 if (dv->dv_dict == NULL) {
3290 Py_RETURN_NONE;
3291 }
3292 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003293}
3294
3295static int
3296dictkeys_contains(dictviewobject *dv, PyObject *obj)
3297{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003298 if (dv->dv_dict == NULL)
3299 return 0;
3300 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003301}
3302
Guido van Rossum83825ac2007-02-10 04:54:19 +00003303static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003304 (lenfunc)dictview_len, /* sq_length */
3305 0, /* sq_concat */
3306 0, /* sq_repeat */
3307 0, /* sq_item */
3308 0, /* sq_slice */
3309 0, /* sq_ass_item */
3310 0, /* sq_ass_slice */
3311 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003312};
3313
Guido van Rossum523259b2007-08-24 23:41:22 +00003314static PyObject*
3315dictviews_sub(PyObject* self, PyObject *other)
3316{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003317 PyObject *result = PySet_New(self);
3318 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003319 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003321 if (result == NULL)
3322 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003323
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003324 tmp = _PyObject_CallMethodId(result, &PyId_difference_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003325 if (tmp == NULL) {
3326 Py_DECREF(result);
3327 return NULL;
3328 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003330 Py_DECREF(tmp);
3331 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003332}
3333
3334static PyObject*
3335dictviews_and(PyObject* self, PyObject *other)
3336{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003337 PyObject *result = PySet_New(self);
3338 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003339 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003341 if (result == NULL)
3342 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003343
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003344 tmp = _PyObject_CallMethodId(result, &PyId_intersection_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003345 if (tmp == NULL) {
3346 Py_DECREF(result);
3347 return NULL;
3348 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003350 Py_DECREF(tmp);
3351 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003352}
3353
3354static PyObject*
3355dictviews_or(PyObject* self, PyObject *other)
3356{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003357 PyObject *result = PySet_New(self);
3358 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003359 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003361 if (result == NULL)
3362 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003363
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003364 tmp = _PyObject_CallMethodId(result, &PyId_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003365 if (tmp == NULL) {
3366 Py_DECREF(result);
3367 return NULL;
3368 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003370 Py_DECREF(tmp);
3371 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003372}
3373
3374static PyObject*
3375dictviews_xor(PyObject* self, PyObject *other)
3376{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003377 PyObject *result = PySet_New(self);
3378 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003379 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003381 if (result == NULL)
3382 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003383
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003384 tmp = _PyObject_CallMethodId(result, &PyId_symmetric_difference_update, "O",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003385 other);
3386 if (tmp == NULL) {
3387 Py_DECREF(result);
3388 return NULL;
3389 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003391 Py_DECREF(tmp);
3392 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003393}
3394
3395static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003396 0, /*nb_add*/
3397 (binaryfunc)dictviews_sub, /*nb_subtract*/
3398 0, /*nb_multiply*/
3399 0, /*nb_remainder*/
3400 0, /*nb_divmod*/
3401 0, /*nb_power*/
3402 0, /*nb_negative*/
3403 0, /*nb_positive*/
3404 0, /*nb_absolute*/
3405 0, /*nb_bool*/
3406 0, /*nb_invert*/
3407 0, /*nb_lshift*/
3408 0, /*nb_rshift*/
3409 (binaryfunc)dictviews_and, /*nb_and*/
3410 (binaryfunc)dictviews_xor, /*nb_xor*/
3411 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00003412};
3413
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003414static PyObject*
3415dictviews_isdisjoint(PyObject *self, PyObject *other)
3416{
3417 PyObject *it;
3418 PyObject *item = NULL;
3419
3420 if (self == other) {
3421 if (dictview_len((dictviewobject *)self) == 0)
3422 Py_RETURN_TRUE;
3423 else
3424 Py_RETURN_FALSE;
3425 }
3426
3427 /* Iterate over the shorter object (only if other is a set,
3428 * because PySequence_Contains may be expensive otherwise): */
3429 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
3430 Py_ssize_t len_self = dictview_len((dictviewobject *)self);
3431 Py_ssize_t len_other = PyObject_Size(other);
3432 if (len_other == -1)
3433 return NULL;
3434
3435 if ((len_other > len_self)) {
3436 PyObject *tmp = other;
3437 other = self;
3438 self = tmp;
3439 }
3440 }
3441
3442 it = PyObject_GetIter(other);
3443 if (it == NULL)
3444 return NULL;
3445
3446 while ((item = PyIter_Next(it)) != NULL) {
3447 int contains = PySequence_Contains(self, item);
3448 Py_DECREF(item);
3449 if (contains == -1) {
3450 Py_DECREF(it);
3451 return NULL;
3452 }
3453
3454 if (contains) {
3455 Py_DECREF(it);
3456 Py_RETURN_FALSE;
3457 }
3458 }
3459 Py_DECREF(it);
3460 if (PyErr_Occurred())
3461 return NULL; /* PyIter_Next raised an exception. */
3462 Py_RETURN_TRUE;
3463}
3464
3465PyDoc_STRVAR(isdisjoint_doc,
3466"Return True if the view and the given iterable have a null intersection.");
3467
Guido van Rossumb90c8482007-02-10 01:11:45 +00003468static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003469 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3470 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003471 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003472};
3473
3474PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003475 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3476 "dict_keys", /* tp_name */
3477 sizeof(dictviewobject), /* tp_basicsize */
3478 0, /* tp_itemsize */
3479 /* methods */
3480 (destructor)dictview_dealloc, /* tp_dealloc */
3481 0, /* tp_print */
3482 0, /* tp_getattr */
3483 0, /* tp_setattr */
3484 0, /* tp_reserved */
3485 (reprfunc)dictview_repr, /* tp_repr */
3486 &dictviews_as_number, /* tp_as_number */
3487 &dictkeys_as_sequence, /* tp_as_sequence */
3488 0, /* tp_as_mapping */
3489 0, /* tp_hash */
3490 0, /* tp_call */
3491 0, /* tp_str */
3492 PyObject_GenericGetAttr, /* tp_getattro */
3493 0, /* tp_setattro */
3494 0, /* tp_as_buffer */
3495 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3496 0, /* tp_doc */
3497 (traverseproc)dictview_traverse, /* tp_traverse */
3498 0, /* tp_clear */
3499 dictview_richcompare, /* tp_richcompare */
3500 0, /* tp_weaklistoffset */
3501 (getiterfunc)dictkeys_iter, /* tp_iter */
3502 0, /* tp_iternext */
3503 dictkeys_methods, /* tp_methods */
3504 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003505};
3506
3507static PyObject *
3508dictkeys_new(PyObject *dict)
3509{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003510 return dictview_new(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003511}
3512
Guido van Rossum3ac67412007-02-10 18:55:06 +00003513/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003514
3515static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003516dictitems_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003517{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003518 if (dv->dv_dict == NULL) {
3519 Py_RETURN_NONE;
3520 }
3521 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003522}
3523
3524static int
3525dictitems_contains(dictviewobject *dv, PyObject *obj)
3526{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003527 PyObject *key, *value, *found;
3528 if (dv->dv_dict == NULL)
3529 return 0;
3530 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
3531 return 0;
3532 key = PyTuple_GET_ITEM(obj, 0);
3533 value = PyTuple_GET_ITEM(obj, 1);
3534 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
3535 if (found == NULL) {
3536 if (PyErr_Occurred())
3537 return -1;
3538 return 0;
3539 }
3540 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003541}
3542
Guido van Rossum83825ac2007-02-10 04:54:19 +00003543static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003544 (lenfunc)dictview_len, /* sq_length */
3545 0, /* sq_concat */
3546 0, /* sq_repeat */
3547 0, /* sq_item */
3548 0, /* sq_slice */
3549 0, /* sq_ass_item */
3550 0, /* sq_ass_slice */
3551 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003552};
3553
Guido van Rossumb90c8482007-02-10 01:11:45 +00003554static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003555 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3556 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003557 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003558};
3559
3560PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003561 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3562 "dict_items", /* tp_name */
3563 sizeof(dictviewobject), /* tp_basicsize */
3564 0, /* tp_itemsize */
3565 /* methods */
3566 (destructor)dictview_dealloc, /* tp_dealloc */
3567 0, /* tp_print */
3568 0, /* tp_getattr */
3569 0, /* tp_setattr */
3570 0, /* tp_reserved */
3571 (reprfunc)dictview_repr, /* tp_repr */
3572 &dictviews_as_number, /* tp_as_number */
3573 &dictitems_as_sequence, /* tp_as_sequence */
3574 0, /* tp_as_mapping */
3575 0, /* tp_hash */
3576 0, /* tp_call */
3577 0, /* tp_str */
3578 PyObject_GenericGetAttr, /* tp_getattro */
3579 0, /* tp_setattro */
3580 0, /* tp_as_buffer */
3581 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3582 0, /* tp_doc */
3583 (traverseproc)dictview_traverse, /* tp_traverse */
3584 0, /* tp_clear */
3585 dictview_richcompare, /* tp_richcompare */
3586 0, /* tp_weaklistoffset */
3587 (getiterfunc)dictitems_iter, /* tp_iter */
3588 0, /* tp_iternext */
3589 dictitems_methods, /* tp_methods */
3590 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003591};
3592
3593static PyObject *
3594dictitems_new(PyObject *dict)
3595{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003596 return dictview_new(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003597}
3598
Guido van Rossum3ac67412007-02-10 18:55:06 +00003599/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003600
3601static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003602dictvalues_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003603{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003604 if (dv->dv_dict == NULL) {
3605 Py_RETURN_NONE;
3606 }
3607 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003608}
3609
Guido van Rossum83825ac2007-02-10 04:54:19 +00003610static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003611 (lenfunc)dictview_len, /* sq_length */
3612 0, /* sq_concat */
3613 0, /* sq_repeat */
3614 0, /* sq_item */
3615 0, /* sq_slice */
3616 0, /* sq_ass_item */
3617 0, /* sq_ass_slice */
3618 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003619};
3620
Guido van Rossumb90c8482007-02-10 01:11:45 +00003621static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003622 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003623};
3624
3625PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003626 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3627 "dict_values", /* tp_name */
3628 sizeof(dictviewobject), /* tp_basicsize */
3629 0, /* tp_itemsize */
3630 /* methods */
3631 (destructor)dictview_dealloc, /* tp_dealloc */
3632 0, /* tp_print */
3633 0, /* tp_getattr */
3634 0, /* tp_setattr */
3635 0, /* tp_reserved */
3636 (reprfunc)dictview_repr, /* tp_repr */
3637 0, /* tp_as_number */
3638 &dictvalues_as_sequence, /* tp_as_sequence */
3639 0, /* tp_as_mapping */
3640 0, /* tp_hash */
3641 0, /* tp_call */
3642 0, /* tp_str */
3643 PyObject_GenericGetAttr, /* tp_getattro */
3644 0, /* tp_setattro */
3645 0, /* tp_as_buffer */
3646 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3647 0, /* tp_doc */
3648 (traverseproc)dictview_traverse, /* tp_traverse */
3649 0, /* tp_clear */
3650 0, /* tp_richcompare */
3651 0, /* tp_weaklistoffset */
3652 (getiterfunc)dictvalues_iter, /* tp_iter */
3653 0, /* tp_iternext */
3654 dictvalues_methods, /* tp_methods */
3655 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003656};
3657
3658static PyObject *
3659dictvalues_new(PyObject *dict)
3660{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003661 return dictview_new(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003662}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003663
3664/* Returns NULL if cannot allocate a new PyDictKeysObject,
3665 but does not set an error */
3666PyDictKeysObject *
3667_PyDict_NewKeysForClass(void)
3668{
3669 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE_SPLIT);
3670 if (keys == NULL)
3671 PyErr_Clear();
3672 else
3673 keys->dk_lookup = lookdict_split;
3674 return keys;
3675}
3676
3677#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
3678
3679PyObject *
3680PyObject_GenericGetDict(PyObject *obj, void *context)
3681{
3682 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
3683 if (dictptr == NULL) {
3684 PyErr_SetString(PyExc_AttributeError,
3685 "This object has no __dict__");
3686 return NULL;
3687 }
3688 dict = *dictptr;
3689 if (dict == NULL) {
3690 PyTypeObject *tp = Py_TYPE(obj);
3691 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
3692 DK_INCREF(CACHED_KEYS(tp));
3693 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
3694 }
3695 else {
3696 *dictptr = dict = PyDict_New();
3697 }
3698 }
3699 Py_XINCREF(dict);
3700 return dict;
3701}
3702
3703int
3704_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
3705 PyObject *key, PyObject *value)
3706{
3707 PyObject *dict;
3708 int res;
3709 PyDictKeysObject *cached;
3710
3711 assert(dictptr != NULL);
3712 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
3713 assert(dictptr != NULL);
3714 dict = *dictptr;
3715 if (dict == NULL) {
3716 DK_INCREF(cached);
3717 dict = new_dict_with_shared_keys(cached);
3718 if (dict == NULL)
3719 return -1;
3720 *dictptr = dict;
3721 }
3722 if (value == NULL) {
3723 res = PyDict_DelItem(dict, key);
3724 if (cached != ((PyDictObject *)dict)->ma_keys) {
3725 CACHED_KEYS(tp) = NULL;
3726 DK_DECREF(cached);
3727 }
3728 } else {
3729 res = PyDict_SetItem(dict, key, value);
3730 if (cached != ((PyDictObject *)dict)->ma_keys) {
3731 /* Either update tp->ht_cached_keys or delete it */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04003732 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003733 CACHED_KEYS(tp) = make_keys_shared(dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003734 } else {
3735 CACHED_KEYS(tp) = NULL;
3736 }
3737 DK_DECREF(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04003738 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
3739 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003740 }
3741 }
3742 } else {
3743 dict = *dictptr;
3744 if (dict == NULL) {
3745 dict = PyDict_New();
3746 if (dict == NULL)
3747 return -1;
3748 *dictptr = dict;
3749 }
3750 if (value == NULL) {
3751 res = PyDict_DelItem(dict, key);
3752 } else {
3753 res = PyDict_SetItem(dict, key, value);
3754 }
3755 }
3756 return res;
3757}
3758
3759void
3760_PyDictKeys_DecRef(PyDictKeysObject *keys)
3761{
3762 DK_DECREF(keys);
3763}
3764
3765
3766/* ARGSUSED */
3767static PyObject *
3768dummy_repr(PyObject *op)
3769{
3770 return PyUnicode_FromString("<dummy key>");
3771}
3772
3773/* ARGUSED */
3774static void
3775dummy_dealloc(PyObject* ignore)
3776{
3777 /* This should never get called, but we also don't want to SEGV if
3778 * we accidentally decref dummy-key out of existence.
3779 */
3780 Py_FatalError("deallocating <dummy key>");
3781}
3782
3783static PyTypeObject PyDictDummy_Type = {
3784 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3785 "<dummy key> type",
3786 0,
3787 0,
3788 dummy_dealloc, /*tp_dealloc*/ /*never called*/
3789 0, /*tp_print*/
3790 0, /*tp_getattr*/
3791 0, /*tp_setattr*/
3792 0, /*tp_reserved*/
3793 dummy_repr, /*tp_repr*/
3794 0, /*tp_as_number*/
3795 0, /*tp_as_sequence*/
3796 0, /*tp_as_mapping*/
3797 0, /*tp_hash */
3798 0, /*tp_call */
3799 0, /*tp_str */
3800 0, /*tp_getattro */
3801 0, /*tp_setattro */
3802 0, /*tp_as_buffer */
3803 Py_TPFLAGS_DEFAULT, /*tp_flags */
3804};
3805
3806static PyObject _dummy_struct = {
3807 _PyObject_EXTRA_INIT
3808 2, &PyDictDummy_Type
3809};
3810