blob: 7aa5ea83d38dd0c4e546ae28143ae48d546409df [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
Thomas Wouters89f507f2006-12-13 04:49:30 +000098/* Set a key error with the specified argument, wrapping it in a
99 * tuple automatically so that tuple keys are not unpacked as the
100 * exception arguments. */
101static void
102set_key_error(PyObject *arg)
103{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 PyObject *tup;
105 tup = PyTuple_Pack(1, arg);
106 if (!tup)
107 return; /* caller will expect error to be set anyway */
108 PyErr_SetObject(PyExc_KeyError, tup);
109 Py_DECREF(tup);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000110}
111
Tim Peterseb28ef22001-06-02 05:27:19 +0000112#define PERTURB_SHIFT 5
113
Guido van Rossum16e93a81997-01-28 00:00:11 +0000114/*
Tim Peterseb28ef22001-06-02 05:27:19 +0000115Major subtleties ahead: Most hash schemes depend on having a "good" hash
116function, in the sense of simulating randomness. Python doesn't: its most
117important hash functions (for strings and ints) are very regular in common
118cases:
Tim Peters15d49292001-05-27 07:39:22 +0000119
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000120 >>> map(hash, (0, 1, 2, 3))
121 [0, 1, 2, 3]
122 >>> map(hash, ("namea", "nameb", "namec", "named"))
123 [-1658398457, -1658398460, -1658398459, -1658398462]
124 >>>
Tim Peters15d49292001-05-27 07:39:22 +0000125
Tim Peterseb28ef22001-06-02 05:27:19 +0000126This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
127the low-order i bits as the initial table index is extremely fast, and there
128are no collisions at all for dicts indexed by a contiguous range of ints.
129The same is approximately true when keys are "consecutive" strings. So this
130gives better-than-random behavior in common cases, and that's very desirable.
Tim Peters15d49292001-05-27 07:39:22 +0000131
Tim Peterseb28ef22001-06-02 05:27:19 +0000132OTOH, when collisions occur, the tendency to fill contiguous slices of the
133hash table makes a good collision resolution strategy crucial. Taking only
134the last i bits of the hash code is also vulnerable: for example, consider
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135the list [i << 16 for i in range(20000)] as a set of keys. Since ints are
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000136their own hash codes, and this fits in a dict of size 2**15, the last 15 bits
137 of every hash code are all 0: they *all* map to the same table index.
Tim Peters15d49292001-05-27 07:39:22 +0000138
Tim Peterseb28ef22001-06-02 05:27:19 +0000139But catering to unusual cases should not slow the usual ones, so we just take
140the last i bits anyway. It's up to collision resolution to do the rest. If
141we *usually* find the key we're looking for on the first try (and, it turns
142out, we usually do -- the table load factor is kept under 2/3, so the odds
143are solidly in our favor), then it makes best sense to keep the initial index
144computation dirt cheap.
Tim Peters15d49292001-05-27 07:39:22 +0000145
Tim Peterseb28ef22001-06-02 05:27:19 +0000146The first half of collision resolution is to visit table indices via this
147recurrence:
Tim Peters15d49292001-05-27 07:39:22 +0000148
Tim Peterseb28ef22001-06-02 05:27:19 +0000149 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +0000150
Tim Peterseb28ef22001-06-02 05:27:19 +0000151For any initial j in range(2**i), repeating that 2**i times generates each
152int in range(2**i) exactly once (see any text on random-number generation for
153proof). By itself, this doesn't help much: like linear probing (setting
154j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
155order. This would be bad, except that's not the only thing we do, and it's
156actually *good* in the common cases where hash keys are consecutive. In an
157example that's really too small to make this entirely clear, for a table of
158size 2**3 the order of indices is:
Tim Peters15d49292001-05-27 07:39:22 +0000159
Tim Peterseb28ef22001-06-02 05:27:19 +0000160 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
161
162If two things come in at index 5, the first place we look after is index 2,
163not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
164Linear probing is deadly in this case because there the fixed probe order
165is the *same* as the order consecutive keys are likely to arrive. But it's
166extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
167and certain that consecutive hash codes do not.
168
169The other half of the strategy is to get the other bits of the hash code
170into play. This is done by initializing a (unsigned) vrbl "perturb" to the
171full hash code, and changing the recurrence to:
172
173 j = (5*j) + 1 + perturb;
174 perturb >>= PERTURB_SHIFT;
175 use j % 2**i as the next table index;
176
177Now the probe sequence depends (eventually) on every bit in the hash code,
178and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
179because it quickly magnifies small differences in the bits that didn't affect
180the initial index. Note that because perturb is unsigned, if the recurrence
181is executed often enough perturb eventually becomes and remains 0. At that
182point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
183that's certain to find an empty slot eventually (since it generates every int
184in range(2**i), and we make sure there's always at least one empty slot).
185
186Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
187small so that the high bits of the hash code continue to affect the probe
188sequence across iterations; but you want it large so that in really bad cases
189the high-order hash bits have an effect on early iterations. 5 was "the
190best" in minimizing total collisions across experiments Tim Peters ran (on
191both normal and pathological cases), but 4 and 6 weren't significantly worse.
192
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000193Historical: Reimer Behrends contributed the idea of using a polynomial-based
Tim Peterseb28ef22001-06-02 05:27:19 +0000194approach, using repeated multiplication by x in GF(2**n) where an irreducible
195polynomial for each table size was chosen such that x was a primitive root.
196Christian Tismer later extended that to use division by x instead, as an
197efficient way to get the high bits of the hash code into play. This scheme
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000198also gave excellent collision statistics, but was more expensive: two
199if-tests were required inside the loop; computing "the next" index took about
200the same number of operations but without as much potential parallelism
201(e.g., computing 5*j can go on at the same time as computing 1+perturb in the
202above, and then shifting perturb can be done while the table index is being
203masked); and the PyDictObject struct required a member to hold the table's
204polynomial. In Tim's experiments the current scheme ran faster, produced
205equally good collision statistics, needed less code & used less memory.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000206
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000207*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000208
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400209/* Object used as dummy key to fill deleted entries
210 * This could be any unique object,
211 * use a custom type in order to minimise coupling.
212*/
213static PyObject _dummy_struct;
214
215#define dummy (&_dummy_struct)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000216
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000217#ifdef Py_REF_DEBUG
218PyObject *
219_PyDict_Dummy(void)
220{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 return dummy;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000222}
223#endif
224
Fred Drake1bff34a2000-08-31 19:31:38 +0000225/* forward declarations */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400226static PyDictKeyEntry *lookdict(PyDictObject *mp, PyObject *key,
227 Py_hash_t hash, PyObject ***value_addr);
228static PyDictKeyEntry *lookdict_unicode(PyDictObject *mp, PyObject *key,
229 Py_hash_t hash, PyObject ***value_addr);
230static PyDictKeyEntry *
231lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
232 Py_hash_t hash, PyObject ***value_addr);
233static PyDictKeyEntry *lookdict_split(PyDictObject *mp, PyObject *key,
234 Py_hash_t hash, PyObject ***value_addr);
Fred Drake1bff34a2000-08-31 19:31:38 +0000235
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400236static int dictresize(PyDictObject *mp, Py_ssize_t minused);
Tim Petersdea48ec2001-05-22 20:40:22 +0000237
Raymond Hettinger43442782004-03-17 21:55:03 +0000238/* Dictionary reuse scheme to save calls to malloc, free, and memset */
Christian Heimes2202f872008-02-06 14:31:34 +0000239#ifndef PyDict_MAXFREELIST
240#define PyDict_MAXFREELIST 80
241#endif
242static PyDictObject *free_list[PyDict_MAXFREELIST];
243static int numfree = 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000244
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100245int
246PyDict_ClearFreeList(void)
Christian Heimes77c02eb2008-02-09 02:18:51 +0000247{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 PyDictObject *op;
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100249 int ret = numfree;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 while (numfree) {
251 op = free_list[--numfree];
252 assert(PyDict_CheckExact(op));
253 PyObject_GC_Del(op);
254 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100255 return ret;
256}
257
David Malcolm49526f42012-06-22 14:55:41 -0400258/* Print summary info about the state of the optimized allocator */
259void
260_PyDict_DebugMallocStats(FILE *out)
261{
262 _PyDebugAllocatorStats(out,
263 "free PyDictObject", numfree, sizeof(PyDictObject));
264}
265
266
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100267void
268PyDict_Fini(void)
269{
270 PyDict_ClearFreeList();
Christian Heimes77c02eb2008-02-09 02:18:51 +0000271}
272
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200273#define DK_DEBUG_INCREF _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA
274#define DK_DEBUG_DECREF _Py_DEC_REFTOTAL _Py_REF_DEBUG_COMMA
275
276#define DK_INCREF(dk) (DK_DEBUG_INCREF ++(dk)->dk_refcnt)
277#define DK_DECREF(dk) if (DK_DEBUG_DECREF (--(dk)->dk_refcnt) == 0) free_keys_object(dk)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400278#define DK_SIZE(dk) ((dk)->dk_size)
279#define DK_MASK(dk) (((dk)->dk_size)-1)
280#define IS_POWER_OF_2(x) (((x) & (x-1)) == 0)
281
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200282/* USABLE_FRACTION is the maximum dictionary load.
283 * Currently set to (2n+1)/3. Increasing this ratio makes dictionaries more
284 * dense resulting in more collisions. Decreasing it improves sparseness
285 * at the expense of spreading entries over more cache lines and at the
286 * cost of total memory consumed.
287 *
288 * USABLE_FRACTION must obey the following:
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400289 * (0 < USABLE_FRACTION(n) < n) for all n >= 2
290 *
291 * USABLE_FRACTION should be very quick to calculate.
292 * Fractions around 5/8 to 2/3 seem to work well in practice.
293 */
294
295/* Use (2n+1)/3 rather than 2n+3 because: it makes no difference for
296 * combined tables (the two fractions round to the same number n < ),
297 * but 2*4/3 is 2 whereas (2*4+1)/3 is 3 which potentially saves quite
298 * a lot of space for small, split tables */
299#define USABLE_FRACTION(n) ((((n) << 1)+1)/3)
300
301/* Alternative fraction that is otherwise close enough to (2n+1)/3 to make
302 * little difference. 8 * 2/3 == 8 * 5/8 == 5. 16 * 2/3 == 16 * 5/8 == 10.
303 * 32 * 2/3 = 21, 32 * 5/8 = 20.
304 * Its advantage is that it is faster to compute on machines with slow division.
305 * #define USABLE_FRACTION(n) (((n) >> 1) + ((n) >> 2) - ((n) >> 3))
306*/
307
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700308/* GROWTH_RATE. Growth rate upon hitting maximum load.
309 * Currently set to used*2 + capacity/2.
310 * This means that dicts double in size when growing without deletions,
311 * but have more head room when the number of deletions is on a par with the
312 * number of insertions.
313 * Raising this to used*4 doubles memory consumption depending on the size of
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200314 * the dictionary, but results in half the number of resizes, less effort to
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700315 * resize.
316 * GROWTH_RATE was set to used*4 up to version 3.2.
317 * GROWTH_RATE was set to used*2 in version 3.3.0
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200318 */
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700319#define GROWTH_RATE(d) (((d)->ma_used*2)+((d)->ma_keys->dk_size>>1))
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400320
321#define ENSURE_ALLOWS_DELETIONS(d) \
322 if ((d)->ma_keys->dk_lookup == lookdict_unicode_nodummy) { \
323 (d)->ma_keys->dk_lookup = lookdict_unicode; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400325
326/* This immutable, empty PyDictKeysObject is used for PyDict_Clear()
327 * (which cannot fail and thus can do no allocation).
328 */
329static PyDictKeysObject empty_keys_struct = {
330 2, /* dk_refcnt 1 for this struct, 1 for dummy_struct */
331 1, /* dk_size */
332 lookdict_split, /* dk_lookup */
333 0, /* dk_usable (immutable) */
334 {
335 { 0, 0, 0 } /* dk_entries (empty) */
336 }
337};
338
339static PyObject *empty_values[1] = { NULL };
340
341#define Py_EMPTY_KEYS &empty_keys_struct
342
343static PyDictKeysObject *new_keys_object(Py_ssize_t size)
344{
345 PyDictKeysObject *dk;
346 Py_ssize_t i;
347 PyDictKeyEntry *ep0;
348
349 assert(size >= PyDict_MINSIZE_SPLIT);
350 assert(IS_POWER_OF_2(size));
351 dk = PyMem_MALLOC(sizeof(PyDictKeysObject) +
352 sizeof(PyDictKeyEntry) * (size-1));
353 if (dk == NULL) {
354 PyErr_NoMemory();
355 return NULL;
356 }
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200357 DK_DEBUG_INCREF dk->dk_refcnt = 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400358 dk->dk_size = size;
359 dk->dk_usable = USABLE_FRACTION(size);
360 ep0 = &dk->dk_entries[0];
361 /* Hash value of slot 0 is used by popitem, so it must be initialized */
362 ep0->me_hash = 0;
363 for (i = 0; i < size; i++) {
364 ep0[i].me_key = NULL;
365 ep0[i].me_value = NULL;
366 }
367 dk->dk_lookup = lookdict_unicode_nodummy;
368 return dk;
369}
370
371static void
372free_keys_object(PyDictKeysObject *keys)
373{
374 PyDictKeyEntry *entries = &keys->dk_entries[0];
375 Py_ssize_t i, n;
376 for (i = 0, n = DK_SIZE(keys); i < n; i++) {
377 Py_XDECREF(entries[i].me_key);
378 Py_XDECREF(entries[i].me_value);
379 }
380 PyMem_FREE(keys);
381}
382
383#define new_values(size) PyMem_NEW(PyObject *, size)
384
385#define free_values(values) PyMem_FREE(values)
386
387/* Consumes a reference to the keys object */
388static PyObject *
389new_dict(PyDictKeysObject *keys, PyObject **values)
390{
391 PyDictObject *mp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 if (numfree) {
393 mp = free_list[--numfree];
394 assert (mp != NULL);
395 assert (Py_TYPE(mp) == &PyDict_Type);
396 _Py_NewReference((PyObject *)mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400398 else {
399 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
400 if (mp == NULL) {
401 DK_DECREF(keys);
402 free_values(values);
403 return NULL;
404 }
405 }
406 mp->ma_keys = keys;
407 mp->ma_values = values;
408 mp->ma_used = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000410}
411
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400412/* Consumes a reference to the keys object */
413static PyObject *
414new_dict_with_shared_keys(PyDictKeysObject *keys)
415{
416 PyObject **values;
417 Py_ssize_t i, size;
418
419 size = DK_SIZE(keys);
420 values = new_values(size);
421 if (values == NULL) {
422 DK_DECREF(keys);
423 return PyErr_NoMemory();
424 }
425 for (i = 0; i < size; i++) {
426 values[i] = NULL;
427 }
428 return new_dict(keys, values);
429}
430
431PyObject *
432PyDict_New(void)
433{
434 return new_dict(new_keys_object(PyDict_MINSIZE_COMBINED), NULL);
435}
436
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000437/*
438The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000439This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000440Open addressing is preferred over chaining since the link overhead for
441chaining would be substantial (100% with typical malloc overhead).
442
Tim Peterseb28ef22001-06-02 05:27:19 +0000443The initial probe index is computed as hash mod the table size. Subsequent
444probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000445
446All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000447
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000448The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000449contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000450Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000451
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000452lookdict() is general-purpose, and may return NULL if (and only if) a
453comparison raises an exception (this was new in Python 2.5).
Guido van Rossum89d8c602007-09-18 17:26:56 +0000454lookdict_unicode() below is specialized to string keys, comparison of which can
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400455never raise an exception; that function can never return NULL.
456lookdict_unicode_nodummy is further specialized for string keys that cannot be
457the <dummy> value.
458For both, when the key isn't found a PyDictEntry* is returned
459where the key would have been found, *value_addr points to the matching value
460slot.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000461*/
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400462static PyDictKeyEntry *
463lookdict(PyDictObject *mp, PyObject *key,
464 Py_hash_t hash, PyObject ***value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000465{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 register size_t i;
467 register size_t perturb;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400468 register PyDictKeyEntry *freeslot;
Antoine Pitrou9a234902012-05-13 20:48:01 +0200469 register size_t mask;
470 PyDictKeyEntry *ep0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400471 register PyDictKeyEntry *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 register int cmp;
473 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000474
Antoine Pitrou9a234902012-05-13 20:48:01 +0200475top:
476 mask = DK_MASK(mp->ma_keys);
477 ep0 = &mp->ma_keys->dk_entries[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 i = (size_t)hash & mask;
479 ep = &ep0[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400480 if (ep->me_key == NULL || ep->me_key == key) {
481 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400483 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 if (ep->me_key == dummy)
485 freeslot = ep;
486 else {
487 if (ep->me_hash == hash) {
488 startkey = ep->me_key;
489 Py_INCREF(startkey);
490 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
491 Py_DECREF(startkey);
492 if (cmp < 0)
493 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400494 if (ep0 == mp->ma_keys->dk_entries && ep->me_key == startkey) {
495 if (cmp > 0) {
496 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400498 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 }
500 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200501 /* The dict was mutated, restart */
502 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 }
504 }
505 freeslot = NULL;
506 }
Tim Peters15d49292001-05-27 07:39:22 +0000507
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 /* In the loop, me_key == dummy is by far (factor of 100s) the
509 least likely outcome, so test for that last. */
510 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
511 i = (i << 2) + i + perturb + 1;
512 ep = &ep0[i & mask];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400513 if (ep->me_key == NULL) {
514 if (freeslot == NULL) {
515 *value_addr = &ep->me_value;
516 return ep;
517 } else {
518 *value_addr = &freeslot->me_value;
519 return freeslot;
520 }
521 }
522 if (ep->me_key == key) {
523 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400525 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 if (ep->me_hash == hash && ep->me_key != dummy) {
527 startkey = ep->me_key;
528 Py_INCREF(startkey);
529 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
530 Py_DECREF(startkey);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400531 if (cmp < 0) {
532 *value_addr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400534 }
535 if (ep0 == mp->ma_keys->dk_entries && ep->me_key == startkey) {
536 if (cmp > 0) {
537 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400539 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 }
541 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200542 /* The dict was mutated, restart */
543 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 }
545 }
546 else if (ep->me_key == dummy && freeslot == NULL)
547 freeslot = ep;
548 }
549 assert(0); /* NOT REACHED */
550 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000551}
552
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400553/* Specialized version for string-only keys */
554static PyDictKeyEntry *
555lookdict_unicode(PyDictObject *mp, PyObject *key,
556 Py_hash_t hash, PyObject ***value_addr)
Fred Drake1bff34a2000-08-31 19:31:38 +0000557{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 register size_t i;
559 register size_t perturb;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400560 register PyDictKeyEntry *freeslot;
561 register size_t mask = DK_MASK(mp->ma_keys);
562 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
563 register PyDictKeyEntry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 /* Make sure this function doesn't have to handle non-unicode keys,
566 including subclasses of str; e.g., one reason to subclass
567 unicodes is to override __eq__, and for speed we don't cater to
568 that here. */
569 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400570 mp->ma_keys->dk_lookup = lookdict;
571 return lookdict(mp, key, hash, value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 }
Mark Dickinson57e683e2011-09-24 18:18:40 +0100573 i = (size_t)hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 ep = &ep0[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400575 if (ep->me_key == NULL || ep->me_key == key) {
576 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400578 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 if (ep->me_key == dummy)
580 freeslot = ep;
581 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400582 if (ep->me_hash == hash && unicode_eq(ep->me_key, key)) {
583 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400585 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 freeslot = NULL;
587 }
Tim Peters15d49292001-05-27 07:39:22 +0000588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 /* In the loop, me_key == dummy is by far (factor of 100s) the
590 least likely outcome, so test for that last. */
591 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
592 i = (i << 2) + i + perturb + 1;
593 ep = &ep0[i & mask];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400594 if (ep->me_key == NULL) {
595 if (freeslot == NULL) {
596 *value_addr = &ep->me_value;
597 return ep;
598 } else {
599 *value_addr = &freeslot->me_value;
600 return freeslot;
601 }
602 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 if (ep->me_key == key
604 || (ep->me_hash == hash
605 && ep->me_key != dummy
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400606 && unicode_eq(ep->me_key, key))) {
607 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400609 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 if (ep->me_key == dummy && freeslot == NULL)
611 freeslot = ep;
612 }
613 assert(0); /* NOT REACHED */
614 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000615}
616
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400617/* Faster version of lookdict_unicode when it is known that no <dummy> keys
618 * will be present. */
619static PyDictKeyEntry *
620lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
621 Py_hash_t hash, PyObject ***value_addr)
622{
623 register size_t i;
624 register size_t perturb;
625 register size_t mask = DK_MASK(mp->ma_keys);
626 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
627 register PyDictKeyEntry *ep;
628
629 /* Make sure this function doesn't have to handle non-unicode keys,
630 including subclasses of str; e.g., one reason to subclass
631 unicodes is to override __eq__, and for speed we don't cater to
632 that here. */
633 if (!PyUnicode_CheckExact(key)) {
634 mp->ma_keys->dk_lookup = lookdict;
635 return lookdict(mp, key, hash, value_addr);
636 }
637 i = (size_t)hash & mask;
638 ep = &ep0[i];
639 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
640 if (ep->me_key == NULL || ep->me_key == key ||
641 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
642 *value_addr = &ep->me_value;
643 return ep;
644 }
645 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
646 i = (i << 2) + i + perturb + 1;
647 ep = &ep0[i & mask];
648 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
649 if (ep->me_key == NULL || ep->me_key == key ||
650 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
651 *value_addr = &ep->me_value;
652 return ep;
653 }
654 }
655 assert(0); /* NOT REACHED */
656 return 0;
657}
658
659/* Version of lookdict for split tables.
660 * All split tables and only split tables use this lookup function.
661 * Split tables only contain unicode keys and no dummy keys,
662 * so algorithm is the same as lookdict_unicode_nodummy.
663 */
664static PyDictKeyEntry *
665lookdict_split(PyDictObject *mp, PyObject *key,
666 Py_hash_t hash, PyObject ***value_addr)
667{
668 register size_t i;
669 register size_t perturb;
670 register size_t mask = DK_MASK(mp->ma_keys);
671 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
672 register PyDictKeyEntry *ep;
673
674 if (!PyUnicode_CheckExact(key)) {
Benjamin Petersondb780d02012-04-23 13:44:32 -0400675 ep = lookdict(mp, key, hash, value_addr);
676 /* lookdict expects a combined-table, so fix value_addr */
677 i = ep - ep0;
678 *value_addr = &mp->ma_values[i];
679 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400680 }
681 i = (size_t)hash & mask;
682 ep = &ep0[i];
683 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
684 if (ep->me_key == NULL || ep->me_key == key ||
685 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
686 *value_addr = &mp->ma_values[i];
687 return ep;
688 }
689 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
690 i = (i << 2) + i + perturb + 1;
691 ep = &ep0[i & mask];
692 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
693 if (ep->me_key == NULL || ep->me_key == key ||
694 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
695 *value_addr = &mp->ma_values[i & mask];
696 return ep;
697 }
698 }
699 assert(0); /* NOT REACHED */
700 return 0;
701}
702
Benjamin Petersonfb886362010-04-24 18:21:17 +0000703int
704_PyDict_HasOnlyStringKeys(PyObject *dict)
705{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 Py_ssize_t pos = 0;
707 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000708 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400710 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 return 1;
712 while (PyDict_Next(dict, &pos, &key, &value))
713 if (!PyUnicode_Check(key))
714 return 0;
715 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000716}
717
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000718#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 do { \
720 if (!_PyObject_GC_IS_TRACKED(mp)) { \
721 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
722 _PyObject_GC_MAY_BE_TRACKED(value)) { \
723 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 } \
725 } \
726 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000727
728void
729_PyDict_MaybeUntrack(PyObject *op)
730{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 PyDictObject *mp;
732 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400733 Py_ssize_t i, size;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000734
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
736 return;
737
738 mp = (PyDictObject *) op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400739 size = DK_SIZE(mp->ma_keys);
740 if (_PyDict_HasSplitTable(mp)) {
741 for (i = 0; i < size; i++) {
742 if ((value = mp->ma_values[i]) == NULL)
743 continue;
744 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
745 assert(!_PyObject_GC_MAY_BE_TRACKED(
746 mp->ma_keys->dk_entries[i].me_key));
747 return;
748 }
749 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400751 else {
752 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
753 for (i = 0; i < size; i++) {
754 if ((value = ep0[i].me_value) == NULL)
755 continue;
756 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
757 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
758 return;
759 }
760 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000762}
763
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400764/* Internal function to find slot for an item from its hash
765 * when it is known that the key is not present in the dict.
766 */
767static PyDictKeyEntry *
768find_empty_slot(PyDictObject *mp, PyObject *key, Py_hash_t hash,
769 PyObject ***value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000770{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400771 size_t i;
772 size_t perturb;
773 size_t mask = DK_MASK(mp->ma_keys);
774 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
775 PyDictKeyEntry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000776
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400777 assert(key != NULL);
778 if (!PyUnicode_CheckExact(key))
779 mp->ma_keys->dk_lookup = lookdict;
780 i = hash & mask;
781 ep = &ep0[i];
782 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
783 i = (i << 2) + i + perturb + 1;
784 ep = &ep0[i & mask];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400786 assert(ep->me_value == NULL);
787 if (mp->ma_values)
788 *value_addr = &mp->ma_values[i & mask];
789 else
790 *value_addr = &ep->me_value;
791 return ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000792}
793
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400794static int
795insertion_resize(PyDictObject *mp)
796{
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700797 return dictresize(mp, GROWTH_RATE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400798}
Antoine Pitroue965d972012-02-27 00:45:12 +0100799
800/*
801Internal routine to insert a new item into the table.
802Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +0100803Returns -1 if an error occurred, or 0 on success.
804*/
805static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400806insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +0100807{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400808 PyObject *old_value;
809 PyObject **value_addr;
810 PyDictKeyEntry *ep;
811 assert(key != dummy);
Antoine Pitroue965d972012-02-27 00:45:12 +0100812
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400813 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
814 if (insertion_resize(mp) < 0)
815 return -1;
816 }
817
818 ep = mp->ma_keys->dk_lookup(mp, key, hash, &value_addr);
Antoine Pitroue965d972012-02-27 00:45:12 +0100819 if (ep == NULL) {
Antoine Pitroue965d972012-02-27 00:45:12 +0100820 return -1;
821 }
Benjamin Petersona6f195e2012-04-30 10:23:40 -0400822 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400823 MAINTAIN_TRACKING(mp, key, value);
824 old_value = *value_addr;
825 if (old_value != NULL) {
826 assert(ep->me_key != NULL && ep->me_key != dummy);
827 *value_addr = value;
828 Py_DECREF(old_value); /* which **CAN** re-enter */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400829 }
830 else {
831 if (ep->me_key == NULL) {
Benjamin Petersona6f195e2012-04-30 10:23:40 -0400832 Py_INCREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400833 if (mp->ma_keys->dk_usable <= 0) {
834 /* Need to resize. */
835 if (insertion_resize(mp) < 0) {
836 Py_DECREF(key);
837 Py_DECREF(value);
838 return -1;
839 }
840 ep = find_empty_slot(mp, key, hash, &value_addr);
841 }
842 mp->ma_keys->dk_usable--;
843 assert(mp->ma_keys->dk_usable >= 0);
844 ep->me_key = key;
845 ep->me_hash = hash;
846 }
847 else {
848 if (ep->me_key == dummy) {
Benjamin Petersona6f195e2012-04-30 10:23:40 -0400849 Py_INCREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400850 ep->me_key = key;
851 ep->me_hash = hash;
852 Py_DECREF(dummy);
853 } else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400854 assert(_PyDict_HasSplitTable(mp));
855 }
856 }
857 mp->ma_used++;
858 *value_addr = value;
859 }
860 assert(ep->me_key != NULL && ep->me_key != dummy);
861 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
862 return 0;
Antoine Pitroue965d972012-02-27 00:45:12 +0100863}
864
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000865/*
866Internal routine used by dictresize() to insert an item which is
867known to be absent from the dict. This routine also assumes that
868the dict contains no deleted entries. Besides the performance benefit,
869using insertdict() in dictresize() is dangerous (SF bug #1456209).
870Note that no refcounts are changed by this routine; if needed, the caller
871is responsible for incref'ing `key` and `value`.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400872Neither mp->ma_used nor k->dk_usable are modified by this routine; the caller
873must set them correctly
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000874*/
875static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400876insertdict_clean(PyDictObject *mp, PyObject *key, Py_hash_t hash,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 PyObject *value)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000878{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400879 size_t i;
880 size_t perturb;
881 PyDictKeysObject *k = mp->ma_keys;
882 size_t mask = (size_t)DK_SIZE(k)-1;
883 PyDictKeyEntry *ep0 = &k->dk_entries[0];
884 PyDictKeyEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000885
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400886 assert(k->dk_lookup != NULL);
887 assert(value != NULL);
888 assert(key != NULL);
889 assert(key != dummy);
890 assert(PyUnicode_CheckExact(key) || k->dk_lookup == lookdict);
891 i = hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 ep = &ep0[i];
893 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
894 i = (i << 2) + i + perturb + 1;
895 ep = &ep0[i & mask];
896 }
897 assert(ep->me_value == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 ep->me_key = key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000899 ep->me_hash = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000901}
902
903/*
904Restructure the table by allocating a new table and reinserting all
905items again. When entries have been deleted, the new table may
906actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400907If a table is split (its keys and hashes are shared, its values are not),
908then the values are temporarily copied into the table, it is resized as
909a combined table, then the me_value slots in the old table are NULLed out.
910After resizing a table is always combined,
911but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000912*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000913static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000914dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000915{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 Py_ssize_t newsize;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400917 PyDictKeysObject *oldkeys;
918 PyObject **oldvalues;
919 Py_ssize_t i, oldsize;
Tim Peters91a364d2001-05-19 07:04:38 +0000920
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400921/* Find the smallest table size > minused. */
922 for (newsize = PyDict_MINSIZE_COMBINED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 newsize <= minused && newsize > 0;
924 newsize <<= 1)
925 ;
926 if (newsize <= 0) {
927 PyErr_NoMemory();
928 return -1;
929 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400930 oldkeys = mp->ma_keys;
931 oldvalues = mp->ma_values;
932 /* Allocate a new table. */
933 mp->ma_keys = new_keys_object(newsize);
934 if (mp->ma_keys == NULL) {
935 mp->ma_keys = oldkeys;
936 return -1;
937 }
938 if (oldkeys->dk_lookup == lookdict)
939 mp->ma_keys->dk_lookup = lookdict;
940 oldsize = DK_SIZE(oldkeys);
941 mp->ma_values = NULL;
942 /* If empty then nothing to copy so just return */
943 if (oldsize == 1) {
944 assert(oldkeys == Py_EMPTY_KEYS);
945 DK_DECREF(oldkeys);
946 return 0;
947 }
948 /* Main loop below assumes we can transfer refcount to new keys
949 * and that value is stored in me_value.
950 * Increment ref-counts and copy values here to compensate
951 * This (resizing a split table) should be relatively rare */
952 if (oldvalues != NULL) {
953 for (i = 0; i < oldsize; i++) {
954 if (oldvalues[i] != NULL) {
955 Py_INCREF(oldkeys->dk_entries[i].me_key);
956 oldkeys->dk_entries[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 }
959 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400960 /* Main loop */
961 for (i = 0; i < oldsize; i++) {
962 PyDictKeyEntry *ep = &oldkeys->dk_entries[i];
963 if (ep->me_value != NULL) {
964 assert(ep->me_key != dummy);
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000965 insertdict_clean(mp, ep->me_key, ep->me_hash, ep->me_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400968 mp->ma_keys->dk_usable -= mp->ma_used;
969 if (oldvalues != NULL) {
970 /* NULL out me_value slot in oldkeys, in case it was shared */
971 for (i = 0; i < oldsize; i++)
972 oldkeys->dk_entries[i].me_value = NULL;
973 assert(oldvalues != empty_values);
974 free_values(oldvalues);
975 DK_DECREF(oldkeys);
976 }
977 else {
978 assert(oldkeys->dk_lookup != lookdict_split);
979 if (oldkeys->dk_lookup != lookdict_unicode_nodummy) {
980 PyDictKeyEntry *ep0 = &oldkeys->dk_entries[0];
981 for (i = 0; i < oldsize; i++) {
982 if (ep0[i].me_key == dummy)
983 Py_DECREF(dummy);
984 }
985 }
986 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200987 DK_DEBUG_DECREF PyMem_FREE(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400988 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000990}
991
Benjamin Peterson15ee8212012-04-24 14:44:18 -0400992/* Returns NULL if unable to split table.
993 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400994static PyDictKeysObject *
995make_keys_shared(PyObject *op)
996{
997 Py_ssize_t i;
998 Py_ssize_t size;
999 PyDictObject *mp = (PyDictObject *)op;
1000
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001001 if (!PyDict_CheckExact(op))
1002 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001003 if (!_PyDict_HasSplitTable(mp)) {
1004 PyDictKeyEntry *ep0;
1005 PyObject **values;
1006 assert(mp->ma_keys->dk_refcnt == 1);
1007 if (mp->ma_keys->dk_lookup == lookdict) {
1008 return NULL;
1009 }
1010 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
1011 /* Remove dummy keys */
1012 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
1013 return NULL;
1014 }
1015 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
1016 /* Copy values into a new array */
1017 ep0 = &mp->ma_keys->dk_entries[0];
1018 size = DK_SIZE(mp->ma_keys);
1019 values = new_values(size);
1020 if (values == NULL) {
1021 PyErr_SetString(PyExc_MemoryError,
1022 "Not enough memory to allocate new values array");
1023 return NULL;
1024 }
1025 for (i = 0; i < size; i++) {
1026 values[i] = ep0[i].me_value;
1027 ep0[i].me_value = NULL;
1028 }
1029 mp->ma_keys->dk_lookup = lookdict_split;
1030 mp->ma_values = values;
1031 }
1032 DK_INCREF(mp->ma_keys);
1033 return mp->ma_keys;
1034}
Christian Heimes99170a52007-12-19 02:07:34 +00001035
1036PyObject *
1037_PyDict_NewPresized(Py_ssize_t minused)
1038{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001039 Py_ssize_t newsize;
1040 PyDictKeysObject *new_keys;
1041 for (newsize = PyDict_MINSIZE_COMBINED;
1042 newsize <= minused && newsize > 0;
1043 newsize <<= 1)
1044 ;
1045 new_keys = new_keys_object(newsize);
1046 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001048 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001049}
1050
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001051/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1052 * that may occur (originally dicts supported only string keys, and exceptions
1053 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001054 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001055 * (suppressed) occurred while computing the key's hash, or that some error
1056 * (suppressed) occurred when comparing keys in the dict's internal probe
1057 * sequence. A nasty example of the latter is when a Python-coded comparison
1058 * function hits a stack-depth error, which can cause this to return NULL
1059 * even if the key is present.
1060 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001061PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001062PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001063{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001064 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001066 PyDictKeyEntry *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 PyThreadState *tstate;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001068 PyObject **value_addr;
1069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 if (!PyDict_Check(op))
1071 return NULL;
1072 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001073 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 {
1075 hash = PyObject_Hash(key);
1076 if (hash == -1) {
1077 PyErr_Clear();
1078 return NULL;
1079 }
1080 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001081
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 /* We can arrive here with a NULL tstate during initialization: try
1083 running "python -Wi" for an example related to string interning.
1084 Let's just hope that no exception occurs then... This must be
1085 _PyThreadState_Current and not PyThreadState_GET() because in debug
1086 mode, the latter complains if tstate is NULL. */
1087 tstate = (PyThreadState*)_Py_atomic_load_relaxed(
1088 &_PyThreadState_Current);
1089 if (tstate != NULL && tstate->curexc_type != NULL) {
1090 /* preserve the existing exception */
1091 PyObject *err_type, *err_value, *err_tb;
1092 PyErr_Fetch(&err_type, &err_value, &err_tb);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001093 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 /* ignore errors */
1095 PyErr_Restore(err_type, err_value, err_tb);
1096 if (ep == NULL)
1097 return NULL;
1098 }
1099 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001100 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 if (ep == NULL) {
1102 PyErr_Clear();
1103 return NULL;
1104 }
1105 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001106 return *value_addr;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001107}
1108
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001109/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1110 This returns NULL *with* an exception set if an exception occurred.
1111 It returns NULL *without* an exception set if the key wasn't present.
1112*/
1113PyObject *
1114PyDict_GetItemWithError(PyObject *op, PyObject *key)
1115{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001116 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 PyDictObject*mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001118 PyDictKeyEntry *ep;
1119 PyObject **value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 if (!PyDict_Check(op)) {
1122 PyErr_BadInternalCall();
1123 return NULL;
1124 }
1125 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001126 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 {
1128 hash = PyObject_Hash(key);
1129 if (hash == -1) {
1130 return NULL;
1131 }
1132 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001133
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001134 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 if (ep == NULL)
1136 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001137 return *value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001138}
1139
Brett Cannonfd074152012-04-14 14:10:13 -04001140PyObject *
1141_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1142{
1143 PyObject *kv;
1144 kv = _PyUnicode_FromId(key); /* borrowed */
1145 if (kv == NULL)
1146 return NULL;
1147 return PyDict_GetItemWithError(dp, kv);
1148}
1149
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001150/* Fast version of global value lookup.
1151 * Lookup in globals, then builtins.
1152 */
1153PyObject *
1154_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001155{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001156 PyObject *x;
1157 if (PyUnicode_CheckExact(key)) {
1158 PyObject **value_addr;
1159 Py_hash_t hash = ((PyASCIIObject *)key)->hash;
1160 if (hash != -1) {
1161 PyDictKeyEntry *e;
1162 e = globals->ma_keys->dk_lookup(globals, key, hash, &value_addr);
1163 if (e == NULL) {
1164 return NULL;
1165 }
1166 x = *value_addr;
1167 if (x != NULL)
1168 return x;
1169 e = builtins->ma_keys->dk_lookup(builtins, key, hash, &value_addr);
1170 if (e == NULL) {
1171 return NULL;
1172 }
1173 x = *value_addr;
1174 return x;
1175 }
Antoine Pitroue965d972012-02-27 00:45:12 +01001176 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001177 x = PyDict_GetItemWithError((PyObject *)globals, key);
1178 if (x != NULL)
1179 return x;
1180 if (PyErr_Occurred())
1181 return NULL;
1182 return PyDict_GetItemWithError((PyObject *)builtins, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001183}
1184
Antoine Pitroue965d972012-02-27 00:45:12 +01001185/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1186 * dictionary if it's merely replacing the value for an existing key.
1187 * This means that it's safe to loop over a dictionary with PyDict_Next()
1188 * and occasionally replace a value -- but you can't insert new keys or
1189 * remove them.
1190 */
1191int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001192PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001193{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001194 PyDictObject *mp;
1195 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001196 if (!PyDict_Check(op)) {
1197 PyErr_BadInternalCall();
1198 return -1;
1199 }
1200 assert(key);
1201 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001202 mp = (PyDictObject *)op;
1203 if (!PyUnicode_CheckExact(key) ||
1204 (hash = ((PyASCIIObject *) key)->hash) == -1)
1205 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001206 hash = PyObject_Hash(key);
1207 if (hash == -1)
1208 return -1;
1209 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001210
1211 /* insertdict() handles any resizing that might be necessary */
1212 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001213}
1214
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001215int
Tim Peters1f5871e2000-07-04 17:44:48 +00001216PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001217{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001218 PyDictObject *mp;
1219 Py_hash_t hash;
1220 PyDictKeyEntry *ep;
1221 PyObject *old_key, *old_value;
1222 PyObject **value_addr;
Guido van Rossumd7047b31995-01-02 19:07:15 +00001223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 if (!PyDict_Check(op)) {
1225 PyErr_BadInternalCall();
1226 return -1;
1227 }
1228 assert(key);
1229 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001230 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 hash = PyObject_Hash(key);
1232 if (hash == -1)
1233 return -1;
1234 }
1235 mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001236 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 if (ep == NULL)
1238 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001239 if (*value_addr == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 set_key_error(key);
1241 return -1;
1242 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001243 old_value = *value_addr;
1244 *value_addr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001246 if (!_PyDict_HasSplitTable(mp)) {
1247 ENSURE_ALLOWS_DELETIONS(mp);
1248 old_key = ep->me_key;
1249 Py_INCREF(dummy);
1250 ep->me_key = dummy;
1251 Py_DECREF(old_key);
1252 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 Py_DECREF(old_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001255}
1256
Guido van Rossum25831651993-05-19 14:50:45 +00001257void
Tim Peters1f5871e2000-07-04 17:44:48 +00001258PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001259{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001261 PyDictKeysObject *oldkeys;
1262 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 if (!PyDict_Check(op))
1266 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001267 mp = ((PyDictObject *)op);
1268 oldkeys = mp->ma_keys;
1269 oldvalues = mp->ma_values;
1270 if (oldvalues == empty_values)
1271 return;
1272 /* Empty the dict... */
1273 DK_INCREF(Py_EMPTY_KEYS);
1274 mp->ma_keys = Py_EMPTY_KEYS;
1275 mp->ma_values = empty_values;
1276 mp->ma_used = 0;
1277 /* ...then clear the keys and values */
1278 if (oldvalues != NULL) {
1279 n = DK_SIZE(oldkeys);
1280 for (i = 0; i < n; i++)
1281 Py_CLEAR(oldvalues[i]);
1282 free_values(oldvalues);
1283 DK_DECREF(oldkeys);
1284 }
1285 else {
1286 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001287 DK_DECREF(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001288 }
1289}
1290
1291/* Returns -1 if no more items (or op is not a dict),
1292 * index of item otherwise. Stores value in pvalue
1293 */
1294Py_LOCAL_INLINE(Py_ssize_t)
1295dict_next(PyObject *op, Py_ssize_t i, PyObject **pvalue)
1296{
1297 Py_ssize_t mask, offset;
1298 PyDictObject *mp;
1299 PyObject **value_ptr;
1300
1301
1302 if (!PyDict_Check(op))
1303 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001305 if (i < 0)
1306 return -1;
1307 if (mp->ma_values) {
1308 value_ptr = &mp->ma_values[i];
1309 offset = sizeof(PyObject *);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001311 else {
1312 value_ptr = &mp->ma_keys->dk_entries[i].me_value;
1313 offset = sizeof(PyDictKeyEntry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001315 mask = DK_MASK(mp->ma_keys);
1316 while (i <= mask && *value_ptr == NULL) {
1317 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1318 i++;
1319 }
1320 if (i > mask)
1321 return -1;
1322 if (pvalue)
1323 *pvalue = *value_ptr;
1324 return i;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001325}
1326
Tim Peters080c88b2003-02-15 03:01:11 +00001327/*
1328 * Iterate over a dict. Use like so:
1329 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001330 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001331 * PyObject *key, *value;
1332 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001333 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +00001334 * Refer to borrowed references in key and value.
1335 * }
1336 *
1337 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001338 * mutates the dict. One exception: it is safe if the loop merely changes
1339 * the values associated with the keys (but doesn't insert new keys or
1340 * delete keys), via PyDict_SetItem().
1341 */
Guido van Rossum25831651993-05-19 14:50:45 +00001342int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001343PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001344{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001345 PyDictObject *mp;
1346 Py_ssize_t i = dict_next(op, *ppos, pvalue);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 if (i < 0)
1348 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001349 mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 *ppos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 if (pkey)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001352 *pkey = mp->ma_keys->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001354}
1355
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001356/* Internal version of PyDict_Next that returns a hash value in addition
1357 * to the key and value.
1358 */
Thomas Wouterscf297e42007-02-23 15:07:44 +00001359int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001360_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1361 PyObject **pvalue, Py_hash_t *phash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00001362{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001363 PyDictObject *mp;
1364 Py_ssize_t i = dict_next(op, *ppos, pvalue);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 if (i < 0)
1366 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001367 mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 *ppos = i+1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001369 *phash = mp->ma_keys->dk_entries[i].me_hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 if (pkey)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001371 *pkey = mp->ma_keys->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 return 1;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001373}
1374
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001375/* Methods */
1376
1377static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001378dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001379{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001380 PyObject **values = mp->ma_values;
1381 PyDictKeysObject *keys = mp->ma_keys;
1382 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 PyObject_GC_UnTrack(mp);
1384 Py_TRASHCAN_SAFE_BEGIN(mp)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001385 if (values != NULL) {
1386 if (values != empty_values) {
1387 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
1388 Py_XDECREF(values[i]);
1389 }
1390 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001392 DK_DECREF(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001394 else {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001395 assert(keys->dk_refcnt == 1);
1396 DK_DECREF(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001397 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1399 free_list[numfree++] = mp;
1400 else
1401 Py_TYPE(mp)->tp_free((PyObject *)mp);
1402 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001403}
1404
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001405
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001406static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001407dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001408{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 Py_ssize_t i;
1410 PyObject *s, *temp, *colon = NULL;
1411 PyObject *pieces = NULL, *result = NULL;
1412 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +00001413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001414 i = Py_ReprEnter((PyObject *)mp);
1415 if (i != 0) {
1416 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
1417 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 if (mp->ma_used == 0) {
1420 result = PyUnicode_FromString("{}");
1421 goto Done;
1422 }
Tim Petersa7259592001-06-16 05:11:17 +00001423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 pieces = PyList_New(0);
1425 if (pieces == NULL)
1426 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 colon = PyUnicode_FromString(": ");
1429 if (colon == NULL)
1430 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 /* Do repr() on each key+value pair, and insert ": " between them.
1433 Note that repr may mutate the dict. */
1434 i = 0;
1435 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
1436 int status;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001437 /* Prevent repr from deleting key or value during key format. */
1438 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 Py_INCREF(value);
1440 s = PyObject_Repr(key);
1441 PyUnicode_Append(&s, colon);
1442 PyUnicode_AppendAndDel(&s, PyObject_Repr(value));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001443 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 Py_DECREF(value);
1445 if (s == NULL)
1446 goto Done;
1447 status = PyList_Append(pieces, s);
1448 Py_DECREF(s); /* append created a new ref */
1449 if (status < 0)
1450 goto Done;
1451 }
Tim Petersa7259592001-06-16 05:11:17 +00001452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001453 /* Add "{}" decorations to the first and last items. */
1454 assert(PyList_GET_SIZE(pieces) > 0);
1455 s = PyUnicode_FromString("{");
1456 if (s == NULL)
1457 goto Done;
1458 temp = PyList_GET_ITEM(pieces, 0);
1459 PyUnicode_AppendAndDel(&s, temp);
1460 PyList_SET_ITEM(pieces, 0, s);
1461 if (s == NULL)
1462 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 s = PyUnicode_FromString("}");
1465 if (s == NULL)
1466 goto Done;
1467 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
1468 PyUnicode_AppendAndDel(&temp, s);
1469 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
1470 if (temp == NULL)
1471 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 /* Paste them all together with ", " between. */
1474 s = PyUnicode_FromString(", ");
1475 if (s == NULL)
1476 goto Done;
1477 result = PyUnicode_Join(s, pieces);
1478 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +00001479
1480Done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 Py_XDECREF(pieces);
1482 Py_XDECREF(colon);
1483 Py_ReprLeave((PyObject *)mp);
1484 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001485}
1486
Martin v. Löwis18e16552006-02-15 17:27:45 +00001487static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001488dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001489{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001491}
1492
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001493static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001494dict_subscript(PyDictObject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001495{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 PyObject *v;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001497 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001498 PyDictKeyEntry *ep;
1499 PyObject **value_addr;
1500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001502 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001503 hash = PyObject_Hash(key);
1504 if (hash == -1)
1505 return NULL;
1506 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001507 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001508 if (ep == NULL)
1509 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001510 v = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001511 if (v == NULL) {
1512 if (!PyDict_CheckExact(mp)) {
1513 /* Look up __missing__ method if we're a subclass. */
1514 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05001515 _Py_IDENTIFIER(__missing__);
1516 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001517 if (missing != NULL) {
1518 res = PyObject_CallFunctionObjArgs(missing,
1519 key, NULL);
1520 Py_DECREF(missing);
1521 return res;
1522 }
1523 else if (PyErr_Occurred())
1524 return NULL;
1525 }
1526 set_key_error(key);
1527 return NULL;
1528 }
1529 else
1530 Py_INCREF(v);
1531 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001532}
1533
1534static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001535dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001536{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537 if (w == NULL)
1538 return PyDict_DelItem((PyObject *)mp, v);
1539 else
1540 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001541}
1542
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001543static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544 (lenfunc)dict_length, /*mp_length*/
1545 (binaryfunc)dict_subscript, /*mp_subscript*/
1546 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001547};
1548
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001549static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001550dict_keys(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001551{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 register PyObject *v;
1553 register Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001554 PyDictKeyEntry *ep;
1555 Py_ssize_t size, n, offset;
1556 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001557
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001558 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001559 n = mp->ma_used;
1560 v = PyList_New(n);
1561 if (v == NULL)
1562 return NULL;
1563 if (n != mp->ma_used) {
1564 /* Durnit. The allocations caused the dict to resize.
1565 * Just start over, this shouldn't normally happen.
1566 */
1567 Py_DECREF(v);
1568 goto again;
1569 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001570 ep = &mp->ma_keys->dk_entries[0];
1571 size = DK_SIZE(mp->ma_keys);
1572 if (mp->ma_values) {
1573 value_ptr = mp->ma_values;
1574 offset = sizeof(PyObject *);
1575 }
1576 else {
1577 value_ptr = &ep[0].me_value;
1578 offset = sizeof(PyDictKeyEntry);
1579 }
1580 for (i = 0, j = 0; i < size; i++) {
1581 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 PyObject *key = ep[i].me_key;
1583 Py_INCREF(key);
1584 PyList_SET_ITEM(v, j, key);
1585 j++;
1586 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001587 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 }
1589 assert(j == n);
1590 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001591}
1592
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001593static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001594dict_values(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001595{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 register PyObject *v;
1597 register Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001598 Py_ssize_t size, n, offset;
1599 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001600
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001601 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001602 n = mp->ma_used;
1603 v = PyList_New(n);
1604 if (v == NULL)
1605 return NULL;
1606 if (n != mp->ma_used) {
1607 /* Durnit. The allocations caused the dict to resize.
1608 * Just start over, this shouldn't normally happen.
1609 */
1610 Py_DECREF(v);
1611 goto again;
1612 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001613 size = DK_SIZE(mp->ma_keys);
1614 if (mp->ma_values) {
1615 value_ptr = mp->ma_values;
1616 offset = sizeof(PyObject *);
1617 }
1618 else {
1619 value_ptr = &mp->ma_keys->dk_entries[0].me_value;
1620 offset = sizeof(PyDictKeyEntry);
1621 }
1622 for (i = 0, j = 0; i < size; i++) {
1623 PyObject *value = *value_ptr;
1624 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1625 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001626 Py_INCREF(value);
1627 PyList_SET_ITEM(v, j, value);
1628 j++;
1629 }
1630 }
1631 assert(j == n);
1632 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001633}
1634
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001635static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001636dict_items(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001637{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 register PyObject *v;
1639 register Py_ssize_t i, j, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001640 Py_ssize_t size, offset;
1641 PyObject *item, *key;
1642 PyDictKeyEntry *ep;
1643 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645 /* Preallocate the list of tuples, to avoid allocations during
1646 * the loop over the items, which could trigger GC, which
1647 * could resize the dict. :-(
1648 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001649 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001650 n = mp->ma_used;
1651 v = PyList_New(n);
1652 if (v == NULL)
1653 return NULL;
1654 for (i = 0; i < n; i++) {
1655 item = PyTuple_New(2);
1656 if (item == NULL) {
1657 Py_DECREF(v);
1658 return NULL;
1659 }
1660 PyList_SET_ITEM(v, i, item);
1661 }
1662 if (n != mp->ma_used) {
1663 /* Durnit. The allocations caused the dict to resize.
1664 * Just start over, this shouldn't normally happen.
1665 */
1666 Py_DECREF(v);
1667 goto again;
1668 }
1669 /* Nothing we do below makes any function calls. */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001670 ep = mp->ma_keys->dk_entries;
1671 size = DK_SIZE(mp->ma_keys);
1672 if (mp->ma_values) {
1673 value_ptr = mp->ma_values;
1674 offset = sizeof(PyObject *);
1675 }
1676 else {
1677 value_ptr = &ep[0].me_value;
1678 offset = sizeof(PyDictKeyEntry);
1679 }
1680 for (i = 0, j = 0; i < size; i++) {
1681 PyObject *value = *value_ptr;
1682 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1683 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 key = ep[i].me_key;
1685 item = PyList_GET_ITEM(v, j);
1686 Py_INCREF(key);
1687 PyTuple_SET_ITEM(item, 0, key);
1688 Py_INCREF(value);
1689 PyTuple_SET_ITEM(item, 1, value);
1690 j++;
1691 }
1692 }
1693 assert(j == n);
1694 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001695}
1696
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001697static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001698dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001699{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 PyObject *seq;
1701 PyObject *value = Py_None;
1702 PyObject *it; /* iter(seq) */
1703 PyObject *key;
1704 PyObject *d;
1705 int status;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001706
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001707 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
1708 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001709
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001710 d = PyObject_CallObject(cls, NULL);
1711 if (d == NULL)
1712 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001713
Benjamin Peterson9892f522012-10-31 14:22:12 -04001714 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001715 if (PyDict_CheckExact(seq)) {
1716 PyDictObject *mp = (PyDictObject *)d;
1717 PyObject *oldvalue;
1718 Py_ssize_t pos = 0;
1719 PyObject *key;
1720 Py_hash_t hash;
Guido van Rossum58da9312007-11-10 23:39:45 +00001721
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001722 if (dictresize(mp, Py_SIZE(seq))) {
Petri Lehtinena94200e2011-10-24 21:12:58 +03001723 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001725 }
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001726
1727 while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001728 if (insertdict(mp, key, hash, value)) {
1729 Py_DECREF(d);
1730 return NULL;
1731 }
1732 }
1733 return d;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001734 }
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001735 if (PyAnySet_CheckExact(seq)) {
1736 PyDictObject *mp = (PyDictObject *)d;
1737 Py_ssize_t pos = 0;
1738 PyObject *key;
1739 Py_hash_t hash;
Guido van Rossum58da9312007-11-10 23:39:45 +00001740
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001741 if (dictresize(mp, PySet_GET_SIZE(seq))) {
Petri Lehtinena94200e2011-10-24 21:12:58 +03001742 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001744 }
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001745
1746 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001747 if (insertdict(mp, key, hash, value)) {
1748 Py_DECREF(d);
1749 return NULL;
1750 }
1751 }
1752 return d;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001754 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001756 it = PyObject_GetIter(seq);
1757 if (it == NULL){
1758 Py_DECREF(d);
1759 return NULL;
1760 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 if (PyDict_CheckExact(d)) {
1763 while ((key = PyIter_Next(it)) != NULL) {
1764 status = PyDict_SetItem(d, key, value);
1765 Py_DECREF(key);
1766 if (status < 0)
1767 goto Fail;
1768 }
1769 } else {
1770 while ((key = PyIter_Next(it)) != NULL) {
1771 status = PyObject_SetItem(d, key, value);
1772 Py_DECREF(key);
1773 if (status < 0)
1774 goto Fail;
1775 }
1776 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001778 if (PyErr_Occurred())
1779 goto Fail;
1780 Py_DECREF(it);
1781 return d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001782
1783Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001784 Py_DECREF(it);
1785 Py_DECREF(d);
1786 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001787}
1788
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001789static int
1790dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001791{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001792 PyObject *arg = NULL;
1793 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001794
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001795 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1796 result = -1;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001798 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001799 _Py_IDENTIFIER(keys);
1800 if (_PyObject_HasAttrId(arg, &PyId_keys))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 result = PyDict_Merge(self, arg, 1);
1802 else
1803 result = PyDict_MergeFromSeq2(self, arg, 1);
1804 }
1805 if (result == 0 && kwds != NULL) {
1806 if (PyArg_ValidateKeywordArguments(kwds))
1807 result = PyDict_Merge(self, kwds, 1);
1808 else
1809 result = -1;
1810 }
1811 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001812}
1813
1814static PyObject *
1815dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1816{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001817 if (dict_update_common(self, args, kwds, "update") != -1)
1818 Py_RETURN_NONE;
1819 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001820}
1821
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001822/* Update unconditionally replaces existing items.
1823 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001824 otherwise it leaves existing items unchanged.
1825
1826 PyDict_{Update,Merge} update/merge from a mapping object.
1827
Tim Petersf582b822001-12-11 18:51:08 +00001828 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001829 producing iterable objects of length 2.
1830*/
1831
Tim Petersf582b822001-12-11 18:51:08 +00001832int
Tim Peters1fc240e2001-10-26 05:06:50 +00001833PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1834{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001835 PyObject *it; /* iter(seq2) */
1836 Py_ssize_t i; /* index into seq2 of current element */
1837 PyObject *item; /* seq2[i] */
1838 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00001839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001840 assert(d != NULL);
1841 assert(PyDict_Check(d));
1842 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00001843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844 it = PyObject_GetIter(seq2);
1845 if (it == NULL)
1846 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001847
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001848 for (i = 0; ; ++i) {
1849 PyObject *key, *value;
1850 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001852 fast = NULL;
1853 item = PyIter_Next(it);
1854 if (item == NULL) {
1855 if (PyErr_Occurred())
1856 goto Fail;
1857 break;
1858 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001860 /* Convert item to sequence, and verify length 2. */
1861 fast = PySequence_Fast(item, "");
1862 if (fast == NULL) {
1863 if (PyErr_ExceptionMatches(PyExc_TypeError))
1864 PyErr_Format(PyExc_TypeError,
1865 "cannot convert dictionary update "
1866 "sequence element #%zd to a sequence",
1867 i);
1868 goto Fail;
1869 }
1870 n = PySequence_Fast_GET_SIZE(fast);
1871 if (n != 2) {
1872 PyErr_Format(PyExc_ValueError,
1873 "dictionary update sequence element #%zd "
1874 "has length %zd; 2 is required",
1875 i, n);
1876 goto Fail;
1877 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 /* Update/merge with this (key, value) pair. */
1880 key = PySequence_Fast_GET_ITEM(fast, 0);
1881 value = PySequence_Fast_GET_ITEM(fast, 1);
1882 if (override || PyDict_GetItem(d, key) == NULL) {
1883 int status = PyDict_SetItem(d, key, value);
1884 if (status < 0)
1885 goto Fail;
1886 }
1887 Py_DECREF(fast);
1888 Py_DECREF(item);
1889 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 i = 0;
1892 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00001893Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 Py_XDECREF(item);
1895 Py_XDECREF(fast);
1896 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001897Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 Py_DECREF(it);
1899 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001900}
1901
Tim Peters6d6c1a32001-08-02 04:15:00 +00001902int
1903PyDict_Update(PyObject *a, PyObject *b)
1904{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 return PyDict_Merge(a, b, 1);
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001906}
1907
1908int
1909PyDict_Merge(PyObject *a, PyObject *b, int override)
1910{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001911 register PyDictObject *mp, *other;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001912 register Py_ssize_t i, n;
1913 PyDictKeyEntry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915 /* We accept for the argument either a concrete dictionary object,
1916 * or an abstract "mapping" object. For the former, we can do
1917 * things quite efficiently. For the latter, we only require that
1918 * PyMapping_Keys() and PyObject_GetItem() be supported.
1919 */
1920 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1921 PyErr_BadInternalCall();
1922 return -1;
1923 }
1924 mp = (PyDictObject*)a;
1925 if (PyDict_Check(b)) {
1926 other = (PyDictObject*)b;
1927 if (other == mp || other->ma_used == 0)
1928 /* a.update(a) or a.update({}); nothing to do */
1929 return 0;
1930 if (mp->ma_used == 0)
1931 /* Since the target dict is empty, PyDict_GetItem()
1932 * always returns NULL. Setting override to 1
1933 * skips the unnecessary test.
1934 */
1935 override = 1;
1936 /* Do one big resize at the start, rather than
1937 * incrementally resizing as we insert new items. Expect
1938 * that there will be no (or few) overlapping keys.
1939 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001940 if (mp->ma_keys->dk_usable * 3 < other->ma_used * 2)
1941 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001942 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001943 for (i = 0, n = DK_SIZE(other->ma_keys); i < n; i++) {
1944 PyObject *value;
1945 entry = &other->ma_keys->dk_entries[i];
1946 if (other->ma_values)
1947 value = other->ma_values[i];
1948 else
1949 value = entry->me_value;
1950
1951 if (value != NULL &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 (override ||
1953 PyDict_GetItem(a, entry->me_key) == NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001954 if (insertdict(mp, entry->me_key,
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001955 entry->me_hash,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001956 value) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001957 return -1;
1958 }
1959 }
1960 }
1961 else {
1962 /* Do it the generic, slower way */
1963 PyObject *keys = PyMapping_Keys(b);
1964 PyObject *iter;
1965 PyObject *key, *value;
1966 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001968 if (keys == NULL)
1969 /* Docstring says this is equivalent to E.keys() so
1970 * if E doesn't have a .keys() method we want
1971 * AttributeError to percolate up. Might as well
1972 * do the same for any other error.
1973 */
1974 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001976 iter = PyObject_GetIter(keys);
1977 Py_DECREF(keys);
1978 if (iter == NULL)
1979 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001981 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
1982 if (!override && PyDict_GetItem(a, key) != NULL) {
1983 Py_DECREF(key);
1984 continue;
1985 }
1986 value = PyObject_GetItem(b, key);
1987 if (value == NULL) {
1988 Py_DECREF(iter);
1989 Py_DECREF(key);
1990 return -1;
1991 }
1992 status = PyDict_SetItem(a, key, value);
1993 Py_DECREF(key);
1994 Py_DECREF(value);
1995 if (status < 0) {
1996 Py_DECREF(iter);
1997 return -1;
1998 }
1999 }
2000 Py_DECREF(iter);
2001 if (PyErr_Occurred())
2002 /* Iterator completed, via error */
2003 return -1;
2004 }
2005 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002006}
2007
2008static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002009dict_copy(register PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002010{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002011 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002012}
2013
2014PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002015PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002016{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002017 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002018 PyDictObject *mp;
2019 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002020
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002021 if (o == NULL || !PyDict_Check(o)) {
2022 PyErr_BadInternalCall();
2023 return NULL;
2024 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002025 mp = (PyDictObject *)o;
2026 if (_PyDict_HasSplitTable(mp)) {
2027 PyDictObject *split_copy;
2028 PyObject **newvalues = new_values(DK_SIZE(mp->ma_keys));
2029 if (newvalues == NULL)
2030 return PyErr_NoMemory();
2031 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2032 if (split_copy == NULL) {
2033 free_values(newvalues);
2034 return NULL;
2035 }
2036 split_copy->ma_values = newvalues;
2037 split_copy->ma_keys = mp->ma_keys;
2038 split_copy->ma_used = mp->ma_used;
2039 DK_INCREF(mp->ma_keys);
2040 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2041 PyObject *value = mp->ma_values[i];
2042 Py_XINCREF(value);
2043 split_copy->ma_values[i] = value;
2044 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002045 if (_PyObject_GC_IS_TRACKED(mp))
2046 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002047 return (PyObject *)split_copy;
2048 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002049 copy = PyDict_New();
2050 if (copy == NULL)
2051 return NULL;
2052 if (PyDict_Merge(copy, o, 1) == 0)
2053 return copy;
2054 Py_DECREF(copy);
2055 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002056}
2057
Martin v. Löwis18e16552006-02-15 17:27:45 +00002058Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002059PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002060{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061 if (mp == NULL || !PyDict_Check(mp)) {
2062 PyErr_BadInternalCall();
2063 return -1;
2064 }
2065 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002066}
2067
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002068PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002069PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002070{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002071 if (mp == NULL || !PyDict_Check(mp)) {
2072 PyErr_BadInternalCall();
2073 return NULL;
2074 }
2075 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002076}
2077
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002078PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002079PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002080{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002081 if (mp == NULL || !PyDict_Check(mp)) {
2082 PyErr_BadInternalCall();
2083 return NULL;
2084 }
2085 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002086}
2087
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002088PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002089PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002090{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002091 if (mp == NULL || !PyDict_Check(mp)) {
2092 PyErr_BadInternalCall();
2093 return NULL;
2094 }
2095 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002096}
2097
Tim Peterse63415e2001-05-08 04:38:29 +00002098/* Return 1 if dicts equal, 0 if not, -1 if error.
2099 * Gets out as soon as any difference is detected.
2100 * Uses only Py_EQ comparison.
2101 */
2102static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002103dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002104{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002105 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002107 if (a->ma_used != b->ma_used)
2108 /* can't be equal if # of entries differ */
2109 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002110 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002111 for (i = 0; i < DK_SIZE(a->ma_keys); i++) {
2112 PyDictKeyEntry *ep = &a->ma_keys->dk_entries[i];
2113 PyObject *aval;
2114 if (a->ma_values)
2115 aval = a->ma_values[i];
2116 else
2117 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002118 if (aval != NULL) {
2119 int cmp;
2120 PyObject *bval;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002121 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122 /* temporarily bump aval's refcount to ensure it stays
2123 alive until we're done with it */
2124 Py_INCREF(aval);
2125 /* ditto for key */
2126 Py_INCREF(key);
2127 bval = PyDict_GetItemWithError((PyObject *)b, key);
2128 Py_DECREF(key);
2129 if (bval == NULL) {
2130 Py_DECREF(aval);
2131 if (PyErr_Occurred())
2132 return -1;
2133 return 0;
2134 }
2135 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
2136 Py_DECREF(aval);
2137 if (cmp <= 0) /* error or not equal */
2138 return cmp;
2139 }
2140 }
2141 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002142}
Tim Peterse63415e2001-05-08 04:38:29 +00002143
2144static PyObject *
2145dict_richcompare(PyObject *v, PyObject *w, int op)
2146{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002147 int cmp;
2148 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002150 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2151 res = Py_NotImplemented;
2152 }
2153 else if (op == Py_EQ || op == Py_NE) {
2154 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2155 if (cmp < 0)
2156 return NULL;
2157 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2158 }
2159 else
2160 res = Py_NotImplemented;
2161 Py_INCREF(res);
2162 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002163}
Tim Peterse63415e2001-05-08 04:38:29 +00002164
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002165static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002166dict_contains(register PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002167{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002168 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002169 PyDictKeyEntry *ep;
2170 PyObject **value_addr;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002172 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002173 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002174 hash = PyObject_Hash(key);
2175 if (hash == -1)
2176 return NULL;
2177 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002178 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002179 if (ep == NULL)
2180 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002181 return PyBool_FromLong(*value_addr != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002182}
2183
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002184static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002185dict_get(register PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00002186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002187 PyObject *key;
2188 PyObject *failobj = Py_None;
2189 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002190 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002191 PyDictKeyEntry *ep;
2192 PyObject **value_addr;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002194 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
2195 return NULL;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002198 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002199 hash = PyObject_Hash(key);
2200 if (hash == -1)
2201 return NULL;
2202 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002203 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002204 if (ep == NULL)
2205 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002206 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002207 if (val == NULL)
2208 val = failobj;
2209 Py_INCREF(val);
2210 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002211}
2212
Barry Warsawc38c5da1997-10-06 17:49:20 +00002213static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002214dict_setdefault(register PyDictObject *mp, PyObject *args)
Guido van Rossum164452c2000-08-08 16:12:54 +00002215{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002216 PyObject *key;
2217 PyObject *failobj = Py_None;
2218 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002219 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002220 PyDictKeyEntry *ep;
2221 PyObject **value_addr;
Guido van Rossum164452c2000-08-08 16:12:54 +00002222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002223 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
2224 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00002225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002226 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002227 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228 hash = PyObject_Hash(key);
2229 if (hash == -1)
2230 return NULL;
2231 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002232 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002233 if (ep == NULL)
2234 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002235 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002236 if (val == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002237 if (mp->ma_keys->dk_usable <= 0) {
2238 /* Need to resize. */
2239 if (insertion_resize(mp) < 0)
2240 return NULL;
2241 ep = find_empty_slot(mp, key, hash, &value_addr);
2242 }
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002243 Py_INCREF(failobj);
2244 Py_INCREF(key);
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002245 MAINTAIN_TRACKING(mp, key, failobj);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002246 ep->me_key = key;
2247 ep->me_hash = hash;
2248 *value_addr = failobj;
2249 val = failobj;
2250 mp->ma_keys->dk_usable--;
2251 mp->ma_used++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002252 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002253 Py_INCREF(val);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002254 return val;
Guido van Rossum164452c2000-08-08 16:12:54 +00002255}
2256
2257
2258static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002259dict_clear(register PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002260{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261 PyDict_Clear((PyObject *)mp);
2262 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002263}
2264
Guido van Rossumba6ab842000-12-12 22:02:18 +00002265static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002266dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002267{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002268 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002269 PyObject *old_value, *old_key;
2270 PyObject *key, *deflt = NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002271 PyDictKeyEntry *ep;
2272 PyObject **value_addr;
Guido van Rossume027d982002-04-12 15:11:59 +00002273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002274 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2275 return NULL;
2276 if (mp->ma_used == 0) {
2277 if (deflt) {
2278 Py_INCREF(deflt);
2279 return deflt;
2280 }
Raymond Hettingerdd421542010-10-30 08:10:29 +00002281 set_key_error(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002282 return NULL;
2283 }
2284 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002285 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002286 hash = PyObject_Hash(key);
2287 if (hash == -1)
2288 return NULL;
2289 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002290 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002291 if (ep == NULL)
2292 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002293 old_value = *value_addr;
2294 if (old_value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002295 if (deflt) {
2296 Py_INCREF(deflt);
2297 return deflt;
2298 }
2299 set_key_error(key);
2300 return NULL;
2301 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002302 *value_addr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002303 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002304 if (!_PyDict_HasSplitTable(mp)) {
2305 ENSURE_ALLOWS_DELETIONS(mp);
2306 old_key = ep->me_key;
2307 Py_INCREF(dummy);
2308 ep->me_key = dummy;
2309 Py_DECREF(old_key);
2310 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002311 return old_value;
Guido van Rossume027d982002-04-12 15:11:59 +00002312}
2313
2314static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002315dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00002316{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002317 Py_hash_t i = 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002318 PyDictKeyEntry *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002319 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002320
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002322 /* Allocate the result tuple before checking the size. Believe it
2323 * or not, this allocation could trigger a garbage collection which
2324 * could empty the dict, so if we checked the size first and that
2325 * happened, the result would be an infinite loop (searching for an
2326 * entry that no longer exists). Note that the usual popitem()
2327 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2328 * tuple away if the dict *is* empty isn't a significant
2329 * inefficiency -- possible, but unlikely in practice.
2330 */
2331 res = PyTuple_New(2);
2332 if (res == NULL)
2333 return NULL;
2334 if (mp->ma_used == 0) {
2335 Py_DECREF(res);
2336 PyErr_SetString(PyExc_KeyError,
2337 "popitem(): dictionary is empty");
2338 return NULL;
2339 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002340 /* Convert split table to combined table */
2341 if (mp->ma_keys->dk_lookup == lookdict_split) {
2342 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
2343 Py_DECREF(res);
2344 return NULL;
2345 }
2346 }
2347 ENSURE_ALLOWS_DELETIONS(mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002348 /* Set ep to "the first" dict entry with a value. We abuse the hash
2349 * field of slot 0 to hold a search finger:
2350 * If slot 0 has a value, use slot 0.
2351 * Else slot 0 is being used to hold a search finger,
2352 * and we use its hash value as the first index to look.
2353 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002354 ep = &mp->ma_keys->dk_entries[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002355 if (ep->me_value == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002356 Py_ssize_t mask = DK_MASK(mp->ma_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 i = ep->me_hash;
2358 /* The hash field may be a real hash value, or it may be a
2359 * legit search finger, or it may be a once-legit search
2360 * finger that's out of bounds now because it wrapped around
2361 * or the table shrunk -- simply make sure it's in bounds now.
2362 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002363 if (i > mask || i < 1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 i = 1; /* skip slot 0 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002365 while ((ep = &mp->ma_keys->dk_entries[i])->me_value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002367 if (i > mask)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002368 i = 1;
2369 }
2370 }
2371 PyTuple_SET_ITEM(res, 0, ep->me_key);
2372 PyTuple_SET_ITEM(res, 1, ep->me_value);
2373 Py_INCREF(dummy);
2374 ep->me_key = dummy;
2375 ep->me_value = NULL;
2376 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002377 assert(mp->ma_keys->dk_entries[0].me_value == NULL);
2378 mp->ma_keys->dk_entries[0].me_hash = i + 1; /* next place to start */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002380}
2381
Jeremy Hylton8caad492000-06-23 14:18:11 +00002382static int
2383dict_traverse(PyObject *op, visitproc visit, void *arg)
2384{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002385 Py_ssize_t i, n;
2386 PyDictObject *mp = (PyDictObject *)op;
2387 if (mp->ma_keys->dk_lookup == lookdict) {
2388 for (i = 0; i < DK_SIZE(mp->ma_keys); i++) {
2389 if (mp->ma_keys->dk_entries[i].me_value != NULL) {
2390 Py_VISIT(mp->ma_keys->dk_entries[i].me_value);
2391 Py_VISIT(mp->ma_keys->dk_entries[i].me_key);
2392 }
2393 }
2394 } else {
2395 if (mp->ma_values != NULL) {
2396 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2397 Py_VISIT(mp->ma_values[i]);
2398 }
2399 }
2400 else {
2401 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2402 Py_VISIT(mp->ma_keys->dk_entries[i].me_value);
2403 }
2404 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002405 }
2406 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002407}
2408
2409static int
2410dict_tp_clear(PyObject *op)
2411{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002412 PyDict_Clear(op);
2413 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002414}
2415
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002416static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002417
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002418static PyObject *
2419dict_sizeof(PyDictObject *mp)
2420{
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002421 Py_ssize_t size, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002422
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002423 size = DK_SIZE(mp->ma_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002424 res = sizeof(PyDictObject);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002425 if (mp->ma_values)
2426 res += size * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002427 /* If the dictionary is split, the keys portion is accounted-for
2428 in the type object. */
2429 if (mp->ma_keys->dk_refcnt == 1)
2430 res += sizeof(PyDictKeysObject) + (size-1) * sizeof(PyDictKeyEntry);
2431 return PyLong_FromSsize_t(res);
2432}
2433
2434Py_ssize_t
2435_PyDict_KeysSize(PyDictKeysObject *keys)
2436{
2437 return sizeof(PyDictKeysObject) + (DK_SIZE(keys)-1) * sizeof(PyDictKeyEntry);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002438}
2439
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002440PyDoc_STRVAR(contains__doc__,
2441"D.__contains__(k) -> True if D has a key k, else False");
2442
2443PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
2444
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002445PyDoc_STRVAR(sizeof__doc__,
2446"D.__sizeof__() -> size of D in memory, in bytes");
2447
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002448PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002449"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002450
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002451PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002452"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 +00002453
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002454PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002455"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002456If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00002457
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002458PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00002459"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000024602-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002461
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002462PyDoc_STRVAR(update__doc__,
Georg Brandlac0675c2011-12-18 19:30:55 +01002463"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n"
2464"If E present and has a .keys() method, does: for k in E: D[k] = E[k]\n\
2465If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002466In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00002467
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002468PyDoc_STRVAR(fromkeys__doc__,
2469"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
2470v defaults to None.");
2471
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002472PyDoc_STRVAR(clear__doc__,
2473"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002474
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002475PyDoc_STRVAR(copy__doc__,
2476"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00002477
Guido van Rossumb90c8482007-02-10 01:11:45 +00002478/* Forward */
2479static PyObject *dictkeys_new(PyObject *);
2480static PyObject *dictitems_new(PyObject *);
2481static PyObject *dictvalues_new(PyObject *);
2482
Guido van Rossum45c85d12007-07-27 16:31:40 +00002483PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002484 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002485PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002486 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002487PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002488 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00002489
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002490static PyMethodDef mapp_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002491 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
2492 contains__doc__},
2493 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
2494 getitem__doc__},
2495 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
2496 sizeof__doc__},
2497 {"get", (PyCFunction)dict_get, METH_VARARGS,
2498 get__doc__},
2499 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
2500 setdefault_doc__},
2501 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
2502 pop__doc__},
2503 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
2504 popitem__doc__},
2505 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
2506 keys__doc__},
2507 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
2508 items__doc__},
2509 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
2510 values__doc__},
2511 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
2512 update__doc__},
2513 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
2514 fromkeys__doc__},
2515 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
2516 clear__doc__},
2517 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
2518 copy__doc__},
2519 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002520};
2521
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002522/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002523int
2524PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002525{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002526 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002527 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002528 PyDictKeyEntry *ep;
2529 PyObject **value_addr;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002531 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002532 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002533 hash = PyObject_Hash(key);
2534 if (hash == -1)
2535 return -1;
2536 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002537 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
2538 return (ep == NULL) ? -1 : (*value_addr != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002539}
2540
Thomas Wouterscf297e42007-02-23 15:07:44 +00002541/* Internal version of PyDict_Contains used when the hash value is already known */
2542int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002543_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00002544{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002545 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002546 PyDictKeyEntry *ep;
2547 PyObject **value_addr;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002548
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002549 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
2550 return (ep == NULL) ? -1 : (*value_addr != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00002551}
2552
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002553/* Hack to implement "key in dict" */
2554static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002555 0, /* sq_length */
2556 0, /* sq_concat */
2557 0, /* sq_repeat */
2558 0, /* sq_item */
2559 0, /* sq_slice */
2560 0, /* sq_ass_item */
2561 0, /* sq_ass_slice */
2562 PyDict_Contains, /* sq_contains */
2563 0, /* sq_inplace_concat */
2564 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002565};
2566
Guido van Rossum09e563a2001-05-01 12:10:21 +00002567static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002568dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2569{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002570 PyObject *self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002572 assert(type != NULL && type->tp_alloc != NULL);
2573 self = type->tp_alloc(type, 0);
2574 if (self != NULL) {
2575 PyDictObject *d = (PyDictObject *)self;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002576 d->ma_keys = new_keys_object(PyDict_MINSIZE_COMBINED);
2577 /* XXX - Should we raise a no-memory error? */
2578 if (d->ma_keys == NULL) {
2579 DK_INCREF(Py_EMPTY_KEYS);
2580 d->ma_keys = Py_EMPTY_KEYS;
2581 d->ma_values = empty_values;
2582 }
2583 d->ma_used = 0;
Ezio Melotti13925002011-03-16 11:05:33 +02002584 /* The object has been implicitly tracked by tp_alloc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002585 if (type == &PyDict_Type)
2586 _PyObject_GC_UNTRACK(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002587 }
2588 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002589}
2590
Tim Peters25786c02001-09-02 08:22:48 +00002591static int
2592dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2593{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002594 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002595}
2596
Tim Peters6d6c1a32001-08-02 04:15:00 +00002597static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002598dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002599{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002600 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002601}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002602
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002603PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002604"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002605"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002606" (key, value) pairs\n"
2607"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002608" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002609" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002610" d[k] = v\n"
2611"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2612" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002613
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002614PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002615 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2616 "dict",
2617 sizeof(PyDictObject),
2618 0,
2619 (destructor)dict_dealloc, /* tp_dealloc */
2620 0, /* tp_print */
2621 0, /* tp_getattr */
2622 0, /* tp_setattr */
2623 0, /* tp_reserved */
2624 (reprfunc)dict_repr, /* tp_repr */
2625 0, /* tp_as_number */
2626 &dict_as_sequence, /* tp_as_sequence */
2627 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002628 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002629 0, /* tp_call */
2630 0, /* tp_str */
2631 PyObject_GenericGetAttr, /* tp_getattro */
2632 0, /* tp_setattro */
2633 0, /* tp_as_buffer */
2634 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2635 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
2636 dictionary_doc, /* tp_doc */
2637 dict_traverse, /* tp_traverse */
2638 dict_tp_clear, /* tp_clear */
2639 dict_richcompare, /* tp_richcompare */
2640 0, /* tp_weaklistoffset */
2641 (getiterfunc)dict_iter, /* tp_iter */
2642 0, /* tp_iternext */
2643 mapp_methods, /* tp_methods */
2644 0, /* tp_members */
2645 0, /* tp_getset */
2646 0, /* tp_base */
2647 0, /* tp_dict */
2648 0, /* tp_descr_get */
2649 0, /* tp_descr_set */
2650 0, /* tp_dictoffset */
2651 dict_init, /* tp_init */
2652 PyType_GenericAlloc, /* tp_alloc */
2653 dict_new, /* tp_new */
2654 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002655};
2656
Victor Stinner3c1e4812012-03-26 22:10:51 +02002657PyObject *
2658_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
2659{
2660 PyObject *kv;
2661 kv = _PyUnicode_FromId(key); /* borrowed */
2662 if (kv == NULL)
2663 return NULL;
2664 return PyDict_GetItem(dp, kv);
2665}
2666
Guido van Rossum3cca2451997-05-16 14:23:33 +00002667/* For backward compatibility with old dictionary interface */
2668
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002669PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002670PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002671{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002672 PyObject *kv, *rv;
2673 kv = PyUnicode_FromString(key);
2674 if (kv == NULL)
2675 return NULL;
2676 rv = PyDict_GetItem(v, kv);
2677 Py_DECREF(kv);
2678 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002679}
2680
2681int
Victor Stinner3c1e4812012-03-26 22:10:51 +02002682_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
2683{
2684 PyObject *kv;
2685 kv = _PyUnicode_FromId(key); /* borrowed */
2686 if (kv == NULL)
2687 return -1;
2688 return PyDict_SetItem(v, kv, item);
2689}
2690
2691int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002692PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002693{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002694 PyObject *kv;
2695 int err;
2696 kv = PyUnicode_FromString(key);
2697 if (kv == NULL)
2698 return -1;
2699 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
2700 err = PyDict_SetItem(v, kv, item);
2701 Py_DECREF(kv);
2702 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002703}
2704
2705int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002706PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002707{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002708 PyObject *kv;
2709 int err;
2710 kv = PyUnicode_FromString(key);
2711 if (kv == NULL)
2712 return -1;
2713 err = PyDict_DelItem(v, kv);
2714 Py_DECREF(kv);
2715 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002716}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002717
Raymond Hettinger019a1482004-03-18 02:41:19 +00002718/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002719
2720typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002721 PyObject_HEAD
2722 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
2723 Py_ssize_t di_used;
2724 Py_ssize_t di_pos;
2725 PyObject* di_result; /* reusable result tuple for iteritems */
2726 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002727} dictiterobject;
2728
2729static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002730dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002731{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002732 dictiterobject *di;
2733 di = PyObject_GC_New(dictiterobject, itertype);
2734 if (di == NULL)
2735 return NULL;
2736 Py_INCREF(dict);
2737 di->di_dict = dict;
2738 di->di_used = dict->ma_used;
2739 di->di_pos = 0;
2740 di->len = dict->ma_used;
2741 if (itertype == &PyDictIterItem_Type) {
2742 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2743 if (di->di_result == NULL) {
2744 Py_DECREF(di);
2745 return NULL;
2746 }
2747 }
2748 else
2749 di->di_result = NULL;
2750 _PyObject_GC_TRACK(di);
2751 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002752}
2753
2754static void
2755dictiter_dealloc(dictiterobject *di)
2756{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 Py_XDECREF(di->di_dict);
2758 Py_XDECREF(di->di_result);
2759 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002760}
2761
2762static int
2763dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
2764{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002765 Py_VISIT(di->di_dict);
2766 Py_VISIT(di->di_result);
2767 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002768}
2769
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002770static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002771dictiter_len(dictiterobject *di)
2772{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002773 Py_ssize_t len = 0;
2774 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
2775 len = di->len;
2776 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002777}
2778
Guido van Rossumb90c8482007-02-10 01:11:45 +00002779PyDoc_STRVAR(length_hint_doc,
2780 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002781
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002782static PyObject *
2783dictiter_reduce(dictiterobject *di);
2784
2785PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
2786
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002787static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002788 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
2789 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002790 {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS,
2791 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002792 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002793};
2794
Raymond Hettinger019a1482004-03-18 02:41:19 +00002795static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002796{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002797 PyObject *key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002798 register Py_ssize_t i, mask, offset;
2799 register PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002800 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002801 PyObject **value_ptr;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002803 if (d == NULL)
2804 return NULL;
2805 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002806
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002807 if (di->di_used != d->ma_used) {
2808 PyErr_SetString(PyExc_RuntimeError,
2809 "dictionary changed size during iteration");
2810 di->di_used = -1; /* Make this state sticky */
2811 return NULL;
2812 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002814 i = di->di_pos;
2815 if (i < 0)
2816 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002817 k = d->ma_keys;
2818 if (d->ma_values) {
2819 value_ptr = &d->ma_values[i];
2820 offset = sizeof(PyObject *);
2821 }
2822 else {
2823 value_ptr = &k->dk_entries[i].me_value;
2824 offset = sizeof(PyDictKeyEntry);
2825 }
2826 mask = DK_SIZE(k)-1;
2827 while (i <= mask && *value_ptr == NULL) {
2828 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002829 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002830 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002831 di->di_pos = i+1;
2832 if (i > mask)
2833 goto fail;
2834 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002835 key = k->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002836 Py_INCREF(key);
2837 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002838
2839fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002840 Py_DECREF(d);
2841 di->di_dict = NULL;
2842 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002843}
2844
Raymond Hettinger019a1482004-03-18 02:41:19 +00002845PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002846 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2847 "dict_keyiterator", /* tp_name */
2848 sizeof(dictiterobject), /* tp_basicsize */
2849 0, /* tp_itemsize */
2850 /* methods */
2851 (destructor)dictiter_dealloc, /* tp_dealloc */
2852 0, /* tp_print */
2853 0, /* tp_getattr */
2854 0, /* tp_setattr */
2855 0, /* tp_reserved */
2856 0, /* tp_repr */
2857 0, /* tp_as_number */
2858 0, /* tp_as_sequence */
2859 0, /* tp_as_mapping */
2860 0, /* tp_hash */
2861 0, /* tp_call */
2862 0, /* tp_str */
2863 PyObject_GenericGetAttr, /* tp_getattro */
2864 0, /* tp_setattro */
2865 0, /* tp_as_buffer */
2866 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2867 0, /* tp_doc */
2868 (traverseproc)dictiter_traverse, /* tp_traverse */
2869 0, /* tp_clear */
2870 0, /* tp_richcompare */
2871 0, /* tp_weaklistoffset */
2872 PyObject_SelfIter, /* tp_iter */
2873 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
2874 dictiter_methods, /* tp_methods */
2875 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002876};
2877
2878static PyObject *dictiter_iternextvalue(dictiterobject *di)
2879{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002880 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002881 register Py_ssize_t i, mask, offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002882 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002883 PyObject **value_ptr;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002884
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002885 if (d == NULL)
2886 return NULL;
2887 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00002888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002889 if (di->di_used != d->ma_used) {
2890 PyErr_SetString(PyExc_RuntimeError,
2891 "dictionary changed size during iteration");
2892 di->di_used = -1; /* Make this state sticky */
2893 return NULL;
2894 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002896 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002897 mask = DK_SIZE(d->ma_keys)-1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002898 if (i < 0 || i > mask)
2899 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002900 if (d->ma_values) {
2901 value_ptr = &d->ma_values[i];
2902 offset = sizeof(PyObject *);
2903 }
2904 else {
2905 value_ptr = &d->ma_keys->dk_entries[i].me_value;
2906 offset = sizeof(PyDictKeyEntry);
2907 }
2908 while (i <= mask && *value_ptr == NULL) {
2909 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002910 i++;
2911 if (i > mask)
2912 goto fail;
2913 }
2914 di->di_pos = i+1;
2915 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002916 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 Py_INCREF(value);
2918 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002919
2920fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002921 Py_DECREF(d);
2922 di->di_dict = NULL;
2923 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002924}
2925
2926PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002927 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2928 "dict_valueiterator", /* tp_name */
2929 sizeof(dictiterobject), /* tp_basicsize */
2930 0, /* tp_itemsize */
2931 /* methods */
2932 (destructor)dictiter_dealloc, /* tp_dealloc */
2933 0, /* tp_print */
2934 0, /* tp_getattr */
2935 0, /* tp_setattr */
2936 0, /* tp_reserved */
2937 0, /* tp_repr */
2938 0, /* tp_as_number */
2939 0, /* tp_as_sequence */
2940 0, /* tp_as_mapping */
2941 0, /* tp_hash */
2942 0, /* tp_call */
2943 0, /* tp_str */
2944 PyObject_GenericGetAttr, /* tp_getattro */
2945 0, /* tp_setattro */
2946 0, /* tp_as_buffer */
2947 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2948 0, /* tp_doc */
2949 (traverseproc)dictiter_traverse, /* tp_traverse */
2950 0, /* tp_clear */
2951 0, /* tp_richcompare */
2952 0, /* tp_weaklistoffset */
2953 PyObject_SelfIter, /* tp_iter */
2954 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
2955 dictiter_methods, /* tp_methods */
2956 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002957};
2958
2959static PyObject *dictiter_iternextitem(dictiterobject *di)
2960{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002961 PyObject *key, *value, *result = di->di_result;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002962 register Py_ssize_t i, mask, offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002963 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002964 PyObject **value_ptr;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002966 if (d == NULL)
2967 return NULL;
2968 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00002969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002970 if (di->di_used != d->ma_used) {
2971 PyErr_SetString(PyExc_RuntimeError,
2972 "dictionary changed size during iteration");
2973 di->di_used = -1; /* Make this state sticky */
2974 return NULL;
2975 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002977 i = di->di_pos;
2978 if (i < 0)
2979 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002980 mask = DK_SIZE(d->ma_keys)-1;
2981 if (d->ma_values) {
2982 value_ptr = &d->ma_values[i];
2983 offset = sizeof(PyObject *);
2984 }
2985 else {
2986 value_ptr = &d->ma_keys->dk_entries[i].me_value;
2987 offset = sizeof(PyDictKeyEntry);
2988 }
2989 while (i <= mask && *value_ptr == NULL) {
2990 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002991 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002992 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002993 di->di_pos = i+1;
2994 if (i > mask)
2995 goto fail;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002997 if (result->ob_refcnt == 1) {
2998 Py_INCREF(result);
2999 Py_DECREF(PyTuple_GET_ITEM(result, 0));
3000 Py_DECREF(PyTuple_GET_ITEM(result, 1));
3001 } else {
3002 result = PyTuple_New(2);
3003 if (result == NULL)
3004 return NULL;
3005 }
3006 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003007 key = d->ma_keys->dk_entries[i].me_key;
3008 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003009 Py_INCREF(key);
3010 Py_INCREF(value);
3011 PyTuple_SET_ITEM(result, 0, key);
3012 PyTuple_SET_ITEM(result, 1, value);
3013 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003014
3015fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003016 Py_DECREF(d);
3017 di->di_dict = NULL;
3018 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003019}
3020
3021PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003022 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3023 "dict_itemiterator", /* tp_name */
3024 sizeof(dictiterobject), /* tp_basicsize */
3025 0, /* tp_itemsize */
3026 /* methods */
3027 (destructor)dictiter_dealloc, /* tp_dealloc */
3028 0, /* tp_print */
3029 0, /* tp_getattr */
3030 0, /* tp_setattr */
3031 0, /* tp_reserved */
3032 0, /* tp_repr */
3033 0, /* tp_as_number */
3034 0, /* tp_as_sequence */
3035 0, /* tp_as_mapping */
3036 0, /* tp_hash */
3037 0, /* tp_call */
3038 0, /* tp_str */
3039 PyObject_GenericGetAttr, /* tp_getattro */
3040 0, /* tp_setattro */
3041 0, /* tp_as_buffer */
3042 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3043 0, /* tp_doc */
3044 (traverseproc)dictiter_traverse, /* tp_traverse */
3045 0, /* tp_clear */
3046 0, /* tp_richcompare */
3047 0, /* tp_weaklistoffset */
3048 PyObject_SelfIter, /* tp_iter */
3049 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3050 dictiter_methods, /* tp_methods */
3051 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003052};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003053
3054
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003055static PyObject *
3056dictiter_reduce(dictiterobject *di)
3057{
3058 PyObject *list;
3059 dictiterobject tmp;
3060
3061 list = PyList_New(0);
3062 if (!list)
3063 return NULL;
3064
3065 /* copy the itertor state */
3066 tmp = *di;
3067 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003068
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003069 /* iterate the temporary into a list */
3070 for(;;) {
3071 PyObject *element = 0;
3072 if (Py_TYPE(di) == &PyDictIterItem_Type)
3073 element = dictiter_iternextitem(&tmp);
3074 else if (Py_TYPE(di) == &PyDictIterKey_Type)
3075 element = dictiter_iternextkey(&tmp);
3076 else if (Py_TYPE(di) == &PyDictIterValue_Type)
3077 element = dictiter_iternextvalue(&tmp);
3078 else
3079 assert(0);
3080 if (element) {
3081 if (PyList_Append(list, element)) {
3082 Py_DECREF(element);
3083 Py_DECREF(list);
3084 Py_XDECREF(tmp.di_dict);
3085 return NULL;
3086 }
3087 Py_DECREF(element);
3088 } else
3089 break;
3090 }
3091 Py_XDECREF(tmp.di_dict);
3092 /* check for error */
3093 if (tmp.di_dict != NULL) {
3094 /* we have an error */
3095 Py_DECREF(list);
3096 return NULL;
3097 }
Antoine Pitroua7013882012-04-05 00:04:20 +02003098 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003099}
3100
Guido van Rossum3ac67412007-02-10 18:55:06 +00003101/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003102/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003103/***********************************************/
3104
Guido van Rossumb90c8482007-02-10 01:11:45 +00003105/* The instance lay-out is the same for all three; but the type differs. */
3106
3107typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003108 PyObject_HEAD
3109 PyDictObject *dv_dict;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003110} dictviewobject;
3111
3112
3113static void
Guido van Rossum3ac67412007-02-10 18:55:06 +00003114dictview_dealloc(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003115{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003116 Py_XDECREF(dv->dv_dict);
3117 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003118}
3119
3120static int
3121dictview_traverse(dictviewobject *dv, visitproc visit, void *arg)
3122{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003123 Py_VISIT(dv->dv_dict);
3124 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003125}
3126
Guido van Rossum83825ac2007-02-10 04:54:19 +00003127static Py_ssize_t
Guido van Rossum3ac67412007-02-10 18:55:06 +00003128dictview_len(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003129{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003130 Py_ssize_t len = 0;
3131 if (dv->dv_dict != NULL)
3132 len = dv->dv_dict->ma_used;
3133 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003134}
3135
3136static PyObject *
3137dictview_new(PyObject *dict, PyTypeObject *type)
3138{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003139 dictviewobject *dv;
3140 if (dict == NULL) {
3141 PyErr_BadInternalCall();
3142 return NULL;
3143 }
3144 if (!PyDict_Check(dict)) {
3145 /* XXX Get rid of this restriction later */
3146 PyErr_Format(PyExc_TypeError,
3147 "%s() requires a dict argument, not '%s'",
3148 type->tp_name, dict->ob_type->tp_name);
3149 return NULL;
3150 }
3151 dv = PyObject_GC_New(dictviewobject, type);
3152 if (dv == NULL)
3153 return NULL;
3154 Py_INCREF(dict);
3155 dv->dv_dict = (PyDictObject *)dict;
3156 _PyObject_GC_TRACK(dv);
3157 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003158}
3159
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003160/* TODO(guido): The views objects are not complete:
3161
3162 * support more set operations
3163 * support arbitrary mappings?
3164 - either these should be static or exported in dictobject.h
3165 - if public then they should probably be in builtins
3166*/
3167
Guido van Rossumaac530c2007-08-24 22:33:45 +00003168/* Return 1 if self is a subset of other, iterating over self;
3169 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003170static int
3171all_contained_in(PyObject *self, PyObject *other)
3172{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003173 PyObject *iter = PyObject_GetIter(self);
3174 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003175
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003176 if (iter == NULL)
3177 return -1;
3178 for (;;) {
3179 PyObject *next = PyIter_Next(iter);
3180 if (next == NULL) {
3181 if (PyErr_Occurred())
3182 ok = -1;
3183 break;
3184 }
3185 ok = PySequence_Contains(other, next);
3186 Py_DECREF(next);
3187 if (ok <= 0)
3188 break;
3189 }
3190 Py_DECREF(iter);
3191 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003192}
3193
3194static PyObject *
3195dictview_richcompare(PyObject *self, PyObject *other, int op)
3196{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003197 Py_ssize_t len_self, len_other;
3198 int ok;
3199 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003201 assert(self != NULL);
3202 assert(PyDictViewSet_Check(self));
3203 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00003204
Brian Curtindfc80e32011-08-10 20:28:54 -05003205 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
3206 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003208 len_self = PyObject_Size(self);
3209 if (len_self < 0)
3210 return NULL;
3211 len_other = PyObject_Size(other);
3212 if (len_other < 0)
3213 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003215 ok = 0;
3216 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00003217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003218 case Py_NE:
3219 case Py_EQ:
3220 if (len_self == len_other)
3221 ok = all_contained_in(self, other);
3222 if (op == Py_NE && ok >= 0)
3223 ok = !ok;
3224 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003226 case Py_LT:
3227 if (len_self < len_other)
3228 ok = all_contained_in(self, other);
3229 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003231 case Py_LE:
3232 if (len_self <= len_other)
3233 ok = all_contained_in(self, other);
3234 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003236 case Py_GT:
3237 if (len_self > len_other)
3238 ok = all_contained_in(other, self);
3239 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003241 case Py_GE:
3242 if (len_self >= len_other)
3243 ok = all_contained_in(other, self);
3244 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003246 }
3247 if (ok < 0)
3248 return NULL;
3249 result = ok ? Py_True : Py_False;
3250 Py_INCREF(result);
3251 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003252}
3253
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003254static PyObject *
3255dictview_repr(dictviewobject *dv)
3256{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003257 PyObject *seq;
3258 PyObject *result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003260 seq = PySequence_List((PyObject *)dv);
3261 if (seq == NULL)
3262 return NULL;
3263
3264 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
3265 Py_DECREF(seq);
3266 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003267}
3268
Guido van Rossum3ac67412007-02-10 18:55:06 +00003269/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003270
3271static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003272dictkeys_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003273{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003274 if (dv->dv_dict == NULL) {
3275 Py_RETURN_NONE;
3276 }
3277 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003278}
3279
3280static int
3281dictkeys_contains(dictviewobject *dv, PyObject *obj)
3282{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003283 if (dv->dv_dict == NULL)
3284 return 0;
3285 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003286}
3287
Guido van Rossum83825ac2007-02-10 04:54:19 +00003288static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003289 (lenfunc)dictview_len, /* sq_length */
3290 0, /* sq_concat */
3291 0, /* sq_repeat */
3292 0, /* sq_item */
3293 0, /* sq_slice */
3294 0, /* sq_ass_item */
3295 0, /* sq_ass_slice */
3296 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003297};
3298
Guido van Rossum523259b2007-08-24 23:41:22 +00003299static PyObject*
3300dictviews_sub(PyObject* self, PyObject *other)
3301{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003302 PyObject *result = PySet_New(self);
3303 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003304 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003306 if (result == NULL)
3307 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003308
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003309 tmp = _PyObject_CallMethodId(result, &PyId_difference_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003310 if (tmp == NULL) {
3311 Py_DECREF(result);
3312 return NULL;
3313 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003315 Py_DECREF(tmp);
3316 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003317}
3318
3319static PyObject*
3320dictviews_and(PyObject* self, PyObject *other)
3321{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003322 PyObject *result = PySet_New(self);
3323 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003324 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003326 if (result == NULL)
3327 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003328
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003329 tmp = _PyObject_CallMethodId(result, &PyId_intersection_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003330 if (tmp == NULL) {
3331 Py_DECREF(result);
3332 return NULL;
3333 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003335 Py_DECREF(tmp);
3336 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003337}
3338
3339static PyObject*
3340dictviews_or(PyObject* self, PyObject *other)
3341{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003342 PyObject *result = PySet_New(self);
3343 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003344 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003346 if (result == NULL)
3347 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003348
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003349 tmp = _PyObject_CallMethodId(result, &PyId_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003350 if (tmp == NULL) {
3351 Py_DECREF(result);
3352 return NULL;
3353 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003355 Py_DECREF(tmp);
3356 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003357}
3358
3359static PyObject*
3360dictviews_xor(PyObject* self, PyObject *other)
3361{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003362 PyObject *result = PySet_New(self);
3363 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003364 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003366 if (result == NULL)
3367 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003368
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003369 tmp = _PyObject_CallMethodId(result, &PyId_symmetric_difference_update, "O",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003370 other);
3371 if (tmp == NULL) {
3372 Py_DECREF(result);
3373 return NULL;
3374 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003376 Py_DECREF(tmp);
3377 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003378}
3379
3380static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003381 0, /*nb_add*/
3382 (binaryfunc)dictviews_sub, /*nb_subtract*/
3383 0, /*nb_multiply*/
3384 0, /*nb_remainder*/
3385 0, /*nb_divmod*/
3386 0, /*nb_power*/
3387 0, /*nb_negative*/
3388 0, /*nb_positive*/
3389 0, /*nb_absolute*/
3390 0, /*nb_bool*/
3391 0, /*nb_invert*/
3392 0, /*nb_lshift*/
3393 0, /*nb_rshift*/
3394 (binaryfunc)dictviews_and, /*nb_and*/
3395 (binaryfunc)dictviews_xor, /*nb_xor*/
3396 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00003397};
3398
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003399static PyObject*
3400dictviews_isdisjoint(PyObject *self, PyObject *other)
3401{
3402 PyObject *it;
3403 PyObject *item = NULL;
3404
3405 if (self == other) {
3406 if (dictview_len((dictviewobject *)self) == 0)
3407 Py_RETURN_TRUE;
3408 else
3409 Py_RETURN_FALSE;
3410 }
3411
3412 /* Iterate over the shorter object (only if other is a set,
3413 * because PySequence_Contains may be expensive otherwise): */
3414 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
3415 Py_ssize_t len_self = dictview_len((dictviewobject *)self);
3416 Py_ssize_t len_other = PyObject_Size(other);
3417 if (len_other == -1)
3418 return NULL;
3419
3420 if ((len_other > len_self)) {
3421 PyObject *tmp = other;
3422 other = self;
3423 self = tmp;
3424 }
3425 }
3426
3427 it = PyObject_GetIter(other);
3428 if (it == NULL)
3429 return NULL;
3430
3431 while ((item = PyIter_Next(it)) != NULL) {
3432 int contains = PySequence_Contains(self, item);
3433 Py_DECREF(item);
3434 if (contains == -1) {
3435 Py_DECREF(it);
3436 return NULL;
3437 }
3438
3439 if (contains) {
3440 Py_DECREF(it);
3441 Py_RETURN_FALSE;
3442 }
3443 }
3444 Py_DECREF(it);
3445 if (PyErr_Occurred())
3446 return NULL; /* PyIter_Next raised an exception. */
3447 Py_RETURN_TRUE;
3448}
3449
3450PyDoc_STRVAR(isdisjoint_doc,
3451"Return True if the view and the given iterable have a null intersection.");
3452
Guido van Rossumb90c8482007-02-10 01:11:45 +00003453static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003454 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3455 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003456 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003457};
3458
3459PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003460 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3461 "dict_keys", /* tp_name */
3462 sizeof(dictviewobject), /* tp_basicsize */
3463 0, /* tp_itemsize */
3464 /* methods */
3465 (destructor)dictview_dealloc, /* tp_dealloc */
3466 0, /* tp_print */
3467 0, /* tp_getattr */
3468 0, /* tp_setattr */
3469 0, /* tp_reserved */
3470 (reprfunc)dictview_repr, /* tp_repr */
3471 &dictviews_as_number, /* tp_as_number */
3472 &dictkeys_as_sequence, /* tp_as_sequence */
3473 0, /* tp_as_mapping */
3474 0, /* tp_hash */
3475 0, /* tp_call */
3476 0, /* tp_str */
3477 PyObject_GenericGetAttr, /* tp_getattro */
3478 0, /* tp_setattro */
3479 0, /* tp_as_buffer */
3480 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3481 0, /* tp_doc */
3482 (traverseproc)dictview_traverse, /* tp_traverse */
3483 0, /* tp_clear */
3484 dictview_richcompare, /* tp_richcompare */
3485 0, /* tp_weaklistoffset */
3486 (getiterfunc)dictkeys_iter, /* tp_iter */
3487 0, /* tp_iternext */
3488 dictkeys_methods, /* tp_methods */
3489 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003490};
3491
3492static PyObject *
3493dictkeys_new(PyObject *dict)
3494{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003495 return dictview_new(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003496}
3497
Guido van Rossum3ac67412007-02-10 18:55:06 +00003498/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003499
3500static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003501dictitems_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003502{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003503 if (dv->dv_dict == NULL) {
3504 Py_RETURN_NONE;
3505 }
3506 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003507}
3508
3509static int
3510dictitems_contains(dictviewobject *dv, PyObject *obj)
3511{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003512 PyObject *key, *value, *found;
3513 if (dv->dv_dict == NULL)
3514 return 0;
3515 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
3516 return 0;
3517 key = PyTuple_GET_ITEM(obj, 0);
3518 value = PyTuple_GET_ITEM(obj, 1);
3519 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
3520 if (found == NULL) {
3521 if (PyErr_Occurred())
3522 return -1;
3523 return 0;
3524 }
3525 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003526}
3527
Guido van Rossum83825ac2007-02-10 04:54:19 +00003528static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003529 (lenfunc)dictview_len, /* sq_length */
3530 0, /* sq_concat */
3531 0, /* sq_repeat */
3532 0, /* sq_item */
3533 0, /* sq_slice */
3534 0, /* sq_ass_item */
3535 0, /* sq_ass_slice */
3536 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003537};
3538
Guido van Rossumb90c8482007-02-10 01:11:45 +00003539static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003540 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3541 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003542 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003543};
3544
3545PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003546 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3547 "dict_items", /* tp_name */
3548 sizeof(dictviewobject), /* tp_basicsize */
3549 0, /* tp_itemsize */
3550 /* methods */
3551 (destructor)dictview_dealloc, /* tp_dealloc */
3552 0, /* tp_print */
3553 0, /* tp_getattr */
3554 0, /* tp_setattr */
3555 0, /* tp_reserved */
3556 (reprfunc)dictview_repr, /* tp_repr */
3557 &dictviews_as_number, /* tp_as_number */
3558 &dictitems_as_sequence, /* tp_as_sequence */
3559 0, /* tp_as_mapping */
3560 0, /* tp_hash */
3561 0, /* tp_call */
3562 0, /* tp_str */
3563 PyObject_GenericGetAttr, /* tp_getattro */
3564 0, /* tp_setattro */
3565 0, /* tp_as_buffer */
3566 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3567 0, /* tp_doc */
3568 (traverseproc)dictview_traverse, /* tp_traverse */
3569 0, /* tp_clear */
3570 dictview_richcompare, /* tp_richcompare */
3571 0, /* tp_weaklistoffset */
3572 (getiterfunc)dictitems_iter, /* tp_iter */
3573 0, /* tp_iternext */
3574 dictitems_methods, /* tp_methods */
3575 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003576};
3577
3578static PyObject *
3579dictitems_new(PyObject *dict)
3580{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003581 return dictview_new(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003582}
3583
Guido van Rossum3ac67412007-02-10 18:55:06 +00003584/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003585
3586static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003587dictvalues_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003588{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003589 if (dv->dv_dict == NULL) {
3590 Py_RETURN_NONE;
3591 }
3592 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003593}
3594
Guido van Rossum83825ac2007-02-10 04:54:19 +00003595static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003596 (lenfunc)dictview_len, /* sq_length */
3597 0, /* sq_concat */
3598 0, /* sq_repeat */
3599 0, /* sq_item */
3600 0, /* sq_slice */
3601 0, /* sq_ass_item */
3602 0, /* sq_ass_slice */
3603 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003604};
3605
Guido van Rossumb90c8482007-02-10 01:11:45 +00003606static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003607 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003608};
3609
3610PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003611 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3612 "dict_values", /* tp_name */
3613 sizeof(dictviewobject), /* tp_basicsize */
3614 0, /* tp_itemsize */
3615 /* methods */
3616 (destructor)dictview_dealloc, /* tp_dealloc */
3617 0, /* tp_print */
3618 0, /* tp_getattr */
3619 0, /* tp_setattr */
3620 0, /* tp_reserved */
3621 (reprfunc)dictview_repr, /* tp_repr */
3622 0, /* tp_as_number */
3623 &dictvalues_as_sequence, /* tp_as_sequence */
3624 0, /* tp_as_mapping */
3625 0, /* tp_hash */
3626 0, /* tp_call */
3627 0, /* tp_str */
3628 PyObject_GenericGetAttr, /* tp_getattro */
3629 0, /* tp_setattro */
3630 0, /* tp_as_buffer */
3631 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3632 0, /* tp_doc */
3633 (traverseproc)dictview_traverse, /* tp_traverse */
3634 0, /* tp_clear */
3635 0, /* tp_richcompare */
3636 0, /* tp_weaklistoffset */
3637 (getiterfunc)dictvalues_iter, /* tp_iter */
3638 0, /* tp_iternext */
3639 dictvalues_methods, /* tp_methods */
3640 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003641};
3642
3643static PyObject *
3644dictvalues_new(PyObject *dict)
3645{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003646 return dictview_new(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003647}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003648
3649/* Returns NULL if cannot allocate a new PyDictKeysObject,
3650 but does not set an error */
3651PyDictKeysObject *
3652_PyDict_NewKeysForClass(void)
3653{
3654 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE_SPLIT);
3655 if (keys == NULL)
3656 PyErr_Clear();
3657 else
3658 keys->dk_lookup = lookdict_split;
3659 return keys;
3660}
3661
3662#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
3663
3664PyObject *
3665PyObject_GenericGetDict(PyObject *obj, void *context)
3666{
3667 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
3668 if (dictptr == NULL) {
3669 PyErr_SetString(PyExc_AttributeError,
3670 "This object has no __dict__");
3671 return NULL;
3672 }
3673 dict = *dictptr;
3674 if (dict == NULL) {
3675 PyTypeObject *tp = Py_TYPE(obj);
3676 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
3677 DK_INCREF(CACHED_KEYS(tp));
3678 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
3679 }
3680 else {
3681 *dictptr = dict = PyDict_New();
3682 }
3683 }
3684 Py_XINCREF(dict);
3685 return dict;
3686}
3687
3688int
3689_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
3690 PyObject *key, PyObject *value)
3691{
3692 PyObject *dict;
3693 int res;
3694 PyDictKeysObject *cached;
3695
3696 assert(dictptr != NULL);
3697 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
3698 assert(dictptr != NULL);
3699 dict = *dictptr;
3700 if (dict == NULL) {
3701 DK_INCREF(cached);
3702 dict = new_dict_with_shared_keys(cached);
3703 if (dict == NULL)
3704 return -1;
3705 *dictptr = dict;
3706 }
3707 if (value == NULL) {
3708 res = PyDict_DelItem(dict, key);
3709 if (cached != ((PyDictObject *)dict)->ma_keys) {
3710 CACHED_KEYS(tp) = NULL;
3711 DK_DECREF(cached);
3712 }
3713 } else {
3714 res = PyDict_SetItem(dict, key, value);
3715 if (cached != ((PyDictObject *)dict)->ma_keys) {
3716 /* Either update tp->ht_cached_keys or delete it */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04003717 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003718 CACHED_KEYS(tp) = make_keys_shared(dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003719 } else {
3720 CACHED_KEYS(tp) = NULL;
3721 }
3722 DK_DECREF(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04003723 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
3724 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003725 }
3726 }
3727 } else {
3728 dict = *dictptr;
3729 if (dict == NULL) {
3730 dict = PyDict_New();
3731 if (dict == NULL)
3732 return -1;
3733 *dictptr = dict;
3734 }
3735 if (value == NULL) {
3736 res = PyDict_DelItem(dict, key);
3737 } else {
3738 res = PyDict_SetItem(dict, key, value);
3739 }
3740 }
3741 return res;
3742}
3743
3744void
3745_PyDictKeys_DecRef(PyDictKeysObject *keys)
3746{
3747 DK_DECREF(keys);
3748}
3749
3750
3751/* ARGSUSED */
3752static PyObject *
3753dummy_repr(PyObject *op)
3754{
3755 return PyUnicode_FromString("<dummy key>");
3756}
3757
3758/* ARGUSED */
3759static void
3760dummy_dealloc(PyObject* ignore)
3761{
3762 /* This should never get called, but we also don't want to SEGV if
3763 * we accidentally decref dummy-key out of existence.
3764 */
3765 Py_FatalError("deallocating <dummy key>");
3766}
3767
3768static PyTypeObject PyDictDummy_Type = {
3769 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3770 "<dummy key> type",
3771 0,
3772 0,
3773 dummy_dealloc, /*tp_dealloc*/ /*never called*/
3774 0, /*tp_print*/
3775 0, /*tp_getattr*/
3776 0, /*tp_setattr*/
3777 0, /*tp_reserved*/
3778 dummy_repr, /*tp_repr*/
3779 0, /*tp_as_number*/
3780 0, /*tp_as_sequence*/
3781 0, /*tp_as_mapping*/
3782 0, /*tp_hash */
3783 0, /*tp_call */
3784 0, /*tp_str */
3785 0, /*tp_getattro */
3786 0, /*tp_setattro */
3787 0, /*tp_as_buffer */
3788 Py_TPFLAGS_DEFAULT, /*tp_flags */
3789};
3790
3791static PyObject _dummy_struct = {
3792 _PyObject_EXTRA_INIT
3793 2, &PyDictDummy_Type
3794};
3795