blob: 3cbc3fdeb5a3596b0359ea26f94c373c386ce38a [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
Tim Peterseb28ef22001-06-02 05:27:19 +0000187 perturb >>= PERTURB_SHIFT;
INADA Naoki267941c2016-10-06 15:19:07 +0900188 j = (5*j) + 1 + perturb;
Tim Peterseb28ef22001-06-02 05:27:19 +0000189 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 = {
Serhiy Storchaka97932e42016-09-26 23:01:23 +0300422 1, /* dk_refcnt */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400423 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{
INADA Naoki267941c2016-10-06 15:19:07 +0900633 size_t i;
Victor Stinner742da042016-09-07 17:40:12 -0700634 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
INADA Naoki267941c2016-10-06 15:19:07 +0900646 for (size_t perturb = hash;;) {
647 perturb >>= PERTURB_SHIFT;
Victor Stinner742da042016-09-07 17:40:12 -0700648 i = mask & ((i << 2) + i + perturb + 1);
649 ix = dk_get_index(k, i);
650 if (ix == index) {
651 return i;
652 }
653 if (ix == DKIX_EMPTY) {
654 return DKIX_EMPTY;
655 }
656 }
657 assert(0); /* NOT REACHED */
658 return DKIX_ERROR;
659}
660
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000661/*
662The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000663This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000664Open addressing is preferred over chaining since the link overhead for
665chaining would be substantial (100% with typical malloc overhead).
666
Tim Peterseb28ef22001-06-02 05:27:19 +0000667The initial probe index is computed as hash mod the table size. Subsequent
668probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000669
670All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000671
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000672The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000673contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000674Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000675
Victor Stinner742da042016-09-07 17:40:12 -0700676lookdict() is general-purpose, and may return DKIX_ERROR if (and only if) a
Victor Stinnera4348cc2016-09-08 12:01:25 -0700677comparison raises an exception.
Guido van Rossum89d8c602007-09-18 17:26:56 +0000678lookdict_unicode() below is specialized to string keys, comparison of which can
Victor Stinner742da042016-09-07 17:40:12 -0700679never raise an exception; that function can never return DKIX_ERROR.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400680lookdict_unicode_nodummy is further specialized for string keys that cannot be
681the <dummy> value.
Victor Stinner742da042016-09-07 17:40:12 -0700682For both, when the key isn't found a DKIX_EMPTY is returned. hashpos returns
683where the key index should be inserted.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000684*/
Victor Stinner742da042016-09-07 17:40:12 -0700685static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400686lookdict(PyDictObject *mp, PyObject *key,
Victor Stinner742da042016-09-07 17:40:12 -0700687 Py_hash_t hash, PyObject ***value_addr, Py_ssize_t *hashpos)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000688{
INADA Naoki267941c2016-10-06 15:19:07 +0900689 size_t i, mask;
Victor Stinner742da042016-09-07 17:40:12 -0700690 Py_ssize_t ix, freeslot;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200691 int cmp;
Victor Stinner742da042016-09-07 17:40:12 -0700692 PyDictKeysObject *dk;
693 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000695
Antoine Pitrou9a234902012-05-13 20:48:01 +0200696top:
Victor Stinner742da042016-09-07 17:40:12 -0700697 dk = mp->ma_keys;
698 mask = DK_MASK(dk);
699 ep0 = DK_ENTRIES(dk);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700701
702 ix = dk_get_index(dk, i);
703 if (ix == DKIX_EMPTY) {
704 if (hashpos != NULL)
705 *hashpos = i;
706 *value_addr = NULL;
707 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400708 }
Victor Stinner742da042016-09-07 17:40:12 -0700709 if (ix == DKIX_DUMMY) {
710 freeslot = i;
711 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 else {
Victor Stinner742da042016-09-07 17:40:12 -0700713 ep = &ep0[ix];
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300714 assert(ep->me_key != NULL);
Victor Stinner742da042016-09-07 17:40:12 -0700715 if (ep->me_key == key) {
716 *value_addr = &ep->me_value;
717 if (hashpos != NULL)
718 *hashpos = i;
719 return ix;
720 }
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300721 if (ep->me_hash == hash) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 startkey = ep->me_key;
723 Py_INCREF(startkey);
724 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
725 Py_DECREF(startkey);
726 if (cmp < 0)
Victor Stinner742da042016-09-07 17:40:12 -0700727 return DKIX_ERROR;
728 if (dk == mp->ma_keys && ep->me_key == startkey) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400729 if (cmp > 0) {
730 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700731 if (hashpos != NULL)
732 *hashpos = i;
733 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400734 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 }
736 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200737 /* The dict was mutated, restart */
738 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 }
740 }
Victor Stinner742da042016-09-07 17:40:12 -0700741 freeslot = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 }
Tim Peters15d49292001-05-27 07:39:22 +0000743
INADA Naoki267941c2016-10-06 15:19:07 +0900744 for (size_t perturb = hash;;) {
745 perturb >>= PERTURB_SHIFT;
Victor Stinner742da042016-09-07 17:40:12 -0700746 i = ((i << 2) + i + perturb + 1) & mask;
747 ix = dk_get_index(dk, i);
748 if (ix == DKIX_EMPTY) {
749 if (hashpos != NULL) {
750 *hashpos = (freeslot == -1) ? (Py_ssize_t)i : freeslot;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400751 }
Victor Stinner742da042016-09-07 17:40:12 -0700752 *value_addr = NULL;
753 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400754 }
Victor Stinner742da042016-09-07 17:40:12 -0700755 if (ix == DKIX_DUMMY) {
756 if (freeslot == -1)
757 freeslot = i;
758 continue;
759 }
760 ep = &ep0[ix];
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300761 assert(ep->me_key != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400762 if (ep->me_key == key) {
Victor Stinner742da042016-09-07 17:40:12 -0700763 if (hashpos != NULL) {
764 *hashpos = i;
765 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400766 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700767 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400768 }
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300769 if (ep->me_hash == hash) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 startkey = ep->me_key;
771 Py_INCREF(startkey);
772 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
773 Py_DECREF(startkey);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400774 if (cmp < 0) {
775 *value_addr = NULL;
Victor Stinner742da042016-09-07 17:40:12 -0700776 return DKIX_ERROR;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400777 }
Victor Stinner742da042016-09-07 17:40:12 -0700778 if (dk == mp->ma_keys && ep->me_key == startkey) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400779 if (cmp > 0) {
Victor Stinner742da042016-09-07 17:40:12 -0700780 if (hashpos != NULL) {
781 *hashpos = i;
782 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400783 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700784 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400785 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 }
787 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200788 /* The dict was mutated, restart */
789 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 }
791 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 }
793 assert(0); /* NOT REACHED */
794 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000795}
796
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400797/* Specialized version for string-only keys */
Victor Stinner742da042016-09-07 17:40:12 -0700798static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400799lookdict_unicode(PyDictObject *mp, PyObject *key,
Victor Stinner742da042016-09-07 17:40:12 -0700800 Py_hash_t hash, PyObject ***value_addr, Py_ssize_t *hashpos)
Fred Drake1bff34a2000-08-31 19:31:38 +0000801{
INADA Naoki267941c2016-10-06 15:19:07 +0900802 size_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200803 size_t mask = DK_MASK(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -0700804 Py_ssize_t ix, freeslot;
805 PyDictKeyEntry *ep, *ep0 = DK_ENTRIES(mp->ma_keys);
Fred Drake1bff34a2000-08-31 19:31:38 +0000806
Victor Stinner742da042016-09-07 17:40:12 -0700807 assert(mp->ma_values == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 /* Make sure this function doesn't have to handle non-unicode keys,
809 including subclasses of str; e.g., one reason to subclass
810 unicodes is to override __eq__, and for speed we don't cater to
811 that here. */
812 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400813 mp->ma_keys->dk_lookup = lookdict;
Victor Stinner742da042016-09-07 17:40:12 -0700814 return lookdict(mp, key, hash, value_addr, hashpos);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 }
Mark Dickinson57e683e2011-09-24 18:18:40 +0100816 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700817 ix = dk_get_index(mp->ma_keys, i);
818 if (ix == DKIX_EMPTY) {
819 if (hashpos != NULL)
820 *hashpos = i;
821 *value_addr = NULL;
822 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400823 }
Victor Stinner742da042016-09-07 17:40:12 -0700824 if (ix == DKIX_DUMMY) {
825 freeslot = i;
826 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 else {
Victor Stinner742da042016-09-07 17:40:12 -0700828 ep = &ep0[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700829 assert(ep->me_key != NULL);
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300830 if (ep->me_key == key
831 || (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700832 if (hashpos != NULL)
833 *hashpos = i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400834 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700835 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400836 }
Victor Stinner742da042016-09-07 17:40:12 -0700837 freeslot = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 }
Tim Peters15d49292001-05-27 07:39:22 +0000839
INADA Naoki267941c2016-10-06 15:19:07 +0900840 for (size_t perturb = hash;;) {
841 perturb >>= PERTURB_SHIFT;
Victor Stinner742da042016-09-07 17:40:12 -0700842 i = mask & ((i << 2) + i + perturb + 1);
843 ix = dk_get_index(mp->ma_keys, i);
844 if (ix == DKIX_EMPTY) {
845 if (hashpos != NULL) {
846 *hashpos = (freeslot == -1) ? (Py_ssize_t)i : freeslot;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400847 }
Victor Stinner742da042016-09-07 17:40:12 -0700848 *value_addr = NULL;
849 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400850 }
Victor Stinner742da042016-09-07 17:40:12 -0700851 if (ix == DKIX_DUMMY) {
852 if (freeslot == -1)
853 freeslot = i;
854 continue;
855 }
856 ep = &ep0[ix];
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300857 assert(ep->me_key != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 if (ep->me_key == key
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300859 || (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400860 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700861 if (hashpos != NULL) {
862 *hashpos = i;
863 }
864 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400865 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 }
867 assert(0); /* NOT REACHED */
868 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000869}
870
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400871/* Faster version of lookdict_unicode when it is known that no <dummy> keys
872 * will be present. */
Victor Stinner742da042016-09-07 17:40:12 -0700873static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400874lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
Victor Stinner742da042016-09-07 17:40:12 -0700875 Py_hash_t hash, PyObject ***value_addr,
876 Py_ssize_t *hashpos)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400877{
INADA Naoki267941c2016-10-06 15:19:07 +0900878 size_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200879 size_t mask = DK_MASK(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -0700880 Py_ssize_t ix;
881 PyDictKeyEntry *ep, *ep0 = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400882
Victor Stinner742da042016-09-07 17:40:12 -0700883 assert(mp->ma_values == NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400884 /* Make sure this function doesn't have to handle non-unicode keys,
885 including subclasses of str; e.g., one reason to subclass
886 unicodes is to override __eq__, and for speed we don't cater to
887 that here. */
888 if (!PyUnicode_CheckExact(key)) {
889 mp->ma_keys->dk_lookup = lookdict;
Victor Stinner742da042016-09-07 17:40:12 -0700890 return lookdict(mp, key, hash, value_addr, hashpos);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400891 }
892 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700893 ix = dk_get_index(mp->ma_keys, i);
894 assert (ix != DKIX_DUMMY);
895 if (ix == DKIX_EMPTY) {
896 if (hashpos != NULL)
897 *hashpos = i;
898 *value_addr = NULL;
899 return DKIX_EMPTY;
900 }
901 ep = &ep0[ix];
Victor Stinnerdee6e252016-09-08 11:16:07 -0700902 assert(ep->me_key != NULL);
903 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700904 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400905 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700906 if (hashpos != NULL)
907 *hashpos = i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400908 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700909 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400910 }
INADA Naoki267941c2016-10-06 15:19:07 +0900911 for (size_t perturb = hash;;) {
912 perturb >>= PERTURB_SHIFT;
Victor Stinner742da042016-09-07 17:40:12 -0700913 i = mask & ((i << 2) + i + perturb + 1);
914 ix = dk_get_index(mp->ma_keys, i);
915 assert (ix != DKIX_DUMMY);
916 if (ix == DKIX_EMPTY) {
917 if (hashpos != NULL)
918 *hashpos = i;
919 *value_addr = NULL;
920 return DKIX_EMPTY;
921 }
922 ep = &ep0[ix];
923 assert(ep->me_key != NULL && PyUnicode_CheckExact(ep->me_key));
924 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400925 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700926 if (hashpos != NULL)
927 *hashpos = i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400928 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700929 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400930 }
931 }
932 assert(0); /* NOT REACHED */
933 return 0;
934}
935
936/* Version of lookdict for split tables.
937 * All split tables and only split tables use this lookup function.
938 * Split tables only contain unicode keys and no dummy keys,
939 * so algorithm is the same as lookdict_unicode_nodummy.
940 */
Victor Stinner742da042016-09-07 17:40:12 -0700941static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400942lookdict_split(PyDictObject *mp, PyObject *key,
Victor Stinner742da042016-09-07 17:40:12 -0700943 Py_hash_t hash, PyObject ***value_addr, Py_ssize_t *hashpos)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400944{
INADA Naoki267941c2016-10-06 15:19:07 +0900945 size_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200946 size_t mask = DK_MASK(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -0700947 Py_ssize_t ix;
948 PyDictKeyEntry *ep, *ep0 = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400949
Victor Stinner742da042016-09-07 17:40:12 -0700950 /* mp must split table */
951 assert(mp->ma_values != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400952 if (!PyUnicode_CheckExact(key)) {
Victor Stinner742da042016-09-07 17:40:12 -0700953 ix = lookdict(mp, key, hash, value_addr, hashpos);
954 if (ix >= 0) {
955 *value_addr = &mp->ma_values[ix];
956 }
957 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400958 }
Victor Stinner742da042016-09-07 17:40:12 -0700959
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400960 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700961 ix = dk_get_index(mp->ma_keys, i);
962 if (ix == DKIX_EMPTY) {
963 if (hashpos != NULL)
964 *hashpos = i;
965 *value_addr = NULL;
966 return DKIX_EMPTY;
967 }
968 assert(ix >= 0);
969 ep = &ep0[ix];
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300970 assert(ep->me_key != NULL && PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700971 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400972 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700973 if (hashpos != NULL)
974 *hashpos = i;
975 *value_addr = &mp->ma_values[ix];
976 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400977 }
INADA Naoki267941c2016-10-06 15:19:07 +0900978 for (size_t perturb = hash;;) {
979 perturb >>= PERTURB_SHIFT;
Victor Stinner742da042016-09-07 17:40:12 -0700980 i = mask & ((i << 2) + i + perturb + 1);
981 ix = dk_get_index(mp->ma_keys, i);
982 if (ix == DKIX_EMPTY) {
983 if (hashpos != NULL)
984 *hashpos = i;
985 *value_addr = NULL;
986 return DKIX_EMPTY;
987 }
988 assert(ix >= 0);
989 ep = &ep0[ix];
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300990 assert(ep->me_key != NULL && PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700991 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400992 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700993 if (hashpos != NULL)
994 *hashpos = i;
995 *value_addr = &mp->ma_values[ix];
996 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400997 }
998 }
999 assert(0); /* NOT REACHED */
1000 return 0;
1001}
1002
Benjamin Petersonfb886362010-04-24 18:21:17 +00001003int
1004_PyDict_HasOnlyStringKeys(PyObject *dict)
1005{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 Py_ssize_t pos = 0;
1007 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +00001008 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001010 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 return 1;
1012 while (PyDict_Next(dict, &pos, &key, &value))
1013 if (!PyUnicode_Check(key))
1014 return 0;
1015 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +00001016}
1017
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001018#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 do { \
1020 if (!_PyObject_GC_IS_TRACKED(mp)) { \
1021 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
1022 _PyObject_GC_MAY_BE_TRACKED(value)) { \
1023 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 } \
1025 } \
1026 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001027
1028void
1029_PyDict_MaybeUntrack(PyObject *op)
1030{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 PyDictObject *mp;
1032 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -07001033 Py_ssize_t i, numentries;
1034 PyDictKeyEntry *ep0;
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001035
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
1037 return;
1038
1039 mp = (PyDictObject *) op;
Victor Stinner742da042016-09-07 17:40:12 -07001040 ep0 = DK_ENTRIES(mp->ma_keys);
1041 numentries = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001042 if (_PyDict_HasSplitTable(mp)) {
Victor Stinner742da042016-09-07 17:40:12 -07001043 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001044 if ((value = mp->ma_values[i]) == NULL)
1045 continue;
1046 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
Victor Stinner742da042016-09-07 17:40:12 -07001047 assert(!_PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001048 return;
1049 }
1050 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001052 else {
Victor Stinner742da042016-09-07 17:40:12 -07001053 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001054 if ((value = ep0[i].me_value) == NULL)
1055 continue;
1056 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
1057 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
1058 return;
1059 }
1060 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001062}
1063
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001064/* Internal function to find slot for an item from its hash
Victor Stinner3c336c52016-09-12 14:17:40 +02001065 when it is known that the key is not present in the dict.
1066
1067 The dict must be combined. */
Victor Stinner99264802016-09-13 09:38:29 +02001068static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001069find_empty_slot(PyDictObject *mp, PyObject *key, Py_hash_t hash,
Victor Stinner742da042016-09-07 17:40:12 -07001070 PyObject ***value_addr, Py_ssize_t *hashpos)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001071{
INADA Naoki267941c2016-10-06 15:19:07 +09001072 size_t i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001073 size_t mask = DK_MASK(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07001074 Py_ssize_t ix;
1075 PyDictKeyEntry *ep, *ep0 = DK_ENTRIES(mp->ma_keys);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001076
Victor Stinner3c336c52016-09-12 14:17:40 +02001077 assert(!_PyDict_HasSplitTable(mp));
Victor Stinner742da042016-09-07 17:40:12 -07001078 assert(hashpos != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001079 assert(key != NULL);
Victor Stinner3c336c52016-09-12 14:17:40 +02001080
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001081 if (!PyUnicode_CheckExact(key))
1082 mp->ma_keys->dk_lookup = lookdict;
1083 i = hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -07001084 ix = dk_get_index(mp->ma_keys, i);
INADA Naoki267941c2016-10-06 15:19:07 +09001085 for (size_t perturb = hash; ix != DKIX_EMPTY;) {
1086 perturb >>= PERTURB_SHIFT;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001087 i = (i << 2) + i + perturb + 1;
Victor Stinner742da042016-09-07 17:40:12 -07001088 ix = dk_get_index(mp->ma_keys, i & mask);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 }
Victor Stinner742da042016-09-07 17:40:12 -07001090 ep = &ep0[mp->ma_keys->dk_nentries];
1091 *hashpos = i & mask;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001092 assert(ep->me_value == NULL);
Victor Stinner99264802016-09-13 09:38:29 +02001093 *value_addr = &ep->me_value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001094}
1095
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001096static int
1097insertion_resize(PyDictObject *mp)
1098{
Raymond Hettinger36f74aa2013-05-17 03:01:13 -07001099 return dictresize(mp, GROWTH_RATE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001100}
Antoine Pitroue965d972012-02-27 00:45:12 +01001101
1102/*
1103Internal routine to insert a new item into the table.
1104Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +01001105Returns -1 if an error occurred, or 0 on success.
1106*/
1107static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001108insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001109{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001110 PyObject *old_value;
1111 PyObject **value_addr;
Victor Stinner742da042016-09-07 17:40:12 -07001112 PyDictKeyEntry *ep, *ep0;
1113 Py_ssize_t hashpos, ix;
Antoine Pitroue965d972012-02-27 00:45:12 +01001114
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001115 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
1116 if (insertion_resize(mp) < 0)
1117 return -1;
1118 }
1119
Victor Stinner742da042016-09-07 17:40:12 -07001120 ix = mp->ma_keys->dk_lookup(mp, key, hash, &value_addr, &hashpos);
1121 if (ix == DKIX_ERROR) {
Antoine Pitroue965d972012-02-27 00:45:12 +01001122 return -1;
1123 }
Victor Stinner742da042016-09-07 17:40:12 -07001124
Antoine Pitroud6967322014-10-18 00:35:00 +02001125 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
Benjamin Petersona6f195e2012-04-30 10:23:40 -04001126 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001127 MAINTAIN_TRACKING(mp, key, value);
Victor Stinner742da042016-09-07 17:40:12 -07001128
1129 /* When insertion order is different from shared key, we can't share
1130 * the key anymore. Convert this instance to combine table.
1131 */
1132 if (_PyDict_HasSplitTable(mp) &&
1133 ((ix >= 0 && *value_addr == NULL && mp->ma_used != ix) ||
1134 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
1135 if (insertion_resize(mp) < 0) {
1136 Py_DECREF(value);
1137 return -1;
1138 }
1139 find_empty_slot(mp, key, hash, &value_addr, &hashpos);
1140 ix = DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001141 }
Victor Stinner742da042016-09-07 17:40:12 -07001142
1143 if (ix == DKIX_EMPTY) {
1144 /* Insert into new slot. */
1145 if (mp->ma_keys->dk_usable <= 0) {
1146 /* Need to resize. */
1147 if (insertion_resize(mp) < 0) {
1148 Py_DECREF(value);
1149 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001150 }
Victor Stinner742da042016-09-07 17:40:12 -07001151 find_empty_slot(mp, key, hash, &value_addr, &hashpos);
1152 }
1153 ep0 = DK_ENTRIES(mp->ma_keys);
1154 ep = &ep0[mp->ma_keys->dk_nentries];
1155 dk_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
1156 Py_INCREF(key);
1157 ep->me_key = key;
1158 ep->me_hash = hash;
1159 if (mp->ma_values) {
1160 assert (mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
1161 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001162 }
1163 else {
Victor Stinner742da042016-09-07 17:40:12 -07001164 ep->me_value = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001165 }
1166 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001167 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07001168 mp->ma_keys->dk_usable--;
1169 mp->ma_keys->dk_nentries++;
1170 assert(mp->ma_keys->dk_usable >= 0);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001171 assert(_PyDict_CheckConsistency(mp));
Victor Stinner742da042016-09-07 17:40:12 -07001172 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001173 }
Victor Stinner742da042016-09-07 17:40:12 -07001174
1175 assert(value_addr != NULL);
1176
1177 old_value = *value_addr;
1178 if (old_value != NULL) {
1179 *value_addr = value;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001180 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +02001181 assert(_PyDict_CheckConsistency(mp));
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001182
Victor Stinner742da042016-09-07 17:40:12 -07001183 Py_DECREF(old_value); /* which **CAN** re-enter (see issue #22653) */
1184 return 0;
1185 }
1186
1187 /* pending state */
1188 assert(_PyDict_HasSplitTable(mp));
1189 assert(ix == mp->ma_used);
1190 *value_addr = value;
1191 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001192 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +02001193 assert(_PyDict_CheckConsistency(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001194 return 0;
Antoine Pitroue965d972012-02-27 00:45:12 +01001195}
1196
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001197/*
Serhiy Storchaka7f0514a2016-10-31 20:14:05 +02001198Internal routine used by dictresize() to insert an item which is
1199known to be absent from the dict. This routine also assumes that
1200the dict contains no deleted entries. Besides the performance benefit,
1201using insertdict() in dictresize() is dangerous (SF bug #1456209).
1202Note that no refcounts are changed by this routine; if needed, the caller
1203is responsible for incref'ing `key` and `value`.
1204Neither mp->ma_used nor k->dk_usable are modified by this routine; the caller
1205must set them correctly
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001206*/
1207static void
Serhiy Storchaka7f0514a2016-10-31 20:14:05 +02001208insertdict_clean(PyDictObject *mp, PyObject *key, Py_hash_t hash,
1209 PyObject *value)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001210{
Serhiy Storchaka7f0514a2016-10-31 20:14:05 +02001211 size_t i;
1212 PyDictKeysObject *k = mp->ma_keys;
1213 size_t mask = (size_t)DK_SIZE(k)-1;
1214 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
1215 PyDictKeyEntry *ep;
1216
1217 assert(k->dk_lookup != NULL);
1218 assert(value != NULL);
1219 assert(key != NULL);
1220 assert(PyUnicode_CheckExact(key) || k->dk_lookup == lookdict);
1221 i = hash & mask;
1222 for (size_t perturb = hash; dk_get_index(k, i) != DKIX_EMPTY;) {
1223 perturb >>= PERTURB_SHIFT;
1224 i = mask & ((i << 2) + i + perturb + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 }
Serhiy Storchaka7f0514a2016-10-31 20:14:05 +02001226 ep = &ep0[k->dk_nentries];
1227 assert(ep->me_value == NULL);
1228 dk_set_index(k, i, k->dk_nentries);
1229 k->dk_nentries++;
1230 ep->me_key = key;
1231 ep->me_hash = hash;
1232 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001233}
1234
1235/*
1236Restructure the table by allocating a new table and reinserting all
1237items again. When entries have been deleted, the new table may
1238actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001239If a table is split (its keys and hashes are shared, its values are not),
1240then the values are temporarily copied into the table, it is resized as
1241a combined table, then the me_value slots in the old table are NULLed out.
1242After resizing a table is always combined,
1243but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001244*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001245static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001246dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001247{
Serhiy Storchaka7f0514a2016-10-31 20:14:05 +02001248 Py_ssize_t i, newsize;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001249 PyDictKeysObject *oldkeys;
1250 PyObject **oldvalues;
Serhiy Storchaka7f0514a2016-10-31 20:14:05 +02001251 PyDictKeyEntry *ep0;
Tim Peters91a364d2001-05-19 07:04:38 +00001252
Victor Stinner742da042016-09-07 17:40:12 -07001253 /* Find the smallest table size > minused. */
1254 for (newsize = PyDict_MINSIZE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 newsize <= minused && newsize > 0;
1256 newsize <<= 1)
1257 ;
1258 if (newsize <= 0) {
1259 PyErr_NoMemory();
1260 return -1;
1261 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001262 oldkeys = mp->ma_keys;
Serhiy Storchaka7f0514a2016-10-31 20:14:05 +02001263 oldvalues = mp->ma_values;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001264 /* Allocate a new table. */
1265 mp->ma_keys = new_keys_object(newsize);
1266 if (mp->ma_keys == NULL) {
1267 mp->ma_keys = oldkeys;
1268 return -1;
1269 }
1270 if (oldkeys->dk_lookup == lookdict)
1271 mp->ma_keys->dk_lookup = lookdict;
Serhiy Storchaka7f0514a2016-10-31 20:14:05 +02001272 mp->ma_values = NULL;
1273 ep0 = DK_ENTRIES(oldkeys);
1274 /* Main loop below assumes we can transfer refcount to new keys
1275 * and that value is stored in me_value.
1276 * Increment ref-counts and copy values here to compensate
1277 * This (resizing a split table) should be relatively rare */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001278 if (oldvalues != NULL) {
Serhiy Storchaka7f0514a2016-10-31 20:14:05 +02001279 for (i = 0; i < oldkeys->dk_nentries; i++) {
1280 if (oldvalues[i] != NULL) {
1281 Py_INCREF(ep0[i].me_key);
1282 ep0[i].me_value = oldvalues[i];
1283 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 }
Serhiy Storchaka7f0514a2016-10-31 20:14:05 +02001285 }
1286 /* Main loop */
1287 for (i = 0; i < oldkeys->dk_nentries; i++) {
1288 PyDictKeyEntry *ep = &ep0[i];
1289 if (ep->me_value != NULL) {
1290 insertdict_clean(mp, ep->me_key, ep->me_hash, ep->me_value);
1291 }
1292 }
1293 mp->ma_keys->dk_usable -= mp->ma_used;
1294 if (oldvalues != NULL) {
1295 /* NULL out me_value slot in oldkeys, in case it was shared */
1296 for (i = 0; i < oldkeys->dk_nentries; i++)
1297 ep0[i].me_value = NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001298 DK_DECREF(oldkeys);
Victor Stinner742da042016-09-07 17:40:12 -07001299 if (oldvalues != empty_values) {
1300 free_values(oldvalues);
1301 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001302 }
Serhiy Storchaka7f0514a2016-10-31 20:14:05 +02001303 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001304 assert(oldkeys->dk_lookup != lookdict_split);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001305 assert(oldkeys->dk_refcnt == 1);
Serhiy Storchaka7f0514a2016-10-31 20:14:05 +02001306 DK_DEBUG_DECREF PyObject_FREE(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001307 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001309}
1310
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001311/* Returns NULL if unable to split table.
1312 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001313static PyDictKeysObject *
1314make_keys_shared(PyObject *op)
1315{
1316 Py_ssize_t i;
1317 Py_ssize_t size;
1318 PyDictObject *mp = (PyDictObject *)op;
1319
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001320 if (!PyDict_CheckExact(op))
1321 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001322 if (!_PyDict_HasSplitTable(mp)) {
1323 PyDictKeyEntry *ep0;
1324 PyObject **values;
1325 assert(mp->ma_keys->dk_refcnt == 1);
1326 if (mp->ma_keys->dk_lookup == lookdict) {
1327 return NULL;
1328 }
1329 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
1330 /* Remove dummy keys */
1331 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
1332 return NULL;
1333 }
1334 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
1335 /* Copy values into a new array */
Victor Stinner742da042016-09-07 17:40:12 -07001336 ep0 = DK_ENTRIES(mp->ma_keys);
1337 size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001338 values = new_values(size);
1339 if (values == NULL) {
1340 PyErr_SetString(PyExc_MemoryError,
1341 "Not enough memory to allocate new values array");
1342 return NULL;
1343 }
1344 for (i = 0; i < size; i++) {
1345 values[i] = ep0[i].me_value;
1346 ep0[i].me_value = NULL;
1347 }
1348 mp->ma_keys->dk_lookup = lookdict_split;
1349 mp->ma_values = values;
1350 }
1351 DK_INCREF(mp->ma_keys);
1352 return mp->ma_keys;
1353}
Christian Heimes99170a52007-12-19 02:07:34 +00001354
1355PyObject *
1356_PyDict_NewPresized(Py_ssize_t minused)
1357{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001358 Py_ssize_t newsize;
1359 PyDictKeysObject *new_keys;
Victor Stinner742da042016-09-07 17:40:12 -07001360 for (newsize = PyDict_MINSIZE;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001361 newsize <= minused && newsize > 0;
1362 newsize <<= 1)
1363 ;
1364 new_keys = new_keys_object(newsize);
1365 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001367 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001368}
1369
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001370/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1371 * that may occur (originally dicts supported only string keys, and exceptions
1372 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001373 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001374 * (suppressed) occurred while computing the key's hash, or that some error
1375 * (suppressed) occurred when comparing keys in the dict's internal probe
1376 * sequence. A nasty example of the latter is when a Python-coded comparison
1377 * function hits a stack-depth error, which can cause this to return NULL
1378 * even if the key is present.
1379 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001380PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001381PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001382{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001383 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07001384 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 PyDictObject *mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 PyThreadState *tstate;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001387 PyObject **value_addr;
1388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 if (!PyDict_Check(op))
1390 return NULL;
1391 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001392 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 {
1394 hash = PyObject_Hash(key);
1395 if (hash == -1) {
1396 PyErr_Clear();
1397 return NULL;
1398 }
1399 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 /* We can arrive here with a NULL tstate during initialization: try
1402 running "python -Wi" for an example related to string interning.
1403 Let's just hope that no exception occurs then... This must be
1404 _PyThreadState_Current and not PyThreadState_GET() because in debug
1405 mode, the latter complains if tstate is NULL. */
Victor Stinnerbfd316e2016-01-20 11:12:38 +01001406 tstate = _PyThreadState_UncheckedGet();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 if (tstate != NULL && tstate->curexc_type != NULL) {
1408 /* preserve the existing exception */
1409 PyObject *err_type, *err_value, *err_tb;
1410 PyErr_Fetch(&err_type, &err_value, &err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001411 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 /* ignore errors */
1413 PyErr_Restore(err_type, err_value, err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001414 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 return NULL;
1416 }
1417 else {
Victor Stinner742da042016-09-07 17:40:12 -07001418 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
1419 if (ix < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 PyErr_Clear();
1421 return NULL;
1422 }
1423 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001424 return *value_addr;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001425}
1426
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001427PyObject *
1428_PyDict_GetItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1429{
Victor Stinner742da042016-09-07 17:40:12 -07001430 Py_ssize_t ix;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001431 PyDictObject *mp = (PyDictObject *)op;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001432 PyThreadState *tstate;
1433 PyObject **value_addr;
1434
1435 if (!PyDict_Check(op))
1436 return NULL;
1437
1438 /* We can arrive here with a NULL tstate during initialization: try
1439 running "python -Wi" for an example related to string interning.
1440 Let's just hope that no exception occurs then... This must be
1441 _PyThreadState_Current and not PyThreadState_GET() because in debug
1442 mode, the latter complains if tstate is NULL. */
Victor Stinnerbfd316e2016-01-20 11:12:38 +01001443 tstate = _PyThreadState_UncheckedGet();
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001444 if (tstate != NULL && tstate->curexc_type != NULL) {
1445 /* preserve the existing exception */
1446 PyObject *err_type, *err_value, *err_tb;
1447 PyErr_Fetch(&err_type, &err_value, &err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001448 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001449 /* ignore errors */
1450 PyErr_Restore(err_type, err_value, err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001451 if (ix == DKIX_EMPTY)
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001452 return NULL;
1453 }
1454 else {
Victor Stinner742da042016-09-07 17:40:12 -07001455 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
1456 if (ix == DKIX_EMPTY) {
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001457 PyErr_Clear();
1458 return NULL;
1459 }
1460 }
1461 return *value_addr;
1462}
1463
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001464/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1465 This returns NULL *with* an exception set if an exception occurred.
1466 It returns NULL *without* an exception set if the key wasn't present.
1467*/
1468PyObject *
1469PyDict_GetItemWithError(PyObject *op, PyObject *key)
1470{
Victor Stinner742da042016-09-07 17:40:12 -07001471 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001472 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 PyDictObject*mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001474 PyObject **value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 if (!PyDict_Check(op)) {
1477 PyErr_BadInternalCall();
1478 return NULL;
1479 }
1480 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001481 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 {
1483 hash = PyObject_Hash(key);
1484 if (hash == -1) {
1485 return NULL;
1486 }
1487 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001488
Victor Stinner742da042016-09-07 17:40:12 -07001489 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
1490 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001491 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001492 return *value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001493}
1494
Brett Cannonfd074152012-04-14 14:10:13 -04001495PyObject *
1496_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1497{
1498 PyObject *kv;
1499 kv = _PyUnicode_FromId(key); /* borrowed */
1500 if (kv == NULL)
1501 return NULL;
1502 return PyDict_GetItemWithError(dp, kv);
1503}
1504
Victor Stinnerb4efc962015-11-20 09:24:02 +01001505/* Fast version of global value lookup (LOAD_GLOBAL).
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001506 * Lookup in globals, then builtins.
Victor Stinnerb4efc962015-11-20 09:24:02 +01001507 *
1508 * Raise an exception and return NULL if an error occurred (ex: computing the
1509 * key hash failed, key comparison failed, ...). Return NULL if the key doesn't
1510 * exist. Return the value if the key exists.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001511 */
1512PyObject *
1513_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001514{
Victor Stinner742da042016-09-07 17:40:12 -07001515 Py_ssize_t ix;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001516 Py_hash_t hash;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001517 PyObject **value_addr;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001518
1519 if (!PyUnicode_CheckExact(key) ||
1520 (hash = ((PyASCIIObject *) key)->hash) == -1)
1521 {
1522 hash = PyObject_Hash(key);
1523 if (hash == -1)
1524 return NULL;
Antoine Pitroue965d972012-02-27 00:45:12 +01001525 }
Victor Stinnerb4efc962015-11-20 09:24:02 +01001526
1527 /* namespace 1: globals */
Victor Stinner742da042016-09-07 17:40:12 -07001528 ix = globals->ma_keys->dk_lookup(globals, key, hash, &value_addr, NULL);
1529 if (ix == DKIX_ERROR)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001530 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001531 if (ix != DKIX_EMPTY && *value_addr != NULL)
1532 return *value_addr;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001533
1534 /* namespace 2: builtins */
Victor Stinner742da042016-09-07 17:40:12 -07001535 ix = builtins->ma_keys->dk_lookup(builtins, key, hash, &value_addr, NULL);
1536 if (ix < 0)
Victor Stinnerb4efc962015-11-20 09:24:02 +01001537 return NULL;
1538 return *value_addr;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001539}
1540
Antoine Pitroue965d972012-02-27 00:45:12 +01001541/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1542 * dictionary if it's merely replacing the value for an existing key.
1543 * This means that it's safe to loop over a dictionary with PyDict_Next()
1544 * and occasionally replace a value -- but you can't insert new keys or
1545 * remove them.
1546 */
1547int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001548PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001549{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001550 PyDictObject *mp;
1551 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001552 if (!PyDict_Check(op)) {
1553 PyErr_BadInternalCall();
1554 return -1;
1555 }
1556 assert(key);
1557 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001558 mp = (PyDictObject *)op;
1559 if (!PyUnicode_CheckExact(key) ||
1560 (hash = ((PyASCIIObject *) key)->hash) == -1)
1561 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001562 hash = PyObject_Hash(key);
1563 if (hash == -1)
1564 return -1;
1565 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001566
1567 /* insertdict() handles any resizing that might be necessary */
1568 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001569}
1570
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001571int
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001572_PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
1573 Py_hash_t hash)
1574{
1575 PyDictObject *mp;
1576
1577 if (!PyDict_Check(op)) {
1578 PyErr_BadInternalCall();
1579 return -1;
1580 }
1581 assert(key);
1582 assert(value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001583 assert(hash != -1);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001584 mp = (PyDictObject *)op;
1585
1586 /* insertdict() handles any resizing that might be necessary */
1587 return insertdict(mp, key, hash, value);
1588}
1589
1590int
Tim Peters1f5871e2000-07-04 17:44:48 +00001591PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001592{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001593 Py_hash_t hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +00001594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 assert(key);
1596 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001597 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 hash = PyObject_Hash(key);
1599 if (hash == -1)
1600 return -1;
1601 }
Victor Stinner742da042016-09-07 17:40:12 -07001602
1603 return _PyDict_DelItem_KnownHash(op, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001604}
1605
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001606int
1607_PyDict_DelItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1608{
Victor Stinner742da042016-09-07 17:40:12 -07001609 Py_ssize_t hashpos, ix;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001610 PyDictObject *mp;
1611 PyDictKeyEntry *ep;
1612 PyObject *old_key, *old_value;
1613 PyObject **value_addr;
1614
1615 if (!PyDict_Check(op)) {
1616 PyErr_BadInternalCall();
1617 return -1;
1618 }
1619 assert(key);
1620 assert(hash != -1);
1621 mp = (PyDictObject *)op;
Victor Stinner742da042016-09-07 17:40:12 -07001622 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, &hashpos);
1623 if (ix == DKIX_ERROR)
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001624 return -1;
Victor Stinner742da042016-09-07 17:40:12 -07001625 if (ix == DKIX_EMPTY || *value_addr == NULL) {
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001626 _PyErr_SetKeyError(key);
1627 return -1;
1628 }
Victor Stinner742da042016-09-07 17:40:12 -07001629 assert(dk_get_index(mp->ma_keys, hashpos) == ix);
Victor Stinner78601a32016-09-09 19:28:36 -07001630
1631 // Split table doesn't allow deletion. Combine it.
1632 if (_PyDict_HasSplitTable(mp)) {
1633 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1634 return -1;
1635 }
1636 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, &hashpos);
1637 assert(ix >= 0);
1638 }
1639
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001640 old_value = *value_addr;
Victor Stinner78601a32016-09-09 19:28:36 -07001641 assert(old_value != NULL);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001642 *value_addr = NULL;
1643 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001644 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner78601a32016-09-09 19:28:36 -07001645 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1646 dk_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
1647 ENSURE_ALLOWS_DELETIONS(mp);
1648 old_key = ep->me_key;
1649 ep->me_key = NULL;
1650 Py_DECREF(old_key);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001651 Py_DECREF(old_value);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001652
1653 assert(_PyDict_CheckConsistency(mp));
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001654 return 0;
1655}
1656
Guido van Rossum25831651993-05-19 14:50:45 +00001657void
Tim Peters1f5871e2000-07-04 17:44:48 +00001658PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001659{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001661 PyDictKeysObject *oldkeys;
1662 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 if (!PyDict_Check(op))
1666 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001667 mp = ((PyDictObject *)op);
1668 oldkeys = mp->ma_keys;
1669 oldvalues = mp->ma_values;
1670 if (oldvalues == empty_values)
1671 return;
1672 /* Empty the dict... */
1673 DK_INCREF(Py_EMPTY_KEYS);
1674 mp->ma_keys = Py_EMPTY_KEYS;
1675 mp->ma_values = empty_values;
1676 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001677 mp->ma_version_tag = DICT_NEXT_VERSION();
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001678 /* ...then clear the keys and values */
1679 if (oldvalues != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001680 n = oldkeys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001681 for (i = 0; i < n; i++)
1682 Py_CLEAR(oldvalues[i]);
1683 free_values(oldvalues);
1684 DK_DECREF(oldkeys);
1685 }
1686 else {
1687 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001688 DK_DECREF(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001689 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02001690 assert(_PyDict_CheckConsistency(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001691}
1692
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001693/* Internal version of PyDict_Next that returns a hash value in addition
1694 * to the key and value.
1695 * Return 1 on success, return 0 when the reached the end of the dictionary
1696 * (or if op is not a dictionary)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001697 */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001698int
1699_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1700 PyObject **pvalue, Py_hash_t *phash)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001701{
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001702 Py_ssize_t i, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001703 PyDictObject *mp;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001704 PyDictKeyEntry *entry_ptr;
1705 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001706
1707 if (!PyDict_Check(op))
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001708 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709 mp = (PyDictObject *)op;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001710 i = *ppos;
Victor Stinner742da042016-09-07 17:40:12 -07001711 n = mp->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001712 if ((size_t)i >= (size_t)n)
1713 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001714 if (mp->ma_values) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001715 PyObject **value_ptr = &mp->ma_values[i];
1716 while (i < n && *value_ptr == NULL) {
1717 value_ptr++;
1718 i++;
Victor Stinner742da042016-09-07 17:40:12 -07001719 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001720 if (i >= n)
1721 return 0;
1722 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
1723 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001725 else {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001726 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
1727 while (i < n && entry_ptr->me_value == NULL) {
1728 entry_ptr++;
1729 i++;
Victor Stinner742da042016-09-07 17:40:12 -07001730 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001731 if (i >= n)
1732 return 0;
1733 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001734 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001735 *ppos = i+1;
1736 if (pkey)
1737 *pkey = entry_ptr->me_key;
1738 if (phash)
1739 *phash = entry_ptr->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001740 if (pvalue)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001741 *pvalue = value;
1742 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001743}
1744
Tim Peters080c88b2003-02-15 03:01:11 +00001745/*
1746 * Iterate over a dict. Use like so:
1747 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001748 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001749 * PyObject *key, *value;
1750 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001751 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001752 * Refer to borrowed references in key and value.
Tim Peters080c88b2003-02-15 03:01:11 +00001753 * }
1754 *
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001755 * Return 1 on success, return 0 when the reached the end of the dictionary
1756 * (or if op is not a dictionary)
1757 *
Tim Peters080c88b2003-02-15 03:01:11 +00001758 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001759 * mutates the dict. One exception: it is safe if the loop merely changes
1760 * the values associated with the keys (but doesn't insert new keys or
1761 * delete keys), via PyDict_SetItem().
1762 */
Guido van Rossum25831651993-05-19 14:50:45 +00001763int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001764PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001765{
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001766 return _PyDict_Next(op, ppos, pkey, pvalue, NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001767}
1768
Eric Snow96c6af92015-05-29 22:21:39 -06001769/* Internal version of dict.pop(). */
1770PyObject *
Yury Selivanov684ef2c2016-10-28 19:01:21 -04001771_PyDict_Pop(PyObject *dict, PyObject *key, PyObject *deflt)
Eric Snow96c6af92015-05-29 22:21:39 -06001772{
1773 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07001774 Py_ssize_t ix, hashpos;
Eric Snow96c6af92015-05-29 22:21:39 -06001775 PyObject *old_value, *old_key;
1776 PyDictKeyEntry *ep;
1777 PyObject **value_addr;
Yury Selivanov684ef2c2016-10-28 19:01:21 -04001778 PyDictObject *mp;
1779
1780 assert(PyDict_Check(dict));
1781 mp = (PyDictObject *)dict;
Eric Snow96c6af92015-05-29 22:21:39 -06001782
1783 if (mp->ma_used == 0) {
1784 if (deflt) {
1785 Py_INCREF(deflt);
1786 return deflt;
1787 }
1788 _PyErr_SetKeyError(key);
1789 return NULL;
1790 }
1791 if (!PyUnicode_CheckExact(key) ||
1792 (hash = ((PyASCIIObject *) key)->hash) == -1) {
1793 hash = PyObject_Hash(key);
1794 if (hash == -1)
1795 return NULL;
1796 }
Victor Stinner742da042016-09-07 17:40:12 -07001797 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, &hashpos);
1798 if (ix == DKIX_ERROR)
Eric Snow96c6af92015-05-29 22:21:39 -06001799 return NULL;
Victor Stinnerd0ad11f2016-09-13 16:56:38 +02001800 if (ix == DKIX_EMPTY || *value_addr == NULL) {
Eric Snow96c6af92015-05-29 22:21:39 -06001801 if (deflt) {
1802 Py_INCREF(deflt);
1803 return deflt;
1804 }
1805 _PyErr_SetKeyError(key);
1806 return NULL;
1807 }
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001808
Victor Stinner78601a32016-09-09 19:28:36 -07001809 // Split table doesn't allow deletion. Combine it.
1810 if (_PyDict_HasSplitTable(mp)) {
1811 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1812 return NULL;
1813 }
1814 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, &hashpos);
1815 assert(ix >= 0);
1816 }
1817
Victor Stinner742da042016-09-07 17:40:12 -07001818 old_value = *value_addr;
Victor Stinner78601a32016-09-09 19:28:36 -07001819 assert(old_value != NULL);
Eric Snow96c6af92015-05-29 22:21:39 -06001820 *value_addr = NULL;
1821 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001822 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner78601a32016-09-09 19:28:36 -07001823 dk_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
1824 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1825 ENSURE_ALLOWS_DELETIONS(mp);
1826 old_key = ep->me_key;
1827 ep->me_key = NULL;
1828 Py_DECREF(old_key);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001829
1830 assert(_PyDict_CheckConsistency(mp));
Eric Snow96c6af92015-05-29 22:21:39 -06001831 return old_value;
1832}
1833
1834/* Internal version of dict.from_keys(). It is subclass-friendly. */
1835PyObject *
1836_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
1837{
1838 PyObject *it; /* iter(iterable) */
1839 PyObject *key;
1840 PyObject *d;
1841 int status;
1842
1843 d = PyObject_CallObject(cls, NULL);
1844 if (d == NULL)
1845 return NULL;
1846
1847 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
1848 if (PyDict_CheckExact(iterable)) {
1849 PyDictObject *mp = (PyDictObject *)d;
1850 PyObject *oldvalue;
1851 Py_ssize_t pos = 0;
1852 PyObject *key;
1853 Py_hash_t hash;
1854
Victor Stinner742da042016-09-07 17:40:12 -07001855 if (dictresize(mp, ESTIMATE_SIZE(Py_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001856 Py_DECREF(d);
1857 return NULL;
1858 }
1859
1860 while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
1861 if (insertdict(mp, key, hash, value)) {
1862 Py_DECREF(d);
1863 return NULL;
1864 }
1865 }
1866 return d;
1867 }
1868 if (PyAnySet_CheckExact(iterable)) {
1869 PyDictObject *mp = (PyDictObject *)d;
1870 Py_ssize_t pos = 0;
1871 PyObject *key;
1872 Py_hash_t hash;
1873
Victor Stinner742da042016-09-07 17:40:12 -07001874 if (dictresize(mp, ESTIMATE_SIZE(PySet_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001875 Py_DECREF(d);
1876 return NULL;
1877 }
1878
1879 while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
1880 if (insertdict(mp, key, hash, value)) {
1881 Py_DECREF(d);
1882 return NULL;
1883 }
1884 }
1885 return d;
1886 }
1887 }
1888
1889 it = PyObject_GetIter(iterable);
1890 if (it == NULL){
1891 Py_DECREF(d);
1892 return NULL;
1893 }
1894
1895 if (PyDict_CheckExact(d)) {
1896 while ((key = PyIter_Next(it)) != NULL) {
1897 status = PyDict_SetItem(d, key, value);
1898 Py_DECREF(key);
1899 if (status < 0)
1900 goto Fail;
1901 }
1902 } else {
1903 while ((key = PyIter_Next(it)) != NULL) {
1904 status = PyObject_SetItem(d, key, value);
1905 Py_DECREF(key);
1906 if (status < 0)
1907 goto Fail;
1908 }
1909 }
1910
1911 if (PyErr_Occurred())
1912 goto Fail;
1913 Py_DECREF(it);
1914 return d;
1915
1916Fail:
1917 Py_DECREF(it);
1918 Py_DECREF(d);
1919 return NULL;
1920}
1921
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001922/* Methods */
1923
1924static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001925dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001926{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001927 PyObject **values = mp->ma_values;
1928 PyDictKeysObject *keys = mp->ma_keys;
1929 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 PyObject_GC_UnTrack(mp);
1931 Py_TRASHCAN_SAFE_BEGIN(mp)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001932 if (values != NULL) {
1933 if (values != empty_values) {
Victor Stinner742da042016-09-07 17:40:12 -07001934 for (i = 0, n = mp->ma_keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001935 Py_XDECREF(values[i]);
1936 }
1937 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001939 DK_DECREF(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02001941 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001942 assert(keys->dk_refcnt == 1);
1943 DK_DECREF(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001944 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001945 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1946 free_list[numfree++] = mp;
1947 else
1948 Py_TYPE(mp)->tp_free((PyObject *)mp);
1949 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001950}
1951
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001952
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001953static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001954dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001955{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001957 PyObject *key = NULL, *value = NULL;
1958 _PyUnicodeWriter writer;
1959 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00001960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 i = Py_ReprEnter((PyObject *)mp);
1962 if (i != 0) {
1963 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
1964 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001966 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001967 Py_ReprLeave((PyObject *)mp);
1968 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001969 }
Tim Petersa7259592001-06-16 05:11:17 +00001970
Victor Stinnerf91929b2013-11-19 13:07:38 +01001971 _PyUnicodeWriter_Init(&writer);
1972 writer.overallocate = 1;
1973 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
1974 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00001975
Victor Stinnerf91929b2013-11-19 13:07:38 +01001976 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
1977 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001979 /* Do repr() on each key+value pair, and insert ": " between them.
1980 Note that repr may mutate the dict. */
1981 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001982 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001983 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001984 PyObject *s;
1985 int res;
1986
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001987 /* Prevent repr from deleting key or value during key format. */
1988 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02001990
Victor Stinnerf91929b2013-11-19 13:07:38 +01001991 if (!first) {
1992 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
1993 goto error;
1994 }
1995 first = 0;
1996
1997 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001998 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01001999 goto error;
2000 res = _PyUnicodeWriter_WriteStr(&writer, s);
2001 Py_DECREF(s);
2002 if (res < 0)
2003 goto error;
2004
2005 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
2006 goto error;
2007
2008 s = PyObject_Repr(value);
2009 if (s == NULL)
2010 goto error;
2011 res = _PyUnicodeWriter_WriteStr(&writer, s);
2012 Py_DECREF(s);
2013 if (res < 0)
2014 goto error;
2015
2016 Py_CLEAR(key);
2017 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002018 }
Tim Petersa7259592001-06-16 05:11:17 +00002019
Victor Stinnerf91929b2013-11-19 13:07:38 +01002020 writer.overallocate = 0;
2021 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
2022 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00002023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002024 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01002025
2026 return _PyUnicodeWriter_Finish(&writer);
2027
2028error:
2029 Py_ReprLeave((PyObject *)mp);
2030 _PyUnicodeWriter_Dealloc(&writer);
2031 Py_XDECREF(key);
2032 Py_XDECREF(value);
2033 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002034}
2035
Martin v. Löwis18e16552006-02-15 17:27:45 +00002036static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002037dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002038{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002039 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002040}
2041
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002042static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002043dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002044{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002045 PyObject *v;
Victor Stinner742da042016-09-07 17:40:12 -07002046 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002047 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002048 PyObject **value_addr;
2049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002050 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002051 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002052 hash = PyObject_Hash(key);
2053 if (hash == -1)
2054 return NULL;
2055 }
Victor Stinner742da042016-09-07 17:40:12 -07002056 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
2057 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002058 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002059 if (ix == DKIX_EMPTY || *value_addr == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002060 if (!PyDict_CheckExact(mp)) {
2061 /* Look up __missing__ method if we're a subclass. */
2062 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05002063 _Py_IDENTIFIER(__missing__);
2064 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002065 if (missing != NULL) {
2066 res = PyObject_CallFunctionObjArgs(missing,
2067 key, NULL);
2068 Py_DECREF(missing);
2069 return res;
2070 }
2071 else if (PyErr_Occurred())
2072 return NULL;
2073 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07002074 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002075 return NULL;
2076 }
Victor Stinner742da042016-09-07 17:40:12 -07002077 v = *value_addr;
2078 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002079 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002080}
2081
2082static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002083dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002084{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002085 if (w == NULL)
2086 return PyDict_DelItem((PyObject *)mp, v);
2087 else
2088 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002089}
2090
Guido van Rossuma9e7a811997-05-13 21:02:11 +00002091static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002092 (lenfunc)dict_length, /*mp_length*/
2093 (binaryfunc)dict_subscript, /*mp_subscript*/
2094 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002095};
2096
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002097static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002098dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002099{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002100 PyObject *v;
2101 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002102 PyDictKeyEntry *ep;
2103 Py_ssize_t size, n, offset;
2104 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002105
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002106 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002107 n = mp->ma_used;
2108 v = PyList_New(n);
2109 if (v == NULL)
2110 return NULL;
2111 if (n != mp->ma_used) {
2112 /* Durnit. The allocations caused the dict to resize.
2113 * Just start over, this shouldn't normally happen.
2114 */
2115 Py_DECREF(v);
2116 goto again;
2117 }
Victor Stinner742da042016-09-07 17:40:12 -07002118 ep = DK_ENTRIES(mp->ma_keys);
2119 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002120 if (mp->ma_values) {
2121 value_ptr = mp->ma_values;
2122 offset = sizeof(PyObject *);
2123 }
2124 else {
2125 value_ptr = &ep[0].me_value;
2126 offset = sizeof(PyDictKeyEntry);
2127 }
2128 for (i = 0, j = 0; i < size; i++) {
2129 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002130 PyObject *key = ep[i].me_key;
2131 Py_INCREF(key);
2132 PyList_SET_ITEM(v, j, key);
2133 j++;
2134 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002135 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002136 }
2137 assert(j == n);
2138 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002139}
2140
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002141static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002142dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002143{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002144 PyObject *v;
2145 Py_ssize_t i, j;
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002146 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002147 Py_ssize_t size, n, offset;
2148 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002149
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002150 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002151 n = mp->ma_used;
2152 v = PyList_New(n);
2153 if (v == NULL)
2154 return NULL;
2155 if (n != mp->ma_used) {
2156 /* Durnit. The allocations caused the dict to resize.
2157 * Just start over, this shouldn't normally happen.
2158 */
2159 Py_DECREF(v);
2160 goto again;
2161 }
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002162 ep = DK_ENTRIES(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002163 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002164 if (mp->ma_values) {
2165 value_ptr = mp->ma_values;
2166 offset = sizeof(PyObject *);
2167 }
2168 else {
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002169 value_ptr = &ep[0].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002170 offset = sizeof(PyDictKeyEntry);
2171 }
2172 for (i = 0, j = 0; i < size; i++) {
2173 PyObject *value = *value_ptr;
2174 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2175 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002176 Py_INCREF(value);
2177 PyList_SET_ITEM(v, j, value);
2178 j++;
2179 }
2180 }
2181 assert(j == n);
2182 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002183}
2184
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002185static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002186dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002187{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002188 PyObject *v;
2189 Py_ssize_t i, j, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002190 Py_ssize_t size, offset;
2191 PyObject *item, *key;
2192 PyDictKeyEntry *ep;
2193 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002195 /* Preallocate the list of tuples, to avoid allocations during
2196 * the loop over the items, which could trigger GC, which
2197 * could resize the dict. :-(
2198 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002199 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002200 n = mp->ma_used;
2201 v = PyList_New(n);
2202 if (v == NULL)
2203 return NULL;
2204 for (i = 0; i < n; i++) {
2205 item = PyTuple_New(2);
2206 if (item == NULL) {
2207 Py_DECREF(v);
2208 return NULL;
2209 }
2210 PyList_SET_ITEM(v, i, item);
2211 }
2212 if (n != mp->ma_used) {
2213 /* Durnit. The allocations caused the dict to resize.
2214 * Just start over, this shouldn't normally happen.
2215 */
2216 Py_DECREF(v);
2217 goto again;
2218 }
2219 /* Nothing we do below makes any function calls. */
Victor Stinner742da042016-09-07 17:40:12 -07002220 ep = DK_ENTRIES(mp->ma_keys);
2221 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002222 if (mp->ma_values) {
2223 value_ptr = mp->ma_values;
2224 offset = sizeof(PyObject *);
2225 }
2226 else {
2227 value_ptr = &ep[0].me_value;
2228 offset = sizeof(PyDictKeyEntry);
2229 }
2230 for (i = 0, j = 0; i < size; i++) {
2231 PyObject *value = *value_ptr;
2232 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2233 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002234 key = ep[i].me_key;
2235 item = PyList_GET_ITEM(v, j);
2236 Py_INCREF(key);
2237 PyTuple_SET_ITEM(item, 0, key);
2238 Py_INCREF(value);
2239 PyTuple_SET_ITEM(item, 1, value);
2240 j++;
2241 }
2242 }
2243 assert(j == n);
2244 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002245}
2246
Larry Hastings5c661892014-01-24 06:17:25 -08002247/*[clinic input]
2248@classmethod
2249dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08002250 iterable: object
2251 value: object=None
2252 /
2253
2254Returns a new dict with keys from iterable and values equal to value.
2255[clinic start generated code]*/
2256
Larry Hastings5c661892014-01-24 06:17:25 -08002257static PyObject *
2258dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002259/*[clinic end generated code: output=8fb98e4b10384999 input=b85a667f9bf4669d]*/
Larry Hastings5c661892014-01-24 06:17:25 -08002260{
Eric Snow96c6af92015-05-29 22:21:39 -06002261 return _PyDict_FromKeys((PyObject *)type, iterable, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002262}
2263
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002264static int
Victor Stinner742da042016-09-07 17:40:12 -07002265dict_update_common(PyObject *self, PyObject *args, PyObject *kwds,
2266 const char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002267{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002268 PyObject *arg = NULL;
2269 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002271 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
2272 result = -1;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002274 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002275 _Py_IDENTIFIER(keys);
2276 if (_PyObject_HasAttrId(arg, &PyId_keys))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002277 result = PyDict_Merge(self, arg, 1);
2278 else
2279 result = PyDict_MergeFromSeq2(self, arg, 1);
2280 }
2281 if (result == 0 && kwds != NULL) {
2282 if (PyArg_ValidateKeywordArguments(kwds))
2283 result = PyDict_Merge(self, kwds, 1);
2284 else
2285 result = -1;
2286 }
2287 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002288}
2289
2290static PyObject *
2291dict_update(PyObject *self, PyObject *args, PyObject *kwds)
2292{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002293 if (dict_update_common(self, args, kwds, "update") != -1)
2294 Py_RETURN_NONE;
2295 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002296}
2297
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002298/* Update unconditionally replaces existing items.
2299 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00002300 otherwise it leaves existing items unchanged.
2301
2302 PyDict_{Update,Merge} update/merge from a mapping object.
2303
Tim Petersf582b822001-12-11 18:51:08 +00002304 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00002305 producing iterable objects of length 2.
2306*/
2307
Tim Petersf582b822001-12-11 18:51:08 +00002308int
Tim Peters1fc240e2001-10-26 05:06:50 +00002309PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
2310{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002311 PyObject *it; /* iter(seq2) */
2312 Py_ssize_t i; /* index into seq2 of current element */
2313 PyObject *item; /* seq2[i] */
2314 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00002315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002316 assert(d != NULL);
2317 assert(PyDict_Check(d));
2318 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00002319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002320 it = PyObject_GetIter(seq2);
2321 if (it == NULL)
2322 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002324 for (i = 0; ; ++i) {
2325 PyObject *key, *value;
2326 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00002327
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002328 fast = NULL;
2329 item = PyIter_Next(it);
2330 if (item == NULL) {
2331 if (PyErr_Occurred())
2332 goto Fail;
2333 break;
2334 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002336 /* Convert item to sequence, and verify length 2. */
2337 fast = PySequence_Fast(item, "");
2338 if (fast == NULL) {
2339 if (PyErr_ExceptionMatches(PyExc_TypeError))
2340 PyErr_Format(PyExc_TypeError,
2341 "cannot convert dictionary update "
2342 "sequence element #%zd to a sequence",
2343 i);
2344 goto Fail;
2345 }
2346 n = PySequence_Fast_GET_SIZE(fast);
2347 if (n != 2) {
2348 PyErr_Format(PyExc_ValueError,
2349 "dictionary update sequence element #%zd "
2350 "has length %zd; 2 is required",
2351 i, n);
2352 goto Fail;
2353 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002355 /* Update/merge with this (key, value) pair. */
2356 key = PySequence_Fast_GET_ITEM(fast, 0);
2357 value = PySequence_Fast_GET_ITEM(fast, 1);
2358 if (override || PyDict_GetItem(d, key) == NULL) {
2359 int status = PyDict_SetItem(d, key, value);
2360 if (status < 0)
2361 goto Fail;
2362 }
2363 Py_DECREF(fast);
2364 Py_DECREF(item);
2365 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002367 i = 0;
Victor Stinner611b0fa2016-09-14 15:02:01 +02002368 assert(_PyDict_CheckConsistency((PyDictObject *)d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00002370Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002371 Py_XDECREF(item);
2372 Py_XDECREF(fast);
2373 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002374Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002375 Py_DECREF(it);
2376 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00002377}
2378
doko@ubuntu.comde69ee72016-10-11 08:04:02 +02002379static int
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002380dict_merge(PyObject *a, PyObject *b, int override)
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002381{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002382 PyDictObject *mp, *other;
2383 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -07002384 PyDictKeyEntry *entry, *ep0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002385
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002386 assert(0 <= override && override <= 2);
2387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388 /* We accept for the argument either a concrete dictionary object,
2389 * or an abstract "mapping" object. For the former, we can do
2390 * things quite efficiently. For the latter, we only require that
2391 * PyMapping_Keys() and PyObject_GetItem() be supported.
2392 */
2393 if (a == NULL || !PyDict_Check(a) || b == NULL) {
2394 PyErr_BadInternalCall();
2395 return -1;
2396 }
2397 mp = (PyDictObject*)a;
2398 if (PyDict_Check(b)) {
2399 other = (PyDictObject*)b;
2400 if (other == mp || other->ma_used == 0)
2401 /* a.update(a) or a.update({}); nothing to do */
2402 return 0;
2403 if (mp->ma_used == 0)
2404 /* Since the target dict is empty, PyDict_GetItem()
2405 * always returns NULL. Setting override to 1
2406 * skips the unnecessary test.
2407 */
2408 override = 1;
2409 /* Do one big resize at the start, rather than
2410 * incrementally resizing as we insert new items. Expect
2411 * that there will be no (or few) overlapping keys.
2412 */
INADA Naokib1152be2016-10-27 19:26:50 +09002413 if (USABLE_FRACTION(mp->ma_keys->dk_size) < other->ma_used) {
2414 if (dictresize(mp, ESTIMATE_SIZE(mp->ma_used + other->ma_used))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002415 return -1;
INADA Naokib1152be2016-10-27 19:26:50 +09002416 }
2417 }
Victor Stinner742da042016-09-07 17:40:12 -07002418 ep0 = DK_ENTRIES(other->ma_keys);
2419 for (i = 0, n = other->ma_keys->dk_nentries; i < n; i++) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002420 PyObject *key, *value;
2421 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002422 entry = &ep0[i];
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002423 key = entry->me_key;
2424 hash = entry->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002425 if (other->ma_values)
2426 value = other->ma_values[i];
2427 else
2428 value = entry->me_value;
2429
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002430 if (value != NULL) {
2431 int err = 0;
2432 Py_INCREF(key);
2433 Py_INCREF(value);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002434 if (override == 1 || _PyDict_GetItem_KnownHash(a, key, hash) == NULL)
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002435 err = insertdict(mp, key, hash, value);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002436 else if (override != 0) {
2437 _PyErr_SetKeyError(key);
2438 Py_DECREF(value);
2439 Py_DECREF(key);
2440 return -1;
2441 }
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002442 Py_DECREF(value);
2443 Py_DECREF(key);
2444 if (err != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002445 return -1;
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002446
Victor Stinner742da042016-09-07 17:40:12 -07002447 if (n != other->ma_keys->dk_nentries) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002448 PyErr_SetString(PyExc_RuntimeError,
2449 "dict mutated during update");
2450 return -1;
2451 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002452 }
2453 }
2454 }
2455 else {
2456 /* Do it the generic, slower way */
2457 PyObject *keys = PyMapping_Keys(b);
2458 PyObject *iter;
2459 PyObject *key, *value;
2460 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002462 if (keys == NULL)
2463 /* Docstring says this is equivalent to E.keys() so
2464 * if E doesn't have a .keys() method we want
2465 * AttributeError to percolate up. Might as well
2466 * do the same for any other error.
2467 */
2468 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002470 iter = PyObject_GetIter(keys);
2471 Py_DECREF(keys);
2472 if (iter == NULL)
2473 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002475 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002476 if (override != 1 && PyDict_GetItem(a, key) != NULL) {
2477 if (override != 0) {
2478 _PyErr_SetKeyError(key);
2479 Py_DECREF(key);
2480 Py_DECREF(iter);
2481 return -1;
2482 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002483 Py_DECREF(key);
2484 continue;
2485 }
2486 value = PyObject_GetItem(b, key);
2487 if (value == NULL) {
2488 Py_DECREF(iter);
2489 Py_DECREF(key);
2490 return -1;
2491 }
2492 status = PyDict_SetItem(a, key, value);
2493 Py_DECREF(key);
2494 Py_DECREF(value);
2495 if (status < 0) {
2496 Py_DECREF(iter);
2497 return -1;
2498 }
2499 }
2500 Py_DECREF(iter);
2501 if (PyErr_Occurred())
2502 /* Iterator completed, via error */
2503 return -1;
2504 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02002505 assert(_PyDict_CheckConsistency((PyDictObject *)a));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002506 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002507}
2508
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002509int
2510PyDict_Update(PyObject *a, PyObject *b)
2511{
2512 return dict_merge(a, b, 1);
2513}
2514
2515int
2516PyDict_Merge(PyObject *a, PyObject *b, int override)
2517{
2518 /* XXX Deprecate override not in (0, 1). */
2519 return dict_merge(a, b, override != 0);
2520}
2521
2522int
2523_PyDict_MergeEx(PyObject *a, PyObject *b, int override)
2524{
2525 return dict_merge(a, b, override);
2526}
2527
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002528static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002529dict_copy(PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002530{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002531 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002532}
2533
2534PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002535PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002536{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002537 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002538 PyDictObject *mp;
2539 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002541 if (o == NULL || !PyDict_Check(o)) {
2542 PyErr_BadInternalCall();
2543 return NULL;
2544 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002545 mp = (PyDictObject *)o;
2546 if (_PyDict_HasSplitTable(mp)) {
2547 PyDictObject *split_copy;
Victor Stinner742da042016-09-07 17:40:12 -07002548 Py_ssize_t size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
2549 PyObject **newvalues;
2550 newvalues = new_values(size);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002551 if (newvalues == NULL)
2552 return PyErr_NoMemory();
2553 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2554 if (split_copy == NULL) {
2555 free_values(newvalues);
2556 return NULL;
2557 }
2558 split_copy->ma_values = newvalues;
2559 split_copy->ma_keys = mp->ma_keys;
2560 split_copy->ma_used = mp->ma_used;
2561 DK_INCREF(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002562 for (i = 0, n = size; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002563 PyObject *value = mp->ma_values[i];
2564 Py_XINCREF(value);
2565 split_copy->ma_values[i] = value;
2566 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002567 if (_PyObject_GC_IS_TRACKED(mp))
2568 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002569 return (PyObject *)split_copy;
2570 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002571 copy = PyDict_New();
2572 if (copy == NULL)
2573 return NULL;
2574 if (PyDict_Merge(copy, o, 1) == 0)
2575 return copy;
2576 Py_DECREF(copy);
2577 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002578}
2579
Martin v. Löwis18e16552006-02-15 17:27:45 +00002580Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002581PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002582{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002583 if (mp == NULL || !PyDict_Check(mp)) {
2584 PyErr_BadInternalCall();
2585 return -1;
2586 }
2587 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002588}
2589
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002590PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002591PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002592{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002593 if (mp == NULL || !PyDict_Check(mp)) {
2594 PyErr_BadInternalCall();
2595 return NULL;
2596 }
2597 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002598}
2599
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002600PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002601PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002602{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002603 if (mp == NULL || !PyDict_Check(mp)) {
2604 PyErr_BadInternalCall();
2605 return NULL;
2606 }
2607 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002608}
2609
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002610PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002611PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002612{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002613 if (mp == NULL || !PyDict_Check(mp)) {
2614 PyErr_BadInternalCall();
2615 return NULL;
2616 }
2617 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002618}
2619
Tim Peterse63415e2001-05-08 04:38:29 +00002620/* Return 1 if dicts equal, 0 if not, -1 if error.
2621 * Gets out as soon as any difference is detected.
2622 * Uses only Py_EQ comparison.
2623 */
2624static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002625dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002626{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002627 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002629 if (a->ma_used != b->ma_used)
2630 /* can't be equal if # of entries differ */
2631 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002632 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Victor Stinner742da042016-09-07 17:40:12 -07002633 for (i = 0; i < a->ma_keys->dk_nentries; i++) {
2634 PyDictKeyEntry *ep = &DK_ENTRIES(a->ma_keys)[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002635 PyObject *aval;
2636 if (a->ma_values)
2637 aval = a->ma_values[i];
2638 else
2639 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002640 if (aval != NULL) {
2641 int cmp;
2642 PyObject *bval;
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002643 PyObject **vaddr;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002644 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002645 /* temporarily bump aval's refcount to ensure it stays
2646 alive until we're done with it */
2647 Py_INCREF(aval);
2648 /* ditto for key */
2649 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002650 /* reuse the known hash value */
Victor Stinner742da042016-09-07 17:40:12 -07002651 if ((b->ma_keys->dk_lookup)(b, key, ep->me_hash, &vaddr, NULL) < 0)
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002652 bval = NULL;
2653 else
2654 bval = *vaddr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002655 Py_DECREF(key);
2656 if (bval == NULL) {
2657 Py_DECREF(aval);
2658 if (PyErr_Occurred())
2659 return -1;
2660 return 0;
2661 }
2662 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
2663 Py_DECREF(aval);
2664 if (cmp <= 0) /* error or not equal */
2665 return cmp;
2666 }
2667 }
2668 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002669}
Tim Peterse63415e2001-05-08 04:38:29 +00002670
2671static PyObject *
2672dict_richcompare(PyObject *v, PyObject *w, int op)
2673{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002674 int cmp;
2675 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002677 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2678 res = Py_NotImplemented;
2679 }
2680 else if (op == Py_EQ || op == Py_NE) {
2681 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2682 if (cmp < 0)
2683 return NULL;
2684 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2685 }
2686 else
2687 res = Py_NotImplemented;
2688 Py_INCREF(res);
2689 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002690}
Tim Peterse63415e2001-05-08 04:38:29 +00002691
Larry Hastings61272b72014-01-07 12:41:53 -08002692/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002693
2694@coexist
2695dict.__contains__
2696
2697 key: object
2698 /
2699
Meador Ingee02de8c2014-01-14 16:48:31 -06002700True if D has a key k, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002701[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002702
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002703static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002704dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002705/*[clinic end generated code: output=a3d03db709ed6e6b input=b852b2a19b51ab24]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002706{
Larry Hastingsc2047262014-01-25 20:43:29 -08002707 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002708 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002709 Py_ssize_t ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002710 PyObject **value_addr;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +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)
2722 Py_RETURN_FALSE;
2723 Py_RETURN_TRUE;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002724}
2725
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002726static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002727dict_get(PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00002728{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002729 PyObject *key;
2730 PyObject *failobj = Py_None;
2731 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002732 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002733 Py_ssize_t ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002734 PyObject **value_addr;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002735
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002736 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
2737 return NULL;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002739 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002740 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002741 hash = PyObject_Hash(key);
2742 if (hash == -1)
2743 return NULL;
2744 }
Victor Stinner742da042016-09-07 17:40:12 -07002745 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
2746 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002747 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002748 if (ix == DKIX_EMPTY || *value_addr == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002749 val = failobj;
Victor Stinner742da042016-09-07 17:40:12 -07002750 else
2751 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002752 Py_INCREF(val);
2753 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002754}
2755
Benjamin Peterson00e98862013-03-07 22:16:29 -05002756PyObject *
2757PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002758{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002759 PyDictObject *mp = (PyDictObject *)d;
INADA Naoki93f26f72016-11-02 18:45:16 +09002760 PyObject *value;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002761 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002762 Py_ssize_t hashpos, ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002763 PyObject **value_addr;
Guido van Rossum164452c2000-08-08 16:12:54 +00002764
Benjamin Peterson00e98862013-03-07 22:16:29 -05002765 if (!PyDict_Check(d)) {
2766 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002767 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002768 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002770 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002771 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002772 hash = PyObject_Hash(key);
2773 if (hash == -1)
2774 return NULL;
2775 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002776
2777 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
2778 if (insertion_resize(mp) < 0)
2779 return NULL;
2780 }
2781
Victor Stinner742da042016-09-07 17:40:12 -07002782 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, &hashpos);
2783 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002784 return NULL;
INADA Naoki93f26f72016-11-02 18:45:16 +09002785
2786 if (_PyDict_HasSplitTable(mp) &&
2787 ((ix >= 0 && *value_addr == NULL && mp->ma_used != ix) ||
2788 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
2789 if (insertion_resize(mp) < 0) {
2790 return NULL;
2791 }
2792 find_empty_slot(mp, key, hash, &value_addr, &hashpos);
2793 ix = DKIX_EMPTY;
2794 }
2795
2796 if (ix == DKIX_EMPTY) {
2797 PyDictKeyEntry *ep, *ep0;
2798 value = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002799 if (mp->ma_keys->dk_usable <= 0) {
Victor Stinner3c336c52016-09-12 14:17:40 +02002800 if (insertion_resize(mp) < 0) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002801 return NULL;
Victor Stinner3c336c52016-09-12 14:17:40 +02002802 }
Victor Stinner742da042016-09-07 17:40:12 -07002803 find_empty_slot(mp, key, hash, &value_addr, &hashpos);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002804 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002805 ep0 = DK_ENTRIES(mp->ma_keys);
2806 ep = &ep0[mp->ma_keys->dk_nentries];
2807 dk_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002808 Py_INCREF(key);
INADA Naoki93f26f72016-11-02 18:45:16 +09002809 Py_INCREF(value);
2810 MAINTAIN_TRACKING(mp, key, value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002811 ep->me_key = key;
2812 ep->me_hash = hash;
Victor Stinner742da042016-09-07 17:40:12 -07002813 if (mp->ma_values) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002814 assert(mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
2815 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Victor Stinner742da042016-09-07 17:40:12 -07002816 }
2817 else {
INADA Naoki93f26f72016-11-02 18:45:16 +09002818 ep->me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07002819 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002820 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002821 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naoki93f26f72016-11-02 18:45:16 +09002822 mp->ma_keys->dk_usable--;
2823 mp->ma_keys->dk_nentries++;
2824 assert(mp->ma_keys->dk_usable >= 0);
2825 }
2826 else if (*value_addr == NULL) {
2827 value = defaultobj;
2828 assert(_PyDict_HasSplitTable(mp));
2829 assert(ix == mp->ma_used);
2830 Py_INCREF(value);
2831 MAINTAIN_TRACKING(mp, key, value);
2832 *value_addr = value;
2833 mp->ma_used++;
2834 mp->ma_version_tag = DICT_NEXT_VERSION();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002835 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02002836 else {
INADA Naoki93f26f72016-11-02 18:45:16 +09002837 value = *value_addr;
Victor Stinner611b0fa2016-09-14 15:02:01 +02002838 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002839
2840 assert(_PyDict_CheckConsistency(mp));
2841 return value;
Guido van Rossum164452c2000-08-08 16:12:54 +00002842}
2843
Benjamin Peterson00e98862013-03-07 22:16:29 -05002844static PyObject *
2845dict_setdefault(PyDictObject *mp, PyObject *args)
2846{
2847 PyObject *key, *val;
2848 PyObject *defaultobj = Py_None;
2849
2850 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &defaultobj))
2851 return NULL;
2852
Benjamin Peterson55898502013-03-08 08:36:49 -05002853 val = PyDict_SetDefault((PyObject *)mp, key, defaultobj);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002854 Py_XINCREF(val);
2855 return val;
2856}
Guido van Rossum164452c2000-08-08 16:12:54 +00002857
2858static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002859dict_clear(PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002860{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002861 PyDict_Clear((PyObject *)mp);
2862 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002863}
2864
Guido van Rossumba6ab842000-12-12 22:02:18 +00002865static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002866dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002867{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002868 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00002869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002870 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2871 return NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06002872
Yury Selivanov684ef2c2016-10-28 19:01:21 -04002873 return _PyDict_Pop((PyObject*)mp, key, deflt);
Guido van Rossume027d982002-04-12 15:11:59 +00002874}
2875
2876static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002877dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00002878{
Victor Stinner742da042016-09-07 17:40:12 -07002879 Py_ssize_t i, j;
2880 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002881 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002883 /* Allocate the result tuple before checking the size. Believe it
2884 * or not, this allocation could trigger a garbage collection which
2885 * could empty the dict, so if we checked the size first and that
2886 * happened, the result would be an infinite loop (searching for an
2887 * entry that no longer exists). Note that the usual popitem()
2888 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2889 * tuple away if the dict *is* empty isn't a significant
2890 * inefficiency -- possible, but unlikely in practice.
2891 */
2892 res = PyTuple_New(2);
2893 if (res == NULL)
2894 return NULL;
2895 if (mp->ma_used == 0) {
2896 Py_DECREF(res);
2897 PyErr_SetString(PyExc_KeyError,
2898 "popitem(): dictionary is empty");
2899 return NULL;
2900 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002901 /* Convert split table to combined table */
2902 if (mp->ma_keys->dk_lookup == lookdict_split) {
2903 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
2904 Py_DECREF(res);
2905 return NULL;
2906 }
2907 }
2908 ENSURE_ALLOWS_DELETIONS(mp);
Victor Stinner742da042016-09-07 17:40:12 -07002909
2910 /* Pop last item */
2911 ep0 = DK_ENTRIES(mp->ma_keys);
2912 i = mp->ma_keys->dk_nentries - 1;
2913 while (i >= 0 && ep0[i].me_value == NULL) {
2914 i--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002915 }
Victor Stinner742da042016-09-07 17:40:12 -07002916 assert(i >= 0);
2917
2918 ep = &ep0[i];
2919 j = lookdict_index(mp->ma_keys, ep->me_hash, i);
2920 assert(j >= 0);
2921 assert(dk_get_index(mp->ma_keys, j) == i);
2922 dk_set_index(mp->ma_keys, j, DKIX_DUMMY);
2923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002924 PyTuple_SET_ITEM(res, 0, ep->me_key);
2925 PyTuple_SET_ITEM(res, 1, ep->me_value);
Victor Stinner742da042016-09-07 17:40:12 -07002926 ep->me_key = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002927 ep->me_value = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002928 /* We can't dk_usable++ since there is DKIX_DUMMY in indices */
2929 mp->ma_keys->dk_nentries = i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002930 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002931 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +02002932 assert(_PyDict_CheckConsistency(mp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002933 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002934}
2935
Jeremy Hylton8caad492000-06-23 14:18:11 +00002936static int
2937dict_traverse(PyObject *op, visitproc visit, void *arg)
2938{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002939 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson55f44522016-09-05 12:12:59 -07002940 PyDictKeysObject *keys = mp->ma_keys;
Serhiy Storchaka46825d22016-09-26 21:29:34 +03002941 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Victor Stinner742da042016-09-07 17:40:12 -07002942 Py_ssize_t i, n = keys->dk_nentries;
2943
Benjamin Peterson55f44522016-09-05 12:12:59 -07002944 if (keys->dk_lookup == lookdict) {
2945 for (i = 0; i < n; i++) {
2946 if (entries[i].me_value != NULL) {
2947 Py_VISIT(entries[i].me_value);
2948 Py_VISIT(entries[i].me_key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002949 }
2950 }
Victor Stinner742da042016-09-07 17:40:12 -07002951 }
2952 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002953 if (mp->ma_values != NULL) {
Benjamin Peterson55f44522016-09-05 12:12:59 -07002954 for (i = 0; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002955 Py_VISIT(mp->ma_values[i]);
2956 }
2957 }
2958 else {
Benjamin Peterson55f44522016-09-05 12:12:59 -07002959 for (i = 0; i < n; i++) {
2960 Py_VISIT(entries[i].me_value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002961 }
2962 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002963 }
2964 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002965}
2966
2967static int
2968dict_tp_clear(PyObject *op)
2969{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002970 PyDict_Clear(op);
2971 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002972}
2973
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002974static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002975
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002976Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06002977_PyDict_SizeOf(PyDictObject *mp)
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002978{
Victor Stinner742da042016-09-07 17:40:12 -07002979 Py_ssize_t size, usable, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002980
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002981 size = DK_SIZE(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002982 usable = USABLE_FRACTION(size);
2983
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002984 res = _PyObject_SIZE(Py_TYPE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002985 if (mp->ma_values)
Victor Stinner742da042016-09-07 17:40:12 -07002986 res += usable * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002987 /* If the dictionary is split, the keys portion is accounted-for
2988 in the type object. */
2989 if (mp->ma_keys->dk_refcnt == 1)
Victor Stinner98ee9d52016-09-08 09:33:56 -07002990 res += (sizeof(PyDictKeysObject)
2991 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
2992 + DK_IXSIZE(mp->ma_keys) * size
2993 + sizeof(PyDictKeyEntry) * usable);
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002994 return res;
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002995}
2996
2997Py_ssize_t
2998_PyDict_KeysSize(PyDictKeysObject *keys)
2999{
Victor Stinner98ee9d52016-09-08 09:33:56 -07003000 return (sizeof(PyDictKeysObject)
3001 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
3002 + DK_IXSIZE(keys) * DK_SIZE(keys)
3003 + USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry));
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003004}
3005
doko@ubuntu.com17210f52016-01-14 14:04:59 +01003006static PyObject *
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003007dict_sizeof(PyDictObject *mp)
3008{
3009 return PyLong_FromSsize_t(_PyDict_SizeOf(mp));
3010}
3011
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00003012PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
3013
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003014PyDoc_STRVAR(sizeof__doc__,
3015"D.__sizeof__() -> size of D in memory, in bytes");
3016
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003017PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00003018"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003019
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003020PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00003021"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 +00003022
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003023PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00003024"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00003025If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00003026
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003027PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00003028"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000030292-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003030
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003031PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04003032"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
3033If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
3034If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
3035In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00003036
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003037PyDoc_STRVAR(clear__doc__,
3038"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003039
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003040PyDoc_STRVAR(copy__doc__,
3041"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00003042
Guido van Rossumb90c8482007-02-10 01:11:45 +00003043/* Forward */
3044static PyObject *dictkeys_new(PyObject *);
3045static PyObject *dictitems_new(PyObject *);
3046static PyObject *dictvalues_new(PyObject *);
3047
Guido van Rossum45c85d12007-07-27 16:31:40 +00003048PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003049 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003050PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003051 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003052PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003053 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00003054
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003055static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07003056 DICT___CONTAINS___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003057 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
3058 getitem__doc__},
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003059 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003060 sizeof__doc__},
3061 {"get", (PyCFunction)dict_get, METH_VARARGS,
3062 get__doc__},
3063 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
3064 setdefault_doc__},
3065 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
3066 pop__doc__},
3067 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
3068 popitem__doc__},
3069 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
3070 keys__doc__},
3071 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
3072 items__doc__},
3073 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
3074 values__doc__},
3075 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
3076 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08003077 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003078 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
3079 clear__doc__},
3080 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
3081 copy__doc__},
3082 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003083};
3084
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00003085/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00003086int
3087PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003088{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003089 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07003090 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003091 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003092 PyObject **value_addr;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003094 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003095 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003096 hash = PyObject_Hash(key);
3097 if (hash == -1)
3098 return -1;
3099 }
Victor Stinner742da042016-09-07 17:40:12 -07003100 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
3101 if (ix == DKIX_ERROR)
3102 return -1;
3103 return (ix != DKIX_EMPTY && *value_addr != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003104}
3105
Thomas Wouterscf297e42007-02-23 15:07:44 +00003106/* Internal version of PyDict_Contains used when the hash value is already known */
3107int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003108_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00003109{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003110 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003111 PyObject **value_addr;
Victor Stinner742da042016-09-07 17:40:12 -07003112 Py_ssize_t ix;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003113
Victor Stinner742da042016-09-07 17:40:12 -07003114 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
3115 if (ix == DKIX_ERROR)
3116 return -1;
3117 return (ix != DKIX_EMPTY && *value_addr != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00003118}
3119
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003120/* Hack to implement "key in dict" */
3121static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003122 0, /* sq_length */
3123 0, /* sq_concat */
3124 0, /* sq_repeat */
3125 0, /* sq_item */
3126 0, /* sq_slice */
3127 0, /* sq_ass_item */
3128 0, /* sq_ass_slice */
3129 PyDict_Contains, /* sq_contains */
3130 0, /* sq_inplace_concat */
3131 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003132};
3133
Guido van Rossum09e563a2001-05-01 12:10:21 +00003134static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003135dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3136{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003137 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003138 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003140 assert(type != NULL && type->tp_alloc != NULL);
3141 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02003142 if (self == NULL)
3143 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003144 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003145
Victor Stinnera9f61a52013-07-16 22:17:26 +02003146 /* The object has been implicitly tracked by tp_alloc */
3147 if (type == &PyDict_Type)
3148 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003149
3150 d->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003151 d->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07003152 d->ma_keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003153 if (d->ma_keys == NULL) {
3154 Py_DECREF(self);
3155 return NULL;
3156 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02003157 assert(_PyDict_CheckConsistency(d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003158 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003159}
3160
Tim Peters25786c02001-09-02 08:22:48 +00003161static int
3162dict_init(PyObject *self, PyObject *args, PyObject *kwds)
3163{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003164 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00003165}
3166
Tim Peters6d6c1a32001-08-02 04:15:00 +00003167static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003168dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00003169{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003170 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003171}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003172
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003173PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00003174"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00003175"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003176" (key, value) pairs\n"
3177"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00003178" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003179" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00003180" d[k] = v\n"
3181"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
3182" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00003183
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003184PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003185 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3186 "dict",
3187 sizeof(PyDictObject),
3188 0,
3189 (destructor)dict_dealloc, /* tp_dealloc */
3190 0, /* tp_print */
3191 0, /* tp_getattr */
3192 0, /* tp_setattr */
3193 0, /* tp_reserved */
3194 (reprfunc)dict_repr, /* tp_repr */
3195 0, /* tp_as_number */
3196 &dict_as_sequence, /* tp_as_sequence */
3197 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003198 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003199 0, /* tp_call */
3200 0, /* tp_str */
3201 PyObject_GenericGetAttr, /* tp_getattro */
3202 0, /* tp_setattro */
3203 0, /* tp_as_buffer */
3204 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3205 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
3206 dictionary_doc, /* tp_doc */
3207 dict_traverse, /* tp_traverse */
3208 dict_tp_clear, /* tp_clear */
3209 dict_richcompare, /* tp_richcompare */
3210 0, /* tp_weaklistoffset */
3211 (getiterfunc)dict_iter, /* tp_iter */
3212 0, /* tp_iternext */
3213 mapp_methods, /* tp_methods */
3214 0, /* tp_members */
3215 0, /* tp_getset */
3216 0, /* tp_base */
3217 0, /* tp_dict */
3218 0, /* tp_descr_get */
3219 0, /* tp_descr_set */
3220 0, /* tp_dictoffset */
3221 dict_init, /* tp_init */
3222 PyType_GenericAlloc, /* tp_alloc */
3223 dict_new, /* tp_new */
3224 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003225};
3226
Victor Stinner3c1e4812012-03-26 22:10:51 +02003227PyObject *
3228_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
3229{
3230 PyObject *kv;
3231 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02003232 if (kv == NULL) {
3233 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003234 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02003235 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02003236 return PyDict_GetItem(dp, kv);
3237}
3238
Guido van Rossum3cca2451997-05-16 14:23:33 +00003239/* For backward compatibility with old dictionary interface */
3240
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003241PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003242PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003243{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003244 PyObject *kv, *rv;
3245 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003246 if (kv == NULL) {
3247 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003248 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003249 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003250 rv = PyDict_GetItem(v, kv);
3251 Py_DECREF(kv);
3252 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003253}
3254
3255int
Victor Stinner3c1e4812012-03-26 22:10:51 +02003256_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
3257{
3258 PyObject *kv;
3259 kv = _PyUnicode_FromId(key); /* borrowed */
3260 if (kv == NULL)
3261 return -1;
3262 return PyDict_SetItem(v, kv, item);
3263}
3264
3265int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003266PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003267{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003268 PyObject *kv;
3269 int err;
3270 kv = PyUnicode_FromString(key);
3271 if (kv == NULL)
3272 return -1;
3273 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
3274 err = PyDict_SetItem(v, kv, item);
3275 Py_DECREF(kv);
3276 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003277}
3278
3279int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01003280_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
3281{
3282 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
3283 if (kv == NULL)
3284 return -1;
3285 return PyDict_DelItem(v, kv);
3286}
3287
3288int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003289PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003290{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003291 PyObject *kv;
3292 int err;
3293 kv = PyUnicode_FromString(key);
3294 if (kv == NULL)
3295 return -1;
3296 err = PyDict_DelItem(v, kv);
3297 Py_DECREF(kv);
3298 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003299}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003300
Raymond Hettinger019a1482004-03-18 02:41:19 +00003301/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003302
3303typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003304 PyObject_HEAD
3305 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
3306 Py_ssize_t di_used;
3307 Py_ssize_t di_pos;
3308 PyObject* di_result; /* reusable result tuple for iteritems */
3309 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003310} dictiterobject;
3311
3312static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003313dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003314{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003315 dictiterobject *di;
3316 di = PyObject_GC_New(dictiterobject, itertype);
3317 if (di == NULL)
3318 return NULL;
3319 Py_INCREF(dict);
3320 di->di_dict = dict;
3321 di->di_used = dict->ma_used;
3322 di->di_pos = 0;
3323 di->len = dict->ma_used;
3324 if (itertype == &PyDictIterItem_Type) {
3325 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
3326 if (di->di_result == NULL) {
3327 Py_DECREF(di);
3328 return NULL;
3329 }
3330 }
3331 else
3332 di->di_result = NULL;
3333 _PyObject_GC_TRACK(di);
3334 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003335}
3336
3337static void
3338dictiter_dealloc(dictiterobject *di)
3339{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003340 Py_XDECREF(di->di_dict);
3341 Py_XDECREF(di->di_result);
3342 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003343}
3344
3345static int
3346dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
3347{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003348 Py_VISIT(di->di_dict);
3349 Py_VISIT(di->di_result);
3350 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003351}
3352
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003353static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003354dictiter_len(dictiterobject *di)
3355{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003356 Py_ssize_t len = 0;
3357 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
3358 len = di->len;
3359 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003360}
3361
Guido van Rossumb90c8482007-02-10 01:11:45 +00003362PyDoc_STRVAR(length_hint_doc,
3363 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003364
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003365static PyObject *
3366dictiter_reduce(dictiterobject *di);
3367
3368PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3369
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003370static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003371 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
3372 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003373 {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS,
3374 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003375 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003376};
3377
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003378static PyObject*
3379dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00003380{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003381 PyObject *key;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003382 Py_ssize_t i, n;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003383 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003384 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00003385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003386 if (d == NULL)
3387 return NULL;
3388 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00003389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003390 if (di->di_used != d->ma_used) {
3391 PyErr_SetString(PyExc_RuntimeError,
3392 "dictionary changed size during iteration");
3393 di->di_used = -1; /* Make this state sticky */
3394 return NULL;
3395 }
Guido van Rossum2147df72002-07-16 20:30:22 +00003396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003397 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003398 k = d->ma_keys;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003399 n = k->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003400 if (d->ma_values) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003401 PyObject **value_ptr = &d->ma_values[i];
3402 while (i < n && *value_ptr == NULL) {
3403 value_ptr++;
3404 i++;
3405 }
3406 if (i >= n)
3407 goto fail;
3408 key = DK_ENTRIES(k)[i].me_key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003409 }
3410 else {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003411 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
3412 while (i < n && entry_ptr->me_value == NULL) {
3413 entry_ptr++;
3414 i++;
3415 }
3416 if (i >= n)
3417 goto fail;
3418 key = entry_ptr->me_key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003419 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003420 di->di_pos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003421 di->len--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003422 Py_INCREF(key);
3423 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003424
3425fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003426 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003427 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003428 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003429}
3430
Raymond Hettinger019a1482004-03-18 02:41:19 +00003431PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003432 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3433 "dict_keyiterator", /* tp_name */
3434 sizeof(dictiterobject), /* tp_basicsize */
3435 0, /* tp_itemsize */
3436 /* methods */
3437 (destructor)dictiter_dealloc, /* tp_dealloc */
3438 0, /* tp_print */
3439 0, /* tp_getattr */
3440 0, /* tp_setattr */
3441 0, /* tp_reserved */
3442 0, /* tp_repr */
3443 0, /* tp_as_number */
3444 0, /* tp_as_sequence */
3445 0, /* tp_as_mapping */
3446 0, /* tp_hash */
3447 0, /* tp_call */
3448 0, /* tp_str */
3449 PyObject_GenericGetAttr, /* tp_getattro */
3450 0, /* tp_setattro */
3451 0, /* tp_as_buffer */
3452 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3453 0, /* tp_doc */
3454 (traverseproc)dictiter_traverse, /* tp_traverse */
3455 0, /* tp_clear */
3456 0, /* tp_richcompare */
3457 0, /* tp_weaklistoffset */
3458 PyObject_SelfIter, /* tp_iter */
3459 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
3460 dictiter_methods, /* tp_methods */
3461 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003462};
3463
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003464static PyObject *
3465dictiter_iternextvalue(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003466{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003467 PyObject *value;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003468 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003469 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003471 if (d == NULL)
3472 return NULL;
3473 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003475 if (di->di_used != d->ma_used) {
3476 PyErr_SetString(PyExc_RuntimeError,
3477 "dictionary changed size during iteration");
3478 di->di_used = -1; /* Make this state sticky */
3479 return NULL;
3480 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003482 i = di->di_pos;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003483 n = d->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003484 if (d->ma_values) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003485 PyObject **value_ptr = &d->ma_values[i];
3486 while (i < n && *value_ptr == NULL) {
3487 value_ptr++;
3488 i++;
3489 }
3490 if (i >= n)
3491 goto fail;
3492 value = *value_ptr;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003493 }
3494 else {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003495 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3496 while (i < n && entry_ptr->me_value == NULL) {
3497 entry_ptr++;
3498 i++;
3499 }
3500 if (i >= n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003501 goto fail;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003502 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003503 }
3504 di->di_pos = i+1;
3505 di->len--;
3506 Py_INCREF(value);
3507 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003508
3509fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003510 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003511 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003512 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003513}
3514
3515PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003516 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3517 "dict_valueiterator", /* tp_name */
3518 sizeof(dictiterobject), /* tp_basicsize */
3519 0, /* tp_itemsize */
3520 /* methods */
3521 (destructor)dictiter_dealloc, /* tp_dealloc */
3522 0, /* tp_print */
3523 0, /* tp_getattr */
3524 0, /* tp_setattr */
3525 0, /* tp_reserved */
3526 0, /* tp_repr */
3527 0, /* tp_as_number */
3528 0, /* tp_as_sequence */
3529 0, /* tp_as_mapping */
3530 0, /* tp_hash */
3531 0, /* tp_call */
3532 0, /* tp_str */
3533 PyObject_GenericGetAttr, /* tp_getattro */
3534 0, /* tp_setattro */
3535 0, /* tp_as_buffer */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003536 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003537 0, /* tp_doc */
3538 (traverseproc)dictiter_traverse, /* tp_traverse */
3539 0, /* tp_clear */
3540 0, /* tp_richcompare */
3541 0, /* tp_weaklistoffset */
3542 PyObject_SelfIter, /* tp_iter */
3543 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3544 dictiter_methods, /* tp_methods */
3545 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003546};
3547
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003548static PyObject *
3549dictiter_iternextitem(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003550{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003551 PyObject *key, *value, *result = di->di_result;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003552 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003553 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003555 if (d == NULL)
3556 return NULL;
3557 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003559 if (di->di_used != d->ma_used) {
3560 PyErr_SetString(PyExc_RuntimeError,
3561 "dictionary changed size during iteration");
3562 di->di_used = -1; /* Make this state sticky */
3563 return NULL;
3564 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003566 i = di->di_pos;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003567 n = d->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003568 if (d->ma_values) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003569 PyObject **value_ptr = &d->ma_values[i];
3570 while (i < n && *value_ptr == NULL) {
3571 value_ptr++;
3572 i++;
3573 }
3574 if (i >= n)
3575 goto fail;
3576 key = DK_ENTRIES(d->ma_keys)[i].me_key;
3577 value = *value_ptr;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003578 }
3579 else {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003580 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3581 while (i < n && entry_ptr->me_value == NULL) {
3582 entry_ptr++;
3583 i++;
3584 }
3585 if (i >= n)
3586 goto fail;
3587 key = entry_ptr->me_key;
3588 value = entry_ptr->me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003589 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003590 di->di_pos = i+1;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003591 di->len--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003592 if (result->ob_refcnt == 1) {
3593 Py_INCREF(result);
3594 Py_DECREF(PyTuple_GET_ITEM(result, 0));
3595 Py_DECREF(PyTuple_GET_ITEM(result, 1));
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003596 }
3597 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003598 result = PyTuple_New(2);
3599 if (result == NULL)
3600 return NULL;
3601 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003602 Py_INCREF(key);
3603 Py_INCREF(value);
Eric Snow96c6af92015-05-29 22:21:39 -06003604 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3605 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003606 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003607
3608fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003609 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003610 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003611 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003612}
3613
3614PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003615 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3616 "dict_itemiterator", /* tp_name */
3617 sizeof(dictiterobject), /* tp_basicsize */
3618 0, /* tp_itemsize */
3619 /* methods */
3620 (destructor)dictiter_dealloc, /* tp_dealloc */
3621 0, /* tp_print */
3622 0, /* tp_getattr */
3623 0, /* tp_setattr */
3624 0, /* tp_reserved */
3625 0, /* tp_repr */
3626 0, /* tp_as_number */
3627 0, /* tp_as_sequence */
3628 0, /* tp_as_mapping */
3629 0, /* tp_hash */
3630 0, /* tp_call */
3631 0, /* tp_str */
3632 PyObject_GenericGetAttr, /* tp_getattro */
3633 0, /* tp_setattro */
3634 0, /* tp_as_buffer */
3635 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3636 0, /* tp_doc */
3637 (traverseproc)dictiter_traverse, /* tp_traverse */
3638 0, /* tp_clear */
3639 0, /* tp_richcompare */
3640 0, /* tp_weaklistoffset */
3641 PyObject_SelfIter, /* tp_iter */
3642 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3643 dictiter_methods, /* tp_methods */
3644 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003645};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003646
3647
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003648static PyObject *
3649dictiter_reduce(dictiterobject *di)
3650{
3651 PyObject *list;
3652 dictiterobject tmp;
3653
3654 list = PyList_New(0);
3655 if (!list)
3656 return NULL;
3657
3658 /* copy the itertor state */
3659 tmp = *di;
3660 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003661
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003662 /* iterate the temporary into a list */
3663 for(;;) {
3664 PyObject *element = 0;
3665 if (Py_TYPE(di) == &PyDictIterItem_Type)
3666 element = dictiter_iternextitem(&tmp);
3667 else if (Py_TYPE(di) == &PyDictIterKey_Type)
3668 element = dictiter_iternextkey(&tmp);
3669 else if (Py_TYPE(di) == &PyDictIterValue_Type)
3670 element = dictiter_iternextvalue(&tmp);
3671 else
3672 assert(0);
3673 if (element) {
3674 if (PyList_Append(list, element)) {
3675 Py_DECREF(element);
3676 Py_DECREF(list);
3677 Py_XDECREF(tmp.di_dict);
3678 return NULL;
3679 }
3680 Py_DECREF(element);
3681 } else
3682 break;
3683 }
3684 Py_XDECREF(tmp.di_dict);
3685 /* check for error */
3686 if (tmp.di_dict != NULL) {
3687 /* we have an error */
3688 Py_DECREF(list);
3689 return NULL;
3690 }
Antoine Pitroua7013882012-04-05 00:04:20 +02003691 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003692}
3693
Guido van Rossum3ac67412007-02-10 18:55:06 +00003694/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003695/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003696/***********************************************/
3697
Guido van Rossumb90c8482007-02-10 01:11:45 +00003698/* The instance lay-out is the same for all three; but the type differs. */
3699
Guido van Rossumb90c8482007-02-10 01:11:45 +00003700static void
Eric Snow96c6af92015-05-29 22:21:39 -06003701dictview_dealloc(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003702{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003703 Py_XDECREF(dv->dv_dict);
3704 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003705}
3706
3707static int
Eric Snow96c6af92015-05-29 22:21:39 -06003708dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003709{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003710 Py_VISIT(dv->dv_dict);
3711 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003712}
3713
Guido van Rossum83825ac2007-02-10 04:54:19 +00003714static Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003715dictview_len(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003716{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003717 Py_ssize_t len = 0;
3718 if (dv->dv_dict != NULL)
3719 len = dv->dv_dict->ma_used;
3720 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003721}
3722
Eric Snow96c6af92015-05-29 22:21:39 -06003723PyObject *
3724_PyDictView_New(PyObject *dict, PyTypeObject *type)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003725{
Eric Snow96c6af92015-05-29 22:21:39 -06003726 _PyDictViewObject *dv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003727 if (dict == NULL) {
3728 PyErr_BadInternalCall();
3729 return NULL;
3730 }
3731 if (!PyDict_Check(dict)) {
3732 /* XXX Get rid of this restriction later */
3733 PyErr_Format(PyExc_TypeError,
3734 "%s() requires a dict argument, not '%s'",
3735 type->tp_name, dict->ob_type->tp_name);
3736 return NULL;
3737 }
Eric Snow96c6af92015-05-29 22:21:39 -06003738 dv = PyObject_GC_New(_PyDictViewObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003739 if (dv == NULL)
3740 return NULL;
3741 Py_INCREF(dict);
3742 dv->dv_dict = (PyDictObject *)dict;
3743 _PyObject_GC_TRACK(dv);
3744 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003745}
3746
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003747/* TODO(guido): The views objects are not complete:
3748
3749 * support more set operations
3750 * support arbitrary mappings?
3751 - either these should be static or exported in dictobject.h
3752 - if public then they should probably be in builtins
3753*/
3754
Guido van Rossumaac530c2007-08-24 22:33:45 +00003755/* Return 1 if self is a subset of other, iterating over self;
3756 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003757static int
3758all_contained_in(PyObject *self, PyObject *other)
3759{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003760 PyObject *iter = PyObject_GetIter(self);
3761 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003763 if (iter == NULL)
3764 return -1;
3765 for (;;) {
3766 PyObject *next = PyIter_Next(iter);
3767 if (next == NULL) {
3768 if (PyErr_Occurred())
3769 ok = -1;
3770 break;
3771 }
3772 ok = PySequence_Contains(other, next);
3773 Py_DECREF(next);
3774 if (ok <= 0)
3775 break;
3776 }
3777 Py_DECREF(iter);
3778 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003779}
3780
3781static PyObject *
3782dictview_richcompare(PyObject *self, PyObject *other, int op)
3783{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003784 Py_ssize_t len_self, len_other;
3785 int ok;
3786 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003788 assert(self != NULL);
3789 assert(PyDictViewSet_Check(self));
3790 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00003791
Brian Curtindfc80e32011-08-10 20:28:54 -05003792 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
3793 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003794
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003795 len_self = PyObject_Size(self);
3796 if (len_self < 0)
3797 return NULL;
3798 len_other = PyObject_Size(other);
3799 if (len_other < 0)
3800 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003802 ok = 0;
3803 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00003804
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003805 case Py_NE:
3806 case Py_EQ:
3807 if (len_self == len_other)
3808 ok = all_contained_in(self, other);
3809 if (op == Py_NE && ok >= 0)
3810 ok = !ok;
3811 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003813 case Py_LT:
3814 if (len_self < len_other)
3815 ok = all_contained_in(self, other);
3816 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003818 case Py_LE:
3819 if (len_self <= len_other)
3820 ok = all_contained_in(self, other);
3821 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003823 case Py_GT:
3824 if (len_self > len_other)
3825 ok = all_contained_in(other, self);
3826 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003827
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003828 case Py_GE:
3829 if (len_self >= len_other)
3830 ok = all_contained_in(other, self);
3831 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003832
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003833 }
3834 if (ok < 0)
3835 return NULL;
3836 result = ok ? Py_True : Py_False;
3837 Py_INCREF(result);
3838 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003839}
3840
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003841static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003842dictview_repr(_PyDictViewObject *dv)
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003843{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003844 PyObject *seq;
3845 PyObject *result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003847 seq = PySequence_List((PyObject *)dv);
3848 if (seq == NULL)
3849 return NULL;
3850
3851 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
3852 Py_DECREF(seq);
3853 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003854}
3855
Guido van Rossum3ac67412007-02-10 18:55:06 +00003856/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003857
3858static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003859dictkeys_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003860{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003861 if (dv->dv_dict == NULL) {
3862 Py_RETURN_NONE;
3863 }
3864 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003865}
3866
3867static int
Eric Snow96c6af92015-05-29 22:21:39 -06003868dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00003869{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003870 if (dv->dv_dict == NULL)
3871 return 0;
3872 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003873}
3874
Guido van Rossum83825ac2007-02-10 04:54:19 +00003875static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003876 (lenfunc)dictview_len, /* sq_length */
3877 0, /* sq_concat */
3878 0, /* sq_repeat */
3879 0, /* sq_item */
3880 0, /* sq_slice */
3881 0, /* sq_ass_item */
3882 0, /* sq_ass_slice */
3883 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003884};
3885
Guido van Rossum523259b2007-08-24 23:41:22 +00003886static PyObject*
3887dictviews_sub(PyObject* self, PyObject *other)
3888{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003889 PyObject *result = PySet_New(self);
3890 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003891 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003893 if (result == NULL)
3894 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003895
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003896 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003897 if (tmp == NULL) {
3898 Py_DECREF(result);
3899 return NULL;
3900 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003902 Py_DECREF(tmp);
3903 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003904}
3905
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003906PyObject*
3907_PyDictView_Intersect(PyObject* self, PyObject *other)
Guido van Rossum523259b2007-08-24 23:41:22 +00003908{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003909 PyObject *result = PySet_New(self);
3910 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003911 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003912
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003913 if (result == NULL)
3914 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003915
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003916 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_intersection_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003917 if (tmp == NULL) {
3918 Py_DECREF(result);
3919 return NULL;
3920 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003922 Py_DECREF(tmp);
3923 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003924}
3925
3926static PyObject*
3927dictviews_or(PyObject* self, PyObject *other)
3928{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003929 PyObject *result = PySet_New(self);
3930 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003931 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003933 if (result == NULL)
3934 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003935
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003936 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003937 if (tmp == NULL) {
3938 Py_DECREF(result);
3939 return NULL;
3940 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003942 Py_DECREF(tmp);
3943 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003944}
3945
3946static PyObject*
3947dictviews_xor(PyObject* self, PyObject *other)
3948{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003949 PyObject *result = PySet_New(self);
3950 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003951 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003953 if (result == NULL)
3954 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003955
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003956 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_symmetric_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003957 if (tmp == NULL) {
3958 Py_DECREF(result);
3959 return NULL;
3960 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003962 Py_DECREF(tmp);
3963 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003964}
3965
3966static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003967 0, /*nb_add*/
3968 (binaryfunc)dictviews_sub, /*nb_subtract*/
3969 0, /*nb_multiply*/
3970 0, /*nb_remainder*/
3971 0, /*nb_divmod*/
3972 0, /*nb_power*/
3973 0, /*nb_negative*/
3974 0, /*nb_positive*/
3975 0, /*nb_absolute*/
3976 0, /*nb_bool*/
3977 0, /*nb_invert*/
3978 0, /*nb_lshift*/
3979 0, /*nb_rshift*/
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003980 (binaryfunc)_PyDictView_Intersect, /*nb_and*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003981 (binaryfunc)dictviews_xor, /*nb_xor*/
3982 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00003983};
3984
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003985static PyObject*
3986dictviews_isdisjoint(PyObject *self, PyObject *other)
3987{
3988 PyObject *it;
3989 PyObject *item = NULL;
3990
3991 if (self == other) {
Eric Snow96c6af92015-05-29 22:21:39 -06003992 if (dictview_len((_PyDictViewObject *)self) == 0)
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003993 Py_RETURN_TRUE;
3994 else
3995 Py_RETURN_FALSE;
3996 }
3997
3998 /* Iterate over the shorter object (only if other is a set,
3999 * because PySequence_Contains may be expensive otherwise): */
4000 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
Eric Snow96c6af92015-05-29 22:21:39 -06004001 Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self);
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004002 Py_ssize_t len_other = PyObject_Size(other);
4003 if (len_other == -1)
4004 return NULL;
4005
4006 if ((len_other > len_self)) {
4007 PyObject *tmp = other;
4008 other = self;
4009 self = tmp;
4010 }
4011 }
4012
4013 it = PyObject_GetIter(other);
4014 if (it == NULL)
4015 return NULL;
4016
4017 while ((item = PyIter_Next(it)) != NULL) {
4018 int contains = PySequence_Contains(self, item);
4019 Py_DECREF(item);
4020 if (contains == -1) {
4021 Py_DECREF(it);
4022 return NULL;
4023 }
4024
4025 if (contains) {
4026 Py_DECREF(it);
4027 Py_RETURN_FALSE;
4028 }
4029 }
4030 Py_DECREF(it);
4031 if (PyErr_Occurred())
4032 return NULL; /* PyIter_Next raised an exception. */
4033 Py_RETURN_TRUE;
4034}
4035
4036PyDoc_STRVAR(isdisjoint_doc,
4037"Return True if the view and the given iterable have a null intersection.");
4038
Guido van Rossumb90c8482007-02-10 01:11:45 +00004039static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004040 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4041 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004042 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004043};
4044
4045PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004046 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4047 "dict_keys", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004048 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004049 0, /* tp_itemsize */
4050 /* methods */
4051 (destructor)dictview_dealloc, /* tp_dealloc */
4052 0, /* tp_print */
4053 0, /* tp_getattr */
4054 0, /* tp_setattr */
4055 0, /* tp_reserved */
4056 (reprfunc)dictview_repr, /* tp_repr */
4057 &dictviews_as_number, /* tp_as_number */
4058 &dictkeys_as_sequence, /* tp_as_sequence */
4059 0, /* tp_as_mapping */
4060 0, /* tp_hash */
4061 0, /* tp_call */
4062 0, /* tp_str */
4063 PyObject_GenericGetAttr, /* tp_getattro */
4064 0, /* tp_setattro */
4065 0, /* tp_as_buffer */
4066 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4067 0, /* tp_doc */
4068 (traverseproc)dictview_traverse, /* tp_traverse */
4069 0, /* tp_clear */
4070 dictview_richcompare, /* tp_richcompare */
4071 0, /* tp_weaklistoffset */
4072 (getiterfunc)dictkeys_iter, /* tp_iter */
4073 0, /* tp_iternext */
4074 dictkeys_methods, /* tp_methods */
4075 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004076};
4077
4078static PyObject *
4079dictkeys_new(PyObject *dict)
4080{
Eric Snow96c6af92015-05-29 22:21:39 -06004081 return _PyDictView_New(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004082}
4083
Guido van Rossum3ac67412007-02-10 18:55:06 +00004084/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004085
4086static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004087dictitems_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004088{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004089 if (dv->dv_dict == NULL) {
4090 Py_RETURN_NONE;
4091 }
4092 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004093}
4094
4095static int
Eric Snow96c6af92015-05-29 22:21:39 -06004096dictitems_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004097{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004098 PyObject *key, *value, *found;
4099 if (dv->dv_dict == NULL)
4100 return 0;
4101 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
4102 return 0;
4103 key = PyTuple_GET_ITEM(obj, 0);
4104 value = PyTuple_GET_ITEM(obj, 1);
Raymond Hettinger6692f012016-09-18 21:46:08 -07004105 found = PyDict_GetItemWithError((PyObject *)dv->dv_dict, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004106 if (found == NULL) {
4107 if (PyErr_Occurred())
4108 return -1;
4109 return 0;
4110 }
4111 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004112}
4113
Guido van Rossum83825ac2007-02-10 04:54:19 +00004114static PySequenceMethods dictitems_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)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004123};
4124
Guido van Rossumb90c8482007-02-10 01:11:45 +00004125static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004126 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4127 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004128 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004129};
4130
4131PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004132 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4133 "dict_items", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004134 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004135 0, /* tp_itemsize */
4136 /* methods */
4137 (destructor)dictview_dealloc, /* tp_dealloc */
4138 0, /* tp_print */
4139 0, /* tp_getattr */
4140 0, /* tp_setattr */
4141 0, /* tp_reserved */
4142 (reprfunc)dictview_repr, /* tp_repr */
4143 &dictviews_as_number, /* tp_as_number */
4144 &dictitems_as_sequence, /* tp_as_sequence */
4145 0, /* tp_as_mapping */
4146 0, /* tp_hash */
4147 0, /* tp_call */
4148 0, /* tp_str */
4149 PyObject_GenericGetAttr, /* tp_getattro */
4150 0, /* tp_setattro */
4151 0, /* tp_as_buffer */
4152 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4153 0, /* tp_doc */
4154 (traverseproc)dictview_traverse, /* tp_traverse */
4155 0, /* tp_clear */
4156 dictview_richcompare, /* tp_richcompare */
4157 0, /* tp_weaklistoffset */
4158 (getiterfunc)dictitems_iter, /* tp_iter */
4159 0, /* tp_iternext */
4160 dictitems_methods, /* tp_methods */
4161 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004162};
4163
4164static PyObject *
4165dictitems_new(PyObject *dict)
4166{
Eric Snow96c6af92015-05-29 22:21:39 -06004167 return _PyDictView_New(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004168}
4169
Guido van Rossum3ac67412007-02-10 18:55:06 +00004170/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004171
4172static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004173dictvalues_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004174{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004175 if (dv->dv_dict == NULL) {
4176 Py_RETURN_NONE;
4177 }
4178 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004179}
4180
Guido van Rossum83825ac2007-02-10 04:54:19 +00004181static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004182 (lenfunc)dictview_len, /* sq_length */
4183 0, /* sq_concat */
4184 0, /* sq_repeat */
4185 0, /* sq_item */
4186 0, /* sq_slice */
4187 0, /* sq_ass_item */
4188 0, /* sq_ass_slice */
4189 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004190};
4191
Guido van Rossumb90c8482007-02-10 01:11:45 +00004192static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004193 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004194};
4195
4196PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004197 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4198 "dict_values", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004199 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004200 0, /* tp_itemsize */
4201 /* methods */
4202 (destructor)dictview_dealloc, /* tp_dealloc */
4203 0, /* tp_print */
4204 0, /* tp_getattr */
4205 0, /* tp_setattr */
4206 0, /* tp_reserved */
4207 (reprfunc)dictview_repr, /* tp_repr */
4208 0, /* tp_as_number */
4209 &dictvalues_as_sequence, /* tp_as_sequence */
4210 0, /* tp_as_mapping */
4211 0, /* tp_hash */
4212 0, /* tp_call */
4213 0, /* tp_str */
4214 PyObject_GenericGetAttr, /* tp_getattro */
4215 0, /* tp_setattro */
4216 0, /* tp_as_buffer */
4217 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4218 0, /* tp_doc */
4219 (traverseproc)dictview_traverse, /* tp_traverse */
4220 0, /* tp_clear */
4221 0, /* tp_richcompare */
4222 0, /* tp_weaklistoffset */
4223 (getiterfunc)dictvalues_iter, /* tp_iter */
4224 0, /* tp_iternext */
4225 dictvalues_methods, /* tp_methods */
4226 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004227};
4228
4229static PyObject *
4230dictvalues_new(PyObject *dict)
4231{
Eric Snow96c6af92015-05-29 22:21:39 -06004232 return _PyDictView_New(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004233}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004234
4235/* Returns NULL if cannot allocate a new PyDictKeysObject,
4236 but does not set an error */
4237PyDictKeysObject *
4238_PyDict_NewKeysForClass(void)
4239{
Victor Stinner742da042016-09-07 17:40:12 -07004240 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004241 if (keys == NULL)
4242 PyErr_Clear();
4243 else
4244 keys->dk_lookup = lookdict_split;
4245 return keys;
4246}
4247
4248#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
4249
4250PyObject *
4251PyObject_GenericGetDict(PyObject *obj, void *context)
4252{
4253 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
4254 if (dictptr == NULL) {
4255 PyErr_SetString(PyExc_AttributeError,
4256 "This object has no __dict__");
4257 return NULL;
4258 }
4259 dict = *dictptr;
4260 if (dict == NULL) {
4261 PyTypeObject *tp = Py_TYPE(obj);
4262 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
4263 DK_INCREF(CACHED_KEYS(tp));
4264 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
4265 }
4266 else {
4267 *dictptr = dict = PyDict_New();
4268 }
4269 }
4270 Py_XINCREF(dict);
4271 return dict;
4272}
4273
4274int
4275_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
Victor Stinner742da042016-09-07 17:40:12 -07004276 PyObject *key, PyObject *value)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004277{
4278 PyObject *dict;
4279 int res;
4280 PyDictKeysObject *cached;
4281
4282 assert(dictptr != NULL);
4283 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
4284 assert(dictptr != NULL);
4285 dict = *dictptr;
4286 if (dict == NULL) {
4287 DK_INCREF(cached);
4288 dict = new_dict_with_shared_keys(cached);
4289 if (dict == NULL)
4290 return -1;
4291 *dictptr = dict;
4292 }
4293 if (value == NULL) {
4294 res = PyDict_DelItem(dict, key);
4295 if (cached != ((PyDictObject *)dict)->ma_keys) {
4296 CACHED_KEYS(tp) = NULL;
4297 DK_DECREF(cached);
4298 }
4299 } else {
4300 res = PyDict_SetItem(dict, key, value);
4301 if (cached != ((PyDictObject *)dict)->ma_keys) {
4302 /* Either update tp->ht_cached_keys or delete it */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004303 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004304 CACHED_KEYS(tp) = make_keys_shared(dict);
Victor Stinner742da042016-09-07 17:40:12 -07004305 }
4306 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004307 CACHED_KEYS(tp) = NULL;
4308 }
4309 DK_DECREF(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004310 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
4311 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004312 }
4313 }
4314 } else {
4315 dict = *dictptr;
4316 if (dict == NULL) {
4317 dict = PyDict_New();
4318 if (dict == NULL)
4319 return -1;
4320 *dictptr = dict;
4321 }
4322 if (value == NULL) {
4323 res = PyDict_DelItem(dict, key);
4324 } else {
4325 res = PyDict_SetItem(dict, key, value);
4326 }
4327 }
4328 return res;
4329}
4330
4331void
4332_PyDictKeys_DecRef(PyDictKeysObject *keys)
4333{
4334 DK_DECREF(keys);
4335}