blob: d08a40ef48780b2a36de77c5fe72425d845f1ef1 [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
258void
259PyDict_Fini(void)
260{
261 PyDict_ClearFreeList();
Christian Heimes77c02eb2008-02-09 02:18:51 +0000262}
263
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400264#define DK_INCREF(dk) (++(dk)->dk_refcnt)
265#define DK_DECREF(dk) if ((--(dk)->dk_refcnt) == 0) free_keys_object(dk)
266#define DK_SIZE(dk) ((dk)->dk_size)
267#define DK_MASK(dk) (((dk)->dk_size)-1)
268#define IS_POWER_OF_2(x) (((x) & (x-1)) == 0)
269
270/* USABLE_FRACTION must obey the following:
271 * (0 < USABLE_FRACTION(n) < n) for all n >= 2
272 *
273 * USABLE_FRACTION should be very quick to calculate.
274 * Fractions around 5/8 to 2/3 seem to work well in practice.
275 */
276
277/* Use (2n+1)/3 rather than 2n+3 because: it makes no difference for
278 * combined tables (the two fractions round to the same number n < ),
279 * but 2*4/3 is 2 whereas (2*4+1)/3 is 3 which potentially saves quite
280 * a lot of space for small, split tables */
281#define USABLE_FRACTION(n) ((((n) << 1)+1)/3)
282
283/* Alternative fraction that is otherwise close enough to (2n+1)/3 to make
284 * little difference. 8 * 2/3 == 8 * 5/8 == 5. 16 * 2/3 == 16 * 5/8 == 10.
285 * 32 * 2/3 = 21, 32 * 5/8 = 20.
286 * Its advantage is that it is faster to compute on machines with slow division.
287 * #define USABLE_FRACTION(n) (((n) >> 1) + ((n) >> 2) - ((n) >> 3))
288*/
289
290
291#define ENSURE_ALLOWS_DELETIONS(d) \
292 if ((d)->ma_keys->dk_lookup == lookdict_unicode_nodummy) { \
293 (d)->ma_keys->dk_lookup = lookdict_unicode; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000294 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400295
296/* This immutable, empty PyDictKeysObject is used for PyDict_Clear()
297 * (which cannot fail and thus can do no allocation).
298 */
299static PyDictKeysObject empty_keys_struct = {
300 2, /* dk_refcnt 1 for this struct, 1 for dummy_struct */
301 1, /* dk_size */
302 lookdict_split, /* dk_lookup */
303 0, /* dk_usable (immutable) */
304 {
305 { 0, 0, 0 } /* dk_entries (empty) */
306 }
307};
308
309static PyObject *empty_values[1] = { NULL };
310
311#define Py_EMPTY_KEYS &empty_keys_struct
312
313static PyDictKeysObject *new_keys_object(Py_ssize_t size)
314{
315 PyDictKeysObject *dk;
316 Py_ssize_t i;
317 PyDictKeyEntry *ep0;
318
319 assert(size >= PyDict_MINSIZE_SPLIT);
320 assert(IS_POWER_OF_2(size));
321 dk = PyMem_MALLOC(sizeof(PyDictKeysObject) +
322 sizeof(PyDictKeyEntry) * (size-1));
323 if (dk == NULL) {
324 PyErr_NoMemory();
325 return NULL;
326 }
327 dk->dk_refcnt = 1;
328 dk->dk_size = size;
329 dk->dk_usable = USABLE_FRACTION(size);
330 ep0 = &dk->dk_entries[0];
331 /* Hash value of slot 0 is used by popitem, so it must be initialized */
332 ep0->me_hash = 0;
333 for (i = 0; i < size; i++) {
334 ep0[i].me_key = NULL;
335 ep0[i].me_value = NULL;
336 }
337 dk->dk_lookup = lookdict_unicode_nodummy;
338 return dk;
339}
340
341static void
342free_keys_object(PyDictKeysObject *keys)
343{
344 PyDictKeyEntry *entries = &keys->dk_entries[0];
345 Py_ssize_t i, n;
346 for (i = 0, n = DK_SIZE(keys); i < n; i++) {
347 Py_XDECREF(entries[i].me_key);
348 Py_XDECREF(entries[i].me_value);
349 }
350 PyMem_FREE(keys);
351}
352
353#define new_values(size) PyMem_NEW(PyObject *, size)
354
355#define free_values(values) PyMem_FREE(values)
356
357/* Consumes a reference to the keys object */
358static PyObject *
359new_dict(PyDictKeysObject *keys, PyObject **values)
360{
361 PyDictObject *mp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 if (numfree) {
363 mp = free_list[--numfree];
364 assert (mp != NULL);
365 assert (Py_TYPE(mp) == &PyDict_Type);
366 _Py_NewReference((PyObject *)mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400368 else {
369 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
370 if (mp == NULL) {
371 DK_DECREF(keys);
372 free_values(values);
373 return NULL;
374 }
375 }
376 mp->ma_keys = keys;
377 mp->ma_values = values;
378 mp->ma_used = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000380}
381
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400382/* Consumes a reference to the keys object */
383static PyObject *
384new_dict_with_shared_keys(PyDictKeysObject *keys)
385{
386 PyObject **values;
387 Py_ssize_t i, size;
388
389 size = DK_SIZE(keys);
390 values = new_values(size);
391 if (values == NULL) {
392 DK_DECREF(keys);
393 return PyErr_NoMemory();
394 }
395 for (i = 0; i < size; i++) {
396 values[i] = NULL;
397 }
398 return new_dict(keys, values);
399}
400
401PyObject *
402PyDict_New(void)
403{
404 return new_dict(new_keys_object(PyDict_MINSIZE_COMBINED), NULL);
405}
406
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000407/*
408The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000409This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000410Open addressing is preferred over chaining since the link overhead for
411chaining would be substantial (100% with typical malloc overhead).
412
Tim Peterseb28ef22001-06-02 05:27:19 +0000413The initial probe index is computed as hash mod the table size. Subsequent
414probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000415
416All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000417
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000418The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000419contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000420Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000421
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000422lookdict() is general-purpose, and may return NULL if (and only if) a
423comparison raises an exception (this was new in Python 2.5).
Guido van Rossum89d8c602007-09-18 17:26:56 +0000424lookdict_unicode() below is specialized to string keys, comparison of which can
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400425never raise an exception; that function can never return NULL.
426lookdict_unicode_nodummy is further specialized for string keys that cannot be
427the <dummy> value.
428For both, when the key isn't found a PyDictEntry* is returned
429where the key would have been found, *value_addr points to the matching value
430slot.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000431*/
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400432static PyDictKeyEntry *
433lookdict(PyDictObject *mp, PyObject *key,
434 Py_hash_t hash, PyObject ***value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000435{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 register size_t i;
437 register size_t perturb;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400438 register PyDictKeyEntry *freeslot;
439 register size_t mask = DK_MASK(mp->ma_keys);
440 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
441 register PyDictKeyEntry *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 register int cmp;
443 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 i = (size_t)hash & mask;
446 ep = &ep0[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400447 if (ep->me_key == NULL || ep->me_key == key) {
448 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400450 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 if (ep->me_key == dummy)
452 freeslot = ep;
453 else {
454 if (ep->me_hash == hash) {
455 startkey = ep->me_key;
456 Py_INCREF(startkey);
457 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
458 Py_DECREF(startkey);
459 if (cmp < 0)
460 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400461 if (ep0 == mp->ma_keys->dk_entries && ep->me_key == startkey) {
462 if (cmp > 0) {
463 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400465 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 }
467 else {
Victor Stinner198b2912012-03-06 01:03:13 +0100468 PyErr_SetString(PyExc_RuntimeError,
469 "dictionary changed size during lookup");
470 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 }
472 }
473 freeslot = NULL;
474 }
Tim Peters15d49292001-05-27 07:39:22 +0000475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 /* In the loop, me_key == dummy is by far (factor of 100s) the
477 least likely outcome, so test for that last. */
478 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
479 i = (i << 2) + i + perturb + 1;
480 ep = &ep0[i & mask];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400481 if (ep->me_key == NULL) {
482 if (freeslot == NULL) {
483 *value_addr = &ep->me_value;
484 return ep;
485 } else {
486 *value_addr = &freeslot->me_value;
487 return freeslot;
488 }
489 }
490 if (ep->me_key == key) {
491 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400493 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 if (ep->me_hash == hash && ep->me_key != dummy) {
495 startkey = ep->me_key;
496 Py_INCREF(startkey);
497 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
498 Py_DECREF(startkey);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400499 if (cmp < 0) {
500 *value_addr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400502 }
503 if (ep0 == mp->ma_keys->dk_entries && ep->me_key == startkey) {
504 if (cmp > 0) {
505 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400507 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 }
509 else {
Victor Stinner198b2912012-03-06 01:03:13 +0100510 PyErr_SetString(PyExc_RuntimeError,
511 "dictionary changed size during lookup");
512 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 }
514 }
515 else if (ep->me_key == dummy && freeslot == NULL)
516 freeslot = ep;
517 }
518 assert(0); /* NOT REACHED */
519 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000520}
521
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400522/* Specialized version for string-only keys */
523static PyDictKeyEntry *
524lookdict_unicode(PyDictObject *mp, PyObject *key,
525 Py_hash_t hash, PyObject ***value_addr)
Fred Drake1bff34a2000-08-31 19:31:38 +0000526{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 register size_t i;
528 register size_t perturb;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400529 register PyDictKeyEntry *freeslot;
530 register size_t mask = DK_MASK(mp->ma_keys);
531 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
532 register PyDictKeyEntry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 /* Make sure this function doesn't have to handle non-unicode keys,
535 including subclasses of str; e.g., one reason to subclass
536 unicodes is to override __eq__, and for speed we don't cater to
537 that here. */
538 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400539 mp->ma_keys->dk_lookup = lookdict;
540 return lookdict(mp, key, hash, value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 }
Mark Dickinson57e683e2011-09-24 18:18:40 +0100542 i = (size_t)hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 ep = &ep0[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400544 if (ep->me_key == NULL || ep->me_key == key) {
545 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400547 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 if (ep->me_key == dummy)
549 freeslot = ep;
550 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400551 if (ep->me_hash == hash && unicode_eq(ep->me_key, key)) {
552 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400554 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 freeslot = NULL;
556 }
Tim Peters15d49292001-05-27 07:39:22 +0000557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 /* In the loop, me_key == dummy is by far (factor of 100s) the
559 least likely outcome, so test for that last. */
560 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
561 i = (i << 2) + i + perturb + 1;
562 ep = &ep0[i & mask];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400563 if (ep->me_key == NULL) {
564 if (freeslot == NULL) {
565 *value_addr = &ep->me_value;
566 return ep;
567 } else {
568 *value_addr = &freeslot->me_value;
569 return freeslot;
570 }
571 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 if (ep->me_key == key
573 || (ep->me_hash == hash
574 && ep->me_key != dummy
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400575 && unicode_eq(ep->me_key, key))) {
576 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400578 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 if (ep->me_key == dummy && freeslot == NULL)
580 freeslot = ep;
581 }
582 assert(0); /* NOT REACHED */
583 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000584}
585
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400586/* Faster version of lookdict_unicode when it is known that no <dummy> keys
587 * will be present. */
588static PyDictKeyEntry *
589lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
590 Py_hash_t hash, PyObject ***value_addr)
591{
592 register size_t i;
593 register size_t perturb;
594 register size_t mask = DK_MASK(mp->ma_keys);
595 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
596 register PyDictKeyEntry *ep;
597
598 /* Make sure this function doesn't have to handle non-unicode keys,
599 including subclasses of str; e.g., one reason to subclass
600 unicodes is to override __eq__, and for speed we don't cater to
601 that here. */
602 if (!PyUnicode_CheckExact(key)) {
603 mp->ma_keys->dk_lookup = lookdict;
604 return lookdict(mp, key, hash, value_addr);
605 }
606 i = (size_t)hash & mask;
607 ep = &ep0[i];
608 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
609 if (ep->me_key == NULL || ep->me_key == key ||
610 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
611 *value_addr = &ep->me_value;
612 return ep;
613 }
614 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
615 i = (i << 2) + i + perturb + 1;
616 ep = &ep0[i & mask];
617 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
618 if (ep->me_key == NULL || ep->me_key == key ||
619 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
620 *value_addr = &ep->me_value;
621 return ep;
622 }
623 }
624 assert(0); /* NOT REACHED */
625 return 0;
626}
627
628/* Version of lookdict for split tables.
629 * All split tables and only split tables use this lookup function.
630 * Split tables only contain unicode keys and no dummy keys,
631 * so algorithm is the same as lookdict_unicode_nodummy.
632 */
633static PyDictKeyEntry *
634lookdict_split(PyDictObject *mp, PyObject *key,
635 Py_hash_t hash, PyObject ***value_addr)
636{
637 register size_t i;
638 register size_t perturb;
639 register size_t mask = DK_MASK(mp->ma_keys);
640 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
641 register PyDictKeyEntry *ep;
642
643 if (!PyUnicode_CheckExact(key)) {
Benjamin Petersondb780d02012-04-23 13:44:32 -0400644 ep = lookdict(mp, key, hash, value_addr);
645 /* lookdict expects a combined-table, so fix value_addr */
646 i = ep - ep0;
647 *value_addr = &mp->ma_values[i];
648 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400649 }
650 i = (size_t)hash & mask;
651 ep = &ep0[i];
652 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
653 if (ep->me_key == NULL || ep->me_key == key ||
654 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
655 *value_addr = &mp->ma_values[i];
656 return ep;
657 }
658 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
659 i = (i << 2) + i + perturb + 1;
660 ep = &ep0[i & mask];
661 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
662 if (ep->me_key == NULL || ep->me_key == key ||
663 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
664 *value_addr = &mp->ma_values[i & mask];
665 return ep;
666 }
667 }
668 assert(0); /* NOT REACHED */
669 return 0;
670}
671
Benjamin Petersonfb886362010-04-24 18:21:17 +0000672int
673_PyDict_HasOnlyStringKeys(PyObject *dict)
674{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 Py_ssize_t pos = 0;
676 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000677 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400679 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 return 1;
681 while (PyDict_Next(dict, &pos, &key, &value))
682 if (!PyUnicode_Check(key))
683 return 0;
684 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000685}
686
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000687#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 do { \
689 if (!_PyObject_GC_IS_TRACKED(mp)) { \
690 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
691 _PyObject_GC_MAY_BE_TRACKED(value)) { \
692 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 } \
694 } \
695 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000696
697void
698_PyDict_MaybeUntrack(PyObject *op)
699{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 PyDictObject *mp;
701 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400702 Py_ssize_t i, size;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
705 return;
706
707 mp = (PyDictObject *) op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400708 size = DK_SIZE(mp->ma_keys);
709 if (_PyDict_HasSplitTable(mp)) {
710 for (i = 0; i < size; i++) {
711 if ((value = mp->ma_values[i]) == NULL)
712 continue;
713 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
714 assert(!_PyObject_GC_MAY_BE_TRACKED(
715 mp->ma_keys->dk_entries[i].me_key));
716 return;
717 }
718 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400720 else {
721 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
722 for (i = 0; i < size; i++) {
723 if ((value = ep0[i].me_value) == NULL)
724 continue;
725 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
726 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
727 return;
728 }
729 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000731}
732
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400733/* Internal function to find slot for an item from its hash
734 * when it is known that the key is not present in the dict.
735 */
736static PyDictKeyEntry *
737find_empty_slot(PyDictObject *mp, PyObject *key, Py_hash_t hash,
738 PyObject ***value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000739{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400740 size_t i;
741 size_t perturb;
742 size_t mask = DK_MASK(mp->ma_keys);
743 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
744 PyDictKeyEntry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000745
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400746 assert(key != NULL);
747 if (!PyUnicode_CheckExact(key))
748 mp->ma_keys->dk_lookup = lookdict;
749 i = hash & mask;
750 ep = &ep0[i];
751 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
752 i = (i << 2) + i + perturb + 1;
753 ep = &ep0[i & mask];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400755 assert(ep->me_value == NULL);
756 if (mp->ma_values)
757 *value_addr = &mp->ma_values[i & mask];
758 else
759 *value_addr = &ep->me_value;
760 return ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000761}
762
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400763static int
764insertion_resize(PyDictObject *mp)
765{
766 /*
767 * Double the size of the dict,
768 * Previous versions quadrupled size, but doing so may result in excessive
769 * memory use. Doubling keeps the number of resizes low without wasting
770 * too much memory.
771 */
772 return dictresize(mp, 2 * mp->ma_used);
773}
Antoine Pitroue965d972012-02-27 00:45:12 +0100774
775/*
776Internal routine to insert a new item into the table.
777Used both by the internal resize routine and by the public insert routine.
778Eats a reference to key and one to value.
779Returns -1 if an error occurred, or 0 on success.
780*/
781static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400782insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +0100783{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400784 PyObject *old_value;
785 PyObject **value_addr;
786 PyDictKeyEntry *ep;
787 assert(key != dummy);
Antoine Pitroue965d972012-02-27 00:45:12 +0100788
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400789 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
790 if (insertion_resize(mp) < 0)
791 return -1;
792 }
793
794 ep = mp->ma_keys->dk_lookup(mp, key, hash, &value_addr);
Antoine Pitroue965d972012-02-27 00:45:12 +0100795 if (ep == NULL) {
796 Py_DECREF(key);
797 Py_DECREF(value);
798 return -1;
799 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400800 MAINTAIN_TRACKING(mp, key, value);
801 old_value = *value_addr;
802 if (old_value != NULL) {
803 assert(ep->me_key != NULL && ep->me_key != dummy);
804 *value_addr = value;
805 Py_DECREF(old_value); /* which **CAN** re-enter */
806 Py_DECREF(key);
807 }
808 else {
809 if (ep->me_key == NULL) {
810 if (mp->ma_keys->dk_usable <= 0) {
811 /* Need to resize. */
812 if (insertion_resize(mp) < 0) {
813 Py_DECREF(key);
814 Py_DECREF(value);
815 return -1;
816 }
817 ep = find_empty_slot(mp, key, hash, &value_addr);
818 }
819 mp->ma_keys->dk_usable--;
820 assert(mp->ma_keys->dk_usable >= 0);
821 ep->me_key = key;
822 ep->me_hash = hash;
823 }
824 else {
825 if (ep->me_key == dummy) {
826 ep->me_key = key;
827 ep->me_hash = hash;
828 Py_DECREF(dummy);
829 } else {
830 Py_DECREF(key);
831 assert(_PyDict_HasSplitTable(mp));
832 }
833 }
834 mp->ma_used++;
835 *value_addr = value;
836 }
837 assert(ep->me_key != NULL && ep->me_key != dummy);
838 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
839 return 0;
Antoine Pitroue965d972012-02-27 00:45:12 +0100840}
841
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000842/*
843Internal routine used by dictresize() to insert an item which is
844known to be absent from the dict. This routine also assumes that
845the dict contains no deleted entries. Besides the performance benefit,
846using insertdict() in dictresize() is dangerous (SF bug #1456209).
847Note that no refcounts are changed by this routine; if needed, the caller
848is responsible for incref'ing `key` and `value`.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400849Neither mp->ma_used nor k->dk_usable are modified by this routine; the caller
850must set them correctly
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000851*/
852static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400853insertdict_clean(PyDictObject *mp, PyObject *key, Py_hash_t hash,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 PyObject *value)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000855{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400856 size_t i;
857 size_t perturb;
858 PyDictKeysObject *k = mp->ma_keys;
859 size_t mask = (size_t)DK_SIZE(k)-1;
860 PyDictKeyEntry *ep0 = &k->dk_entries[0];
861 PyDictKeyEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000862
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400863 assert(k->dk_lookup != NULL);
864 assert(value != NULL);
865 assert(key != NULL);
866 assert(key != dummy);
867 assert(PyUnicode_CheckExact(key) || k->dk_lookup == lookdict);
868 i = hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 ep = &ep0[i];
870 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
871 i = (i << 2) + i + perturb + 1;
872 ep = &ep0[i & mask];
873 }
874 assert(ep->me_value == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 ep->me_key = key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000876 ep->me_hash = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000878}
879
880/*
881Restructure the table by allocating a new table and reinserting all
882items again. When entries have been deleted, the new table may
883actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400884If a table is split (its keys and hashes are shared, its values are not),
885then the values are temporarily copied into the table, it is resized as
886a combined table, then the me_value slots in the old table are NULLed out.
887After resizing a table is always combined,
888but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000889*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000890static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000891dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000892{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 Py_ssize_t newsize;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400894 PyDictKeysObject *oldkeys;
895 PyObject **oldvalues;
896 Py_ssize_t i, oldsize;
Tim Peters91a364d2001-05-19 07:04:38 +0000897
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400898/* Find the smallest table size > minused. */
899 for (newsize = PyDict_MINSIZE_COMBINED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 newsize <= minused && newsize > 0;
901 newsize <<= 1)
902 ;
903 if (newsize <= 0) {
904 PyErr_NoMemory();
905 return -1;
906 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400907 oldkeys = mp->ma_keys;
908 oldvalues = mp->ma_values;
909 /* Allocate a new table. */
910 mp->ma_keys = new_keys_object(newsize);
911 if (mp->ma_keys == NULL) {
912 mp->ma_keys = oldkeys;
913 return -1;
914 }
915 if (oldkeys->dk_lookup == lookdict)
916 mp->ma_keys->dk_lookup = lookdict;
917 oldsize = DK_SIZE(oldkeys);
918 mp->ma_values = NULL;
919 /* If empty then nothing to copy so just return */
920 if (oldsize == 1) {
921 assert(oldkeys == Py_EMPTY_KEYS);
922 DK_DECREF(oldkeys);
923 return 0;
924 }
925 /* Main loop below assumes we can transfer refcount to new keys
926 * and that value is stored in me_value.
927 * Increment ref-counts and copy values here to compensate
928 * This (resizing a split table) should be relatively rare */
929 if (oldvalues != NULL) {
930 for (i = 0; i < oldsize; i++) {
931 if (oldvalues[i] != NULL) {
932 Py_INCREF(oldkeys->dk_entries[i].me_key);
933 oldkeys->dk_entries[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 }
936 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400937 /* Main loop */
938 for (i = 0; i < oldsize; i++) {
939 PyDictKeyEntry *ep = &oldkeys->dk_entries[i];
940 if (ep->me_value != NULL) {
941 assert(ep->me_key != dummy);
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000942 insertdict_clean(mp, ep->me_key, ep->me_hash, ep->me_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400945 mp->ma_keys->dk_usable -= mp->ma_used;
946 if (oldvalues != NULL) {
947 /* NULL out me_value slot in oldkeys, in case it was shared */
948 for (i = 0; i < oldsize; i++)
949 oldkeys->dk_entries[i].me_value = NULL;
950 assert(oldvalues != empty_values);
951 free_values(oldvalues);
952 DK_DECREF(oldkeys);
953 }
954 else {
955 assert(oldkeys->dk_lookup != lookdict_split);
956 if (oldkeys->dk_lookup != lookdict_unicode_nodummy) {
957 PyDictKeyEntry *ep0 = &oldkeys->dk_entries[0];
958 for (i = 0; i < oldsize; i++) {
959 if (ep0[i].me_key == dummy)
960 Py_DECREF(dummy);
961 }
962 }
963 assert(oldkeys->dk_refcnt == 1);
964 PyMem_FREE(oldkeys);
965 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000967}
968
Benjamin Peterson15ee8212012-04-24 14:44:18 -0400969/* Returns NULL if unable to split table.
970 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400971static PyDictKeysObject *
972make_keys_shared(PyObject *op)
973{
974 Py_ssize_t i;
975 Py_ssize_t size;
976 PyDictObject *mp = (PyDictObject *)op;
977
Benjamin Peterson15ee8212012-04-24 14:44:18 -0400978 if (!PyDict_CheckExact(op))
979 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400980 if (!_PyDict_HasSplitTable(mp)) {
981 PyDictKeyEntry *ep0;
982 PyObject **values;
983 assert(mp->ma_keys->dk_refcnt == 1);
984 if (mp->ma_keys->dk_lookup == lookdict) {
985 return NULL;
986 }
987 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
988 /* Remove dummy keys */
989 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
990 return NULL;
991 }
992 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
993 /* Copy values into a new array */
994 ep0 = &mp->ma_keys->dk_entries[0];
995 size = DK_SIZE(mp->ma_keys);
996 values = new_values(size);
997 if (values == NULL) {
998 PyErr_SetString(PyExc_MemoryError,
999 "Not enough memory to allocate new values array");
1000 return NULL;
1001 }
1002 for (i = 0; i < size; i++) {
1003 values[i] = ep0[i].me_value;
1004 ep0[i].me_value = NULL;
1005 }
1006 mp->ma_keys->dk_lookup = lookdict_split;
1007 mp->ma_values = values;
1008 }
1009 DK_INCREF(mp->ma_keys);
1010 return mp->ma_keys;
1011}
Christian Heimes99170a52007-12-19 02:07:34 +00001012
1013PyObject *
1014_PyDict_NewPresized(Py_ssize_t minused)
1015{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001016 Py_ssize_t newsize;
1017 PyDictKeysObject *new_keys;
1018 for (newsize = PyDict_MINSIZE_COMBINED;
1019 newsize <= minused && newsize > 0;
1020 newsize <<= 1)
1021 ;
1022 new_keys = new_keys_object(newsize);
1023 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001025 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001026}
1027
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001028/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1029 * that may occur (originally dicts supported only string keys, and exceptions
1030 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001031 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001032 * (suppressed) occurred while computing the key's hash, or that some error
1033 * (suppressed) occurred when comparing keys in the dict's internal probe
1034 * sequence. A nasty example of the latter is when a Python-coded comparison
1035 * function hits a stack-depth error, which can cause this to return NULL
1036 * even if the key is present.
1037 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001038PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001039PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001040{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001041 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001043 PyDictKeyEntry *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 PyThreadState *tstate;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001045 PyObject **value_addr;
1046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 if (!PyDict_Check(op))
1048 return NULL;
1049 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001050 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 {
1052 hash = PyObject_Hash(key);
1053 if (hash == -1) {
1054 PyErr_Clear();
1055 return NULL;
1056 }
1057 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 /* We can arrive here with a NULL tstate during initialization: try
1060 running "python -Wi" for an example related to string interning.
1061 Let's just hope that no exception occurs then... This must be
1062 _PyThreadState_Current and not PyThreadState_GET() because in debug
1063 mode, the latter complains if tstate is NULL. */
1064 tstate = (PyThreadState*)_Py_atomic_load_relaxed(
1065 &_PyThreadState_Current);
1066 if (tstate != NULL && tstate->curexc_type != NULL) {
1067 /* preserve the existing exception */
1068 PyObject *err_type, *err_value, *err_tb;
1069 PyErr_Fetch(&err_type, &err_value, &err_tb);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001070 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 /* ignore errors */
1072 PyErr_Restore(err_type, err_value, err_tb);
1073 if (ep == NULL)
1074 return NULL;
1075 }
1076 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001077 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 if (ep == NULL) {
1079 PyErr_Clear();
1080 return NULL;
1081 }
1082 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001083 return *value_addr;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001084}
1085
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001086/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1087 This returns NULL *with* an exception set if an exception occurred.
1088 It returns NULL *without* an exception set if the key wasn't present.
1089*/
1090PyObject *
1091PyDict_GetItemWithError(PyObject *op, PyObject *key)
1092{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001093 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 PyDictObject*mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001095 PyDictKeyEntry *ep;
1096 PyObject **value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 if (!PyDict_Check(op)) {
1099 PyErr_BadInternalCall();
1100 return NULL;
1101 }
1102 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001103 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 {
1105 hash = PyObject_Hash(key);
1106 if (hash == -1) {
1107 return NULL;
1108 }
1109 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001110
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001111 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 if (ep == NULL)
1113 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001114 return *value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001115}
1116
Brett Cannonfd074152012-04-14 14:10:13 -04001117PyObject *
1118_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1119{
1120 PyObject *kv;
1121 kv = _PyUnicode_FromId(key); /* borrowed */
1122 if (kv == NULL)
1123 return NULL;
1124 return PyDict_GetItemWithError(dp, kv);
1125}
1126
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001127/* Fast version of global value lookup.
1128 * Lookup in globals, then builtins.
1129 */
1130PyObject *
1131_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001132{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001133 PyObject *x;
1134 if (PyUnicode_CheckExact(key)) {
1135 PyObject **value_addr;
1136 Py_hash_t hash = ((PyASCIIObject *)key)->hash;
1137 if (hash != -1) {
1138 PyDictKeyEntry *e;
1139 e = globals->ma_keys->dk_lookup(globals, key, hash, &value_addr);
1140 if (e == NULL) {
1141 return NULL;
1142 }
1143 x = *value_addr;
1144 if (x != NULL)
1145 return x;
1146 e = builtins->ma_keys->dk_lookup(builtins, key, hash, &value_addr);
1147 if (e == NULL) {
1148 return NULL;
1149 }
1150 x = *value_addr;
1151 return x;
1152 }
Antoine Pitroue965d972012-02-27 00:45:12 +01001153 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001154 x = PyDict_GetItemWithError((PyObject *)globals, key);
1155 if (x != NULL)
1156 return x;
1157 if (PyErr_Occurred())
1158 return NULL;
1159 return PyDict_GetItemWithError((PyObject *)builtins, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001160}
1161
Antoine Pitroue965d972012-02-27 00:45:12 +01001162/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1163 * dictionary if it's merely replacing the value for an existing key.
1164 * This means that it's safe to loop over a dictionary with PyDict_Next()
1165 * and occasionally replace a value -- but you can't insert new keys or
1166 * remove them.
1167 */
1168int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001169PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001170{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001171 PyDictObject *mp;
1172 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001173 if (!PyDict_Check(op)) {
1174 PyErr_BadInternalCall();
1175 return -1;
1176 }
1177 assert(key);
1178 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001179 mp = (PyDictObject *)op;
1180 if (!PyUnicode_CheckExact(key) ||
1181 (hash = ((PyASCIIObject *) key)->hash) == -1)
1182 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001183 hash = PyObject_Hash(key);
1184 if (hash == -1)
1185 return -1;
1186 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001187 Py_INCREF(value);
1188 Py_INCREF(key);
1189
1190 /* insertdict() handles any resizing that might be necessary */
1191 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001192}
1193
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001194int
Tim Peters1f5871e2000-07-04 17:44:48 +00001195PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001196{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001197 PyDictObject *mp;
1198 Py_hash_t hash;
1199 PyDictKeyEntry *ep;
1200 PyObject *old_key, *old_value;
1201 PyObject **value_addr;
Guido van Rossumd7047b31995-01-02 19:07:15 +00001202
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 if (!PyDict_Check(op)) {
1204 PyErr_BadInternalCall();
1205 return -1;
1206 }
1207 assert(key);
1208 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001209 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 hash = PyObject_Hash(key);
1211 if (hash == -1)
1212 return -1;
1213 }
1214 mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001215 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 if (ep == NULL)
1217 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001218 if (*value_addr == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 set_key_error(key);
1220 return -1;
1221 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001222 old_value = *value_addr;
1223 *value_addr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001225 if (!_PyDict_HasSplitTable(mp)) {
1226 ENSURE_ALLOWS_DELETIONS(mp);
1227 old_key = ep->me_key;
1228 Py_INCREF(dummy);
1229 ep->me_key = dummy;
1230 Py_DECREF(old_key);
1231 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 Py_DECREF(old_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001234}
1235
Guido van Rossum25831651993-05-19 14:50:45 +00001236void
Tim Peters1f5871e2000-07-04 17:44:48 +00001237PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001238{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001240 PyDictKeysObject *oldkeys;
1241 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 if (!PyDict_Check(op))
1245 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001246 mp = ((PyDictObject *)op);
1247 oldkeys = mp->ma_keys;
1248 oldvalues = mp->ma_values;
1249 if (oldvalues == empty_values)
1250 return;
1251 /* Empty the dict... */
1252 DK_INCREF(Py_EMPTY_KEYS);
1253 mp->ma_keys = Py_EMPTY_KEYS;
1254 mp->ma_values = empty_values;
1255 mp->ma_used = 0;
1256 /* ...then clear the keys and values */
1257 if (oldvalues != NULL) {
1258 n = DK_SIZE(oldkeys);
1259 for (i = 0; i < n; i++)
1260 Py_CLEAR(oldvalues[i]);
1261 free_values(oldvalues);
1262 DK_DECREF(oldkeys);
1263 }
1264 else {
1265 assert(oldkeys->dk_refcnt == 1);
1266 free_keys_object(oldkeys);
1267 }
1268}
1269
1270/* Returns -1 if no more items (or op is not a dict),
1271 * index of item otherwise. Stores value in pvalue
1272 */
1273Py_LOCAL_INLINE(Py_ssize_t)
1274dict_next(PyObject *op, Py_ssize_t i, PyObject **pvalue)
1275{
1276 Py_ssize_t mask, offset;
1277 PyDictObject *mp;
1278 PyObject **value_ptr;
1279
1280
1281 if (!PyDict_Check(op))
1282 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001284 if (i < 0)
1285 return -1;
1286 if (mp->ma_values) {
1287 value_ptr = &mp->ma_values[i];
1288 offset = sizeof(PyObject *);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001290 else {
1291 value_ptr = &mp->ma_keys->dk_entries[i].me_value;
1292 offset = sizeof(PyDictKeyEntry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001294 mask = DK_MASK(mp->ma_keys);
1295 while (i <= mask && *value_ptr == NULL) {
1296 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1297 i++;
1298 }
1299 if (i > mask)
1300 return -1;
1301 if (pvalue)
1302 *pvalue = *value_ptr;
1303 return i;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001304}
1305
Tim Peters080c88b2003-02-15 03:01:11 +00001306/*
1307 * Iterate over a dict. Use like so:
1308 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001309 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001310 * PyObject *key, *value;
1311 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001312 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +00001313 * Refer to borrowed references in key and value.
1314 * }
1315 *
1316 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001317 * mutates the dict. One exception: it is safe if the loop merely changes
1318 * the values associated with the keys (but doesn't insert new keys or
1319 * delete keys), via PyDict_SetItem().
1320 */
Guido van Rossum25831651993-05-19 14:50:45 +00001321int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001322PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001323{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001324 PyDictObject *mp;
1325 Py_ssize_t i = dict_next(op, *ppos, pvalue);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 if (i < 0)
1327 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001328 mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 *ppos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 if (pkey)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001331 *pkey = mp->ma_keys->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001333}
1334
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001335/* Internal version of PyDict_Next that returns a hash value in addition
1336 * to the key and value.
1337 */
Thomas Wouterscf297e42007-02-23 15:07:44 +00001338int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001339_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1340 PyObject **pvalue, Py_hash_t *phash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00001341{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001342 PyDictObject *mp;
1343 Py_ssize_t i = dict_next(op, *ppos, pvalue);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 if (i < 0)
1345 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001346 mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 *ppos = i+1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001348 *phash = mp->ma_keys->dk_entries[i].me_hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 if (pkey)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001350 *pkey = mp->ma_keys->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 return 1;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001352}
1353
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001354/* Methods */
1355
1356static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001357dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001358{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001359 PyObject **values = mp->ma_values;
1360 PyDictKeysObject *keys = mp->ma_keys;
1361 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 PyObject_GC_UnTrack(mp);
1363 Py_TRASHCAN_SAFE_BEGIN(mp)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001364 if (values != NULL) {
1365 if (values != empty_values) {
1366 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
1367 Py_XDECREF(values[i]);
1368 }
1369 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001371 DK_DECREF(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001373 else {
1374 free_keys_object(keys);
1375 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1377 free_list[numfree++] = mp;
1378 else
1379 Py_TYPE(mp)->tp_free((PyObject *)mp);
1380 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001381}
1382
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001383
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001384static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001385dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001386{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 Py_ssize_t i;
1388 PyObject *s, *temp, *colon = NULL;
1389 PyObject *pieces = NULL, *result = NULL;
1390 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +00001391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 i = Py_ReprEnter((PyObject *)mp);
1393 if (i != 0) {
1394 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
1395 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 if (mp->ma_used == 0) {
1398 result = PyUnicode_FromString("{}");
1399 goto Done;
1400 }
Tim Petersa7259592001-06-16 05:11:17 +00001401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 pieces = PyList_New(0);
1403 if (pieces == NULL)
1404 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 colon = PyUnicode_FromString(": ");
1407 if (colon == NULL)
1408 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 /* Do repr() on each key+value pair, and insert ": " between them.
1411 Note that repr may mutate the dict. */
1412 i = 0;
1413 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
1414 int status;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001415 /* Prevent repr from deleting key or value during key format. */
1416 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 Py_INCREF(value);
1418 s = PyObject_Repr(key);
1419 PyUnicode_Append(&s, colon);
1420 PyUnicode_AppendAndDel(&s, PyObject_Repr(value));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001421 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 Py_DECREF(value);
1423 if (s == NULL)
1424 goto Done;
1425 status = PyList_Append(pieces, s);
1426 Py_DECREF(s); /* append created a new ref */
1427 if (status < 0)
1428 goto Done;
1429 }
Tim Petersa7259592001-06-16 05:11:17 +00001430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001431 /* Add "{}" decorations to the first and last items. */
1432 assert(PyList_GET_SIZE(pieces) > 0);
1433 s = PyUnicode_FromString("{");
1434 if (s == NULL)
1435 goto Done;
1436 temp = PyList_GET_ITEM(pieces, 0);
1437 PyUnicode_AppendAndDel(&s, temp);
1438 PyList_SET_ITEM(pieces, 0, s);
1439 if (s == NULL)
1440 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 s = PyUnicode_FromString("}");
1443 if (s == NULL)
1444 goto Done;
1445 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
1446 PyUnicode_AppendAndDel(&temp, s);
1447 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
1448 if (temp == NULL)
1449 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001451 /* Paste them all together with ", " between. */
1452 s = PyUnicode_FromString(", ");
1453 if (s == NULL)
1454 goto Done;
1455 result = PyUnicode_Join(s, pieces);
1456 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +00001457
1458Done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001459 Py_XDECREF(pieces);
1460 Py_XDECREF(colon);
1461 Py_ReprLeave((PyObject *)mp);
1462 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001463}
1464
Martin v. Löwis18e16552006-02-15 17:27:45 +00001465static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001466dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001467{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001468 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001469}
1470
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001471static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001472dict_subscript(PyDictObject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001473{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 PyObject *v;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001475 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001476 PyDictKeyEntry *ep;
1477 PyObject **value_addr;
1478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001480 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 hash = PyObject_Hash(key);
1482 if (hash == -1)
1483 return NULL;
1484 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001485 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 if (ep == NULL)
1487 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001488 v = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489 if (v == NULL) {
1490 if (!PyDict_CheckExact(mp)) {
1491 /* Look up __missing__ method if we're a subclass. */
1492 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05001493 _Py_IDENTIFIER(__missing__);
1494 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001495 if (missing != NULL) {
1496 res = PyObject_CallFunctionObjArgs(missing,
1497 key, NULL);
1498 Py_DECREF(missing);
1499 return res;
1500 }
1501 else if (PyErr_Occurred())
1502 return NULL;
1503 }
1504 set_key_error(key);
1505 return NULL;
1506 }
1507 else
1508 Py_INCREF(v);
1509 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001510}
1511
1512static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001513dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001514{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 if (w == NULL)
1516 return PyDict_DelItem((PyObject *)mp, v);
1517 else
1518 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001519}
1520
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001521static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001522 (lenfunc)dict_length, /*mp_length*/
1523 (binaryfunc)dict_subscript, /*mp_subscript*/
1524 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001525};
1526
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001527static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001528dict_keys(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001529{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 register PyObject *v;
1531 register Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001532 PyDictKeyEntry *ep;
1533 Py_ssize_t size, n, offset;
1534 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001535
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001536 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537 n = mp->ma_used;
1538 v = PyList_New(n);
1539 if (v == NULL)
1540 return NULL;
1541 if (n != mp->ma_used) {
1542 /* Durnit. The allocations caused the dict to resize.
1543 * Just start over, this shouldn't normally happen.
1544 */
1545 Py_DECREF(v);
1546 goto again;
1547 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001548 ep = &mp->ma_keys->dk_entries[0];
1549 size = DK_SIZE(mp->ma_keys);
1550 if (mp->ma_values) {
1551 value_ptr = mp->ma_values;
1552 offset = sizeof(PyObject *);
1553 }
1554 else {
1555 value_ptr = &ep[0].me_value;
1556 offset = sizeof(PyDictKeyEntry);
1557 }
1558 for (i = 0, j = 0; i < size; i++) {
1559 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560 PyObject *key = ep[i].me_key;
1561 Py_INCREF(key);
1562 PyList_SET_ITEM(v, j, key);
1563 j++;
1564 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001565 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 }
1567 assert(j == n);
1568 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001569}
1570
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001571static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001572dict_values(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001573{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 register PyObject *v;
1575 register Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001576 Py_ssize_t size, n, offset;
1577 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001578
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001579 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001580 n = mp->ma_used;
1581 v = PyList_New(n);
1582 if (v == NULL)
1583 return NULL;
1584 if (n != mp->ma_used) {
1585 /* Durnit. The allocations caused the dict to resize.
1586 * Just start over, this shouldn't normally happen.
1587 */
1588 Py_DECREF(v);
1589 goto again;
1590 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001591 size = DK_SIZE(mp->ma_keys);
1592 if (mp->ma_values) {
1593 value_ptr = mp->ma_values;
1594 offset = sizeof(PyObject *);
1595 }
1596 else {
1597 value_ptr = &mp->ma_keys->dk_entries[0].me_value;
1598 offset = sizeof(PyDictKeyEntry);
1599 }
1600 for (i = 0, j = 0; i < size; i++) {
1601 PyObject *value = *value_ptr;
1602 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1603 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604 Py_INCREF(value);
1605 PyList_SET_ITEM(v, j, value);
1606 j++;
1607 }
1608 }
1609 assert(j == n);
1610 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001611}
1612
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001613static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001614dict_items(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001615{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 register PyObject *v;
1617 register Py_ssize_t i, j, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001618 Py_ssize_t size, offset;
1619 PyObject *item, *key;
1620 PyDictKeyEntry *ep;
1621 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 /* Preallocate the list of tuples, to avoid allocations during
1624 * the loop over the items, which could trigger GC, which
1625 * could resize the dict. :-(
1626 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001627 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 n = mp->ma_used;
1629 v = PyList_New(n);
1630 if (v == NULL)
1631 return NULL;
1632 for (i = 0; i < n; i++) {
1633 item = PyTuple_New(2);
1634 if (item == NULL) {
1635 Py_DECREF(v);
1636 return NULL;
1637 }
1638 PyList_SET_ITEM(v, i, item);
1639 }
1640 if (n != mp->ma_used) {
1641 /* Durnit. The allocations caused the dict to resize.
1642 * Just start over, this shouldn't normally happen.
1643 */
1644 Py_DECREF(v);
1645 goto again;
1646 }
1647 /* Nothing we do below makes any function calls. */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001648 ep = mp->ma_keys->dk_entries;
1649 size = DK_SIZE(mp->ma_keys);
1650 if (mp->ma_values) {
1651 value_ptr = mp->ma_values;
1652 offset = sizeof(PyObject *);
1653 }
1654 else {
1655 value_ptr = &ep[0].me_value;
1656 offset = sizeof(PyDictKeyEntry);
1657 }
1658 for (i = 0, j = 0; i < size; i++) {
1659 PyObject *value = *value_ptr;
1660 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1661 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001662 key = ep[i].me_key;
1663 item = PyList_GET_ITEM(v, j);
1664 Py_INCREF(key);
1665 PyTuple_SET_ITEM(item, 0, key);
1666 Py_INCREF(value);
1667 PyTuple_SET_ITEM(item, 1, value);
1668 j++;
1669 }
1670 }
1671 assert(j == n);
1672 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001673}
1674
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001675static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001676dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001677{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001678 PyObject *seq;
1679 PyObject *value = Py_None;
1680 PyObject *it; /* iter(seq) */
1681 PyObject *key;
1682 PyObject *d;
1683 int status;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
1686 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001687
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 d = PyObject_CallObject(cls, NULL);
1689 if (d == NULL)
1690 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001691
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001692 if (PyDict_CheckExact(d) && PyDict_CheckExact(seq)) {
1693 PyDictObject *mp = (PyDictObject *)d;
1694 PyObject *oldvalue;
1695 Py_ssize_t pos = 0;
1696 PyObject *key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001697 Py_hash_t hash;
Guido van Rossum58da9312007-11-10 23:39:45 +00001698
Petri Lehtinena94200e2011-10-24 21:12:58 +03001699 if (dictresize(mp, Py_SIZE(seq))) {
1700 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001702 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
1705 Py_INCREF(key);
1706 Py_INCREF(value);
Petri Lehtinena94200e2011-10-24 21:12:58 +03001707 if (insertdict(mp, key, hash, value)) {
1708 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001710 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 }
1712 return d;
1713 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) {
1716 PyDictObject *mp = (PyDictObject *)d;
1717 Py_ssize_t pos = 0;
1718 PyObject *key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001719 Py_hash_t hash;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001720
Petri Lehtinena94200e2011-10-24 21:12:58 +03001721 if (dictresize(mp, PySet_GET_SIZE(seq))) {
1722 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001724 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001725
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001726 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
1727 Py_INCREF(key);
1728 Py_INCREF(value);
Petri Lehtinena94200e2011-10-24 21:12:58 +03001729 if (insertdict(mp, key, hash, value)) {
1730 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001732 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 }
1734 return d;
1735 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001736
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 it = PyObject_GetIter(seq);
1738 if (it == NULL){
1739 Py_DECREF(d);
1740 return NULL;
1741 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 if (PyDict_CheckExact(d)) {
1744 while ((key = PyIter_Next(it)) != NULL) {
1745 status = PyDict_SetItem(d, key, value);
1746 Py_DECREF(key);
1747 if (status < 0)
1748 goto Fail;
1749 }
1750 } else {
1751 while ((key = PyIter_Next(it)) != NULL) {
1752 status = PyObject_SetItem(d, key, value);
1753 Py_DECREF(key);
1754 if (status < 0)
1755 goto Fail;
1756 }
1757 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001759 if (PyErr_Occurred())
1760 goto Fail;
1761 Py_DECREF(it);
1762 return d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001763
1764Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001765 Py_DECREF(it);
1766 Py_DECREF(d);
1767 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001768}
1769
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001770static int
1771dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001772{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773 PyObject *arg = NULL;
1774 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1777 result = -1;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001780 _Py_IDENTIFIER(keys);
1781 if (_PyObject_HasAttrId(arg, &PyId_keys))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001782 result = PyDict_Merge(self, arg, 1);
1783 else
1784 result = PyDict_MergeFromSeq2(self, arg, 1);
1785 }
1786 if (result == 0 && kwds != NULL) {
1787 if (PyArg_ValidateKeywordArguments(kwds))
1788 result = PyDict_Merge(self, kwds, 1);
1789 else
1790 result = -1;
1791 }
1792 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001793}
1794
1795static PyObject *
1796dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1797{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001798 if (dict_update_common(self, args, kwds, "update") != -1)
1799 Py_RETURN_NONE;
1800 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001801}
1802
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001803/* Update unconditionally replaces existing items.
1804 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001805 otherwise it leaves existing items unchanged.
1806
1807 PyDict_{Update,Merge} update/merge from a mapping object.
1808
Tim Petersf582b822001-12-11 18:51:08 +00001809 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001810 producing iterable objects of length 2.
1811*/
1812
Tim Petersf582b822001-12-11 18:51:08 +00001813int
Tim Peters1fc240e2001-10-26 05:06:50 +00001814PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1815{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816 PyObject *it; /* iter(seq2) */
1817 Py_ssize_t i; /* index into seq2 of current element */
1818 PyObject *item; /* seq2[i] */
1819 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00001820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001821 assert(d != NULL);
1822 assert(PyDict_Check(d));
1823 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00001824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 it = PyObject_GetIter(seq2);
1826 if (it == NULL)
1827 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001829 for (i = 0; ; ++i) {
1830 PyObject *key, *value;
1831 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001832
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001833 fast = NULL;
1834 item = PyIter_Next(it);
1835 if (item == NULL) {
1836 if (PyErr_Occurred())
1837 goto Fail;
1838 break;
1839 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001840
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001841 /* Convert item to sequence, and verify length 2. */
1842 fast = PySequence_Fast(item, "");
1843 if (fast == NULL) {
1844 if (PyErr_ExceptionMatches(PyExc_TypeError))
1845 PyErr_Format(PyExc_TypeError,
1846 "cannot convert dictionary update "
1847 "sequence element #%zd to a sequence",
1848 i);
1849 goto Fail;
1850 }
1851 n = PySequence_Fast_GET_SIZE(fast);
1852 if (n != 2) {
1853 PyErr_Format(PyExc_ValueError,
1854 "dictionary update sequence element #%zd "
1855 "has length %zd; 2 is required",
1856 i, n);
1857 goto Fail;
1858 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001860 /* Update/merge with this (key, value) pair. */
1861 key = PySequence_Fast_GET_ITEM(fast, 0);
1862 value = PySequence_Fast_GET_ITEM(fast, 1);
1863 if (override || PyDict_GetItem(d, key) == NULL) {
1864 int status = PyDict_SetItem(d, key, value);
1865 if (status < 0)
1866 goto Fail;
1867 }
1868 Py_DECREF(fast);
1869 Py_DECREF(item);
1870 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 i = 0;
1873 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00001874Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875 Py_XDECREF(item);
1876 Py_XDECREF(fast);
1877 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001878Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 Py_DECREF(it);
1880 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001881}
1882
Tim Peters6d6c1a32001-08-02 04:15:00 +00001883int
1884PyDict_Update(PyObject *a, PyObject *b)
1885{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886 return PyDict_Merge(a, b, 1);
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001887}
1888
1889int
1890PyDict_Merge(PyObject *a, PyObject *b, int override)
1891{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 register PyDictObject *mp, *other;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001893 register Py_ssize_t i, n;
1894 PyDictKeyEntry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 /* We accept for the argument either a concrete dictionary object,
1897 * or an abstract "mapping" object. For the former, we can do
1898 * things quite efficiently. For the latter, we only require that
1899 * PyMapping_Keys() and PyObject_GetItem() be supported.
1900 */
1901 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1902 PyErr_BadInternalCall();
1903 return -1;
1904 }
1905 mp = (PyDictObject*)a;
1906 if (PyDict_Check(b)) {
1907 other = (PyDictObject*)b;
1908 if (other == mp || other->ma_used == 0)
1909 /* a.update(a) or a.update({}); nothing to do */
1910 return 0;
1911 if (mp->ma_used == 0)
1912 /* Since the target dict is empty, PyDict_GetItem()
1913 * always returns NULL. Setting override to 1
1914 * skips the unnecessary test.
1915 */
1916 override = 1;
1917 /* Do one big resize at the start, rather than
1918 * incrementally resizing as we insert new items. Expect
1919 * that there will be no (or few) overlapping keys.
1920 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001921 if (mp->ma_keys->dk_usable * 3 < other->ma_used * 2)
1922 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001923 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001924 for (i = 0, n = DK_SIZE(other->ma_keys); i < n; i++) {
1925 PyObject *value;
1926 entry = &other->ma_keys->dk_entries[i];
1927 if (other->ma_values)
1928 value = other->ma_values[i];
1929 else
1930 value = entry->me_value;
1931
1932 if (value != NULL &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001933 (override ||
1934 PyDict_GetItem(a, entry->me_key) == NULL)) {
1935 Py_INCREF(entry->me_key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001936 Py_INCREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 if (insertdict(mp, entry->me_key,
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001938 entry->me_hash,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001939 value) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 return -1;
1941 }
1942 }
1943 }
1944 else {
1945 /* Do it the generic, slower way */
1946 PyObject *keys = PyMapping_Keys(b);
1947 PyObject *iter;
1948 PyObject *key, *value;
1949 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 if (keys == NULL)
1952 /* Docstring says this is equivalent to E.keys() so
1953 * if E doesn't have a .keys() method we want
1954 * AttributeError to percolate up. Might as well
1955 * do the same for any other error.
1956 */
1957 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001958
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959 iter = PyObject_GetIter(keys);
1960 Py_DECREF(keys);
1961 if (iter == NULL)
1962 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
1965 if (!override && PyDict_GetItem(a, key) != NULL) {
1966 Py_DECREF(key);
1967 continue;
1968 }
1969 value = PyObject_GetItem(b, key);
1970 if (value == NULL) {
1971 Py_DECREF(iter);
1972 Py_DECREF(key);
1973 return -1;
1974 }
1975 status = PyDict_SetItem(a, key, value);
1976 Py_DECREF(key);
1977 Py_DECREF(value);
1978 if (status < 0) {
1979 Py_DECREF(iter);
1980 return -1;
1981 }
1982 }
1983 Py_DECREF(iter);
1984 if (PyErr_Occurred())
1985 /* Iterator completed, via error */
1986 return -1;
1987 }
1988 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001989}
1990
1991static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001992dict_copy(register PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001993{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001994 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001995}
1996
1997PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001998PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001999{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002001 PyDictObject *mp;
2002 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 if (o == NULL || !PyDict_Check(o)) {
2005 PyErr_BadInternalCall();
2006 return NULL;
2007 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002008 mp = (PyDictObject *)o;
2009 if (_PyDict_HasSplitTable(mp)) {
2010 PyDictObject *split_copy;
2011 PyObject **newvalues = new_values(DK_SIZE(mp->ma_keys));
2012 if (newvalues == NULL)
2013 return PyErr_NoMemory();
2014 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2015 if (split_copy == NULL) {
2016 free_values(newvalues);
2017 return NULL;
2018 }
2019 split_copy->ma_values = newvalues;
2020 split_copy->ma_keys = mp->ma_keys;
2021 split_copy->ma_used = mp->ma_used;
2022 DK_INCREF(mp->ma_keys);
2023 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2024 PyObject *value = mp->ma_values[i];
2025 Py_XINCREF(value);
2026 split_copy->ma_values[i] = value;
2027 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002028 if (_PyObject_GC_IS_TRACKED(mp))
2029 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002030 return (PyObject *)split_copy;
2031 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002032 copy = PyDict_New();
2033 if (copy == NULL)
2034 return NULL;
2035 if (PyDict_Merge(copy, o, 1) == 0)
2036 return copy;
2037 Py_DECREF(copy);
2038 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002039}
2040
Martin v. Löwis18e16552006-02-15 17:27:45 +00002041Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002042PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002043{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002044 if (mp == NULL || !PyDict_Check(mp)) {
2045 PyErr_BadInternalCall();
2046 return -1;
2047 }
2048 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002049}
2050
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002051PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002052PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002053{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002054 if (mp == NULL || !PyDict_Check(mp)) {
2055 PyErr_BadInternalCall();
2056 return NULL;
2057 }
2058 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002059}
2060
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002061PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002062PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002063{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002064 if (mp == NULL || !PyDict_Check(mp)) {
2065 PyErr_BadInternalCall();
2066 return NULL;
2067 }
2068 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002069}
2070
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002071PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002072PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002073{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002074 if (mp == NULL || !PyDict_Check(mp)) {
2075 PyErr_BadInternalCall();
2076 return NULL;
2077 }
2078 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002079}
2080
Tim Peterse63415e2001-05-08 04:38:29 +00002081/* Return 1 if dicts equal, 0 if not, -1 if error.
2082 * Gets out as soon as any difference is detected.
2083 * Uses only Py_EQ comparison.
2084 */
2085static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002086dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002087{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002088 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002090 if (a->ma_used != b->ma_used)
2091 /* can't be equal if # of entries differ */
2092 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002093 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002094 for (i = 0; i < DK_SIZE(a->ma_keys); i++) {
2095 PyDictKeyEntry *ep = &a->ma_keys->dk_entries[i];
2096 PyObject *aval;
2097 if (a->ma_values)
2098 aval = a->ma_values[i];
2099 else
2100 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002101 if (aval != NULL) {
2102 int cmp;
2103 PyObject *bval;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002104 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002105 /* temporarily bump aval's refcount to ensure it stays
2106 alive until we're done with it */
2107 Py_INCREF(aval);
2108 /* ditto for key */
2109 Py_INCREF(key);
2110 bval = PyDict_GetItemWithError((PyObject *)b, key);
2111 Py_DECREF(key);
2112 if (bval == NULL) {
2113 Py_DECREF(aval);
2114 if (PyErr_Occurred())
2115 return -1;
2116 return 0;
2117 }
2118 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
2119 Py_DECREF(aval);
2120 if (cmp <= 0) /* error or not equal */
2121 return cmp;
2122 }
2123 }
2124 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002125}
Tim Peterse63415e2001-05-08 04:38:29 +00002126
2127static PyObject *
2128dict_richcompare(PyObject *v, PyObject *w, int op)
2129{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002130 int cmp;
2131 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002132
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002133 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2134 res = Py_NotImplemented;
2135 }
2136 else if (op == Py_EQ || op == Py_NE) {
2137 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2138 if (cmp < 0)
2139 return NULL;
2140 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2141 }
2142 else
2143 res = Py_NotImplemented;
2144 Py_INCREF(res);
2145 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002146}
Tim Peterse63415e2001-05-08 04:38:29 +00002147
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002148static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002149dict_contains(register PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002150{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002151 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002152 PyDictKeyEntry *ep;
2153 PyObject **value_addr;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002155 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002156 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002157 hash = PyObject_Hash(key);
2158 if (hash == -1)
2159 return NULL;
2160 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002161 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162 if (ep == NULL)
2163 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002164 return PyBool_FromLong(*value_addr != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002165}
2166
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002167static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002168dict_get(register PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00002169{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002170 PyObject *key;
2171 PyObject *failobj = Py_None;
2172 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002173 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002174 PyDictKeyEntry *ep;
2175 PyObject **value_addr;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002177 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
2178 return NULL;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002180 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002181 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002182 hash = PyObject_Hash(key);
2183 if (hash == -1)
2184 return NULL;
2185 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002186 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002187 if (ep == NULL)
2188 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002189 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002190 if (val == NULL)
2191 val = failobj;
2192 Py_INCREF(val);
2193 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002194}
2195
Barry Warsawc38c5da1997-10-06 17:49:20 +00002196static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002197dict_setdefault(register PyDictObject *mp, PyObject *args)
Guido van Rossum164452c2000-08-08 16:12:54 +00002198{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002199 PyObject *key;
2200 PyObject *failobj = Py_None;
2201 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002202 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002203 PyDictKeyEntry *ep;
2204 PyObject **value_addr;
Guido van Rossum164452c2000-08-08 16:12:54 +00002205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002206 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
2207 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00002208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002209 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002210 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 hash = PyObject_Hash(key);
2212 if (hash == -1)
2213 return NULL;
2214 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002215 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002216 if (ep == NULL)
2217 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002218 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002219 if (val == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002220 Py_INCREF(failobj);
2221 Py_INCREF(key);
2222 if (mp->ma_keys->dk_usable <= 0) {
2223 /* Need to resize. */
2224 if (insertion_resize(mp) < 0)
2225 return NULL;
2226 ep = find_empty_slot(mp, key, hash, &value_addr);
2227 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002228 MAINTAIN_TRACKING(mp, key, failobj);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002229 ep->me_key = key;
2230 ep->me_hash = hash;
2231 *value_addr = failobj;
2232 val = failobj;
2233 mp->ma_keys->dk_usable--;
2234 mp->ma_used++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002235 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002236 Py_INCREF(val);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002237 return val;
Guido van Rossum164452c2000-08-08 16:12:54 +00002238}
2239
2240
2241static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002242dict_clear(register PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002243{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002244 PyDict_Clear((PyObject *)mp);
2245 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002246}
2247
Guido van Rossumba6ab842000-12-12 22:02:18 +00002248static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002249dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002250{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002251 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002252 PyObject *old_value, *old_key;
2253 PyObject *key, *deflt = NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002254 PyDictKeyEntry *ep;
2255 PyObject **value_addr;
Guido van Rossume027d982002-04-12 15:11:59 +00002256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002257 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2258 return NULL;
2259 if (mp->ma_used == 0) {
2260 if (deflt) {
2261 Py_INCREF(deflt);
2262 return deflt;
2263 }
Raymond Hettingerdd421542010-10-30 08:10:29 +00002264 set_key_error(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002265 return NULL;
2266 }
2267 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002268 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002269 hash = PyObject_Hash(key);
2270 if (hash == -1)
2271 return NULL;
2272 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002273 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002274 if (ep == NULL)
2275 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002276 old_value = *value_addr;
2277 if (old_value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 if (deflt) {
2279 Py_INCREF(deflt);
2280 return deflt;
2281 }
2282 set_key_error(key);
2283 return NULL;
2284 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002285 *value_addr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002286 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002287 if (!_PyDict_HasSplitTable(mp)) {
2288 ENSURE_ALLOWS_DELETIONS(mp);
2289 old_key = ep->me_key;
2290 Py_INCREF(dummy);
2291 ep->me_key = dummy;
2292 Py_DECREF(old_key);
2293 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 return old_value;
Guido van Rossume027d982002-04-12 15:11:59 +00002295}
2296
2297static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002298dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00002299{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002300 Py_hash_t i = 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002301 PyDictKeyEntry *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002302 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002303
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002305 /* Allocate the result tuple before checking the size. Believe it
2306 * or not, this allocation could trigger a garbage collection which
2307 * could empty the dict, so if we checked the size first and that
2308 * happened, the result would be an infinite loop (searching for an
2309 * entry that no longer exists). Note that the usual popitem()
2310 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2311 * tuple away if the dict *is* empty isn't a significant
2312 * inefficiency -- possible, but unlikely in practice.
2313 */
2314 res = PyTuple_New(2);
2315 if (res == NULL)
2316 return NULL;
2317 if (mp->ma_used == 0) {
2318 Py_DECREF(res);
2319 PyErr_SetString(PyExc_KeyError,
2320 "popitem(): dictionary is empty");
2321 return NULL;
2322 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002323 /* Convert split table to combined table */
2324 if (mp->ma_keys->dk_lookup == lookdict_split) {
2325 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
2326 Py_DECREF(res);
2327 return NULL;
2328 }
2329 }
2330 ENSURE_ALLOWS_DELETIONS(mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002331 /* Set ep to "the first" dict entry with a value. We abuse the hash
2332 * field of slot 0 to hold a search finger:
2333 * If slot 0 has a value, use slot 0.
2334 * Else slot 0 is being used to hold a search finger,
2335 * and we use its hash value as the first index to look.
2336 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002337 ep = &mp->ma_keys->dk_entries[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002338 if (ep->me_value == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002339 Py_ssize_t mask = DK_MASK(mp->ma_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002340 i = ep->me_hash;
2341 /* The hash field may be a real hash value, or it may be a
2342 * legit search finger, or it may be a once-legit search
2343 * finger that's out of bounds now because it wrapped around
2344 * or the table shrunk -- simply make sure it's in bounds now.
2345 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002346 if (i > mask || i < 1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002347 i = 1; /* skip slot 0 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002348 while ((ep = &mp->ma_keys->dk_entries[i])->me_value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002349 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002350 if (i > mask)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002351 i = 1;
2352 }
2353 }
2354 PyTuple_SET_ITEM(res, 0, ep->me_key);
2355 PyTuple_SET_ITEM(res, 1, ep->me_value);
2356 Py_INCREF(dummy);
2357 ep->me_key = dummy;
2358 ep->me_value = NULL;
2359 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002360 assert(mp->ma_keys->dk_entries[0].me_value == NULL);
2361 mp->ma_keys->dk_entries[0].me_hash = i + 1; /* next place to start */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002362 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002363}
2364
Jeremy Hylton8caad492000-06-23 14:18:11 +00002365static int
2366dict_traverse(PyObject *op, visitproc visit, void *arg)
2367{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002368 Py_ssize_t i, n;
2369 PyDictObject *mp = (PyDictObject *)op;
2370 if (mp->ma_keys->dk_lookup == lookdict) {
2371 for (i = 0; i < DK_SIZE(mp->ma_keys); i++) {
2372 if (mp->ma_keys->dk_entries[i].me_value != NULL) {
2373 Py_VISIT(mp->ma_keys->dk_entries[i].me_value);
2374 Py_VISIT(mp->ma_keys->dk_entries[i].me_key);
2375 }
2376 }
2377 } else {
2378 if (mp->ma_values != NULL) {
2379 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2380 Py_VISIT(mp->ma_values[i]);
2381 }
2382 }
2383 else {
2384 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2385 Py_VISIT(mp->ma_keys->dk_entries[i].me_value);
2386 }
2387 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388 }
2389 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002390}
2391
2392static int
2393dict_tp_clear(PyObject *op)
2394{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 PyDict_Clear(op);
2396 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002397}
2398
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002399static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002400
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002401static PyObject *
2402dict_sizeof(PyDictObject *mp)
2403{
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002404 Py_ssize_t size, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002405
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002406 size = DK_SIZE(mp->ma_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002407 res = sizeof(PyDictObject);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002408 if (mp->ma_values)
2409 res += size * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002410 /* If the dictionary is split, the keys portion is accounted-for
2411 in the type object. */
2412 if (mp->ma_keys->dk_refcnt == 1)
2413 res += sizeof(PyDictKeysObject) + (size-1) * sizeof(PyDictKeyEntry);
2414 return PyLong_FromSsize_t(res);
2415}
2416
2417Py_ssize_t
2418_PyDict_KeysSize(PyDictKeysObject *keys)
2419{
2420 return sizeof(PyDictKeysObject) + (DK_SIZE(keys)-1) * sizeof(PyDictKeyEntry);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002421}
2422
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002423PyDoc_STRVAR(contains__doc__,
2424"D.__contains__(k) -> True if D has a key k, else False");
2425
2426PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
2427
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002428PyDoc_STRVAR(sizeof__doc__,
2429"D.__sizeof__() -> size of D in memory, in bytes");
2430
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002431PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002432"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002433
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002434PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002435"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 +00002436
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002437PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002438"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002439If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00002440
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002441PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00002442"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000024432-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002444
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002445PyDoc_STRVAR(update__doc__,
Georg Brandlac0675c2011-12-18 19:30:55 +01002446"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n"
2447"If E present and has a .keys() method, does: for k in E: D[k] = E[k]\n\
2448If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002449In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00002450
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002451PyDoc_STRVAR(fromkeys__doc__,
2452"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
2453v defaults to None.");
2454
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002455PyDoc_STRVAR(clear__doc__,
2456"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002457
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002458PyDoc_STRVAR(copy__doc__,
2459"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00002460
Guido van Rossumb90c8482007-02-10 01:11:45 +00002461/* Forward */
2462static PyObject *dictkeys_new(PyObject *);
2463static PyObject *dictitems_new(PyObject *);
2464static PyObject *dictvalues_new(PyObject *);
2465
Guido van Rossum45c85d12007-07-27 16:31:40 +00002466PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002467 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002468PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002469 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002470PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002471 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00002472
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002473static PyMethodDef mapp_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002474 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
2475 contains__doc__},
2476 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
2477 getitem__doc__},
2478 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
2479 sizeof__doc__},
2480 {"get", (PyCFunction)dict_get, METH_VARARGS,
2481 get__doc__},
2482 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
2483 setdefault_doc__},
2484 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
2485 pop__doc__},
2486 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
2487 popitem__doc__},
2488 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
2489 keys__doc__},
2490 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
2491 items__doc__},
2492 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
2493 values__doc__},
2494 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
2495 update__doc__},
2496 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
2497 fromkeys__doc__},
2498 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
2499 clear__doc__},
2500 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
2501 copy__doc__},
2502 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002503};
2504
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002505/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002506int
2507PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002508{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002509 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002510 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002511 PyDictKeyEntry *ep;
2512 PyObject **value_addr;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002514 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002515 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002516 hash = PyObject_Hash(key);
2517 if (hash == -1)
2518 return -1;
2519 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002520 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
2521 return (ep == NULL) ? -1 : (*value_addr != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002522}
2523
Thomas Wouterscf297e42007-02-23 15:07:44 +00002524/* Internal version of PyDict_Contains used when the hash value is already known */
2525int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002526_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00002527{
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;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002531
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002532 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
2533 return (ep == NULL) ? -1 : (*value_addr != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00002534}
2535
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002536/* Hack to implement "key in dict" */
2537static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002538 0, /* sq_length */
2539 0, /* sq_concat */
2540 0, /* sq_repeat */
2541 0, /* sq_item */
2542 0, /* sq_slice */
2543 0, /* sq_ass_item */
2544 0, /* sq_ass_slice */
2545 PyDict_Contains, /* sq_contains */
2546 0, /* sq_inplace_concat */
2547 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002548};
2549
Guido van Rossum09e563a2001-05-01 12:10:21 +00002550static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002551dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2552{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002553 PyObject *self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002555 assert(type != NULL && type->tp_alloc != NULL);
2556 self = type->tp_alloc(type, 0);
2557 if (self != NULL) {
2558 PyDictObject *d = (PyDictObject *)self;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002559 d->ma_keys = new_keys_object(PyDict_MINSIZE_COMBINED);
2560 /* XXX - Should we raise a no-memory error? */
2561 if (d->ma_keys == NULL) {
2562 DK_INCREF(Py_EMPTY_KEYS);
2563 d->ma_keys = Py_EMPTY_KEYS;
2564 d->ma_values = empty_values;
2565 }
2566 d->ma_used = 0;
Ezio Melotti13925002011-03-16 11:05:33 +02002567 /* The object has been implicitly tracked by tp_alloc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002568 if (type == &PyDict_Type)
2569 _PyObject_GC_UNTRACK(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002570 }
2571 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002572}
2573
Tim Peters25786c02001-09-02 08:22:48 +00002574static int
2575dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2576{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002577 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002578}
2579
Tim Peters6d6c1a32001-08-02 04:15:00 +00002580static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002581dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002582{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002583 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002584}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002585
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002586PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002587"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002588"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002589" (key, value) pairs\n"
2590"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002591" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002592" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002593" d[k] = v\n"
2594"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2595" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002596
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002597PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002598 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2599 "dict",
2600 sizeof(PyDictObject),
2601 0,
2602 (destructor)dict_dealloc, /* tp_dealloc */
2603 0, /* tp_print */
2604 0, /* tp_getattr */
2605 0, /* tp_setattr */
2606 0, /* tp_reserved */
2607 (reprfunc)dict_repr, /* tp_repr */
2608 0, /* tp_as_number */
2609 &dict_as_sequence, /* tp_as_sequence */
2610 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002611 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002612 0, /* tp_call */
2613 0, /* tp_str */
2614 PyObject_GenericGetAttr, /* tp_getattro */
2615 0, /* tp_setattro */
2616 0, /* tp_as_buffer */
2617 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2618 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
2619 dictionary_doc, /* tp_doc */
2620 dict_traverse, /* tp_traverse */
2621 dict_tp_clear, /* tp_clear */
2622 dict_richcompare, /* tp_richcompare */
2623 0, /* tp_weaklistoffset */
2624 (getiterfunc)dict_iter, /* tp_iter */
2625 0, /* tp_iternext */
2626 mapp_methods, /* tp_methods */
2627 0, /* tp_members */
2628 0, /* tp_getset */
2629 0, /* tp_base */
2630 0, /* tp_dict */
2631 0, /* tp_descr_get */
2632 0, /* tp_descr_set */
2633 0, /* tp_dictoffset */
2634 dict_init, /* tp_init */
2635 PyType_GenericAlloc, /* tp_alloc */
2636 dict_new, /* tp_new */
2637 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002638};
2639
Victor Stinner3c1e4812012-03-26 22:10:51 +02002640PyObject *
2641_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
2642{
2643 PyObject *kv;
2644 kv = _PyUnicode_FromId(key); /* borrowed */
2645 if (kv == NULL)
2646 return NULL;
2647 return PyDict_GetItem(dp, kv);
2648}
2649
Guido van Rossum3cca2451997-05-16 14:23:33 +00002650/* For backward compatibility with old dictionary interface */
2651
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002652PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002653PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002654{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002655 PyObject *kv, *rv;
2656 kv = PyUnicode_FromString(key);
2657 if (kv == NULL)
2658 return NULL;
2659 rv = PyDict_GetItem(v, kv);
2660 Py_DECREF(kv);
2661 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002662}
2663
2664int
Victor Stinner3c1e4812012-03-26 22:10:51 +02002665_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
2666{
2667 PyObject *kv;
2668 kv = _PyUnicode_FromId(key); /* borrowed */
2669 if (kv == NULL)
2670 return -1;
2671 return PyDict_SetItem(v, kv, item);
2672}
2673
2674int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002675PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002676{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002677 PyObject *kv;
2678 int err;
2679 kv = PyUnicode_FromString(key);
2680 if (kv == NULL)
2681 return -1;
2682 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
2683 err = PyDict_SetItem(v, kv, item);
2684 Py_DECREF(kv);
2685 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002686}
2687
2688int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002689PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002690{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002691 PyObject *kv;
2692 int err;
2693 kv = PyUnicode_FromString(key);
2694 if (kv == NULL)
2695 return -1;
2696 err = PyDict_DelItem(v, kv);
2697 Py_DECREF(kv);
2698 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002699}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002700
Raymond Hettinger019a1482004-03-18 02:41:19 +00002701/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002702
2703typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002704 PyObject_HEAD
2705 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
2706 Py_ssize_t di_used;
2707 Py_ssize_t di_pos;
2708 PyObject* di_result; /* reusable result tuple for iteritems */
2709 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002710} dictiterobject;
2711
2712static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002713dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002714{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002715 dictiterobject *di;
2716 di = PyObject_GC_New(dictiterobject, itertype);
2717 if (di == NULL)
2718 return NULL;
2719 Py_INCREF(dict);
2720 di->di_dict = dict;
2721 di->di_used = dict->ma_used;
2722 di->di_pos = 0;
2723 di->len = dict->ma_used;
2724 if (itertype == &PyDictIterItem_Type) {
2725 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2726 if (di->di_result == NULL) {
2727 Py_DECREF(di);
2728 return NULL;
2729 }
2730 }
2731 else
2732 di->di_result = NULL;
2733 _PyObject_GC_TRACK(di);
2734 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002735}
2736
2737static void
2738dictiter_dealloc(dictiterobject *di)
2739{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002740 Py_XDECREF(di->di_dict);
2741 Py_XDECREF(di->di_result);
2742 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002743}
2744
2745static int
2746dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
2747{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002748 Py_VISIT(di->di_dict);
2749 Py_VISIT(di->di_result);
2750 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002751}
2752
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002753static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002754dictiter_len(dictiterobject *di)
2755{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002756 Py_ssize_t len = 0;
2757 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
2758 len = di->len;
2759 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002760}
2761
Guido van Rossumb90c8482007-02-10 01:11:45 +00002762PyDoc_STRVAR(length_hint_doc,
2763 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002764
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002765static PyObject *
2766dictiter_reduce(dictiterobject *di);
2767
2768PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
2769
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002770static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002771 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
2772 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002773 {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS,
2774 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002775 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002776};
2777
Raymond Hettinger019a1482004-03-18 02:41:19 +00002778static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002779{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002780 PyObject *key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002781 register Py_ssize_t i, mask, offset;
2782 register PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002783 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002784 PyObject **value_ptr;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002785
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002786 if (d == NULL)
2787 return NULL;
2788 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002790 if (di->di_used != d->ma_used) {
2791 PyErr_SetString(PyExc_RuntimeError,
2792 "dictionary changed size during iteration");
2793 di->di_used = -1; /* Make this state sticky */
2794 return NULL;
2795 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002797 i = di->di_pos;
2798 if (i < 0)
2799 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002800 k = d->ma_keys;
2801 if (d->ma_values) {
2802 value_ptr = &d->ma_values[i];
2803 offset = sizeof(PyObject *);
2804 }
2805 else {
2806 value_ptr = &k->dk_entries[i].me_value;
2807 offset = sizeof(PyDictKeyEntry);
2808 }
2809 mask = DK_SIZE(k)-1;
2810 while (i <= mask && *value_ptr == NULL) {
2811 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002812 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002813 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002814 di->di_pos = i+1;
2815 if (i > mask)
2816 goto fail;
2817 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002818 key = k->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002819 Py_INCREF(key);
2820 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002821
2822fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002823 Py_DECREF(d);
2824 di->di_dict = NULL;
2825 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002826}
2827
Raymond Hettinger019a1482004-03-18 02:41:19 +00002828PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002829 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2830 "dict_keyiterator", /* tp_name */
2831 sizeof(dictiterobject), /* tp_basicsize */
2832 0, /* tp_itemsize */
2833 /* methods */
2834 (destructor)dictiter_dealloc, /* tp_dealloc */
2835 0, /* tp_print */
2836 0, /* tp_getattr */
2837 0, /* tp_setattr */
2838 0, /* tp_reserved */
2839 0, /* tp_repr */
2840 0, /* tp_as_number */
2841 0, /* tp_as_sequence */
2842 0, /* tp_as_mapping */
2843 0, /* tp_hash */
2844 0, /* tp_call */
2845 0, /* tp_str */
2846 PyObject_GenericGetAttr, /* tp_getattro */
2847 0, /* tp_setattro */
2848 0, /* tp_as_buffer */
2849 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2850 0, /* tp_doc */
2851 (traverseproc)dictiter_traverse, /* tp_traverse */
2852 0, /* tp_clear */
2853 0, /* tp_richcompare */
2854 0, /* tp_weaklistoffset */
2855 PyObject_SelfIter, /* tp_iter */
2856 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
2857 dictiter_methods, /* tp_methods */
2858 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002859};
2860
2861static PyObject *dictiter_iternextvalue(dictiterobject *di)
2862{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002863 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002864 register Py_ssize_t i, mask, offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002865 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002866 PyObject **value_ptr;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002868 if (d == NULL)
2869 return NULL;
2870 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00002871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002872 if (di->di_used != d->ma_used) {
2873 PyErr_SetString(PyExc_RuntimeError,
2874 "dictionary changed size during iteration");
2875 di->di_used = -1; /* Make this state sticky */
2876 return NULL;
2877 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002880 mask = DK_SIZE(d->ma_keys)-1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002881 if (i < 0 || i > mask)
2882 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002883 if (d->ma_values) {
2884 value_ptr = &d->ma_values[i];
2885 offset = sizeof(PyObject *);
2886 }
2887 else {
2888 value_ptr = &d->ma_keys->dk_entries[i].me_value;
2889 offset = sizeof(PyDictKeyEntry);
2890 }
2891 while (i <= mask && *value_ptr == NULL) {
2892 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002893 i++;
2894 if (i > mask)
2895 goto fail;
2896 }
2897 di->di_pos = i+1;
2898 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002899 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002900 Py_INCREF(value);
2901 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002902
2903fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002904 Py_DECREF(d);
2905 di->di_dict = NULL;
2906 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002907}
2908
2909PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002910 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2911 "dict_valueiterator", /* tp_name */
2912 sizeof(dictiterobject), /* tp_basicsize */
2913 0, /* tp_itemsize */
2914 /* methods */
2915 (destructor)dictiter_dealloc, /* tp_dealloc */
2916 0, /* tp_print */
2917 0, /* tp_getattr */
2918 0, /* tp_setattr */
2919 0, /* tp_reserved */
2920 0, /* tp_repr */
2921 0, /* tp_as_number */
2922 0, /* tp_as_sequence */
2923 0, /* tp_as_mapping */
2924 0, /* tp_hash */
2925 0, /* tp_call */
2926 0, /* tp_str */
2927 PyObject_GenericGetAttr, /* tp_getattro */
2928 0, /* tp_setattro */
2929 0, /* tp_as_buffer */
2930 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2931 0, /* tp_doc */
2932 (traverseproc)dictiter_traverse, /* tp_traverse */
2933 0, /* tp_clear */
2934 0, /* tp_richcompare */
2935 0, /* tp_weaklistoffset */
2936 PyObject_SelfIter, /* tp_iter */
2937 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
2938 dictiter_methods, /* tp_methods */
2939 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002940};
2941
2942static PyObject *dictiter_iternextitem(dictiterobject *di)
2943{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002944 PyObject *key, *value, *result = di->di_result;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002945 register Py_ssize_t i, mask, offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002946 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002947 PyObject **value_ptr;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002949 if (d == NULL)
2950 return NULL;
2951 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00002952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002953 if (di->di_used != d->ma_used) {
2954 PyErr_SetString(PyExc_RuntimeError,
2955 "dictionary changed size during iteration");
2956 di->di_used = -1; /* Make this state sticky */
2957 return NULL;
2958 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002960 i = di->di_pos;
2961 if (i < 0)
2962 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002963 mask = DK_SIZE(d->ma_keys)-1;
2964 if (d->ma_values) {
2965 value_ptr = &d->ma_values[i];
2966 offset = sizeof(PyObject *);
2967 }
2968 else {
2969 value_ptr = &d->ma_keys->dk_entries[i].me_value;
2970 offset = sizeof(PyDictKeyEntry);
2971 }
2972 while (i <= mask && *value_ptr == NULL) {
2973 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002974 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002975 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002976 di->di_pos = i+1;
2977 if (i > mask)
2978 goto fail;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002980 if (result->ob_refcnt == 1) {
2981 Py_INCREF(result);
2982 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2983 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2984 } else {
2985 result = PyTuple_New(2);
2986 if (result == NULL)
2987 return NULL;
2988 }
2989 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002990 key = d->ma_keys->dk_entries[i].me_key;
2991 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002992 Py_INCREF(key);
2993 Py_INCREF(value);
2994 PyTuple_SET_ITEM(result, 0, key);
2995 PyTuple_SET_ITEM(result, 1, value);
2996 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002997
2998fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002999 Py_DECREF(d);
3000 di->di_dict = NULL;
3001 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003002}
3003
3004PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003005 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3006 "dict_itemiterator", /* tp_name */
3007 sizeof(dictiterobject), /* tp_basicsize */
3008 0, /* tp_itemsize */
3009 /* methods */
3010 (destructor)dictiter_dealloc, /* tp_dealloc */
3011 0, /* tp_print */
3012 0, /* tp_getattr */
3013 0, /* tp_setattr */
3014 0, /* tp_reserved */
3015 0, /* tp_repr */
3016 0, /* tp_as_number */
3017 0, /* tp_as_sequence */
3018 0, /* tp_as_mapping */
3019 0, /* tp_hash */
3020 0, /* tp_call */
3021 0, /* tp_str */
3022 PyObject_GenericGetAttr, /* tp_getattro */
3023 0, /* tp_setattro */
3024 0, /* tp_as_buffer */
3025 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3026 0, /* tp_doc */
3027 (traverseproc)dictiter_traverse, /* tp_traverse */
3028 0, /* tp_clear */
3029 0, /* tp_richcompare */
3030 0, /* tp_weaklistoffset */
3031 PyObject_SelfIter, /* tp_iter */
3032 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3033 dictiter_methods, /* tp_methods */
3034 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003035};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003036
3037
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003038static PyObject *
3039dictiter_reduce(dictiterobject *di)
3040{
3041 PyObject *list;
3042 dictiterobject tmp;
3043
3044 list = PyList_New(0);
3045 if (!list)
3046 return NULL;
3047
3048 /* copy the itertor state */
3049 tmp = *di;
3050 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003051
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003052 /* iterate the temporary into a list */
3053 for(;;) {
3054 PyObject *element = 0;
3055 if (Py_TYPE(di) == &PyDictIterItem_Type)
3056 element = dictiter_iternextitem(&tmp);
3057 else if (Py_TYPE(di) == &PyDictIterKey_Type)
3058 element = dictiter_iternextkey(&tmp);
3059 else if (Py_TYPE(di) == &PyDictIterValue_Type)
3060 element = dictiter_iternextvalue(&tmp);
3061 else
3062 assert(0);
3063 if (element) {
3064 if (PyList_Append(list, element)) {
3065 Py_DECREF(element);
3066 Py_DECREF(list);
3067 Py_XDECREF(tmp.di_dict);
3068 return NULL;
3069 }
3070 Py_DECREF(element);
3071 } else
3072 break;
3073 }
3074 Py_XDECREF(tmp.di_dict);
3075 /* check for error */
3076 if (tmp.di_dict != NULL) {
3077 /* we have an error */
3078 Py_DECREF(list);
3079 return NULL;
3080 }
Antoine Pitroua7013882012-04-05 00:04:20 +02003081 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003082}
3083
Guido van Rossum3ac67412007-02-10 18:55:06 +00003084/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003085/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003086/***********************************************/
3087
Guido van Rossumb90c8482007-02-10 01:11:45 +00003088/* The instance lay-out is the same for all three; but the type differs. */
3089
3090typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003091 PyObject_HEAD
3092 PyDictObject *dv_dict;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003093} dictviewobject;
3094
3095
3096static void
Guido van Rossum3ac67412007-02-10 18:55:06 +00003097dictview_dealloc(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003098{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003099 Py_XDECREF(dv->dv_dict);
3100 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003101}
3102
3103static int
3104dictview_traverse(dictviewobject *dv, visitproc visit, void *arg)
3105{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003106 Py_VISIT(dv->dv_dict);
3107 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003108}
3109
Guido van Rossum83825ac2007-02-10 04:54:19 +00003110static Py_ssize_t
Guido van Rossum3ac67412007-02-10 18:55:06 +00003111dictview_len(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003112{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003113 Py_ssize_t len = 0;
3114 if (dv->dv_dict != NULL)
3115 len = dv->dv_dict->ma_used;
3116 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003117}
3118
3119static PyObject *
3120dictview_new(PyObject *dict, PyTypeObject *type)
3121{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003122 dictviewobject *dv;
3123 if (dict == NULL) {
3124 PyErr_BadInternalCall();
3125 return NULL;
3126 }
3127 if (!PyDict_Check(dict)) {
3128 /* XXX Get rid of this restriction later */
3129 PyErr_Format(PyExc_TypeError,
3130 "%s() requires a dict argument, not '%s'",
3131 type->tp_name, dict->ob_type->tp_name);
3132 return NULL;
3133 }
3134 dv = PyObject_GC_New(dictviewobject, type);
3135 if (dv == NULL)
3136 return NULL;
3137 Py_INCREF(dict);
3138 dv->dv_dict = (PyDictObject *)dict;
3139 _PyObject_GC_TRACK(dv);
3140 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003141}
3142
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003143/* TODO(guido): The views objects are not complete:
3144
3145 * support more set operations
3146 * support arbitrary mappings?
3147 - either these should be static or exported in dictobject.h
3148 - if public then they should probably be in builtins
3149*/
3150
Guido van Rossumaac530c2007-08-24 22:33:45 +00003151/* Return 1 if self is a subset of other, iterating over self;
3152 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003153static int
3154all_contained_in(PyObject *self, PyObject *other)
3155{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003156 PyObject *iter = PyObject_GetIter(self);
3157 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003159 if (iter == NULL)
3160 return -1;
3161 for (;;) {
3162 PyObject *next = PyIter_Next(iter);
3163 if (next == NULL) {
3164 if (PyErr_Occurred())
3165 ok = -1;
3166 break;
3167 }
3168 ok = PySequence_Contains(other, next);
3169 Py_DECREF(next);
3170 if (ok <= 0)
3171 break;
3172 }
3173 Py_DECREF(iter);
3174 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003175}
3176
3177static PyObject *
3178dictview_richcompare(PyObject *self, PyObject *other, int op)
3179{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003180 Py_ssize_t len_self, len_other;
3181 int ok;
3182 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003184 assert(self != NULL);
3185 assert(PyDictViewSet_Check(self));
3186 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00003187
Brian Curtindfc80e32011-08-10 20:28:54 -05003188 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
3189 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003191 len_self = PyObject_Size(self);
3192 if (len_self < 0)
3193 return NULL;
3194 len_other = PyObject_Size(other);
3195 if (len_other < 0)
3196 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003197
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003198 ok = 0;
3199 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00003200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003201 case Py_NE:
3202 case Py_EQ:
3203 if (len_self == len_other)
3204 ok = all_contained_in(self, other);
3205 if (op == Py_NE && ok >= 0)
3206 ok = !ok;
3207 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003209 case Py_LT:
3210 if (len_self < len_other)
3211 ok = all_contained_in(self, other);
3212 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003214 case Py_LE:
3215 if (len_self <= len_other)
3216 ok = all_contained_in(self, other);
3217 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003219 case Py_GT:
3220 if (len_self > len_other)
3221 ok = all_contained_in(other, self);
3222 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003224 case Py_GE:
3225 if (len_self >= len_other)
3226 ok = all_contained_in(other, self);
3227 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003229 }
3230 if (ok < 0)
3231 return NULL;
3232 result = ok ? Py_True : Py_False;
3233 Py_INCREF(result);
3234 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003235}
3236
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003237static PyObject *
3238dictview_repr(dictviewobject *dv)
3239{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003240 PyObject *seq;
3241 PyObject *result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003243 seq = PySequence_List((PyObject *)dv);
3244 if (seq == NULL)
3245 return NULL;
3246
3247 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
3248 Py_DECREF(seq);
3249 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003250}
3251
Guido van Rossum3ac67412007-02-10 18:55:06 +00003252/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003253
3254static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003255dictkeys_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003256{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003257 if (dv->dv_dict == NULL) {
3258 Py_RETURN_NONE;
3259 }
3260 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003261}
3262
3263static int
3264dictkeys_contains(dictviewobject *dv, PyObject *obj)
3265{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003266 if (dv->dv_dict == NULL)
3267 return 0;
3268 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003269}
3270
Guido van Rossum83825ac2007-02-10 04:54:19 +00003271static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003272 (lenfunc)dictview_len, /* sq_length */
3273 0, /* sq_concat */
3274 0, /* sq_repeat */
3275 0, /* sq_item */
3276 0, /* sq_slice */
3277 0, /* sq_ass_item */
3278 0, /* sq_ass_slice */
3279 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003280};
3281
Guido van Rossum523259b2007-08-24 23:41:22 +00003282static PyObject*
3283dictviews_sub(PyObject* self, PyObject *other)
3284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003285 PyObject *result = PySet_New(self);
3286 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003287 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003289 if (result == NULL)
3290 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003291
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003292 tmp = _PyObject_CallMethodId(result, &PyId_difference_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003293 if (tmp == NULL) {
3294 Py_DECREF(result);
3295 return NULL;
3296 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003298 Py_DECREF(tmp);
3299 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003300}
3301
3302static PyObject*
3303dictviews_and(PyObject* self, PyObject *other)
3304{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003305 PyObject *result = PySet_New(self);
3306 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003307 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003309 if (result == NULL)
3310 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003311
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003312 tmp = _PyObject_CallMethodId(result, &PyId_intersection_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003313 if (tmp == NULL) {
3314 Py_DECREF(result);
3315 return NULL;
3316 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003318 Py_DECREF(tmp);
3319 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003320}
3321
3322static PyObject*
3323dictviews_or(PyObject* self, PyObject *other)
3324{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003325 PyObject *result = PySet_New(self);
3326 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003327 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003329 if (result == NULL)
3330 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003331
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003332 tmp = _PyObject_CallMethodId(result, &PyId_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003333 if (tmp == NULL) {
3334 Py_DECREF(result);
3335 return NULL;
3336 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003338 Py_DECREF(tmp);
3339 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003340}
3341
3342static PyObject*
3343dictviews_xor(PyObject* self, PyObject *other)
3344{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003345 PyObject *result = PySet_New(self);
3346 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003347 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003349 if (result == NULL)
3350 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003351
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003352 tmp = _PyObject_CallMethodId(result, &PyId_symmetric_difference_update, "O",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003353 other);
3354 if (tmp == NULL) {
3355 Py_DECREF(result);
3356 return NULL;
3357 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003359 Py_DECREF(tmp);
3360 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003361}
3362
3363static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003364 0, /*nb_add*/
3365 (binaryfunc)dictviews_sub, /*nb_subtract*/
3366 0, /*nb_multiply*/
3367 0, /*nb_remainder*/
3368 0, /*nb_divmod*/
3369 0, /*nb_power*/
3370 0, /*nb_negative*/
3371 0, /*nb_positive*/
3372 0, /*nb_absolute*/
3373 0, /*nb_bool*/
3374 0, /*nb_invert*/
3375 0, /*nb_lshift*/
3376 0, /*nb_rshift*/
3377 (binaryfunc)dictviews_and, /*nb_and*/
3378 (binaryfunc)dictviews_xor, /*nb_xor*/
3379 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00003380};
3381
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003382static PyObject*
3383dictviews_isdisjoint(PyObject *self, PyObject *other)
3384{
3385 PyObject *it;
3386 PyObject *item = NULL;
3387
3388 if (self == other) {
3389 if (dictview_len((dictviewobject *)self) == 0)
3390 Py_RETURN_TRUE;
3391 else
3392 Py_RETURN_FALSE;
3393 }
3394
3395 /* Iterate over the shorter object (only if other is a set,
3396 * because PySequence_Contains may be expensive otherwise): */
3397 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
3398 Py_ssize_t len_self = dictview_len((dictviewobject *)self);
3399 Py_ssize_t len_other = PyObject_Size(other);
3400 if (len_other == -1)
3401 return NULL;
3402
3403 if ((len_other > len_self)) {
3404 PyObject *tmp = other;
3405 other = self;
3406 self = tmp;
3407 }
3408 }
3409
3410 it = PyObject_GetIter(other);
3411 if (it == NULL)
3412 return NULL;
3413
3414 while ((item = PyIter_Next(it)) != NULL) {
3415 int contains = PySequence_Contains(self, item);
3416 Py_DECREF(item);
3417 if (contains == -1) {
3418 Py_DECREF(it);
3419 return NULL;
3420 }
3421
3422 if (contains) {
3423 Py_DECREF(it);
3424 Py_RETURN_FALSE;
3425 }
3426 }
3427 Py_DECREF(it);
3428 if (PyErr_Occurred())
3429 return NULL; /* PyIter_Next raised an exception. */
3430 Py_RETURN_TRUE;
3431}
3432
3433PyDoc_STRVAR(isdisjoint_doc,
3434"Return True if the view and the given iterable have a null intersection.");
3435
Guido van Rossumb90c8482007-02-10 01:11:45 +00003436static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003437 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3438 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003439 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003440};
3441
3442PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003443 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3444 "dict_keys", /* tp_name */
3445 sizeof(dictviewobject), /* tp_basicsize */
3446 0, /* tp_itemsize */
3447 /* methods */
3448 (destructor)dictview_dealloc, /* tp_dealloc */
3449 0, /* tp_print */
3450 0, /* tp_getattr */
3451 0, /* tp_setattr */
3452 0, /* tp_reserved */
3453 (reprfunc)dictview_repr, /* tp_repr */
3454 &dictviews_as_number, /* tp_as_number */
3455 &dictkeys_as_sequence, /* tp_as_sequence */
3456 0, /* tp_as_mapping */
3457 0, /* tp_hash */
3458 0, /* tp_call */
3459 0, /* tp_str */
3460 PyObject_GenericGetAttr, /* tp_getattro */
3461 0, /* tp_setattro */
3462 0, /* tp_as_buffer */
3463 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3464 0, /* tp_doc */
3465 (traverseproc)dictview_traverse, /* tp_traverse */
3466 0, /* tp_clear */
3467 dictview_richcompare, /* tp_richcompare */
3468 0, /* tp_weaklistoffset */
3469 (getiterfunc)dictkeys_iter, /* tp_iter */
3470 0, /* tp_iternext */
3471 dictkeys_methods, /* tp_methods */
3472 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003473};
3474
3475static PyObject *
3476dictkeys_new(PyObject *dict)
3477{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003478 return dictview_new(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003479}
3480
Guido van Rossum3ac67412007-02-10 18:55:06 +00003481/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003482
3483static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003484dictitems_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003485{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003486 if (dv->dv_dict == NULL) {
3487 Py_RETURN_NONE;
3488 }
3489 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003490}
3491
3492static int
3493dictitems_contains(dictviewobject *dv, PyObject *obj)
3494{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003495 PyObject *key, *value, *found;
3496 if (dv->dv_dict == NULL)
3497 return 0;
3498 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
3499 return 0;
3500 key = PyTuple_GET_ITEM(obj, 0);
3501 value = PyTuple_GET_ITEM(obj, 1);
3502 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
3503 if (found == NULL) {
3504 if (PyErr_Occurred())
3505 return -1;
3506 return 0;
3507 }
3508 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003509}
3510
Guido van Rossum83825ac2007-02-10 04:54:19 +00003511static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003512 (lenfunc)dictview_len, /* sq_length */
3513 0, /* sq_concat */
3514 0, /* sq_repeat */
3515 0, /* sq_item */
3516 0, /* sq_slice */
3517 0, /* sq_ass_item */
3518 0, /* sq_ass_slice */
3519 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003520};
3521
Guido van Rossumb90c8482007-02-10 01:11:45 +00003522static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003523 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3524 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003525 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003526};
3527
3528PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003529 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3530 "dict_items", /* tp_name */
3531 sizeof(dictviewobject), /* tp_basicsize */
3532 0, /* tp_itemsize */
3533 /* methods */
3534 (destructor)dictview_dealloc, /* tp_dealloc */
3535 0, /* tp_print */
3536 0, /* tp_getattr */
3537 0, /* tp_setattr */
3538 0, /* tp_reserved */
3539 (reprfunc)dictview_repr, /* tp_repr */
3540 &dictviews_as_number, /* tp_as_number */
3541 &dictitems_as_sequence, /* tp_as_sequence */
3542 0, /* tp_as_mapping */
3543 0, /* tp_hash */
3544 0, /* tp_call */
3545 0, /* tp_str */
3546 PyObject_GenericGetAttr, /* tp_getattro */
3547 0, /* tp_setattro */
3548 0, /* tp_as_buffer */
3549 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3550 0, /* tp_doc */
3551 (traverseproc)dictview_traverse, /* tp_traverse */
3552 0, /* tp_clear */
3553 dictview_richcompare, /* tp_richcompare */
3554 0, /* tp_weaklistoffset */
3555 (getiterfunc)dictitems_iter, /* tp_iter */
3556 0, /* tp_iternext */
3557 dictitems_methods, /* tp_methods */
3558 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003559};
3560
3561static PyObject *
3562dictitems_new(PyObject *dict)
3563{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003564 return dictview_new(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003565}
3566
Guido van Rossum3ac67412007-02-10 18:55:06 +00003567/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003568
3569static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003570dictvalues_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003571{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003572 if (dv->dv_dict == NULL) {
3573 Py_RETURN_NONE;
3574 }
3575 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003576}
3577
Guido van Rossum83825ac2007-02-10 04:54:19 +00003578static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003579 (lenfunc)dictview_len, /* sq_length */
3580 0, /* sq_concat */
3581 0, /* sq_repeat */
3582 0, /* sq_item */
3583 0, /* sq_slice */
3584 0, /* sq_ass_item */
3585 0, /* sq_ass_slice */
3586 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003587};
3588
Guido van Rossumb90c8482007-02-10 01:11:45 +00003589static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003590 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003591};
3592
3593PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003594 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3595 "dict_values", /* tp_name */
3596 sizeof(dictviewobject), /* tp_basicsize */
3597 0, /* tp_itemsize */
3598 /* methods */
3599 (destructor)dictview_dealloc, /* tp_dealloc */
3600 0, /* tp_print */
3601 0, /* tp_getattr */
3602 0, /* tp_setattr */
3603 0, /* tp_reserved */
3604 (reprfunc)dictview_repr, /* tp_repr */
3605 0, /* tp_as_number */
3606 &dictvalues_as_sequence, /* tp_as_sequence */
3607 0, /* tp_as_mapping */
3608 0, /* tp_hash */
3609 0, /* tp_call */
3610 0, /* tp_str */
3611 PyObject_GenericGetAttr, /* tp_getattro */
3612 0, /* tp_setattro */
3613 0, /* tp_as_buffer */
3614 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3615 0, /* tp_doc */
3616 (traverseproc)dictview_traverse, /* tp_traverse */
3617 0, /* tp_clear */
3618 0, /* tp_richcompare */
3619 0, /* tp_weaklistoffset */
3620 (getiterfunc)dictvalues_iter, /* tp_iter */
3621 0, /* tp_iternext */
3622 dictvalues_methods, /* tp_methods */
3623 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003624};
3625
3626static PyObject *
3627dictvalues_new(PyObject *dict)
3628{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003629 return dictview_new(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003630}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003631
3632/* Returns NULL if cannot allocate a new PyDictKeysObject,
3633 but does not set an error */
3634PyDictKeysObject *
3635_PyDict_NewKeysForClass(void)
3636{
3637 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE_SPLIT);
3638 if (keys == NULL)
3639 PyErr_Clear();
3640 else
3641 keys->dk_lookup = lookdict_split;
3642 return keys;
3643}
3644
3645#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
3646
3647PyObject *
3648PyObject_GenericGetDict(PyObject *obj, void *context)
3649{
3650 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
3651 if (dictptr == NULL) {
3652 PyErr_SetString(PyExc_AttributeError,
3653 "This object has no __dict__");
3654 return NULL;
3655 }
3656 dict = *dictptr;
3657 if (dict == NULL) {
3658 PyTypeObject *tp = Py_TYPE(obj);
3659 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
3660 DK_INCREF(CACHED_KEYS(tp));
3661 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
3662 }
3663 else {
3664 *dictptr = dict = PyDict_New();
3665 }
3666 }
3667 Py_XINCREF(dict);
3668 return dict;
3669}
3670
3671int
3672_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
3673 PyObject *key, PyObject *value)
3674{
3675 PyObject *dict;
3676 int res;
3677 PyDictKeysObject *cached;
3678
3679 assert(dictptr != NULL);
3680 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
3681 assert(dictptr != NULL);
3682 dict = *dictptr;
3683 if (dict == NULL) {
3684 DK_INCREF(cached);
3685 dict = new_dict_with_shared_keys(cached);
3686 if (dict == NULL)
3687 return -1;
3688 *dictptr = dict;
3689 }
3690 if (value == NULL) {
3691 res = PyDict_DelItem(dict, key);
3692 if (cached != ((PyDictObject *)dict)->ma_keys) {
3693 CACHED_KEYS(tp) = NULL;
3694 DK_DECREF(cached);
3695 }
3696 } else {
3697 res = PyDict_SetItem(dict, key, value);
3698 if (cached != ((PyDictObject *)dict)->ma_keys) {
3699 /* Either update tp->ht_cached_keys or delete it */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04003700 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003701 CACHED_KEYS(tp) = make_keys_shared(dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003702 } else {
3703 CACHED_KEYS(tp) = NULL;
3704 }
3705 DK_DECREF(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04003706 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
3707 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003708 }
3709 }
3710 } else {
3711 dict = *dictptr;
3712 if (dict == NULL) {
3713 dict = PyDict_New();
3714 if (dict == NULL)
3715 return -1;
3716 *dictptr = dict;
3717 }
3718 if (value == NULL) {
3719 res = PyDict_DelItem(dict, key);
3720 } else {
3721 res = PyDict_SetItem(dict, key, value);
3722 }
3723 }
3724 return res;
3725}
3726
3727void
3728_PyDictKeys_DecRef(PyDictKeysObject *keys)
3729{
3730 DK_DECREF(keys);
3731}
3732
3733
3734/* ARGSUSED */
3735static PyObject *
3736dummy_repr(PyObject *op)
3737{
3738 return PyUnicode_FromString("<dummy key>");
3739}
3740
3741/* ARGUSED */
3742static void
3743dummy_dealloc(PyObject* ignore)
3744{
3745 /* This should never get called, but we also don't want to SEGV if
3746 * we accidentally decref dummy-key out of existence.
3747 */
3748 Py_FatalError("deallocating <dummy key>");
3749}
3750
3751static PyTypeObject PyDictDummy_Type = {
3752 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3753 "<dummy key> type",
3754 0,
3755 0,
3756 dummy_dealloc, /*tp_dealloc*/ /*never called*/
3757 0, /*tp_print*/
3758 0, /*tp_getattr*/
3759 0, /*tp_setattr*/
3760 0, /*tp_reserved*/
3761 dummy_repr, /*tp_repr*/
3762 0, /*tp_as_number*/
3763 0, /*tp_as_sequence*/
3764 0, /*tp_as_mapping*/
3765 0, /*tp_hash */
3766 0, /*tp_call */
3767 0, /*tp_str */
3768 0, /*tp_getattro */
3769 0, /*tp_setattro */
3770 0, /*tp_as_buffer */
3771 Py_TPFLAGS_DEFAULT, /*tp_flags */
3772};
3773
3774static PyObject _dummy_struct = {
3775 _PyObject_EXTRA_INIT
3776 2, &PyDictDummy_Type
3777};
3778