blob: 8c09e46f9bc5600ab1b01df1c2f70158138d0e52 [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
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200308/* GROWTH_RATE. Growth rate upon hitting maximum load. Currently set to *2.
309 * Raising this to *4 doubles memory consumption depending on the size of
310 * the dictionary, but results in half the number of resizes, less effort to
311 * resize and better sparseness for some (but not all dict sizes).
312 * Setting to *4 eliminates every other resize step.
313 * GROWTH_RATE was set to *4 up to version 3.2.
314 */
315#define GROWTH_RATE(x) ((x) * 2)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400316
317#define ENSURE_ALLOWS_DELETIONS(d) \
318 if ((d)->ma_keys->dk_lookup == lookdict_unicode_nodummy) { \
319 (d)->ma_keys->dk_lookup = lookdict_unicode; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400321
322/* This immutable, empty PyDictKeysObject is used for PyDict_Clear()
323 * (which cannot fail and thus can do no allocation).
324 */
325static PyDictKeysObject empty_keys_struct = {
326 2, /* dk_refcnt 1 for this struct, 1 for dummy_struct */
327 1, /* dk_size */
328 lookdict_split, /* dk_lookup */
329 0, /* dk_usable (immutable) */
330 {
331 { 0, 0, 0 } /* dk_entries (empty) */
332 }
333};
334
335static PyObject *empty_values[1] = { NULL };
336
337#define Py_EMPTY_KEYS &empty_keys_struct
338
339static PyDictKeysObject *new_keys_object(Py_ssize_t size)
340{
341 PyDictKeysObject *dk;
342 Py_ssize_t i;
343 PyDictKeyEntry *ep0;
344
345 assert(size >= PyDict_MINSIZE_SPLIT);
346 assert(IS_POWER_OF_2(size));
347 dk = PyMem_MALLOC(sizeof(PyDictKeysObject) +
348 sizeof(PyDictKeyEntry) * (size-1));
349 if (dk == NULL) {
350 PyErr_NoMemory();
351 return NULL;
352 }
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200353 DK_DEBUG_INCREF dk->dk_refcnt = 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400354 dk->dk_size = size;
355 dk->dk_usable = USABLE_FRACTION(size);
356 ep0 = &dk->dk_entries[0];
357 /* Hash value of slot 0 is used by popitem, so it must be initialized */
358 ep0->me_hash = 0;
359 for (i = 0; i < size; i++) {
360 ep0[i].me_key = NULL;
361 ep0[i].me_value = NULL;
362 }
363 dk->dk_lookup = lookdict_unicode_nodummy;
364 return dk;
365}
366
367static void
368free_keys_object(PyDictKeysObject *keys)
369{
370 PyDictKeyEntry *entries = &keys->dk_entries[0];
371 Py_ssize_t i, n;
372 for (i = 0, n = DK_SIZE(keys); i < n; i++) {
373 Py_XDECREF(entries[i].me_key);
374 Py_XDECREF(entries[i].me_value);
375 }
376 PyMem_FREE(keys);
377}
378
379#define new_values(size) PyMem_NEW(PyObject *, size)
380
381#define free_values(values) PyMem_FREE(values)
382
383/* Consumes a reference to the keys object */
384static PyObject *
385new_dict(PyDictKeysObject *keys, PyObject **values)
386{
387 PyDictObject *mp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 if (numfree) {
389 mp = free_list[--numfree];
390 assert (mp != NULL);
391 assert (Py_TYPE(mp) == &PyDict_Type);
392 _Py_NewReference((PyObject *)mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400394 else {
395 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
396 if (mp == NULL) {
397 DK_DECREF(keys);
398 free_values(values);
399 return NULL;
400 }
401 }
402 mp->ma_keys = keys;
403 mp->ma_values = values;
404 mp->ma_used = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000406}
407
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400408/* Consumes a reference to the keys object */
409static PyObject *
410new_dict_with_shared_keys(PyDictKeysObject *keys)
411{
412 PyObject **values;
413 Py_ssize_t i, size;
414
415 size = DK_SIZE(keys);
416 values = new_values(size);
417 if (values == NULL) {
418 DK_DECREF(keys);
419 return PyErr_NoMemory();
420 }
421 for (i = 0; i < size; i++) {
422 values[i] = NULL;
423 }
424 return new_dict(keys, values);
425}
426
427PyObject *
428PyDict_New(void)
429{
430 return new_dict(new_keys_object(PyDict_MINSIZE_COMBINED), NULL);
431}
432
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000433/*
434The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000435This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000436Open addressing is preferred over chaining since the link overhead for
437chaining would be substantial (100% with typical malloc overhead).
438
Tim Peterseb28ef22001-06-02 05:27:19 +0000439The initial probe index is computed as hash mod the table size. Subsequent
440probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000441
442All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000443
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000444The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000445contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000446Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000447
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000448lookdict() is general-purpose, and may return NULL if (and only if) a
449comparison raises an exception (this was new in Python 2.5).
Guido van Rossum89d8c602007-09-18 17:26:56 +0000450lookdict_unicode() below is specialized to string keys, comparison of which can
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400451never raise an exception; that function can never return NULL.
452lookdict_unicode_nodummy is further specialized for string keys that cannot be
453the <dummy> value.
454For both, when the key isn't found a PyDictEntry* is returned
455where the key would have been found, *value_addr points to the matching value
456slot.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000457*/
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400458static PyDictKeyEntry *
459lookdict(PyDictObject *mp, PyObject *key,
460 Py_hash_t hash, PyObject ***value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000461{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 register size_t i;
463 register size_t perturb;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400464 register PyDictKeyEntry *freeslot;
Antoine Pitrou9a234902012-05-13 20:48:01 +0200465 register size_t mask;
466 PyDictKeyEntry *ep0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400467 register PyDictKeyEntry *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 register int cmp;
469 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000470
Antoine Pitrou9a234902012-05-13 20:48:01 +0200471top:
472 mask = DK_MASK(mp->ma_keys);
473 ep0 = &mp->ma_keys->dk_entries[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 i = (size_t)hash & mask;
475 ep = &ep0[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400476 if (ep->me_key == NULL || ep->me_key == key) {
477 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400479 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 if (ep->me_key == dummy)
481 freeslot = ep;
482 else {
483 if (ep->me_hash == hash) {
484 startkey = ep->me_key;
485 Py_INCREF(startkey);
486 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
487 Py_DECREF(startkey);
488 if (cmp < 0)
489 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400490 if (ep0 == mp->ma_keys->dk_entries && ep->me_key == startkey) {
491 if (cmp > 0) {
492 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400494 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 }
496 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200497 /* The dict was mutated, restart */
498 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 }
500 }
501 freeslot = NULL;
502 }
Tim Peters15d49292001-05-27 07:39:22 +0000503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 /* In the loop, me_key == dummy is by far (factor of 100s) the
505 least likely outcome, so test for that last. */
506 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
507 i = (i << 2) + i + perturb + 1;
508 ep = &ep0[i & mask];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400509 if (ep->me_key == NULL) {
510 if (freeslot == NULL) {
511 *value_addr = &ep->me_value;
512 return ep;
513 } else {
514 *value_addr = &freeslot->me_value;
515 return freeslot;
516 }
517 }
518 if (ep->me_key == key) {
519 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400521 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 if (ep->me_hash == hash && ep->me_key != dummy) {
523 startkey = ep->me_key;
524 Py_INCREF(startkey);
525 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
526 Py_DECREF(startkey);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400527 if (cmp < 0) {
528 *value_addr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400530 }
531 if (ep0 == mp->ma_keys->dk_entries && ep->me_key == startkey) {
532 if (cmp > 0) {
533 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400535 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 }
537 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200538 /* The dict was mutated, restart */
539 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 }
541 }
542 else if (ep->me_key == dummy && freeslot == NULL)
543 freeslot = ep;
544 }
545 assert(0); /* NOT REACHED */
546 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000547}
548
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400549/* Specialized version for string-only keys */
550static PyDictKeyEntry *
551lookdict_unicode(PyDictObject *mp, PyObject *key,
552 Py_hash_t hash, PyObject ***value_addr)
Fred Drake1bff34a2000-08-31 19:31:38 +0000553{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 register size_t i;
555 register size_t perturb;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400556 register PyDictKeyEntry *freeslot;
557 register size_t mask = DK_MASK(mp->ma_keys);
558 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
559 register PyDictKeyEntry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 /* Make sure this function doesn't have to handle non-unicode keys,
562 including subclasses of str; e.g., one reason to subclass
563 unicodes is to override __eq__, and for speed we don't cater to
564 that here. */
565 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400566 mp->ma_keys->dk_lookup = lookdict;
567 return lookdict(mp, key, hash, value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 }
Mark Dickinson57e683e2011-09-24 18:18:40 +0100569 i = (size_t)hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 ep = &ep0[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400571 if (ep->me_key == NULL || ep->me_key == key) {
572 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400574 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 if (ep->me_key == dummy)
576 freeslot = ep;
577 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400578 if (ep->me_hash == hash && unicode_eq(ep->me_key, key)) {
579 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400581 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 freeslot = NULL;
583 }
Tim Peters15d49292001-05-27 07:39:22 +0000584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 /* In the loop, me_key == dummy is by far (factor of 100s) the
586 least likely outcome, so test for that last. */
587 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
588 i = (i << 2) + i + perturb + 1;
589 ep = &ep0[i & mask];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400590 if (ep->me_key == NULL) {
591 if (freeslot == NULL) {
592 *value_addr = &ep->me_value;
593 return ep;
594 } else {
595 *value_addr = &freeslot->me_value;
596 return freeslot;
597 }
598 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 if (ep->me_key == key
600 || (ep->me_hash == hash
601 && ep->me_key != dummy
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400602 && unicode_eq(ep->me_key, key))) {
603 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400605 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 if (ep->me_key == dummy && freeslot == NULL)
607 freeslot = ep;
608 }
609 assert(0); /* NOT REACHED */
610 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000611}
612
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400613/* Faster version of lookdict_unicode when it is known that no <dummy> keys
614 * will be present. */
615static PyDictKeyEntry *
616lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
617 Py_hash_t hash, PyObject ***value_addr)
618{
619 register size_t i;
620 register size_t perturb;
621 register size_t mask = DK_MASK(mp->ma_keys);
622 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
623 register PyDictKeyEntry *ep;
624
625 /* Make sure this function doesn't have to handle non-unicode keys,
626 including subclasses of str; e.g., one reason to subclass
627 unicodes is to override __eq__, and for speed we don't cater to
628 that here. */
629 if (!PyUnicode_CheckExact(key)) {
630 mp->ma_keys->dk_lookup = lookdict;
631 return lookdict(mp, key, hash, value_addr);
632 }
633 i = (size_t)hash & mask;
634 ep = &ep0[i];
635 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
636 if (ep->me_key == NULL || ep->me_key == key ||
637 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
638 *value_addr = &ep->me_value;
639 return ep;
640 }
641 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
642 i = (i << 2) + i + perturb + 1;
643 ep = &ep0[i & mask];
644 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
645 if (ep->me_key == NULL || ep->me_key == key ||
646 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
647 *value_addr = &ep->me_value;
648 return ep;
649 }
650 }
651 assert(0); /* NOT REACHED */
652 return 0;
653}
654
655/* Version of lookdict for split tables.
656 * All split tables and only split tables use this lookup function.
657 * Split tables only contain unicode keys and no dummy keys,
658 * so algorithm is the same as lookdict_unicode_nodummy.
659 */
660static PyDictKeyEntry *
661lookdict_split(PyDictObject *mp, PyObject *key,
662 Py_hash_t hash, PyObject ***value_addr)
663{
664 register size_t i;
665 register size_t perturb;
666 register size_t mask = DK_MASK(mp->ma_keys);
667 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
668 register PyDictKeyEntry *ep;
669
670 if (!PyUnicode_CheckExact(key)) {
Benjamin Petersondb780d02012-04-23 13:44:32 -0400671 ep = lookdict(mp, key, hash, value_addr);
672 /* lookdict expects a combined-table, so fix value_addr */
673 i = ep - ep0;
674 *value_addr = &mp->ma_values[i];
675 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400676 }
677 i = (size_t)hash & mask;
678 ep = &ep0[i];
679 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
680 if (ep->me_key == NULL || ep->me_key == key ||
681 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
682 *value_addr = &mp->ma_values[i];
683 return ep;
684 }
685 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
686 i = (i << 2) + i + perturb + 1;
687 ep = &ep0[i & mask];
688 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
689 if (ep->me_key == NULL || ep->me_key == key ||
690 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
691 *value_addr = &mp->ma_values[i & mask];
692 return ep;
693 }
694 }
695 assert(0); /* NOT REACHED */
696 return 0;
697}
698
Benjamin Petersonfb886362010-04-24 18:21:17 +0000699int
700_PyDict_HasOnlyStringKeys(PyObject *dict)
701{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 Py_ssize_t pos = 0;
703 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000704 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400706 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 return 1;
708 while (PyDict_Next(dict, &pos, &key, &value))
709 if (!PyUnicode_Check(key))
710 return 0;
711 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000712}
713
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000714#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 do { \
716 if (!_PyObject_GC_IS_TRACKED(mp)) { \
717 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
718 _PyObject_GC_MAY_BE_TRACKED(value)) { \
719 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 } \
721 } \
722 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000723
724void
725_PyDict_MaybeUntrack(PyObject *op)
726{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 PyDictObject *mp;
728 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400729 Py_ssize_t i, size;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000730
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
732 return;
733
734 mp = (PyDictObject *) op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400735 size = DK_SIZE(mp->ma_keys);
736 if (_PyDict_HasSplitTable(mp)) {
737 for (i = 0; i < size; i++) {
738 if ((value = mp->ma_values[i]) == NULL)
739 continue;
740 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
741 assert(!_PyObject_GC_MAY_BE_TRACKED(
742 mp->ma_keys->dk_entries[i].me_key));
743 return;
744 }
745 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400747 else {
748 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
749 for (i = 0; i < size; i++) {
750 if ((value = ep0[i].me_value) == NULL)
751 continue;
752 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
753 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
754 return;
755 }
756 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000758}
759
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400760/* Internal function to find slot for an item from its hash
761 * when it is known that the key is not present in the dict.
762 */
763static PyDictKeyEntry *
764find_empty_slot(PyDictObject *mp, PyObject *key, Py_hash_t hash,
765 PyObject ***value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000766{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400767 size_t i;
768 size_t perturb;
769 size_t mask = DK_MASK(mp->ma_keys);
770 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
771 PyDictKeyEntry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000772
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400773 assert(key != NULL);
774 if (!PyUnicode_CheckExact(key))
775 mp->ma_keys->dk_lookup = lookdict;
776 i = hash & mask;
777 ep = &ep0[i];
778 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
779 i = (i << 2) + i + perturb + 1;
780 ep = &ep0[i & mask];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400782 assert(ep->me_value == NULL);
783 if (mp->ma_values)
784 *value_addr = &mp->ma_values[i & mask];
785 else
786 *value_addr = &ep->me_value;
787 return ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000788}
789
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400790static int
791insertion_resize(PyDictObject *mp)
792{
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200793 return dictresize(mp, GROWTH_RATE(mp->ma_used));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400794}
Antoine Pitroue965d972012-02-27 00:45:12 +0100795
796/*
797Internal routine to insert a new item into the table.
798Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +0100799Returns -1 if an error occurred, or 0 on success.
800*/
801static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400802insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +0100803{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400804 PyObject *old_value;
805 PyObject **value_addr;
806 PyDictKeyEntry *ep;
807 assert(key != dummy);
Antoine Pitroue965d972012-02-27 00:45:12 +0100808
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400809 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
810 if (insertion_resize(mp) < 0)
811 return -1;
812 }
813
814 ep = mp->ma_keys->dk_lookup(mp, key, hash, &value_addr);
Antoine Pitroue965d972012-02-27 00:45:12 +0100815 if (ep == NULL) {
Antoine Pitroue965d972012-02-27 00:45:12 +0100816 return -1;
817 }
Benjamin Petersona6f195e2012-04-30 10:23:40 -0400818 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400819 MAINTAIN_TRACKING(mp, key, value);
820 old_value = *value_addr;
821 if (old_value != NULL) {
822 assert(ep->me_key != NULL && ep->me_key != dummy);
823 *value_addr = value;
824 Py_DECREF(old_value); /* which **CAN** re-enter */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400825 }
826 else {
827 if (ep->me_key == NULL) {
Benjamin Petersona6f195e2012-04-30 10:23:40 -0400828 Py_INCREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400829 if (mp->ma_keys->dk_usable <= 0) {
830 /* Need to resize. */
831 if (insertion_resize(mp) < 0) {
832 Py_DECREF(key);
833 Py_DECREF(value);
834 return -1;
835 }
836 ep = find_empty_slot(mp, key, hash, &value_addr);
837 }
838 mp->ma_keys->dk_usable--;
839 assert(mp->ma_keys->dk_usable >= 0);
840 ep->me_key = key;
841 ep->me_hash = hash;
842 }
843 else {
844 if (ep->me_key == dummy) {
Benjamin Petersona6f195e2012-04-30 10:23:40 -0400845 Py_INCREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400846 ep->me_key = key;
847 ep->me_hash = hash;
848 Py_DECREF(dummy);
849 } else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400850 assert(_PyDict_HasSplitTable(mp));
851 }
852 }
853 mp->ma_used++;
854 *value_addr = value;
855 }
856 assert(ep->me_key != NULL && ep->me_key != dummy);
857 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
858 return 0;
Antoine Pitroue965d972012-02-27 00:45:12 +0100859}
860
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000861/*
862Internal routine used by dictresize() to insert an item which is
863known to be absent from the dict. This routine also assumes that
864the dict contains no deleted entries. Besides the performance benefit,
865using insertdict() in dictresize() is dangerous (SF bug #1456209).
866Note that no refcounts are changed by this routine; if needed, the caller
867is responsible for incref'ing `key` and `value`.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400868Neither mp->ma_used nor k->dk_usable are modified by this routine; the caller
869must set them correctly
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000870*/
871static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400872insertdict_clean(PyDictObject *mp, PyObject *key, Py_hash_t hash,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 PyObject *value)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000874{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400875 size_t i;
876 size_t perturb;
877 PyDictKeysObject *k = mp->ma_keys;
878 size_t mask = (size_t)DK_SIZE(k)-1;
879 PyDictKeyEntry *ep0 = &k->dk_entries[0];
880 PyDictKeyEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000881
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400882 assert(k->dk_lookup != NULL);
883 assert(value != NULL);
884 assert(key != NULL);
885 assert(key != dummy);
886 assert(PyUnicode_CheckExact(key) || k->dk_lookup == lookdict);
887 i = hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 ep = &ep0[i];
889 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
890 i = (i << 2) + i + perturb + 1;
891 ep = &ep0[i & mask];
892 }
893 assert(ep->me_value == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 ep->me_key = key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000895 ep->me_hash = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000897}
898
899/*
900Restructure the table by allocating a new table and reinserting all
901items again. When entries have been deleted, the new table may
902actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400903If a table is split (its keys and hashes are shared, its values are not),
904then the values are temporarily copied into the table, it is resized as
905a combined table, then the me_value slots in the old table are NULLed out.
906After resizing a table is always combined,
907but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000908*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000909static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000910dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000911{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 Py_ssize_t newsize;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400913 PyDictKeysObject *oldkeys;
914 PyObject **oldvalues;
915 Py_ssize_t i, oldsize;
Tim Peters91a364d2001-05-19 07:04:38 +0000916
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400917/* Find the smallest table size > minused. */
918 for (newsize = PyDict_MINSIZE_COMBINED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 newsize <= minused && newsize > 0;
920 newsize <<= 1)
921 ;
922 if (newsize <= 0) {
923 PyErr_NoMemory();
924 return -1;
925 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400926 oldkeys = mp->ma_keys;
927 oldvalues = mp->ma_values;
928 /* Allocate a new table. */
929 mp->ma_keys = new_keys_object(newsize);
930 if (mp->ma_keys == NULL) {
931 mp->ma_keys = oldkeys;
932 return -1;
933 }
934 if (oldkeys->dk_lookup == lookdict)
935 mp->ma_keys->dk_lookup = lookdict;
936 oldsize = DK_SIZE(oldkeys);
937 mp->ma_values = NULL;
938 /* If empty then nothing to copy so just return */
939 if (oldsize == 1) {
940 assert(oldkeys == Py_EMPTY_KEYS);
941 DK_DECREF(oldkeys);
942 return 0;
943 }
944 /* Main loop below assumes we can transfer refcount to new keys
945 * and that value is stored in me_value.
946 * Increment ref-counts and copy values here to compensate
947 * This (resizing a split table) should be relatively rare */
948 if (oldvalues != NULL) {
949 for (i = 0; i < oldsize; i++) {
950 if (oldvalues[i] != NULL) {
951 Py_INCREF(oldkeys->dk_entries[i].me_key);
952 oldkeys->dk_entries[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 }
955 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400956 /* Main loop */
957 for (i = 0; i < oldsize; i++) {
958 PyDictKeyEntry *ep = &oldkeys->dk_entries[i];
959 if (ep->me_value != NULL) {
960 assert(ep->me_key != dummy);
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000961 insertdict_clean(mp, ep->me_key, ep->me_hash, ep->me_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400964 mp->ma_keys->dk_usable -= mp->ma_used;
965 if (oldvalues != NULL) {
966 /* NULL out me_value slot in oldkeys, in case it was shared */
967 for (i = 0; i < oldsize; i++)
968 oldkeys->dk_entries[i].me_value = NULL;
969 assert(oldvalues != empty_values);
970 free_values(oldvalues);
971 DK_DECREF(oldkeys);
972 }
973 else {
974 assert(oldkeys->dk_lookup != lookdict_split);
975 if (oldkeys->dk_lookup != lookdict_unicode_nodummy) {
976 PyDictKeyEntry *ep0 = &oldkeys->dk_entries[0];
977 for (i = 0; i < oldsize; i++) {
978 if (ep0[i].me_key == dummy)
979 Py_DECREF(dummy);
980 }
981 }
982 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200983 DK_DEBUG_DECREF PyMem_FREE(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400984 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000986}
987
Benjamin Peterson15ee8212012-04-24 14:44:18 -0400988/* Returns NULL if unable to split table.
989 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400990static PyDictKeysObject *
991make_keys_shared(PyObject *op)
992{
993 Py_ssize_t i;
994 Py_ssize_t size;
995 PyDictObject *mp = (PyDictObject *)op;
996
Benjamin Peterson15ee8212012-04-24 14:44:18 -0400997 if (!PyDict_CheckExact(op))
998 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400999 if (!_PyDict_HasSplitTable(mp)) {
1000 PyDictKeyEntry *ep0;
1001 PyObject **values;
1002 assert(mp->ma_keys->dk_refcnt == 1);
1003 if (mp->ma_keys->dk_lookup == lookdict) {
1004 return NULL;
1005 }
1006 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
1007 /* Remove dummy keys */
1008 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
1009 return NULL;
1010 }
1011 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
1012 /* Copy values into a new array */
1013 ep0 = &mp->ma_keys->dk_entries[0];
1014 size = DK_SIZE(mp->ma_keys);
1015 values = new_values(size);
1016 if (values == NULL) {
1017 PyErr_SetString(PyExc_MemoryError,
1018 "Not enough memory to allocate new values array");
1019 return NULL;
1020 }
1021 for (i = 0; i < size; i++) {
1022 values[i] = ep0[i].me_value;
1023 ep0[i].me_value = NULL;
1024 }
1025 mp->ma_keys->dk_lookup = lookdict_split;
1026 mp->ma_values = values;
1027 }
1028 DK_INCREF(mp->ma_keys);
1029 return mp->ma_keys;
1030}
Christian Heimes99170a52007-12-19 02:07:34 +00001031
1032PyObject *
1033_PyDict_NewPresized(Py_ssize_t minused)
1034{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001035 Py_ssize_t newsize;
1036 PyDictKeysObject *new_keys;
1037 for (newsize = PyDict_MINSIZE_COMBINED;
1038 newsize <= minused && newsize > 0;
1039 newsize <<= 1)
1040 ;
1041 new_keys = new_keys_object(newsize);
1042 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001044 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001045}
1046
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001047/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1048 * that may occur (originally dicts supported only string keys, and exceptions
1049 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001050 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001051 * (suppressed) occurred while computing the key's hash, or that some error
1052 * (suppressed) occurred when comparing keys in the dict's internal probe
1053 * sequence. A nasty example of the latter is when a Python-coded comparison
1054 * function hits a stack-depth error, which can cause this to return NULL
1055 * even if the key is present.
1056 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001057PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001058PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001059{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001060 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001062 PyDictKeyEntry *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 PyThreadState *tstate;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001064 PyObject **value_addr;
1065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 if (!PyDict_Check(op))
1067 return NULL;
1068 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001069 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 {
1071 hash = PyObject_Hash(key);
1072 if (hash == -1) {
1073 PyErr_Clear();
1074 return NULL;
1075 }
1076 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 /* We can arrive here with a NULL tstate during initialization: try
1079 running "python -Wi" for an example related to string interning.
1080 Let's just hope that no exception occurs then... This must be
1081 _PyThreadState_Current and not PyThreadState_GET() because in debug
1082 mode, the latter complains if tstate is NULL. */
1083 tstate = (PyThreadState*)_Py_atomic_load_relaxed(
1084 &_PyThreadState_Current);
1085 if (tstate != NULL && tstate->curexc_type != NULL) {
1086 /* preserve the existing exception */
1087 PyObject *err_type, *err_value, *err_tb;
1088 PyErr_Fetch(&err_type, &err_value, &err_tb);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001089 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 /* ignore errors */
1091 PyErr_Restore(err_type, err_value, err_tb);
1092 if (ep == NULL)
1093 return NULL;
1094 }
1095 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001096 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 if (ep == NULL) {
1098 PyErr_Clear();
1099 return NULL;
1100 }
1101 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001102 return *value_addr;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001103}
1104
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001105/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1106 This returns NULL *with* an exception set if an exception occurred.
1107 It returns NULL *without* an exception set if the key wasn't present.
1108*/
1109PyObject *
1110PyDict_GetItemWithError(PyObject *op, PyObject *key)
1111{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001112 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 PyDictObject*mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001114 PyDictKeyEntry *ep;
1115 PyObject **value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 if (!PyDict_Check(op)) {
1118 PyErr_BadInternalCall();
1119 return NULL;
1120 }
1121 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001122 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 {
1124 hash = PyObject_Hash(key);
1125 if (hash == -1) {
1126 return NULL;
1127 }
1128 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001129
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001130 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 if (ep == NULL)
1132 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001133 return *value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001134}
1135
Brett Cannonfd074152012-04-14 14:10:13 -04001136PyObject *
1137_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1138{
1139 PyObject *kv;
1140 kv = _PyUnicode_FromId(key); /* borrowed */
1141 if (kv == NULL)
1142 return NULL;
1143 return PyDict_GetItemWithError(dp, kv);
1144}
1145
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001146/* Fast version of global value lookup.
1147 * Lookup in globals, then builtins.
1148 */
1149PyObject *
1150_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001151{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001152 PyObject *x;
1153 if (PyUnicode_CheckExact(key)) {
1154 PyObject **value_addr;
1155 Py_hash_t hash = ((PyASCIIObject *)key)->hash;
1156 if (hash != -1) {
1157 PyDictKeyEntry *e;
1158 e = globals->ma_keys->dk_lookup(globals, key, hash, &value_addr);
1159 if (e == NULL) {
1160 return NULL;
1161 }
1162 x = *value_addr;
1163 if (x != NULL)
1164 return x;
1165 e = builtins->ma_keys->dk_lookup(builtins, key, hash, &value_addr);
1166 if (e == NULL) {
1167 return NULL;
1168 }
1169 x = *value_addr;
1170 return x;
1171 }
Antoine Pitroue965d972012-02-27 00:45:12 +01001172 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001173 x = PyDict_GetItemWithError((PyObject *)globals, key);
1174 if (x != NULL)
1175 return x;
1176 if (PyErr_Occurred())
1177 return NULL;
1178 return PyDict_GetItemWithError((PyObject *)builtins, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001179}
1180
Antoine Pitroue965d972012-02-27 00:45:12 +01001181/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1182 * dictionary if it's merely replacing the value for an existing key.
1183 * This means that it's safe to loop over a dictionary with PyDict_Next()
1184 * and occasionally replace a value -- but you can't insert new keys or
1185 * remove them.
1186 */
1187int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001188PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001189{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001190 PyDictObject *mp;
1191 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001192 if (!PyDict_Check(op)) {
1193 PyErr_BadInternalCall();
1194 return -1;
1195 }
1196 assert(key);
1197 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001198 mp = (PyDictObject *)op;
1199 if (!PyUnicode_CheckExact(key) ||
1200 (hash = ((PyASCIIObject *) key)->hash) == -1)
1201 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001202 hash = PyObject_Hash(key);
1203 if (hash == -1)
1204 return -1;
1205 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001206
1207 /* insertdict() handles any resizing that might be necessary */
1208 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001209}
1210
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001211int
Tim Peters1f5871e2000-07-04 17:44:48 +00001212PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001213{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001214 PyDictObject *mp;
1215 Py_hash_t hash;
1216 PyDictKeyEntry *ep;
1217 PyObject *old_key, *old_value;
1218 PyObject **value_addr;
Guido van Rossumd7047b31995-01-02 19:07:15 +00001219
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 if (!PyDict_Check(op)) {
1221 PyErr_BadInternalCall();
1222 return -1;
1223 }
1224 assert(key);
1225 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001226 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 hash = PyObject_Hash(key);
1228 if (hash == -1)
1229 return -1;
1230 }
1231 mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001232 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 if (ep == NULL)
1234 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001235 if (*value_addr == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 set_key_error(key);
1237 return -1;
1238 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001239 old_value = *value_addr;
1240 *value_addr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001242 if (!_PyDict_HasSplitTable(mp)) {
1243 ENSURE_ALLOWS_DELETIONS(mp);
1244 old_key = ep->me_key;
1245 Py_INCREF(dummy);
1246 ep->me_key = dummy;
1247 Py_DECREF(old_key);
1248 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 Py_DECREF(old_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001251}
1252
Guido van Rossum25831651993-05-19 14:50:45 +00001253void
Tim Peters1f5871e2000-07-04 17:44:48 +00001254PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001255{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001257 PyDictKeysObject *oldkeys;
1258 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 if (!PyDict_Check(op))
1262 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001263 mp = ((PyDictObject *)op);
1264 oldkeys = mp->ma_keys;
1265 oldvalues = mp->ma_values;
1266 if (oldvalues == empty_values)
1267 return;
1268 /* Empty the dict... */
1269 DK_INCREF(Py_EMPTY_KEYS);
1270 mp->ma_keys = Py_EMPTY_KEYS;
1271 mp->ma_values = empty_values;
1272 mp->ma_used = 0;
1273 /* ...then clear the keys and values */
1274 if (oldvalues != NULL) {
1275 n = DK_SIZE(oldkeys);
1276 for (i = 0; i < n; i++)
1277 Py_CLEAR(oldvalues[i]);
1278 free_values(oldvalues);
1279 DK_DECREF(oldkeys);
1280 }
1281 else {
1282 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001283 DK_DECREF(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001284 }
1285}
1286
1287/* Returns -1 if no more items (or op is not a dict),
1288 * index of item otherwise. Stores value in pvalue
1289 */
1290Py_LOCAL_INLINE(Py_ssize_t)
1291dict_next(PyObject *op, Py_ssize_t i, PyObject **pvalue)
1292{
1293 Py_ssize_t mask, offset;
1294 PyDictObject *mp;
1295 PyObject **value_ptr;
1296
1297
1298 if (!PyDict_Check(op))
1299 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001301 if (i < 0)
1302 return -1;
1303 if (mp->ma_values) {
1304 value_ptr = &mp->ma_values[i];
1305 offset = sizeof(PyObject *);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001307 else {
1308 value_ptr = &mp->ma_keys->dk_entries[i].me_value;
1309 offset = sizeof(PyDictKeyEntry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001311 mask = DK_MASK(mp->ma_keys);
1312 while (i <= mask && *value_ptr == NULL) {
1313 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1314 i++;
1315 }
1316 if (i > mask)
1317 return -1;
1318 if (pvalue)
1319 *pvalue = *value_ptr;
1320 return i;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001321}
1322
Tim Peters080c88b2003-02-15 03:01:11 +00001323/*
1324 * Iterate over a dict. Use like so:
1325 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001326 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001327 * PyObject *key, *value;
1328 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001329 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +00001330 * Refer to borrowed references in key and value.
1331 * }
1332 *
1333 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001334 * mutates the dict. One exception: it is safe if the loop merely changes
1335 * the values associated with the keys (but doesn't insert new keys or
1336 * delete keys), via PyDict_SetItem().
1337 */
Guido van Rossum25831651993-05-19 14:50:45 +00001338int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001339PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001340{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001341 PyDictObject *mp;
1342 Py_ssize_t i = dict_next(op, *ppos, pvalue);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 if (i < 0)
1344 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001345 mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 *ppos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 if (pkey)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001348 *pkey = mp->ma_keys->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001350}
1351
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001352/* Internal version of PyDict_Next that returns a hash value in addition
1353 * to the key and value.
1354 */
Thomas Wouterscf297e42007-02-23 15:07:44 +00001355int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001356_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1357 PyObject **pvalue, Py_hash_t *phash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00001358{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001359 PyDictObject *mp;
1360 Py_ssize_t i = dict_next(op, *ppos, pvalue);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 if (i < 0)
1362 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001363 mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 *ppos = i+1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001365 *phash = mp->ma_keys->dk_entries[i].me_hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 if (pkey)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001367 *pkey = mp->ma_keys->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 return 1;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001369}
1370
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001371/* Methods */
1372
1373static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001374dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001375{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001376 PyObject **values = mp->ma_values;
1377 PyDictKeysObject *keys = mp->ma_keys;
1378 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 PyObject_GC_UnTrack(mp);
1380 Py_TRASHCAN_SAFE_BEGIN(mp)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001381 if (values != NULL) {
1382 if (values != empty_values) {
1383 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
1384 Py_XDECREF(values[i]);
1385 }
1386 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001388 DK_DECREF(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001390 else {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001391 assert(keys->dk_refcnt == 1);
1392 DK_DECREF(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001393 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1395 free_list[numfree++] = mp;
1396 else
1397 Py_TYPE(mp)->tp_free((PyObject *)mp);
1398 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001399}
1400
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001401
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001402static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001403dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001404{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 Py_ssize_t i;
1406 PyObject *s, *temp, *colon = NULL;
1407 PyObject *pieces = NULL, *result = NULL;
1408 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +00001409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 i = Py_ReprEnter((PyObject *)mp);
1411 if (i != 0) {
1412 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
1413 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 if (mp->ma_used == 0) {
1416 result = PyUnicode_FromString("{}");
1417 goto Done;
1418 }
Tim Petersa7259592001-06-16 05:11:17 +00001419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 pieces = PyList_New(0);
1421 if (pieces == NULL)
1422 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 colon = PyUnicode_FromString(": ");
1425 if (colon == NULL)
1426 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 /* Do repr() on each key+value pair, and insert ": " between them.
1429 Note that repr may mutate the dict. */
1430 i = 0;
1431 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
1432 int status;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001433 /* Prevent repr from deleting key or value during key format. */
1434 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 Py_INCREF(value);
1436 s = PyObject_Repr(key);
1437 PyUnicode_Append(&s, colon);
1438 PyUnicode_AppendAndDel(&s, PyObject_Repr(value));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001439 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 Py_DECREF(value);
1441 if (s == NULL)
1442 goto Done;
1443 status = PyList_Append(pieces, s);
1444 Py_DECREF(s); /* append created a new ref */
1445 if (status < 0)
1446 goto Done;
1447 }
Tim Petersa7259592001-06-16 05:11:17 +00001448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 /* Add "{}" decorations to the first and last items. */
1450 assert(PyList_GET_SIZE(pieces) > 0);
1451 s = PyUnicode_FromString("{");
1452 if (s == NULL)
1453 goto Done;
1454 temp = PyList_GET_ITEM(pieces, 0);
1455 PyUnicode_AppendAndDel(&s, temp);
1456 PyList_SET_ITEM(pieces, 0, s);
1457 if (s == NULL)
1458 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 s = PyUnicode_FromString("}");
1461 if (s == NULL)
1462 goto Done;
1463 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
1464 PyUnicode_AppendAndDel(&temp, s);
1465 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
1466 if (temp == NULL)
1467 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 /* Paste them all together with ", " between. */
1470 s = PyUnicode_FromString(", ");
1471 if (s == NULL)
1472 goto Done;
1473 result = PyUnicode_Join(s, pieces);
1474 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +00001475
1476Done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 Py_XDECREF(pieces);
1478 Py_XDECREF(colon);
1479 Py_ReprLeave((PyObject *)mp);
1480 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001481}
1482
Martin v. Löwis18e16552006-02-15 17:27:45 +00001483static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001484dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001485{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001487}
1488
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001489static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001490dict_subscript(PyDictObject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001491{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001492 PyObject *v;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001493 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001494 PyDictKeyEntry *ep;
1495 PyObject **value_addr;
1496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001498 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001499 hash = PyObject_Hash(key);
1500 if (hash == -1)
1501 return NULL;
1502 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001503 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 if (ep == NULL)
1505 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001506 v = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 if (v == NULL) {
1508 if (!PyDict_CheckExact(mp)) {
1509 /* Look up __missing__ method if we're a subclass. */
1510 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05001511 _Py_IDENTIFIER(__missing__);
1512 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513 if (missing != NULL) {
1514 res = PyObject_CallFunctionObjArgs(missing,
1515 key, NULL);
1516 Py_DECREF(missing);
1517 return res;
1518 }
1519 else if (PyErr_Occurred())
1520 return NULL;
1521 }
1522 set_key_error(key);
1523 return NULL;
1524 }
1525 else
1526 Py_INCREF(v);
1527 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001528}
1529
1530static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001531dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001532{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001533 if (w == NULL)
1534 return PyDict_DelItem((PyObject *)mp, v);
1535 else
1536 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001537}
1538
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001539static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001540 (lenfunc)dict_length, /*mp_length*/
1541 (binaryfunc)dict_subscript, /*mp_subscript*/
1542 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001543};
1544
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001545static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001546dict_keys(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001547{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548 register PyObject *v;
1549 register Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001550 PyDictKeyEntry *ep;
1551 Py_ssize_t size, n, offset;
1552 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001553
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001554 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 n = mp->ma_used;
1556 v = PyList_New(n);
1557 if (v == NULL)
1558 return NULL;
1559 if (n != mp->ma_used) {
1560 /* Durnit. The allocations caused the dict to resize.
1561 * Just start over, this shouldn't normally happen.
1562 */
1563 Py_DECREF(v);
1564 goto again;
1565 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001566 ep = &mp->ma_keys->dk_entries[0];
1567 size = DK_SIZE(mp->ma_keys);
1568 if (mp->ma_values) {
1569 value_ptr = mp->ma_values;
1570 offset = sizeof(PyObject *);
1571 }
1572 else {
1573 value_ptr = &ep[0].me_value;
1574 offset = sizeof(PyDictKeyEntry);
1575 }
1576 for (i = 0, j = 0; i < size; i++) {
1577 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578 PyObject *key = ep[i].me_key;
1579 Py_INCREF(key);
1580 PyList_SET_ITEM(v, j, key);
1581 j++;
1582 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001583 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584 }
1585 assert(j == n);
1586 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001587}
1588
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001589static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001590dict_values(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001591{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592 register PyObject *v;
1593 register Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001594 Py_ssize_t size, n, offset;
1595 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001596
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001597 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 n = mp->ma_used;
1599 v = PyList_New(n);
1600 if (v == NULL)
1601 return NULL;
1602 if (n != mp->ma_used) {
1603 /* Durnit. The allocations caused the dict to resize.
1604 * Just start over, this shouldn't normally happen.
1605 */
1606 Py_DECREF(v);
1607 goto again;
1608 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001609 size = DK_SIZE(mp->ma_keys);
1610 if (mp->ma_values) {
1611 value_ptr = mp->ma_values;
1612 offset = sizeof(PyObject *);
1613 }
1614 else {
1615 value_ptr = &mp->ma_keys->dk_entries[0].me_value;
1616 offset = sizeof(PyDictKeyEntry);
1617 }
1618 for (i = 0, j = 0; i < size; i++) {
1619 PyObject *value = *value_ptr;
1620 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1621 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 Py_INCREF(value);
1623 PyList_SET_ITEM(v, j, value);
1624 j++;
1625 }
1626 }
1627 assert(j == n);
1628 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001629}
1630
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001631static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001632dict_items(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001633{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 register PyObject *v;
1635 register Py_ssize_t i, j, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001636 Py_ssize_t size, offset;
1637 PyObject *item, *key;
1638 PyDictKeyEntry *ep;
1639 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 /* Preallocate the list of tuples, to avoid allocations during
1642 * the loop over the items, which could trigger GC, which
1643 * could resize the dict. :-(
1644 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001645 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 n = mp->ma_used;
1647 v = PyList_New(n);
1648 if (v == NULL)
1649 return NULL;
1650 for (i = 0; i < n; i++) {
1651 item = PyTuple_New(2);
1652 if (item == NULL) {
1653 Py_DECREF(v);
1654 return NULL;
1655 }
1656 PyList_SET_ITEM(v, i, item);
1657 }
1658 if (n != mp->ma_used) {
1659 /* Durnit. The allocations caused the dict to resize.
1660 * Just start over, this shouldn't normally happen.
1661 */
1662 Py_DECREF(v);
1663 goto again;
1664 }
1665 /* Nothing we do below makes any function calls. */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001666 ep = mp->ma_keys->dk_entries;
1667 size = DK_SIZE(mp->ma_keys);
1668 if (mp->ma_values) {
1669 value_ptr = mp->ma_values;
1670 offset = sizeof(PyObject *);
1671 }
1672 else {
1673 value_ptr = &ep[0].me_value;
1674 offset = sizeof(PyDictKeyEntry);
1675 }
1676 for (i = 0, j = 0; i < size; i++) {
1677 PyObject *value = *value_ptr;
1678 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1679 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 key = ep[i].me_key;
1681 item = PyList_GET_ITEM(v, j);
1682 Py_INCREF(key);
1683 PyTuple_SET_ITEM(item, 0, key);
1684 Py_INCREF(value);
1685 PyTuple_SET_ITEM(item, 1, value);
1686 j++;
1687 }
1688 }
1689 assert(j == n);
1690 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001691}
1692
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001693static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001694dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001695{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001696 PyObject *seq;
1697 PyObject *value = Py_None;
1698 PyObject *it; /* iter(seq) */
1699 PyObject *key;
1700 PyObject *d;
1701 int status;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
1704 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001705
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001706 d = PyObject_CallObject(cls, NULL);
1707 if (d == NULL)
1708 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001709
Benjamin Peterson9892f522012-10-31 14:22:12 -04001710 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001711 if (PyDict_CheckExact(seq)) {
1712 PyDictObject *mp = (PyDictObject *)d;
1713 PyObject *oldvalue;
1714 Py_ssize_t pos = 0;
1715 PyObject *key;
1716 Py_hash_t hash;
Guido van Rossum58da9312007-11-10 23:39:45 +00001717
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001718 if (dictresize(mp, Py_SIZE(seq))) {
Petri Lehtinena94200e2011-10-24 21:12:58 +03001719 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001720 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001721 }
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001722
1723 while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001724 if (insertdict(mp, key, hash, value)) {
1725 Py_DECREF(d);
1726 return NULL;
1727 }
1728 }
1729 return d;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001730 }
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001731 if (PyAnySet_CheckExact(seq)) {
1732 PyDictObject *mp = (PyDictObject *)d;
1733 Py_ssize_t pos = 0;
1734 PyObject *key;
1735 Py_hash_t hash;
Guido van Rossum58da9312007-11-10 23:39:45 +00001736
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001737 if (dictresize(mp, PySet_GET_SIZE(seq))) {
Petri Lehtinena94200e2011-10-24 21:12:58 +03001738 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001739 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001740 }
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001741
1742 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
Benjamin Petersond1f2cb32012-10-31 14:05:55 -04001743 if (insertdict(mp, key, hash, value)) {
1744 Py_DECREF(d);
1745 return NULL;
1746 }
1747 }
1748 return d;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001750 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001752 it = PyObject_GetIter(seq);
1753 if (it == NULL){
1754 Py_DECREF(d);
1755 return NULL;
1756 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001757
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001758 if (PyDict_CheckExact(d)) {
1759 while ((key = PyIter_Next(it)) != NULL) {
1760 status = PyDict_SetItem(d, key, value);
1761 Py_DECREF(key);
1762 if (status < 0)
1763 goto Fail;
1764 }
1765 } else {
1766 while ((key = PyIter_Next(it)) != NULL) {
1767 status = PyObject_SetItem(d, key, value);
1768 Py_DECREF(key);
1769 if (status < 0)
1770 goto Fail;
1771 }
1772 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774 if (PyErr_Occurred())
1775 goto Fail;
1776 Py_DECREF(it);
1777 return d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001778
1779Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001780 Py_DECREF(it);
1781 Py_DECREF(d);
1782 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001783}
1784
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001785static int
1786dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001787{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001788 PyObject *arg = NULL;
1789 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1792 result = -1;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001794 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001795 _Py_IDENTIFIER(keys);
1796 if (_PyObject_HasAttrId(arg, &PyId_keys))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001797 result = PyDict_Merge(self, arg, 1);
1798 else
1799 result = PyDict_MergeFromSeq2(self, arg, 1);
1800 }
1801 if (result == 0 && kwds != NULL) {
1802 if (PyArg_ValidateKeywordArguments(kwds))
1803 result = PyDict_Merge(self, kwds, 1);
1804 else
1805 result = -1;
1806 }
1807 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001808}
1809
1810static PyObject *
1811dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1812{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001813 if (dict_update_common(self, args, kwds, "update") != -1)
1814 Py_RETURN_NONE;
1815 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001816}
1817
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001818/* Update unconditionally replaces existing items.
1819 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001820 otherwise it leaves existing items unchanged.
1821
1822 PyDict_{Update,Merge} update/merge from a mapping object.
1823
Tim Petersf582b822001-12-11 18:51:08 +00001824 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001825 producing iterable objects of length 2.
1826*/
1827
Tim Petersf582b822001-12-11 18:51:08 +00001828int
Tim Peters1fc240e2001-10-26 05:06:50 +00001829PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1830{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001831 PyObject *it; /* iter(seq2) */
1832 Py_ssize_t i; /* index into seq2 of current element */
1833 PyObject *item; /* seq2[i] */
1834 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00001835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 assert(d != NULL);
1837 assert(PyDict_Check(d));
1838 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00001839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001840 it = PyObject_GetIter(seq2);
1841 if (it == NULL)
1842 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844 for (i = 0; ; ++i) {
1845 PyObject *key, *value;
1846 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001847
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001848 fast = NULL;
1849 item = PyIter_Next(it);
1850 if (item == NULL) {
1851 if (PyErr_Occurred())
1852 goto Fail;
1853 break;
1854 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856 /* Convert item to sequence, and verify length 2. */
1857 fast = PySequence_Fast(item, "");
1858 if (fast == NULL) {
1859 if (PyErr_ExceptionMatches(PyExc_TypeError))
1860 PyErr_Format(PyExc_TypeError,
1861 "cannot convert dictionary update "
1862 "sequence element #%zd to a sequence",
1863 i);
1864 goto Fail;
1865 }
1866 n = PySequence_Fast_GET_SIZE(fast);
1867 if (n != 2) {
1868 PyErr_Format(PyExc_ValueError,
1869 "dictionary update sequence element #%zd "
1870 "has length %zd; 2 is required",
1871 i, n);
1872 goto Fail;
1873 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875 /* Update/merge with this (key, value) pair. */
1876 key = PySequence_Fast_GET_ITEM(fast, 0);
1877 value = PySequence_Fast_GET_ITEM(fast, 1);
1878 if (override || PyDict_GetItem(d, key) == NULL) {
1879 int status = PyDict_SetItem(d, key, value);
1880 if (status < 0)
1881 goto Fail;
1882 }
1883 Py_DECREF(fast);
1884 Py_DECREF(item);
1885 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887 i = 0;
1888 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00001889Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890 Py_XDECREF(item);
1891 Py_XDECREF(fast);
1892 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001893Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 Py_DECREF(it);
1895 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001896}
1897
Tim Peters6d6c1a32001-08-02 04:15:00 +00001898int
1899PyDict_Update(PyObject *a, PyObject *b)
1900{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001901 return PyDict_Merge(a, b, 1);
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001902}
1903
1904int
1905PyDict_Merge(PyObject *a, PyObject *b, int override)
1906{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001907 register PyDictObject *mp, *other;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001908 register Py_ssize_t i, n;
1909 PyDictKeyEntry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001911 /* We accept for the argument either a concrete dictionary object,
1912 * or an abstract "mapping" object. For the former, we can do
1913 * things quite efficiently. For the latter, we only require that
1914 * PyMapping_Keys() and PyObject_GetItem() be supported.
1915 */
1916 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1917 PyErr_BadInternalCall();
1918 return -1;
1919 }
1920 mp = (PyDictObject*)a;
1921 if (PyDict_Check(b)) {
1922 other = (PyDictObject*)b;
1923 if (other == mp || other->ma_used == 0)
1924 /* a.update(a) or a.update({}); nothing to do */
1925 return 0;
1926 if (mp->ma_used == 0)
1927 /* Since the target dict is empty, PyDict_GetItem()
1928 * always returns NULL. Setting override to 1
1929 * skips the unnecessary test.
1930 */
1931 override = 1;
1932 /* Do one big resize at the start, rather than
1933 * incrementally resizing as we insert new items. Expect
1934 * that there will be no (or few) overlapping keys.
1935 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001936 if (mp->ma_keys->dk_usable * 3 < other->ma_used * 2)
1937 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001939 for (i = 0, n = DK_SIZE(other->ma_keys); i < n; i++) {
1940 PyObject *value;
1941 entry = &other->ma_keys->dk_entries[i];
1942 if (other->ma_values)
1943 value = other->ma_values[i];
1944 else
1945 value = entry->me_value;
1946
1947 if (value != NULL &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001948 (override ||
1949 PyDict_GetItem(a, entry->me_key) == NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001950 if (insertdict(mp, entry->me_key,
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001951 entry->me_hash,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001952 value) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001953 return -1;
1954 }
1955 }
1956 }
1957 else {
1958 /* Do it the generic, slower way */
1959 PyObject *keys = PyMapping_Keys(b);
1960 PyObject *iter;
1961 PyObject *key, *value;
1962 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964 if (keys == NULL)
1965 /* Docstring says this is equivalent to E.keys() so
1966 * if E doesn't have a .keys() method we want
1967 * AttributeError to percolate up. Might as well
1968 * do the same for any other error.
1969 */
1970 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001972 iter = PyObject_GetIter(keys);
1973 Py_DECREF(keys);
1974 if (iter == NULL)
1975 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001977 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
1978 if (!override && PyDict_GetItem(a, key) != NULL) {
1979 Py_DECREF(key);
1980 continue;
1981 }
1982 value = PyObject_GetItem(b, key);
1983 if (value == NULL) {
1984 Py_DECREF(iter);
1985 Py_DECREF(key);
1986 return -1;
1987 }
1988 status = PyDict_SetItem(a, key, value);
1989 Py_DECREF(key);
1990 Py_DECREF(value);
1991 if (status < 0) {
1992 Py_DECREF(iter);
1993 return -1;
1994 }
1995 }
1996 Py_DECREF(iter);
1997 if (PyErr_Occurred())
1998 /* Iterator completed, via error */
1999 return -1;
2000 }
2001 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002002}
2003
2004static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002005dict_copy(register PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002006{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002007 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002008}
2009
2010PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002011PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002012{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002013 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002014 PyDictObject *mp;
2015 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002016
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002017 if (o == NULL || !PyDict_Check(o)) {
2018 PyErr_BadInternalCall();
2019 return NULL;
2020 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002021 mp = (PyDictObject *)o;
2022 if (_PyDict_HasSplitTable(mp)) {
2023 PyDictObject *split_copy;
2024 PyObject **newvalues = new_values(DK_SIZE(mp->ma_keys));
2025 if (newvalues == NULL)
2026 return PyErr_NoMemory();
2027 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2028 if (split_copy == NULL) {
2029 free_values(newvalues);
2030 return NULL;
2031 }
2032 split_copy->ma_values = newvalues;
2033 split_copy->ma_keys = mp->ma_keys;
2034 split_copy->ma_used = mp->ma_used;
2035 DK_INCREF(mp->ma_keys);
2036 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2037 PyObject *value = mp->ma_values[i];
2038 Py_XINCREF(value);
2039 split_copy->ma_values[i] = value;
2040 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002041 if (_PyObject_GC_IS_TRACKED(mp))
2042 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002043 return (PyObject *)split_copy;
2044 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002045 copy = PyDict_New();
2046 if (copy == NULL)
2047 return NULL;
2048 if (PyDict_Merge(copy, o, 1) == 0)
2049 return copy;
2050 Py_DECREF(copy);
2051 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002052}
2053
Martin v. Löwis18e16552006-02-15 17:27:45 +00002054Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002055PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002056{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002057 if (mp == NULL || !PyDict_Check(mp)) {
2058 PyErr_BadInternalCall();
2059 return -1;
2060 }
2061 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002062}
2063
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002064PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002065PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002066{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 if (mp == NULL || !PyDict_Check(mp)) {
2068 PyErr_BadInternalCall();
2069 return NULL;
2070 }
2071 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002072}
2073
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002074PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002075PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002076{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002077 if (mp == NULL || !PyDict_Check(mp)) {
2078 PyErr_BadInternalCall();
2079 return NULL;
2080 }
2081 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002082}
2083
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002084PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002085PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002086{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002087 if (mp == NULL || !PyDict_Check(mp)) {
2088 PyErr_BadInternalCall();
2089 return NULL;
2090 }
2091 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002092}
2093
Tim Peterse63415e2001-05-08 04:38:29 +00002094/* Return 1 if dicts equal, 0 if not, -1 if error.
2095 * Gets out as soon as any difference is detected.
2096 * Uses only Py_EQ comparison.
2097 */
2098static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002099dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002100{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002101 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002102
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002103 if (a->ma_used != b->ma_used)
2104 /* can't be equal if # of entries differ */
2105 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002106 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002107 for (i = 0; i < DK_SIZE(a->ma_keys); i++) {
2108 PyDictKeyEntry *ep = &a->ma_keys->dk_entries[i];
2109 PyObject *aval;
2110 if (a->ma_values)
2111 aval = a->ma_values[i];
2112 else
2113 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002114 if (aval != NULL) {
2115 int cmp;
2116 PyObject *bval;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002117 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002118 /* temporarily bump aval's refcount to ensure it stays
2119 alive until we're done with it */
2120 Py_INCREF(aval);
2121 /* ditto for key */
2122 Py_INCREF(key);
2123 bval = PyDict_GetItemWithError((PyObject *)b, key);
2124 Py_DECREF(key);
2125 if (bval == NULL) {
2126 Py_DECREF(aval);
2127 if (PyErr_Occurred())
2128 return -1;
2129 return 0;
2130 }
2131 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
2132 Py_DECREF(aval);
2133 if (cmp <= 0) /* error or not equal */
2134 return cmp;
2135 }
2136 }
2137 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002138}
Tim Peterse63415e2001-05-08 04:38:29 +00002139
2140static PyObject *
2141dict_richcompare(PyObject *v, PyObject *w, int op)
2142{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002143 int cmp;
2144 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002146 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2147 res = Py_NotImplemented;
2148 }
2149 else if (op == Py_EQ || op == Py_NE) {
2150 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2151 if (cmp < 0)
2152 return NULL;
2153 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2154 }
2155 else
2156 res = Py_NotImplemented;
2157 Py_INCREF(res);
2158 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002159}
Tim Peterse63415e2001-05-08 04:38:29 +00002160
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002161static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002162dict_contains(register PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002163{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002164 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002165 PyDictKeyEntry *ep;
2166 PyObject **value_addr;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002168 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002169 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002170 hash = PyObject_Hash(key);
2171 if (hash == -1)
2172 return NULL;
2173 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002174 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175 if (ep == NULL)
2176 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002177 return PyBool_FromLong(*value_addr != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002178}
2179
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002180static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002181dict_get(register PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00002182{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002183 PyObject *key;
2184 PyObject *failobj = Py_None;
2185 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002186 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002187 PyDictKeyEntry *ep;
2188 PyObject **value_addr;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002190 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
2191 return NULL;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002193 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002194 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002195 hash = PyObject_Hash(key);
2196 if (hash == -1)
2197 return NULL;
2198 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002199 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002200 if (ep == NULL)
2201 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002202 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002203 if (val == NULL)
2204 val = failobj;
2205 Py_INCREF(val);
2206 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002207}
2208
Barry Warsawc38c5da1997-10-06 17:49:20 +00002209static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002210dict_setdefault(register PyDictObject *mp, PyObject *args)
Guido van Rossum164452c2000-08-08 16:12:54 +00002211{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002212 PyObject *key;
2213 PyObject *failobj = Py_None;
2214 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002215 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002216 PyDictKeyEntry *ep;
2217 PyObject **value_addr;
Guido van Rossum164452c2000-08-08 16:12:54 +00002218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002219 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
2220 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00002221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002222 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002223 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002224 hash = PyObject_Hash(key);
2225 if (hash == -1)
2226 return NULL;
2227 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002228 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002229 if (ep == NULL)
2230 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002231 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002232 if (val == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002233 if (mp->ma_keys->dk_usable <= 0) {
2234 /* Need to resize. */
2235 if (insertion_resize(mp) < 0)
2236 return NULL;
2237 ep = find_empty_slot(mp, key, hash, &value_addr);
2238 }
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002239 Py_INCREF(failobj);
2240 Py_INCREF(key);
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002241 MAINTAIN_TRACKING(mp, key, failobj);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002242 ep->me_key = key;
2243 ep->me_hash = hash;
2244 *value_addr = failobj;
2245 val = failobj;
2246 mp->ma_keys->dk_usable--;
2247 mp->ma_used++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002248 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002249 Py_INCREF(val);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002250 return val;
Guido van Rossum164452c2000-08-08 16:12:54 +00002251}
2252
2253
2254static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002255dict_clear(register PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002256{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002257 PyDict_Clear((PyObject *)mp);
2258 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002259}
2260
Guido van Rossumba6ab842000-12-12 22:02:18 +00002261static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002262dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002263{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002264 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002265 PyObject *old_value, *old_key;
2266 PyObject *key, *deflt = NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002267 PyDictKeyEntry *ep;
2268 PyObject **value_addr;
Guido van Rossume027d982002-04-12 15:11:59 +00002269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002270 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2271 return NULL;
2272 if (mp->ma_used == 0) {
2273 if (deflt) {
2274 Py_INCREF(deflt);
2275 return deflt;
2276 }
Raymond Hettingerdd421542010-10-30 08:10:29 +00002277 set_key_error(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 return NULL;
2279 }
2280 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002281 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002282 hash = PyObject_Hash(key);
2283 if (hash == -1)
2284 return NULL;
2285 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002286 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002287 if (ep == NULL)
2288 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002289 old_value = *value_addr;
2290 if (old_value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002291 if (deflt) {
2292 Py_INCREF(deflt);
2293 return deflt;
2294 }
2295 set_key_error(key);
2296 return NULL;
2297 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002298 *value_addr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002299 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002300 if (!_PyDict_HasSplitTable(mp)) {
2301 ENSURE_ALLOWS_DELETIONS(mp);
2302 old_key = ep->me_key;
2303 Py_INCREF(dummy);
2304 ep->me_key = dummy;
2305 Py_DECREF(old_key);
2306 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 return old_value;
Guido van Rossume027d982002-04-12 15:11:59 +00002308}
2309
2310static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002311dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00002312{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002313 Py_hash_t i = 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002314 PyDictKeyEntry *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002315 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002316
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002318 /* Allocate the result tuple before checking the size. Believe it
2319 * or not, this allocation could trigger a garbage collection which
2320 * could empty the dict, so if we checked the size first and that
2321 * happened, the result would be an infinite loop (searching for an
2322 * entry that no longer exists). Note that the usual popitem()
2323 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2324 * tuple away if the dict *is* empty isn't a significant
2325 * inefficiency -- possible, but unlikely in practice.
2326 */
2327 res = PyTuple_New(2);
2328 if (res == NULL)
2329 return NULL;
2330 if (mp->ma_used == 0) {
2331 Py_DECREF(res);
2332 PyErr_SetString(PyExc_KeyError,
2333 "popitem(): dictionary is empty");
2334 return NULL;
2335 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002336 /* Convert split table to combined table */
2337 if (mp->ma_keys->dk_lookup == lookdict_split) {
2338 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
2339 Py_DECREF(res);
2340 return NULL;
2341 }
2342 }
2343 ENSURE_ALLOWS_DELETIONS(mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002344 /* Set ep to "the first" dict entry with a value. We abuse the hash
2345 * field of slot 0 to hold a search finger:
2346 * If slot 0 has a value, use slot 0.
2347 * Else slot 0 is being used to hold a search finger,
2348 * and we use its hash value as the first index to look.
2349 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002350 ep = &mp->ma_keys->dk_entries[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002351 if (ep->me_value == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002352 Py_ssize_t mask = DK_MASK(mp->ma_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002353 i = ep->me_hash;
2354 /* The hash field may be a real hash value, or it may be a
2355 * legit search finger, or it may be a once-legit search
2356 * finger that's out of bounds now because it wrapped around
2357 * or the table shrunk -- simply make sure it's in bounds now.
2358 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002359 if (i > mask || i < 1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360 i = 1; /* skip slot 0 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002361 while ((ep = &mp->ma_keys->dk_entries[i])->me_value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002362 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002363 if (i > mask)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 i = 1;
2365 }
2366 }
2367 PyTuple_SET_ITEM(res, 0, ep->me_key);
2368 PyTuple_SET_ITEM(res, 1, ep->me_value);
2369 Py_INCREF(dummy);
2370 ep->me_key = dummy;
2371 ep->me_value = NULL;
2372 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002373 assert(mp->ma_keys->dk_entries[0].me_value == NULL);
2374 mp->ma_keys->dk_entries[0].me_hash = i + 1; /* next place to start */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002375 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002376}
2377
Jeremy Hylton8caad492000-06-23 14:18:11 +00002378static int
2379dict_traverse(PyObject *op, visitproc visit, void *arg)
2380{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002381 Py_ssize_t i, n;
2382 PyDictObject *mp = (PyDictObject *)op;
2383 if (mp->ma_keys->dk_lookup == lookdict) {
2384 for (i = 0; i < DK_SIZE(mp->ma_keys); i++) {
2385 if (mp->ma_keys->dk_entries[i].me_value != NULL) {
2386 Py_VISIT(mp->ma_keys->dk_entries[i].me_value);
2387 Py_VISIT(mp->ma_keys->dk_entries[i].me_key);
2388 }
2389 }
2390 } else {
2391 if (mp->ma_values != NULL) {
2392 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2393 Py_VISIT(mp->ma_values[i]);
2394 }
2395 }
2396 else {
2397 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2398 Py_VISIT(mp->ma_keys->dk_entries[i].me_value);
2399 }
2400 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 }
2402 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002403}
2404
2405static int
2406dict_tp_clear(PyObject *op)
2407{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002408 PyDict_Clear(op);
2409 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002410}
2411
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002412static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002413
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002414static PyObject *
2415dict_sizeof(PyDictObject *mp)
2416{
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002417 Py_ssize_t size, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002418
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002419 size = DK_SIZE(mp->ma_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002420 res = sizeof(PyDictObject);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002421 if (mp->ma_values)
2422 res += size * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002423 /* If the dictionary is split, the keys portion is accounted-for
2424 in the type object. */
2425 if (mp->ma_keys->dk_refcnt == 1)
2426 res += sizeof(PyDictKeysObject) + (size-1) * sizeof(PyDictKeyEntry);
2427 return PyLong_FromSsize_t(res);
2428}
2429
2430Py_ssize_t
2431_PyDict_KeysSize(PyDictKeysObject *keys)
2432{
2433 return sizeof(PyDictKeysObject) + (DK_SIZE(keys)-1) * sizeof(PyDictKeyEntry);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002434}
2435
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002436PyDoc_STRVAR(contains__doc__,
2437"D.__contains__(k) -> True if D has a key k, else False");
2438
2439PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
2440
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002441PyDoc_STRVAR(sizeof__doc__,
2442"D.__sizeof__() -> size of D in memory, in bytes");
2443
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002444PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002445"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002446
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002447PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002448"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 +00002449
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002450PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002451"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002452If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00002453
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002454PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00002455"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000024562-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002457
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002458PyDoc_STRVAR(update__doc__,
Georg Brandlac0675c2011-12-18 19:30:55 +01002459"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n"
2460"If E present and has a .keys() method, does: for k in E: D[k] = E[k]\n\
2461If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002462In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00002463
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002464PyDoc_STRVAR(fromkeys__doc__,
2465"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
2466v defaults to None.");
2467
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002468PyDoc_STRVAR(clear__doc__,
2469"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002470
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002471PyDoc_STRVAR(copy__doc__,
2472"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00002473
Guido van Rossumb90c8482007-02-10 01:11:45 +00002474/* Forward */
2475static PyObject *dictkeys_new(PyObject *);
2476static PyObject *dictitems_new(PyObject *);
2477static PyObject *dictvalues_new(PyObject *);
2478
Guido van Rossum45c85d12007-07-27 16:31:40 +00002479PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002480 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002481PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002483PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002484 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00002485
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002486static PyMethodDef mapp_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002487 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
2488 contains__doc__},
2489 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
2490 getitem__doc__},
2491 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
2492 sizeof__doc__},
2493 {"get", (PyCFunction)dict_get, METH_VARARGS,
2494 get__doc__},
2495 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
2496 setdefault_doc__},
2497 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
2498 pop__doc__},
2499 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
2500 popitem__doc__},
2501 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
2502 keys__doc__},
2503 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
2504 items__doc__},
2505 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
2506 values__doc__},
2507 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
2508 update__doc__},
2509 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
2510 fromkeys__doc__},
2511 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
2512 clear__doc__},
2513 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
2514 copy__doc__},
2515 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002516};
2517
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002518/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002519int
2520PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002521{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002522 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002523 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002524 PyDictKeyEntry *ep;
2525 PyObject **value_addr;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002527 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002528 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002529 hash = PyObject_Hash(key);
2530 if (hash == -1)
2531 return -1;
2532 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002533 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
2534 return (ep == NULL) ? -1 : (*value_addr != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002535}
2536
Thomas Wouterscf297e42007-02-23 15:07:44 +00002537/* Internal version of PyDict_Contains used when the hash value is already known */
2538int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002539_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00002540{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002541 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002542 PyDictKeyEntry *ep;
2543 PyObject **value_addr;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002544
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002545 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
2546 return (ep == NULL) ? -1 : (*value_addr != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00002547}
2548
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002549/* Hack to implement "key in dict" */
2550static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002551 0, /* sq_length */
2552 0, /* sq_concat */
2553 0, /* sq_repeat */
2554 0, /* sq_item */
2555 0, /* sq_slice */
2556 0, /* sq_ass_item */
2557 0, /* sq_ass_slice */
2558 PyDict_Contains, /* sq_contains */
2559 0, /* sq_inplace_concat */
2560 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002561};
2562
Guido van Rossum09e563a2001-05-01 12:10:21 +00002563static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002564dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2565{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002566 PyObject *self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002568 assert(type != NULL && type->tp_alloc != NULL);
2569 self = type->tp_alloc(type, 0);
2570 if (self != NULL) {
2571 PyDictObject *d = (PyDictObject *)self;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002572 d->ma_keys = new_keys_object(PyDict_MINSIZE_COMBINED);
2573 /* XXX - Should we raise a no-memory error? */
2574 if (d->ma_keys == NULL) {
2575 DK_INCREF(Py_EMPTY_KEYS);
2576 d->ma_keys = Py_EMPTY_KEYS;
2577 d->ma_values = empty_values;
2578 }
2579 d->ma_used = 0;
Ezio Melotti13925002011-03-16 11:05:33 +02002580 /* The object has been implicitly tracked by tp_alloc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002581 if (type == &PyDict_Type)
2582 _PyObject_GC_UNTRACK(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002583 }
2584 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002585}
2586
Tim Peters25786c02001-09-02 08:22:48 +00002587static int
2588dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2589{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002590 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002591}
2592
Tim Peters6d6c1a32001-08-02 04:15:00 +00002593static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002594dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002595{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002596 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002597}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002598
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002599PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002600"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002601"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002602" (key, value) pairs\n"
2603"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002604" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002605" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002606" d[k] = v\n"
2607"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2608" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002609
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002610PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002611 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2612 "dict",
2613 sizeof(PyDictObject),
2614 0,
2615 (destructor)dict_dealloc, /* tp_dealloc */
2616 0, /* tp_print */
2617 0, /* tp_getattr */
2618 0, /* tp_setattr */
2619 0, /* tp_reserved */
2620 (reprfunc)dict_repr, /* tp_repr */
2621 0, /* tp_as_number */
2622 &dict_as_sequence, /* tp_as_sequence */
2623 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002624 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002625 0, /* tp_call */
2626 0, /* tp_str */
2627 PyObject_GenericGetAttr, /* tp_getattro */
2628 0, /* tp_setattro */
2629 0, /* tp_as_buffer */
2630 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2631 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
2632 dictionary_doc, /* tp_doc */
2633 dict_traverse, /* tp_traverse */
2634 dict_tp_clear, /* tp_clear */
2635 dict_richcompare, /* tp_richcompare */
2636 0, /* tp_weaklistoffset */
2637 (getiterfunc)dict_iter, /* tp_iter */
2638 0, /* tp_iternext */
2639 mapp_methods, /* tp_methods */
2640 0, /* tp_members */
2641 0, /* tp_getset */
2642 0, /* tp_base */
2643 0, /* tp_dict */
2644 0, /* tp_descr_get */
2645 0, /* tp_descr_set */
2646 0, /* tp_dictoffset */
2647 dict_init, /* tp_init */
2648 PyType_GenericAlloc, /* tp_alloc */
2649 dict_new, /* tp_new */
2650 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002651};
2652
Victor Stinner3c1e4812012-03-26 22:10:51 +02002653PyObject *
2654_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
2655{
2656 PyObject *kv;
2657 kv = _PyUnicode_FromId(key); /* borrowed */
2658 if (kv == NULL)
2659 return NULL;
2660 return PyDict_GetItem(dp, kv);
2661}
2662
Guido van Rossum3cca2451997-05-16 14:23:33 +00002663/* For backward compatibility with old dictionary interface */
2664
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002665PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002666PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002667{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002668 PyObject *kv, *rv;
2669 kv = PyUnicode_FromString(key);
2670 if (kv == NULL)
2671 return NULL;
2672 rv = PyDict_GetItem(v, kv);
2673 Py_DECREF(kv);
2674 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002675}
2676
2677int
Victor Stinner3c1e4812012-03-26 22:10:51 +02002678_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
2679{
2680 PyObject *kv;
2681 kv = _PyUnicode_FromId(key); /* borrowed */
2682 if (kv == NULL)
2683 return -1;
2684 return PyDict_SetItem(v, kv, item);
2685}
2686
2687int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002688PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002689{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002690 PyObject *kv;
2691 int err;
2692 kv = PyUnicode_FromString(key);
2693 if (kv == NULL)
2694 return -1;
2695 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
2696 err = PyDict_SetItem(v, kv, item);
2697 Py_DECREF(kv);
2698 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002699}
2700
2701int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002702PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002703{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002704 PyObject *kv;
2705 int err;
2706 kv = PyUnicode_FromString(key);
2707 if (kv == NULL)
2708 return -1;
2709 err = PyDict_DelItem(v, kv);
2710 Py_DECREF(kv);
2711 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002712}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002713
Raymond Hettinger019a1482004-03-18 02:41:19 +00002714/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002715
2716typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002717 PyObject_HEAD
2718 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
2719 Py_ssize_t di_used;
2720 Py_ssize_t di_pos;
2721 PyObject* di_result; /* reusable result tuple for iteritems */
2722 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002723} dictiterobject;
2724
2725static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002726dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002727{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002728 dictiterobject *di;
2729 di = PyObject_GC_New(dictiterobject, itertype);
2730 if (di == NULL)
2731 return NULL;
2732 Py_INCREF(dict);
2733 di->di_dict = dict;
2734 di->di_used = dict->ma_used;
2735 di->di_pos = 0;
2736 di->len = dict->ma_used;
2737 if (itertype == &PyDictIterItem_Type) {
2738 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2739 if (di->di_result == NULL) {
2740 Py_DECREF(di);
2741 return NULL;
2742 }
2743 }
2744 else
2745 di->di_result = NULL;
2746 _PyObject_GC_TRACK(di);
2747 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002748}
2749
2750static void
2751dictiter_dealloc(dictiterobject *di)
2752{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002753 Py_XDECREF(di->di_dict);
2754 Py_XDECREF(di->di_result);
2755 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002756}
2757
2758static int
2759dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
2760{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002761 Py_VISIT(di->di_dict);
2762 Py_VISIT(di->di_result);
2763 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002764}
2765
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002766static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002767dictiter_len(dictiterobject *di)
2768{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002769 Py_ssize_t len = 0;
2770 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
2771 len = di->len;
2772 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002773}
2774
Guido van Rossumb90c8482007-02-10 01:11:45 +00002775PyDoc_STRVAR(length_hint_doc,
2776 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002777
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002778static PyObject *
2779dictiter_reduce(dictiterobject *di);
2780
2781PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
2782
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002783static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002784 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
2785 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002786 {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS,
2787 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002788 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002789};
2790
Raymond Hettinger019a1482004-03-18 02:41:19 +00002791static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002792{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002793 PyObject *key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002794 register Py_ssize_t i, mask, offset;
2795 register PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002796 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002797 PyObject **value_ptr;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002799 if (d == NULL)
2800 return NULL;
2801 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002803 if (di->di_used != d->ma_used) {
2804 PyErr_SetString(PyExc_RuntimeError,
2805 "dictionary changed size during iteration");
2806 di->di_used = -1; /* Make this state sticky */
2807 return NULL;
2808 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002810 i = di->di_pos;
2811 if (i < 0)
2812 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002813 k = d->ma_keys;
2814 if (d->ma_values) {
2815 value_ptr = &d->ma_values[i];
2816 offset = sizeof(PyObject *);
2817 }
2818 else {
2819 value_ptr = &k->dk_entries[i].me_value;
2820 offset = sizeof(PyDictKeyEntry);
2821 }
2822 mask = DK_SIZE(k)-1;
2823 while (i <= mask && *value_ptr == NULL) {
2824 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002825 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002826 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002827 di->di_pos = i+1;
2828 if (i > mask)
2829 goto fail;
2830 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002831 key = k->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002832 Py_INCREF(key);
2833 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002834
2835fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002836 Py_DECREF(d);
2837 di->di_dict = NULL;
2838 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002839}
2840
Raymond Hettinger019a1482004-03-18 02:41:19 +00002841PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002842 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2843 "dict_keyiterator", /* tp_name */
2844 sizeof(dictiterobject), /* tp_basicsize */
2845 0, /* tp_itemsize */
2846 /* methods */
2847 (destructor)dictiter_dealloc, /* tp_dealloc */
2848 0, /* tp_print */
2849 0, /* tp_getattr */
2850 0, /* tp_setattr */
2851 0, /* tp_reserved */
2852 0, /* tp_repr */
2853 0, /* tp_as_number */
2854 0, /* tp_as_sequence */
2855 0, /* tp_as_mapping */
2856 0, /* tp_hash */
2857 0, /* tp_call */
2858 0, /* tp_str */
2859 PyObject_GenericGetAttr, /* tp_getattro */
2860 0, /* tp_setattro */
2861 0, /* tp_as_buffer */
2862 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2863 0, /* tp_doc */
2864 (traverseproc)dictiter_traverse, /* tp_traverse */
2865 0, /* tp_clear */
2866 0, /* tp_richcompare */
2867 0, /* tp_weaklistoffset */
2868 PyObject_SelfIter, /* tp_iter */
2869 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
2870 dictiter_methods, /* tp_methods */
2871 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002872};
2873
2874static PyObject *dictiter_iternextvalue(dictiterobject *di)
2875{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002877 register Py_ssize_t i, mask, offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002878 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002879 PyObject **value_ptr;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002881 if (d == NULL)
2882 return NULL;
2883 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00002884
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002885 if (di->di_used != d->ma_used) {
2886 PyErr_SetString(PyExc_RuntimeError,
2887 "dictionary changed size during iteration");
2888 di->di_used = -1; /* Make this state sticky */
2889 return NULL;
2890 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002892 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002893 mask = DK_SIZE(d->ma_keys)-1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002894 if (i < 0 || i > mask)
2895 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002896 if (d->ma_values) {
2897 value_ptr = &d->ma_values[i];
2898 offset = sizeof(PyObject *);
2899 }
2900 else {
2901 value_ptr = &d->ma_keys->dk_entries[i].me_value;
2902 offset = sizeof(PyDictKeyEntry);
2903 }
2904 while (i <= mask && *value_ptr == NULL) {
2905 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002906 i++;
2907 if (i > mask)
2908 goto fail;
2909 }
2910 di->di_pos = i+1;
2911 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002912 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 Py_INCREF(value);
2914 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002915
2916fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 Py_DECREF(d);
2918 di->di_dict = NULL;
2919 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002920}
2921
2922PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002923 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2924 "dict_valueiterator", /* tp_name */
2925 sizeof(dictiterobject), /* tp_basicsize */
2926 0, /* tp_itemsize */
2927 /* methods */
2928 (destructor)dictiter_dealloc, /* tp_dealloc */
2929 0, /* tp_print */
2930 0, /* tp_getattr */
2931 0, /* tp_setattr */
2932 0, /* tp_reserved */
2933 0, /* tp_repr */
2934 0, /* tp_as_number */
2935 0, /* tp_as_sequence */
2936 0, /* tp_as_mapping */
2937 0, /* tp_hash */
2938 0, /* tp_call */
2939 0, /* tp_str */
2940 PyObject_GenericGetAttr, /* tp_getattro */
2941 0, /* tp_setattro */
2942 0, /* tp_as_buffer */
2943 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2944 0, /* tp_doc */
2945 (traverseproc)dictiter_traverse, /* tp_traverse */
2946 0, /* tp_clear */
2947 0, /* tp_richcompare */
2948 0, /* tp_weaklistoffset */
2949 PyObject_SelfIter, /* tp_iter */
2950 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
2951 dictiter_methods, /* tp_methods */
2952 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002953};
2954
2955static PyObject *dictiter_iternextitem(dictiterobject *di)
2956{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002957 PyObject *key, *value, *result = di->di_result;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002958 register Py_ssize_t i, mask, offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002959 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002960 PyObject **value_ptr;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002962 if (d == NULL)
2963 return NULL;
2964 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00002965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002966 if (di->di_used != d->ma_used) {
2967 PyErr_SetString(PyExc_RuntimeError,
2968 "dictionary changed size during iteration");
2969 di->di_used = -1; /* Make this state sticky */
2970 return NULL;
2971 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002973 i = di->di_pos;
2974 if (i < 0)
2975 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002976 mask = DK_SIZE(d->ma_keys)-1;
2977 if (d->ma_values) {
2978 value_ptr = &d->ma_values[i];
2979 offset = sizeof(PyObject *);
2980 }
2981 else {
2982 value_ptr = &d->ma_keys->dk_entries[i].me_value;
2983 offset = sizeof(PyDictKeyEntry);
2984 }
2985 while (i <= mask && *value_ptr == NULL) {
2986 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002987 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002988 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002989 di->di_pos = i+1;
2990 if (i > mask)
2991 goto fail;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002992
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002993 if (result->ob_refcnt == 1) {
2994 Py_INCREF(result);
2995 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2996 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2997 } else {
2998 result = PyTuple_New(2);
2999 if (result == NULL)
3000 return NULL;
3001 }
3002 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003003 key = d->ma_keys->dk_entries[i].me_key;
3004 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003005 Py_INCREF(key);
3006 Py_INCREF(value);
3007 PyTuple_SET_ITEM(result, 0, key);
3008 PyTuple_SET_ITEM(result, 1, value);
3009 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003010
3011fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003012 Py_DECREF(d);
3013 di->di_dict = NULL;
3014 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003015}
3016
3017PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003018 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3019 "dict_itemiterator", /* tp_name */
3020 sizeof(dictiterobject), /* tp_basicsize */
3021 0, /* tp_itemsize */
3022 /* methods */
3023 (destructor)dictiter_dealloc, /* tp_dealloc */
3024 0, /* tp_print */
3025 0, /* tp_getattr */
3026 0, /* tp_setattr */
3027 0, /* tp_reserved */
3028 0, /* tp_repr */
3029 0, /* tp_as_number */
3030 0, /* tp_as_sequence */
3031 0, /* tp_as_mapping */
3032 0, /* tp_hash */
3033 0, /* tp_call */
3034 0, /* tp_str */
3035 PyObject_GenericGetAttr, /* tp_getattro */
3036 0, /* tp_setattro */
3037 0, /* tp_as_buffer */
3038 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3039 0, /* tp_doc */
3040 (traverseproc)dictiter_traverse, /* tp_traverse */
3041 0, /* tp_clear */
3042 0, /* tp_richcompare */
3043 0, /* tp_weaklistoffset */
3044 PyObject_SelfIter, /* tp_iter */
3045 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3046 dictiter_methods, /* tp_methods */
3047 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003048};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003049
3050
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003051static PyObject *
3052dictiter_reduce(dictiterobject *di)
3053{
3054 PyObject *list;
3055 dictiterobject tmp;
3056
3057 list = PyList_New(0);
3058 if (!list)
3059 return NULL;
3060
3061 /* copy the itertor state */
3062 tmp = *di;
3063 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003064
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003065 /* iterate the temporary into a list */
3066 for(;;) {
3067 PyObject *element = 0;
3068 if (Py_TYPE(di) == &PyDictIterItem_Type)
3069 element = dictiter_iternextitem(&tmp);
3070 else if (Py_TYPE(di) == &PyDictIterKey_Type)
3071 element = dictiter_iternextkey(&tmp);
3072 else if (Py_TYPE(di) == &PyDictIterValue_Type)
3073 element = dictiter_iternextvalue(&tmp);
3074 else
3075 assert(0);
3076 if (element) {
3077 if (PyList_Append(list, element)) {
3078 Py_DECREF(element);
3079 Py_DECREF(list);
3080 Py_XDECREF(tmp.di_dict);
3081 return NULL;
3082 }
3083 Py_DECREF(element);
3084 } else
3085 break;
3086 }
3087 Py_XDECREF(tmp.di_dict);
3088 /* check for error */
3089 if (tmp.di_dict != NULL) {
3090 /* we have an error */
3091 Py_DECREF(list);
3092 return NULL;
3093 }
Antoine Pitroua7013882012-04-05 00:04:20 +02003094 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003095}
3096
Guido van Rossum3ac67412007-02-10 18:55:06 +00003097/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003098/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003099/***********************************************/
3100
Guido van Rossumb90c8482007-02-10 01:11:45 +00003101/* The instance lay-out is the same for all three; but the type differs. */
3102
3103typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003104 PyObject_HEAD
3105 PyDictObject *dv_dict;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003106} dictviewobject;
3107
3108
3109static void
Guido van Rossum3ac67412007-02-10 18:55:06 +00003110dictview_dealloc(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003111{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003112 Py_XDECREF(dv->dv_dict);
3113 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003114}
3115
3116static int
3117dictview_traverse(dictviewobject *dv, visitproc visit, void *arg)
3118{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003119 Py_VISIT(dv->dv_dict);
3120 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003121}
3122
Guido van Rossum83825ac2007-02-10 04:54:19 +00003123static Py_ssize_t
Guido van Rossum3ac67412007-02-10 18:55:06 +00003124dictview_len(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003125{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003126 Py_ssize_t len = 0;
3127 if (dv->dv_dict != NULL)
3128 len = dv->dv_dict->ma_used;
3129 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003130}
3131
3132static PyObject *
3133dictview_new(PyObject *dict, PyTypeObject *type)
3134{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003135 dictviewobject *dv;
3136 if (dict == NULL) {
3137 PyErr_BadInternalCall();
3138 return NULL;
3139 }
3140 if (!PyDict_Check(dict)) {
3141 /* XXX Get rid of this restriction later */
3142 PyErr_Format(PyExc_TypeError,
3143 "%s() requires a dict argument, not '%s'",
3144 type->tp_name, dict->ob_type->tp_name);
3145 return NULL;
3146 }
3147 dv = PyObject_GC_New(dictviewobject, type);
3148 if (dv == NULL)
3149 return NULL;
3150 Py_INCREF(dict);
3151 dv->dv_dict = (PyDictObject *)dict;
3152 _PyObject_GC_TRACK(dv);
3153 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003154}
3155
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003156/* TODO(guido): The views objects are not complete:
3157
3158 * support more set operations
3159 * support arbitrary mappings?
3160 - either these should be static or exported in dictobject.h
3161 - if public then they should probably be in builtins
3162*/
3163
Guido van Rossumaac530c2007-08-24 22:33:45 +00003164/* Return 1 if self is a subset of other, iterating over self;
3165 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003166static int
3167all_contained_in(PyObject *self, PyObject *other)
3168{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003169 PyObject *iter = PyObject_GetIter(self);
3170 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003172 if (iter == NULL)
3173 return -1;
3174 for (;;) {
3175 PyObject *next = PyIter_Next(iter);
3176 if (next == NULL) {
3177 if (PyErr_Occurred())
3178 ok = -1;
3179 break;
3180 }
3181 ok = PySequence_Contains(other, next);
3182 Py_DECREF(next);
3183 if (ok <= 0)
3184 break;
3185 }
3186 Py_DECREF(iter);
3187 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003188}
3189
3190static PyObject *
3191dictview_richcompare(PyObject *self, PyObject *other, int op)
3192{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003193 Py_ssize_t len_self, len_other;
3194 int ok;
3195 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003197 assert(self != NULL);
3198 assert(PyDictViewSet_Check(self));
3199 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00003200
Brian Curtindfc80e32011-08-10 20:28:54 -05003201 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
3202 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003204 len_self = PyObject_Size(self);
3205 if (len_self < 0)
3206 return NULL;
3207 len_other = PyObject_Size(other);
3208 if (len_other < 0)
3209 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003211 ok = 0;
3212 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00003213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003214 case Py_NE:
3215 case Py_EQ:
3216 if (len_self == len_other)
3217 ok = all_contained_in(self, other);
3218 if (op == Py_NE && ok >= 0)
3219 ok = !ok;
3220 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003222 case Py_LT:
3223 if (len_self < len_other)
3224 ok = all_contained_in(self, other);
3225 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003227 case Py_LE:
3228 if (len_self <= len_other)
3229 ok = all_contained_in(self, other);
3230 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003232 case Py_GT:
3233 if (len_self > len_other)
3234 ok = all_contained_in(other, self);
3235 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003237 case Py_GE:
3238 if (len_self >= len_other)
3239 ok = all_contained_in(other, self);
3240 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003242 }
3243 if (ok < 0)
3244 return NULL;
3245 result = ok ? Py_True : Py_False;
3246 Py_INCREF(result);
3247 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003248}
3249
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003250static PyObject *
3251dictview_repr(dictviewobject *dv)
3252{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003253 PyObject *seq;
3254 PyObject *result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003256 seq = PySequence_List((PyObject *)dv);
3257 if (seq == NULL)
3258 return NULL;
3259
3260 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
3261 Py_DECREF(seq);
3262 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003263}
3264
Guido van Rossum3ac67412007-02-10 18:55:06 +00003265/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003266
3267static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003268dictkeys_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003270 if (dv->dv_dict == NULL) {
3271 Py_RETURN_NONE;
3272 }
3273 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003274}
3275
3276static int
3277dictkeys_contains(dictviewobject *dv, PyObject *obj)
3278{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003279 if (dv->dv_dict == NULL)
3280 return 0;
3281 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003282}
3283
Guido van Rossum83825ac2007-02-10 04:54:19 +00003284static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003285 (lenfunc)dictview_len, /* sq_length */
3286 0, /* sq_concat */
3287 0, /* sq_repeat */
3288 0, /* sq_item */
3289 0, /* sq_slice */
3290 0, /* sq_ass_item */
3291 0, /* sq_ass_slice */
3292 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003293};
3294
Guido van Rossum523259b2007-08-24 23:41:22 +00003295static PyObject*
3296dictviews_sub(PyObject* self, PyObject *other)
3297{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003298 PyObject *result = PySet_New(self);
3299 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003300 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003302 if (result == NULL)
3303 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003304
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003305 tmp = _PyObject_CallMethodId(result, &PyId_difference_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003306 if (tmp == NULL) {
3307 Py_DECREF(result);
3308 return NULL;
3309 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003311 Py_DECREF(tmp);
3312 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003313}
3314
3315static PyObject*
3316dictviews_and(PyObject* self, PyObject *other)
3317{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003318 PyObject *result = PySet_New(self);
3319 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003320 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003322 if (result == NULL)
3323 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003324
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003325 tmp = _PyObject_CallMethodId(result, &PyId_intersection_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003326 if (tmp == NULL) {
3327 Py_DECREF(result);
3328 return NULL;
3329 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003331 Py_DECREF(tmp);
3332 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003333}
3334
3335static PyObject*
3336dictviews_or(PyObject* self, PyObject *other)
3337{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003338 PyObject *result = PySet_New(self);
3339 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003340 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003342 if (result == NULL)
3343 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003344
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003345 tmp = _PyObject_CallMethodId(result, &PyId_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003346 if (tmp == NULL) {
3347 Py_DECREF(result);
3348 return NULL;
3349 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003351 Py_DECREF(tmp);
3352 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003353}
3354
3355static PyObject*
3356dictviews_xor(PyObject* self, PyObject *other)
3357{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003358 PyObject *result = PySet_New(self);
3359 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003360 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003362 if (result == NULL)
3363 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003364
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003365 tmp = _PyObject_CallMethodId(result, &PyId_symmetric_difference_update, "O",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003366 other);
3367 if (tmp == NULL) {
3368 Py_DECREF(result);
3369 return NULL;
3370 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003372 Py_DECREF(tmp);
3373 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003374}
3375
3376static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003377 0, /*nb_add*/
3378 (binaryfunc)dictviews_sub, /*nb_subtract*/
3379 0, /*nb_multiply*/
3380 0, /*nb_remainder*/
3381 0, /*nb_divmod*/
3382 0, /*nb_power*/
3383 0, /*nb_negative*/
3384 0, /*nb_positive*/
3385 0, /*nb_absolute*/
3386 0, /*nb_bool*/
3387 0, /*nb_invert*/
3388 0, /*nb_lshift*/
3389 0, /*nb_rshift*/
3390 (binaryfunc)dictviews_and, /*nb_and*/
3391 (binaryfunc)dictviews_xor, /*nb_xor*/
3392 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00003393};
3394
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003395static PyObject*
3396dictviews_isdisjoint(PyObject *self, PyObject *other)
3397{
3398 PyObject *it;
3399 PyObject *item = NULL;
3400
3401 if (self == other) {
3402 if (dictview_len((dictviewobject *)self) == 0)
3403 Py_RETURN_TRUE;
3404 else
3405 Py_RETURN_FALSE;
3406 }
3407
3408 /* Iterate over the shorter object (only if other is a set,
3409 * because PySequence_Contains may be expensive otherwise): */
3410 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
3411 Py_ssize_t len_self = dictview_len((dictviewobject *)self);
3412 Py_ssize_t len_other = PyObject_Size(other);
3413 if (len_other == -1)
3414 return NULL;
3415
3416 if ((len_other > len_self)) {
3417 PyObject *tmp = other;
3418 other = self;
3419 self = tmp;
3420 }
3421 }
3422
3423 it = PyObject_GetIter(other);
3424 if (it == NULL)
3425 return NULL;
3426
3427 while ((item = PyIter_Next(it)) != NULL) {
3428 int contains = PySequence_Contains(self, item);
3429 Py_DECREF(item);
3430 if (contains == -1) {
3431 Py_DECREF(it);
3432 return NULL;
3433 }
3434
3435 if (contains) {
3436 Py_DECREF(it);
3437 Py_RETURN_FALSE;
3438 }
3439 }
3440 Py_DECREF(it);
3441 if (PyErr_Occurred())
3442 return NULL; /* PyIter_Next raised an exception. */
3443 Py_RETURN_TRUE;
3444}
3445
3446PyDoc_STRVAR(isdisjoint_doc,
3447"Return True if the view and the given iterable have a null intersection.");
3448
Guido van Rossumb90c8482007-02-10 01:11:45 +00003449static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003450 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3451 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003452 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003453};
3454
3455PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003456 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3457 "dict_keys", /* tp_name */
3458 sizeof(dictviewobject), /* tp_basicsize */
3459 0, /* tp_itemsize */
3460 /* methods */
3461 (destructor)dictview_dealloc, /* tp_dealloc */
3462 0, /* tp_print */
3463 0, /* tp_getattr */
3464 0, /* tp_setattr */
3465 0, /* tp_reserved */
3466 (reprfunc)dictview_repr, /* tp_repr */
3467 &dictviews_as_number, /* tp_as_number */
3468 &dictkeys_as_sequence, /* tp_as_sequence */
3469 0, /* tp_as_mapping */
3470 0, /* tp_hash */
3471 0, /* tp_call */
3472 0, /* tp_str */
3473 PyObject_GenericGetAttr, /* tp_getattro */
3474 0, /* tp_setattro */
3475 0, /* tp_as_buffer */
3476 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3477 0, /* tp_doc */
3478 (traverseproc)dictview_traverse, /* tp_traverse */
3479 0, /* tp_clear */
3480 dictview_richcompare, /* tp_richcompare */
3481 0, /* tp_weaklistoffset */
3482 (getiterfunc)dictkeys_iter, /* tp_iter */
3483 0, /* tp_iternext */
3484 dictkeys_methods, /* tp_methods */
3485 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003486};
3487
3488static PyObject *
3489dictkeys_new(PyObject *dict)
3490{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003491 return dictview_new(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003492}
3493
Guido van Rossum3ac67412007-02-10 18:55:06 +00003494/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003495
3496static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003497dictitems_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003499 if (dv->dv_dict == NULL) {
3500 Py_RETURN_NONE;
3501 }
3502 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003503}
3504
3505static int
3506dictitems_contains(dictviewobject *dv, PyObject *obj)
3507{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003508 PyObject *key, *value, *found;
3509 if (dv->dv_dict == NULL)
3510 return 0;
3511 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
3512 return 0;
3513 key = PyTuple_GET_ITEM(obj, 0);
3514 value = PyTuple_GET_ITEM(obj, 1);
3515 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
3516 if (found == NULL) {
3517 if (PyErr_Occurred())
3518 return -1;
3519 return 0;
3520 }
3521 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003522}
3523
Guido van Rossum83825ac2007-02-10 04:54:19 +00003524static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003525 (lenfunc)dictview_len, /* sq_length */
3526 0, /* sq_concat */
3527 0, /* sq_repeat */
3528 0, /* sq_item */
3529 0, /* sq_slice */
3530 0, /* sq_ass_item */
3531 0, /* sq_ass_slice */
3532 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003533};
3534
Guido van Rossumb90c8482007-02-10 01:11:45 +00003535static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003536 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3537 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003538 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003539};
3540
3541PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003542 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3543 "dict_items", /* tp_name */
3544 sizeof(dictviewobject), /* tp_basicsize */
3545 0, /* tp_itemsize */
3546 /* methods */
3547 (destructor)dictview_dealloc, /* tp_dealloc */
3548 0, /* tp_print */
3549 0, /* tp_getattr */
3550 0, /* tp_setattr */
3551 0, /* tp_reserved */
3552 (reprfunc)dictview_repr, /* tp_repr */
3553 &dictviews_as_number, /* tp_as_number */
3554 &dictitems_as_sequence, /* tp_as_sequence */
3555 0, /* tp_as_mapping */
3556 0, /* tp_hash */
3557 0, /* tp_call */
3558 0, /* tp_str */
3559 PyObject_GenericGetAttr, /* tp_getattro */
3560 0, /* tp_setattro */
3561 0, /* tp_as_buffer */
3562 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3563 0, /* tp_doc */
3564 (traverseproc)dictview_traverse, /* tp_traverse */
3565 0, /* tp_clear */
3566 dictview_richcompare, /* tp_richcompare */
3567 0, /* tp_weaklistoffset */
3568 (getiterfunc)dictitems_iter, /* tp_iter */
3569 0, /* tp_iternext */
3570 dictitems_methods, /* tp_methods */
3571 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003572};
3573
3574static PyObject *
3575dictitems_new(PyObject *dict)
3576{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003577 return dictview_new(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003578}
3579
Guido van Rossum3ac67412007-02-10 18:55:06 +00003580/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003581
3582static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003583dictvalues_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003584{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003585 if (dv->dv_dict == NULL) {
3586 Py_RETURN_NONE;
3587 }
3588 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003589}
3590
Guido van Rossum83825ac2007-02-10 04:54:19 +00003591static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003592 (lenfunc)dictview_len, /* sq_length */
3593 0, /* sq_concat */
3594 0, /* sq_repeat */
3595 0, /* sq_item */
3596 0, /* sq_slice */
3597 0, /* sq_ass_item */
3598 0, /* sq_ass_slice */
3599 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003600};
3601
Guido van Rossumb90c8482007-02-10 01:11:45 +00003602static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003603 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003604};
3605
3606PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003607 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3608 "dict_values", /* tp_name */
3609 sizeof(dictviewobject), /* tp_basicsize */
3610 0, /* tp_itemsize */
3611 /* methods */
3612 (destructor)dictview_dealloc, /* tp_dealloc */
3613 0, /* tp_print */
3614 0, /* tp_getattr */
3615 0, /* tp_setattr */
3616 0, /* tp_reserved */
3617 (reprfunc)dictview_repr, /* tp_repr */
3618 0, /* tp_as_number */
3619 &dictvalues_as_sequence, /* tp_as_sequence */
3620 0, /* tp_as_mapping */
3621 0, /* tp_hash */
3622 0, /* tp_call */
3623 0, /* tp_str */
3624 PyObject_GenericGetAttr, /* tp_getattro */
3625 0, /* tp_setattro */
3626 0, /* tp_as_buffer */
3627 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3628 0, /* tp_doc */
3629 (traverseproc)dictview_traverse, /* tp_traverse */
3630 0, /* tp_clear */
3631 0, /* tp_richcompare */
3632 0, /* tp_weaklistoffset */
3633 (getiterfunc)dictvalues_iter, /* tp_iter */
3634 0, /* tp_iternext */
3635 dictvalues_methods, /* tp_methods */
3636 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003637};
3638
3639static PyObject *
3640dictvalues_new(PyObject *dict)
3641{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003642 return dictview_new(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003643}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003644
3645/* Returns NULL if cannot allocate a new PyDictKeysObject,
3646 but does not set an error */
3647PyDictKeysObject *
3648_PyDict_NewKeysForClass(void)
3649{
3650 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE_SPLIT);
3651 if (keys == NULL)
3652 PyErr_Clear();
3653 else
3654 keys->dk_lookup = lookdict_split;
3655 return keys;
3656}
3657
3658#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
3659
3660PyObject *
3661PyObject_GenericGetDict(PyObject *obj, void *context)
3662{
3663 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
3664 if (dictptr == NULL) {
3665 PyErr_SetString(PyExc_AttributeError,
3666 "This object has no __dict__");
3667 return NULL;
3668 }
3669 dict = *dictptr;
3670 if (dict == NULL) {
3671 PyTypeObject *tp = Py_TYPE(obj);
3672 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
3673 DK_INCREF(CACHED_KEYS(tp));
3674 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
3675 }
3676 else {
3677 *dictptr = dict = PyDict_New();
3678 }
3679 }
3680 Py_XINCREF(dict);
3681 return dict;
3682}
3683
3684int
3685_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
3686 PyObject *key, PyObject *value)
3687{
3688 PyObject *dict;
3689 int res;
3690 PyDictKeysObject *cached;
3691
3692 assert(dictptr != NULL);
3693 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
3694 assert(dictptr != NULL);
3695 dict = *dictptr;
3696 if (dict == NULL) {
3697 DK_INCREF(cached);
3698 dict = new_dict_with_shared_keys(cached);
3699 if (dict == NULL)
3700 return -1;
3701 *dictptr = dict;
3702 }
3703 if (value == NULL) {
3704 res = PyDict_DelItem(dict, key);
3705 if (cached != ((PyDictObject *)dict)->ma_keys) {
3706 CACHED_KEYS(tp) = NULL;
3707 DK_DECREF(cached);
3708 }
3709 } else {
3710 res = PyDict_SetItem(dict, key, value);
3711 if (cached != ((PyDictObject *)dict)->ma_keys) {
3712 /* Either update tp->ht_cached_keys or delete it */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04003713 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003714 CACHED_KEYS(tp) = make_keys_shared(dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003715 } else {
3716 CACHED_KEYS(tp) = NULL;
3717 }
3718 DK_DECREF(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04003719 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
3720 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003721 }
3722 }
3723 } else {
3724 dict = *dictptr;
3725 if (dict == NULL) {
3726 dict = PyDict_New();
3727 if (dict == NULL)
3728 return -1;
3729 *dictptr = dict;
3730 }
3731 if (value == NULL) {
3732 res = PyDict_DelItem(dict, key);
3733 } else {
3734 res = PyDict_SetItem(dict, key, value);
3735 }
3736 }
3737 return res;
3738}
3739
3740void
3741_PyDictKeys_DecRef(PyDictKeysObject *keys)
3742{
3743 DK_DECREF(keys);
3744}
3745
3746
3747/* ARGSUSED */
3748static PyObject *
3749dummy_repr(PyObject *op)
3750{
3751 return PyUnicode_FromString("<dummy key>");
3752}
3753
3754/* ARGUSED */
3755static void
3756dummy_dealloc(PyObject* ignore)
3757{
3758 /* This should never get called, but we also don't want to SEGV if
3759 * we accidentally decref dummy-key out of existence.
3760 */
3761 Py_FatalError("deallocating <dummy key>");
3762}
3763
3764static PyTypeObject PyDictDummy_Type = {
3765 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3766 "<dummy key> type",
3767 0,
3768 0,
3769 dummy_dealloc, /*tp_dealloc*/ /*never called*/
3770 0, /*tp_print*/
3771 0, /*tp_getattr*/
3772 0, /*tp_setattr*/
3773 0, /*tp_reserved*/
3774 dummy_repr, /*tp_repr*/
3775 0, /*tp_as_number*/
3776 0, /*tp_as_sequence*/
3777 0, /*tp_as_mapping*/
3778 0, /*tp_hash */
3779 0, /*tp_call */
3780 0, /*tp_str */
3781 0, /*tp_getattro */
3782 0, /*tp_setattro */
3783 0, /*tp_as_buffer */
3784 Py_TPFLAGS_DEFAULT, /*tp_flags */
3785};
3786
3787static PyObject _dummy_struct = {
3788 _PyObject_EXTRA_INIT
3789 2, &PyDictDummy_Type
3790};
3791