blob: a3c640939db62b6fb03000f8412a6dd58ef792ad [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;
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002117 PyObject **vaddr;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002118 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002119 /* temporarily bump aval's refcount to ensure it stays
2120 alive until we're done with it */
2121 Py_INCREF(aval);
2122 /* ditto for key */
2123 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002124 /* reuse the known hash value */
2125 if ((b->ma_keys->dk_lookup)(b, key, ep->me_hash, &vaddr) == NULL)
2126 bval = NULL;
2127 else
2128 bval = *vaddr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002129 Py_DECREF(key);
2130 if (bval == NULL) {
2131 Py_DECREF(aval);
2132 if (PyErr_Occurred())
2133 return -1;
2134 return 0;
2135 }
2136 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
2137 Py_DECREF(aval);
2138 if (cmp <= 0) /* error or not equal */
2139 return cmp;
2140 }
2141 }
2142 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002143}
Tim Peterse63415e2001-05-08 04:38:29 +00002144
2145static PyObject *
2146dict_richcompare(PyObject *v, PyObject *w, int op)
2147{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002148 int cmp;
2149 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002151 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2152 res = Py_NotImplemented;
2153 }
2154 else if (op == Py_EQ || op == Py_NE) {
2155 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2156 if (cmp < 0)
2157 return NULL;
2158 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2159 }
2160 else
2161 res = Py_NotImplemented;
2162 Py_INCREF(res);
2163 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002164}
Tim Peterse63415e2001-05-08 04:38:29 +00002165
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002166static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002167dict_contains(register PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002168{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002169 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002170 PyDictKeyEntry *ep;
2171 PyObject **value_addr;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002173 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002174 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175 hash = PyObject_Hash(key);
2176 if (hash == -1)
2177 return NULL;
2178 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002179 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002180 if (ep == NULL)
2181 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002182 return PyBool_FromLong(*value_addr != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002183}
2184
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002185static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002186dict_get(register PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00002187{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002188 PyObject *key;
2189 PyObject *failobj = Py_None;
2190 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002191 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002192 PyDictKeyEntry *ep;
2193 PyObject **value_addr;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002195 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
2196 return NULL;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002197
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002198 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002199 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002200 hash = PyObject_Hash(key);
2201 if (hash == -1)
2202 return NULL;
2203 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002204 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002205 if (ep == NULL)
2206 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002207 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002208 if (val == NULL)
2209 val = failobj;
2210 Py_INCREF(val);
2211 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002212}
2213
Barry Warsawc38c5da1997-10-06 17:49:20 +00002214static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002215dict_setdefault(register PyDictObject *mp, PyObject *args)
Guido van Rossum164452c2000-08-08 16:12:54 +00002216{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002217 PyObject *key;
2218 PyObject *failobj = Py_None;
2219 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002220 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002221 PyDictKeyEntry *ep;
2222 PyObject **value_addr;
Guido van Rossum164452c2000-08-08 16:12:54 +00002223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002224 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
2225 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00002226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002227 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002228 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002229 hash = PyObject_Hash(key);
2230 if (hash == -1)
2231 return NULL;
2232 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002233 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002234 if (ep == NULL)
2235 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002236 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002237 if (val == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002238 Py_INCREF(failobj);
2239 Py_INCREF(key);
2240 if (mp->ma_keys->dk_usable <= 0) {
2241 /* Need to resize. */
2242 if (insertion_resize(mp) < 0)
2243 return NULL;
2244 ep = find_empty_slot(mp, key, hash, &value_addr);
2245 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002246 MAINTAIN_TRACKING(mp, key, failobj);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002247 ep->me_key = key;
2248 ep->me_hash = hash;
2249 *value_addr = failobj;
2250 val = failobj;
2251 mp->ma_keys->dk_usable--;
2252 mp->ma_used++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002253 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002254 Py_INCREF(val);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002255 return val;
Guido van Rossum164452c2000-08-08 16:12:54 +00002256}
2257
2258
2259static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002260dict_clear(register PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002261{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002262 PyDict_Clear((PyObject *)mp);
2263 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002264}
2265
Guido van Rossumba6ab842000-12-12 22:02:18 +00002266static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002267dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002268{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002269 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002270 PyObject *old_value, *old_key;
2271 PyObject *key, *deflt = NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002272 PyDictKeyEntry *ep;
2273 PyObject **value_addr;
Guido van Rossume027d982002-04-12 15:11:59 +00002274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002275 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2276 return NULL;
2277 if (mp->ma_used == 0) {
2278 if (deflt) {
2279 Py_INCREF(deflt);
2280 return deflt;
2281 }
Raymond Hettingerdd421542010-10-30 08:10:29 +00002282 set_key_error(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002283 return NULL;
2284 }
2285 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002286 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002287 hash = PyObject_Hash(key);
2288 if (hash == -1)
2289 return NULL;
2290 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002291 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002292 if (ep == NULL)
2293 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002294 old_value = *value_addr;
2295 if (old_value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002296 if (deflt) {
2297 Py_INCREF(deflt);
2298 return deflt;
2299 }
2300 set_key_error(key);
2301 return NULL;
2302 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002303 *value_addr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002304 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002305 if (!_PyDict_HasSplitTable(mp)) {
2306 ENSURE_ALLOWS_DELETIONS(mp);
2307 old_key = ep->me_key;
2308 Py_INCREF(dummy);
2309 ep->me_key = dummy;
2310 Py_DECREF(old_key);
2311 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002312 return old_value;
Guido van Rossume027d982002-04-12 15:11:59 +00002313}
2314
2315static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002316dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00002317{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002318 Py_hash_t i = 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002319 PyDictKeyEntry *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002320 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002321
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002323 /* Allocate the result tuple before checking the size. Believe it
2324 * or not, this allocation could trigger a garbage collection which
2325 * could empty the dict, so if we checked the size first and that
2326 * happened, the result would be an infinite loop (searching for an
2327 * entry that no longer exists). Note that the usual popitem()
2328 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2329 * tuple away if the dict *is* empty isn't a significant
2330 * inefficiency -- possible, but unlikely in practice.
2331 */
2332 res = PyTuple_New(2);
2333 if (res == NULL)
2334 return NULL;
2335 if (mp->ma_used == 0) {
2336 Py_DECREF(res);
2337 PyErr_SetString(PyExc_KeyError,
2338 "popitem(): dictionary is empty");
2339 return NULL;
2340 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002341 /* Convert split table to combined table */
2342 if (mp->ma_keys->dk_lookup == lookdict_split) {
2343 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
2344 Py_DECREF(res);
2345 return NULL;
2346 }
2347 }
2348 ENSURE_ALLOWS_DELETIONS(mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002349 /* Set ep to "the first" dict entry with a value. We abuse the hash
2350 * field of slot 0 to hold a search finger:
2351 * If slot 0 has a value, use slot 0.
2352 * Else slot 0 is being used to hold a search finger,
2353 * and we use its hash value as the first index to look.
2354 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002355 ep = &mp->ma_keys->dk_entries[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002356 if (ep->me_value == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002357 Py_ssize_t mask = DK_MASK(mp->ma_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 i = ep->me_hash;
2359 /* The hash field may be a real hash value, or it may be a
2360 * legit search finger, or it may be a once-legit search
2361 * finger that's out of bounds now because it wrapped around
2362 * or the table shrunk -- simply make sure it's in bounds now.
2363 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002364 if (i > mask || i < 1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002365 i = 1; /* skip slot 0 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002366 while ((ep = &mp->ma_keys->dk_entries[i])->me_value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002367 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002368 if (i > mask)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 i = 1;
2370 }
2371 }
2372 PyTuple_SET_ITEM(res, 0, ep->me_key);
2373 PyTuple_SET_ITEM(res, 1, ep->me_value);
2374 Py_INCREF(dummy);
2375 ep->me_key = dummy;
2376 ep->me_value = NULL;
2377 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002378 assert(mp->ma_keys->dk_entries[0].me_value == NULL);
2379 mp->ma_keys->dk_entries[0].me_hash = i + 1; /* next place to start */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002380 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002381}
2382
Jeremy Hylton8caad492000-06-23 14:18:11 +00002383static int
2384dict_traverse(PyObject *op, visitproc visit, void *arg)
2385{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002386 Py_ssize_t i, n;
2387 PyDictObject *mp = (PyDictObject *)op;
2388 if (mp->ma_keys->dk_lookup == lookdict) {
2389 for (i = 0; i < DK_SIZE(mp->ma_keys); i++) {
2390 if (mp->ma_keys->dk_entries[i].me_value != NULL) {
2391 Py_VISIT(mp->ma_keys->dk_entries[i].me_value);
2392 Py_VISIT(mp->ma_keys->dk_entries[i].me_key);
2393 }
2394 }
2395 } else {
2396 if (mp->ma_values != NULL) {
2397 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2398 Py_VISIT(mp->ma_values[i]);
2399 }
2400 }
2401 else {
2402 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2403 Py_VISIT(mp->ma_keys->dk_entries[i].me_value);
2404 }
2405 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002406 }
2407 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002408}
2409
2410static int
2411dict_tp_clear(PyObject *op)
2412{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002413 PyDict_Clear(op);
2414 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002415}
2416
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002417static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002418
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002419static PyObject *
2420dict_sizeof(PyDictObject *mp)
2421{
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002422 Py_ssize_t size, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002423
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002424 size = DK_SIZE(mp->ma_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002425 res = sizeof(PyDictObject);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002426 if (mp->ma_values)
2427 res += size * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002428 /* If the dictionary is split, the keys portion is accounted-for
2429 in the type object. */
2430 if (mp->ma_keys->dk_refcnt == 1)
2431 res += sizeof(PyDictKeysObject) + (size-1) * sizeof(PyDictKeyEntry);
2432 return PyLong_FromSsize_t(res);
2433}
2434
2435Py_ssize_t
2436_PyDict_KeysSize(PyDictKeysObject *keys)
2437{
2438 return sizeof(PyDictKeysObject) + (DK_SIZE(keys)-1) * sizeof(PyDictKeyEntry);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002439}
2440
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002441PyDoc_STRVAR(contains__doc__,
2442"D.__contains__(k) -> True if D has a key k, else False");
2443
2444PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
2445
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002446PyDoc_STRVAR(sizeof__doc__,
2447"D.__sizeof__() -> size of D in memory, in bytes");
2448
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002449PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002450"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002451
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002452PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002453"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 +00002454
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002455PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002456"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002457If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00002458
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002459PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00002460"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000024612-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002462
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002463PyDoc_STRVAR(update__doc__,
Georg Brandlac0675c2011-12-18 19:30:55 +01002464"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n"
2465"If E present and has a .keys() method, does: for k in E: D[k] = E[k]\n\
2466If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002467In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00002468
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002469PyDoc_STRVAR(fromkeys__doc__,
2470"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
2471v defaults to None.");
2472
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002473PyDoc_STRVAR(clear__doc__,
2474"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002475
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002476PyDoc_STRVAR(copy__doc__,
2477"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00002478
Guido van Rossumb90c8482007-02-10 01:11:45 +00002479/* Forward */
2480static PyObject *dictkeys_new(PyObject *);
2481static PyObject *dictitems_new(PyObject *);
2482static PyObject *dictvalues_new(PyObject *);
2483
Guido van Rossum45c85d12007-07-27 16:31:40 +00002484PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002485 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002486PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002487 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002488PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002489 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00002490
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002491static PyMethodDef mapp_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002492 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
2493 contains__doc__},
2494 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
2495 getitem__doc__},
2496 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
2497 sizeof__doc__},
2498 {"get", (PyCFunction)dict_get, METH_VARARGS,
2499 get__doc__},
2500 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
2501 setdefault_doc__},
2502 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
2503 pop__doc__},
2504 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
2505 popitem__doc__},
2506 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
2507 keys__doc__},
2508 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
2509 items__doc__},
2510 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
2511 values__doc__},
2512 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
2513 update__doc__},
2514 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
2515 fromkeys__doc__},
2516 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
2517 clear__doc__},
2518 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
2519 copy__doc__},
2520 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002521};
2522
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002523/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002524int
2525PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002526{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002527 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002528 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002529 PyDictKeyEntry *ep;
2530 PyObject **value_addr;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002532 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002533 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002534 hash = PyObject_Hash(key);
2535 if (hash == -1)
2536 return -1;
2537 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002538 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
2539 return (ep == NULL) ? -1 : (*value_addr != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002540}
2541
Thomas Wouterscf297e42007-02-23 15:07:44 +00002542/* Internal version of PyDict_Contains used when the hash value is already known */
2543int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002544_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00002545{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002546 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002547 PyDictKeyEntry *ep;
2548 PyObject **value_addr;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002549
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002550 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
2551 return (ep == NULL) ? -1 : (*value_addr != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00002552}
2553
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002554/* Hack to implement "key in dict" */
2555static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002556 0, /* sq_length */
2557 0, /* sq_concat */
2558 0, /* sq_repeat */
2559 0, /* sq_item */
2560 0, /* sq_slice */
2561 0, /* sq_ass_item */
2562 0, /* sq_ass_slice */
2563 PyDict_Contains, /* sq_contains */
2564 0, /* sq_inplace_concat */
2565 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002566};
2567
Guido van Rossum09e563a2001-05-01 12:10:21 +00002568static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002569dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2570{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002571 PyObject *self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002573 assert(type != NULL && type->tp_alloc != NULL);
2574 self = type->tp_alloc(type, 0);
2575 if (self != NULL) {
2576 PyDictObject *d = (PyDictObject *)self;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002577 d->ma_keys = new_keys_object(PyDict_MINSIZE_COMBINED);
2578 /* XXX - Should we raise a no-memory error? */
2579 if (d->ma_keys == NULL) {
2580 DK_INCREF(Py_EMPTY_KEYS);
2581 d->ma_keys = Py_EMPTY_KEYS;
2582 d->ma_values = empty_values;
2583 }
2584 d->ma_used = 0;
Ezio Melotti13925002011-03-16 11:05:33 +02002585 /* The object has been implicitly tracked by tp_alloc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002586 if (type == &PyDict_Type)
2587 _PyObject_GC_UNTRACK(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002588 }
2589 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002590}
2591
Tim Peters25786c02001-09-02 08:22:48 +00002592static int
2593dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2594{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002595 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002596}
2597
Tim Peters6d6c1a32001-08-02 04:15:00 +00002598static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002599dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002600{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002601 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002602}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002603
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002604PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002605"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002606"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002607" (key, value) pairs\n"
2608"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002609" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002610" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002611" d[k] = v\n"
2612"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2613" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002614
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002615PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002616 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2617 "dict",
2618 sizeof(PyDictObject),
2619 0,
2620 (destructor)dict_dealloc, /* tp_dealloc */
2621 0, /* tp_print */
2622 0, /* tp_getattr */
2623 0, /* tp_setattr */
2624 0, /* tp_reserved */
2625 (reprfunc)dict_repr, /* tp_repr */
2626 0, /* tp_as_number */
2627 &dict_as_sequence, /* tp_as_sequence */
2628 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002629 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002630 0, /* tp_call */
2631 0, /* tp_str */
2632 PyObject_GenericGetAttr, /* tp_getattro */
2633 0, /* tp_setattro */
2634 0, /* tp_as_buffer */
2635 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2636 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
2637 dictionary_doc, /* tp_doc */
2638 dict_traverse, /* tp_traverse */
2639 dict_tp_clear, /* tp_clear */
2640 dict_richcompare, /* tp_richcompare */
2641 0, /* tp_weaklistoffset */
2642 (getiterfunc)dict_iter, /* tp_iter */
2643 0, /* tp_iternext */
2644 mapp_methods, /* tp_methods */
2645 0, /* tp_members */
2646 0, /* tp_getset */
2647 0, /* tp_base */
2648 0, /* tp_dict */
2649 0, /* tp_descr_get */
2650 0, /* tp_descr_set */
2651 0, /* tp_dictoffset */
2652 dict_init, /* tp_init */
2653 PyType_GenericAlloc, /* tp_alloc */
2654 dict_new, /* tp_new */
2655 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002656};
2657
Victor Stinner3c1e4812012-03-26 22:10:51 +02002658PyObject *
2659_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
2660{
2661 PyObject *kv;
2662 kv = _PyUnicode_FromId(key); /* borrowed */
2663 if (kv == NULL)
2664 return NULL;
2665 return PyDict_GetItem(dp, kv);
2666}
2667
Guido van Rossum3cca2451997-05-16 14:23:33 +00002668/* For backward compatibility with old dictionary interface */
2669
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002670PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002671PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002672{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002673 PyObject *kv, *rv;
2674 kv = PyUnicode_FromString(key);
2675 if (kv == NULL)
2676 return NULL;
2677 rv = PyDict_GetItem(v, kv);
2678 Py_DECREF(kv);
2679 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002680}
2681
2682int
Victor Stinner3c1e4812012-03-26 22:10:51 +02002683_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
2684{
2685 PyObject *kv;
2686 kv = _PyUnicode_FromId(key); /* borrowed */
2687 if (kv == NULL)
2688 return -1;
2689 return PyDict_SetItem(v, kv, item);
2690}
2691
2692int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002693PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002694{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002695 PyObject *kv;
2696 int err;
2697 kv = PyUnicode_FromString(key);
2698 if (kv == NULL)
2699 return -1;
2700 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
2701 err = PyDict_SetItem(v, kv, item);
2702 Py_DECREF(kv);
2703 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002704}
2705
2706int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002707PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002708{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002709 PyObject *kv;
2710 int err;
2711 kv = PyUnicode_FromString(key);
2712 if (kv == NULL)
2713 return -1;
2714 err = PyDict_DelItem(v, kv);
2715 Py_DECREF(kv);
2716 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002717}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002718
Raymond Hettinger019a1482004-03-18 02:41:19 +00002719/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002720
2721typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002722 PyObject_HEAD
2723 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
2724 Py_ssize_t di_used;
2725 Py_ssize_t di_pos;
2726 PyObject* di_result; /* reusable result tuple for iteritems */
2727 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002728} dictiterobject;
2729
2730static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002731dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002732{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002733 dictiterobject *di;
2734 di = PyObject_GC_New(dictiterobject, itertype);
2735 if (di == NULL)
2736 return NULL;
2737 Py_INCREF(dict);
2738 di->di_dict = dict;
2739 di->di_used = dict->ma_used;
2740 di->di_pos = 0;
2741 di->len = dict->ma_used;
2742 if (itertype == &PyDictIterItem_Type) {
2743 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2744 if (di->di_result == NULL) {
2745 Py_DECREF(di);
2746 return NULL;
2747 }
2748 }
2749 else
2750 di->di_result = NULL;
2751 _PyObject_GC_TRACK(di);
2752 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002753}
2754
2755static void
2756dictiter_dealloc(dictiterobject *di)
2757{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002758 Py_XDECREF(di->di_dict);
2759 Py_XDECREF(di->di_result);
2760 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002761}
2762
2763static int
2764dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
2765{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002766 Py_VISIT(di->di_dict);
2767 Py_VISIT(di->di_result);
2768 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002769}
2770
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002771static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002772dictiter_len(dictiterobject *di)
2773{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002774 Py_ssize_t len = 0;
2775 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
2776 len = di->len;
2777 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002778}
2779
Guido van Rossumb90c8482007-02-10 01:11:45 +00002780PyDoc_STRVAR(length_hint_doc,
2781 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002782
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002783static PyObject *
2784dictiter_reduce(dictiterobject *di);
2785
2786PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
2787
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002788static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002789 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
2790 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002791 {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS,
2792 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002793 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002794};
2795
Raymond Hettinger019a1482004-03-18 02:41:19 +00002796static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002797{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002798 PyObject *key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002799 register Py_ssize_t i, mask, offset;
2800 register PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002801 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002802 PyObject **value_ptr;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002803
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002804 if (d == NULL)
2805 return NULL;
2806 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002808 if (di->di_used != d->ma_used) {
2809 PyErr_SetString(PyExc_RuntimeError,
2810 "dictionary changed size during iteration");
2811 di->di_used = -1; /* Make this state sticky */
2812 return NULL;
2813 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002814
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002815 i = di->di_pos;
2816 if (i < 0)
2817 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002818 k = d->ma_keys;
2819 if (d->ma_values) {
2820 value_ptr = &d->ma_values[i];
2821 offset = sizeof(PyObject *);
2822 }
2823 else {
2824 value_ptr = &k->dk_entries[i].me_value;
2825 offset = sizeof(PyDictKeyEntry);
2826 }
2827 mask = DK_SIZE(k)-1;
2828 while (i <= mask && *value_ptr == NULL) {
2829 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002830 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002831 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002832 di->di_pos = i+1;
2833 if (i > mask)
2834 goto fail;
2835 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002836 key = k->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002837 Py_INCREF(key);
2838 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002839
2840fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002841 Py_DECREF(d);
2842 di->di_dict = NULL;
2843 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002844}
2845
Raymond Hettinger019a1482004-03-18 02:41:19 +00002846PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002847 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2848 "dict_keyiterator", /* tp_name */
2849 sizeof(dictiterobject), /* tp_basicsize */
2850 0, /* tp_itemsize */
2851 /* methods */
2852 (destructor)dictiter_dealloc, /* tp_dealloc */
2853 0, /* tp_print */
2854 0, /* tp_getattr */
2855 0, /* tp_setattr */
2856 0, /* tp_reserved */
2857 0, /* tp_repr */
2858 0, /* tp_as_number */
2859 0, /* tp_as_sequence */
2860 0, /* tp_as_mapping */
2861 0, /* tp_hash */
2862 0, /* tp_call */
2863 0, /* tp_str */
2864 PyObject_GenericGetAttr, /* tp_getattro */
2865 0, /* tp_setattro */
2866 0, /* tp_as_buffer */
2867 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2868 0, /* tp_doc */
2869 (traverseproc)dictiter_traverse, /* tp_traverse */
2870 0, /* tp_clear */
2871 0, /* tp_richcompare */
2872 0, /* tp_weaklistoffset */
2873 PyObject_SelfIter, /* tp_iter */
2874 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
2875 dictiter_methods, /* tp_methods */
2876 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002877};
2878
2879static PyObject *dictiter_iternextvalue(dictiterobject *di)
2880{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002881 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002882 register Py_ssize_t i, mask, offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002883 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002884 PyObject **value_ptr;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002886 if (d == NULL)
2887 return NULL;
2888 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00002889
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002890 if (di->di_used != d->ma_used) {
2891 PyErr_SetString(PyExc_RuntimeError,
2892 "dictionary changed size during iteration");
2893 di->di_used = -1; /* Make this state sticky */
2894 return NULL;
2895 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002896
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002897 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002898 mask = DK_SIZE(d->ma_keys)-1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002899 if (i < 0 || i > mask)
2900 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002901 if (d->ma_values) {
2902 value_ptr = &d->ma_values[i];
2903 offset = sizeof(PyObject *);
2904 }
2905 else {
2906 value_ptr = &d->ma_keys->dk_entries[i].me_value;
2907 offset = sizeof(PyDictKeyEntry);
2908 }
2909 while (i <= mask && *value_ptr == NULL) {
2910 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002911 i++;
2912 if (i > mask)
2913 goto fail;
2914 }
2915 di->di_pos = i+1;
2916 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002917 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002918 Py_INCREF(value);
2919 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002920
2921fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002922 Py_DECREF(d);
2923 di->di_dict = NULL;
2924 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002925}
2926
2927PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002928 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2929 "dict_valueiterator", /* tp_name */
2930 sizeof(dictiterobject), /* tp_basicsize */
2931 0, /* tp_itemsize */
2932 /* methods */
2933 (destructor)dictiter_dealloc, /* tp_dealloc */
2934 0, /* tp_print */
2935 0, /* tp_getattr */
2936 0, /* tp_setattr */
2937 0, /* tp_reserved */
2938 0, /* tp_repr */
2939 0, /* tp_as_number */
2940 0, /* tp_as_sequence */
2941 0, /* tp_as_mapping */
2942 0, /* tp_hash */
2943 0, /* tp_call */
2944 0, /* tp_str */
2945 PyObject_GenericGetAttr, /* tp_getattro */
2946 0, /* tp_setattro */
2947 0, /* tp_as_buffer */
2948 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2949 0, /* tp_doc */
2950 (traverseproc)dictiter_traverse, /* tp_traverse */
2951 0, /* tp_clear */
2952 0, /* tp_richcompare */
2953 0, /* tp_weaklistoffset */
2954 PyObject_SelfIter, /* tp_iter */
2955 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
2956 dictiter_methods, /* tp_methods */
2957 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002958};
2959
2960static PyObject *dictiter_iternextitem(dictiterobject *di)
2961{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002962 PyObject *key, *value, *result = di->di_result;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002963 register Py_ssize_t i, mask, offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002964 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002965 PyObject **value_ptr;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002966
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002967 if (d == NULL)
2968 return NULL;
2969 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00002970
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002971 if (di->di_used != d->ma_used) {
2972 PyErr_SetString(PyExc_RuntimeError,
2973 "dictionary changed size during iteration");
2974 di->di_used = -1; /* Make this state sticky */
2975 return NULL;
2976 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002977
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002978 i = di->di_pos;
2979 if (i < 0)
2980 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002981 mask = DK_SIZE(d->ma_keys)-1;
2982 if (d->ma_values) {
2983 value_ptr = &d->ma_values[i];
2984 offset = sizeof(PyObject *);
2985 }
2986 else {
2987 value_ptr = &d->ma_keys->dk_entries[i].me_value;
2988 offset = sizeof(PyDictKeyEntry);
2989 }
2990 while (i <= mask && *value_ptr == NULL) {
2991 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002992 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002993 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002994 di->di_pos = i+1;
2995 if (i > mask)
2996 goto fail;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002998 if (result->ob_refcnt == 1) {
2999 Py_INCREF(result);
3000 Py_DECREF(PyTuple_GET_ITEM(result, 0));
3001 Py_DECREF(PyTuple_GET_ITEM(result, 1));
3002 } else {
3003 result = PyTuple_New(2);
3004 if (result == NULL)
3005 return NULL;
3006 }
3007 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003008 key = d->ma_keys->dk_entries[i].me_key;
3009 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003010 Py_INCREF(key);
3011 Py_INCREF(value);
3012 PyTuple_SET_ITEM(result, 0, key);
3013 PyTuple_SET_ITEM(result, 1, value);
3014 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003015
3016fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003017 Py_DECREF(d);
3018 di->di_dict = NULL;
3019 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003020}
3021
3022PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003023 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3024 "dict_itemiterator", /* tp_name */
3025 sizeof(dictiterobject), /* tp_basicsize */
3026 0, /* tp_itemsize */
3027 /* methods */
3028 (destructor)dictiter_dealloc, /* tp_dealloc */
3029 0, /* tp_print */
3030 0, /* tp_getattr */
3031 0, /* tp_setattr */
3032 0, /* tp_reserved */
3033 0, /* tp_repr */
3034 0, /* tp_as_number */
3035 0, /* tp_as_sequence */
3036 0, /* tp_as_mapping */
3037 0, /* tp_hash */
3038 0, /* tp_call */
3039 0, /* tp_str */
3040 PyObject_GenericGetAttr, /* tp_getattro */
3041 0, /* tp_setattro */
3042 0, /* tp_as_buffer */
3043 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3044 0, /* tp_doc */
3045 (traverseproc)dictiter_traverse, /* tp_traverse */
3046 0, /* tp_clear */
3047 0, /* tp_richcompare */
3048 0, /* tp_weaklistoffset */
3049 PyObject_SelfIter, /* tp_iter */
3050 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3051 dictiter_methods, /* tp_methods */
3052 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003053};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003054
3055
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003056static PyObject *
3057dictiter_reduce(dictiterobject *di)
3058{
3059 PyObject *list;
3060 dictiterobject tmp;
3061
3062 list = PyList_New(0);
3063 if (!list)
3064 return NULL;
3065
3066 /* copy the itertor state */
3067 tmp = *di;
3068 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003069
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003070 /* iterate the temporary into a list */
3071 for(;;) {
3072 PyObject *element = 0;
3073 if (Py_TYPE(di) == &PyDictIterItem_Type)
3074 element = dictiter_iternextitem(&tmp);
3075 else if (Py_TYPE(di) == &PyDictIterKey_Type)
3076 element = dictiter_iternextkey(&tmp);
3077 else if (Py_TYPE(di) == &PyDictIterValue_Type)
3078 element = dictiter_iternextvalue(&tmp);
3079 else
3080 assert(0);
3081 if (element) {
3082 if (PyList_Append(list, element)) {
3083 Py_DECREF(element);
3084 Py_DECREF(list);
3085 Py_XDECREF(tmp.di_dict);
3086 return NULL;
3087 }
3088 Py_DECREF(element);
3089 } else
3090 break;
3091 }
3092 Py_XDECREF(tmp.di_dict);
3093 /* check for error */
3094 if (tmp.di_dict != NULL) {
3095 /* we have an error */
3096 Py_DECREF(list);
3097 return NULL;
3098 }
Antoine Pitroua7013882012-04-05 00:04:20 +02003099 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003100}
3101
Guido van Rossum3ac67412007-02-10 18:55:06 +00003102/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003103/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003104/***********************************************/
3105
Guido van Rossumb90c8482007-02-10 01:11:45 +00003106/* The instance lay-out is the same for all three; but the type differs. */
3107
3108typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003109 PyObject_HEAD
3110 PyDictObject *dv_dict;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003111} dictviewobject;
3112
3113
3114static void
Guido van Rossum3ac67412007-02-10 18:55:06 +00003115dictview_dealloc(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003116{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003117 Py_XDECREF(dv->dv_dict);
3118 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003119}
3120
3121static int
3122dictview_traverse(dictviewobject *dv, visitproc visit, void *arg)
3123{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003124 Py_VISIT(dv->dv_dict);
3125 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003126}
3127
Guido van Rossum83825ac2007-02-10 04:54:19 +00003128static Py_ssize_t
Guido van Rossum3ac67412007-02-10 18:55:06 +00003129dictview_len(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003130{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003131 Py_ssize_t len = 0;
3132 if (dv->dv_dict != NULL)
3133 len = dv->dv_dict->ma_used;
3134 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003135}
3136
3137static PyObject *
3138dictview_new(PyObject *dict, PyTypeObject *type)
3139{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003140 dictviewobject *dv;
3141 if (dict == NULL) {
3142 PyErr_BadInternalCall();
3143 return NULL;
3144 }
3145 if (!PyDict_Check(dict)) {
3146 /* XXX Get rid of this restriction later */
3147 PyErr_Format(PyExc_TypeError,
3148 "%s() requires a dict argument, not '%s'",
3149 type->tp_name, dict->ob_type->tp_name);
3150 return NULL;
3151 }
3152 dv = PyObject_GC_New(dictviewobject, type);
3153 if (dv == NULL)
3154 return NULL;
3155 Py_INCREF(dict);
3156 dv->dv_dict = (PyDictObject *)dict;
3157 _PyObject_GC_TRACK(dv);
3158 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003159}
3160
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003161/* TODO(guido): The views objects are not complete:
3162
3163 * support more set operations
3164 * support arbitrary mappings?
3165 - either these should be static or exported in dictobject.h
3166 - if public then they should probably be in builtins
3167*/
3168
Guido van Rossumaac530c2007-08-24 22:33:45 +00003169/* Return 1 if self is a subset of other, iterating over self;
3170 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003171static int
3172all_contained_in(PyObject *self, PyObject *other)
3173{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003174 PyObject *iter = PyObject_GetIter(self);
3175 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003177 if (iter == NULL)
3178 return -1;
3179 for (;;) {
3180 PyObject *next = PyIter_Next(iter);
3181 if (next == NULL) {
3182 if (PyErr_Occurred())
3183 ok = -1;
3184 break;
3185 }
3186 ok = PySequence_Contains(other, next);
3187 Py_DECREF(next);
3188 if (ok <= 0)
3189 break;
3190 }
3191 Py_DECREF(iter);
3192 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003193}
3194
3195static PyObject *
3196dictview_richcompare(PyObject *self, PyObject *other, int op)
3197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003198 Py_ssize_t len_self, len_other;
3199 int ok;
3200 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003202 assert(self != NULL);
3203 assert(PyDictViewSet_Check(self));
3204 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00003205
Brian Curtindfc80e32011-08-10 20:28:54 -05003206 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
3207 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003209 len_self = PyObject_Size(self);
3210 if (len_self < 0)
3211 return NULL;
3212 len_other = PyObject_Size(other);
3213 if (len_other < 0)
3214 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003216 ok = 0;
3217 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00003218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003219 case Py_NE:
3220 case Py_EQ:
3221 if (len_self == len_other)
3222 ok = all_contained_in(self, other);
3223 if (op == Py_NE && ok >= 0)
3224 ok = !ok;
3225 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003227 case Py_LT:
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_LE:
3233 if (len_self <= len_other)
3234 ok = all_contained_in(self, other);
3235 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003237 case Py_GT:
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 case Py_GE:
3243 if (len_self >= len_other)
3244 ok = all_contained_in(other, self);
3245 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003247 }
3248 if (ok < 0)
3249 return NULL;
3250 result = ok ? Py_True : Py_False;
3251 Py_INCREF(result);
3252 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003253}
3254
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003255static PyObject *
3256dictview_repr(dictviewobject *dv)
3257{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003258 PyObject *seq;
3259 PyObject *result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003261 seq = PySequence_List((PyObject *)dv);
3262 if (seq == NULL)
3263 return NULL;
3264
3265 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
3266 Py_DECREF(seq);
3267 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003268}
3269
Guido van Rossum3ac67412007-02-10 18:55:06 +00003270/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003271
3272static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003273dictkeys_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003274{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003275 if (dv->dv_dict == NULL) {
3276 Py_RETURN_NONE;
3277 }
3278 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003279}
3280
3281static int
3282dictkeys_contains(dictviewobject *dv, PyObject *obj)
3283{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003284 if (dv->dv_dict == NULL)
3285 return 0;
3286 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003287}
3288
Guido van Rossum83825ac2007-02-10 04:54:19 +00003289static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003290 (lenfunc)dictview_len, /* sq_length */
3291 0, /* sq_concat */
3292 0, /* sq_repeat */
3293 0, /* sq_item */
3294 0, /* sq_slice */
3295 0, /* sq_ass_item */
3296 0, /* sq_ass_slice */
3297 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003298};
3299
Guido van Rossum523259b2007-08-24 23:41:22 +00003300static PyObject*
3301dictviews_sub(PyObject* self, PyObject *other)
3302{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003303 PyObject *result = PySet_New(self);
3304 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003305 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003307 if (result == NULL)
3308 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003309
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003310 tmp = _PyObject_CallMethodId(result, &PyId_difference_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003311 if (tmp == NULL) {
3312 Py_DECREF(result);
3313 return NULL;
3314 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003316 Py_DECREF(tmp);
3317 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003318}
3319
3320static PyObject*
3321dictviews_and(PyObject* self, PyObject *other)
3322{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003323 PyObject *result = PySet_New(self);
3324 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003325 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003327 if (result == NULL)
3328 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003329
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003330 tmp = _PyObject_CallMethodId(result, &PyId_intersection_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003331 if (tmp == NULL) {
3332 Py_DECREF(result);
3333 return NULL;
3334 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003336 Py_DECREF(tmp);
3337 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003338}
3339
3340static PyObject*
3341dictviews_or(PyObject* self, PyObject *other)
3342{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003343 PyObject *result = PySet_New(self);
3344 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003345 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003347 if (result == NULL)
3348 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003349
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003350 tmp = _PyObject_CallMethodId(result, &PyId_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003351 if (tmp == NULL) {
3352 Py_DECREF(result);
3353 return NULL;
3354 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003356 Py_DECREF(tmp);
3357 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003358}
3359
3360static PyObject*
3361dictviews_xor(PyObject* self, PyObject *other)
3362{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003363 PyObject *result = PySet_New(self);
3364 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003365 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003367 if (result == NULL)
3368 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003369
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003370 tmp = _PyObject_CallMethodId(result, &PyId_symmetric_difference_update, "O",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003371 other);
3372 if (tmp == NULL) {
3373 Py_DECREF(result);
3374 return NULL;
3375 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003377 Py_DECREF(tmp);
3378 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003379}
3380
3381static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003382 0, /*nb_add*/
3383 (binaryfunc)dictviews_sub, /*nb_subtract*/
3384 0, /*nb_multiply*/
3385 0, /*nb_remainder*/
3386 0, /*nb_divmod*/
3387 0, /*nb_power*/
3388 0, /*nb_negative*/
3389 0, /*nb_positive*/
3390 0, /*nb_absolute*/
3391 0, /*nb_bool*/
3392 0, /*nb_invert*/
3393 0, /*nb_lshift*/
3394 0, /*nb_rshift*/
3395 (binaryfunc)dictviews_and, /*nb_and*/
3396 (binaryfunc)dictviews_xor, /*nb_xor*/
3397 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00003398};
3399
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003400static PyObject*
3401dictviews_isdisjoint(PyObject *self, PyObject *other)
3402{
3403 PyObject *it;
3404 PyObject *item = NULL;
3405
3406 if (self == other) {
3407 if (dictview_len((dictviewobject *)self) == 0)
3408 Py_RETURN_TRUE;
3409 else
3410 Py_RETURN_FALSE;
3411 }
3412
3413 /* Iterate over the shorter object (only if other is a set,
3414 * because PySequence_Contains may be expensive otherwise): */
3415 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
3416 Py_ssize_t len_self = dictview_len((dictviewobject *)self);
3417 Py_ssize_t len_other = PyObject_Size(other);
3418 if (len_other == -1)
3419 return NULL;
3420
3421 if ((len_other > len_self)) {
3422 PyObject *tmp = other;
3423 other = self;
3424 self = tmp;
3425 }
3426 }
3427
3428 it = PyObject_GetIter(other);
3429 if (it == NULL)
3430 return NULL;
3431
3432 while ((item = PyIter_Next(it)) != NULL) {
3433 int contains = PySequence_Contains(self, item);
3434 Py_DECREF(item);
3435 if (contains == -1) {
3436 Py_DECREF(it);
3437 return NULL;
3438 }
3439
3440 if (contains) {
3441 Py_DECREF(it);
3442 Py_RETURN_FALSE;
3443 }
3444 }
3445 Py_DECREF(it);
3446 if (PyErr_Occurred())
3447 return NULL; /* PyIter_Next raised an exception. */
3448 Py_RETURN_TRUE;
3449}
3450
3451PyDoc_STRVAR(isdisjoint_doc,
3452"Return True if the view and the given iterable have a null intersection.");
3453
Guido van Rossumb90c8482007-02-10 01:11:45 +00003454static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003455 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3456 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003457 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003458};
3459
3460PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003461 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3462 "dict_keys", /* tp_name */
3463 sizeof(dictviewobject), /* tp_basicsize */
3464 0, /* tp_itemsize */
3465 /* methods */
3466 (destructor)dictview_dealloc, /* tp_dealloc */
3467 0, /* tp_print */
3468 0, /* tp_getattr */
3469 0, /* tp_setattr */
3470 0, /* tp_reserved */
3471 (reprfunc)dictview_repr, /* tp_repr */
3472 &dictviews_as_number, /* tp_as_number */
3473 &dictkeys_as_sequence, /* tp_as_sequence */
3474 0, /* tp_as_mapping */
3475 0, /* tp_hash */
3476 0, /* tp_call */
3477 0, /* tp_str */
3478 PyObject_GenericGetAttr, /* tp_getattro */
3479 0, /* tp_setattro */
3480 0, /* tp_as_buffer */
3481 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3482 0, /* tp_doc */
3483 (traverseproc)dictview_traverse, /* tp_traverse */
3484 0, /* tp_clear */
3485 dictview_richcompare, /* tp_richcompare */
3486 0, /* tp_weaklistoffset */
3487 (getiterfunc)dictkeys_iter, /* tp_iter */
3488 0, /* tp_iternext */
3489 dictkeys_methods, /* tp_methods */
3490 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003491};
3492
3493static PyObject *
3494dictkeys_new(PyObject *dict)
3495{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003496 return dictview_new(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003497}
3498
Guido van Rossum3ac67412007-02-10 18:55:06 +00003499/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003500
3501static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003502dictitems_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003503{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003504 if (dv->dv_dict == NULL) {
3505 Py_RETURN_NONE;
3506 }
3507 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003508}
3509
3510static int
3511dictitems_contains(dictviewobject *dv, PyObject *obj)
3512{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003513 PyObject *key, *value, *found;
3514 if (dv->dv_dict == NULL)
3515 return 0;
3516 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
3517 return 0;
3518 key = PyTuple_GET_ITEM(obj, 0);
3519 value = PyTuple_GET_ITEM(obj, 1);
3520 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
3521 if (found == NULL) {
3522 if (PyErr_Occurred())
3523 return -1;
3524 return 0;
3525 }
3526 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003527}
3528
Guido van Rossum83825ac2007-02-10 04:54:19 +00003529static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003530 (lenfunc)dictview_len, /* sq_length */
3531 0, /* sq_concat */
3532 0, /* sq_repeat */
3533 0, /* sq_item */
3534 0, /* sq_slice */
3535 0, /* sq_ass_item */
3536 0, /* sq_ass_slice */
3537 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003538};
3539
Guido van Rossumb90c8482007-02-10 01:11:45 +00003540static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003541 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3542 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003543 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003544};
3545
3546PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003547 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3548 "dict_items", /* tp_name */
3549 sizeof(dictviewobject), /* tp_basicsize */
3550 0, /* tp_itemsize */
3551 /* methods */
3552 (destructor)dictview_dealloc, /* tp_dealloc */
3553 0, /* tp_print */
3554 0, /* tp_getattr */
3555 0, /* tp_setattr */
3556 0, /* tp_reserved */
3557 (reprfunc)dictview_repr, /* tp_repr */
3558 &dictviews_as_number, /* tp_as_number */
3559 &dictitems_as_sequence, /* tp_as_sequence */
3560 0, /* tp_as_mapping */
3561 0, /* tp_hash */
3562 0, /* tp_call */
3563 0, /* tp_str */
3564 PyObject_GenericGetAttr, /* tp_getattro */
3565 0, /* tp_setattro */
3566 0, /* tp_as_buffer */
3567 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3568 0, /* tp_doc */
3569 (traverseproc)dictview_traverse, /* tp_traverse */
3570 0, /* tp_clear */
3571 dictview_richcompare, /* tp_richcompare */
3572 0, /* tp_weaklistoffset */
3573 (getiterfunc)dictitems_iter, /* tp_iter */
3574 0, /* tp_iternext */
3575 dictitems_methods, /* tp_methods */
3576 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003577};
3578
3579static PyObject *
3580dictitems_new(PyObject *dict)
3581{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003582 return dictview_new(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003583}
3584
Guido van Rossum3ac67412007-02-10 18:55:06 +00003585/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003586
3587static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003588dictvalues_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003589{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003590 if (dv->dv_dict == NULL) {
3591 Py_RETURN_NONE;
3592 }
3593 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003594}
3595
Guido van Rossum83825ac2007-02-10 04:54:19 +00003596static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003597 (lenfunc)dictview_len, /* sq_length */
3598 0, /* sq_concat */
3599 0, /* sq_repeat */
3600 0, /* sq_item */
3601 0, /* sq_slice */
3602 0, /* sq_ass_item */
3603 0, /* sq_ass_slice */
3604 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003605};
3606
Guido van Rossumb90c8482007-02-10 01:11:45 +00003607static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003608 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003609};
3610
3611PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003612 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3613 "dict_values", /* tp_name */
3614 sizeof(dictviewobject), /* tp_basicsize */
3615 0, /* tp_itemsize */
3616 /* methods */
3617 (destructor)dictview_dealloc, /* tp_dealloc */
3618 0, /* tp_print */
3619 0, /* tp_getattr */
3620 0, /* tp_setattr */
3621 0, /* tp_reserved */
3622 (reprfunc)dictview_repr, /* tp_repr */
3623 0, /* tp_as_number */
3624 &dictvalues_as_sequence, /* tp_as_sequence */
3625 0, /* tp_as_mapping */
3626 0, /* tp_hash */
3627 0, /* tp_call */
3628 0, /* tp_str */
3629 PyObject_GenericGetAttr, /* tp_getattro */
3630 0, /* tp_setattro */
3631 0, /* tp_as_buffer */
3632 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3633 0, /* tp_doc */
3634 (traverseproc)dictview_traverse, /* tp_traverse */
3635 0, /* tp_clear */
3636 0, /* tp_richcompare */
3637 0, /* tp_weaklistoffset */
3638 (getiterfunc)dictvalues_iter, /* tp_iter */
3639 0, /* tp_iternext */
3640 dictvalues_methods, /* tp_methods */
3641 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003642};
3643
3644static PyObject *
3645dictvalues_new(PyObject *dict)
3646{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003647 return dictview_new(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003648}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003649
3650/* Returns NULL if cannot allocate a new PyDictKeysObject,
3651 but does not set an error */
3652PyDictKeysObject *
3653_PyDict_NewKeysForClass(void)
3654{
3655 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE_SPLIT);
3656 if (keys == NULL)
3657 PyErr_Clear();
3658 else
3659 keys->dk_lookup = lookdict_split;
3660 return keys;
3661}
3662
3663#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
3664
3665PyObject *
3666PyObject_GenericGetDict(PyObject *obj, void *context)
3667{
3668 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
3669 if (dictptr == NULL) {
3670 PyErr_SetString(PyExc_AttributeError,
3671 "This object has no __dict__");
3672 return NULL;
3673 }
3674 dict = *dictptr;
3675 if (dict == NULL) {
3676 PyTypeObject *tp = Py_TYPE(obj);
3677 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
3678 DK_INCREF(CACHED_KEYS(tp));
3679 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
3680 }
3681 else {
3682 *dictptr = dict = PyDict_New();
3683 }
3684 }
3685 Py_XINCREF(dict);
3686 return dict;
3687}
3688
3689int
3690_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
3691 PyObject *key, PyObject *value)
3692{
3693 PyObject *dict;
3694 int res;
3695 PyDictKeysObject *cached;
3696
3697 assert(dictptr != NULL);
3698 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
3699 assert(dictptr != NULL);
3700 dict = *dictptr;
3701 if (dict == NULL) {
3702 DK_INCREF(cached);
3703 dict = new_dict_with_shared_keys(cached);
3704 if (dict == NULL)
3705 return -1;
3706 *dictptr = dict;
3707 }
3708 if (value == NULL) {
3709 res = PyDict_DelItem(dict, key);
3710 if (cached != ((PyDictObject *)dict)->ma_keys) {
3711 CACHED_KEYS(tp) = NULL;
3712 DK_DECREF(cached);
3713 }
3714 } else {
3715 res = PyDict_SetItem(dict, key, value);
3716 if (cached != ((PyDictObject *)dict)->ma_keys) {
3717 /* Either update tp->ht_cached_keys or delete it */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04003718 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003719 CACHED_KEYS(tp) = make_keys_shared(dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003720 } else {
3721 CACHED_KEYS(tp) = NULL;
3722 }
3723 DK_DECREF(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04003724 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
3725 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003726 }
3727 }
3728 } else {
3729 dict = *dictptr;
3730 if (dict == NULL) {
3731 dict = PyDict_New();
3732 if (dict == NULL)
3733 return -1;
3734 *dictptr = dict;
3735 }
3736 if (value == NULL) {
3737 res = PyDict_DelItem(dict, key);
3738 } else {
3739 res = PyDict_SetItem(dict, key, value);
3740 }
3741 }
3742 return res;
3743}
3744
3745void
3746_PyDictKeys_DecRef(PyDictKeysObject *keys)
3747{
3748 DK_DECREF(keys);
3749}
3750
3751
3752/* ARGSUSED */
3753static PyObject *
3754dummy_repr(PyObject *op)
3755{
3756 return PyUnicode_FromString("<dummy key>");
3757}
3758
3759/* ARGUSED */
3760static void
3761dummy_dealloc(PyObject* ignore)
3762{
3763 /* This should never get called, but we also don't want to SEGV if
3764 * we accidentally decref dummy-key out of existence.
3765 */
3766 Py_FatalError("deallocating <dummy key>");
3767}
3768
3769static PyTypeObject PyDictDummy_Type = {
3770 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3771 "<dummy key> type",
3772 0,
3773 0,
3774 dummy_dealloc, /*tp_dealloc*/ /*never called*/
3775 0, /*tp_print*/
3776 0, /*tp_getattr*/
3777 0, /*tp_setattr*/
3778 0, /*tp_reserved*/
3779 dummy_repr, /*tp_repr*/
3780 0, /*tp_as_number*/
3781 0, /*tp_as_sequence*/
3782 0, /*tp_as_mapping*/
3783 0, /*tp_hash */
3784 0, /*tp_call */
3785 0, /*tp_str */
3786 0, /*tp_getattro */
3787 0, /*tp_setattro */
3788 0, /*tp_as_buffer */
3789 Py_TPFLAGS_DEFAULT, /*tp_flags */
3790};
3791
3792static PyObject _dummy_struct = {
3793 _PyObject_EXTRA_INIT
3794 2, &PyDictDummy_Type
3795};
3796