blob: 04764429293eadee19e60245845996835971dd41 [file] [log] [blame]
Guido van Rossum2bc13791999-03-24 19:06:42 +00001/* Dictionary object implementation using a hash table */
Guido van Rossum9bfef441993-03-29 10:43:31 +00002
Raymond Hettinger930427b2003-05-03 06:51:59 +00003/* The distribution includes a separate file, Objects/dictnotes.txt,
Tim Peters60b29962006-01-01 01:19:23 +00004 describing explorations into dictionary design and optimization.
Raymond Hettinger930427b2003-05-03 06:51:59 +00005 It covers typical dictionary use patterns, the parameters for
6 tuning dictionaries, and several ideas for possible optimizations.
7*/
8
Victor Stinner742da042016-09-07 17:40:12 -07009/* PyDictKeysObject
10
11This implements the dictionary's hashtable.
12
Berker Peksag71c01d42016-09-09 03:57:23 +030013As of Python 3.6, this is compact and ordered. Basic idea is described here.
Benjamin Peterson003f0592016-09-08 10:14:31 -070014https://morepypy.blogspot.com/2015/01/faster-more-memory-efficient-and-more.html
Victor Stinner742da042016-09-07 17:40:12 -070015
16layout:
17
18+---------------+
19| dk_refcnt |
20| dk_size |
21| dk_lookup |
22| dk_usable |
23| dk_nentries |
24+---------------+
25| dk_indices |
26| |
27+---------------+
28| dk_entries |
29| |
30+---------------+
31
32dk_indices is actual hashtable. It holds index in entries, or DKIX_EMPTY(-1)
33or DKIX_DUMMY(-2).
34Size of indices is dk_size. Type of each index in indices is vary on dk_size:
35
36* int8 for dk_size <= 128
37* int16 for 256 <= dk_size <= 2**15
38* int32 for 2**16 <= dk_size <= 2**31
39* int64 for 2**32 <= dk_size
40
41dk_entries is array of PyDictKeyEntry. It's size is USABLE_FRACTION(dk_size).
42DK_ENTRIES(dk) can be used to get pointer to entries.
43
44NOTE: Since negative value is used for DKIX_EMPTY and DKIX_DUMMY, type of
45dk_indices entry is signed integer and int16 is used for table which
46dk_size == 256.
47*/
48
Benjamin Peterson7d95e402012-04-23 11:24:50 -040049
50/*
Benjamin Peterson7d95e402012-04-23 11:24:50 -040051The DictObject can be in one of two forms.
Victor Stinner742da042016-09-07 17:40:12 -070052
Benjamin Peterson7d95e402012-04-23 11:24:50 -040053Either:
54 A combined table:
55 ma_values == NULL, dk_refcnt == 1.
56 Values are stored in the me_value field of the PyDictKeysObject.
Benjamin Peterson7d95e402012-04-23 11:24:50 -040057Or:
58 A split table:
59 ma_values != NULL, dk_refcnt >= 1
60 Values are stored in the ma_values array.
Victor Stinner742da042016-09-07 17:40:12 -070061 Only string (unicode) keys are allowed.
62 All dicts sharing same key must have same insertion order.
Benjamin Peterson7d95e402012-04-23 11:24:50 -040063
Victor Stinner742da042016-09-07 17:40:12 -070064There are four kinds of slots in the table (slot is index, and
65DK_ENTRIES(keys)[index] if index >= 0):
66
671. Unused. index == DKIX_EMPTY
68 Does not hold an active (key, value) pair now and never did. Unused can
69 transition to Active upon key insertion. This is each slot's initial state.
70
712. Active. index >= 0, me_key != NULL and me_value != NULL
72 Holds an active (key, value) pair. Active can transition to Dummy or
73 Pending upon key deletion (for combined and split tables respectively).
74 This is the only case in which me_value != NULL.
75
763. Dummy. index == DKIX_DUMMY (combined only)
77 Previously held an active (key, value) pair, but that was deleted and an
78 active pair has not yet overwritten the slot. Dummy can transition to
79 Active upon key insertion. Dummy slots cannot be made Unused again
80 else the probe sequence in case of collision would have no way to know
81 they were once active.
82
834. Pending. index >= 0, key != NULL, and value == NULL (split only)
84 Not yet inserted in split-table.
Benjamin Peterson7d95e402012-04-23 11:24:50 -040085*/
86
Victor Stinner742da042016-09-07 17:40:12 -070087/*
88Preserving insertion order
Benjamin Peterson7d95e402012-04-23 11:24:50 -040089
Victor Stinner742da042016-09-07 17:40:12 -070090It's simple for combined table. Since dk_entries is mostly append only, we can
91get insertion order by just iterating dk_entries.
92
93One exception is .popitem(). It removes last item in dk_entries and decrement
94dk_nentries to achieve amortized O(1). Since there are DKIX_DUMMY remains in
95dk_indices, we can't increment dk_usable even though dk_nentries is
96decremented.
97
98In split table, inserting into pending entry is allowed only for dk_entries[ix]
99where ix == mp->ma_used. Inserting into other index and deleting item cause
100converting the dict to the combined table.
101*/
102
103/* PyDict_MINSIZE is the starting size for any new dict.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400104 * 8 allows dicts with no more than 5 active entries; experiments suggested
105 * this suffices for the majority of dicts (consisting mostly of usually-small
106 * dicts created to pass keyword arguments).
107 * Making this 8, rather than 4 reduces the number of resizes for most
108 * dictionaries, without any significant extra memory use.
109 */
Victor Stinner742da042016-09-07 17:40:12 -0700110#define PyDict_MINSIZE 8
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400111
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000112#include "Python.h"
Eric Snow96c6af92015-05-29 22:21:39 -0600113#include "dict-common.h"
Victor Stinner990397e2016-09-09 20:22:59 -0700114#include "stringlib/eq.h" /* to get unicode_eq() */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000115
Larry Hastings61272b72014-01-07 12:41:53 -0800116/*[clinic input]
Larry Hastingsc2047262014-01-25 20:43:29 -0800117class dict "PyDictObject *" "&PyDict_Type"
Larry Hastings61272b72014-01-07 12:41:53 -0800118[clinic start generated code]*/
Larry Hastings581ee362014-01-28 05:00:08 -0800119/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f157a5a0ce9589d6]*/
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800120
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400121
122/*
123To ensure the lookup algorithm terminates, there must be at least one Unused
124slot (NULL key) in the table.
125To avoid slowing down lookups on a near-full table, we resize the table when
126it's USABLE_FRACTION (currently two-thirds) full.
127*/
Guido van Rossum16e93a81997-01-28 00:00:11 +0000128
Tim Peterseb28ef22001-06-02 05:27:19 +0000129#define PERTURB_SHIFT 5
130
Guido van Rossum16e93a81997-01-28 00:00:11 +0000131/*
Tim Peterseb28ef22001-06-02 05:27:19 +0000132Major subtleties ahead: Most hash schemes depend on having a "good" hash
133function, in the sense of simulating randomness. Python doesn't: its most
R David Murray537ad7a2016-07-10 12:33:18 -0400134important hash functions (for ints) are very regular in common
Tim Peterseb28ef22001-06-02 05:27:19 +0000135cases:
Tim Peters15d49292001-05-27 07:39:22 +0000136
R David Murray537ad7a2016-07-10 12:33:18 -0400137 >>>[hash(i) for i in range(4)]
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000138 [0, 1, 2, 3]
Tim Peters15d49292001-05-27 07:39:22 +0000139
Tim Peterseb28ef22001-06-02 05:27:19 +0000140This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
141the low-order i bits as the initial table index is extremely fast, and there
R David Murray537ad7a2016-07-10 12:33:18 -0400142are no collisions at all for dicts indexed by a contiguous range of ints. So
143this gives better-than-random behavior in common cases, and that's very
144desirable.
Tim Peters15d49292001-05-27 07:39:22 +0000145
Tim Peterseb28ef22001-06-02 05:27:19 +0000146OTOH, when collisions occur, the tendency to fill contiguous slices of the
147hash table makes a good collision resolution strategy crucial. Taking only
148the last i bits of the hash code is also vulnerable: for example, consider
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149the list [i << 16 for i in range(20000)] as a set of keys. Since ints are
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000150their own hash codes, and this fits in a dict of size 2**15, the last 15 bits
151 of every hash code are all 0: they *all* map to the same table index.
Tim Peters15d49292001-05-27 07:39:22 +0000152
Tim Peterseb28ef22001-06-02 05:27:19 +0000153But catering to unusual cases should not slow the usual ones, so we just take
154the last i bits anyway. It's up to collision resolution to do the rest. If
155we *usually* find the key we're looking for on the first try (and, it turns
156out, we usually do -- the table load factor is kept under 2/3, so the odds
157are solidly in our favor), then it makes best sense to keep the initial index
158computation dirt cheap.
Tim Peters15d49292001-05-27 07:39:22 +0000159
Tim Peterseb28ef22001-06-02 05:27:19 +0000160The first half of collision resolution is to visit table indices via this
161recurrence:
Tim Peters15d49292001-05-27 07:39:22 +0000162
Tim Peterseb28ef22001-06-02 05:27:19 +0000163 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +0000164
Tim Peterseb28ef22001-06-02 05:27:19 +0000165For any initial j in range(2**i), repeating that 2**i times generates each
166int in range(2**i) exactly once (see any text on random-number generation for
167proof). By itself, this doesn't help much: like linear probing (setting
168j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
169order. This would be bad, except that's not the only thing we do, and it's
170actually *good* in the common cases where hash keys are consecutive. In an
171example that's really too small to make this entirely clear, for a table of
172size 2**3 the order of indices is:
Tim Peters15d49292001-05-27 07:39:22 +0000173
Tim Peterseb28ef22001-06-02 05:27:19 +0000174 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
175
176If two things come in at index 5, the first place we look after is index 2,
177not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
178Linear probing is deadly in this case because there the fixed probe order
179is the *same* as the order consecutive keys are likely to arrive. But it's
180extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
181and certain that consecutive hash codes do not.
182
183The other half of the strategy is to get the other bits of the hash code
184into play. This is done by initializing a (unsigned) vrbl "perturb" to the
185full hash code, and changing the recurrence to:
186
187 j = (5*j) + 1 + perturb;
188 perturb >>= PERTURB_SHIFT;
189 use j % 2**i as the next table index;
190
191Now the probe sequence depends (eventually) on every bit in the hash code,
192and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
193because it quickly magnifies small differences in the bits that didn't affect
194the initial index. Note that because perturb is unsigned, if the recurrence
195is executed often enough perturb eventually becomes and remains 0. At that
196point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
197that's certain to find an empty slot eventually (since it generates every int
198in range(2**i), and we make sure there's always at least one empty slot).
199
200Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
201small so that the high bits of the hash code continue to affect the probe
202sequence across iterations; but you want it large so that in really bad cases
203the high-order hash bits have an effect on early iterations. 5 was "the
204best" in minimizing total collisions across experiments Tim Peters ran (on
205both normal and pathological cases), but 4 and 6 weren't significantly worse.
206
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000207Historical: Reimer Behrends contributed the idea of using a polynomial-based
Tim Peterseb28ef22001-06-02 05:27:19 +0000208approach, using repeated multiplication by x in GF(2**n) where an irreducible
209polynomial for each table size was chosen such that x was a primitive root.
210Christian Tismer later extended that to use division by x instead, as an
211efficient way to get the high bits of the hash code into play. This scheme
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000212also gave excellent collision statistics, but was more expensive: two
213if-tests were required inside the loop; computing "the next" index took about
214the same number of operations but without as much potential parallelism
215(e.g., computing 5*j can go on at the same time as computing 1+perturb in the
216above, and then shifting perturb can be done while the table index is being
217masked); and the PyDictObject struct required a member to hold the table's
218polynomial. In Tim's experiments the current scheme ran faster, produced
219equally good collision statistics, needed less code & used less memory.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000220
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000221*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000222
Fred Drake1bff34a2000-08-31 19:31:38 +0000223/* forward declarations */
Victor Stinner742da042016-09-07 17:40:12 -0700224static Py_ssize_t lookdict(PyDictObject *mp, PyObject *key,
225 Py_hash_t hash, PyObject ***value_addr,
226 Py_ssize_t *hashpos);
227static Py_ssize_t lookdict_unicode(PyDictObject *mp, PyObject *key,
228 Py_hash_t hash, PyObject ***value_addr,
229 Py_ssize_t *hashpos);
230static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400231lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
Victor Stinner742da042016-09-07 17:40:12 -0700232 Py_hash_t hash, PyObject ***value_addr,
233 Py_ssize_t *hashpos);
234static Py_ssize_t lookdict_split(PyDictObject *mp, PyObject *key,
235 Py_hash_t hash, PyObject ***value_addr,
236 Py_ssize_t *hashpos);
Fred Drake1bff34a2000-08-31 19:31:38 +0000237
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400238static int dictresize(PyDictObject *mp, Py_ssize_t minused);
Tim Petersdea48ec2001-05-22 20:40:22 +0000239
Benjamin Peterson3c569292016-09-08 13:16:41 -0700240/*Global counter used to set ma_version_tag field of dictionary.
Victor Stinner3b6a6b42016-09-08 12:51:24 -0700241 * It is incremented each time that a dictionary is created and each
242 * time that a dictionary is modified. */
243static uint64_t pydict_global_version = 0;
244
245#define DICT_NEXT_VERSION() (++pydict_global_version)
246
Victor Stinner742da042016-09-07 17:40:12 -0700247/* Dictionary reuse scheme to save calls to malloc and free */
Christian Heimes2202f872008-02-06 14:31:34 +0000248#ifndef PyDict_MAXFREELIST
249#define PyDict_MAXFREELIST 80
250#endif
251static PyDictObject *free_list[PyDict_MAXFREELIST];
252static int numfree = 0;
Victor Stinner742da042016-09-07 17:40:12 -0700253static PyDictKeysObject *keys_free_list[PyDict_MAXFREELIST];
254static int numfreekeys = 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000255
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300256#include "clinic/dictobject.c.h"
257
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100258int
259PyDict_ClearFreeList(void)
Christian Heimes77c02eb2008-02-09 02:18:51 +0000260{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 PyDictObject *op;
Victor Stinner742da042016-09-07 17:40:12 -0700262 int ret = numfree + numfreekeys;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 while (numfree) {
264 op = free_list[--numfree];
265 assert(PyDict_CheckExact(op));
266 PyObject_GC_Del(op);
267 }
Victor Stinner742da042016-09-07 17:40:12 -0700268 while (numfreekeys) {
269 PyObject_FREE(keys_free_list[--numfreekeys]);
270 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100271 return ret;
272}
273
David Malcolm49526f42012-06-22 14:55:41 -0400274/* Print summary info about the state of the optimized allocator */
275void
276_PyDict_DebugMallocStats(FILE *out)
277{
278 _PyDebugAllocatorStats(out,
279 "free PyDictObject", numfree, sizeof(PyDictObject));
280}
281
282
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100283void
284PyDict_Fini(void)
285{
286 PyDict_ClearFreeList();
Christian Heimes77c02eb2008-02-09 02:18:51 +0000287}
288
Victor Stinner742da042016-09-07 17:40:12 -0700289#define DK_SIZE(dk) ((dk)->dk_size)
290#if SIZEOF_VOID_P > 4
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700291#define DK_IXSIZE(dk) \
292 (DK_SIZE(dk) <= 0xff ? \
293 1 : DK_SIZE(dk) <= 0xffff ? \
294 2 : DK_SIZE(dk) <= 0xffffffff ? \
Benjamin Peterson3c569292016-09-08 13:16:41 -0700295 4 : sizeof(int64_t))
Victor Stinner742da042016-09-07 17:40:12 -0700296#else
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700297#define DK_IXSIZE(dk) \
298 (DK_SIZE(dk) <= 0xff ? \
299 1 : DK_SIZE(dk) <= 0xffff ? \
Benjamin Peterson3c569292016-09-08 13:16:41 -0700300 2 : sizeof(int32_t))
Victor Stinner742da042016-09-07 17:40:12 -0700301#endif
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700302#define DK_ENTRIES(dk) \
Benjamin Peterson186122e2016-09-08 12:20:12 -0700303 ((PyDictKeyEntry*)(&(dk)->dk_indices.as_1[DK_SIZE(dk) * DK_IXSIZE(dk)]))
Victor Stinner742da042016-09-07 17:40:12 -0700304
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200305#define DK_DEBUG_INCREF _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA
306#define DK_DEBUG_DECREF _Py_DEC_REFTOTAL _Py_REF_DEBUG_COMMA
307
308#define DK_INCREF(dk) (DK_DEBUG_INCREF ++(dk)->dk_refcnt)
309#define DK_DECREF(dk) if (DK_DEBUG_DECREF (--(dk)->dk_refcnt) == 0) free_keys_object(dk)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400310#define DK_MASK(dk) (((dk)->dk_size)-1)
311#define IS_POWER_OF_2(x) (((x) & (x-1)) == 0)
312
Victor Stinner742da042016-09-07 17:40:12 -0700313/* lookup indices. returns DKIX_EMPTY, DKIX_DUMMY, or ix >=0 */
Benjamin Peterson73222252016-09-08 09:58:47 -0700314static inline Py_ssize_t
Victor Stinner742da042016-09-07 17:40:12 -0700315dk_get_index(PyDictKeysObject *keys, Py_ssize_t i)
316{
317 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700318 Py_ssize_t ix;
319
Victor Stinner742da042016-09-07 17:40:12 -0700320 if (s <= 0xff) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700321 int8_t *indices = keys->dk_indices.as_1;
Victor Stinner208857e2016-09-08 11:35:46 -0700322 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700323 }
324 else if (s <= 0xffff) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700325 int16_t *indices = keys->dk_indices.as_2;
Victor Stinner208857e2016-09-08 11:35:46 -0700326 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700327 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700328#if SIZEOF_VOID_P > 4
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300329 else if (s > 0xffffffff) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700330 int64_t *indices = keys->dk_indices.as_8;
Victor Stinner208857e2016-09-08 11:35:46 -0700331 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700332 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700333#endif
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300334 else {
335 int32_t *indices = keys->dk_indices.as_4;
336 ix = indices[i];
337 }
Victor Stinner71211e32016-09-08 10:52:46 -0700338 assert(ix >= DKIX_DUMMY);
339 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700340}
341
342/* write to indices. */
Benjamin Peterson73222252016-09-08 09:58:47 -0700343static inline void
Victor Stinner742da042016-09-07 17:40:12 -0700344dk_set_index(PyDictKeysObject *keys, Py_ssize_t i, Py_ssize_t ix)
345{
346 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700347
348 assert(ix >= DKIX_DUMMY);
349
Victor Stinner742da042016-09-07 17:40:12 -0700350 if (s <= 0xff) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700351 int8_t *indices = keys->dk_indices.as_1;
Victor Stinner71211e32016-09-08 10:52:46 -0700352 assert(ix <= 0x7f);
Victor Stinner208857e2016-09-08 11:35:46 -0700353 indices[i] = (char)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700354 }
355 else if (s <= 0xffff) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700356 int16_t *indices = keys->dk_indices.as_2;
Victor Stinner71211e32016-09-08 10:52:46 -0700357 assert(ix <= 0x7fff);
Victor Stinner208857e2016-09-08 11:35:46 -0700358 indices[i] = (int16_t)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700359 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700360#if SIZEOF_VOID_P > 4
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300361 else if (s > 0xffffffff) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700362 int64_t *indices = keys->dk_indices.as_8;
Victor Stinner208857e2016-09-08 11:35:46 -0700363 indices[i] = ix;
Victor Stinner742da042016-09-07 17:40:12 -0700364 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700365#endif
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300366 else {
367 int32_t *indices = keys->dk_indices.as_4;
368 assert(ix <= 0x7fffffff);
369 indices[i] = (int32_t)ix;
370 }
Victor Stinner742da042016-09-07 17:40:12 -0700371}
372
373
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200374/* USABLE_FRACTION is the maximum dictionary load.
Victor Stinner742da042016-09-07 17:40:12 -0700375 * Increasing this ratio makes dictionaries more dense resulting in more
376 * collisions. Decreasing it improves sparseness at the expense of spreading
377 * indices over more cache lines and at the cost of total memory consumed.
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200378 *
379 * USABLE_FRACTION must obey the following:
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400380 * (0 < USABLE_FRACTION(n) < n) for all n >= 2
381 *
Victor Stinner742da042016-09-07 17:40:12 -0700382 * USABLE_FRACTION should be quick to calculate.
383 * Fractions around 1/2 to 2/3 seem to work well in practice.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400384 */
Victor Stinner742da042016-09-07 17:40:12 -0700385#define USABLE_FRACTION(n) (((n) << 1)/3)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400386
Victor Stinner742da042016-09-07 17:40:12 -0700387/* ESTIMATE_SIZE is reverse function of USABLE_FRACTION.
388 * This can be used to reserve enough size to insert n entries without
389 * resizing.
390 */
391#define ESTIMATE_SIZE(n) (((n)*3) >> 1)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400392
Victor Stinner742da042016-09-07 17:40:12 -0700393/* Alternative fraction that is otherwise close enough to 2n/3 to make
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400394 * little difference. 8 * 2/3 == 8 * 5/8 == 5. 16 * 2/3 == 16 * 5/8 == 10.
395 * 32 * 2/3 = 21, 32 * 5/8 = 20.
396 * Its advantage is that it is faster to compute on machines with slow division.
397 * #define USABLE_FRACTION(n) (((n) >> 1) + ((n) >> 2) - ((n) >> 3))
Victor Stinner742da042016-09-07 17:40:12 -0700398 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400399
Victor Stinnera9f61a52013-07-16 22:17:26 +0200400/* GROWTH_RATE. Growth rate upon hitting maximum load.
401 * Currently set to used*2 + capacity/2.
402 * This means that dicts double in size when growing without deletions,
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700403 * but have more head room when the number of deletions is on a par with the
404 * number of insertions.
405 * Raising this to used*4 doubles memory consumption depending on the size of
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200406 * the dictionary, but results in half the number of resizes, less effort to
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700407 * resize.
408 * GROWTH_RATE was set to used*4 up to version 3.2.
409 * GROWTH_RATE was set to used*2 in version 3.3.0
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200410 */
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700411#define GROWTH_RATE(d) (((d)->ma_used*2)+((d)->ma_keys->dk_size>>1))
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400412
413#define ENSURE_ALLOWS_DELETIONS(d) \
414 if ((d)->ma_keys->dk_lookup == lookdict_unicode_nodummy) { \
415 (d)->ma_keys->dk_lookup = lookdict_unicode; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400417
418/* This immutable, empty PyDictKeysObject is used for PyDict_Clear()
419 * (which cannot fail and thus can do no allocation).
420 */
421static PyDictKeysObject empty_keys_struct = {
422 2, /* dk_refcnt 1 for this struct, 1 for dummy_struct */
423 1, /* dk_size */
424 lookdict_split, /* dk_lookup */
425 0, /* dk_usable (immutable) */
Victor Stinner742da042016-09-07 17:40:12 -0700426 0, /* dk_nentries */
Benjamin Peterson186122e2016-09-08 12:20:12 -0700427 .dk_indices = { .as_1 = {DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY,
428 DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY}},
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400429};
430
431static PyObject *empty_values[1] = { NULL };
432
433#define Py_EMPTY_KEYS &empty_keys_struct
434
Victor Stinner611b0fa2016-09-14 15:02:01 +0200435/* Uncomment to check the dict content in _PyDict_CheckConsistency() */
436/* #define DEBUG_PYDICT */
437
438
439#ifdef Py_DEBUG
440static int
441_PyDict_CheckConsistency(PyDictObject *mp)
442{
443 PyDictKeysObject *keys = mp->ma_keys;
444 int splitted = _PyDict_HasSplitTable(mp);
445 Py_ssize_t usable = USABLE_FRACTION(keys->dk_size);
446#ifdef DEBUG_PYDICT
447 PyDictKeyEntry *entries = DK_ENTRIES(keys);
448 Py_ssize_t i;
449#endif
450
451 assert(0 <= mp->ma_used && mp->ma_used <= usable);
452 assert(IS_POWER_OF_2(keys->dk_size));
453 assert(0 <= keys->dk_usable
454 && keys->dk_usable <= usable);
455 assert(0 <= keys->dk_nentries
456 && keys->dk_nentries <= usable);
457 assert(keys->dk_usable + keys->dk_nentries <= usable);
458
459 if (!splitted) {
460 /* combined table */
461 assert(keys->dk_refcnt == 1);
462 }
463
464#ifdef DEBUG_PYDICT
465 for (i=0; i < keys->dk_size; i++) {
466 Py_ssize_t ix = dk_get_index(keys, i);
467 assert(DKIX_DUMMY <= ix && ix <= usable);
468 }
469
470 for (i=0; i < usable; i++) {
471 PyDictKeyEntry *entry = &entries[i];
472 PyObject *key = entry->me_key;
473
474 if (key != NULL) {
475 if (PyUnicode_CheckExact(key)) {
476 Py_hash_t hash = ((PyASCIIObject *)key)->hash;
477 assert(hash != -1);
478 assert(entry->me_hash == hash);
479 }
480 else {
481 /* test_dict fails if PyObject_Hash() is called again */
482 assert(entry->me_hash != -1);
483 }
484 if (!splitted) {
485 assert(entry->me_value != NULL);
486 }
487 }
488
489 if (splitted) {
490 assert(entry->me_value == NULL);
491 }
492 }
493
494 if (splitted) {
495 /* splitted table */
496 for (i=0; i < mp->ma_used; i++) {
497 assert(mp->ma_values[i] != NULL);
498 }
499 }
500#endif
501
502 return 1;
503}
504#endif
505
506
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400507static PyDictKeysObject *new_keys_object(Py_ssize_t size)
508{
509 PyDictKeysObject *dk;
Victor Stinner742da042016-09-07 17:40:12 -0700510 Py_ssize_t es, usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400511
Victor Stinner742da042016-09-07 17:40:12 -0700512 assert(size >= PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400513 assert(IS_POWER_OF_2(size));
Victor Stinner742da042016-09-07 17:40:12 -0700514
515 usable = USABLE_FRACTION(size);
516 if (size <= 0xff) {
517 es = 1;
518 }
519 else if (size <= 0xffff) {
520 es = 2;
521 }
522#if SIZEOF_VOID_P > 4
523 else if (size <= 0xffffffff) {
524 es = 4;
525 }
526#endif
527 else {
528 es = sizeof(Py_ssize_t);
529 }
530
531 if (size == PyDict_MINSIZE && numfreekeys > 0) {
532 dk = keys_free_list[--numfreekeys];
533 }
534 else {
Victor Stinner98ee9d52016-09-08 09:33:56 -0700535 dk = PyObject_MALLOC(sizeof(PyDictKeysObject)
536 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
537 + es * size
538 + sizeof(PyDictKeyEntry) * usable);
Victor Stinner742da042016-09-07 17:40:12 -0700539 if (dk == NULL) {
540 PyErr_NoMemory();
541 return NULL;
542 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400543 }
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200544 DK_DEBUG_INCREF dk->dk_refcnt = 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400545 dk->dk_size = size;
Victor Stinner742da042016-09-07 17:40:12 -0700546 dk->dk_usable = usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400547 dk->dk_lookup = lookdict_unicode_nodummy;
Victor Stinner742da042016-09-07 17:40:12 -0700548 dk->dk_nentries = 0;
Benjamin Peterson186122e2016-09-08 12:20:12 -0700549 memset(&dk->dk_indices.as_1[0], 0xff, es * size);
Victor Stinner742da042016-09-07 17:40:12 -0700550 memset(DK_ENTRIES(dk), 0, sizeof(PyDictKeyEntry) * usable);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400551 return dk;
552}
553
554static void
555free_keys_object(PyDictKeysObject *keys)
556{
Victor Stinner742da042016-09-07 17:40:12 -0700557 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400558 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -0700559 for (i = 0, n = keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400560 Py_XDECREF(entries[i].me_key);
561 Py_XDECREF(entries[i].me_value);
562 }
Victor Stinner742da042016-09-07 17:40:12 -0700563 if (keys->dk_size == PyDict_MINSIZE && numfreekeys < PyDict_MAXFREELIST) {
564 keys_free_list[numfreekeys++] = keys;
565 return;
566 }
Raymond Hettingerce5179f2016-01-31 08:56:21 -0800567 PyObject_FREE(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400568}
569
570#define new_values(size) PyMem_NEW(PyObject *, size)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400571#define free_values(values) PyMem_FREE(values)
572
573/* Consumes a reference to the keys object */
574static PyObject *
575new_dict(PyDictKeysObject *keys, PyObject **values)
576{
577 PyDictObject *mp;
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200578 assert(keys != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 if (numfree) {
580 mp = free_list[--numfree];
581 assert (mp != NULL);
582 assert (Py_TYPE(mp) == &PyDict_Type);
583 _Py_NewReference((PyObject *)mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400585 else {
586 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
587 if (mp == NULL) {
588 DK_DECREF(keys);
589 free_values(values);
590 return NULL;
591 }
592 }
593 mp->ma_keys = keys;
594 mp->ma_values = values;
595 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -0700596 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +0200597 assert(_PyDict_CheckConsistency(mp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000599}
600
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400601/* Consumes a reference to the keys object */
602static PyObject *
603new_dict_with_shared_keys(PyDictKeysObject *keys)
604{
605 PyObject **values;
606 Py_ssize_t i, size;
607
Victor Stinner742da042016-09-07 17:40:12 -0700608 size = USABLE_FRACTION(DK_SIZE(keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400609 values = new_values(size);
610 if (values == NULL) {
611 DK_DECREF(keys);
612 return PyErr_NoMemory();
613 }
614 for (i = 0; i < size; i++) {
615 values[i] = NULL;
616 }
617 return new_dict(keys, values);
618}
619
620PyObject *
621PyDict_New(void)
622{
Victor Stinner742da042016-09-07 17:40:12 -0700623 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200624 if (keys == NULL)
625 return NULL;
626 return new_dict(keys, NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400627}
628
Victor Stinner742da042016-09-07 17:40:12 -0700629/* Search index of hash table from offset of entry table */
630static Py_ssize_t
631lookdict_index(PyDictKeysObject *k, Py_hash_t hash, Py_ssize_t index)
632{
633 size_t i, perturb;
634 size_t mask = DK_MASK(k);
635 Py_ssize_t ix;
636
637 i = (size_t)hash & mask;
638 ix = dk_get_index(k, i);
639 if (ix == index) {
640 return i;
641 }
642 if (ix == DKIX_EMPTY) {
643 return DKIX_EMPTY;
644 }
645
646 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
647 i = mask & ((i << 2) + i + perturb + 1);
648 ix = dk_get_index(k, i);
649 if (ix == index) {
650 return i;
651 }
652 if (ix == DKIX_EMPTY) {
653 return DKIX_EMPTY;
654 }
655 }
656 assert(0); /* NOT REACHED */
657 return DKIX_ERROR;
658}
659
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000660/*
661The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000662This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000663Open addressing is preferred over chaining since the link overhead for
664chaining would be substantial (100% with typical malloc overhead).
665
Tim Peterseb28ef22001-06-02 05:27:19 +0000666The initial probe index is computed as hash mod the table size. Subsequent
667probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000668
669All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000670
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000671The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000672contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000673Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000674
Victor Stinner742da042016-09-07 17:40:12 -0700675lookdict() is general-purpose, and may return DKIX_ERROR if (and only if) a
Victor Stinnera4348cc2016-09-08 12:01:25 -0700676comparison raises an exception.
Guido van Rossum89d8c602007-09-18 17:26:56 +0000677lookdict_unicode() below is specialized to string keys, comparison of which can
Victor Stinner742da042016-09-07 17:40:12 -0700678never raise an exception; that function can never return DKIX_ERROR.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400679lookdict_unicode_nodummy is further specialized for string keys that cannot be
680the <dummy> value.
Victor Stinner742da042016-09-07 17:40:12 -0700681For both, when the key isn't found a DKIX_EMPTY is returned. hashpos returns
682where the key index should be inserted.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000683*/
Victor Stinner742da042016-09-07 17:40:12 -0700684static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400685lookdict(PyDictObject *mp, PyObject *key,
Victor Stinner742da042016-09-07 17:40:12 -0700686 Py_hash_t hash, PyObject ***value_addr, Py_ssize_t *hashpos)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000687{
Victor Stinner742da042016-09-07 17:40:12 -0700688 size_t i, perturb, mask;
689 Py_ssize_t ix, freeslot;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200690 int cmp;
Victor Stinner742da042016-09-07 17:40:12 -0700691 PyDictKeysObject *dk;
692 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000694
Antoine Pitrou9a234902012-05-13 20:48:01 +0200695top:
Victor Stinner742da042016-09-07 17:40:12 -0700696 dk = mp->ma_keys;
697 mask = DK_MASK(dk);
698 ep0 = DK_ENTRIES(dk);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700700
701 ix = dk_get_index(dk, i);
702 if (ix == DKIX_EMPTY) {
703 if (hashpos != NULL)
704 *hashpos = i;
705 *value_addr = NULL;
706 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400707 }
Victor Stinner742da042016-09-07 17:40:12 -0700708 if (ix == DKIX_DUMMY) {
709 freeslot = i;
710 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 else {
Victor Stinner742da042016-09-07 17:40:12 -0700712 ep = &ep0[ix];
713 if (ep->me_key == key) {
714 *value_addr = &ep->me_value;
715 if (hashpos != NULL)
716 *hashpos = i;
717 return ix;
718 }
719 if (ep->me_key != NULL && ep->me_hash == hash) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 startkey = ep->me_key;
721 Py_INCREF(startkey);
722 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
723 Py_DECREF(startkey);
724 if (cmp < 0)
Victor Stinner742da042016-09-07 17:40:12 -0700725 return DKIX_ERROR;
726 if (dk == mp->ma_keys && ep->me_key == startkey) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400727 if (cmp > 0) {
728 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700729 if (hashpos != NULL)
730 *hashpos = i;
731 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400732 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 }
734 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200735 /* The dict was mutated, restart */
736 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 }
738 }
Victor Stinner742da042016-09-07 17:40:12 -0700739 freeslot = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 }
Tim Peters15d49292001-05-27 07:39:22 +0000741
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
Victor Stinner742da042016-09-07 17:40:12 -0700743 i = ((i << 2) + i + perturb + 1) & mask;
744 ix = dk_get_index(dk, i);
745 if (ix == DKIX_EMPTY) {
746 if (hashpos != NULL) {
747 *hashpos = (freeslot == -1) ? (Py_ssize_t)i : freeslot;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400748 }
Victor Stinner742da042016-09-07 17:40:12 -0700749 *value_addr = NULL;
750 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400751 }
Victor Stinner742da042016-09-07 17:40:12 -0700752 if (ix == DKIX_DUMMY) {
753 if (freeslot == -1)
754 freeslot = i;
755 continue;
756 }
757 ep = &ep0[ix];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400758 if (ep->me_key == key) {
Victor Stinner742da042016-09-07 17:40:12 -0700759 if (hashpos != NULL) {
760 *hashpos = i;
761 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400762 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700763 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400764 }
Victor Stinner742da042016-09-07 17:40:12 -0700765 if (ep->me_hash == hash && ep->me_key != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 startkey = ep->me_key;
767 Py_INCREF(startkey);
768 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
769 Py_DECREF(startkey);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400770 if (cmp < 0) {
771 *value_addr = NULL;
Victor Stinner742da042016-09-07 17:40:12 -0700772 return DKIX_ERROR;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400773 }
Victor Stinner742da042016-09-07 17:40:12 -0700774 if (dk == mp->ma_keys && ep->me_key == startkey) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400775 if (cmp > 0) {
Victor Stinner742da042016-09-07 17:40:12 -0700776 if (hashpos != NULL) {
777 *hashpos = i;
778 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400779 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700780 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400781 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 }
783 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200784 /* The dict was mutated, restart */
785 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 }
787 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 }
789 assert(0); /* NOT REACHED */
790 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000791}
792
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400793/* Specialized version for string-only keys */
Victor Stinner742da042016-09-07 17:40:12 -0700794static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400795lookdict_unicode(PyDictObject *mp, PyObject *key,
Victor Stinner742da042016-09-07 17:40:12 -0700796 Py_hash_t hash, PyObject ***value_addr, Py_ssize_t *hashpos)
Fred Drake1bff34a2000-08-31 19:31:38 +0000797{
Victor Stinner742da042016-09-07 17:40:12 -0700798 size_t i, perturb;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200799 size_t mask = DK_MASK(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -0700800 Py_ssize_t ix, freeslot;
801 PyDictKeyEntry *ep, *ep0 = DK_ENTRIES(mp->ma_keys);
Fred Drake1bff34a2000-08-31 19:31:38 +0000802
Victor Stinner742da042016-09-07 17:40:12 -0700803 assert(mp->ma_values == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 /* Make sure this function doesn't have to handle non-unicode keys,
805 including subclasses of str; e.g., one reason to subclass
806 unicodes is to override __eq__, and for speed we don't cater to
807 that here. */
808 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400809 mp->ma_keys->dk_lookup = lookdict;
Victor Stinner742da042016-09-07 17:40:12 -0700810 return lookdict(mp, key, hash, value_addr, hashpos);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 }
Mark Dickinson57e683e2011-09-24 18:18:40 +0100812 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700813 ix = dk_get_index(mp->ma_keys, i);
814 if (ix == DKIX_EMPTY) {
815 if (hashpos != NULL)
816 *hashpos = i;
817 *value_addr = NULL;
818 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400819 }
Victor Stinner742da042016-09-07 17:40:12 -0700820 if (ix == DKIX_DUMMY) {
821 freeslot = i;
822 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 else {
Victor Stinner742da042016-09-07 17:40:12 -0700824 ep = &ep0[ix];
825 /* only split table can be ix != DKIX_DUMMY && me_key == NULL */
826 assert(ep->me_key != NULL);
827 if (ep->me_key == key || (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
828 if (hashpos != NULL)
829 *hashpos = i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400830 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700831 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400832 }
Victor Stinner742da042016-09-07 17:40:12 -0700833 freeslot = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 }
Tim Peters15d49292001-05-27 07:39:22 +0000835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
Victor Stinner742da042016-09-07 17:40:12 -0700837 i = mask & ((i << 2) + i + perturb + 1);
838 ix = dk_get_index(mp->ma_keys, i);
839 if (ix == DKIX_EMPTY) {
840 if (hashpos != NULL) {
841 *hashpos = (freeslot == -1) ? (Py_ssize_t)i : freeslot;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400842 }
Victor Stinner742da042016-09-07 17:40:12 -0700843 *value_addr = NULL;
844 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400845 }
Victor Stinner742da042016-09-07 17:40:12 -0700846 if (ix == DKIX_DUMMY) {
847 if (freeslot == -1)
848 freeslot = i;
849 continue;
850 }
851 ep = &ep0[ix];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 if (ep->me_key == key
853 || (ep->me_hash == hash
Victor Stinner742da042016-09-07 17:40:12 -0700854 && ep->me_key != NULL
855 && unicode_eq(ep->me_key, key))) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400856 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700857 if (hashpos != NULL) {
858 *hashpos = i;
859 }
860 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400861 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 }
863 assert(0); /* NOT REACHED */
864 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000865}
866
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400867/* Faster version of lookdict_unicode when it is known that no <dummy> keys
868 * will be present. */
Victor Stinner742da042016-09-07 17:40:12 -0700869static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400870lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
Victor Stinner742da042016-09-07 17:40:12 -0700871 Py_hash_t hash, PyObject ***value_addr,
872 Py_ssize_t *hashpos)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400873{
Victor Stinner742da042016-09-07 17:40:12 -0700874 size_t i, perturb;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200875 size_t mask = DK_MASK(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -0700876 Py_ssize_t ix;
877 PyDictKeyEntry *ep, *ep0 = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400878
Victor Stinner742da042016-09-07 17:40:12 -0700879 assert(mp->ma_values == NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400880 /* Make sure this function doesn't have to handle non-unicode keys,
881 including subclasses of str; e.g., one reason to subclass
882 unicodes is to override __eq__, and for speed we don't cater to
883 that here. */
884 if (!PyUnicode_CheckExact(key)) {
885 mp->ma_keys->dk_lookup = lookdict;
Victor Stinner742da042016-09-07 17:40:12 -0700886 return lookdict(mp, key, hash, value_addr, hashpos);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400887 }
888 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700889 ix = dk_get_index(mp->ma_keys, i);
890 assert (ix != DKIX_DUMMY);
891 if (ix == DKIX_EMPTY) {
892 if (hashpos != NULL)
893 *hashpos = i;
894 *value_addr = NULL;
895 return DKIX_EMPTY;
896 }
897 ep = &ep0[ix];
Victor Stinnerdee6e252016-09-08 11:16:07 -0700898 assert(ep->me_key != NULL);
899 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700900 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400901 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700902 if (hashpos != NULL)
903 *hashpos = i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400904 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700905 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400906 }
907 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
Victor Stinner742da042016-09-07 17:40:12 -0700908 i = mask & ((i << 2) + i + perturb + 1);
909 ix = dk_get_index(mp->ma_keys, i);
910 assert (ix != DKIX_DUMMY);
911 if (ix == DKIX_EMPTY) {
912 if (hashpos != NULL)
913 *hashpos = i;
914 *value_addr = NULL;
915 return DKIX_EMPTY;
916 }
917 ep = &ep0[ix];
918 assert(ep->me_key != NULL && PyUnicode_CheckExact(ep->me_key));
919 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400920 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700921 if (hashpos != NULL)
922 *hashpos = i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400923 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700924 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400925 }
926 }
927 assert(0); /* NOT REACHED */
928 return 0;
929}
930
931/* Version of lookdict for split tables.
932 * All split tables and only split tables use this lookup function.
933 * Split tables only contain unicode keys and no dummy keys,
934 * so algorithm is the same as lookdict_unicode_nodummy.
935 */
Victor Stinner742da042016-09-07 17:40:12 -0700936static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400937lookdict_split(PyDictObject *mp, PyObject *key,
Victor Stinner742da042016-09-07 17:40:12 -0700938 Py_hash_t hash, PyObject ***value_addr, Py_ssize_t *hashpos)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400939{
Victor Stinner742da042016-09-07 17:40:12 -0700940 size_t i, perturb;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200941 size_t mask = DK_MASK(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -0700942 Py_ssize_t ix;
943 PyDictKeyEntry *ep, *ep0 = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400944
Victor Stinner742da042016-09-07 17:40:12 -0700945 /* mp must split table */
946 assert(mp->ma_values != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400947 if (!PyUnicode_CheckExact(key)) {
Victor Stinner742da042016-09-07 17:40:12 -0700948 ix = lookdict(mp, key, hash, value_addr, hashpos);
949 if (ix >= 0) {
950 *value_addr = &mp->ma_values[ix];
951 }
952 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400953 }
Victor Stinner742da042016-09-07 17:40:12 -0700954
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400955 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700956 ix = dk_get_index(mp->ma_keys, i);
957 if (ix == DKIX_EMPTY) {
958 if (hashpos != NULL)
959 *hashpos = i;
960 *value_addr = NULL;
961 return DKIX_EMPTY;
962 }
963 assert(ix >= 0);
964 ep = &ep0[ix];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400965 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700966 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400967 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700968 if (hashpos != NULL)
969 *hashpos = i;
970 *value_addr = &mp->ma_values[ix];
971 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400972 }
973 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
Victor Stinner742da042016-09-07 17:40:12 -0700974 i = mask & ((i << 2) + i + perturb + 1);
975 ix = dk_get_index(mp->ma_keys, i);
976 if (ix == DKIX_EMPTY) {
977 if (hashpos != NULL)
978 *hashpos = i;
979 *value_addr = NULL;
980 return DKIX_EMPTY;
981 }
982 assert(ix >= 0);
983 ep = &ep0[ix];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400984 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700985 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400986 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700987 if (hashpos != NULL)
988 *hashpos = i;
989 *value_addr = &mp->ma_values[ix];
990 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400991 }
992 }
993 assert(0); /* NOT REACHED */
994 return 0;
995}
996
Benjamin Petersonfb886362010-04-24 18:21:17 +0000997int
998_PyDict_HasOnlyStringKeys(PyObject *dict)
999{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 Py_ssize_t pos = 0;
1001 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +00001002 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001004 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 return 1;
1006 while (PyDict_Next(dict, &pos, &key, &value))
1007 if (!PyUnicode_Check(key))
1008 return 0;
1009 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +00001010}
1011
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001012#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 do { \
1014 if (!_PyObject_GC_IS_TRACKED(mp)) { \
1015 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
1016 _PyObject_GC_MAY_BE_TRACKED(value)) { \
1017 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 } \
1019 } \
1020 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001021
1022void
1023_PyDict_MaybeUntrack(PyObject *op)
1024{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 PyDictObject *mp;
1026 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -07001027 Py_ssize_t i, numentries;
1028 PyDictKeyEntry *ep0;
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
1031 return;
1032
1033 mp = (PyDictObject *) op;
Victor Stinner742da042016-09-07 17:40:12 -07001034 ep0 = DK_ENTRIES(mp->ma_keys);
1035 numentries = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001036 if (_PyDict_HasSplitTable(mp)) {
Victor Stinner742da042016-09-07 17:40:12 -07001037 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001038 if ((value = mp->ma_values[i]) == NULL)
1039 continue;
1040 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
Victor Stinner742da042016-09-07 17:40:12 -07001041 assert(!_PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001042 return;
1043 }
1044 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001046 else {
Victor Stinner742da042016-09-07 17:40:12 -07001047 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001048 if ((value = ep0[i].me_value) == NULL)
1049 continue;
1050 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
1051 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
1052 return;
1053 }
1054 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001056}
1057
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001058/* Internal function to find slot for an item from its hash
Victor Stinner3c336c52016-09-12 14:17:40 +02001059 when it is known that the key is not present in the dict.
1060
1061 The dict must be combined. */
Victor Stinner99264802016-09-13 09:38:29 +02001062static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001063find_empty_slot(PyDictObject *mp, PyObject *key, Py_hash_t hash,
Victor Stinner742da042016-09-07 17:40:12 -07001064 PyObject ***value_addr, Py_ssize_t *hashpos)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001065{
Victor Stinner742da042016-09-07 17:40:12 -07001066 size_t i, perturb;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001067 size_t mask = DK_MASK(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07001068 Py_ssize_t ix;
1069 PyDictKeyEntry *ep, *ep0 = DK_ENTRIES(mp->ma_keys);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001070
Victor Stinner3c336c52016-09-12 14:17:40 +02001071 assert(!_PyDict_HasSplitTable(mp));
Victor Stinner742da042016-09-07 17:40:12 -07001072 assert(hashpos != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001073 assert(key != NULL);
Victor Stinner3c336c52016-09-12 14:17:40 +02001074
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001075 if (!PyUnicode_CheckExact(key))
1076 mp->ma_keys->dk_lookup = lookdict;
1077 i = hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -07001078 ix = dk_get_index(mp->ma_keys, i);
1079 for (perturb = hash; ix != DKIX_EMPTY; perturb >>= PERTURB_SHIFT) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001080 i = (i << 2) + i + perturb + 1;
Victor Stinner742da042016-09-07 17:40:12 -07001081 ix = dk_get_index(mp->ma_keys, i & mask);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 }
Victor Stinner742da042016-09-07 17:40:12 -07001083 ep = &ep0[mp->ma_keys->dk_nentries];
1084 *hashpos = i & mask;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001085 assert(ep->me_value == NULL);
Victor Stinner99264802016-09-13 09:38:29 +02001086 *value_addr = &ep->me_value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001087}
1088
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001089static int
1090insertion_resize(PyDictObject *mp)
1091{
Raymond Hettinger36f74aa2013-05-17 03:01:13 -07001092 return dictresize(mp, GROWTH_RATE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001093}
Antoine Pitroue965d972012-02-27 00:45:12 +01001094
1095/*
1096Internal routine to insert a new item into the table.
1097Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +01001098Returns -1 if an error occurred, or 0 on success.
1099*/
1100static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001101insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001102{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001103 PyObject *old_value;
1104 PyObject **value_addr;
Victor Stinner742da042016-09-07 17:40:12 -07001105 PyDictKeyEntry *ep, *ep0;
1106 Py_ssize_t hashpos, ix;
Antoine Pitroue965d972012-02-27 00:45:12 +01001107
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001108 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
1109 if (insertion_resize(mp) < 0)
1110 return -1;
1111 }
1112
Victor Stinner742da042016-09-07 17:40:12 -07001113 ix = mp->ma_keys->dk_lookup(mp, key, hash, &value_addr, &hashpos);
1114 if (ix == DKIX_ERROR) {
Antoine Pitroue965d972012-02-27 00:45:12 +01001115 return -1;
1116 }
Victor Stinner742da042016-09-07 17:40:12 -07001117
Antoine Pitroud6967322014-10-18 00:35:00 +02001118 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
Benjamin Petersona6f195e2012-04-30 10:23:40 -04001119 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001120 MAINTAIN_TRACKING(mp, key, value);
Victor Stinner742da042016-09-07 17:40:12 -07001121
1122 /* When insertion order is different from shared key, we can't share
1123 * the key anymore. Convert this instance to combine table.
1124 */
1125 if (_PyDict_HasSplitTable(mp) &&
1126 ((ix >= 0 && *value_addr == NULL && mp->ma_used != ix) ||
1127 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
1128 if (insertion_resize(mp) < 0) {
1129 Py_DECREF(value);
1130 return -1;
1131 }
1132 find_empty_slot(mp, key, hash, &value_addr, &hashpos);
1133 ix = DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001134 }
Victor Stinner742da042016-09-07 17:40:12 -07001135
1136 if (ix == DKIX_EMPTY) {
1137 /* Insert into new slot. */
1138 if (mp->ma_keys->dk_usable <= 0) {
1139 /* Need to resize. */
1140 if (insertion_resize(mp) < 0) {
1141 Py_DECREF(value);
1142 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001143 }
Victor Stinner742da042016-09-07 17:40:12 -07001144 find_empty_slot(mp, key, hash, &value_addr, &hashpos);
1145 }
1146 ep0 = DK_ENTRIES(mp->ma_keys);
1147 ep = &ep0[mp->ma_keys->dk_nentries];
1148 dk_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
1149 Py_INCREF(key);
1150 ep->me_key = key;
1151 ep->me_hash = hash;
1152 if (mp->ma_values) {
1153 assert (mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
1154 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001155 }
1156 else {
Victor Stinner742da042016-09-07 17:40:12 -07001157 ep->me_value = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001158 }
1159 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001160 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07001161 mp->ma_keys->dk_usable--;
1162 mp->ma_keys->dk_nentries++;
1163 assert(mp->ma_keys->dk_usable >= 0);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001164 assert(_PyDict_CheckConsistency(mp));
Victor Stinner742da042016-09-07 17:40:12 -07001165 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001166 }
Victor Stinner742da042016-09-07 17:40:12 -07001167
1168 assert(value_addr != NULL);
1169
1170 old_value = *value_addr;
1171 if (old_value != NULL) {
1172 *value_addr = value;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001173 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +02001174 assert(_PyDict_CheckConsistency(mp));
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001175
Victor Stinner742da042016-09-07 17:40:12 -07001176 Py_DECREF(old_value); /* which **CAN** re-enter (see issue #22653) */
1177 return 0;
1178 }
1179
1180 /* pending state */
1181 assert(_PyDict_HasSplitTable(mp));
1182 assert(ix == mp->ma_used);
1183 *value_addr = value;
1184 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001185 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +02001186 assert(_PyDict_CheckConsistency(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001187 return 0;
Antoine Pitroue965d972012-02-27 00:45:12 +01001188}
1189
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001190/*
1191Internal routine used by dictresize() to insert an item which is
1192known to be absent from the dict. This routine also assumes that
1193the dict contains no deleted entries. Besides the performance benefit,
1194using insertdict() in dictresize() is dangerous (SF bug #1456209).
1195Note that no refcounts are changed by this routine; if needed, the caller
1196is responsible for incref'ing `key` and `value`.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001197Neither mp->ma_used nor k->dk_usable are modified by this routine; the caller
1198must set them correctly
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001199*/
1200static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001201insertdict_clean(PyDictObject *mp, PyObject *key, Py_hash_t hash,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 PyObject *value)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001203{
Victor Stinner742da042016-09-07 17:40:12 -07001204 size_t i, perturb;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001205 PyDictKeysObject *k = mp->ma_keys;
1206 size_t mask = (size_t)DK_SIZE(k)-1;
Victor Stinner742da042016-09-07 17:40:12 -07001207 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001208 PyDictKeyEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001209
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001210 assert(k->dk_lookup != NULL);
1211 assert(value != NULL);
1212 assert(key != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001213 assert(PyUnicode_CheckExact(key) || k->dk_lookup == lookdict);
1214 i = hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -07001215 for (perturb = hash; dk_get_index(k, i) != DKIX_EMPTY;
1216 perturb >>= PERTURB_SHIFT) {
1217 i = mask & ((i << 2) + i + perturb + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 }
Victor Stinner742da042016-09-07 17:40:12 -07001219 ep = &ep0[k->dk_nentries];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 assert(ep->me_value == NULL);
Victor Stinner742da042016-09-07 17:40:12 -07001221 dk_set_index(k, i, k->dk_nentries);
1222 k->dk_nentries++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 ep->me_key = key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001224 ep->me_hash = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001226}
1227
1228/*
1229Restructure the table by allocating a new table and reinserting all
1230items again. When entries have been deleted, the new table may
1231actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001232If a table is split (its keys and hashes are shared, its values are not),
1233then the values are temporarily copied into the table, it is resized as
1234a combined table, then the me_value slots in the old table are NULLed out.
1235After resizing a table is always combined,
1236but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001237*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001238static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001239dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001240{
Victor Stinner742da042016-09-07 17:40:12 -07001241 Py_ssize_t i, newsize;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001242 PyDictKeysObject *oldkeys;
1243 PyObject **oldvalues;
Victor Stinner742da042016-09-07 17:40:12 -07001244 PyDictKeyEntry *ep0;
Tim Peters91a364d2001-05-19 07:04:38 +00001245
Victor Stinner742da042016-09-07 17:40:12 -07001246 /* Find the smallest table size > minused. */
1247 for (newsize = PyDict_MINSIZE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 newsize <= minused && newsize > 0;
1249 newsize <<= 1)
1250 ;
1251 if (newsize <= 0) {
1252 PyErr_NoMemory();
1253 return -1;
1254 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001255 oldkeys = mp->ma_keys;
1256 oldvalues = mp->ma_values;
1257 /* Allocate a new table. */
1258 mp->ma_keys = new_keys_object(newsize);
1259 if (mp->ma_keys == NULL) {
1260 mp->ma_keys = oldkeys;
1261 return -1;
1262 }
1263 if (oldkeys->dk_lookup == lookdict)
1264 mp->ma_keys->dk_lookup = lookdict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001265 mp->ma_values = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001266 ep0 = DK_ENTRIES(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001267 /* Main loop below assumes we can transfer refcount to new keys
1268 * and that value is stored in me_value.
1269 * Increment ref-counts and copy values here to compensate
1270 * This (resizing a split table) should be relatively rare */
1271 if (oldvalues != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001272 for (i = 0; i < oldkeys->dk_nentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001273 if (oldvalues[i] != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001274 Py_INCREF(ep0[i].me_key);
1275 ep0[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 }
1278 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001279 /* Main loop */
Victor Stinner742da042016-09-07 17:40:12 -07001280 for (i = 0; i < oldkeys->dk_nentries; i++) {
1281 PyDictKeyEntry *ep = &ep0[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001282 if (ep->me_value != NULL) {
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001283 insertdict_clean(mp, ep->me_key, ep->me_hash, ep->me_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001286 mp->ma_keys->dk_usable -= mp->ma_used;
1287 if (oldvalues != NULL) {
1288 /* NULL out me_value slot in oldkeys, in case it was shared */
Victor Stinner742da042016-09-07 17:40:12 -07001289 for (i = 0; i < oldkeys->dk_nentries; i++)
1290 ep0[i].me_value = NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001291 DK_DECREF(oldkeys);
Victor Stinner742da042016-09-07 17:40:12 -07001292 if (oldvalues != empty_values) {
1293 free_values(oldvalues);
1294 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001295 }
1296 else {
1297 assert(oldkeys->dk_lookup != lookdict_split);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001298 assert(oldkeys->dk_refcnt == 1);
Raymond Hettingerce5179f2016-01-31 08:56:21 -08001299 DK_DEBUG_DECREF PyObject_FREE(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001300 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001302}
1303
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001304/* Returns NULL if unable to split table.
1305 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001306static PyDictKeysObject *
1307make_keys_shared(PyObject *op)
1308{
1309 Py_ssize_t i;
1310 Py_ssize_t size;
1311 PyDictObject *mp = (PyDictObject *)op;
1312
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001313 if (!PyDict_CheckExact(op))
1314 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001315 if (!_PyDict_HasSplitTable(mp)) {
1316 PyDictKeyEntry *ep0;
1317 PyObject **values;
1318 assert(mp->ma_keys->dk_refcnt == 1);
1319 if (mp->ma_keys->dk_lookup == lookdict) {
1320 return NULL;
1321 }
1322 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
1323 /* Remove dummy keys */
1324 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
1325 return NULL;
1326 }
1327 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
1328 /* Copy values into a new array */
Victor Stinner742da042016-09-07 17:40:12 -07001329 ep0 = DK_ENTRIES(mp->ma_keys);
1330 size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001331 values = new_values(size);
1332 if (values == NULL) {
1333 PyErr_SetString(PyExc_MemoryError,
1334 "Not enough memory to allocate new values array");
1335 return NULL;
1336 }
1337 for (i = 0; i < size; i++) {
1338 values[i] = ep0[i].me_value;
1339 ep0[i].me_value = NULL;
1340 }
1341 mp->ma_keys->dk_lookup = lookdict_split;
1342 mp->ma_values = values;
1343 }
1344 DK_INCREF(mp->ma_keys);
1345 return mp->ma_keys;
1346}
Christian Heimes99170a52007-12-19 02:07:34 +00001347
1348PyObject *
1349_PyDict_NewPresized(Py_ssize_t minused)
1350{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001351 Py_ssize_t newsize;
1352 PyDictKeysObject *new_keys;
Victor Stinner742da042016-09-07 17:40:12 -07001353 for (newsize = PyDict_MINSIZE;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001354 newsize <= minused && newsize > 0;
1355 newsize <<= 1)
1356 ;
1357 new_keys = new_keys_object(newsize);
1358 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001359 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001360 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001361}
1362
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001363/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1364 * that may occur (originally dicts supported only string keys, and exceptions
1365 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001366 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001367 * (suppressed) occurred while computing the key's hash, or that some error
1368 * (suppressed) occurred when comparing keys in the dict's internal probe
1369 * sequence. A nasty example of the latter is when a Python-coded comparison
1370 * function hits a stack-depth error, which can cause this to return NULL
1371 * even if the key is present.
1372 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001373PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001374PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001375{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001376 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07001377 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 PyDictObject *mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 PyThreadState *tstate;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001380 PyObject **value_addr;
1381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 if (!PyDict_Check(op))
1383 return NULL;
1384 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001385 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 {
1387 hash = PyObject_Hash(key);
1388 if (hash == -1) {
1389 PyErr_Clear();
1390 return NULL;
1391 }
1392 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 /* We can arrive here with a NULL tstate during initialization: try
1395 running "python -Wi" for an example related to string interning.
1396 Let's just hope that no exception occurs then... This must be
1397 _PyThreadState_Current and not PyThreadState_GET() because in debug
1398 mode, the latter complains if tstate is NULL. */
Victor Stinnerbfd316e2016-01-20 11:12:38 +01001399 tstate = _PyThreadState_UncheckedGet();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 if (tstate != NULL && tstate->curexc_type != NULL) {
1401 /* preserve the existing exception */
1402 PyObject *err_type, *err_value, *err_tb;
1403 PyErr_Fetch(&err_type, &err_value, &err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001404 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 /* ignore errors */
1406 PyErr_Restore(err_type, err_value, err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001407 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 return NULL;
1409 }
1410 else {
Victor Stinner742da042016-09-07 17:40:12 -07001411 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
1412 if (ix < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 PyErr_Clear();
1414 return NULL;
1415 }
1416 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001417 return *value_addr;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001418}
1419
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001420PyObject *
1421_PyDict_GetItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1422{
Victor Stinner742da042016-09-07 17:40:12 -07001423 Py_ssize_t ix;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001424 PyDictObject *mp = (PyDictObject *)op;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001425 PyThreadState *tstate;
1426 PyObject **value_addr;
1427
1428 if (!PyDict_Check(op))
1429 return NULL;
1430
1431 /* We can arrive here with a NULL tstate during initialization: try
1432 running "python -Wi" for an example related to string interning.
1433 Let's just hope that no exception occurs then... This must be
1434 _PyThreadState_Current and not PyThreadState_GET() because in debug
1435 mode, the latter complains if tstate is NULL. */
Victor Stinnerbfd316e2016-01-20 11:12:38 +01001436 tstate = _PyThreadState_UncheckedGet();
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001437 if (tstate != NULL && tstate->curexc_type != NULL) {
1438 /* preserve the existing exception */
1439 PyObject *err_type, *err_value, *err_tb;
1440 PyErr_Fetch(&err_type, &err_value, &err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001441 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001442 /* ignore errors */
1443 PyErr_Restore(err_type, err_value, err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001444 if (ix == DKIX_EMPTY)
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001445 return NULL;
1446 }
1447 else {
Victor Stinner742da042016-09-07 17:40:12 -07001448 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
1449 if (ix == DKIX_EMPTY) {
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001450 PyErr_Clear();
1451 return NULL;
1452 }
1453 }
1454 return *value_addr;
1455}
1456
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001457/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1458 This returns NULL *with* an exception set if an exception occurred.
1459 It returns NULL *without* an exception set if the key wasn't present.
1460*/
1461PyObject *
1462PyDict_GetItemWithError(PyObject *op, PyObject *key)
1463{
Victor Stinner742da042016-09-07 17:40:12 -07001464 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001465 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001466 PyDictObject*mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001467 PyObject **value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 if (!PyDict_Check(op)) {
1470 PyErr_BadInternalCall();
1471 return NULL;
1472 }
1473 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001474 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475 {
1476 hash = PyObject_Hash(key);
1477 if (hash == -1) {
1478 return NULL;
1479 }
1480 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001481
Victor Stinner742da042016-09-07 17:40:12 -07001482 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
1483 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001485 return *value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001486}
1487
Brett Cannonfd074152012-04-14 14:10:13 -04001488PyObject *
1489_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1490{
1491 PyObject *kv;
1492 kv = _PyUnicode_FromId(key); /* borrowed */
1493 if (kv == NULL)
1494 return NULL;
1495 return PyDict_GetItemWithError(dp, kv);
1496}
1497
Victor Stinnerb4efc962015-11-20 09:24:02 +01001498/* Fast version of global value lookup (LOAD_GLOBAL).
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001499 * Lookup in globals, then builtins.
Victor Stinnerb4efc962015-11-20 09:24:02 +01001500 *
1501 * Raise an exception and return NULL if an error occurred (ex: computing the
1502 * key hash failed, key comparison failed, ...). Return NULL if the key doesn't
1503 * exist. Return the value if the key exists.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001504 */
1505PyObject *
1506_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001507{
Victor Stinner742da042016-09-07 17:40:12 -07001508 Py_ssize_t ix;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001509 Py_hash_t hash;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001510 PyObject **value_addr;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001511
1512 if (!PyUnicode_CheckExact(key) ||
1513 (hash = ((PyASCIIObject *) key)->hash) == -1)
1514 {
1515 hash = PyObject_Hash(key);
1516 if (hash == -1)
1517 return NULL;
Antoine Pitroue965d972012-02-27 00:45:12 +01001518 }
Victor Stinnerb4efc962015-11-20 09:24:02 +01001519
1520 /* namespace 1: globals */
Victor Stinner742da042016-09-07 17:40:12 -07001521 ix = globals->ma_keys->dk_lookup(globals, key, hash, &value_addr, NULL);
1522 if (ix == DKIX_ERROR)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001523 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001524 if (ix != DKIX_EMPTY && *value_addr != NULL)
1525 return *value_addr;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001526
1527 /* namespace 2: builtins */
Victor Stinner742da042016-09-07 17:40:12 -07001528 ix = builtins->ma_keys->dk_lookup(builtins, key, hash, &value_addr, NULL);
1529 if (ix < 0)
Victor Stinnerb4efc962015-11-20 09:24:02 +01001530 return NULL;
1531 return *value_addr;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001532}
1533
Antoine Pitroue965d972012-02-27 00:45:12 +01001534/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1535 * dictionary if it's merely replacing the value for an existing key.
1536 * This means that it's safe to loop over a dictionary with PyDict_Next()
1537 * and occasionally replace a value -- but you can't insert new keys or
1538 * remove them.
1539 */
1540int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001541PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001542{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001543 PyDictObject *mp;
1544 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001545 if (!PyDict_Check(op)) {
1546 PyErr_BadInternalCall();
1547 return -1;
1548 }
1549 assert(key);
1550 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001551 mp = (PyDictObject *)op;
1552 if (!PyUnicode_CheckExact(key) ||
1553 (hash = ((PyASCIIObject *) key)->hash) == -1)
1554 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001555 hash = PyObject_Hash(key);
1556 if (hash == -1)
1557 return -1;
1558 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001559
1560 /* insertdict() handles any resizing that might be necessary */
1561 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001562}
1563
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001564int
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001565_PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
1566 Py_hash_t hash)
1567{
1568 PyDictObject *mp;
1569
1570 if (!PyDict_Check(op)) {
1571 PyErr_BadInternalCall();
1572 return -1;
1573 }
1574 assert(key);
1575 assert(value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001576 assert(hash != -1);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001577 mp = (PyDictObject *)op;
1578
1579 /* insertdict() handles any resizing that might be necessary */
1580 return insertdict(mp, key, hash, value);
1581}
1582
1583int
Tim Peters1f5871e2000-07-04 17:44:48 +00001584PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001585{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001586 Py_hash_t hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +00001587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 assert(key);
1589 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001590 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 hash = PyObject_Hash(key);
1592 if (hash == -1)
1593 return -1;
1594 }
Victor Stinner742da042016-09-07 17:40:12 -07001595
1596 return _PyDict_DelItem_KnownHash(op, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001597}
1598
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001599int
1600_PyDict_DelItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1601{
Victor Stinner742da042016-09-07 17:40:12 -07001602 Py_ssize_t hashpos, ix;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001603 PyDictObject *mp;
1604 PyDictKeyEntry *ep;
1605 PyObject *old_key, *old_value;
1606 PyObject **value_addr;
1607
1608 if (!PyDict_Check(op)) {
1609 PyErr_BadInternalCall();
1610 return -1;
1611 }
1612 assert(key);
1613 assert(hash != -1);
1614 mp = (PyDictObject *)op;
Victor Stinner742da042016-09-07 17:40:12 -07001615 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, &hashpos);
1616 if (ix == DKIX_ERROR)
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001617 return -1;
Victor Stinner742da042016-09-07 17:40:12 -07001618 if (ix == DKIX_EMPTY || *value_addr == NULL) {
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001619 _PyErr_SetKeyError(key);
1620 return -1;
1621 }
Victor Stinner742da042016-09-07 17:40:12 -07001622 assert(dk_get_index(mp->ma_keys, hashpos) == ix);
Victor Stinner78601a32016-09-09 19:28:36 -07001623
1624 // Split table doesn't allow deletion. Combine it.
1625 if (_PyDict_HasSplitTable(mp)) {
1626 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1627 return -1;
1628 }
1629 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, &hashpos);
1630 assert(ix >= 0);
1631 }
1632
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001633 old_value = *value_addr;
Victor Stinner78601a32016-09-09 19:28:36 -07001634 assert(old_value != NULL);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001635 *value_addr = NULL;
1636 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001637 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner78601a32016-09-09 19:28:36 -07001638 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1639 dk_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
1640 ENSURE_ALLOWS_DELETIONS(mp);
1641 old_key = ep->me_key;
1642 ep->me_key = NULL;
1643 Py_DECREF(old_key);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001644 Py_DECREF(old_value);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001645
1646 assert(_PyDict_CheckConsistency(mp));
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001647 return 0;
1648}
1649
Guido van Rossum25831651993-05-19 14:50:45 +00001650void
Tim Peters1f5871e2000-07-04 17:44:48 +00001651PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001652{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001654 PyDictKeysObject *oldkeys;
1655 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 if (!PyDict_Check(op))
1659 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001660 mp = ((PyDictObject *)op);
1661 oldkeys = mp->ma_keys;
1662 oldvalues = mp->ma_values;
1663 if (oldvalues == empty_values)
1664 return;
1665 /* Empty the dict... */
1666 DK_INCREF(Py_EMPTY_KEYS);
1667 mp->ma_keys = Py_EMPTY_KEYS;
1668 mp->ma_values = empty_values;
1669 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001670 mp->ma_version_tag = DICT_NEXT_VERSION();
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001671 /* ...then clear the keys and values */
1672 if (oldvalues != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001673 n = oldkeys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001674 for (i = 0; i < n; i++)
1675 Py_CLEAR(oldvalues[i]);
1676 free_values(oldvalues);
1677 DK_DECREF(oldkeys);
1678 }
1679 else {
1680 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001681 DK_DECREF(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001682 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02001683 assert(_PyDict_CheckConsistency(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001684}
1685
1686/* Returns -1 if no more items (or op is not a dict),
1687 * index of item otherwise. Stores value in pvalue
1688 */
Benjamin Peterson73222252016-09-08 09:58:47 -07001689static inline Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001690dict_next(PyObject *op, Py_ssize_t i, PyObject **pvalue)
1691{
Victor Stinner742da042016-09-07 17:40:12 -07001692 Py_ssize_t n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001693 PyDictObject *mp;
Victor Stinner742da042016-09-07 17:40:12 -07001694 PyObject **value_ptr = NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001695
1696 if (!PyDict_Check(op))
1697 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001698 mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001699 if (i < 0)
1700 return -1;
Victor Stinner742da042016-09-07 17:40:12 -07001701
1702 n = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001703 if (mp->ma_values) {
Victor Stinner742da042016-09-07 17:40:12 -07001704 for (; i < n; i++) {
1705 value_ptr = &mp->ma_values[i];
1706 if (*value_ptr != NULL)
1707 break;
1708 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001710 else {
Victor Stinner742da042016-09-07 17:40:12 -07001711 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
1712 for (; i < n; i++) {
1713 value_ptr = &ep0[i].me_value;
1714 if (*value_ptr != NULL)
1715 break;
1716 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717 }
Victor Stinner742da042016-09-07 17:40:12 -07001718 if (i >= n)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001719 return -1;
1720 if (pvalue)
1721 *pvalue = *value_ptr;
1722 return i;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001723}
1724
Tim Peters080c88b2003-02-15 03:01:11 +00001725/*
1726 * Iterate over a dict. Use like so:
1727 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001728 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001729 * PyObject *key, *value;
1730 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001731 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +00001732 * Refer to borrowed references in key and value.
1733 * }
1734 *
1735 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001736 * mutates the dict. One exception: it is safe if the loop merely changes
1737 * the values associated with the keys (but doesn't insert new keys or
1738 * delete keys), via PyDict_SetItem().
1739 */
Guido van Rossum25831651993-05-19 14:50:45 +00001740int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001741PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001742{
Victor Stinner742da042016-09-07 17:40:12 -07001743 PyDictObject *mp = (PyDictObject*)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001744 Py_ssize_t i = dict_next(op, *ppos, pvalue);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001745 if (i < 0)
1746 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001747 mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001748 *ppos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 if (pkey)
Victor Stinner742da042016-09-07 17:40:12 -07001750 *pkey = DK_ENTRIES(mp->ma_keys)[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001751 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001752}
1753
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001754/* Internal version of PyDict_Next that returns a hash value in addition
1755 * to the key and value.
1756 */
Thomas Wouterscf297e42007-02-23 15:07:44 +00001757int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001758_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1759 PyObject **pvalue, Py_hash_t *phash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00001760{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001761 PyDictObject *mp;
Victor Stinner742da042016-09-07 17:40:12 -07001762 PyDictKeyEntry *ep0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001763 Py_ssize_t i = dict_next(op, *ppos, pvalue);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001764 if (i < 0)
1765 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001766 mp = (PyDictObject *)op;
Victor Stinner742da042016-09-07 17:40:12 -07001767 ep0 = DK_ENTRIES(mp->ma_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 *ppos = i+1;
Victor Stinner742da042016-09-07 17:40:12 -07001769 *phash = ep0[i].me_hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 if (pkey)
Victor Stinner742da042016-09-07 17:40:12 -07001771 *pkey = ep0[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 return 1;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001773}
1774
Eric Snow96c6af92015-05-29 22:21:39 -06001775/* Internal version of dict.pop(). */
1776PyObject *
1777_PyDict_Pop(PyDictObject *mp, PyObject *key, PyObject *deflt)
1778{
1779 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07001780 Py_ssize_t ix, hashpos;
Eric Snow96c6af92015-05-29 22:21:39 -06001781 PyObject *old_value, *old_key;
1782 PyDictKeyEntry *ep;
1783 PyObject **value_addr;
1784
1785 if (mp->ma_used == 0) {
1786 if (deflt) {
1787 Py_INCREF(deflt);
1788 return deflt;
1789 }
1790 _PyErr_SetKeyError(key);
1791 return NULL;
1792 }
1793 if (!PyUnicode_CheckExact(key) ||
1794 (hash = ((PyASCIIObject *) key)->hash) == -1) {
1795 hash = PyObject_Hash(key);
1796 if (hash == -1)
1797 return NULL;
1798 }
Victor Stinner742da042016-09-07 17:40:12 -07001799 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, &hashpos);
1800 if (ix == DKIX_ERROR)
Eric Snow96c6af92015-05-29 22:21:39 -06001801 return NULL;
Victor Stinnerd0ad11f2016-09-13 16:56:38 +02001802 if (ix == DKIX_EMPTY || *value_addr == NULL) {
Eric Snow96c6af92015-05-29 22:21:39 -06001803 if (deflt) {
1804 Py_INCREF(deflt);
1805 return deflt;
1806 }
1807 _PyErr_SetKeyError(key);
1808 return NULL;
1809 }
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001810
Victor Stinner78601a32016-09-09 19:28:36 -07001811 // Split table doesn't allow deletion. Combine it.
1812 if (_PyDict_HasSplitTable(mp)) {
1813 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1814 return NULL;
1815 }
1816 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, &hashpos);
1817 assert(ix >= 0);
1818 }
1819
Victor Stinner742da042016-09-07 17:40:12 -07001820 old_value = *value_addr;
Victor Stinner78601a32016-09-09 19:28:36 -07001821 assert(old_value != NULL);
Eric Snow96c6af92015-05-29 22:21:39 -06001822 *value_addr = NULL;
1823 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001824 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner78601a32016-09-09 19:28:36 -07001825 dk_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
1826 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1827 ENSURE_ALLOWS_DELETIONS(mp);
1828 old_key = ep->me_key;
1829 ep->me_key = NULL;
1830 Py_DECREF(old_key);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001831
1832 assert(_PyDict_CheckConsistency(mp));
Eric Snow96c6af92015-05-29 22:21:39 -06001833 return old_value;
1834}
1835
1836/* Internal version of dict.from_keys(). It is subclass-friendly. */
1837PyObject *
1838_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
1839{
1840 PyObject *it; /* iter(iterable) */
1841 PyObject *key;
1842 PyObject *d;
1843 int status;
1844
1845 d = PyObject_CallObject(cls, NULL);
1846 if (d == NULL)
1847 return NULL;
1848
1849 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
1850 if (PyDict_CheckExact(iterable)) {
1851 PyDictObject *mp = (PyDictObject *)d;
1852 PyObject *oldvalue;
1853 Py_ssize_t pos = 0;
1854 PyObject *key;
1855 Py_hash_t hash;
1856
Victor Stinner742da042016-09-07 17:40:12 -07001857 if (dictresize(mp, ESTIMATE_SIZE(Py_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001858 Py_DECREF(d);
1859 return NULL;
1860 }
1861
1862 while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
1863 if (insertdict(mp, key, hash, value)) {
1864 Py_DECREF(d);
1865 return NULL;
1866 }
1867 }
1868 return d;
1869 }
1870 if (PyAnySet_CheckExact(iterable)) {
1871 PyDictObject *mp = (PyDictObject *)d;
1872 Py_ssize_t pos = 0;
1873 PyObject *key;
1874 Py_hash_t hash;
1875
Victor Stinner742da042016-09-07 17:40:12 -07001876 if (dictresize(mp, ESTIMATE_SIZE(PySet_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001877 Py_DECREF(d);
1878 return NULL;
1879 }
1880
1881 while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
1882 if (insertdict(mp, key, hash, value)) {
1883 Py_DECREF(d);
1884 return NULL;
1885 }
1886 }
1887 return d;
1888 }
1889 }
1890
1891 it = PyObject_GetIter(iterable);
1892 if (it == NULL){
1893 Py_DECREF(d);
1894 return NULL;
1895 }
1896
1897 if (PyDict_CheckExact(d)) {
1898 while ((key = PyIter_Next(it)) != NULL) {
1899 status = PyDict_SetItem(d, key, value);
1900 Py_DECREF(key);
1901 if (status < 0)
1902 goto Fail;
1903 }
1904 } else {
1905 while ((key = PyIter_Next(it)) != NULL) {
1906 status = PyObject_SetItem(d, key, value);
1907 Py_DECREF(key);
1908 if (status < 0)
1909 goto Fail;
1910 }
1911 }
1912
1913 if (PyErr_Occurred())
1914 goto Fail;
1915 Py_DECREF(it);
1916 return d;
1917
1918Fail:
1919 Py_DECREF(it);
1920 Py_DECREF(d);
1921 return NULL;
1922}
1923
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001924/* Methods */
1925
1926static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001927dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001928{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001929 PyObject **values = mp->ma_values;
1930 PyDictKeysObject *keys = mp->ma_keys;
1931 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932 PyObject_GC_UnTrack(mp);
1933 Py_TRASHCAN_SAFE_BEGIN(mp)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001934 if (values != NULL) {
1935 if (values != empty_values) {
Victor Stinner742da042016-09-07 17:40:12 -07001936 for (i = 0, n = mp->ma_keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001937 Py_XDECREF(values[i]);
1938 }
1939 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001941 DK_DECREF(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001942 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02001943 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001944 assert(keys->dk_refcnt == 1);
1945 DK_DECREF(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001946 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001947 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1948 free_list[numfree++] = mp;
1949 else
1950 Py_TYPE(mp)->tp_free((PyObject *)mp);
1951 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001952}
1953
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001954
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001955static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001956dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001957{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001958 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001959 PyObject *key = NULL, *value = NULL;
1960 _PyUnicodeWriter writer;
1961 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00001962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001963 i = Py_ReprEnter((PyObject *)mp);
1964 if (i != 0) {
1965 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
1966 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001968 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001969 Py_ReprLeave((PyObject *)mp);
1970 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001971 }
Tim Petersa7259592001-06-16 05:11:17 +00001972
Victor Stinnerf91929b2013-11-19 13:07:38 +01001973 _PyUnicodeWriter_Init(&writer);
1974 writer.overallocate = 1;
1975 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
1976 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00001977
Victor Stinnerf91929b2013-11-19 13:07:38 +01001978 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
1979 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001981 /* Do repr() on each key+value pair, and insert ": " between them.
1982 Note that repr may mutate the dict. */
1983 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001984 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001985 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001986 PyObject *s;
1987 int res;
1988
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001989 /* Prevent repr from deleting key or value during key format. */
1990 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001991 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02001992
Victor Stinnerf91929b2013-11-19 13:07:38 +01001993 if (!first) {
1994 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
1995 goto error;
1996 }
1997 first = 0;
1998
1999 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01002001 goto error;
2002 res = _PyUnicodeWriter_WriteStr(&writer, s);
2003 Py_DECREF(s);
2004 if (res < 0)
2005 goto error;
2006
2007 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
2008 goto error;
2009
2010 s = PyObject_Repr(value);
2011 if (s == NULL)
2012 goto error;
2013 res = _PyUnicodeWriter_WriteStr(&writer, s);
2014 Py_DECREF(s);
2015 if (res < 0)
2016 goto error;
2017
2018 Py_CLEAR(key);
2019 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002020 }
Tim Petersa7259592001-06-16 05:11:17 +00002021
Victor Stinnerf91929b2013-11-19 13:07:38 +01002022 writer.overallocate = 0;
2023 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
2024 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00002025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002026 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01002027
2028 return _PyUnicodeWriter_Finish(&writer);
2029
2030error:
2031 Py_ReprLeave((PyObject *)mp);
2032 _PyUnicodeWriter_Dealloc(&writer);
2033 Py_XDECREF(key);
2034 Py_XDECREF(value);
2035 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002036}
2037
Martin v. Löwis18e16552006-02-15 17:27:45 +00002038static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002039dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002040{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002041 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002042}
2043
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002044static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002045dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002046{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002047 PyObject *v;
Victor Stinner742da042016-09-07 17:40:12 -07002048 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002049 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002050 PyObject **value_addr;
2051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002052 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002053 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002054 hash = PyObject_Hash(key);
2055 if (hash == -1)
2056 return NULL;
2057 }
Victor Stinner742da042016-09-07 17:40:12 -07002058 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
2059 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002060 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002061 if (ix == DKIX_EMPTY || *value_addr == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002062 if (!PyDict_CheckExact(mp)) {
2063 /* Look up __missing__ method if we're a subclass. */
2064 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05002065 _Py_IDENTIFIER(__missing__);
2066 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 if (missing != NULL) {
2068 res = PyObject_CallFunctionObjArgs(missing,
2069 key, NULL);
2070 Py_DECREF(missing);
2071 return res;
2072 }
2073 else if (PyErr_Occurred())
2074 return NULL;
2075 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07002076 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002077 return NULL;
2078 }
Victor Stinner742da042016-09-07 17:40:12 -07002079 v = *value_addr;
2080 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002081 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002082}
2083
2084static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002085dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002086{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002087 if (w == NULL)
2088 return PyDict_DelItem((PyObject *)mp, v);
2089 else
2090 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002091}
2092
Guido van Rossuma9e7a811997-05-13 21:02:11 +00002093static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002094 (lenfunc)dict_length, /*mp_length*/
2095 (binaryfunc)dict_subscript, /*mp_subscript*/
2096 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002097};
2098
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002099static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002100dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002101{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002102 PyObject *v;
2103 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002104 PyDictKeyEntry *ep;
2105 Py_ssize_t size, n, offset;
2106 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002107
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002108 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002109 n = mp->ma_used;
2110 v = PyList_New(n);
2111 if (v == NULL)
2112 return NULL;
2113 if (n != mp->ma_used) {
2114 /* Durnit. The allocations caused the dict to resize.
2115 * Just start over, this shouldn't normally happen.
2116 */
2117 Py_DECREF(v);
2118 goto again;
2119 }
Victor Stinner742da042016-09-07 17:40:12 -07002120 ep = DK_ENTRIES(mp->ma_keys);
2121 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002122 if (mp->ma_values) {
2123 value_ptr = mp->ma_values;
2124 offset = sizeof(PyObject *);
2125 }
2126 else {
2127 value_ptr = &ep[0].me_value;
2128 offset = sizeof(PyDictKeyEntry);
2129 }
2130 for (i = 0, j = 0; i < size; i++) {
2131 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002132 PyObject *key = ep[i].me_key;
2133 Py_INCREF(key);
2134 PyList_SET_ITEM(v, j, key);
2135 j++;
2136 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002137 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138 }
2139 assert(j == n);
2140 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002141}
2142
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002143static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002144dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002145{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002146 PyObject *v;
2147 Py_ssize_t i, j;
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002148 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002149 Py_ssize_t size, n, offset;
2150 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002151
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002152 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002153 n = mp->ma_used;
2154 v = PyList_New(n);
2155 if (v == NULL)
2156 return NULL;
2157 if (n != mp->ma_used) {
2158 /* Durnit. The allocations caused the dict to resize.
2159 * Just start over, this shouldn't normally happen.
2160 */
2161 Py_DECREF(v);
2162 goto again;
2163 }
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002164 ep = DK_ENTRIES(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002165 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002166 if (mp->ma_values) {
2167 value_ptr = mp->ma_values;
2168 offset = sizeof(PyObject *);
2169 }
2170 else {
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002171 value_ptr = &ep[0].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002172 offset = sizeof(PyDictKeyEntry);
2173 }
2174 for (i = 0, j = 0; i < size; i++) {
2175 PyObject *value = *value_ptr;
2176 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2177 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002178 Py_INCREF(value);
2179 PyList_SET_ITEM(v, j, value);
2180 j++;
2181 }
2182 }
2183 assert(j == n);
2184 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002185}
2186
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002187static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002188dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002189{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002190 PyObject *v;
2191 Py_ssize_t i, j, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002192 Py_ssize_t size, offset;
2193 PyObject *item, *key;
2194 PyDictKeyEntry *ep;
2195 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 /* Preallocate the list of tuples, to avoid allocations during
2198 * the loop over the items, which could trigger GC, which
2199 * could resize the dict. :-(
2200 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002201 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002202 n = mp->ma_used;
2203 v = PyList_New(n);
2204 if (v == NULL)
2205 return NULL;
2206 for (i = 0; i < n; i++) {
2207 item = PyTuple_New(2);
2208 if (item == NULL) {
2209 Py_DECREF(v);
2210 return NULL;
2211 }
2212 PyList_SET_ITEM(v, i, item);
2213 }
2214 if (n != mp->ma_used) {
2215 /* Durnit. The allocations caused the dict to resize.
2216 * Just start over, this shouldn't normally happen.
2217 */
2218 Py_DECREF(v);
2219 goto again;
2220 }
2221 /* Nothing we do below makes any function calls. */
Victor Stinner742da042016-09-07 17:40:12 -07002222 ep = DK_ENTRIES(mp->ma_keys);
2223 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002224 if (mp->ma_values) {
2225 value_ptr = mp->ma_values;
2226 offset = sizeof(PyObject *);
2227 }
2228 else {
2229 value_ptr = &ep[0].me_value;
2230 offset = sizeof(PyDictKeyEntry);
2231 }
2232 for (i = 0, j = 0; i < size; i++) {
2233 PyObject *value = *value_ptr;
2234 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2235 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002236 key = ep[i].me_key;
2237 item = PyList_GET_ITEM(v, j);
2238 Py_INCREF(key);
2239 PyTuple_SET_ITEM(item, 0, key);
2240 Py_INCREF(value);
2241 PyTuple_SET_ITEM(item, 1, value);
2242 j++;
2243 }
2244 }
2245 assert(j == n);
2246 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002247}
2248
Larry Hastings5c661892014-01-24 06:17:25 -08002249/*[clinic input]
2250@classmethod
2251dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08002252 iterable: object
2253 value: object=None
2254 /
2255
2256Returns a new dict with keys from iterable and values equal to value.
2257[clinic start generated code]*/
2258
Larry Hastings5c661892014-01-24 06:17:25 -08002259static PyObject *
2260dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002261/*[clinic end generated code: output=8fb98e4b10384999 input=b85a667f9bf4669d]*/
Larry Hastings5c661892014-01-24 06:17:25 -08002262{
Eric Snow96c6af92015-05-29 22:21:39 -06002263 return _PyDict_FromKeys((PyObject *)type, iterable, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002264}
2265
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002266static int
Victor Stinner742da042016-09-07 17:40:12 -07002267dict_update_common(PyObject *self, PyObject *args, PyObject *kwds,
2268 const char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002270 PyObject *arg = NULL;
2271 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002273 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
2274 result = -1;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002276 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002277 _Py_IDENTIFIER(keys);
2278 if (_PyObject_HasAttrId(arg, &PyId_keys))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002279 result = PyDict_Merge(self, arg, 1);
2280 else
2281 result = PyDict_MergeFromSeq2(self, arg, 1);
2282 }
2283 if (result == 0 && kwds != NULL) {
2284 if (PyArg_ValidateKeywordArguments(kwds))
2285 result = PyDict_Merge(self, kwds, 1);
2286 else
2287 result = -1;
2288 }
2289 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002290}
2291
2292static PyObject *
2293dict_update(PyObject *self, PyObject *args, PyObject *kwds)
2294{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002295 if (dict_update_common(self, args, kwds, "update") != -1)
2296 Py_RETURN_NONE;
2297 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002298}
2299
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002300/* Update unconditionally replaces existing items.
2301 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00002302 otherwise it leaves existing items unchanged.
2303
2304 PyDict_{Update,Merge} update/merge from a mapping object.
2305
Tim Petersf582b822001-12-11 18:51:08 +00002306 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00002307 producing iterable objects of length 2.
2308*/
2309
Tim Petersf582b822001-12-11 18:51:08 +00002310int
Tim Peters1fc240e2001-10-26 05:06:50 +00002311PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
2312{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002313 PyObject *it; /* iter(seq2) */
2314 Py_ssize_t i; /* index into seq2 of current element */
2315 PyObject *item; /* seq2[i] */
2316 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00002317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002318 assert(d != NULL);
2319 assert(PyDict_Check(d));
2320 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00002321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002322 it = PyObject_GetIter(seq2);
2323 if (it == NULL)
2324 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002326 for (i = 0; ; ++i) {
2327 PyObject *key, *value;
2328 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00002329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002330 fast = NULL;
2331 item = PyIter_Next(it);
2332 if (item == NULL) {
2333 if (PyErr_Occurred())
2334 goto Fail;
2335 break;
2336 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002338 /* Convert item to sequence, and verify length 2. */
2339 fast = PySequence_Fast(item, "");
2340 if (fast == NULL) {
2341 if (PyErr_ExceptionMatches(PyExc_TypeError))
2342 PyErr_Format(PyExc_TypeError,
2343 "cannot convert dictionary update "
2344 "sequence element #%zd to a sequence",
2345 i);
2346 goto Fail;
2347 }
2348 n = PySequence_Fast_GET_SIZE(fast);
2349 if (n != 2) {
2350 PyErr_Format(PyExc_ValueError,
2351 "dictionary update sequence element #%zd "
2352 "has length %zd; 2 is required",
2353 i, n);
2354 goto Fail;
2355 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 /* Update/merge with this (key, value) pair. */
2358 key = PySequence_Fast_GET_ITEM(fast, 0);
2359 value = PySequence_Fast_GET_ITEM(fast, 1);
2360 if (override || PyDict_GetItem(d, key) == NULL) {
2361 int status = PyDict_SetItem(d, key, value);
2362 if (status < 0)
2363 goto Fail;
2364 }
2365 Py_DECREF(fast);
2366 Py_DECREF(item);
2367 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 i = 0;
Victor Stinner611b0fa2016-09-14 15:02:01 +02002370 assert(_PyDict_CheckConsistency((PyDictObject *)d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002371 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00002372Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002373 Py_XDECREF(item);
2374 Py_XDECREF(fast);
2375 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002376Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002377 Py_DECREF(it);
2378 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00002379}
2380
Tim Peters6d6c1a32001-08-02 04:15:00 +00002381int
2382PyDict_Update(PyObject *a, PyObject *b)
2383{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002384 return PyDict_Merge(a, b, 1);
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002385}
2386
2387int
2388PyDict_Merge(PyObject *a, PyObject *b, int override)
2389{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002390 PyDictObject *mp, *other;
2391 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -07002392 PyDictKeyEntry *entry, *ep0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 /* We accept for the argument either a concrete dictionary object,
2395 * or an abstract "mapping" object. For the former, we can do
2396 * things quite efficiently. For the latter, we only require that
2397 * PyMapping_Keys() and PyObject_GetItem() be supported.
2398 */
2399 if (a == NULL || !PyDict_Check(a) || b == NULL) {
2400 PyErr_BadInternalCall();
2401 return -1;
2402 }
2403 mp = (PyDictObject*)a;
2404 if (PyDict_Check(b)) {
2405 other = (PyDictObject*)b;
2406 if (other == mp || other->ma_used == 0)
2407 /* a.update(a) or a.update({}); nothing to do */
2408 return 0;
2409 if (mp->ma_used == 0)
2410 /* Since the target dict is empty, PyDict_GetItem()
2411 * always returns NULL. Setting override to 1
2412 * skips the unnecessary test.
2413 */
2414 override = 1;
2415 /* Do one big resize at the start, rather than
2416 * incrementally resizing as we insert new items. Expect
2417 * that there will be no (or few) overlapping keys.
2418 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002419 if (mp->ma_keys->dk_usable * 3 < other->ma_used * 2)
2420 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002421 return -1;
Victor Stinner742da042016-09-07 17:40:12 -07002422 ep0 = DK_ENTRIES(other->ma_keys);
2423 for (i = 0, n = other->ma_keys->dk_nentries; i < n; i++) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002424 PyObject *key, *value;
2425 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002426 entry = &ep0[i];
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002427 key = entry->me_key;
2428 hash = entry->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002429 if (other->ma_values)
2430 value = other->ma_values[i];
2431 else
2432 value = entry->me_value;
2433
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002434 if (value != NULL) {
2435 int err = 0;
2436 Py_INCREF(key);
2437 Py_INCREF(value);
2438 if (override || PyDict_GetItem(a, key) == NULL)
2439 err = insertdict(mp, key, hash, value);
2440 Py_DECREF(value);
2441 Py_DECREF(key);
2442 if (err != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002443 return -1;
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002444
Victor Stinner742da042016-09-07 17:40:12 -07002445 if (n != other->ma_keys->dk_nentries) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002446 PyErr_SetString(PyExc_RuntimeError,
2447 "dict mutated during update");
2448 return -1;
2449 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002450 }
2451 }
2452 }
2453 else {
2454 /* Do it the generic, slower way */
2455 PyObject *keys = PyMapping_Keys(b);
2456 PyObject *iter;
2457 PyObject *key, *value;
2458 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002460 if (keys == NULL)
2461 /* Docstring says this is equivalent to E.keys() so
2462 * if E doesn't have a .keys() method we want
2463 * AttributeError to percolate up. Might as well
2464 * do the same for any other error.
2465 */
2466 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002467
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002468 iter = PyObject_GetIter(keys);
2469 Py_DECREF(keys);
2470 if (iter == NULL)
2471 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002473 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
2474 if (!override && PyDict_GetItem(a, key) != NULL) {
2475 Py_DECREF(key);
2476 continue;
2477 }
2478 value = PyObject_GetItem(b, key);
2479 if (value == NULL) {
2480 Py_DECREF(iter);
2481 Py_DECREF(key);
2482 return -1;
2483 }
2484 status = PyDict_SetItem(a, key, value);
2485 Py_DECREF(key);
2486 Py_DECREF(value);
2487 if (status < 0) {
2488 Py_DECREF(iter);
2489 return -1;
2490 }
2491 }
2492 Py_DECREF(iter);
2493 if (PyErr_Occurred())
2494 /* Iterator completed, via error */
2495 return -1;
2496 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02002497 assert(_PyDict_CheckConsistency((PyDictObject *)a));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002498 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002499}
2500
2501static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002502dict_copy(PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002503{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002504 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002505}
2506
2507PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002508PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002509{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002510 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002511 PyDictObject *mp;
2512 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002514 if (o == NULL || !PyDict_Check(o)) {
2515 PyErr_BadInternalCall();
2516 return NULL;
2517 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002518 mp = (PyDictObject *)o;
2519 if (_PyDict_HasSplitTable(mp)) {
2520 PyDictObject *split_copy;
Victor Stinner742da042016-09-07 17:40:12 -07002521 Py_ssize_t size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
2522 PyObject **newvalues;
2523 newvalues = new_values(size);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002524 if (newvalues == NULL)
2525 return PyErr_NoMemory();
2526 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2527 if (split_copy == NULL) {
2528 free_values(newvalues);
2529 return NULL;
2530 }
2531 split_copy->ma_values = newvalues;
2532 split_copy->ma_keys = mp->ma_keys;
2533 split_copy->ma_used = mp->ma_used;
2534 DK_INCREF(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002535 for (i = 0, n = size; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002536 PyObject *value = mp->ma_values[i];
2537 Py_XINCREF(value);
2538 split_copy->ma_values[i] = value;
2539 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002540 if (_PyObject_GC_IS_TRACKED(mp))
2541 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002542 return (PyObject *)split_copy;
2543 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002544 copy = PyDict_New();
2545 if (copy == NULL)
2546 return NULL;
2547 if (PyDict_Merge(copy, o, 1) == 0)
2548 return copy;
2549 Py_DECREF(copy);
2550 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002551}
2552
Martin v. Löwis18e16552006-02-15 17:27:45 +00002553Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002554PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002555{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002556 if (mp == NULL || !PyDict_Check(mp)) {
2557 PyErr_BadInternalCall();
2558 return -1;
2559 }
2560 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002561}
2562
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002563PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002564PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002565{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002566 if (mp == NULL || !PyDict_Check(mp)) {
2567 PyErr_BadInternalCall();
2568 return NULL;
2569 }
2570 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002571}
2572
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002573PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002574PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002575{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002576 if (mp == NULL || !PyDict_Check(mp)) {
2577 PyErr_BadInternalCall();
2578 return NULL;
2579 }
2580 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002581}
2582
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002583PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002584PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002585{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002586 if (mp == NULL || !PyDict_Check(mp)) {
2587 PyErr_BadInternalCall();
2588 return NULL;
2589 }
2590 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002591}
2592
Tim Peterse63415e2001-05-08 04:38:29 +00002593/* Return 1 if dicts equal, 0 if not, -1 if error.
2594 * Gets out as soon as any difference is detected.
2595 * Uses only Py_EQ comparison.
2596 */
2597static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002598dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002599{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002600 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002601
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002602 if (a->ma_used != b->ma_used)
2603 /* can't be equal if # of entries differ */
2604 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002605 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Victor Stinner742da042016-09-07 17:40:12 -07002606 for (i = 0; i < a->ma_keys->dk_nentries; i++) {
2607 PyDictKeyEntry *ep = &DK_ENTRIES(a->ma_keys)[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002608 PyObject *aval;
2609 if (a->ma_values)
2610 aval = a->ma_values[i];
2611 else
2612 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002613 if (aval != NULL) {
2614 int cmp;
2615 PyObject *bval;
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002616 PyObject **vaddr;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002617 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002618 /* temporarily bump aval's refcount to ensure it stays
2619 alive until we're done with it */
2620 Py_INCREF(aval);
2621 /* ditto for key */
2622 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002623 /* reuse the known hash value */
Victor Stinner742da042016-09-07 17:40:12 -07002624 if ((b->ma_keys->dk_lookup)(b, key, ep->me_hash, &vaddr, NULL) < 0)
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002625 bval = NULL;
2626 else
2627 bval = *vaddr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002628 Py_DECREF(key);
2629 if (bval == NULL) {
2630 Py_DECREF(aval);
2631 if (PyErr_Occurred())
2632 return -1;
2633 return 0;
2634 }
2635 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
2636 Py_DECREF(aval);
2637 if (cmp <= 0) /* error or not equal */
2638 return cmp;
2639 }
2640 }
2641 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002642}
Tim Peterse63415e2001-05-08 04:38:29 +00002643
2644static PyObject *
2645dict_richcompare(PyObject *v, PyObject *w, int op)
2646{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002647 int cmp;
2648 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002649
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002650 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2651 res = Py_NotImplemented;
2652 }
2653 else if (op == Py_EQ || op == Py_NE) {
2654 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2655 if (cmp < 0)
2656 return NULL;
2657 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2658 }
2659 else
2660 res = Py_NotImplemented;
2661 Py_INCREF(res);
2662 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002663}
Tim Peterse63415e2001-05-08 04:38:29 +00002664
Larry Hastings61272b72014-01-07 12:41:53 -08002665/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002666
2667@coexist
2668dict.__contains__
2669
2670 key: object
2671 /
2672
Meador Ingee02de8c2014-01-14 16:48:31 -06002673True if D has a key k, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002674[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002675
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002676static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002677dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002678/*[clinic end generated code: output=a3d03db709ed6e6b input=b852b2a19b51ab24]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002679{
Larry Hastingsc2047262014-01-25 20:43:29 -08002680 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002681 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002682 Py_ssize_t ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002683 PyObject **value_addr;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002685 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002686 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002687 hash = PyObject_Hash(key);
2688 if (hash == -1)
2689 return NULL;
2690 }
Victor Stinner742da042016-09-07 17:40:12 -07002691 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
2692 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002693 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002694 if (ix == DKIX_EMPTY || *value_addr == NULL)
2695 Py_RETURN_FALSE;
2696 Py_RETURN_TRUE;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002697}
2698
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002699static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002700dict_get(PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00002701{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002702 PyObject *key;
2703 PyObject *failobj = Py_None;
2704 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002705 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002706 Py_ssize_t ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002707 PyObject **value_addr;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002709 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
2710 return NULL;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002712 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002713 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002714 hash = PyObject_Hash(key);
2715 if (hash == -1)
2716 return NULL;
2717 }
Victor Stinner742da042016-09-07 17:40:12 -07002718 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
2719 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002720 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002721 if (ix == DKIX_EMPTY || *value_addr == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002722 val = failobj;
Victor Stinner742da042016-09-07 17:40:12 -07002723 else
2724 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002725 Py_INCREF(val);
2726 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002727}
2728
Benjamin Peterson00e98862013-03-07 22:16:29 -05002729PyObject *
2730PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002731{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002732 PyDictObject *mp = (PyDictObject *)d;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002733 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002734 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002735 Py_ssize_t hashpos, ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002736 PyDictKeyEntry *ep;
2737 PyObject **value_addr;
Guido van Rossum164452c2000-08-08 16:12:54 +00002738
Benjamin Peterson00e98862013-03-07 22:16:29 -05002739 if (!PyDict_Check(d)) {
2740 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002741 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002742 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002743 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002744 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002745 hash = PyObject_Hash(key);
2746 if (hash == -1)
2747 return NULL;
2748 }
Victor Stinner742da042016-09-07 17:40:12 -07002749 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, &hashpos);
2750 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002751 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002752 if (ix == DKIX_EMPTY || *value_addr == NULL) {
2753 val = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002754 if (mp->ma_keys->dk_usable <= 0) {
2755 /* Need to resize. */
Victor Stinner3c336c52016-09-12 14:17:40 +02002756 if (insertion_resize(mp) < 0) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002757 return NULL;
Victor Stinner3c336c52016-09-12 14:17:40 +02002758 }
Victor Stinner742da042016-09-07 17:40:12 -07002759 find_empty_slot(mp, key, hash, &value_addr, &hashpos);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002760 }
Victor Stinner742da042016-09-07 17:40:12 -07002761 ix = mp->ma_keys->dk_nentries;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002762 Py_INCREF(defaultobj);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002763 Py_INCREF(key);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002764 MAINTAIN_TRACKING(mp, key, defaultobj);
Victor Stinner742da042016-09-07 17:40:12 -07002765 dk_set_index(mp->ma_keys, hashpos, ix);
2766 ep = &DK_ENTRIES(mp->ma_keys)[ix];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002767 ep->me_key = key;
2768 ep->me_hash = hash;
Victor Stinner742da042016-09-07 17:40:12 -07002769 if (mp->ma_values) {
2770 mp->ma_values[ix] = val;
2771 }
2772 else {
2773 ep->me_value = val;
2774 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002775 mp->ma_keys->dk_usable--;
Victor Stinner742da042016-09-07 17:40:12 -07002776 mp->ma_keys->dk_nentries++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002777 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002778 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +02002779 assert(_PyDict_CheckConsistency(mp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002780 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02002781 else {
Victor Stinner742da042016-09-07 17:40:12 -07002782 val = *value_addr;
Victor Stinner611b0fa2016-09-14 15:02:01 +02002783 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002784 return val;
Guido van Rossum164452c2000-08-08 16:12:54 +00002785}
2786
Benjamin Peterson00e98862013-03-07 22:16:29 -05002787static PyObject *
2788dict_setdefault(PyDictObject *mp, PyObject *args)
2789{
2790 PyObject *key, *val;
2791 PyObject *defaultobj = Py_None;
2792
2793 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &defaultobj))
2794 return NULL;
2795
Benjamin Peterson55898502013-03-08 08:36:49 -05002796 val = PyDict_SetDefault((PyObject *)mp, key, defaultobj);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002797 Py_XINCREF(val);
2798 return val;
2799}
Guido van Rossum164452c2000-08-08 16:12:54 +00002800
2801static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002802dict_clear(PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002803{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002804 PyDict_Clear((PyObject *)mp);
2805 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002806}
2807
Guido van Rossumba6ab842000-12-12 22:02:18 +00002808static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002809dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002810{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002811 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00002812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002813 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2814 return NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06002815
2816 return _PyDict_Pop(mp, key, deflt);
Guido van Rossume027d982002-04-12 15:11:59 +00002817}
2818
2819static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002820dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00002821{
Victor Stinner742da042016-09-07 17:40:12 -07002822 Py_ssize_t i, j;
2823 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002824 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002826 /* Allocate the result tuple before checking the size. Believe it
2827 * or not, this allocation could trigger a garbage collection which
2828 * could empty the dict, so if we checked the size first and that
2829 * happened, the result would be an infinite loop (searching for an
2830 * entry that no longer exists). Note that the usual popitem()
2831 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2832 * tuple away if the dict *is* empty isn't a significant
2833 * inefficiency -- possible, but unlikely in practice.
2834 */
2835 res = PyTuple_New(2);
2836 if (res == NULL)
2837 return NULL;
2838 if (mp->ma_used == 0) {
2839 Py_DECREF(res);
2840 PyErr_SetString(PyExc_KeyError,
2841 "popitem(): dictionary is empty");
2842 return NULL;
2843 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002844 /* Convert split table to combined table */
2845 if (mp->ma_keys->dk_lookup == lookdict_split) {
2846 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
2847 Py_DECREF(res);
2848 return NULL;
2849 }
2850 }
2851 ENSURE_ALLOWS_DELETIONS(mp);
Victor Stinner742da042016-09-07 17:40:12 -07002852
2853 /* Pop last item */
2854 ep0 = DK_ENTRIES(mp->ma_keys);
2855 i = mp->ma_keys->dk_nentries - 1;
2856 while (i >= 0 && ep0[i].me_value == NULL) {
2857 i--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002858 }
Victor Stinner742da042016-09-07 17:40:12 -07002859 assert(i >= 0);
2860
2861 ep = &ep0[i];
2862 j = lookdict_index(mp->ma_keys, ep->me_hash, i);
2863 assert(j >= 0);
2864 assert(dk_get_index(mp->ma_keys, j) == i);
2865 dk_set_index(mp->ma_keys, j, DKIX_DUMMY);
2866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002867 PyTuple_SET_ITEM(res, 0, ep->me_key);
2868 PyTuple_SET_ITEM(res, 1, ep->me_value);
Victor Stinner742da042016-09-07 17:40:12 -07002869 ep->me_key = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002870 ep->me_value = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002871 /* We can't dk_usable++ since there is DKIX_DUMMY in indices */
2872 mp->ma_keys->dk_nentries = i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002873 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002874 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +02002875 assert(_PyDict_CheckConsistency(mp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002877}
2878
Jeremy Hylton8caad492000-06-23 14:18:11 +00002879static int
2880dict_traverse(PyObject *op, visitproc visit, void *arg)
2881{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002882 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson55f44522016-09-05 12:12:59 -07002883 PyDictKeysObject *keys = mp->ma_keys;
Victor Stinner742da042016-09-07 17:40:12 -07002884 PyDictKeyEntry *entries = DK_ENTRIES(mp->ma_keys);
2885 Py_ssize_t i, n = keys->dk_nentries;
2886
Benjamin Peterson55f44522016-09-05 12:12:59 -07002887 if (keys->dk_lookup == lookdict) {
2888 for (i = 0; i < n; i++) {
2889 if (entries[i].me_value != NULL) {
2890 Py_VISIT(entries[i].me_value);
2891 Py_VISIT(entries[i].me_key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002892 }
2893 }
Victor Stinner742da042016-09-07 17:40:12 -07002894 }
2895 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002896 if (mp->ma_values != NULL) {
Benjamin Peterson55f44522016-09-05 12:12:59 -07002897 for (i = 0; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002898 Py_VISIT(mp->ma_values[i]);
2899 }
2900 }
2901 else {
Benjamin Peterson55f44522016-09-05 12:12:59 -07002902 for (i = 0; i < n; i++) {
2903 Py_VISIT(entries[i].me_value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002904 }
2905 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002906 }
2907 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002908}
2909
2910static int
2911dict_tp_clear(PyObject *op)
2912{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 PyDict_Clear(op);
2914 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002915}
2916
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002917static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002918
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002919Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06002920_PyDict_SizeOf(PyDictObject *mp)
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002921{
Victor Stinner742da042016-09-07 17:40:12 -07002922 Py_ssize_t size, usable, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002923
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002924 size = DK_SIZE(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002925 usable = USABLE_FRACTION(size);
2926
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002927 res = _PyObject_SIZE(Py_TYPE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002928 if (mp->ma_values)
Victor Stinner742da042016-09-07 17:40:12 -07002929 res += usable * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002930 /* If the dictionary is split, the keys portion is accounted-for
2931 in the type object. */
2932 if (mp->ma_keys->dk_refcnt == 1)
Victor Stinner98ee9d52016-09-08 09:33:56 -07002933 res += (sizeof(PyDictKeysObject)
2934 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
2935 + DK_IXSIZE(mp->ma_keys) * size
2936 + sizeof(PyDictKeyEntry) * usable);
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002937 return res;
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002938}
2939
2940Py_ssize_t
2941_PyDict_KeysSize(PyDictKeysObject *keys)
2942{
Victor Stinner98ee9d52016-09-08 09:33:56 -07002943 return (sizeof(PyDictKeysObject)
2944 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
2945 + DK_IXSIZE(keys) * DK_SIZE(keys)
2946 + USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry));
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002947}
2948
doko@ubuntu.com17210f52016-01-14 14:04:59 +01002949static PyObject *
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002950dict_sizeof(PyDictObject *mp)
2951{
2952 return PyLong_FromSsize_t(_PyDict_SizeOf(mp));
2953}
2954
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002955PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
2956
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002957PyDoc_STRVAR(sizeof__doc__,
2958"D.__sizeof__() -> size of D in memory, in bytes");
2959
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002960PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002961"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002962
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002963PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002964"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 +00002965
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002966PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002967"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002968If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00002969
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002970PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00002971"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000029722-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002973
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002974PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04002975"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
2976If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
2977If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
2978In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00002979
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002980PyDoc_STRVAR(clear__doc__,
2981"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002982
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002983PyDoc_STRVAR(copy__doc__,
2984"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00002985
Guido van Rossumb90c8482007-02-10 01:11:45 +00002986/* Forward */
2987static PyObject *dictkeys_new(PyObject *);
2988static PyObject *dictitems_new(PyObject *);
2989static PyObject *dictvalues_new(PyObject *);
2990
Guido van Rossum45c85d12007-07-27 16:31:40 +00002991PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002992 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002993PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002994 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002995PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002996 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00002997
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002998static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07002999 DICT___CONTAINS___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003000 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
3001 getitem__doc__},
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003002 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003003 sizeof__doc__},
3004 {"get", (PyCFunction)dict_get, METH_VARARGS,
3005 get__doc__},
3006 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
3007 setdefault_doc__},
3008 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
3009 pop__doc__},
3010 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
3011 popitem__doc__},
3012 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
3013 keys__doc__},
3014 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
3015 items__doc__},
3016 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
3017 values__doc__},
3018 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
3019 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08003020 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003021 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
3022 clear__doc__},
3023 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
3024 copy__doc__},
3025 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003026};
3027
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00003028/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00003029int
3030PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003031{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003032 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07003033 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003034 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003035 PyObject **value_addr;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003036
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003037 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003038 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003039 hash = PyObject_Hash(key);
3040 if (hash == -1)
3041 return -1;
3042 }
Victor Stinner742da042016-09-07 17:40:12 -07003043 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
3044 if (ix == DKIX_ERROR)
3045 return -1;
3046 return (ix != DKIX_EMPTY && *value_addr != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003047}
3048
Thomas Wouterscf297e42007-02-23 15:07:44 +00003049/* Internal version of PyDict_Contains used when the hash value is already known */
3050int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003051_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00003052{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003053 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003054 PyObject **value_addr;
Victor Stinner742da042016-09-07 17:40:12 -07003055 Py_ssize_t ix;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003056
Victor Stinner742da042016-09-07 17:40:12 -07003057 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
3058 if (ix == DKIX_ERROR)
3059 return -1;
3060 return (ix != DKIX_EMPTY && *value_addr != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00003061}
3062
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003063/* Hack to implement "key in dict" */
3064static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003065 0, /* sq_length */
3066 0, /* sq_concat */
3067 0, /* sq_repeat */
3068 0, /* sq_item */
3069 0, /* sq_slice */
3070 0, /* sq_ass_item */
3071 0, /* sq_ass_slice */
3072 PyDict_Contains, /* sq_contains */
3073 0, /* sq_inplace_concat */
3074 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003075};
3076
Guido van Rossum09e563a2001-05-01 12:10:21 +00003077static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003078dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3079{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003080 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003081 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003083 assert(type != NULL && type->tp_alloc != NULL);
3084 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02003085 if (self == NULL)
3086 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003087 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003088
Victor Stinnera9f61a52013-07-16 22:17:26 +02003089 /* The object has been implicitly tracked by tp_alloc */
3090 if (type == &PyDict_Type)
3091 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003092
3093 d->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003094 d->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07003095 d->ma_keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003096 if (d->ma_keys == NULL) {
3097 Py_DECREF(self);
3098 return NULL;
3099 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02003100 assert(_PyDict_CheckConsistency(d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003101 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003102}
3103
Tim Peters25786c02001-09-02 08:22:48 +00003104static int
3105dict_init(PyObject *self, PyObject *args, PyObject *kwds)
3106{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003107 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00003108}
3109
Tim Peters6d6c1a32001-08-02 04:15:00 +00003110static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003111dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00003112{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003113 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003114}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003115
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003116PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00003117"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00003118"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003119" (key, value) pairs\n"
3120"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00003121" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003122" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00003123" d[k] = v\n"
3124"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
3125" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00003126
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003127PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003128 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3129 "dict",
3130 sizeof(PyDictObject),
3131 0,
3132 (destructor)dict_dealloc, /* tp_dealloc */
3133 0, /* tp_print */
3134 0, /* tp_getattr */
3135 0, /* tp_setattr */
3136 0, /* tp_reserved */
3137 (reprfunc)dict_repr, /* tp_repr */
3138 0, /* tp_as_number */
3139 &dict_as_sequence, /* tp_as_sequence */
3140 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003141 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003142 0, /* tp_call */
3143 0, /* tp_str */
3144 PyObject_GenericGetAttr, /* tp_getattro */
3145 0, /* tp_setattro */
3146 0, /* tp_as_buffer */
3147 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3148 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
3149 dictionary_doc, /* tp_doc */
3150 dict_traverse, /* tp_traverse */
3151 dict_tp_clear, /* tp_clear */
3152 dict_richcompare, /* tp_richcompare */
3153 0, /* tp_weaklistoffset */
3154 (getiterfunc)dict_iter, /* tp_iter */
3155 0, /* tp_iternext */
3156 mapp_methods, /* tp_methods */
3157 0, /* tp_members */
3158 0, /* tp_getset */
3159 0, /* tp_base */
3160 0, /* tp_dict */
3161 0, /* tp_descr_get */
3162 0, /* tp_descr_set */
3163 0, /* tp_dictoffset */
3164 dict_init, /* tp_init */
3165 PyType_GenericAlloc, /* tp_alloc */
3166 dict_new, /* tp_new */
3167 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003168};
3169
Victor Stinner3c1e4812012-03-26 22:10:51 +02003170PyObject *
3171_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
3172{
3173 PyObject *kv;
3174 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02003175 if (kv == NULL) {
3176 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003177 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02003178 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02003179 return PyDict_GetItem(dp, kv);
3180}
3181
Guido van Rossum3cca2451997-05-16 14:23:33 +00003182/* For backward compatibility with old dictionary interface */
3183
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003184PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003185PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003187 PyObject *kv, *rv;
3188 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003189 if (kv == NULL) {
3190 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003191 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003192 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003193 rv = PyDict_GetItem(v, kv);
3194 Py_DECREF(kv);
3195 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003196}
3197
3198int
Victor Stinner3c1e4812012-03-26 22:10:51 +02003199_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
3200{
3201 PyObject *kv;
3202 kv = _PyUnicode_FromId(key); /* borrowed */
3203 if (kv == NULL)
3204 return -1;
3205 return PyDict_SetItem(v, kv, item);
3206}
3207
3208int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003209PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003210{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003211 PyObject *kv;
3212 int err;
3213 kv = PyUnicode_FromString(key);
3214 if (kv == NULL)
3215 return -1;
3216 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
3217 err = PyDict_SetItem(v, kv, item);
3218 Py_DECREF(kv);
3219 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003220}
3221
3222int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01003223_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
3224{
3225 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
3226 if (kv == NULL)
3227 return -1;
3228 return PyDict_DelItem(v, kv);
3229}
3230
3231int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003232PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003233{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003234 PyObject *kv;
3235 int err;
3236 kv = PyUnicode_FromString(key);
3237 if (kv == NULL)
3238 return -1;
3239 err = PyDict_DelItem(v, kv);
3240 Py_DECREF(kv);
3241 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003242}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003243
Raymond Hettinger019a1482004-03-18 02:41:19 +00003244/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003245
3246typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003247 PyObject_HEAD
3248 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
3249 Py_ssize_t di_used;
3250 Py_ssize_t di_pos;
3251 PyObject* di_result; /* reusable result tuple for iteritems */
3252 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003253} dictiterobject;
3254
3255static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003256dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003257{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003258 dictiterobject *di;
3259 di = PyObject_GC_New(dictiterobject, itertype);
3260 if (di == NULL)
3261 return NULL;
3262 Py_INCREF(dict);
3263 di->di_dict = dict;
3264 di->di_used = dict->ma_used;
3265 di->di_pos = 0;
3266 di->len = dict->ma_used;
3267 if (itertype == &PyDictIterItem_Type) {
3268 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
3269 if (di->di_result == NULL) {
3270 Py_DECREF(di);
3271 return NULL;
3272 }
3273 }
3274 else
3275 di->di_result = NULL;
3276 _PyObject_GC_TRACK(di);
3277 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003278}
3279
3280static void
3281dictiter_dealloc(dictiterobject *di)
3282{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003283 Py_XDECREF(di->di_dict);
3284 Py_XDECREF(di->di_result);
3285 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003286}
3287
3288static int
3289dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
3290{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003291 Py_VISIT(di->di_dict);
3292 Py_VISIT(di->di_result);
3293 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003294}
3295
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003296static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003297dictiter_len(dictiterobject *di)
3298{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003299 Py_ssize_t len = 0;
3300 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
3301 len = di->len;
3302 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003303}
3304
Guido van Rossumb90c8482007-02-10 01:11:45 +00003305PyDoc_STRVAR(length_hint_doc,
3306 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003307
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003308static PyObject *
3309dictiter_reduce(dictiterobject *di);
3310
3311PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3312
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003313static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003314 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
3315 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003316 {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS,
3317 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003318 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003319};
3320
Raymond Hettinger019a1482004-03-18 02:41:19 +00003321static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00003322{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003323 PyObject *key;
Victor Stinner742da042016-09-07 17:40:12 -07003324 Py_ssize_t i, n, offset;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003325 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003326 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003327 PyObject **value_ptr;
Guido van Rossum213c7a62001-04-23 14:08:49 +00003328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003329 if (d == NULL)
3330 return NULL;
3331 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00003332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003333 if (di->di_used != d->ma_used) {
3334 PyErr_SetString(PyExc_RuntimeError,
3335 "dictionary changed size during iteration");
3336 di->di_used = -1; /* Make this state sticky */
3337 return NULL;
3338 }
Guido van Rossum2147df72002-07-16 20:30:22 +00003339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003340 i = di->di_pos;
3341 if (i < 0)
3342 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003343 k = d->ma_keys;
3344 if (d->ma_values) {
3345 value_ptr = &d->ma_values[i];
3346 offset = sizeof(PyObject *);
3347 }
3348 else {
Victor Stinner742da042016-09-07 17:40:12 -07003349 value_ptr = &DK_ENTRIES(k)[i].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003350 offset = sizeof(PyDictKeyEntry);
3351 }
Victor Stinner742da042016-09-07 17:40:12 -07003352 n = k->dk_nentries - 1;
3353 while (i <= n && *value_ptr == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003354 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003355 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003356 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003357 di->di_pos = i+1;
Victor Stinner742da042016-09-07 17:40:12 -07003358 if (i > n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003359 goto fail;
3360 di->len--;
Victor Stinner742da042016-09-07 17:40:12 -07003361 key = DK_ENTRIES(k)[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003362 Py_INCREF(key);
3363 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003364
3365fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003366 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003367 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003368 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003369}
3370
Raymond Hettinger019a1482004-03-18 02:41:19 +00003371PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003372 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3373 "dict_keyiterator", /* tp_name */
3374 sizeof(dictiterobject), /* tp_basicsize */
3375 0, /* tp_itemsize */
3376 /* methods */
3377 (destructor)dictiter_dealloc, /* tp_dealloc */
3378 0, /* tp_print */
3379 0, /* tp_getattr */
3380 0, /* tp_setattr */
3381 0, /* tp_reserved */
3382 0, /* tp_repr */
3383 0, /* tp_as_number */
3384 0, /* tp_as_sequence */
3385 0, /* tp_as_mapping */
3386 0, /* tp_hash */
3387 0, /* tp_call */
3388 0, /* tp_str */
3389 PyObject_GenericGetAttr, /* tp_getattro */
3390 0, /* tp_setattro */
3391 0, /* tp_as_buffer */
3392 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3393 0, /* tp_doc */
3394 (traverseproc)dictiter_traverse, /* tp_traverse */
3395 0, /* tp_clear */
3396 0, /* tp_richcompare */
3397 0, /* tp_weaklistoffset */
3398 PyObject_SelfIter, /* tp_iter */
3399 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
3400 dictiter_methods, /* tp_methods */
3401 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003402};
3403
3404static PyObject *dictiter_iternextvalue(dictiterobject *di)
3405{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003406 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -07003407 Py_ssize_t i, n, offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003408 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003409 PyObject **value_ptr;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003411 if (d == NULL)
3412 return NULL;
3413 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003415 if (di->di_used != d->ma_used) {
3416 PyErr_SetString(PyExc_RuntimeError,
3417 "dictionary changed size during iteration");
3418 di->di_used = -1; /* Make this state sticky */
3419 return NULL;
3420 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003422 i = di->di_pos;
Victor Stinner742da042016-09-07 17:40:12 -07003423 n = d->ma_keys->dk_nentries - 1;
3424 if (i < 0 || i > n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003425 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003426 if (d->ma_values) {
3427 value_ptr = &d->ma_values[i];
3428 offset = sizeof(PyObject *);
3429 }
3430 else {
Victor Stinner742da042016-09-07 17:40:12 -07003431 value_ptr = &DK_ENTRIES(d->ma_keys)[i].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003432 offset = sizeof(PyDictKeyEntry);
3433 }
Victor Stinner742da042016-09-07 17:40:12 -07003434 while (i <= n && *value_ptr == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003435 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003436 i++;
Victor Stinner742da042016-09-07 17:40:12 -07003437 if (i > n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003438 goto fail;
3439 }
3440 di->di_pos = i+1;
3441 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003442 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003443 Py_INCREF(value);
3444 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003445
3446fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003447 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003448 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003449 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003450}
3451
3452PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003453 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3454 "dict_valueiterator", /* tp_name */
3455 sizeof(dictiterobject), /* tp_basicsize */
3456 0, /* tp_itemsize */
3457 /* methods */
3458 (destructor)dictiter_dealloc, /* tp_dealloc */
3459 0, /* tp_print */
3460 0, /* tp_getattr */
3461 0, /* tp_setattr */
3462 0, /* tp_reserved */
3463 0, /* tp_repr */
3464 0, /* tp_as_number */
3465 0, /* tp_as_sequence */
3466 0, /* tp_as_mapping */
3467 0, /* tp_hash */
3468 0, /* tp_call */
3469 0, /* tp_str */
3470 PyObject_GenericGetAttr, /* tp_getattro */
3471 0, /* tp_setattro */
3472 0, /* tp_as_buffer */
3473 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3474 0, /* tp_doc */
3475 (traverseproc)dictiter_traverse, /* tp_traverse */
3476 0, /* tp_clear */
3477 0, /* tp_richcompare */
3478 0, /* tp_weaklistoffset */
3479 PyObject_SelfIter, /* tp_iter */
3480 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3481 dictiter_methods, /* tp_methods */
3482 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003483};
3484
3485static PyObject *dictiter_iternextitem(dictiterobject *di)
3486{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003487 PyObject *key, *value, *result = di->di_result;
Victor Stinner742da042016-09-07 17:40:12 -07003488 Py_ssize_t i, n, offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003489 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003490 PyObject **value_ptr;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003492 if (d == NULL)
3493 return NULL;
3494 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003496 if (di->di_used != d->ma_used) {
3497 PyErr_SetString(PyExc_RuntimeError,
3498 "dictionary changed size during iteration");
3499 di->di_used = -1; /* Make this state sticky */
3500 return NULL;
3501 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003503 i = di->di_pos;
3504 if (i < 0)
3505 goto fail;
Victor Stinner742da042016-09-07 17:40:12 -07003506 n = d->ma_keys->dk_nentries - 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003507 if (d->ma_values) {
3508 value_ptr = &d->ma_values[i];
3509 offset = sizeof(PyObject *);
3510 }
3511 else {
Victor Stinner742da042016-09-07 17:40:12 -07003512 value_ptr = &DK_ENTRIES(d->ma_keys)[i].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003513 offset = sizeof(PyDictKeyEntry);
3514 }
Victor Stinner742da042016-09-07 17:40:12 -07003515 while (i <= n && *value_ptr == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003516 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003517 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003518 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003519 di->di_pos = i+1;
Victor Stinner742da042016-09-07 17:40:12 -07003520 if (i > n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003521 goto fail;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003523 if (result->ob_refcnt == 1) {
3524 Py_INCREF(result);
3525 Py_DECREF(PyTuple_GET_ITEM(result, 0));
3526 Py_DECREF(PyTuple_GET_ITEM(result, 1));
3527 } else {
3528 result = PyTuple_New(2);
3529 if (result == NULL)
3530 return NULL;
3531 }
3532 di->len--;
Victor Stinner742da042016-09-07 17:40:12 -07003533 key = DK_ENTRIES(d->ma_keys)[i].me_key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003534 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003535 Py_INCREF(key);
3536 Py_INCREF(value);
Eric Snow96c6af92015-05-29 22:21:39 -06003537 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3538 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003539 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003540
3541fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003542 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003543 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003544 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003545}
3546
3547PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003548 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3549 "dict_itemiterator", /* tp_name */
3550 sizeof(dictiterobject), /* tp_basicsize */
3551 0, /* tp_itemsize */
3552 /* methods */
3553 (destructor)dictiter_dealloc, /* tp_dealloc */
3554 0, /* tp_print */
3555 0, /* tp_getattr */
3556 0, /* tp_setattr */
3557 0, /* tp_reserved */
3558 0, /* tp_repr */
3559 0, /* tp_as_number */
3560 0, /* tp_as_sequence */
3561 0, /* tp_as_mapping */
3562 0, /* tp_hash */
3563 0, /* tp_call */
3564 0, /* tp_str */
3565 PyObject_GenericGetAttr, /* tp_getattro */
3566 0, /* tp_setattro */
3567 0, /* tp_as_buffer */
3568 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3569 0, /* tp_doc */
3570 (traverseproc)dictiter_traverse, /* tp_traverse */
3571 0, /* tp_clear */
3572 0, /* tp_richcompare */
3573 0, /* tp_weaklistoffset */
3574 PyObject_SelfIter, /* tp_iter */
3575 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3576 dictiter_methods, /* tp_methods */
3577 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003578};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003579
3580
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003581static PyObject *
3582dictiter_reduce(dictiterobject *di)
3583{
3584 PyObject *list;
3585 dictiterobject tmp;
3586
3587 list = PyList_New(0);
3588 if (!list)
3589 return NULL;
3590
3591 /* copy the itertor state */
3592 tmp = *di;
3593 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003594
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003595 /* iterate the temporary into a list */
3596 for(;;) {
3597 PyObject *element = 0;
3598 if (Py_TYPE(di) == &PyDictIterItem_Type)
3599 element = dictiter_iternextitem(&tmp);
3600 else if (Py_TYPE(di) == &PyDictIterKey_Type)
3601 element = dictiter_iternextkey(&tmp);
3602 else if (Py_TYPE(di) == &PyDictIterValue_Type)
3603 element = dictiter_iternextvalue(&tmp);
3604 else
3605 assert(0);
3606 if (element) {
3607 if (PyList_Append(list, element)) {
3608 Py_DECREF(element);
3609 Py_DECREF(list);
3610 Py_XDECREF(tmp.di_dict);
3611 return NULL;
3612 }
3613 Py_DECREF(element);
3614 } else
3615 break;
3616 }
3617 Py_XDECREF(tmp.di_dict);
3618 /* check for error */
3619 if (tmp.di_dict != NULL) {
3620 /* we have an error */
3621 Py_DECREF(list);
3622 return NULL;
3623 }
Antoine Pitroua7013882012-04-05 00:04:20 +02003624 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003625}
3626
Guido van Rossum3ac67412007-02-10 18:55:06 +00003627/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003628/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003629/***********************************************/
3630
Guido van Rossumb90c8482007-02-10 01:11:45 +00003631/* The instance lay-out is the same for all three; but the type differs. */
3632
Guido van Rossumb90c8482007-02-10 01:11:45 +00003633static void
Eric Snow96c6af92015-05-29 22:21:39 -06003634dictview_dealloc(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003635{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003636 Py_XDECREF(dv->dv_dict);
3637 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003638}
3639
3640static int
Eric Snow96c6af92015-05-29 22:21:39 -06003641dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003642{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003643 Py_VISIT(dv->dv_dict);
3644 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003645}
3646
Guido van Rossum83825ac2007-02-10 04:54:19 +00003647static Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003648dictview_len(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003649{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003650 Py_ssize_t len = 0;
3651 if (dv->dv_dict != NULL)
3652 len = dv->dv_dict->ma_used;
3653 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003654}
3655
Eric Snow96c6af92015-05-29 22:21:39 -06003656PyObject *
3657_PyDictView_New(PyObject *dict, PyTypeObject *type)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003658{
Eric Snow96c6af92015-05-29 22:21:39 -06003659 _PyDictViewObject *dv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003660 if (dict == NULL) {
3661 PyErr_BadInternalCall();
3662 return NULL;
3663 }
3664 if (!PyDict_Check(dict)) {
3665 /* XXX Get rid of this restriction later */
3666 PyErr_Format(PyExc_TypeError,
3667 "%s() requires a dict argument, not '%s'",
3668 type->tp_name, dict->ob_type->tp_name);
3669 return NULL;
3670 }
Eric Snow96c6af92015-05-29 22:21:39 -06003671 dv = PyObject_GC_New(_PyDictViewObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003672 if (dv == NULL)
3673 return NULL;
3674 Py_INCREF(dict);
3675 dv->dv_dict = (PyDictObject *)dict;
3676 _PyObject_GC_TRACK(dv);
3677 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003678}
3679
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003680/* TODO(guido): The views objects are not complete:
3681
3682 * support more set operations
3683 * support arbitrary mappings?
3684 - either these should be static or exported in dictobject.h
3685 - if public then they should probably be in builtins
3686*/
3687
Guido van Rossumaac530c2007-08-24 22:33:45 +00003688/* Return 1 if self is a subset of other, iterating over self;
3689 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003690static int
3691all_contained_in(PyObject *self, PyObject *other)
3692{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003693 PyObject *iter = PyObject_GetIter(self);
3694 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003696 if (iter == NULL)
3697 return -1;
3698 for (;;) {
3699 PyObject *next = PyIter_Next(iter);
3700 if (next == NULL) {
3701 if (PyErr_Occurred())
3702 ok = -1;
3703 break;
3704 }
3705 ok = PySequence_Contains(other, next);
3706 Py_DECREF(next);
3707 if (ok <= 0)
3708 break;
3709 }
3710 Py_DECREF(iter);
3711 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003712}
3713
3714static PyObject *
3715dictview_richcompare(PyObject *self, PyObject *other, int op)
3716{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003717 Py_ssize_t len_self, len_other;
3718 int ok;
3719 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003721 assert(self != NULL);
3722 assert(PyDictViewSet_Check(self));
3723 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00003724
Brian Curtindfc80e32011-08-10 20:28:54 -05003725 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
3726 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003727
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003728 len_self = PyObject_Size(self);
3729 if (len_self < 0)
3730 return NULL;
3731 len_other = PyObject_Size(other);
3732 if (len_other < 0)
3733 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003734
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003735 ok = 0;
3736 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00003737
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003738 case Py_NE:
3739 case Py_EQ:
3740 if (len_self == len_other)
3741 ok = all_contained_in(self, other);
3742 if (op == Py_NE && ok >= 0)
3743 ok = !ok;
3744 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003745
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003746 case Py_LT:
3747 if (len_self < len_other)
3748 ok = all_contained_in(self, other);
3749 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003751 case Py_LE:
3752 if (len_self <= len_other)
3753 ok = all_contained_in(self, other);
3754 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003756 case Py_GT:
3757 if (len_self > len_other)
3758 ok = all_contained_in(other, self);
3759 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003760
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003761 case Py_GE:
3762 if (len_self >= len_other)
3763 ok = all_contained_in(other, self);
3764 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003766 }
3767 if (ok < 0)
3768 return NULL;
3769 result = ok ? Py_True : Py_False;
3770 Py_INCREF(result);
3771 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003772}
3773
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003774static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003775dictview_repr(_PyDictViewObject *dv)
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003776{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003777 PyObject *seq;
3778 PyObject *result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003779
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003780 seq = PySequence_List((PyObject *)dv);
3781 if (seq == NULL)
3782 return NULL;
3783
3784 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
3785 Py_DECREF(seq);
3786 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003787}
3788
Guido van Rossum3ac67412007-02-10 18:55:06 +00003789/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003790
3791static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003792dictkeys_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003793{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003794 if (dv->dv_dict == NULL) {
3795 Py_RETURN_NONE;
3796 }
3797 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003798}
3799
3800static int
Eric Snow96c6af92015-05-29 22:21:39 -06003801dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00003802{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003803 if (dv->dv_dict == NULL)
3804 return 0;
3805 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003806}
3807
Guido van Rossum83825ac2007-02-10 04:54:19 +00003808static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003809 (lenfunc)dictview_len, /* sq_length */
3810 0, /* sq_concat */
3811 0, /* sq_repeat */
3812 0, /* sq_item */
3813 0, /* sq_slice */
3814 0, /* sq_ass_item */
3815 0, /* sq_ass_slice */
3816 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003817};
3818
Guido van Rossum523259b2007-08-24 23:41:22 +00003819static PyObject*
3820dictviews_sub(PyObject* self, PyObject *other)
3821{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003822 PyObject *result = PySet_New(self);
3823 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003824 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003826 if (result == NULL)
3827 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003828
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003829 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003830 if (tmp == NULL) {
3831 Py_DECREF(result);
3832 return NULL;
3833 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003835 Py_DECREF(tmp);
3836 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003837}
3838
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003839PyObject*
3840_PyDictView_Intersect(PyObject* self, PyObject *other)
Guido van Rossum523259b2007-08-24 23:41:22 +00003841{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003842 PyObject *result = PySet_New(self);
3843 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003844 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003846 if (result == NULL)
3847 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003848
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003849 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_intersection_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003850 if (tmp == NULL) {
3851 Py_DECREF(result);
3852 return NULL;
3853 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003854
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003855 Py_DECREF(tmp);
3856 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003857}
3858
3859static PyObject*
3860dictviews_or(PyObject* self, PyObject *other)
3861{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003862 PyObject *result = PySet_New(self);
3863 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003864 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003866 if (result == NULL)
3867 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003868
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003869 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003870 if (tmp == NULL) {
3871 Py_DECREF(result);
3872 return NULL;
3873 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003875 Py_DECREF(tmp);
3876 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003877}
3878
3879static PyObject*
3880dictviews_xor(PyObject* self, PyObject *other)
3881{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003882 PyObject *result = PySet_New(self);
3883 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003884 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003886 if (result == NULL)
3887 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003888
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003889 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_symmetric_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003890 if (tmp == NULL) {
3891 Py_DECREF(result);
3892 return NULL;
3893 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003895 Py_DECREF(tmp);
3896 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003897}
3898
3899static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003900 0, /*nb_add*/
3901 (binaryfunc)dictviews_sub, /*nb_subtract*/
3902 0, /*nb_multiply*/
3903 0, /*nb_remainder*/
3904 0, /*nb_divmod*/
3905 0, /*nb_power*/
3906 0, /*nb_negative*/
3907 0, /*nb_positive*/
3908 0, /*nb_absolute*/
3909 0, /*nb_bool*/
3910 0, /*nb_invert*/
3911 0, /*nb_lshift*/
3912 0, /*nb_rshift*/
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003913 (binaryfunc)_PyDictView_Intersect, /*nb_and*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003914 (binaryfunc)dictviews_xor, /*nb_xor*/
3915 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00003916};
3917
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003918static PyObject*
3919dictviews_isdisjoint(PyObject *self, PyObject *other)
3920{
3921 PyObject *it;
3922 PyObject *item = NULL;
3923
3924 if (self == other) {
Eric Snow96c6af92015-05-29 22:21:39 -06003925 if (dictview_len((_PyDictViewObject *)self) == 0)
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003926 Py_RETURN_TRUE;
3927 else
3928 Py_RETURN_FALSE;
3929 }
3930
3931 /* Iterate over the shorter object (only if other is a set,
3932 * because PySequence_Contains may be expensive otherwise): */
3933 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
Eric Snow96c6af92015-05-29 22:21:39 -06003934 Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self);
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003935 Py_ssize_t len_other = PyObject_Size(other);
3936 if (len_other == -1)
3937 return NULL;
3938
3939 if ((len_other > len_self)) {
3940 PyObject *tmp = other;
3941 other = self;
3942 self = tmp;
3943 }
3944 }
3945
3946 it = PyObject_GetIter(other);
3947 if (it == NULL)
3948 return NULL;
3949
3950 while ((item = PyIter_Next(it)) != NULL) {
3951 int contains = PySequence_Contains(self, item);
3952 Py_DECREF(item);
3953 if (contains == -1) {
3954 Py_DECREF(it);
3955 return NULL;
3956 }
3957
3958 if (contains) {
3959 Py_DECREF(it);
3960 Py_RETURN_FALSE;
3961 }
3962 }
3963 Py_DECREF(it);
3964 if (PyErr_Occurred())
3965 return NULL; /* PyIter_Next raised an exception. */
3966 Py_RETURN_TRUE;
3967}
3968
3969PyDoc_STRVAR(isdisjoint_doc,
3970"Return True if the view and the given iterable have a null intersection.");
3971
Guido van Rossumb90c8482007-02-10 01:11:45 +00003972static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003973 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3974 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003975 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003976};
3977
3978PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003979 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3980 "dict_keys", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06003981 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003982 0, /* tp_itemsize */
3983 /* methods */
3984 (destructor)dictview_dealloc, /* tp_dealloc */
3985 0, /* tp_print */
3986 0, /* tp_getattr */
3987 0, /* tp_setattr */
3988 0, /* tp_reserved */
3989 (reprfunc)dictview_repr, /* tp_repr */
3990 &dictviews_as_number, /* tp_as_number */
3991 &dictkeys_as_sequence, /* tp_as_sequence */
3992 0, /* tp_as_mapping */
3993 0, /* tp_hash */
3994 0, /* tp_call */
3995 0, /* tp_str */
3996 PyObject_GenericGetAttr, /* tp_getattro */
3997 0, /* tp_setattro */
3998 0, /* tp_as_buffer */
3999 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4000 0, /* tp_doc */
4001 (traverseproc)dictview_traverse, /* tp_traverse */
4002 0, /* tp_clear */
4003 dictview_richcompare, /* tp_richcompare */
4004 0, /* tp_weaklistoffset */
4005 (getiterfunc)dictkeys_iter, /* tp_iter */
4006 0, /* tp_iternext */
4007 dictkeys_methods, /* tp_methods */
4008 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004009};
4010
4011static PyObject *
4012dictkeys_new(PyObject *dict)
4013{
Eric Snow96c6af92015-05-29 22:21:39 -06004014 return _PyDictView_New(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004015}
4016
Guido van Rossum3ac67412007-02-10 18:55:06 +00004017/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004018
4019static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004020dictitems_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004021{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004022 if (dv->dv_dict == NULL) {
4023 Py_RETURN_NONE;
4024 }
4025 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004026}
4027
4028static int
Eric Snow96c6af92015-05-29 22:21:39 -06004029dictitems_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004030{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004031 PyObject *key, *value, *found;
4032 if (dv->dv_dict == NULL)
4033 return 0;
4034 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
4035 return 0;
4036 key = PyTuple_GET_ITEM(obj, 0);
4037 value = PyTuple_GET_ITEM(obj, 1);
4038 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
4039 if (found == NULL) {
4040 if (PyErr_Occurred())
4041 return -1;
4042 return 0;
4043 }
4044 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004045}
4046
Guido van Rossum83825ac2007-02-10 04:54:19 +00004047static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004048 (lenfunc)dictview_len, /* sq_length */
4049 0, /* sq_concat */
4050 0, /* sq_repeat */
4051 0, /* sq_item */
4052 0, /* sq_slice */
4053 0, /* sq_ass_item */
4054 0, /* sq_ass_slice */
4055 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004056};
4057
Guido van Rossumb90c8482007-02-10 01:11:45 +00004058static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004059 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4060 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004061 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004062};
4063
4064PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004065 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4066 "dict_items", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004067 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004068 0, /* tp_itemsize */
4069 /* methods */
4070 (destructor)dictview_dealloc, /* tp_dealloc */
4071 0, /* tp_print */
4072 0, /* tp_getattr */
4073 0, /* tp_setattr */
4074 0, /* tp_reserved */
4075 (reprfunc)dictview_repr, /* tp_repr */
4076 &dictviews_as_number, /* tp_as_number */
4077 &dictitems_as_sequence, /* tp_as_sequence */
4078 0, /* tp_as_mapping */
4079 0, /* tp_hash */
4080 0, /* tp_call */
4081 0, /* tp_str */
4082 PyObject_GenericGetAttr, /* tp_getattro */
4083 0, /* tp_setattro */
4084 0, /* tp_as_buffer */
4085 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4086 0, /* tp_doc */
4087 (traverseproc)dictview_traverse, /* tp_traverse */
4088 0, /* tp_clear */
4089 dictview_richcompare, /* tp_richcompare */
4090 0, /* tp_weaklistoffset */
4091 (getiterfunc)dictitems_iter, /* tp_iter */
4092 0, /* tp_iternext */
4093 dictitems_methods, /* tp_methods */
4094 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004095};
4096
4097static PyObject *
4098dictitems_new(PyObject *dict)
4099{
Eric Snow96c6af92015-05-29 22:21:39 -06004100 return _PyDictView_New(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004101}
4102
Guido van Rossum3ac67412007-02-10 18:55:06 +00004103/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004104
4105static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004106dictvalues_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004107{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004108 if (dv->dv_dict == NULL) {
4109 Py_RETURN_NONE;
4110 }
4111 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004112}
4113
Guido van Rossum83825ac2007-02-10 04:54:19 +00004114static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004115 (lenfunc)dictview_len, /* sq_length */
4116 0, /* sq_concat */
4117 0, /* sq_repeat */
4118 0, /* sq_item */
4119 0, /* sq_slice */
4120 0, /* sq_ass_item */
4121 0, /* sq_ass_slice */
4122 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004123};
4124
Guido van Rossumb90c8482007-02-10 01:11:45 +00004125static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004126 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004127};
4128
4129PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004130 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4131 "dict_values", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004132 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004133 0, /* tp_itemsize */
4134 /* methods */
4135 (destructor)dictview_dealloc, /* tp_dealloc */
4136 0, /* tp_print */
4137 0, /* tp_getattr */
4138 0, /* tp_setattr */
4139 0, /* tp_reserved */
4140 (reprfunc)dictview_repr, /* tp_repr */
4141 0, /* tp_as_number */
4142 &dictvalues_as_sequence, /* tp_as_sequence */
4143 0, /* tp_as_mapping */
4144 0, /* tp_hash */
4145 0, /* tp_call */
4146 0, /* tp_str */
4147 PyObject_GenericGetAttr, /* tp_getattro */
4148 0, /* tp_setattro */
4149 0, /* tp_as_buffer */
4150 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4151 0, /* tp_doc */
4152 (traverseproc)dictview_traverse, /* tp_traverse */
4153 0, /* tp_clear */
4154 0, /* tp_richcompare */
4155 0, /* tp_weaklistoffset */
4156 (getiterfunc)dictvalues_iter, /* tp_iter */
4157 0, /* tp_iternext */
4158 dictvalues_methods, /* tp_methods */
4159 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004160};
4161
4162static PyObject *
4163dictvalues_new(PyObject *dict)
4164{
Eric Snow96c6af92015-05-29 22:21:39 -06004165 return _PyDictView_New(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004166}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004167
4168/* Returns NULL if cannot allocate a new PyDictKeysObject,
4169 but does not set an error */
4170PyDictKeysObject *
4171_PyDict_NewKeysForClass(void)
4172{
Victor Stinner742da042016-09-07 17:40:12 -07004173 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004174 if (keys == NULL)
4175 PyErr_Clear();
4176 else
4177 keys->dk_lookup = lookdict_split;
4178 return keys;
4179}
4180
4181#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
4182
4183PyObject *
4184PyObject_GenericGetDict(PyObject *obj, void *context)
4185{
4186 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
4187 if (dictptr == NULL) {
4188 PyErr_SetString(PyExc_AttributeError,
4189 "This object has no __dict__");
4190 return NULL;
4191 }
4192 dict = *dictptr;
4193 if (dict == NULL) {
4194 PyTypeObject *tp = Py_TYPE(obj);
4195 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
4196 DK_INCREF(CACHED_KEYS(tp));
4197 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
4198 }
4199 else {
4200 *dictptr = dict = PyDict_New();
4201 }
4202 }
4203 Py_XINCREF(dict);
4204 return dict;
4205}
4206
4207int
4208_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
Victor Stinner742da042016-09-07 17:40:12 -07004209 PyObject *key, PyObject *value)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004210{
4211 PyObject *dict;
4212 int res;
4213 PyDictKeysObject *cached;
4214
4215 assert(dictptr != NULL);
4216 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
4217 assert(dictptr != NULL);
4218 dict = *dictptr;
4219 if (dict == NULL) {
4220 DK_INCREF(cached);
4221 dict = new_dict_with_shared_keys(cached);
4222 if (dict == NULL)
4223 return -1;
4224 *dictptr = dict;
4225 }
4226 if (value == NULL) {
4227 res = PyDict_DelItem(dict, key);
4228 if (cached != ((PyDictObject *)dict)->ma_keys) {
4229 CACHED_KEYS(tp) = NULL;
4230 DK_DECREF(cached);
4231 }
4232 } else {
4233 res = PyDict_SetItem(dict, key, value);
4234 if (cached != ((PyDictObject *)dict)->ma_keys) {
4235 /* Either update tp->ht_cached_keys or delete it */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004236 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004237 CACHED_KEYS(tp) = make_keys_shared(dict);
Victor Stinner742da042016-09-07 17:40:12 -07004238 }
4239 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004240 CACHED_KEYS(tp) = NULL;
4241 }
4242 DK_DECREF(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004243 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
4244 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004245 }
4246 }
4247 } else {
4248 dict = *dictptr;
4249 if (dict == NULL) {
4250 dict = PyDict_New();
4251 if (dict == NULL)
4252 return -1;
4253 *dictptr = dict;
4254 }
4255 if (value == NULL) {
4256 res = PyDict_DelItem(dict, key);
4257 } else {
4258 res = PyDict_SetItem(dict, key, value);
4259 }
4260 }
4261 return res;
4262}
4263
4264void
4265_PyDictKeys_DecRef(PyDictKeysObject *keys)
4266{
4267 DK_DECREF(keys);
4268}