blob: 524ff67837bcf70eb2a21ba0aee463d98757524e [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
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001105// Same to insertdict but specialized for ma_keys = Py_EMPTY_KEYS.
1106static int
1107insert_to_emptydict(PyDictObject *mp, PyObject *key, Py_hash_t hash,
1108 PyObject *value)
1109{
1110 assert(mp->ma_keys == Py_EMPTY_KEYS);
1111
1112 PyDictKeysObject *newkeys = new_keys_object(PyDict_MINSIZE);
1113 if (newkeys == NULL) {
1114 return -1;
1115 }
1116 if (!PyUnicode_CheckExact(key)) {
1117 newkeys->dk_lookup = lookdict;
1118 }
1119 dictkeys_decref(Py_EMPTY_KEYS);
1120 mp->ma_keys = newkeys;
1121 mp->ma_values = NULL;
1122
1123 Py_INCREF(key);
1124 Py_INCREF(value);
1125 MAINTAIN_TRACKING(mp, key, value);
1126
1127 size_t hashpos = (size_t)hash & (PyDict_MINSIZE-1);
1128 PyDictKeyEntry *ep = &DK_ENTRIES(mp->ma_keys)[0];
1129 dictkeys_set_index(mp->ma_keys, hashpos, 0);
1130 ep->me_key = key;
1131 ep->me_hash = hash;
1132 ep->me_value = value;
1133 mp->ma_used++;
1134 mp->ma_version_tag = DICT_NEXT_VERSION();
1135 mp->ma_keys->dk_usable--;
1136 mp->ma_keys->dk_nentries++;
1137 return 0;
1138}
1139
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001140/*
luzpaza5293b42017-11-05 07:37:50 -06001141Internal routine used by dictresize() to build a hashtable of entries.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001142*/
1143static void
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001144build_indices(PyDictKeysObject *keys, PyDictKeyEntry *ep, Py_ssize_t n)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001145{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001146 size_t mask = (size_t)DK_SIZE(keys) - 1;
1147 for (Py_ssize_t ix = 0; ix != n; ix++, ep++) {
1148 Py_hash_t hash = ep->me_hash;
1149 size_t i = hash & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001150 for (size_t perturb = hash; dictkeys_get_index(keys, i) != DKIX_EMPTY;) {
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001151 perturb >>= PERTURB_SHIFT;
INADA Naoki870c2862017-06-24 09:03:19 +09001152 i = mask & (i*5 + perturb + 1);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001153 }
INADA Naokia7576492018-11-14 18:39:27 +09001154 dictkeys_set_index(keys, i, ix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001156}
1157
1158/*
1159Restructure the table by allocating a new table and reinserting all
1160items again. When entries have been deleted, the new table may
1161actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001162If a table is split (its keys and hashes are shared, its values are not),
1163then the values are temporarily copied into the table, it is resized as
1164a combined table, then the me_value slots in the old table are NULLed out.
1165After resizing a table is always combined,
1166but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001167*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001168static int
Victor Stinner3d3f2642016-12-15 17:21:23 +01001169dictresize(PyDictObject *mp, Py_ssize_t minsize)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001170{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001171 Py_ssize_t newsize, numentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001172 PyDictKeysObject *oldkeys;
1173 PyObject **oldvalues;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001174 PyDictKeyEntry *oldentries, *newentries;
Tim Peters91a364d2001-05-19 07:04:38 +00001175
Victor Stinner742da042016-09-07 17:40:12 -07001176 /* Find the smallest table size > minused. */
1177 for (newsize = PyDict_MINSIZE;
Victor Stinner3d3f2642016-12-15 17:21:23 +01001178 newsize < minsize && newsize > 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 newsize <<= 1)
1180 ;
1181 if (newsize <= 0) {
1182 PyErr_NoMemory();
1183 return -1;
1184 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001185
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001186 oldkeys = mp->ma_keys;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001187
1188 /* NOTE: Current odict checks mp->ma_keys to detect resize happen.
1189 * So we can't reuse oldkeys even if oldkeys->dk_size == newsize.
1190 * TODO: Try reusing oldkeys when reimplement odict.
1191 */
1192
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001193 /* Allocate a new table. */
1194 mp->ma_keys = new_keys_object(newsize);
1195 if (mp->ma_keys == NULL) {
1196 mp->ma_keys = oldkeys;
1197 return -1;
1198 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01001199 // New table must be large enough.
1200 assert(mp->ma_keys->dk_usable >= mp->ma_used);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001201 if (oldkeys->dk_lookup == lookdict)
1202 mp->ma_keys->dk_lookup = lookdict;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001203
1204 numentries = mp->ma_used;
1205 oldentries = DK_ENTRIES(oldkeys);
1206 newentries = DK_ENTRIES(mp->ma_keys);
1207 oldvalues = mp->ma_values;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001208 if (oldvalues != NULL) {
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001209 /* Convert split table into new combined table.
1210 * We must incref keys; we can transfer values.
1211 * Note that values of split table is always dense.
1212 */
1213 for (Py_ssize_t i = 0; i < numentries; i++) {
1214 assert(oldvalues[i] != NULL);
1215 PyDictKeyEntry *ep = &oldentries[i];
1216 PyObject *key = ep->me_key;
1217 Py_INCREF(key);
1218 newentries[i].me_key = key;
1219 newentries[i].me_hash = ep->me_hash;
1220 newentries[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001222
INADA Naokia7576492018-11-14 18:39:27 +09001223 dictkeys_decref(oldkeys);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001224 mp->ma_values = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001225 if (oldvalues != empty_values) {
1226 free_values(oldvalues);
1227 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001228 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001229 else { // combined table.
1230 if (oldkeys->dk_nentries == numentries) {
1231 memcpy(newentries, oldentries, numentries * sizeof(PyDictKeyEntry));
1232 }
1233 else {
1234 PyDictKeyEntry *ep = oldentries;
1235 for (Py_ssize_t i = 0; i < numentries; i++) {
1236 while (ep->me_value == NULL)
1237 ep++;
1238 newentries[i] = *ep++;
1239 }
1240 }
1241
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001242 assert(oldkeys->dk_lookup != lookdict_split);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001243 assert(oldkeys->dk_refcnt == 1);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001244 if (oldkeys->dk_size == PyDict_MINSIZE &&
1245 numfreekeys < PyDict_MAXFREELIST) {
INADA Naokia7576492018-11-14 18:39:27 +09001246 _Py_DEC_REFTOTAL;
1247 keys_free_list[numfreekeys++] = oldkeys;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001248 }
1249 else {
INADA Naokia7576492018-11-14 18:39:27 +09001250 _Py_DEC_REFTOTAL;
1251 PyObject_FREE(oldkeys);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001252 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001253 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001254
1255 build_indices(mp->ma_keys, newentries, numentries);
1256 mp->ma_keys->dk_usable -= numentries;
1257 mp->ma_keys->dk_nentries = numentries;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001259}
1260
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001261/* Returns NULL if unable to split table.
1262 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001263static PyDictKeysObject *
1264make_keys_shared(PyObject *op)
1265{
1266 Py_ssize_t i;
1267 Py_ssize_t size;
1268 PyDictObject *mp = (PyDictObject *)op;
1269
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001270 if (!PyDict_CheckExact(op))
1271 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001272 if (!_PyDict_HasSplitTable(mp)) {
1273 PyDictKeyEntry *ep0;
1274 PyObject **values;
1275 assert(mp->ma_keys->dk_refcnt == 1);
1276 if (mp->ma_keys->dk_lookup == lookdict) {
1277 return NULL;
1278 }
1279 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
1280 /* Remove dummy keys */
1281 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
1282 return NULL;
1283 }
1284 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
1285 /* Copy values into a new array */
Victor Stinner742da042016-09-07 17:40:12 -07001286 ep0 = DK_ENTRIES(mp->ma_keys);
1287 size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001288 values = new_values(size);
1289 if (values == NULL) {
1290 PyErr_SetString(PyExc_MemoryError,
1291 "Not enough memory to allocate new values array");
1292 return NULL;
1293 }
1294 for (i = 0; i < size; i++) {
1295 values[i] = ep0[i].me_value;
1296 ep0[i].me_value = NULL;
1297 }
1298 mp->ma_keys->dk_lookup = lookdict_split;
1299 mp->ma_values = values;
1300 }
INADA Naokia7576492018-11-14 18:39:27 +09001301 dictkeys_incref(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001302 return mp->ma_keys;
1303}
Christian Heimes99170a52007-12-19 02:07:34 +00001304
1305PyObject *
1306_PyDict_NewPresized(Py_ssize_t minused)
1307{
INADA Naoki92c50ee2016-11-22 00:57:02 +09001308 const Py_ssize_t max_presize = 128 * 1024;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001309 Py_ssize_t newsize;
1310 PyDictKeysObject *new_keys;
INADA Naoki92c50ee2016-11-22 00:57:02 +09001311
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001312 if (minused <= USABLE_FRACTION(PyDict_MINSIZE)) {
Inada Naokif2a18672019-03-12 17:25:44 +09001313 return PyDict_New();
1314 }
INADA Naoki92c50ee2016-11-22 00:57:02 +09001315 /* There are no strict guarantee that returned dict can contain minused
1316 * items without resize. So we create medium size dict instead of very
1317 * large dict or MemoryError.
1318 */
1319 if (minused > USABLE_FRACTION(max_presize)) {
1320 newsize = max_presize;
1321 }
1322 else {
1323 Py_ssize_t minsize = ESTIMATE_SIZE(minused);
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001324 newsize = PyDict_MINSIZE*2;
INADA Naoki92c50ee2016-11-22 00:57:02 +09001325 while (newsize < minsize) {
1326 newsize <<= 1;
1327 }
1328 }
1329 assert(IS_POWER_OF_2(newsize));
1330
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001331 new_keys = new_keys_object(newsize);
1332 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001334 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001335}
1336
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001337/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1338 * that may occur (originally dicts supported only string keys, and exceptions
1339 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001340 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001341 * (suppressed) occurred while computing the key's hash, or that some error
1342 * (suppressed) occurred when comparing keys in the dict's internal probe
1343 * sequence. A nasty example of the latter is when a Python-coded comparison
1344 * function hits a stack-depth error, which can cause this to return NULL
1345 * even if the key is present.
1346 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001347PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001348PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001349{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001350 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07001351 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 PyDictObject *mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 PyThreadState *tstate;
INADA Naokiba609772016-12-07 20:41:42 +09001354 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 if (!PyDict_Check(op))
1357 return NULL;
1358 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001359 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 {
1361 hash = PyObject_Hash(key);
1362 if (hash == -1) {
1363 PyErr_Clear();
1364 return NULL;
1365 }
1366 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 /* We can arrive here with a NULL tstate during initialization: try
1369 running "python -Wi" for an example related to string interning.
1370 Let's just hope that no exception occurs then... This must be
Victor Stinner50b48572018-11-01 01:51:40 +01001371 _PyThreadState_GET() and not PyThreadState_Get() because the latter
Victor Stinner9204fb82018-10-30 15:13:17 +01001372 abort Python if tstate is NULL. */
Victor Stinner50b48572018-11-01 01:51:40 +01001373 tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 if (tstate != NULL && tstate->curexc_type != NULL) {
1375 /* preserve the existing exception */
1376 PyObject *err_type, *err_value, *err_tb;
1377 PyErr_Fetch(&err_type, &err_value, &err_tb);
INADA Naoki778928b2017-08-03 23:45:15 +09001378 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 /* ignore errors */
1380 PyErr_Restore(err_type, err_value, err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001381 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 return NULL;
1383 }
1384 else {
INADA Naoki778928b2017-08-03 23:45:15 +09001385 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001386 if (ix < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 PyErr_Clear();
1388 return NULL;
1389 }
1390 }
INADA Naokiba609772016-12-07 20:41:42 +09001391 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001392}
1393
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001394/* Same as PyDict_GetItemWithError() but with hash supplied by caller.
1395 This returns NULL *with* an exception set if an exception occurred.
1396 It returns NULL *without* an exception set if the key wasn't present.
1397*/
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001398PyObject *
1399_PyDict_GetItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1400{
Victor Stinner742da042016-09-07 17:40:12 -07001401 Py_ssize_t ix;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001402 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001403 PyObject *value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001404
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001405 if (!PyDict_Check(op)) {
1406 PyErr_BadInternalCall();
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001407 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001408 }
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001409
INADA Naoki778928b2017-08-03 23:45:15 +09001410 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001411 if (ix < 0) {
1412 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001413 }
INADA Naokiba609772016-12-07 20:41:42 +09001414 return value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001415}
1416
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001417/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1418 This returns NULL *with* an exception set if an exception occurred.
1419 It returns NULL *without* an exception set if the key wasn't present.
1420*/
1421PyObject *
1422PyDict_GetItemWithError(PyObject *op, PyObject *key)
1423{
Victor Stinner742da042016-09-07 17:40:12 -07001424 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001425 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 PyDictObject*mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001427 PyObject *value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 if (!PyDict_Check(op)) {
1430 PyErr_BadInternalCall();
1431 return NULL;
1432 }
1433 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001434 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 {
1436 hash = PyObject_Hash(key);
1437 if (hash == -1) {
1438 return NULL;
1439 }
1440 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001441
INADA Naoki778928b2017-08-03 23:45:15 +09001442 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001443 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001445 return value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001446}
1447
Brett Cannonfd074152012-04-14 14:10:13 -04001448PyObject *
1449_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1450{
1451 PyObject *kv;
1452 kv = _PyUnicode_FromId(key); /* borrowed */
1453 if (kv == NULL)
1454 return NULL;
1455 return PyDict_GetItemWithError(dp, kv);
1456}
1457
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001458PyObject *
1459_PyDict_GetItemStringWithError(PyObject *v, const char *key)
1460{
1461 PyObject *kv, *rv;
1462 kv = PyUnicode_FromString(key);
1463 if (kv == NULL) {
1464 return NULL;
1465 }
1466 rv = PyDict_GetItemWithError(v, kv);
1467 Py_DECREF(kv);
1468 return rv;
1469}
1470
Victor Stinnerb4efc962015-11-20 09:24:02 +01001471/* Fast version of global value lookup (LOAD_GLOBAL).
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001472 * Lookup in globals, then builtins.
Victor Stinnerb4efc962015-11-20 09:24:02 +01001473 *
1474 * Raise an exception and return NULL if an error occurred (ex: computing the
1475 * key hash failed, key comparison failed, ...). Return NULL if the key doesn't
1476 * exist. Return the value if the key exists.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001477 */
1478PyObject *
1479_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001480{
Victor Stinner742da042016-09-07 17:40:12 -07001481 Py_ssize_t ix;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001482 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09001483 PyObject *value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001484
1485 if (!PyUnicode_CheckExact(key) ||
1486 (hash = ((PyASCIIObject *) key)->hash) == -1)
1487 {
1488 hash = PyObject_Hash(key);
1489 if (hash == -1)
1490 return NULL;
Antoine Pitroue965d972012-02-27 00:45:12 +01001491 }
Victor Stinnerb4efc962015-11-20 09:24:02 +01001492
1493 /* namespace 1: globals */
INADA Naoki778928b2017-08-03 23:45:15 +09001494 ix = globals->ma_keys->dk_lookup(globals, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001495 if (ix == DKIX_ERROR)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001496 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001497 if (ix != DKIX_EMPTY && value != NULL)
1498 return value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001499
1500 /* namespace 2: builtins */
INADA Naoki778928b2017-08-03 23:45:15 +09001501 ix = builtins->ma_keys->dk_lookup(builtins, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001502 if (ix < 0)
Victor Stinnerb4efc962015-11-20 09:24:02 +01001503 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001504 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001505}
1506
Antoine Pitroue965d972012-02-27 00:45:12 +01001507/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1508 * dictionary if it's merely replacing the value for an existing key.
1509 * This means that it's safe to loop over a dictionary with PyDict_Next()
1510 * and occasionally replace a value -- but you can't insert new keys or
1511 * remove them.
1512 */
1513int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001514PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001515{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001516 PyDictObject *mp;
1517 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001518 if (!PyDict_Check(op)) {
1519 PyErr_BadInternalCall();
1520 return -1;
1521 }
1522 assert(key);
1523 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001524 mp = (PyDictObject *)op;
1525 if (!PyUnicode_CheckExact(key) ||
1526 (hash = ((PyASCIIObject *) key)->hash) == -1)
1527 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001528 hash = PyObject_Hash(key);
1529 if (hash == -1)
1530 return -1;
1531 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001532
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001533 if (mp->ma_keys == Py_EMPTY_KEYS) {
1534 return insert_to_emptydict(mp, key, hash, value);
1535 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001536 /* insertdict() handles any resizing that might be necessary */
1537 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001538}
1539
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001540int
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001541_PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
1542 Py_hash_t hash)
1543{
1544 PyDictObject *mp;
1545
1546 if (!PyDict_Check(op)) {
1547 PyErr_BadInternalCall();
1548 return -1;
1549 }
1550 assert(key);
1551 assert(value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001552 assert(hash != -1);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001553 mp = (PyDictObject *)op;
1554
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001555 if (mp->ma_keys == Py_EMPTY_KEYS) {
1556 return insert_to_emptydict(mp, key, hash, value);
1557 }
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001558 /* insertdict() handles any resizing that might be necessary */
1559 return insertdict(mp, key, hash, value);
1560}
1561
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001562static int
INADA Naoki778928b2017-08-03 23:45:15 +09001563delitem_common(PyDictObject *mp, Py_hash_t hash, Py_ssize_t ix,
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001564 PyObject *old_value)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001565{
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001566 PyObject *old_key;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001567 PyDictKeyEntry *ep;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001568
INADA Naoki778928b2017-08-03 23:45:15 +09001569 Py_ssize_t hashpos = lookdict_index(mp->ma_keys, hash, ix);
1570 assert(hashpos >= 0);
1571
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001572 mp->ma_used--;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001573 mp->ma_version_tag = DICT_NEXT_VERSION();
1574 ep = &DK_ENTRIES(mp->ma_keys)[ix];
INADA Naokia7576492018-11-14 18:39:27 +09001575 dictkeys_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001576 ENSURE_ALLOWS_DELETIONS(mp);
1577 old_key = ep->me_key;
1578 ep->me_key = NULL;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001579 ep->me_value = NULL;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001580 Py_DECREF(old_key);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001581 Py_DECREF(old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001582
1583 assert(_PyDict_CheckConsistency(mp));
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001584 return 0;
1585}
1586
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001587int
Tim Peters1f5871e2000-07-04 17:44:48 +00001588PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001589{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001590 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 assert(key);
1592 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001593 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 hash = PyObject_Hash(key);
1595 if (hash == -1)
1596 return -1;
1597 }
Victor Stinner742da042016-09-07 17:40:12 -07001598
1599 return _PyDict_DelItem_KnownHash(op, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001600}
1601
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001602int
1603_PyDict_DelItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1604{
INADA Naoki778928b2017-08-03 23:45:15 +09001605 Py_ssize_t ix;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001606 PyDictObject *mp;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001607 PyObject *old_value;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001608
1609 if (!PyDict_Check(op)) {
1610 PyErr_BadInternalCall();
1611 return -1;
1612 }
1613 assert(key);
1614 assert(hash != -1);
1615 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001616 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001617 if (ix == DKIX_ERROR)
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001618 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09001619 if (ix == DKIX_EMPTY || old_value == NULL) {
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001620 _PyErr_SetKeyError(key);
1621 return -1;
1622 }
Victor Stinner78601a32016-09-09 19:28:36 -07001623
1624 // Split table doesn't allow deletion. Combine it.
1625 if (_PyDict_HasSplitTable(mp)) {
1626 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1627 return -1;
1628 }
INADA Naoki778928b2017-08-03 23:45:15 +09001629 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001630 assert(ix >= 0);
1631 }
1632
INADA Naoki778928b2017-08-03 23:45:15 +09001633 return delitem_common(mp, hash, ix, old_value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001634}
1635
Antoine Pitroud741ed42016-12-27 14:23:43 +01001636/* This function promises that the predicate -> deletion sequence is atomic
1637 * (i.e. protected by the GIL), assuming the predicate itself doesn't
1638 * release the GIL.
1639 */
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001640int
1641_PyDict_DelItemIf(PyObject *op, PyObject *key,
1642 int (*predicate)(PyObject *value))
1643{
Antoine Pitroud741ed42016-12-27 14:23:43 +01001644 Py_ssize_t hashpos, ix;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001645 PyDictObject *mp;
1646 Py_hash_t hash;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001647 PyObject *old_value;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001648 int res;
1649
1650 if (!PyDict_Check(op)) {
1651 PyErr_BadInternalCall();
1652 return -1;
1653 }
1654 assert(key);
1655 hash = PyObject_Hash(key);
1656 if (hash == -1)
1657 return -1;
1658 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001659 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001660 if (ix == DKIX_ERROR)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001661 return -1;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001662 if (ix == DKIX_EMPTY || old_value == NULL) {
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001663 _PyErr_SetKeyError(key);
1664 return -1;
1665 }
Antoine Pitroud741ed42016-12-27 14:23:43 +01001666
1667 // Split table doesn't allow deletion. Combine it.
1668 if (_PyDict_HasSplitTable(mp)) {
1669 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1670 return -1;
1671 }
INADA Naoki778928b2017-08-03 23:45:15 +09001672 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001673 assert(ix >= 0);
1674 }
1675
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001676 res = predicate(old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001677 if (res == -1)
1678 return -1;
INADA Naoki778928b2017-08-03 23:45:15 +09001679
1680 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1681 assert(hashpos >= 0);
1682
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001683 if (res > 0)
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001684 return delitem_common(mp, hashpos, ix, old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001685 else
1686 return 0;
1687}
1688
1689
Guido van Rossum25831651993-05-19 14:50:45 +00001690void
Tim Peters1f5871e2000-07-04 17:44:48 +00001691PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001692{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001694 PyDictKeysObject *oldkeys;
1695 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001696 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001698 if (!PyDict_Check(op))
1699 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001700 mp = ((PyDictObject *)op);
1701 oldkeys = mp->ma_keys;
1702 oldvalues = mp->ma_values;
1703 if (oldvalues == empty_values)
1704 return;
1705 /* Empty the dict... */
INADA Naokia7576492018-11-14 18:39:27 +09001706 dictkeys_incref(Py_EMPTY_KEYS);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001707 mp->ma_keys = Py_EMPTY_KEYS;
1708 mp->ma_values = empty_values;
1709 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001710 mp->ma_version_tag = DICT_NEXT_VERSION();
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001711 /* ...then clear the keys and values */
1712 if (oldvalues != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001713 n = oldkeys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001714 for (i = 0; i < n; i++)
1715 Py_CLEAR(oldvalues[i]);
1716 free_values(oldvalues);
INADA Naokia7576492018-11-14 18:39:27 +09001717 dictkeys_decref(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001718 }
1719 else {
1720 assert(oldkeys->dk_refcnt == 1);
INADA Naokia7576492018-11-14 18:39:27 +09001721 dictkeys_decref(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001722 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02001723 assert(_PyDict_CheckConsistency(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001724}
1725
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001726/* Internal version of PyDict_Next that returns a hash value in addition
1727 * to the key and value.
1728 * Return 1 on success, return 0 when the reached the end of the dictionary
1729 * (or if op is not a dictionary)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001730 */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001731int
1732_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1733 PyObject **pvalue, Py_hash_t *phash)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001734{
INADA Naokica2d8be2016-11-04 16:59:10 +09001735 Py_ssize_t i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001736 PyDictObject *mp;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001737 PyDictKeyEntry *entry_ptr;
1738 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001739
1740 if (!PyDict_Check(op))
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001741 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 mp = (PyDictObject *)op;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001743 i = *ppos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001744 if (mp->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09001745 if (i < 0 || i >= mp->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001746 return 0;
INADA Naokica2d8be2016-11-04 16:59:10 +09001747 /* values of split table is always dense */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001748 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
INADA Naokica2d8be2016-11-04 16:59:10 +09001749 value = mp->ma_values[i];
1750 assert(value != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001751 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001752 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09001753 Py_ssize_t n = mp->ma_keys->dk_nentries;
1754 if (i < 0 || i >= n)
1755 return 0;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001756 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
1757 while (i < n && entry_ptr->me_value == NULL) {
1758 entry_ptr++;
1759 i++;
Victor Stinner742da042016-09-07 17:40:12 -07001760 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001761 if (i >= n)
1762 return 0;
1763 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001764 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001765 *ppos = i+1;
1766 if (pkey)
1767 *pkey = entry_ptr->me_key;
1768 if (phash)
1769 *phash = entry_ptr->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001770 if (pvalue)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001771 *pvalue = value;
1772 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001773}
1774
Tim Peters080c88b2003-02-15 03:01:11 +00001775/*
1776 * Iterate over a dict. Use like so:
1777 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001778 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001779 * PyObject *key, *value;
1780 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001781 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001782 * Refer to borrowed references in key and value.
Tim Peters080c88b2003-02-15 03:01:11 +00001783 * }
1784 *
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001785 * Return 1 on success, return 0 when the reached the end of the dictionary
1786 * (or if op is not a dictionary)
1787 *
Tim Peters080c88b2003-02-15 03:01:11 +00001788 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001789 * mutates the dict. One exception: it is safe if the loop merely changes
1790 * the values associated with the keys (but doesn't insert new keys or
1791 * delete keys), via PyDict_SetItem().
1792 */
Guido van Rossum25831651993-05-19 14:50:45 +00001793int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001794PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001795{
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001796 return _PyDict_Next(op, ppos, pkey, pvalue, NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001797}
1798
Eric Snow96c6af92015-05-29 22:21:39 -06001799/* Internal version of dict.pop(). */
1800PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001801_PyDict_Pop_KnownHash(PyObject *dict, PyObject *key, Py_hash_t hash, PyObject *deflt)
Eric Snow96c6af92015-05-29 22:21:39 -06001802{
Victor Stinner742da042016-09-07 17:40:12 -07001803 Py_ssize_t ix, hashpos;
Eric Snow96c6af92015-05-29 22:21:39 -06001804 PyObject *old_value, *old_key;
1805 PyDictKeyEntry *ep;
Yury Selivanov684ef2c2016-10-28 19:01:21 -04001806 PyDictObject *mp;
1807
1808 assert(PyDict_Check(dict));
1809 mp = (PyDictObject *)dict;
Eric Snow96c6af92015-05-29 22:21:39 -06001810
1811 if (mp->ma_used == 0) {
1812 if (deflt) {
1813 Py_INCREF(deflt);
1814 return deflt;
1815 }
1816 _PyErr_SetKeyError(key);
1817 return NULL;
1818 }
INADA Naoki778928b2017-08-03 23:45:15 +09001819 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001820 if (ix == DKIX_ERROR)
Eric Snow96c6af92015-05-29 22:21:39 -06001821 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001822 if (ix == DKIX_EMPTY || old_value == NULL) {
Eric Snow96c6af92015-05-29 22:21:39 -06001823 if (deflt) {
1824 Py_INCREF(deflt);
1825 return deflt;
1826 }
1827 _PyErr_SetKeyError(key);
1828 return NULL;
1829 }
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001830
Victor Stinner78601a32016-09-09 19:28:36 -07001831 // Split table doesn't allow deletion. Combine it.
1832 if (_PyDict_HasSplitTable(mp)) {
1833 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1834 return NULL;
1835 }
INADA Naoki778928b2017-08-03 23:45:15 +09001836 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001837 assert(ix >= 0);
1838 }
1839
INADA Naoki778928b2017-08-03 23:45:15 +09001840 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1841 assert(hashpos >= 0);
Victor Stinner78601a32016-09-09 19:28:36 -07001842 assert(old_value != NULL);
Eric Snow96c6af92015-05-29 22:21:39 -06001843 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001844 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokia7576492018-11-14 18:39:27 +09001845 dictkeys_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
Victor Stinner78601a32016-09-09 19:28:36 -07001846 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1847 ENSURE_ALLOWS_DELETIONS(mp);
1848 old_key = ep->me_key;
1849 ep->me_key = NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001850 ep->me_value = NULL;
Victor Stinner78601a32016-09-09 19:28:36 -07001851 Py_DECREF(old_key);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001852
1853 assert(_PyDict_CheckConsistency(mp));
Eric Snow96c6af92015-05-29 22:21:39 -06001854 return old_value;
1855}
1856
Serhiy Storchaka67796522017-01-12 18:34:33 +02001857PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001858_PyDict_Pop(PyObject *dict, PyObject *key, PyObject *deflt)
Serhiy Storchaka67796522017-01-12 18:34:33 +02001859{
1860 Py_hash_t hash;
1861
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001862 if (((PyDictObject *)dict)->ma_used == 0) {
Serhiy Storchaka67796522017-01-12 18:34:33 +02001863 if (deflt) {
1864 Py_INCREF(deflt);
1865 return deflt;
1866 }
1867 _PyErr_SetKeyError(key);
1868 return NULL;
1869 }
1870 if (!PyUnicode_CheckExact(key) ||
1871 (hash = ((PyASCIIObject *) key)->hash) == -1) {
1872 hash = PyObject_Hash(key);
1873 if (hash == -1)
1874 return NULL;
1875 }
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001876 return _PyDict_Pop_KnownHash(dict, key, hash, deflt);
Serhiy Storchaka67796522017-01-12 18:34:33 +02001877}
1878
Eric Snow96c6af92015-05-29 22:21:39 -06001879/* Internal version of dict.from_keys(). It is subclass-friendly. */
1880PyObject *
1881_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
1882{
1883 PyObject *it; /* iter(iterable) */
1884 PyObject *key;
1885 PyObject *d;
1886 int status;
1887
Victor Stinnera5ed5f02016-12-06 18:45:50 +01001888 d = _PyObject_CallNoArg(cls);
Eric Snow96c6af92015-05-29 22:21:39 -06001889 if (d == NULL)
1890 return NULL;
1891
1892 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
1893 if (PyDict_CheckExact(iterable)) {
1894 PyDictObject *mp = (PyDictObject *)d;
1895 PyObject *oldvalue;
1896 Py_ssize_t pos = 0;
1897 PyObject *key;
1898 Py_hash_t hash;
1899
Serhiy Storchakac61ac162017-03-21 08:52:38 +02001900 if (dictresize(mp, ESTIMATE_SIZE(PyDict_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001901 Py_DECREF(d);
1902 return NULL;
1903 }
1904
1905 while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
1906 if (insertdict(mp, key, hash, value)) {
1907 Py_DECREF(d);
1908 return NULL;
1909 }
1910 }
1911 return d;
1912 }
1913 if (PyAnySet_CheckExact(iterable)) {
1914 PyDictObject *mp = (PyDictObject *)d;
1915 Py_ssize_t pos = 0;
1916 PyObject *key;
1917 Py_hash_t hash;
1918
Victor Stinner742da042016-09-07 17:40:12 -07001919 if (dictresize(mp, ESTIMATE_SIZE(PySet_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001920 Py_DECREF(d);
1921 return NULL;
1922 }
1923
1924 while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
1925 if (insertdict(mp, key, hash, value)) {
1926 Py_DECREF(d);
1927 return NULL;
1928 }
1929 }
1930 return d;
1931 }
1932 }
1933
1934 it = PyObject_GetIter(iterable);
1935 if (it == NULL){
1936 Py_DECREF(d);
1937 return NULL;
1938 }
1939
1940 if (PyDict_CheckExact(d)) {
1941 while ((key = PyIter_Next(it)) != NULL) {
1942 status = PyDict_SetItem(d, key, value);
1943 Py_DECREF(key);
1944 if (status < 0)
1945 goto Fail;
1946 }
1947 } else {
1948 while ((key = PyIter_Next(it)) != NULL) {
1949 status = PyObject_SetItem(d, key, value);
1950 Py_DECREF(key);
1951 if (status < 0)
1952 goto Fail;
1953 }
1954 }
1955
1956 if (PyErr_Occurred())
1957 goto Fail;
1958 Py_DECREF(it);
1959 return d;
1960
1961Fail:
1962 Py_DECREF(it);
1963 Py_DECREF(d);
1964 return NULL;
1965}
1966
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001967/* Methods */
1968
1969static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001970dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001971{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001972 PyObject **values = mp->ma_values;
1973 PyDictKeysObject *keys = mp->ma_keys;
1974 Py_ssize_t i, n;
INADA Naokia6296d32017-08-24 14:55:17 +09001975
1976 /* bpo-31095: UnTrack is needed before calling any callbacks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001977 PyObject_GC_UnTrack(mp);
1978 Py_TRASHCAN_SAFE_BEGIN(mp)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001979 if (values != NULL) {
1980 if (values != empty_values) {
Victor Stinner742da042016-09-07 17:40:12 -07001981 for (i = 0, n = mp->ma_keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001982 Py_XDECREF(values[i]);
1983 }
1984 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001985 }
INADA Naokia7576492018-11-14 18:39:27 +09001986 dictkeys_decref(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001987 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02001988 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001989 assert(keys->dk_refcnt == 1);
INADA Naokia7576492018-11-14 18:39:27 +09001990 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001991 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001992 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1993 free_list[numfree++] = mp;
1994 else
1995 Py_TYPE(mp)->tp_free((PyObject *)mp);
1996 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001997}
1998
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001999
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002000static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002001dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002002{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01002004 PyObject *key = NULL, *value = NULL;
2005 _PyUnicodeWriter writer;
2006 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00002007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002008 i = Py_ReprEnter((PyObject *)mp);
2009 if (i != 0) {
2010 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
2011 }
Guido van Rossum255443b1998-04-10 22:47:14 +00002012
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002013 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01002014 Py_ReprLeave((PyObject *)mp);
2015 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002016 }
Tim Petersa7259592001-06-16 05:11:17 +00002017
Victor Stinnerf91929b2013-11-19 13:07:38 +01002018 _PyUnicodeWriter_Init(&writer);
2019 writer.overallocate = 1;
2020 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
2021 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00002022
Victor Stinnerf91929b2013-11-19 13:07:38 +01002023 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
2024 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00002025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002026 /* Do repr() on each key+value pair, and insert ": " between them.
2027 Note that repr may mutate the dict. */
2028 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01002029 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002030 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01002031 PyObject *s;
2032 int res;
2033
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002034 /* Prevent repr from deleting key or value during key format. */
2035 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002036 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02002037
Victor Stinnerf91929b2013-11-19 13:07:38 +01002038 if (!first) {
2039 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
2040 goto error;
2041 }
2042 first = 0;
2043
2044 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002045 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01002046 goto error;
2047 res = _PyUnicodeWriter_WriteStr(&writer, s);
2048 Py_DECREF(s);
2049 if (res < 0)
2050 goto error;
2051
2052 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
2053 goto error;
2054
2055 s = PyObject_Repr(value);
2056 if (s == NULL)
2057 goto error;
2058 res = _PyUnicodeWriter_WriteStr(&writer, s);
2059 Py_DECREF(s);
2060 if (res < 0)
2061 goto error;
2062
2063 Py_CLEAR(key);
2064 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002065 }
Tim Petersa7259592001-06-16 05:11:17 +00002066
Victor Stinnerf91929b2013-11-19 13:07:38 +01002067 writer.overallocate = 0;
2068 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
2069 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00002070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002071 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01002072
2073 return _PyUnicodeWriter_Finish(&writer);
2074
2075error:
2076 Py_ReprLeave((PyObject *)mp);
2077 _PyUnicodeWriter_Dealloc(&writer);
2078 Py_XDECREF(key);
2079 Py_XDECREF(value);
2080 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002081}
2082
Martin v. Löwis18e16552006-02-15 17:27:45 +00002083static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002084dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002085{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002086 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002087}
2088
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002089static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002090dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002091{
Victor Stinner742da042016-09-07 17:40:12 -07002092 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002093 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09002094 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002096 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002097 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002098 hash = PyObject_Hash(key);
2099 if (hash == -1)
2100 return NULL;
2101 }
INADA Naoki778928b2017-08-03 23:45:15 +09002102 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002103 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002104 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002105 if (ix == DKIX_EMPTY || value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002106 if (!PyDict_CheckExact(mp)) {
2107 /* Look up __missing__ method if we're a subclass. */
2108 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05002109 _Py_IDENTIFIER(__missing__);
2110 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002111 if (missing != NULL) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01002112 res = PyObject_CallFunctionObjArgs(missing,
2113 key, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002114 Py_DECREF(missing);
2115 return res;
2116 }
2117 else if (PyErr_Occurred())
2118 return NULL;
2119 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07002120 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002121 return NULL;
2122 }
INADA Naokiba609772016-12-07 20:41:42 +09002123 Py_INCREF(value);
2124 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002125}
2126
2127static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002128dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002129{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002130 if (w == NULL)
2131 return PyDict_DelItem((PyObject *)mp, v);
2132 else
2133 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002134}
2135
Guido van Rossuma9e7a811997-05-13 21:02:11 +00002136static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002137 (lenfunc)dict_length, /*mp_length*/
2138 (binaryfunc)dict_subscript, /*mp_subscript*/
2139 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002140};
2141
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002142static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002143dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002144{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002145 PyObject *v;
2146 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002147 PyDictKeyEntry *ep;
2148 Py_ssize_t size, n, offset;
2149 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002150
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002151 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002152 n = mp->ma_used;
2153 v = PyList_New(n);
2154 if (v == NULL)
2155 return NULL;
2156 if (n != mp->ma_used) {
2157 /* Durnit. The allocations caused the dict to resize.
2158 * Just start over, this shouldn't normally happen.
2159 */
2160 Py_DECREF(v);
2161 goto again;
2162 }
Victor Stinner742da042016-09-07 17:40:12 -07002163 ep = DK_ENTRIES(mp->ma_keys);
2164 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002165 if (mp->ma_values) {
2166 value_ptr = mp->ma_values;
2167 offset = sizeof(PyObject *);
2168 }
2169 else {
2170 value_ptr = &ep[0].me_value;
2171 offset = sizeof(PyDictKeyEntry);
2172 }
2173 for (i = 0, j = 0; i < size; i++) {
2174 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175 PyObject *key = ep[i].me_key;
2176 Py_INCREF(key);
2177 PyList_SET_ITEM(v, j, key);
2178 j++;
2179 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002180 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002181 }
2182 assert(j == n);
2183 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002184}
2185
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002186static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002187dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002188{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002189 PyObject *v;
2190 Py_ssize_t i, j;
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002191 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002192 Py_ssize_t size, n, offset;
2193 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002194
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002195 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002196 n = mp->ma_used;
2197 v = PyList_New(n);
2198 if (v == NULL)
2199 return NULL;
2200 if (n != mp->ma_used) {
2201 /* Durnit. The allocations caused the dict to resize.
2202 * Just start over, this shouldn't normally happen.
2203 */
2204 Py_DECREF(v);
2205 goto again;
2206 }
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002207 ep = DK_ENTRIES(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002208 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002209 if (mp->ma_values) {
2210 value_ptr = mp->ma_values;
2211 offset = sizeof(PyObject *);
2212 }
2213 else {
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002214 value_ptr = &ep[0].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002215 offset = sizeof(PyDictKeyEntry);
2216 }
2217 for (i = 0, j = 0; i < size; i++) {
2218 PyObject *value = *value_ptr;
2219 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2220 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002221 Py_INCREF(value);
2222 PyList_SET_ITEM(v, j, value);
2223 j++;
2224 }
2225 }
2226 assert(j == n);
2227 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002228}
2229
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002230static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002231dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002232{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002233 PyObject *v;
2234 Py_ssize_t i, j, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002235 Py_ssize_t size, offset;
2236 PyObject *item, *key;
2237 PyDictKeyEntry *ep;
2238 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002240 /* Preallocate the list of tuples, to avoid allocations during
2241 * the loop over the items, which could trigger GC, which
2242 * could resize the dict. :-(
2243 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002244 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002245 n = mp->ma_used;
2246 v = PyList_New(n);
2247 if (v == NULL)
2248 return NULL;
2249 for (i = 0; i < n; i++) {
2250 item = PyTuple_New(2);
2251 if (item == NULL) {
2252 Py_DECREF(v);
2253 return NULL;
2254 }
2255 PyList_SET_ITEM(v, i, item);
2256 }
2257 if (n != mp->ma_used) {
2258 /* Durnit. The allocations caused the dict to resize.
2259 * Just start over, this shouldn't normally happen.
2260 */
2261 Py_DECREF(v);
2262 goto again;
2263 }
2264 /* Nothing we do below makes any function calls. */
Victor Stinner742da042016-09-07 17:40:12 -07002265 ep = DK_ENTRIES(mp->ma_keys);
2266 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002267 if (mp->ma_values) {
2268 value_ptr = mp->ma_values;
2269 offset = sizeof(PyObject *);
2270 }
2271 else {
2272 value_ptr = &ep[0].me_value;
2273 offset = sizeof(PyDictKeyEntry);
2274 }
2275 for (i = 0, j = 0; i < size; i++) {
2276 PyObject *value = *value_ptr;
2277 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2278 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002279 key = ep[i].me_key;
2280 item = PyList_GET_ITEM(v, j);
2281 Py_INCREF(key);
2282 PyTuple_SET_ITEM(item, 0, key);
2283 Py_INCREF(value);
2284 PyTuple_SET_ITEM(item, 1, value);
2285 j++;
2286 }
2287 }
2288 assert(j == n);
2289 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002290}
2291
Larry Hastings5c661892014-01-24 06:17:25 -08002292/*[clinic input]
2293@classmethod
2294dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08002295 iterable: object
2296 value: object=None
2297 /
2298
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002299Create a new dictionary with keys from iterable and values set to value.
Larry Hastings5c661892014-01-24 06:17:25 -08002300[clinic start generated code]*/
2301
Larry Hastings5c661892014-01-24 06:17:25 -08002302static PyObject *
2303dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002304/*[clinic end generated code: output=8fb98e4b10384999 input=382ba4855d0f74c3]*/
Larry Hastings5c661892014-01-24 06:17:25 -08002305{
Eric Snow96c6af92015-05-29 22:21:39 -06002306 return _PyDict_FromKeys((PyObject *)type, iterable, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002307}
2308
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002309static int
Victor Stinner742da042016-09-07 17:40:12 -07002310dict_update_common(PyObject *self, PyObject *args, PyObject *kwds,
2311 const char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002312{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002313 PyObject *arg = NULL;
2314 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002315
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002316 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002317 result = -1;
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002318 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002319 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002320 _Py_IDENTIFIER(keys);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002321 PyObject *func;
2322 if (_PyObject_LookupAttrId(arg, &PyId_keys, &func) < 0) {
2323 result = -1;
2324 }
2325 else if (func != NULL) {
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002326 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002327 result = PyDict_Merge(self, arg, 1);
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002328 }
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002329 else {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002330 result = PyDict_MergeFromSeq2(self, arg, 1);
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002331 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002332 }
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002334 if (result == 0 && kwds != NULL) {
2335 if (PyArg_ValidateKeywordArguments(kwds))
2336 result = PyDict_Merge(self, kwds, 1);
2337 else
2338 result = -1;
2339 }
2340 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002341}
2342
Victor Stinner91f0d4a2017-01-19 12:45:06 +01002343/* Note: dict.update() uses the METH_VARARGS|METH_KEYWORDS calling convention.
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +03002344 Using METH_FASTCALL|METH_KEYWORDS would make dict.update(**dict2) calls
2345 slower, see the issue #29312. */
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002346static PyObject *
2347dict_update(PyObject *self, PyObject *args, PyObject *kwds)
2348{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002349 if (dict_update_common(self, args, kwds, "update") != -1)
2350 Py_RETURN_NONE;
2351 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002352}
2353
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002354/* Update unconditionally replaces existing items.
2355 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00002356 otherwise it leaves existing items unchanged.
2357
2358 PyDict_{Update,Merge} update/merge from a mapping object.
2359
Tim Petersf582b822001-12-11 18:51:08 +00002360 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00002361 producing iterable objects of length 2.
2362*/
2363
Tim Petersf582b822001-12-11 18:51:08 +00002364int
Tim Peters1fc240e2001-10-26 05:06:50 +00002365PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
2366{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002367 PyObject *it; /* iter(seq2) */
2368 Py_ssize_t i; /* index into seq2 of current element */
2369 PyObject *item; /* seq2[i] */
2370 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00002371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002372 assert(d != NULL);
2373 assert(PyDict_Check(d));
2374 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00002375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376 it = PyObject_GetIter(seq2);
2377 if (it == NULL)
2378 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002380 for (i = 0; ; ++i) {
2381 PyObject *key, *value;
2382 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00002383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002384 fast = NULL;
2385 item = PyIter_Next(it);
2386 if (item == NULL) {
2387 if (PyErr_Occurred())
2388 goto Fail;
2389 break;
2390 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392 /* Convert item to sequence, and verify length 2. */
2393 fast = PySequence_Fast(item, "");
2394 if (fast == NULL) {
2395 if (PyErr_ExceptionMatches(PyExc_TypeError))
2396 PyErr_Format(PyExc_TypeError,
2397 "cannot convert dictionary update "
2398 "sequence element #%zd to a sequence",
2399 i);
2400 goto Fail;
2401 }
2402 n = PySequence_Fast_GET_SIZE(fast);
2403 if (n != 2) {
2404 PyErr_Format(PyExc_ValueError,
2405 "dictionary update sequence element #%zd "
2406 "has length %zd; 2 is required",
2407 i, n);
2408 goto Fail;
2409 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002411 /* Update/merge with this (key, value) pair. */
2412 key = PySequence_Fast_GET_ITEM(fast, 0);
2413 value = PySequence_Fast_GET_ITEM(fast, 1);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002414 Py_INCREF(key);
2415 Py_INCREF(value);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002416 if (override) {
2417 if (PyDict_SetItem(d, key, value) < 0) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002418 Py_DECREF(key);
2419 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002420 goto Fail;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002421 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002422 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002423 else if (PyDict_GetItemWithError(d, key) == NULL) {
2424 if (PyErr_Occurred() || PyDict_SetItem(d, key, value) < 0) {
2425 Py_DECREF(key);
2426 Py_DECREF(value);
2427 goto Fail;
2428 }
2429 }
2430
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002431 Py_DECREF(key);
2432 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002433 Py_DECREF(fast);
2434 Py_DECREF(item);
2435 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002437 i = 0;
Victor Stinner611b0fa2016-09-14 15:02:01 +02002438 assert(_PyDict_CheckConsistency((PyDictObject *)d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002439 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00002440Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002441 Py_XDECREF(item);
2442 Py_XDECREF(fast);
2443 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002444Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002445 Py_DECREF(it);
2446 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00002447}
2448
doko@ubuntu.comc96df682016-10-11 08:04:02 +02002449static int
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002450dict_merge(PyObject *a, PyObject *b, int override)
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002451{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002452 PyDictObject *mp, *other;
2453 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -07002454 PyDictKeyEntry *entry, *ep0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002455
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002456 assert(0 <= override && override <= 2);
2457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002458 /* We accept for the argument either a concrete dictionary object,
2459 * or an abstract "mapping" object. For the former, we can do
2460 * things quite efficiently. For the latter, we only require that
2461 * PyMapping_Keys() and PyObject_GetItem() be supported.
2462 */
2463 if (a == NULL || !PyDict_Check(a) || b == NULL) {
2464 PyErr_BadInternalCall();
2465 return -1;
2466 }
2467 mp = (PyDictObject*)a;
INADA Naoki2aaf98c2018-09-26 12:59:00 +09002468 if (PyDict_Check(b) && (Py_TYPE(b)->tp_iter == (getiterfunc)dict_iter)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002469 other = (PyDictObject*)b;
2470 if (other == mp || other->ma_used == 0)
2471 /* a.update(a) or a.update({}); nothing to do */
2472 return 0;
2473 if (mp->ma_used == 0)
2474 /* Since the target dict is empty, PyDict_GetItem()
2475 * always returns NULL. Setting override to 1
2476 * skips the unnecessary test.
2477 */
2478 override = 1;
2479 /* Do one big resize at the start, rather than
2480 * incrementally resizing as we insert new items. Expect
2481 * that there will be no (or few) overlapping keys.
2482 */
INADA Naokib1152be2016-10-27 19:26:50 +09002483 if (USABLE_FRACTION(mp->ma_keys->dk_size) < other->ma_used) {
2484 if (dictresize(mp, ESTIMATE_SIZE(mp->ma_used + other->ma_used))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002485 return -1;
INADA Naokib1152be2016-10-27 19:26:50 +09002486 }
2487 }
Victor Stinner742da042016-09-07 17:40:12 -07002488 ep0 = DK_ENTRIES(other->ma_keys);
2489 for (i = 0, n = other->ma_keys->dk_nentries; i < n; i++) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002490 PyObject *key, *value;
2491 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002492 entry = &ep0[i];
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002493 key = entry->me_key;
2494 hash = entry->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002495 if (other->ma_values)
2496 value = other->ma_values[i];
2497 else
2498 value = entry->me_value;
2499
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002500 if (value != NULL) {
2501 int err = 0;
2502 Py_INCREF(key);
2503 Py_INCREF(value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002504 if (override == 1)
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002505 err = insertdict(mp, key, hash, value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002506 else if (_PyDict_GetItem_KnownHash(a, key, hash) == NULL) {
2507 if (PyErr_Occurred()) {
2508 Py_DECREF(value);
2509 Py_DECREF(key);
2510 return -1;
2511 }
2512 err = insertdict(mp, key, hash, value);
2513 }
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002514 else if (override != 0) {
2515 _PyErr_SetKeyError(key);
2516 Py_DECREF(value);
2517 Py_DECREF(key);
2518 return -1;
2519 }
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002520 Py_DECREF(value);
2521 Py_DECREF(key);
2522 if (err != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002523 return -1;
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002524
Victor Stinner742da042016-09-07 17:40:12 -07002525 if (n != other->ma_keys->dk_nentries) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002526 PyErr_SetString(PyExc_RuntimeError,
2527 "dict mutated during update");
2528 return -1;
2529 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002530 }
2531 }
2532 }
2533 else {
2534 /* Do it the generic, slower way */
2535 PyObject *keys = PyMapping_Keys(b);
2536 PyObject *iter;
2537 PyObject *key, *value;
2538 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002540 if (keys == NULL)
2541 /* Docstring says this is equivalent to E.keys() so
2542 * if E doesn't have a .keys() method we want
2543 * AttributeError to percolate up. Might as well
2544 * do the same for any other error.
2545 */
2546 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002548 iter = PyObject_GetIter(keys);
2549 Py_DECREF(keys);
2550 if (iter == NULL)
2551 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002553 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002554 if (override != 1) {
2555 if (PyDict_GetItemWithError(a, key) != NULL) {
2556 if (override != 0) {
2557 _PyErr_SetKeyError(key);
2558 Py_DECREF(key);
2559 Py_DECREF(iter);
2560 return -1;
2561 }
2562 Py_DECREF(key);
2563 continue;
2564 }
2565 else if (PyErr_Occurred()) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002566 Py_DECREF(key);
2567 Py_DECREF(iter);
2568 return -1;
2569 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002570 }
2571 value = PyObject_GetItem(b, key);
2572 if (value == NULL) {
2573 Py_DECREF(iter);
2574 Py_DECREF(key);
2575 return -1;
2576 }
2577 status = PyDict_SetItem(a, key, value);
2578 Py_DECREF(key);
2579 Py_DECREF(value);
2580 if (status < 0) {
2581 Py_DECREF(iter);
2582 return -1;
2583 }
2584 }
2585 Py_DECREF(iter);
2586 if (PyErr_Occurred())
2587 /* Iterator completed, via error */
2588 return -1;
2589 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02002590 assert(_PyDict_CheckConsistency((PyDictObject *)a));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002591 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002592}
2593
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002594int
2595PyDict_Update(PyObject *a, PyObject *b)
2596{
2597 return dict_merge(a, b, 1);
2598}
2599
2600int
2601PyDict_Merge(PyObject *a, PyObject *b, int override)
2602{
2603 /* XXX Deprecate override not in (0, 1). */
2604 return dict_merge(a, b, override != 0);
2605}
2606
2607int
2608_PyDict_MergeEx(PyObject *a, PyObject *b, int override)
2609{
2610 return dict_merge(a, b, override);
2611}
2612
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002613static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302614dict_copy(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002615{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002616 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002617}
2618
2619PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002620PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002621{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002622 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002623 PyDictObject *mp;
2624 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002625
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002626 if (o == NULL || !PyDict_Check(o)) {
2627 PyErr_BadInternalCall();
2628 return NULL;
2629 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002630
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002631 mp = (PyDictObject *)o;
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002632 if (mp->ma_used == 0) {
2633 /* The dict is empty; just return a new dict. */
2634 return PyDict_New();
2635 }
2636
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002637 if (_PyDict_HasSplitTable(mp)) {
2638 PyDictObject *split_copy;
Victor Stinner742da042016-09-07 17:40:12 -07002639 Py_ssize_t size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
2640 PyObject **newvalues;
2641 newvalues = new_values(size);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002642 if (newvalues == NULL)
2643 return PyErr_NoMemory();
2644 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2645 if (split_copy == NULL) {
2646 free_values(newvalues);
2647 return NULL;
2648 }
2649 split_copy->ma_values = newvalues;
2650 split_copy->ma_keys = mp->ma_keys;
2651 split_copy->ma_used = mp->ma_used;
INADA Naokid1c82c52018-04-03 11:43:53 +09002652 split_copy->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokia7576492018-11-14 18:39:27 +09002653 dictkeys_incref(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002654 for (i = 0, n = size; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002655 PyObject *value = mp->ma_values[i];
2656 Py_XINCREF(value);
2657 split_copy->ma_values[i] = value;
2658 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002659 if (_PyObject_GC_IS_TRACKED(mp))
2660 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002661 return (PyObject *)split_copy;
2662 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002663
2664 if (PyDict_CheckExact(mp) && mp->ma_values == NULL &&
2665 (mp->ma_used >= (mp->ma_keys->dk_nentries * 2) / 3))
2666 {
2667 /* Use fast-copy if:
2668
2669 (1) 'mp' is an instance of a subclassed dict; and
2670
2671 (2) 'mp' is not a split-dict; and
2672
2673 (3) if 'mp' is non-compact ('del' operation does not resize dicts),
2674 do fast-copy only if it has at most 1/3 non-used keys.
2675
Ville Skyttä61f82e02018-04-20 23:08:45 +03002676 The last condition (3) is important to guard against a pathological
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002677 case when a large dict is almost emptied with multiple del/pop
2678 operations and copied after that. In cases like this, we defer to
2679 PyDict_Merge, which produces a compacted copy.
2680 */
2681 return clone_combined_dict(mp);
2682 }
2683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002684 copy = PyDict_New();
2685 if (copy == NULL)
2686 return NULL;
2687 if (PyDict_Merge(copy, o, 1) == 0)
2688 return copy;
2689 Py_DECREF(copy);
2690 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002691}
2692
Martin v. Löwis18e16552006-02-15 17:27:45 +00002693Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002694PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002695{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002696 if (mp == NULL || !PyDict_Check(mp)) {
2697 PyErr_BadInternalCall();
2698 return -1;
2699 }
2700 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002701}
2702
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002703PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002704PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002705{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002706 if (mp == NULL || !PyDict_Check(mp)) {
2707 PyErr_BadInternalCall();
2708 return NULL;
2709 }
2710 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002711}
2712
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002713PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002714PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002715{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002716 if (mp == NULL || !PyDict_Check(mp)) {
2717 PyErr_BadInternalCall();
2718 return NULL;
2719 }
2720 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002721}
2722
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002723PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002724PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002725{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002726 if (mp == NULL || !PyDict_Check(mp)) {
2727 PyErr_BadInternalCall();
2728 return NULL;
2729 }
2730 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002731}
2732
Tim Peterse63415e2001-05-08 04:38:29 +00002733/* Return 1 if dicts equal, 0 if not, -1 if error.
2734 * Gets out as soon as any difference is detected.
2735 * Uses only Py_EQ comparison.
2736 */
2737static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002738dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002739{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002740 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002741
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002742 if (a->ma_used != b->ma_used)
2743 /* can't be equal if # of entries differ */
2744 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002745 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Victor Stinner742da042016-09-07 17:40:12 -07002746 for (i = 0; i < a->ma_keys->dk_nentries; i++) {
2747 PyDictKeyEntry *ep = &DK_ENTRIES(a->ma_keys)[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002748 PyObject *aval;
2749 if (a->ma_values)
2750 aval = a->ma_values[i];
2751 else
2752 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002753 if (aval != NULL) {
2754 int cmp;
2755 PyObject *bval;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002756 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 /* temporarily bump aval's refcount to ensure it stays
2758 alive until we're done with it */
2759 Py_INCREF(aval);
2760 /* ditto for key */
2761 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002762 /* reuse the known hash value */
INADA Naoki778928b2017-08-03 23:45:15 +09002763 b->ma_keys->dk_lookup(b, key, ep->me_hash, &bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002764 if (bval == NULL) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002765 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002766 Py_DECREF(aval);
2767 if (PyErr_Occurred())
2768 return -1;
2769 return 0;
2770 }
2771 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002772 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002773 Py_DECREF(aval);
2774 if (cmp <= 0) /* error or not equal */
2775 return cmp;
2776 }
2777 }
2778 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002779}
Tim Peterse63415e2001-05-08 04:38:29 +00002780
2781static PyObject *
2782dict_richcompare(PyObject *v, PyObject *w, int op)
2783{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002784 int cmp;
2785 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002787 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2788 res = Py_NotImplemented;
2789 }
2790 else if (op == Py_EQ || op == Py_NE) {
2791 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2792 if (cmp < 0)
2793 return NULL;
2794 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2795 }
2796 else
2797 res = Py_NotImplemented;
2798 Py_INCREF(res);
2799 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002800}
Tim Peterse63415e2001-05-08 04:38:29 +00002801
Larry Hastings61272b72014-01-07 12:41:53 -08002802/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002803
2804@coexist
2805dict.__contains__
2806
2807 key: object
2808 /
2809
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002810True if the dictionary has the specified key, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002811[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002812
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002813static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002814dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka19d25972017-02-04 08:05:07 +02002815/*[clinic end generated code: output=a3d03db709ed6e6b input=fe1cb42ad831e820]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002816{
Larry Hastingsc2047262014-01-25 20:43:29 -08002817 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002818 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002819 Py_ssize_t ix;
INADA Naokiba609772016-12-07 20:41:42 +09002820 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002822 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002823 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002824 hash = PyObject_Hash(key);
2825 if (hash == -1)
2826 return NULL;
2827 }
INADA Naoki778928b2017-08-03 23:45:15 +09002828 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002829 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002830 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002831 if (ix == DKIX_EMPTY || value == NULL)
Victor Stinner742da042016-09-07 17:40:12 -07002832 Py_RETURN_FALSE;
2833 Py_RETURN_TRUE;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002834}
2835
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002836/*[clinic input]
2837dict.get
2838
2839 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002840 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002841 /
2842
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002843Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002844[clinic start generated code]*/
2845
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002846static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002847dict_get_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002848/*[clinic end generated code: output=bba707729dee05bf input=279ddb5790b6b107]*/
Barry Warsawc38c5da1997-10-06 17:49:20 +00002849{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002850 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002851 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002852 Py_ssize_t ix;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002854 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002855 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002856 hash = PyObject_Hash(key);
2857 if (hash == -1)
2858 return NULL;
2859 }
INADA Naoki778928b2017-08-03 23:45:15 +09002860 ix = (self->ma_keys->dk_lookup) (self, key, hash, &val);
Victor Stinner742da042016-09-07 17:40:12 -07002861 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002862 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002863 if (ix == DKIX_EMPTY || val == NULL) {
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002864 val = default_value;
INADA Naokiba609772016-12-07 20:41:42 +09002865 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002866 Py_INCREF(val);
2867 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002868}
2869
Benjamin Peterson00e98862013-03-07 22:16:29 -05002870PyObject *
2871PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002872{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002873 PyDictObject *mp = (PyDictObject *)d;
INADA Naoki93f26f72016-11-02 18:45:16 +09002874 PyObject *value;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002875 Py_hash_t hash;
Guido van Rossum164452c2000-08-08 16:12:54 +00002876
Benjamin Peterson00e98862013-03-07 22:16:29 -05002877 if (!PyDict_Check(d)) {
2878 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002880 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002882 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002883 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002884 hash = PyObject_Hash(key);
2885 if (hash == -1)
2886 return NULL;
2887 }
Inada Naoki2ddc7f62019-03-18 20:38:33 +09002888 if (mp->ma_keys == Py_EMPTY_KEYS) {
2889 if (insert_to_emptydict(mp, key, hash, defaultobj) < 0) {
2890 return NULL;
2891 }
2892 return defaultobj;
2893 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002894
2895 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
2896 if (insertion_resize(mp) < 0)
2897 return NULL;
2898 }
2899
INADA Naoki778928b2017-08-03 23:45:15 +09002900 Py_ssize_t ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002901 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002902 return NULL;
INADA Naoki93f26f72016-11-02 18:45:16 +09002903
2904 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09002905 ((ix >= 0 && value == NULL && mp->ma_used != ix) ||
INADA Naoki93f26f72016-11-02 18:45:16 +09002906 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
2907 if (insertion_resize(mp) < 0) {
2908 return NULL;
2909 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002910 ix = DKIX_EMPTY;
2911 }
2912
2913 if (ix == DKIX_EMPTY) {
2914 PyDictKeyEntry *ep, *ep0;
2915 value = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002916 if (mp->ma_keys->dk_usable <= 0) {
Victor Stinner3c336c52016-09-12 14:17:40 +02002917 if (insertion_resize(mp) < 0) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002918 return NULL;
Victor Stinner3c336c52016-09-12 14:17:40 +02002919 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002920 }
INADA Naoki778928b2017-08-03 23:45:15 +09002921 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naoki93f26f72016-11-02 18:45:16 +09002922 ep0 = DK_ENTRIES(mp->ma_keys);
2923 ep = &ep0[mp->ma_keys->dk_nentries];
INADA Naokia7576492018-11-14 18:39:27 +09002924 dictkeys_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002925 Py_INCREF(key);
INADA Naoki93f26f72016-11-02 18:45:16 +09002926 Py_INCREF(value);
2927 MAINTAIN_TRACKING(mp, key, value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002928 ep->me_key = key;
2929 ep->me_hash = hash;
INADA Naokiba609772016-12-07 20:41:42 +09002930 if (_PyDict_HasSplitTable(mp)) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002931 assert(mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
2932 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Victor Stinner742da042016-09-07 17:40:12 -07002933 }
2934 else {
INADA Naoki93f26f72016-11-02 18:45:16 +09002935 ep->me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07002936 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002937 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002938 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naoki93f26f72016-11-02 18:45:16 +09002939 mp->ma_keys->dk_usable--;
2940 mp->ma_keys->dk_nentries++;
2941 assert(mp->ma_keys->dk_usable >= 0);
2942 }
INADA Naokiba609772016-12-07 20:41:42 +09002943 else if (value == NULL) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002944 value = defaultobj;
2945 assert(_PyDict_HasSplitTable(mp));
2946 assert(ix == mp->ma_used);
2947 Py_INCREF(value);
2948 MAINTAIN_TRACKING(mp, key, value);
INADA Naokiba609772016-12-07 20:41:42 +09002949 mp->ma_values[ix] = value;
INADA Naoki93f26f72016-11-02 18:45:16 +09002950 mp->ma_used++;
2951 mp->ma_version_tag = DICT_NEXT_VERSION();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002952 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002953
2954 assert(_PyDict_CheckConsistency(mp));
2955 return value;
Guido van Rossum164452c2000-08-08 16:12:54 +00002956}
2957
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002958/*[clinic input]
2959dict.setdefault
2960
2961 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002962 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002963 /
2964
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002965Insert key with a value of default if key is not in the dictionary.
2966
2967Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002968[clinic start generated code]*/
2969
Benjamin Peterson00e98862013-03-07 22:16:29 -05002970static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002971dict_setdefault_impl(PyDictObject *self, PyObject *key,
2972 PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002973/*[clinic end generated code: output=f8c1101ebf69e220 input=0f063756e815fd9d]*/
Benjamin Peterson00e98862013-03-07 22:16:29 -05002974{
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002975 PyObject *val;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002976
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002977 val = PyDict_SetDefault((PyObject *)self, key, default_value);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002978 Py_XINCREF(val);
2979 return val;
2980}
Guido van Rossum164452c2000-08-08 16:12:54 +00002981
2982static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302983dict_clear(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002984{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002985 PyDict_Clear((PyObject *)mp);
2986 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002987}
2988
Guido van Rossumba6ab842000-12-12 22:02:18 +00002989static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002990dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002991{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002992 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00002993
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002994 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2995 return NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06002996
Yury Selivanov684ef2c2016-10-28 19:01:21 -04002997 return _PyDict_Pop((PyObject*)mp, key, deflt);
Guido van Rossume027d982002-04-12 15:11:59 +00002998}
2999
3000static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303001dict_popitem(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Guido van Rossumba6ab842000-12-12 22:02:18 +00003002{
Victor Stinner742da042016-09-07 17:40:12 -07003003 Py_ssize_t i, j;
3004 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003005 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00003006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003007 /* Allocate the result tuple before checking the size. Believe it
3008 * or not, this allocation could trigger a garbage collection which
3009 * could empty the dict, so if we checked the size first and that
3010 * happened, the result would be an infinite loop (searching for an
3011 * entry that no longer exists). Note that the usual popitem()
3012 * idiom is "while d: k, v = d.popitem()". so needing to throw the
3013 * tuple away if the dict *is* empty isn't a significant
3014 * inefficiency -- possible, but unlikely in practice.
3015 */
3016 res = PyTuple_New(2);
3017 if (res == NULL)
3018 return NULL;
3019 if (mp->ma_used == 0) {
3020 Py_DECREF(res);
3021 PyErr_SetString(PyExc_KeyError,
3022 "popitem(): dictionary is empty");
3023 return NULL;
3024 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003025 /* Convert split table to combined table */
3026 if (mp->ma_keys->dk_lookup == lookdict_split) {
3027 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
3028 Py_DECREF(res);
3029 return NULL;
3030 }
3031 }
3032 ENSURE_ALLOWS_DELETIONS(mp);
Victor Stinner742da042016-09-07 17:40:12 -07003033
3034 /* Pop last item */
3035 ep0 = DK_ENTRIES(mp->ma_keys);
3036 i = mp->ma_keys->dk_nentries - 1;
3037 while (i >= 0 && ep0[i].me_value == NULL) {
3038 i--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003039 }
Victor Stinner742da042016-09-07 17:40:12 -07003040 assert(i >= 0);
3041
3042 ep = &ep0[i];
3043 j = lookdict_index(mp->ma_keys, ep->me_hash, i);
3044 assert(j >= 0);
INADA Naokia7576492018-11-14 18:39:27 +09003045 assert(dictkeys_get_index(mp->ma_keys, j) == i);
3046 dictkeys_set_index(mp->ma_keys, j, DKIX_DUMMY);
Victor Stinner742da042016-09-07 17:40:12 -07003047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003048 PyTuple_SET_ITEM(res, 0, ep->me_key);
3049 PyTuple_SET_ITEM(res, 1, ep->me_value);
Victor Stinner742da042016-09-07 17:40:12 -07003050 ep->me_key = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003051 ep->me_value = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07003052 /* We can't dk_usable++ since there is DKIX_DUMMY in indices */
3053 mp->ma_keys->dk_nentries = i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003054 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003055 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +02003056 assert(_PyDict_CheckConsistency(mp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003057 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00003058}
3059
Jeremy Hylton8caad492000-06-23 14:18:11 +00003060static int
3061dict_traverse(PyObject *op, visitproc visit, void *arg)
3062{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003063 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson55f44522016-09-05 12:12:59 -07003064 PyDictKeysObject *keys = mp->ma_keys;
Serhiy Storchaka46825d22016-09-26 21:29:34 +03003065 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Victor Stinner742da042016-09-07 17:40:12 -07003066 Py_ssize_t i, n = keys->dk_nentries;
3067
Benjamin Peterson55f44522016-09-05 12:12:59 -07003068 if (keys->dk_lookup == lookdict) {
3069 for (i = 0; i < n; i++) {
3070 if (entries[i].me_value != NULL) {
3071 Py_VISIT(entries[i].me_value);
3072 Py_VISIT(entries[i].me_key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003073 }
3074 }
Victor Stinner742da042016-09-07 17:40:12 -07003075 }
3076 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003077 if (mp->ma_values != NULL) {
Benjamin Peterson55f44522016-09-05 12:12:59 -07003078 for (i = 0; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003079 Py_VISIT(mp->ma_values[i]);
3080 }
3081 }
3082 else {
Benjamin Peterson55f44522016-09-05 12:12:59 -07003083 for (i = 0; i < n; i++) {
3084 Py_VISIT(entries[i].me_value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003085 }
3086 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003087 }
3088 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00003089}
3090
3091static int
3092dict_tp_clear(PyObject *op)
3093{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003094 PyDict_Clear(op);
3095 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00003096}
3097
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003098static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003099
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003100Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003101_PyDict_SizeOf(PyDictObject *mp)
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003102{
Victor Stinner742da042016-09-07 17:40:12 -07003103 Py_ssize_t size, usable, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003104
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003105 size = DK_SIZE(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07003106 usable = USABLE_FRACTION(size);
3107
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02003108 res = _PyObject_SIZE(Py_TYPE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003109 if (mp->ma_values)
Victor Stinner742da042016-09-07 17:40:12 -07003110 res += usable * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003111 /* If the dictionary is split, the keys portion is accounted-for
3112 in the type object. */
3113 if (mp->ma_keys->dk_refcnt == 1)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003114 res += (sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003115 + DK_IXSIZE(mp->ma_keys) * size
3116 + sizeof(PyDictKeyEntry) * usable);
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003117 return res;
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003118}
3119
3120Py_ssize_t
3121_PyDict_KeysSize(PyDictKeysObject *keys)
3122{
Victor Stinner98ee9d52016-09-08 09:33:56 -07003123 return (sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003124 + DK_IXSIZE(keys) * DK_SIZE(keys)
3125 + USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry));
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003126}
3127
doko@ubuntu.com17210f52016-01-14 14:04:59 +01003128static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303129dict_sizeof(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003130{
3131 return PyLong_FromSsize_t(_PyDict_SizeOf(mp));
3132}
3133
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00003134PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
3135
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003136PyDoc_STRVAR(sizeof__doc__,
3137"D.__sizeof__() -> size of D in memory, in bytes");
3138
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003139PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00003140"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00003141If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00003142
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003143PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00003144"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000031452-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003146
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003147PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04003148"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
3149If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
3150If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
3151In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00003152
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003153PyDoc_STRVAR(clear__doc__,
3154"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003155
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003156PyDoc_STRVAR(copy__doc__,
3157"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00003158
Guido van Rossumb90c8482007-02-10 01:11:45 +00003159/* Forward */
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303160static PyObject *dictkeys_new(PyObject *, PyObject *);
3161static PyObject *dictitems_new(PyObject *, PyObject *);
3162static PyObject *dictvalues_new(PyObject *, PyObject *);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003163
Guido van Rossum45c85d12007-07-27 16:31:40 +00003164PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003165 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003166PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003167 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003168PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003169 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00003170
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003171static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07003172 DICT___CONTAINS___METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003173 {"__getitem__", (PyCFunction)(void(*)(void))dict_subscript, METH_O | METH_COEXIST,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003174 getitem__doc__},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003175 {"__sizeof__", (PyCFunction)(void(*)(void))dict_sizeof, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003176 sizeof__doc__},
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003177 DICT_GET_METHODDEF
3178 DICT_SETDEFAULT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003179 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
3180 pop__doc__},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003181 {"popitem", (PyCFunction)(void(*)(void))dict_popitem, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003182 popitem__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303183 {"keys", dictkeys_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003184 keys__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303185 {"items", dictitems_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003186 items__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303187 {"values", dictvalues_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003188 values__doc__},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003189 {"update", (PyCFunction)(void(*)(void))dict_update, METH_VARARGS | METH_KEYWORDS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003190 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08003191 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003192 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
3193 clear__doc__},
3194 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
3195 copy__doc__},
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003196 DICT___REVERSED___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003197 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003198};
3199
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00003200/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00003201int
3202PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003203{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003204 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07003205 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003206 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003207 PyObject *value;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003209 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003210 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003211 hash = PyObject_Hash(key);
3212 if (hash == -1)
3213 return -1;
3214 }
INADA Naoki778928b2017-08-03 23:45:15 +09003215 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003216 if (ix == DKIX_ERROR)
3217 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003218 return (ix != DKIX_EMPTY && value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003219}
3220
Thomas Wouterscf297e42007-02-23 15:07:44 +00003221/* Internal version of PyDict_Contains used when the hash value is already known */
3222int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003223_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00003224{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003225 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003226 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -07003227 Py_ssize_t ix;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003228
INADA Naoki778928b2017-08-03 23:45:15 +09003229 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003230 if (ix == DKIX_ERROR)
3231 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003232 return (ix != DKIX_EMPTY && value != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00003233}
3234
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003235/* Hack to implement "key in dict" */
3236static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003237 0, /* sq_length */
3238 0, /* sq_concat */
3239 0, /* sq_repeat */
3240 0, /* sq_item */
3241 0, /* sq_slice */
3242 0, /* sq_ass_item */
3243 0, /* sq_ass_slice */
3244 PyDict_Contains, /* sq_contains */
3245 0, /* sq_inplace_concat */
3246 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003247};
3248
Guido van Rossum09e563a2001-05-01 12:10:21 +00003249static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003250dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3251{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003252 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003253 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003255 assert(type != NULL && type->tp_alloc != NULL);
3256 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02003257 if (self == NULL)
3258 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003259 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003260
Victor Stinnera9f61a52013-07-16 22:17:26 +02003261 /* The object has been implicitly tracked by tp_alloc */
3262 if (type == &PyDict_Type)
3263 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003264
3265 d->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003266 d->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07003267 d->ma_keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003268 if (d->ma_keys == NULL) {
3269 Py_DECREF(self);
3270 return NULL;
3271 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02003272 assert(_PyDict_CheckConsistency(d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003273 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003274}
3275
Tim Peters25786c02001-09-02 08:22:48 +00003276static int
3277dict_init(PyObject *self, PyObject *args, PyObject *kwds)
3278{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003279 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00003280}
3281
Tim Peters6d6c1a32001-08-02 04:15:00 +00003282static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003283dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00003284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003285 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003286}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003287
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003288PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00003289"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00003290"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003291" (key, value) pairs\n"
3292"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00003293" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003294" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00003295" d[k] = v\n"
3296"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
3297" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00003298
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003299PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003300 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3301 "dict",
3302 sizeof(PyDictObject),
3303 0,
3304 (destructor)dict_dealloc, /* tp_dealloc */
3305 0, /* tp_print */
3306 0, /* tp_getattr */
3307 0, /* tp_setattr */
3308 0, /* tp_reserved */
3309 (reprfunc)dict_repr, /* tp_repr */
3310 0, /* tp_as_number */
3311 &dict_as_sequence, /* tp_as_sequence */
3312 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003313 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003314 0, /* tp_call */
3315 0, /* tp_str */
3316 PyObject_GenericGetAttr, /* tp_getattro */
3317 0, /* tp_setattro */
3318 0, /* tp_as_buffer */
3319 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3320 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
3321 dictionary_doc, /* tp_doc */
3322 dict_traverse, /* tp_traverse */
3323 dict_tp_clear, /* tp_clear */
3324 dict_richcompare, /* tp_richcompare */
3325 0, /* tp_weaklistoffset */
3326 (getiterfunc)dict_iter, /* tp_iter */
3327 0, /* tp_iternext */
3328 mapp_methods, /* tp_methods */
3329 0, /* tp_members */
3330 0, /* tp_getset */
3331 0, /* tp_base */
3332 0, /* tp_dict */
3333 0, /* tp_descr_get */
3334 0, /* tp_descr_set */
3335 0, /* tp_dictoffset */
3336 dict_init, /* tp_init */
3337 PyType_GenericAlloc, /* tp_alloc */
3338 dict_new, /* tp_new */
3339 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003340};
3341
Victor Stinner3c1e4812012-03-26 22:10:51 +02003342PyObject *
3343_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
3344{
3345 PyObject *kv;
3346 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02003347 if (kv == NULL) {
3348 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003349 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02003350 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02003351 return PyDict_GetItem(dp, kv);
3352}
3353
Guido van Rossum3cca2451997-05-16 14:23:33 +00003354/* For backward compatibility with old dictionary interface */
3355
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003356PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003357PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003358{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003359 PyObject *kv, *rv;
3360 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003361 if (kv == NULL) {
3362 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003363 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003364 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003365 rv = PyDict_GetItem(v, kv);
3366 Py_DECREF(kv);
3367 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003368}
3369
3370int
Victor Stinner3c1e4812012-03-26 22:10:51 +02003371_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
3372{
3373 PyObject *kv;
3374 kv = _PyUnicode_FromId(key); /* borrowed */
3375 if (kv == NULL)
3376 return -1;
3377 return PyDict_SetItem(v, kv, item);
3378}
3379
3380int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003381PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003382{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003383 PyObject *kv;
3384 int err;
3385 kv = PyUnicode_FromString(key);
3386 if (kv == NULL)
3387 return -1;
3388 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
3389 err = PyDict_SetItem(v, kv, item);
3390 Py_DECREF(kv);
3391 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003392}
3393
3394int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01003395_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
3396{
3397 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
3398 if (kv == NULL)
3399 return -1;
3400 return PyDict_DelItem(v, kv);
3401}
3402
3403int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003404PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003405{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003406 PyObject *kv;
3407 int err;
3408 kv = PyUnicode_FromString(key);
3409 if (kv == NULL)
3410 return -1;
3411 err = PyDict_DelItem(v, kv);
3412 Py_DECREF(kv);
3413 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003414}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003415
Raymond Hettinger019a1482004-03-18 02:41:19 +00003416/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003417
3418typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003419 PyObject_HEAD
3420 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
3421 Py_ssize_t di_used;
3422 Py_ssize_t di_pos;
3423 PyObject* di_result; /* reusable result tuple for iteritems */
3424 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003425} dictiterobject;
3426
3427static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003428dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003429{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003430 dictiterobject *di;
3431 di = PyObject_GC_New(dictiterobject, itertype);
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003432 if (di == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003433 return NULL;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003434 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003435 Py_INCREF(dict);
3436 di->di_dict = dict;
3437 di->di_used = dict->ma_used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003438 di->len = dict->ma_used;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003439 if ((itertype == &PyDictRevIterKey_Type ||
3440 itertype == &PyDictRevIterItem_Type ||
3441 itertype == &PyDictRevIterValue_Type) && dict->ma_used) {
3442 di->di_pos = dict->ma_keys->dk_nentries - 1;
3443 }
3444 else {
3445 di->di_pos = 0;
3446 }
3447 if (itertype == &PyDictIterItem_Type ||
3448 itertype == &PyDictRevIterItem_Type) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003449 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
3450 if (di->di_result == NULL) {
3451 Py_DECREF(di);
3452 return NULL;
3453 }
3454 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003455 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003456 di->di_result = NULL;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003457 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003458 _PyObject_GC_TRACK(di);
3459 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003460}
3461
3462static void
3463dictiter_dealloc(dictiterobject *di)
3464{
INADA Naokia6296d32017-08-24 14:55:17 +09003465 /* bpo-31095: UnTrack is needed before calling any callbacks */
3466 _PyObject_GC_UNTRACK(di);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003467 Py_XDECREF(di->di_dict);
3468 Py_XDECREF(di->di_result);
3469 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003470}
3471
3472static int
3473dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
3474{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003475 Py_VISIT(di->di_dict);
3476 Py_VISIT(di->di_result);
3477 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003478}
3479
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003480static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303481dictiter_len(dictiterobject *di, PyObject *Py_UNUSED(ignored))
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003482{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003483 Py_ssize_t len = 0;
3484 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
3485 len = di->len;
3486 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003487}
3488
Guido van Rossumb90c8482007-02-10 01:11:45 +00003489PyDoc_STRVAR(length_hint_doc,
3490 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003491
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003492static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303493dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003494
3495PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3496
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003497static PyMethodDef dictiter_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003498 {"__length_hint__", (PyCFunction)(void(*)(void))dictiter_len, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003499 length_hint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003500 {"__reduce__", (PyCFunction)(void(*)(void))dictiter_reduce, METH_NOARGS,
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003501 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003502 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003503};
3504
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003505static PyObject*
3506dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00003507{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003508 PyObject *key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003509 Py_ssize_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003510 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003511 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00003512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003513 if (d == NULL)
3514 return NULL;
3515 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00003516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003517 if (di->di_used != d->ma_used) {
3518 PyErr_SetString(PyExc_RuntimeError,
3519 "dictionary changed size during iteration");
3520 di->di_used = -1; /* Make this state sticky */
3521 return NULL;
3522 }
Guido van Rossum2147df72002-07-16 20:30:22 +00003523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003524 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003525 k = d->ma_keys;
INADA Naokica2d8be2016-11-04 16:59:10 +09003526 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003527 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003528 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003529 goto fail;
3530 key = DK_ENTRIES(k)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003531 assert(d->ma_values[i] != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003532 }
3533 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003534 Py_ssize_t n = k->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003535 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
3536 while (i < n && entry_ptr->me_value == NULL) {
3537 entry_ptr++;
3538 i++;
3539 }
3540 if (i >= n)
3541 goto fail;
3542 key = entry_ptr->me_key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003543 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003544 di->di_pos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003545 di->len--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003546 Py_INCREF(key);
3547 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003548
3549fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003550 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003551 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003552 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003553}
3554
Raymond Hettinger019a1482004-03-18 02:41:19 +00003555PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003556 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3557 "dict_keyiterator", /* tp_name */
3558 sizeof(dictiterobject), /* tp_basicsize */
3559 0, /* tp_itemsize */
3560 /* methods */
3561 (destructor)dictiter_dealloc, /* tp_dealloc */
3562 0, /* tp_print */
3563 0, /* tp_getattr */
3564 0, /* tp_setattr */
3565 0, /* tp_reserved */
3566 0, /* tp_repr */
3567 0, /* tp_as_number */
3568 0, /* tp_as_sequence */
3569 0, /* tp_as_mapping */
3570 0, /* tp_hash */
3571 0, /* tp_call */
3572 0, /* tp_str */
3573 PyObject_GenericGetAttr, /* tp_getattro */
3574 0, /* tp_setattro */
3575 0, /* tp_as_buffer */
3576 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3577 0, /* tp_doc */
3578 (traverseproc)dictiter_traverse, /* tp_traverse */
3579 0, /* tp_clear */
3580 0, /* tp_richcompare */
3581 0, /* tp_weaklistoffset */
3582 PyObject_SelfIter, /* tp_iter */
3583 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
3584 dictiter_methods, /* tp_methods */
3585 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003586};
3587
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003588static PyObject *
3589dictiter_iternextvalue(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003590{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003591 PyObject *value;
INADA Naokica2d8be2016-11-04 16:59:10 +09003592 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003593 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003595 if (d == NULL)
3596 return NULL;
3597 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003599 if (di->di_used != d->ma_used) {
3600 PyErr_SetString(PyExc_RuntimeError,
3601 "dictionary changed size during iteration");
3602 di->di_used = -1; /* Make this state sticky */
3603 return NULL;
3604 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003606 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003607 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003608 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003609 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003610 goto fail;
INADA Naokica2d8be2016-11-04 16:59:10 +09003611 value = d->ma_values[i];
3612 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003613 }
3614 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003615 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003616 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3617 while (i < n && entry_ptr->me_value == NULL) {
3618 entry_ptr++;
3619 i++;
3620 }
3621 if (i >= n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003622 goto fail;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003623 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003624 }
3625 di->di_pos = i+1;
3626 di->len--;
3627 Py_INCREF(value);
3628 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003629
3630fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003631 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003632 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003633 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003634}
3635
3636PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003637 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3638 "dict_valueiterator", /* tp_name */
3639 sizeof(dictiterobject), /* tp_basicsize */
3640 0, /* tp_itemsize */
3641 /* methods */
3642 (destructor)dictiter_dealloc, /* tp_dealloc */
3643 0, /* tp_print */
3644 0, /* tp_getattr */
3645 0, /* tp_setattr */
3646 0, /* tp_reserved */
3647 0, /* tp_repr */
3648 0, /* tp_as_number */
3649 0, /* tp_as_sequence */
3650 0, /* tp_as_mapping */
3651 0, /* tp_hash */
3652 0, /* tp_call */
3653 0, /* tp_str */
3654 PyObject_GenericGetAttr, /* tp_getattro */
3655 0, /* tp_setattro */
3656 0, /* tp_as_buffer */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003657 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003658 0, /* tp_doc */
3659 (traverseproc)dictiter_traverse, /* tp_traverse */
3660 0, /* tp_clear */
3661 0, /* tp_richcompare */
3662 0, /* tp_weaklistoffset */
3663 PyObject_SelfIter, /* tp_iter */
3664 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3665 dictiter_methods, /* tp_methods */
3666 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003667};
3668
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003669static PyObject *
3670dictiter_iternextitem(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003671{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003672 PyObject *key, *value, *result;
INADA Naokica2d8be2016-11-04 16:59:10 +09003673 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003674 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003676 if (d == NULL)
3677 return NULL;
3678 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003680 if (di->di_used != d->ma_used) {
3681 PyErr_SetString(PyExc_RuntimeError,
3682 "dictionary changed size during iteration");
3683 di->di_used = -1; /* Make this state sticky */
3684 return NULL;
3685 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003686
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003687 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003688 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003689 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003690 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003691 goto fail;
3692 key = DK_ENTRIES(d->ma_keys)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003693 value = d->ma_values[i];
3694 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003695 }
3696 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003697 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003698 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3699 while (i < n && entry_ptr->me_value == NULL) {
3700 entry_ptr++;
3701 i++;
3702 }
3703 if (i >= n)
3704 goto fail;
3705 key = entry_ptr->me_key;
3706 value = entry_ptr->me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003707 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003708 di->di_pos = i+1;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003709 di->len--;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003710 Py_INCREF(key);
3711 Py_INCREF(value);
3712 result = di->di_result;
3713 if (Py_REFCNT(result) == 1) {
3714 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3715 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3716 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3717 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003718 Py_INCREF(result);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003719 Py_DECREF(oldkey);
3720 Py_DECREF(oldvalue);
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003721 }
3722 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003723 result = PyTuple_New(2);
3724 if (result == NULL)
3725 return NULL;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003726 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3727 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003728 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003729 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003730
3731fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003732 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003733 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003734 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003735}
3736
3737PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003738 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3739 "dict_itemiterator", /* tp_name */
3740 sizeof(dictiterobject), /* tp_basicsize */
3741 0, /* tp_itemsize */
3742 /* methods */
3743 (destructor)dictiter_dealloc, /* tp_dealloc */
3744 0, /* tp_print */
3745 0, /* tp_getattr */
3746 0, /* tp_setattr */
3747 0, /* tp_reserved */
3748 0, /* tp_repr */
3749 0, /* tp_as_number */
3750 0, /* tp_as_sequence */
3751 0, /* tp_as_mapping */
3752 0, /* tp_hash */
3753 0, /* tp_call */
3754 0, /* tp_str */
3755 PyObject_GenericGetAttr, /* tp_getattro */
3756 0, /* tp_setattro */
3757 0, /* tp_as_buffer */
3758 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3759 0, /* tp_doc */
3760 (traverseproc)dictiter_traverse, /* tp_traverse */
3761 0, /* tp_clear */
3762 0, /* tp_richcompare */
3763 0, /* tp_weaklistoffset */
3764 PyObject_SelfIter, /* tp_iter */
3765 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3766 dictiter_methods, /* tp_methods */
3767 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003768};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003769
3770
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003771/* dictreviter */
3772
3773static PyObject *
3774dictreviter_iternext(dictiterobject *di)
3775{
3776 PyDictObject *d = di->di_dict;
3777
3778 if (d == NULL) {
3779 return NULL;
3780 }
3781 assert (PyDict_Check(d));
3782
3783 if (di->di_used != d->ma_used) {
3784 PyErr_SetString(PyExc_RuntimeError,
3785 "dictionary changed size during iteration");
3786 di->di_used = -1; /* Make this state sticky */
3787 return NULL;
3788 }
3789
3790 Py_ssize_t i = di->di_pos;
3791 PyDictKeysObject *k = d->ma_keys;
3792 PyObject *key, *value, *result;
3793
3794 if (d->ma_values) {
3795 if (i < 0) {
3796 goto fail;
3797 }
3798 key = DK_ENTRIES(k)[i].me_key;
3799 value = d->ma_values[i];
3800 assert (value != NULL);
3801 }
3802 else {
3803 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
3804 while (i >= 0 && entry_ptr->me_value == NULL) {
3805 entry_ptr--;
3806 i--;
3807 }
3808 if (i < 0) {
3809 goto fail;
3810 }
3811 key = entry_ptr->me_key;
3812 value = entry_ptr->me_value;
3813 }
3814 di->di_pos = i-1;
3815 di->len--;
3816
3817 if (Py_TYPE(di) == &PyDictRevIterKey_Type) {
3818 Py_INCREF(key);
3819 return key;
3820 }
3821 else if (Py_TYPE(di) == &PyDictRevIterValue_Type) {
3822 Py_INCREF(value);
3823 return value;
3824 }
3825 else if (Py_TYPE(di) == &PyDictRevIterItem_Type) {
3826 Py_INCREF(key);
3827 Py_INCREF(value);
3828 result = di->di_result;
3829 if (Py_REFCNT(result) == 1) {
3830 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3831 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3832 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3833 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
3834 Py_INCREF(result);
3835 Py_DECREF(oldkey);
3836 Py_DECREF(oldvalue);
3837 }
3838 else {
3839 result = PyTuple_New(2);
3840 if (result == NULL) {
3841 return NULL;
3842 }
3843 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3844 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
3845 }
3846 return result;
3847 }
3848 else {
3849 Py_UNREACHABLE();
3850 }
3851
3852fail:
3853 di->di_dict = NULL;
3854 Py_DECREF(d);
3855 return NULL;
3856}
3857
3858PyTypeObject PyDictRevIterKey_Type = {
3859 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3860 "dict_reversekeyiterator",
3861 sizeof(dictiterobject),
3862 .tp_dealloc = (destructor)dictiter_dealloc,
3863 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
3864 .tp_traverse = (traverseproc)dictiter_traverse,
3865 .tp_iter = PyObject_SelfIter,
3866 .tp_iternext = (iternextfunc)dictreviter_iternext,
3867 .tp_methods = dictiter_methods
3868};
3869
3870
3871/*[clinic input]
3872dict.__reversed__
3873
3874Return a reverse iterator over the dict keys.
3875[clinic start generated code]*/
3876
3877static PyObject *
3878dict___reversed___impl(PyDictObject *self)
3879/*[clinic end generated code: output=e674483336d1ed51 input=23210ef3477d8c4d]*/
3880{
3881 assert (PyDict_Check(self));
3882 return dictiter_new(self, &PyDictRevIterKey_Type);
3883}
3884
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003885static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303886dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003887{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003888 _Py_IDENTIFIER(iter);
Sergey Fedoseev63958442018-10-20 05:43:33 +05003889 /* copy the iterator state */
3890 dictiterobject tmp = *di;
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003891 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003892
Sergey Fedoseev63958442018-10-20 05:43:33 +05003893 PyObject *list = PySequence_List((PyObject*)&tmp);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003894 Py_XDECREF(tmp.di_dict);
Sergey Fedoseev63958442018-10-20 05:43:33 +05003895 if (list == NULL) {
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003896 return NULL;
3897 }
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003898 return Py_BuildValue("N(N)", _PyEval_GetBuiltinId(&PyId_iter), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003899}
3900
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003901PyTypeObject PyDictRevIterItem_Type = {
3902 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3903 "dict_reverseitemiterator",
3904 sizeof(dictiterobject),
3905 .tp_dealloc = (destructor)dictiter_dealloc,
3906 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
3907 .tp_traverse = (traverseproc)dictiter_traverse,
3908 .tp_iter = PyObject_SelfIter,
3909 .tp_iternext = (iternextfunc)dictreviter_iternext,
3910 .tp_methods = dictiter_methods
3911};
3912
3913PyTypeObject PyDictRevIterValue_Type = {
3914 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3915 "dict_reversevalueiterator",
3916 sizeof(dictiterobject),
3917 .tp_dealloc = (destructor)dictiter_dealloc,
3918 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
3919 .tp_traverse = (traverseproc)dictiter_traverse,
3920 .tp_iter = PyObject_SelfIter,
3921 .tp_iternext = (iternextfunc)dictreviter_iternext,
3922 .tp_methods = dictiter_methods
3923};
3924
Guido van Rossum3ac67412007-02-10 18:55:06 +00003925/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003926/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003927/***********************************************/
3928
Guido van Rossumb90c8482007-02-10 01:11:45 +00003929/* The instance lay-out is the same for all three; but the type differs. */
3930
Guido van Rossumb90c8482007-02-10 01:11:45 +00003931static void
Eric Snow96c6af92015-05-29 22:21:39 -06003932dictview_dealloc(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003933{
INADA Naokia6296d32017-08-24 14:55:17 +09003934 /* bpo-31095: UnTrack is needed before calling any callbacks */
3935 _PyObject_GC_UNTRACK(dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003936 Py_XDECREF(dv->dv_dict);
3937 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003938}
3939
3940static int
Eric Snow96c6af92015-05-29 22:21:39 -06003941dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003942{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003943 Py_VISIT(dv->dv_dict);
3944 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003945}
3946
Guido van Rossum83825ac2007-02-10 04:54:19 +00003947static Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003948dictview_len(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003949{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003950 Py_ssize_t len = 0;
3951 if (dv->dv_dict != NULL)
3952 len = dv->dv_dict->ma_used;
3953 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003954}
3955
Eric Snow96c6af92015-05-29 22:21:39 -06003956PyObject *
3957_PyDictView_New(PyObject *dict, PyTypeObject *type)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003958{
Eric Snow96c6af92015-05-29 22:21:39 -06003959 _PyDictViewObject *dv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003960 if (dict == NULL) {
3961 PyErr_BadInternalCall();
3962 return NULL;
3963 }
3964 if (!PyDict_Check(dict)) {
3965 /* XXX Get rid of this restriction later */
3966 PyErr_Format(PyExc_TypeError,
3967 "%s() requires a dict argument, not '%s'",
3968 type->tp_name, dict->ob_type->tp_name);
3969 return NULL;
3970 }
Eric Snow96c6af92015-05-29 22:21:39 -06003971 dv = PyObject_GC_New(_PyDictViewObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003972 if (dv == NULL)
3973 return NULL;
3974 Py_INCREF(dict);
3975 dv->dv_dict = (PyDictObject *)dict;
3976 _PyObject_GC_TRACK(dv);
3977 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003978}
3979
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003980/* TODO(guido): The views objects are not complete:
3981
3982 * support more set operations
3983 * support arbitrary mappings?
3984 - either these should be static or exported in dictobject.h
3985 - if public then they should probably be in builtins
3986*/
3987
Guido van Rossumaac530c2007-08-24 22:33:45 +00003988/* Return 1 if self is a subset of other, iterating over self;
3989 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003990static int
3991all_contained_in(PyObject *self, PyObject *other)
3992{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003993 PyObject *iter = PyObject_GetIter(self);
3994 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003996 if (iter == NULL)
3997 return -1;
3998 for (;;) {
3999 PyObject *next = PyIter_Next(iter);
4000 if (next == NULL) {
4001 if (PyErr_Occurred())
4002 ok = -1;
4003 break;
4004 }
4005 ok = PySequence_Contains(other, next);
4006 Py_DECREF(next);
4007 if (ok <= 0)
4008 break;
4009 }
4010 Py_DECREF(iter);
4011 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00004012}
4013
4014static PyObject *
4015dictview_richcompare(PyObject *self, PyObject *other, int op)
4016{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004017 Py_ssize_t len_self, len_other;
4018 int ok;
4019 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004020
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004021 assert(self != NULL);
4022 assert(PyDictViewSet_Check(self));
4023 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00004024
Brian Curtindfc80e32011-08-10 20:28:54 -05004025 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
4026 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004027
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004028 len_self = PyObject_Size(self);
4029 if (len_self < 0)
4030 return NULL;
4031 len_other = PyObject_Size(other);
4032 if (len_other < 0)
4033 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004035 ok = 0;
4036 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00004037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004038 case Py_NE:
4039 case Py_EQ:
4040 if (len_self == len_other)
4041 ok = all_contained_in(self, other);
4042 if (op == Py_NE && ok >= 0)
4043 ok = !ok;
4044 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004046 case Py_LT:
4047 if (len_self < len_other)
4048 ok = all_contained_in(self, other);
4049 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004050
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004051 case Py_LE:
4052 if (len_self <= len_other)
4053 ok = all_contained_in(self, other);
4054 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004056 case Py_GT:
4057 if (len_self > len_other)
4058 ok = all_contained_in(other, self);
4059 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004061 case Py_GE:
4062 if (len_self >= len_other)
4063 ok = all_contained_in(other, self);
4064 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004066 }
4067 if (ok < 0)
4068 return NULL;
4069 result = ok ? Py_True : Py_False;
4070 Py_INCREF(result);
4071 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00004072}
4073
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004074static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004075dictview_repr(_PyDictViewObject *dv)
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004076{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004077 PyObject *seq;
bennorthd7773d92018-01-26 15:46:01 +00004078 PyObject *result = NULL;
4079 Py_ssize_t rc;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004080
bennorthd7773d92018-01-26 15:46:01 +00004081 rc = Py_ReprEnter((PyObject *)dv);
4082 if (rc != 0) {
4083 return rc > 0 ? PyUnicode_FromString("...") : NULL;
4084 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004085 seq = PySequence_List((PyObject *)dv);
bennorthd7773d92018-01-26 15:46:01 +00004086 if (seq == NULL) {
4087 goto Done;
4088 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004089 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
4090 Py_DECREF(seq);
bennorthd7773d92018-01-26 15:46:01 +00004091
4092Done:
4093 Py_ReprLeave((PyObject *)dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004094 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004095}
4096
Guido van Rossum3ac67412007-02-10 18:55:06 +00004097/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004098
4099static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004100dictkeys_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004101{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004102 if (dv->dv_dict == NULL) {
4103 Py_RETURN_NONE;
4104 }
4105 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004106}
4107
4108static int
Eric Snow96c6af92015-05-29 22:21:39 -06004109dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004110{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004111 if (dv->dv_dict == NULL)
4112 return 0;
4113 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004114}
4115
Guido van Rossum83825ac2007-02-10 04:54:19 +00004116static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004117 (lenfunc)dictview_len, /* sq_length */
4118 0, /* sq_concat */
4119 0, /* sq_repeat */
4120 0, /* sq_item */
4121 0, /* sq_slice */
4122 0, /* sq_ass_item */
4123 0, /* sq_ass_slice */
4124 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004125};
4126
Guido van Rossum523259b2007-08-24 23:41:22 +00004127static PyObject*
4128dictviews_sub(PyObject* self, PyObject *other)
4129{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004130 PyObject *result = PySet_New(self);
4131 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004132 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004133
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004134 if (result == NULL)
4135 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004136
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08004137 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004138 if (tmp == NULL) {
4139 Py_DECREF(result);
4140 return NULL;
4141 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004143 Py_DECREF(tmp);
4144 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004145}
4146
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004147PyObject*
4148_PyDictView_Intersect(PyObject* self, PyObject *other)
Guido van Rossum523259b2007-08-24 23:41:22 +00004149{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004150 PyObject *result = PySet_New(self);
4151 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004152 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004153
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004154 if (result == NULL)
4155 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004156
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08004157 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_intersection_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004158 if (tmp == NULL) {
4159 Py_DECREF(result);
4160 return NULL;
4161 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004162
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004163 Py_DECREF(tmp);
4164 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004165}
4166
4167static PyObject*
4168dictviews_or(PyObject* self, PyObject *other)
4169{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004170 PyObject *result = PySet_New(self);
4171 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004172 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02004173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004174 if (result == NULL)
4175 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004176
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08004177 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004178 if (tmp == NULL) {
4179 Py_DECREF(result);
4180 return NULL;
4181 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004182
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004183 Py_DECREF(tmp);
4184 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004185}
4186
4187static PyObject*
4188dictviews_xor(PyObject* self, PyObject *other)
4189{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004190 PyObject *result = PySet_New(self);
4191 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004192 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004194 if (result == NULL)
4195 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004196
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08004197 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_symmetric_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004198 if (tmp == NULL) {
4199 Py_DECREF(result);
4200 return NULL;
4201 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004202
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004203 Py_DECREF(tmp);
4204 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004205}
4206
4207static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004208 0, /*nb_add*/
4209 (binaryfunc)dictviews_sub, /*nb_subtract*/
4210 0, /*nb_multiply*/
4211 0, /*nb_remainder*/
4212 0, /*nb_divmod*/
4213 0, /*nb_power*/
4214 0, /*nb_negative*/
4215 0, /*nb_positive*/
4216 0, /*nb_absolute*/
4217 0, /*nb_bool*/
4218 0, /*nb_invert*/
4219 0, /*nb_lshift*/
4220 0, /*nb_rshift*/
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004221 (binaryfunc)_PyDictView_Intersect, /*nb_and*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004222 (binaryfunc)dictviews_xor, /*nb_xor*/
4223 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00004224};
4225
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004226static PyObject*
4227dictviews_isdisjoint(PyObject *self, PyObject *other)
4228{
4229 PyObject *it;
4230 PyObject *item = NULL;
4231
4232 if (self == other) {
Eric Snow96c6af92015-05-29 22:21:39 -06004233 if (dictview_len((_PyDictViewObject *)self) == 0)
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004234 Py_RETURN_TRUE;
4235 else
4236 Py_RETURN_FALSE;
4237 }
4238
4239 /* Iterate over the shorter object (only if other is a set,
4240 * because PySequence_Contains may be expensive otherwise): */
4241 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
Eric Snow96c6af92015-05-29 22:21:39 -06004242 Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self);
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004243 Py_ssize_t len_other = PyObject_Size(other);
4244 if (len_other == -1)
4245 return NULL;
4246
4247 if ((len_other > len_self)) {
4248 PyObject *tmp = other;
4249 other = self;
4250 self = tmp;
4251 }
4252 }
4253
4254 it = PyObject_GetIter(other);
4255 if (it == NULL)
4256 return NULL;
4257
4258 while ((item = PyIter_Next(it)) != NULL) {
4259 int contains = PySequence_Contains(self, item);
4260 Py_DECREF(item);
4261 if (contains == -1) {
4262 Py_DECREF(it);
4263 return NULL;
4264 }
4265
4266 if (contains) {
4267 Py_DECREF(it);
4268 Py_RETURN_FALSE;
4269 }
4270 }
4271 Py_DECREF(it);
4272 if (PyErr_Occurred())
4273 return NULL; /* PyIter_Next raised an exception. */
4274 Py_RETURN_TRUE;
4275}
4276
4277PyDoc_STRVAR(isdisjoint_doc,
4278"Return True if the view and the given iterable have a null intersection.");
4279
Serhiy Storchaka81524022018-11-27 13:05:02 +02004280static PyObject* dictkeys_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored));
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004281
4282PyDoc_STRVAR(reversed_keys_doc,
4283"Return a reverse iterator over the dict keys.");
4284
Guido van Rossumb90c8482007-02-10 01:11:45 +00004285static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004286 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4287 isdisjoint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004288 {"__reversed__", (PyCFunction)(void(*)(void))dictkeys_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004289 reversed_keys_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004290 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004291};
4292
4293PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004294 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4295 "dict_keys", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004296 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004297 0, /* tp_itemsize */
4298 /* methods */
4299 (destructor)dictview_dealloc, /* tp_dealloc */
4300 0, /* tp_print */
4301 0, /* tp_getattr */
4302 0, /* tp_setattr */
4303 0, /* tp_reserved */
4304 (reprfunc)dictview_repr, /* tp_repr */
4305 &dictviews_as_number, /* tp_as_number */
4306 &dictkeys_as_sequence, /* tp_as_sequence */
4307 0, /* tp_as_mapping */
4308 0, /* tp_hash */
4309 0, /* tp_call */
4310 0, /* tp_str */
4311 PyObject_GenericGetAttr, /* tp_getattro */
4312 0, /* tp_setattro */
4313 0, /* tp_as_buffer */
4314 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4315 0, /* tp_doc */
4316 (traverseproc)dictview_traverse, /* tp_traverse */
4317 0, /* tp_clear */
4318 dictview_richcompare, /* tp_richcompare */
4319 0, /* tp_weaklistoffset */
4320 (getiterfunc)dictkeys_iter, /* tp_iter */
4321 0, /* tp_iternext */
4322 dictkeys_methods, /* tp_methods */
4323 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004324};
4325
4326static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304327dictkeys_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004328{
Eric Snow96c6af92015-05-29 22:21:39 -06004329 return _PyDictView_New(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004330}
4331
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004332static PyObject *
Serhiy Storchaka81524022018-11-27 13:05:02 +02004333dictkeys_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored))
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004334{
4335 if (dv->dv_dict == NULL) {
4336 Py_RETURN_NONE;
4337 }
4338 return dictiter_new(dv->dv_dict, &PyDictRevIterKey_Type);
4339}
4340
Guido van Rossum3ac67412007-02-10 18:55:06 +00004341/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004342
4343static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004344dictitems_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004345{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004346 if (dv->dv_dict == NULL) {
4347 Py_RETURN_NONE;
4348 }
4349 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004350}
4351
4352static int
Eric Snow96c6af92015-05-29 22:21:39 -06004353dictitems_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004354{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004355 int result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004356 PyObject *key, *value, *found;
4357 if (dv->dv_dict == NULL)
4358 return 0;
4359 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
4360 return 0;
4361 key = PyTuple_GET_ITEM(obj, 0);
4362 value = PyTuple_GET_ITEM(obj, 1);
Raymond Hettinger6692f012016-09-18 21:46:08 -07004363 found = PyDict_GetItemWithError((PyObject *)dv->dv_dict, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004364 if (found == NULL) {
4365 if (PyErr_Occurred())
4366 return -1;
4367 return 0;
4368 }
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004369 Py_INCREF(found);
4370 result = PyObject_RichCompareBool(value, found, Py_EQ);
4371 Py_DECREF(found);
4372 return result;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004373}
4374
Guido van Rossum83825ac2007-02-10 04:54:19 +00004375static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004376 (lenfunc)dictview_len, /* sq_length */
4377 0, /* sq_concat */
4378 0, /* sq_repeat */
4379 0, /* sq_item */
4380 0, /* sq_slice */
4381 0, /* sq_ass_item */
4382 0, /* sq_ass_slice */
4383 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004384};
4385
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004386static PyObject* dictitems_reversed(_PyDictViewObject *dv);
4387
4388PyDoc_STRVAR(reversed_items_doc,
4389"Return a reverse iterator over the dict items.");
4390
Guido van Rossumb90c8482007-02-10 01:11:45 +00004391static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004392 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4393 isdisjoint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004394 {"__reversed__", (PyCFunction)(void(*)(void))dictitems_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004395 reversed_items_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004396 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004397};
4398
4399PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004400 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4401 "dict_items", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004402 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004403 0, /* tp_itemsize */
4404 /* methods */
4405 (destructor)dictview_dealloc, /* tp_dealloc */
4406 0, /* tp_print */
4407 0, /* tp_getattr */
4408 0, /* tp_setattr */
4409 0, /* tp_reserved */
4410 (reprfunc)dictview_repr, /* tp_repr */
4411 &dictviews_as_number, /* tp_as_number */
4412 &dictitems_as_sequence, /* tp_as_sequence */
4413 0, /* tp_as_mapping */
4414 0, /* tp_hash */
4415 0, /* tp_call */
4416 0, /* tp_str */
4417 PyObject_GenericGetAttr, /* tp_getattro */
4418 0, /* tp_setattro */
4419 0, /* tp_as_buffer */
4420 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4421 0, /* tp_doc */
4422 (traverseproc)dictview_traverse, /* tp_traverse */
4423 0, /* tp_clear */
4424 dictview_richcompare, /* tp_richcompare */
4425 0, /* tp_weaklistoffset */
4426 (getiterfunc)dictitems_iter, /* tp_iter */
4427 0, /* tp_iternext */
4428 dictitems_methods, /* tp_methods */
4429 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004430};
4431
4432static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304433dictitems_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004434{
Eric Snow96c6af92015-05-29 22:21:39 -06004435 return _PyDictView_New(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004436}
4437
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004438static PyObject *
4439dictitems_reversed(_PyDictViewObject *dv)
4440{
4441 if (dv->dv_dict == NULL) {
4442 Py_RETURN_NONE;
4443 }
4444 return dictiter_new(dv->dv_dict, &PyDictRevIterItem_Type);
4445}
4446
Guido van Rossum3ac67412007-02-10 18:55:06 +00004447/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004448
4449static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004450dictvalues_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004451{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004452 if (dv->dv_dict == NULL) {
4453 Py_RETURN_NONE;
4454 }
4455 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004456}
4457
Guido van Rossum83825ac2007-02-10 04:54:19 +00004458static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004459 (lenfunc)dictview_len, /* sq_length */
4460 0, /* sq_concat */
4461 0, /* sq_repeat */
4462 0, /* sq_item */
4463 0, /* sq_slice */
4464 0, /* sq_ass_item */
4465 0, /* sq_ass_slice */
4466 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004467};
4468
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004469static PyObject* dictvalues_reversed(_PyDictViewObject *dv);
4470
4471PyDoc_STRVAR(reversed_values_doc,
4472"Return a reverse iterator over the dict values.");
4473
Guido van Rossumb90c8482007-02-10 01:11:45 +00004474static PyMethodDef dictvalues_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004475 {"__reversed__", (PyCFunction)(void(*)(void))dictvalues_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004476 reversed_values_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004477 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004478};
4479
4480PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004481 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4482 "dict_values", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004483 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004484 0, /* tp_itemsize */
4485 /* methods */
4486 (destructor)dictview_dealloc, /* tp_dealloc */
4487 0, /* tp_print */
4488 0, /* tp_getattr */
4489 0, /* tp_setattr */
4490 0, /* tp_reserved */
4491 (reprfunc)dictview_repr, /* tp_repr */
4492 0, /* tp_as_number */
4493 &dictvalues_as_sequence, /* tp_as_sequence */
4494 0, /* tp_as_mapping */
4495 0, /* tp_hash */
4496 0, /* tp_call */
4497 0, /* tp_str */
4498 PyObject_GenericGetAttr, /* tp_getattro */
4499 0, /* tp_setattro */
4500 0, /* tp_as_buffer */
4501 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4502 0, /* tp_doc */
4503 (traverseproc)dictview_traverse, /* tp_traverse */
4504 0, /* tp_clear */
4505 0, /* tp_richcompare */
4506 0, /* tp_weaklistoffset */
4507 (getiterfunc)dictvalues_iter, /* tp_iter */
4508 0, /* tp_iternext */
4509 dictvalues_methods, /* tp_methods */
4510 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004511};
4512
4513static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304514dictvalues_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004515{
Eric Snow96c6af92015-05-29 22:21:39 -06004516 return _PyDictView_New(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004517}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004518
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004519static PyObject *
4520dictvalues_reversed(_PyDictViewObject *dv)
4521{
4522 if (dv->dv_dict == NULL) {
4523 Py_RETURN_NONE;
4524 }
4525 return dictiter_new(dv->dv_dict, &PyDictRevIterValue_Type);
4526}
4527
4528
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004529/* Returns NULL if cannot allocate a new PyDictKeysObject,
4530 but does not set an error */
4531PyDictKeysObject *
4532_PyDict_NewKeysForClass(void)
4533{
Victor Stinner742da042016-09-07 17:40:12 -07004534 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004535 if (keys == NULL)
4536 PyErr_Clear();
4537 else
4538 keys->dk_lookup = lookdict_split;
4539 return keys;
4540}
4541
4542#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
4543
4544PyObject *
4545PyObject_GenericGetDict(PyObject *obj, void *context)
4546{
4547 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
4548 if (dictptr == NULL) {
4549 PyErr_SetString(PyExc_AttributeError,
4550 "This object has no __dict__");
4551 return NULL;
4552 }
4553 dict = *dictptr;
4554 if (dict == NULL) {
4555 PyTypeObject *tp = Py_TYPE(obj);
4556 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
INADA Naokia7576492018-11-14 18:39:27 +09004557 dictkeys_incref(CACHED_KEYS(tp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004558 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
4559 }
4560 else {
4561 *dictptr = dict = PyDict_New();
4562 }
4563 }
4564 Py_XINCREF(dict);
4565 return dict;
4566}
4567
4568int
4569_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
Victor Stinner742da042016-09-07 17:40:12 -07004570 PyObject *key, PyObject *value)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004571{
4572 PyObject *dict;
4573 int res;
4574 PyDictKeysObject *cached;
4575
4576 assert(dictptr != NULL);
4577 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
4578 assert(dictptr != NULL);
4579 dict = *dictptr;
4580 if (dict == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +09004581 dictkeys_incref(cached);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004582 dict = new_dict_with_shared_keys(cached);
4583 if (dict == NULL)
4584 return -1;
4585 *dictptr = dict;
4586 }
4587 if (value == NULL) {
4588 res = PyDict_DelItem(dict, key);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004589 // Since key sharing dict doesn't allow deletion, PyDict_DelItem()
4590 // always converts dict to combined form.
4591 if ((cached = CACHED_KEYS(tp)) != NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004592 CACHED_KEYS(tp) = NULL;
INADA Naokia7576492018-11-14 18:39:27 +09004593 dictkeys_decref(cached);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004594 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01004595 }
4596 else {
INADA Naoki2294f3a2017-02-12 13:51:30 +09004597 int was_shared = (cached == ((PyDictObject *)dict)->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004598 res = PyDict_SetItem(dict, key, value);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004599 if (was_shared &&
4600 (cached = CACHED_KEYS(tp)) != NULL &&
4601 cached != ((PyDictObject *)dict)->ma_keys) {
Victor Stinner3d3f2642016-12-15 17:21:23 +01004602 /* PyDict_SetItem() may call dictresize and convert split table
4603 * into combined table. In such case, convert it to split
4604 * table again and update type's shared key only when this is
4605 * the only dict sharing key with the type.
4606 *
4607 * This is to allow using shared key in class like this:
4608 *
4609 * class C:
4610 * def __init__(self):
4611 * # one dict resize happens
4612 * self.a, self.b, self.c = 1, 2, 3
4613 * self.d, self.e, self.f = 4, 5, 6
4614 * a = C()
4615 */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004616 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004617 CACHED_KEYS(tp) = make_keys_shared(dict);
Victor Stinner742da042016-09-07 17:40:12 -07004618 }
4619 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004620 CACHED_KEYS(tp) = NULL;
4621 }
INADA Naokia7576492018-11-14 18:39:27 +09004622 dictkeys_decref(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004623 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
4624 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004625 }
4626 }
4627 } else {
4628 dict = *dictptr;
4629 if (dict == NULL) {
4630 dict = PyDict_New();
4631 if (dict == NULL)
4632 return -1;
4633 *dictptr = dict;
4634 }
4635 if (value == NULL) {
4636 res = PyDict_DelItem(dict, key);
4637 } else {
4638 res = PyDict_SetItem(dict, key, value);
4639 }
4640 }
4641 return res;
4642}
4643
4644void
4645_PyDictKeys_DecRef(PyDictKeysObject *keys)
4646{
INADA Naokia7576492018-11-14 18:39:27 +09004647 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004648}