blob: 9b5c0a3be9ab7aabd1335de59fa56e82c40289a9 [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
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200452#ifdef DEBUG_PYDICT
453# define ASSERT_CONSISTENT(op) assert(_PyDict_CheckConsistency((PyObject *)(op), 1))
454#else
455# define ASSERT_CONSISTENT(op) assert(_PyDict_CheckConsistency((PyObject *)(op), 0))
456#endif
Victor Stinner611b0fa2016-09-14 15:02:01 +0200457
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200458
459int
460_PyDict_CheckConsistency(PyObject *op, int check_content)
Victor Stinner611b0fa2016-09-14 15:02:01 +0200461{
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200462 _PyObject_ASSERT(op, PyDict_Check(op));
463 PyDictObject *mp = (PyDictObject *)op;
Victor Stinner50fe3f82018-10-26 18:47:15 +0200464
Victor Stinner611b0fa2016-09-14 15:02:01 +0200465 PyDictKeysObject *keys = mp->ma_keys;
466 int splitted = _PyDict_HasSplitTable(mp);
467 Py_ssize_t usable = USABLE_FRACTION(keys->dk_size);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200468
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200469 _PyObject_ASSERT(op, 0 <= mp->ma_used && mp->ma_used <= usable);
470 _PyObject_ASSERT(op, IS_POWER_OF_2(keys->dk_size));
471 _PyObject_ASSERT(op, 0 <= keys->dk_usable && keys->dk_usable <= usable);
472 _PyObject_ASSERT(op, 0 <= keys->dk_nentries && keys->dk_nentries <= usable);
473 _PyObject_ASSERT(op, keys->dk_usable + keys->dk_nentries <= usable);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200474
475 if (!splitted) {
476 /* combined table */
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200477 _PyObject_ASSERT(op, keys->dk_refcnt == 1);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200478 }
479
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200480 if (check_content) {
481 PyDictKeyEntry *entries = DK_ENTRIES(keys);
482 Py_ssize_t i;
Victor Stinner611b0fa2016-09-14 15:02:01 +0200483
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200484 for (i=0; i < keys->dk_size; i++) {
485 Py_ssize_t ix = dictkeys_get_index(keys, i);
486 _PyObject_ASSERT(op, DKIX_DUMMY <= ix && ix <= usable);
487 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200488
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200489 for (i=0; i < usable; i++) {
490 PyDictKeyEntry *entry = &entries[i];
491 PyObject *key = entry->me_key;
492
493 if (key != NULL) {
494 if (PyUnicode_CheckExact(key)) {
495 Py_hash_t hash = ((PyASCIIObject *)key)->hash;
496 _PyObject_ASSERT(op, hash != -1);
497 _PyObject_ASSERT(op, entry->me_hash == hash);
498 }
499 else {
500 /* test_dict fails if PyObject_Hash() is called again */
501 _PyObject_ASSERT(op, entry->me_hash != -1);
502 }
503 if (!splitted) {
504 _PyObject_ASSERT(op, entry->me_value != NULL);
505 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200506 }
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200507
508 if (splitted) {
509 _PyObject_ASSERT(op, entry->me_value == NULL);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200510 }
511 }
512
513 if (splitted) {
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200514 /* splitted table */
515 for (i=0; i < mp->ma_used; i++) {
516 _PyObject_ASSERT(op, mp->ma_values[i] != NULL);
517 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200518 }
519 }
520
Victor Stinner611b0fa2016-09-14 15:02:01 +0200521 return 1;
522}
Victor Stinner611b0fa2016-09-14 15:02:01 +0200523
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 Stinner0fc91ee2019-04-12 21:51:34 +0200617 ASSERT_CONSISTENT(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;
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200678 ASSERT_CONSISTENT(new);
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500679 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 Stinner0fc91ee2019-04-12 21:51:34 +02001078 ASSERT_CONSISTENT(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 Stinner0fc91ee2019-04-12 21:51:34 +02001097 ASSERT_CONSISTENT(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
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001585 ASSERT_CONSISTENT(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 Stinner0fc91ee2019-04-12 21:51:34 +02001725 ASSERT_CONSISTENT(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
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001855 ASSERT_CONSISTENT(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;
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002150 Py_ssize_t n, offset;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002151 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);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002166 if (mp->ma_values) {
2167 value_ptr = mp->ma_values;
2168 offset = sizeof(PyObject *);
2169 }
2170 else {
2171 value_ptr = &ep[0].me_value;
2172 offset = sizeof(PyDictKeyEntry);
2173 }
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002174 for (i = 0, j = 0; j < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002175 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002176 PyObject *key = ep[i].me_key;
2177 Py_INCREF(key);
2178 PyList_SET_ITEM(v, j, key);
2179 j++;
2180 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002181 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002182 }
2183 assert(j == n);
2184 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002185}
2186
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002187static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002188dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002189{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002190 PyObject *v;
2191 Py_ssize_t i, j;
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002192 PyDictKeyEntry *ep;
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002193 Py_ssize_t n, offset;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002194 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002195
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002196 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 n = mp->ma_used;
2198 v = PyList_New(n);
2199 if (v == NULL)
2200 return NULL;
2201 if (n != mp->ma_used) {
2202 /* Durnit. The allocations caused the dict to resize.
2203 * Just start over, this shouldn't normally happen.
2204 */
2205 Py_DECREF(v);
2206 goto again;
2207 }
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002208 ep = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002209 if (mp->ma_values) {
2210 value_ptr = mp->ma_values;
2211 offset = sizeof(PyObject *);
2212 }
2213 else {
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002214 value_ptr = &ep[0].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002215 offset = sizeof(PyDictKeyEntry);
2216 }
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002217 for (i = 0, j = 0; j < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002218 PyObject *value = *value_ptr;
2219 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2220 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002221 Py_INCREF(value);
2222 PyList_SET_ITEM(v, j, value);
2223 j++;
2224 }
2225 }
2226 assert(j == n);
2227 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002228}
2229
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002230static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002231dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002232{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002233 PyObject *v;
2234 Py_ssize_t i, j, n;
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002235 Py_ssize_t offset;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002236 PyObject *item, *key;
2237 PyDictKeyEntry *ep;
2238 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002240 /* Preallocate the list of tuples, to avoid allocations during
2241 * the loop over the items, which could trigger GC, which
2242 * could resize the dict. :-(
2243 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002244 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002245 n = mp->ma_used;
2246 v = PyList_New(n);
2247 if (v == NULL)
2248 return NULL;
2249 for (i = 0; i < n; i++) {
2250 item = PyTuple_New(2);
2251 if (item == NULL) {
2252 Py_DECREF(v);
2253 return NULL;
2254 }
2255 PyList_SET_ITEM(v, i, item);
2256 }
2257 if (n != mp->ma_used) {
2258 /* Durnit. The allocations caused the dict to resize.
2259 * Just start over, this shouldn't normally happen.
2260 */
2261 Py_DECREF(v);
2262 goto again;
2263 }
2264 /* Nothing we do below makes any function calls. */
Victor Stinner742da042016-09-07 17:40:12 -07002265 ep = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002266 if (mp->ma_values) {
2267 value_ptr = mp->ma_values;
2268 offset = sizeof(PyObject *);
2269 }
2270 else {
2271 value_ptr = &ep[0].me_value;
2272 offset = sizeof(PyDictKeyEntry);
2273 }
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002274 for (i = 0, j = 0; j < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002275 PyObject *value = *value_ptr;
2276 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2277 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 key = ep[i].me_key;
2279 item = PyList_GET_ITEM(v, j);
2280 Py_INCREF(key);
2281 PyTuple_SET_ITEM(item, 0, key);
2282 Py_INCREF(value);
2283 PyTuple_SET_ITEM(item, 1, value);
2284 j++;
2285 }
2286 }
2287 assert(j == n);
2288 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002289}
2290
Larry Hastings5c661892014-01-24 06:17:25 -08002291/*[clinic input]
2292@classmethod
2293dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08002294 iterable: object
2295 value: object=None
2296 /
2297
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002298Create a new dictionary with keys from iterable and values set to value.
Larry Hastings5c661892014-01-24 06:17:25 -08002299[clinic start generated code]*/
2300
Larry Hastings5c661892014-01-24 06:17:25 -08002301static PyObject *
2302dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002303/*[clinic end generated code: output=8fb98e4b10384999 input=382ba4855d0f74c3]*/
Larry Hastings5c661892014-01-24 06:17:25 -08002304{
Eric Snow96c6af92015-05-29 22:21:39 -06002305 return _PyDict_FromKeys((PyObject *)type, iterable, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002306}
2307
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002308static int
Victor Stinner742da042016-09-07 17:40:12 -07002309dict_update_common(PyObject *self, PyObject *args, PyObject *kwds,
2310 const char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002311{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002312 PyObject *arg = NULL;
2313 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002314
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002315 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002316 result = -1;
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002317 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002318 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002319 _Py_IDENTIFIER(keys);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002320 PyObject *func;
2321 if (_PyObject_LookupAttrId(arg, &PyId_keys, &func) < 0) {
2322 result = -1;
2323 }
2324 else if (func != NULL) {
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002325 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002326 result = PyDict_Merge(self, arg, 1);
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002327 }
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002328 else {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002329 result = PyDict_MergeFromSeq2(self, arg, 1);
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002330 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002331 }
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002333 if (result == 0 && kwds != NULL) {
2334 if (PyArg_ValidateKeywordArguments(kwds))
2335 result = PyDict_Merge(self, kwds, 1);
2336 else
2337 result = -1;
2338 }
2339 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002340}
2341
Victor Stinner91f0d4a2017-01-19 12:45:06 +01002342/* Note: dict.update() uses the METH_VARARGS|METH_KEYWORDS calling convention.
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +03002343 Using METH_FASTCALL|METH_KEYWORDS would make dict.update(**dict2) calls
2344 slower, see the issue #29312. */
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002345static PyObject *
2346dict_update(PyObject *self, PyObject *args, PyObject *kwds)
2347{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002348 if (dict_update_common(self, args, kwds, "update") != -1)
2349 Py_RETURN_NONE;
2350 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002351}
2352
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002353/* Update unconditionally replaces existing items.
2354 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00002355 otherwise it leaves existing items unchanged.
2356
2357 PyDict_{Update,Merge} update/merge from a mapping object.
2358
Tim Petersf582b822001-12-11 18:51:08 +00002359 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00002360 producing iterable objects of length 2.
2361*/
2362
Tim Petersf582b822001-12-11 18:51:08 +00002363int
Tim Peters1fc240e2001-10-26 05:06:50 +00002364PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
2365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 PyObject *it; /* iter(seq2) */
2367 Py_ssize_t i; /* index into seq2 of current element */
2368 PyObject *item; /* seq2[i] */
2369 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00002370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002371 assert(d != NULL);
2372 assert(PyDict_Check(d));
2373 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00002374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002375 it = PyObject_GetIter(seq2);
2376 if (it == NULL)
2377 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379 for (i = 0; ; ++i) {
2380 PyObject *key, *value;
2381 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00002382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 fast = NULL;
2384 item = PyIter_Next(it);
2385 if (item == NULL) {
2386 if (PyErr_Occurred())
2387 goto Fail;
2388 break;
2389 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 /* Convert item to sequence, and verify length 2. */
2392 fast = PySequence_Fast(item, "");
2393 if (fast == NULL) {
2394 if (PyErr_ExceptionMatches(PyExc_TypeError))
2395 PyErr_Format(PyExc_TypeError,
2396 "cannot convert dictionary update "
2397 "sequence element #%zd to a sequence",
2398 i);
2399 goto Fail;
2400 }
2401 n = PySequence_Fast_GET_SIZE(fast);
2402 if (n != 2) {
2403 PyErr_Format(PyExc_ValueError,
2404 "dictionary update sequence element #%zd "
2405 "has length %zd; 2 is required",
2406 i, n);
2407 goto Fail;
2408 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002410 /* Update/merge with this (key, value) pair. */
2411 key = PySequence_Fast_GET_ITEM(fast, 0);
2412 value = PySequence_Fast_GET_ITEM(fast, 1);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002413 Py_INCREF(key);
2414 Py_INCREF(value);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002415 if (override) {
2416 if (PyDict_SetItem(d, key, value) < 0) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002417 Py_DECREF(key);
2418 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002419 goto Fail;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002420 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002421 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002422 else if (PyDict_GetItemWithError(d, key) == NULL) {
2423 if (PyErr_Occurred() || PyDict_SetItem(d, key, value) < 0) {
2424 Py_DECREF(key);
2425 Py_DECREF(value);
2426 goto Fail;
2427 }
2428 }
2429
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002430 Py_DECREF(key);
2431 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 Py_DECREF(fast);
2433 Py_DECREF(item);
2434 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002436 i = 0;
Victor Stinner0fc91ee2019-04-12 21:51:34 +02002437 ASSERT_CONSISTENT(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002438 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00002439Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002440 Py_XDECREF(item);
2441 Py_XDECREF(fast);
2442 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002443Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002444 Py_DECREF(it);
2445 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00002446}
2447
doko@ubuntu.comc96df682016-10-11 08:04:02 +02002448static int
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002449dict_merge(PyObject *a, PyObject *b, int override)
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002450{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002451 PyDictObject *mp, *other;
2452 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -07002453 PyDictKeyEntry *entry, *ep0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002454
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002455 assert(0 <= override && override <= 2);
2456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002457 /* We accept for the argument either a concrete dictionary object,
2458 * or an abstract "mapping" object. For the former, we can do
2459 * things quite efficiently. For the latter, we only require that
2460 * PyMapping_Keys() and PyObject_GetItem() be supported.
2461 */
2462 if (a == NULL || !PyDict_Check(a) || b == NULL) {
2463 PyErr_BadInternalCall();
2464 return -1;
2465 }
2466 mp = (PyDictObject*)a;
INADA Naoki2aaf98c2018-09-26 12:59:00 +09002467 if (PyDict_Check(b) && (Py_TYPE(b)->tp_iter == (getiterfunc)dict_iter)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002468 other = (PyDictObject*)b;
2469 if (other == mp || other->ma_used == 0)
2470 /* a.update(a) or a.update({}); nothing to do */
2471 return 0;
2472 if (mp->ma_used == 0)
2473 /* Since the target dict is empty, PyDict_GetItem()
2474 * always returns NULL. Setting override to 1
2475 * skips the unnecessary test.
2476 */
2477 override = 1;
2478 /* Do one big resize at the start, rather than
2479 * incrementally resizing as we insert new items. Expect
2480 * that there will be no (or few) overlapping keys.
2481 */
INADA Naokib1152be2016-10-27 19:26:50 +09002482 if (USABLE_FRACTION(mp->ma_keys->dk_size) < other->ma_used) {
2483 if (dictresize(mp, ESTIMATE_SIZE(mp->ma_used + other->ma_used))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002484 return -1;
INADA Naokib1152be2016-10-27 19:26:50 +09002485 }
2486 }
Victor Stinner742da042016-09-07 17:40:12 -07002487 ep0 = DK_ENTRIES(other->ma_keys);
2488 for (i = 0, n = other->ma_keys->dk_nentries; i < n; i++) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002489 PyObject *key, *value;
2490 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002491 entry = &ep0[i];
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002492 key = entry->me_key;
2493 hash = entry->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002494 if (other->ma_values)
2495 value = other->ma_values[i];
2496 else
2497 value = entry->me_value;
2498
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002499 if (value != NULL) {
2500 int err = 0;
2501 Py_INCREF(key);
2502 Py_INCREF(value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002503 if (override == 1)
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002504 err = insertdict(mp, key, hash, value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002505 else if (_PyDict_GetItem_KnownHash(a, key, hash) == NULL) {
2506 if (PyErr_Occurred()) {
2507 Py_DECREF(value);
2508 Py_DECREF(key);
2509 return -1;
2510 }
2511 err = insertdict(mp, key, hash, value);
2512 }
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002513 else if (override != 0) {
2514 _PyErr_SetKeyError(key);
2515 Py_DECREF(value);
2516 Py_DECREF(key);
2517 return -1;
2518 }
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002519 Py_DECREF(value);
2520 Py_DECREF(key);
2521 if (err != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002522 return -1;
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002523
Victor Stinner742da042016-09-07 17:40:12 -07002524 if (n != other->ma_keys->dk_nentries) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002525 PyErr_SetString(PyExc_RuntimeError,
2526 "dict mutated during update");
2527 return -1;
2528 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002529 }
2530 }
2531 }
2532 else {
2533 /* Do it the generic, slower way */
2534 PyObject *keys = PyMapping_Keys(b);
2535 PyObject *iter;
2536 PyObject *key, *value;
2537 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002539 if (keys == NULL)
2540 /* Docstring says this is equivalent to E.keys() so
2541 * if E doesn't have a .keys() method we want
2542 * AttributeError to percolate up. Might as well
2543 * do the same for any other error.
2544 */
2545 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002547 iter = PyObject_GetIter(keys);
2548 Py_DECREF(keys);
2549 if (iter == NULL)
2550 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002552 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002553 if (override != 1) {
2554 if (PyDict_GetItemWithError(a, key) != NULL) {
2555 if (override != 0) {
2556 _PyErr_SetKeyError(key);
2557 Py_DECREF(key);
2558 Py_DECREF(iter);
2559 return -1;
2560 }
2561 Py_DECREF(key);
2562 continue;
2563 }
2564 else if (PyErr_Occurred()) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002565 Py_DECREF(key);
2566 Py_DECREF(iter);
2567 return -1;
2568 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002569 }
2570 value = PyObject_GetItem(b, key);
2571 if (value == NULL) {
2572 Py_DECREF(iter);
2573 Py_DECREF(key);
2574 return -1;
2575 }
2576 status = PyDict_SetItem(a, key, value);
2577 Py_DECREF(key);
2578 Py_DECREF(value);
2579 if (status < 0) {
2580 Py_DECREF(iter);
2581 return -1;
2582 }
2583 }
2584 Py_DECREF(iter);
2585 if (PyErr_Occurred())
2586 /* Iterator completed, via error */
2587 return -1;
2588 }
Victor Stinner0fc91ee2019-04-12 21:51:34 +02002589 ASSERT_CONSISTENT(a);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002590 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002591}
2592
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002593int
2594PyDict_Update(PyObject *a, PyObject *b)
2595{
2596 return dict_merge(a, b, 1);
2597}
2598
2599int
2600PyDict_Merge(PyObject *a, PyObject *b, int override)
2601{
2602 /* XXX Deprecate override not in (0, 1). */
2603 return dict_merge(a, b, override != 0);
2604}
2605
2606int
2607_PyDict_MergeEx(PyObject *a, PyObject *b, int override)
2608{
2609 return dict_merge(a, b, override);
2610}
2611
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002612static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302613dict_copy(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002614{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002615 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002616}
2617
2618PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002619PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002620{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002621 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002622 PyDictObject *mp;
2623 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002625 if (o == NULL || !PyDict_Check(o)) {
2626 PyErr_BadInternalCall();
2627 return NULL;
2628 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002629
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002630 mp = (PyDictObject *)o;
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002631 if (mp->ma_used == 0) {
2632 /* The dict is empty; just return a new dict. */
2633 return PyDict_New();
2634 }
2635
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002636 if (_PyDict_HasSplitTable(mp)) {
2637 PyDictObject *split_copy;
Victor Stinner742da042016-09-07 17:40:12 -07002638 Py_ssize_t size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
2639 PyObject **newvalues;
2640 newvalues = new_values(size);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002641 if (newvalues == NULL)
2642 return PyErr_NoMemory();
2643 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2644 if (split_copy == NULL) {
2645 free_values(newvalues);
2646 return NULL;
2647 }
2648 split_copy->ma_values = newvalues;
2649 split_copy->ma_keys = mp->ma_keys;
2650 split_copy->ma_used = mp->ma_used;
INADA Naokid1c82c52018-04-03 11:43:53 +09002651 split_copy->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokia7576492018-11-14 18:39:27 +09002652 dictkeys_incref(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002653 for (i = 0, n = size; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002654 PyObject *value = mp->ma_values[i];
2655 Py_XINCREF(value);
2656 split_copy->ma_values[i] = value;
2657 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002658 if (_PyObject_GC_IS_TRACKED(mp))
2659 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002660 return (PyObject *)split_copy;
2661 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002662
2663 if (PyDict_CheckExact(mp) && mp->ma_values == NULL &&
2664 (mp->ma_used >= (mp->ma_keys->dk_nentries * 2) / 3))
2665 {
2666 /* Use fast-copy if:
2667
2668 (1) 'mp' is an instance of a subclassed dict; and
2669
2670 (2) 'mp' is not a split-dict; and
2671
2672 (3) if 'mp' is non-compact ('del' operation does not resize dicts),
2673 do fast-copy only if it has at most 1/3 non-used keys.
2674
Ville Skyttä61f82e02018-04-20 23:08:45 +03002675 The last condition (3) is important to guard against a pathological
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002676 case when a large dict is almost emptied with multiple del/pop
2677 operations and copied after that. In cases like this, we defer to
2678 PyDict_Merge, which produces a compacted copy.
2679 */
2680 return clone_combined_dict(mp);
2681 }
2682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002683 copy = PyDict_New();
2684 if (copy == NULL)
2685 return NULL;
2686 if (PyDict_Merge(copy, o, 1) == 0)
2687 return copy;
2688 Py_DECREF(copy);
2689 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002690}
2691
Martin v. Löwis18e16552006-02-15 17:27:45 +00002692Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002693PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002694{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002695 if (mp == NULL || !PyDict_Check(mp)) {
2696 PyErr_BadInternalCall();
2697 return -1;
2698 }
2699 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002700}
2701
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002702PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002703PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002704{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002705 if (mp == NULL || !PyDict_Check(mp)) {
2706 PyErr_BadInternalCall();
2707 return NULL;
2708 }
2709 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002710}
2711
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002712PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002713PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002714{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002715 if (mp == NULL || !PyDict_Check(mp)) {
2716 PyErr_BadInternalCall();
2717 return NULL;
2718 }
2719 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002720}
2721
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002722PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002723PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002724{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002725 if (mp == NULL || !PyDict_Check(mp)) {
2726 PyErr_BadInternalCall();
2727 return NULL;
2728 }
2729 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002730}
2731
Tim Peterse63415e2001-05-08 04:38:29 +00002732/* Return 1 if dicts equal, 0 if not, -1 if error.
2733 * Gets out as soon as any difference is detected.
2734 * Uses only Py_EQ comparison.
2735 */
2736static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002737dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002738{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002739 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002741 if (a->ma_used != b->ma_used)
2742 /* can't be equal if # of entries differ */
2743 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002744 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Victor Stinner742da042016-09-07 17:40:12 -07002745 for (i = 0; i < a->ma_keys->dk_nentries; i++) {
2746 PyDictKeyEntry *ep = &DK_ENTRIES(a->ma_keys)[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002747 PyObject *aval;
2748 if (a->ma_values)
2749 aval = a->ma_values[i];
2750 else
2751 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002752 if (aval != NULL) {
2753 int cmp;
2754 PyObject *bval;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002755 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002756 /* temporarily bump aval's refcount to ensure it stays
2757 alive until we're done with it */
2758 Py_INCREF(aval);
2759 /* ditto for key */
2760 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002761 /* reuse the known hash value */
INADA Naoki778928b2017-08-03 23:45:15 +09002762 b->ma_keys->dk_lookup(b, key, ep->me_hash, &bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002763 if (bval == NULL) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002764 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002765 Py_DECREF(aval);
2766 if (PyErr_Occurred())
2767 return -1;
2768 return 0;
2769 }
2770 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002771 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002772 Py_DECREF(aval);
2773 if (cmp <= 0) /* error or not equal */
2774 return cmp;
2775 }
2776 }
2777 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002778}
Tim Peterse63415e2001-05-08 04:38:29 +00002779
2780static PyObject *
2781dict_richcompare(PyObject *v, PyObject *w, int op)
2782{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002783 int cmp;
2784 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002785
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002786 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2787 res = Py_NotImplemented;
2788 }
2789 else if (op == Py_EQ || op == Py_NE) {
2790 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2791 if (cmp < 0)
2792 return NULL;
2793 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2794 }
2795 else
2796 res = Py_NotImplemented;
2797 Py_INCREF(res);
2798 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002799}
Tim Peterse63415e2001-05-08 04:38:29 +00002800
Larry Hastings61272b72014-01-07 12:41:53 -08002801/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002802
2803@coexist
2804dict.__contains__
2805
2806 key: object
2807 /
2808
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002809True if the dictionary has the specified key, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002810[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002811
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002812static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002813dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka19d25972017-02-04 08:05:07 +02002814/*[clinic end generated code: output=a3d03db709ed6e6b input=fe1cb42ad831e820]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002815{
Larry Hastingsc2047262014-01-25 20:43:29 -08002816 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002817 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002818 Py_ssize_t ix;
INADA Naokiba609772016-12-07 20:41:42 +09002819 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002821 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002822 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002823 hash = PyObject_Hash(key);
2824 if (hash == -1)
2825 return NULL;
2826 }
INADA Naoki778928b2017-08-03 23:45:15 +09002827 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002828 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002829 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002830 if (ix == DKIX_EMPTY || value == NULL)
Victor Stinner742da042016-09-07 17:40:12 -07002831 Py_RETURN_FALSE;
2832 Py_RETURN_TRUE;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002833}
2834
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002835/*[clinic input]
2836dict.get
2837
2838 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002839 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002840 /
2841
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002842Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002843[clinic start generated code]*/
2844
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002845static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002846dict_get_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002847/*[clinic end generated code: output=bba707729dee05bf input=279ddb5790b6b107]*/
Barry Warsawc38c5da1997-10-06 17:49:20 +00002848{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002849 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002850 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002851 Py_ssize_t ix;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002853 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002854 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002855 hash = PyObject_Hash(key);
2856 if (hash == -1)
2857 return NULL;
2858 }
INADA Naoki778928b2017-08-03 23:45:15 +09002859 ix = (self->ma_keys->dk_lookup) (self, key, hash, &val);
Victor Stinner742da042016-09-07 17:40:12 -07002860 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002861 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002862 if (ix == DKIX_EMPTY || val == NULL) {
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002863 val = default_value;
INADA Naokiba609772016-12-07 20:41:42 +09002864 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002865 Py_INCREF(val);
2866 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002867}
2868
Benjamin Peterson00e98862013-03-07 22:16:29 -05002869PyObject *
2870PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002871{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002872 PyDictObject *mp = (PyDictObject *)d;
INADA Naoki93f26f72016-11-02 18:45:16 +09002873 PyObject *value;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002874 Py_hash_t hash;
Guido van Rossum164452c2000-08-08 16:12:54 +00002875
Benjamin Peterson00e98862013-03-07 22:16:29 -05002876 if (!PyDict_Check(d)) {
2877 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002878 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002879 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002881 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002882 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002883 hash = PyObject_Hash(key);
2884 if (hash == -1)
2885 return NULL;
2886 }
Inada Naoki2ddc7f62019-03-18 20:38:33 +09002887 if (mp->ma_keys == Py_EMPTY_KEYS) {
2888 if (insert_to_emptydict(mp, key, hash, defaultobj) < 0) {
2889 return NULL;
2890 }
2891 return defaultobj;
2892 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002893
2894 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
2895 if (insertion_resize(mp) < 0)
2896 return NULL;
2897 }
2898
INADA Naoki778928b2017-08-03 23:45:15 +09002899 Py_ssize_t ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002900 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 return NULL;
INADA Naoki93f26f72016-11-02 18:45:16 +09002902
2903 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09002904 ((ix >= 0 && value == NULL && mp->ma_used != ix) ||
INADA Naoki93f26f72016-11-02 18:45:16 +09002905 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
2906 if (insertion_resize(mp) < 0) {
2907 return NULL;
2908 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002909 ix = DKIX_EMPTY;
2910 }
2911
2912 if (ix == DKIX_EMPTY) {
2913 PyDictKeyEntry *ep, *ep0;
2914 value = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002915 if (mp->ma_keys->dk_usable <= 0) {
Victor Stinner3c336c52016-09-12 14:17:40 +02002916 if (insertion_resize(mp) < 0) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002917 return NULL;
Victor Stinner3c336c52016-09-12 14:17:40 +02002918 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002919 }
INADA Naoki778928b2017-08-03 23:45:15 +09002920 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naoki93f26f72016-11-02 18:45:16 +09002921 ep0 = DK_ENTRIES(mp->ma_keys);
2922 ep = &ep0[mp->ma_keys->dk_nentries];
INADA Naokia7576492018-11-14 18:39:27 +09002923 dictkeys_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002924 Py_INCREF(key);
INADA Naoki93f26f72016-11-02 18:45:16 +09002925 Py_INCREF(value);
2926 MAINTAIN_TRACKING(mp, key, value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002927 ep->me_key = key;
2928 ep->me_hash = hash;
INADA Naokiba609772016-12-07 20:41:42 +09002929 if (_PyDict_HasSplitTable(mp)) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002930 assert(mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
2931 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Victor Stinner742da042016-09-07 17:40:12 -07002932 }
2933 else {
INADA Naoki93f26f72016-11-02 18:45:16 +09002934 ep->me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07002935 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002936 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002937 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naoki93f26f72016-11-02 18:45:16 +09002938 mp->ma_keys->dk_usable--;
2939 mp->ma_keys->dk_nentries++;
2940 assert(mp->ma_keys->dk_usable >= 0);
2941 }
INADA Naokiba609772016-12-07 20:41:42 +09002942 else if (value == NULL) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002943 value = defaultobj;
2944 assert(_PyDict_HasSplitTable(mp));
2945 assert(ix == mp->ma_used);
2946 Py_INCREF(value);
2947 MAINTAIN_TRACKING(mp, key, value);
INADA Naokiba609772016-12-07 20:41:42 +09002948 mp->ma_values[ix] = value;
INADA Naoki93f26f72016-11-02 18:45:16 +09002949 mp->ma_used++;
2950 mp->ma_version_tag = DICT_NEXT_VERSION();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002951 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002952
Victor Stinner0fc91ee2019-04-12 21:51:34 +02002953 ASSERT_CONSISTENT(mp);
INADA Naoki93f26f72016-11-02 18:45:16 +09002954 return value;
Guido van Rossum164452c2000-08-08 16:12:54 +00002955}
2956
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002957/*[clinic input]
2958dict.setdefault
2959
2960 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002961 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002962 /
2963
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002964Insert key with a value of default if key is not in the dictionary.
2965
2966Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002967[clinic start generated code]*/
2968
Benjamin Peterson00e98862013-03-07 22:16:29 -05002969static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002970dict_setdefault_impl(PyDictObject *self, PyObject *key,
2971 PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002972/*[clinic end generated code: output=f8c1101ebf69e220 input=0f063756e815fd9d]*/
Benjamin Peterson00e98862013-03-07 22:16:29 -05002973{
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002974 PyObject *val;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002975
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002976 val = PyDict_SetDefault((PyObject *)self, key, default_value);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002977 Py_XINCREF(val);
2978 return val;
2979}
Guido van Rossum164452c2000-08-08 16:12:54 +00002980
2981static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302982dict_clear(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002983{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002984 PyDict_Clear((PyObject *)mp);
2985 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002986}
2987
Inada Naoki9e4f2f32019-04-12 16:11:28 +09002988/*[clinic input]
2989dict.pop
2990
2991 key: object
2992 default: object = NULL
2993 /
2994
2995Remove specified key and return the corresponding value.
2996
2997If key is not found, default is returned if given, otherwise KeyError is raised
2998[clinic start generated code]*/
2999
Guido van Rossumba6ab842000-12-12 22:02:18 +00003000static PyObject *
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003001dict_pop_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
3002/*[clinic end generated code: output=3abb47b89f24c21c input=016f6a000e4e633b]*/
Guido van Rossume027d982002-04-12 15:11:59 +00003003{
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003004 return _PyDict_Pop((PyObject*)self, key, default_value);
Guido van Rossume027d982002-04-12 15:11:59 +00003005}
3006
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003007/*[clinic input]
3008dict.popitem
3009
3010Remove and return a (key, value) pair as a 2-tuple.
3011
3012Pairs are returned in LIFO (last-in, first-out) order.
3013Raises KeyError if the dict is empty.
3014[clinic start generated code]*/
3015
Guido van Rossume027d982002-04-12 15:11:59 +00003016static PyObject *
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003017dict_popitem_impl(PyDictObject *self)
3018/*[clinic end generated code: output=e65fcb04420d230d input=1c38a49f21f64941]*/
Guido van Rossumba6ab842000-12-12 22:02:18 +00003019{
Victor Stinner742da042016-09-07 17:40:12 -07003020 Py_ssize_t i, j;
3021 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003022 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00003023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003024 /* Allocate the result tuple before checking the size. Believe it
3025 * or not, this allocation could trigger a garbage collection which
3026 * could empty the dict, so if we checked the size first and that
3027 * happened, the result would be an infinite loop (searching for an
3028 * entry that no longer exists). Note that the usual popitem()
3029 * idiom is "while d: k, v = d.popitem()". so needing to throw the
3030 * tuple away if the dict *is* empty isn't a significant
3031 * inefficiency -- possible, but unlikely in practice.
3032 */
3033 res = PyTuple_New(2);
3034 if (res == NULL)
3035 return NULL;
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003036 if (self->ma_used == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003037 Py_DECREF(res);
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003038 PyErr_SetString(PyExc_KeyError, "popitem(): dictionary is empty");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003039 return NULL;
3040 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003041 /* Convert split table to combined table */
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003042 if (self->ma_keys->dk_lookup == lookdict_split) {
3043 if (dictresize(self, DK_SIZE(self->ma_keys))) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003044 Py_DECREF(res);
3045 return NULL;
3046 }
3047 }
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003048 ENSURE_ALLOWS_DELETIONS(self);
Victor Stinner742da042016-09-07 17:40:12 -07003049
3050 /* Pop last item */
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003051 ep0 = DK_ENTRIES(self->ma_keys);
3052 i = self->ma_keys->dk_nentries - 1;
Victor Stinner742da042016-09-07 17:40:12 -07003053 while (i >= 0 && ep0[i].me_value == NULL) {
3054 i--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003055 }
Victor Stinner742da042016-09-07 17:40:12 -07003056 assert(i >= 0);
3057
3058 ep = &ep0[i];
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003059 j = lookdict_index(self->ma_keys, ep->me_hash, i);
Victor Stinner742da042016-09-07 17:40:12 -07003060 assert(j >= 0);
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003061 assert(dictkeys_get_index(self->ma_keys, j) == i);
3062 dictkeys_set_index(self->ma_keys, j, DKIX_DUMMY);
Victor Stinner742da042016-09-07 17:40:12 -07003063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003064 PyTuple_SET_ITEM(res, 0, ep->me_key);
3065 PyTuple_SET_ITEM(res, 1, ep->me_value);
Victor Stinner742da042016-09-07 17:40:12 -07003066 ep->me_key = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003067 ep->me_value = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07003068 /* We can't dk_usable++ since there is DKIX_DUMMY in indices */
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003069 self->ma_keys->dk_nentries = i;
3070 self->ma_used--;
3071 self->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner0fc91ee2019-04-12 21:51:34 +02003072 ASSERT_CONSISTENT(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003073 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00003074}
3075
Jeremy Hylton8caad492000-06-23 14:18:11 +00003076static int
3077dict_traverse(PyObject *op, visitproc visit, void *arg)
3078{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003079 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson55f44522016-09-05 12:12:59 -07003080 PyDictKeysObject *keys = mp->ma_keys;
Serhiy Storchaka46825d22016-09-26 21:29:34 +03003081 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Victor Stinner742da042016-09-07 17:40:12 -07003082 Py_ssize_t i, n = keys->dk_nentries;
3083
Benjamin Peterson55f44522016-09-05 12:12:59 -07003084 if (keys->dk_lookup == lookdict) {
3085 for (i = 0; i < n; i++) {
3086 if (entries[i].me_value != NULL) {
3087 Py_VISIT(entries[i].me_value);
3088 Py_VISIT(entries[i].me_key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003089 }
3090 }
Victor Stinner742da042016-09-07 17:40:12 -07003091 }
3092 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003093 if (mp->ma_values != NULL) {
Benjamin Peterson55f44522016-09-05 12:12:59 -07003094 for (i = 0; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003095 Py_VISIT(mp->ma_values[i]);
3096 }
3097 }
3098 else {
Benjamin Peterson55f44522016-09-05 12:12:59 -07003099 for (i = 0; i < n; i++) {
3100 Py_VISIT(entries[i].me_value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003101 }
3102 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003103 }
3104 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00003105}
3106
3107static int
3108dict_tp_clear(PyObject *op)
3109{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003110 PyDict_Clear(op);
3111 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00003112}
3113
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003114static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003115
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003116Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003117_PyDict_SizeOf(PyDictObject *mp)
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003118{
Victor Stinner742da042016-09-07 17:40:12 -07003119 Py_ssize_t size, usable, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003120
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003121 size = DK_SIZE(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07003122 usable = USABLE_FRACTION(size);
3123
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02003124 res = _PyObject_SIZE(Py_TYPE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003125 if (mp->ma_values)
Victor Stinner742da042016-09-07 17:40:12 -07003126 res += usable * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003127 /* If the dictionary is split, the keys portion is accounted-for
3128 in the type object. */
3129 if (mp->ma_keys->dk_refcnt == 1)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003130 res += (sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003131 + DK_IXSIZE(mp->ma_keys) * size
3132 + sizeof(PyDictKeyEntry) * usable);
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003133 return res;
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003134}
3135
3136Py_ssize_t
3137_PyDict_KeysSize(PyDictKeysObject *keys)
3138{
Victor Stinner98ee9d52016-09-08 09:33:56 -07003139 return (sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003140 + DK_IXSIZE(keys) * DK_SIZE(keys)
3141 + USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry));
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003142}
3143
doko@ubuntu.com17210f52016-01-14 14:04:59 +01003144static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303145dict_sizeof(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003146{
3147 return PyLong_FromSsize_t(_PyDict_SizeOf(mp));
3148}
3149
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00003150PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
3151
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003152PyDoc_STRVAR(sizeof__doc__,
3153"D.__sizeof__() -> size of D in memory, in bytes");
3154
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003155PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04003156"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
3157If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
3158If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
3159In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00003160
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003161PyDoc_STRVAR(clear__doc__,
3162"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003163
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003164PyDoc_STRVAR(copy__doc__,
3165"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00003166
Guido van Rossumb90c8482007-02-10 01:11:45 +00003167/* Forward */
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303168static PyObject *dictkeys_new(PyObject *, PyObject *);
3169static PyObject *dictitems_new(PyObject *, PyObject *);
3170static PyObject *dictvalues_new(PyObject *, PyObject *);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003171
Guido van Rossum45c85d12007-07-27 16:31:40 +00003172PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003173 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003174PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003175 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003176PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003177 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00003178
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003179static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07003180 DICT___CONTAINS___METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003181 {"__getitem__", (PyCFunction)(void(*)(void))dict_subscript, METH_O | METH_COEXIST,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003182 getitem__doc__},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003183 {"__sizeof__", (PyCFunction)(void(*)(void))dict_sizeof, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003184 sizeof__doc__},
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003185 DICT_GET_METHODDEF
3186 DICT_SETDEFAULT_METHODDEF
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003187 DICT_POP_METHODDEF
3188 DICT_POPITEM_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303189 {"keys", dictkeys_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003190 keys__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303191 {"items", dictitems_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003192 items__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303193 {"values", dictvalues_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003194 values__doc__},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003195 {"update", (PyCFunction)(void(*)(void))dict_update, METH_VARARGS | METH_KEYWORDS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003196 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08003197 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003198 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
3199 clear__doc__},
3200 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
3201 copy__doc__},
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003202 DICT___REVERSED___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003203 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003204};
3205
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00003206/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00003207int
3208PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003209{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003210 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07003211 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003212 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003213 PyObject *value;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003215 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003216 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003217 hash = PyObject_Hash(key);
3218 if (hash == -1)
3219 return -1;
3220 }
INADA Naoki778928b2017-08-03 23:45:15 +09003221 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003222 if (ix == DKIX_ERROR)
3223 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003224 return (ix != DKIX_EMPTY && value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003225}
3226
Thomas Wouterscf297e42007-02-23 15:07:44 +00003227/* Internal version of PyDict_Contains used when the hash value is already known */
3228int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003229_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00003230{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003231 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003232 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -07003233 Py_ssize_t ix;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003234
INADA Naoki778928b2017-08-03 23:45:15 +09003235 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003236 if (ix == DKIX_ERROR)
3237 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003238 return (ix != DKIX_EMPTY && value != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00003239}
3240
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003241/* Hack to implement "key in dict" */
3242static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003243 0, /* sq_length */
3244 0, /* sq_concat */
3245 0, /* sq_repeat */
3246 0, /* sq_item */
3247 0, /* sq_slice */
3248 0, /* sq_ass_item */
3249 0, /* sq_ass_slice */
3250 PyDict_Contains, /* sq_contains */
3251 0, /* sq_inplace_concat */
3252 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003253};
3254
Guido van Rossum09e563a2001-05-01 12:10:21 +00003255static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003256dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3257{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003258 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003259 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003261 assert(type != NULL && type->tp_alloc != NULL);
3262 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02003263 if (self == NULL)
3264 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003265 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003266
Victor Stinnera9f61a52013-07-16 22:17:26 +02003267 /* The object has been implicitly tracked by tp_alloc */
3268 if (type == &PyDict_Type)
3269 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003270
3271 d->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003272 d->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07003273 d->ma_keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003274 if (d->ma_keys == NULL) {
3275 Py_DECREF(self);
3276 return NULL;
3277 }
Victor Stinner0fc91ee2019-04-12 21:51:34 +02003278 ASSERT_CONSISTENT(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003279 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003280}
3281
Tim Peters25786c02001-09-02 08:22:48 +00003282static int
3283dict_init(PyObject *self, PyObject *args, PyObject *kwds)
3284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003285 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00003286}
3287
Tim Peters6d6c1a32001-08-02 04:15:00 +00003288static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003289dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00003290{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003291 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003292}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003293
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003294PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00003295"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00003296"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003297" (key, value) pairs\n"
3298"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00003299" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003300" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00003301" d[k] = v\n"
3302"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
3303" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00003304
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003305PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003306 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3307 "dict",
3308 sizeof(PyDictObject),
3309 0,
3310 (destructor)dict_dealloc, /* tp_dealloc */
3311 0, /* tp_print */
3312 0, /* tp_getattr */
3313 0, /* tp_setattr */
3314 0, /* tp_reserved */
3315 (reprfunc)dict_repr, /* tp_repr */
3316 0, /* tp_as_number */
3317 &dict_as_sequence, /* tp_as_sequence */
3318 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003319 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003320 0, /* tp_call */
3321 0, /* tp_str */
3322 PyObject_GenericGetAttr, /* tp_getattro */
3323 0, /* tp_setattro */
3324 0, /* tp_as_buffer */
3325 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3326 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
3327 dictionary_doc, /* tp_doc */
3328 dict_traverse, /* tp_traverse */
3329 dict_tp_clear, /* tp_clear */
3330 dict_richcompare, /* tp_richcompare */
3331 0, /* tp_weaklistoffset */
3332 (getiterfunc)dict_iter, /* tp_iter */
3333 0, /* tp_iternext */
3334 mapp_methods, /* tp_methods */
3335 0, /* tp_members */
3336 0, /* tp_getset */
3337 0, /* tp_base */
3338 0, /* tp_dict */
3339 0, /* tp_descr_get */
3340 0, /* tp_descr_set */
3341 0, /* tp_dictoffset */
3342 dict_init, /* tp_init */
3343 PyType_GenericAlloc, /* tp_alloc */
3344 dict_new, /* tp_new */
3345 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003346};
3347
Victor Stinner3c1e4812012-03-26 22:10:51 +02003348PyObject *
3349_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
3350{
3351 PyObject *kv;
3352 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02003353 if (kv == NULL) {
3354 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003355 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02003356 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02003357 return PyDict_GetItem(dp, kv);
3358}
3359
Guido van Rossum3cca2451997-05-16 14:23:33 +00003360/* For backward compatibility with old dictionary interface */
3361
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003362PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003363PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003364{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003365 PyObject *kv, *rv;
3366 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003367 if (kv == NULL) {
3368 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003369 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003370 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003371 rv = PyDict_GetItem(v, kv);
3372 Py_DECREF(kv);
3373 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003374}
3375
3376int
Victor Stinner3c1e4812012-03-26 22:10:51 +02003377_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
3378{
3379 PyObject *kv;
3380 kv = _PyUnicode_FromId(key); /* borrowed */
3381 if (kv == NULL)
3382 return -1;
3383 return PyDict_SetItem(v, kv, item);
3384}
3385
3386int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003387PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003388{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003389 PyObject *kv;
3390 int err;
3391 kv = PyUnicode_FromString(key);
3392 if (kv == NULL)
3393 return -1;
3394 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
3395 err = PyDict_SetItem(v, kv, item);
3396 Py_DECREF(kv);
3397 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003398}
3399
3400int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01003401_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
3402{
3403 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
3404 if (kv == NULL)
3405 return -1;
3406 return PyDict_DelItem(v, kv);
3407}
3408
3409int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003410PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003411{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003412 PyObject *kv;
3413 int err;
3414 kv = PyUnicode_FromString(key);
3415 if (kv == NULL)
3416 return -1;
3417 err = PyDict_DelItem(v, kv);
3418 Py_DECREF(kv);
3419 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003420}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003421
Raymond Hettinger019a1482004-03-18 02:41:19 +00003422/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003423
3424typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003425 PyObject_HEAD
3426 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
3427 Py_ssize_t di_used;
3428 Py_ssize_t di_pos;
3429 PyObject* di_result; /* reusable result tuple for iteritems */
3430 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003431} dictiterobject;
3432
3433static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003434dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003435{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003436 dictiterobject *di;
3437 di = PyObject_GC_New(dictiterobject, itertype);
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003438 if (di == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003439 return NULL;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003440 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003441 Py_INCREF(dict);
3442 di->di_dict = dict;
3443 di->di_used = dict->ma_used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003444 di->len = dict->ma_used;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003445 if ((itertype == &PyDictRevIterKey_Type ||
3446 itertype == &PyDictRevIterItem_Type ||
3447 itertype == &PyDictRevIterValue_Type) && dict->ma_used) {
3448 di->di_pos = dict->ma_keys->dk_nentries - 1;
3449 }
3450 else {
3451 di->di_pos = 0;
3452 }
3453 if (itertype == &PyDictIterItem_Type ||
3454 itertype == &PyDictRevIterItem_Type) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003455 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
3456 if (di->di_result == NULL) {
3457 Py_DECREF(di);
3458 return NULL;
3459 }
3460 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003461 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003462 di->di_result = NULL;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003463 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003464 _PyObject_GC_TRACK(di);
3465 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003466}
3467
3468static void
3469dictiter_dealloc(dictiterobject *di)
3470{
INADA Naokia6296d32017-08-24 14:55:17 +09003471 /* bpo-31095: UnTrack is needed before calling any callbacks */
3472 _PyObject_GC_UNTRACK(di);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003473 Py_XDECREF(di->di_dict);
3474 Py_XDECREF(di->di_result);
3475 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003476}
3477
3478static int
3479dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
3480{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003481 Py_VISIT(di->di_dict);
3482 Py_VISIT(di->di_result);
3483 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003484}
3485
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003486static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303487dictiter_len(dictiterobject *di, PyObject *Py_UNUSED(ignored))
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003488{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003489 Py_ssize_t len = 0;
3490 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
3491 len = di->len;
3492 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003493}
3494
Guido van Rossumb90c8482007-02-10 01:11:45 +00003495PyDoc_STRVAR(length_hint_doc,
3496 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003497
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003498static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303499dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003500
3501PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3502
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003503static PyMethodDef dictiter_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003504 {"__length_hint__", (PyCFunction)(void(*)(void))dictiter_len, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003505 length_hint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003506 {"__reduce__", (PyCFunction)(void(*)(void))dictiter_reduce, METH_NOARGS,
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003507 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003508 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003509};
3510
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003511static PyObject*
3512dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00003513{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003514 PyObject *key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003515 Py_ssize_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003516 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003517 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00003518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003519 if (d == NULL)
3520 return NULL;
3521 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00003522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003523 if (di->di_used != d->ma_used) {
3524 PyErr_SetString(PyExc_RuntimeError,
3525 "dictionary changed size during iteration");
3526 di->di_used = -1; /* Make this state sticky */
3527 return NULL;
3528 }
Guido van Rossum2147df72002-07-16 20:30:22 +00003529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003530 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003531 k = d->ma_keys;
INADA Naokica2d8be2016-11-04 16:59:10 +09003532 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003533 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003534 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003535 goto fail;
3536 key = DK_ENTRIES(k)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003537 assert(d->ma_values[i] != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003538 }
3539 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003540 Py_ssize_t n = k->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003541 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
3542 while (i < n && entry_ptr->me_value == NULL) {
3543 entry_ptr++;
3544 i++;
3545 }
3546 if (i >= n)
3547 goto fail;
3548 key = entry_ptr->me_key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003549 }
Thomas Perl796cc6e2019-03-28 07:03:25 +01003550 // We found an element (key), but did not expect it
3551 if (di->len == 0) {
3552 PyErr_SetString(PyExc_RuntimeError,
3553 "dictionary keys changed during iteration");
3554 goto fail;
3555 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003556 di->di_pos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003557 di->len--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003558 Py_INCREF(key);
3559 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003560
3561fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003562 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003563 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003564 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003565}
3566
Raymond Hettinger019a1482004-03-18 02:41:19 +00003567PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003568 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3569 "dict_keyiterator", /* tp_name */
3570 sizeof(dictiterobject), /* tp_basicsize */
3571 0, /* tp_itemsize */
3572 /* methods */
3573 (destructor)dictiter_dealloc, /* tp_dealloc */
3574 0, /* tp_print */
3575 0, /* tp_getattr */
3576 0, /* tp_setattr */
3577 0, /* tp_reserved */
3578 0, /* tp_repr */
3579 0, /* tp_as_number */
3580 0, /* tp_as_sequence */
3581 0, /* tp_as_mapping */
3582 0, /* tp_hash */
3583 0, /* tp_call */
3584 0, /* tp_str */
3585 PyObject_GenericGetAttr, /* tp_getattro */
3586 0, /* tp_setattro */
3587 0, /* tp_as_buffer */
3588 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3589 0, /* tp_doc */
3590 (traverseproc)dictiter_traverse, /* tp_traverse */
3591 0, /* tp_clear */
3592 0, /* tp_richcompare */
3593 0, /* tp_weaklistoffset */
3594 PyObject_SelfIter, /* tp_iter */
3595 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
3596 dictiter_methods, /* tp_methods */
3597 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003598};
3599
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003600static PyObject *
3601dictiter_iternextvalue(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003602{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003603 PyObject *value;
INADA Naokica2d8be2016-11-04 16:59:10 +09003604 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003605 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003607 if (d == NULL)
3608 return NULL;
3609 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003611 if (di->di_used != d->ma_used) {
3612 PyErr_SetString(PyExc_RuntimeError,
3613 "dictionary changed size during iteration");
3614 di->di_used = -1; /* Make this state sticky */
3615 return NULL;
3616 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003617
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003618 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003619 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003620 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003621 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003622 goto fail;
INADA Naokica2d8be2016-11-04 16:59:10 +09003623 value = d->ma_values[i];
3624 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003625 }
3626 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003627 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003628 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3629 while (i < n && entry_ptr->me_value == NULL) {
3630 entry_ptr++;
3631 i++;
3632 }
3633 if (i >= n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003634 goto fail;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003635 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003636 }
Thomas Perlb8311cf2019-04-02 11:30:10 +02003637 // We found an element, but did not expect it
3638 if (di->len == 0) {
3639 PyErr_SetString(PyExc_RuntimeError,
3640 "dictionary keys changed during iteration");
3641 goto fail;
3642 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003643 di->di_pos = i+1;
3644 di->len--;
3645 Py_INCREF(value);
3646 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003647
3648fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003649 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003650 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003651 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003652}
3653
3654PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003655 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3656 "dict_valueiterator", /* tp_name */
3657 sizeof(dictiterobject), /* tp_basicsize */
3658 0, /* tp_itemsize */
3659 /* methods */
3660 (destructor)dictiter_dealloc, /* tp_dealloc */
3661 0, /* tp_print */
3662 0, /* tp_getattr */
3663 0, /* tp_setattr */
3664 0, /* tp_reserved */
3665 0, /* tp_repr */
3666 0, /* tp_as_number */
3667 0, /* tp_as_sequence */
3668 0, /* tp_as_mapping */
3669 0, /* tp_hash */
3670 0, /* tp_call */
3671 0, /* tp_str */
3672 PyObject_GenericGetAttr, /* tp_getattro */
3673 0, /* tp_setattro */
3674 0, /* tp_as_buffer */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003675 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003676 0, /* tp_doc */
3677 (traverseproc)dictiter_traverse, /* tp_traverse */
3678 0, /* tp_clear */
3679 0, /* tp_richcompare */
3680 0, /* tp_weaklistoffset */
3681 PyObject_SelfIter, /* tp_iter */
3682 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3683 dictiter_methods, /* tp_methods */
3684 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003685};
3686
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003687static PyObject *
3688dictiter_iternextitem(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003689{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003690 PyObject *key, *value, *result;
INADA Naokica2d8be2016-11-04 16:59:10 +09003691 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003692 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003694 if (d == NULL)
3695 return NULL;
3696 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003698 if (di->di_used != d->ma_used) {
3699 PyErr_SetString(PyExc_RuntimeError,
3700 "dictionary changed size during iteration");
3701 di->di_used = -1; /* Make this state sticky */
3702 return NULL;
3703 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003705 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003706 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003707 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003708 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003709 goto fail;
3710 key = DK_ENTRIES(d->ma_keys)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003711 value = d->ma_values[i];
3712 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003713 }
3714 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003715 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003716 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3717 while (i < n && entry_ptr->me_value == NULL) {
3718 entry_ptr++;
3719 i++;
3720 }
3721 if (i >= n)
3722 goto fail;
3723 key = entry_ptr->me_key;
3724 value = entry_ptr->me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003725 }
Thomas Perlb8311cf2019-04-02 11:30:10 +02003726 // We found an element, but did not expect it
3727 if (di->len == 0) {
3728 PyErr_SetString(PyExc_RuntimeError,
3729 "dictionary keys changed during iteration");
3730 goto fail;
3731 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003732 di->di_pos = i+1;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003733 di->len--;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003734 Py_INCREF(key);
3735 Py_INCREF(value);
3736 result = di->di_result;
3737 if (Py_REFCNT(result) == 1) {
3738 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3739 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3740 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3741 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003742 Py_INCREF(result);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003743 Py_DECREF(oldkey);
3744 Py_DECREF(oldvalue);
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003745 }
3746 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003747 result = PyTuple_New(2);
3748 if (result == NULL)
3749 return NULL;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003750 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3751 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003752 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003753 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003754
3755fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003756 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003757 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003758 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003759}
3760
3761PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003762 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3763 "dict_itemiterator", /* tp_name */
3764 sizeof(dictiterobject), /* tp_basicsize */
3765 0, /* tp_itemsize */
3766 /* methods */
3767 (destructor)dictiter_dealloc, /* tp_dealloc */
3768 0, /* tp_print */
3769 0, /* tp_getattr */
3770 0, /* tp_setattr */
3771 0, /* tp_reserved */
3772 0, /* tp_repr */
3773 0, /* tp_as_number */
3774 0, /* tp_as_sequence */
3775 0, /* tp_as_mapping */
3776 0, /* tp_hash */
3777 0, /* tp_call */
3778 0, /* tp_str */
3779 PyObject_GenericGetAttr, /* tp_getattro */
3780 0, /* tp_setattro */
3781 0, /* tp_as_buffer */
3782 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3783 0, /* tp_doc */
3784 (traverseproc)dictiter_traverse, /* tp_traverse */
3785 0, /* tp_clear */
3786 0, /* tp_richcompare */
3787 0, /* tp_weaklistoffset */
3788 PyObject_SelfIter, /* tp_iter */
3789 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3790 dictiter_methods, /* tp_methods */
3791 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003792};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003793
3794
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003795/* dictreviter */
3796
3797static PyObject *
3798dictreviter_iternext(dictiterobject *di)
3799{
3800 PyDictObject *d = di->di_dict;
3801
3802 if (d == NULL) {
3803 return NULL;
3804 }
3805 assert (PyDict_Check(d));
3806
3807 if (di->di_used != d->ma_used) {
3808 PyErr_SetString(PyExc_RuntimeError,
3809 "dictionary changed size during iteration");
3810 di->di_used = -1; /* Make this state sticky */
3811 return NULL;
3812 }
3813
3814 Py_ssize_t i = di->di_pos;
3815 PyDictKeysObject *k = d->ma_keys;
3816 PyObject *key, *value, *result;
3817
3818 if (d->ma_values) {
3819 if (i < 0) {
3820 goto fail;
3821 }
3822 key = DK_ENTRIES(k)[i].me_key;
3823 value = d->ma_values[i];
3824 assert (value != NULL);
3825 }
3826 else {
3827 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
3828 while (i >= 0 && entry_ptr->me_value == NULL) {
3829 entry_ptr--;
3830 i--;
3831 }
3832 if (i < 0) {
3833 goto fail;
3834 }
3835 key = entry_ptr->me_key;
3836 value = entry_ptr->me_value;
3837 }
3838 di->di_pos = i-1;
3839 di->len--;
3840
3841 if (Py_TYPE(di) == &PyDictRevIterKey_Type) {
3842 Py_INCREF(key);
3843 return key;
3844 }
3845 else if (Py_TYPE(di) == &PyDictRevIterValue_Type) {
3846 Py_INCREF(value);
3847 return value;
3848 }
3849 else if (Py_TYPE(di) == &PyDictRevIterItem_Type) {
3850 Py_INCREF(key);
3851 Py_INCREF(value);
3852 result = di->di_result;
3853 if (Py_REFCNT(result) == 1) {
3854 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3855 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3856 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3857 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
3858 Py_INCREF(result);
3859 Py_DECREF(oldkey);
3860 Py_DECREF(oldvalue);
3861 }
3862 else {
3863 result = PyTuple_New(2);
3864 if (result == NULL) {
3865 return NULL;
3866 }
3867 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3868 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
3869 }
3870 return result;
3871 }
3872 else {
3873 Py_UNREACHABLE();
3874 }
3875
3876fail:
3877 di->di_dict = NULL;
3878 Py_DECREF(d);
3879 return NULL;
3880}
3881
3882PyTypeObject PyDictRevIterKey_Type = {
3883 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3884 "dict_reversekeyiterator",
3885 sizeof(dictiterobject),
3886 .tp_dealloc = (destructor)dictiter_dealloc,
3887 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
3888 .tp_traverse = (traverseproc)dictiter_traverse,
3889 .tp_iter = PyObject_SelfIter,
3890 .tp_iternext = (iternextfunc)dictreviter_iternext,
3891 .tp_methods = dictiter_methods
3892};
3893
3894
3895/*[clinic input]
3896dict.__reversed__
3897
3898Return a reverse iterator over the dict keys.
3899[clinic start generated code]*/
3900
3901static PyObject *
3902dict___reversed___impl(PyDictObject *self)
3903/*[clinic end generated code: output=e674483336d1ed51 input=23210ef3477d8c4d]*/
3904{
3905 assert (PyDict_Check(self));
3906 return dictiter_new(self, &PyDictRevIterKey_Type);
3907}
3908
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003909static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303910dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003911{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003912 _Py_IDENTIFIER(iter);
Sergey Fedoseev63958442018-10-20 05:43:33 +05003913 /* copy the iterator state */
3914 dictiterobject tmp = *di;
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003915 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003916
Sergey Fedoseev63958442018-10-20 05:43:33 +05003917 PyObject *list = PySequence_List((PyObject*)&tmp);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003918 Py_XDECREF(tmp.di_dict);
Sergey Fedoseev63958442018-10-20 05:43:33 +05003919 if (list == NULL) {
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003920 return NULL;
3921 }
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003922 return Py_BuildValue("N(N)", _PyEval_GetBuiltinId(&PyId_iter), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003923}
3924
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003925PyTypeObject PyDictRevIterItem_Type = {
3926 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3927 "dict_reverseitemiterator",
3928 sizeof(dictiterobject),
3929 .tp_dealloc = (destructor)dictiter_dealloc,
3930 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
3931 .tp_traverse = (traverseproc)dictiter_traverse,
3932 .tp_iter = PyObject_SelfIter,
3933 .tp_iternext = (iternextfunc)dictreviter_iternext,
3934 .tp_methods = dictiter_methods
3935};
3936
3937PyTypeObject PyDictRevIterValue_Type = {
3938 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3939 "dict_reversevalueiterator",
3940 sizeof(dictiterobject),
3941 .tp_dealloc = (destructor)dictiter_dealloc,
3942 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
3943 .tp_traverse = (traverseproc)dictiter_traverse,
3944 .tp_iter = PyObject_SelfIter,
3945 .tp_iternext = (iternextfunc)dictreviter_iternext,
3946 .tp_methods = dictiter_methods
3947};
3948
Guido van Rossum3ac67412007-02-10 18:55:06 +00003949/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003950/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003951/***********************************************/
3952
Guido van Rossumb90c8482007-02-10 01:11:45 +00003953/* The instance lay-out is the same for all three; but the type differs. */
3954
Guido van Rossumb90c8482007-02-10 01:11:45 +00003955static void
Eric Snow96c6af92015-05-29 22:21:39 -06003956dictview_dealloc(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003957{
INADA Naokia6296d32017-08-24 14:55:17 +09003958 /* bpo-31095: UnTrack is needed before calling any callbacks */
3959 _PyObject_GC_UNTRACK(dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003960 Py_XDECREF(dv->dv_dict);
3961 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003962}
3963
3964static int
Eric Snow96c6af92015-05-29 22:21:39 -06003965dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003966{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003967 Py_VISIT(dv->dv_dict);
3968 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003969}
3970
Guido van Rossum83825ac2007-02-10 04:54:19 +00003971static Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003972dictview_len(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003973{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003974 Py_ssize_t len = 0;
3975 if (dv->dv_dict != NULL)
3976 len = dv->dv_dict->ma_used;
3977 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003978}
3979
Eric Snow96c6af92015-05-29 22:21:39 -06003980PyObject *
3981_PyDictView_New(PyObject *dict, PyTypeObject *type)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003982{
Eric Snow96c6af92015-05-29 22:21:39 -06003983 _PyDictViewObject *dv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003984 if (dict == NULL) {
3985 PyErr_BadInternalCall();
3986 return NULL;
3987 }
3988 if (!PyDict_Check(dict)) {
3989 /* XXX Get rid of this restriction later */
3990 PyErr_Format(PyExc_TypeError,
3991 "%s() requires a dict argument, not '%s'",
3992 type->tp_name, dict->ob_type->tp_name);
3993 return NULL;
3994 }
Eric Snow96c6af92015-05-29 22:21:39 -06003995 dv = PyObject_GC_New(_PyDictViewObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003996 if (dv == NULL)
3997 return NULL;
3998 Py_INCREF(dict);
3999 dv->dv_dict = (PyDictObject *)dict;
4000 _PyObject_GC_TRACK(dv);
4001 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004002}
4003
Neal Norwitze36f2ba2007-02-26 23:12:28 +00004004/* TODO(guido): The views objects are not complete:
4005
4006 * support more set operations
4007 * support arbitrary mappings?
4008 - either these should be static or exported in dictobject.h
4009 - if public then they should probably be in builtins
4010*/
4011
Guido van Rossumaac530c2007-08-24 22:33:45 +00004012/* Return 1 if self is a subset of other, iterating over self;
4013 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00004014static int
4015all_contained_in(PyObject *self, PyObject *other)
4016{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004017 PyObject *iter = PyObject_GetIter(self);
4018 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00004019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004020 if (iter == NULL)
4021 return -1;
4022 for (;;) {
4023 PyObject *next = PyIter_Next(iter);
4024 if (next == NULL) {
4025 if (PyErr_Occurred())
4026 ok = -1;
4027 break;
4028 }
4029 ok = PySequence_Contains(other, next);
4030 Py_DECREF(next);
4031 if (ok <= 0)
4032 break;
4033 }
4034 Py_DECREF(iter);
4035 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00004036}
4037
4038static PyObject *
4039dictview_richcompare(PyObject *self, PyObject *other, int op)
4040{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004041 Py_ssize_t len_self, len_other;
4042 int ok;
4043 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004045 assert(self != NULL);
4046 assert(PyDictViewSet_Check(self));
4047 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00004048
Brian Curtindfc80e32011-08-10 20:28:54 -05004049 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
4050 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004052 len_self = PyObject_Size(self);
4053 if (len_self < 0)
4054 return NULL;
4055 len_other = PyObject_Size(other);
4056 if (len_other < 0)
4057 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004059 ok = 0;
4060 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00004061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004062 case Py_NE:
4063 case Py_EQ:
4064 if (len_self == len_other)
4065 ok = all_contained_in(self, other);
4066 if (op == Py_NE && ok >= 0)
4067 ok = !ok;
4068 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004070 case Py_LT:
4071 if (len_self < len_other)
4072 ok = all_contained_in(self, other);
4073 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004075 case Py_LE:
4076 if (len_self <= len_other)
4077 ok = all_contained_in(self, other);
4078 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004079
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004080 case Py_GT:
4081 if (len_self > len_other)
4082 ok = all_contained_in(other, self);
4083 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004085 case Py_GE:
4086 if (len_self >= len_other)
4087 ok = all_contained_in(other, self);
4088 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004090 }
4091 if (ok < 0)
4092 return NULL;
4093 result = ok ? Py_True : Py_False;
4094 Py_INCREF(result);
4095 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00004096}
4097
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004098static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004099dictview_repr(_PyDictViewObject *dv)
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004100{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004101 PyObject *seq;
bennorthd7773d92018-01-26 15:46:01 +00004102 PyObject *result = NULL;
4103 Py_ssize_t rc;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004104
bennorthd7773d92018-01-26 15:46:01 +00004105 rc = Py_ReprEnter((PyObject *)dv);
4106 if (rc != 0) {
4107 return rc > 0 ? PyUnicode_FromString("...") : NULL;
4108 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004109 seq = PySequence_List((PyObject *)dv);
bennorthd7773d92018-01-26 15:46:01 +00004110 if (seq == NULL) {
4111 goto Done;
4112 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004113 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
4114 Py_DECREF(seq);
bennorthd7773d92018-01-26 15:46:01 +00004115
4116Done:
4117 Py_ReprLeave((PyObject *)dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004118 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004119}
4120
Guido van Rossum3ac67412007-02-10 18:55:06 +00004121/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004122
4123static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004124dictkeys_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004125{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004126 if (dv->dv_dict == NULL) {
4127 Py_RETURN_NONE;
4128 }
4129 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004130}
4131
4132static int
Eric Snow96c6af92015-05-29 22:21:39 -06004133dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004134{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004135 if (dv->dv_dict == NULL)
4136 return 0;
4137 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004138}
4139
Guido van Rossum83825ac2007-02-10 04:54:19 +00004140static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004141 (lenfunc)dictview_len, /* sq_length */
4142 0, /* sq_concat */
4143 0, /* sq_repeat */
4144 0, /* sq_item */
4145 0, /* sq_slice */
4146 0, /* sq_ass_item */
4147 0, /* sq_ass_slice */
4148 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004149};
4150
Guido van Rossum523259b2007-08-24 23:41:22 +00004151static PyObject*
4152dictviews_sub(PyObject* self, PyObject *other)
4153{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004154 PyObject *result = PySet_New(self);
4155 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004156 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004158 if (result == NULL)
4159 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004160
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08004161 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004162 if (tmp == NULL) {
4163 Py_DECREF(result);
4164 return NULL;
4165 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004167 Py_DECREF(tmp);
4168 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004169}
4170
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004171PyObject*
4172_PyDictView_Intersect(PyObject* self, PyObject *other)
Guido van Rossum523259b2007-08-24 23:41:22 +00004173{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004174 PyObject *result = PySet_New(self);
4175 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004176 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004178 if (result == NULL)
4179 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004180
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08004181 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_intersection_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004182 if (tmp == NULL) {
4183 Py_DECREF(result);
4184 return NULL;
4185 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004186
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004187 Py_DECREF(tmp);
4188 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004189}
4190
4191static PyObject*
4192dictviews_or(PyObject* self, PyObject *other)
4193{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004194 PyObject *result = PySet_New(self);
4195 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004196 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02004197
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004198 if (result == NULL)
4199 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004200
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08004201 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004202 if (tmp == NULL) {
4203 Py_DECREF(result);
4204 return NULL;
4205 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004207 Py_DECREF(tmp);
4208 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004209}
4210
4211static PyObject*
4212dictviews_xor(PyObject* self, PyObject *other)
4213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004214 PyObject *result = PySet_New(self);
4215 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004216 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004218 if (result == NULL)
4219 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004220
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08004221 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_symmetric_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004222 if (tmp == NULL) {
4223 Py_DECREF(result);
4224 return NULL;
4225 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004227 Py_DECREF(tmp);
4228 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004229}
4230
4231static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004232 0, /*nb_add*/
4233 (binaryfunc)dictviews_sub, /*nb_subtract*/
4234 0, /*nb_multiply*/
4235 0, /*nb_remainder*/
4236 0, /*nb_divmod*/
4237 0, /*nb_power*/
4238 0, /*nb_negative*/
4239 0, /*nb_positive*/
4240 0, /*nb_absolute*/
4241 0, /*nb_bool*/
4242 0, /*nb_invert*/
4243 0, /*nb_lshift*/
4244 0, /*nb_rshift*/
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004245 (binaryfunc)_PyDictView_Intersect, /*nb_and*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004246 (binaryfunc)dictviews_xor, /*nb_xor*/
4247 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00004248};
4249
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004250static PyObject*
4251dictviews_isdisjoint(PyObject *self, PyObject *other)
4252{
4253 PyObject *it;
4254 PyObject *item = NULL;
4255
4256 if (self == other) {
Eric Snow96c6af92015-05-29 22:21:39 -06004257 if (dictview_len((_PyDictViewObject *)self) == 0)
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004258 Py_RETURN_TRUE;
4259 else
4260 Py_RETURN_FALSE;
4261 }
4262
4263 /* Iterate over the shorter object (only if other is a set,
4264 * because PySequence_Contains may be expensive otherwise): */
4265 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
Eric Snow96c6af92015-05-29 22:21:39 -06004266 Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self);
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004267 Py_ssize_t len_other = PyObject_Size(other);
4268 if (len_other == -1)
4269 return NULL;
4270
4271 if ((len_other > len_self)) {
4272 PyObject *tmp = other;
4273 other = self;
4274 self = tmp;
4275 }
4276 }
4277
4278 it = PyObject_GetIter(other);
4279 if (it == NULL)
4280 return NULL;
4281
4282 while ((item = PyIter_Next(it)) != NULL) {
4283 int contains = PySequence_Contains(self, item);
4284 Py_DECREF(item);
4285 if (contains == -1) {
4286 Py_DECREF(it);
4287 return NULL;
4288 }
4289
4290 if (contains) {
4291 Py_DECREF(it);
4292 Py_RETURN_FALSE;
4293 }
4294 }
4295 Py_DECREF(it);
4296 if (PyErr_Occurred())
4297 return NULL; /* PyIter_Next raised an exception. */
4298 Py_RETURN_TRUE;
4299}
4300
4301PyDoc_STRVAR(isdisjoint_doc,
4302"Return True if the view and the given iterable have a null intersection.");
4303
Serhiy Storchaka81524022018-11-27 13:05:02 +02004304static PyObject* dictkeys_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored));
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004305
4306PyDoc_STRVAR(reversed_keys_doc,
4307"Return a reverse iterator over the dict keys.");
4308
Guido van Rossumb90c8482007-02-10 01:11:45 +00004309static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004310 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4311 isdisjoint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004312 {"__reversed__", (PyCFunction)(void(*)(void))dictkeys_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004313 reversed_keys_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004314 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004315};
4316
4317PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004318 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4319 "dict_keys", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004320 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004321 0, /* tp_itemsize */
4322 /* methods */
4323 (destructor)dictview_dealloc, /* tp_dealloc */
4324 0, /* tp_print */
4325 0, /* tp_getattr */
4326 0, /* tp_setattr */
4327 0, /* tp_reserved */
4328 (reprfunc)dictview_repr, /* tp_repr */
4329 &dictviews_as_number, /* tp_as_number */
4330 &dictkeys_as_sequence, /* tp_as_sequence */
4331 0, /* tp_as_mapping */
4332 0, /* tp_hash */
4333 0, /* tp_call */
4334 0, /* tp_str */
4335 PyObject_GenericGetAttr, /* tp_getattro */
4336 0, /* tp_setattro */
4337 0, /* tp_as_buffer */
4338 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4339 0, /* tp_doc */
4340 (traverseproc)dictview_traverse, /* tp_traverse */
4341 0, /* tp_clear */
4342 dictview_richcompare, /* tp_richcompare */
4343 0, /* tp_weaklistoffset */
4344 (getiterfunc)dictkeys_iter, /* tp_iter */
4345 0, /* tp_iternext */
4346 dictkeys_methods, /* tp_methods */
4347 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004348};
4349
4350static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304351dictkeys_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004352{
Eric Snow96c6af92015-05-29 22:21:39 -06004353 return _PyDictView_New(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004354}
4355
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004356static PyObject *
Serhiy Storchaka81524022018-11-27 13:05:02 +02004357dictkeys_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored))
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004358{
4359 if (dv->dv_dict == NULL) {
4360 Py_RETURN_NONE;
4361 }
4362 return dictiter_new(dv->dv_dict, &PyDictRevIterKey_Type);
4363}
4364
Guido van Rossum3ac67412007-02-10 18:55:06 +00004365/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004366
4367static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004368dictitems_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004369{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004370 if (dv->dv_dict == NULL) {
4371 Py_RETURN_NONE;
4372 }
4373 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004374}
4375
4376static int
Eric Snow96c6af92015-05-29 22:21:39 -06004377dictitems_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004378{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004379 int result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004380 PyObject *key, *value, *found;
4381 if (dv->dv_dict == NULL)
4382 return 0;
4383 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
4384 return 0;
4385 key = PyTuple_GET_ITEM(obj, 0);
4386 value = PyTuple_GET_ITEM(obj, 1);
Raymond Hettinger6692f012016-09-18 21:46:08 -07004387 found = PyDict_GetItemWithError((PyObject *)dv->dv_dict, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004388 if (found == NULL) {
4389 if (PyErr_Occurred())
4390 return -1;
4391 return 0;
4392 }
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004393 Py_INCREF(found);
4394 result = PyObject_RichCompareBool(value, found, Py_EQ);
4395 Py_DECREF(found);
4396 return result;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004397}
4398
Guido van Rossum83825ac2007-02-10 04:54:19 +00004399static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004400 (lenfunc)dictview_len, /* sq_length */
4401 0, /* sq_concat */
4402 0, /* sq_repeat */
4403 0, /* sq_item */
4404 0, /* sq_slice */
4405 0, /* sq_ass_item */
4406 0, /* sq_ass_slice */
4407 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004408};
4409
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004410static PyObject* dictitems_reversed(_PyDictViewObject *dv);
4411
4412PyDoc_STRVAR(reversed_items_doc,
4413"Return a reverse iterator over the dict items.");
4414
Guido van Rossumb90c8482007-02-10 01:11:45 +00004415static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004416 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4417 isdisjoint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004418 {"__reversed__", (PyCFunction)(void(*)(void))dictitems_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004419 reversed_items_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004420 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004421};
4422
4423PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004424 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4425 "dict_items", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004426 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004427 0, /* tp_itemsize */
4428 /* methods */
4429 (destructor)dictview_dealloc, /* tp_dealloc */
4430 0, /* tp_print */
4431 0, /* tp_getattr */
4432 0, /* tp_setattr */
4433 0, /* tp_reserved */
4434 (reprfunc)dictview_repr, /* tp_repr */
4435 &dictviews_as_number, /* tp_as_number */
4436 &dictitems_as_sequence, /* tp_as_sequence */
4437 0, /* tp_as_mapping */
4438 0, /* tp_hash */
4439 0, /* tp_call */
4440 0, /* tp_str */
4441 PyObject_GenericGetAttr, /* tp_getattro */
4442 0, /* tp_setattro */
4443 0, /* tp_as_buffer */
4444 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4445 0, /* tp_doc */
4446 (traverseproc)dictview_traverse, /* tp_traverse */
4447 0, /* tp_clear */
4448 dictview_richcompare, /* tp_richcompare */
4449 0, /* tp_weaklistoffset */
4450 (getiterfunc)dictitems_iter, /* tp_iter */
4451 0, /* tp_iternext */
4452 dictitems_methods, /* tp_methods */
4453 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004454};
4455
4456static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304457dictitems_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004458{
Eric Snow96c6af92015-05-29 22:21:39 -06004459 return _PyDictView_New(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004460}
4461
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004462static PyObject *
4463dictitems_reversed(_PyDictViewObject *dv)
4464{
4465 if (dv->dv_dict == NULL) {
4466 Py_RETURN_NONE;
4467 }
4468 return dictiter_new(dv->dv_dict, &PyDictRevIterItem_Type);
4469}
4470
Guido van Rossum3ac67412007-02-10 18:55:06 +00004471/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004472
4473static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004474dictvalues_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004475{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004476 if (dv->dv_dict == NULL) {
4477 Py_RETURN_NONE;
4478 }
4479 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004480}
4481
Guido van Rossum83825ac2007-02-10 04:54:19 +00004482static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004483 (lenfunc)dictview_len, /* sq_length */
4484 0, /* sq_concat */
4485 0, /* sq_repeat */
4486 0, /* sq_item */
4487 0, /* sq_slice */
4488 0, /* sq_ass_item */
4489 0, /* sq_ass_slice */
4490 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004491};
4492
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004493static PyObject* dictvalues_reversed(_PyDictViewObject *dv);
4494
4495PyDoc_STRVAR(reversed_values_doc,
4496"Return a reverse iterator over the dict values.");
4497
Guido van Rossumb90c8482007-02-10 01:11:45 +00004498static PyMethodDef dictvalues_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004499 {"__reversed__", (PyCFunction)(void(*)(void))dictvalues_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004500 reversed_values_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004501 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004502};
4503
4504PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004505 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4506 "dict_values", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004507 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004508 0, /* tp_itemsize */
4509 /* methods */
4510 (destructor)dictview_dealloc, /* tp_dealloc */
4511 0, /* tp_print */
4512 0, /* tp_getattr */
4513 0, /* tp_setattr */
4514 0, /* tp_reserved */
4515 (reprfunc)dictview_repr, /* tp_repr */
4516 0, /* tp_as_number */
4517 &dictvalues_as_sequence, /* tp_as_sequence */
4518 0, /* tp_as_mapping */
4519 0, /* tp_hash */
4520 0, /* tp_call */
4521 0, /* tp_str */
4522 PyObject_GenericGetAttr, /* tp_getattro */
4523 0, /* tp_setattro */
4524 0, /* tp_as_buffer */
4525 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4526 0, /* tp_doc */
4527 (traverseproc)dictview_traverse, /* tp_traverse */
4528 0, /* tp_clear */
4529 0, /* tp_richcompare */
4530 0, /* tp_weaklistoffset */
4531 (getiterfunc)dictvalues_iter, /* tp_iter */
4532 0, /* tp_iternext */
4533 dictvalues_methods, /* tp_methods */
4534 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004535};
4536
4537static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304538dictvalues_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004539{
Eric Snow96c6af92015-05-29 22:21:39 -06004540 return _PyDictView_New(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004541}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004542
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004543static PyObject *
4544dictvalues_reversed(_PyDictViewObject *dv)
4545{
4546 if (dv->dv_dict == NULL) {
4547 Py_RETURN_NONE;
4548 }
4549 return dictiter_new(dv->dv_dict, &PyDictRevIterValue_Type);
4550}
4551
4552
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004553/* Returns NULL if cannot allocate a new PyDictKeysObject,
4554 but does not set an error */
4555PyDictKeysObject *
4556_PyDict_NewKeysForClass(void)
4557{
Victor Stinner742da042016-09-07 17:40:12 -07004558 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004559 if (keys == NULL)
4560 PyErr_Clear();
4561 else
4562 keys->dk_lookup = lookdict_split;
4563 return keys;
4564}
4565
4566#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
4567
4568PyObject *
4569PyObject_GenericGetDict(PyObject *obj, void *context)
4570{
4571 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
4572 if (dictptr == NULL) {
4573 PyErr_SetString(PyExc_AttributeError,
4574 "This object has no __dict__");
4575 return NULL;
4576 }
4577 dict = *dictptr;
4578 if (dict == NULL) {
4579 PyTypeObject *tp = Py_TYPE(obj);
4580 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
INADA Naokia7576492018-11-14 18:39:27 +09004581 dictkeys_incref(CACHED_KEYS(tp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004582 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
4583 }
4584 else {
4585 *dictptr = dict = PyDict_New();
4586 }
4587 }
4588 Py_XINCREF(dict);
4589 return dict;
4590}
4591
4592int
4593_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
Victor Stinner742da042016-09-07 17:40:12 -07004594 PyObject *key, PyObject *value)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004595{
4596 PyObject *dict;
4597 int res;
4598 PyDictKeysObject *cached;
4599
4600 assert(dictptr != NULL);
4601 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
4602 assert(dictptr != NULL);
4603 dict = *dictptr;
4604 if (dict == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +09004605 dictkeys_incref(cached);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004606 dict = new_dict_with_shared_keys(cached);
4607 if (dict == NULL)
4608 return -1;
4609 *dictptr = dict;
4610 }
4611 if (value == NULL) {
4612 res = PyDict_DelItem(dict, key);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004613 // Since key sharing dict doesn't allow deletion, PyDict_DelItem()
4614 // always converts dict to combined form.
4615 if ((cached = CACHED_KEYS(tp)) != NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004616 CACHED_KEYS(tp) = NULL;
INADA Naokia7576492018-11-14 18:39:27 +09004617 dictkeys_decref(cached);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004618 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01004619 }
4620 else {
INADA Naoki2294f3a2017-02-12 13:51:30 +09004621 int was_shared = (cached == ((PyDictObject *)dict)->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004622 res = PyDict_SetItem(dict, key, value);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004623 if (was_shared &&
4624 (cached = CACHED_KEYS(tp)) != NULL &&
4625 cached != ((PyDictObject *)dict)->ma_keys) {
Victor Stinner3d3f2642016-12-15 17:21:23 +01004626 /* PyDict_SetItem() may call dictresize and convert split table
4627 * into combined table. In such case, convert it to split
4628 * table again and update type's shared key only when this is
4629 * the only dict sharing key with the type.
4630 *
4631 * This is to allow using shared key in class like this:
4632 *
4633 * class C:
4634 * def __init__(self):
4635 * # one dict resize happens
4636 * self.a, self.b, self.c = 1, 2, 3
4637 * self.d, self.e, self.f = 4, 5, 6
4638 * a = C()
4639 */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004640 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004641 CACHED_KEYS(tp) = make_keys_shared(dict);
Victor Stinner742da042016-09-07 17:40:12 -07004642 }
4643 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004644 CACHED_KEYS(tp) = NULL;
4645 }
INADA Naokia7576492018-11-14 18:39:27 +09004646 dictkeys_decref(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004647 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
4648 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004649 }
4650 }
4651 } else {
4652 dict = *dictptr;
4653 if (dict == NULL) {
4654 dict = PyDict_New();
4655 if (dict == NULL)
4656 return -1;
4657 *dictptr = dict;
4658 }
4659 if (value == NULL) {
4660 res = PyDict_DelItem(dict, key);
4661 } else {
4662 res = PyDict_SetItem(dict, key, value);
4663 }
4664 }
4665 return res;
4666}
4667
4668void
4669_PyDictKeys_DecRef(PyDictKeysObject *keys)
4670{
INADA Naokia7576492018-11-14 18:39:27 +09004671 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004672}