blob: 7ea979cd17611612a676271fa6e4b3435e4f958d [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);
Zackery Spytz3d07c1e2019-03-23 20:23:29 -0600607 if (values != empty_values) {
608 free_values(values);
609 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400610 return NULL;
611 }
612 }
613 mp->ma_keys = keys;
614 mp->ma_values = values;
615 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -0700616 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +0200617 assert(_PyDict_CheckConsistency(mp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000619}
620
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400621/* Consumes a reference to the keys object */
622static PyObject *
623new_dict_with_shared_keys(PyDictKeysObject *keys)
624{
625 PyObject **values;
626 Py_ssize_t i, size;
627
Victor Stinner742da042016-09-07 17:40:12 -0700628 size = USABLE_FRACTION(DK_SIZE(keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400629 values = new_values(size);
630 if (values == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +0900631 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400632 return PyErr_NoMemory();
633 }
634 for (i = 0; i < size; i++) {
635 values[i] = NULL;
636 }
637 return new_dict(keys, values);
638}
639
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500640
641static PyObject *
642clone_combined_dict(PyDictObject *orig)
643{
644 assert(PyDict_CheckExact(orig));
645 assert(orig->ma_values == NULL);
646 assert(orig->ma_keys->dk_refcnt == 1);
647
648 Py_ssize_t keys_size = _PyDict_KeysSize(orig->ma_keys);
649 PyDictKeysObject *keys = PyObject_Malloc(keys_size);
650 if (keys == NULL) {
651 PyErr_NoMemory();
652 return NULL;
653 }
654
655 memcpy(keys, orig->ma_keys, keys_size);
656
657 /* After copying key/value pairs, we need to incref all
658 keys and values and they are about to be co-owned by a
659 new dict object. */
660 PyDictKeyEntry *ep0 = DK_ENTRIES(keys);
661 Py_ssize_t n = keys->dk_nentries;
662 for (Py_ssize_t i = 0; i < n; i++) {
663 PyDictKeyEntry *entry = &ep0[i];
664 PyObject *value = entry->me_value;
665 if (value != NULL) {
666 Py_INCREF(value);
667 Py_INCREF(entry->me_key);
668 }
669 }
670
671 PyDictObject *new = (PyDictObject *)new_dict(keys, NULL);
672 if (new == NULL) {
673 /* In case of an error, `new_dict()` takes care of
674 cleaning up `keys`. */
675 return NULL;
676 }
677 new->ma_used = orig->ma_used;
678 assert(_PyDict_CheckConsistency(new));
679 if (_PyObject_GC_IS_TRACKED(orig)) {
680 /* Maintain tracking. */
681 _PyObject_GC_TRACK(new);
682 }
Yury Selivanov0b752282018-07-06 12:20:07 -0400683
684 /* Since we copied the keys table we now have an extra reference
685 in the system. Manually call _Py_INC_REFTOTAL to signal that
INADA Naokia7576492018-11-14 18:39:27 +0900686 we have it now; calling dictkeys_incref would be an error as
Yury Selivanov0b752282018-07-06 12:20:07 -0400687 keys->dk_refcnt is already set to 1 (after memcpy). */
688 _Py_INC_REFTOTAL;
689
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500690 return (PyObject *)new;
691}
692
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400693PyObject *
694PyDict_New(void)
695{
Inada Naokif2a18672019-03-12 17:25:44 +0900696 dictkeys_incref(Py_EMPTY_KEYS);
697 return new_dict(Py_EMPTY_KEYS, empty_values);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400698}
699
Victor Stinner742da042016-09-07 17:40:12 -0700700/* Search index of hash table from offset of entry table */
701static Py_ssize_t
702lookdict_index(PyDictKeysObject *k, Py_hash_t hash, Py_ssize_t index)
703{
Victor Stinner742da042016-09-07 17:40:12 -0700704 size_t mask = DK_MASK(k);
INADA Naoki073ae482017-06-23 15:22:50 +0900705 size_t perturb = (size_t)hash;
706 size_t i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700707
INADA Naoki073ae482017-06-23 15:22:50 +0900708 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900709 Py_ssize_t ix = dictkeys_get_index(k, i);
Victor Stinner742da042016-09-07 17:40:12 -0700710 if (ix == index) {
711 return i;
712 }
713 if (ix == DKIX_EMPTY) {
714 return DKIX_EMPTY;
715 }
INADA Naoki073ae482017-06-23 15:22:50 +0900716 perturb >>= PERTURB_SHIFT;
717 i = mask & (i*5 + perturb + 1);
Victor Stinner742da042016-09-07 17:40:12 -0700718 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700719 Py_UNREACHABLE();
Victor Stinner742da042016-09-07 17:40:12 -0700720}
721
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000722/*
723The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000724This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000725Open addressing is preferred over chaining since the link overhead for
726chaining would be substantial (100% with typical malloc overhead).
727
Tim Peterseb28ef22001-06-02 05:27:19 +0000728The initial probe index is computed as hash mod the table size. Subsequent
729probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000730
731All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000732
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000733The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000734contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000735Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000736
Victor Stinner742da042016-09-07 17:40:12 -0700737lookdict() is general-purpose, and may return DKIX_ERROR if (and only if) a
Victor Stinnera4348cc2016-09-08 12:01:25 -0700738comparison raises an exception.
Guido van Rossum89d8c602007-09-18 17:26:56 +0000739lookdict_unicode() below is specialized to string keys, comparison of which can
INADA Naoki1b8df102017-02-20 22:48:10 +0900740never raise an exception; that function can never return DKIX_ERROR when key
741is string. Otherwise, it falls back to lookdict().
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400742lookdict_unicode_nodummy is further specialized for string keys that cannot be
743the <dummy> value.
INADA Naoki778928b2017-08-03 23:45:15 +0900744For both, when the key isn't found a DKIX_EMPTY is returned.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000745*/
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100746static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400747lookdict(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900748 Py_hash_t hash, PyObject **value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000749{
INADA Naoki778928b2017-08-03 23:45:15 +0900750 size_t i, mask, perturb;
Victor Stinner742da042016-09-07 17:40:12 -0700751 PyDictKeysObject *dk;
INADA Naoki778928b2017-08-03 23:45:15 +0900752 PyDictKeyEntry *ep0;
Tim Peterseb28ef22001-06-02 05:27:19 +0000753
Antoine Pitrou9a234902012-05-13 20:48:01 +0200754top:
Victor Stinner742da042016-09-07 17:40:12 -0700755 dk = mp->ma_keys;
Victor Stinner742da042016-09-07 17:40:12 -0700756 ep0 = DK_ENTRIES(dk);
INADA Naoki778928b2017-08-03 23:45:15 +0900757 mask = DK_MASK(dk);
758 perturb = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700760
INADA Naoki778928b2017-08-03 23:45:15 +0900761 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900762 Py_ssize_t ix = dictkeys_get_index(dk, i);
Victor Stinner742da042016-09-07 17:40:12 -0700763 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700764 *value_addr = NULL;
765 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400766 }
INADA Naoki778928b2017-08-03 23:45:15 +0900767 if (ix >= 0) {
768 PyDictKeyEntry *ep = &ep0[ix];
769 assert(ep->me_key != NULL);
770 if (ep->me_key == key) {
771 *value_addr = ep->me_value;
772 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700773 }
INADA Naoki778928b2017-08-03 23:45:15 +0900774 if (ep->me_hash == hash) {
775 PyObject *startkey = ep->me_key;
776 Py_INCREF(startkey);
777 int cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
778 Py_DECREF(startkey);
779 if (cmp < 0) {
780 *value_addr = NULL;
781 return DKIX_ERROR;
782 }
783 if (dk == mp->ma_keys && ep->me_key == startkey) {
784 if (cmp > 0) {
785 *value_addr = ep->me_value;
786 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700787 }
INADA Naoki778928b2017-08-03 23:45:15 +0900788 }
789 else {
790 /* The dict was mutated, restart */
791 goto top;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400792 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 }
INADA Naoki778928b2017-08-03 23:45:15 +0900795 perturb >>= PERTURB_SHIFT;
796 i = (i*5 + perturb + 1) & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700798 Py_UNREACHABLE();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000799}
800
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400801/* Specialized version for string-only keys */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100802static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400803lookdict_unicode(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900804 Py_hash_t hash, PyObject **value_addr)
Fred Drake1bff34a2000-08-31 19:31:38 +0000805{
Victor Stinner742da042016-09-07 17:40:12 -0700806 assert(mp->ma_values == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 /* Make sure this function doesn't have to handle non-unicode keys,
808 including subclasses of str; e.g., one reason to subclass
809 unicodes is to override __eq__, and for speed we don't cater to
810 that here. */
811 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400812 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900813 return lookdict(mp, key, hash, value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 }
Tim Peters15d49292001-05-27 07:39:22 +0000815
INADA Naoki778928b2017-08-03 23:45:15 +0900816 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
817 size_t mask = DK_MASK(mp->ma_keys);
818 size_t perturb = (size_t)hash;
819 size_t i = (size_t)hash & mask;
820
821 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900822 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
Victor Stinner742da042016-09-07 17:40:12 -0700823 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700824 *value_addr = NULL;
825 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400826 }
INADA Naoki778928b2017-08-03 23:45:15 +0900827 if (ix >= 0) {
828 PyDictKeyEntry *ep = &ep0[ix];
829 assert(ep->me_key != NULL);
830 assert(PyUnicode_CheckExact(ep->me_key));
831 if (ep->me_key == key ||
832 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
833 *value_addr = ep->me_value;
834 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700835 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400836 }
INADA Naoki778928b2017-08-03 23:45:15 +0900837 perturb >>= PERTURB_SHIFT;
838 i = mask & (i*5 + perturb + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700840 Py_UNREACHABLE();
Fred Drake1bff34a2000-08-31 19:31:38 +0000841}
842
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400843/* Faster version of lookdict_unicode when it is known that no <dummy> keys
844 * will be present. */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100845static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400846lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900847 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400848{
Victor Stinner742da042016-09-07 17:40:12 -0700849 assert(mp->ma_values == NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400850 /* Make sure this function doesn't have to handle non-unicode keys,
851 including subclasses of str; e.g., one reason to subclass
852 unicodes is to override __eq__, and for speed we don't cater to
853 that here. */
854 if (!PyUnicode_CheckExact(key)) {
855 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900856 return lookdict(mp, key, hash, value_addr);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400857 }
INADA Naoki778928b2017-08-03 23:45:15 +0900858
859 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
860 size_t mask = DK_MASK(mp->ma_keys);
861 size_t perturb = (size_t)hash;
862 size_t i = (size_t)hash & mask;
863
864 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900865 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
Victor Stinner742da042016-09-07 17:40:12 -0700866 assert (ix != DKIX_DUMMY);
867 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700868 *value_addr = NULL;
869 return DKIX_EMPTY;
870 }
INADA Naoki778928b2017-08-03 23:45:15 +0900871 PyDictKeyEntry *ep = &ep0[ix];
872 assert(ep->me_key != NULL);
873 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700874 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400875 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900876 *value_addr = ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700877 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400878 }
INADA Naoki778928b2017-08-03 23:45:15 +0900879 perturb >>= PERTURB_SHIFT;
880 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400881 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700882 Py_UNREACHABLE();
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400883}
884
885/* Version of lookdict for split tables.
886 * All split tables and only split tables use this lookup function.
887 * Split tables only contain unicode keys and no dummy keys,
888 * so algorithm is the same as lookdict_unicode_nodummy.
889 */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100890static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400891lookdict_split(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900892 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400893{
Victor Stinner742da042016-09-07 17:40:12 -0700894 /* mp must split table */
895 assert(mp->ma_values != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400896 if (!PyUnicode_CheckExact(key)) {
INADA Naoki778928b2017-08-03 23:45:15 +0900897 Py_ssize_t ix = lookdict(mp, key, hash, value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700898 if (ix >= 0) {
INADA Naokiba609772016-12-07 20:41:42 +0900899 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700900 }
901 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400902 }
Victor Stinner742da042016-09-07 17:40:12 -0700903
INADA Naoki778928b2017-08-03 23:45:15 +0900904 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
905 size_t mask = DK_MASK(mp->ma_keys);
906 size_t perturb = (size_t)hash;
907 size_t i = (size_t)hash & mask;
908
909 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900910 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
INADA Naoki778928b2017-08-03 23:45:15 +0900911 assert (ix != DKIX_DUMMY);
Victor Stinner742da042016-09-07 17:40:12 -0700912 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700913 *value_addr = NULL;
914 return DKIX_EMPTY;
915 }
INADA Naoki778928b2017-08-03 23:45:15 +0900916 PyDictKeyEntry *ep = &ep0[ix];
917 assert(ep->me_key != NULL);
918 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700919 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400920 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900921 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700922 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400923 }
INADA Naoki778928b2017-08-03 23:45:15 +0900924 perturb >>= PERTURB_SHIFT;
925 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400926 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700927 Py_UNREACHABLE();
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400928}
929
Benjamin Petersonfb886362010-04-24 18:21:17 +0000930int
931_PyDict_HasOnlyStringKeys(PyObject *dict)
932{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 Py_ssize_t pos = 0;
934 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000935 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400937 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 return 1;
939 while (PyDict_Next(dict, &pos, &key, &value))
940 if (!PyUnicode_Check(key))
941 return 0;
942 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000943}
944
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000945#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 do { \
947 if (!_PyObject_GC_IS_TRACKED(mp)) { \
948 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
949 _PyObject_GC_MAY_BE_TRACKED(value)) { \
950 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 } \
952 } \
953 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000954
955void
956_PyDict_MaybeUntrack(PyObject *op)
957{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 PyDictObject *mp;
959 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -0700960 Py_ssize_t i, numentries;
961 PyDictKeyEntry *ep0;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
964 return;
965
966 mp = (PyDictObject *) op;
Victor Stinner742da042016-09-07 17:40:12 -0700967 ep0 = DK_ENTRIES(mp->ma_keys);
968 numentries = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400969 if (_PyDict_HasSplitTable(mp)) {
Victor Stinner742da042016-09-07 17:40:12 -0700970 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400971 if ((value = mp->ma_values[i]) == NULL)
972 continue;
973 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
Victor Stinner742da042016-09-07 17:40:12 -0700974 assert(!_PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400975 return;
976 }
977 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400979 else {
Victor Stinner742da042016-09-07 17:40:12 -0700980 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400981 if ((value = ep0[i].me_value) == NULL)
982 continue;
983 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
984 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
985 return;
986 }
987 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000989}
990
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400991/* Internal function to find slot for an item from its hash
Victor Stinner3c336c52016-09-12 14:17:40 +0200992 when it is known that the key is not present in the dict.
993
994 The dict must be combined. */
INADA Naokiba609772016-12-07 20:41:42 +0900995static Py_ssize_t
INADA Naoki778928b2017-08-03 23:45:15 +0900996find_empty_slot(PyDictKeysObject *keys, Py_hash_t hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000997{
INADA Naoki778928b2017-08-03 23:45:15 +0900998 assert(keys != NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000999
INADA Naoki778928b2017-08-03 23:45:15 +09001000 const size_t mask = DK_MASK(keys);
1001 size_t i = hash & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001002 Py_ssize_t ix = dictkeys_get_index(keys, i);
INADA Naoki778928b2017-08-03 23:45:15 +09001003 for (size_t perturb = hash; ix >= 0;) {
INADA Naoki267941c2016-10-06 15:19:07 +09001004 perturb >>= PERTURB_SHIFT;
INADA Naoki778928b2017-08-03 23:45:15 +09001005 i = (i*5 + perturb + 1) & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001006 ix = dictkeys_get_index(keys, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 }
INADA Naoki778928b2017-08-03 23:45:15 +09001008 return i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001009}
1010
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001011static int
1012insertion_resize(PyDictObject *mp)
1013{
Raymond Hettinger36f74aa2013-05-17 03:01:13 -07001014 return dictresize(mp, GROWTH_RATE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001015}
Antoine Pitroue965d972012-02-27 00:45:12 +01001016
1017/*
1018Internal routine to insert a new item into the table.
1019Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +01001020Returns -1 if an error occurred, or 0 on success.
1021*/
1022static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001023insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001024{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001025 PyObject *old_value;
INADA Naokiba609772016-12-07 20:41:42 +09001026 PyDictKeyEntry *ep;
Antoine Pitroue965d972012-02-27 00:45:12 +01001027
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001028 Py_INCREF(key);
1029 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001030 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
1031 if (insertion_resize(mp) < 0)
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001032 goto Fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001033 }
1034
INADA Naoki778928b2017-08-03 23:45:15 +09001035 Py_ssize_t ix = mp->ma_keys->dk_lookup(mp, key, hash, &old_value);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001036 if (ix == DKIX_ERROR)
1037 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001038
Antoine Pitroud6967322014-10-18 00:35:00 +02001039 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001040 MAINTAIN_TRACKING(mp, key, value);
Victor Stinner742da042016-09-07 17:40:12 -07001041
1042 /* When insertion order is different from shared key, we can't share
1043 * the key anymore. Convert this instance to combine table.
1044 */
1045 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09001046 ((ix >= 0 && old_value == NULL && mp->ma_used != ix) ||
Victor Stinner742da042016-09-07 17:40:12 -07001047 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001048 if (insertion_resize(mp) < 0)
1049 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001050 ix = DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001051 }
Victor Stinner742da042016-09-07 17:40:12 -07001052
1053 if (ix == DKIX_EMPTY) {
1054 /* Insert into new slot. */
INADA Naokiba609772016-12-07 20:41:42 +09001055 assert(old_value == NULL);
Victor Stinner742da042016-09-07 17:40:12 -07001056 if (mp->ma_keys->dk_usable <= 0) {
1057 /* Need to resize. */
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001058 if (insertion_resize(mp) < 0)
1059 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001060 }
INADA Naoki778928b2017-08-03 23:45:15 +09001061 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naokiba609772016-12-07 20:41:42 +09001062 ep = &DK_ENTRIES(mp->ma_keys)[mp->ma_keys->dk_nentries];
INADA Naokia7576492018-11-14 18:39:27 +09001063 dictkeys_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Victor Stinner742da042016-09-07 17:40:12 -07001064 ep->me_key = key;
1065 ep->me_hash = hash;
1066 if (mp->ma_values) {
1067 assert (mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
1068 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001069 }
1070 else {
Victor Stinner742da042016-09-07 17:40:12 -07001071 ep->me_value = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001072 }
1073 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001074 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07001075 mp->ma_keys->dk_usable--;
1076 mp->ma_keys->dk_nentries++;
1077 assert(mp->ma_keys->dk_usable >= 0);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001078 assert(_PyDict_CheckConsistency(mp));
Victor Stinner742da042016-09-07 17:40:12 -07001079 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001080 }
Victor Stinner742da042016-09-07 17:40:12 -07001081
INADA Naokiba609772016-12-07 20:41:42 +09001082 if (_PyDict_HasSplitTable(mp)) {
1083 mp->ma_values[ix] = value;
1084 if (old_value == NULL) {
1085 /* pending state */
1086 assert(ix == mp->ma_used);
1087 mp->ma_used++;
1088 }
1089 }
1090 else {
1091 assert(old_value != NULL);
1092 DK_ENTRIES(mp->ma_keys)[ix].me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07001093 }
1094
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001095 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokiba609772016-12-07 20:41:42 +09001096 Py_XDECREF(old_value); /* which **CAN** re-enter (see issue #22653) */
Victor Stinner611b0fa2016-09-14 15:02:01 +02001097 assert(_PyDict_CheckConsistency(mp));
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001098 Py_DECREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001099 return 0;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001100
1101Fail:
1102 Py_DECREF(value);
1103 Py_DECREF(key);
1104 return -1;
Antoine Pitroue965d972012-02-27 00:45:12 +01001105}
1106
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001107// Same to insertdict but specialized for ma_keys = Py_EMPTY_KEYS.
1108static int
1109insert_to_emptydict(PyDictObject *mp, PyObject *key, Py_hash_t hash,
1110 PyObject *value)
1111{
1112 assert(mp->ma_keys == Py_EMPTY_KEYS);
1113
1114 PyDictKeysObject *newkeys = new_keys_object(PyDict_MINSIZE);
1115 if (newkeys == NULL) {
1116 return -1;
1117 }
1118 if (!PyUnicode_CheckExact(key)) {
1119 newkeys->dk_lookup = lookdict;
1120 }
1121 dictkeys_decref(Py_EMPTY_KEYS);
1122 mp->ma_keys = newkeys;
1123 mp->ma_values = NULL;
1124
1125 Py_INCREF(key);
1126 Py_INCREF(value);
1127 MAINTAIN_TRACKING(mp, key, value);
1128
1129 size_t hashpos = (size_t)hash & (PyDict_MINSIZE-1);
1130 PyDictKeyEntry *ep = &DK_ENTRIES(mp->ma_keys)[0];
1131 dictkeys_set_index(mp->ma_keys, hashpos, 0);
1132 ep->me_key = key;
1133 ep->me_hash = hash;
1134 ep->me_value = value;
1135 mp->ma_used++;
1136 mp->ma_version_tag = DICT_NEXT_VERSION();
1137 mp->ma_keys->dk_usable--;
1138 mp->ma_keys->dk_nentries++;
1139 return 0;
1140}
1141
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001142/*
luzpaza5293b42017-11-05 07:37:50 -06001143Internal routine used by dictresize() to build a hashtable of entries.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001144*/
1145static void
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001146build_indices(PyDictKeysObject *keys, PyDictKeyEntry *ep, Py_ssize_t n)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001147{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001148 size_t mask = (size_t)DK_SIZE(keys) - 1;
1149 for (Py_ssize_t ix = 0; ix != n; ix++, ep++) {
1150 Py_hash_t hash = ep->me_hash;
1151 size_t i = hash & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001152 for (size_t perturb = hash; dictkeys_get_index(keys, i) != DKIX_EMPTY;) {
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001153 perturb >>= PERTURB_SHIFT;
INADA Naoki870c2862017-06-24 09:03:19 +09001154 i = mask & (i*5 + perturb + 1);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001155 }
INADA Naokia7576492018-11-14 18:39:27 +09001156 dictkeys_set_index(keys, i, ix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001158}
1159
1160/*
1161Restructure the table by allocating a new table and reinserting all
1162items again. When entries have been deleted, the new table may
1163actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001164If a table is split (its keys and hashes are shared, its values are not),
1165then the values are temporarily copied into the table, it is resized as
1166a combined table, then the me_value slots in the old table are NULLed out.
1167After resizing a table is always combined,
1168but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001169*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001170static int
Victor Stinner3d3f2642016-12-15 17:21:23 +01001171dictresize(PyDictObject *mp, Py_ssize_t minsize)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001172{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001173 Py_ssize_t newsize, numentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001174 PyDictKeysObject *oldkeys;
1175 PyObject **oldvalues;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001176 PyDictKeyEntry *oldentries, *newentries;
Tim Peters91a364d2001-05-19 07:04:38 +00001177
Victor Stinner742da042016-09-07 17:40:12 -07001178 /* Find the smallest table size > minused. */
1179 for (newsize = PyDict_MINSIZE;
Victor Stinner3d3f2642016-12-15 17:21:23 +01001180 newsize < minsize && newsize > 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 newsize <<= 1)
1182 ;
1183 if (newsize <= 0) {
1184 PyErr_NoMemory();
1185 return -1;
1186 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001187
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001188 oldkeys = mp->ma_keys;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001189
1190 /* NOTE: Current odict checks mp->ma_keys to detect resize happen.
1191 * So we can't reuse oldkeys even if oldkeys->dk_size == newsize.
1192 * TODO: Try reusing oldkeys when reimplement odict.
1193 */
1194
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001195 /* Allocate a new table. */
1196 mp->ma_keys = new_keys_object(newsize);
1197 if (mp->ma_keys == NULL) {
1198 mp->ma_keys = oldkeys;
1199 return -1;
1200 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01001201 // New table must be large enough.
1202 assert(mp->ma_keys->dk_usable >= mp->ma_used);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001203 if (oldkeys->dk_lookup == lookdict)
1204 mp->ma_keys->dk_lookup = lookdict;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001205
1206 numentries = mp->ma_used;
1207 oldentries = DK_ENTRIES(oldkeys);
1208 newentries = DK_ENTRIES(mp->ma_keys);
1209 oldvalues = mp->ma_values;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001210 if (oldvalues != NULL) {
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001211 /* Convert split table into new combined table.
1212 * We must incref keys; we can transfer values.
1213 * Note that values of split table is always dense.
1214 */
1215 for (Py_ssize_t i = 0; i < numentries; i++) {
1216 assert(oldvalues[i] != NULL);
1217 PyDictKeyEntry *ep = &oldentries[i];
1218 PyObject *key = ep->me_key;
1219 Py_INCREF(key);
1220 newentries[i].me_key = key;
1221 newentries[i].me_hash = ep->me_hash;
1222 newentries[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001224
INADA Naokia7576492018-11-14 18:39:27 +09001225 dictkeys_decref(oldkeys);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001226 mp->ma_values = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001227 if (oldvalues != empty_values) {
1228 free_values(oldvalues);
1229 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001230 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001231 else { // combined table.
1232 if (oldkeys->dk_nentries == numentries) {
1233 memcpy(newentries, oldentries, numentries * sizeof(PyDictKeyEntry));
1234 }
1235 else {
1236 PyDictKeyEntry *ep = oldentries;
1237 for (Py_ssize_t i = 0; i < numentries; i++) {
1238 while (ep->me_value == NULL)
1239 ep++;
1240 newentries[i] = *ep++;
1241 }
1242 }
1243
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001244 assert(oldkeys->dk_lookup != lookdict_split);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001245 assert(oldkeys->dk_refcnt == 1);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001246 if (oldkeys->dk_size == PyDict_MINSIZE &&
1247 numfreekeys < PyDict_MAXFREELIST) {
INADA Naokia7576492018-11-14 18:39:27 +09001248 _Py_DEC_REFTOTAL;
1249 keys_free_list[numfreekeys++] = oldkeys;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001250 }
1251 else {
INADA Naokia7576492018-11-14 18:39:27 +09001252 _Py_DEC_REFTOTAL;
1253 PyObject_FREE(oldkeys);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001254 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001255 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001256
1257 build_indices(mp->ma_keys, newentries, numentries);
1258 mp->ma_keys->dk_usable -= numentries;
1259 mp->ma_keys->dk_nentries = numentries;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001261}
1262
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001263/* Returns NULL if unable to split table.
1264 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001265static PyDictKeysObject *
1266make_keys_shared(PyObject *op)
1267{
1268 Py_ssize_t i;
1269 Py_ssize_t size;
1270 PyDictObject *mp = (PyDictObject *)op;
1271
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001272 if (!PyDict_CheckExact(op))
1273 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001274 if (!_PyDict_HasSplitTable(mp)) {
1275 PyDictKeyEntry *ep0;
1276 PyObject **values;
1277 assert(mp->ma_keys->dk_refcnt == 1);
1278 if (mp->ma_keys->dk_lookup == lookdict) {
1279 return NULL;
1280 }
1281 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
1282 /* Remove dummy keys */
1283 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
1284 return NULL;
1285 }
1286 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
1287 /* Copy values into a new array */
Victor Stinner742da042016-09-07 17:40:12 -07001288 ep0 = DK_ENTRIES(mp->ma_keys);
1289 size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001290 values = new_values(size);
1291 if (values == NULL) {
1292 PyErr_SetString(PyExc_MemoryError,
1293 "Not enough memory to allocate new values array");
1294 return NULL;
1295 }
1296 for (i = 0; i < size; i++) {
1297 values[i] = ep0[i].me_value;
1298 ep0[i].me_value = NULL;
1299 }
1300 mp->ma_keys->dk_lookup = lookdict_split;
1301 mp->ma_values = values;
1302 }
INADA Naokia7576492018-11-14 18:39:27 +09001303 dictkeys_incref(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001304 return mp->ma_keys;
1305}
Christian Heimes99170a52007-12-19 02:07:34 +00001306
1307PyObject *
1308_PyDict_NewPresized(Py_ssize_t minused)
1309{
INADA Naoki92c50ee2016-11-22 00:57:02 +09001310 const Py_ssize_t max_presize = 128 * 1024;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001311 Py_ssize_t newsize;
1312 PyDictKeysObject *new_keys;
INADA Naoki92c50ee2016-11-22 00:57:02 +09001313
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001314 if (minused <= USABLE_FRACTION(PyDict_MINSIZE)) {
Inada Naokif2a18672019-03-12 17:25:44 +09001315 return PyDict_New();
1316 }
INADA Naoki92c50ee2016-11-22 00:57:02 +09001317 /* There are no strict guarantee that returned dict can contain minused
1318 * items without resize. So we create medium size dict instead of very
1319 * large dict or MemoryError.
1320 */
1321 if (minused > USABLE_FRACTION(max_presize)) {
1322 newsize = max_presize;
1323 }
1324 else {
1325 Py_ssize_t minsize = ESTIMATE_SIZE(minused);
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001326 newsize = PyDict_MINSIZE*2;
INADA Naoki92c50ee2016-11-22 00:57:02 +09001327 while (newsize < minsize) {
1328 newsize <<= 1;
1329 }
1330 }
1331 assert(IS_POWER_OF_2(newsize));
1332
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001333 new_keys = new_keys_object(newsize);
1334 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001336 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001337}
1338
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001339/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1340 * that may occur (originally dicts supported only string keys, and exceptions
1341 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001342 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001343 * (suppressed) occurred while computing the key's hash, or that some error
1344 * (suppressed) occurred when comparing keys in the dict's internal probe
1345 * sequence. A nasty example of the latter is when a Python-coded comparison
1346 * function hits a stack-depth error, which can cause this to return NULL
1347 * even if the key is present.
1348 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001349PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001350PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001351{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001352 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07001353 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 PyDictObject *mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 PyThreadState *tstate;
INADA Naokiba609772016-12-07 20:41:42 +09001356 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001358 if (!PyDict_Check(op))
1359 return NULL;
1360 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001361 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 {
1363 hash = PyObject_Hash(key);
1364 if (hash == -1) {
1365 PyErr_Clear();
1366 return NULL;
1367 }
1368 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 /* We can arrive here with a NULL tstate during initialization: try
1371 running "python -Wi" for an example related to string interning.
1372 Let's just hope that no exception occurs then... This must be
Victor Stinner50b48572018-11-01 01:51:40 +01001373 _PyThreadState_GET() and not PyThreadState_Get() because the latter
Victor Stinner9204fb82018-10-30 15:13:17 +01001374 abort Python if tstate is NULL. */
Victor Stinner50b48572018-11-01 01:51:40 +01001375 tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 if (tstate != NULL && tstate->curexc_type != NULL) {
1377 /* preserve the existing exception */
1378 PyObject *err_type, *err_value, *err_tb;
1379 PyErr_Fetch(&err_type, &err_value, &err_tb);
INADA Naoki778928b2017-08-03 23:45:15 +09001380 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 /* ignore errors */
1382 PyErr_Restore(err_type, err_value, err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001383 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 return NULL;
1385 }
1386 else {
INADA Naoki778928b2017-08-03 23:45:15 +09001387 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001388 if (ix < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 PyErr_Clear();
1390 return NULL;
1391 }
1392 }
INADA Naokiba609772016-12-07 20:41:42 +09001393 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001394}
1395
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001396/* Same as PyDict_GetItemWithError() but with hash supplied by caller.
1397 This returns NULL *with* an exception set if an exception occurred.
1398 It returns NULL *without* an exception set if the key wasn't present.
1399*/
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001400PyObject *
1401_PyDict_GetItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1402{
Victor Stinner742da042016-09-07 17:40:12 -07001403 Py_ssize_t ix;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001404 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001405 PyObject *value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001406
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001407 if (!PyDict_Check(op)) {
1408 PyErr_BadInternalCall();
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001409 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001410 }
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001411
INADA Naoki778928b2017-08-03 23:45:15 +09001412 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001413 if (ix < 0) {
1414 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001415 }
INADA Naokiba609772016-12-07 20:41:42 +09001416 return value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001417}
1418
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001419/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1420 This returns NULL *with* an exception set if an exception occurred.
1421 It returns NULL *without* an exception set if the key wasn't present.
1422*/
1423PyObject *
1424PyDict_GetItemWithError(PyObject *op, PyObject *key)
1425{
Victor Stinner742da042016-09-07 17:40:12 -07001426 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001427 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 PyDictObject*mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001429 PyObject *value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001431 if (!PyDict_Check(op)) {
1432 PyErr_BadInternalCall();
1433 return NULL;
1434 }
1435 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001436 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 {
1438 hash = PyObject_Hash(key);
1439 if (hash == -1) {
1440 return NULL;
1441 }
1442 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001443
INADA Naoki778928b2017-08-03 23:45:15 +09001444 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001445 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001447 return value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001448}
1449
Brett Cannonfd074152012-04-14 14:10:13 -04001450PyObject *
1451_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1452{
1453 PyObject *kv;
1454 kv = _PyUnicode_FromId(key); /* borrowed */
1455 if (kv == NULL)
1456 return NULL;
1457 return PyDict_GetItemWithError(dp, kv);
1458}
1459
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001460PyObject *
1461_PyDict_GetItemStringWithError(PyObject *v, const char *key)
1462{
1463 PyObject *kv, *rv;
1464 kv = PyUnicode_FromString(key);
1465 if (kv == NULL) {
1466 return NULL;
1467 }
1468 rv = PyDict_GetItemWithError(v, kv);
1469 Py_DECREF(kv);
1470 return rv;
1471}
1472
Victor Stinnerb4efc962015-11-20 09:24:02 +01001473/* Fast version of global value lookup (LOAD_GLOBAL).
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001474 * Lookup in globals, then builtins.
Victor Stinnerb4efc962015-11-20 09:24:02 +01001475 *
1476 * Raise an exception and return NULL if an error occurred (ex: computing the
1477 * key hash failed, key comparison failed, ...). Return NULL if the key doesn't
1478 * exist. Return the value if the key exists.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001479 */
1480PyObject *
1481_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001482{
Victor Stinner742da042016-09-07 17:40:12 -07001483 Py_ssize_t ix;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001484 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09001485 PyObject *value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001486
1487 if (!PyUnicode_CheckExact(key) ||
1488 (hash = ((PyASCIIObject *) key)->hash) == -1)
1489 {
1490 hash = PyObject_Hash(key);
1491 if (hash == -1)
1492 return NULL;
Antoine Pitroue965d972012-02-27 00:45:12 +01001493 }
Victor Stinnerb4efc962015-11-20 09:24:02 +01001494
1495 /* namespace 1: globals */
INADA Naoki778928b2017-08-03 23:45:15 +09001496 ix = globals->ma_keys->dk_lookup(globals, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001497 if (ix == DKIX_ERROR)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001498 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001499 if (ix != DKIX_EMPTY && value != NULL)
1500 return value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001501
1502 /* namespace 2: builtins */
INADA Naoki778928b2017-08-03 23:45:15 +09001503 ix = builtins->ma_keys->dk_lookup(builtins, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001504 if (ix < 0)
Victor Stinnerb4efc962015-11-20 09:24:02 +01001505 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001506 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001507}
1508
Antoine Pitroue965d972012-02-27 00:45:12 +01001509/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1510 * dictionary if it's merely replacing the value for an existing key.
1511 * This means that it's safe to loop over a dictionary with PyDict_Next()
1512 * and occasionally replace a value -- but you can't insert new keys or
1513 * remove them.
1514 */
1515int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001516PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001517{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001518 PyDictObject *mp;
1519 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001520 if (!PyDict_Check(op)) {
1521 PyErr_BadInternalCall();
1522 return -1;
1523 }
1524 assert(key);
1525 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001526 mp = (PyDictObject *)op;
1527 if (!PyUnicode_CheckExact(key) ||
1528 (hash = ((PyASCIIObject *) key)->hash) == -1)
1529 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001530 hash = PyObject_Hash(key);
1531 if (hash == -1)
1532 return -1;
1533 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001534
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001535 if (mp->ma_keys == Py_EMPTY_KEYS) {
1536 return insert_to_emptydict(mp, key, hash, value);
1537 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001538 /* insertdict() handles any resizing that might be necessary */
1539 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001540}
1541
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001542int
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001543_PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
1544 Py_hash_t hash)
1545{
1546 PyDictObject *mp;
1547
1548 if (!PyDict_Check(op)) {
1549 PyErr_BadInternalCall();
1550 return -1;
1551 }
1552 assert(key);
1553 assert(value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001554 assert(hash != -1);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001555 mp = (PyDictObject *)op;
1556
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001557 if (mp->ma_keys == Py_EMPTY_KEYS) {
1558 return insert_to_emptydict(mp, key, hash, value);
1559 }
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001560 /* insertdict() handles any resizing that might be necessary */
1561 return insertdict(mp, key, hash, value);
1562}
1563
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001564static int
INADA Naoki778928b2017-08-03 23:45:15 +09001565delitem_common(PyDictObject *mp, Py_hash_t hash, Py_ssize_t ix,
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001566 PyObject *old_value)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001567{
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001568 PyObject *old_key;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001569 PyDictKeyEntry *ep;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001570
INADA Naoki778928b2017-08-03 23:45:15 +09001571 Py_ssize_t hashpos = lookdict_index(mp->ma_keys, hash, ix);
1572 assert(hashpos >= 0);
1573
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001574 mp->ma_used--;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001575 mp->ma_version_tag = DICT_NEXT_VERSION();
1576 ep = &DK_ENTRIES(mp->ma_keys)[ix];
INADA Naokia7576492018-11-14 18:39:27 +09001577 dictkeys_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001578 ENSURE_ALLOWS_DELETIONS(mp);
1579 old_key = ep->me_key;
1580 ep->me_key = NULL;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001581 ep->me_value = NULL;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001582 Py_DECREF(old_key);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001583 Py_DECREF(old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001584
1585 assert(_PyDict_CheckConsistency(mp));
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001586 return 0;
1587}
1588
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001589int
Tim Peters1f5871e2000-07-04 17:44:48 +00001590PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001591{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001592 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 assert(key);
1594 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001595 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 hash = PyObject_Hash(key);
1597 if (hash == -1)
1598 return -1;
1599 }
Victor Stinner742da042016-09-07 17:40:12 -07001600
1601 return _PyDict_DelItem_KnownHash(op, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001602}
1603
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001604int
1605_PyDict_DelItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1606{
INADA Naoki778928b2017-08-03 23:45:15 +09001607 Py_ssize_t ix;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001608 PyDictObject *mp;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001609 PyObject *old_value;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001610
1611 if (!PyDict_Check(op)) {
1612 PyErr_BadInternalCall();
1613 return -1;
1614 }
1615 assert(key);
1616 assert(hash != -1);
1617 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001618 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001619 if (ix == DKIX_ERROR)
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001620 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09001621 if (ix == DKIX_EMPTY || old_value == NULL) {
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001622 _PyErr_SetKeyError(key);
1623 return -1;
1624 }
Victor Stinner78601a32016-09-09 19:28:36 -07001625
1626 // Split table doesn't allow deletion. Combine it.
1627 if (_PyDict_HasSplitTable(mp)) {
1628 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1629 return -1;
1630 }
INADA Naoki778928b2017-08-03 23:45:15 +09001631 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001632 assert(ix >= 0);
1633 }
1634
INADA Naoki778928b2017-08-03 23:45:15 +09001635 return delitem_common(mp, hash, ix, old_value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001636}
1637
Antoine Pitroud741ed42016-12-27 14:23:43 +01001638/* This function promises that the predicate -> deletion sequence is atomic
1639 * (i.e. protected by the GIL), assuming the predicate itself doesn't
1640 * release the GIL.
1641 */
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001642int
1643_PyDict_DelItemIf(PyObject *op, PyObject *key,
1644 int (*predicate)(PyObject *value))
1645{
Antoine Pitroud741ed42016-12-27 14:23:43 +01001646 Py_ssize_t hashpos, ix;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001647 PyDictObject *mp;
1648 Py_hash_t hash;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001649 PyObject *old_value;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001650 int res;
1651
1652 if (!PyDict_Check(op)) {
1653 PyErr_BadInternalCall();
1654 return -1;
1655 }
1656 assert(key);
1657 hash = PyObject_Hash(key);
1658 if (hash == -1)
1659 return -1;
1660 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001661 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001662 if (ix == DKIX_ERROR)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001663 return -1;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001664 if (ix == DKIX_EMPTY || old_value == NULL) {
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001665 _PyErr_SetKeyError(key);
1666 return -1;
1667 }
Antoine Pitroud741ed42016-12-27 14:23:43 +01001668
1669 // Split table doesn't allow deletion. Combine it.
1670 if (_PyDict_HasSplitTable(mp)) {
1671 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1672 return -1;
1673 }
INADA Naoki778928b2017-08-03 23:45:15 +09001674 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001675 assert(ix >= 0);
1676 }
1677
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001678 res = predicate(old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001679 if (res == -1)
1680 return -1;
INADA Naoki778928b2017-08-03 23:45:15 +09001681
1682 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1683 assert(hashpos >= 0);
1684
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001685 if (res > 0)
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001686 return delitem_common(mp, hashpos, ix, old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001687 else
1688 return 0;
1689}
1690
1691
Guido van Rossum25831651993-05-19 14:50:45 +00001692void
Tim Peters1f5871e2000-07-04 17:44:48 +00001693PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001694{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001696 PyDictKeysObject *oldkeys;
1697 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001698 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 if (!PyDict_Check(op))
1701 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001702 mp = ((PyDictObject *)op);
1703 oldkeys = mp->ma_keys;
1704 oldvalues = mp->ma_values;
1705 if (oldvalues == empty_values)
1706 return;
1707 /* Empty the dict... */
INADA Naokia7576492018-11-14 18:39:27 +09001708 dictkeys_incref(Py_EMPTY_KEYS);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001709 mp->ma_keys = Py_EMPTY_KEYS;
1710 mp->ma_values = empty_values;
1711 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001712 mp->ma_version_tag = DICT_NEXT_VERSION();
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001713 /* ...then clear the keys and values */
1714 if (oldvalues != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001715 n = oldkeys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001716 for (i = 0; i < n; i++)
1717 Py_CLEAR(oldvalues[i]);
1718 free_values(oldvalues);
INADA Naokia7576492018-11-14 18:39:27 +09001719 dictkeys_decref(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001720 }
1721 else {
1722 assert(oldkeys->dk_refcnt == 1);
INADA Naokia7576492018-11-14 18:39:27 +09001723 dictkeys_decref(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001724 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02001725 assert(_PyDict_CheckConsistency(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001726}
1727
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001728/* Internal version of PyDict_Next that returns a hash value in addition
1729 * to the key and value.
1730 * Return 1 on success, return 0 when the reached the end of the dictionary
1731 * (or if op is not a dictionary)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001732 */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001733int
1734_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1735 PyObject **pvalue, Py_hash_t *phash)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001736{
INADA Naokica2d8be2016-11-04 16:59:10 +09001737 Py_ssize_t i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001738 PyDictObject *mp;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001739 PyDictKeyEntry *entry_ptr;
1740 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001741
1742 if (!PyDict_Check(op))
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001743 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001744 mp = (PyDictObject *)op;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001745 i = *ppos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001746 if (mp->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09001747 if (i < 0 || i >= mp->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001748 return 0;
INADA Naokica2d8be2016-11-04 16:59:10 +09001749 /* values of split table is always dense */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001750 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
INADA Naokica2d8be2016-11-04 16:59:10 +09001751 value = mp->ma_values[i];
1752 assert(value != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001754 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09001755 Py_ssize_t n = mp->ma_keys->dk_nentries;
1756 if (i < 0 || i >= n)
1757 return 0;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001758 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
1759 while (i < n && entry_ptr->me_value == NULL) {
1760 entry_ptr++;
1761 i++;
Victor Stinner742da042016-09-07 17:40:12 -07001762 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001763 if (i >= n)
1764 return 0;
1765 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001767 *ppos = i+1;
1768 if (pkey)
1769 *pkey = entry_ptr->me_key;
1770 if (phash)
1771 *phash = entry_ptr->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001772 if (pvalue)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001773 *pvalue = value;
1774 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001775}
1776
Tim Peters080c88b2003-02-15 03:01:11 +00001777/*
1778 * Iterate over a dict. Use like so:
1779 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001780 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001781 * PyObject *key, *value;
1782 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001783 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001784 * Refer to borrowed references in key and value.
Tim Peters080c88b2003-02-15 03:01:11 +00001785 * }
1786 *
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001787 * Return 1 on success, return 0 when the reached the end of the dictionary
1788 * (or if op is not a dictionary)
1789 *
Tim Peters080c88b2003-02-15 03:01:11 +00001790 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001791 * mutates the dict. One exception: it is safe if the loop merely changes
1792 * the values associated with the keys (but doesn't insert new keys or
1793 * delete keys), via PyDict_SetItem().
1794 */
Guido van Rossum25831651993-05-19 14:50:45 +00001795int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001796PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001797{
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001798 return _PyDict_Next(op, ppos, pkey, pvalue, NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001799}
1800
Eric Snow96c6af92015-05-29 22:21:39 -06001801/* Internal version of dict.pop(). */
1802PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001803_PyDict_Pop_KnownHash(PyObject *dict, PyObject *key, Py_hash_t hash, PyObject *deflt)
Eric Snow96c6af92015-05-29 22:21:39 -06001804{
Victor Stinner742da042016-09-07 17:40:12 -07001805 Py_ssize_t ix, hashpos;
Eric Snow96c6af92015-05-29 22:21:39 -06001806 PyObject *old_value, *old_key;
1807 PyDictKeyEntry *ep;
Yury Selivanov684ef2c2016-10-28 19:01:21 -04001808 PyDictObject *mp;
1809
1810 assert(PyDict_Check(dict));
1811 mp = (PyDictObject *)dict;
Eric Snow96c6af92015-05-29 22:21:39 -06001812
1813 if (mp->ma_used == 0) {
1814 if (deflt) {
1815 Py_INCREF(deflt);
1816 return deflt;
1817 }
1818 _PyErr_SetKeyError(key);
1819 return NULL;
1820 }
INADA Naoki778928b2017-08-03 23:45:15 +09001821 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001822 if (ix == DKIX_ERROR)
Eric Snow96c6af92015-05-29 22:21:39 -06001823 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001824 if (ix == DKIX_EMPTY || old_value == NULL) {
Eric Snow96c6af92015-05-29 22:21:39 -06001825 if (deflt) {
1826 Py_INCREF(deflt);
1827 return deflt;
1828 }
1829 _PyErr_SetKeyError(key);
1830 return NULL;
1831 }
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001832
Victor Stinner78601a32016-09-09 19:28:36 -07001833 // Split table doesn't allow deletion. Combine it.
1834 if (_PyDict_HasSplitTable(mp)) {
1835 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1836 return NULL;
1837 }
INADA Naoki778928b2017-08-03 23:45:15 +09001838 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001839 assert(ix >= 0);
1840 }
1841
INADA Naoki778928b2017-08-03 23:45:15 +09001842 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1843 assert(hashpos >= 0);
Victor Stinner78601a32016-09-09 19:28:36 -07001844 assert(old_value != NULL);
Eric Snow96c6af92015-05-29 22:21:39 -06001845 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001846 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokia7576492018-11-14 18:39:27 +09001847 dictkeys_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
Victor Stinner78601a32016-09-09 19:28:36 -07001848 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1849 ENSURE_ALLOWS_DELETIONS(mp);
1850 old_key = ep->me_key;
1851 ep->me_key = NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001852 ep->me_value = NULL;
Victor Stinner78601a32016-09-09 19:28:36 -07001853 Py_DECREF(old_key);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001854
1855 assert(_PyDict_CheckConsistency(mp));
Eric Snow96c6af92015-05-29 22:21:39 -06001856 return old_value;
1857}
1858
Serhiy Storchaka67796522017-01-12 18:34:33 +02001859PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001860_PyDict_Pop(PyObject *dict, PyObject *key, PyObject *deflt)
Serhiy Storchaka67796522017-01-12 18:34:33 +02001861{
1862 Py_hash_t hash;
1863
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001864 if (((PyDictObject *)dict)->ma_used == 0) {
Serhiy Storchaka67796522017-01-12 18:34:33 +02001865 if (deflt) {
1866 Py_INCREF(deflt);
1867 return deflt;
1868 }
1869 _PyErr_SetKeyError(key);
1870 return NULL;
1871 }
1872 if (!PyUnicode_CheckExact(key) ||
1873 (hash = ((PyASCIIObject *) key)->hash) == -1) {
1874 hash = PyObject_Hash(key);
1875 if (hash == -1)
1876 return NULL;
1877 }
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001878 return _PyDict_Pop_KnownHash(dict, key, hash, deflt);
Serhiy Storchaka67796522017-01-12 18:34:33 +02001879}
1880
Eric Snow96c6af92015-05-29 22:21:39 -06001881/* Internal version of dict.from_keys(). It is subclass-friendly. */
1882PyObject *
1883_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
1884{
1885 PyObject *it; /* iter(iterable) */
1886 PyObject *key;
1887 PyObject *d;
1888 int status;
1889
Victor Stinnera5ed5f02016-12-06 18:45:50 +01001890 d = _PyObject_CallNoArg(cls);
Eric Snow96c6af92015-05-29 22:21:39 -06001891 if (d == NULL)
1892 return NULL;
1893
1894 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
1895 if (PyDict_CheckExact(iterable)) {
1896 PyDictObject *mp = (PyDictObject *)d;
1897 PyObject *oldvalue;
1898 Py_ssize_t pos = 0;
1899 PyObject *key;
1900 Py_hash_t hash;
1901
Serhiy Storchakac61ac162017-03-21 08:52:38 +02001902 if (dictresize(mp, ESTIMATE_SIZE(PyDict_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001903 Py_DECREF(d);
1904 return NULL;
1905 }
1906
1907 while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
1908 if (insertdict(mp, key, hash, value)) {
1909 Py_DECREF(d);
1910 return NULL;
1911 }
1912 }
1913 return d;
1914 }
1915 if (PyAnySet_CheckExact(iterable)) {
1916 PyDictObject *mp = (PyDictObject *)d;
1917 Py_ssize_t pos = 0;
1918 PyObject *key;
1919 Py_hash_t hash;
1920
Victor Stinner742da042016-09-07 17:40:12 -07001921 if (dictresize(mp, ESTIMATE_SIZE(PySet_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001922 Py_DECREF(d);
1923 return NULL;
1924 }
1925
1926 while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
1927 if (insertdict(mp, key, hash, value)) {
1928 Py_DECREF(d);
1929 return NULL;
1930 }
1931 }
1932 return d;
1933 }
1934 }
1935
1936 it = PyObject_GetIter(iterable);
1937 if (it == NULL){
1938 Py_DECREF(d);
1939 return NULL;
1940 }
1941
1942 if (PyDict_CheckExact(d)) {
1943 while ((key = PyIter_Next(it)) != NULL) {
1944 status = PyDict_SetItem(d, key, value);
1945 Py_DECREF(key);
1946 if (status < 0)
1947 goto Fail;
1948 }
1949 } else {
1950 while ((key = PyIter_Next(it)) != NULL) {
1951 status = PyObject_SetItem(d, key, value);
1952 Py_DECREF(key);
1953 if (status < 0)
1954 goto Fail;
1955 }
1956 }
1957
1958 if (PyErr_Occurred())
1959 goto Fail;
1960 Py_DECREF(it);
1961 return d;
1962
1963Fail:
1964 Py_DECREF(it);
1965 Py_DECREF(d);
1966 return NULL;
1967}
1968
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001969/* Methods */
1970
1971static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001972dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001973{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001974 PyObject **values = mp->ma_values;
1975 PyDictKeysObject *keys = mp->ma_keys;
1976 Py_ssize_t i, n;
INADA Naokia6296d32017-08-24 14:55:17 +09001977
1978 /* bpo-31095: UnTrack is needed before calling any callbacks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001979 PyObject_GC_UnTrack(mp);
1980 Py_TRASHCAN_SAFE_BEGIN(mp)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001981 if (values != NULL) {
1982 if (values != empty_values) {
Victor Stinner742da042016-09-07 17:40:12 -07001983 for (i = 0, n = mp->ma_keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001984 Py_XDECREF(values[i]);
1985 }
1986 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001987 }
INADA Naokia7576492018-11-14 18:39:27 +09001988 dictkeys_decref(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02001990 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001991 assert(keys->dk_refcnt == 1);
INADA Naokia7576492018-11-14 18:39:27 +09001992 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001993 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001994 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1995 free_list[numfree++] = mp;
1996 else
1997 Py_TYPE(mp)->tp_free((PyObject *)mp);
1998 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001999}
2000
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002001
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002002static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002003dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002004{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002005 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01002006 PyObject *key = NULL, *value = NULL;
2007 _PyUnicodeWriter writer;
2008 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00002009
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002010 i = Py_ReprEnter((PyObject *)mp);
2011 if (i != 0) {
2012 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
2013 }
Guido van Rossum255443b1998-04-10 22:47:14 +00002014
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01002016 Py_ReprLeave((PyObject *)mp);
2017 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002018 }
Tim Petersa7259592001-06-16 05:11:17 +00002019
Victor Stinnerf91929b2013-11-19 13:07:38 +01002020 _PyUnicodeWriter_Init(&writer);
2021 writer.overallocate = 1;
2022 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
2023 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00002024
Victor Stinnerf91929b2013-11-19 13:07:38 +01002025 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
2026 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00002027
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002028 /* Do repr() on each key+value pair, and insert ": " between them.
2029 Note that repr may mutate the dict. */
2030 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01002031 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002032 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01002033 PyObject *s;
2034 int res;
2035
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002036 /* Prevent repr from deleting key or value during key format. */
2037 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002038 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02002039
Victor Stinnerf91929b2013-11-19 13:07:38 +01002040 if (!first) {
2041 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
2042 goto error;
2043 }
2044 first = 0;
2045
2046 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002047 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01002048 goto error;
2049 res = _PyUnicodeWriter_WriteStr(&writer, s);
2050 Py_DECREF(s);
2051 if (res < 0)
2052 goto error;
2053
2054 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
2055 goto error;
2056
2057 s = PyObject_Repr(value);
2058 if (s == NULL)
2059 goto error;
2060 res = _PyUnicodeWriter_WriteStr(&writer, s);
2061 Py_DECREF(s);
2062 if (res < 0)
2063 goto error;
2064
2065 Py_CLEAR(key);
2066 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 }
Tim Petersa7259592001-06-16 05:11:17 +00002068
Victor Stinnerf91929b2013-11-19 13:07:38 +01002069 writer.overallocate = 0;
2070 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
2071 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00002072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002073 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01002074
2075 return _PyUnicodeWriter_Finish(&writer);
2076
2077error:
2078 Py_ReprLeave((PyObject *)mp);
2079 _PyUnicodeWriter_Dealloc(&writer);
2080 Py_XDECREF(key);
2081 Py_XDECREF(value);
2082 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002083}
2084
Martin v. Löwis18e16552006-02-15 17:27:45 +00002085static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002086dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002087{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002088 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002089}
2090
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002091static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002092dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002093{
Victor Stinner742da042016-09-07 17:40:12 -07002094 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002095 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09002096 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002098 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002099 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002100 hash = PyObject_Hash(key);
2101 if (hash == -1)
2102 return NULL;
2103 }
INADA Naoki778928b2017-08-03 23:45:15 +09002104 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002105 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002106 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002107 if (ix == DKIX_EMPTY || value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002108 if (!PyDict_CheckExact(mp)) {
2109 /* Look up __missing__ method if we're a subclass. */
2110 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05002111 _Py_IDENTIFIER(__missing__);
2112 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002113 if (missing != NULL) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01002114 res = PyObject_CallFunctionObjArgs(missing,
2115 key, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002116 Py_DECREF(missing);
2117 return res;
2118 }
2119 else if (PyErr_Occurred())
2120 return NULL;
2121 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07002122 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002123 return NULL;
2124 }
INADA Naokiba609772016-12-07 20:41:42 +09002125 Py_INCREF(value);
2126 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002127}
2128
2129static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002130dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002131{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002132 if (w == NULL)
2133 return PyDict_DelItem((PyObject *)mp, v);
2134 else
2135 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002136}
2137
Guido van Rossuma9e7a811997-05-13 21:02:11 +00002138static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002139 (lenfunc)dict_length, /*mp_length*/
2140 (binaryfunc)dict_subscript, /*mp_subscript*/
2141 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002142};
2143
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002144static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002145dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002146{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002147 PyObject *v;
2148 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002149 PyDictKeyEntry *ep;
2150 Py_ssize_t size, n, offset;
2151 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002152
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002153 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002154 n = mp->ma_used;
2155 v = PyList_New(n);
2156 if (v == NULL)
2157 return NULL;
2158 if (n != mp->ma_used) {
2159 /* Durnit. The allocations caused the dict to resize.
2160 * Just start over, this shouldn't normally happen.
2161 */
2162 Py_DECREF(v);
2163 goto again;
2164 }
Victor Stinner742da042016-09-07 17:40:12 -07002165 ep = DK_ENTRIES(mp->ma_keys);
2166 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002167 if (mp->ma_values) {
2168 value_ptr = mp->ma_values;
2169 offset = sizeof(PyObject *);
2170 }
2171 else {
2172 value_ptr = &ep[0].me_value;
2173 offset = sizeof(PyDictKeyEntry);
2174 }
2175 for (i = 0, j = 0; i < size; i++) {
2176 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002177 PyObject *key = ep[i].me_key;
2178 Py_INCREF(key);
2179 PyList_SET_ITEM(v, j, key);
2180 j++;
2181 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002182 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002183 }
2184 assert(j == n);
2185 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002186}
2187
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002188static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002189dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002190{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002191 PyObject *v;
2192 Py_ssize_t i, j;
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002193 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002194 Py_ssize_t size, n, offset;
2195 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002196
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002197 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002198 n = mp->ma_used;
2199 v = PyList_New(n);
2200 if (v == NULL)
2201 return NULL;
2202 if (n != mp->ma_used) {
2203 /* Durnit. The allocations caused the dict to resize.
2204 * Just start over, this shouldn't normally happen.
2205 */
2206 Py_DECREF(v);
2207 goto again;
2208 }
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002209 ep = DK_ENTRIES(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002210 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002211 if (mp->ma_values) {
2212 value_ptr = mp->ma_values;
2213 offset = sizeof(PyObject *);
2214 }
2215 else {
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002216 value_ptr = &ep[0].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002217 offset = sizeof(PyDictKeyEntry);
2218 }
2219 for (i = 0, j = 0; i < size; i++) {
2220 PyObject *value = *value_ptr;
2221 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2222 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002223 Py_INCREF(value);
2224 PyList_SET_ITEM(v, j, value);
2225 j++;
2226 }
2227 }
2228 assert(j == n);
2229 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002230}
2231
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002232static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002233dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002234{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002235 PyObject *v;
2236 Py_ssize_t i, j, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002237 Py_ssize_t size, offset;
2238 PyObject *item, *key;
2239 PyDictKeyEntry *ep;
2240 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002242 /* Preallocate the list of tuples, to avoid allocations during
2243 * the loop over the items, which could trigger GC, which
2244 * could resize the dict. :-(
2245 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002246 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002247 n = mp->ma_used;
2248 v = PyList_New(n);
2249 if (v == NULL)
2250 return NULL;
2251 for (i = 0; i < n; i++) {
2252 item = PyTuple_New(2);
2253 if (item == NULL) {
2254 Py_DECREF(v);
2255 return NULL;
2256 }
2257 PyList_SET_ITEM(v, i, item);
2258 }
2259 if (n != mp->ma_used) {
2260 /* Durnit. The allocations caused the dict to resize.
2261 * Just start over, this shouldn't normally happen.
2262 */
2263 Py_DECREF(v);
2264 goto again;
2265 }
2266 /* Nothing we do below makes any function calls. */
Victor Stinner742da042016-09-07 17:40:12 -07002267 ep = DK_ENTRIES(mp->ma_keys);
2268 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002269 if (mp->ma_values) {
2270 value_ptr = mp->ma_values;
2271 offset = sizeof(PyObject *);
2272 }
2273 else {
2274 value_ptr = &ep[0].me_value;
2275 offset = sizeof(PyDictKeyEntry);
2276 }
2277 for (i = 0, j = 0; i < size; i++) {
2278 PyObject *value = *value_ptr;
2279 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2280 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 key = ep[i].me_key;
2282 item = PyList_GET_ITEM(v, j);
2283 Py_INCREF(key);
2284 PyTuple_SET_ITEM(item, 0, key);
2285 Py_INCREF(value);
2286 PyTuple_SET_ITEM(item, 1, value);
2287 j++;
2288 }
2289 }
2290 assert(j == n);
2291 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002292}
2293
Larry Hastings5c661892014-01-24 06:17:25 -08002294/*[clinic input]
2295@classmethod
2296dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08002297 iterable: object
2298 value: object=None
2299 /
2300
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002301Create a new dictionary with keys from iterable and values set to value.
Larry Hastings5c661892014-01-24 06:17:25 -08002302[clinic start generated code]*/
2303
Larry Hastings5c661892014-01-24 06:17:25 -08002304static PyObject *
2305dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002306/*[clinic end generated code: output=8fb98e4b10384999 input=382ba4855d0f74c3]*/
Larry Hastings5c661892014-01-24 06:17:25 -08002307{
Eric Snow96c6af92015-05-29 22:21:39 -06002308 return _PyDict_FromKeys((PyObject *)type, iterable, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002309}
2310
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002311static int
Victor Stinner742da042016-09-07 17:40:12 -07002312dict_update_common(PyObject *self, PyObject *args, PyObject *kwds,
2313 const char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002314{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002315 PyObject *arg = NULL;
2316 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002317
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002318 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002319 result = -1;
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002320 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002321 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002322 _Py_IDENTIFIER(keys);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002323 PyObject *func;
2324 if (_PyObject_LookupAttrId(arg, &PyId_keys, &func) < 0) {
2325 result = -1;
2326 }
2327 else if (func != NULL) {
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002328 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002329 result = PyDict_Merge(self, arg, 1);
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002330 }
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002331 else {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002332 result = PyDict_MergeFromSeq2(self, arg, 1);
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002333 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002334 }
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002336 if (result == 0 && kwds != NULL) {
2337 if (PyArg_ValidateKeywordArguments(kwds))
2338 result = PyDict_Merge(self, kwds, 1);
2339 else
2340 result = -1;
2341 }
2342 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002343}
2344
Victor Stinner91f0d4a2017-01-19 12:45:06 +01002345/* Note: dict.update() uses the METH_VARARGS|METH_KEYWORDS calling convention.
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +03002346 Using METH_FASTCALL|METH_KEYWORDS would make dict.update(**dict2) calls
2347 slower, see the issue #29312. */
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002348static PyObject *
2349dict_update(PyObject *self, PyObject *args, PyObject *kwds)
2350{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002351 if (dict_update_common(self, args, kwds, "update") != -1)
2352 Py_RETURN_NONE;
2353 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002354}
2355
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002356/* Update unconditionally replaces existing items.
2357 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00002358 otherwise it leaves existing items unchanged.
2359
2360 PyDict_{Update,Merge} update/merge from a mapping object.
2361
Tim Petersf582b822001-12-11 18:51:08 +00002362 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00002363 producing iterable objects of length 2.
2364*/
2365
Tim Petersf582b822001-12-11 18:51:08 +00002366int
Tim Peters1fc240e2001-10-26 05:06:50 +00002367PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
2368{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 PyObject *it; /* iter(seq2) */
2370 Py_ssize_t i; /* index into seq2 of current element */
2371 PyObject *item; /* seq2[i] */
2372 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00002373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002374 assert(d != NULL);
2375 assert(PyDict_Check(d));
2376 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00002377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002378 it = PyObject_GetIter(seq2);
2379 if (it == NULL)
2380 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002382 for (i = 0; ; ++i) {
2383 PyObject *key, *value;
2384 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00002385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386 fast = NULL;
2387 item = PyIter_Next(it);
2388 if (item == NULL) {
2389 if (PyErr_Occurred())
2390 goto Fail;
2391 break;
2392 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 /* Convert item to sequence, and verify length 2. */
2395 fast = PySequence_Fast(item, "");
2396 if (fast == NULL) {
2397 if (PyErr_ExceptionMatches(PyExc_TypeError))
2398 PyErr_Format(PyExc_TypeError,
2399 "cannot convert dictionary update "
2400 "sequence element #%zd to a sequence",
2401 i);
2402 goto Fail;
2403 }
2404 n = PySequence_Fast_GET_SIZE(fast);
2405 if (n != 2) {
2406 PyErr_Format(PyExc_ValueError,
2407 "dictionary update sequence element #%zd "
2408 "has length %zd; 2 is required",
2409 i, n);
2410 goto Fail;
2411 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002413 /* Update/merge with this (key, value) pair. */
2414 key = PySequence_Fast_GET_ITEM(fast, 0);
2415 value = PySequence_Fast_GET_ITEM(fast, 1);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002416 Py_INCREF(key);
2417 Py_INCREF(value);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002418 if (override) {
2419 if (PyDict_SetItem(d, key, value) < 0) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002420 Py_DECREF(key);
2421 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002422 goto Fail;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002423 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002424 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002425 else if (PyDict_GetItemWithError(d, key) == NULL) {
2426 if (PyErr_Occurred() || PyDict_SetItem(d, key, value) < 0) {
2427 Py_DECREF(key);
2428 Py_DECREF(value);
2429 goto Fail;
2430 }
2431 }
2432
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002433 Py_DECREF(key);
2434 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002435 Py_DECREF(fast);
2436 Py_DECREF(item);
2437 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002439 i = 0;
Victor Stinner611b0fa2016-09-14 15:02:01 +02002440 assert(_PyDict_CheckConsistency((PyDictObject *)d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002441 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00002442Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002443 Py_XDECREF(item);
2444 Py_XDECREF(fast);
2445 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002446Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002447 Py_DECREF(it);
2448 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00002449}
2450
doko@ubuntu.comc96df682016-10-11 08:04:02 +02002451static int
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002452dict_merge(PyObject *a, PyObject *b, int override)
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002453{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002454 PyDictObject *mp, *other;
2455 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -07002456 PyDictKeyEntry *entry, *ep0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002457
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002458 assert(0 <= override && override <= 2);
2459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002460 /* We accept for the argument either a concrete dictionary object,
2461 * or an abstract "mapping" object. For the former, we can do
2462 * things quite efficiently. For the latter, we only require that
2463 * PyMapping_Keys() and PyObject_GetItem() be supported.
2464 */
2465 if (a == NULL || !PyDict_Check(a) || b == NULL) {
2466 PyErr_BadInternalCall();
2467 return -1;
2468 }
2469 mp = (PyDictObject*)a;
INADA Naoki2aaf98c2018-09-26 12:59:00 +09002470 if (PyDict_Check(b) && (Py_TYPE(b)->tp_iter == (getiterfunc)dict_iter)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002471 other = (PyDictObject*)b;
2472 if (other == mp || other->ma_used == 0)
2473 /* a.update(a) or a.update({}); nothing to do */
2474 return 0;
2475 if (mp->ma_used == 0)
2476 /* Since the target dict is empty, PyDict_GetItem()
2477 * always returns NULL. Setting override to 1
2478 * skips the unnecessary test.
2479 */
2480 override = 1;
2481 /* Do one big resize at the start, rather than
2482 * incrementally resizing as we insert new items. Expect
2483 * that there will be no (or few) overlapping keys.
2484 */
INADA Naokib1152be2016-10-27 19:26:50 +09002485 if (USABLE_FRACTION(mp->ma_keys->dk_size) < other->ma_used) {
2486 if (dictresize(mp, ESTIMATE_SIZE(mp->ma_used + other->ma_used))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002487 return -1;
INADA Naokib1152be2016-10-27 19:26:50 +09002488 }
2489 }
Victor Stinner742da042016-09-07 17:40:12 -07002490 ep0 = DK_ENTRIES(other->ma_keys);
2491 for (i = 0, n = other->ma_keys->dk_nentries; i < n; i++) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002492 PyObject *key, *value;
2493 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002494 entry = &ep0[i];
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002495 key = entry->me_key;
2496 hash = entry->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002497 if (other->ma_values)
2498 value = other->ma_values[i];
2499 else
2500 value = entry->me_value;
2501
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002502 if (value != NULL) {
2503 int err = 0;
2504 Py_INCREF(key);
2505 Py_INCREF(value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002506 if (override == 1)
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002507 err = insertdict(mp, key, hash, value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002508 else if (_PyDict_GetItem_KnownHash(a, key, hash) == NULL) {
2509 if (PyErr_Occurred()) {
2510 Py_DECREF(value);
2511 Py_DECREF(key);
2512 return -1;
2513 }
2514 err = insertdict(mp, key, hash, value);
2515 }
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002516 else if (override != 0) {
2517 _PyErr_SetKeyError(key);
2518 Py_DECREF(value);
2519 Py_DECREF(key);
2520 return -1;
2521 }
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002522 Py_DECREF(value);
2523 Py_DECREF(key);
2524 if (err != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002525 return -1;
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002526
Victor Stinner742da042016-09-07 17:40:12 -07002527 if (n != other->ma_keys->dk_nentries) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002528 PyErr_SetString(PyExc_RuntimeError,
2529 "dict mutated during update");
2530 return -1;
2531 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002532 }
2533 }
2534 }
2535 else {
2536 /* Do it the generic, slower way */
2537 PyObject *keys = PyMapping_Keys(b);
2538 PyObject *iter;
2539 PyObject *key, *value;
2540 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002542 if (keys == NULL)
2543 /* Docstring says this is equivalent to E.keys() so
2544 * if E doesn't have a .keys() method we want
2545 * AttributeError to percolate up. Might as well
2546 * do the same for any other error.
2547 */
2548 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002550 iter = PyObject_GetIter(keys);
2551 Py_DECREF(keys);
2552 if (iter == NULL)
2553 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002555 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002556 if (override != 1) {
2557 if (PyDict_GetItemWithError(a, key) != NULL) {
2558 if (override != 0) {
2559 _PyErr_SetKeyError(key);
2560 Py_DECREF(key);
2561 Py_DECREF(iter);
2562 return -1;
2563 }
2564 Py_DECREF(key);
2565 continue;
2566 }
2567 else if (PyErr_Occurred()) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002568 Py_DECREF(key);
2569 Py_DECREF(iter);
2570 return -1;
2571 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002572 }
2573 value = PyObject_GetItem(b, key);
2574 if (value == NULL) {
2575 Py_DECREF(iter);
2576 Py_DECREF(key);
2577 return -1;
2578 }
2579 status = PyDict_SetItem(a, key, value);
2580 Py_DECREF(key);
2581 Py_DECREF(value);
2582 if (status < 0) {
2583 Py_DECREF(iter);
2584 return -1;
2585 }
2586 }
2587 Py_DECREF(iter);
2588 if (PyErr_Occurred())
2589 /* Iterator completed, via error */
2590 return -1;
2591 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02002592 assert(_PyDict_CheckConsistency((PyDictObject *)a));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002593 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002594}
2595
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002596int
2597PyDict_Update(PyObject *a, PyObject *b)
2598{
2599 return dict_merge(a, b, 1);
2600}
2601
2602int
2603PyDict_Merge(PyObject *a, PyObject *b, int override)
2604{
2605 /* XXX Deprecate override not in (0, 1). */
2606 return dict_merge(a, b, override != 0);
2607}
2608
2609int
2610_PyDict_MergeEx(PyObject *a, PyObject *b, int override)
2611{
2612 return dict_merge(a, b, override);
2613}
2614
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002615static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302616dict_copy(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002617{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002618 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002619}
2620
2621PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002622PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002623{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002624 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002625 PyDictObject *mp;
2626 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002628 if (o == NULL || !PyDict_Check(o)) {
2629 PyErr_BadInternalCall();
2630 return NULL;
2631 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002632
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002633 mp = (PyDictObject *)o;
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002634 if (mp->ma_used == 0) {
2635 /* The dict is empty; just return a new dict. */
2636 return PyDict_New();
2637 }
2638
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002639 if (_PyDict_HasSplitTable(mp)) {
2640 PyDictObject *split_copy;
Victor Stinner742da042016-09-07 17:40:12 -07002641 Py_ssize_t size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
2642 PyObject **newvalues;
2643 newvalues = new_values(size);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002644 if (newvalues == NULL)
2645 return PyErr_NoMemory();
2646 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2647 if (split_copy == NULL) {
2648 free_values(newvalues);
2649 return NULL;
2650 }
2651 split_copy->ma_values = newvalues;
2652 split_copy->ma_keys = mp->ma_keys;
2653 split_copy->ma_used = mp->ma_used;
INADA Naokid1c82c52018-04-03 11:43:53 +09002654 split_copy->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokia7576492018-11-14 18:39:27 +09002655 dictkeys_incref(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002656 for (i = 0, n = size; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002657 PyObject *value = mp->ma_values[i];
2658 Py_XINCREF(value);
2659 split_copy->ma_values[i] = value;
2660 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002661 if (_PyObject_GC_IS_TRACKED(mp))
2662 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002663 return (PyObject *)split_copy;
2664 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002665
2666 if (PyDict_CheckExact(mp) && mp->ma_values == NULL &&
2667 (mp->ma_used >= (mp->ma_keys->dk_nentries * 2) / 3))
2668 {
2669 /* Use fast-copy if:
2670
2671 (1) 'mp' is an instance of a subclassed dict; and
2672
2673 (2) 'mp' is not a split-dict; and
2674
2675 (3) if 'mp' is non-compact ('del' operation does not resize dicts),
2676 do fast-copy only if it has at most 1/3 non-used keys.
2677
Ville Skyttä61f82e02018-04-20 23:08:45 +03002678 The last condition (3) is important to guard against a pathological
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002679 case when a large dict is almost emptied with multiple del/pop
2680 operations and copied after that. In cases like this, we defer to
2681 PyDict_Merge, which produces a compacted copy.
2682 */
2683 return clone_combined_dict(mp);
2684 }
2685
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002686 copy = PyDict_New();
2687 if (copy == NULL)
2688 return NULL;
2689 if (PyDict_Merge(copy, o, 1) == 0)
2690 return copy;
2691 Py_DECREF(copy);
2692 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002693}
2694
Martin v. Löwis18e16552006-02-15 17:27:45 +00002695Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002696PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002697{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002698 if (mp == NULL || !PyDict_Check(mp)) {
2699 PyErr_BadInternalCall();
2700 return -1;
2701 }
2702 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002703}
2704
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002705PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002706PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002707{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002708 if (mp == NULL || !PyDict_Check(mp)) {
2709 PyErr_BadInternalCall();
2710 return NULL;
2711 }
2712 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002713}
2714
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002715PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002716PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002717{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002718 if (mp == NULL || !PyDict_Check(mp)) {
2719 PyErr_BadInternalCall();
2720 return NULL;
2721 }
2722 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002723}
2724
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002725PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002726PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002727{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002728 if (mp == NULL || !PyDict_Check(mp)) {
2729 PyErr_BadInternalCall();
2730 return NULL;
2731 }
2732 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002733}
2734
Tim Peterse63415e2001-05-08 04:38:29 +00002735/* Return 1 if dicts equal, 0 if not, -1 if error.
2736 * Gets out as soon as any difference is detected.
2737 * Uses only Py_EQ comparison.
2738 */
2739static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002740dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002741{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002742 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002744 if (a->ma_used != b->ma_used)
2745 /* can't be equal if # of entries differ */
2746 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002747 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Victor Stinner742da042016-09-07 17:40:12 -07002748 for (i = 0; i < a->ma_keys->dk_nentries; i++) {
2749 PyDictKeyEntry *ep = &DK_ENTRIES(a->ma_keys)[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002750 PyObject *aval;
2751 if (a->ma_values)
2752 aval = a->ma_values[i];
2753 else
2754 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002755 if (aval != NULL) {
2756 int cmp;
2757 PyObject *bval;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002758 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002759 /* temporarily bump aval's refcount to ensure it stays
2760 alive until we're done with it */
2761 Py_INCREF(aval);
2762 /* ditto for key */
2763 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002764 /* reuse the known hash value */
INADA Naoki778928b2017-08-03 23:45:15 +09002765 b->ma_keys->dk_lookup(b, key, ep->me_hash, &bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002766 if (bval == NULL) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002767 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002768 Py_DECREF(aval);
2769 if (PyErr_Occurred())
2770 return -1;
2771 return 0;
2772 }
2773 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002774 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002775 Py_DECREF(aval);
2776 if (cmp <= 0) /* error or not equal */
2777 return cmp;
2778 }
2779 }
2780 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002781}
Tim Peterse63415e2001-05-08 04:38:29 +00002782
2783static PyObject *
2784dict_richcompare(PyObject *v, PyObject *w, int op)
2785{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002786 int cmp;
2787 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002788
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002789 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2790 res = Py_NotImplemented;
2791 }
2792 else if (op == Py_EQ || op == Py_NE) {
2793 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2794 if (cmp < 0)
2795 return NULL;
2796 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2797 }
2798 else
2799 res = Py_NotImplemented;
2800 Py_INCREF(res);
2801 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002802}
Tim Peterse63415e2001-05-08 04:38:29 +00002803
Larry Hastings61272b72014-01-07 12:41:53 -08002804/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002805
2806@coexist
2807dict.__contains__
2808
2809 key: object
2810 /
2811
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002812True if the dictionary has the specified key, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002813[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002814
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002815static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002816dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka19d25972017-02-04 08:05:07 +02002817/*[clinic end generated code: output=a3d03db709ed6e6b input=fe1cb42ad831e820]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002818{
Larry Hastingsc2047262014-01-25 20:43:29 -08002819 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002820 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002821 Py_ssize_t ix;
INADA Naokiba609772016-12-07 20:41:42 +09002822 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002824 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002825 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002826 hash = PyObject_Hash(key);
2827 if (hash == -1)
2828 return NULL;
2829 }
INADA Naoki778928b2017-08-03 23:45:15 +09002830 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002831 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002832 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002833 if (ix == DKIX_EMPTY || value == NULL)
Victor Stinner742da042016-09-07 17:40:12 -07002834 Py_RETURN_FALSE;
2835 Py_RETURN_TRUE;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002836}
2837
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002838/*[clinic input]
2839dict.get
2840
2841 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002842 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002843 /
2844
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002845Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002846[clinic start generated code]*/
2847
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002848static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002849dict_get_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002850/*[clinic end generated code: output=bba707729dee05bf input=279ddb5790b6b107]*/
Barry Warsawc38c5da1997-10-06 17:49:20 +00002851{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002852 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002853 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002854 Py_ssize_t ix;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002856 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002857 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002858 hash = PyObject_Hash(key);
2859 if (hash == -1)
2860 return NULL;
2861 }
INADA Naoki778928b2017-08-03 23:45:15 +09002862 ix = (self->ma_keys->dk_lookup) (self, key, hash, &val);
Victor Stinner742da042016-09-07 17:40:12 -07002863 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002864 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002865 if (ix == DKIX_EMPTY || val == NULL) {
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002866 val = default_value;
INADA Naokiba609772016-12-07 20:41:42 +09002867 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002868 Py_INCREF(val);
2869 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002870}
2871
Benjamin Peterson00e98862013-03-07 22:16:29 -05002872PyObject *
2873PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002874{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002875 PyDictObject *mp = (PyDictObject *)d;
INADA Naoki93f26f72016-11-02 18:45:16 +09002876 PyObject *value;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002877 Py_hash_t hash;
Guido van Rossum164452c2000-08-08 16:12:54 +00002878
Benjamin Peterson00e98862013-03-07 22:16:29 -05002879 if (!PyDict_Check(d)) {
2880 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002881 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002882 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002884 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002885 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002886 hash = PyObject_Hash(key);
2887 if (hash == -1)
2888 return NULL;
2889 }
Inada Naoki2ddc7f62019-03-18 20:38:33 +09002890 if (mp->ma_keys == Py_EMPTY_KEYS) {
2891 if (insert_to_emptydict(mp, key, hash, defaultobj) < 0) {
2892 return NULL;
2893 }
2894 return defaultobj;
2895 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002896
2897 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
2898 if (insertion_resize(mp) < 0)
2899 return NULL;
2900 }
2901
INADA Naoki778928b2017-08-03 23:45:15 +09002902 Py_ssize_t ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002903 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002904 return NULL;
INADA Naoki93f26f72016-11-02 18:45:16 +09002905
2906 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09002907 ((ix >= 0 && value == NULL && mp->ma_used != ix) ||
INADA Naoki93f26f72016-11-02 18:45:16 +09002908 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
2909 if (insertion_resize(mp) < 0) {
2910 return NULL;
2911 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002912 ix = DKIX_EMPTY;
2913 }
2914
2915 if (ix == DKIX_EMPTY) {
2916 PyDictKeyEntry *ep, *ep0;
2917 value = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002918 if (mp->ma_keys->dk_usable <= 0) {
Victor Stinner3c336c52016-09-12 14:17:40 +02002919 if (insertion_resize(mp) < 0) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002920 return NULL;
Victor Stinner3c336c52016-09-12 14:17:40 +02002921 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002922 }
INADA Naoki778928b2017-08-03 23:45:15 +09002923 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naoki93f26f72016-11-02 18:45:16 +09002924 ep0 = DK_ENTRIES(mp->ma_keys);
2925 ep = &ep0[mp->ma_keys->dk_nentries];
INADA Naokia7576492018-11-14 18:39:27 +09002926 dictkeys_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002927 Py_INCREF(key);
INADA Naoki93f26f72016-11-02 18:45:16 +09002928 Py_INCREF(value);
2929 MAINTAIN_TRACKING(mp, key, value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002930 ep->me_key = key;
2931 ep->me_hash = hash;
INADA Naokiba609772016-12-07 20:41:42 +09002932 if (_PyDict_HasSplitTable(mp)) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002933 assert(mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
2934 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Victor Stinner742da042016-09-07 17:40:12 -07002935 }
2936 else {
INADA Naoki93f26f72016-11-02 18:45:16 +09002937 ep->me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07002938 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002939 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002940 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naoki93f26f72016-11-02 18:45:16 +09002941 mp->ma_keys->dk_usable--;
2942 mp->ma_keys->dk_nentries++;
2943 assert(mp->ma_keys->dk_usable >= 0);
2944 }
INADA Naokiba609772016-12-07 20:41:42 +09002945 else if (value == NULL) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002946 value = defaultobj;
2947 assert(_PyDict_HasSplitTable(mp));
2948 assert(ix == mp->ma_used);
2949 Py_INCREF(value);
2950 MAINTAIN_TRACKING(mp, key, value);
INADA Naokiba609772016-12-07 20:41:42 +09002951 mp->ma_values[ix] = value;
INADA Naoki93f26f72016-11-02 18:45:16 +09002952 mp->ma_used++;
2953 mp->ma_version_tag = DICT_NEXT_VERSION();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002954 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002955
2956 assert(_PyDict_CheckConsistency(mp));
2957 return value;
Guido van Rossum164452c2000-08-08 16:12:54 +00002958}
2959
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002960/*[clinic input]
2961dict.setdefault
2962
2963 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002964 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002965 /
2966
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002967Insert key with a value of default if key is not in the dictionary.
2968
2969Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002970[clinic start generated code]*/
2971
Benjamin Peterson00e98862013-03-07 22:16:29 -05002972static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002973dict_setdefault_impl(PyDictObject *self, PyObject *key,
2974 PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002975/*[clinic end generated code: output=f8c1101ebf69e220 input=0f063756e815fd9d]*/
Benjamin Peterson00e98862013-03-07 22:16:29 -05002976{
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002977 PyObject *val;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002978
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002979 val = PyDict_SetDefault((PyObject *)self, key, default_value);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002980 Py_XINCREF(val);
2981 return val;
2982}
Guido van Rossum164452c2000-08-08 16:12:54 +00002983
2984static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302985dict_clear(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002986{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002987 PyDict_Clear((PyObject *)mp);
2988 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002989}
2990
Guido van Rossumba6ab842000-12-12 22:02:18 +00002991static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002992dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002993{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002994 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00002995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002996 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2997 return NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06002998
Yury Selivanov684ef2c2016-10-28 19:01:21 -04002999 return _PyDict_Pop((PyObject*)mp, key, deflt);
Guido van Rossume027d982002-04-12 15:11:59 +00003000}
3001
3002static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303003dict_popitem(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Guido van Rossumba6ab842000-12-12 22:02:18 +00003004{
Victor Stinner742da042016-09-07 17:40:12 -07003005 Py_ssize_t i, j;
3006 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003007 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00003008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003009 /* Allocate the result tuple before checking the size. Believe it
3010 * or not, this allocation could trigger a garbage collection which
3011 * could empty the dict, so if we checked the size first and that
3012 * happened, the result would be an infinite loop (searching for an
3013 * entry that no longer exists). Note that the usual popitem()
3014 * idiom is "while d: k, v = d.popitem()". so needing to throw the
3015 * tuple away if the dict *is* empty isn't a significant
3016 * inefficiency -- possible, but unlikely in practice.
3017 */
3018 res = PyTuple_New(2);
3019 if (res == NULL)
3020 return NULL;
3021 if (mp->ma_used == 0) {
3022 Py_DECREF(res);
3023 PyErr_SetString(PyExc_KeyError,
3024 "popitem(): dictionary is empty");
3025 return NULL;
3026 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003027 /* Convert split table to combined table */
3028 if (mp->ma_keys->dk_lookup == lookdict_split) {
3029 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
3030 Py_DECREF(res);
3031 return NULL;
3032 }
3033 }
3034 ENSURE_ALLOWS_DELETIONS(mp);
Victor Stinner742da042016-09-07 17:40:12 -07003035
3036 /* Pop last item */
3037 ep0 = DK_ENTRIES(mp->ma_keys);
3038 i = mp->ma_keys->dk_nentries - 1;
3039 while (i >= 0 && ep0[i].me_value == NULL) {
3040 i--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003041 }
Victor Stinner742da042016-09-07 17:40:12 -07003042 assert(i >= 0);
3043
3044 ep = &ep0[i];
3045 j = lookdict_index(mp->ma_keys, ep->me_hash, i);
3046 assert(j >= 0);
INADA Naokia7576492018-11-14 18:39:27 +09003047 assert(dictkeys_get_index(mp->ma_keys, j) == i);
3048 dictkeys_set_index(mp->ma_keys, j, DKIX_DUMMY);
Victor Stinner742da042016-09-07 17:40:12 -07003049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003050 PyTuple_SET_ITEM(res, 0, ep->me_key);
3051 PyTuple_SET_ITEM(res, 1, ep->me_value);
Victor Stinner742da042016-09-07 17:40:12 -07003052 ep->me_key = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003053 ep->me_value = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07003054 /* We can't dk_usable++ since there is DKIX_DUMMY in indices */
3055 mp->ma_keys->dk_nentries = i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003056 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003057 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +02003058 assert(_PyDict_CheckConsistency(mp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003059 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00003060}
3061
Jeremy Hylton8caad492000-06-23 14:18:11 +00003062static int
3063dict_traverse(PyObject *op, visitproc visit, void *arg)
3064{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003065 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson55f44522016-09-05 12:12:59 -07003066 PyDictKeysObject *keys = mp->ma_keys;
Serhiy Storchaka46825d22016-09-26 21:29:34 +03003067 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Victor Stinner742da042016-09-07 17:40:12 -07003068 Py_ssize_t i, n = keys->dk_nentries;
3069
Benjamin Peterson55f44522016-09-05 12:12:59 -07003070 if (keys->dk_lookup == lookdict) {
3071 for (i = 0; i < n; i++) {
3072 if (entries[i].me_value != NULL) {
3073 Py_VISIT(entries[i].me_value);
3074 Py_VISIT(entries[i].me_key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003075 }
3076 }
Victor Stinner742da042016-09-07 17:40:12 -07003077 }
3078 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003079 if (mp->ma_values != NULL) {
Benjamin Peterson55f44522016-09-05 12:12:59 -07003080 for (i = 0; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003081 Py_VISIT(mp->ma_values[i]);
3082 }
3083 }
3084 else {
Benjamin Peterson55f44522016-09-05 12:12:59 -07003085 for (i = 0; i < n; i++) {
3086 Py_VISIT(entries[i].me_value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003087 }
3088 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003089 }
3090 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00003091}
3092
3093static int
3094dict_tp_clear(PyObject *op)
3095{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003096 PyDict_Clear(op);
3097 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00003098}
3099
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003100static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003101
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003102Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003103_PyDict_SizeOf(PyDictObject *mp)
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003104{
Victor Stinner742da042016-09-07 17:40:12 -07003105 Py_ssize_t size, usable, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003106
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003107 size = DK_SIZE(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07003108 usable = USABLE_FRACTION(size);
3109
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02003110 res = _PyObject_SIZE(Py_TYPE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003111 if (mp->ma_values)
Victor Stinner742da042016-09-07 17:40:12 -07003112 res += usable * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003113 /* If the dictionary is split, the keys portion is accounted-for
3114 in the type object. */
3115 if (mp->ma_keys->dk_refcnt == 1)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003116 res += (sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003117 + DK_IXSIZE(mp->ma_keys) * size
3118 + sizeof(PyDictKeyEntry) * usable);
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003119 return res;
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003120}
3121
3122Py_ssize_t
3123_PyDict_KeysSize(PyDictKeysObject *keys)
3124{
Victor Stinner98ee9d52016-09-08 09:33:56 -07003125 return (sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003126 + DK_IXSIZE(keys) * DK_SIZE(keys)
3127 + USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry));
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003128}
3129
doko@ubuntu.com17210f52016-01-14 14:04:59 +01003130static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303131dict_sizeof(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003132{
3133 return PyLong_FromSsize_t(_PyDict_SizeOf(mp));
3134}
3135
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00003136PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
3137
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003138PyDoc_STRVAR(sizeof__doc__,
3139"D.__sizeof__() -> size of D in memory, in bytes");
3140
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003141PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00003142"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00003143If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00003144
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003145PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00003146"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000031472-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003148
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003149PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04003150"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
3151If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
3152If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
3153In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00003154
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003155PyDoc_STRVAR(clear__doc__,
3156"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003157
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003158PyDoc_STRVAR(copy__doc__,
3159"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00003160
Guido van Rossumb90c8482007-02-10 01:11:45 +00003161/* Forward */
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303162static PyObject *dictkeys_new(PyObject *, PyObject *);
3163static PyObject *dictitems_new(PyObject *, PyObject *);
3164static PyObject *dictvalues_new(PyObject *, PyObject *);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003165
Guido van Rossum45c85d12007-07-27 16:31:40 +00003166PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003167 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003168PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003169 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003170PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003171 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00003172
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003173static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07003174 DICT___CONTAINS___METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003175 {"__getitem__", (PyCFunction)(void(*)(void))dict_subscript, METH_O | METH_COEXIST,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003176 getitem__doc__},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003177 {"__sizeof__", (PyCFunction)(void(*)(void))dict_sizeof, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003178 sizeof__doc__},
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003179 DICT_GET_METHODDEF
3180 DICT_SETDEFAULT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003181 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
3182 pop__doc__},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003183 {"popitem", (PyCFunction)(void(*)(void))dict_popitem, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003184 popitem__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303185 {"keys", dictkeys_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003186 keys__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303187 {"items", dictitems_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003188 items__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303189 {"values", dictvalues_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003190 values__doc__},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003191 {"update", (PyCFunction)(void(*)(void))dict_update, METH_VARARGS | METH_KEYWORDS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003192 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08003193 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003194 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
3195 clear__doc__},
3196 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
3197 copy__doc__},
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003198 DICT___REVERSED___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003199 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003200};
3201
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00003202/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00003203int
3204PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003205{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003206 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07003207 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003208 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003209 PyObject *value;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003211 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003212 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003213 hash = PyObject_Hash(key);
3214 if (hash == -1)
3215 return -1;
3216 }
INADA Naoki778928b2017-08-03 23:45:15 +09003217 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003218 if (ix == DKIX_ERROR)
3219 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003220 return (ix != DKIX_EMPTY && value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003221}
3222
Thomas Wouterscf297e42007-02-23 15:07:44 +00003223/* Internal version of PyDict_Contains used when the hash value is already known */
3224int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003225_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00003226{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003227 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003228 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -07003229 Py_ssize_t ix;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003230
INADA Naoki778928b2017-08-03 23:45:15 +09003231 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003232 if (ix == DKIX_ERROR)
3233 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003234 return (ix != DKIX_EMPTY && value != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00003235}
3236
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003237/* Hack to implement "key in dict" */
3238static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003239 0, /* sq_length */
3240 0, /* sq_concat */
3241 0, /* sq_repeat */
3242 0, /* sq_item */
3243 0, /* sq_slice */
3244 0, /* sq_ass_item */
3245 0, /* sq_ass_slice */
3246 PyDict_Contains, /* sq_contains */
3247 0, /* sq_inplace_concat */
3248 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003249};
3250
Guido van Rossum09e563a2001-05-01 12:10:21 +00003251static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003252dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3253{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003254 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003255 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003257 assert(type != NULL && type->tp_alloc != NULL);
3258 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02003259 if (self == NULL)
3260 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003261 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003262
Victor Stinnera9f61a52013-07-16 22:17:26 +02003263 /* The object has been implicitly tracked by tp_alloc */
3264 if (type == &PyDict_Type)
3265 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003266
3267 d->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003268 d->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07003269 d->ma_keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003270 if (d->ma_keys == NULL) {
3271 Py_DECREF(self);
3272 return NULL;
3273 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02003274 assert(_PyDict_CheckConsistency(d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003275 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003276}
3277
Tim Peters25786c02001-09-02 08:22:48 +00003278static int
3279dict_init(PyObject *self, PyObject *args, PyObject *kwds)
3280{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003281 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00003282}
3283
Tim Peters6d6c1a32001-08-02 04:15:00 +00003284static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003285dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00003286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003287 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003288}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003289
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003290PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00003291"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00003292"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003293" (key, value) pairs\n"
3294"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00003295" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003296" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00003297" d[k] = v\n"
3298"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
3299" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00003300
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003301PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003302 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3303 "dict",
3304 sizeof(PyDictObject),
3305 0,
3306 (destructor)dict_dealloc, /* tp_dealloc */
3307 0, /* tp_print */
3308 0, /* tp_getattr */
3309 0, /* tp_setattr */
3310 0, /* tp_reserved */
3311 (reprfunc)dict_repr, /* tp_repr */
3312 0, /* tp_as_number */
3313 &dict_as_sequence, /* tp_as_sequence */
3314 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003315 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003316 0, /* tp_call */
3317 0, /* tp_str */
3318 PyObject_GenericGetAttr, /* tp_getattro */
3319 0, /* tp_setattro */
3320 0, /* tp_as_buffer */
3321 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3322 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
3323 dictionary_doc, /* tp_doc */
3324 dict_traverse, /* tp_traverse */
3325 dict_tp_clear, /* tp_clear */
3326 dict_richcompare, /* tp_richcompare */
3327 0, /* tp_weaklistoffset */
3328 (getiterfunc)dict_iter, /* tp_iter */
3329 0, /* tp_iternext */
3330 mapp_methods, /* tp_methods */
3331 0, /* tp_members */
3332 0, /* tp_getset */
3333 0, /* tp_base */
3334 0, /* tp_dict */
3335 0, /* tp_descr_get */
3336 0, /* tp_descr_set */
3337 0, /* tp_dictoffset */
3338 dict_init, /* tp_init */
3339 PyType_GenericAlloc, /* tp_alloc */
3340 dict_new, /* tp_new */
3341 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003342};
3343
Victor Stinner3c1e4812012-03-26 22:10:51 +02003344PyObject *
3345_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
3346{
3347 PyObject *kv;
3348 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02003349 if (kv == NULL) {
3350 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003351 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02003352 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02003353 return PyDict_GetItem(dp, kv);
3354}
3355
Guido van Rossum3cca2451997-05-16 14:23:33 +00003356/* For backward compatibility with old dictionary interface */
3357
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003358PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003359PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003361 PyObject *kv, *rv;
3362 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003363 if (kv == NULL) {
3364 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003365 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003366 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003367 rv = PyDict_GetItem(v, kv);
3368 Py_DECREF(kv);
3369 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003370}
3371
3372int
Victor Stinner3c1e4812012-03-26 22:10:51 +02003373_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
3374{
3375 PyObject *kv;
3376 kv = _PyUnicode_FromId(key); /* borrowed */
3377 if (kv == NULL)
3378 return -1;
3379 return PyDict_SetItem(v, kv, item);
3380}
3381
3382int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003383PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003384{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003385 PyObject *kv;
3386 int err;
3387 kv = PyUnicode_FromString(key);
3388 if (kv == NULL)
3389 return -1;
3390 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
3391 err = PyDict_SetItem(v, kv, item);
3392 Py_DECREF(kv);
3393 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003394}
3395
3396int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01003397_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
3398{
3399 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
3400 if (kv == NULL)
3401 return -1;
3402 return PyDict_DelItem(v, kv);
3403}
3404
3405int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003406PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003407{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003408 PyObject *kv;
3409 int err;
3410 kv = PyUnicode_FromString(key);
3411 if (kv == NULL)
3412 return -1;
3413 err = PyDict_DelItem(v, kv);
3414 Py_DECREF(kv);
3415 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003416}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003417
Raymond Hettinger019a1482004-03-18 02:41:19 +00003418/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003419
3420typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003421 PyObject_HEAD
3422 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
3423 Py_ssize_t di_used;
3424 Py_ssize_t di_pos;
3425 PyObject* di_result; /* reusable result tuple for iteritems */
3426 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003427} dictiterobject;
3428
3429static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003430dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003431{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003432 dictiterobject *di;
3433 di = PyObject_GC_New(dictiterobject, itertype);
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003434 if (di == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003435 return NULL;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003436 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003437 Py_INCREF(dict);
3438 di->di_dict = dict;
3439 di->di_used = dict->ma_used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003440 di->len = dict->ma_used;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003441 if ((itertype == &PyDictRevIterKey_Type ||
3442 itertype == &PyDictRevIterItem_Type ||
3443 itertype == &PyDictRevIterValue_Type) && dict->ma_used) {
3444 di->di_pos = dict->ma_keys->dk_nentries - 1;
3445 }
3446 else {
3447 di->di_pos = 0;
3448 }
3449 if (itertype == &PyDictIterItem_Type ||
3450 itertype == &PyDictRevIterItem_Type) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003451 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
3452 if (di->di_result == NULL) {
3453 Py_DECREF(di);
3454 return NULL;
3455 }
3456 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003457 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003458 di->di_result = NULL;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003459 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003460 _PyObject_GC_TRACK(di);
3461 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003462}
3463
3464static void
3465dictiter_dealloc(dictiterobject *di)
3466{
INADA Naokia6296d32017-08-24 14:55:17 +09003467 /* bpo-31095: UnTrack is needed before calling any callbacks */
3468 _PyObject_GC_UNTRACK(di);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003469 Py_XDECREF(di->di_dict);
3470 Py_XDECREF(di->di_result);
3471 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003472}
3473
3474static int
3475dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
3476{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003477 Py_VISIT(di->di_dict);
3478 Py_VISIT(di->di_result);
3479 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003480}
3481
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003482static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303483dictiter_len(dictiterobject *di, PyObject *Py_UNUSED(ignored))
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003484{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003485 Py_ssize_t len = 0;
3486 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
3487 len = di->len;
3488 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003489}
3490
Guido van Rossumb90c8482007-02-10 01:11:45 +00003491PyDoc_STRVAR(length_hint_doc,
3492 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003493
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003494static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303495dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003496
3497PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3498
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003499static PyMethodDef dictiter_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003500 {"__length_hint__", (PyCFunction)(void(*)(void))dictiter_len, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003501 length_hint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003502 {"__reduce__", (PyCFunction)(void(*)(void))dictiter_reduce, METH_NOARGS,
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003503 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003504 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003505};
3506
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003507static PyObject*
3508dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00003509{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003510 PyObject *key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003511 Py_ssize_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003512 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003513 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00003514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003515 if (d == NULL)
3516 return NULL;
3517 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00003518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003519 if (di->di_used != d->ma_used) {
3520 PyErr_SetString(PyExc_RuntimeError,
3521 "dictionary changed size during iteration");
3522 di->di_used = -1; /* Make this state sticky */
3523 return NULL;
3524 }
Guido van Rossum2147df72002-07-16 20:30:22 +00003525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003526 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003527 k = d->ma_keys;
INADA Naokica2d8be2016-11-04 16:59:10 +09003528 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003529 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003530 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003531 goto fail;
3532 key = DK_ENTRIES(k)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003533 assert(d->ma_values[i] != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003534 }
3535 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003536 Py_ssize_t n = k->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003537 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
3538 while (i < n && entry_ptr->me_value == NULL) {
3539 entry_ptr++;
3540 i++;
3541 }
3542 if (i >= n)
3543 goto fail;
3544 key = entry_ptr->me_key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003545 }
Thomas Perl796cc6e2019-03-28 07:03:25 +01003546 // We found an element (key), but did not expect it
3547 if (di->len == 0) {
3548 PyErr_SetString(PyExc_RuntimeError,
3549 "dictionary keys changed during iteration");
3550 goto fail;
3551 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003552 di->di_pos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003553 di->len--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003554 Py_INCREF(key);
3555 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003556
3557fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003558 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003559 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003560 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003561}
3562
Raymond Hettinger019a1482004-03-18 02:41:19 +00003563PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003564 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3565 "dict_keyiterator", /* tp_name */
3566 sizeof(dictiterobject), /* tp_basicsize */
3567 0, /* tp_itemsize */
3568 /* methods */
3569 (destructor)dictiter_dealloc, /* tp_dealloc */
3570 0, /* tp_print */
3571 0, /* tp_getattr */
3572 0, /* tp_setattr */
3573 0, /* tp_reserved */
3574 0, /* tp_repr */
3575 0, /* tp_as_number */
3576 0, /* tp_as_sequence */
3577 0, /* tp_as_mapping */
3578 0, /* tp_hash */
3579 0, /* tp_call */
3580 0, /* tp_str */
3581 PyObject_GenericGetAttr, /* tp_getattro */
3582 0, /* tp_setattro */
3583 0, /* tp_as_buffer */
3584 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3585 0, /* tp_doc */
3586 (traverseproc)dictiter_traverse, /* tp_traverse */
3587 0, /* tp_clear */
3588 0, /* tp_richcompare */
3589 0, /* tp_weaklistoffset */
3590 PyObject_SelfIter, /* tp_iter */
3591 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
3592 dictiter_methods, /* tp_methods */
3593 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003594};
3595
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003596static PyObject *
3597dictiter_iternextvalue(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003598{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003599 PyObject *value;
INADA Naokica2d8be2016-11-04 16:59:10 +09003600 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003601 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003603 if (d == NULL)
3604 return NULL;
3605 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003607 if (di->di_used != d->ma_used) {
3608 PyErr_SetString(PyExc_RuntimeError,
3609 "dictionary changed size during iteration");
3610 di->di_used = -1; /* Make this state sticky */
3611 return NULL;
3612 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003614 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003615 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003616 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003617 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003618 goto fail;
INADA Naokica2d8be2016-11-04 16:59:10 +09003619 value = d->ma_values[i];
3620 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003621 }
3622 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003623 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003624 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3625 while (i < n && entry_ptr->me_value == NULL) {
3626 entry_ptr++;
3627 i++;
3628 }
3629 if (i >= n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003630 goto fail;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003631 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003632 }
3633 di->di_pos = i+1;
3634 di->len--;
3635 Py_INCREF(value);
3636 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003637
3638fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003639 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003640 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003641 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003642}
3643
3644PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003645 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3646 "dict_valueiterator", /* tp_name */
3647 sizeof(dictiterobject), /* tp_basicsize */
3648 0, /* tp_itemsize */
3649 /* methods */
3650 (destructor)dictiter_dealloc, /* tp_dealloc */
3651 0, /* tp_print */
3652 0, /* tp_getattr */
3653 0, /* tp_setattr */
3654 0, /* tp_reserved */
3655 0, /* tp_repr */
3656 0, /* tp_as_number */
3657 0, /* tp_as_sequence */
3658 0, /* tp_as_mapping */
3659 0, /* tp_hash */
3660 0, /* tp_call */
3661 0, /* tp_str */
3662 PyObject_GenericGetAttr, /* tp_getattro */
3663 0, /* tp_setattro */
3664 0, /* tp_as_buffer */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003665 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003666 0, /* tp_doc */
3667 (traverseproc)dictiter_traverse, /* tp_traverse */
3668 0, /* tp_clear */
3669 0, /* tp_richcompare */
3670 0, /* tp_weaklistoffset */
3671 PyObject_SelfIter, /* tp_iter */
3672 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3673 dictiter_methods, /* tp_methods */
3674 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003675};
3676
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003677static PyObject *
3678dictiter_iternextitem(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003679{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003680 PyObject *key, *value, *result;
INADA Naokica2d8be2016-11-04 16:59:10 +09003681 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003682 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003684 if (d == NULL)
3685 return NULL;
3686 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003687
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003688 if (di->di_used != d->ma_used) {
3689 PyErr_SetString(PyExc_RuntimeError,
3690 "dictionary changed size during iteration");
3691 di->di_used = -1; /* Make this state sticky */
3692 return NULL;
3693 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003694
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003695 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003696 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003697 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003698 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003699 goto fail;
3700 key = DK_ENTRIES(d->ma_keys)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003701 value = d->ma_values[i];
3702 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003703 }
3704 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003705 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003706 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3707 while (i < n && entry_ptr->me_value == NULL) {
3708 entry_ptr++;
3709 i++;
3710 }
3711 if (i >= n)
3712 goto fail;
3713 key = entry_ptr->me_key;
3714 value = entry_ptr->me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003715 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003716 di->di_pos = i+1;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003717 di->len--;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003718 Py_INCREF(key);
3719 Py_INCREF(value);
3720 result = di->di_result;
3721 if (Py_REFCNT(result) == 1) {
3722 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3723 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3724 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3725 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003726 Py_INCREF(result);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003727 Py_DECREF(oldkey);
3728 Py_DECREF(oldvalue);
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003729 }
3730 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003731 result = PyTuple_New(2);
3732 if (result == NULL)
3733 return NULL;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003734 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3735 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003736 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003737 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003738
3739fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003740 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003741 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003742 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003743}
3744
3745PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003746 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3747 "dict_itemiterator", /* tp_name */
3748 sizeof(dictiterobject), /* tp_basicsize */
3749 0, /* tp_itemsize */
3750 /* methods */
3751 (destructor)dictiter_dealloc, /* tp_dealloc */
3752 0, /* tp_print */
3753 0, /* tp_getattr */
3754 0, /* tp_setattr */
3755 0, /* tp_reserved */
3756 0, /* tp_repr */
3757 0, /* tp_as_number */
3758 0, /* tp_as_sequence */
3759 0, /* tp_as_mapping */
3760 0, /* tp_hash */
3761 0, /* tp_call */
3762 0, /* tp_str */
3763 PyObject_GenericGetAttr, /* tp_getattro */
3764 0, /* tp_setattro */
3765 0, /* tp_as_buffer */
3766 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3767 0, /* tp_doc */
3768 (traverseproc)dictiter_traverse, /* tp_traverse */
3769 0, /* tp_clear */
3770 0, /* tp_richcompare */
3771 0, /* tp_weaklistoffset */
3772 PyObject_SelfIter, /* tp_iter */
3773 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3774 dictiter_methods, /* tp_methods */
3775 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003776};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003777
3778
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003779/* dictreviter */
3780
3781static PyObject *
3782dictreviter_iternext(dictiterobject *di)
3783{
3784 PyDictObject *d = di->di_dict;
3785
3786 if (d == NULL) {
3787 return NULL;
3788 }
3789 assert (PyDict_Check(d));
3790
3791 if (di->di_used != d->ma_used) {
3792 PyErr_SetString(PyExc_RuntimeError,
3793 "dictionary changed size during iteration");
3794 di->di_used = -1; /* Make this state sticky */
3795 return NULL;
3796 }
3797
3798 Py_ssize_t i = di->di_pos;
3799 PyDictKeysObject *k = d->ma_keys;
3800 PyObject *key, *value, *result;
3801
3802 if (d->ma_values) {
3803 if (i < 0) {
3804 goto fail;
3805 }
3806 key = DK_ENTRIES(k)[i].me_key;
3807 value = d->ma_values[i];
3808 assert (value != NULL);
3809 }
3810 else {
3811 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
3812 while (i >= 0 && entry_ptr->me_value == NULL) {
3813 entry_ptr--;
3814 i--;
3815 }
3816 if (i < 0) {
3817 goto fail;
3818 }
3819 key = entry_ptr->me_key;
3820 value = entry_ptr->me_value;
3821 }
3822 di->di_pos = i-1;
3823 di->len--;
3824
3825 if (Py_TYPE(di) == &PyDictRevIterKey_Type) {
3826 Py_INCREF(key);
3827 return key;
3828 }
3829 else if (Py_TYPE(di) == &PyDictRevIterValue_Type) {
3830 Py_INCREF(value);
3831 return value;
3832 }
3833 else if (Py_TYPE(di) == &PyDictRevIterItem_Type) {
3834 Py_INCREF(key);
3835 Py_INCREF(value);
3836 result = di->di_result;
3837 if (Py_REFCNT(result) == 1) {
3838 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3839 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3840 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3841 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
3842 Py_INCREF(result);
3843 Py_DECREF(oldkey);
3844 Py_DECREF(oldvalue);
3845 }
3846 else {
3847 result = PyTuple_New(2);
3848 if (result == NULL) {
3849 return NULL;
3850 }
3851 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3852 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
3853 }
3854 return result;
3855 }
3856 else {
3857 Py_UNREACHABLE();
3858 }
3859
3860fail:
3861 di->di_dict = NULL;
3862 Py_DECREF(d);
3863 return NULL;
3864}
3865
3866PyTypeObject PyDictRevIterKey_Type = {
3867 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3868 "dict_reversekeyiterator",
3869 sizeof(dictiterobject),
3870 .tp_dealloc = (destructor)dictiter_dealloc,
3871 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
3872 .tp_traverse = (traverseproc)dictiter_traverse,
3873 .tp_iter = PyObject_SelfIter,
3874 .tp_iternext = (iternextfunc)dictreviter_iternext,
3875 .tp_methods = dictiter_methods
3876};
3877
3878
3879/*[clinic input]
3880dict.__reversed__
3881
3882Return a reverse iterator over the dict keys.
3883[clinic start generated code]*/
3884
3885static PyObject *
3886dict___reversed___impl(PyDictObject *self)
3887/*[clinic end generated code: output=e674483336d1ed51 input=23210ef3477d8c4d]*/
3888{
3889 assert (PyDict_Check(self));
3890 return dictiter_new(self, &PyDictRevIterKey_Type);
3891}
3892
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003893static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303894dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003895{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003896 _Py_IDENTIFIER(iter);
Sergey Fedoseev63958442018-10-20 05:43:33 +05003897 /* copy the iterator state */
3898 dictiterobject tmp = *di;
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003899 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003900
Sergey Fedoseev63958442018-10-20 05:43:33 +05003901 PyObject *list = PySequence_List((PyObject*)&tmp);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003902 Py_XDECREF(tmp.di_dict);
Sergey Fedoseev63958442018-10-20 05:43:33 +05003903 if (list == NULL) {
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003904 return NULL;
3905 }
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003906 return Py_BuildValue("N(N)", _PyEval_GetBuiltinId(&PyId_iter), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003907}
3908
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003909PyTypeObject PyDictRevIterItem_Type = {
3910 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3911 "dict_reverseitemiterator",
3912 sizeof(dictiterobject),
3913 .tp_dealloc = (destructor)dictiter_dealloc,
3914 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
3915 .tp_traverse = (traverseproc)dictiter_traverse,
3916 .tp_iter = PyObject_SelfIter,
3917 .tp_iternext = (iternextfunc)dictreviter_iternext,
3918 .tp_methods = dictiter_methods
3919};
3920
3921PyTypeObject PyDictRevIterValue_Type = {
3922 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3923 "dict_reversevalueiterator",
3924 sizeof(dictiterobject),
3925 .tp_dealloc = (destructor)dictiter_dealloc,
3926 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
3927 .tp_traverse = (traverseproc)dictiter_traverse,
3928 .tp_iter = PyObject_SelfIter,
3929 .tp_iternext = (iternextfunc)dictreviter_iternext,
3930 .tp_methods = dictiter_methods
3931};
3932
Guido van Rossum3ac67412007-02-10 18:55:06 +00003933/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003934/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003935/***********************************************/
3936
Guido van Rossumb90c8482007-02-10 01:11:45 +00003937/* The instance lay-out is the same for all three; but the type differs. */
3938
Guido van Rossumb90c8482007-02-10 01:11:45 +00003939static void
Eric Snow96c6af92015-05-29 22:21:39 -06003940dictview_dealloc(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003941{
INADA Naokia6296d32017-08-24 14:55:17 +09003942 /* bpo-31095: UnTrack is needed before calling any callbacks */
3943 _PyObject_GC_UNTRACK(dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003944 Py_XDECREF(dv->dv_dict);
3945 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003946}
3947
3948static int
Eric Snow96c6af92015-05-29 22:21:39 -06003949dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003950{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003951 Py_VISIT(dv->dv_dict);
3952 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003953}
3954
Guido van Rossum83825ac2007-02-10 04:54:19 +00003955static Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003956dictview_len(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003957{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003958 Py_ssize_t len = 0;
3959 if (dv->dv_dict != NULL)
3960 len = dv->dv_dict->ma_used;
3961 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003962}
3963
Eric Snow96c6af92015-05-29 22:21:39 -06003964PyObject *
3965_PyDictView_New(PyObject *dict, PyTypeObject *type)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003966{
Eric Snow96c6af92015-05-29 22:21:39 -06003967 _PyDictViewObject *dv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003968 if (dict == NULL) {
3969 PyErr_BadInternalCall();
3970 return NULL;
3971 }
3972 if (!PyDict_Check(dict)) {
3973 /* XXX Get rid of this restriction later */
3974 PyErr_Format(PyExc_TypeError,
3975 "%s() requires a dict argument, not '%s'",
3976 type->tp_name, dict->ob_type->tp_name);
3977 return NULL;
3978 }
Eric Snow96c6af92015-05-29 22:21:39 -06003979 dv = PyObject_GC_New(_PyDictViewObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003980 if (dv == NULL)
3981 return NULL;
3982 Py_INCREF(dict);
3983 dv->dv_dict = (PyDictObject *)dict;
3984 _PyObject_GC_TRACK(dv);
3985 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003986}
3987
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003988/* TODO(guido): The views objects are not complete:
3989
3990 * support more set operations
3991 * support arbitrary mappings?
3992 - either these should be static or exported in dictobject.h
3993 - if public then they should probably be in builtins
3994*/
3995
Guido van Rossumaac530c2007-08-24 22:33:45 +00003996/* Return 1 if self is a subset of other, iterating over self;
3997 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003998static int
3999all_contained_in(PyObject *self, PyObject *other)
4000{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004001 PyObject *iter = PyObject_GetIter(self);
4002 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00004003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004004 if (iter == NULL)
4005 return -1;
4006 for (;;) {
4007 PyObject *next = PyIter_Next(iter);
4008 if (next == NULL) {
4009 if (PyErr_Occurred())
4010 ok = -1;
4011 break;
4012 }
4013 ok = PySequence_Contains(other, next);
4014 Py_DECREF(next);
4015 if (ok <= 0)
4016 break;
4017 }
4018 Py_DECREF(iter);
4019 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00004020}
4021
4022static PyObject *
4023dictview_richcompare(PyObject *self, PyObject *other, int op)
4024{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004025 Py_ssize_t len_self, len_other;
4026 int ok;
4027 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004029 assert(self != NULL);
4030 assert(PyDictViewSet_Check(self));
4031 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00004032
Brian Curtindfc80e32011-08-10 20:28:54 -05004033 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
4034 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004035
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004036 len_self = PyObject_Size(self);
4037 if (len_self < 0)
4038 return NULL;
4039 len_other = PyObject_Size(other);
4040 if (len_other < 0)
4041 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004043 ok = 0;
4044 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00004045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004046 case Py_NE:
4047 case Py_EQ:
4048 if (len_self == len_other)
4049 ok = all_contained_in(self, other);
4050 if (op == Py_NE && ok >= 0)
4051 ok = !ok;
4052 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004054 case Py_LT:
4055 if (len_self < len_other)
4056 ok = all_contained_in(self, other);
4057 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004059 case Py_LE:
4060 if (len_self <= len_other)
4061 ok = all_contained_in(self, other);
4062 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004064 case Py_GT:
4065 if (len_self > len_other)
4066 ok = all_contained_in(other, self);
4067 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004068
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004069 case Py_GE:
4070 if (len_self >= len_other)
4071 ok = all_contained_in(other, self);
4072 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004074 }
4075 if (ok < 0)
4076 return NULL;
4077 result = ok ? Py_True : Py_False;
4078 Py_INCREF(result);
4079 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00004080}
4081
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004082static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004083dictview_repr(_PyDictViewObject *dv)
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004084{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004085 PyObject *seq;
bennorthd7773d92018-01-26 15:46:01 +00004086 PyObject *result = NULL;
4087 Py_ssize_t rc;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004088
bennorthd7773d92018-01-26 15:46:01 +00004089 rc = Py_ReprEnter((PyObject *)dv);
4090 if (rc != 0) {
4091 return rc > 0 ? PyUnicode_FromString("...") : NULL;
4092 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004093 seq = PySequence_List((PyObject *)dv);
bennorthd7773d92018-01-26 15:46:01 +00004094 if (seq == NULL) {
4095 goto Done;
4096 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004097 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
4098 Py_DECREF(seq);
bennorthd7773d92018-01-26 15:46:01 +00004099
4100Done:
4101 Py_ReprLeave((PyObject *)dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004102 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004103}
4104
Guido van Rossum3ac67412007-02-10 18:55:06 +00004105/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004106
4107static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004108dictkeys_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004109{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004110 if (dv->dv_dict == NULL) {
4111 Py_RETURN_NONE;
4112 }
4113 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004114}
4115
4116static int
Eric Snow96c6af92015-05-29 22:21:39 -06004117dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004118{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004119 if (dv->dv_dict == NULL)
4120 return 0;
4121 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004122}
4123
Guido van Rossum83825ac2007-02-10 04:54:19 +00004124static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004125 (lenfunc)dictview_len, /* sq_length */
4126 0, /* sq_concat */
4127 0, /* sq_repeat */
4128 0, /* sq_item */
4129 0, /* sq_slice */
4130 0, /* sq_ass_item */
4131 0, /* sq_ass_slice */
4132 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004133};
4134
Guido van Rossum523259b2007-08-24 23:41:22 +00004135static PyObject*
4136dictviews_sub(PyObject* self, PyObject *other)
4137{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004138 PyObject *result = PySet_New(self);
4139 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004140 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004142 if (result == NULL)
4143 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004144
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08004145 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004146 if (tmp == NULL) {
4147 Py_DECREF(result);
4148 return NULL;
4149 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004151 Py_DECREF(tmp);
4152 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004153}
4154
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004155PyObject*
4156_PyDictView_Intersect(PyObject* self, PyObject *other)
Guido van Rossum523259b2007-08-24 23:41:22 +00004157{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004158 PyObject *result = PySet_New(self);
4159 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004160 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004162 if (result == NULL)
4163 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004164
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08004165 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_intersection_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004166 if (tmp == NULL) {
4167 Py_DECREF(result);
4168 return NULL;
4169 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004171 Py_DECREF(tmp);
4172 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004173}
4174
4175static PyObject*
4176dictviews_or(PyObject* self, PyObject *other)
4177{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004178 PyObject *result = PySet_New(self);
4179 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004180 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02004181
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004182 if (result == NULL)
4183 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004184
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08004185 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004186 if (tmp == NULL) {
4187 Py_DECREF(result);
4188 return NULL;
4189 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004191 Py_DECREF(tmp);
4192 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004193}
4194
4195static PyObject*
4196dictviews_xor(PyObject* self, PyObject *other)
4197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004198 PyObject *result = PySet_New(self);
4199 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004200 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004202 if (result == NULL)
4203 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004204
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08004205 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_symmetric_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004206 if (tmp == NULL) {
4207 Py_DECREF(result);
4208 return NULL;
4209 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004211 Py_DECREF(tmp);
4212 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004213}
4214
4215static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004216 0, /*nb_add*/
4217 (binaryfunc)dictviews_sub, /*nb_subtract*/
4218 0, /*nb_multiply*/
4219 0, /*nb_remainder*/
4220 0, /*nb_divmod*/
4221 0, /*nb_power*/
4222 0, /*nb_negative*/
4223 0, /*nb_positive*/
4224 0, /*nb_absolute*/
4225 0, /*nb_bool*/
4226 0, /*nb_invert*/
4227 0, /*nb_lshift*/
4228 0, /*nb_rshift*/
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004229 (binaryfunc)_PyDictView_Intersect, /*nb_and*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004230 (binaryfunc)dictviews_xor, /*nb_xor*/
4231 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00004232};
4233
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004234static PyObject*
4235dictviews_isdisjoint(PyObject *self, PyObject *other)
4236{
4237 PyObject *it;
4238 PyObject *item = NULL;
4239
4240 if (self == other) {
Eric Snow96c6af92015-05-29 22:21:39 -06004241 if (dictview_len((_PyDictViewObject *)self) == 0)
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004242 Py_RETURN_TRUE;
4243 else
4244 Py_RETURN_FALSE;
4245 }
4246
4247 /* Iterate over the shorter object (only if other is a set,
4248 * because PySequence_Contains may be expensive otherwise): */
4249 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
Eric Snow96c6af92015-05-29 22:21:39 -06004250 Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self);
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004251 Py_ssize_t len_other = PyObject_Size(other);
4252 if (len_other == -1)
4253 return NULL;
4254
4255 if ((len_other > len_self)) {
4256 PyObject *tmp = other;
4257 other = self;
4258 self = tmp;
4259 }
4260 }
4261
4262 it = PyObject_GetIter(other);
4263 if (it == NULL)
4264 return NULL;
4265
4266 while ((item = PyIter_Next(it)) != NULL) {
4267 int contains = PySequence_Contains(self, item);
4268 Py_DECREF(item);
4269 if (contains == -1) {
4270 Py_DECREF(it);
4271 return NULL;
4272 }
4273
4274 if (contains) {
4275 Py_DECREF(it);
4276 Py_RETURN_FALSE;
4277 }
4278 }
4279 Py_DECREF(it);
4280 if (PyErr_Occurred())
4281 return NULL; /* PyIter_Next raised an exception. */
4282 Py_RETURN_TRUE;
4283}
4284
4285PyDoc_STRVAR(isdisjoint_doc,
4286"Return True if the view and the given iterable have a null intersection.");
4287
Serhiy Storchaka81524022018-11-27 13:05:02 +02004288static PyObject* dictkeys_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored));
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004289
4290PyDoc_STRVAR(reversed_keys_doc,
4291"Return a reverse iterator over the dict keys.");
4292
Guido van Rossumb90c8482007-02-10 01:11:45 +00004293static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004294 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4295 isdisjoint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004296 {"__reversed__", (PyCFunction)(void(*)(void))dictkeys_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004297 reversed_keys_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004298 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004299};
4300
4301PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004302 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4303 "dict_keys", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004304 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004305 0, /* tp_itemsize */
4306 /* methods */
4307 (destructor)dictview_dealloc, /* tp_dealloc */
4308 0, /* tp_print */
4309 0, /* tp_getattr */
4310 0, /* tp_setattr */
4311 0, /* tp_reserved */
4312 (reprfunc)dictview_repr, /* tp_repr */
4313 &dictviews_as_number, /* tp_as_number */
4314 &dictkeys_as_sequence, /* tp_as_sequence */
4315 0, /* tp_as_mapping */
4316 0, /* tp_hash */
4317 0, /* tp_call */
4318 0, /* tp_str */
4319 PyObject_GenericGetAttr, /* tp_getattro */
4320 0, /* tp_setattro */
4321 0, /* tp_as_buffer */
4322 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4323 0, /* tp_doc */
4324 (traverseproc)dictview_traverse, /* tp_traverse */
4325 0, /* tp_clear */
4326 dictview_richcompare, /* tp_richcompare */
4327 0, /* tp_weaklistoffset */
4328 (getiterfunc)dictkeys_iter, /* tp_iter */
4329 0, /* tp_iternext */
4330 dictkeys_methods, /* tp_methods */
4331 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004332};
4333
4334static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304335dictkeys_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004336{
Eric Snow96c6af92015-05-29 22:21:39 -06004337 return _PyDictView_New(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004338}
4339
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004340static PyObject *
Serhiy Storchaka81524022018-11-27 13:05:02 +02004341dictkeys_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored))
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004342{
4343 if (dv->dv_dict == NULL) {
4344 Py_RETURN_NONE;
4345 }
4346 return dictiter_new(dv->dv_dict, &PyDictRevIterKey_Type);
4347}
4348
Guido van Rossum3ac67412007-02-10 18:55:06 +00004349/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004350
4351static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004352dictitems_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004353{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004354 if (dv->dv_dict == NULL) {
4355 Py_RETURN_NONE;
4356 }
4357 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004358}
4359
4360static int
Eric Snow96c6af92015-05-29 22:21:39 -06004361dictitems_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004362{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004363 int result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004364 PyObject *key, *value, *found;
4365 if (dv->dv_dict == NULL)
4366 return 0;
4367 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
4368 return 0;
4369 key = PyTuple_GET_ITEM(obj, 0);
4370 value = PyTuple_GET_ITEM(obj, 1);
Raymond Hettinger6692f012016-09-18 21:46:08 -07004371 found = PyDict_GetItemWithError((PyObject *)dv->dv_dict, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004372 if (found == NULL) {
4373 if (PyErr_Occurred())
4374 return -1;
4375 return 0;
4376 }
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004377 Py_INCREF(found);
4378 result = PyObject_RichCompareBool(value, found, Py_EQ);
4379 Py_DECREF(found);
4380 return result;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004381}
4382
Guido van Rossum83825ac2007-02-10 04:54:19 +00004383static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004384 (lenfunc)dictview_len, /* sq_length */
4385 0, /* sq_concat */
4386 0, /* sq_repeat */
4387 0, /* sq_item */
4388 0, /* sq_slice */
4389 0, /* sq_ass_item */
4390 0, /* sq_ass_slice */
4391 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004392};
4393
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004394static PyObject* dictitems_reversed(_PyDictViewObject *dv);
4395
4396PyDoc_STRVAR(reversed_items_doc,
4397"Return a reverse iterator over the dict items.");
4398
Guido van Rossumb90c8482007-02-10 01:11:45 +00004399static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004400 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4401 isdisjoint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004402 {"__reversed__", (PyCFunction)(void(*)(void))dictitems_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004403 reversed_items_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004404 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004405};
4406
4407PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004408 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4409 "dict_items", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004410 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004411 0, /* tp_itemsize */
4412 /* methods */
4413 (destructor)dictview_dealloc, /* tp_dealloc */
4414 0, /* tp_print */
4415 0, /* tp_getattr */
4416 0, /* tp_setattr */
4417 0, /* tp_reserved */
4418 (reprfunc)dictview_repr, /* tp_repr */
4419 &dictviews_as_number, /* tp_as_number */
4420 &dictitems_as_sequence, /* tp_as_sequence */
4421 0, /* tp_as_mapping */
4422 0, /* tp_hash */
4423 0, /* tp_call */
4424 0, /* tp_str */
4425 PyObject_GenericGetAttr, /* tp_getattro */
4426 0, /* tp_setattro */
4427 0, /* tp_as_buffer */
4428 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4429 0, /* tp_doc */
4430 (traverseproc)dictview_traverse, /* tp_traverse */
4431 0, /* tp_clear */
4432 dictview_richcompare, /* tp_richcompare */
4433 0, /* tp_weaklistoffset */
4434 (getiterfunc)dictitems_iter, /* tp_iter */
4435 0, /* tp_iternext */
4436 dictitems_methods, /* tp_methods */
4437 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004438};
4439
4440static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304441dictitems_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004442{
Eric Snow96c6af92015-05-29 22:21:39 -06004443 return _PyDictView_New(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004444}
4445
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004446static PyObject *
4447dictitems_reversed(_PyDictViewObject *dv)
4448{
4449 if (dv->dv_dict == NULL) {
4450 Py_RETURN_NONE;
4451 }
4452 return dictiter_new(dv->dv_dict, &PyDictRevIterItem_Type);
4453}
4454
Guido van Rossum3ac67412007-02-10 18:55:06 +00004455/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004456
4457static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004458dictvalues_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004459{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004460 if (dv->dv_dict == NULL) {
4461 Py_RETURN_NONE;
4462 }
4463 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004464}
4465
Guido van Rossum83825ac2007-02-10 04:54:19 +00004466static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004467 (lenfunc)dictview_len, /* sq_length */
4468 0, /* sq_concat */
4469 0, /* sq_repeat */
4470 0, /* sq_item */
4471 0, /* sq_slice */
4472 0, /* sq_ass_item */
4473 0, /* sq_ass_slice */
4474 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004475};
4476
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004477static PyObject* dictvalues_reversed(_PyDictViewObject *dv);
4478
4479PyDoc_STRVAR(reversed_values_doc,
4480"Return a reverse iterator over the dict values.");
4481
Guido van Rossumb90c8482007-02-10 01:11:45 +00004482static PyMethodDef dictvalues_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004483 {"__reversed__", (PyCFunction)(void(*)(void))dictvalues_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004484 reversed_values_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004485 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004486};
4487
4488PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004489 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4490 "dict_values", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004491 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004492 0, /* tp_itemsize */
4493 /* methods */
4494 (destructor)dictview_dealloc, /* tp_dealloc */
4495 0, /* tp_print */
4496 0, /* tp_getattr */
4497 0, /* tp_setattr */
4498 0, /* tp_reserved */
4499 (reprfunc)dictview_repr, /* tp_repr */
4500 0, /* tp_as_number */
4501 &dictvalues_as_sequence, /* tp_as_sequence */
4502 0, /* tp_as_mapping */
4503 0, /* tp_hash */
4504 0, /* tp_call */
4505 0, /* tp_str */
4506 PyObject_GenericGetAttr, /* tp_getattro */
4507 0, /* tp_setattro */
4508 0, /* tp_as_buffer */
4509 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4510 0, /* tp_doc */
4511 (traverseproc)dictview_traverse, /* tp_traverse */
4512 0, /* tp_clear */
4513 0, /* tp_richcompare */
4514 0, /* tp_weaklistoffset */
4515 (getiterfunc)dictvalues_iter, /* tp_iter */
4516 0, /* tp_iternext */
4517 dictvalues_methods, /* tp_methods */
4518 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004519};
4520
4521static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304522dictvalues_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004523{
Eric Snow96c6af92015-05-29 22:21:39 -06004524 return _PyDictView_New(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004525}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004526
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004527static PyObject *
4528dictvalues_reversed(_PyDictViewObject *dv)
4529{
4530 if (dv->dv_dict == NULL) {
4531 Py_RETURN_NONE;
4532 }
4533 return dictiter_new(dv->dv_dict, &PyDictRevIterValue_Type);
4534}
4535
4536
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004537/* Returns NULL if cannot allocate a new PyDictKeysObject,
4538 but does not set an error */
4539PyDictKeysObject *
4540_PyDict_NewKeysForClass(void)
4541{
Victor Stinner742da042016-09-07 17:40:12 -07004542 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004543 if (keys == NULL)
4544 PyErr_Clear();
4545 else
4546 keys->dk_lookup = lookdict_split;
4547 return keys;
4548}
4549
4550#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
4551
4552PyObject *
4553PyObject_GenericGetDict(PyObject *obj, void *context)
4554{
4555 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
4556 if (dictptr == NULL) {
4557 PyErr_SetString(PyExc_AttributeError,
4558 "This object has no __dict__");
4559 return NULL;
4560 }
4561 dict = *dictptr;
4562 if (dict == NULL) {
4563 PyTypeObject *tp = Py_TYPE(obj);
4564 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
INADA Naokia7576492018-11-14 18:39:27 +09004565 dictkeys_incref(CACHED_KEYS(tp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004566 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
4567 }
4568 else {
4569 *dictptr = dict = PyDict_New();
4570 }
4571 }
4572 Py_XINCREF(dict);
4573 return dict;
4574}
4575
4576int
4577_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
Victor Stinner742da042016-09-07 17:40:12 -07004578 PyObject *key, PyObject *value)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004579{
4580 PyObject *dict;
4581 int res;
4582 PyDictKeysObject *cached;
4583
4584 assert(dictptr != NULL);
4585 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
4586 assert(dictptr != NULL);
4587 dict = *dictptr;
4588 if (dict == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +09004589 dictkeys_incref(cached);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004590 dict = new_dict_with_shared_keys(cached);
4591 if (dict == NULL)
4592 return -1;
4593 *dictptr = dict;
4594 }
4595 if (value == NULL) {
4596 res = PyDict_DelItem(dict, key);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004597 // Since key sharing dict doesn't allow deletion, PyDict_DelItem()
4598 // always converts dict to combined form.
4599 if ((cached = CACHED_KEYS(tp)) != NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004600 CACHED_KEYS(tp) = NULL;
INADA Naokia7576492018-11-14 18:39:27 +09004601 dictkeys_decref(cached);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004602 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01004603 }
4604 else {
INADA Naoki2294f3a2017-02-12 13:51:30 +09004605 int was_shared = (cached == ((PyDictObject *)dict)->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004606 res = PyDict_SetItem(dict, key, value);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004607 if (was_shared &&
4608 (cached = CACHED_KEYS(tp)) != NULL &&
4609 cached != ((PyDictObject *)dict)->ma_keys) {
Victor Stinner3d3f2642016-12-15 17:21:23 +01004610 /* PyDict_SetItem() may call dictresize and convert split table
4611 * into combined table. In such case, convert it to split
4612 * table again and update type's shared key only when this is
4613 * the only dict sharing key with the type.
4614 *
4615 * This is to allow using shared key in class like this:
4616 *
4617 * class C:
4618 * def __init__(self):
4619 * # one dict resize happens
4620 * self.a, self.b, self.c = 1, 2, 3
4621 * self.d, self.e, self.f = 4, 5, 6
4622 * a = C()
4623 */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004624 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004625 CACHED_KEYS(tp) = make_keys_shared(dict);
Victor Stinner742da042016-09-07 17:40:12 -07004626 }
4627 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004628 CACHED_KEYS(tp) = NULL;
4629 }
INADA Naokia7576492018-11-14 18:39:27 +09004630 dictkeys_decref(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004631 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
4632 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004633 }
4634 }
4635 } else {
4636 dict = *dictptr;
4637 if (dict == NULL) {
4638 dict = PyDict_New();
4639 if (dict == NULL)
4640 return -1;
4641 *dictptr = dict;
4642 }
4643 if (value == NULL) {
4644 res = PyDict_DelItem(dict, key);
4645 } else {
4646 res = PyDict_SetItem(dict, key, value);
4647 }
4648 }
4649 return res;
4650}
4651
4652void
4653_PyDictKeys_DecRef(PyDictKeysObject *keys)
4654{
INADA Naokia7576492018-11-14 18:39:27 +09004655 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004656}