blob: fd1d46c6c32e785e8e286188be41258b537d8b6b [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
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200264#define DK_DEBUG_INCREF _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA
265#define DK_DEBUG_DECREF _Py_DEC_REFTOTAL _Py_REF_DEBUG_COMMA
266
267#define DK_INCREF(dk) (DK_DEBUG_INCREF ++(dk)->dk_refcnt)
268#define DK_DECREF(dk) if (DK_DEBUG_DECREF (--(dk)->dk_refcnt) == 0) free_keys_object(dk)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400269#define DK_SIZE(dk) ((dk)->dk_size)
270#define DK_MASK(dk) (((dk)->dk_size)-1)
271#define IS_POWER_OF_2(x) (((x) & (x-1)) == 0)
272
273/* USABLE_FRACTION must obey the following:
274 * (0 < USABLE_FRACTION(n) < n) for all n >= 2
275 *
276 * USABLE_FRACTION should be very quick to calculate.
277 * Fractions around 5/8 to 2/3 seem to work well in practice.
278 */
279
280/* Use (2n+1)/3 rather than 2n+3 because: it makes no difference for
281 * combined tables (the two fractions round to the same number n < ),
282 * but 2*4/3 is 2 whereas (2*4+1)/3 is 3 which potentially saves quite
283 * a lot of space for small, split tables */
284#define USABLE_FRACTION(n) ((((n) << 1)+1)/3)
285
286/* Alternative fraction that is otherwise close enough to (2n+1)/3 to make
287 * little difference. 8 * 2/3 == 8 * 5/8 == 5. 16 * 2/3 == 16 * 5/8 == 10.
288 * 32 * 2/3 = 21, 32 * 5/8 = 20.
289 * Its advantage is that it is faster to compute on machines with slow division.
290 * #define USABLE_FRACTION(n) (((n) >> 1) + ((n) >> 2) - ((n) >> 3))
291*/
292
293
294#define ENSURE_ALLOWS_DELETIONS(d) \
295 if ((d)->ma_keys->dk_lookup == lookdict_unicode_nodummy) { \
296 (d)->ma_keys->dk_lookup = lookdict_unicode; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400298
299/* This immutable, empty PyDictKeysObject is used for PyDict_Clear()
300 * (which cannot fail and thus can do no allocation).
301 */
302static PyDictKeysObject empty_keys_struct = {
303 2, /* dk_refcnt 1 for this struct, 1 for dummy_struct */
304 1, /* dk_size */
305 lookdict_split, /* dk_lookup */
306 0, /* dk_usable (immutable) */
307 {
308 { 0, 0, 0 } /* dk_entries (empty) */
309 }
310};
311
312static PyObject *empty_values[1] = { NULL };
313
314#define Py_EMPTY_KEYS &empty_keys_struct
315
316static PyDictKeysObject *new_keys_object(Py_ssize_t size)
317{
318 PyDictKeysObject *dk;
319 Py_ssize_t i;
320 PyDictKeyEntry *ep0;
321
322 assert(size >= PyDict_MINSIZE_SPLIT);
323 assert(IS_POWER_OF_2(size));
324 dk = PyMem_MALLOC(sizeof(PyDictKeysObject) +
325 sizeof(PyDictKeyEntry) * (size-1));
326 if (dk == NULL) {
327 PyErr_NoMemory();
328 return NULL;
329 }
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200330 DK_DEBUG_INCREF dk->dk_refcnt = 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400331 dk->dk_size = size;
332 dk->dk_usable = USABLE_FRACTION(size);
333 ep0 = &dk->dk_entries[0];
334 /* Hash value of slot 0 is used by popitem, so it must be initialized */
335 ep0->me_hash = 0;
336 for (i = 0; i < size; i++) {
337 ep0[i].me_key = NULL;
338 ep0[i].me_value = NULL;
339 }
340 dk->dk_lookup = lookdict_unicode_nodummy;
341 return dk;
342}
343
344static void
345free_keys_object(PyDictKeysObject *keys)
346{
347 PyDictKeyEntry *entries = &keys->dk_entries[0];
348 Py_ssize_t i, n;
349 for (i = 0, n = DK_SIZE(keys); i < n; i++) {
350 Py_XDECREF(entries[i].me_key);
351 Py_XDECREF(entries[i].me_value);
352 }
353 PyMem_FREE(keys);
354}
355
356#define new_values(size) PyMem_NEW(PyObject *, size)
357
358#define free_values(values) PyMem_FREE(values)
359
360/* Consumes a reference to the keys object */
361static PyObject *
362new_dict(PyDictKeysObject *keys, PyObject **values)
363{
364 PyDictObject *mp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 if (numfree) {
366 mp = free_list[--numfree];
367 assert (mp != NULL);
368 assert (Py_TYPE(mp) == &PyDict_Type);
369 _Py_NewReference((PyObject *)mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400371 else {
372 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
373 if (mp == NULL) {
374 DK_DECREF(keys);
375 free_values(values);
376 return NULL;
377 }
378 }
379 mp->ma_keys = keys;
380 mp->ma_values = values;
381 mp->ma_used = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000383}
384
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400385/* Consumes a reference to the keys object */
386static PyObject *
387new_dict_with_shared_keys(PyDictKeysObject *keys)
388{
389 PyObject **values;
390 Py_ssize_t i, size;
391
392 size = DK_SIZE(keys);
393 values = new_values(size);
394 if (values == NULL) {
395 DK_DECREF(keys);
396 return PyErr_NoMemory();
397 }
398 for (i = 0; i < size; i++) {
399 values[i] = NULL;
400 }
401 return new_dict(keys, values);
402}
403
404PyObject *
405PyDict_New(void)
406{
407 return new_dict(new_keys_object(PyDict_MINSIZE_COMBINED), NULL);
408}
409
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000410/*
411The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000412This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000413Open addressing is preferred over chaining since the link overhead for
414chaining would be substantial (100% with typical malloc overhead).
415
Tim Peterseb28ef22001-06-02 05:27:19 +0000416The initial probe index is computed as hash mod the table size. Subsequent
417probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000418
419All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000420
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000421The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000422contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000423Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000424
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000425lookdict() is general-purpose, and may return NULL if (and only if) a
426comparison raises an exception (this was new in Python 2.5).
Guido van Rossum89d8c602007-09-18 17:26:56 +0000427lookdict_unicode() below is specialized to string keys, comparison of which can
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400428never raise an exception; that function can never return NULL.
429lookdict_unicode_nodummy is further specialized for string keys that cannot be
430the <dummy> value.
431For both, when the key isn't found a PyDictEntry* is returned
432where the key would have been found, *value_addr points to the matching value
433slot.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000434*/
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400435static PyDictKeyEntry *
436lookdict(PyDictObject *mp, PyObject *key,
437 Py_hash_t hash, PyObject ***value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000438{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 register size_t i;
440 register size_t perturb;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400441 register PyDictKeyEntry *freeslot;
Antoine Pitrou9a234902012-05-13 20:48:01 +0200442 register size_t mask;
443 PyDictKeyEntry *ep0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400444 register PyDictKeyEntry *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 register int cmp;
446 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000447
Antoine Pitrou9a234902012-05-13 20:48:01 +0200448top:
449 mask = DK_MASK(mp->ma_keys);
450 ep0 = &mp->ma_keys->dk_entries[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 i = (size_t)hash & mask;
452 ep = &ep0[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400453 if (ep->me_key == NULL || ep->me_key == key) {
454 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400456 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 if (ep->me_key == dummy)
458 freeslot = ep;
459 else {
460 if (ep->me_hash == hash) {
461 startkey = ep->me_key;
462 Py_INCREF(startkey);
463 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
464 Py_DECREF(startkey);
465 if (cmp < 0)
466 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400467 if (ep0 == mp->ma_keys->dk_entries && ep->me_key == startkey) {
468 if (cmp > 0) {
469 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400471 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 }
473 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200474 /* The dict was mutated, restart */
475 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 }
477 }
478 freeslot = NULL;
479 }
Tim Peters15d49292001-05-27 07:39:22 +0000480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 /* In the loop, me_key == dummy is by far (factor of 100s) the
482 least likely outcome, so test for that last. */
483 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
484 i = (i << 2) + i + perturb + 1;
485 ep = &ep0[i & mask];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400486 if (ep->me_key == NULL) {
487 if (freeslot == NULL) {
488 *value_addr = &ep->me_value;
489 return ep;
490 } else {
491 *value_addr = &freeslot->me_value;
492 return freeslot;
493 }
494 }
495 if (ep->me_key == key) {
496 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400498 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 if (ep->me_hash == hash && ep->me_key != dummy) {
500 startkey = ep->me_key;
501 Py_INCREF(startkey);
502 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
503 Py_DECREF(startkey);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400504 if (cmp < 0) {
505 *value_addr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400507 }
508 if (ep0 == mp->ma_keys->dk_entries && ep->me_key == startkey) {
509 if (cmp > 0) {
510 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400512 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 }
514 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200515 /* The dict was mutated, restart */
516 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 }
518 }
519 else if (ep->me_key == dummy && freeslot == NULL)
520 freeslot = ep;
521 }
522 assert(0); /* NOT REACHED */
523 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000524}
525
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400526/* Specialized version for string-only keys */
527static PyDictKeyEntry *
528lookdict_unicode(PyDictObject *mp, PyObject *key,
529 Py_hash_t hash, PyObject ***value_addr)
Fred Drake1bff34a2000-08-31 19:31:38 +0000530{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 register size_t i;
532 register size_t perturb;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400533 register PyDictKeyEntry *freeslot;
534 register size_t mask = DK_MASK(mp->ma_keys);
535 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
536 register PyDictKeyEntry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 /* Make sure this function doesn't have to handle non-unicode keys,
539 including subclasses of str; e.g., one reason to subclass
540 unicodes is to override __eq__, and for speed we don't cater to
541 that here. */
542 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400543 mp->ma_keys->dk_lookup = lookdict;
544 return lookdict(mp, key, hash, value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 }
Mark Dickinson57e683e2011-09-24 18:18:40 +0100546 i = (size_t)hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 ep = &ep0[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400548 if (ep->me_key == NULL || ep->me_key == key) {
549 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400551 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 if (ep->me_key == dummy)
553 freeslot = ep;
554 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400555 if (ep->me_hash == hash && unicode_eq(ep->me_key, key)) {
556 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400558 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 freeslot = NULL;
560 }
Tim Peters15d49292001-05-27 07:39:22 +0000561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 /* In the loop, me_key == dummy is by far (factor of 100s) the
563 least likely outcome, so test for that last. */
564 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
565 i = (i << 2) + i + perturb + 1;
566 ep = &ep0[i & mask];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400567 if (ep->me_key == NULL) {
568 if (freeslot == NULL) {
569 *value_addr = &ep->me_value;
570 return ep;
571 } else {
572 *value_addr = &freeslot->me_value;
573 return freeslot;
574 }
575 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 if (ep->me_key == key
577 || (ep->me_hash == hash
578 && ep->me_key != dummy
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400579 && unicode_eq(ep->me_key, key))) {
580 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400582 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 if (ep->me_key == dummy && freeslot == NULL)
584 freeslot = ep;
585 }
586 assert(0); /* NOT REACHED */
587 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000588}
589
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400590/* Faster version of lookdict_unicode when it is known that no <dummy> keys
591 * will be present. */
592static PyDictKeyEntry *
593lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
594 Py_hash_t hash, PyObject ***value_addr)
595{
596 register size_t i;
597 register size_t perturb;
598 register size_t mask = DK_MASK(mp->ma_keys);
599 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
600 register PyDictKeyEntry *ep;
601
602 /* Make sure this function doesn't have to handle non-unicode keys,
603 including subclasses of str; e.g., one reason to subclass
604 unicodes is to override __eq__, and for speed we don't cater to
605 that here. */
606 if (!PyUnicode_CheckExact(key)) {
607 mp->ma_keys->dk_lookup = lookdict;
608 return lookdict(mp, key, hash, value_addr);
609 }
610 i = (size_t)hash & mask;
611 ep = &ep0[i];
612 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
613 if (ep->me_key == NULL || ep->me_key == key ||
614 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
615 *value_addr = &ep->me_value;
616 return ep;
617 }
618 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
619 i = (i << 2) + i + perturb + 1;
620 ep = &ep0[i & mask];
621 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
622 if (ep->me_key == NULL || ep->me_key == key ||
623 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
624 *value_addr = &ep->me_value;
625 return ep;
626 }
627 }
628 assert(0); /* NOT REACHED */
629 return 0;
630}
631
632/* Version of lookdict for split tables.
633 * All split tables and only split tables use this lookup function.
634 * Split tables only contain unicode keys and no dummy keys,
635 * so algorithm is the same as lookdict_unicode_nodummy.
636 */
637static PyDictKeyEntry *
638lookdict_split(PyDictObject *mp, PyObject *key,
639 Py_hash_t hash, PyObject ***value_addr)
640{
641 register size_t i;
642 register size_t perturb;
643 register size_t mask = DK_MASK(mp->ma_keys);
644 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
645 register PyDictKeyEntry *ep;
646
647 if (!PyUnicode_CheckExact(key)) {
Benjamin Petersondb780d02012-04-23 13:44:32 -0400648 ep = lookdict(mp, key, hash, value_addr);
649 /* lookdict expects a combined-table, so fix value_addr */
650 i = ep - ep0;
651 *value_addr = &mp->ma_values[i];
652 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400653 }
654 i = (size_t)hash & mask;
655 ep = &ep0[i];
656 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
657 if (ep->me_key == NULL || ep->me_key == key ||
658 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
659 *value_addr = &mp->ma_values[i];
660 return ep;
661 }
662 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
663 i = (i << 2) + i + perturb + 1;
664 ep = &ep0[i & mask];
665 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
666 if (ep->me_key == NULL || ep->me_key == key ||
667 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
668 *value_addr = &mp->ma_values[i & mask];
669 return ep;
670 }
671 }
672 assert(0); /* NOT REACHED */
673 return 0;
674}
675
Benjamin Petersonfb886362010-04-24 18:21:17 +0000676int
677_PyDict_HasOnlyStringKeys(PyObject *dict)
678{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 Py_ssize_t pos = 0;
680 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000681 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400683 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 return 1;
685 while (PyDict_Next(dict, &pos, &key, &value))
686 if (!PyUnicode_Check(key))
687 return 0;
688 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000689}
690
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000691#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 do { \
693 if (!_PyObject_GC_IS_TRACKED(mp)) { \
694 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
695 _PyObject_GC_MAY_BE_TRACKED(value)) { \
696 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 } \
698 } \
699 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000700
701void
702_PyDict_MaybeUntrack(PyObject *op)
703{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 PyDictObject *mp;
705 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400706 Py_ssize_t i, size;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
709 return;
710
711 mp = (PyDictObject *) op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400712 size = DK_SIZE(mp->ma_keys);
713 if (_PyDict_HasSplitTable(mp)) {
714 for (i = 0; i < size; i++) {
715 if ((value = mp->ma_values[i]) == NULL)
716 continue;
717 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
718 assert(!_PyObject_GC_MAY_BE_TRACKED(
719 mp->ma_keys->dk_entries[i].me_key));
720 return;
721 }
722 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400724 else {
725 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
726 for (i = 0; i < size; i++) {
727 if ((value = ep0[i].me_value) == NULL)
728 continue;
729 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
730 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
731 return;
732 }
733 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000735}
736
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400737/* Internal function to find slot for an item from its hash
738 * when it is known that the key is not present in the dict.
739 */
740static PyDictKeyEntry *
741find_empty_slot(PyDictObject *mp, PyObject *key, Py_hash_t hash,
742 PyObject ***value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000743{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400744 size_t i;
745 size_t perturb;
746 size_t mask = DK_MASK(mp->ma_keys);
747 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
748 PyDictKeyEntry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000749
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400750 assert(key != NULL);
751 if (!PyUnicode_CheckExact(key))
752 mp->ma_keys->dk_lookup = lookdict;
753 i = hash & mask;
754 ep = &ep0[i];
755 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
756 i = (i << 2) + i + perturb + 1;
757 ep = &ep0[i & mask];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400759 assert(ep->me_value == NULL);
760 if (mp->ma_values)
761 *value_addr = &mp->ma_values[i & mask];
762 else
763 *value_addr = &ep->me_value;
764 return ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000765}
766
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400767static int
768insertion_resize(PyDictObject *mp)
769{
770 /*
771 * Double the size of the dict,
772 * Previous versions quadrupled size, but doing so may result in excessive
773 * memory use. Doubling keeps the number of resizes low without wasting
774 * too much memory.
775 */
776 return dictresize(mp, 2 * mp->ma_used);
777}
Antoine Pitroue965d972012-02-27 00:45:12 +0100778
779/*
780Internal routine to insert a new item into the table.
781Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +0100782Returns -1 if an error occurred, or 0 on success.
783*/
784static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400785insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +0100786{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400787 PyObject *old_value;
788 PyObject **value_addr;
789 PyDictKeyEntry *ep;
790 assert(key != dummy);
Antoine Pitroue965d972012-02-27 00:45:12 +0100791
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400792 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
793 if (insertion_resize(mp) < 0)
794 return -1;
795 }
796
797 ep = mp->ma_keys->dk_lookup(mp, key, hash, &value_addr);
Antoine Pitroue965d972012-02-27 00:45:12 +0100798 if (ep == NULL) {
Antoine Pitroue965d972012-02-27 00:45:12 +0100799 return -1;
800 }
Benjamin Petersona6f195e2012-04-30 10:23:40 -0400801 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400802 MAINTAIN_TRACKING(mp, key, value);
803 old_value = *value_addr;
804 if (old_value != NULL) {
805 assert(ep->me_key != NULL && ep->me_key != dummy);
806 *value_addr = value;
807 Py_DECREF(old_value); /* which **CAN** re-enter */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400808 }
809 else {
810 if (ep->me_key == NULL) {
Benjamin Petersona6f195e2012-04-30 10:23:40 -0400811 Py_INCREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400812 if (mp->ma_keys->dk_usable <= 0) {
813 /* Need to resize. */
814 if (insertion_resize(mp) < 0) {
815 Py_DECREF(key);
816 Py_DECREF(value);
817 return -1;
818 }
819 ep = find_empty_slot(mp, key, hash, &value_addr);
820 }
821 mp->ma_keys->dk_usable--;
822 assert(mp->ma_keys->dk_usable >= 0);
823 ep->me_key = key;
824 ep->me_hash = hash;
825 }
826 else {
827 if (ep->me_key == dummy) {
Benjamin Petersona6f195e2012-04-30 10:23:40 -0400828 Py_INCREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400829 ep->me_key = key;
830 ep->me_hash = hash;
831 Py_DECREF(dummy);
832 } else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400833 assert(_PyDict_HasSplitTable(mp));
834 }
835 }
836 mp->ma_used++;
837 *value_addr = value;
838 }
839 assert(ep->me_key != NULL && ep->me_key != dummy);
840 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
841 return 0;
Antoine Pitroue965d972012-02-27 00:45:12 +0100842}
843
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000844/*
845Internal routine used by dictresize() to insert an item which is
846known to be absent from the dict. This routine also assumes that
847the dict contains no deleted entries. Besides the performance benefit,
848using insertdict() in dictresize() is dangerous (SF bug #1456209).
849Note that no refcounts are changed by this routine; if needed, the caller
850is responsible for incref'ing `key` and `value`.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400851Neither mp->ma_used nor k->dk_usable are modified by this routine; the caller
852must set them correctly
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000853*/
854static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400855insertdict_clean(PyDictObject *mp, PyObject *key, Py_hash_t hash,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 PyObject *value)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000857{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400858 size_t i;
859 size_t perturb;
860 PyDictKeysObject *k = mp->ma_keys;
861 size_t mask = (size_t)DK_SIZE(k)-1;
862 PyDictKeyEntry *ep0 = &k->dk_entries[0];
863 PyDictKeyEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000864
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400865 assert(k->dk_lookup != NULL);
866 assert(value != NULL);
867 assert(key != NULL);
868 assert(key != dummy);
869 assert(PyUnicode_CheckExact(key) || k->dk_lookup == lookdict);
870 i = hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 ep = &ep0[i];
872 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
873 i = (i << 2) + i + perturb + 1;
874 ep = &ep0[i & mask];
875 }
876 assert(ep->me_value == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 ep->me_key = key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000878 ep->me_hash = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000880}
881
882/*
883Restructure the table by allocating a new table and reinserting all
884items again. When entries have been deleted, the new table may
885actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400886If a table is split (its keys and hashes are shared, its values are not),
887then the values are temporarily copied into the table, it is resized as
888a combined table, then the me_value slots in the old table are NULLed out.
889After resizing a table is always combined,
890but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000891*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000892static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000893dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000894{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 Py_ssize_t newsize;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400896 PyDictKeysObject *oldkeys;
897 PyObject **oldvalues;
898 Py_ssize_t i, oldsize;
Tim Peters91a364d2001-05-19 07:04:38 +0000899
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400900/* Find the smallest table size > minused. */
901 for (newsize = PyDict_MINSIZE_COMBINED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 newsize <= minused && newsize > 0;
903 newsize <<= 1)
904 ;
905 if (newsize <= 0) {
906 PyErr_NoMemory();
907 return -1;
908 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400909 oldkeys = mp->ma_keys;
910 oldvalues = mp->ma_values;
911 /* Allocate a new table. */
912 mp->ma_keys = new_keys_object(newsize);
913 if (mp->ma_keys == NULL) {
914 mp->ma_keys = oldkeys;
915 return -1;
916 }
917 if (oldkeys->dk_lookup == lookdict)
918 mp->ma_keys->dk_lookup = lookdict;
919 oldsize = DK_SIZE(oldkeys);
920 mp->ma_values = NULL;
921 /* If empty then nothing to copy so just return */
922 if (oldsize == 1) {
923 assert(oldkeys == Py_EMPTY_KEYS);
924 DK_DECREF(oldkeys);
925 return 0;
926 }
927 /* Main loop below assumes we can transfer refcount to new keys
928 * and that value is stored in me_value.
929 * Increment ref-counts and copy values here to compensate
930 * This (resizing a split table) should be relatively rare */
931 if (oldvalues != NULL) {
932 for (i = 0; i < oldsize; i++) {
933 if (oldvalues[i] != NULL) {
934 Py_INCREF(oldkeys->dk_entries[i].me_key);
935 oldkeys->dk_entries[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 }
938 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400939 /* Main loop */
940 for (i = 0; i < oldsize; i++) {
941 PyDictKeyEntry *ep = &oldkeys->dk_entries[i];
942 if (ep->me_value != NULL) {
943 assert(ep->me_key != dummy);
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000944 insertdict_clean(mp, ep->me_key, ep->me_hash, ep->me_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400947 mp->ma_keys->dk_usable -= mp->ma_used;
948 if (oldvalues != NULL) {
949 /* NULL out me_value slot in oldkeys, in case it was shared */
950 for (i = 0; i < oldsize; i++)
951 oldkeys->dk_entries[i].me_value = NULL;
952 assert(oldvalues != empty_values);
953 free_values(oldvalues);
954 DK_DECREF(oldkeys);
955 }
956 else {
957 assert(oldkeys->dk_lookup != lookdict_split);
958 if (oldkeys->dk_lookup != lookdict_unicode_nodummy) {
959 PyDictKeyEntry *ep0 = &oldkeys->dk_entries[0];
960 for (i = 0; i < oldsize; i++) {
961 if (ep0[i].me_key == dummy)
962 Py_DECREF(dummy);
963 }
964 }
965 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200966 DK_DEBUG_DECREF PyMem_FREE(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400967 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000969}
970
Benjamin Peterson15ee8212012-04-24 14:44:18 -0400971/* Returns NULL if unable to split table.
972 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400973static PyDictKeysObject *
974make_keys_shared(PyObject *op)
975{
976 Py_ssize_t i;
977 Py_ssize_t size;
978 PyDictObject *mp = (PyDictObject *)op;
979
Benjamin Peterson15ee8212012-04-24 14:44:18 -0400980 if (!PyDict_CheckExact(op))
981 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400982 if (!_PyDict_HasSplitTable(mp)) {
983 PyDictKeyEntry *ep0;
984 PyObject **values;
985 assert(mp->ma_keys->dk_refcnt == 1);
986 if (mp->ma_keys->dk_lookup == lookdict) {
987 return NULL;
988 }
989 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
990 /* Remove dummy keys */
991 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
992 return NULL;
993 }
994 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
995 /* Copy values into a new array */
996 ep0 = &mp->ma_keys->dk_entries[0];
997 size = DK_SIZE(mp->ma_keys);
998 values = new_values(size);
999 if (values == NULL) {
1000 PyErr_SetString(PyExc_MemoryError,
1001 "Not enough memory to allocate new values array");
1002 return NULL;
1003 }
1004 for (i = 0; i < size; i++) {
1005 values[i] = ep0[i].me_value;
1006 ep0[i].me_value = NULL;
1007 }
1008 mp->ma_keys->dk_lookup = lookdict_split;
1009 mp->ma_values = values;
1010 }
1011 DK_INCREF(mp->ma_keys);
1012 return mp->ma_keys;
1013}
Christian Heimes99170a52007-12-19 02:07:34 +00001014
1015PyObject *
1016_PyDict_NewPresized(Py_ssize_t minused)
1017{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001018 Py_ssize_t newsize;
1019 PyDictKeysObject *new_keys;
1020 for (newsize = PyDict_MINSIZE_COMBINED;
1021 newsize <= minused && newsize > 0;
1022 newsize <<= 1)
1023 ;
1024 new_keys = new_keys_object(newsize);
1025 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001027 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001028}
1029
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001030/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1031 * that may occur (originally dicts supported only string keys, and exceptions
1032 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001033 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001034 * (suppressed) occurred while computing the key's hash, or that some error
1035 * (suppressed) occurred when comparing keys in the dict's internal probe
1036 * sequence. A nasty example of the latter is when a Python-coded comparison
1037 * function hits a stack-depth error, which can cause this to return NULL
1038 * even if the key is present.
1039 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001040PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001041PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001042{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001043 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001045 PyDictKeyEntry *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 PyThreadState *tstate;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001047 PyObject **value_addr;
1048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 if (!PyDict_Check(op))
1050 return NULL;
1051 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001052 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 {
1054 hash = PyObject_Hash(key);
1055 if (hash == -1) {
1056 PyErr_Clear();
1057 return NULL;
1058 }
1059 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 /* We can arrive here with a NULL tstate during initialization: try
1062 running "python -Wi" for an example related to string interning.
1063 Let's just hope that no exception occurs then... This must be
1064 _PyThreadState_Current and not PyThreadState_GET() because in debug
1065 mode, the latter complains if tstate is NULL. */
1066 tstate = (PyThreadState*)_Py_atomic_load_relaxed(
1067 &_PyThreadState_Current);
1068 if (tstate != NULL && tstate->curexc_type != NULL) {
1069 /* preserve the existing exception */
1070 PyObject *err_type, *err_value, *err_tb;
1071 PyErr_Fetch(&err_type, &err_value, &err_tb);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001072 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 /* ignore errors */
1074 PyErr_Restore(err_type, err_value, err_tb);
1075 if (ep == NULL)
1076 return NULL;
1077 }
1078 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001079 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 if (ep == NULL) {
1081 PyErr_Clear();
1082 return NULL;
1083 }
1084 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001085 return *value_addr;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001086}
1087
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001088/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1089 This returns NULL *with* an exception set if an exception occurred.
1090 It returns NULL *without* an exception set if the key wasn't present.
1091*/
1092PyObject *
1093PyDict_GetItemWithError(PyObject *op, PyObject *key)
1094{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001095 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 PyDictObject*mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001097 PyDictKeyEntry *ep;
1098 PyObject **value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 if (!PyDict_Check(op)) {
1101 PyErr_BadInternalCall();
1102 return NULL;
1103 }
1104 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001105 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 {
1107 hash = PyObject_Hash(key);
1108 if (hash == -1) {
1109 return NULL;
1110 }
1111 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001112
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001113 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 if (ep == NULL)
1115 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001116 return *value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001117}
1118
Brett Cannonfd074152012-04-14 14:10:13 -04001119PyObject *
1120_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1121{
1122 PyObject *kv;
1123 kv = _PyUnicode_FromId(key); /* borrowed */
1124 if (kv == NULL)
1125 return NULL;
1126 return PyDict_GetItemWithError(dp, kv);
1127}
1128
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001129/* Fast version of global value lookup.
1130 * Lookup in globals, then builtins.
1131 */
1132PyObject *
1133_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001134{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001135 PyObject *x;
1136 if (PyUnicode_CheckExact(key)) {
1137 PyObject **value_addr;
1138 Py_hash_t hash = ((PyASCIIObject *)key)->hash;
1139 if (hash != -1) {
1140 PyDictKeyEntry *e;
1141 e = globals->ma_keys->dk_lookup(globals, key, hash, &value_addr);
1142 if (e == NULL) {
1143 return NULL;
1144 }
1145 x = *value_addr;
1146 if (x != NULL)
1147 return x;
1148 e = builtins->ma_keys->dk_lookup(builtins, key, hash, &value_addr);
1149 if (e == NULL) {
1150 return NULL;
1151 }
1152 x = *value_addr;
1153 return x;
1154 }
Antoine Pitroue965d972012-02-27 00:45:12 +01001155 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001156 x = PyDict_GetItemWithError((PyObject *)globals, key);
1157 if (x != NULL)
1158 return x;
1159 if (PyErr_Occurred())
1160 return NULL;
1161 return PyDict_GetItemWithError((PyObject *)builtins, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001162}
1163
Antoine Pitroue965d972012-02-27 00:45:12 +01001164/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1165 * dictionary if it's merely replacing the value for an existing key.
1166 * This means that it's safe to loop over a dictionary with PyDict_Next()
1167 * and occasionally replace a value -- but you can't insert new keys or
1168 * remove them.
1169 */
1170int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001171PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001172{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001173 PyDictObject *mp;
1174 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001175 if (!PyDict_Check(op)) {
1176 PyErr_BadInternalCall();
1177 return -1;
1178 }
1179 assert(key);
1180 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001181 mp = (PyDictObject *)op;
1182 if (!PyUnicode_CheckExact(key) ||
1183 (hash = ((PyASCIIObject *) key)->hash) == -1)
1184 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001185 hash = PyObject_Hash(key);
1186 if (hash == -1)
1187 return -1;
1188 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001189
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);
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001266 DK_DECREF(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001267 }
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 {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001374 assert(keys->dk_refcnt == 1);
1375 DK_DECREF(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001376 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1378 free_list[numfree++] = mp;
1379 else
1380 Py_TYPE(mp)->tp_free((PyObject *)mp);
1381 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001382}
1383
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001384
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001385static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001386dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001387{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 Py_ssize_t i;
1389 PyObject *s, *temp, *colon = NULL;
1390 PyObject *pieces = NULL, *result = NULL;
1391 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +00001392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 i = Py_ReprEnter((PyObject *)mp);
1394 if (i != 0) {
1395 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
1396 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 if (mp->ma_used == 0) {
1399 result = PyUnicode_FromString("{}");
1400 goto Done;
1401 }
Tim Petersa7259592001-06-16 05:11:17 +00001402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 pieces = PyList_New(0);
1404 if (pieces == NULL)
1405 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 colon = PyUnicode_FromString(": ");
1408 if (colon == NULL)
1409 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 /* Do repr() on each key+value pair, and insert ": " between them.
1412 Note that repr may mutate the dict. */
1413 i = 0;
1414 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
1415 int status;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001416 /* Prevent repr from deleting key or value during key format. */
1417 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 Py_INCREF(value);
1419 s = PyObject_Repr(key);
1420 PyUnicode_Append(&s, colon);
1421 PyUnicode_AppendAndDel(&s, PyObject_Repr(value));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001422 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 Py_DECREF(value);
1424 if (s == NULL)
1425 goto Done;
1426 status = PyList_Append(pieces, s);
1427 Py_DECREF(s); /* append created a new ref */
1428 if (status < 0)
1429 goto Done;
1430 }
Tim Petersa7259592001-06-16 05:11:17 +00001431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 /* Add "{}" decorations to the first and last items. */
1433 assert(PyList_GET_SIZE(pieces) > 0);
1434 s = PyUnicode_FromString("{");
1435 if (s == NULL)
1436 goto Done;
1437 temp = PyList_GET_ITEM(pieces, 0);
1438 PyUnicode_AppendAndDel(&s, temp);
1439 PyList_SET_ITEM(pieces, 0, s);
1440 if (s == NULL)
1441 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 s = PyUnicode_FromString("}");
1444 if (s == NULL)
1445 goto Done;
1446 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
1447 PyUnicode_AppendAndDel(&temp, s);
1448 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
1449 if (temp == NULL)
1450 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 /* Paste them all together with ", " between. */
1453 s = PyUnicode_FromString(", ");
1454 if (s == NULL)
1455 goto Done;
1456 result = PyUnicode_Join(s, pieces);
1457 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +00001458
1459Done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 Py_XDECREF(pieces);
1461 Py_XDECREF(colon);
1462 Py_ReprLeave((PyObject *)mp);
1463 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001464}
1465
Martin v. Löwis18e16552006-02-15 17:27:45 +00001466static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001467dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001468{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001470}
1471
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001472static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001473dict_subscript(PyDictObject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001474{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475 PyObject *v;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001476 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001477 PyDictKeyEntry *ep;
1478 PyObject **value_addr;
1479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001481 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 hash = PyObject_Hash(key);
1483 if (hash == -1)
1484 return NULL;
1485 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001486 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 if (ep == NULL)
1488 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001489 v = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 if (v == NULL) {
1491 if (!PyDict_CheckExact(mp)) {
1492 /* Look up __missing__ method if we're a subclass. */
1493 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05001494 _Py_IDENTIFIER(__missing__);
1495 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 if (missing != NULL) {
1497 res = PyObject_CallFunctionObjArgs(missing,
1498 key, NULL);
1499 Py_DECREF(missing);
1500 return res;
1501 }
1502 else if (PyErr_Occurred())
1503 return NULL;
1504 }
1505 set_key_error(key);
1506 return NULL;
1507 }
1508 else
1509 Py_INCREF(v);
1510 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001511}
1512
1513static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001514dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001515{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516 if (w == NULL)
1517 return PyDict_DelItem((PyObject *)mp, v);
1518 else
1519 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001520}
1521
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001522static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001523 (lenfunc)dict_length, /*mp_length*/
1524 (binaryfunc)dict_subscript, /*mp_subscript*/
1525 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001526};
1527
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001528static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001529dict_keys(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001530{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001531 register PyObject *v;
1532 register Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001533 PyDictKeyEntry *ep;
1534 Py_ssize_t size, n, offset;
1535 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001536
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001537 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 n = mp->ma_used;
1539 v = PyList_New(n);
1540 if (v == NULL)
1541 return NULL;
1542 if (n != mp->ma_used) {
1543 /* Durnit. The allocations caused the dict to resize.
1544 * Just start over, this shouldn't normally happen.
1545 */
1546 Py_DECREF(v);
1547 goto again;
1548 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001549 ep = &mp->ma_keys->dk_entries[0];
1550 size = DK_SIZE(mp->ma_keys);
1551 if (mp->ma_values) {
1552 value_ptr = mp->ma_values;
1553 offset = sizeof(PyObject *);
1554 }
1555 else {
1556 value_ptr = &ep[0].me_value;
1557 offset = sizeof(PyDictKeyEntry);
1558 }
1559 for (i = 0, j = 0; i < size; i++) {
1560 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 PyObject *key = ep[i].me_key;
1562 Py_INCREF(key);
1563 PyList_SET_ITEM(v, j, key);
1564 j++;
1565 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001566 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 }
1568 assert(j == n);
1569 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001570}
1571
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001572static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001573dict_values(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001574{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001575 register PyObject *v;
1576 register Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001577 Py_ssize_t size, n, offset;
1578 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001579
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001580 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 n = mp->ma_used;
1582 v = PyList_New(n);
1583 if (v == NULL)
1584 return NULL;
1585 if (n != mp->ma_used) {
1586 /* Durnit. The allocations caused the dict to resize.
1587 * Just start over, this shouldn't normally happen.
1588 */
1589 Py_DECREF(v);
1590 goto again;
1591 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001592 size = DK_SIZE(mp->ma_keys);
1593 if (mp->ma_values) {
1594 value_ptr = mp->ma_values;
1595 offset = sizeof(PyObject *);
1596 }
1597 else {
1598 value_ptr = &mp->ma_keys->dk_entries[0].me_value;
1599 offset = sizeof(PyDictKeyEntry);
1600 }
1601 for (i = 0, j = 0; i < size; i++) {
1602 PyObject *value = *value_ptr;
1603 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1604 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 Py_INCREF(value);
1606 PyList_SET_ITEM(v, j, value);
1607 j++;
1608 }
1609 }
1610 assert(j == n);
1611 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001612}
1613
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001614static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001615dict_items(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001616{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 register PyObject *v;
1618 register Py_ssize_t i, j, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001619 Py_ssize_t size, offset;
1620 PyObject *item, *key;
1621 PyDictKeyEntry *ep;
1622 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 /* Preallocate the list of tuples, to avoid allocations during
1625 * the loop over the items, which could trigger GC, which
1626 * could resize the dict. :-(
1627 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001628 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001629 n = mp->ma_used;
1630 v = PyList_New(n);
1631 if (v == NULL)
1632 return NULL;
1633 for (i = 0; i < n; i++) {
1634 item = PyTuple_New(2);
1635 if (item == NULL) {
1636 Py_DECREF(v);
1637 return NULL;
1638 }
1639 PyList_SET_ITEM(v, i, item);
1640 }
1641 if (n != mp->ma_used) {
1642 /* Durnit. The allocations caused the dict to resize.
1643 * Just start over, this shouldn't normally happen.
1644 */
1645 Py_DECREF(v);
1646 goto again;
1647 }
1648 /* Nothing we do below makes any function calls. */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001649 ep = mp->ma_keys->dk_entries;
1650 size = DK_SIZE(mp->ma_keys);
1651 if (mp->ma_values) {
1652 value_ptr = mp->ma_values;
1653 offset = sizeof(PyObject *);
1654 }
1655 else {
1656 value_ptr = &ep[0].me_value;
1657 offset = sizeof(PyDictKeyEntry);
1658 }
1659 for (i = 0, j = 0; i < size; i++) {
1660 PyObject *value = *value_ptr;
1661 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1662 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 key = ep[i].me_key;
1664 item = PyList_GET_ITEM(v, j);
1665 Py_INCREF(key);
1666 PyTuple_SET_ITEM(item, 0, key);
1667 Py_INCREF(value);
1668 PyTuple_SET_ITEM(item, 1, value);
1669 j++;
1670 }
1671 }
1672 assert(j == n);
1673 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001674}
1675
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001676static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001677dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001678{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 PyObject *seq;
1680 PyObject *value = Py_None;
1681 PyObject *it; /* iter(seq) */
1682 PyObject *key;
1683 PyObject *d;
1684 int status;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001685
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
1687 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001689 d = PyObject_CallObject(cls, NULL);
1690 if (d == NULL)
1691 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 if (PyDict_CheckExact(d) && PyDict_CheckExact(seq)) {
1694 PyDictObject *mp = (PyDictObject *)d;
1695 PyObject *oldvalue;
1696 Py_ssize_t pos = 0;
1697 PyObject *key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001698 Py_hash_t hash;
Guido van Rossum58da9312007-11-10 23:39:45 +00001699
Petri Lehtinena94200e2011-10-24 21:12:58 +03001700 if (dictresize(mp, Py_SIZE(seq))) {
1701 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001703 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
Petri Lehtinena94200e2011-10-24 21:12:58 +03001706 if (insertdict(mp, key, hash, value)) {
1707 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001709 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001710 }
1711 return d;
1712 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) {
1715 PyDictObject *mp = (PyDictObject *)d;
1716 Py_ssize_t pos = 0;
1717 PyObject *key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001718 Py_hash_t hash;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001719
Petri Lehtinena94200e2011-10-24 21:12:58 +03001720 if (dictresize(mp, PySet_GET_SIZE(seq))) {
1721 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001723 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001724
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
Petri Lehtinena94200e2011-10-24 21:12:58 +03001726 if (insertdict(mp, key, hash, value)) {
1727 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001728 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001729 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001730 }
1731 return d;
1732 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001734 it = PyObject_GetIter(seq);
1735 if (it == NULL){
1736 Py_DECREF(d);
1737 return NULL;
1738 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001739
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001740 if (PyDict_CheckExact(d)) {
1741 while ((key = PyIter_Next(it)) != NULL) {
1742 status = PyDict_SetItem(d, key, value);
1743 Py_DECREF(key);
1744 if (status < 0)
1745 goto Fail;
1746 }
1747 } else {
1748 while ((key = PyIter_Next(it)) != NULL) {
1749 status = PyObject_SetItem(d, key, value);
1750 Py_DECREF(key);
1751 if (status < 0)
1752 goto Fail;
1753 }
1754 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001756 if (PyErr_Occurred())
1757 goto Fail;
1758 Py_DECREF(it);
1759 return d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001760
1761Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 Py_DECREF(it);
1763 Py_DECREF(d);
1764 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001765}
1766
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001767static int
1768dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001769{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 PyObject *arg = NULL;
1771 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1774 result = -1;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001777 _Py_IDENTIFIER(keys);
1778 if (_PyObject_HasAttrId(arg, &PyId_keys))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779 result = PyDict_Merge(self, arg, 1);
1780 else
1781 result = PyDict_MergeFromSeq2(self, arg, 1);
1782 }
1783 if (result == 0 && kwds != NULL) {
1784 if (PyArg_ValidateKeywordArguments(kwds))
1785 result = PyDict_Merge(self, kwds, 1);
1786 else
1787 result = -1;
1788 }
1789 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001790}
1791
1792static PyObject *
1793dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1794{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001795 if (dict_update_common(self, args, kwds, "update") != -1)
1796 Py_RETURN_NONE;
1797 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001798}
1799
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001800/* Update unconditionally replaces existing items.
1801 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001802 otherwise it leaves existing items unchanged.
1803
1804 PyDict_{Update,Merge} update/merge from a mapping object.
1805
Tim Petersf582b822001-12-11 18:51:08 +00001806 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001807 producing iterable objects of length 2.
1808*/
1809
Tim Petersf582b822001-12-11 18:51:08 +00001810int
Tim Peters1fc240e2001-10-26 05:06:50 +00001811PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1812{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001813 PyObject *it; /* iter(seq2) */
1814 Py_ssize_t i; /* index into seq2 of current element */
1815 PyObject *item; /* seq2[i] */
1816 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00001817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 assert(d != NULL);
1819 assert(PyDict_Check(d));
1820 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00001821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 it = PyObject_GetIter(seq2);
1823 if (it == NULL)
1824 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001826 for (i = 0; ; ++i) {
1827 PyObject *key, *value;
1828 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 fast = NULL;
1831 item = PyIter_Next(it);
1832 if (item == NULL) {
1833 if (PyErr_Occurred())
1834 goto Fail;
1835 break;
1836 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001838 /* Convert item to sequence, and verify length 2. */
1839 fast = PySequence_Fast(item, "");
1840 if (fast == NULL) {
1841 if (PyErr_ExceptionMatches(PyExc_TypeError))
1842 PyErr_Format(PyExc_TypeError,
1843 "cannot convert dictionary update "
1844 "sequence element #%zd to a sequence",
1845 i);
1846 goto Fail;
1847 }
1848 n = PySequence_Fast_GET_SIZE(fast);
1849 if (n != 2) {
1850 PyErr_Format(PyExc_ValueError,
1851 "dictionary update sequence element #%zd "
1852 "has length %zd; 2 is required",
1853 i, n);
1854 goto Fail;
1855 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 /* Update/merge with this (key, value) pair. */
1858 key = PySequence_Fast_GET_ITEM(fast, 0);
1859 value = PySequence_Fast_GET_ITEM(fast, 1);
1860 if (override || PyDict_GetItem(d, key) == NULL) {
1861 int status = PyDict_SetItem(d, key, value);
1862 if (status < 0)
1863 goto Fail;
1864 }
1865 Py_DECREF(fast);
1866 Py_DECREF(item);
1867 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001868
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001869 i = 0;
1870 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00001871Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 Py_XDECREF(item);
1873 Py_XDECREF(fast);
1874 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001875Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 Py_DECREF(it);
1877 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001878}
1879
Tim Peters6d6c1a32001-08-02 04:15:00 +00001880int
1881PyDict_Update(PyObject *a, PyObject *b)
1882{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001883 return PyDict_Merge(a, b, 1);
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001884}
1885
1886int
1887PyDict_Merge(PyObject *a, PyObject *b, int override)
1888{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889 register PyDictObject *mp, *other;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001890 register Py_ssize_t i, n;
1891 PyDictKeyEntry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893 /* We accept for the argument either a concrete dictionary object,
1894 * or an abstract "mapping" object. For the former, we can do
1895 * things quite efficiently. For the latter, we only require that
1896 * PyMapping_Keys() and PyObject_GetItem() be supported.
1897 */
1898 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1899 PyErr_BadInternalCall();
1900 return -1;
1901 }
1902 mp = (PyDictObject*)a;
1903 if (PyDict_Check(b)) {
1904 other = (PyDictObject*)b;
1905 if (other == mp || other->ma_used == 0)
1906 /* a.update(a) or a.update({}); nothing to do */
1907 return 0;
1908 if (mp->ma_used == 0)
1909 /* Since the target dict is empty, PyDict_GetItem()
1910 * always returns NULL. Setting override to 1
1911 * skips the unnecessary test.
1912 */
1913 override = 1;
1914 /* Do one big resize at the start, rather than
1915 * incrementally resizing as we insert new items. Expect
1916 * that there will be no (or few) overlapping keys.
1917 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001918 if (mp->ma_keys->dk_usable * 3 < other->ma_used * 2)
1919 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001921 for (i = 0, n = DK_SIZE(other->ma_keys); i < n; i++) {
1922 PyObject *value;
1923 entry = &other->ma_keys->dk_entries[i];
1924 if (other->ma_values)
1925 value = other->ma_values[i];
1926 else
1927 value = entry->me_value;
1928
1929 if (value != NULL &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 (override ||
1931 PyDict_GetItem(a, entry->me_key) == NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932 if (insertdict(mp, entry->me_key,
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001933 entry->me_hash,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001934 value) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001935 return -1;
1936 }
1937 }
1938 }
1939 else {
1940 /* Do it the generic, slower way */
1941 PyObject *keys = PyMapping_Keys(b);
1942 PyObject *iter;
1943 PyObject *key, *value;
1944 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001946 if (keys == NULL)
1947 /* Docstring says this is equivalent to E.keys() so
1948 * if E doesn't have a .keys() method we want
1949 * AttributeError to percolate up. Might as well
1950 * do the same for any other error.
1951 */
1952 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001954 iter = PyObject_GetIter(keys);
1955 Py_DECREF(keys);
1956 if (iter == NULL)
1957 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001958
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
1960 if (!override && PyDict_GetItem(a, key) != NULL) {
1961 Py_DECREF(key);
1962 continue;
1963 }
1964 value = PyObject_GetItem(b, key);
1965 if (value == NULL) {
1966 Py_DECREF(iter);
1967 Py_DECREF(key);
1968 return -1;
1969 }
1970 status = PyDict_SetItem(a, key, value);
1971 Py_DECREF(key);
1972 Py_DECREF(value);
1973 if (status < 0) {
1974 Py_DECREF(iter);
1975 return -1;
1976 }
1977 }
1978 Py_DECREF(iter);
1979 if (PyErr_Occurred())
1980 /* Iterator completed, via error */
1981 return -1;
1982 }
1983 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001984}
1985
1986static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001987dict_copy(register PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001988{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001990}
1991
1992PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001993PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001994{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001995 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001996 PyDictObject *mp;
1997 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001999 if (o == NULL || !PyDict_Check(o)) {
2000 PyErr_BadInternalCall();
2001 return NULL;
2002 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002003 mp = (PyDictObject *)o;
2004 if (_PyDict_HasSplitTable(mp)) {
2005 PyDictObject *split_copy;
2006 PyObject **newvalues = new_values(DK_SIZE(mp->ma_keys));
2007 if (newvalues == NULL)
2008 return PyErr_NoMemory();
2009 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2010 if (split_copy == NULL) {
2011 free_values(newvalues);
2012 return NULL;
2013 }
2014 split_copy->ma_values = newvalues;
2015 split_copy->ma_keys = mp->ma_keys;
2016 split_copy->ma_used = mp->ma_used;
2017 DK_INCREF(mp->ma_keys);
2018 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2019 PyObject *value = mp->ma_values[i];
2020 Py_XINCREF(value);
2021 split_copy->ma_values[i] = value;
2022 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002023 if (_PyObject_GC_IS_TRACKED(mp))
2024 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002025 return (PyObject *)split_copy;
2026 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027 copy = PyDict_New();
2028 if (copy == NULL)
2029 return NULL;
2030 if (PyDict_Merge(copy, o, 1) == 0)
2031 return copy;
2032 Py_DECREF(copy);
2033 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002034}
2035
Martin v. Löwis18e16552006-02-15 17:27:45 +00002036Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002037PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002038{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002039 if (mp == NULL || !PyDict_Check(mp)) {
2040 PyErr_BadInternalCall();
2041 return -1;
2042 }
2043 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002044}
2045
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002046PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002047PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002048{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002049 if (mp == NULL || !PyDict_Check(mp)) {
2050 PyErr_BadInternalCall();
2051 return NULL;
2052 }
2053 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002054}
2055
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002056PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002057PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002058{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059 if (mp == NULL || !PyDict_Check(mp)) {
2060 PyErr_BadInternalCall();
2061 return NULL;
2062 }
2063 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002064}
2065
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002066PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002067PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002068{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 if (mp == NULL || !PyDict_Check(mp)) {
2070 PyErr_BadInternalCall();
2071 return NULL;
2072 }
2073 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002074}
2075
Tim Peterse63415e2001-05-08 04:38:29 +00002076/* Return 1 if dicts equal, 0 if not, -1 if error.
2077 * Gets out as soon as any difference is detected.
2078 * Uses only Py_EQ comparison.
2079 */
2080static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002081dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002082{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002083 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002085 if (a->ma_used != b->ma_used)
2086 /* can't be equal if # of entries differ */
2087 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002088 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002089 for (i = 0; i < DK_SIZE(a->ma_keys); i++) {
2090 PyDictKeyEntry *ep = &a->ma_keys->dk_entries[i];
2091 PyObject *aval;
2092 if (a->ma_values)
2093 aval = a->ma_values[i];
2094 else
2095 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002096 if (aval != NULL) {
2097 int cmp;
2098 PyObject *bval;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002099 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002100 /* temporarily bump aval's refcount to ensure it stays
2101 alive until we're done with it */
2102 Py_INCREF(aval);
2103 /* ditto for key */
2104 Py_INCREF(key);
2105 bval = PyDict_GetItemWithError((PyObject *)b, key);
2106 Py_DECREF(key);
2107 if (bval == NULL) {
2108 Py_DECREF(aval);
2109 if (PyErr_Occurred())
2110 return -1;
2111 return 0;
2112 }
2113 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
2114 Py_DECREF(aval);
2115 if (cmp <= 0) /* error or not equal */
2116 return cmp;
2117 }
2118 }
2119 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002120}
Tim Peterse63415e2001-05-08 04:38:29 +00002121
2122static PyObject *
2123dict_richcompare(PyObject *v, PyObject *w, int op)
2124{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002125 int cmp;
2126 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002128 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2129 res = Py_NotImplemented;
2130 }
2131 else if (op == Py_EQ || op == Py_NE) {
2132 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2133 if (cmp < 0)
2134 return NULL;
2135 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2136 }
2137 else
2138 res = Py_NotImplemented;
2139 Py_INCREF(res);
2140 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002141}
Tim Peterse63415e2001-05-08 04:38:29 +00002142
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002143static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002144dict_contains(register PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002145{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002146 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002147 PyDictKeyEntry *ep;
2148 PyObject **value_addr;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002150 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002151 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002152 hash = PyObject_Hash(key);
2153 if (hash == -1)
2154 return NULL;
2155 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002156 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002157 if (ep == NULL)
2158 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002159 return PyBool_FromLong(*value_addr != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002160}
2161
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002162static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002163dict_get(register PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00002164{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002165 PyObject *key;
2166 PyObject *failobj = Py_None;
2167 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002168 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002169 PyDictKeyEntry *ep;
2170 PyObject **value_addr;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002172 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
2173 return NULL;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002176 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002177 hash = PyObject_Hash(key);
2178 if (hash == -1)
2179 return NULL;
2180 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002181 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002182 if (ep == NULL)
2183 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002184 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002185 if (val == NULL)
2186 val = failobj;
2187 Py_INCREF(val);
2188 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002189}
2190
Barry Warsawc38c5da1997-10-06 17:49:20 +00002191static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002192dict_setdefault(register PyDictObject *mp, PyObject *args)
Guido van Rossum164452c2000-08-08 16:12:54 +00002193{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002194 PyObject *key;
2195 PyObject *failobj = Py_None;
2196 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002197 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002198 PyDictKeyEntry *ep;
2199 PyObject **value_addr;
Guido van Rossum164452c2000-08-08 16:12:54 +00002200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002201 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
2202 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00002203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002204 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002205 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002206 hash = PyObject_Hash(key);
2207 if (hash == -1)
2208 return NULL;
2209 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002210 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 if (ep == NULL)
2212 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002213 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002214 if (val == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002215 Py_INCREF(failobj);
2216 Py_INCREF(key);
2217 if (mp->ma_keys->dk_usable <= 0) {
2218 /* Need to resize. */
2219 if (insertion_resize(mp) < 0)
2220 return NULL;
2221 ep = find_empty_slot(mp, key, hash, &value_addr);
2222 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002223 MAINTAIN_TRACKING(mp, key, failobj);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002224 ep->me_key = key;
2225 ep->me_hash = hash;
2226 *value_addr = failobj;
2227 val = failobj;
2228 mp->ma_keys->dk_usable--;
2229 mp->ma_used++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002230 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002231 Py_INCREF(val);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002232 return val;
Guido van Rossum164452c2000-08-08 16:12:54 +00002233}
2234
2235
2236static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002237dict_clear(register PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002238{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002239 PyDict_Clear((PyObject *)mp);
2240 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002241}
2242
Guido van Rossumba6ab842000-12-12 22:02:18 +00002243static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002244dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002245{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002246 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002247 PyObject *old_value, *old_key;
2248 PyObject *key, *deflt = NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002249 PyDictKeyEntry *ep;
2250 PyObject **value_addr;
Guido van Rossume027d982002-04-12 15:11:59 +00002251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002252 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2253 return NULL;
2254 if (mp->ma_used == 0) {
2255 if (deflt) {
2256 Py_INCREF(deflt);
2257 return deflt;
2258 }
Raymond Hettingerdd421542010-10-30 08:10:29 +00002259 set_key_error(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002260 return NULL;
2261 }
2262 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002263 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002264 hash = PyObject_Hash(key);
2265 if (hash == -1)
2266 return NULL;
2267 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002268 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002269 if (ep == NULL)
2270 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002271 old_value = *value_addr;
2272 if (old_value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002273 if (deflt) {
2274 Py_INCREF(deflt);
2275 return deflt;
2276 }
2277 set_key_error(key);
2278 return NULL;
2279 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002280 *value_addr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002282 if (!_PyDict_HasSplitTable(mp)) {
2283 ENSURE_ALLOWS_DELETIONS(mp);
2284 old_key = ep->me_key;
2285 Py_INCREF(dummy);
2286 ep->me_key = dummy;
2287 Py_DECREF(old_key);
2288 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002289 return old_value;
Guido van Rossume027d982002-04-12 15:11:59 +00002290}
2291
2292static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002293dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00002294{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002295 Py_hash_t i = 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002296 PyDictKeyEntry *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002297 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002298
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002300 /* Allocate the result tuple before checking the size. Believe it
2301 * or not, this allocation could trigger a garbage collection which
2302 * could empty the dict, so if we checked the size first and that
2303 * happened, the result would be an infinite loop (searching for an
2304 * entry that no longer exists). Note that the usual popitem()
2305 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2306 * tuple away if the dict *is* empty isn't a significant
2307 * inefficiency -- possible, but unlikely in practice.
2308 */
2309 res = PyTuple_New(2);
2310 if (res == NULL)
2311 return NULL;
2312 if (mp->ma_used == 0) {
2313 Py_DECREF(res);
2314 PyErr_SetString(PyExc_KeyError,
2315 "popitem(): dictionary is empty");
2316 return NULL;
2317 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002318 /* Convert split table to combined table */
2319 if (mp->ma_keys->dk_lookup == lookdict_split) {
2320 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
2321 Py_DECREF(res);
2322 return NULL;
2323 }
2324 }
2325 ENSURE_ALLOWS_DELETIONS(mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002326 /* Set ep to "the first" dict entry with a value. We abuse the hash
2327 * field of slot 0 to hold a search finger:
2328 * If slot 0 has a value, use slot 0.
2329 * Else slot 0 is being used to hold a search finger,
2330 * and we use its hash value as the first index to look.
2331 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002332 ep = &mp->ma_keys->dk_entries[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002333 if (ep->me_value == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002334 Py_ssize_t mask = DK_MASK(mp->ma_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002335 i = ep->me_hash;
2336 /* The hash field may be a real hash value, or it may be a
2337 * legit search finger, or it may be a once-legit search
2338 * finger that's out of bounds now because it wrapped around
2339 * or the table shrunk -- simply make sure it's in bounds now.
2340 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002341 if (i > mask || i < 1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002342 i = 1; /* skip slot 0 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002343 while ((ep = &mp->ma_keys->dk_entries[i])->me_value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002344 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002345 if (i > mask)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346 i = 1;
2347 }
2348 }
2349 PyTuple_SET_ITEM(res, 0, ep->me_key);
2350 PyTuple_SET_ITEM(res, 1, ep->me_value);
2351 Py_INCREF(dummy);
2352 ep->me_key = dummy;
2353 ep->me_value = NULL;
2354 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002355 assert(mp->ma_keys->dk_entries[0].me_value == NULL);
2356 mp->ma_keys->dk_entries[0].me_hash = i + 1; /* next place to start */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002358}
2359
Jeremy Hylton8caad492000-06-23 14:18:11 +00002360static int
2361dict_traverse(PyObject *op, visitproc visit, void *arg)
2362{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002363 Py_ssize_t i, n;
2364 PyDictObject *mp = (PyDictObject *)op;
2365 if (mp->ma_keys->dk_lookup == lookdict) {
2366 for (i = 0; i < DK_SIZE(mp->ma_keys); i++) {
2367 if (mp->ma_keys->dk_entries[i].me_value != NULL) {
2368 Py_VISIT(mp->ma_keys->dk_entries[i].me_value);
2369 Py_VISIT(mp->ma_keys->dk_entries[i].me_key);
2370 }
2371 }
2372 } else {
2373 if (mp->ma_values != NULL) {
2374 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2375 Py_VISIT(mp->ma_values[i]);
2376 }
2377 }
2378 else {
2379 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2380 Py_VISIT(mp->ma_keys->dk_entries[i].me_value);
2381 }
2382 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 }
2384 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002385}
2386
2387static int
2388dict_tp_clear(PyObject *op)
2389{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002390 PyDict_Clear(op);
2391 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002392}
2393
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002394static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002395
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002396static PyObject *
2397dict_sizeof(PyDictObject *mp)
2398{
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002399 Py_ssize_t size, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002400
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002401 size = DK_SIZE(mp->ma_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002402 res = sizeof(PyDictObject);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002403 if (mp->ma_values)
2404 res += size * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002405 /* If the dictionary is split, the keys portion is accounted-for
2406 in the type object. */
2407 if (mp->ma_keys->dk_refcnt == 1)
2408 res += sizeof(PyDictKeysObject) + (size-1) * sizeof(PyDictKeyEntry);
2409 return PyLong_FromSsize_t(res);
2410}
2411
2412Py_ssize_t
2413_PyDict_KeysSize(PyDictKeysObject *keys)
2414{
2415 return sizeof(PyDictKeysObject) + (DK_SIZE(keys)-1) * sizeof(PyDictKeyEntry);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002416}
2417
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002418PyDoc_STRVAR(contains__doc__,
2419"D.__contains__(k) -> True if D has a key k, else False");
2420
2421PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
2422
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002423PyDoc_STRVAR(sizeof__doc__,
2424"D.__sizeof__() -> size of D in memory, in bytes");
2425
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002426PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002427"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002428
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002429PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002430"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 +00002431
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002432PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002433"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002434If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00002435
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002436PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00002437"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000024382-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002439
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002440PyDoc_STRVAR(update__doc__,
Georg Brandlac0675c2011-12-18 19:30:55 +01002441"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n"
2442"If E present and has a .keys() method, does: for k in E: D[k] = E[k]\n\
2443If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002444In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00002445
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002446PyDoc_STRVAR(fromkeys__doc__,
2447"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
2448v defaults to None.");
2449
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002450PyDoc_STRVAR(clear__doc__,
2451"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002452
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002453PyDoc_STRVAR(copy__doc__,
2454"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00002455
Guido van Rossumb90c8482007-02-10 01:11:45 +00002456/* Forward */
2457static PyObject *dictkeys_new(PyObject *);
2458static PyObject *dictitems_new(PyObject *);
2459static PyObject *dictvalues_new(PyObject *);
2460
Guido van Rossum45c85d12007-07-27 16:31:40 +00002461PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002462 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002463PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002464 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002465PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002466 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00002467
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002468static PyMethodDef mapp_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002469 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
2470 contains__doc__},
2471 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
2472 getitem__doc__},
2473 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
2474 sizeof__doc__},
2475 {"get", (PyCFunction)dict_get, METH_VARARGS,
2476 get__doc__},
2477 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
2478 setdefault_doc__},
2479 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
2480 pop__doc__},
2481 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
2482 popitem__doc__},
2483 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
2484 keys__doc__},
2485 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
2486 items__doc__},
2487 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
2488 values__doc__},
2489 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
2490 update__doc__},
2491 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
2492 fromkeys__doc__},
2493 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
2494 clear__doc__},
2495 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
2496 copy__doc__},
2497 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002498};
2499
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002500/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002501int
2502PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002503{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002504 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002505 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002506 PyDictKeyEntry *ep;
2507 PyObject **value_addr;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002509 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002510 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002511 hash = PyObject_Hash(key);
2512 if (hash == -1)
2513 return -1;
2514 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002515 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
2516 return (ep == NULL) ? -1 : (*value_addr != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002517}
2518
Thomas Wouterscf297e42007-02-23 15:07:44 +00002519/* Internal version of PyDict_Contains used when the hash value is already known */
2520int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002521_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00002522{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002523 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002524 PyDictKeyEntry *ep;
2525 PyObject **value_addr;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002526
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002527 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
2528 return (ep == NULL) ? -1 : (*value_addr != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00002529}
2530
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002531/* Hack to implement "key in dict" */
2532static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002533 0, /* sq_length */
2534 0, /* sq_concat */
2535 0, /* sq_repeat */
2536 0, /* sq_item */
2537 0, /* sq_slice */
2538 0, /* sq_ass_item */
2539 0, /* sq_ass_slice */
2540 PyDict_Contains, /* sq_contains */
2541 0, /* sq_inplace_concat */
2542 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002543};
2544
Guido van Rossum09e563a2001-05-01 12:10:21 +00002545static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002546dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2547{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002548 PyObject *self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002550 assert(type != NULL && type->tp_alloc != NULL);
2551 self = type->tp_alloc(type, 0);
2552 if (self != NULL) {
2553 PyDictObject *d = (PyDictObject *)self;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002554 d->ma_keys = new_keys_object(PyDict_MINSIZE_COMBINED);
2555 /* XXX - Should we raise a no-memory error? */
2556 if (d->ma_keys == NULL) {
2557 DK_INCREF(Py_EMPTY_KEYS);
2558 d->ma_keys = Py_EMPTY_KEYS;
2559 d->ma_values = empty_values;
2560 }
2561 d->ma_used = 0;
Ezio Melotti13925002011-03-16 11:05:33 +02002562 /* The object has been implicitly tracked by tp_alloc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002563 if (type == &PyDict_Type)
2564 _PyObject_GC_UNTRACK(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002565 }
2566 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002567}
2568
Tim Peters25786c02001-09-02 08:22:48 +00002569static int
2570dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2571{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002572 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002573}
2574
Tim Peters6d6c1a32001-08-02 04:15:00 +00002575static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002576dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002577{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002578 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002579}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002580
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002581PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002582"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002583"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002584" (key, value) pairs\n"
2585"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002586" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002587" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002588" d[k] = v\n"
2589"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2590" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002591
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002592PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002593 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2594 "dict",
2595 sizeof(PyDictObject),
2596 0,
2597 (destructor)dict_dealloc, /* tp_dealloc */
2598 0, /* tp_print */
2599 0, /* tp_getattr */
2600 0, /* tp_setattr */
2601 0, /* tp_reserved */
2602 (reprfunc)dict_repr, /* tp_repr */
2603 0, /* tp_as_number */
2604 &dict_as_sequence, /* tp_as_sequence */
2605 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002606 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002607 0, /* tp_call */
2608 0, /* tp_str */
2609 PyObject_GenericGetAttr, /* tp_getattro */
2610 0, /* tp_setattro */
2611 0, /* tp_as_buffer */
2612 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2613 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
2614 dictionary_doc, /* tp_doc */
2615 dict_traverse, /* tp_traverse */
2616 dict_tp_clear, /* tp_clear */
2617 dict_richcompare, /* tp_richcompare */
2618 0, /* tp_weaklistoffset */
2619 (getiterfunc)dict_iter, /* tp_iter */
2620 0, /* tp_iternext */
2621 mapp_methods, /* tp_methods */
2622 0, /* tp_members */
2623 0, /* tp_getset */
2624 0, /* tp_base */
2625 0, /* tp_dict */
2626 0, /* tp_descr_get */
2627 0, /* tp_descr_set */
2628 0, /* tp_dictoffset */
2629 dict_init, /* tp_init */
2630 PyType_GenericAlloc, /* tp_alloc */
2631 dict_new, /* tp_new */
2632 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002633};
2634
Victor Stinner3c1e4812012-03-26 22:10:51 +02002635PyObject *
2636_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
2637{
2638 PyObject *kv;
2639 kv = _PyUnicode_FromId(key); /* borrowed */
2640 if (kv == NULL)
2641 return NULL;
2642 return PyDict_GetItem(dp, kv);
2643}
2644
Guido van Rossum3cca2451997-05-16 14:23:33 +00002645/* For backward compatibility with old dictionary interface */
2646
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002647PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002648PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002649{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002650 PyObject *kv, *rv;
2651 kv = PyUnicode_FromString(key);
2652 if (kv == NULL)
2653 return NULL;
2654 rv = PyDict_GetItem(v, kv);
2655 Py_DECREF(kv);
2656 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002657}
2658
2659int
Victor Stinner3c1e4812012-03-26 22:10:51 +02002660_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
2661{
2662 PyObject *kv;
2663 kv = _PyUnicode_FromId(key); /* borrowed */
2664 if (kv == NULL)
2665 return -1;
2666 return PyDict_SetItem(v, kv, item);
2667}
2668
2669int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002670PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002671{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002672 PyObject *kv;
2673 int err;
2674 kv = PyUnicode_FromString(key);
2675 if (kv == NULL)
2676 return -1;
2677 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
2678 err = PyDict_SetItem(v, kv, item);
2679 Py_DECREF(kv);
2680 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002681}
2682
2683int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002684PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002685{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002686 PyObject *kv;
2687 int err;
2688 kv = PyUnicode_FromString(key);
2689 if (kv == NULL)
2690 return -1;
2691 err = PyDict_DelItem(v, kv);
2692 Py_DECREF(kv);
2693 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002694}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002695
Raymond Hettinger019a1482004-03-18 02:41:19 +00002696/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002697
2698typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002699 PyObject_HEAD
2700 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
2701 Py_ssize_t di_used;
2702 Py_ssize_t di_pos;
2703 PyObject* di_result; /* reusable result tuple for iteritems */
2704 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002705} dictiterobject;
2706
2707static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002708dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002709{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002710 dictiterobject *di;
2711 di = PyObject_GC_New(dictiterobject, itertype);
2712 if (di == NULL)
2713 return NULL;
2714 Py_INCREF(dict);
2715 di->di_dict = dict;
2716 di->di_used = dict->ma_used;
2717 di->di_pos = 0;
2718 di->len = dict->ma_used;
2719 if (itertype == &PyDictIterItem_Type) {
2720 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2721 if (di->di_result == NULL) {
2722 Py_DECREF(di);
2723 return NULL;
2724 }
2725 }
2726 else
2727 di->di_result = NULL;
2728 _PyObject_GC_TRACK(di);
2729 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002730}
2731
2732static void
2733dictiter_dealloc(dictiterobject *di)
2734{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002735 Py_XDECREF(di->di_dict);
2736 Py_XDECREF(di->di_result);
2737 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002738}
2739
2740static int
2741dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
2742{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002743 Py_VISIT(di->di_dict);
2744 Py_VISIT(di->di_result);
2745 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002746}
2747
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002748static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002749dictiter_len(dictiterobject *di)
2750{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002751 Py_ssize_t len = 0;
2752 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
2753 len = di->len;
2754 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002755}
2756
Guido van Rossumb90c8482007-02-10 01:11:45 +00002757PyDoc_STRVAR(length_hint_doc,
2758 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002759
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002760static PyObject *
2761dictiter_reduce(dictiterobject *di);
2762
2763PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
2764
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002765static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002766 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
2767 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002768 {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS,
2769 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002770 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002771};
2772
Raymond Hettinger019a1482004-03-18 02:41:19 +00002773static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002774{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002775 PyObject *key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002776 register Py_ssize_t i, mask, offset;
2777 register PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002778 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002779 PyObject **value_ptr;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002781 if (d == NULL)
2782 return NULL;
2783 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002784
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002785 if (di->di_used != d->ma_used) {
2786 PyErr_SetString(PyExc_RuntimeError,
2787 "dictionary changed size during iteration");
2788 di->di_used = -1; /* Make this state sticky */
2789 return NULL;
2790 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002792 i = di->di_pos;
2793 if (i < 0)
2794 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002795 k = d->ma_keys;
2796 if (d->ma_values) {
2797 value_ptr = &d->ma_values[i];
2798 offset = sizeof(PyObject *);
2799 }
2800 else {
2801 value_ptr = &k->dk_entries[i].me_value;
2802 offset = sizeof(PyDictKeyEntry);
2803 }
2804 mask = DK_SIZE(k)-1;
2805 while (i <= mask && *value_ptr == NULL) {
2806 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002807 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002808 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002809 di->di_pos = i+1;
2810 if (i > mask)
2811 goto fail;
2812 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002813 key = k->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002814 Py_INCREF(key);
2815 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002816
2817fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002818 Py_DECREF(d);
2819 di->di_dict = NULL;
2820 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002821}
2822
Raymond Hettinger019a1482004-03-18 02:41:19 +00002823PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002824 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2825 "dict_keyiterator", /* tp_name */
2826 sizeof(dictiterobject), /* tp_basicsize */
2827 0, /* tp_itemsize */
2828 /* methods */
2829 (destructor)dictiter_dealloc, /* tp_dealloc */
2830 0, /* tp_print */
2831 0, /* tp_getattr */
2832 0, /* tp_setattr */
2833 0, /* tp_reserved */
2834 0, /* tp_repr */
2835 0, /* tp_as_number */
2836 0, /* tp_as_sequence */
2837 0, /* tp_as_mapping */
2838 0, /* tp_hash */
2839 0, /* tp_call */
2840 0, /* tp_str */
2841 PyObject_GenericGetAttr, /* tp_getattro */
2842 0, /* tp_setattro */
2843 0, /* tp_as_buffer */
2844 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2845 0, /* tp_doc */
2846 (traverseproc)dictiter_traverse, /* tp_traverse */
2847 0, /* tp_clear */
2848 0, /* tp_richcompare */
2849 0, /* tp_weaklistoffset */
2850 PyObject_SelfIter, /* tp_iter */
2851 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
2852 dictiter_methods, /* tp_methods */
2853 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002854};
2855
2856static PyObject *dictiter_iternextvalue(dictiterobject *di)
2857{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002858 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002859 register Py_ssize_t i, mask, offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002860 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002861 PyObject **value_ptr;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002863 if (d == NULL)
2864 return NULL;
2865 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00002866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002867 if (di->di_used != d->ma_used) {
2868 PyErr_SetString(PyExc_RuntimeError,
2869 "dictionary changed size during iteration");
2870 di->di_used = -1; /* Make this state sticky */
2871 return NULL;
2872 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002873
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002875 mask = DK_SIZE(d->ma_keys)-1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 if (i < 0 || i > mask)
2877 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002878 if (d->ma_values) {
2879 value_ptr = &d->ma_values[i];
2880 offset = sizeof(PyObject *);
2881 }
2882 else {
2883 value_ptr = &d->ma_keys->dk_entries[i].me_value;
2884 offset = sizeof(PyDictKeyEntry);
2885 }
2886 while (i <= mask && *value_ptr == NULL) {
2887 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002888 i++;
2889 if (i > mask)
2890 goto fail;
2891 }
2892 di->di_pos = i+1;
2893 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002894 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002895 Py_INCREF(value);
2896 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002897
2898fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002899 Py_DECREF(d);
2900 di->di_dict = NULL;
2901 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002902}
2903
2904PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002905 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2906 "dict_valueiterator", /* tp_name */
2907 sizeof(dictiterobject), /* tp_basicsize */
2908 0, /* tp_itemsize */
2909 /* methods */
2910 (destructor)dictiter_dealloc, /* tp_dealloc */
2911 0, /* tp_print */
2912 0, /* tp_getattr */
2913 0, /* tp_setattr */
2914 0, /* tp_reserved */
2915 0, /* tp_repr */
2916 0, /* tp_as_number */
2917 0, /* tp_as_sequence */
2918 0, /* tp_as_mapping */
2919 0, /* tp_hash */
2920 0, /* tp_call */
2921 0, /* tp_str */
2922 PyObject_GenericGetAttr, /* tp_getattro */
2923 0, /* tp_setattro */
2924 0, /* tp_as_buffer */
2925 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2926 0, /* tp_doc */
2927 (traverseproc)dictiter_traverse, /* tp_traverse */
2928 0, /* tp_clear */
2929 0, /* tp_richcompare */
2930 0, /* tp_weaklistoffset */
2931 PyObject_SelfIter, /* tp_iter */
2932 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
2933 dictiter_methods, /* tp_methods */
2934 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002935};
2936
2937static PyObject *dictiter_iternextitem(dictiterobject *di)
2938{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002939 PyObject *key, *value, *result = di->di_result;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002940 register Py_ssize_t i, mask, offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002941 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002942 PyObject **value_ptr;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002944 if (d == NULL)
2945 return NULL;
2946 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00002947
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002948 if (di->di_used != d->ma_used) {
2949 PyErr_SetString(PyExc_RuntimeError,
2950 "dictionary changed size during iteration");
2951 di->di_used = -1; /* Make this state sticky */
2952 return NULL;
2953 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002955 i = di->di_pos;
2956 if (i < 0)
2957 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002958 mask = DK_SIZE(d->ma_keys)-1;
2959 if (d->ma_values) {
2960 value_ptr = &d->ma_values[i];
2961 offset = sizeof(PyObject *);
2962 }
2963 else {
2964 value_ptr = &d->ma_keys->dk_entries[i].me_value;
2965 offset = sizeof(PyDictKeyEntry);
2966 }
2967 while (i <= mask && *value_ptr == NULL) {
2968 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002969 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002970 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002971 di->di_pos = i+1;
2972 if (i > mask)
2973 goto fail;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002975 if (result->ob_refcnt == 1) {
2976 Py_INCREF(result);
2977 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2978 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2979 } else {
2980 result = PyTuple_New(2);
2981 if (result == NULL)
2982 return NULL;
2983 }
2984 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002985 key = d->ma_keys->dk_entries[i].me_key;
2986 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002987 Py_INCREF(key);
2988 Py_INCREF(value);
2989 PyTuple_SET_ITEM(result, 0, key);
2990 PyTuple_SET_ITEM(result, 1, value);
2991 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002992
2993fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002994 Py_DECREF(d);
2995 di->di_dict = NULL;
2996 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002997}
2998
2999PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003000 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3001 "dict_itemiterator", /* tp_name */
3002 sizeof(dictiterobject), /* tp_basicsize */
3003 0, /* tp_itemsize */
3004 /* methods */
3005 (destructor)dictiter_dealloc, /* tp_dealloc */
3006 0, /* tp_print */
3007 0, /* tp_getattr */
3008 0, /* tp_setattr */
3009 0, /* tp_reserved */
3010 0, /* tp_repr */
3011 0, /* tp_as_number */
3012 0, /* tp_as_sequence */
3013 0, /* tp_as_mapping */
3014 0, /* tp_hash */
3015 0, /* tp_call */
3016 0, /* tp_str */
3017 PyObject_GenericGetAttr, /* tp_getattro */
3018 0, /* tp_setattro */
3019 0, /* tp_as_buffer */
3020 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3021 0, /* tp_doc */
3022 (traverseproc)dictiter_traverse, /* tp_traverse */
3023 0, /* tp_clear */
3024 0, /* tp_richcompare */
3025 0, /* tp_weaklistoffset */
3026 PyObject_SelfIter, /* tp_iter */
3027 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3028 dictiter_methods, /* tp_methods */
3029 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003030};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003031
3032
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003033static PyObject *
3034dictiter_reduce(dictiterobject *di)
3035{
3036 PyObject *list;
3037 dictiterobject tmp;
3038
3039 list = PyList_New(0);
3040 if (!list)
3041 return NULL;
3042
3043 /* copy the itertor state */
3044 tmp = *di;
3045 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003046
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003047 /* iterate the temporary into a list */
3048 for(;;) {
3049 PyObject *element = 0;
3050 if (Py_TYPE(di) == &PyDictIterItem_Type)
3051 element = dictiter_iternextitem(&tmp);
3052 else if (Py_TYPE(di) == &PyDictIterKey_Type)
3053 element = dictiter_iternextkey(&tmp);
3054 else if (Py_TYPE(di) == &PyDictIterValue_Type)
3055 element = dictiter_iternextvalue(&tmp);
3056 else
3057 assert(0);
3058 if (element) {
3059 if (PyList_Append(list, element)) {
3060 Py_DECREF(element);
3061 Py_DECREF(list);
3062 Py_XDECREF(tmp.di_dict);
3063 return NULL;
3064 }
3065 Py_DECREF(element);
3066 } else
3067 break;
3068 }
3069 Py_XDECREF(tmp.di_dict);
3070 /* check for error */
3071 if (tmp.di_dict != NULL) {
3072 /* we have an error */
3073 Py_DECREF(list);
3074 return NULL;
3075 }
Antoine Pitroua7013882012-04-05 00:04:20 +02003076 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003077}
3078
Guido van Rossum3ac67412007-02-10 18:55:06 +00003079/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003080/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003081/***********************************************/
3082
Guido van Rossumb90c8482007-02-10 01:11:45 +00003083/* The instance lay-out is the same for all three; but the type differs. */
3084
3085typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003086 PyObject_HEAD
3087 PyDictObject *dv_dict;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003088} dictviewobject;
3089
3090
3091static void
Guido van Rossum3ac67412007-02-10 18:55:06 +00003092dictview_dealloc(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003093{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003094 Py_XDECREF(dv->dv_dict);
3095 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003096}
3097
3098static int
3099dictview_traverse(dictviewobject *dv, visitproc visit, void *arg)
3100{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003101 Py_VISIT(dv->dv_dict);
3102 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003103}
3104
Guido van Rossum83825ac2007-02-10 04:54:19 +00003105static Py_ssize_t
Guido van Rossum3ac67412007-02-10 18:55:06 +00003106dictview_len(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003107{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003108 Py_ssize_t len = 0;
3109 if (dv->dv_dict != NULL)
3110 len = dv->dv_dict->ma_used;
3111 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003112}
3113
3114static PyObject *
3115dictview_new(PyObject *dict, PyTypeObject *type)
3116{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003117 dictviewobject *dv;
3118 if (dict == NULL) {
3119 PyErr_BadInternalCall();
3120 return NULL;
3121 }
3122 if (!PyDict_Check(dict)) {
3123 /* XXX Get rid of this restriction later */
3124 PyErr_Format(PyExc_TypeError,
3125 "%s() requires a dict argument, not '%s'",
3126 type->tp_name, dict->ob_type->tp_name);
3127 return NULL;
3128 }
3129 dv = PyObject_GC_New(dictviewobject, type);
3130 if (dv == NULL)
3131 return NULL;
3132 Py_INCREF(dict);
3133 dv->dv_dict = (PyDictObject *)dict;
3134 _PyObject_GC_TRACK(dv);
3135 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003136}
3137
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003138/* TODO(guido): The views objects are not complete:
3139
3140 * support more set operations
3141 * support arbitrary mappings?
3142 - either these should be static or exported in dictobject.h
3143 - if public then they should probably be in builtins
3144*/
3145
Guido van Rossumaac530c2007-08-24 22:33:45 +00003146/* Return 1 if self is a subset of other, iterating over self;
3147 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003148static int
3149all_contained_in(PyObject *self, PyObject *other)
3150{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003151 PyObject *iter = PyObject_GetIter(self);
3152 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003153
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003154 if (iter == NULL)
3155 return -1;
3156 for (;;) {
3157 PyObject *next = PyIter_Next(iter);
3158 if (next == NULL) {
3159 if (PyErr_Occurred())
3160 ok = -1;
3161 break;
3162 }
3163 ok = PySequence_Contains(other, next);
3164 Py_DECREF(next);
3165 if (ok <= 0)
3166 break;
3167 }
3168 Py_DECREF(iter);
3169 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003170}
3171
3172static PyObject *
3173dictview_richcompare(PyObject *self, PyObject *other, int op)
3174{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003175 Py_ssize_t len_self, len_other;
3176 int ok;
3177 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003179 assert(self != NULL);
3180 assert(PyDictViewSet_Check(self));
3181 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00003182
Brian Curtindfc80e32011-08-10 20:28:54 -05003183 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
3184 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003186 len_self = PyObject_Size(self);
3187 if (len_self < 0)
3188 return NULL;
3189 len_other = PyObject_Size(other);
3190 if (len_other < 0)
3191 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003193 ok = 0;
3194 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00003195
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003196 case Py_NE:
3197 case Py_EQ:
3198 if (len_self == len_other)
3199 ok = all_contained_in(self, other);
3200 if (op == Py_NE && ok >= 0)
3201 ok = !ok;
3202 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003204 case Py_LT:
3205 if (len_self < len_other)
3206 ok = all_contained_in(self, other);
3207 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003209 case Py_LE:
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_GT:
3215 if (len_self > len_other)
3216 ok = all_contained_in(other, self);
3217 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003219 case Py_GE:
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 }
3225 if (ok < 0)
3226 return NULL;
3227 result = ok ? Py_True : Py_False;
3228 Py_INCREF(result);
3229 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003230}
3231
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003232static PyObject *
3233dictview_repr(dictviewobject *dv)
3234{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003235 PyObject *seq;
3236 PyObject *result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003238 seq = PySequence_List((PyObject *)dv);
3239 if (seq == NULL)
3240 return NULL;
3241
3242 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
3243 Py_DECREF(seq);
3244 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003245}
3246
Guido van Rossum3ac67412007-02-10 18:55:06 +00003247/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003248
3249static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003250dictkeys_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003251{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003252 if (dv->dv_dict == NULL) {
3253 Py_RETURN_NONE;
3254 }
3255 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003256}
3257
3258static int
3259dictkeys_contains(dictviewobject *dv, PyObject *obj)
3260{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003261 if (dv->dv_dict == NULL)
3262 return 0;
3263 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003264}
3265
Guido van Rossum83825ac2007-02-10 04:54:19 +00003266static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003267 (lenfunc)dictview_len, /* sq_length */
3268 0, /* sq_concat */
3269 0, /* sq_repeat */
3270 0, /* sq_item */
3271 0, /* sq_slice */
3272 0, /* sq_ass_item */
3273 0, /* sq_ass_slice */
3274 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003275};
3276
Guido van Rossum523259b2007-08-24 23:41:22 +00003277static PyObject*
3278dictviews_sub(PyObject* self, PyObject *other)
3279{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003280 PyObject *result = PySet_New(self);
3281 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003282 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003284 if (result == NULL)
3285 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003286
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003287 tmp = _PyObject_CallMethodId(result, &PyId_difference_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003288 if (tmp == NULL) {
3289 Py_DECREF(result);
3290 return NULL;
3291 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003293 Py_DECREF(tmp);
3294 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003295}
3296
3297static PyObject*
3298dictviews_and(PyObject* self, PyObject *other)
3299{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003300 PyObject *result = PySet_New(self);
3301 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003302 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003304 if (result == NULL)
3305 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003306
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003307 tmp = _PyObject_CallMethodId(result, &PyId_intersection_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003308 if (tmp == NULL) {
3309 Py_DECREF(result);
3310 return NULL;
3311 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003313 Py_DECREF(tmp);
3314 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003315}
3316
3317static PyObject*
3318dictviews_or(PyObject* self, PyObject *other)
3319{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003320 PyObject *result = PySet_New(self);
3321 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003322 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003324 if (result == NULL)
3325 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003326
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003327 tmp = _PyObject_CallMethodId(result, &PyId_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003328 if (tmp == NULL) {
3329 Py_DECREF(result);
3330 return NULL;
3331 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003333 Py_DECREF(tmp);
3334 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003335}
3336
3337static PyObject*
3338dictviews_xor(PyObject* self, PyObject *other)
3339{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003340 PyObject *result = PySet_New(self);
3341 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003342 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003344 if (result == NULL)
3345 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003346
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003347 tmp = _PyObject_CallMethodId(result, &PyId_symmetric_difference_update, "O",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003348 other);
3349 if (tmp == NULL) {
3350 Py_DECREF(result);
3351 return NULL;
3352 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003354 Py_DECREF(tmp);
3355 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003356}
3357
3358static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003359 0, /*nb_add*/
3360 (binaryfunc)dictviews_sub, /*nb_subtract*/
3361 0, /*nb_multiply*/
3362 0, /*nb_remainder*/
3363 0, /*nb_divmod*/
3364 0, /*nb_power*/
3365 0, /*nb_negative*/
3366 0, /*nb_positive*/
3367 0, /*nb_absolute*/
3368 0, /*nb_bool*/
3369 0, /*nb_invert*/
3370 0, /*nb_lshift*/
3371 0, /*nb_rshift*/
3372 (binaryfunc)dictviews_and, /*nb_and*/
3373 (binaryfunc)dictviews_xor, /*nb_xor*/
3374 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00003375};
3376
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003377static PyObject*
3378dictviews_isdisjoint(PyObject *self, PyObject *other)
3379{
3380 PyObject *it;
3381 PyObject *item = NULL;
3382
3383 if (self == other) {
3384 if (dictview_len((dictviewobject *)self) == 0)
3385 Py_RETURN_TRUE;
3386 else
3387 Py_RETURN_FALSE;
3388 }
3389
3390 /* Iterate over the shorter object (only if other is a set,
3391 * because PySequence_Contains may be expensive otherwise): */
3392 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
3393 Py_ssize_t len_self = dictview_len((dictviewobject *)self);
3394 Py_ssize_t len_other = PyObject_Size(other);
3395 if (len_other == -1)
3396 return NULL;
3397
3398 if ((len_other > len_self)) {
3399 PyObject *tmp = other;
3400 other = self;
3401 self = tmp;
3402 }
3403 }
3404
3405 it = PyObject_GetIter(other);
3406 if (it == NULL)
3407 return NULL;
3408
3409 while ((item = PyIter_Next(it)) != NULL) {
3410 int contains = PySequence_Contains(self, item);
3411 Py_DECREF(item);
3412 if (contains == -1) {
3413 Py_DECREF(it);
3414 return NULL;
3415 }
3416
3417 if (contains) {
3418 Py_DECREF(it);
3419 Py_RETURN_FALSE;
3420 }
3421 }
3422 Py_DECREF(it);
3423 if (PyErr_Occurred())
3424 return NULL; /* PyIter_Next raised an exception. */
3425 Py_RETURN_TRUE;
3426}
3427
3428PyDoc_STRVAR(isdisjoint_doc,
3429"Return True if the view and the given iterable have a null intersection.");
3430
Guido van Rossumb90c8482007-02-10 01:11:45 +00003431static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003432 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3433 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003434 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003435};
3436
3437PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003438 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3439 "dict_keys", /* tp_name */
3440 sizeof(dictviewobject), /* tp_basicsize */
3441 0, /* tp_itemsize */
3442 /* methods */
3443 (destructor)dictview_dealloc, /* tp_dealloc */
3444 0, /* tp_print */
3445 0, /* tp_getattr */
3446 0, /* tp_setattr */
3447 0, /* tp_reserved */
3448 (reprfunc)dictview_repr, /* tp_repr */
3449 &dictviews_as_number, /* tp_as_number */
3450 &dictkeys_as_sequence, /* tp_as_sequence */
3451 0, /* tp_as_mapping */
3452 0, /* tp_hash */
3453 0, /* tp_call */
3454 0, /* tp_str */
3455 PyObject_GenericGetAttr, /* tp_getattro */
3456 0, /* tp_setattro */
3457 0, /* tp_as_buffer */
3458 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3459 0, /* tp_doc */
3460 (traverseproc)dictview_traverse, /* tp_traverse */
3461 0, /* tp_clear */
3462 dictview_richcompare, /* tp_richcompare */
3463 0, /* tp_weaklistoffset */
3464 (getiterfunc)dictkeys_iter, /* tp_iter */
3465 0, /* tp_iternext */
3466 dictkeys_methods, /* tp_methods */
3467 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003468};
3469
3470static PyObject *
3471dictkeys_new(PyObject *dict)
3472{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003473 return dictview_new(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003474}
3475
Guido van Rossum3ac67412007-02-10 18:55:06 +00003476/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003477
3478static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003479dictitems_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003480{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003481 if (dv->dv_dict == NULL) {
3482 Py_RETURN_NONE;
3483 }
3484 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003485}
3486
3487static int
3488dictitems_contains(dictviewobject *dv, PyObject *obj)
3489{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003490 PyObject *key, *value, *found;
3491 if (dv->dv_dict == NULL)
3492 return 0;
3493 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
3494 return 0;
3495 key = PyTuple_GET_ITEM(obj, 0);
3496 value = PyTuple_GET_ITEM(obj, 1);
3497 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
3498 if (found == NULL) {
3499 if (PyErr_Occurred())
3500 return -1;
3501 return 0;
3502 }
3503 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003504}
3505
Guido van Rossum83825ac2007-02-10 04:54:19 +00003506static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003507 (lenfunc)dictview_len, /* sq_length */
3508 0, /* sq_concat */
3509 0, /* sq_repeat */
3510 0, /* sq_item */
3511 0, /* sq_slice */
3512 0, /* sq_ass_item */
3513 0, /* sq_ass_slice */
3514 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003515};
3516
Guido van Rossumb90c8482007-02-10 01:11:45 +00003517static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003518 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3519 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003520 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003521};
3522
3523PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003524 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3525 "dict_items", /* tp_name */
3526 sizeof(dictviewobject), /* tp_basicsize */
3527 0, /* tp_itemsize */
3528 /* methods */
3529 (destructor)dictview_dealloc, /* tp_dealloc */
3530 0, /* tp_print */
3531 0, /* tp_getattr */
3532 0, /* tp_setattr */
3533 0, /* tp_reserved */
3534 (reprfunc)dictview_repr, /* tp_repr */
3535 &dictviews_as_number, /* tp_as_number */
3536 &dictitems_as_sequence, /* tp_as_sequence */
3537 0, /* tp_as_mapping */
3538 0, /* tp_hash */
3539 0, /* tp_call */
3540 0, /* tp_str */
3541 PyObject_GenericGetAttr, /* tp_getattro */
3542 0, /* tp_setattro */
3543 0, /* tp_as_buffer */
3544 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3545 0, /* tp_doc */
3546 (traverseproc)dictview_traverse, /* tp_traverse */
3547 0, /* tp_clear */
3548 dictview_richcompare, /* tp_richcompare */
3549 0, /* tp_weaklistoffset */
3550 (getiterfunc)dictitems_iter, /* tp_iter */
3551 0, /* tp_iternext */
3552 dictitems_methods, /* tp_methods */
3553 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003554};
3555
3556static PyObject *
3557dictitems_new(PyObject *dict)
3558{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003559 return dictview_new(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003560}
3561
Guido van Rossum3ac67412007-02-10 18:55:06 +00003562/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003563
3564static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003565dictvalues_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003566{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003567 if (dv->dv_dict == NULL) {
3568 Py_RETURN_NONE;
3569 }
3570 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003571}
3572
Guido van Rossum83825ac2007-02-10 04:54:19 +00003573static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003574 (lenfunc)dictview_len, /* sq_length */
3575 0, /* sq_concat */
3576 0, /* sq_repeat */
3577 0, /* sq_item */
3578 0, /* sq_slice */
3579 0, /* sq_ass_item */
3580 0, /* sq_ass_slice */
3581 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003582};
3583
Guido van Rossumb90c8482007-02-10 01:11:45 +00003584static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003585 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003586};
3587
3588PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003589 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3590 "dict_values", /* tp_name */
3591 sizeof(dictviewobject), /* tp_basicsize */
3592 0, /* tp_itemsize */
3593 /* methods */
3594 (destructor)dictview_dealloc, /* tp_dealloc */
3595 0, /* tp_print */
3596 0, /* tp_getattr */
3597 0, /* tp_setattr */
3598 0, /* tp_reserved */
3599 (reprfunc)dictview_repr, /* tp_repr */
3600 0, /* tp_as_number */
3601 &dictvalues_as_sequence, /* tp_as_sequence */
3602 0, /* tp_as_mapping */
3603 0, /* tp_hash */
3604 0, /* tp_call */
3605 0, /* tp_str */
3606 PyObject_GenericGetAttr, /* tp_getattro */
3607 0, /* tp_setattro */
3608 0, /* tp_as_buffer */
3609 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3610 0, /* tp_doc */
3611 (traverseproc)dictview_traverse, /* tp_traverse */
3612 0, /* tp_clear */
3613 0, /* tp_richcompare */
3614 0, /* tp_weaklistoffset */
3615 (getiterfunc)dictvalues_iter, /* tp_iter */
3616 0, /* tp_iternext */
3617 dictvalues_methods, /* tp_methods */
3618 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003619};
3620
3621static PyObject *
3622dictvalues_new(PyObject *dict)
3623{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003624 return dictview_new(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003625}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003626
3627/* Returns NULL if cannot allocate a new PyDictKeysObject,
3628 but does not set an error */
3629PyDictKeysObject *
3630_PyDict_NewKeysForClass(void)
3631{
3632 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE_SPLIT);
3633 if (keys == NULL)
3634 PyErr_Clear();
3635 else
3636 keys->dk_lookup = lookdict_split;
3637 return keys;
3638}
3639
3640#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
3641
3642PyObject *
3643PyObject_GenericGetDict(PyObject *obj, void *context)
3644{
3645 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
3646 if (dictptr == NULL) {
3647 PyErr_SetString(PyExc_AttributeError,
3648 "This object has no __dict__");
3649 return NULL;
3650 }
3651 dict = *dictptr;
3652 if (dict == NULL) {
3653 PyTypeObject *tp = Py_TYPE(obj);
3654 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
3655 DK_INCREF(CACHED_KEYS(tp));
3656 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
3657 }
3658 else {
3659 *dictptr = dict = PyDict_New();
3660 }
3661 }
3662 Py_XINCREF(dict);
3663 return dict;
3664}
3665
3666int
3667_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
3668 PyObject *key, PyObject *value)
3669{
3670 PyObject *dict;
3671 int res;
3672 PyDictKeysObject *cached;
3673
3674 assert(dictptr != NULL);
3675 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
3676 assert(dictptr != NULL);
3677 dict = *dictptr;
3678 if (dict == NULL) {
3679 DK_INCREF(cached);
3680 dict = new_dict_with_shared_keys(cached);
3681 if (dict == NULL)
3682 return -1;
3683 *dictptr = dict;
3684 }
3685 if (value == NULL) {
3686 res = PyDict_DelItem(dict, key);
3687 if (cached != ((PyDictObject *)dict)->ma_keys) {
3688 CACHED_KEYS(tp) = NULL;
3689 DK_DECREF(cached);
3690 }
3691 } else {
3692 res = PyDict_SetItem(dict, key, value);
3693 if (cached != ((PyDictObject *)dict)->ma_keys) {
3694 /* Either update tp->ht_cached_keys or delete it */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04003695 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003696 CACHED_KEYS(tp) = make_keys_shared(dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003697 } else {
3698 CACHED_KEYS(tp) = NULL;
3699 }
3700 DK_DECREF(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04003701 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
3702 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003703 }
3704 }
3705 } else {
3706 dict = *dictptr;
3707 if (dict == NULL) {
3708 dict = PyDict_New();
3709 if (dict == NULL)
3710 return -1;
3711 *dictptr = dict;
3712 }
3713 if (value == NULL) {
3714 res = PyDict_DelItem(dict, key);
3715 } else {
3716 res = PyDict_SetItem(dict, key, value);
3717 }
3718 }
3719 return res;
3720}
3721
3722void
3723_PyDictKeys_DecRef(PyDictKeysObject *keys)
3724{
3725 DK_DECREF(keys);
3726}
3727
3728
3729/* ARGSUSED */
3730static PyObject *
3731dummy_repr(PyObject *op)
3732{
3733 return PyUnicode_FromString("<dummy key>");
3734}
3735
3736/* ARGUSED */
3737static void
3738dummy_dealloc(PyObject* ignore)
3739{
3740 /* This should never get called, but we also don't want to SEGV if
3741 * we accidentally decref dummy-key out of existence.
3742 */
3743 Py_FatalError("deallocating <dummy key>");
3744}
3745
3746static PyTypeObject PyDictDummy_Type = {
3747 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3748 "<dummy key> type",
3749 0,
3750 0,
3751 dummy_dealloc, /*tp_dealloc*/ /*never called*/
3752 0, /*tp_print*/
3753 0, /*tp_getattr*/
3754 0, /*tp_setattr*/
3755 0, /*tp_reserved*/
3756 dummy_repr, /*tp_repr*/
3757 0, /*tp_as_number*/
3758 0, /*tp_as_sequence*/
3759 0, /*tp_as_mapping*/
3760 0, /*tp_hash */
3761 0, /*tp_call */
3762 0, /*tp_str */
3763 0, /*tp_getattro */
3764 0, /*tp_setattro */
3765 0, /*tp_as_buffer */
3766 Py_TPFLAGS_DEFAULT, /*tp_flags */
3767};
3768
3769static PyObject _dummy_struct = {
3770 _PyObject_EXTRA_INIT
3771 2, &PyDictDummy_Type
3772};
3773