blob: 108c6128ab1acbcf736b31a3b720a3352372f3ea [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
Raymond Hettingerb12785d2016-10-22 09:58:14 -070013As of Python 3.6, this is compact and ordered. Basic idea is described here:
14* https://mail.python.org/pipermail/python-dev/2012-December/123028.html
15* https://morepypy.blogspot.com/2015/01/faster-more-memory-efficient-and-more.html
Victor Stinner742da042016-09-07 17:40:12 -070016
17layout:
18
19+---------------+
20| dk_refcnt |
21| dk_size |
22| dk_lookup |
23| dk_usable |
24| dk_nentries |
25+---------------+
26| dk_indices |
27| |
28+---------------+
29| dk_entries |
30| |
31+---------------+
32
33dk_indices is actual hashtable. It holds index in entries, or DKIX_EMPTY(-1)
34or DKIX_DUMMY(-2).
35Size of indices is dk_size. Type of each index in indices is vary on dk_size:
36
37* int8 for dk_size <= 128
38* int16 for 256 <= dk_size <= 2**15
39* int32 for 2**16 <= dk_size <= 2**31
40* int64 for 2**32 <= dk_size
41
42dk_entries is array of PyDictKeyEntry. It's size is USABLE_FRACTION(dk_size).
43DK_ENTRIES(dk) can be used to get pointer to entries.
44
45NOTE: Since negative value is used for DKIX_EMPTY and DKIX_DUMMY, type of
46dk_indices entry is signed integer and int16 is used for table which
47dk_size == 256.
48*/
49
Benjamin Peterson7d95e402012-04-23 11:24:50 -040050
51/*
Benjamin Peterson7d95e402012-04-23 11:24:50 -040052The DictObject can be in one of two forms.
Victor Stinner742da042016-09-07 17:40:12 -070053
Benjamin Peterson7d95e402012-04-23 11:24:50 -040054Either:
55 A combined table:
56 ma_values == NULL, dk_refcnt == 1.
57 Values are stored in the me_value field of the PyDictKeysObject.
Benjamin Peterson7d95e402012-04-23 11:24:50 -040058Or:
59 A split table:
60 ma_values != NULL, dk_refcnt >= 1
61 Values are stored in the ma_values array.
Victor Stinner742da042016-09-07 17:40:12 -070062 Only string (unicode) keys are allowed.
63 All dicts sharing same key must have same insertion order.
Benjamin Peterson7d95e402012-04-23 11:24:50 -040064
Victor Stinner742da042016-09-07 17:40:12 -070065There are four kinds of slots in the table (slot is index, and
66DK_ENTRIES(keys)[index] if index >= 0):
67
681. Unused. index == DKIX_EMPTY
69 Does not hold an active (key, value) pair now and never did. Unused can
70 transition to Active upon key insertion. This is each slot's initial state.
71
722. Active. index >= 0, me_key != NULL and me_value != NULL
73 Holds an active (key, value) pair. Active can transition to Dummy or
74 Pending upon key deletion (for combined and split tables respectively).
75 This is the only case in which me_value != NULL.
76
773. Dummy. index == DKIX_DUMMY (combined only)
78 Previously held an active (key, value) pair, but that was deleted and an
79 active pair has not yet overwritten the slot. Dummy can transition to
80 Active upon key insertion. Dummy slots cannot be made Unused again
81 else the probe sequence in case of collision would have no way to know
82 they were once active.
83
844. Pending. index >= 0, key != NULL, and value == NULL (split only)
85 Not yet inserted in split-table.
Benjamin Peterson7d95e402012-04-23 11:24:50 -040086*/
87
Victor Stinner742da042016-09-07 17:40:12 -070088/*
89Preserving insertion order
Benjamin Peterson7d95e402012-04-23 11:24:50 -040090
Victor Stinner742da042016-09-07 17:40:12 -070091It's simple for combined table. Since dk_entries is mostly append only, we can
92get insertion order by just iterating dk_entries.
93
94One exception is .popitem(). It removes last item in dk_entries and decrement
95dk_nentries to achieve amortized O(1). Since there are DKIX_DUMMY remains in
96dk_indices, we can't increment dk_usable even though dk_nentries is
97decremented.
98
99In split table, inserting into pending entry is allowed only for dk_entries[ix]
100where ix == mp->ma_used. Inserting into other index and deleting item cause
101converting the dict to the combined table.
102*/
103
104/* PyDict_MINSIZE is the starting size for any new dict.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400105 * 8 allows dicts with no more than 5 active entries; experiments suggested
106 * this suffices for the majority of dicts (consisting mostly of usually-small
107 * dicts created to pass keyword arguments).
108 * Making this 8, rather than 4 reduces the number of resizes for most
109 * dictionaries, without any significant extra memory use.
110 */
Victor Stinner742da042016-09-07 17:40:12 -0700111#define PyDict_MINSIZE 8
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400112
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000113#include "Python.h"
Victor Stinnerbcda8f12018-11-21 22:27:47 +0100114#include "pycore_object.h"
Victor Stinner621cebe2018-11-12 16:53:38 +0100115#include "pycore_pystate.h"
Eric Snow96c6af92015-05-29 22:21:39 -0600116#include "dict-common.h"
Victor Stinner990397e2016-09-09 20:22:59 -0700117#include "stringlib/eq.h" /* to get unicode_eq() */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000118
Larry Hastings61272b72014-01-07 12:41:53 -0800119/*[clinic input]
Larry Hastingsc2047262014-01-25 20:43:29 -0800120class dict "PyDictObject *" "&PyDict_Type"
Larry Hastings61272b72014-01-07 12:41:53 -0800121[clinic start generated code]*/
Larry Hastings581ee362014-01-28 05:00:08 -0800122/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f157a5a0ce9589d6]*/
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800123
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400124
125/*
126To ensure the lookup algorithm terminates, there must be at least one Unused
127slot (NULL key) in the table.
128To avoid slowing down lookups on a near-full table, we resize the table when
129it's USABLE_FRACTION (currently two-thirds) full.
130*/
Guido van Rossum16e93a81997-01-28 00:00:11 +0000131
Tim Peterseb28ef22001-06-02 05:27:19 +0000132#define PERTURB_SHIFT 5
133
Guido van Rossum16e93a81997-01-28 00:00:11 +0000134/*
Tim Peterseb28ef22001-06-02 05:27:19 +0000135Major subtleties ahead: Most hash schemes depend on having a "good" hash
136function, in the sense of simulating randomness. Python doesn't: its most
R David Murray537ad7a2016-07-10 12:33:18 -0400137important hash functions (for ints) are very regular in common
Tim Peterseb28ef22001-06-02 05:27:19 +0000138cases:
Tim Peters15d49292001-05-27 07:39:22 +0000139
R David Murray537ad7a2016-07-10 12:33:18 -0400140 >>>[hash(i) for i in range(4)]
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000141 [0, 1, 2, 3]
Tim Peters15d49292001-05-27 07:39:22 +0000142
Tim Peterseb28ef22001-06-02 05:27:19 +0000143This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
144the low-order i bits as the initial table index is extremely fast, and there
R David Murray537ad7a2016-07-10 12:33:18 -0400145are no collisions at all for dicts indexed by a contiguous range of ints. So
146this gives better-than-random behavior in common cases, and that's very
147desirable.
Tim Peters15d49292001-05-27 07:39:22 +0000148
Tim Peterseb28ef22001-06-02 05:27:19 +0000149OTOH, when collisions occur, the tendency to fill contiguous slices of the
150hash table makes a good collision resolution strategy crucial. Taking only
151the last i bits of the hash code is also vulnerable: for example, consider
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152the list [i << 16 for i in range(20000)] as a set of keys. Since ints are
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000153their own hash codes, and this fits in a dict of size 2**15, the last 15 bits
154 of every hash code are all 0: they *all* map to the same table index.
Tim Peters15d49292001-05-27 07:39:22 +0000155
Tim Peterseb28ef22001-06-02 05:27:19 +0000156But catering to unusual cases should not slow the usual ones, so we just take
157the last i bits anyway. It's up to collision resolution to do the rest. If
158we *usually* find the key we're looking for on the first try (and, it turns
159out, we usually do -- the table load factor is kept under 2/3, so the odds
160are solidly in our favor), then it makes best sense to keep the initial index
161computation dirt cheap.
Tim Peters15d49292001-05-27 07:39:22 +0000162
Tim Peterseb28ef22001-06-02 05:27:19 +0000163The first half of collision resolution is to visit table indices via this
164recurrence:
Tim Peters15d49292001-05-27 07:39:22 +0000165
Tim Peterseb28ef22001-06-02 05:27:19 +0000166 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +0000167
Tim Peterseb28ef22001-06-02 05:27:19 +0000168For any initial j in range(2**i), repeating that 2**i times generates each
169int in range(2**i) exactly once (see any text on random-number generation for
170proof). By itself, this doesn't help much: like linear probing (setting
171j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
172order. This would be bad, except that's not the only thing we do, and it's
173actually *good* in the common cases where hash keys are consecutive. In an
174example that's really too small to make this entirely clear, for a table of
175size 2**3 the order of indices is:
Tim Peters15d49292001-05-27 07:39:22 +0000176
Tim Peterseb28ef22001-06-02 05:27:19 +0000177 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
178
179If two things come in at index 5, the first place we look after is index 2,
180not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
181Linear probing is deadly in this case because there the fixed probe order
182is the *same* as the order consecutive keys are likely to arrive. But it's
183extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
184and certain that consecutive hash codes do not.
185
186The other half of the strategy is to get the other bits of the hash code
187into play. This is done by initializing a (unsigned) vrbl "perturb" to the
188full hash code, and changing the recurrence to:
189
Tim Peterseb28ef22001-06-02 05:27:19 +0000190 perturb >>= PERTURB_SHIFT;
INADA Naoki267941c2016-10-06 15:19:07 +0900191 j = (5*j) + 1 + perturb;
Tim Peterseb28ef22001-06-02 05:27:19 +0000192 use j % 2**i as the next table index;
193
194Now the probe sequence depends (eventually) on every bit in the hash code,
195and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
196because it quickly magnifies small differences in the bits that didn't affect
197the initial index. Note that because perturb is unsigned, if the recurrence
198is executed often enough perturb eventually becomes and remains 0. At that
199point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
200that's certain to find an empty slot eventually (since it generates every int
201in range(2**i), and we make sure there's always at least one empty slot).
202
203Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
204small so that the high bits of the hash code continue to affect the probe
205sequence across iterations; but you want it large so that in really bad cases
206the high-order hash bits have an effect on early iterations. 5 was "the
207best" in minimizing total collisions across experiments Tim Peters ran (on
208both normal and pathological cases), but 4 and 6 weren't significantly worse.
209
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000210Historical: Reimer Behrends contributed the idea of using a polynomial-based
Tim Peterseb28ef22001-06-02 05:27:19 +0000211approach, using repeated multiplication by x in GF(2**n) where an irreducible
212polynomial for each table size was chosen such that x was a primitive root.
213Christian Tismer later extended that to use division by x instead, as an
214efficient way to get the high bits of the hash code into play. This scheme
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000215also gave excellent collision statistics, but was more expensive: two
216if-tests were required inside the loop; computing "the next" index took about
217the same number of operations but without as much potential parallelism
218(e.g., computing 5*j can go on at the same time as computing 1+perturb in the
219above, and then shifting perturb can be done while the table index is being
220masked); and the PyDictObject struct required a member to hold the table's
221polynomial. In Tim's experiments the current scheme ran faster, produced
222equally good collision statistics, needed less code & used less memory.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000223
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000224*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000225
Fred Drake1bff34a2000-08-31 19:31:38 +0000226/* forward declarations */
Victor Stinner742da042016-09-07 17:40:12 -0700227static Py_ssize_t lookdict(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900228 Py_hash_t hash, PyObject **value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700229static Py_ssize_t lookdict_unicode(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900230 Py_hash_t hash, PyObject **value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700231static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400232lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900233 Py_hash_t hash, PyObject **value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700234static Py_ssize_t lookdict_split(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900235 Py_hash_t hash, PyObject **value_addr);
Fred Drake1bff34a2000-08-31 19:31:38 +0000236
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400237static int dictresize(PyDictObject *mp, Py_ssize_t minused);
Tim Petersdea48ec2001-05-22 20:40:22 +0000238
INADA Naoki2aaf98c2018-09-26 12:59:00 +0900239static PyObject* dict_iter(PyDictObject *dict);
240
Benjamin Peterson3c569292016-09-08 13:16:41 -0700241/*Global counter used to set ma_version_tag field of dictionary.
Victor Stinner3b6a6b42016-09-08 12:51:24 -0700242 * It is incremented each time that a dictionary is created and each
243 * time that a dictionary is modified. */
244static uint64_t pydict_global_version = 0;
245
246#define DICT_NEXT_VERSION() (++pydict_global_version)
247
Victor Stinner742da042016-09-07 17:40:12 -0700248/* Dictionary reuse scheme to save calls to malloc and free */
Christian Heimes2202f872008-02-06 14:31:34 +0000249#ifndef PyDict_MAXFREELIST
250#define PyDict_MAXFREELIST 80
251#endif
252static PyDictObject *free_list[PyDict_MAXFREELIST];
253static int numfree = 0;
Victor Stinner742da042016-09-07 17:40:12 -0700254static PyDictKeysObject *keys_free_list[PyDict_MAXFREELIST];
255static int numfreekeys = 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000256
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300257#include "clinic/dictobject.c.h"
258
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100259int
260PyDict_ClearFreeList(void)
Christian Heimes77c02eb2008-02-09 02:18:51 +0000261{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 PyDictObject *op;
Victor Stinner742da042016-09-07 17:40:12 -0700263 int ret = numfree + numfreekeys;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 while (numfree) {
265 op = free_list[--numfree];
266 assert(PyDict_CheckExact(op));
267 PyObject_GC_Del(op);
268 }
Victor Stinner742da042016-09-07 17:40:12 -0700269 while (numfreekeys) {
270 PyObject_FREE(keys_free_list[--numfreekeys]);
271 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100272 return ret;
273}
274
David Malcolm49526f42012-06-22 14:55:41 -0400275/* Print summary info about the state of the optimized allocator */
276void
277_PyDict_DebugMallocStats(FILE *out)
278{
279 _PyDebugAllocatorStats(out,
280 "free PyDictObject", numfree, sizeof(PyDictObject));
281}
282
283
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100284void
285PyDict_Fini(void)
286{
287 PyDict_ClearFreeList();
Christian Heimes77c02eb2008-02-09 02:18:51 +0000288}
289
Victor Stinner742da042016-09-07 17:40:12 -0700290#define DK_SIZE(dk) ((dk)->dk_size)
291#if SIZEOF_VOID_P > 4
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700292#define DK_IXSIZE(dk) \
293 (DK_SIZE(dk) <= 0xff ? \
294 1 : DK_SIZE(dk) <= 0xffff ? \
295 2 : DK_SIZE(dk) <= 0xffffffff ? \
Benjamin Peterson3c569292016-09-08 13:16:41 -0700296 4 : sizeof(int64_t))
Victor Stinner742da042016-09-07 17:40:12 -0700297#else
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700298#define DK_IXSIZE(dk) \
299 (DK_SIZE(dk) <= 0xff ? \
300 1 : DK_SIZE(dk) <= 0xffff ? \
Benjamin Peterson3c569292016-09-08 13:16:41 -0700301 2 : sizeof(int32_t))
Victor Stinner742da042016-09-07 17:40:12 -0700302#endif
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700303#define DK_ENTRIES(dk) \
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700304 ((PyDictKeyEntry*)(&((int8_t*)((dk)->dk_indices))[DK_SIZE(dk) * DK_IXSIZE(dk)]))
Victor Stinner742da042016-09-07 17:40:12 -0700305
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400306#define DK_MASK(dk) (((dk)->dk_size)-1)
307#define IS_POWER_OF_2(x) (((x) & (x-1)) == 0)
308
INADA Naokia7576492018-11-14 18:39:27 +0900309static void free_keys_object(PyDictKeysObject *keys);
310
311static inline void
312dictkeys_incref(PyDictKeysObject *dk)
313{
314 _Py_INC_REFTOTAL;
315 dk->dk_refcnt++;
316}
317
318static inline void
319dictkeys_decref(PyDictKeysObject *dk)
320{
321 assert(dk->dk_refcnt > 0);
322 _Py_DEC_REFTOTAL;
323 if (--dk->dk_refcnt == 0) {
324 free_keys_object(dk);
325 }
326}
327
Victor Stinner742da042016-09-07 17:40:12 -0700328/* lookup indices. returns DKIX_EMPTY, DKIX_DUMMY, or ix >=0 */
Benjamin Peterson73222252016-09-08 09:58:47 -0700329static inline Py_ssize_t
INADA Naokia7576492018-11-14 18:39:27 +0900330dictkeys_get_index(PyDictKeysObject *keys, Py_ssize_t i)
Victor Stinner742da042016-09-07 17:40:12 -0700331{
332 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700333 Py_ssize_t ix;
334
Victor Stinner742da042016-09-07 17:40:12 -0700335 if (s <= 0xff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700336 int8_t *indices = (int8_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700337 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700338 }
339 else if (s <= 0xffff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700340 int16_t *indices = (int16_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700341 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700342 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700343#if SIZEOF_VOID_P > 4
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300344 else if (s > 0xffffffff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700345 int64_t *indices = (int64_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700346 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700347 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700348#endif
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300349 else {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700350 int32_t *indices = (int32_t*)(keys->dk_indices);
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300351 ix = indices[i];
352 }
Victor Stinner71211e32016-09-08 10:52:46 -0700353 assert(ix >= DKIX_DUMMY);
354 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700355}
356
357/* write to indices. */
Benjamin Peterson73222252016-09-08 09:58:47 -0700358static inline void
INADA Naokia7576492018-11-14 18:39:27 +0900359dictkeys_set_index(PyDictKeysObject *keys, Py_ssize_t i, Py_ssize_t ix)
Victor Stinner742da042016-09-07 17:40:12 -0700360{
361 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700362
363 assert(ix >= DKIX_DUMMY);
364
Victor Stinner742da042016-09-07 17:40:12 -0700365 if (s <= 0xff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700366 int8_t *indices = (int8_t*)(keys->dk_indices);
Victor Stinner71211e32016-09-08 10:52:46 -0700367 assert(ix <= 0x7f);
Victor Stinner208857e2016-09-08 11:35:46 -0700368 indices[i] = (char)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700369 }
370 else if (s <= 0xffff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700371 int16_t *indices = (int16_t*)(keys->dk_indices);
Victor Stinner71211e32016-09-08 10:52:46 -0700372 assert(ix <= 0x7fff);
Victor Stinner208857e2016-09-08 11:35:46 -0700373 indices[i] = (int16_t)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700374 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700375#if SIZEOF_VOID_P > 4
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300376 else if (s > 0xffffffff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700377 int64_t *indices = (int64_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700378 indices[i] = ix;
Victor Stinner742da042016-09-07 17:40:12 -0700379 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700380#endif
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300381 else {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700382 int32_t *indices = (int32_t*)(keys->dk_indices);
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300383 assert(ix <= 0x7fffffff);
384 indices[i] = (int32_t)ix;
385 }
Victor Stinner742da042016-09-07 17:40:12 -0700386}
387
388
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200389/* USABLE_FRACTION is the maximum dictionary load.
Victor Stinner742da042016-09-07 17:40:12 -0700390 * Increasing this ratio makes dictionaries more dense resulting in more
391 * collisions. Decreasing it improves sparseness at the expense of spreading
392 * indices over more cache lines and at the cost of total memory consumed.
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200393 *
394 * USABLE_FRACTION must obey the following:
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400395 * (0 < USABLE_FRACTION(n) < n) for all n >= 2
396 *
Victor Stinner742da042016-09-07 17:40:12 -0700397 * USABLE_FRACTION should be quick to calculate.
398 * Fractions around 1/2 to 2/3 seem to work well in practice.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400399 */
Victor Stinner742da042016-09-07 17:40:12 -0700400#define USABLE_FRACTION(n) (((n) << 1)/3)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400401
Victor Stinner742da042016-09-07 17:40:12 -0700402/* ESTIMATE_SIZE is reverse function of USABLE_FRACTION.
403 * This can be used to reserve enough size to insert n entries without
404 * resizing.
405 */
INADA Naoki92c50ee2016-11-22 00:57:02 +0900406#define ESTIMATE_SIZE(n) (((n)*3+1) >> 1)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400407
Victor Stinner742da042016-09-07 17:40:12 -0700408/* Alternative fraction that is otherwise close enough to 2n/3 to make
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400409 * little difference. 8 * 2/3 == 8 * 5/8 == 5. 16 * 2/3 == 16 * 5/8 == 10.
410 * 32 * 2/3 = 21, 32 * 5/8 = 20.
411 * Its advantage is that it is faster to compute on machines with slow division.
412 * #define USABLE_FRACTION(n) (((n) >> 1) + ((n) >> 2) - ((n) >> 3))
Victor Stinner742da042016-09-07 17:40:12 -0700413 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400414
Victor Stinnera9f61a52013-07-16 22:17:26 +0200415/* GROWTH_RATE. Growth rate upon hitting maximum load.
INADA Naoki5fbc5112018-04-17 15:53:34 +0900416 * Currently set to used*3.
Victor Stinnera9f61a52013-07-16 22:17:26 +0200417 * This means that dicts double in size when growing without deletions,
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700418 * but have more head room when the number of deletions is on a par with the
INADA Naoki5fbc5112018-04-17 15:53:34 +0900419 * number of insertions. See also bpo-17563 and bpo-33205.
420 *
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700421 * GROWTH_RATE was set to used*4 up to version 3.2.
422 * GROWTH_RATE was set to used*2 in version 3.3.0
INADA Naoki5fbc5112018-04-17 15:53:34 +0900423 * GROWTH_RATE was set to used*2 + capacity/2 in 3.4.0-3.6.0.
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200424 */
INADA Naoki5fbc5112018-04-17 15:53:34 +0900425#define GROWTH_RATE(d) ((d)->ma_used*3)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400426
427#define ENSURE_ALLOWS_DELETIONS(d) \
428 if ((d)->ma_keys->dk_lookup == lookdict_unicode_nodummy) { \
429 (d)->ma_keys->dk_lookup = lookdict_unicode; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400431
432/* This immutable, empty PyDictKeysObject is used for PyDict_Clear()
433 * (which cannot fail and thus can do no allocation).
434 */
435static PyDictKeysObject empty_keys_struct = {
Serhiy Storchaka97932e42016-09-26 23:01:23 +0300436 1, /* dk_refcnt */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400437 1, /* dk_size */
438 lookdict_split, /* dk_lookup */
439 0, /* dk_usable (immutable) */
Victor Stinner742da042016-09-07 17:40:12 -0700440 0, /* dk_nentries */
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700441 {DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY,
442 DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY}, /* dk_indices */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400443};
444
445static PyObject *empty_values[1] = { NULL };
446
447#define Py_EMPTY_KEYS &empty_keys_struct
448
Victor Stinner611b0fa2016-09-14 15:02:01 +0200449/* Uncomment to check the dict content in _PyDict_CheckConsistency() */
450/* #define DEBUG_PYDICT */
451
452
T. Woutersa00c3fd2017-03-31 09:14:41 -0700453#ifndef NDEBUG
Victor Stinner611b0fa2016-09-14 15:02:01 +0200454static int
455_PyDict_CheckConsistency(PyDictObject *mp)
456{
Victor Stinner50fe3f82018-10-26 18:47:15 +0200457#define ASSERT(expr) _PyObject_ASSERT((PyObject *)mp, (expr))
458
Victor Stinner611b0fa2016-09-14 15:02:01 +0200459 PyDictKeysObject *keys = mp->ma_keys;
460 int splitted = _PyDict_HasSplitTable(mp);
461 Py_ssize_t usable = USABLE_FRACTION(keys->dk_size);
462#ifdef DEBUG_PYDICT
463 PyDictKeyEntry *entries = DK_ENTRIES(keys);
464 Py_ssize_t i;
465#endif
466
Victor Stinner50fe3f82018-10-26 18:47:15 +0200467 ASSERT(0 <= mp->ma_used && mp->ma_used <= usable);
468 ASSERT(IS_POWER_OF_2(keys->dk_size));
469 ASSERT(0 <= keys->dk_usable
Victor Stinner611b0fa2016-09-14 15:02:01 +0200470 && keys->dk_usable <= usable);
Victor Stinner50fe3f82018-10-26 18:47:15 +0200471 ASSERT(0 <= keys->dk_nentries
Victor Stinner611b0fa2016-09-14 15:02:01 +0200472 && keys->dk_nentries <= usable);
Victor Stinner50fe3f82018-10-26 18:47:15 +0200473 ASSERT(keys->dk_usable + keys->dk_nentries <= usable);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200474
475 if (!splitted) {
476 /* combined table */
Victor Stinner50fe3f82018-10-26 18:47:15 +0200477 ASSERT(keys->dk_refcnt == 1);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200478 }
479
480#ifdef DEBUG_PYDICT
481 for (i=0; i < keys->dk_size; i++) {
INADA Naokia7576492018-11-14 18:39:27 +0900482 Py_ssize_t ix = dictkeys_get_index(keys, i);
Victor Stinner50fe3f82018-10-26 18:47:15 +0200483 ASSERT(DKIX_DUMMY <= ix && ix <= usable);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200484 }
485
486 for (i=0; i < usable; i++) {
487 PyDictKeyEntry *entry = &entries[i];
488 PyObject *key = entry->me_key;
489
490 if (key != NULL) {
491 if (PyUnicode_CheckExact(key)) {
492 Py_hash_t hash = ((PyASCIIObject *)key)->hash;
Victor Stinner50fe3f82018-10-26 18:47:15 +0200493 ASSERT(hash != -1);
494 ASSERT(entry->me_hash == hash);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200495 }
496 else {
497 /* test_dict fails if PyObject_Hash() is called again */
Victor Stinner50fe3f82018-10-26 18:47:15 +0200498 ASSERT(entry->me_hash != -1);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200499 }
500 if (!splitted) {
Victor Stinner50fe3f82018-10-26 18:47:15 +0200501 ASSERT(entry->me_value != NULL);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200502 }
503 }
504
505 if (splitted) {
Victor Stinner50fe3f82018-10-26 18:47:15 +0200506 ASSERT(entry->me_value == NULL);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200507 }
508 }
509
510 if (splitted) {
511 /* splitted table */
512 for (i=0; i < mp->ma_used; i++) {
Victor Stinner50fe3f82018-10-26 18:47:15 +0200513 ASSERT(mp->ma_values[i] != NULL);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200514 }
515 }
516#endif
517
518 return 1;
Victor Stinner50fe3f82018-10-26 18:47:15 +0200519
520#undef ASSERT
Victor Stinner611b0fa2016-09-14 15:02:01 +0200521}
522#endif
523
524
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400525static PyDictKeysObject *new_keys_object(Py_ssize_t size)
526{
527 PyDictKeysObject *dk;
Victor Stinner742da042016-09-07 17:40:12 -0700528 Py_ssize_t es, usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400529
Victor Stinner742da042016-09-07 17:40:12 -0700530 assert(size >= PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400531 assert(IS_POWER_OF_2(size));
Victor Stinner742da042016-09-07 17:40:12 -0700532
533 usable = USABLE_FRACTION(size);
534 if (size <= 0xff) {
535 es = 1;
536 }
537 else if (size <= 0xffff) {
538 es = 2;
539 }
540#if SIZEOF_VOID_P > 4
541 else if (size <= 0xffffffff) {
542 es = 4;
543 }
544#endif
545 else {
546 es = sizeof(Py_ssize_t);
547 }
548
549 if (size == PyDict_MINSIZE && numfreekeys > 0) {
550 dk = keys_free_list[--numfreekeys];
551 }
552 else {
Victor Stinner98ee9d52016-09-08 09:33:56 -0700553 dk = PyObject_MALLOC(sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -0700554 + es * size
555 + sizeof(PyDictKeyEntry) * usable);
Victor Stinner742da042016-09-07 17:40:12 -0700556 if (dk == NULL) {
557 PyErr_NoMemory();
558 return NULL;
559 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400560 }
INADA Naokia7576492018-11-14 18:39:27 +0900561 _Py_INC_REFTOTAL;
562 dk->dk_refcnt = 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400563 dk->dk_size = size;
Victor Stinner742da042016-09-07 17:40:12 -0700564 dk->dk_usable = usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400565 dk->dk_lookup = lookdict_unicode_nodummy;
Victor Stinner742da042016-09-07 17:40:12 -0700566 dk->dk_nentries = 0;
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700567 memset(&dk->dk_indices[0], 0xff, es * size);
Victor Stinner742da042016-09-07 17:40:12 -0700568 memset(DK_ENTRIES(dk), 0, sizeof(PyDictKeyEntry) * usable);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400569 return dk;
570}
571
572static void
573free_keys_object(PyDictKeysObject *keys)
574{
Victor Stinner742da042016-09-07 17:40:12 -0700575 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400576 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -0700577 for (i = 0, n = keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400578 Py_XDECREF(entries[i].me_key);
579 Py_XDECREF(entries[i].me_value);
580 }
Victor Stinner742da042016-09-07 17:40:12 -0700581 if (keys->dk_size == PyDict_MINSIZE && numfreekeys < PyDict_MAXFREELIST) {
582 keys_free_list[numfreekeys++] = keys;
583 return;
584 }
Raymond Hettingerce5179f2016-01-31 08:56:21 -0800585 PyObject_FREE(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400586}
587
588#define new_values(size) PyMem_NEW(PyObject *, size)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400589#define free_values(values) PyMem_FREE(values)
590
591/* Consumes a reference to the keys object */
592static PyObject *
593new_dict(PyDictKeysObject *keys, PyObject **values)
594{
595 PyDictObject *mp;
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200596 assert(keys != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 if (numfree) {
598 mp = free_list[--numfree];
599 assert (mp != NULL);
600 assert (Py_TYPE(mp) == &PyDict_Type);
601 _Py_NewReference((PyObject *)mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400603 else {
604 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
605 if (mp == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +0900606 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400607 free_values(values);
608 return NULL;
609 }
610 }
611 mp->ma_keys = keys;
612 mp->ma_values = values;
613 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -0700614 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +0200615 assert(_PyDict_CheckConsistency(mp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000617}
618
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400619/* Consumes a reference to the keys object */
620static PyObject *
621new_dict_with_shared_keys(PyDictKeysObject *keys)
622{
623 PyObject **values;
624 Py_ssize_t i, size;
625
Victor Stinner742da042016-09-07 17:40:12 -0700626 size = USABLE_FRACTION(DK_SIZE(keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400627 values = new_values(size);
628 if (values == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +0900629 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400630 return PyErr_NoMemory();
631 }
632 for (i = 0; i < size; i++) {
633 values[i] = NULL;
634 }
635 return new_dict(keys, values);
636}
637
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500638
639static PyObject *
640clone_combined_dict(PyDictObject *orig)
641{
642 assert(PyDict_CheckExact(orig));
643 assert(orig->ma_values == NULL);
644 assert(orig->ma_keys->dk_refcnt == 1);
645
646 Py_ssize_t keys_size = _PyDict_KeysSize(orig->ma_keys);
647 PyDictKeysObject *keys = PyObject_Malloc(keys_size);
648 if (keys == NULL) {
649 PyErr_NoMemory();
650 return NULL;
651 }
652
653 memcpy(keys, orig->ma_keys, keys_size);
654
655 /* After copying key/value pairs, we need to incref all
656 keys and values and they are about to be co-owned by a
657 new dict object. */
658 PyDictKeyEntry *ep0 = DK_ENTRIES(keys);
659 Py_ssize_t n = keys->dk_nentries;
660 for (Py_ssize_t i = 0; i < n; i++) {
661 PyDictKeyEntry *entry = &ep0[i];
662 PyObject *value = entry->me_value;
663 if (value != NULL) {
664 Py_INCREF(value);
665 Py_INCREF(entry->me_key);
666 }
667 }
668
669 PyDictObject *new = (PyDictObject *)new_dict(keys, NULL);
670 if (new == NULL) {
671 /* In case of an error, `new_dict()` takes care of
672 cleaning up `keys`. */
673 return NULL;
674 }
675 new->ma_used = orig->ma_used;
676 assert(_PyDict_CheckConsistency(new));
677 if (_PyObject_GC_IS_TRACKED(orig)) {
678 /* Maintain tracking. */
679 _PyObject_GC_TRACK(new);
680 }
Yury Selivanov0b752282018-07-06 12:20:07 -0400681
682 /* Since we copied the keys table we now have an extra reference
683 in the system. Manually call _Py_INC_REFTOTAL to signal that
INADA Naokia7576492018-11-14 18:39:27 +0900684 we have it now; calling dictkeys_incref would be an error as
Yury Selivanov0b752282018-07-06 12:20:07 -0400685 keys->dk_refcnt is already set to 1 (after memcpy). */
686 _Py_INC_REFTOTAL;
687
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500688 return (PyObject *)new;
689}
690
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400691PyObject *
692PyDict_New(void)
693{
Inada Naokif2a18672019-03-12 17:25:44 +0900694 dictkeys_incref(Py_EMPTY_KEYS);
695 return new_dict(Py_EMPTY_KEYS, empty_values);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400696}
697
Victor Stinner742da042016-09-07 17:40:12 -0700698/* Search index of hash table from offset of entry table */
699static Py_ssize_t
700lookdict_index(PyDictKeysObject *k, Py_hash_t hash, Py_ssize_t index)
701{
Victor Stinner742da042016-09-07 17:40:12 -0700702 size_t mask = DK_MASK(k);
INADA Naoki073ae482017-06-23 15:22:50 +0900703 size_t perturb = (size_t)hash;
704 size_t i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700705
INADA Naoki073ae482017-06-23 15:22:50 +0900706 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900707 Py_ssize_t ix = dictkeys_get_index(k, i);
Victor Stinner742da042016-09-07 17:40:12 -0700708 if (ix == index) {
709 return i;
710 }
711 if (ix == DKIX_EMPTY) {
712 return DKIX_EMPTY;
713 }
INADA Naoki073ae482017-06-23 15:22:50 +0900714 perturb >>= PERTURB_SHIFT;
715 i = mask & (i*5 + perturb + 1);
Victor Stinner742da042016-09-07 17:40:12 -0700716 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700717 Py_UNREACHABLE();
Victor Stinner742da042016-09-07 17:40:12 -0700718}
719
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000720/*
721The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000722This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000723Open addressing is preferred over chaining since the link overhead for
724chaining would be substantial (100% with typical malloc overhead).
725
Tim Peterseb28ef22001-06-02 05:27:19 +0000726The initial probe index is computed as hash mod the table size. Subsequent
727probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000728
729All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000730
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000731The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000732contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000733Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000734
Victor Stinner742da042016-09-07 17:40:12 -0700735lookdict() is general-purpose, and may return DKIX_ERROR if (and only if) a
Victor Stinnera4348cc2016-09-08 12:01:25 -0700736comparison raises an exception.
Guido van Rossum89d8c602007-09-18 17:26:56 +0000737lookdict_unicode() below is specialized to string keys, comparison of which can
INADA Naoki1b8df102017-02-20 22:48:10 +0900738never raise an exception; that function can never return DKIX_ERROR when key
739is string. Otherwise, it falls back to lookdict().
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400740lookdict_unicode_nodummy is further specialized for string keys that cannot be
741the <dummy> value.
INADA Naoki778928b2017-08-03 23:45:15 +0900742For both, when the key isn't found a DKIX_EMPTY is returned.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000743*/
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100744static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400745lookdict(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900746 Py_hash_t hash, PyObject **value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000747{
INADA Naoki778928b2017-08-03 23:45:15 +0900748 size_t i, mask, perturb;
Victor Stinner742da042016-09-07 17:40:12 -0700749 PyDictKeysObject *dk;
INADA Naoki778928b2017-08-03 23:45:15 +0900750 PyDictKeyEntry *ep0;
Tim Peterseb28ef22001-06-02 05:27:19 +0000751
Antoine Pitrou9a234902012-05-13 20:48:01 +0200752top:
Victor Stinner742da042016-09-07 17:40:12 -0700753 dk = mp->ma_keys;
Victor Stinner742da042016-09-07 17:40:12 -0700754 ep0 = DK_ENTRIES(dk);
INADA Naoki778928b2017-08-03 23:45:15 +0900755 mask = DK_MASK(dk);
756 perturb = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700758
INADA Naoki778928b2017-08-03 23:45:15 +0900759 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900760 Py_ssize_t ix = dictkeys_get_index(dk, i);
Victor Stinner742da042016-09-07 17:40:12 -0700761 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700762 *value_addr = NULL;
763 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400764 }
INADA Naoki778928b2017-08-03 23:45:15 +0900765 if (ix >= 0) {
766 PyDictKeyEntry *ep = &ep0[ix];
767 assert(ep->me_key != NULL);
768 if (ep->me_key == key) {
769 *value_addr = ep->me_value;
770 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700771 }
INADA Naoki778928b2017-08-03 23:45:15 +0900772 if (ep->me_hash == hash) {
773 PyObject *startkey = ep->me_key;
774 Py_INCREF(startkey);
775 int cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
776 Py_DECREF(startkey);
777 if (cmp < 0) {
778 *value_addr = NULL;
779 return DKIX_ERROR;
780 }
781 if (dk == mp->ma_keys && ep->me_key == startkey) {
782 if (cmp > 0) {
783 *value_addr = ep->me_value;
784 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700785 }
INADA Naoki778928b2017-08-03 23:45:15 +0900786 }
787 else {
788 /* The dict was mutated, restart */
789 goto top;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400790 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 }
INADA Naoki778928b2017-08-03 23:45:15 +0900793 perturb >>= PERTURB_SHIFT;
794 i = (i*5 + perturb + 1) & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700796 Py_UNREACHABLE();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000797}
798
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400799/* Specialized version for string-only keys */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100800static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400801lookdict_unicode(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900802 Py_hash_t hash, PyObject **value_addr)
Fred Drake1bff34a2000-08-31 19:31:38 +0000803{
Victor Stinner742da042016-09-07 17:40:12 -0700804 assert(mp->ma_values == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 /* Make sure this function doesn't have to handle non-unicode keys,
806 including subclasses of str; e.g., one reason to subclass
807 unicodes is to override __eq__, and for speed we don't cater to
808 that here. */
809 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400810 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900811 return lookdict(mp, key, hash, value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 }
Tim Peters15d49292001-05-27 07:39:22 +0000813
INADA Naoki778928b2017-08-03 23:45:15 +0900814 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
815 size_t mask = DK_MASK(mp->ma_keys);
816 size_t perturb = (size_t)hash;
817 size_t i = (size_t)hash & mask;
818
819 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900820 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
Victor Stinner742da042016-09-07 17:40:12 -0700821 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700822 *value_addr = NULL;
823 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400824 }
INADA Naoki778928b2017-08-03 23:45:15 +0900825 if (ix >= 0) {
826 PyDictKeyEntry *ep = &ep0[ix];
827 assert(ep->me_key != NULL);
828 assert(PyUnicode_CheckExact(ep->me_key));
829 if (ep->me_key == key ||
830 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
831 *value_addr = ep->me_value;
832 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700833 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400834 }
INADA Naoki778928b2017-08-03 23:45:15 +0900835 perturb >>= PERTURB_SHIFT;
836 i = mask & (i*5 + perturb + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700838 Py_UNREACHABLE();
Fred Drake1bff34a2000-08-31 19:31:38 +0000839}
840
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400841/* Faster version of lookdict_unicode when it is known that no <dummy> keys
842 * will be present. */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100843static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400844lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900845 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400846{
Victor Stinner742da042016-09-07 17:40:12 -0700847 assert(mp->ma_values == NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400848 /* Make sure this function doesn't have to handle non-unicode keys,
849 including subclasses of str; e.g., one reason to subclass
850 unicodes is to override __eq__, and for speed we don't cater to
851 that here. */
852 if (!PyUnicode_CheckExact(key)) {
853 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900854 return lookdict(mp, key, hash, value_addr);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400855 }
INADA Naoki778928b2017-08-03 23:45:15 +0900856
857 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
858 size_t mask = DK_MASK(mp->ma_keys);
859 size_t perturb = (size_t)hash;
860 size_t i = (size_t)hash & mask;
861
862 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900863 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
Victor Stinner742da042016-09-07 17:40:12 -0700864 assert (ix != DKIX_DUMMY);
865 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700866 *value_addr = NULL;
867 return DKIX_EMPTY;
868 }
INADA Naoki778928b2017-08-03 23:45:15 +0900869 PyDictKeyEntry *ep = &ep0[ix];
870 assert(ep->me_key != NULL);
871 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700872 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400873 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900874 *value_addr = ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700875 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400876 }
INADA Naoki778928b2017-08-03 23:45:15 +0900877 perturb >>= PERTURB_SHIFT;
878 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400879 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700880 Py_UNREACHABLE();
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400881}
882
883/* Version of lookdict for split tables.
884 * All split tables and only split tables use this lookup function.
885 * Split tables only contain unicode keys and no dummy keys,
886 * so algorithm is the same as lookdict_unicode_nodummy.
887 */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100888static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400889lookdict_split(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900890 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400891{
Victor Stinner742da042016-09-07 17:40:12 -0700892 /* mp must split table */
893 assert(mp->ma_values != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400894 if (!PyUnicode_CheckExact(key)) {
INADA Naoki778928b2017-08-03 23:45:15 +0900895 Py_ssize_t ix = lookdict(mp, key, hash, value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700896 if (ix >= 0) {
INADA Naokiba609772016-12-07 20:41:42 +0900897 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700898 }
899 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400900 }
Victor Stinner742da042016-09-07 17:40:12 -0700901
INADA Naoki778928b2017-08-03 23:45:15 +0900902 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
903 size_t mask = DK_MASK(mp->ma_keys);
904 size_t perturb = (size_t)hash;
905 size_t i = (size_t)hash & mask;
906
907 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900908 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
INADA Naoki778928b2017-08-03 23:45:15 +0900909 assert (ix != DKIX_DUMMY);
Victor Stinner742da042016-09-07 17:40:12 -0700910 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700911 *value_addr = NULL;
912 return DKIX_EMPTY;
913 }
INADA Naoki778928b2017-08-03 23:45:15 +0900914 PyDictKeyEntry *ep = &ep0[ix];
915 assert(ep->me_key != NULL);
916 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700917 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400918 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900919 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700920 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400921 }
INADA Naoki778928b2017-08-03 23:45:15 +0900922 perturb >>= PERTURB_SHIFT;
923 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400924 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700925 Py_UNREACHABLE();
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400926}
927
Benjamin Petersonfb886362010-04-24 18:21:17 +0000928int
929_PyDict_HasOnlyStringKeys(PyObject *dict)
930{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 Py_ssize_t pos = 0;
932 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000933 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400935 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 return 1;
937 while (PyDict_Next(dict, &pos, &key, &value))
938 if (!PyUnicode_Check(key))
939 return 0;
940 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000941}
942
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000943#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 do { \
945 if (!_PyObject_GC_IS_TRACKED(mp)) { \
946 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
947 _PyObject_GC_MAY_BE_TRACKED(value)) { \
948 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 } \
950 } \
951 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000952
953void
954_PyDict_MaybeUntrack(PyObject *op)
955{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 PyDictObject *mp;
957 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -0700958 Py_ssize_t i, numentries;
959 PyDictKeyEntry *ep0;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
962 return;
963
964 mp = (PyDictObject *) op;
Victor Stinner742da042016-09-07 17:40:12 -0700965 ep0 = DK_ENTRIES(mp->ma_keys);
966 numentries = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400967 if (_PyDict_HasSplitTable(mp)) {
Victor Stinner742da042016-09-07 17:40:12 -0700968 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400969 if ((value = mp->ma_values[i]) == NULL)
970 continue;
971 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
Victor Stinner742da042016-09-07 17:40:12 -0700972 assert(!_PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400973 return;
974 }
975 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400977 else {
Victor Stinner742da042016-09-07 17:40:12 -0700978 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400979 if ((value = ep0[i].me_value) == NULL)
980 continue;
981 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
982 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
983 return;
984 }
985 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000987}
988
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400989/* Internal function to find slot for an item from its hash
Victor Stinner3c336c52016-09-12 14:17:40 +0200990 when it is known that the key is not present in the dict.
991
992 The dict must be combined. */
INADA Naokiba609772016-12-07 20:41:42 +0900993static Py_ssize_t
INADA Naoki778928b2017-08-03 23:45:15 +0900994find_empty_slot(PyDictKeysObject *keys, Py_hash_t hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000995{
INADA Naoki778928b2017-08-03 23:45:15 +0900996 assert(keys != NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000997
INADA Naoki778928b2017-08-03 23:45:15 +0900998 const size_t mask = DK_MASK(keys);
999 size_t i = hash & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001000 Py_ssize_t ix = dictkeys_get_index(keys, i);
INADA Naoki778928b2017-08-03 23:45:15 +09001001 for (size_t perturb = hash; ix >= 0;) {
INADA Naoki267941c2016-10-06 15:19:07 +09001002 perturb >>= PERTURB_SHIFT;
INADA Naoki778928b2017-08-03 23:45:15 +09001003 i = (i*5 + perturb + 1) & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001004 ix = dictkeys_get_index(keys, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 }
INADA Naoki778928b2017-08-03 23:45:15 +09001006 return i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001007}
1008
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001009static int
1010insertion_resize(PyDictObject *mp)
1011{
Raymond Hettinger36f74aa2013-05-17 03:01:13 -07001012 return dictresize(mp, GROWTH_RATE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001013}
Antoine Pitroue965d972012-02-27 00:45:12 +01001014
1015/*
1016Internal routine to insert a new item into the table.
1017Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +01001018Returns -1 if an error occurred, or 0 on success.
1019*/
1020static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001021insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001022{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001023 PyObject *old_value;
INADA Naokiba609772016-12-07 20:41:42 +09001024 PyDictKeyEntry *ep;
Antoine Pitroue965d972012-02-27 00:45:12 +01001025
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001026 Py_INCREF(key);
1027 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001028 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
1029 if (insertion_resize(mp) < 0)
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001030 goto Fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001031 }
1032
INADA Naoki778928b2017-08-03 23:45:15 +09001033 Py_ssize_t ix = mp->ma_keys->dk_lookup(mp, key, hash, &old_value);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001034 if (ix == DKIX_ERROR)
1035 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001036
Antoine Pitroud6967322014-10-18 00:35:00 +02001037 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001038 MAINTAIN_TRACKING(mp, key, value);
Victor Stinner742da042016-09-07 17:40:12 -07001039
1040 /* When insertion order is different from shared key, we can't share
1041 * the key anymore. Convert this instance to combine table.
1042 */
1043 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09001044 ((ix >= 0 && old_value == NULL && mp->ma_used != ix) ||
Victor Stinner742da042016-09-07 17:40:12 -07001045 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001046 if (insertion_resize(mp) < 0)
1047 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001048 ix = DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001049 }
Victor Stinner742da042016-09-07 17:40:12 -07001050
1051 if (ix == DKIX_EMPTY) {
1052 /* Insert into new slot. */
INADA Naokiba609772016-12-07 20:41:42 +09001053 assert(old_value == NULL);
Victor Stinner742da042016-09-07 17:40:12 -07001054 if (mp->ma_keys->dk_usable <= 0) {
1055 /* Need to resize. */
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001056 if (insertion_resize(mp) < 0)
1057 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001058 }
INADA Naoki778928b2017-08-03 23:45:15 +09001059 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naokiba609772016-12-07 20:41:42 +09001060 ep = &DK_ENTRIES(mp->ma_keys)[mp->ma_keys->dk_nentries];
INADA Naokia7576492018-11-14 18:39:27 +09001061 dictkeys_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Victor Stinner742da042016-09-07 17:40:12 -07001062 ep->me_key = key;
1063 ep->me_hash = hash;
1064 if (mp->ma_values) {
1065 assert (mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
1066 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001067 }
1068 else {
Victor Stinner742da042016-09-07 17:40:12 -07001069 ep->me_value = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001070 }
1071 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001072 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07001073 mp->ma_keys->dk_usable--;
1074 mp->ma_keys->dk_nentries++;
1075 assert(mp->ma_keys->dk_usable >= 0);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001076 assert(_PyDict_CheckConsistency(mp));
Victor Stinner742da042016-09-07 17:40:12 -07001077 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001078 }
Victor Stinner742da042016-09-07 17:40:12 -07001079
INADA Naokiba609772016-12-07 20:41:42 +09001080 if (_PyDict_HasSplitTable(mp)) {
1081 mp->ma_values[ix] = value;
1082 if (old_value == NULL) {
1083 /* pending state */
1084 assert(ix == mp->ma_used);
1085 mp->ma_used++;
1086 }
1087 }
1088 else {
1089 assert(old_value != NULL);
1090 DK_ENTRIES(mp->ma_keys)[ix].me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07001091 }
1092
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001093 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokiba609772016-12-07 20:41:42 +09001094 Py_XDECREF(old_value); /* which **CAN** re-enter (see issue #22653) */
Victor Stinner611b0fa2016-09-14 15:02:01 +02001095 assert(_PyDict_CheckConsistency(mp));
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001096 Py_DECREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001097 return 0;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001098
1099Fail:
1100 Py_DECREF(value);
1101 Py_DECREF(key);
1102 return -1;
Antoine Pitroue965d972012-02-27 00:45:12 +01001103}
1104
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001105/*
luzpaza5293b42017-11-05 07:37:50 -06001106Internal routine used by dictresize() to build a hashtable of entries.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001107*/
1108static void
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001109build_indices(PyDictKeysObject *keys, PyDictKeyEntry *ep, Py_ssize_t n)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001110{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001111 size_t mask = (size_t)DK_SIZE(keys) - 1;
1112 for (Py_ssize_t ix = 0; ix != n; ix++, ep++) {
1113 Py_hash_t hash = ep->me_hash;
1114 size_t i = hash & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001115 for (size_t perturb = hash; dictkeys_get_index(keys, i) != DKIX_EMPTY;) {
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001116 perturb >>= PERTURB_SHIFT;
INADA Naoki870c2862017-06-24 09:03:19 +09001117 i = mask & (i*5 + perturb + 1);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001118 }
INADA Naokia7576492018-11-14 18:39:27 +09001119 dictkeys_set_index(keys, i, ix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001121}
1122
1123/*
1124Restructure the table by allocating a new table and reinserting all
1125items again. When entries have been deleted, the new table may
1126actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001127If a table is split (its keys and hashes are shared, its values are not),
1128then the values are temporarily copied into the table, it is resized as
1129a combined table, then the me_value slots in the old table are NULLed out.
1130After resizing a table is always combined,
1131but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001132*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001133static int
Victor Stinner3d3f2642016-12-15 17:21:23 +01001134dictresize(PyDictObject *mp, Py_ssize_t minsize)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001135{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001136 Py_ssize_t newsize, numentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001137 PyDictKeysObject *oldkeys;
1138 PyObject **oldvalues;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001139 PyDictKeyEntry *oldentries, *newentries;
Tim Peters91a364d2001-05-19 07:04:38 +00001140
Victor Stinner742da042016-09-07 17:40:12 -07001141 /* Find the smallest table size > minused. */
1142 for (newsize = PyDict_MINSIZE;
Victor Stinner3d3f2642016-12-15 17:21:23 +01001143 newsize < minsize && newsize > 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 newsize <<= 1)
1145 ;
1146 if (newsize <= 0) {
1147 PyErr_NoMemory();
1148 return -1;
1149 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001150
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001151 oldkeys = mp->ma_keys;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001152
1153 /* NOTE: Current odict checks mp->ma_keys to detect resize happen.
1154 * So we can't reuse oldkeys even if oldkeys->dk_size == newsize.
1155 * TODO: Try reusing oldkeys when reimplement odict.
1156 */
1157
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001158 /* Allocate a new table. */
1159 mp->ma_keys = new_keys_object(newsize);
1160 if (mp->ma_keys == NULL) {
1161 mp->ma_keys = oldkeys;
1162 return -1;
1163 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01001164 // New table must be large enough.
1165 assert(mp->ma_keys->dk_usable >= mp->ma_used);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001166 if (oldkeys->dk_lookup == lookdict)
1167 mp->ma_keys->dk_lookup = lookdict;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001168
1169 numentries = mp->ma_used;
1170 oldentries = DK_ENTRIES(oldkeys);
1171 newentries = DK_ENTRIES(mp->ma_keys);
1172 oldvalues = mp->ma_values;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001173 if (oldvalues != NULL) {
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001174 /* Convert split table into new combined table.
1175 * We must incref keys; we can transfer values.
1176 * Note that values of split table is always dense.
1177 */
1178 for (Py_ssize_t i = 0; i < numentries; i++) {
1179 assert(oldvalues[i] != NULL);
1180 PyDictKeyEntry *ep = &oldentries[i];
1181 PyObject *key = ep->me_key;
1182 Py_INCREF(key);
1183 newentries[i].me_key = key;
1184 newentries[i].me_hash = ep->me_hash;
1185 newentries[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001187
INADA Naokia7576492018-11-14 18:39:27 +09001188 dictkeys_decref(oldkeys);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001189 mp->ma_values = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001190 if (oldvalues != empty_values) {
1191 free_values(oldvalues);
1192 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001193 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001194 else { // combined table.
1195 if (oldkeys->dk_nentries == numentries) {
1196 memcpy(newentries, oldentries, numentries * sizeof(PyDictKeyEntry));
1197 }
1198 else {
1199 PyDictKeyEntry *ep = oldentries;
1200 for (Py_ssize_t i = 0; i < numentries; i++) {
1201 while (ep->me_value == NULL)
1202 ep++;
1203 newentries[i] = *ep++;
1204 }
1205 }
1206
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001207 assert(oldkeys->dk_lookup != lookdict_split);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001208 assert(oldkeys->dk_refcnt == 1);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001209 if (oldkeys->dk_size == PyDict_MINSIZE &&
1210 numfreekeys < PyDict_MAXFREELIST) {
INADA Naokia7576492018-11-14 18:39:27 +09001211 _Py_DEC_REFTOTAL;
1212 keys_free_list[numfreekeys++] = oldkeys;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001213 }
1214 else {
INADA Naokia7576492018-11-14 18:39:27 +09001215 _Py_DEC_REFTOTAL;
1216 PyObject_FREE(oldkeys);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001217 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001218 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001219
1220 build_indices(mp->ma_keys, newentries, numentries);
1221 mp->ma_keys->dk_usable -= numentries;
1222 mp->ma_keys->dk_nentries = numentries;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001224}
1225
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001226/* Returns NULL if unable to split table.
1227 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001228static PyDictKeysObject *
1229make_keys_shared(PyObject *op)
1230{
1231 Py_ssize_t i;
1232 Py_ssize_t size;
1233 PyDictObject *mp = (PyDictObject *)op;
1234
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001235 if (!PyDict_CheckExact(op))
1236 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001237 if (!_PyDict_HasSplitTable(mp)) {
1238 PyDictKeyEntry *ep0;
1239 PyObject **values;
1240 assert(mp->ma_keys->dk_refcnt == 1);
1241 if (mp->ma_keys->dk_lookup == lookdict) {
1242 return NULL;
1243 }
1244 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
1245 /* Remove dummy keys */
1246 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
1247 return NULL;
1248 }
1249 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
1250 /* Copy values into a new array */
Victor Stinner742da042016-09-07 17:40:12 -07001251 ep0 = DK_ENTRIES(mp->ma_keys);
1252 size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001253 values = new_values(size);
1254 if (values == NULL) {
1255 PyErr_SetString(PyExc_MemoryError,
1256 "Not enough memory to allocate new values array");
1257 return NULL;
1258 }
1259 for (i = 0; i < size; i++) {
1260 values[i] = ep0[i].me_value;
1261 ep0[i].me_value = NULL;
1262 }
1263 mp->ma_keys->dk_lookup = lookdict_split;
1264 mp->ma_values = values;
1265 }
INADA Naokia7576492018-11-14 18:39:27 +09001266 dictkeys_incref(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001267 return mp->ma_keys;
1268}
Christian Heimes99170a52007-12-19 02:07:34 +00001269
1270PyObject *
1271_PyDict_NewPresized(Py_ssize_t minused)
1272{
INADA Naoki92c50ee2016-11-22 00:57:02 +09001273 const Py_ssize_t max_presize = 128 * 1024;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001274 Py_ssize_t newsize;
1275 PyDictKeysObject *new_keys;
INADA Naoki92c50ee2016-11-22 00:57:02 +09001276
Inada Naokif2a18672019-03-12 17:25:44 +09001277 if (minused == 0) {
1278 return PyDict_New();
1279 }
INADA Naoki92c50ee2016-11-22 00:57:02 +09001280 /* There are no strict guarantee that returned dict can contain minused
1281 * items without resize. So we create medium size dict instead of very
1282 * large dict or MemoryError.
1283 */
1284 if (minused > USABLE_FRACTION(max_presize)) {
1285 newsize = max_presize;
1286 }
1287 else {
1288 Py_ssize_t minsize = ESTIMATE_SIZE(minused);
1289 newsize = PyDict_MINSIZE;
1290 while (newsize < minsize) {
1291 newsize <<= 1;
1292 }
1293 }
1294 assert(IS_POWER_OF_2(newsize));
1295
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001296 new_keys = new_keys_object(newsize);
1297 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001299 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001300}
1301
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001302/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1303 * that may occur (originally dicts supported only string keys, and exceptions
1304 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001305 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001306 * (suppressed) occurred while computing the key's hash, or that some error
1307 * (suppressed) occurred when comparing keys in the dict's internal probe
1308 * sequence. A nasty example of the latter is when a Python-coded comparison
1309 * function hits a stack-depth error, which can cause this to return NULL
1310 * even if the key is present.
1311 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001312PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001313PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001314{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001315 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07001316 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 PyDictObject *mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 PyThreadState *tstate;
INADA Naokiba609772016-12-07 20:41:42 +09001319 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 if (!PyDict_Check(op))
1322 return NULL;
1323 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001324 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 {
1326 hash = PyObject_Hash(key);
1327 if (hash == -1) {
1328 PyErr_Clear();
1329 return NULL;
1330 }
1331 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 /* We can arrive here with a NULL tstate during initialization: try
1334 running "python -Wi" for an example related to string interning.
1335 Let's just hope that no exception occurs then... This must be
Victor Stinner50b48572018-11-01 01:51:40 +01001336 _PyThreadState_GET() and not PyThreadState_Get() because the latter
Victor Stinner9204fb82018-10-30 15:13:17 +01001337 abort Python if tstate is NULL. */
Victor Stinner50b48572018-11-01 01:51:40 +01001338 tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 if (tstate != NULL && tstate->curexc_type != NULL) {
1340 /* preserve the existing exception */
1341 PyObject *err_type, *err_value, *err_tb;
1342 PyErr_Fetch(&err_type, &err_value, &err_tb);
INADA Naoki778928b2017-08-03 23:45:15 +09001343 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 /* ignore errors */
1345 PyErr_Restore(err_type, err_value, err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001346 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 return NULL;
1348 }
1349 else {
INADA Naoki778928b2017-08-03 23:45:15 +09001350 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001351 if (ix < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 PyErr_Clear();
1353 return NULL;
1354 }
1355 }
INADA Naokiba609772016-12-07 20:41:42 +09001356 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001357}
1358
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001359/* Same as PyDict_GetItemWithError() but with hash supplied by caller.
1360 This returns NULL *with* an exception set if an exception occurred.
1361 It returns NULL *without* an exception set if the key wasn't present.
1362*/
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001363PyObject *
1364_PyDict_GetItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1365{
Victor Stinner742da042016-09-07 17:40:12 -07001366 Py_ssize_t ix;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001367 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001368 PyObject *value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001369
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001370 if (!PyDict_Check(op)) {
1371 PyErr_BadInternalCall();
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001372 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001373 }
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001374
INADA Naoki778928b2017-08-03 23:45:15 +09001375 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001376 if (ix < 0) {
1377 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001378 }
INADA Naokiba609772016-12-07 20:41:42 +09001379 return value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001380}
1381
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001382/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1383 This returns NULL *with* an exception set if an exception occurred.
1384 It returns NULL *without* an exception set if the key wasn't present.
1385*/
1386PyObject *
1387PyDict_GetItemWithError(PyObject *op, PyObject *key)
1388{
Victor Stinner742da042016-09-07 17:40:12 -07001389 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001390 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 PyDictObject*mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001392 PyObject *value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 if (!PyDict_Check(op)) {
1395 PyErr_BadInternalCall();
1396 return NULL;
1397 }
1398 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001399 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 {
1401 hash = PyObject_Hash(key);
1402 if (hash == -1) {
1403 return NULL;
1404 }
1405 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001406
INADA Naoki778928b2017-08-03 23:45:15 +09001407 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001408 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001410 return value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001411}
1412
Brett Cannonfd074152012-04-14 14:10:13 -04001413PyObject *
1414_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1415{
1416 PyObject *kv;
1417 kv = _PyUnicode_FromId(key); /* borrowed */
1418 if (kv == NULL)
1419 return NULL;
1420 return PyDict_GetItemWithError(dp, kv);
1421}
1422
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001423PyObject *
1424_PyDict_GetItemStringWithError(PyObject *v, const char *key)
1425{
1426 PyObject *kv, *rv;
1427 kv = PyUnicode_FromString(key);
1428 if (kv == NULL) {
1429 return NULL;
1430 }
1431 rv = PyDict_GetItemWithError(v, kv);
1432 Py_DECREF(kv);
1433 return rv;
1434}
1435
Victor Stinnerb4efc962015-11-20 09:24:02 +01001436/* Fast version of global value lookup (LOAD_GLOBAL).
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001437 * Lookup in globals, then builtins.
Victor Stinnerb4efc962015-11-20 09:24:02 +01001438 *
1439 * Raise an exception and return NULL if an error occurred (ex: computing the
1440 * key hash failed, key comparison failed, ...). Return NULL if the key doesn't
1441 * exist. Return the value if the key exists.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001442 */
1443PyObject *
1444_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001445{
Victor Stinner742da042016-09-07 17:40:12 -07001446 Py_ssize_t ix;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001447 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09001448 PyObject *value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001449
1450 if (!PyUnicode_CheckExact(key) ||
1451 (hash = ((PyASCIIObject *) key)->hash) == -1)
1452 {
1453 hash = PyObject_Hash(key);
1454 if (hash == -1)
1455 return NULL;
Antoine Pitroue965d972012-02-27 00:45:12 +01001456 }
Victor Stinnerb4efc962015-11-20 09:24:02 +01001457
1458 /* namespace 1: globals */
INADA Naoki778928b2017-08-03 23:45:15 +09001459 ix = globals->ma_keys->dk_lookup(globals, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001460 if (ix == DKIX_ERROR)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001461 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001462 if (ix != DKIX_EMPTY && value != NULL)
1463 return value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001464
1465 /* namespace 2: builtins */
INADA Naoki778928b2017-08-03 23:45:15 +09001466 ix = builtins->ma_keys->dk_lookup(builtins, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001467 if (ix < 0)
Victor Stinnerb4efc962015-11-20 09:24:02 +01001468 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001469 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001470}
1471
Antoine Pitroue965d972012-02-27 00:45:12 +01001472/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1473 * dictionary if it's merely replacing the value for an existing key.
1474 * This means that it's safe to loop over a dictionary with PyDict_Next()
1475 * and occasionally replace a value -- but you can't insert new keys or
1476 * remove them.
1477 */
1478int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001479PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001480{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001481 PyDictObject *mp;
1482 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001483 if (!PyDict_Check(op)) {
1484 PyErr_BadInternalCall();
1485 return -1;
1486 }
1487 assert(key);
1488 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001489 mp = (PyDictObject *)op;
1490 if (!PyUnicode_CheckExact(key) ||
1491 (hash = ((PyASCIIObject *) key)->hash) == -1)
1492 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001493 hash = PyObject_Hash(key);
1494 if (hash == -1)
1495 return -1;
1496 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001497
1498 /* insertdict() handles any resizing that might be necessary */
1499 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001500}
1501
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001502int
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001503_PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
1504 Py_hash_t hash)
1505{
1506 PyDictObject *mp;
1507
1508 if (!PyDict_Check(op)) {
1509 PyErr_BadInternalCall();
1510 return -1;
1511 }
1512 assert(key);
1513 assert(value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001514 assert(hash != -1);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001515 mp = (PyDictObject *)op;
1516
1517 /* insertdict() handles any resizing that might be necessary */
1518 return insertdict(mp, key, hash, value);
1519}
1520
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001521static int
INADA Naoki778928b2017-08-03 23:45:15 +09001522delitem_common(PyDictObject *mp, Py_hash_t hash, Py_ssize_t ix,
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001523 PyObject *old_value)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001524{
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001525 PyObject *old_key;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001526 PyDictKeyEntry *ep;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001527
INADA Naoki778928b2017-08-03 23:45:15 +09001528 Py_ssize_t hashpos = lookdict_index(mp->ma_keys, hash, ix);
1529 assert(hashpos >= 0);
1530
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001531 mp->ma_used--;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001532 mp->ma_version_tag = DICT_NEXT_VERSION();
1533 ep = &DK_ENTRIES(mp->ma_keys)[ix];
INADA Naokia7576492018-11-14 18:39:27 +09001534 dictkeys_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001535 ENSURE_ALLOWS_DELETIONS(mp);
1536 old_key = ep->me_key;
1537 ep->me_key = NULL;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001538 ep->me_value = NULL;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001539 Py_DECREF(old_key);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001540 Py_DECREF(old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001541
1542 assert(_PyDict_CheckConsistency(mp));
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001543 return 0;
1544}
1545
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001546int
Tim Peters1f5871e2000-07-04 17:44:48 +00001547PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001548{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001549 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550 assert(key);
1551 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001552 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001553 hash = PyObject_Hash(key);
1554 if (hash == -1)
1555 return -1;
1556 }
Victor Stinner742da042016-09-07 17:40:12 -07001557
1558 return _PyDict_DelItem_KnownHash(op, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001559}
1560
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001561int
1562_PyDict_DelItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1563{
INADA Naoki778928b2017-08-03 23:45:15 +09001564 Py_ssize_t ix;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001565 PyDictObject *mp;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001566 PyObject *old_value;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001567
1568 if (!PyDict_Check(op)) {
1569 PyErr_BadInternalCall();
1570 return -1;
1571 }
1572 assert(key);
1573 assert(hash != -1);
1574 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001575 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001576 if (ix == DKIX_ERROR)
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001577 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09001578 if (ix == DKIX_EMPTY || old_value == NULL) {
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001579 _PyErr_SetKeyError(key);
1580 return -1;
1581 }
Victor Stinner78601a32016-09-09 19:28:36 -07001582
1583 // Split table doesn't allow deletion. Combine it.
1584 if (_PyDict_HasSplitTable(mp)) {
1585 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1586 return -1;
1587 }
INADA Naoki778928b2017-08-03 23:45:15 +09001588 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001589 assert(ix >= 0);
1590 }
1591
INADA Naoki778928b2017-08-03 23:45:15 +09001592 return delitem_common(mp, hash, ix, old_value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001593}
1594
Antoine Pitroud741ed42016-12-27 14:23:43 +01001595/* This function promises that the predicate -> deletion sequence is atomic
1596 * (i.e. protected by the GIL), assuming the predicate itself doesn't
1597 * release the GIL.
1598 */
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001599int
1600_PyDict_DelItemIf(PyObject *op, PyObject *key,
1601 int (*predicate)(PyObject *value))
1602{
Antoine Pitroud741ed42016-12-27 14:23:43 +01001603 Py_ssize_t hashpos, ix;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001604 PyDictObject *mp;
1605 Py_hash_t hash;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001606 PyObject *old_value;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001607 int res;
1608
1609 if (!PyDict_Check(op)) {
1610 PyErr_BadInternalCall();
1611 return -1;
1612 }
1613 assert(key);
1614 hash = PyObject_Hash(key);
1615 if (hash == -1)
1616 return -1;
1617 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001618 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001619 if (ix == DKIX_ERROR)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001620 return -1;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001621 if (ix == DKIX_EMPTY || old_value == NULL) {
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001622 _PyErr_SetKeyError(key);
1623 return -1;
1624 }
Antoine Pitroud741ed42016-12-27 14:23:43 +01001625
1626 // Split table doesn't allow deletion. Combine it.
1627 if (_PyDict_HasSplitTable(mp)) {
1628 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1629 return -1;
1630 }
INADA Naoki778928b2017-08-03 23:45:15 +09001631 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001632 assert(ix >= 0);
1633 }
1634
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001635 res = predicate(old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001636 if (res == -1)
1637 return -1;
INADA Naoki778928b2017-08-03 23:45:15 +09001638
1639 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1640 assert(hashpos >= 0);
1641
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001642 if (res > 0)
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001643 return delitem_common(mp, hashpos, ix, old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001644 else
1645 return 0;
1646}
1647
1648
Guido van Rossum25831651993-05-19 14:50:45 +00001649void
Tim Peters1f5871e2000-07-04 17:44:48 +00001650PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001651{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001652 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001653 PyDictKeysObject *oldkeys;
1654 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 if (!PyDict_Check(op))
1658 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001659 mp = ((PyDictObject *)op);
1660 oldkeys = mp->ma_keys;
1661 oldvalues = mp->ma_values;
1662 if (oldvalues == empty_values)
1663 return;
1664 /* Empty the dict... */
INADA Naokia7576492018-11-14 18:39:27 +09001665 dictkeys_incref(Py_EMPTY_KEYS);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001666 mp->ma_keys = Py_EMPTY_KEYS;
1667 mp->ma_values = empty_values;
1668 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001669 mp->ma_version_tag = DICT_NEXT_VERSION();
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001670 /* ...then clear the keys and values */
1671 if (oldvalues != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001672 n = oldkeys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001673 for (i = 0; i < n; i++)
1674 Py_CLEAR(oldvalues[i]);
1675 free_values(oldvalues);
INADA Naokia7576492018-11-14 18:39:27 +09001676 dictkeys_decref(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001677 }
1678 else {
1679 assert(oldkeys->dk_refcnt == 1);
INADA Naokia7576492018-11-14 18:39:27 +09001680 dictkeys_decref(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001681 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02001682 assert(_PyDict_CheckConsistency(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001683}
1684
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001685/* Internal version of PyDict_Next that returns a hash value in addition
1686 * to the key and value.
1687 * Return 1 on success, return 0 when the reached the end of the dictionary
1688 * (or if op is not a dictionary)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001689 */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001690int
1691_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1692 PyObject **pvalue, Py_hash_t *phash)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001693{
INADA Naokica2d8be2016-11-04 16:59:10 +09001694 Py_ssize_t i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001695 PyDictObject *mp;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001696 PyDictKeyEntry *entry_ptr;
1697 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001698
1699 if (!PyDict_Check(op))
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001700 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 mp = (PyDictObject *)op;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001702 i = *ppos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001703 if (mp->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09001704 if (i < 0 || i >= mp->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001705 return 0;
INADA Naokica2d8be2016-11-04 16:59:10 +09001706 /* values of split table is always dense */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001707 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
INADA Naokica2d8be2016-11-04 16:59:10 +09001708 value = mp->ma_values[i];
1709 assert(value != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001710 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001711 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09001712 Py_ssize_t n = mp->ma_keys->dk_nentries;
1713 if (i < 0 || i >= n)
1714 return 0;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001715 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
1716 while (i < n && entry_ptr->me_value == NULL) {
1717 entry_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 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001724 *ppos = i+1;
1725 if (pkey)
1726 *pkey = entry_ptr->me_key;
1727 if (phash)
1728 *phash = entry_ptr->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001729 if (pvalue)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001730 *pvalue = value;
1731 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001732}
1733
Tim Peters080c88b2003-02-15 03:01:11 +00001734/*
1735 * Iterate over a dict. Use like so:
1736 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001737 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001738 * PyObject *key, *value;
1739 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001740 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001741 * Refer to borrowed references in key and value.
Tim Peters080c88b2003-02-15 03:01:11 +00001742 * }
1743 *
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001744 * Return 1 on success, return 0 when the reached the end of the dictionary
1745 * (or if op is not a dictionary)
1746 *
Tim Peters080c88b2003-02-15 03:01:11 +00001747 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001748 * mutates the dict. One exception: it is safe if the loop merely changes
1749 * the values associated with the keys (but doesn't insert new keys or
1750 * delete keys), via PyDict_SetItem().
1751 */
Guido van Rossum25831651993-05-19 14:50:45 +00001752int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001753PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001754{
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001755 return _PyDict_Next(op, ppos, pkey, pvalue, NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001756}
1757
Eric Snow96c6af92015-05-29 22:21:39 -06001758/* Internal version of dict.pop(). */
1759PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001760_PyDict_Pop_KnownHash(PyObject *dict, PyObject *key, Py_hash_t hash, PyObject *deflt)
Eric Snow96c6af92015-05-29 22:21:39 -06001761{
Victor Stinner742da042016-09-07 17:40:12 -07001762 Py_ssize_t ix, hashpos;
Eric Snow96c6af92015-05-29 22:21:39 -06001763 PyObject *old_value, *old_key;
1764 PyDictKeyEntry *ep;
Yury Selivanov684ef2c2016-10-28 19:01:21 -04001765 PyDictObject *mp;
1766
1767 assert(PyDict_Check(dict));
1768 mp = (PyDictObject *)dict;
Eric Snow96c6af92015-05-29 22:21:39 -06001769
1770 if (mp->ma_used == 0) {
1771 if (deflt) {
1772 Py_INCREF(deflt);
1773 return deflt;
1774 }
1775 _PyErr_SetKeyError(key);
1776 return NULL;
1777 }
INADA Naoki778928b2017-08-03 23:45:15 +09001778 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001779 if (ix == DKIX_ERROR)
Eric Snow96c6af92015-05-29 22:21:39 -06001780 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001781 if (ix == DKIX_EMPTY || old_value == NULL) {
Eric Snow96c6af92015-05-29 22:21:39 -06001782 if (deflt) {
1783 Py_INCREF(deflt);
1784 return deflt;
1785 }
1786 _PyErr_SetKeyError(key);
1787 return NULL;
1788 }
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001789
Victor Stinner78601a32016-09-09 19:28:36 -07001790 // Split table doesn't allow deletion. Combine it.
1791 if (_PyDict_HasSplitTable(mp)) {
1792 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1793 return NULL;
1794 }
INADA Naoki778928b2017-08-03 23:45:15 +09001795 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001796 assert(ix >= 0);
1797 }
1798
INADA Naoki778928b2017-08-03 23:45:15 +09001799 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1800 assert(hashpos >= 0);
Victor Stinner78601a32016-09-09 19:28:36 -07001801 assert(old_value != NULL);
Eric Snow96c6af92015-05-29 22:21:39 -06001802 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001803 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokia7576492018-11-14 18:39:27 +09001804 dictkeys_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
Victor Stinner78601a32016-09-09 19:28:36 -07001805 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1806 ENSURE_ALLOWS_DELETIONS(mp);
1807 old_key = ep->me_key;
1808 ep->me_key = NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001809 ep->me_value = NULL;
Victor Stinner78601a32016-09-09 19:28:36 -07001810 Py_DECREF(old_key);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001811
1812 assert(_PyDict_CheckConsistency(mp));
Eric Snow96c6af92015-05-29 22:21:39 -06001813 return old_value;
1814}
1815
Serhiy Storchaka67796522017-01-12 18:34:33 +02001816PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001817_PyDict_Pop(PyObject *dict, PyObject *key, PyObject *deflt)
Serhiy Storchaka67796522017-01-12 18:34:33 +02001818{
1819 Py_hash_t hash;
1820
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001821 if (((PyDictObject *)dict)->ma_used == 0) {
Serhiy Storchaka67796522017-01-12 18:34:33 +02001822 if (deflt) {
1823 Py_INCREF(deflt);
1824 return deflt;
1825 }
1826 _PyErr_SetKeyError(key);
1827 return NULL;
1828 }
1829 if (!PyUnicode_CheckExact(key) ||
1830 (hash = ((PyASCIIObject *) key)->hash) == -1) {
1831 hash = PyObject_Hash(key);
1832 if (hash == -1)
1833 return NULL;
1834 }
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001835 return _PyDict_Pop_KnownHash(dict, key, hash, deflt);
Serhiy Storchaka67796522017-01-12 18:34:33 +02001836}
1837
Eric Snow96c6af92015-05-29 22:21:39 -06001838/* Internal version of dict.from_keys(). It is subclass-friendly. */
1839PyObject *
1840_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
1841{
1842 PyObject *it; /* iter(iterable) */
1843 PyObject *key;
1844 PyObject *d;
1845 int status;
1846
Victor Stinnera5ed5f02016-12-06 18:45:50 +01001847 d = _PyObject_CallNoArg(cls);
Eric Snow96c6af92015-05-29 22:21:39 -06001848 if (d == NULL)
1849 return NULL;
1850
1851 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
1852 if (PyDict_CheckExact(iterable)) {
1853 PyDictObject *mp = (PyDictObject *)d;
1854 PyObject *oldvalue;
1855 Py_ssize_t pos = 0;
1856 PyObject *key;
1857 Py_hash_t hash;
1858
Serhiy Storchakac61ac162017-03-21 08:52:38 +02001859 if (dictresize(mp, ESTIMATE_SIZE(PyDict_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001860 Py_DECREF(d);
1861 return NULL;
1862 }
1863
1864 while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
1865 if (insertdict(mp, key, hash, value)) {
1866 Py_DECREF(d);
1867 return NULL;
1868 }
1869 }
1870 return d;
1871 }
1872 if (PyAnySet_CheckExact(iterable)) {
1873 PyDictObject *mp = (PyDictObject *)d;
1874 Py_ssize_t pos = 0;
1875 PyObject *key;
1876 Py_hash_t hash;
1877
Victor Stinner742da042016-09-07 17:40:12 -07001878 if (dictresize(mp, ESTIMATE_SIZE(PySet_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001879 Py_DECREF(d);
1880 return NULL;
1881 }
1882
1883 while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
1884 if (insertdict(mp, key, hash, value)) {
1885 Py_DECREF(d);
1886 return NULL;
1887 }
1888 }
1889 return d;
1890 }
1891 }
1892
1893 it = PyObject_GetIter(iterable);
1894 if (it == NULL){
1895 Py_DECREF(d);
1896 return NULL;
1897 }
1898
1899 if (PyDict_CheckExact(d)) {
1900 while ((key = PyIter_Next(it)) != NULL) {
1901 status = PyDict_SetItem(d, key, value);
1902 Py_DECREF(key);
1903 if (status < 0)
1904 goto Fail;
1905 }
1906 } else {
1907 while ((key = PyIter_Next(it)) != NULL) {
1908 status = PyObject_SetItem(d, key, value);
1909 Py_DECREF(key);
1910 if (status < 0)
1911 goto Fail;
1912 }
1913 }
1914
1915 if (PyErr_Occurred())
1916 goto Fail;
1917 Py_DECREF(it);
1918 return d;
1919
1920Fail:
1921 Py_DECREF(it);
1922 Py_DECREF(d);
1923 return NULL;
1924}
1925
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001926/* Methods */
1927
1928static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001929dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001930{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001931 PyObject **values = mp->ma_values;
1932 PyDictKeysObject *keys = mp->ma_keys;
1933 Py_ssize_t i, n;
INADA Naokia6296d32017-08-24 14:55:17 +09001934
1935 /* bpo-31095: UnTrack is needed before calling any callbacks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001936 PyObject_GC_UnTrack(mp);
1937 Py_TRASHCAN_SAFE_BEGIN(mp)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001938 if (values != NULL) {
1939 if (values != empty_values) {
Victor Stinner742da042016-09-07 17:40:12 -07001940 for (i = 0, n = mp->ma_keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001941 Py_XDECREF(values[i]);
1942 }
1943 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001944 }
INADA Naokia7576492018-11-14 18:39:27 +09001945 dictkeys_decref(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001946 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02001947 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001948 assert(keys->dk_refcnt == 1);
INADA Naokia7576492018-11-14 18:39:27 +09001949 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001950 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1952 free_list[numfree++] = mp;
1953 else
1954 Py_TYPE(mp)->tp_free((PyObject *)mp);
1955 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001956}
1957
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001958
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001959static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001960dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001961{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001962 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001963 PyObject *key = NULL, *value = NULL;
1964 _PyUnicodeWriter writer;
1965 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00001966
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 i = Py_ReprEnter((PyObject *)mp);
1968 if (i != 0) {
1969 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
1970 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001972 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001973 Py_ReprLeave((PyObject *)mp);
1974 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 }
Tim Petersa7259592001-06-16 05:11:17 +00001976
Victor Stinnerf91929b2013-11-19 13:07:38 +01001977 _PyUnicodeWriter_Init(&writer);
1978 writer.overallocate = 1;
1979 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
1980 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00001981
Victor Stinnerf91929b2013-11-19 13:07:38 +01001982 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
1983 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001985 /* Do repr() on each key+value pair, and insert ": " between them.
1986 Note that repr may mutate the dict. */
1987 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001988 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001990 PyObject *s;
1991 int res;
1992
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001993 /* Prevent repr from deleting key or value during key format. */
1994 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001995 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02001996
Victor Stinnerf91929b2013-11-19 13:07:38 +01001997 if (!first) {
1998 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
1999 goto error;
2000 }
2001 first = 0;
2002
2003 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01002005 goto error;
2006 res = _PyUnicodeWriter_WriteStr(&writer, s);
2007 Py_DECREF(s);
2008 if (res < 0)
2009 goto error;
2010
2011 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
2012 goto error;
2013
2014 s = PyObject_Repr(value);
2015 if (s == NULL)
2016 goto error;
2017 res = _PyUnicodeWriter_WriteStr(&writer, s);
2018 Py_DECREF(s);
2019 if (res < 0)
2020 goto error;
2021
2022 Py_CLEAR(key);
2023 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002024 }
Tim Petersa7259592001-06-16 05:11:17 +00002025
Victor Stinnerf91929b2013-11-19 13:07:38 +01002026 writer.overallocate = 0;
2027 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
2028 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00002029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002030 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01002031
2032 return _PyUnicodeWriter_Finish(&writer);
2033
2034error:
2035 Py_ReprLeave((PyObject *)mp);
2036 _PyUnicodeWriter_Dealloc(&writer);
2037 Py_XDECREF(key);
2038 Py_XDECREF(value);
2039 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002040}
2041
Martin v. Löwis18e16552006-02-15 17:27:45 +00002042static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002043dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002044{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002045 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002046}
2047
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002048static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002049dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002050{
Victor Stinner742da042016-09-07 17:40:12 -07002051 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002052 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09002053 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002055 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002056 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002057 hash = PyObject_Hash(key);
2058 if (hash == -1)
2059 return NULL;
2060 }
INADA Naoki778928b2017-08-03 23:45:15 +09002061 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002062 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002063 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002064 if (ix == DKIX_EMPTY || value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002065 if (!PyDict_CheckExact(mp)) {
2066 /* Look up __missing__ method if we're a subclass. */
2067 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05002068 _Py_IDENTIFIER(__missing__);
2069 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002070 if (missing != NULL) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01002071 res = PyObject_CallFunctionObjArgs(missing,
2072 key, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002073 Py_DECREF(missing);
2074 return res;
2075 }
2076 else if (PyErr_Occurred())
2077 return NULL;
2078 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07002079 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002080 return NULL;
2081 }
INADA Naokiba609772016-12-07 20:41:42 +09002082 Py_INCREF(value);
2083 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002084}
2085
2086static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002087dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002088{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002089 if (w == NULL)
2090 return PyDict_DelItem((PyObject *)mp, v);
2091 else
2092 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002093}
2094
Guido van Rossuma9e7a811997-05-13 21:02:11 +00002095static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002096 (lenfunc)dict_length, /*mp_length*/
2097 (binaryfunc)dict_subscript, /*mp_subscript*/
2098 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002099};
2100
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002101static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002102dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002103{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002104 PyObject *v;
2105 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002106 PyDictKeyEntry *ep;
2107 Py_ssize_t size, n, offset;
2108 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002109
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002110 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002111 n = mp->ma_used;
2112 v = PyList_New(n);
2113 if (v == NULL)
2114 return NULL;
2115 if (n != mp->ma_used) {
2116 /* Durnit. The allocations caused the dict to resize.
2117 * Just start over, this shouldn't normally happen.
2118 */
2119 Py_DECREF(v);
2120 goto again;
2121 }
Victor Stinner742da042016-09-07 17:40:12 -07002122 ep = DK_ENTRIES(mp->ma_keys);
2123 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002124 if (mp->ma_values) {
2125 value_ptr = mp->ma_values;
2126 offset = sizeof(PyObject *);
2127 }
2128 else {
2129 value_ptr = &ep[0].me_value;
2130 offset = sizeof(PyDictKeyEntry);
2131 }
2132 for (i = 0, j = 0; i < size; i++) {
2133 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002134 PyObject *key = ep[i].me_key;
2135 Py_INCREF(key);
2136 PyList_SET_ITEM(v, j, key);
2137 j++;
2138 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002139 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002140 }
2141 assert(j == n);
2142 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002143}
2144
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002145static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002146dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002147{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002148 PyObject *v;
2149 Py_ssize_t i, j;
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002150 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002151 Py_ssize_t size, n, offset;
2152 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002153
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002154 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002155 n = mp->ma_used;
2156 v = PyList_New(n);
2157 if (v == NULL)
2158 return NULL;
2159 if (n != mp->ma_used) {
2160 /* Durnit. The allocations caused the dict to resize.
2161 * Just start over, this shouldn't normally happen.
2162 */
2163 Py_DECREF(v);
2164 goto again;
2165 }
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002166 ep = DK_ENTRIES(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002167 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002168 if (mp->ma_values) {
2169 value_ptr = mp->ma_values;
2170 offset = sizeof(PyObject *);
2171 }
2172 else {
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002173 value_ptr = &ep[0].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002174 offset = sizeof(PyDictKeyEntry);
2175 }
2176 for (i = 0, j = 0; i < size; i++) {
2177 PyObject *value = *value_ptr;
2178 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2179 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002180 Py_INCREF(value);
2181 PyList_SET_ITEM(v, j, value);
2182 j++;
2183 }
2184 }
2185 assert(j == n);
2186 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002187}
2188
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002189static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002190dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002191{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002192 PyObject *v;
2193 Py_ssize_t i, j, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002194 Py_ssize_t size, offset;
2195 PyObject *item, *key;
2196 PyDictKeyEntry *ep;
2197 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002199 /* Preallocate the list of tuples, to avoid allocations during
2200 * the loop over the items, which could trigger GC, which
2201 * could resize the dict. :-(
2202 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002203 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002204 n = mp->ma_used;
2205 v = PyList_New(n);
2206 if (v == NULL)
2207 return NULL;
2208 for (i = 0; i < n; i++) {
2209 item = PyTuple_New(2);
2210 if (item == NULL) {
2211 Py_DECREF(v);
2212 return NULL;
2213 }
2214 PyList_SET_ITEM(v, i, item);
2215 }
2216 if (n != mp->ma_used) {
2217 /* Durnit. The allocations caused the dict to resize.
2218 * Just start over, this shouldn't normally happen.
2219 */
2220 Py_DECREF(v);
2221 goto again;
2222 }
2223 /* Nothing we do below makes any function calls. */
Victor Stinner742da042016-09-07 17:40:12 -07002224 ep = DK_ENTRIES(mp->ma_keys);
2225 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002226 if (mp->ma_values) {
2227 value_ptr = mp->ma_values;
2228 offset = sizeof(PyObject *);
2229 }
2230 else {
2231 value_ptr = &ep[0].me_value;
2232 offset = sizeof(PyDictKeyEntry);
2233 }
2234 for (i = 0, j = 0; i < size; i++) {
2235 PyObject *value = *value_ptr;
2236 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2237 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002238 key = ep[i].me_key;
2239 item = PyList_GET_ITEM(v, j);
2240 Py_INCREF(key);
2241 PyTuple_SET_ITEM(item, 0, key);
2242 Py_INCREF(value);
2243 PyTuple_SET_ITEM(item, 1, value);
2244 j++;
2245 }
2246 }
2247 assert(j == n);
2248 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002249}
2250
Larry Hastings5c661892014-01-24 06:17:25 -08002251/*[clinic input]
2252@classmethod
2253dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08002254 iterable: object
2255 value: object=None
2256 /
2257
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002258Create a new dictionary with keys from iterable and values set to value.
Larry Hastings5c661892014-01-24 06:17:25 -08002259[clinic start generated code]*/
2260
Larry Hastings5c661892014-01-24 06:17:25 -08002261static PyObject *
2262dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002263/*[clinic end generated code: output=8fb98e4b10384999 input=382ba4855d0f74c3]*/
Larry Hastings5c661892014-01-24 06:17:25 -08002264{
Eric Snow96c6af92015-05-29 22:21:39 -06002265 return _PyDict_FromKeys((PyObject *)type, iterable, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002266}
2267
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002268static int
Victor Stinner742da042016-09-07 17:40:12 -07002269dict_update_common(PyObject *self, PyObject *args, PyObject *kwds,
2270 const char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002271{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002272 PyObject *arg = NULL;
2273 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002274
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002275 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002276 result = -1;
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002277 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002279 _Py_IDENTIFIER(keys);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002280 PyObject *func;
2281 if (_PyObject_LookupAttrId(arg, &PyId_keys, &func) < 0) {
2282 result = -1;
2283 }
2284 else if (func != NULL) {
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002285 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002286 result = PyDict_Merge(self, arg, 1);
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002287 }
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002288 else {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002289 result = PyDict_MergeFromSeq2(self, arg, 1);
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002290 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002291 }
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002293 if (result == 0 && kwds != NULL) {
2294 if (PyArg_ValidateKeywordArguments(kwds))
2295 result = PyDict_Merge(self, kwds, 1);
2296 else
2297 result = -1;
2298 }
2299 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002300}
2301
Victor Stinner91f0d4a2017-01-19 12:45:06 +01002302/* Note: dict.update() uses the METH_VARARGS|METH_KEYWORDS calling convention.
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +03002303 Using METH_FASTCALL|METH_KEYWORDS would make dict.update(**dict2) calls
2304 slower, see the issue #29312. */
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002305static PyObject *
2306dict_update(PyObject *self, PyObject *args, PyObject *kwds)
2307{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002308 if (dict_update_common(self, args, kwds, "update") != -1)
2309 Py_RETURN_NONE;
2310 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002311}
2312
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002313/* Update unconditionally replaces existing items.
2314 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00002315 otherwise it leaves existing items unchanged.
2316
2317 PyDict_{Update,Merge} update/merge from a mapping object.
2318
Tim Petersf582b822001-12-11 18:51:08 +00002319 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00002320 producing iterable objects of length 2.
2321*/
2322
Tim Petersf582b822001-12-11 18:51:08 +00002323int
Tim Peters1fc240e2001-10-26 05:06:50 +00002324PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
2325{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002326 PyObject *it; /* iter(seq2) */
2327 Py_ssize_t i; /* index into seq2 of current element */
2328 PyObject *item; /* seq2[i] */
2329 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00002330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002331 assert(d != NULL);
2332 assert(PyDict_Check(d));
2333 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00002334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002335 it = PyObject_GetIter(seq2);
2336 if (it == NULL)
2337 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002339 for (i = 0; ; ++i) {
2340 PyObject *key, *value;
2341 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00002342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002343 fast = NULL;
2344 item = PyIter_Next(it);
2345 if (item == NULL) {
2346 if (PyErr_Occurred())
2347 goto Fail;
2348 break;
2349 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002351 /* Convert item to sequence, and verify length 2. */
2352 fast = PySequence_Fast(item, "");
2353 if (fast == NULL) {
2354 if (PyErr_ExceptionMatches(PyExc_TypeError))
2355 PyErr_Format(PyExc_TypeError,
2356 "cannot convert dictionary update "
2357 "sequence element #%zd to a sequence",
2358 i);
2359 goto Fail;
2360 }
2361 n = PySequence_Fast_GET_SIZE(fast);
2362 if (n != 2) {
2363 PyErr_Format(PyExc_ValueError,
2364 "dictionary update sequence element #%zd "
2365 "has length %zd; 2 is required",
2366 i, n);
2367 goto Fail;
2368 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002370 /* Update/merge with this (key, value) pair. */
2371 key = PySequence_Fast_GET_ITEM(fast, 0);
2372 value = PySequence_Fast_GET_ITEM(fast, 1);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002373 Py_INCREF(key);
2374 Py_INCREF(value);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002375 if (override) {
2376 if (PyDict_SetItem(d, key, value) < 0) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002377 Py_DECREF(key);
2378 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379 goto Fail;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002380 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002381 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002382 else if (PyDict_GetItemWithError(d, key) == NULL) {
2383 if (PyErr_Occurred() || PyDict_SetItem(d, key, value) < 0) {
2384 Py_DECREF(key);
2385 Py_DECREF(value);
2386 goto Fail;
2387 }
2388 }
2389
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002390 Py_DECREF(key);
2391 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392 Py_DECREF(fast);
2393 Py_DECREF(item);
2394 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002396 i = 0;
Victor Stinner611b0fa2016-09-14 15:02:01 +02002397 assert(_PyDict_CheckConsistency((PyDictObject *)d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00002399Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002400 Py_XDECREF(item);
2401 Py_XDECREF(fast);
2402 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002403Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002404 Py_DECREF(it);
2405 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00002406}
2407
doko@ubuntu.comc96df682016-10-11 08:04:02 +02002408static int
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002409dict_merge(PyObject *a, PyObject *b, int override)
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002410{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002411 PyDictObject *mp, *other;
2412 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -07002413 PyDictKeyEntry *entry, *ep0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002414
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002415 assert(0 <= override && override <= 2);
2416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002417 /* We accept for the argument either a concrete dictionary object,
2418 * or an abstract "mapping" object. For the former, we can do
2419 * things quite efficiently. For the latter, we only require that
2420 * PyMapping_Keys() and PyObject_GetItem() be supported.
2421 */
2422 if (a == NULL || !PyDict_Check(a) || b == NULL) {
2423 PyErr_BadInternalCall();
2424 return -1;
2425 }
2426 mp = (PyDictObject*)a;
INADA Naoki2aaf98c2018-09-26 12:59:00 +09002427 if (PyDict_Check(b) && (Py_TYPE(b)->tp_iter == (getiterfunc)dict_iter)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002428 other = (PyDictObject*)b;
2429 if (other == mp || other->ma_used == 0)
2430 /* a.update(a) or a.update({}); nothing to do */
2431 return 0;
2432 if (mp->ma_used == 0)
2433 /* Since the target dict is empty, PyDict_GetItem()
2434 * always returns NULL. Setting override to 1
2435 * skips the unnecessary test.
2436 */
2437 override = 1;
2438 /* Do one big resize at the start, rather than
2439 * incrementally resizing as we insert new items. Expect
2440 * that there will be no (or few) overlapping keys.
2441 */
INADA Naokib1152be2016-10-27 19:26:50 +09002442 if (USABLE_FRACTION(mp->ma_keys->dk_size) < other->ma_used) {
2443 if (dictresize(mp, ESTIMATE_SIZE(mp->ma_used + other->ma_used))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002444 return -1;
INADA Naokib1152be2016-10-27 19:26:50 +09002445 }
2446 }
Victor Stinner742da042016-09-07 17:40:12 -07002447 ep0 = DK_ENTRIES(other->ma_keys);
2448 for (i = 0, n = other->ma_keys->dk_nentries; i < n; i++) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002449 PyObject *key, *value;
2450 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002451 entry = &ep0[i];
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002452 key = entry->me_key;
2453 hash = entry->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002454 if (other->ma_values)
2455 value = other->ma_values[i];
2456 else
2457 value = entry->me_value;
2458
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002459 if (value != NULL) {
2460 int err = 0;
2461 Py_INCREF(key);
2462 Py_INCREF(value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002463 if (override == 1)
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002464 err = insertdict(mp, key, hash, value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002465 else if (_PyDict_GetItem_KnownHash(a, key, hash) == NULL) {
2466 if (PyErr_Occurred()) {
2467 Py_DECREF(value);
2468 Py_DECREF(key);
2469 return -1;
2470 }
2471 err = insertdict(mp, key, hash, value);
2472 }
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002473 else if (override != 0) {
2474 _PyErr_SetKeyError(key);
2475 Py_DECREF(value);
2476 Py_DECREF(key);
2477 return -1;
2478 }
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002479 Py_DECREF(value);
2480 Py_DECREF(key);
2481 if (err != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 return -1;
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002483
Victor Stinner742da042016-09-07 17:40:12 -07002484 if (n != other->ma_keys->dk_nentries) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002485 PyErr_SetString(PyExc_RuntimeError,
2486 "dict mutated during update");
2487 return -1;
2488 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002489 }
2490 }
2491 }
2492 else {
2493 /* Do it the generic, slower way */
2494 PyObject *keys = PyMapping_Keys(b);
2495 PyObject *iter;
2496 PyObject *key, *value;
2497 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002499 if (keys == NULL)
2500 /* Docstring says this is equivalent to E.keys() so
2501 * if E doesn't have a .keys() method we want
2502 * AttributeError to percolate up. Might as well
2503 * do the same for any other error.
2504 */
2505 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002507 iter = PyObject_GetIter(keys);
2508 Py_DECREF(keys);
2509 if (iter == NULL)
2510 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002512 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002513 if (override != 1) {
2514 if (PyDict_GetItemWithError(a, key) != NULL) {
2515 if (override != 0) {
2516 _PyErr_SetKeyError(key);
2517 Py_DECREF(key);
2518 Py_DECREF(iter);
2519 return -1;
2520 }
2521 Py_DECREF(key);
2522 continue;
2523 }
2524 else if (PyErr_Occurred()) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002525 Py_DECREF(key);
2526 Py_DECREF(iter);
2527 return -1;
2528 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002529 }
2530 value = PyObject_GetItem(b, key);
2531 if (value == NULL) {
2532 Py_DECREF(iter);
2533 Py_DECREF(key);
2534 return -1;
2535 }
2536 status = PyDict_SetItem(a, key, value);
2537 Py_DECREF(key);
2538 Py_DECREF(value);
2539 if (status < 0) {
2540 Py_DECREF(iter);
2541 return -1;
2542 }
2543 }
2544 Py_DECREF(iter);
2545 if (PyErr_Occurred())
2546 /* Iterator completed, via error */
2547 return -1;
2548 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02002549 assert(_PyDict_CheckConsistency((PyDictObject *)a));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002550 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002551}
2552
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002553int
2554PyDict_Update(PyObject *a, PyObject *b)
2555{
2556 return dict_merge(a, b, 1);
2557}
2558
2559int
2560PyDict_Merge(PyObject *a, PyObject *b, int override)
2561{
2562 /* XXX Deprecate override not in (0, 1). */
2563 return dict_merge(a, b, override != 0);
2564}
2565
2566int
2567_PyDict_MergeEx(PyObject *a, PyObject *b, int override)
2568{
2569 return dict_merge(a, b, override);
2570}
2571
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002572static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302573dict_copy(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002574{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002575 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002576}
2577
2578PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002579PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002580{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002581 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002582 PyDictObject *mp;
2583 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002585 if (o == NULL || !PyDict_Check(o)) {
2586 PyErr_BadInternalCall();
2587 return NULL;
2588 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002589
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002590 mp = (PyDictObject *)o;
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002591 if (mp->ma_used == 0) {
2592 /* The dict is empty; just return a new dict. */
2593 return PyDict_New();
2594 }
2595
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002596 if (_PyDict_HasSplitTable(mp)) {
2597 PyDictObject *split_copy;
Victor Stinner742da042016-09-07 17:40:12 -07002598 Py_ssize_t size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
2599 PyObject **newvalues;
2600 newvalues = new_values(size);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002601 if (newvalues == NULL)
2602 return PyErr_NoMemory();
2603 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2604 if (split_copy == NULL) {
2605 free_values(newvalues);
2606 return NULL;
2607 }
2608 split_copy->ma_values = newvalues;
2609 split_copy->ma_keys = mp->ma_keys;
2610 split_copy->ma_used = mp->ma_used;
INADA Naokid1c82c52018-04-03 11:43:53 +09002611 split_copy->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokia7576492018-11-14 18:39:27 +09002612 dictkeys_incref(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002613 for (i = 0, n = size; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002614 PyObject *value = mp->ma_values[i];
2615 Py_XINCREF(value);
2616 split_copy->ma_values[i] = value;
2617 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002618 if (_PyObject_GC_IS_TRACKED(mp))
2619 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002620 return (PyObject *)split_copy;
2621 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002622
2623 if (PyDict_CheckExact(mp) && mp->ma_values == NULL &&
2624 (mp->ma_used >= (mp->ma_keys->dk_nentries * 2) / 3))
2625 {
2626 /* Use fast-copy if:
2627
2628 (1) 'mp' is an instance of a subclassed dict; and
2629
2630 (2) 'mp' is not a split-dict; and
2631
2632 (3) if 'mp' is non-compact ('del' operation does not resize dicts),
2633 do fast-copy only if it has at most 1/3 non-used keys.
2634
Ville Skyttä61f82e02018-04-20 23:08:45 +03002635 The last condition (3) is important to guard against a pathological
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002636 case when a large dict is almost emptied with multiple del/pop
2637 operations and copied after that. In cases like this, we defer to
2638 PyDict_Merge, which produces a compacted copy.
2639 */
2640 return clone_combined_dict(mp);
2641 }
2642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002643 copy = PyDict_New();
2644 if (copy == NULL)
2645 return NULL;
2646 if (PyDict_Merge(copy, o, 1) == 0)
2647 return copy;
2648 Py_DECREF(copy);
2649 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002650}
2651
Martin v. Löwis18e16552006-02-15 17:27:45 +00002652Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002653PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002654{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002655 if (mp == NULL || !PyDict_Check(mp)) {
2656 PyErr_BadInternalCall();
2657 return -1;
2658 }
2659 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002660}
2661
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002662PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002663PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002664{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002665 if (mp == NULL || !PyDict_Check(mp)) {
2666 PyErr_BadInternalCall();
2667 return NULL;
2668 }
2669 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002670}
2671
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002672PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002673PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002674{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002675 if (mp == NULL || !PyDict_Check(mp)) {
2676 PyErr_BadInternalCall();
2677 return NULL;
2678 }
2679 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002680}
2681
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002682PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002683PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002684{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002685 if (mp == NULL || !PyDict_Check(mp)) {
2686 PyErr_BadInternalCall();
2687 return NULL;
2688 }
2689 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002690}
2691
Tim Peterse63415e2001-05-08 04:38:29 +00002692/* Return 1 if dicts equal, 0 if not, -1 if error.
2693 * Gets out as soon as any difference is detected.
2694 * Uses only Py_EQ comparison.
2695 */
2696static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002697dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002698{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002699 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002701 if (a->ma_used != b->ma_used)
2702 /* can't be equal if # of entries differ */
2703 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002704 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Victor Stinner742da042016-09-07 17:40:12 -07002705 for (i = 0; i < a->ma_keys->dk_nentries; i++) {
2706 PyDictKeyEntry *ep = &DK_ENTRIES(a->ma_keys)[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002707 PyObject *aval;
2708 if (a->ma_values)
2709 aval = a->ma_values[i];
2710 else
2711 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002712 if (aval != NULL) {
2713 int cmp;
2714 PyObject *bval;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002715 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002716 /* temporarily bump aval's refcount to ensure it stays
2717 alive until we're done with it */
2718 Py_INCREF(aval);
2719 /* ditto for key */
2720 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002721 /* reuse the known hash value */
INADA Naoki778928b2017-08-03 23:45:15 +09002722 b->ma_keys->dk_lookup(b, key, ep->me_hash, &bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002723 if (bval == NULL) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002724 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002725 Py_DECREF(aval);
2726 if (PyErr_Occurred())
2727 return -1;
2728 return 0;
2729 }
2730 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002731 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002732 Py_DECREF(aval);
2733 if (cmp <= 0) /* error or not equal */
2734 return cmp;
2735 }
2736 }
2737 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002738}
Tim Peterse63415e2001-05-08 04:38:29 +00002739
2740static PyObject *
2741dict_richcompare(PyObject *v, PyObject *w, int op)
2742{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002743 int cmp;
2744 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002745
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002746 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2747 res = Py_NotImplemented;
2748 }
2749 else if (op == Py_EQ || op == Py_NE) {
2750 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2751 if (cmp < 0)
2752 return NULL;
2753 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2754 }
2755 else
2756 res = Py_NotImplemented;
2757 Py_INCREF(res);
2758 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002759}
Tim Peterse63415e2001-05-08 04:38:29 +00002760
Larry Hastings61272b72014-01-07 12:41:53 -08002761/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002762
2763@coexist
2764dict.__contains__
2765
2766 key: object
2767 /
2768
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002769True if the dictionary has the specified key, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002770[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002771
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002772static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002773dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka19d25972017-02-04 08:05:07 +02002774/*[clinic end generated code: output=a3d03db709ed6e6b input=fe1cb42ad831e820]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002775{
Larry Hastingsc2047262014-01-25 20:43:29 -08002776 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002777 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002778 Py_ssize_t ix;
INADA Naokiba609772016-12-07 20:41:42 +09002779 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002781 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002782 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002783 hash = PyObject_Hash(key);
2784 if (hash == -1)
2785 return NULL;
2786 }
INADA Naoki778928b2017-08-03 23:45:15 +09002787 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002788 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002789 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002790 if (ix == DKIX_EMPTY || value == NULL)
Victor Stinner742da042016-09-07 17:40:12 -07002791 Py_RETURN_FALSE;
2792 Py_RETURN_TRUE;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002793}
2794
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002795/*[clinic input]
2796dict.get
2797
2798 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002799 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002800 /
2801
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002802Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002803[clinic start generated code]*/
2804
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002805static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002806dict_get_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002807/*[clinic end generated code: output=bba707729dee05bf input=279ddb5790b6b107]*/
Barry Warsawc38c5da1997-10-06 17:49:20 +00002808{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002809 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002810 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002811 Py_ssize_t ix;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002813 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002814 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002815 hash = PyObject_Hash(key);
2816 if (hash == -1)
2817 return NULL;
2818 }
INADA Naoki778928b2017-08-03 23:45:15 +09002819 ix = (self->ma_keys->dk_lookup) (self, key, hash, &val);
Victor Stinner742da042016-09-07 17:40:12 -07002820 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002821 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002822 if (ix == DKIX_EMPTY || val == NULL) {
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002823 val = default_value;
INADA Naokiba609772016-12-07 20:41:42 +09002824 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002825 Py_INCREF(val);
2826 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002827}
2828
Benjamin Peterson00e98862013-03-07 22:16:29 -05002829PyObject *
2830PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002831{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002832 PyDictObject *mp = (PyDictObject *)d;
INADA Naoki93f26f72016-11-02 18:45:16 +09002833 PyObject *value;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002834 Py_hash_t hash;
Guido van Rossum164452c2000-08-08 16:12:54 +00002835
Benjamin Peterson00e98862013-03-07 22:16:29 -05002836 if (!PyDict_Check(d)) {
2837 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002838 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002839 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002840
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002841 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002842 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002843 hash = PyObject_Hash(key);
2844 if (hash == -1)
2845 return NULL;
2846 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002847
2848 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
2849 if (insertion_resize(mp) < 0)
2850 return NULL;
2851 }
2852
INADA Naoki778928b2017-08-03 23:45:15 +09002853 Py_ssize_t ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002854 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002855 return NULL;
INADA Naoki93f26f72016-11-02 18:45:16 +09002856
2857 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09002858 ((ix >= 0 && value == NULL && mp->ma_used != ix) ||
INADA Naoki93f26f72016-11-02 18:45:16 +09002859 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
2860 if (insertion_resize(mp) < 0) {
2861 return NULL;
2862 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002863 ix = DKIX_EMPTY;
2864 }
2865
2866 if (ix == DKIX_EMPTY) {
2867 PyDictKeyEntry *ep, *ep0;
2868 value = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002869 if (mp->ma_keys->dk_usable <= 0) {
Victor Stinner3c336c52016-09-12 14:17:40 +02002870 if (insertion_resize(mp) < 0) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002871 return NULL;
Victor Stinner3c336c52016-09-12 14:17:40 +02002872 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002873 }
INADA Naoki778928b2017-08-03 23:45:15 +09002874 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naoki93f26f72016-11-02 18:45:16 +09002875 ep0 = DK_ENTRIES(mp->ma_keys);
2876 ep = &ep0[mp->ma_keys->dk_nentries];
INADA Naokia7576492018-11-14 18:39:27 +09002877 dictkeys_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002878 Py_INCREF(key);
INADA Naoki93f26f72016-11-02 18:45:16 +09002879 Py_INCREF(value);
2880 MAINTAIN_TRACKING(mp, key, value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002881 ep->me_key = key;
2882 ep->me_hash = hash;
INADA Naokiba609772016-12-07 20:41:42 +09002883 if (_PyDict_HasSplitTable(mp)) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002884 assert(mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
2885 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Victor Stinner742da042016-09-07 17:40:12 -07002886 }
2887 else {
INADA Naoki93f26f72016-11-02 18:45:16 +09002888 ep->me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07002889 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002890 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002891 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naoki93f26f72016-11-02 18:45:16 +09002892 mp->ma_keys->dk_usable--;
2893 mp->ma_keys->dk_nentries++;
2894 assert(mp->ma_keys->dk_usable >= 0);
2895 }
INADA Naokiba609772016-12-07 20:41:42 +09002896 else if (value == NULL) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002897 value = defaultobj;
2898 assert(_PyDict_HasSplitTable(mp));
2899 assert(ix == mp->ma_used);
2900 Py_INCREF(value);
2901 MAINTAIN_TRACKING(mp, key, value);
INADA Naokiba609772016-12-07 20:41:42 +09002902 mp->ma_values[ix] = value;
INADA Naoki93f26f72016-11-02 18:45:16 +09002903 mp->ma_used++;
2904 mp->ma_version_tag = DICT_NEXT_VERSION();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002905 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002906
2907 assert(_PyDict_CheckConsistency(mp));
2908 return value;
Guido van Rossum164452c2000-08-08 16:12:54 +00002909}
2910
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002911/*[clinic input]
2912dict.setdefault
2913
2914 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002915 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002916 /
2917
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002918Insert key with a value of default if key is not in the dictionary.
2919
2920Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002921[clinic start generated code]*/
2922
Benjamin Peterson00e98862013-03-07 22:16:29 -05002923static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002924dict_setdefault_impl(PyDictObject *self, PyObject *key,
2925 PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002926/*[clinic end generated code: output=f8c1101ebf69e220 input=0f063756e815fd9d]*/
Benjamin Peterson00e98862013-03-07 22:16:29 -05002927{
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002928 PyObject *val;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002929
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002930 val = PyDict_SetDefault((PyObject *)self, key, default_value);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002931 Py_XINCREF(val);
2932 return val;
2933}
Guido van Rossum164452c2000-08-08 16:12:54 +00002934
2935static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302936dict_clear(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002937{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002938 PyDict_Clear((PyObject *)mp);
2939 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002940}
2941
Guido van Rossumba6ab842000-12-12 22:02:18 +00002942static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002943dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002944{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002945 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00002946
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002947 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2948 return NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06002949
Yury Selivanov684ef2c2016-10-28 19:01:21 -04002950 return _PyDict_Pop((PyObject*)mp, key, deflt);
Guido van Rossume027d982002-04-12 15:11:59 +00002951}
2952
2953static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302954dict_popitem(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Guido van Rossumba6ab842000-12-12 22:02:18 +00002955{
Victor Stinner742da042016-09-07 17:40:12 -07002956 Py_ssize_t i, j;
2957 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002958 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002960 /* Allocate the result tuple before checking the size. Believe it
2961 * or not, this allocation could trigger a garbage collection which
2962 * could empty the dict, so if we checked the size first and that
2963 * happened, the result would be an infinite loop (searching for an
2964 * entry that no longer exists). Note that the usual popitem()
2965 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2966 * tuple away if the dict *is* empty isn't a significant
2967 * inefficiency -- possible, but unlikely in practice.
2968 */
2969 res = PyTuple_New(2);
2970 if (res == NULL)
2971 return NULL;
2972 if (mp->ma_used == 0) {
2973 Py_DECREF(res);
2974 PyErr_SetString(PyExc_KeyError,
2975 "popitem(): dictionary is empty");
2976 return NULL;
2977 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002978 /* Convert split table to combined table */
2979 if (mp->ma_keys->dk_lookup == lookdict_split) {
2980 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
2981 Py_DECREF(res);
2982 return NULL;
2983 }
2984 }
2985 ENSURE_ALLOWS_DELETIONS(mp);
Victor Stinner742da042016-09-07 17:40:12 -07002986
2987 /* Pop last item */
2988 ep0 = DK_ENTRIES(mp->ma_keys);
2989 i = mp->ma_keys->dk_nentries - 1;
2990 while (i >= 0 && ep0[i].me_value == NULL) {
2991 i--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002992 }
Victor Stinner742da042016-09-07 17:40:12 -07002993 assert(i >= 0);
2994
2995 ep = &ep0[i];
2996 j = lookdict_index(mp->ma_keys, ep->me_hash, i);
2997 assert(j >= 0);
INADA Naokia7576492018-11-14 18:39:27 +09002998 assert(dictkeys_get_index(mp->ma_keys, j) == i);
2999 dictkeys_set_index(mp->ma_keys, j, DKIX_DUMMY);
Victor Stinner742da042016-09-07 17:40:12 -07003000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003001 PyTuple_SET_ITEM(res, 0, ep->me_key);
3002 PyTuple_SET_ITEM(res, 1, ep->me_value);
Victor Stinner742da042016-09-07 17:40:12 -07003003 ep->me_key = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003004 ep->me_value = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07003005 /* We can't dk_usable++ since there is DKIX_DUMMY in indices */
3006 mp->ma_keys->dk_nentries = i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003007 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003008 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +02003009 assert(_PyDict_CheckConsistency(mp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003010 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00003011}
3012
Jeremy Hylton8caad492000-06-23 14:18:11 +00003013static int
3014dict_traverse(PyObject *op, visitproc visit, void *arg)
3015{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003016 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson55f44522016-09-05 12:12:59 -07003017 PyDictKeysObject *keys = mp->ma_keys;
Serhiy Storchaka46825d22016-09-26 21:29:34 +03003018 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Victor Stinner742da042016-09-07 17:40:12 -07003019 Py_ssize_t i, n = keys->dk_nentries;
3020
Benjamin Peterson55f44522016-09-05 12:12:59 -07003021 if (keys->dk_lookup == lookdict) {
3022 for (i = 0; i < n; i++) {
3023 if (entries[i].me_value != NULL) {
3024 Py_VISIT(entries[i].me_value);
3025 Py_VISIT(entries[i].me_key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003026 }
3027 }
Victor Stinner742da042016-09-07 17:40:12 -07003028 }
3029 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003030 if (mp->ma_values != NULL) {
Benjamin Peterson55f44522016-09-05 12:12:59 -07003031 for (i = 0; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003032 Py_VISIT(mp->ma_values[i]);
3033 }
3034 }
3035 else {
Benjamin Peterson55f44522016-09-05 12:12:59 -07003036 for (i = 0; i < n; i++) {
3037 Py_VISIT(entries[i].me_value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003038 }
3039 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003040 }
3041 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00003042}
3043
3044static int
3045dict_tp_clear(PyObject *op)
3046{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003047 PyDict_Clear(op);
3048 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00003049}
3050
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003051static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003052
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003053Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003054_PyDict_SizeOf(PyDictObject *mp)
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003055{
Victor Stinner742da042016-09-07 17:40:12 -07003056 Py_ssize_t size, usable, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003057
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003058 size = DK_SIZE(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07003059 usable = USABLE_FRACTION(size);
3060
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02003061 res = _PyObject_SIZE(Py_TYPE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003062 if (mp->ma_values)
Victor Stinner742da042016-09-07 17:40:12 -07003063 res += usable * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003064 /* If the dictionary is split, the keys portion is accounted-for
3065 in the type object. */
3066 if (mp->ma_keys->dk_refcnt == 1)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003067 res += (sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003068 + DK_IXSIZE(mp->ma_keys) * size
3069 + sizeof(PyDictKeyEntry) * usable);
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003070 return res;
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003071}
3072
3073Py_ssize_t
3074_PyDict_KeysSize(PyDictKeysObject *keys)
3075{
Victor Stinner98ee9d52016-09-08 09:33:56 -07003076 return (sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003077 + DK_IXSIZE(keys) * DK_SIZE(keys)
3078 + USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry));
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003079}
3080
doko@ubuntu.com17210f52016-01-14 14:04:59 +01003081static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303082dict_sizeof(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003083{
3084 return PyLong_FromSsize_t(_PyDict_SizeOf(mp));
3085}
3086
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00003087PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
3088
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003089PyDoc_STRVAR(sizeof__doc__,
3090"D.__sizeof__() -> size of D in memory, in bytes");
3091
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003092PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00003093"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00003094If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00003095
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003096PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00003097"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000030982-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003099
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003100PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04003101"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
3102If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
3103If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
3104In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00003105
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003106PyDoc_STRVAR(clear__doc__,
3107"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003108
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003109PyDoc_STRVAR(copy__doc__,
3110"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00003111
Guido van Rossumb90c8482007-02-10 01:11:45 +00003112/* Forward */
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303113static PyObject *dictkeys_new(PyObject *, PyObject *);
3114static PyObject *dictitems_new(PyObject *, PyObject *);
3115static PyObject *dictvalues_new(PyObject *, PyObject *);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003116
Guido van Rossum45c85d12007-07-27 16:31:40 +00003117PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003118 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003119PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003120 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003121PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003122 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00003123
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003124static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07003125 DICT___CONTAINS___METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003126 {"__getitem__", (PyCFunction)(void(*)(void))dict_subscript, METH_O | METH_COEXIST,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003127 getitem__doc__},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003128 {"__sizeof__", (PyCFunction)(void(*)(void))dict_sizeof, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003129 sizeof__doc__},
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003130 DICT_GET_METHODDEF
3131 DICT_SETDEFAULT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003132 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
3133 pop__doc__},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003134 {"popitem", (PyCFunction)(void(*)(void))dict_popitem, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003135 popitem__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303136 {"keys", dictkeys_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003137 keys__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303138 {"items", dictitems_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003139 items__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303140 {"values", dictvalues_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003141 values__doc__},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003142 {"update", (PyCFunction)(void(*)(void))dict_update, METH_VARARGS | METH_KEYWORDS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003143 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08003144 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003145 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
3146 clear__doc__},
3147 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
3148 copy__doc__},
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003149 DICT___REVERSED___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003150 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003151};
3152
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00003153/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00003154int
3155PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003156{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003157 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07003158 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003159 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003160 PyObject *value;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003162 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003163 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003164 hash = PyObject_Hash(key);
3165 if (hash == -1)
3166 return -1;
3167 }
INADA Naoki778928b2017-08-03 23:45:15 +09003168 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003169 if (ix == DKIX_ERROR)
3170 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003171 return (ix != DKIX_EMPTY && value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003172}
3173
Thomas Wouterscf297e42007-02-23 15:07:44 +00003174/* Internal version of PyDict_Contains used when the hash value is already known */
3175int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003176_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00003177{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003178 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003179 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -07003180 Py_ssize_t ix;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003181
INADA Naoki778928b2017-08-03 23:45:15 +09003182 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003183 if (ix == DKIX_ERROR)
3184 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003185 return (ix != DKIX_EMPTY && value != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00003186}
3187
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003188/* Hack to implement "key in dict" */
3189static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003190 0, /* sq_length */
3191 0, /* sq_concat */
3192 0, /* sq_repeat */
3193 0, /* sq_item */
3194 0, /* sq_slice */
3195 0, /* sq_ass_item */
3196 0, /* sq_ass_slice */
3197 PyDict_Contains, /* sq_contains */
3198 0, /* sq_inplace_concat */
3199 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003200};
3201
Guido van Rossum09e563a2001-05-01 12:10:21 +00003202static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003203dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3204{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003205 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003206 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003208 assert(type != NULL && type->tp_alloc != NULL);
3209 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02003210 if (self == NULL)
3211 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003212 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003213
Victor Stinnera9f61a52013-07-16 22:17:26 +02003214 /* The object has been implicitly tracked by tp_alloc */
3215 if (type == &PyDict_Type)
3216 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003217
3218 d->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003219 d->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07003220 d->ma_keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003221 if (d->ma_keys == NULL) {
3222 Py_DECREF(self);
3223 return NULL;
3224 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02003225 assert(_PyDict_CheckConsistency(d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003226 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003227}
3228
Tim Peters25786c02001-09-02 08:22:48 +00003229static int
3230dict_init(PyObject *self, PyObject *args, PyObject *kwds)
3231{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003232 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00003233}
3234
Tim Peters6d6c1a32001-08-02 04:15:00 +00003235static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003236dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00003237{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003238 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003239}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003240
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003241PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00003242"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00003243"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003244" (key, value) pairs\n"
3245"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00003246" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003247" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00003248" d[k] = v\n"
3249"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
3250" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00003251
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003252PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003253 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3254 "dict",
3255 sizeof(PyDictObject),
3256 0,
3257 (destructor)dict_dealloc, /* tp_dealloc */
3258 0, /* tp_print */
3259 0, /* tp_getattr */
3260 0, /* tp_setattr */
3261 0, /* tp_reserved */
3262 (reprfunc)dict_repr, /* tp_repr */
3263 0, /* tp_as_number */
3264 &dict_as_sequence, /* tp_as_sequence */
3265 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003266 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003267 0, /* tp_call */
3268 0, /* tp_str */
3269 PyObject_GenericGetAttr, /* tp_getattro */
3270 0, /* tp_setattro */
3271 0, /* tp_as_buffer */
3272 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3273 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
3274 dictionary_doc, /* tp_doc */
3275 dict_traverse, /* tp_traverse */
3276 dict_tp_clear, /* tp_clear */
3277 dict_richcompare, /* tp_richcompare */
3278 0, /* tp_weaklistoffset */
3279 (getiterfunc)dict_iter, /* tp_iter */
3280 0, /* tp_iternext */
3281 mapp_methods, /* tp_methods */
3282 0, /* tp_members */
3283 0, /* tp_getset */
3284 0, /* tp_base */
3285 0, /* tp_dict */
3286 0, /* tp_descr_get */
3287 0, /* tp_descr_set */
3288 0, /* tp_dictoffset */
3289 dict_init, /* tp_init */
3290 PyType_GenericAlloc, /* tp_alloc */
3291 dict_new, /* tp_new */
3292 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003293};
3294
Victor Stinner3c1e4812012-03-26 22:10:51 +02003295PyObject *
3296_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
3297{
3298 PyObject *kv;
3299 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02003300 if (kv == NULL) {
3301 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003302 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02003303 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02003304 return PyDict_GetItem(dp, kv);
3305}
3306
Guido van Rossum3cca2451997-05-16 14:23:33 +00003307/* For backward compatibility with old dictionary interface */
3308
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003309PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003310PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003311{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003312 PyObject *kv, *rv;
3313 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003314 if (kv == NULL) {
3315 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003316 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003317 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003318 rv = PyDict_GetItem(v, kv);
3319 Py_DECREF(kv);
3320 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003321}
3322
3323int
Victor Stinner3c1e4812012-03-26 22:10:51 +02003324_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
3325{
3326 PyObject *kv;
3327 kv = _PyUnicode_FromId(key); /* borrowed */
3328 if (kv == NULL)
3329 return -1;
3330 return PyDict_SetItem(v, kv, item);
3331}
3332
3333int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003334PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003335{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003336 PyObject *kv;
3337 int err;
3338 kv = PyUnicode_FromString(key);
3339 if (kv == NULL)
3340 return -1;
3341 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
3342 err = PyDict_SetItem(v, kv, item);
3343 Py_DECREF(kv);
3344 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003345}
3346
3347int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01003348_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
3349{
3350 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
3351 if (kv == NULL)
3352 return -1;
3353 return PyDict_DelItem(v, kv);
3354}
3355
3356int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003357PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003358{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003359 PyObject *kv;
3360 int err;
3361 kv = PyUnicode_FromString(key);
3362 if (kv == NULL)
3363 return -1;
3364 err = PyDict_DelItem(v, kv);
3365 Py_DECREF(kv);
3366 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003367}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003368
Raymond Hettinger019a1482004-03-18 02:41:19 +00003369/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003370
3371typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003372 PyObject_HEAD
3373 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
3374 Py_ssize_t di_used;
3375 Py_ssize_t di_pos;
3376 PyObject* di_result; /* reusable result tuple for iteritems */
3377 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003378} dictiterobject;
3379
3380static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003381dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003382{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003383 dictiterobject *di;
3384 di = PyObject_GC_New(dictiterobject, itertype);
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003385 if (di == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003386 return NULL;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003387 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003388 Py_INCREF(dict);
3389 di->di_dict = dict;
3390 di->di_used = dict->ma_used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003391 di->len = dict->ma_used;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003392 if ((itertype == &PyDictRevIterKey_Type ||
3393 itertype == &PyDictRevIterItem_Type ||
3394 itertype == &PyDictRevIterValue_Type) && dict->ma_used) {
3395 di->di_pos = dict->ma_keys->dk_nentries - 1;
3396 }
3397 else {
3398 di->di_pos = 0;
3399 }
3400 if (itertype == &PyDictIterItem_Type ||
3401 itertype == &PyDictRevIterItem_Type) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003402 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
3403 if (di->di_result == NULL) {
3404 Py_DECREF(di);
3405 return NULL;
3406 }
3407 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003408 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003409 di->di_result = NULL;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003410 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003411 _PyObject_GC_TRACK(di);
3412 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003413}
3414
3415static void
3416dictiter_dealloc(dictiterobject *di)
3417{
INADA Naokia6296d32017-08-24 14:55:17 +09003418 /* bpo-31095: UnTrack is needed before calling any callbacks */
3419 _PyObject_GC_UNTRACK(di);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003420 Py_XDECREF(di->di_dict);
3421 Py_XDECREF(di->di_result);
3422 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003423}
3424
3425static int
3426dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
3427{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003428 Py_VISIT(di->di_dict);
3429 Py_VISIT(di->di_result);
3430 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003431}
3432
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003433static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303434dictiter_len(dictiterobject *di, PyObject *Py_UNUSED(ignored))
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003435{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003436 Py_ssize_t len = 0;
3437 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
3438 len = di->len;
3439 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003440}
3441
Guido van Rossumb90c8482007-02-10 01:11:45 +00003442PyDoc_STRVAR(length_hint_doc,
3443 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003444
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003445static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303446dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003447
3448PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3449
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003450static PyMethodDef dictiter_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003451 {"__length_hint__", (PyCFunction)(void(*)(void))dictiter_len, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003452 length_hint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003453 {"__reduce__", (PyCFunction)(void(*)(void))dictiter_reduce, METH_NOARGS,
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003454 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003455 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003456};
3457
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003458static PyObject*
3459dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00003460{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003461 PyObject *key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003462 Py_ssize_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003463 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003464 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00003465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003466 if (d == NULL)
3467 return NULL;
3468 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00003469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003470 if (di->di_used != d->ma_used) {
3471 PyErr_SetString(PyExc_RuntimeError,
3472 "dictionary changed size during iteration");
3473 di->di_used = -1; /* Make this state sticky */
3474 return NULL;
3475 }
Guido van Rossum2147df72002-07-16 20:30:22 +00003476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003477 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003478 k = d->ma_keys;
INADA Naokica2d8be2016-11-04 16:59:10 +09003479 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003480 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003481 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003482 goto fail;
3483 key = DK_ENTRIES(k)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003484 assert(d->ma_values[i] != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003485 }
3486 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003487 Py_ssize_t n = k->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003488 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
3489 while (i < n && entry_ptr->me_value == NULL) {
3490 entry_ptr++;
3491 i++;
3492 }
3493 if (i >= n)
3494 goto fail;
3495 key = entry_ptr->me_key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003496 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003497 di->di_pos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003498 di->len--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003499 Py_INCREF(key);
3500 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003501
3502fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003503 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003504 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003505 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003506}
3507
Raymond Hettinger019a1482004-03-18 02:41:19 +00003508PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003509 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3510 "dict_keyiterator", /* tp_name */
3511 sizeof(dictiterobject), /* tp_basicsize */
3512 0, /* tp_itemsize */
3513 /* methods */
3514 (destructor)dictiter_dealloc, /* tp_dealloc */
3515 0, /* tp_print */
3516 0, /* tp_getattr */
3517 0, /* tp_setattr */
3518 0, /* tp_reserved */
3519 0, /* tp_repr */
3520 0, /* tp_as_number */
3521 0, /* tp_as_sequence */
3522 0, /* tp_as_mapping */
3523 0, /* tp_hash */
3524 0, /* tp_call */
3525 0, /* tp_str */
3526 PyObject_GenericGetAttr, /* tp_getattro */
3527 0, /* tp_setattro */
3528 0, /* tp_as_buffer */
3529 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3530 0, /* tp_doc */
3531 (traverseproc)dictiter_traverse, /* tp_traverse */
3532 0, /* tp_clear */
3533 0, /* tp_richcompare */
3534 0, /* tp_weaklistoffset */
3535 PyObject_SelfIter, /* tp_iter */
3536 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
3537 dictiter_methods, /* tp_methods */
3538 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003539};
3540
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003541static PyObject *
3542dictiter_iternextvalue(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003543{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003544 PyObject *value;
INADA Naokica2d8be2016-11-04 16:59:10 +09003545 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003546 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003548 if (d == NULL)
3549 return NULL;
3550 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003552 if (di->di_used != d->ma_used) {
3553 PyErr_SetString(PyExc_RuntimeError,
3554 "dictionary changed size during iteration");
3555 di->di_used = -1; /* Make this state sticky */
3556 return NULL;
3557 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003559 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003560 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003561 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003562 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003563 goto fail;
INADA Naokica2d8be2016-11-04 16:59:10 +09003564 value = d->ma_values[i];
3565 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003566 }
3567 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003568 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003569 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3570 while (i < n && entry_ptr->me_value == NULL) {
3571 entry_ptr++;
3572 i++;
3573 }
3574 if (i >= n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003575 goto fail;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003576 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003577 }
3578 di->di_pos = i+1;
3579 di->len--;
3580 Py_INCREF(value);
3581 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003582
3583fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003584 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003585 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003586 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003587}
3588
3589PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003590 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3591 "dict_valueiterator", /* tp_name */
3592 sizeof(dictiterobject), /* tp_basicsize */
3593 0, /* tp_itemsize */
3594 /* methods */
3595 (destructor)dictiter_dealloc, /* tp_dealloc */
3596 0, /* tp_print */
3597 0, /* tp_getattr */
3598 0, /* tp_setattr */
3599 0, /* tp_reserved */
3600 0, /* tp_repr */
3601 0, /* tp_as_number */
3602 0, /* tp_as_sequence */
3603 0, /* tp_as_mapping */
3604 0, /* tp_hash */
3605 0, /* tp_call */
3606 0, /* tp_str */
3607 PyObject_GenericGetAttr, /* tp_getattro */
3608 0, /* tp_setattro */
3609 0, /* tp_as_buffer */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003610 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003611 0, /* tp_doc */
3612 (traverseproc)dictiter_traverse, /* tp_traverse */
3613 0, /* tp_clear */
3614 0, /* tp_richcompare */
3615 0, /* tp_weaklistoffset */
3616 PyObject_SelfIter, /* tp_iter */
3617 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3618 dictiter_methods, /* tp_methods */
3619 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003620};
3621
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003622static PyObject *
3623dictiter_iternextitem(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003624{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003625 PyObject *key, *value, *result;
INADA Naokica2d8be2016-11-04 16:59:10 +09003626 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003627 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003629 if (d == NULL)
3630 return NULL;
3631 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003633 if (di->di_used != d->ma_used) {
3634 PyErr_SetString(PyExc_RuntimeError,
3635 "dictionary changed size during iteration");
3636 di->di_used = -1; /* Make this state sticky */
3637 return NULL;
3638 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003640 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003641 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003642 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003643 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003644 goto fail;
3645 key = DK_ENTRIES(d->ma_keys)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003646 value = d->ma_values[i];
3647 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003648 }
3649 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003650 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003651 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3652 while (i < n && entry_ptr->me_value == NULL) {
3653 entry_ptr++;
3654 i++;
3655 }
3656 if (i >= n)
3657 goto fail;
3658 key = entry_ptr->me_key;
3659 value = entry_ptr->me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003660 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003661 di->di_pos = i+1;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003662 di->len--;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003663 Py_INCREF(key);
3664 Py_INCREF(value);
3665 result = di->di_result;
3666 if (Py_REFCNT(result) == 1) {
3667 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3668 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3669 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3670 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003671 Py_INCREF(result);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003672 Py_DECREF(oldkey);
3673 Py_DECREF(oldvalue);
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003674 }
3675 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003676 result = PyTuple_New(2);
3677 if (result == NULL)
3678 return NULL;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003679 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3680 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003681 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003682 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003683
3684fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003685 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003686 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003687 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003688}
3689
3690PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003691 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3692 "dict_itemiterator", /* tp_name */
3693 sizeof(dictiterobject), /* tp_basicsize */
3694 0, /* tp_itemsize */
3695 /* methods */
3696 (destructor)dictiter_dealloc, /* tp_dealloc */
3697 0, /* tp_print */
3698 0, /* tp_getattr */
3699 0, /* tp_setattr */
3700 0, /* tp_reserved */
3701 0, /* tp_repr */
3702 0, /* tp_as_number */
3703 0, /* tp_as_sequence */
3704 0, /* tp_as_mapping */
3705 0, /* tp_hash */
3706 0, /* tp_call */
3707 0, /* tp_str */
3708 PyObject_GenericGetAttr, /* tp_getattro */
3709 0, /* tp_setattro */
3710 0, /* tp_as_buffer */
3711 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3712 0, /* tp_doc */
3713 (traverseproc)dictiter_traverse, /* tp_traverse */
3714 0, /* tp_clear */
3715 0, /* tp_richcompare */
3716 0, /* tp_weaklistoffset */
3717 PyObject_SelfIter, /* tp_iter */
3718 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3719 dictiter_methods, /* tp_methods */
3720 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003721};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003722
3723
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003724/* dictreviter */
3725
3726static PyObject *
3727dictreviter_iternext(dictiterobject *di)
3728{
3729 PyDictObject *d = di->di_dict;
3730
3731 if (d == NULL) {
3732 return NULL;
3733 }
3734 assert (PyDict_Check(d));
3735
3736 if (di->di_used != d->ma_used) {
3737 PyErr_SetString(PyExc_RuntimeError,
3738 "dictionary changed size during iteration");
3739 di->di_used = -1; /* Make this state sticky */
3740 return NULL;
3741 }
3742
3743 Py_ssize_t i = di->di_pos;
3744 PyDictKeysObject *k = d->ma_keys;
3745 PyObject *key, *value, *result;
3746
3747 if (d->ma_values) {
3748 if (i < 0) {
3749 goto fail;
3750 }
3751 key = DK_ENTRIES(k)[i].me_key;
3752 value = d->ma_values[i];
3753 assert (value != NULL);
3754 }
3755 else {
3756 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
3757 while (i >= 0 && entry_ptr->me_value == NULL) {
3758 entry_ptr--;
3759 i--;
3760 }
3761 if (i < 0) {
3762 goto fail;
3763 }
3764 key = entry_ptr->me_key;
3765 value = entry_ptr->me_value;
3766 }
3767 di->di_pos = i-1;
3768 di->len--;
3769
3770 if (Py_TYPE(di) == &PyDictRevIterKey_Type) {
3771 Py_INCREF(key);
3772 return key;
3773 }
3774 else if (Py_TYPE(di) == &PyDictRevIterValue_Type) {
3775 Py_INCREF(value);
3776 return value;
3777 }
3778 else if (Py_TYPE(di) == &PyDictRevIterItem_Type) {
3779 Py_INCREF(key);
3780 Py_INCREF(value);
3781 result = di->di_result;
3782 if (Py_REFCNT(result) == 1) {
3783 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3784 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3785 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3786 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
3787 Py_INCREF(result);
3788 Py_DECREF(oldkey);
3789 Py_DECREF(oldvalue);
3790 }
3791 else {
3792 result = PyTuple_New(2);
3793 if (result == NULL) {
3794 return NULL;
3795 }
3796 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3797 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
3798 }
3799 return result;
3800 }
3801 else {
3802 Py_UNREACHABLE();
3803 }
3804
3805fail:
3806 di->di_dict = NULL;
3807 Py_DECREF(d);
3808 return NULL;
3809}
3810
3811PyTypeObject PyDictRevIterKey_Type = {
3812 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3813 "dict_reversekeyiterator",
3814 sizeof(dictiterobject),
3815 .tp_dealloc = (destructor)dictiter_dealloc,
3816 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
3817 .tp_traverse = (traverseproc)dictiter_traverse,
3818 .tp_iter = PyObject_SelfIter,
3819 .tp_iternext = (iternextfunc)dictreviter_iternext,
3820 .tp_methods = dictiter_methods
3821};
3822
3823
3824/*[clinic input]
3825dict.__reversed__
3826
3827Return a reverse iterator over the dict keys.
3828[clinic start generated code]*/
3829
3830static PyObject *
3831dict___reversed___impl(PyDictObject *self)
3832/*[clinic end generated code: output=e674483336d1ed51 input=23210ef3477d8c4d]*/
3833{
3834 assert (PyDict_Check(self));
3835 return dictiter_new(self, &PyDictRevIterKey_Type);
3836}
3837
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003838static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303839dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003840{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003841 _Py_IDENTIFIER(iter);
Sergey Fedoseev63958442018-10-20 05:43:33 +05003842 /* copy the iterator state */
3843 dictiterobject tmp = *di;
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003844 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003845
Sergey Fedoseev63958442018-10-20 05:43:33 +05003846 PyObject *list = PySequence_List((PyObject*)&tmp);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003847 Py_XDECREF(tmp.di_dict);
Sergey Fedoseev63958442018-10-20 05:43:33 +05003848 if (list == NULL) {
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003849 return NULL;
3850 }
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003851 return Py_BuildValue("N(N)", _PyEval_GetBuiltinId(&PyId_iter), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003852}
3853
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003854PyTypeObject PyDictRevIterItem_Type = {
3855 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3856 "dict_reverseitemiterator",
3857 sizeof(dictiterobject),
3858 .tp_dealloc = (destructor)dictiter_dealloc,
3859 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
3860 .tp_traverse = (traverseproc)dictiter_traverse,
3861 .tp_iter = PyObject_SelfIter,
3862 .tp_iternext = (iternextfunc)dictreviter_iternext,
3863 .tp_methods = dictiter_methods
3864};
3865
3866PyTypeObject PyDictRevIterValue_Type = {
3867 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3868 "dict_reversevalueiterator",
3869 sizeof(dictiterobject),
3870 .tp_dealloc = (destructor)dictiter_dealloc,
3871 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
3872 .tp_traverse = (traverseproc)dictiter_traverse,
3873 .tp_iter = PyObject_SelfIter,
3874 .tp_iternext = (iternextfunc)dictreviter_iternext,
3875 .tp_methods = dictiter_methods
3876};
3877
Guido van Rossum3ac67412007-02-10 18:55:06 +00003878/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003879/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003880/***********************************************/
3881
Guido van Rossumb90c8482007-02-10 01:11:45 +00003882/* The instance lay-out is the same for all three; but the type differs. */
3883
Guido van Rossumb90c8482007-02-10 01:11:45 +00003884static void
Eric Snow96c6af92015-05-29 22:21:39 -06003885dictview_dealloc(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003886{
INADA Naokia6296d32017-08-24 14:55:17 +09003887 /* bpo-31095: UnTrack is needed before calling any callbacks */
3888 _PyObject_GC_UNTRACK(dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003889 Py_XDECREF(dv->dv_dict);
3890 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003891}
3892
3893static int
Eric Snow96c6af92015-05-29 22:21:39 -06003894dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003895{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003896 Py_VISIT(dv->dv_dict);
3897 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003898}
3899
Guido van Rossum83825ac2007-02-10 04:54:19 +00003900static Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003901dictview_len(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003902{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003903 Py_ssize_t len = 0;
3904 if (dv->dv_dict != NULL)
3905 len = dv->dv_dict->ma_used;
3906 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003907}
3908
Eric Snow96c6af92015-05-29 22:21:39 -06003909PyObject *
3910_PyDictView_New(PyObject *dict, PyTypeObject *type)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003911{
Eric Snow96c6af92015-05-29 22:21:39 -06003912 _PyDictViewObject *dv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003913 if (dict == NULL) {
3914 PyErr_BadInternalCall();
3915 return NULL;
3916 }
3917 if (!PyDict_Check(dict)) {
3918 /* XXX Get rid of this restriction later */
3919 PyErr_Format(PyExc_TypeError,
3920 "%s() requires a dict argument, not '%s'",
3921 type->tp_name, dict->ob_type->tp_name);
3922 return NULL;
3923 }
Eric Snow96c6af92015-05-29 22:21:39 -06003924 dv = PyObject_GC_New(_PyDictViewObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003925 if (dv == NULL)
3926 return NULL;
3927 Py_INCREF(dict);
3928 dv->dv_dict = (PyDictObject *)dict;
3929 _PyObject_GC_TRACK(dv);
3930 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003931}
3932
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003933/* TODO(guido): The views objects are not complete:
3934
3935 * support more set operations
3936 * support arbitrary mappings?
3937 - either these should be static or exported in dictobject.h
3938 - if public then they should probably be in builtins
3939*/
3940
Guido van Rossumaac530c2007-08-24 22:33:45 +00003941/* Return 1 if self is a subset of other, iterating over self;
3942 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003943static int
3944all_contained_in(PyObject *self, PyObject *other)
3945{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003946 PyObject *iter = PyObject_GetIter(self);
3947 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003949 if (iter == NULL)
3950 return -1;
3951 for (;;) {
3952 PyObject *next = PyIter_Next(iter);
3953 if (next == NULL) {
3954 if (PyErr_Occurred())
3955 ok = -1;
3956 break;
3957 }
3958 ok = PySequence_Contains(other, next);
3959 Py_DECREF(next);
3960 if (ok <= 0)
3961 break;
3962 }
3963 Py_DECREF(iter);
3964 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003965}
3966
3967static PyObject *
3968dictview_richcompare(PyObject *self, PyObject *other, int op)
3969{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003970 Py_ssize_t len_self, len_other;
3971 int ok;
3972 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003973
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003974 assert(self != NULL);
3975 assert(PyDictViewSet_Check(self));
3976 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00003977
Brian Curtindfc80e32011-08-10 20:28:54 -05003978 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
3979 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003981 len_self = PyObject_Size(self);
3982 if (len_self < 0)
3983 return NULL;
3984 len_other = PyObject_Size(other);
3985 if (len_other < 0)
3986 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003988 ok = 0;
3989 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00003990
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003991 case Py_NE:
3992 case Py_EQ:
3993 if (len_self == len_other)
3994 ok = all_contained_in(self, other);
3995 if (op == Py_NE && ok >= 0)
3996 ok = !ok;
3997 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003999 case Py_LT:
4000 if (len_self < len_other)
4001 ok = all_contained_in(self, other);
4002 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004004 case Py_LE:
4005 if (len_self <= len_other)
4006 ok = all_contained_in(self, other);
4007 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004009 case Py_GT:
4010 if (len_self > len_other)
4011 ok = all_contained_in(other, self);
4012 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004014 case Py_GE:
4015 if (len_self >= len_other)
4016 ok = all_contained_in(other, self);
4017 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004018
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004019 }
4020 if (ok < 0)
4021 return NULL;
4022 result = ok ? Py_True : Py_False;
4023 Py_INCREF(result);
4024 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00004025}
4026
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004027static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004028dictview_repr(_PyDictViewObject *dv)
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004029{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004030 PyObject *seq;
bennorthd7773d92018-01-26 15:46:01 +00004031 PyObject *result = NULL;
4032 Py_ssize_t rc;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004033
bennorthd7773d92018-01-26 15:46:01 +00004034 rc = Py_ReprEnter((PyObject *)dv);
4035 if (rc != 0) {
4036 return rc > 0 ? PyUnicode_FromString("...") : NULL;
4037 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004038 seq = PySequence_List((PyObject *)dv);
bennorthd7773d92018-01-26 15:46:01 +00004039 if (seq == NULL) {
4040 goto Done;
4041 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004042 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
4043 Py_DECREF(seq);
bennorthd7773d92018-01-26 15:46:01 +00004044
4045Done:
4046 Py_ReprLeave((PyObject *)dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004047 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004048}
4049
Guido van Rossum3ac67412007-02-10 18:55:06 +00004050/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004051
4052static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004053dictkeys_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004054{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004055 if (dv->dv_dict == NULL) {
4056 Py_RETURN_NONE;
4057 }
4058 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004059}
4060
4061static int
Eric Snow96c6af92015-05-29 22:21:39 -06004062dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004063{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004064 if (dv->dv_dict == NULL)
4065 return 0;
4066 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004067}
4068
Guido van Rossum83825ac2007-02-10 04:54:19 +00004069static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004070 (lenfunc)dictview_len, /* sq_length */
4071 0, /* sq_concat */
4072 0, /* sq_repeat */
4073 0, /* sq_item */
4074 0, /* sq_slice */
4075 0, /* sq_ass_item */
4076 0, /* sq_ass_slice */
4077 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004078};
4079
Guido van Rossum523259b2007-08-24 23:41:22 +00004080static PyObject*
4081dictviews_sub(PyObject* self, PyObject *other)
4082{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004083 PyObject *result = PySet_New(self);
4084 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004085 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004087 if (result == NULL)
4088 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004089
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08004090 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004091 if (tmp == NULL) {
4092 Py_DECREF(result);
4093 return NULL;
4094 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004096 Py_DECREF(tmp);
4097 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004098}
4099
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004100PyObject*
4101_PyDictView_Intersect(PyObject* self, PyObject *other)
Guido van Rossum523259b2007-08-24 23:41:22 +00004102{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004103 PyObject *result = PySet_New(self);
4104 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004105 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004107 if (result == NULL)
4108 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004109
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08004110 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_intersection_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004111 if (tmp == NULL) {
4112 Py_DECREF(result);
4113 return NULL;
4114 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004116 Py_DECREF(tmp);
4117 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004118}
4119
4120static PyObject*
4121dictviews_or(PyObject* self, PyObject *other)
4122{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004123 PyObject *result = PySet_New(self);
4124 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004125 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02004126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004127 if (result == NULL)
4128 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004129
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08004130 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004131 if (tmp == NULL) {
4132 Py_DECREF(result);
4133 return NULL;
4134 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004136 Py_DECREF(tmp);
4137 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004138}
4139
4140static PyObject*
4141dictviews_xor(PyObject* self, PyObject *other)
4142{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004143 PyObject *result = PySet_New(self);
4144 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004145 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004147 if (result == NULL)
4148 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004149
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08004150 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_symmetric_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004151 if (tmp == NULL) {
4152 Py_DECREF(result);
4153 return NULL;
4154 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004156 Py_DECREF(tmp);
4157 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004158}
4159
4160static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004161 0, /*nb_add*/
4162 (binaryfunc)dictviews_sub, /*nb_subtract*/
4163 0, /*nb_multiply*/
4164 0, /*nb_remainder*/
4165 0, /*nb_divmod*/
4166 0, /*nb_power*/
4167 0, /*nb_negative*/
4168 0, /*nb_positive*/
4169 0, /*nb_absolute*/
4170 0, /*nb_bool*/
4171 0, /*nb_invert*/
4172 0, /*nb_lshift*/
4173 0, /*nb_rshift*/
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004174 (binaryfunc)_PyDictView_Intersect, /*nb_and*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004175 (binaryfunc)dictviews_xor, /*nb_xor*/
4176 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00004177};
4178
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004179static PyObject*
4180dictviews_isdisjoint(PyObject *self, PyObject *other)
4181{
4182 PyObject *it;
4183 PyObject *item = NULL;
4184
4185 if (self == other) {
Eric Snow96c6af92015-05-29 22:21:39 -06004186 if (dictview_len((_PyDictViewObject *)self) == 0)
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004187 Py_RETURN_TRUE;
4188 else
4189 Py_RETURN_FALSE;
4190 }
4191
4192 /* Iterate over the shorter object (only if other is a set,
4193 * because PySequence_Contains may be expensive otherwise): */
4194 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
Eric Snow96c6af92015-05-29 22:21:39 -06004195 Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self);
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004196 Py_ssize_t len_other = PyObject_Size(other);
4197 if (len_other == -1)
4198 return NULL;
4199
4200 if ((len_other > len_self)) {
4201 PyObject *tmp = other;
4202 other = self;
4203 self = tmp;
4204 }
4205 }
4206
4207 it = PyObject_GetIter(other);
4208 if (it == NULL)
4209 return NULL;
4210
4211 while ((item = PyIter_Next(it)) != NULL) {
4212 int contains = PySequence_Contains(self, item);
4213 Py_DECREF(item);
4214 if (contains == -1) {
4215 Py_DECREF(it);
4216 return NULL;
4217 }
4218
4219 if (contains) {
4220 Py_DECREF(it);
4221 Py_RETURN_FALSE;
4222 }
4223 }
4224 Py_DECREF(it);
4225 if (PyErr_Occurred())
4226 return NULL; /* PyIter_Next raised an exception. */
4227 Py_RETURN_TRUE;
4228}
4229
4230PyDoc_STRVAR(isdisjoint_doc,
4231"Return True if the view and the given iterable have a null intersection.");
4232
Serhiy Storchaka81524022018-11-27 13:05:02 +02004233static PyObject* dictkeys_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored));
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004234
4235PyDoc_STRVAR(reversed_keys_doc,
4236"Return a reverse iterator over the dict keys.");
4237
Guido van Rossumb90c8482007-02-10 01:11:45 +00004238static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004239 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4240 isdisjoint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004241 {"__reversed__", (PyCFunction)(void(*)(void))dictkeys_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004242 reversed_keys_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004243 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004244};
4245
4246PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004247 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4248 "dict_keys", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004249 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004250 0, /* tp_itemsize */
4251 /* methods */
4252 (destructor)dictview_dealloc, /* tp_dealloc */
4253 0, /* tp_print */
4254 0, /* tp_getattr */
4255 0, /* tp_setattr */
4256 0, /* tp_reserved */
4257 (reprfunc)dictview_repr, /* tp_repr */
4258 &dictviews_as_number, /* tp_as_number */
4259 &dictkeys_as_sequence, /* tp_as_sequence */
4260 0, /* tp_as_mapping */
4261 0, /* tp_hash */
4262 0, /* tp_call */
4263 0, /* tp_str */
4264 PyObject_GenericGetAttr, /* tp_getattro */
4265 0, /* tp_setattro */
4266 0, /* tp_as_buffer */
4267 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4268 0, /* tp_doc */
4269 (traverseproc)dictview_traverse, /* tp_traverse */
4270 0, /* tp_clear */
4271 dictview_richcompare, /* tp_richcompare */
4272 0, /* tp_weaklistoffset */
4273 (getiterfunc)dictkeys_iter, /* tp_iter */
4274 0, /* tp_iternext */
4275 dictkeys_methods, /* tp_methods */
4276 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004277};
4278
4279static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304280dictkeys_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004281{
Eric Snow96c6af92015-05-29 22:21:39 -06004282 return _PyDictView_New(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004283}
4284
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004285static PyObject *
Serhiy Storchaka81524022018-11-27 13:05:02 +02004286dictkeys_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored))
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004287{
4288 if (dv->dv_dict == NULL) {
4289 Py_RETURN_NONE;
4290 }
4291 return dictiter_new(dv->dv_dict, &PyDictRevIterKey_Type);
4292}
4293
Guido van Rossum3ac67412007-02-10 18:55:06 +00004294/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004295
4296static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004297dictitems_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004298{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004299 if (dv->dv_dict == NULL) {
4300 Py_RETURN_NONE;
4301 }
4302 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004303}
4304
4305static int
Eric Snow96c6af92015-05-29 22:21:39 -06004306dictitems_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004307{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004308 int result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004309 PyObject *key, *value, *found;
4310 if (dv->dv_dict == NULL)
4311 return 0;
4312 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
4313 return 0;
4314 key = PyTuple_GET_ITEM(obj, 0);
4315 value = PyTuple_GET_ITEM(obj, 1);
Raymond Hettinger6692f012016-09-18 21:46:08 -07004316 found = PyDict_GetItemWithError((PyObject *)dv->dv_dict, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004317 if (found == NULL) {
4318 if (PyErr_Occurred())
4319 return -1;
4320 return 0;
4321 }
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004322 Py_INCREF(found);
4323 result = PyObject_RichCompareBool(value, found, Py_EQ);
4324 Py_DECREF(found);
4325 return result;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004326}
4327
Guido van Rossum83825ac2007-02-10 04:54:19 +00004328static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004329 (lenfunc)dictview_len, /* sq_length */
4330 0, /* sq_concat */
4331 0, /* sq_repeat */
4332 0, /* sq_item */
4333 0, /* sq_slice */
4334 0, /* sq_ass_item */
4335 0, /* sq_ass_slice */
4336 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004337};
4338
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004339static PyObject* dictitems_reversed(_PyDictViewObject *dv);
4340
4341PyDoc_STRVAR(reversed_items_doc,
4342"Return a reverse iterator over the dict items.");
4343
Guido van Rossumb90c8482007-02-10 01:11:45 +00004344static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004345 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4346 isdisjoint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004347 {"__reversed__", (PyCFunction)(void(*)(void))dictitems_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004348 reversed_items_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004349 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004350};
4351
4352PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004353 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4354 "dict_items", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004355 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004356 0, /* tp_itemsize */
4357 /* methods */
4358 (destructor)dictview_dealloc, /* tp_dealloc */
4359 0, /* tp_print */
4360 0, /* tp_getattr */
4361 0, /* tp_setattr */
4362 0, /* tp_reserved */
4363 (reprfunc)dictview_repr, /* tp_repr */
4364 &dictviews_as_number, /* tp_as_number */
4365 &dictitems_as_sequence, /* tp_as_sequence */
4366 0, /* tp_as_mapping */
4367 0, /* tp_hash */
4368 0, /* tp_call */
4369 0, /* tp_str */
4370 PyObject_GenericGetAttr, /* tp_getattro */
4371 0, /* tp_setattro */
4372 0, /* tp_as_buffer */
4373 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4374 0, /* tp_doc */
4375 (traverseproc)dictview_traverse, /* tp_traverse */
4376 0, /* tp_clear */
4377 dictview_richcompare, /* tp_richcompare */
4378 0, /* tp_weaklistoffset */
4379 (getiterfunc)dictitems_iter, /* tp_iter */
4380 0, /* tp_iternext */
4381 dictitems_methods, /* tp_methods */
4382 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004383};
4384
4385static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304386dictitems_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004387{
Eric Snow96c6af92015-05-29 22:21:39 -06004388 return _PyDictView_New(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004389}
4390
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004391static PyObject *
4392dictitems_reversed(_PyDictViewObject *dv)
4393{
4394 if (dv->dv_dict == NULL) {
4395 Py_RETURN_NONE;
4396 }
4397 return dictiter_new(dv->dv_dict, &PyDictRevIterItem_Type);
4398}
4399
Guido van Rossum3ac67412007-02-10 18:55:06 +00004400/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004401
4402static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004403dictvalues_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004404{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004405 if (dv->dv_dict == NULL) {
4406 Py_RETURN_NONE;
4407 }
4408 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004409}
4410
Guido van Rossum83825ac2007-02-10 04:54:19 +00004411static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004412 (lenfunc)dictview_len, /* sq_length */
4413 0, /* sq_concat */
4414 0, /* sq_repeat */
4415 0, /* sq_item */
4416 0, /* sq_slice */
4417 0, /* sq_ass_item */
4418 0, /* sq_ass_slice */
4419 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004420};
4421
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004422static PyObject* dictvalues_reversed(_PyDictViewObject *dv);
4423
4424PyDoc_STRVAR(reversed_values_doc,
4425"Return a reverse iterator over the dict values.");
4426
Guido van Rossumb90c8482007-02-10 01:11:45 +00004427static PyMethodDef dictvalues_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004428 {"__reversed__", (PyCFunction)(void(*)(void))dictvalues_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004429 reversed_values_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004430 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004431};
4432
4433PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004434 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4435 "dict_values", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004436 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004437 0, /* tp_itemsize */
4438 /* methods */
4439 (destructor)dictview_dealloc, /* tp_dealloc */
4440 0, /* tp_print */
4441 0, /* tp_getattr */
4442 0, /* tp_setattr */
4443 0, /* tp_reserved */
4444 (reprfunc)dictview_repr, /* tp_repr */
4445 0, /* tp_as_number */
4446 &dictvalues_as_sequence, /* tp_as_sequence */
4447 0, /* tp_as_mapping */
4448 0, /* tp_hash */
4449 0, /* tp_call */
4450 0, /* tp_str */
4451 PyObject_GenericGetAttr, /* tp_getattro */
4452 0, /* tp_setattro */
4453 0, /* tp_as_buffer */
4454 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4455 0, /* tp_doc */
4456 (traverseproc)dictview_traverse, /* tp_traverse */
4457 0, /* tp_clear */
4458 0, /* tp_richcompare */
4459 0, /* tp_weaklistoffset */
4460 (getiterfunc)dictvalues_iter, /* tp_iter */
4461 0, /* tp_iternext */
4462 dictvalues_methods, /* tp_methods */
4463 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004464};
4465
4466static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304467dictvalues_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004468{
Eric Snow96c6af92015-05-29 22:21:39 -06004469 return _PyDictView_New(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004470}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004471
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004472static PyObject *
4473dictvalues_reversed(_PyDictViewObject *dv)
4474{
4475 if (dv->dv_dict == NULL) {
4476 Py_RETURN_NONE;
4477 }
4478 return dictiter_new(dv->dv_dict, &PyDictRevIterValue_Type);
4479}
4480
4481
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004482/* Returns NULL if cannot allocate a new PyDictKeysObject,
4483 but does not set an error */
4484PyDictKeysObject *
4485_PyDict_NewKeysForClass(void)
4486{
Victor Stinner742da042016-09-07 17:40:12 -07004487 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004488 if (keys == NULL)
4489 PyErr_Clear();
4490 else
4491 keys->dk_lookup = lookdict_split;
4492 return keys;
4493}
4494
4495#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
4496
4497PyObject *
4498PyObject_GenericGetDict(PyObject *obj, void *context)
4499{
4500 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
4501 if (dictptr == NULL) {
4502 PyErr_SetString(PyExc_AttributeError,
4503 "This object has no __dict__");
4504 return NULL;
4505 }
4506 dict = *dictptr;
4507 if (dict == NULL) {
4508 PyTypeObject *tp = Py_TYPE(obj);
4509 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
INADA Naokia7576492018-11-14 18:39:27 +09004510 dictkeys_incref(CACHED_KEYS(tp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004511 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
4512 }
4513 else {
4514 *dictptr = dict = PyDict_New();
4515 }
4516 }
4517 Py_XINCREF(dict);
4518 return dict;
4519}
4520
4521int
4522_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
Victor Stinner742da042016-09-07 17:40:12 -07004523 PyObject *key, PyObject *value)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004524{
4525 PyObject *dict;
4526 int res;
4527 PyDictKeysObject *cached;
4528
4529 assert(dictptr != NULL);
4530 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
4531 assert(dictptr != NULL);
4532 dict = *dictptr;
4533 if (dict == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +09004534 dictkeys_incref(cached);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004535 dict = new_dict_with_shared_keys(cached);
4536 if (dict == NULL)
4537 return -1;
4538 *dictptr = dict;
4539 }
4540 if (value == NULL) {
4541 res = PyDict_DelItem(dict, key);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004542 // Since key sharing dict doesn't allow deletion, PyDict_DelItem()
4543 // always converts dict to combined form.
4544 if ((cached = CACHED_KEYS(tp)) != NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004545 CACHED_KEYS(tp) = NULL;
INADA Naokia7576492018-11-14 18:39:27 +09004546 dictkeys_decref(cached);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004547 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01004548 }
4549 else {
INADA Naoki2294f3a2017-02-12 13:51:30 +09004550 int was_shared = (cached == ((PyDictObject *)dict)->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004551 res = PyDict_SetItem(dict, key, value);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004552 if (was_shared &&
4553 (cached = CACHED_KEYS(tp)) != NULL &&
4554 cached != ((PyDictObject *)dict)->ma_keys) {
Victor Stinner3d3f2642016-12-15 17:21:23 +01004555 /* PyDict_SetItem() may call dictresize and convert split table
4556 * into combined table. In such case, convert it to split
4557 * table again and update type's shared key only when this is
4558 * the only dict sharing key with the type.
4559 *
4560 * This is to allow using shared key in class like this:
4561 *
4562 * class C:
4563 * def __init__(self):
4564 * # one dict resize happens
4565 * self.a, self.b, self.c = 1, 2, 3
4566 * self.d, self.e, self.f = 4, 5, 6
4567 * a = C()
4568 */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004569 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004570 CACHED_KEYS(tp) = make_keys_shared(dict);
Victor Stinner742da042016-09-07 17:40:12 -07004571 }
4572 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004573 CACHED_KEYS(tp) = NULL;
4574 }
INADA Naokia7576492018-11-14 18:39:27 +09004575 dictkeys_decref(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004576 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
4577 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004578 }
4579 }
4580 } else {
4581 dict = *dictptr;
4582 if (dict == NULL) {
4583 dict = PyDict_New();
4584 if (dict == NULL)
4585 return -1;
4586 *dictptr = dict;
4587 }
4588 if (value == NULL) {
4589 res = PyDict_DelItem(dict, key);
4590 } else {
4591 res = PyDict_SetItem(dict, key, value);
4592 }
4593 }
4594 return res;
4595}
4596
4597void
4598_PyDictKeys_DecRef(PyDictKeysObject *keys)
4599{
INADA Naokia7576492018-11-14 18:39:27 +09004600 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004601}