blob: b5cbfb1f253cb954c87df4a39044a4d00c253008 [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
Victor Stinnera9f61a52013-07-16 22:17:26 +0200308/* 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,
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700311 * 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;
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200392 assert(keys != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 if (numfree) {
394 mp = free_list[--numfree];
395 assert (mp != NULL);
396 assert (Py_TYPE(mp) == &PyDict_Type);
397 _Py_NewReference((PyObject *)mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400399 else {
400 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
401 if (mp == NULL) {
402 DK_DECREF(keys);
403 free_values(values);
404 return NULL;
405 }
406 }
407 mp->ma_keys = keys;
408 mp->ma_values = values;
409 mp->ma_used = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000411}
412
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400413/* Consumes a reference to the keys object */
414static PyObject *
415new_dict_with_shared_keys(PyDictKeysObject *keys)
416{
417 PyObject **values;
418 Py_ssize_t i, size;
419
420 size = DK_SIZE(keys);
421 values = new_values(size);
422 if (values == NULL) {
423 DK_DECREF(keys);
424 return PyErr_NoMemory();
425 }
426 for (i = 0; i < size; i++) {
427 values[i] = NULL;
428 }
429 return new_dict(keys, values);
430}
431
432PyObject *
433PyDict_New(void)
434{
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200435 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE_COMBINED);
436 if (keys == NULL)
437 return NULL;
438 return new_dict(keys, NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400439}
440
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000441/*
442The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000443This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000444Open addressing is preferred over chaining since the link overhead for
445chaining would be substantial (100% with typical malloc overhead).
446
Tim Peterseb28ef22001-06-02 05:27:19 +0000447The initial probe index is computed as hash mod the table size. Subsequent
448probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000449
450All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000451
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000452The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000453contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000454Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000455
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000456lookdict() is general-purpose, and may return NULL if (and only if) a
457comparison raises an exception (this was new in Python 2.5).
Guido van Rossum89d8c602007-09-18 17:26:56 +0000458lookdict_unicode() below is specialized to string keys, comparison of which can
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400459never raise an exception; that function can never return NULL.
460lookdict_unicode_nodummy is further specialized for string keys that cannot be
461the <dummy> value.
462For both, when the key isn't found a PyDictEntry* is returned
463where the key would have been found, *value_addr points to the matching value
464slot.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000465*/
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400466static PyDictKeyEntry *
467lookdict(PyDictObject *mp, PyObject *key,
468 Py_hash_t hash, PyObject ***value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000469{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200470 size_t i;
471 size_t perturb;
472 PyDictKeyEntry *freeslot;
473 size_t mask;
Antoine Pitrou9a234902012-05-13 20:48:01 +0200474 PyDictKeyEntry *ep0;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200475 PyDictKeyEntry *ep;
476 int cmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000478
Antoine Pitrou9a234902012-05-13 20:48:01 +0200479top:
480 mask = DK_MASK(mp->ma_keys);
481 ep0 = &mp->ma_keys->dk_entries[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 i = (size_t)hash & mask;
483 ep = &ep0[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400484 if (ep->me_key == NULL || ep->me_key == key) {
485 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400487 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 if (ep->me_key == dummy)
489 freeslot = ep;
490 else {
491 if (ep->me_hash == hash) {
492 startkey = ep->me_key;
493 Py_INCREF(startkey);
494 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
495 Py_DECREF(startkey);
496 if (cmp < 0)
497 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400498 if (ep0 == mp->ma_keys->dk_entries && ep->me_key == startkey) {
499 if (cmp > 0) {
500 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400502 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 }
504 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200505 /* The dict was mutated, restart */
506 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 }
508 }
509 freeslot = NULL;
510 }
Tim Peters15d49292001-05-27 07:39:22 +0000511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 /* In the loop, me_key == dummy is by far (factor of 100s) the
513 least likely outcome, so test for that last. */
514 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
515 i = (i << 2) + i + perturb + 1;
516 ep = &ep0[i & mask];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400517 if (ep->me_key == NULL) {
518 if (freeslot == NULL) {
519 *value_addr = &ep->me_value;
520 return ep;
521 } else {
522 *value_addr = &freeslot->me_value;
523 return freeslot;
524 }
525 }
526 if (ep->me_key == key) {
527 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400529 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 if (ep->me_hash == hash && ep->me_key != dummy) {
531 startkey = ep->me_key;
532 Py_INCREF(startkey);
533 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
534 Py_DECREF(startkey);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400535 if (cmp < 0) {
536 *value_addr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400538 }
539 if (ep0 == mp->ma_keys->dk_entries && ep->me_key == startkey) {
540 if (cmp > 0) {
541 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400543 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 }
545 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200546 /* The dict was mutated, restart */
547 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 }
549 }
550 else if (ep->me_key == dummy && freeslot == NULL)
551 freeslot = ep;
552 }
553 assert(0); /* NOT REACHED */
554 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000555}
556
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400557/* Specialized version for string-only keys */
558static PyDictKeyEntry *
559lookdict_unicode(PyDictObject *mp, PyObject *key,
560 Py_hash_t hash, PyObject ***value_addr)
Fred Drake1bff34a2000-08-31 19:31:38 +0000561{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200562 size_t i;
563 size_t perturb;
564 PyDictKeyEntry *freeslot;
565 size_t mask = DK_MASK(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400566 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200567 PyDictKeyEntry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 /* Make sure this function doesn't have to handle non-unicode keys,
570 including subclasses of str; e.g., one reason to subclass
571 unicodes is to override __eq__, and for speed we don't cater to
572 that here. */
573 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400574 mp->ma_keys->dk_lookup = lookdict;
575 return lookdict(mp, key, hash, value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 }
Mark Dickinson57e683e2011-09-24 18:18:40 +0100577 i = (size_t)hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 ep = &ep0[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400579 if (ep->me_key == NULL || ep->me_key == key) {
580 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400582 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 if (ep->me_key == dummy)
584 freeslot = ep;
585 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400586 if (ep->me_hash == hash && unicode_eq(ep->me_key, key)) {
587 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400589 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 freeslot = NULL;
591 }
Tim Peters15d49292001-05-27 07:39:22 +0000592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 /* In the loop, me_key == dummy is by far (factor of 100s) the
594 least likely outcome, so test for that last. */
595 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
596 i = (i << 2) + i + perturb + 1;
597 ep = &ep0[i & mask];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400598 if (ep->me_key == NULL) {
599 if (freeslot == NULL) {
600 *value_addr = &ep->me_value;
601 return ep;
602 } else {
603 *value_addr = &freeslot->me_value;
604 return freeslot;
605 }
606 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 if (ep->me_key == key
608 || (ep->me_hash == hash
609 && ep->me_key != dummy
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400610 && unicode_eq(ep->me_key, key))) {
611 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400613 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 if (ep->me_key == dummy && freeslot == NULL)
615 freeslot = ep;
616 }
617 assert(0); /* NOT REACHED */
618 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000619}
620
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400621/* Faster version of lookdict_unicode when it is known that no <dummy> keys
622 * will be present. */
623static PyDictKeyEntry *
624lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
625 Py_hash_t hash, PyObject ***value_addr)
626{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200627 size_t i;
628 size_t perturb;
629 size_t mask = DK_MASK(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400630 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200631 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400632
633 /* Make sure this function doesn't have to handle non-unicode keys,
634 including subclasses of str; e.g., one reason to subclass
635 unicodes is to override __eq__, and for speed we don't cater to
636 that here. */
637 if (!PyUnicode_CheckExact(key)) {
638 mp->ma_keys->dk_lookup = lookdict;
639 return lookdict(mp, key, hash, value_addr);
640 }
641 i = (size_t)hash & mask;
642 ep = &ep0[i];
643 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
644 if (ep->me_key == NULL || ep->me_key == key ||
645 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
646 *value_addr = &ep->me_value;
647 return ep;
648 }
649 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
650 i = (i << 2) + i + perturb + 1;
651 ep = &ep0[i & mask];
652 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
653 if (ep->me_key == NULL || ep->me_key == key ||
654 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
655 *value_addr = &ep->me_value;
656 return ep;
657 }
658 }
659 assert(0); /* NOT REACHED */
660 return 0;
661}
662
663/* Version of lookdict for split tables.
664 * All split tables and only split tables use this lookup function.
665 * Split tables only contain unicode keys and no dummy keys,
666 * so algorithm is the same as lookdict_unicode_nodummy.
667 */
668static PyDictKeyEntry *
669lookdict_split(PyDictObject *mp, PyObject *key,
670 Py_hash_t hash, PyObject ***value_addr)
671{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200672 size_t i;
673 size_t perturb;
674 size_t mask = DK_MASK(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400675 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200676 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400677
678 if (!PyUnicode_CheckExact(key)) {
Benjamin Petersondb780d02012-04-23 13:44:32 -0400679 ep = lookdict(mp, key, hash, value_addr);
680 /* lookdict expects a combined-table, so fix value_addr */
681 i = ep - ep0;
682 *value_addr = &mp->ma_values[i];
683 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400684 }
685 i = (size_t)hash & mask;
686 ep = &ep0[i];
687 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
688 if (ep->me_key == NULL || ep->me_key == key ||
689 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
690 *value_addr = &mp->ma_values[i];
691 return ep;
692 }
693 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
694 i = (i << 2) + i + perturb + 1;
695 ep = &ep0[i & mask];
696 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
697 if (ep->me_key == NULL || ep->me_key == key ||
698 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
699 *value_addr = &mp->ma_values[i & mask];
700 return ep;
701 }
702 }
703 assert(0); /* NOT REACHED */
704 return 0;
705}
706
Benjamin Petersonfb886362010-04-24 18:21:17 +0000707int
708_PyDict_HasOnlyStringKeys(PyObject *dict)
709{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 Py_ssize_t pos = 0;
711 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000712 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400714 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 return 1;
716 while (PyDict_Next(dict, &pos, &key, &value))
717 if (!PyUnicode_Check(key))
718 return 0;
719 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000720}
721
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000722#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 do { \
724 if (!_PyObject_GC_IS_TRACKED(mp)) { \
725 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
726 _PyObject_GC_MAY_BE_TRACKED(value)) { \
727 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 } \
729 } \
730 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000731
732void
733_PyDict_MaybeUntrack(PyObject *op)
734{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 PyDictObject *mp;
736 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400737 Py_ssize_t i, size;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
740 return;
741
742 mp = (PyDictObject *) op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400743 size = DK_SIZE(mp->ma_keys);
744 if (_PyDict_HasSplitTable(mp)) {
745 for (i = 0; i < size; i++) {
746 if ((value = mp->ma_values[i]) == NULL)
747 continue;
748 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
749 assert(!_PyObject_GC_MAY_BE_TRACKED(
750 mp->ma_keys->dk_entries[i].me_key));
751 return;
752 }
753 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400755 else {
756 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
757 for (i = 0; i < size; i++) {
758 if ((value = ep0[i].me_value) == NULL)
759 continue;
760 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
761 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
762 return;
763 }
764 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000766}
767
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400768/* Internal function to find slot for an item from its hash
769 * when it is known that the key is not present in the dict.
770 */
771static PyDictKeyEntry *
772find_empty_slot(PyDictObject *mp, PyObject *key, Py_hash_t hash,
773 PyObject ***value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000774{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400775 size_t i;
776 size_t perturb;
777 size_t mask = DK_MASK(mp->ma_keys);
778 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
779 PyDictKeyEntry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000780
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400781 assert(key != NULL);
782 if (!PyUnicode_CheckExact(key))
783 mp->ma_keys->dk_lookup = lookdict;
784 i = hash & mask;
785 ep = &ep0[i];
786 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
787 i = (i << 2) + i + perturb + 1;
788 ep = &ep0[i & mask];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400790 assert(ep->me_value == NULL);
791 if (mp->ma_values)
792 *value_addr = &mp->ma_values[i & mask];
793 else
794 *value_addr = &ep->me_value;
795 return ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000796}
797
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400798static int
799insertion_resize(PyDictObject *mp)
800{
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700801 return dictresize(mp, GROWTH_RATE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400802}
Antoine Pitroue965d972012-02-27 00:45:12 +0100803
804/*
805Internal routine to insert a new item into the table.
806Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +0100807Returns -1 if an error occurred, or 0 on success.
808*/
809static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400810insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +0100811{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400812 PyObject *old_value;
813 PyObject **value_addr;
814 PyDictKeyEntry *ep;
815 assert(key != dummy);
Antoine Pitroue965d972012-02-27 00:45:12 +0100816
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400817 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
818 if (insertion_resize(mp) < 0)
819 return -1;
820 }
821
822 ep = mp->ma_keys->dk_lookup(mp, key, hash, &value_addr);
Antoine Pitroue965d972012-02-27 00:45:12 +0100823 if (ep == NULL) {
Antoine Pitroue965d972012-02-27 00:45:12 +0100824 return -1;
825 }
Benjamin Petersona6f195e2012-04-30 10:23:40 -0400826 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400827 MAINTAIN_TRACKING(mp, key, value);
828 old_value = *value_addr;
829 if (old_value != NULL) {
830 assert(ep->me_key != NULL && ep->me_key != dummy);
831 *value_addr = value;
832 Py_DECREF(old_value); /* which **CAN** re-enter */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400833 }
834 else {
835 if (ep->me_key == NULL) {
Benjamin Petersona6f195e2012-04-30 10:23:40 -0400836 Py_INCREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400837 if (mp->ma_keys->dk_usable <= 0) {
838 /* Need to resize. */
839 if (insertion_resize(mp) < 0) {
840 Py_DECREF(key);
841 Py_DECREF(value);
842 return -1;
843 }
844 ep = find_empty_slot(mp, key, hash, &value_addr);
845 }
846 mp->ma_keys->dk_usable--;
847 assert(mp->ma_keys->dk_usable >= 0);
848 ep->me_key = key;
849 ep->me_hash = hash;
850 }
851 else {
852 if (ep->me_key == dummy) {
Benjamin Petersona6f195e2012-04-30 10:23:40 -0400853 Py_INCREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400854 ep->me_key = key;
855 ep->me_hash = hash;
856 Py_DECREF(dummy);
857 } else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400858 assert(_PyDict_HasSplitTable(mp));
859 }
860 }
861 mp->ma_used++;
862 *value_addr = value;
863 }
864 assert(ep->me_key != NULL && ep->me_key != dummy);
865 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
866 return 0;
Antoine Pitroue965d972012-02-27 00:45:12 +0100867}
868
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000869/*
870Internal routine used by dictresize() to insert an item which is
871known to be absent from the dict. This routine also assumes that
872the dict contains no deleted entries. Besides the performance benefit,
873using insertdict() in dictresize() is dangerous (SF bug #1456209).
874Note that no refcounts are changed by this routine; if needed, the caller
875is responsible for incref'ing `key` and `value`.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400876Neither mp->ma_used nor k->dk_usable are modified by this routine; the caller
877must set them correctly
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000878*/
879static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400880insertdict_clean(PyDictObject *mp, PyObject *key, Py_hash_t hash,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 PyObject *value)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000882{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400883 size_t i;
884 size_t perturb;
885 PyDictKeysObject *k = mp->ma_keys;
886 size_t mask = (size_t)DK_SIZE(k)-1;
887 PyDictKeyEntry *ep0 = &k->dk_entries[0];
888 PyDictKeyEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000889
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400890 assert(k->dk_lookup != NULL);
891 assert(value != NULL);
892 assert(key != NULL);
893 assert(key != dummy);
894 assert(PyUnicode_CheckExact(key) || k->dk_lookup == lookdict);
895 i = hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 ep = &ep0[i];
897 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
898 i = (i << 2) + i + perturb + 1;
899 ep = &ep0[i & mask];
900 }
901 assert(ep->me_value == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 ep->me_key = key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000903 ep->me_hash = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000905}
906
907/*
908Restructure the table by allocating a new table and reinserting all
909items again. When entries have been deleted, the new table may
910actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400911If a table is split (its keys and hashes are shared, its values are not),
912then the values are temporarily copied into the table, it is resized as
913a combined table, then the me_value slots in the old table are NULLed out.
914After resizing a table is always combined,
915but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000916*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000917static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000918dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000919{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 Py_ssize_t newsize;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400921 PyDictKeysObject *oldkeys;
922 PyObject **oldvalues;
923 Py_ssize_t i, oldsize;
Tim Peters91a364d2001-05-19 07:04:38 +0000924
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400925/* Find the smallest table size > minused. */
926 for (newsize = PyDict_MINSIZE_COMBINED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 newsize <= minused && newsize > 0;
928 newsize <<= 1)
929 ;
930 if (newsize <= 0) {
931 PyErr_NoMemory();
932 return -1;
933 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400934 oldkeys = mp->ma_keys;
935 oldvalues = mp->ma_values;
936 /* Allocate a new table. */
937 mp->ma_keys = new_keys_object(newsize);
938 if (mp->ma_keys == NULL) {
939 mp->ma_keys = oldkeys;
940 return -1;
941 }
942 if (oldkeys->dk_lookup == lookdict)
943 mp->ma_keys->dk_lookup = lookdict;
944 oldsize = DK_SIZE(oldkeys);
945 mp->ma_values = NULL;
946 /* If empty then nothing to copy so just return */
947 if (oldsize == 1) {
948 assert(oldkeys == Py_EMPTY_KEYS);
949 DK_DECREF(oldkeys);
950 return 0;
951 }
952 /* Main loop below assumes we can transfer refcount to new keys
953 * and that value is stored in me_value.
954 * Increment ref-counts and copy values here to compensate
955 * This (resizing a split table) should be relatively rare */
956 if (oldvalues != NULL) {
957 for (i = 0; i < oldsize; i++) {
958 if (oldvalues[i] != NULL) {
959 Py_INCREF(oldkeys->dk_entries[i].me_key);
960 oldkeys->dk_entries[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 }
963 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400964 /* Main loop */
965 for (i = 0; i < oldsize; i++) {
966 PyDictKeyEntry *ep = &oldkeys->dk_entries[i];
967 if (ep->me_value != NULL) {
968 assert(ep->me_key != dummy);
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000969 insertdict_clean(mp, ep->me_key, ep->me_hash, ep->me_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400972 mp->ma_keys->dk_usable -= mp->ma_used;
973 if (oldvalues != NULL) {
974 /* NULL out me_value slot in oldkeys, in case it was shared */
975 for (i = 0; i < oldsize; i++)
976 oldkeys->dk_entries[i].me_value = NULL;
977 assert(oldvalues != empty_values);
978 free_values(oldvalues);
979 DK_DECREF(oldkeys);
980 }
981 else {
982 assert(oldkeys->dk_lookup != lookdict_split);
983 if (oldkeys->dk_lookup != lookdict_unicode_nodummy) {
984 PyDictKeyEntry *ep0 = &oldkeys->dk_entries[0];
985 for (i = 0; i < oldsize; i++) {
986 if (ep0[i].me_key == dummy)
987 Py_DECREF(dummy);
988 }
989 }
990 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200991 DK_DEBUG_DECREF PyMem_FREE(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400992 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000994}
995
Benjamin Peterson15ee8212012-04-24 14:44:18 -0400996/* Returns NULL if unable to split table.
997 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400998static PyDictKeysObject *
999make_keys_shared(PyObject *op)
1000{
1001 Py_ssize_t i;
1002 Py_ssize_t size;
1003 PyDictObject *mp = (PyDictObject *)op;
1004
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001005 if (!PyDict_CheckExact(op))
1006 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001007 if (!_PyDict_HasSplitTable(mp)) {
1008 PyDictKeyEntry *ep0;
1009 PyObject **values;
1010 assert(mp->ma_keys->dk_refcnt == 1);
1011 if (mp->ma_keys->dk_lookup == lookdict) {
1012 return NULL;
1013 }
1014 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
1015 /* Remove dummy keys */
1016 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
1017 return NULL;
1018 }
1019 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
1020 /* Copy values into a new array */
1021 ep0 = &mp->ma_keys->dk_entries[0];
1022 size = DK_SIZE(mp->ma_keys);
1023 values = new_values(size);
1024 if (values == NULL) {
1025 PyErr_SetString(PyExc_MemoryError,
1026 "Not enough memory to allocate new values array");
1027 return NULL;
1028 }
1029 for (i = 0; i < size; i++) {
1030 values[i] = ep0[i].me_value;
1031 ep0[i].me_value = NULL;
1032 }
1033 mp->ma_keys->dk_lookup = lookdict_split;
1034 mp->ma_values = values;
1035 }
1036 DK_INCREF(mp->ma_keys);
1037 return mp->ma_keys;
1038}
Christian Heimes99170a52007-12-19 02:07:34 +00001039
1040PyObject *
1041_PyDict_NewPresized(Py_ssize_t minused)
1042{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001043 Py_ssize_t newsize;
1044 PyDictKeysObject *new_keys;
1045 for (newsize = PyDict_MINSIZE_COMBINED;
1046 newsize <= minused && newsize > 0;
1047 newsize <<= 1)
1048 ;
1049 new_keys = new_keys_object(newsize);
1050 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001052 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001053}
1054
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001055/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1056 * that may occur (originally dicts supported only string keys, and exceptions
1057 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001058 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001059 * (suppressed) occurred while computing the key's hash, or that some error
1060 * (suppressed) occurred when comparing keys in the dict's internal probe
1061 * sequence. A nasty example of the latter is when a Python-coded comparison
1062 * function hits a stack-depth error, which can cause this to return NULL
1063 * even if the key is present.
1064 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001065PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001066PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001067{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001068 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001070 PyDictKeyEntry *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 PyThreadState *tstate;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001072 PyObject **value_addr;
1073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 if (!PyDict_Check(op))
1075 return NULL;
1076 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001077 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 {
1079 hash = PyObject_Hash(key);
1080 if (hash == -1) {
1081 PyErr_Clear();
1082 return NULL;
1083 }
1084 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001085
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 /* We can arrive here with a NULL tstate during initialization: try
1087 running "python -Wi" for an example related to string interning.
1088 Let's just hope that no exception occurs then... This must be
1089 _PyThreadState_Current and not PyThreadState_GET() because in debug
1090 mode, the latter complains if tstate is NULL. */
1091 tstate = (PyThreadState*)_Py_atomic_load_relaxed(
1092 &_PyThreadState_Current);
1093 if (tstate != NULL && tstate->curexc_type != NULL) {
1094 /* preserve the existing exception */
1095 PyObject *err_type, *err_value, *err_tb;
1096 PyErr_Fetch(&err_type, &err_value, &err_tb);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001097 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 /* ignore errors */
1099 PyErr_Restore(err_type, err_value, err_tb);
1100 if (ep == NULL)
1101 return NULL;
1102 }
1103 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001104 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 if (ep == NULL) {
1106 PyErr_Clear();
1107 return NULL;
1108 }
1109 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001110 return *value_addr;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001111}
1112
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001113/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1114 This returns NULL *with* an exception set if an exception occurred.
1115 It returns NULL *without* an exception set if the key wasn't present.
1116*/
1117PyObject *
1118PyDict_GetItemWithError(PyObject *op, PyObject *key)
1119{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001120 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 PyDictObject*mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001122 PyDictKeyEntry *ep;
1123 PyObject **value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001124
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 if (!PyDict_Check(op)) {
1126 PyErr_BadInternalCall();
1127 return NULL;
1128 }
1129 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001130 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 {
1132 hash = PyObject_Hash(key);
1133 if (hash == -1) {
1134 return NULL;
1135 }
1136 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001137
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001138 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 if (ep == NULL)
1140 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001141 return *value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001142}
1143
Brett Cannonfd074152012-04-14 14:10:13 -04001144PyObject *
1145_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1146{
1147 PyObject *kv;
1148 kv = _PyUnicode_FromId(key); /* borrowed */
1149 if (kv == NULL)
1150 return NULL;
1151 return PyDict_GetItemWithError(dp, kv);
1152}
1153
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001154/* Fast version of global value lookup.
1155 * Lookup in globals, then builtins.
1156 */
1157PyObject *
1158_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001159{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001160 PyObject *x;
1161 if (PyUnicode_CheckExact(key)) {
1162 PyObject **value_addr;
1163 Py_hash_t hash = ((PyASCIIObject *)key)->hash;
1164 if (hash != -1) {
1165 PyDictKeyEntry *e;
1166 e = globals->ma_keys->dk_lookup(globals, key, hash, &value_addr);
1167 if (e == NULL) {
1168 return NULL;
1169 }
1170 x = *value_addr;
1171 if (x != NULL)
1172 return x;
1173 e = builtins->ma_keys->dk_lookup(builtins, key, hash, &value_addr);
1174 if (e == NULL) {
1175 return NULL;
1176 }
1177 x = *value_addr;
1178 return x;
1179 }
Antoine Pitroue965d972012-02-27 00:45:12 +01001180 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001181 x = PyDict_GetItemWithError((PyObject *)globals, key);
1182 if (x != NULL)
1183 return x;
1184 if (PyErr_Occurred())
1185 return NULL;
1186 return PyDict_GetItemWithError((PyObject *)builtins, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001187}
1188
Antoine Pitroue965d972012-02-27 00:45:12 +01001189/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1190 * dictionary if it's merely replacing the value for an existing key.
1191 * This means that it's safe to loop over a dictionary with PyDict_Next()
1192 * and occasionally replace a value -- but you can't insert new keys or
1193 * remove them.
1194 */
1195int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001196PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001197{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001198 PyDictObject *mp;
1199 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001200 if (!PyDict_Check(op)) {
1201 PyErr_BadInternalCall();
1202 return -1;
1203 }
1204 assert(key);
1205 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001206 mp = (PyDictObject *)op;
1207 if (!PyUnicode_CheckExact(key) ||
1208 (hash = ((PyASCIIObject *) key)->hash) == -1)
1209 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001210 hash = PyObject_Hash(key);
1211 if (hash == -1)
1212 return -1;
1213 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001214
1215 /* insertdict() handles any resizing that might be necessary */
1216 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001217}
1218
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001219int
Tim Peters1f5871e2000-07-04 17:44:48 +00001220PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001221{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001222 PyDictObject *mp;
1223 Py_hash_t hash;
1224 PyDictKeyEntry *ep;
1225 PyObject *old_key, *old_value;
1226 PyObject **value_addr;
Guido van Rossumd7047b31995-01-02 19:07:15 +00001227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 if (!PyDict_Check(op)) {
1229 PyErr_BadInternalCall();
1230 return -1;
1231 }
1232 assert(key);
1233 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001234 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 hash = PyObject_Hash(key);
1236 if (hash == -1)
1237 return -1;
1238 }
1239 mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001240 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 if (ep == NULL)
1242 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001243 if (*value_addr == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 set_key_error(key);
1245 return -1;
1246 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001247 old_value = *value_addr;
1248 *value_addr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001250 if (!_PyDict_HasSplitTable(mp)) {
1251 ENSURE_ALLOWS_DELETIONS(mp);
1252 old_key = ep->me_key;
1253 Py_INCREF(dummy);
1254 ep->me_key = dummy;
1255 Py_DECREF(old_key);
1256 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 Py_DECREF(old_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001259}
1260
Guido van Rossum25831651993-05-19 14:50:45 +00001261void
Tim Peters1f5871e2000-07-04 17:44:48 +00001262PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001265 PyDictKeysObject *oldkeys;
1266 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 if (!PyDict_Check(op))
1270 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001271 mp = ((PyDictObject *)op);
1272 oldkeys = mp->ma_keys;
1273 oldvalues = mp->ma_values;
1274 if (oldvalues == empty_values)
1275 return;
1276 /* Empty the dict... */
1277 DK_INCREF(Py_EMPTY_KEYS);
1278 mp->ma_keys = Py_EMPTY_KEYS;
1279 mp->ma_values = empty_values;
1280 mp->ma_used = 0;
1281 /* ...then clear the keys and values */
1282 if (oldvalues != NULL) {
1283 n = DK_SIZE(oldkeys);
1284 for (i = 0; i < n; i++)
1285 Py_CLEAR(oldvalues[i]);
1286 free_values(oldvalues);
1287 DK_DECREF(oldkeys);
1288 }
1289 else {
1290 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001291 DK_DECREF(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001292 }
1293}
1294
1295/* Returns -1 if no more items (or op is not a dict),
1296 * index of item otherwise. Stores value in pvalue
1297 */
1298Py_LOCAL_INLINE(Py_ssize_t)
1299dict_next(PyObject *op, Py_ssize_t i, PyObject **pvalue)
1300{
1301 Py_ssize_t mask, offset;
1302 PyDictObject *mp;
1303 PyObject **value_ptr;
1304
1305
1306 if (!PyDict_Check(op))
1307 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001309 if (i < 0)
1310 return -1;
1311 if (mp->ma_values) {
1312 value_ptr = &mp->ma_values[i];
1313 offset = sizeof(PyObject *);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001315 else {
1316 value_ptr = &mp->ma_keys->dk_entries[i].me_value;
1317 offset = sizeof(PyDictKeyEntry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001319 mask = DK_MASK(mp->ma_keys);
1320 while (i <= mask && *value_ptr == NULL) {
1321 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1322 i++;
1323 }
1324 if (i > mask)
1325 return -1;
1326 if (pvalue)
1327 *pvalue = *value_ptr;
1328 return i;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001329}
1330
Tim Peters080c88b2003-02-15 03:01:11 +00001331/*
1332 * Iterate over a dict. Use like so:
1333 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001334 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001335 * PyObject *key, *value;
1336 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001337 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +00001338 * Refer to borrowed references in key and value.
1339 * }
1340 *
1341 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001342 * mutates the dict. One exception: it is safe if the loop merely changes
1343 * the values associated with the keys (but doesn't insert new keys or
1344 * delete keys), via PyDict_SetItem().
1345 */
Guido van Rossum25831651993-05-19 14:50:45 +00001346int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001347PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001348{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001349 PyDictObject *mp;
1350 Py_ssize_t i = dict_next(op, *ppos, pvalue);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 if (i < 0)
1352 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001353 mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 *ppos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 if (pkey)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001356 *pkey = mp->ma_keys->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001358}
1359
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001360/* Internal version of PyDict_Next that returns a hash value in addition
1361 * to the key and value.
1362 */
Thomas Wouterscf297e42007-02-23 15:07:44 +00001363int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001364_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1365 PyObject **pvalue, Py_hash_t *phash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00001366{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001367 PyDictObject *mp;
1368 Py_ssize_t i = dict_next(op, *ppos, pvalue);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 if (i < 0)
1370 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001371 mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 *ppos = i+1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001373 *phash = mp->ma_keys->dk_entries[i].me_hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 if (pkey)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001375 *pkey = mp->ma_keys->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 return 1;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001377}
1378
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001379/* Methods */
1380
1381static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001382dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001383{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001384 PyObject **values = mp->ma_values;
1385 PyDictKeysObject *keys = mp->ma_keys;
1386 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 PyObject_GC_UnTrack(mp);
1388 Py_TRASHCAN_SAFE_BEGIN(mp)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001389 if (values != NULL) {
1390 if (values != empty_values) {
1391 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
1392 Py_XDECREF(values[i]);
1393 }
1394 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001396 DK_DECREF(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02001398 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001399 assert(keys->dk_refcnt == 1);
1400 DK_DECREF(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001401 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1403 free_list[numfree++] = mp;
1404 else
1405 Py_TYPE(mp)->tp_free((PyObject *)mp);
1406 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001407}
1408
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001409
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001410static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001411dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001412{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 Py_ssize_t i;
1414 PyObject *s, *temp, *colon = NULL;
1415 PyObject *pieces = NULL, *result = NULL;
1416 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +00001417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 i = Py_ReprEnter((PyObject *)mp);
1419 if (i != 0) {
1420 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
1421 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 if (mp->ma_used == 0) {
1424 result = PyUnicode_FromString("{}");
1425 goto Done;
1426 }
Tim Petersa7259592001-06-16 05:11:17 +00001427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 pieces = PyList_New(0);
1429 if (pieces == NULL)
1430 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 colon = PyUnicode_FromString(": ");
1433 if (colon == NULL)
1434 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 /* Do repr() on each key+value pair, and insert ": " between them.
1437 Note that repr may mutate the dict. */
1438 i = 0;
1439 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
1440 int status;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001441 /* Prevent repr from deleting key or value during key format. */
1442 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 Py_INCREF(value);
1444 s = PyObject_Repr(key);
1445 PyUnicode_Append(&s, colon);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02001446 if (s == NULL)
1447 goto Done;
1448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 PyUnicode_AppendAndDel(&s, PyObject_Repr(value));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001450 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001451 Py_DECREF(value);
1452 if (s == NULL)
1453 goto Done;
1454 status = PyList_Append(pieces, s);
1455 Py_DECREF(s); /* append created a new ref */
1456 if (status < 0)
1457 goto Done;
1458 }
Tim Petersa7259592001-06-16 05:11:17 +00001459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 /* Add "{}" decorations to the first and last items. */
1461 assert(PyList_GET_SIZE(pieces) > 0);
1462 s = PyUnicode_FromString("{");
1463 if (s == NULL)
1464 goto Done;
1465 temp = PyList_GET_ITEM(pieces, 0);
1466 PyUnicode_AppendAndDel(&s, temp);
1467 PyList_SET_ITEM(pieces, 0, s);
1468 if (s == NULL)
1469 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001471 s = PyUnicode_FromString("}");
1472 if (s == NULL)
1473 goto Done;
1474 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
1475 PyUnicode_AppendAndDel(&temp, s);
1476 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
1477 if (temp == NULL)
1478 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480 /* Paste them all together with ", " between. */
1481 s = PyUnicode_FromString(", ");
1482 if (s == NULL)
1483 goto Done;
1484 result = PyUnicode_Join(s, pieces);
1485 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +00001486
1487Done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001488 Py_XDECREF(pieces);
1489 Py_XDECREF(colon);
1490 Py_ReprLeave((PyObject *)mp);
1491 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001492}
1493
Martin v. Löwis18e16552006-02-15 17:27:45 +00001494static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001495dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001496{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001498}
1499
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001500static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001501dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001502{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001503 PyObject *v;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001504 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001505 PyDictKeyEntry *ep;
1506 PyObject **value_addr;
1507
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001508 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001509 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 hash = PyObject_Hash(key);
1511 if (hash == -1)
1512 return NULL;
1513 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001514 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 if (ep == NULL)
1516 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001517 v = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001518 if (v == NULL) {
1519 if (!PyDict_CheckExact(mp)) {
1520 /* Look up __missing__ method if we're a subclass. */
1521 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05001522 _Py_IDENTIFIER(__missing__);
1523 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 if (missing != NULL) {
1525 res = PyObject_CallFunctionObjArgs(missing,
1526 key, NULL);
1527 Py_DECREF(missing);
1528 return res;
1529 }
1530 else if (PyErr_Occurred())
1531 return NULL;
1532 }
1533 set_key_error(key);
1534 return NULL;
1535 }
1536 else
1537 Py_INCREF(v);
1538 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001539}
1540
1541static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001542dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001543{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544 if (w == NULL)
1545 return PyDict_DelItem((PyObject *)mp, v);
1546 else
1547 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001548}
1549
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001550static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 (lenfunc)dict_length, /*mp_length*/
1552 (binaryfunc)dict_subscript, /*mp_subscript*/
1553 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001554};
1555
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001556static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001557dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001558{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001559 PyObject *v;
1560 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001561 PyDictKeyEntry *ep;
1562 Py_ssize_t size, n, offset;
1563 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001564
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001565 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 n = mp->ma_used;
1567 v = PyList_New(n);
1568 if (v == NULL)
1569 return NULL;
1570 if (n != mp->ma_used) {
1571 /* Durnit. The allocations caused the dict to resize.
1572 * Just start over, this shouldn't normally happen.
1573 */
1574 Py_DECREF(v);
1575 goto again;
1576 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001577 ep = &mp->ma_keys->dk_entries[0];
1578 size = DK_SIZE(mp->ma_keys);
1579 if (mp->ma_values) {
1580 value_ptr = mp->ma_values;
1581 offset = sizeof(PyObject *);
1582 }
1583 else {
1584 value_ptr = &ep[0].me_value;
1585 offset = sizeof(PyDictKeyEntry);
1586 }
1587 for (i = 0, j = 0; i < size; i++) {
1588 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001589 PyObject *key = ep[i].me_key;
1590 Py_INCREF(key);
1591 PyList_SET_ITEM(v, j, key);
1592 j++;
1593 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001594 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 }
1596 assert(j == n);
1597 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001598}
1599
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001600static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001601dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001602{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001603 PyObject *v;
1604 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001605 Py_ssize_t size, n, offset;
1606 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001607
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001608 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609 n = mp->ma_used;
1610 v = PyList_New(n);
1611 if (v == NULL)
1612 return NULL;
1613 if (n != mp->ma_used) {
1614 /* Durnit. The allocations caused the dict to resize.
1615 * Just start over, this shouldn't normally happen.
1616 */
1617 Py_DECREF(v);
1618 goto again;
1619 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001620 size = DK_SIZE(mp->ma_keys);
1621 if (mp->ma_values) {
1622 value_ptr = mp->ma_values;
1623 offset = sizeof(PyObject *);
1624 }
1625 else {
1626 value_ptr = &mp->ma_keys->dk_entries[0].me_value;
1627 offset = sizeof(PyDictKeyEntry);
1628 }
1629 for (i = 0, j = 0; i < size; i++) {
1630 PyObject *value = *value_ptr;
1631 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1632 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 Py_INCREF(value);
1634 PyList_SET_ITEM(v, j, value);
1635 j++;
1636 }
1637 }
1638 assert(j == n);
1639 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001640}
1641
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001642static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001643dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001644{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001645 PyObject *v;
1646 Py_ssize_t i, j, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001647 Py_ssize_t size, offset;
1648 PyObject *item, *key;
1649 PyDictKeyEntry *ep;
1650 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001652 /* Preallocate the list of tuples, to avoid allocations during
1653 * the loop over the items, which could trigger GC, which
1654 * could resize the dict. :-(
1655 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001656 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 n = mp->ma_used;
1658 v = PyList_New(n);
1659 if (v == NULL)
1660 return NULL;
1661 for (i = 0; i < n; i++) {
1662 item = PyTuple_New(2);
1663 if (item == NULL) {
1664 Py_DECREF(v);
1665 return NULL;
1666 }
1667 PyList_SET_ITEM(v, i, item);
1668 }
1669 if (n != mp->ma_used) {
1670 /* Durnit. The allocations caused the dict to resize.
1671 * Just start over, this shouldn't normally happen.
1672 */
1673 Py_DECREF(v);
1674 goto again;
1675 }
1676 /* Nothing we do below makes any function calls. */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001677 ep = mp->ma_keys->dk_entries;
1678 size = DK_SIZE(mp->ma_keys);
1679 if (mp->ma_values) {
1680 value_ptr = mp->ma_values;
1681 offset = sizeof(PyObject *);
1682 }
1683 else {
1684 value_ptr = &ep[0].me_value;
1685 offset = sizeof(PyDictKeyEntry);
1686 }
1687 for (i = 0, j = 0; i < size; i++) {
1688 PyObject *value = *value_ptr;
1689 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1690 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 key = ep[i].me_key;
1692 item = PyList_GET_ITEM(v, j);
1693 Py_INCREF(key);
1694 PyTuple_SET_ITEM(item, 0, key);
1695 Py_INCREF(value);
1696 PyTuple_SET_ITEM(item, 1, value);
1697 j++;
1698 }
1699 }
1700 assert(j == n);
1701 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001702}
1703
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001704static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001705dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001706{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001707 PyObject *seq;
1708 PyObject *value = Py_None;
1709 PyObject *it; /* iter(seq) */
1710 PyObject *key;
1711 PyObject *d;
1712 int status;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
1715 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717 d = PyObject_CallObject(cls, NULL);
1718 if (d == NULL)
1719 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001720
Benjamin Peterson9892f522012-10-31 14:22:12 -04001721 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001722 if (PyDict_CheckExact(seq)) {
1723 PyDictObject *mp = (PyDictObject *)d;
1724 PyObject *oldvalue;
1725 Py_ssize_t pos = 0;
1726 PyObject *key;
1727 Py_hash_t hash;
Guido van Rossum58da9312007-11-10 23:39:45 +00001728
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001729 if (dictresize(mp, Py_SIZE(seq))) {
Petri Lehtinena94200e2011-10-24 21:12:58 +03001730 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001732 }
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001733
1734 while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001735 if (insertdict(mp, key, hash, value)) {
1736 Py_DECREF(d);
1737 return NULL;
1738 }
1739 }
1740 return d;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 }
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001742 if (PyAnySet_CheckExact(seq)) {
1743 PyDictObject *mp = (PyDictObject *)d;
1744 Py_ssize_t pos = 0;
1745 PyObject *key;
1746 Py_hash_t hash;
Guido van Rossum58da9312007-11-10 23:39:45 +00001747
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001748 if (dictresize(mp, PySet_GET_SIZE(seq))) {
Petri Lehtinena94200e2011-10-24 21:12:58 +03001749 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001750 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001751 }
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001752
1753 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001754 if (insertdict(mp, key, hash, value)) {
1755 Py_DECREF(d);
1756 return NULL;
1757 }
1758 }
1759 return d;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001760 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001761 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763 it = PyObject_GetIter(seq);
1764 if (it == NULL){
1765 Py_DECREF(d);
1766 return NULL;
1767 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 if (PyDict_CheckExact(d)) {
1770 while ((key = PyIter_Next(it)) != NULL) {
1771 status = PyDict_SetItem(d, key, value);
1772 Py_DECREF(key);
1773 if (status < 0)
1774 goto Fail;
1775 }
1776 } else {
1777 while ((key = PyIter_Next(it)) != NULL) {
1778 status = PyObject_SetItem(d, key, value);
1779 Py_DECREF(key);
1780 if (status < 0)
1781 goto Fail;
1782 }
1783 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001784
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 if (PyErr_Occurred())
1786 goto Fail;
1787 Py_DECREF(it);
1788 return d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001789
1790Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 Py_DECREF(it);
1792 Py_DECREF(d);
1793 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001794}
1795
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001796static int
1797dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001798{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 PyObject *arg = NULL;
1800 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001802 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1803 result = -1;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001804
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001805 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001806 _Py_IDENTIFIER(keys);
1807 if (_PyObject_HasAttrId(arg, &PyId_keys))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 result = PyDict_Merge(self, arg, 1);
1809 else
1810 result = PyDict_MergeFromSeq2(self, arg, 1);
1811 }
1812 if (result == 0 && kwds != NULL) {
1813 if (PyArg_ValidateKeywordArguments(kwds))
1814 result = PyDict_Merge(self, kwds, 1);
1815 else
1816 result = -1;
1817 }
1818 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001819}
1820
1821static PyObject *
1822dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1823{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 if (dict_update_common(self, args, kwds, "update") != -1)
1825 Py_RETURN_NONE;
1826 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001827}
1828
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001829/* Update unconditionally replaces existing items.
1830 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001831 otherwise it leaves existing items unchanged.
1832
1833 PyDict_{Update,Merge} update/merge from a mapping object.
1834
Tim Petersf582b822001-12-11 18:51:08 +00001835 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001836 producing iterable objects of length 2.
1837*/
1838
Tim Petersf582b822001-12-11 18:51:08 +00001839int
Tim Peters1fc240e2001-10-26 05:06:50 +00001840PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1841{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001842 PyObject *it; /* iter(seq2) */
1843 Py_ssize_t i; /* index into seq2 of current element */
1844 PyObject *item; /* seq2[i] */
1845 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00001846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 assert(d != NULL);
1848 assert(PyDict_Check(d));
1849 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00001850
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001851 it = PyObject_GetIter(seq2);
1852 if (it == NULL)
1853 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001854
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855 for (i = 0; ; ++i) {
1856 PyObject *key, *value;
1857 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859 fast = NULL;
1860 item = PyIter_Next(it);
1861 if (item == NULL) {
1862 if (PyErr_Occurred())
1863 goto Fail;
1864 break;
1865 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001867 /* Convert item to sequence, and verify length 2. */
1868 fast = PySequence_Fast(item, "");
1869 if (fast == NULL) {
1870 if (PyErr_ExceptionMatches(PyExc_TypeError))
1871 PyErr_Format(PyExc_TypeError,
1872 "cannot convert dictionary update "
1873 "sequence element #%zd to a sequence",
1874 i);
1875 goto Fail;
1876 }
1877 n = PySequence_Fast_GET_SIZE(fast);
1878 if (n != 2) {
1879 PyErr_Format(PyExc_ValueError,
1880 "dictionary update sequence element #%zd "
1881 "has length %zd; 2 is required",
1882 i, n);
1883 goto Fail;
1884 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886 /* Update/merge with this (key, value) pair. */
1887 key = PySequence_Fast_GET_ITEM(fast, 0);
1888 value = PySequence_Fast_GET_ITEM(fast, 1);
1889 if (override || PyDict_GetItem(d, key) == NULL) {
1890 int status = PyDict_SetItem(d, key, value);
1891 if (status < 0)
1892 goto Fail;
1893 }
1894 Py_DECREF(fast);
1895 Py_DECREF(item);
1896 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 i = 0;
1899 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00001900Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001901 Py_XDECREF(item);
1902 Py_XDECREF(fast);
1903 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001904Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 Py_DECREF(it);
1906 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001907}
1908
Tim Peters6d6c1a32001-08-02 04:15:00 +00001909int
1910PyDict_Update(PyObject *a, PyObject *b)
1911{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001912 return PyDict_Merge(a, b, 1);
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001913}
1914
1915int
1916PyDict_Merge(PyObject *a, PyObject *b, int override)
1917{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001918 PyDictObject *mp, *other;
1919 Py_ssize_t i, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001920 PyDictKeyEntry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 /* We accept for the argument either a concrete dictionary object,
1923 * or an abstract "mapping" object. For the former, we can do
1924 * things quite efficiently. For the latter, we only require that
1925 * PyMapping_Keys() and PyObject_GetItem() be supported.
1926 */
1927 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1928 PyErr_BadInternalCall();
1929 return -1;
1930 }
1931 mp = (PyDictObject*)a;
1932 if (PyDict_Check(b)) {
1933 other = (PyDictObject*)b;
1934 if (other == mp || other->ma_used == 0)
1935 /* a.update(a) or a.update({}); nothing to do */
1936 return 0;
1937 if (mp->ma_used == 0)
1938 /* Since the target dict is empty, PyDict_GetItem()
1939 * always returns NULL. Setting override to 1
1940 * skips the unnecessary test.
1941 */
1942 override = 1;
1943 /* Do one big resize at the start, rather than
1944 * incrementally resizing as we insert new items. Expect
1945 * that there will be no (or few) overlapping keys.
1946 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001947 if (mp->ma_keys->dk_usable * 3 < other->ma_used * 2)
1948 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001950 for (i = 0, n = DK_SIZE(other->ma_keys); i < n; i++) {
1951 PyObject *value;
1952 entry = &other->ma_keys->dk_entries[i];
1953 if (other->ma_values)
1954 value = other->ma_values[i];
1955 else
1956 value = entry->me_value;
1957
1958 if (value != NULL &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959 (override ||
1960 PyDict_GetItem(a, entry->me_key) == NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 if (insertdict(mp, entry->me_key,
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001962 entry->me_hash,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001963 value) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964 return -1;
1965 }
1966 }
1967 }
1968 else {
1969 /* Do it the generic, slower way */
1970 PyObject *keys = PyMapping_Keys(b);
1971 PyObject *iter;
1972 PyObject *key, *value;
1973 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 if (keys == NULL)
1976 /* Docstring says this is equivalent to E.keys() so
1977 * if E doesn't have a .keys() method we want
1978 * AttributeError to percolate up. Might as well
1979 * do the same for any other error.
1980 */
1981 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001982
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001983 iter = PyObject_GetIter(keys);
1984 Py_DECREF(keys);
1985 if (iter == NULL)
1986 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001988 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
1989 if (!override && PyDict_GetItem(a, key) != NULL) {
1990 Py_DECREF(key);
1991 continue;
1992 }
1993 value = PyObject_GetItem(b, key);
1994 if (value == NULL) {
1995 Py_DECREF(iter);
1996 Py_DECREF(key);
1997 return -1;
1998 }
1999 status = PyDict_SetItem(a, key, value);
2000 Py_DECREF(key);
2001 Py_DECREF(value);
2002 if (status < 0) {
2003 Py_DECREF(iter);
2004 return -1;
2005 }
2006 }
2007 Py_DECREF(iter);
2008 if (PyErr_Occurred())
2009 /* Iterator completed, via error */
2010 return -1;
2011 }
2012 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002013}
2014
2015static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002016dict_copy(PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002017{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002018 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002019}
2020
2021PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002022PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002023{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002024 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002025 PyDictObject *mp;
2026 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002027
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002028 if (o == NULL || !PyDict_Check(o)) {
2029 PyErr_BadInternalCall();
2030 return NULL;
2031 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002032 mp = (PyDictObject *)o;
2033 if (_PyDict_HasSplitTable(mp)) {
2034 PyDictObject *split_copy;
2035 PyObject **newvalues = new_values(DK_SIZE(mp->ma_keys));
2036 if (newvalues == NULL)
2037 return PyErr_NoMemory();
2038 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2039 if (split_copy == NULL) {
2040 free_values(newvalues);
2041 return NULL;
2042 }
2043 split_copy->ma_values = newvalues;
2044 split_copy->ma_keys = mp->ma_keys;
2045 split_copy->ma_used = mp->ma_used;
2046 DK_INCREF(mp->ma_keys);
2047 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2048 PyObject *value = mp->ma_values[i];
2049 Py_XINCREF(value);
2050 split_copy->ma_values[i] = value;
2051 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002052 if (_PyObject_GC_IS_TRACKED(mp))
2053 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002054 return (PyObject *)split_copy;
2055 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002056 copy = PyDict_New();
2057 if (copy == NULL)
2058 return NULL;
2059 if (PyDict_Merge(copy, o, 1) == 0)
2060 return copy;
2061 Py_DECREF(copy);
2062 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002063}
2064
Martin v. Löwis18e16552006-02-15 17:27:45 +00002065Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002066PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002067{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002068 if (mp == NULL || !PyDict_Check(mp)) {
2069 PyErr_BadInternalCall();
2070 return -1;
2071 }
2072 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002073}
2074
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002075PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002076PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002077{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002078 if (mp == NULL || !PyDict_Check(mp)) {
2079 PyErr_BadInternalCall();
2080 return NULL;
2081 }
2082 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002083}
2084
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002085PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002086PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002087{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002088 if (mp == NULL || !PyDict_Check(mp)) {
2089 PyErr_BadInternalCall();
2090 return NULL;
2091 }
2092 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002093}
2094
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002095PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002096PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002097{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002098 if (mp == NULL || !PyDict_Check(mp)) {
2099 PyErr_BadInternalCall();
2100 return NULL;
2101 }
2102 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002103}
2104
Tim Peterse63415e2001-05-08 04:38:29 +00002105/* Return 1 if dicts equal, 0 if not, -1 if error.
2106 * Gets out as soon as any difference is detected.
2107 * Uses only Py_EQ comparison.
2108 */
2109static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002110dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002111{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002112 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002114 if (a->ma_used != b->ma_used)
2115 /* can't be equal if # of entries differ */
2116 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002117 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002118 for (i = 0; i < DK_SIZE(a->ma_keys); i++) {
2119 PyDictKeyEntry *ep = &a->ma_keys->dk_entries[i];
2120 PyObject *aval;
2121 if (a->ma_values)
2122 aval = a->ma_values[i];
2123 else
2124 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002125 if (aval != NULL) {
2126 int cmp;
2127 PyObject *bval;
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002128 PyObject **vaddr;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002129 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002130 /* temporarily bump aval's refcount to ensure it stays
2131 alive until we're done with it */
2132 Py_INCREF(aval);
2133 /* ditto for key */
2134 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002135 /* reuse the known hash value */
2136 if ((b->ma_keys->dk_lookup)(b, key, ep->me_hash, &vaddr) == NULL)
2137 bval = NULL;
2138 else
2139 bval = *vaddr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002140 Py_DECREF(key);
2141 if (bval == NULL) {
2142 Py_DECREF(aval);
2143 if (PyErr_Occurred())
2144 return -1;
2145 return 0;
2146 }
2147 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
2148 Py_DECREF(aval);
2149 if (cmp <= 0) /* error or not equal */
2150 return cmp;
2151 }
2152 }
2153 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002154}
Tim Peterse63415e2001-05-08 04:38:29 +00002155
2156static PyObject *
2157dict_richcompare(PyObject *v, PyObject *w, int op)
2158{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002159 int cmp;
2160 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2163 res = Py_NotImplemented;
2164 }
2165 else if (op == Py_EQ || op == Py_NE) {
2166 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2167 if (cmp < 0)
2168 return NULL;
2169 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2170 }
2171 else
2172 res = Py_NotImplemented;
2173 Py_INCREF(res);
2174 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002175}
Tim Peterse63415e2001-05-08 04:38:29 +00002176
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002177static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002178dict_contains(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002179{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002180 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002181 PyDictKeyEntry *ep;
2182 PyObject **value_addr;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002184 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002185 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002186 hash = PyObject_Hash(key);
2187 if (hash == -1)
2188 return NULL;
2189 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002190 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002191 if (ep == NULL)
2192 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002193 return PyBool_FromLong(*value_addr != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002194}
2195
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002196static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002197dict_get(PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00002198{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002199 PyObject *key;
2200 PyObject *failobj = Py_None;
2201 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002202 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002203 PyDictKeyEntry *ep;
2204 PyObject **value_addr;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002206 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
2207 return NULL;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002209 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002210 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 hash = PyObject_Hash(key);
2212 if (hash == -1)
2213 return NULL;
2214 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002215 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002216 if (ep == NULL)
2217 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002218 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002219 if (val == NULL)
2220 val = failobj;
2221 Py_INCREF(val);
2222 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002223}
2224
Benjamin Peterson00e98862013-03-07 22:16:29 -05002225PyObject *
2226PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002227{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002228 PyDictObject *mp = (PyDictObject *)d;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002229 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002230 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002231 PyDictKeyEntry *ep;
2232 PyObject **value_addr;
Guido van Rossum164452c2000-08-08 16:12:54 +00002233
Benjamin Peterson00e98862013-03-07 22:16:29 -05002234 if (!PyDict_Check(d)) {
2235 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002236 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002237 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002238 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002239 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002240 hash = PyObject_Hash(key);
2241 if (hash == -1)
2242 return NULL;
2243 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002244 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002245 if (ep == NULL)
2246 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002247 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002248 if (val == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002249 if (mp->ma_keys->dk_usable <= 0) {
2250 /* Need to resize. */
2251 if (insertion_resize(mp) < 0)
2252 return NULL;
2253 ep = find_empty_slot(mp, key, hash, &value_addr);
2254 }
Benjamin Peterson00e98862013-03-07 22:16:29 -05002255 Py_INCREF(defaultobj);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002256 Py_INCREF(key);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002257 MAINTAIN_TRACKING(mp, key, defaultobj);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002258 ep->me_key = key;
2259 ep->me_hash = hash;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002260 *value_addr = defaultobj;
2261 val = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002262 mp->ma_keys->dk_usable--;
2263 mp->ma_used++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002264 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002265 return val;
Guido van Rossum164452c2000-08-08 16:12:54 +00002266}
2267
Benjamin Peterson00e98862013-03-07 22:16:29 -05002268static PyObject *
2269dict_setdefault(PyDictObject *mp, PyObject *args)
2270{
2271 PyObject *key, *val;
2272 PyObject *defaultobj = Py_None;
2273
2274 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &defaultobj))
2275 return NULL;
2276
Benjamin Peterson55898502013-03-08 08:36:49 -05002277 val = PyDict_SetDefault((PyObject *)mp, key, defaultobj);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002278 Py_XINCREF(val);
2279 return val;
2280}
Guido van Rossum164452c2000-08-08 16:12:54 +00002281
2282static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002283dict_clear(PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002285 PyDict_Clear((PyObject *)mp);
2286 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002287}
2288
Guido van Rossumba6ab842000-12-12 22:02:18 +00002289static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002290dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002291{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002292 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002293 PyObject *old_value, *old_key;
2294 PyObject *key, *deflt = NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002295 PyDictKeyEntry *ep;
2296 PyObject **value_addr;
Guido van Rossume027d982002-04-12 15:11:59 +00002297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002298 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2299 return NULL;
2300 if (mp->ma_used == 0) {
2301 if (deflt) {
2302 Py_INCREF(deflt);
2303 return deflt;
2304 }
Raymond Hettingerdd421542010-10-30 08:10:29 +00002305 set_key_error(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002306 return NULL;
2307 }
2308 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002309 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 hash = PyObject_Hash(key);
2311 if (hash == -1)
2312 return NULL;
2313 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002314 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002315 if (ep == NULL)
2316 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002317 old_value = *value_addr;
2318 if (old_value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002319 if (deflt) {
2320 Py_INCREF(deflt);
2321 return deflt;
2322 }
2323 set_key_error(key);
2324 return NULL;
2325 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002326 *value_addr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002327 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002328 if (!_PyDict_HasSplitTable(mp)) {
2329 ENSURE_ALLOWS_DELETIONS(mp);
2330 old_key = ep->me_key;
2331 Py_INCREF(dummy);
2332 ep->me_key = dummy;
2333 Py_DECREF(old_key);
2334 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002335 return old_value;
Guido van Rossume027d982002-04-12 15:11:59 +00002336}
2337
2338static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002339dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00002340{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002341 Py_hash_t i = 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002342 PyDictKeyEntry *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002343 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002344
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346 /* Allocate the result tuple before checking the size. Believe it
2347 * or not, this allocation could trigger a garbage collection which
2348 * could empty the dict, so if we checked the size first and that
2349 * happened, the result would be an infinite loop (searching for an
2350 * entry that no longer exists). Note that the usual popitem()
2351 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2352 * tuple away if the dict *is* empty isn't a significant
2353 * inefficiency -- possible, but unlikely in practice.
2354 */
2355 res = PyTuple_New(2);
2356 if (res == NULL)
2357 return NULL;
2358 if (mp->ma_used == 0) {
2359 Py_DECREF(res);
2360 PyErr_SetString(PyExc_KeyError,
2361 "popitem(): dictionary is empty");
2362 return NULL;
2363 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002364 /* Convert split table to combined table */
2365 if (mp->ma_keys->dk_lookup == lookdict_split) {
2366 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
2367 Py_DECREF(res);
2368 return NULL;
2369 }
2370 }
2371 ENSURE_ALLOWS_DELETIONS(mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002372 /* Set ep to "the first" dict entry with a value. We abuse the hash
2373 * field of slot 0 to hold a search finger:
2374 * If slot 0 has a value, use slot 0.
2375 * Else slot 0 is being used to hold a search finger,
2376 * and we use its hash value as the first index to look.
2377 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002378 ep = &mp->ma_keys->dk_entries[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379 if (ep->me_value == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002380 Py_ssize_t mask = DK_MASK(mp->ma_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002381 i = ep->me_hash;
2382 /* The hash field may be a real hash value, or it may be a
2383 * legit search finger, or it may be a once-legit search
2384 * finger that's out of bounds now because it wrapped around
2385 * or the table shrunk -- simply make sure it's in bounds now.
2386 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002387 if (i > mask || i < 1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388 i = 1; /* skip slot 0 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002389 while ((ep = &mp->ma_keys->dk_entries[i])->me_value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002390 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002391 if (i > mask)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392 i = 1;
2393 }
2394 }
2395 PyTuple_SET_ITEM(res, 0, ep->me_key);
2396 PyTuple_SET_ITEM(res, 1, ep->me_value);
2397 Py_INCREF(dummy);
2398 ep->me_key = dummy;
2399 ep->me_value = NULL;
2400 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002401 assert(mp->ma_keys->dk_entries[0].me_value == NULL);
2402 mp->ma_keys->dk_entries[0].me_hash = i + 1; /* next place to start */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002403 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002404}
2405
Jeremy Hylton8caad492000-06-23 14:18:11 +00002406static int
2407dict_traverse(PyObject *op, visitproc visit, void *arg)
2408{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002409 Py_ssize_t i, n;
2410 PyDictObject *mp = (PyDictObject *)op;
2411 if (mp->ma_keys->dk_lookup == lookdict) {
2412 for (i = 0; i < DK_SIZE(mp->ma_keys); i++) {
2413 if (mp->ma_keys->dk_entries[i].me_value != NULL) {
2414 Py_VISIT(mp->ma_keys->dk_entries[i].me_value);
2415 Py_VISIT(mp->ma_keys->dk_entries[i].me_key);
2416 }
2417 }
2418 } else {
2419 if (mp->ma_values != NULL) {
2420 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2421 Py_VISIT(mp->ma_values[i]);
2422 }
2423 }
2424 else {
2425 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2426 Py_VISIT(mp->ma_keys->dk_entries[i].me_value);
2427 }
2428 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 }
2430 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002431}
2432
2433static int
2434dict_tp_clear(PyObject *op)
2435{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002436 PyDict_Clear(op);
2437 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002438}
2439
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002440static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002441
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002442static PyObject *
2443dict_sizeof(PyDictObject *mp)
2444{
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002445 Py_ssize_t size, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002446
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002447 size = DK_SIZE(mp->ma_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002448 res = sizeof(PyDictObject);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002449 if (mp->ma_values)
2450 res += size * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002451 /* If the dictionary is split, the keys portion is accounted-for
2452 in the type object. */
2453 if (mp->ma_keys->dk_refcnt == 1)
2454 res += sizeof(PyDictKeysObject) + (size-1) * sizeof(PyDictKeyEntry);
2455 return PyLong_FromSsize_t(res);
2456}
2457
2458Py_ssize_t
2459_PyDict_KeysSize(PyDictKeysObject *keys)
2460{
2461 return sizeof(PyDictKeysObject) + (DK_SIZE(keys)-1) * sizeof(PyDictKeyEntry);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002462}
2463
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002464PyDoc_STRVAR(contains__doc__,
2465"D.__contains__(k) -> True if D has a key k, else False");
2466
2467PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
2468
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002469PyDoc_STRVAR(sizeof__doc__,
2470"D.__sizeof__() -> size of D in memory, in bytes");
2471
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002472PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002473"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002474
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002475PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002476"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 +00002477
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002478PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002479"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002480If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00002481
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002482PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00002483"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000024842-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002485
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002486PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04002487"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
2488If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
2489If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
2490In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00002491
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002492PyDoc_STRVAR(fromkeys__doc__,
2493"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
2494v defaults to None.");
2495
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002496PyDoc_STRVAR(clear__doc__,
2497"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002498
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002499PyDoc_STRVAR(copy__doc__,
2500"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00002501
Guido van Rossumb90c8482007-02-10 01:11:45 +00002502/* Forward */
2503static PyObject *dictkeys_new(PyObject *);
2504static PyObject *dictitems_new(PyObject *);
2505static PyObject *dictvalues_new(PyObject *);
2506
Guido van Rossum45c85d12007-07-27 16:31:40 +00002507PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002508 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002509PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002510 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002511PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002512 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00002513
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002514static PyMethodDef mapp_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002515 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
2516 contains__doc__},
2517 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
2518 getitem__doc__},
2519 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
2520 sizeof__doc__},
2521 {"get", (PyCFunction)dict_get, METH_VARARGS,
2522 get__doc__},
2523 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
2524 setdefault_doc__},
2525 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
2526 pop__doc__},
2527 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
2528 popitem__doc__},
2529 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
2530 keys__doc__},
2531 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
2532 items__doc__},
2533 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
2534 values__doc__},
2535 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
2536 update__doc__},
2537 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
2538 fromkeys__doc__},
2539 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
2540 clear__doc__},
2541 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
2542 copy__doc__},
2543 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002544};
2545
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002546/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002547int
2548PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002549{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002550 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002551 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002552 PyDictKeyEntry *ep;
2553 PyObject **value_addr;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002555 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002556 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002557 hash = PyObject_Hash(key);
2558 if (hash == -1)
2559 return -1;
2560 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002561 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
2562 return (ep == NULL) ? -1 : (*value_addr != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002563}
2564
Thomas Wouterscf297e42007-02-23 15:07:44 +00002565/* Internal version of PyDict_Contains used when the hash value is already known */
2566int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002567_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00002568{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002569 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002570 PyDictKeyEntry *ep;
2571 PyObject **value_addr;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002572
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002573 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
2574 return (ep == NULL) ? -1 : (*value_addr != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00002575}
2576
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002577/* Hack to implement "key in dict" */
2578static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002579 0, /* sq_length */
2580 0, /* sq_concat */
2581 0, /* sq_repeat */
2582 0, /* sq_item */
2583 0, /* sq_slice */
2584 0, /* sq_ass_item */
2585 0, /* sq_ass_slice */
2586 PyDict_Contains, /* sq_contains */
2587 0, /* sq_inplace_concat */
2588 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002589};
2590
Guido van Rossum09e563a2001-05-01 12:10:21 +00002591static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002592dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2593{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002594 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02002595 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002597 assert(type != NULL && type->tp_alloc != NULL);
2598 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02002599 if (self == NULL)
2600 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02002601 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02002602
Victor Stinnera9f61a52013-07-16 22:17:26 +02002603 /* The object has been implicitly tracked by tp_alloc */
2604 if (type == &PyDict_Type)
2605 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02002606
2607 d->ma_used = 0;
2608 d->ma_keys = new_keys_object(PyDict_MINSIZE_COMBINED);
2609 if (d->ma_keys == NULL) {
2610 Py_DECREF(self);
2611 return NULL;
2612 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002613 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002614}
2615
Tim Peters25786c02001-09-02 08:22:48 +00002616static int
2617dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2618{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002619 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002620}
2621
Tim Peters6d6c1a32001-08-02 04:15:00 +00002622static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002623dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002624{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002625 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002626}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002627
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002628PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002629"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002630"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002631" (key, value) pairs\n"
2632"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002633" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002634" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002635" d[k] = v\n"
2636"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2637" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002638
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002639PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002640 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2641 "dict",
2642 sizeof(PyDictObject),
2643 0,
2644 (destructor)dict_dealloc, /* tp_dealloc */
2645 0, /* tp_print */
2646 0, /* tp_getattr */
2647 0, /* tp_setattr */
2648 0, /* tp_reserved */
2649 (reprfunc)dict_repr, /* tp_repr */
2650 0, /* tp_as_number */
2651 &dict_as_sequence, /* tp_as_sequence */
2652 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002653 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002654 0, /* tp_call */
2655 0, /* tp_str */
2656 PyObject_GenericGetAttr, /* tp_getattro */
2657 0, /* tp_setattro */
2658 0, /* tp_as_buffer */
2659 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2660 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
2661 dictionary_doc, /* tp_doc */
2662 dict_traverse, /* tp_traverse */
2663 dict_tp_clear, /* tp_clear */
2664 dict_richcompare, /* tp_richcompare */
2665 0, /* tp_weaklistoffset */
2666 (getiterfunc)dict_iter, /* tp_iter */
2667 0, /* tp_iternext */
2668 mapp_methods, /* tp_methods */
2669 0, /* tp_members */
2670 0, /* tp_getset */
2671 0, /* tp_base */
2672 0, /* tp_dict */
2673 0, /* tp_descr_get */
2674 0, /* tp_descr_set */
2675 0, /* tp_dictoffset */
2676 dict_init, /* tp_init */
2677 PyType_GenericAlloc, /* tp_alloc */
2678 dict_new, /* tp_new */
2679 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002680};
2681
Victor Stinner3c1e4812012-03-26 22:10:51 +02002682PyObject *
2683_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
2684{
2685 PyObject *kv;
2686 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02002687 if (kv == NULL) {
2688 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002689 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02002690 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02002691 return PyDict_GetItem(dp, kv);
2692}
2693
Guido van Rossum3cca2451997-05-16 14:23:33 +00002694/* For backward compatibility with old dictionary interface */
2695
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002696PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002697PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002698{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002699 PyObject *kv, *rv;
2700 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02002701 if (kv == NULL) {
2702 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002703 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02002704 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002705 rv = PyDict_GetItem(v, kv);
2706 Py_DECREF(kv);
2707 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002708}
2709
2710int
Victor Stinner3c1e4812012-03-26 22:10:51 +02002711_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
2712{
2713 PyObject *kv;
2714 kv = _PyUnicode_FromId(key); /* borrowed */
2715 if (kv == NULL)
2716 return -1;
2717 return PyDict_SetItem(v, kv, item);
2718}
2719
2720int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002721PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002722{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002723 PyObject *kv;
2724 int err;
2725 kv = PyUnicode_FromString(key);
2726 if (kv == NULL)
2727 return -1;
2728 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
2729 err = PyDict_SetItem(v, kv, item);
2730 Py_DECREF(kv);
2731 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002732}
2733
2734int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002735PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002736{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002737 PyObject *kv;
2738 int err;
2739 kv = PyUnicode_FromString(key);
2740 if (kv == NULL)
2741 return -1;
2742 err = PyDict_DelItem(v, kv);
2743 Py_DECREF(kv);
2744 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002745}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002746
Raymond Hettinger019a1482004-03-18 02:41:19 +00002747/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002748
2749typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002750 PyObject_HEAD
2751 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
2752 Py_ssize_t di_used;
2753 Py_ssize_t di_pos;
2754 PyObject* di_result; /* reusable result tuple for iteritems */
2755 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002756} dictiterobject;
2757
2758static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002759dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002760{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002761 dictiterobject *di;
2762 di = PyObject_GC_New(dictiterobject, itertype);
2763 if (di == NULL)
2764 return NULL;
2765 Py_INCREF(dict);
2766 di->di_dict = dict;
2767 di->di_used = dict->ma_used;
2768 di->di_pos = 0;
2769 di->len = dict->ma_used;
2770 if (itertype == &PyDictIterItem_Type) {
2771 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2772 if (di->di_result == NULL) {
2773 Py_DECREF(di);
2774 return NULL;
2775 }
2776 }
2777 else
2778 di->di_result = NULL;
2779 _PyObject_GC_TRACK(di);
2780 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002781}
2782
2783static void
2784dictiter_dealloc(dictiterobject *di)
2785{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002786 Py_XDECREF(di->di_dict);
2787 Py_XDECREF(di->di_result);
2788 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002789}
2790
2791static int
2792dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
2793{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002794 Py_VISIT(di->di_dict);
2795 Py_VISIT(di->di_result);
2796 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002797}
2798
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002799static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002800dictiter_len(dictiterobject *di)
2801{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002802 Py_ssize_t len = 0;
2803 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
2804 len = di->len;
2805 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002806}
2807
Guido van Rossumb90c8482007-02-10 01:11:45 +00002808PyDoc_STRVAR(length_hint_doc,
2809 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002810
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002811static PyObject *
2812dictiter_reduce(dictiterobject *di);
2813
2814PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
2815
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002816static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002817 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
2818 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002819 {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS,
2820 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002821 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002822};
2823
Raymond Hettinger019a1482004-03-18 02:41:19 +00002824static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002825{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002826 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002827 Py_ssize_t i, mask, offset;
2828 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002829 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002830 PyObject **value_ptr;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002832 if (d == NULL)
2833 return NULL;
2834 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002836 if (di->di_used != d->ma_used) {
2837 PyErr_SetString(PyExc_RuntimeError,
2838 "dictionary changed size during iteration");
2839 di->di_used = -1; /* Make this state sticky */
2840 return NULL;
2841 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002842
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002843 i = di->di_pos;
2844 if (i < 0)
2845 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002846 k = d->ma_keys;
2847 if (d->ma_values) {
2848 value_ptr = &d->ma_values[i];
2849 offset = sizeof(PyObject *);
2850 }
2851 else {
2852 value_ptr = &k->dk_entries[i].me_value;
2853 offset = sizeof(PyDictKeyEntry);
2854 }
2855 mask = DK_SIZE(k)-1;
2856 while (i <= mask && *value_ptr == NULL) {
2857 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002858 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002859 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002860 di->di_pos = i+1;
2861 if (i > mask)
2862 goto fail;
2863 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002864 key = k->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002865 Py_INCREF(key);
2866 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002867
2868fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002869 Py_DECREF(d);
2870 di->di_dict = NULL;
2871 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002872}
2873
Raymond Hettinger019a1482004-03-18 02:41:19 +00002874PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002875 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2876 "dict_keyiterator", /* tp_name */
2877 sizeof(dictiterobject), /* tp_basicsize */
2878 0, /* tp_itemsize */
2879 /* methods */
2880 (destructor)dictiter_dealloc, /* tp_dealloc */
2881 0, /* tp_print */
2882 0, /* tp_getattr */
2883 0, /* tp_setattr */
2884 0, /* tp_reserved */
2885 0, /* tp_repr */
2886 0, /* tp_as_number */
2887 0, /* tp_as_sequence */
2888 0, /* tp_as_mapping */
2889 0, /* tp_hash */
2890 0, /* tp_call */
2891 0, /* tp_str */
2892 PyObject_GenericGetAttr, /* tp_getattro */
2893 0, /* tp_setattro */
2894 0, /* tp_as_buffer */
2895 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2896 0, /* tp_doc */
2897 (traverseproc)dictiter_traverse, /* tp_traverse */
2898 0, /* tp_clear */
2899 0, /* tp_richcompare */
2900 0, /* tp_weaklistoffset */
2901 PyObject_SelfIter, /* tp_iter */
2902 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
2903 dictiter_methods, /* tp_methods */
2904 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002905};
2906
2907static PyObject *dictiter_iternextvalue(dictiterobject *di)
2908{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002909 PyObject *value;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002910 Py_ssize_t i, mask, offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002911 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002912 PyObject **value_ptr;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002913
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002914 if (d == NULL)
2915 return NULL;
2916 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00002917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002918 if (di->di_used != d->ma_used) {
2919 PyErr_SetString(PyExc_RuntimeError,
2920 "dictionary changed size during iteration");
2921 di->di_used = -1; /* Make this state sticky */
2922 return NULL;
2923 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002925 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002926 mask = DK_SIZE(d->ma_keys)-1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002927 if (i < 0 || i > mask)
2928 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002929 if (d->ma_values) {
2930 value_ptr = &d->ma_values[i];
2931 offset = sizeof(PyObject *);
2932 }
2933 else {
2934 value_ptr = &d->ma_keys->dk_entries[i].me_value;
2935 offset = sizeof(PyDictKeyEntry);
2936 }
2937 while (i <= mask && *value_ptr == NULL) {
2938 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002939 i++;
2940 if (i > mask)
2941 goto fail;
2942 }
2943 di->di_pos = i+1;
2944 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002945 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002946 Py_INCREF(value);
2947 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002948
2949fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002950 Py_DECREF(d);
2951 di->di_dict = NULL;
2952 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002953}
2954
2955PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002956 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2957 "dict_valueiterator", /* tp_name */
2958 sizeof(dictiterobject), /* tp_basicsize */
2959 0, /* tp_itemsize */
2960 /* methods */
2961 (destructor)dictiter_dealloc, /* tp_dealloc */
2962 0, /* tp_print */
2963 0, /* tp_getattr */
2964 0, /* tp_setattr */
2965 0, /* tp_reserved */
2966 0, /* tp_repr */
2967 0, /* tp_as_number */
2968 0, /* tp_as_sequence */
2969 0, /* tp_as_mapping */
2970 0, /* tp_hash */
2971 0, /* tp_call */
2972 0, /* tp_str */
2973 PyObject_GenericGetAttr, /* tp_getattro */
2974 0, /* tp_setattro */
2975 0, /* tp_as_buffer */
2976 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2977 0, /* tp_doc */
2978 (traverseproc)dictiter_traverse, /* tp_traverse */
2979 0, /* tp_clear */
2980 0, /* tp_richcompare */
2981 0, /* tp_weaklistoffset */
2982 PyObject_SelfIter, /* tp_iter */
2983 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
2984 dictiter_methods, /* tp_methods */
2985 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002986};
2987
2988static PyObject *dictiter_iternextitem(dictiterobject *di)
2989{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002990 PyObject *key, *value, *result = di->di_result;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002991 Py_ssize_t i, mask, offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002992 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002993 PyObject **value_ptr;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002995 if (d == NULL)
2996 return NULL;
2997 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00002998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002999 if (di->di_used != d->ma_used) {
3000 PyErr_SetString(PyExc_RuntimeError,
3001 "dictionary changed size during iteration");
3002 di->di_used = -1; /* Make this state sticky */
3003 return NULL;
3004 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003006 i = di->di_pos;
3007 if (i < 0)
3008 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003009 mask = DK_SIZE(d->ma_keys)-1;
3010 if (d->ma_values) {
3011 value_ptr = &d->ma_values[i];
3012 offset = sizeof(PyObject *);
3013 }
3014 else {
3015 value_ptr = &d->ma_keys->dk_entries[i].me_value;
3016 offset = sizeof(PyDictKeyEntry);
3017 }
3018 while (i <= mask && *value_ptr == NULL) {
3019 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003020 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003021 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003022 di->di_pos = i+1;
3023 if (i > mask)
3024 goto fail;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003026 if (result->ob_refcnt == 1) {
3027 Py_INCREF(result);
3028 Py_DECREF(PyTuple_GET_ITEM(result, 0));
3029 Py_DECREF(PyTuple_GET_ITEM(result, 1));
3030 } else {
3031 result = PyTuple_New(2);
3032 if (result == NULL)
3033 return NULL;
3034 }
3035 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003036 key = d->ma_keys->dk_entries[i].me_key;
3037 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003038 Py_INCREF(key);
3039 Py_INCREF(value);
3040 PyTuple_SET_ITEM(result, 0, key);
3041 PyTuple_SET_ITEM(result, 1, value);
3042 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003043
3044fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003045 Py_DECREF(d);
3046 di->di_dict = NULL;
3047 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003048}
3049
3050PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003051 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3052 "dict_itemiterator", /* tp_name */
3053 sizeof(dictiterobject), /* tp_basicsize */
3054 0, /* tp_itemsize */
3055 /* methods */
3056 (destructor)dictiter_dealloc, /* tp_dealloc */
3057 0, /* tp_print */
3058 0, /* tp_getattr */
3059 0, /* tp_setattr */
3060 0, /* tp_reserved */
3061 0, /* tp_repr */
3062 0, /* tp_as_number */
3063 0, /* tp_as_sequence */
3064 0, /* tp_as_mapping */
3065 0, /* tp_hash */
3066 0, /* tp_call */
3067 0, /* tp_str */
3068 PyObject_GenericGetAttr, /* tp_getattro */
3069 0, /* tp_setattro */
3070 0, /* tp_as_buffer */
3071 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3072 0, /* tp_doc */
3073 (traverseproc)dictiter_traverse, /* tp_traverse */
3074 0, /* tp_clear */
3075 0, /* tp_richcompare */
3076 0, /* tp_weaklistoffset */
3077 PyObject_SelfIter, /* tp_iter */
3078 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3079 dictiter_methods, /* tp_methods */
3080 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003081};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003082
3083
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003084static PyObject *
3085dictiter_reduce(dictiterobject *di)
3086{
3087 PyObject *list;
3088 dictiterobject tmp;
3089
3090 list = PyList_New(0);
3091 if (!list)
3092 return NULL;
3093
3094 /* copy the itertor state */
3095 tmp = *di;
3096 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003097
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003098 /* iterate the temporary into a list */
3099 for(;;) {
3100 PyObject *element = 0;
3101 if (Py_TYPE(di) == &PyDictIterItem_Type)
3102 element = dictiter_iternextitem(&tmp);
3103 else if (Py_TYPE(di) == &PyDictIterKey_Type)
3104 element = dictiter_iternextkey(&tmp);
3105 else if (Py_TYPE(di) == &PyDictIterValue_Type)
3106 element = dictiter_iternextvalue(&tmp);
3107 else
3108 assert(0);
3109 if (element) {
3110 if (PyList_Append(list, element)) {
3111 Py_DECREF(element);
3112 Py_DECREF(list);
3113 Py_XDECREF(tmp.di_dict);
3114 return NULL;
3115 }
3116 Py_DECREF(element);
3117 } else
3118 break;
3119 }
3120 Py_XDECREF(tmp.di_dict);
3121 /* check for error */
3122 if (tmp.di_dict != NULL) {
3123 /* we have an error */
3124 Py_DECREF(list);
3125 return NULL;
3126 }
Antoine Pitroua7013882012-04-05 00:04:20 +02003127 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003128}
3129
Guido van Rossum3ac67412007-02-10 18:55:06 +00003130/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003131/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003132/***********************************************/
3133
Guido van Rossumb90c8482007-02-10 01:11:45 +00003134/* The instance lay-out is the same for all three; but the type differs. */
3135
3136typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003137 PyObject_HEAD
3138 PyDictObject *dv_dict;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003139} dictviewobject;
3140
3141
3142static void
Guido van Rossum3ac67412007-02-10 18:55:06 +00003143dictview_dealloc(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003144{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003145 Py_XDECREF(dv->dv_dict);
3146 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003147}
3148
3149static int
3150dictview_traverse(dictviewobject *dv, visitproc visit, void *arg)
3151{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003152 Py_VISIT(dv->dv_dict);
3153 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003154}
3155
Guido van Rossum83825ac2007-02-10 04:54:19 +00003156static Py_ssize_t
Guido van Rossum3ac67412007-02-10 18:55:06 +00003157dictview_len(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003158{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003159 Py_ssize_t len = 0;
3160 if (dv->dv_dict != NULL)
3161 len = dv->dv_dict->ma_used;
3162 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003163}
3164
3165static PyObject *
3166dictview_new(PyObject *dict, PyTypeObject *type)
3167{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003168 dictviewobject *dv;
3169 if (dict == NULL) {
3170 PyErr_BadInternalCall();
3171 return NULL;
3172 }
3173 if (!PyDict_Check(dict)) {
3174 /* XXX Get rid of this restriction later */
3175 PyErr_Format(PyExc_TypeError,
3176 "%s() requires a dict argument, not '%s'",
3177 type->tp_name, dict->ob_type->tp_name);
3178 return NULL;
3179 }
3180 dv = PyObject_GC_New(dictviewobject, type);
3181 if (dv == NULL)
3182 return NULL;
3183 Py_INCREF(dict);
3184 dv->dv_dict = (PyDictObject *)dict;
3185 _PyObject_GC_TRACK(dv);
3186 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003187}
3188
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003189/* TODO(guido): The views objects are not complete:
3190
3191 * support more set operations
3192 * support arbitrary mappings?
3193 - either these should be static or exported in dictobject.h
3194 - if public then they should probably be in builtins
3195*/
3196
Guido van Rossumaac530c2007-08-24 22:33:45 +00003197/* Return 1 if self is a subset of other, iterating over self;
3198 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003199static int
3200all_contained_in(PyObject *self, PyObject *other)
3201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003202 PyObject *iter = PyObject_GetIter(self);
3203 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003204
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003205 if (iter == NULL)
3206 return -1;
3207 for (;;) {
3208 PyObject *next = PyIter_Next(iter);
3209 if (next == NULL) {
3210 if (PyErr_Occurred())
3211 ok = -1;
3212 break;
3213 }
3214 ok = PySequence_Contains(other, next);
3215 Py_DECREF(next);
3216 if (ok <= 0)
3217 break;
3218 }
3219 Py_DECREF(iter);
3220 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003221}
3222
3223static PyObject *
3224dictview_richcompare(PyObject *self, PyObject *other, int op)
3225{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003226 Py_ssize_t len_self, len_other;
3227 int ok;
3228 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003230 assert(self != NULL);
3231 assert(PyDictViewSet_Check(self));
3232 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00003233
Brian Curtindfc80e32011-08-10 20:28:54 -05003234 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
3235 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003237 len_self = PyObject_Size(self);
3238 if (len_self < 0)
3239 return NULL;
3240 len_other = PyObject_Size(other);
3241 if (len_other < 0)
3242 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003244 ok = 0;
3245 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00003246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003247 case Py_NE:
3248 case Py_EQ:
3249 if (len_self == len_other)
3250 ok = all_contained_in(self, other);
3251 if (op == Py_NE && ok >= 0)
3252 ok = !ok;
3253 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003255 case Py_LT:
3256 if (len_self < len_other)
3257 ok = all_contained_in(self, other);
3258 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003260 case Py_LE:
3261 if (len_self <= len_other)
3262 ok = all_contained_in(self, other);
3263 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003265 case Py_GT:
3266 if (len_self > len_other)
3267 ok = all_contained_in(other, self);
3268 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003270 case Py_GE:
3271 if (len_self >= len_other)
3272 ok = all_contained_in(other, self);
3273 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003275 }
3276 if (ok < 0)
3277 return NULL;
3278 result = ok ? Py_True : Py_False;
3279 Py_INCREF(result);
3280 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003281}
3282
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003283static PyObject *
3284dictview_repr(dictviewobject *dv)
3285{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003286 PyObject *seq;
3287 PyObject *result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003289 seq = PySequence_List((PyObject *)dv);
3290 if (seq == NULL)
3291 return NULL;
3292
3293 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
3294 Py_DECREF(seq);
3295 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003296}
3297
Guido van Rossum3ac67412007-02-10 18:55:06 +00003298/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003299
3300static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003301dictkeys_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003302{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003303 if (dv->dv_dict == NULL) {
3304 Py_RETURN_NONE;
3305 }
3306 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003307}
3308
3309static int
3310dictkeys_contains(dictviewobject *dv, PyObject *obj)
3311{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003312 if (dv->dv_dict == NULL)
3313 return 0;
3314 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003315}
3316
Guido van Rossum83825ac2007-02-10 04:54:19 +00003317static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003318 (lenfunc)dictview_len, /* sq_length */
3319 0, /* sq_concat */
3320 0, /* sq_repeat */
3321 0, /* sq_item */
3322 0, /* sq_slice */
3323 0, /* sq_ass_item */
3324 0, /* sq_ass_slice */
3325 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003326};
3327
Guido van Rossum523259b2007-08-24 23:41:22 +00003328static PyObject*
3329dictviews_sub(PyObject* self, PyObject *other)
3330{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003331 PyObject *result = PySet_New(self);
3332 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003333 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003335 if (result == NULL)
3336 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003337
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003338 tmp = _PyObject_CallMethodId(result, &PyId_difference_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003339 if (tmp == NULL) {
3340 Py_DECREF(result);
3341 return NULL;
3342 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003344 Py_DECREF(tmp);
3345 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003346}
3347
3348static PyObject*
3349dictviews_and(PyObject* self, PyObject *other)
3350{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003351 PyObject *result = PySet_New(self);
3352 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003353 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003355 if (result == NULL)
3356 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003357
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003358 tmp = _PyObject_CallMethodId(result, &PyId_intersection_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003359 if (tmp == NULL) {
3360 Py_DECREF(result);
3361 return NULL;
3362 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003364 Py_DECREF(tmp);
3365 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003366}
3367
3368static PyObject*
3369dictviews_or(PyObject* self, PyObject *other)
3370{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003371 PyObject *result = PySet_New(self);
3372 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003373 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003375 if (result == NULL)
3376 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003377
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003378 tmp = _PyObject_CallMethodId(result, &PyId_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003379 if (tmp == NULL) {
3380 Py_DECREF(result);
3381 return NULL;
3382 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003384 Py_DECREF(tmp);
3385 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003386}
3387
3388static PyObject*
3389dictviews_xor(PyObject* self, PyObject *other)
3390{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003391 PyObject *result = PySet_New(self);
3392 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003393 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003395 if (result == NULL)
3396 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003397
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003398 tmp = _PyObject_CallMethodId(result, &PyId_symmetric_difference_update, "O",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003399 other);
3400 if (tmp == NULL) {
3401 Py_DECREF(result);
3402 return NULL;
3403 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003405 Py_DECREF(tmp);
3406 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003407}
3408
3409static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003410 0, /*nb_add*/
3411 (binaryfunc)dictviews_sub, /*nb_subtract*/
3412 0, /*nb_multiply*/
3413 0, /*nb_remainder*/
3414 0, /*nb_divmod*/
3415 0, /*nb_power*/
3416 0, /*nb_negative*/
3417 0, /*nb_positive*/
3418 0, /*nb_absolute*/
3419 0, /*nb_bool*/
3420 0, /*nb_invert*/
3421 0, /*nb_lshift*/
3422 0, /*nb_rshift*/
3423 (binaryfunc)dictviews_and, /*nb_and*/
3424 (binaryfunc)dictviews_xor, /*nb_xor*/
3425 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00003426};
3427
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003428static PyObject*
3429dictviews_isdisjoint(PyObject *self, PyObject *other)
3430{
3431 PyObject *it;
3432 PyObject *item = NULL;
3433
3434 if (self == other) {
3435 if (dictview_len((dictviewobject *)self) == 0)
3436 Py_RETURN_TRUE;
3437 else
3438 Py_RETURN_FALSE;
3439 }
3440
3441 /* Iterate over the shorter object (only if other is a set,
3442 * because PySequence_Contains may be expensive otherwise): */
3443 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
3444 Py_ssize_t len_self = dictview_len((dictviewobject *)self);
3445 Py_ssize_t len_other = PyObject_Size(other);
3446 if (len_other == -1)
3447 return NULL;
3448
3449 if ((len_other > len_self)) {
3450 PyObject *tmp = other;
3451 other = self;
3452 self = tmp;
3453 }
3454 }
3455
3456 it = PyObject_GetIter(other);
3457 if (it == NULL)
3458 return NULL;
3459
3460 while ((item = PyIter_Next(it)) != NULL) {
3461 int contains = PySequence_Contains(self, item);
3462 Py_DECREF(item);
3463 if (contains == -1) {
3464 Py_DECREF(it);
3465 return NULL;
3466 }
3467
3468 if (contains) {
3469 Py_DECREF(it);
3470 Py_RETURN_FALSE;
3471 }
3472 }
3473 Py_DECREF(it);
3474 if (PyErr_Occurred())
3475 return NULL; /* PyIter_Next raised an exception. */
3476 Py_RETURN_TRUE;
3477}
3478
3479PyDoc_STRVAR(isdisjoint_doc,
3480"Return True if the view and the given iterable have a null intersection.");
3481
Guido van Rossumb90c8482007-02-10 01:11:45 +00003482static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003483 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3484 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003485 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003486};
3487
3488PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003489 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3490 "dict_keys", /* tp_name */
3491 sizeof(dictviewobject), /* tp_basicsize */
3492 0, /* tp_itemsize */
3493 /* methods */
3494 (destructor)dictview_dealloc, /* tp_dealloc */
3495 0, /* tp_print */
3496 0, /* tp_getattr */
3497 0, /* tp_setattr */
3498 0, /* tp_reserved */
3499 (reprfunc)dictview_repr, /* tp_repr */
3500 &dictviews_as_number, /* tp_as_number */
3501 &dictkeys_as_sequence, /* tp_as_sequence */
3502 0, /* tp_as_mapping */
3503 0, /* tp_hash */
3504 0, /* tp_call */
3505 0, /* tp_str */
3506 PyObject_GenericGetAttr, /* tp_getattro */
3507 0, /* tp_setattro */
3508 0, /* tp_as_buffer */
3509 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3510 0, /* tp_doc */
3511 (traverseproc)dictview_traverse, /* tp_traverse */
3512 0, /* tp_clear */
3513 dictview_richcompare, /* tp_richcompare */
3514 0, /* tp_weaklistoffset */
3515 (getiterfunc)dictkeys_iter, /* tp_iter */
3516 0, /* tp_iternext */
3517 dictkeys_methods, /* tp_methods */
3518 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003519};
3520
3521static PyObject *
3522dictkeys_new(PyObject *dict)
3523{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003524 return dictview_new(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003525}
3526
Guido van Rossum3ac67412007-02-10 18:55:06 +00003527/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003528
3529static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003530dictitems_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003531{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003532 if (dv->dv_dict == NULL) {
3533 Py_RETURN_NONE;
3534 }
3535 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003536}
3537
3538static int
3539dictitems_contains(dictviewobject *dv, PyObject *obj)
3540{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003541 PyObject *key, *value, *found;
3542 if (dv->dv_dict == NULL)
3543 return 0;
3544 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
3545 return 0;
3546 key = PyTuple_GET_ITEM(obj, 0);
3547 value = PyTuple_GET_ITEM(obj, 1);
3548 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
3549 if (found == NULL) {
3550 if (PyErr_Occurred())
3551 return -1;
3552 return 0;
3553 }
3554 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003555}
3556
Guido van Rossum83825ac2007-02-10 04:54:19 +00003557static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003558 (lenfunc)dictview_len, /* sq_length */
3559 0, /* sq_concat */
3560 0, /* sq_repeat */
3561 0, /* sq_item */
3562 0, /* sq_slice */
3563 0, /* sq_ass_item */
3564 0, /* sq_ass_slice */
3565 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003566};
3567
Guido van Rossumb90c8482007-02-10 01:11:45 +00003568static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003569 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3570 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003571 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003572};
3573
3574PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003575 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3576 "dict_items", /* tp_name */
3577 sizeof(dictviewobject), /* tp_basicsize */
3578 0, /* tp_itemsize */
3579 /* methods */
3580 (destructor)dictview_dealloc, /* tp_dealloc */
3581 0, /* tp_print */
3582 0, /* tp_getattr */
3583 0, /* tp_setattr */
3584 0, /* tp_reserved */
3585 (reprfunc)dictview_repr, /* tp_repr */
3586 &dictviews_as_number, /* tp_as_number */
3587 &dictitems_as_sequence, /* tp_as_sequence */
3588 0, /* tp_as_mapping */
3589 0, /* tp_hash */
3590 0, /* tp_call */
3591 0, /* tp_str */
3592 PyObject_GenericGetAttr, /* tp_getattro */
3593 0, /* tp_setattro */
3594 0, /* tp_as_buffer */
3595 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3596 0, /* tp_doc */
3597 (traverseproc)dictview_traverse, /* tp_traverse */
3598 0, /* tp_clear */
3599 dictview_richcompare, /* tp_richcompare */
3600 0, /* tp_weaklistoffset */
3601 (getiterfunc)dictitems_iter, /* tp_iter */
3602 0, /* tp_iternext */
3603 dictitems_methods, /* tp_methods */
3604 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003605};
3606
3607static PyObject *
3608dictitems_new(PyObject *dict)
3609{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003610 return dictview_new(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003611}
3612
Guido van Rossum3ac67412007-02-10 18:55:06 +00003613/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003614
3615static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003616dictvalues_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003617{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003618 if (dv->dv_dict == NULL) {
3619 Py_RETURN_NONE;
3620 }
3621 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003622}
3623
Guido van Rossum83825ac2007-02-10 04:54:19 +00003624static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003625 (lenfunc)dictview_len, /* sq_length */
3626 0, /* sq_concat */
3627 0, /* sq_repeat */
3628 0, /* sq_item */
3629 0, /* sq_slice */
3630 0, /* sq_ass_item */
3631 0, /* sq_ass_slice */
3632 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003633};
3634
Guido van Rossumb90c8482007-02-10 01:11:45 +00003635static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003636 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003637};
3638
3639PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003640 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3641 "dict_values", /* tp_name */
3642 sizeof(dictviewobject), /* tp_basicsize */
3643 0, /* tp_itemsize */
3644 /* methods */
3645 (destructor)dictview_dealloc, /* tp_dealloc */
3646 0, /* tp_print */
3647 0, /* tp_getattr */
3648 0, /* tp_setattr */
3649 0, /* tp_reserved */
3650 (reprfunc)dictview_repr, /* tp_repr */
3651 0, /* tp_as_number */
3652 &dictvalues_as_sequence, /* tp_as_sequence */
3653 0, /* tp_as_mapping */
3654 0, /* tp_hash */
3655 0, /* tp_call */
3656 0, /* tp_str */
3657 PyObject_GenericGetAttr, /* tp_getattro */
3658 0, /* tp_setattro */
3659 0, /* tp_as_buffer */
3660 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3661 0, /* tp_doc */
3662 (traverseproc)dictview_traverse, /* tp_traverse */
3663 0, /* tp_clear */
3664 0, /* tp_richcompare */
3665 0, /* tp_weaklistoffset */
3666 (getiterfunc)dictvalues_iter, /* tp_iter */
3667 0, /* tp_iternext */
3668 dictvalues_methods, /* tp_methods */
3669 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003670};
3671
3672static PyObject *
3673dictvalues_new(PyObject *dict)
3674{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003675 return dictview_new(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003676}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003677
3678/* Returns NULL if cannot allocate a new PyDictKeysObject,
3679 but does not set an error */
3680PyDictKeysObject *
3681_PyDict_NewKeysForClass(void)
3682{
3683 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE_SPLIT);
3684 if (keys == NULL)
3685 PyErr_Clear();
3686 else
3687 keys->dk_lookup = lookdict_split;
3688 return keys;
3689}
3690
3691#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
3692
3693PyObject *
3694PyObject_GenericGetDict(PyObject *obj, void *context)
3695{
3696 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
3697 if (dictptr == NULL) {
3698 PyErr_SetString(PyExc_AttributeError,
3699 "This object has no __dict__");
3700 return NULL;
3701 }
3702 dict = *dictptr;
3703 if (dict == NULL) {
3704 PyTypeObject *tp = Py_TYPE(obj);
3705 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
3706 DK_INCREF(CACHED_KEYS(tp));
3707 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
3708 }
3709 else {
3710 *dictptr = dict = PyDict_New();
3711 }
3712 }
3713 Py_XINCREF(dict);
3714 return dict;
3715}
3716
3717int
3718_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
3719 PyObject *key, PyObject *value)
3720{
3721 PyObject *dict;
3722 int res;
3723 PyDictKeysObject *cached;
3724
3725 assert(dictptr != NULL);
3726 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
3727 assert(dictptr != NULL);
3728 dict = *dictptr;
3729 if (dict == NULL) {
3730 DK_INCREF(cached);
3731 dict = new_dict_with_shared_keys(cached);
3732 if (dict == NULL)
3733 return -1;
3734 *dictptr = dict;
3735 }
3736 if (value == NULL) {
3737 res = PyDict_DelItem(dict, key);
3738 if (cached != ((PyDictObject *)dict)->ma_keys) {
3739 CACHED_KEYS(tp) = NULL;
3740 DK_DECREF(cached);
3741 }
3742 } else {
3743 res = PyDict_SetItem(dict, key, value);
3744 if (cached != ((PyDictObject *)dict)->ma_keys) {
3745 /* Either update tp->ht_cached_keys or delete it */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04003746 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003747 CACHED_KEYS(tp) = make_keys_shared(dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003748 } else {
3749 CACHED_KEYS(tp) = NULL;
3750 }
3751 DK_DECREF(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04003752 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
3753 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003754 }
3755 }
3756 } else {
3757 dict = *dictptr;
3758 if (dict == NULL) {
3759 dict = PyDict_New();
3760 if (dict == NULL)
3761 return -1;
3762 *dictptr = dict;
3763 }
3764 if (value == NULL) {
3765 res = PyDict_DelItem(dict, key);
3766 } else {
3767 res = PyDict_SetItem(dict, key, value);
3768 }
3769 }
3770 return res;
3771}
3772
3773void
3774_PyDictKeys_DecRef(PyDictKeysObject *keys)
3775{
3776 DK_DECREF(keys);
3777}
3778
3779
3780/* ARGSUSED */
3781static PyObject *
3782dummy_repr(PyObject *op)
3783{
3784 return PyUnicode_FromString("<dummy key>");
3785}
3786
3787/* ARGUSED */
3788static void
3789dummy_dealloc(PyObject* ignore)
3790{
3791 /* This should never get called, but we also don't want to SEGV if
3792 * we accidentally decref dummy-key out of existence.
3793 */
3794 Py_FatalError("deallocating <dummy key>");
3795}
3796
3797static PyTypeObject PyDictDummy_Type = {
3798 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3799 "<dummy key> type",
3800 0,
3801 0,
3802 dummy_dealloc, /*tp_dealloc*/ /*never called*/
3803 0, /*tp_print*/
3804 0, /*tp_getattr*/
3805 0, /*tp_setattr*/
3806 0, /*tp_reserved*/
3807 dummy_repr, /*tp_repr*/
3808 0, /*tp_as_number*/
3809 0, /*tp_as_sequence*/
3810 0, /*tp_as_mapping*/
3811 0, /*tp_hash */
3812 0, /*tp_call */
3813 0, /*tp_str */
3814 0, /*tp_getattro */
3815 0, /*tp_setattro */
3816 0, /*tp_as_buffer */
3817 Py_TPFLAGS_DEFAULT, /*tp_flags */
3818};
3819
3820static PyObject _dummy_struct = {
3821 _PyObject_EXTRA_INIT
3822 2, &PyDictDummy_Type
3823};
3824