blob: 773eda0d75a9c88677b326d036382d8f6a23ac30 [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
dalgarno359143c2019-09-10 10:45:07 +010042dk_entries is array of PyDictKeyEntry. Its size is USABLE_FRACTION(dk_size).
Victor Stinner742da042016-09-07 17:40:12 -070043DK_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"
Inada Naokid9323a82020-08-07 14:08:55 +0900114#include "pycore_bitutils.h" // _Py_bit_length
Victor Stinnere5014be2020-04-14 17:52:15 +0200115#include "pycore_gc.h" // _PyObject_GC_IS_TRACKED()
Victor Stinner59d3dce2020-06-02 14:03:25 +0200116#include "pycore_object.h" // _PyObject_GC_TRACK()
117#include "pycore_pyerrors.h" // _PyErr_Fetch()
Victor Stinnere5014be2020-04-14 17:52:15 +0200118#include "pycore_pystate.h" // _PyThreadState_GET()
Eric Snow96c6af92015-05-29 22:21:39 -0600119#include "dict-common.h"
Victor Stinnere5014be2020-04-14 17:52:15 +0200120#include "stringlib/eq.h" // unicode_eq()
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000121
Larry Hastings61272b72014-01-07 12:41:53 -0800122/*[clinic input]
Larry Hastingsc2047262014-01-25 20:43:29 -0800123class dict "PyDictObject *" "&PyDict_Type"
Larry Hastings61272b72014-01-07 12:41:53 -0800124[clinic start generated code]*/
Larry Hastings581ee362014-01-28 05:00:08 -0800125/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f157a5a0ce9589d6]*/
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800126
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400127
128/*
129To ensure the lookup algorithm terminates, there must be at least one Unused
130slot (NULL key) in the table.
131To avoid slowing down lookups on a near-full table, we resize the table when
132it's USABLE_FRACTION (currently two-thirds) full.
133*/
Guido van Rossum16e93a81997-01-28 00:00:11 +0000134
Tim Peterseb28ef22001-06-02 05:27:19 +0000135#define PERTURB_SHIFT 5
136
Guido van Rossum16e93a81997-01-28 00:00:11 +0000137/*
Tim Peterseb28ef22001-06-02 05:27:19 +0000138Major subtleties ahead: Most hash schemes depend on having a "good" hash
139function, in the sense of simulating randomness. Python doesn't: its most
R David Murray537ad7a2016-07-10 12:33:18 -0400140important hash functions (for ints) are very regular in common
Tim Peterseb28ef22001-06-02 05:27:19 +0000141cases:
Tim Peters15d49292001-05-27 07:39:22 +0000142
R David Murray537ad7a2016-07-10 12:33:18 -0400143 >>>[hash(i) for i in range(4)]
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000144 [0, 1, 2, 3]
Tim Peters15d49292001-05-27 07:39:22 +0000145
Tim Peterseb28ef22001-06-02 05:27:19 +0000146This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
147the low-order i bits as the initial table index is extremely fast, and there
R David Murray537ad7a2016-07-10 12:33:18 -0400148are no collisions at all for dicts indexed by a contiguous range of ints. So
149this gives better-than-random behavior in common cases, and that's very
150desirable.
Tim Peters15d49292001-05-27 07:39:22 +0000151
Tim Peterseb28ef22001-06-02 05:27:19 +0000152OTOH, when collisions occur, the tendency to fill contiguous slices of the
153hash table makes a good collision resolution strategy crucial. Taking only
154the last i bits of the hash code is also vulnerable: for example, consider
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155the list [i << 16 for i in range(20000)] as a set of keys. Since ints are
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000156their own hash codes, and this fits in a dict of size 2**15, the last 15 bits
157 of every hash code are all 0: they *all* map to the same table index.
Tim Peters15d49292001-05-27 07:39:22 +0000158
Tim Peterseb28ef22001-06-02 05:27:19 +0000159But catering to unusual cases should not slow the usual ones, so we just take
160the last i bits anyway. It's up to collision resolution to do the rest. If
161we *usually* find the key we're looking for on the first try (and, it turns
162out, we usually do -- the table load factor is kept under 2/3, so the odds
163are solidly in our favor), then it makes best sense to keep the initial index
164computation dirt cheap.
Tim Peters15d49292001-05-27 07:39:22 +0000165
Tim Peterseb28ef22001-06-02 05:27:19 +0000166The first half of collision resolution is to visit table indices via this
167recurrence:
Tim Peters15d49292001-05-27 07:39:22 +0000168
Tim Peterseb28ef22001-06-02 05:27:19 +0000169 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +0000170
Tim Peterseb28ef22001-06-02 05:27:19 +0000171For any initial j in range(2**i), repeating that 2**i times generates each
172int in range(2**i) exactly once (see any text on random-number generation for
173proof). By itself, this doesn't help much: like linear probing (setting
174j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
175order. This would be bad, except that's not the only thing we do, and it's
176actually *good* in the common cases where hash keys are consecutive. In an
177example that's really too small to make this entirely clear, for a table of
178size 2**3 the order of indices is:
Tim Peters15d49292001-05-27 07:39:22 +0000179
Tim Peterseb28ef22001-06-02 05:27:19 +0000180 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
181
182If two things come in at index 5, the first place we look after is index 2,
183not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
184Linear probing is deadly in this case because there the fixed probe order
185is the *same* as the order consecutive keys are likely to arrive. But it's
186extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
187and certain that consecutive hash codes do not.
188
189The other half of the strategy is to get the other bits of the hash code
190into play. This is done by initializing a (unsigned) vrbl "perturb" to the
191full hash code, and changing the recurrence to:
192
Tim Peterseb28ef22001-06-02 05:27:19 +0000193 perturb >>= PERTURB_SHIFT;
INADA Naoki267941c2016-10-06 15:19:07 +0900194 j = (5*j) + 1 + perturb;
Tim Peterseb28ef22001-06-02 05:27:19 +0000195 use j % 2**i as the next table index;
196
197Now the probe sequence depends (eventually) on every bit in the hash code,
198and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
199because it quickly magnifies small differences in the bits that didn't affect
200the initial index. Note that because perturb is unsigned, if the recurrence
201is executed often enough perturb eventually becomes and remains 0. At that
202point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
203that's certain to find an empty slot eventually (since it generates every int
204in range(2**i), and we make sure there's always at least one empty slot).
205
206Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
207small so that the high bits of the hash code continue to affect the probe
208sequence across iterations; but you want it large so that in really bad cases
209the high-order hash bits have an effect on early iterations. 5 was "the
210best" in minimizing total collisions across experiments Tim Peters ran (on
211both normal and pathological cases), but 4 and 6 weren't significantly worse.
212
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000213Historical: Reimer Behrends contributed the idea of using a polynomial-based
Tim Peterseb28ef22001-06-02 05:27:19 +0000214approach, using repeated multiplication by x in GF(2**n) where an irreducible
215polynomial for each table size was chosen such that x was a primitive root.
216Christian Tismer later extended that to use division by x instead, as an
217efficient way to get the high bits of the hash code into play. This scheme
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000218also gave excellent collision statistics, but was more expensive: two
219if-tests were required inside the loop; computing "the next" index took about
220the same number of operations but without as much potential parallelism
221(e.g., computing 5*j can go on at the same time as computing 1+perturb in the
222above, and then shifting perturb can be done while the table index is being
223masked); and the PyDictObject struct required a member to hold the table's
224polynomial. In Tim's experiments the current scheme ran faster, produced
225equally good collision statistics, needed less code & used less memory.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000226
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000227*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000228
Fred Drake1bff34a2000-08-31 19:31:38 +0000229/* forward declarations */
Victor Stinner742da042016-09-07 17:40:12 -0700230static Py_ssize_t lookdict(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900231 Py_hash_t hash, PyObject **value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700232static Py_ssize_t lookdict_unicode(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
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400235lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900236 Py_hash_t hash, PyObject **value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700237static Py_ssize_t lookdict_split(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900238 Py_hash_t hash, PyObject **value_addr);
Fred Drake1bff34a2000-08-31 19:31:38 +0000239
Inada Naokid9323a82020-08-07 14:08:55 +0900240static int dictresize(PyDictObject *mp, Py_ssize_t newsize);
Tim Petersdea48ec2001-05-22 20:40:22 +0000241
INADA Naoki2aaf98c2018-09-26 12:59:00 +0900242static PyObject* dict_iter(PyDictObject *dict);
243
Benjamin Peterson3c569292016-09-08 13:16:41 -0700244/*Global counter used to set ma_version_tag field of dictionary.
Victor Stinner3b6a6b42016-09-08 12:51:24 -0700245 * It is incremented each time that a dictionary is created and each
246 * time that a dictionary is modified. */
247static uint64_t pydict_global_version = 0;
248
249#define DICT_NEXT_VERSION() (++pydict_global_version)
250
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300251#include "clinic/dictobject.c.h"
252
Victor Stinner522691c2020-06-23 16:40:40 +0200253
254static struct _Py_dict_state *
255get_dict_state(void)
256{
257 PyInterpreterState *interp = _PyInterpreterState_GET();
258 return &interp->dict_state;
259}
260
261
Victor Stinnerae00a5a2020-04-29 02:29:20 +0200262void
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100263_PyDict_ClearFreeList(PyInterpreterState *interp)
Christian Heimes77c02eb2008-02-09 02:18:51 +0000264{
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100265 struct _Py_dict_state *state = &interp->dict_state;
Victor Stinnerb4e85ca2020-06-23 11:33:18 +0200266 while (state->numfree) {
267 PyDictObject *op = state->free_list[--state->numfree];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 assert(PyDict_CheckExact(op));
269 PyObject_GC_Del(op);
270 }
Victor Stinnerb4e85ca2020-06-23 11:33:18 +0200271 while (state->keys_numfree) {
Victor Stinner32bd68c2020-12-01 10:37:39 +0100272 PyObject_Free(state->keys_free_list[--state->keys_numfree]);
Victor Stinner742da042016-09-07 17:40:12 -0700273 }
Victor Stinnerb4e85ca2020-06-23 11:33:18 +0200274}
275
276
277void
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100278_PyDict_Fini(PyInterpreterState *interp)
Victor Stinnerb4e85ca2020-06-23 11:33:18 +0200279{
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100280 _PyDict_ClearFreeList(interp);
Victor Stinnerb4e85ca2020-06-23 11:33:18 +0200281#ifdef Py_DEBUG
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100282 struct _Py_dict_state *state = &interp->dict_state;
Victor Stinnerb4e85ca2020-06-23 11:33:18 +0200283 state->numfree = -1;
284 state->keys_numfree = -1;
Victor Stinnerb4b53862020-05-05 19:55:29 +0200285#endif
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100286}
287
Victor Stinnerb4e85ca2020-06-23 11:33:18 +0200288
David Malcolm49526f42012-06-22 14:55:41 -0400289/* Print summary info about the state of the optimized allocator */
290void
291_PyDict_DebugMallocStats(FILE *out)
292{
Victor Stinner522691c2020-06-23 16:40:40 +0200293 struct _Py_dict_state *state = get_dict_state();
Victor Stinnerb4e85ca2020-06-23 11:33:18 +0200294 _PyDebugAllocatorStats(out, "free PyDictObject",
295 state->numfree, sizeof(PyDictObject));
David Malcolm49526f42012-06-22 14:55:41 -0400296}
297
298
Victor Stinner742da042016-09-07 17:40:12 -0700299#define DK_SIZE(dk) ((dk)->dk_size)
300#if SIZEOF_VOID_P > 4
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700301#define DK_IXSIZE(dk) \
302 (DK_SIZE(dk) <= 0xff ? \
303 1 : DK_SIZE(dk) <= 0xffff ? \
304 2 : DK_SIZE(dk) <= 0xffffffff ? \
Benjamin Peterson3c569292016-09-08 13:16:41 -0700305 4 : sizeof(int64_t))
Victor Stinner742da042016-09-07 17:40:12 -0700306#else
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700307#define DK_IXSIZE(dk) \
308 (DK_SIZE(dk) <= 0xff ? \
309 1 : DK_SIZE(dk) <= 0xffff ? \
Benjamin Peterson3c569292016-09-08 13:16:41 -0700310 2 : sizeof(int32_t))
Victor Stinner742da042016-09-07 17:40:12 -0700311#endif
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700312#define DK_ENTRIES(dk) \
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700313 ((PyDictKeyEntry*)(&((int8_t*)((dk)->dk_indices))[DK_SIZE(dk) * DK_IXSIZE(dk)]))
Victor Stinner742da042016-09-07 17:40:12 -0700314
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400315#define DK_MASK(dk) (((dk)->dk_size)-1)
316#define IS_POWER_OF_2(x) (((x) & (x-1)) == 0)
317
INADA Naokia7576492018-11-14 18:39:27 +0900318static void free_keys_object(PyDictKeysObject *keys);
319
320static inline void
321dictkeys_incref(PyDictKeysObject *dk)
322{
Victor Stinner49932fe2020-02-03 17:55:05 +0100323#ifdef Py_REF_DEBUG
324 _Py_RefTotal++;
325#endif
INADA Naokia7576492018-11-14 18:39:27 +0900326 dk->dk_refcnt++;
327}
328
329static inline void
330dictkeys_decref(PyDictKeysObject *dk)
331{
332 assert(dk->dk_refcnt > 0);
Victor Stinner49932fe2020-02-03 17:55:05 +0100333#ifdef Py_REF_DEBUG
334 _Py_RefTotal--;
335#endif
INADA Naokia7576492018-11-14 18:39:27 +0900336 if (--dk->dk_refcnt == 0) {
337 free_keys_object(dk);
338 }
339}
340
Victor Stinner742da042016-09-07 17:40:12 -0700341/* lookup indices. returns DKIX_EMPTY, DKIX_DUMMY, or ix >=0 */
Benjamin Peterson73222252016-09-08 09:58:47 -0700342static inline Py_ssize_t
Andy Lester62d21c92020-03-25 23:13:01 -0500343dictkeys_get_index(const PyDictKeysObject *keys, Py_ssize_t i)
Victor Stinner742da042016-09-07 17:40:12 -0700344{
345 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700346 Py_ssize_t ix;
347
Victor Stinner742da042016-09-07 17:40:12 -0700348 if (s <= 0xff) {
Andy Lester62d21c92020-03-25 23:13:01 -0500349 const int8_t *indices = (const int8_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700350 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700351 }
352 else if (s <= 0xffff) {
Andy Lester62d21c92020-03-25 23:13:01 -0500353 const int16_t *indices = (const int16_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700354 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700355 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700356#if SIZEOF_VOID_P > 4
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300357 else if (s > 0xffffffff) {
Andy Lester62d21c92020-03-25 23:13:01 -0500358 const int64_t *indices = (const int64_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700359 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700360 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700361#endif
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300362 else {
Andy Lester62d21c92020-03-25 23:13:01 -0500363 const int32_t *indices = (const int32_t*)(keys->dk_indices);
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300364 ix = indices[i];
365 }
Victor Stinner71211e32016-09-08 10:52:46 -0700366 assert(ix >= DKIX_DUMMY);
367 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700368}
369
370/* write to indices. */
Benjamin Peterson73222252016-09-08 09:58:47 -0700371static inline void
INADA Naokia7576492018-11-14 18:39:27 +0900372dictkeys_set_index(PyDictKeysObject *keys, Py_ssize_t i, Py_ssize_t ix)
Victor Stinner742da042016-09-07 17:40:12 -0700373{
374 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700375
376 assert(ix >= DKIX_DUMMY);
377
Victor Stinner742da042016-09-07 17:40:12 -0700378 if (s <= 0xff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700379 int8_t *indices = (int8_t*)(keys->dk_indices);
Victor Stinner71211e32016-09-08 10:52:46 -0700380 assert(ix <= 0x7f);
Victor Stinner208857e2016-09-08 11:35:46 -0700381 indices[i] = (char)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700382 }
383 else if (s <= 0xffff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700384 int16_t *indices = (int16_t*)(keys->dk_indices);
Victor Stinner71211e32016-09-08 10:52:46 -0700385 assert(ix <= 0x7fff);
Victor Stinner208857e2016-09-08 11:35:46 -0700386 indices[i] = (int16_t)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700387 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700388#if SIZEOF_VOID_P > 4
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300389 else if (s > 0xffffffff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700390 int64_t *indices = (int64_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700391 indices[i] = ix;
Victor Stinner742da042016-09-07 17:40:12 -0700392 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700393#endif
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300394 else {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700395 int32_t *indices = (int32_t*)(keys->dk_indices);
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300396 assert(ix <= 0x7fffffff);
397 indices[i] = (int32_t)ix;
398 }
Victor Stinner742da042016-09-07 17:40:12 -0700399}
400
401
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200402/* USABLE_FRACTION is the maximum dictionary load.
Victor Stinner742da042016-09-07 17:40:12 -0700403 * Increasing this ratio makes dictionaries more dense resulting in more
404 * collisions. Decreasing it improves sparseness at the expense of spreading
405 * indices over more cache lines and at the cost of total memory consumed.
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200406 *
407 * USABLE_FRACTION must obey the following:
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400408 * (0 < USABLE_FRACTION(n) < n) for all n >= 2
409 *
Victor Stinner742da042016-09-07 17:40:12 -0700410 * USABLE_FRACTION should be quick to calculate.
411 * Fractions around 1/2 to 2/3 seem to work well in practice.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400412 */
Victor Stinner742da042016-09-07 17:40:12 -0700413#define USABLE_FRACTION(n) (((n) << 1)/3)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400414
Inada Naokid9323a82020-08-07 14:08:55 +0900415/* Find the smallest dk_size >= minsize. */
416static inline Py_ssize_t
417calculate_keysize(Py_ssize_t minsize)
418{
419#if SIZEOF_LONG == SIZEOF_SIZE_T
420 minsize = (minsize | PyDict_MINSIZE) - 1;
421 return 1LL << _Py_bit_length(minsize | (PyDict_MINSIZE-1));
422#elif defined(_MSC_VER)
423 // On 64bit Windows, sizeof(long) == 4.
424 minsize = (minsize | PyDict_MINSIZE) - 1;
425 unsigned long msb;
426 _BitScanReverse64(&msb, (uint64_t)minsize);
427 return 1LL << (msb + 1);
428#else
429 Py_ssize_t size;
430 for (size = PyDict_MINSIZE;
431 size < minsize && size > 0;
432 size <<= 1)
433 ;
434 return size;
435#endif
436}
437
438/* estimate_keysize is reverse function of USABLE_FRACTION.
439 *
Victor Stinner742da042016-09-07 17:40:12 -0700440 * This can be used to reserve enough size to insert n entries without
441 * resizing.
442 */
Inada Naokid9323a82020-08-07 14:08:55 +0900443static inline Py_ssize_t
444estimate_keysize(Py_ssize_t n)
445{
446 return calculate_keysize((n*3 + 1) / 2);
447}
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400448
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400449
Victor Stinnera9f61a52013-07-16 22:17:26 +0200450/* GROWTH_RATE. Growth rate upon hitting maximum load.
INADA Naoki5fbc5112018-04-17 15:53:34 +0900451 * Currently set to used*3.
Victor Stinnera9f61a52013-07-16 22:17:26 +0200452 * This means that dicts double in size when growing without deletions,
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700453 * but have more head room when the number of deletions is on a par with the
INADA Naoki5fbc5112018-04-17 15:53:34 +0900454 * number of insertions. See also bpo-17563 and bpo-33205.
455 *
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700456 * GROWTH_RATE was set to used*4 up to version 3.2.
457 * GROWTH_RATE was set to used*2 in version 3.3.0
INADA Naoki5fbc5112018-04-17 15:53:34 +0900458 * GROWTH_RATE was set to used*2 + capacity/2 in 3.4.0-3.6.0.
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200459 */
INADA Naoki5fbc5112018-04-17 15:53:34 +0900460#define GROWTH_RATE(d) ((d)->ma_used*3)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400461
462#define ENSURE_ALLOWS_DELETIONS(d) \
463 if ((d)->ma_keys->dk_lookup == lookdict_unicode_nodummy) { \
464 (d)->ma_keys->dk_lookup = lookdict_unicode; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400466
467/* This immutable, empty PyDictKeysObject is used for PyDict_Clear()
468 * (which cannot fail and thus can do no allocation).
469 */
470static PyDictKeysObject empty_keys_struct = {
Serhiy Storchaka97932e42016-09-26 23:01:23 +0300471 1, /* dk_refcnt */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400472 1, /* dk_size */
473 lookdict_split, /* dk_lookup */
474 0, /* dk_usable (immutable) */
Victor Stinner742da042016-09-07 17:40:12 -0700475 0, /* dk_nentries */
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700476 {DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY,
477 DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY}, /* dk_indices */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400478};
479
480static PyObject *empty_values[1] = { NULL };
481
482#define Py_EMPTY_KEYS &empty_keys_struct
483
Victor Stinner611b0fa2016-09-14 15:02:01 +0200484/* Uncomment to check the dict content in _PyDict_CheckConsistency() */
485/* #define DEBUG_PYDICT */
486
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200487#ifdef DEBUG_PYDICT
488# define ASSERT_CONSISTENT(op) assert(_PyDict_CheckConsistency((PyObject *)(op), 1))
489#else
490# define ASSERT_CONSISTENT(op) assert(_PyDict_CheckConsistency((PyObject *)(op), 0))
491#endif
Victor Stinner611b0fa2016-09-14 15:02:01 +0200492
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200493
494int
495_PyDict_CheckConsistency(PyObject *op, int check_content)
Victor Stinner611b0fa2016-09-14 15:02:01 +0200496{
Victor Stinner68762572019-10-07 18:42:01 +0200497#define CHECK(expr) \
498 do { if (!(expr)) { _PyObject_ASSERT_FAILED_MSG(op, Py_STRINGIFY(expr)); } } while (0)
499
500 assert(op != NULL);
501 CHECK(PyDict_Check(op));
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200502 PyDictObject *mp = (PyDictObject *)op;
Victor Stinner50fe3f82018-10-26 18:47:15 +0200503
Victor Stinner611b0fa2016-09-14 15:02:01 +0200504 PyDictKeysObject *keys = mp->ma_keys;
505 int splitted = _PyDict_HasSplitTable(mp);
506 Py_ssize_t usable = USABLE_FRACTION(keys->dk_size);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200507
Victor Stinner68762572019-10-07 18:42:01 +0200508 CHECK(0 <= mp->ma_used && mp->ma_used <= usable);
509 CHECK(IS_POWER_OF_2(keys->dk_size));
510 CHECK(0 <= keys->dk_usable && keys->dk_usable <= usable);
511 CHECK(0 <= keys->dk_nentries && keys->dk_nentries <= usable);
512 CHECK(keys->dk_usable + keys->dk_nentries <= usable);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200513
514 if (!splitted) {
515 /* combined table */
Victor Stinner68762572019-10-07 18:42:01 +0200516 CHECK(keys->dk_refcnt == 1);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200517 }
518
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200519 if (check_content) {
520 PyDictKeyEntry *entries = DK_ENTRIES(keys);
521 Py_ssize_t i;
Victor Stinner611b0fa2016-09-14 15:02:01 +0200522
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200523 for (i=0; i < keys->dk_size; i++) {
524 Py_ssize_t ix = dictkeys_get_index(keys, i);
Victor Stinner68762572019-10-07 18:42:01 +0200525 CHECK(DKIX_DUMMY <= ix && ix <= usable);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200526 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200527
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200528 for (i=0; i < usable; i++) {
529 PyDictKeyEntry *entry = &entries[i];
530 PyObject *key = entry->me_key;
531
532 if (key != NULL) {
533 if (PyUnicode_CheckExact(key)) {
534 Py_hash_t hash = ((PyASCIIObject *)key)->hash;
Victor Stinner68762572019-10-07 18:42:01 +0200535 CHECK(hash != -1);
536 CHECK(entry->me_hash == hash);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200537 }
538 else {
539 /* test_dict fails if PyObject_Hash() is called again */
Victor Stinner68762572019-10-07 18:42:01 +0200540 CHECK(entry->me_hash != -1);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200541 }
542 if (!splitted) {
Victor Stinner68762572019-10-07 18:42:01 +0200543 CHECK(entry->me_value != NULL);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200544 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200545 }
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200546
547 if (splitted) {
Victor Stinner68762572019-10-07 18:42:01 +0200548 CHECK(entry->me_value == NULL);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200549 }
550 }
551
552 if (splitted) {
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200553 /* splitted table */
554 for (i=0; i < mp->ma_used; i++) {
Victor Stinner68762572019-10-07 18:42:01 +0200555 CHECK(mp->ma_values[i] != NULL);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200556 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200557 }
558 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200559 return 1;
Victor Stinner68762572019-10-07 18:42:01 +0200560
561#undef CHECK
Victor Stinner611b0fa2016-09-14 15:02:01 +0200562}
Victor Stinner611b0fa2016-09-14 15:02:01 +0200563
564
Victor Stinnerb4e85ca2020-06-23 11:33:18 +0200565static PyDictKeysObject*
566new_keys_object(Py_ssize_t size)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400567{
568 PyDictKeysObject *dk;
Victor Stinner742da042016-09-07 17:40:12 -0700569 Py_ssize_t es, usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400570
Victor Stinner742da042016-09-07 17:40:12 -0700571 assert(size >= PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400572 assert(IS_POWER_OF_2(size));
Victor Stinner742da042016-09-07 17:40:12 -0700573
574 usable = USABLE_FRACTION(size);
575 if (size <= 0xff) {
576 es = 1;
577 }
578 else if (size <= 0xffff) {
579 es = 2;
580 }
581#if SIZEOF_VOID_P > 4
582 else if (size <= 0xffffffff) {
583 es = 4;
584 }
585#endif
586 else {
587 es = sizeof(Py_ssize_t);
588 }
589
Victor Stinner522691c2020-06-23 16:40:40 +0200590 struct _Py_dict_state *state = get_dict_state();
Victor Stinnerb4e85ca2020-06-23 11:33:18 +0200591#ifdef Py_DEBUG
592 // new_keys_object() must not be called after _PyDict_Fini()
593 assert(state->keys_numfree != -1);
594#endif
595 if (size == PyDict_MINSIZE && state->keys_numfree > 0) {
596 dk = state->keys_free_list[--state->keys_numfree];
Victor Stinner742da042016-09-07 17:40:12 -0700597 }
Victor Stinnerb4b53862020-05-05 19:55:29 +0200598 else
Victor Stinnerb4b53862020-05-05 19:55:29 +0200599 {
Victor Stinner32bd68c2020-12-01 10:37:39 +0100600 dk = PyObject_Malloc(sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -0700601 + es * size
602 + sizeof(PyDictKeyEntry) * usable);
Victor Stinner742da042016-09-07 17:40:12 -0700603 if (dk == NULL) {
604 PyErr_NoMemory();
605 return NULL;
606 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400607 }
Victor Stinner49932fe2020-02-03 17:55:05 +0100608#ifdef Py_REF_DEBUG
609 _Py_RefTotal++;
610#endif
INADA Naokia7576492018-11-14 18:39:27 +0900611 dk->dk_refcnt = 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400612 dk->dk_size = size;
Victor Stinner742da042016-09-07 17:40:12 -0700613 dk->dk_usable = usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400614 dk->dk_lookup = lookdict_unicode_nodummy;
Victor Stinner742da042016-09-07 17:40:12 -0700615 dk->dk_nentries = 0;
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700616 memset(&dk->dk_indices[0], 0xff, es * size);
Victor Stinner742da042016-09-07 17:40:12 -0700617 memset(DK_ENTRIES(dk), 0, sizeof(PyDictKeyEntry) * usable);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400618 return dk;
619}
620
621static void
622free_keys_object(PyDictKeysObject *keys)
623{
Victor Stinner742da042016-09-07 17:40:12 -0700624 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400625 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -0700626 for (i = 0, n = keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400627 Py_XDECREF(entries[i].me_key);
628 Py_XDECREF(entries[i].me_value);
629 }
Victor Stinner522691c2020-06-23 16:40:40 +0200630 struct _Py_dict_state *state = get_dict_state();
Victor Stinnerb4e85ca2020-06-23 11:33:18 +0200631#ifdef Py_DEBUG
632 // free_keys_object() must not be called after _PyDict_Fini()
633 assert(state->keys_numfree != -1);
634#endif
635 if (keys->dk_size == PyDict_MINSIZE && state->keys_numfree < PyDict_MAXFREELIST) {
636 state->keys_free_list[state->keys_numfree++] = keys;
Victor Stinner742da042016-09-07 17:40:12 -0700637 return;
638 }
Victor Stinner32bd68c2020-12-01 10:37:39 +0100639 PyObject_Free(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400640}
641
642#define new_values(size) PyMem_NEW(PyObject *, size)
Victor Stinner00d7abd2020-12-01 09:56:42 +0100643#define free_values(values) PyMem_Free(values)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400644
645/* Consumes a reference to the keys object */
646static PyObject *
647new_dict(PyDictKeysObject *keys, PyObject **values)
648{
649 PyDictObject *mp;
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200650 assert(keys != NULL);
Victor Stinner522691c2020-06-23 16:40:40 +0200651 struct _Py_dict_state *state = get_dict_state();
Victor Stinnerb4e85ca2020-06-23 11:33:18 +0200652#ifdef Py_DEBUG
653 // new_dict() must not be called after _PyDict_Fini()
654 assert(state->numfree != -1);
655#endif
656 if (state->numfree) {
657 mp = state->free_list[--state->numfree];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 assert (mp != NULL);
Dong-hee Na1b55b652020-02-17 19:09:15 +0900659 assert (Py_IS_TYPE(mp, &PyDict_Type));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 _Py_NewReference((PyObject *)mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 }
Victor Stinnerb4e85ca2020-06-23 11:33:18 +0200662 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400663 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
664 if (mp == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +0900665 dictkeys_decref(keys);
Zackery Spytz3d07c1e2019-03-23 20:23:29 -0600666 if (values != empty_values) {
667 free_values(values);
668 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400669 return NULL;
670 }
671 }
672 mp->ma_keys = keys;
673 mp->ma_values = values;
674 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -0700675 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200676 ASSERT_CONSISTENT(mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000678}
679
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400680/* Consumes a reference to the keys object */
681static PyObject *
682new_dict_with_shared_keys(PyDictKeysObject *keys)
683{
684 PyObject **values;
685 Py_ssize_t i, size;
686
Victor Stinner742da042016-09-07 17:40:12 -0700687 size = USABLE_FRACTION(DK_SIZE(keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400688 values = new_values(size);
689 if (values == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +0900690 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400691 return PyErr_NoMemory();
692 }
693 for (i = 0; i < size; i++) {
694 values[i] = NULL;
695 }
696 return new_dict(keys, values);
697}
698
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500699
Inada Naokidb6d9a52020-08-04 11:08:06 +0900700static PyDictKeysObject *
701clone_combined_dict_keys(PyDictObject *orig)
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500702{
Inada Naokidb6d9a52020-08-04 11:08:06 +0900703 assert(PyDict_Check(orig));
704 assert(Py_TYPE(orig)->tp_iter == (getiterfunc)dict_iter);
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500705 assert(orig->ma_values == NULL);
706 assert(orig->ma_keys->dk_refcnt == 1);
707
708 Py_ssize_t keys_size = _PyDict_KeysSize(orig->ma_keys);
709 PyDictKeysObject *keys = PyObject_Malloc(keys_size);
710 if (keys == NULL) {
711 PyErr_NoMemory();
712 return NULL;
713 }
714
715 memcpy(keys, orig->ma_keys, keys_size);
716
717 /* After copying key/value pairs, we need to incref all
718 keys and values and they are about to be co-owned by a
719 new dict object. */
720 PyDictKeyEntry *ep0 = DK_ENTRIES(keys);
721 Py_ssize_t n = keys->dk_nentries;
722 for (Py_ssize_t i = 0; i < n; i++) {
723 PyDictKeyEntry *entry = &ep0[i];
724 PyObject *value = entry->me_value;
725 if (value != NULL) {
726 Py_INCREF(value);
727 Py_INCREF(entry->me_key);
728 }
729 }
730
Yury Selivanov0b752282018-07-06 12:20:07 -0400731 /* Since we copied the keys table we now have an extra reference
Victor Stinner49932fe2020-02-03 17:55:05 +0100732 in the system. Manually call increment _Py_RefTotal to signal that
INADA Naokia7576492018-11-14 18:39:27 +0900733 we have it now; calling dictkeys_incref would be an error as
Yury Selivanov0b752282018-07-06 12:20:07 -0400734 keys->dk_refcnt is already set to 1 (after memcpy). */
Victor Stinner49932fe2020-02-03 17:55:05 +0100735#ifdef Py_REF_DEBUG
736 _Py_RefTotal++;
737#endif
Inada Naokidb6d9a52020-08-04 11:08:06 +0900738 return keys;
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500739}
740
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400741PyObject *
742PyDict_New(void)
743{
Inada Naokif2a18672019-03-12 17:25:44 +0900744 dictkeys_incref(Py_EMPTY_KEYS);
745 return new_dict(Py_EMPTY_KEYS, empty_values);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400746}
747
Victor Stinner742da042016-09-07 17:40:12 -0700748/* Search index of hash table from offset of entry table */
749static Py_ssize_t
750lookdict_index(PyDictKeysObject *k, Py_hash_t hash, Py_ssize_t index)
751{
Victor Stinner742da042016-09-07 17:40:12 -0700752 size_t mask = DK_MASK(k);
INADA Naoki073ae482017-06-23 15:22:50 +0900753 size_t perturb = (size_t)hash;
754 size_t i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700755
INADA Naoki073ae482017-06-23 15:22:50 +0900756 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900757 Py_ssize_t ix = dictkeys_get_index(k, i);
Victor Stinner742da042016-09-07 17:40:12 -0700758 if (ix == index) {
759 return i;
760 }
761 if (ix == DKIX_EMPTY) {
762 return DKIX_EMPTY;
763 }
INADA Naoki073ae482017-06-23 15:22:50 +0900764 perturb >>= PERTURB_SHIFT;
765 i = mask & (i*5 + perturb + 1);
Victor Stinner742da042016-09-07 17:40:12 -0700766 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700767 Py_UNREACHABLE();
Victor Stinner742da042016-09-07 17:40:12 -0700768}
769
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000770/*
771The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000772This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000773Open addressing is preferred over chaining since the link overhead for
774chaining would be substantial (100% with typical malloc overhead).
775
Tim Peterseb28ef22001-06-02 05:27:19 +0000776The initial probe index is computed as hash mod the table size. Subsequent
777probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000778
779All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000780
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000781The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000782contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000783Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000784
Victor Stinner742da042016-09-07 17:40:12 -0700785lookdict() is general-purpose, and may return DKIX_ERROR if (and only if) a
Victor Stinnera4348cc2016-09-08 12:01:25 -0700786comparison raises an exception.
Guido van Rossum89d8c602007-09-18 17:26:56 +0000787lookdict_unicode() below is specialized to string keys, comparison of which can
INADA Naoki1b8df102017-02-20 22:48:10 +0900788never raise an exception; that function can never return DKIX_ERROR when key
789is string. Otherwise, it falls back to lookdict().
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400790lookdict_unicode_nodummy is further specialized for string keys that cannot be
791the <dummy> value.
INADA Naoki778928b2017-08-03 23:45:15 +0900792For both, when the key isn't found a DKIX_EMPTY is returned.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000793*/
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100794static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400795lookdict(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900796 Py_hash_t hash, PyObject **value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000797{
INADA Naoki778928b2017-08-03 23:45:15 +0900798 size_t i, mask, perturb;
Victor Stinner742da042016-09-07 17:40:12 -0700799 PyDictKeysObject *dk;
INADA Naoki778928b2017-08-03 23:45:15 +0900800 PyDictKeyEntry *ep0;
Tim Peterseb28ef22001-06-02 05:27:19 +0000801
Antoine Pitrou9a234902012-05-13 20:48:01 +0200802top:
Victor Stinner742da042016-09-07 17:40:12 -0700803 dk = mp->ma_keys;
Victor Stinner742da042016-09-07 17:40:12 -0700804 ep0 = DK_ENTRIES(dk);
INADA Naoki778928b2017-08-03 23:45:15 +0900805 mask = DK_MASK(dk);
806 perturb = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700808
INADA Naoki778928b2017-08-03 23:45:15 +0900809 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900810 Py_ssize_t ix = dictkeys_get_index(dk, i);
Victor Stinner742da042016-09-07 17:40:12 -0700811 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700812 *value_addr = NULL;
813 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400814 }
INADA Naoki778928b2017-08-03 23:45:15 +0900815 if (ix >= 0) {
816 PyDictKeyEntry *ep = &ep0[ix];
817 assert(ep->me_key != NULL);
818 if (ep->me_key == key) {
819 *value_addr = ep->me_value;
820 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700821 }
INADA Naoki778928b2017-08-03 23:45:15 +0900822 if (ep->me_hash == hash) {
823 PyObject *startkey = ep->me_key;
824 Py_INCREF(startkey);
825 int cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
826 Py_DECREF(startkey);
827 if (cmp < 0) {
828 *value_addr = NULL;
829 return DKIX_ERROR;
830 }
831 if (dk == mp->ma_keys && ep->me_key == startkey) {
832 if (cmp > 0) {
833 *value_addr = ep->me_value;
834 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700835 }
INADA Naoki778928b2017-08-03 23:45:15 +0900836 }
837 else {
838 /* The dict was mutated, restart */
839 goto top;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400840 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 }
INADA Naoki778928b2017-08-03 23:45:15 +0900843 perturb >>= PERTURB_SHIFT;
844 i = (i*5 + perturb + 1) & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700846 Py_UNREACHABLE();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000847}
848
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400849/* Specialized version for string-only keys */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100850static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400851lookdict_unicode(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900852 Py_hash_t hash, PyObject **value_addr)
Fred Drake1bff34a2000-08-31 19:31:38 +0000853{
Victor Stinner742da042016-09-07 17:40:12 -0700854 assert(mp->ma_values == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 /* Make sure this function doesn't have to handle non-unicode keys,
856 including subclasses of str; e.g., one reason to subclass
857 unicodes is to override __eq__, and for speed we don't cater to
858 that here. */
859 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400860 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900861 return lookdict(mp, key, hash, value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 }
Tim Peters15d49292001-05-27 07:39:22 +0000863
INADA Naoki778928b2017-08-03 23:45:15 +0900864 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
865 size_t mask = DK_MASK(mp->ma_keys);
866 size_t perturb = (size_t)hash;
867 size_t i = (size_t)hash & mask;
868
869 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900870 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
Victor Stinner742da042016-09-07 17:40:12 -0700871 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700872 *value_addr = NULL;
873 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400874 }
INADA Naoki778928b2017-08-03 23:45:15 +0900875 if (ix >= 0) {
876 PyDictKeyEntry *ep = &ep0[ix];
877 assert(ep->me_key != NULL);
878 assert(PyUnicode_CheckExact(ep->me_key));
879 if (ep->me_key == key ||
880 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
881 *value_addr = ep->me_value;
882 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700883 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400884 }
INADA Naoki778928b2017-08-03 23:45:15 +0900885 perturb >>= PERTURB_SHIFT;
886 i = mask & (i*5 + perturb + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700888 Py_UNREACHABLE();
Fred Drake1bff34a2000-08-31 19:31:38 +0000889}
890
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400891/* Faster version of lookdict_unicode when it is known that no <dummy> keys
892 * will be present. */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100893static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400894lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900895 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400896{
Victor Stinner742da042016-09-07 17:40:12 -0700897 assert(mp->ma_values == NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400898 /* Make sure this function doesn't have to handle non-unicode keys,
899 including subclasses of str; e.g., one reason to subclass
900 unicodes is to override __eq__, and for speed we don't cater to
901 that here. */
902 if (!PyUnicode_CheckExact(key)) {
903 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900904 return lookdict(mp, key, hash, value_addr);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400905 }
INADA Naoki778928b2017-08-03 23:45:15 +0900906
907 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
908 size_t mask = DK_MASK(mp->ma_keys);
909 size_t perturb = (size_t)hash;
910 size_t i = (size_t)hash & mask;
911
912 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900913 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
Victor Stinner742da042016-09-07 17:40:12 -0700914 assert (ix != DKIX_DUMMY);
915 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700916 *value_addr = NULL;
917 return DKIX_EMPTY;
918 }
INADA Naoki778928b2017-08-03 23:45:15 +0900919 PyDictKeyEntry *ep = &ep0[ix];
920 assert(ep->me_key != NULL);
921 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700922 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400923 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900924 *value_addr = ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700925 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400926 }
INADA Naoki778928b2017-08-03 23:45:15 +0900927 perturb >>= PERTURB_SHIFT;
928 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400929 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700930 Py_UNREACHABLE();
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400931}
932
933/* Version of lookdict for split tables.
934 * All split tables and only split tables use this lookup function.
935 * Split tables only contain unicode keys and no dummy keys,
936 * so algorithm is the same as lookdict_unicode_nodummy.
937 */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100938static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400939lookdict_split(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900940 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400941{
Victor Stinner742da042016-09-07 17:40:12 -0700942 /* mp must split table */
943 assert(mp->ma_values != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400944 if (!PyUnicode_CheckExact(key)) {
INADA Naoki778928b2017-08-03 23:45:15 +0900945 Py_ssize_t ix = lookdict(mp, key, hash, value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700946 if (ix >= 0) {
INADA Naokiba609772016-12-07 20:41:42 +0900947 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700948 }
949 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400950 }
Victor Stinner742da042016-09-07 17:40:12 -0700951
INADA Naoki778928b2017-08-03 23:45:15 +0900952 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
953 size_t mask = DK_MASK(mp->ma_keys);
954 size_t perturb = (size_t)hash;
955 size_t i = (size_t)hash & mask;
956
957 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900958 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
INADA Naoki778928b2017-08-03 23:45:15 +0900959 assert (ix != DKIX_DUMMY);
Victor Stinner742da042016-09-07 17:40:12 -0700960 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700961 *value_addr = NULL;
962 return DKIX_EMPTY;
963 }
INADA Naoki778928b2017-08-03 23:45:15 +0900964 PyDictKeyEntry *ep = &ep0[ix];
965 assert(ep->me_key != NULL);
966 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700967 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400968 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900969 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700970 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400971 }
INADA Naoki778928b2017-08-03 23:45:15 +0900972 perturb >>= PERTURB_SHIFT;
973 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400974 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700975 Py_UNREACHABLE();
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400976}
977
Benjamin Petersonfb886362010-04-24 18:21:17 +0000978int
979_PyDict_HasOnlyStringKeys(PyObject *dict)
980{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 Py_ssize_t pos = 0;
982 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000983 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400985 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 return 1;
987 while (PyDict_Next(dict, &pos, &key, &value))
988 if (!PyUnicode_Check(key))
989 return 0;
990 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000991}
992
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000993#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 do { \
995 if (!_PyObject_GC_IS_TRACKED(mp)) { \
996 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
997 _PyObject_GC_MAY_BE_TRACKED(value)) { \
998 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 } \
1000 } \
1001 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001002
1003void
1004_PyDict_MaybeUntrack(PyObject *op)
1005{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 PyDictObject *mp;
1007 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -07001008 Py_ssize_t i, numentries;
1009 PyDictKeyEntry *ep0;
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
1012 return;
1013
1014 mp = (PyDictObject *) op;
Victor Stinner742da042016-09-07 17:40:12 -07001015 ep0 = DK_ENTRIES(mp->ma_keys);
1016 numentries = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001017 if (_PyDict_HasSplitTable(mp)) {
Victor Stinner742da042016-09-07 17:40:12 -07001018 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001019 if ((value = mp->ma_values[i]) == NULL)
1020 continue;
1021 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
Victor Stinner742da042016-09-07 17:40:12 -07001022 assert(!_PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001023 return;
1024 }
1025 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001027 else {
Victor Stinner742da042016-09-07 17:40:12 -07001028 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001029 if ((value = ep0[i].me_value) == NULL)
1030 continue;
1031 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
1032 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
1033 return;
1034 }
1035 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001037}
1038
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001039/* Internal function to find slot for an item from its hash
Victor Stinner3c336c52016-09-12 14:17:40 +02001040 when it is known that the key is not present in the dict.
1041
1042 The dict must be combined. */
INADA Naokiba609772016-12-07 20:41:42 +09001043static Py_ssize_t
INADA Naoki778928b2017-08-03 23:45:15 +09001044find_empty_slot(PyDictKeysObject *keys, Py_hash_t hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001045{
INADA Naoki778928b2017-08-03 23:45:15 +09001046 assert(keys != NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001047
INADA Naoki778928b2017-08-03 23:45:15 +09001048 const size_t mask = DK_MASK(keys);
1049 size_t i = hash & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001050 Py_ssize_t ix = dictkeys_get_index(keys, i);
INADA Naoki778928b2017-08-03 23:45:15 +09001051 for (size_t perturb = hash; ix >= 0;) {
INADA Naoki267941c2016-10-06 15:19:07 +09001052 perturb >>= PERTURB_SHIFT;
INADA Naoki778928b2017-08-03 23:45:15 +09001053 i = (i*5 + perturb + 1) & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001054 ix = dictkeys_get_index(keys, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 }
INADA Naoki778928b2017-08-03 23:45:15 +09001056 return i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001057}
1058
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001059static int
1060insertion_resize(PyDictObject *mp)
1061{
Inada Naokid9323a82020-08-07 14:08:55 +09001062 return dictresize(mp, calculate_keysize(GROWTH_RATE(mp)));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001063}
Antoine Pitroue965d972012-02-27 00:45:12 +01001064
1065/*
1066Internal routine to insert a new item into the table.
1067Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +01001068Returns -1 if an error occurred, or 0 on success.
1069*/
1070static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001071insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001072{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001073 PyObject *old_value;
INADA Naokiba609772016-12-07 20:41:42 +09001074 PyDictKeyEntry *ep;
Antoine Pitroue965d972012-02-27 00:45:12 +01001075
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001076 Py_INCREF(key);
1077 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001078 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
1079 if (insertion_resize(mp) < 0)
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001080 goto Fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001081 }
1082
INADA Naoki778928b2017-08-03 23:45:15 +09001083 Py_ssize_t ix = mp->ma_keys->dk_lookup(mp, key, hash, &old_value);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001084 if (ix == DKIX_ERROR)
1085 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001086
Antoine Pitroud6967322014-10-18 00:35:00 +02001087 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001088 MAINTAIN_TRACKING(mp, key, value);
Victor Stinner742da042016-09-07 17:40:12 -07001089
1090 /* When insertion order is different from shared key, we can't share
1091 * the key anymore. Convert this instance to combine table.
1092 */
1093 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09001094 ((ix >= 0 && old_value == NULL && mp->ma_used != ix) ||
Victor Stinner742da042016-09-07 17:40:12 -07001095 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001096 if (insertion_resize(mp) < 0)
1097 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001098 ix = DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001099 }
Victor Stinner742da042016-09-07 17:40:12 -07001100
1101 if (ix == DKIX_EMPTY) {
1102 /* Insert into new slot. */
INADA Naokiba609772016-12-07 20:41:42 +09001103 assert(old_value == NULL);
Victor Stinner742da042016-09-07 17:40:12 -07001104 if (mp->ma_keys->dk_usable <= 0) {
1105 /* Need to resize. */
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001106 if (insertion_resize(mp) < 0)
1107 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001108 }
INADA Naoki778928b2017-08-03 23:45:15 +09001109 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naokiba609772016-12-07 20:41:42 +09001110 ep = &DK_ENTRIES(mp->ma_keys)[mp->ma_keys->dk_nentries];
INADA Naokia7576492018-11-14 18:39:27 +09001111 dictkeys_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Victor Stinner742da042016-09-07 17:40:12 -07001112 ep->me_key = key;
1113 ep->me_hash = hash;
1114 if (mp->ma_values) {
1115 assert (mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
1116 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001117 }
1118 else {
Victor Stinner742da042016-09-07 17:40:12 -07001119 ep->me_value = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001120 }
1121 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001122 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07001123 mp->ma_keys->dk_usable--;
1124 mp->ma_keys->dk_nentries++;
1125 assert(mp->ma_keys->dk_usable >= 0);
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001126 ASSERT_CONSISTENT(mp);
Victor Stinner742da042016-09-07 17:40:12 -07001127 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001128 }
Victor Stinner742da042016-09-07 17:40:12 -07001129
Inada Naoki91234a12019-06-03 21:30:58 +09001130 if (old_value != value) {
1131 if (_PyDict_HasSplitTable(mp)) {
1132 mp->ma_values[ix] = value;
1133 if (old_value == NULL) {
1134 /* pending state */
1135 assert(ix == mp->ma_used);
1136 mp->ma_used++;
1137 }
INADA Naokiba609772016-12-07 20:41:42 +09001138 }
Inada Naoki91234a12019-06-03 21:30:58 +09001139 else {
1140 assert(old_value != NULL);
1141 DK_ENTRIES(mp->ma_keys)[ix].me_value = value;
1142 }
1143 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokiba609772016-12-07 20:41:42 +09001144 }
INADA Naokiba609772016-12-07 20:41:42 +09001145 Py_XDECREF(old_value); /* which **CAN** re-enter (see issue #22653) */
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001146 ASSERT_CONSISTENT(mp);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001147 Py_DECREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001148 return 0;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001149
1150Fail:
1151 Py_DECREF(value);
1152 Py_DECREF(key);
1153 return -1;
Antoine Pitroue965d972012-02-27 00:45:12 +01001154}
1155
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001156// Same to insertdict but specialized for ma_keys = Py_EMPTY_KEYS.
1157static int
1158insert_to_emptydict(PyDictObject *mp, PyObject *key, Py_hash_t hash,
1159 PyObject *value)
1160{
1161 assert(mp->ma_keys == Py_EMPTY_KEYS);
1162
1163 PyDictKeysObject *newkeys = new_keys_object(PyDict_MINSIZE);
1164 if (newkeys == NULL) {
1165 return -1;
1166 }
1167 if (!PyUnicode_CheckExact(key)) {
1168 newkeys->dk_lookup = lookdict;
1169 }
1170 dictkeys_decref(Py_EMPTY_KEYS);
1171 mp->ma_keys = newkeys;
1172 mp->ma_values = NULL;
1173
1174 Py_INCREF(key);
1175 Py_INCREF(value);
1176 MAINTAIN_TRACKING(mp, key, value);
1177
1178 size_t hashpos = (size_t)hash & (PyDict_MINSIZE-1);
Dong-hee Nac39d1dd2019-10-11 17:43:11 +09001179 PyDictKeyEntry *ep = DK_ENTRIES(mp->ma_keys);
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001180 dictkeys_set_index(mp->ma_keys, hashpos, 0);
1181 ep->me_key = key;
1182 ep->me_hash = hash;
1183 ep->me_value = value;
1184 mp->ma_used++;
1185 mp->ma_version_tag = DICT_NEXT_VERSION();
1186 mp->ma_keys->dk_usable--;
1187 mp->ma_keys->dk_nentries++;
1188 return 0;
1189}
1190
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001191/*
luzpaza5293b42017-11-05 07:37:50 -06001192Internal routine used by dictresize() to build a hashtable of entries.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001193*/
1194static void
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001195build_indices(PyDictKeysObject *keys, PyDictKeyEntry *ep, Py_ssize_t n)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001196{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001197 size_t mask = (size_t)DK_SIZE(keys) - 1;
1198 for (Py_ssize_t ix = 0; ix != n; ix++, ep++) {
1199 Py_hash_t hash = ep->me_hash;
1200 size_t i = hash & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001201 for (size_t perturb = hash; dictkeys_get_index(keys, i) != DKIX_EMPTY;) {
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001202 perturb >>= PERTURB_SHIFT;
INADA Naoki870c2862017-06-24 09:03:19 +09001203 i = mask & (i*5 + perturb + 1);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001204 }
INADA Naokia7576492018-11-14 18:39:27 +09001205 dictkeys_set_index(keys, i, ix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001207}
1208
1209/*
1210Restructure the table by allocating a new table and reinserting all
1211items again. When entries have been deleted, the new table may
1212actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001213If a table is split (its keys and hashes are shared, its values are not),
1214then the values are temporarily copied into the table, it is resized as
1215a combined table, then the me_value slots in the old table are NULLed out.
1216After resizing a table is always combined,
1217but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001218*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001219static int
Inada Naokid9323a82020-08-07 14:08:55 +09001220dictresize(PyDictObject *mp, Py_ssize_t newsize)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001221{
Inada Naokid9323a82020-08-07 14:08:55 +09001222 Py_ssize_t numentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001223 PyDictKeysObject *oldkeys;
1224 PyObject **oldvalues;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001225 PyDictKeyEntry *oldentries, *newentries;
Tim Peters91a364d2001-05-19 07:04:38 +00001226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 if (newsize <= 0) {
1228 PyErr_NoMemory();
1229 return -1;
1230 }
Inada Naokid9323a82020-08-07 14:08:55 +09001231 assert(IS_POWER_OF_2(newsize));
1232 assert(newsize >= PyDict_MINSIZE);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001233
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001234 oldkeys = mp->ma_keys;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001235
1236 /* NOTE: Current odict checks mp->ma_keys to detect resize happen.
1237 * So we can't reuse oldkeys even if oldkeys->dk_size == newsize.
1238 * TODO: Try reusing oldkeys when reimplement odict.
1239 */
1240
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001241 /* Allocate a new table. */
1242 mp->ma_keys = new_keys_object(newsize);
1243 if (mp->ma_keys == NULL) {
1244 mp->ma_keys = oldkeys;
1245 return -1;
1246 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01001247 // New table must be large enough.
1248 assert(mp->ma_keys->dk_usable >= mp->ma_used);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001249 if (oldkeys->dk_lookup == lookdict)
1250 mp->ma_keys->dk_lookup = lookdict;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001251
1252 numentries = mp->ma_used;
1253 oldentries = DK_ENTRIES(oldkeys);
1254 newentries = DK_ENTRIES(mp->ma_keys);
1255 oldvalues = mp->ma_values;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001256 if (oldvalues != NULL) {
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001257 /* Convert split table into new combined table.
1258 * We must incref keys; we can transfer values.
1259 * Note that values of split table is always dense.
1260 */
1261 for (Py_ssize_t i = 0; i < numentries; i++) {
1262 assert(oldvalues[i] != NULL);
1263 PyDictKeyEntry *ep = &oldentries[i];
1264 PyObject *key = ep->me_key;
1265 Py_INCREF(key);
1266 newentries[i].me_key = key;
1267 newentries[i].me_hash = ep->me_hash;
1268 newentries[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001270
INADA Naokia7576492018-11-14 18:39:27 +09001271 dictkeys_decref(oldkeys);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001272 mp->ma_values = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001273 if (oldvalues != empty_values) {
1274 free_values(oldvalues);
1275 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001276 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001277 else { // combined table.
1278 if (oldkeys->dk_nentries == numentries) {
1279 memcpy(newentries, oldentries, numentries * sizeof(PyDictKeyEntry));
1280 }
1281 else {
1282 PyDictKeyEntry *ep = oldentries;
1283 for (Py_ssize_t i = 0; i < numentries; i++) {
1284 while (ep->me_value == NULL)
1285 ep++;
1286 newentries[i] = *ep++;
1287 }
1288 }
1289
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001290 assert(oldkeys->dk_lookup != lookdict_split);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001291 assert(oldkeys->dk_refcnt == 1);
Victor Stinner49932fe2020-02-03 17:55:05 +01001292#ifdef Py_REF_DEBUG
1293 _Py_RefTotal--;
1294#endif
Victor Stinner522691c2020-06-23 16:40:40 +02001295 struct _Py_dict_state *state = get_dict_state();
Victor Stinnerb4e85ca2020-06-23 11:33:18 +02001296#ifdef Py_DEBUG
1297 // dictresize() must not be called after _PyDict_Fini()
1298 assert(state->keys_numfree != -1);
Victor Stinnerb4b53862020-05-05 19:55:29 +02001299#endif
Victor Stinnerb4e85ca2020-06-23 11:33:18 +02001300 if (oldkeys->dk_size == PyDict_MINSIZE &&
1301 state->keys_numfree < PyDict_MAXFREELIST)
Victor Stinnerb4b53862020-05-05 19:55:29 +02001302 {
Victor Stinnerb4e85ca2020-06-23 11:33:18 +02001303 state->keys_free_list[state->keys_numfree++] = oldkeys;
1304 }
1305 else {
Victor Stinner32bd68c2020-12-01 10:37:39 +01001306 PyObject_Free(oldkeys);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001307 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001308 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001309
1310 build_indices(mp->ma_keys, newentries, numentries);
1311 mp->ma_keys->dk_usable -= numentries;
1312 mp->ma_keys->dk_nentries = numentries;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001314}
1315
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001316/* Returns NULL if unable to split table.
1317 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001318static PyDictKeysObject *
1319make_keys_shared(PyObject *op)
1320{
1321 Py_ssize_t i;
1322 Py_ssize_t size;
1323 PyDictObject *mp = (PyDictObject *)op;
1324
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001325 if (!PyDict_CheckExact(op))
1326 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001327 if (!_PyDict_HasSplitTable(mp)) {
1328 PyDictKeyEntry *ep0;
1329 PyObject **values;
1330 assert(mp->ma_keys->dk_refcnt == 1);
1331 if (mp->ma_keys->dk_lookup == lookdict) {
1332 return NULL;
1333 }
1334 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
1335 /* Remove dummy keys */
1336 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
1337 return NULL;
1338 }
1339 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
1340 /* Copy values into a new array */
Victor Stinner742da042016-09-07 17:40:12 -07001341 ep0 = DK_ENTRIES(mp->ma_keys);
1342 size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001343 values = new_values(size);
1344 if (values == NULL) {
1345 PyErr_SetString(PyExc_MemoryError,
1346 "Not enough memory to allocate new values array");
1347 return NULL;
1348 }
1349 for (i = 0; i < size; i++) {
1350 values[i] = ep0[i].me_value;
1351 ep0[i].me_value = NULL;
1352 }
1353 mp->ma_keys->dk_lookup = lookdict_split;
1354 mp->ma_values = values;
1355 }
INADA Naokia7576492018-11-14 18:39:27 +09001356 dictkeys_incref(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001357 return mp->ma_keys;
1358}
Christian Heimes99170a52007-12-19 02:07:34 +00001359
1360PyObject *
1361_PyDict_NewPresized(Py_ssize_t minused)
1362{
INADA Naoki92c50ee2016-11-22 00:57:02 +09001363 const Py_ssize_t max_presize = 128 * 1024;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001364 Py_ssize_t newsize;
1365 PyDictKeysObject *new_keys;
INADA Naoki92c50ee2016-11-22 00:57:02 +09001366
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001367 if (minused <= USABLE_FRACTION(PyDict_MINSIZE)) {
Inada Naokif2a18672019-03-12 17:25:44 +09001368 return PyDict_New();
1369 }
INADA Naoki92c50ee2016-11-22 00:57:02 +09001370 /* There are no strict guarantee that returned dict can contain minused
1371 * items without resize. So we create medium size dict instead of very
1372 * large dict or MemoryError.
1373 */
1374 if (minused > USABLE_FRACTION(max_presize)) {
1375 newsize = max_presize;
1376 }
1377 else {
Inada Naokid9323a82020-08-07 14:08:55 +09001378 newsize = estimate_keysize(minused);
INADA Naoki92c50ee2016-11-22 00:57:02 +09001379 }
INADA Naoki92c50ee2016-11-22 00:57:02 +09001380
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001381 new_keys = new_keys_object(newsize);
1382 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001384 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001385}
1386
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001387/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1388 * that may occur (originally dicts supported only string keys, and exceptions
1389 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001390 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001391 * (suppressed) occurred while computing the key's hash, or that some error
1392 * (suppressed) occurred when comparing keys in the dict's internal probe
1393 * sequence. A nasty example of the latter is when a Python-coded comparison
1394 * function hits a stack-depth error, which can cause this to return NULL
1395 * even if the key is present.
1396 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001397PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001398PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001399{
Victor Stinner59d3dce2020-06-02 14:03:25 +02001400 if (!PyDict_Check(op)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 return NULL;
Victor Stinner59d3dce2020-06-02 14:03:25 +02001402 }
1403 PyDictObject *mp = (PyDictObject *)op;
1404
1405 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001407 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 {
1409 hash = PyObject_Hash(key);
1410 if (hash == -1) {
1411 PyErr_Clear();
1412 return NULL;
1413 }
1414 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001415
Victor Stinner59d3dce2020-06-02 14:03:25 +02001416 PyThreadState *tstate = _PyThreadState_GET();
1417#ifdef Py_DEBUG
1418 // bpo-40839: Before Python 3.10, it was possible to call PyDict_GetItem()
1419 // with the GIL released.
1420 _Py_EnsureTstateNotNULL(tstate);
1421#endif
1422
1423 /* Preserve the existing exception */
1424 PyObject *exc_type, *exc_value, *exc_tb;
1425 PyObject *value;
1426 Py_ssize_t ix;
1427
1428 _PyErr_Fetch(tstate, &exc_type, &exc_value, &exc_tb);
1429 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
1430
1431 /* Ignore any exception raised by the lookup */
1432 _PyErr_Restore(tstate, exc_type, exc_value, exc_tb);
1433
1434 if (ix < 0) {
1435 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 }
INADA Naokiba609772016-12-07 20:41:42 +09001437 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001438}
1439
Pablo Galindo109826c2020-10-20 06:22:44 +01001440Py_ssize_t
1441_PyDict_GetItemHint(PyDictObject *mp, PyObject *key,
1442 Py_ssize_t hint, PyObject **value)
1443{
1444 Py_hash_t hash;
1445 PyThreadState *tstate;
1446
1447 assert(*value == NULL);
1448 assert(PyDict_CheckExact((PyObject*)mp));
1449 assert(PyUnicode_CheckExact(key));
1450
Pablo Galindo492d5132020-10-25 06:08:17 +00001451 if (hint >= 0 && hint < mp->ma_keys->dk_nentries) {
Pablo Galindo109826c2020-10-20 06:22:44 +01001452 PyObject *res = NULL;
1453
1454 PyDictKeyEntry *ep = DK_ENTRIES(mp->ma_keys) + (size_t)hint;
1455 if (ep->me_key == key) {
1456 if (mp->ma_keys->dk_lookup == lookdict_split) {
1457 assert(mp->ma_values != NULL);
1458 res = mp->ma_values[(size_t)hint];
1459 }
1460 else {
1461 res = ep->me_value;
1462 }
1463 if (res != NULL) {
1464 *value = res;
1465 return hint;
1466 }
1467 }
1468 }
1469
1470 if ((hash = ((PyASCIIObject *) key)->hash) == -1)
1471 {
1472 hash = PyObject_Hash(key);
1473 if (hash == -1) {
1474 PyErr_Clear();
1475 return -1;
1476 }
1477 }
1478
1479 // We can arrive here with a NULL tstate during initialization: try
1480 // running "python -Wi" for an example related to string interning
1481 tstate = _PyThreadState_UncheckedGet();
1482 Py_ssize_t ix = 0;
1483 if (tstate != NULL && tstate->curexc_type != NULL) {
1484 /* preserve the existing exception */
1485 PyObject *err_type, *err_value, *err_tb;
1486 PyErr_Fetch(&err_type, &err_value, &err_tb);
1487 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, value);
1488 /* ignore errors */
1489 PyErr_Restore(err_type, err_value, err_tb);
1490 if (ix < 0) {
1491 return -1;
1492 }
1493 }
1494 else {
1495 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, value);
1496 if (ix < 0) {
1497 PyErr_Clear();
1498 return -1;
1499 }
1500 }
1501
1502 return ix;
1503}
1504
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001505/* Same as PyDict_GetItemWithError() but with hash supplied by caller.
1506 This returns NULL *with* an exception set if an exception occurred.
1507 It returns NULL *without* an exception set if the key wasn't present.
1508*/
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001509PyObject *
1510_PyDict_GetItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1511{
Victor Stinner742da042016-09-07 17:40:12 -07001512 Py_ssize_t ix;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001513 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001514 PyObject *value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001515
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001516 if (!PyDict_Check(op)) {
1517 PyErr_BadInternalCall();
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001518 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001519 }
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001520
INADA Naoki778928b2017-08-03 23:45:15 +09001521 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001522 if (ix < 0) {
1523 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001524 }
INADA Naokiba609772016-12-07 20:41:42 +09001525 return value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001526}
1527
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001528/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1529 This returns NULL *with* an exception set if an exception occurred.
1530 It returns NULL *without* an exception set if the key wasn't present.
1531*/
1532PyObject *
1533PyDict_GetItemWithError(PyObject *op, PyObject *key)
1534{
Victor Stinner742da042016-09-07 17:40:12 -07001535 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001536 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537 PyDictObject*mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001538 PyObject *value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001540 if (!PyDict_Check(op)) {
1541 PyErr_BadInternalCall();
1542 return NULL;
1543 }
1544 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001545 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001546 {
1547 hash = PyObject_Hash(key);
1548 if (hash == -1) {
1549 return NULL;
1550 }
1551 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001552
INADA Naoki778928b2017-08-03 23:45:15 +09001553 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001554 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001556 return value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001557}
1558
Brett Cannonfd074152012-04-14 14:10:13 -04001559PyObject *
1560_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1561{
1562 PyObject *kv;
1563 kv = _PyUnicode_FromId(key); /* borrowed */
1564 if (kv == NULL)
1565 return NULL;
scoder6067d4b2020-05-11 06:04:31 +02001566 Py_hash_t hash = ((PyASCIIObject *) kv)->hash;
1567 assert (hash != -1); /* interned strings have their hash value initialised */
1568 return _PyDict_GetItem_KnownHash(dp, kv, hash);
Brett Cannonfd074152012-04-14 14:10:13 -04001569}
1570
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001571PyObject *
1572_PyDict_GetItemStringWithError(PyObject *v, const char *key)
1573{
1574 PyObject *kv, *rv;
1575 kv = PyUnicode_FromString(key);
1576 if (kv == NULL) {
1577 return NULL;
1578 }
1579 rv = PyDict_GetItemWithError(v, kv);
1580 Py_DECREF(kv);
1581 return rv;
1582}
1583
Victor Stinnerb4efc962015-11-20 09:24:02 +01001584/* Fast version of global value lookup (LOAD_GLOBAL).
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001585 * Lookup in globals, then builtins.
Victor Stinnerb4efc962015-11-20 09:24:02 +01001586 *
1587 * Raise an exception and return NULL if an error occurred (ex: computing the
1588 * key hash failed, key comparison failed, ...). Return NULL if the key doesn't
1589 * exist. Return the value if the key exists.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001590 */
1591PyObject *
1592_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001593{
Victor Stinner742da042016-09-07 17:40:12 -07001594 Py_ssize_t ix;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001595 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09001596 PyObject *value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001597
1598 if (!PyUnicode_CheckExact(key) ||
1599 (hash = ((PyASCIIObject *) key)->hash) == -1)
1600 {
1601 hash = PyObject_Hash(key);
1602 if (hash == -1)
1603 return NULL;
Antoine Pitroue965d972012-02-27 00:45:12 +01001604 }
Victor Stinnerb4efc962015-11-20 09:24:02 +01001605
1606 /* namespace 1: globals */
INADA Naoki778928b2017-08-03 23:45:15 +09001607 ix = globals->ma_keys->dk_lookup(globals, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001608 if (ix == DKIX_ERROR)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001609 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001610 if (ix != DKIX_EMPTY && value != NULL)
1611 return value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001612
1613 /* namespace 2: builtins */
INADA Naoki778928b2017-08-03 23:45:15 +09001614 ix = builtins->ma_keys->dk_lookup(builtins, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001615 if (ix < 0)
Victor Stinnerb4efc962015-11-20 09:24:02 +01001616 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001617 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001618}
1619
Antoine Pitroue965d972012-02-27 00:45:12 +01001620/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1621 * dictionary if it's merely replacing the value for an existing key.
1622 * This means that it's safe to loop over a dictionary with PyDict_Next()
1623 * and occasionally replace a value -- but you can't insert new keys or
1624 * remove them.
1625 */
1626int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001627PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001628{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001629 PyDictObject *mp;
1630 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001631 if (!PyDict_Check(op)) {
1632 PyErr_BadInternalCall();
1633 return -1;
1634 }
1635 assert(key);
1636 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001637 mp = (PyDictObject *)op;
1638 if (!PyUnicode_CheckExact(key) ||
1639 (hash = ((PyASCIIObject *) key)->hash) == -1)
1640 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001641 hash = PyObject_Hash(key);
1642 if (hash == -1)
1643 return -1;
1644 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001645
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001646 if (mp->ma_keys == Py_EMPTY_KEYS) {
1647 return insert_to_emptydict(mp, key, hash, value);
1648 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001649 /* insertdict() handles any resizing that might be necessary */
1650 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001651}
1652
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001653int
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001654_PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
1655 Py_hash_t hash)
1656{
1657 PyDictObject *mp;
1658
1659 if (!PyDict_Check(op)) {
1660 PyErr_BadInternalCall();
1661 return -1;
1662 }
1663 assert(key);
1664 assert(value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001665 assert(hash != -1);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001666 mp = (PyDictObject *)op;
1667
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001668 if (mp->ma_keys == Py_EMPTY_KEYS) {
1669 return insert_to_emptydict(mp, key, hash, value);
1670 }
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001671 /* insertdict() handles any resizing that might be necessary */
1672 return insertdict(mp, key, hash, value);
1673}
1674
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001675static int
INADA Naoki778928b2017-08-03 23:45:15 +09001676delitem_common(PyDictObject *mp, Py_hash_t hash, Py_ssize_t ix,
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001677 PyObject *old_value)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001678{
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001679 PyObject *old_key;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001680 PyDictKeyEntry *ep;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001681
INADA Naoki778928b2017-08-03 23:45:15 +09001682 Py_ssize_t hashpos = lookdict_index(mp->ma_keys, hash, ix);
1683 assert(hashpos >= 0);
1684
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001685 mp->ma_used--;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001686 mp->ma_version_tag = DICT_NEXT_VERSION();
1687 ep = &DK_ENTRIES(mp->ma_keys)[ix];
INADA Naokia7576492018-11-14 18:39:27 +09001688 dictkeys_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001689 ENSURE_ALLOWS_DELETIONS(mp);
1690 old_key = ep->me_key;
1691 ep->me_key = NULL;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001692 ep->me_value = NULL;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001693 Py_DECREF(old_key);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001694 Py_DECREF(old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001695
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001696 ASSERT_CONSISTENT(mp);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001697 return 0;
1698}
1699
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001700int
Tim Peters1f5871e2000-07-04 17:44:48 +00001701PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001702{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001703 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 assert(key);
1705 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001706 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001707 hash = PyObject_Hash(key);
1708 if (hash == -1)
1709 return -1;
1710 }
Victor Stinner742da042016-09-07 17:40:12 -07001711
1712 return _PyDict_DelItem_KnownHash(op, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001713}
1714
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001715int
1716_PyDict_DelItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1717{
INADA Naoki778928b2017-08-03 23:45:15 +09001718 Py_ssize_t ix;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001719 PyDictObject *mp;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001720 PyObject *old_value;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001721
1722 if (!PyDict_Check(op)) {
1723 PyErr_BadInternalCall();
1724 return -1;
1725 }
1726 assert(key);
1727 assert(hash != -1);
1728 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001729 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001730 if (ix == DKIX_ERROR)
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001731 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09001732 if (ix == DKIX_EMPTY || old_value == NULL) {
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001733 _PyErr_SetKeyError(key);
1734 return -1;
1735 }
Victor Stinner78601a32016-09-09 19:28:36 -07001736
1737 // Split table doesn't allow deletion. Combine it.
1738 if (_PyDict_HasSplitTable(mp)) {
1739 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1740 return -1;
1741 }
INADA Naoki778928b2017-08-03 23:45:15 +09001742 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001743 assert(ix >= 0);
1744 }
1745
INADA Naoki778928b2017-08-03 23:45:15 +09001746 return delitem_common(mp, hash, ix, old_value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001747}
1748
Antoine Pitroud741ed42016-12-27 14:23:43 +01001749/* This function promises that the predicate -> deletion sequence is atomic
1750 * (i.e. protected by the GIL), assuming the predicate itself doesn't
1751 * release the GIL.
1752 */
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001753int
1754_PyDict_DelItemIf(PyObject *op, PyObject *key,
1755 int (*predicate)(PyObject *value))
1756{
Antoine Pitroud741ed42016-12-27 14:23:43 +01001757 Py_ssize_t hashpos, ix;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001758 PyDictObject *mp;
1759 Py_hash_t hash;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001760 PyObject *old_value;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001761 int res;
1762
1763 if (!PyDict_Check(op)) {
1764 PyErr_BadInternalCall();
1765 return -1;
1766 }
1767 assert(key);
1768 hash = PyObject_Hash(key);
1769 if (hash == -1)
1770 return -1;
1771 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001772 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001773 if (ix == DKIX_ERROR)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001774 return -1;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001775 if (ix == DKIX_EMPTY || old_value == NULL) {
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001776 _PyErr_SetKeyError(key);
1777 return -1;
1778 }
Antoine Pitroud741ed42016-12-27 14:23:43 +01001779
1780 // Split table doesn't allow deletion. Combine it.
1781 if (_PyDict_HasSplitTable(mp)) {
1782 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1783 return -1;
1784 }
INADA Naoki778928b2017-08-03 23:45:15 +09001785 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001786 assert(ix >= 0);
1787 }
1788
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001789 res = predicate(old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001790 if (res == -1)
1791 return -1;
INADA Naoki778928b2017-08-03 23:45:15 +09001792
1793 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1794 assert(hashpos >= 0);
1795
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001796 if (res > 0)
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001797 return delitem_common(mp, hashpos, ix, old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001798 else
1799 return 0;
1800}
1801
1802
Guido van Rossum25831651993-05-19 14:50:45 +00001803void
Tim Peters1f5871e2000-07-04 17:44:48 +00001804PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001805{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001806 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001807 PyDictKeysObject *oldkeys;
1808 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001809 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001810
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001811 if (!PyDict_Check(op))
1812 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001813 mp = ((PyDictObject *)op);
1814 oldkeys = mp->ma_keys;
1815 oldvalues = mp->ma_values;
1816 if (oldvalues == empty_values)
1817 return;
1818 /* Empty the dict... */
INADA Naokia7576492018-11-14 18:39:27 +09001819 dictkeys_incref(Py_EMPTY_KEYS);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001820 mp->ma_keys = Py_EMPTY_KEYS;
1821 mp->ma_values = empty_values;
1822 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001823 mp->ma_version_tag = DICT_NEXT_VERSION();
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001824 /* ...then clear the keys and values */
1825 if (oldvalues != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001826 n = oldkeys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001827 for (i = 0; i < n; i++)
1828 Py_CLEAR(oldvalues[i]);
1829 free_values(oldvalues);
INADA Naokia7576492018-11-14 18:39:27 +09001830 dictkeys_decref(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001831 }
1832 else {
1833 assert(oldkeys->dk_refcnt == 1);
INADA Naokia7576492018-11-14 18:39:27 +09001834 dictkeys_decref(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001835 }
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001836 ASSERT_CONSISTENT(mp);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001837}
1838
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001839/* Internal version of PyDict_Next that returns a hash value in addition
1840 * to the key and value.
1841 * Return 1 on success, return 0 when the reached the end of the dictionary
1842 * (or if op is not a dictionary)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001843 */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001844int
1845_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1846 PyObject **pvalue, Py_hash_t *phash)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001847{
INADA Naokica2d8be2016-11-04 16:59:10 +09001848 Py_ssize_t i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001849 PyDictObject *mp;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001850 PyDictKeyEntry *entry_ptr;
1851 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001852
1853 if (!PyDict_Check(op))
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001854 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855 mp = (PyDictObject *)op;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001856 i = *ppos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001857 if (mp->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09001858 if (i < 0 || i >= mp->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001859 return 0;
INADA Naokica2d8be2016-11-04 16:59:10 +09001860 /* values of split table is always dense */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001861 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
INADA Naokica2d8be2016-11-04 16:59:10 +09001862 value = mp->ma_values[i];
1863 assert(value != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001864 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001865 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09001866 Py_ssize_t n = mp->ma_keys->dk_nentries;
1867 if (i < 0 || i >= n)
1868 return 0;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001869 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
1870 while (i < n && entry_ptr->me_value == NULL) {
1871 entry_ptr++;
1872 i++;
Victor Stinner742da042016-09-07 17:40:12 -07001873 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001874 if (i >= n)
1875 return 0;
1876 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001877 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001878 *ppos = i+1;
1879 if (pkey)
1880 *pkey = entry_ptr->me_key;
1881 if (phash)
1882 *phash = entry_ptr->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001883 if (pvalue)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001884 *pvalue = value;
1885 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001886}
1887
Tim Peters080c88b2003-02-15 03:01:11 +00001888/*
1889 * Iterate over a dict. Use like so:
1890 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001891 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001892 * PyObject *key, *value;
1893 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001894 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001895 * Refer to borrowed references in key and value.
Tim Peters080c88b2003-02-15 03:01:11 +00001896 * }
1897 *
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001898 * Return 1 on success, return 0 when the reached the end of the dictionary
1899 * (or if op is not a dictionary)
1900 *
Tim Peters080c88b2003-02-15 03:01:11 +00001901 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001902 * mutates the dict. One exception: it is safe if the loop merely changes
1903 * the values associated with the keys (but doesn't insert new keys or
1904 * delete keys), via PyDict_SetItem().
1905 */
Guido van Rossum25831651993-05-19 14:50:45 +00001906int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001907PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001908{
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001909 return _PyDict_Next(op, ppos, pkey, pvalue, NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001910}
1911
Eric Snow96c6af92015-05-29 22:21:39 -06001912/* Internal version of dict.pop(). */
1913PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001914_PyDict_Pop_KnownHash(PyObject *dict, PyObject *key, Py_hash_t hash, PyObject *deflt)
Eric Snow96c6af92015-05-29 22:21:39 -06001915{
Victor Stinner742da042016-09-07 17:40:12 -07001916 Py_ssize_t ix, hashpos;
Eric Snow96c6af92015-05-29 22:21:39 -06001917 PyObject *old_value, *old_key;
1918 PyDictKeyEntry *ep;
Yury Selivanov684ef2c2016-10-28 19:01:21 -04001919 PyDictObject *mp;
1920
1921 assert(PyDict_Check(dict));
1922 mp = (PyDictObject *)dict;
Eric Snow96c6af92015-05-29 22:21:39 -06001923
1924 if (mp->ma_used == 0) {
1925 if (deflt) {
1926 Py_INCREF(deflt);
1927 return deflt;
1928 }
1929 _PyErr_SetKeyError(key);
1930 return NULL;
1931 }
INADA Naoki778928b2017-08-03 23:45:15 +09001932 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001933 if (ix == DKIX_ERROR)
Eric Snow96c6af92015-05-29 22:21:39 -06001934 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001935 if (ix == DKIX_EMPTY || old_value == NULL) {
Eric Snow96c6af92015-05-29 22:21:39 -06001936 if (deflt) {
1937 Py_INCREF(deflt);
1938 return deflt;
1939 }
1940 _PyErr_SetKeyError(key);
1941 return NULL;
1942 }
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001943
Victor Stinner78601a32016-09-09 19:28:36 -07001944 // Split table doesn't allow deletion. Combine it.
1945 if (_PyDict_HasSplitTable(mp)) {
1946 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1947 return NULL;
1948 }
INADA Naoki778928b2017-08-03 23:45:15 +09001949 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001950 assert(ix >= 0);
1951 }
1952
INADA Naoki778928b2017-08-03 23:45:15 +09001953 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1954 assert(hashpos >= 0);
Victor Stinner78601a32016-09-09 19:28:36 -07001955 assert(old_value != NULL);
Eric Snow96c6af92015-05-29 22:21:39 -06001956 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001957 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokia7576492018-11-14 18:39:27 +09001958 dictkeys_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
Victor Stinner78601a32016-09-09 19:28:36 -07001959 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1960 ENSURE_ALLOWS_DELETIONS(mp);
1961 old_key = ep->me_key;
1962 ep->me_key = NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001963 ep->me_value = NULL;
Victor Stinner78601a32016-09-09 19:28:36 -07001964 Py_DECREF(old_key);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001965
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001966 ASSERT_CONSISTENT(mp);
Eric Snow96c6af92015-05-29 22:21:39 -06001967 return old_value;
1968}
1969
Serhiy Storchaka67796522017-01-12 18:34:33 +02001970PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001971_PyDict_Pop(PyObject *dict, PyObject *key, PyObject *deflt)
Serhiy Storchaka67796522017-01-12 18:34:33 +02001972{
1973 Py_hash_t hash;
1974
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001975 if (((PyDictObject *)dict)->ma_used == 0) {
Serhiy Storchaka67796522017-01-12 18:34:33 +02001976 if (deflt) {
1977 Py_INCREF(deflt);
1978 return deflt;
1979 }
1980 _PyErr_SetKeyError(key);
1981 return NULL;
1982 }
1983 if (!PyUnicode_CheckExact(key) ||
1984 (hash = ((PyASCIIObject *) key)->hash) == -1) {
1985 hash = PyObject_Hash(key);
1986 if (hash == -1)
1987 return NULL;
1988 }
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001989 return _PyDict_Pop_KnownHash(dict, key, hash, deflt);
Serhiy Storchaka67796522017-01-12 18:34:33 +02001990}
1991
Eric Snow96c6af92015-05-29 22:21:39 -06001992/* Internal version of dict.from_keys(). It is subclass-friendly. */
1993PyObject *
1994_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
1995{
1996 PyObject *it; /* iter(iterable) */
1997 PyObject *key;
1998 PyObject *d;
1999 int status;
2000
Victor Stinnera5ed5f02016-12-06 18:45:50 +01002001 d = _PyObject_CallNoArg(cls);
Eric Snow96c6af92015-05-29 22:21:39 -06002002 if (d == NULL)
2003 return NULL;
2004
2005 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
2006 if (PyDict_CheckExact(iterable)) {
2007 PyDictObject *mp = (PyDictObject *)d;
2008 PyObject *oldvalue;
2009 Py_ssize_t pos = 0;
2010 PyObject *key;
2011 Py_hash_t hash;
2012
Inada Naokid9323a82020-08-07 14:08:55 +09002013 if (dictresize(mp, estimate_keysize(PyDict_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06002014 Py_DECREF(d);
2015 return NULL;
2016 }
2017
2018 while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
2019 if (insertdict(mp, key, hash, value)) {
2020 Py_DECREF(d);
2021 return NULL;
2022 }
2023 }
2024 return d;
2025 }
2026 if (PyAnySet_CheckExact(iterable)) {
2027 PyDictObject *mp = (PyDictObject *)d;
2028 Py_ssize_t pos = 0;
2029 PyObject *key;
2030 Py_hash_t hash;
2031
Inada Naokid9323a82020-08-07 14:08:55 +09002032 if (dictresize(mp, estimate_keysize(PySet_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06002033 Py_DECREF(d);
2034 return NULL;
2035 }
2036
2037 while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
2038 if (insertdict(mp, key, hash, value)) {
2039 Py_DECREF(d);
2040 return NULL;
2041 }
2042 }
2043 return d;
2044 }
2045 }
2046
2047 it = PyObject_GetIter(iterable);
2048 if (it == NULL){
2049 Py_DECREF(d);
2050 return NULL;
2051 }
2052
2053 if (PyDict_CheckExact(d)) {
2054 while ((key = PyIter_Next(it)) != NULL) {
2055 status = PyDict_SetItem(d, key, value);
2056 Py_DECREF(key);
2057 if (status < 0)
2058 goto Fail;
2059 }
2060 } else {
2061 while ((key = PyIter_Next(it)) != NULL) {
2062 status = PyObject_SetItem(d, key, value);
2063 Py_DECREF(key);
2064 if (status < 0)
2065 goto Fail;
2066 }
2067 }
2068
2069 if (PyErr_Occurred())
2070 goto Fail;
2071 Py_DECREF(it);
2072 return d;
2073
2074Fail:
2075 Py_DECREF(it);
2076 Py_DECREF(d);
2077 return NULL;
2078}
2079
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002080/* Methods */
2081
2082static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002083dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002084{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002085 PyObject **values = mp->ma_values;
2086 PyDictKeysObject *keys = mp->ma_keys;
2087 Py_ssize_t i, n;
INADA Naokia6296d32017-08-24 14:55:17 +09002088
2089 /* bpo-31095: UnTrack is needed before calling any callbacks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002090 PyObject_GC_UnTrack(mp);
Jeroen Demeyer351c6742019-05-10 19:21:11 +02002091 Py_TRASHCAN_BEGIN(mp, dict_dealloc)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002092 if (values != NULL) {
2093 if (values != empty_values) {
Victor Stinner742da042016-09-07 17:40:12 -07002094 for (i = 0, n = mp->ma_keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002095 Py_XDECREF(values[i]);
2096 }
2097 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002098 }
INADA Naokia7576492018-11-14 18:39:27 +09002099 dictkeys_decref(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002100 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02002101 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02002102 assert(keys->dk_refcnt == 1);
INADA Naokia7576492018-11-14 18:39:27 +09002103 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002104 }
Victor Stinner522691c2020-06-23 16:40:40 +02002105 struct _Py_dict_state *state = get_dict_state();
Victor Stinnerb4e85ca2020-06-23 11:33:18 +02002106#ifdef Py_DEBUG
2107 // new_dict() must not be called after _PyDict_Fini()
2108 assert(state->numfree != -1);
Victor Stinnerb4b53862020-05-05 19:55:29 +02002109#endif
Victor Stinnerb4e85ca2020-06-23 11:33:18 +02002110 if (state->numfree < PyDict_MAXFREELIST && Py_IS_TYPE(mp, &PyDict_Type)) {
2111 state->free_list[state->numfree++] = mp;
2112 }
2113 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002114 Py_TYPE(mp)->tp_free((PyObject *)mp);
Victor Stinnerb4b53862020-05-05 19:55:29 +02002115 }
Jeroen Demeyer351c6742019-05-10 19:21:11 +02002116 Py_TRASHCAN_END
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002117}
2118
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002119
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002120static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002121dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002122{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002123 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01002124 PyObject *key = NULL, *value = NULL;
2125 _PyUnicodeWriter writer;
2126 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00002127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002128 i = Py_ReprEnter((PyObject *)mp);
2129 if (i != 0) {
2130 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
2131 }
Guido van Rossum255443b1998-04-10 22:47:14 +00002132
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002133 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01002134 Py_ReprLeave((PyObject *)mp);
2135 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002136 }
Tim Petersa7259592001-06-16 05:11:17 +00002137
Victor Stinnerf91929b2013-11-19 13:07:38 +01002138 _PyUnicodeWriter_Init(&writer);
2139 writer.overallocate = 1;
2140 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
2141 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00002142
Victor Stinnerf91929b2013-11-19 13:07:38 +01002143 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
2144 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00002145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002146 /* Do repr() on each key+value pair, and insert ": " between them.
2147 Note that repr may mutate the dict. */
2148 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01002149 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002150 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01002151 PyObject *s;
2152 int res;
2153
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002154 /* Prevent repr from deleting key or value during key format. */
2155 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002156 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02002157
Victor Stinnerf91929b2013-11-19 13:07:38 +01002158 if (!first) {
2159 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
2160 goto error;
2161 }
2162 first = 0;
2163
2164 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002165 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01002166 goto error;
2167 res = _PyUnicodeWriter_WriteStr(&writer, s);
2168 Py_DECREF(s);
2169 if (res < 0)
2170 goto error;
2171
2172 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
2173 goto error;
2174
2175 s = PyObject_Repr(value);
2176 if (s == NULL)
2177 goto error;
2178 res = _PyUnicodeWriter_WriteStr(&writer, s);
2179 Py_DECREF(s);
2180 if (res < 0)
2181 goto error;
2182
2183 Py_CLEAR(key);
2184 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002185 }
Tim Petersa7259592001-06-16 05:11:17 +00002186
Victor Stinnerf91929b2013-11-19 13:07:38 +01002187 writer.overallocate = 0;
2188 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
2189 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00002190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002191 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01002192
2193 return _PyUnicodeWriter_Finish(&writer);
2194
2195error:
2196 Py_ReprLeave((PyObject *)mp);
2197 _PyUnicodeWriter_Dealloc(&writer);
2198 Py_XDECREF(key);
2199 Py_XDECREF(value);
2200 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002201}
2202
Martin v. Löwis18e16552006-02-15 17:27:45 +00002203static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002204dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002205{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002206 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002207}
2208
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002209static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002210dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002211{
Victor Stinner742da042016-09-07 17:40:12 -07002212 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002213 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09002214 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002216 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002217 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002218 hash = PyObject_Hash(key);
2219 if (hash == -1)
2220 return NULL;
2221 }
INADA Naoki778928b2017-08-03 23:45:15 +09002222 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002223 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002224 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002225 if (ix == DKIX_EMPTY || value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002226 if (!PyDict_CheckExact(mp)) {
2227 /* Look up __missing__ method if we're a subclass. */
2228 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05002229 _Py_IDENTIFIER(__missing__);
2230 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002231 if (missing != NULL) {
Petr Viktorinffd97532020-02-11 17:46:57 +01002232 res = PyObject_CallOneArg(missing, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002233 Py_DECREF(missing);
2234 return res;
2235 }
2236 else if (PyErr_Occurred())
2237 return NULL;
2238 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07002239 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002240 return NULL;
2241 }
INADA Naokiba609772016-12-07 20:41:42 +09002242 Py_INCREF(value);
2243 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002244}
2245
2246static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002247dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002248{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002249 if (w == NULL)
2250 return PyDict_DelItem((PyObject *)mp, v);
2251 else
2252 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002253}
2254
Guido van Rossuma9e7a811997-05-13 21:02:11 +00002255static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002256 (lenfunc)dict_length, /*mp_length*/
2257 (binaryfunc)dict_subscript, /*mp_subscript*/
2258 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002259};
2260
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002261static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002262dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002263{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002264 PyObject *v;
2265 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002266 PyDictKeyEntry *ep;
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002267 Py_ssize_t n, offset;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002268 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002269
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002270 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002271 n = mp->ma_used;
2272 v = PyList_New(n);
2273 if (v == NULL)
2274 return NULL;
2275 if (n != mp->ma_used) {
2276 /* Durnit. The allocations caused the dict to resize.
2277 * Just start over, this shouldn't normally happen.
2278 */
2279 Py_DECREF(v);
2280 goto again;
2281 }
Victor Stinner742da042016-09-07 17:40:12 -07002282 ep = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002283 if (mp->ma_values) {
2284 value_ptr = mp->ma_values;
2285 offset = sizeof(PyObject *);
2286 }
2287 else {
2288 value_ptr = &ep[0].me_value;
2289 offset = sizeof(PyDictKeyEntry);
2290 }
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002291 for (i = 0, j = 0; j < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002292 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002293 PyObject *key = ep[i].me_key;
2294 Py_INCREF(key);
2295 PyList_SET_ITEM(v, j, key);
2296 j++;
2297 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002298 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002299 }
2300 assert(j == n);
2301 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002302}
2303
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002304static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002305dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002306{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002307 PyObject *v;
2308 Py_ssize_t i, j;
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002309 PyDictKeyEntry *ep;
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002310 Py_ssize_t n, offset;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002311 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002312
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002313 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002314 n = mp->ma_used;
2315 v = PyList_New(n);
2316 if (v == NULL)
2317 return NULL;
2318 if (n != mp->ma_used) {
2319 /* Durnit. The allocations caused the dict to resize.
2320 * Just start over, this shouldn't normally happen.
2321 */
2322 Py_DECREF(v);
2323 goto again;
2324 }
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002325 ep = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002326 if (mp->ma_values) {
2327 value_ptr = mp->ma_values;
2328 offset = sizeof(PyObject *);
2329 }
2330 else {
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002331 value_ptr = &ep[0].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002332 offset = sizeof(PyDictKeyEntry);
2333 }
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002334 for (i = 0, j = 0; j < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002335 PyObject *value = *value_ptr;
2336 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2337 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002338 Py_INCREF(value);
2339 PyList_SET_ITEM(v, j, value);
2340 j++;
2341 }
2342 }
2343 assert(j == n);
2344 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002345}
2346
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002347static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002348dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002349{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002350 PyObject *v;
2351 Py_ssize_t i, j, n;
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002352 Py_ssize_t offset;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002353 PyObject *item, *key;
2354 PyDictKeyEntry *ep;
2355 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 /* Preallocate the list of tuples, to avoid allocations during
2358 * the loop over the items, which could trigger GC, which
2359 * could resize the dict. :-(
2360 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002361 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002362 n = mp->ma_used;
2363 v = PyList_New(n);
2364 if (v == NULL)
2365 return NULL;
2366 for (i = 0; i < n; i++) {
2367 item = PyTuple_New(2);
2368 if (item == NULL) {
2369 Py_DECREF(v);
2370 return NULL;
2371 }
2372 PyList_SET_ITEM(v, i, item);
2373 }
2374 if (n != mp->ma_used) {
2375 /* Durnit. The allocations caused the dict to resize.
2376 * Just start over, this shouldn't normally happen.
2377 */
2378 Py_DECREF(v);
2379 goto again;
2380 }
2381 /* Nothing we do below makes any function calls. */
Victor Stinner742da042016-09-07 17:40:12 -07002382 ep = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002383 if (mp->ma_values) {
2384 value_ptr = mp->ma_values;
2385 offset = sizeof(PyObject *);
2386 }
2387 else {
2388 value_ptr = &ep[0].me_value;
2389 offset = sizeof(PyDictKeyEntry);
2390 }
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002391 for (i = 0, j = 0; j < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002392 PyObject *value = *value_ptr;
2393 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2394 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 key = ep[i].me_key;
2396 item = PyList_GET_ITEM(v, j);
2397 Py_INCREF(key);
2398 PyTuple_SET_ITEM(item, 0, key);
2399 Py_INCREF(value);
2400 PyTuple_SET_ITEM(item, 1, value);
2401 j++;
2402 }
2403 }
2404 assert(j == n);
2405 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002406}
2407
Larry Hastings5c661892014-01-24 06:17:25 -08002408/*[clinic input]
2409@classmethod
2410dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08002411 iterable: object
2412 value: object=None
2413 /
2414
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002415Create a new dictionary with keys from iterable and values set to value.
Larry Hastings5c661892014-01-24 06:17:25 -08002416[clinic start generated code]*/
2417
Larry Hastings5c661892014-01-24 06:17:25 -08002418static PyObject *
2419dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002420/*[clinic end generated code: output=8fb98e4b10384999 input=382ba4855d0f74c3]*/
Larry Hastings5c661892014-01-24 06:17:25 -08002421{
Eric Snow96c6af92015-05-29 22:21:39 -06002422 return _PyDict_FromKeys((PyObject *)type, iterable, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002423}
2424
Brandt Buchereb8ac572020-02-24 19:47:34 -08002425/* Single-arg dict update; used by dict_update_common and operators. */
2426static int
2427dict_update_arg(PyObject *self, PyObject *arg)
2428{
2429 if (PyDict_CheckExact(arg)) {
2430 return PyDict_Merge(self, arg, 1);
2431 }
2432 _Py_IDENTIFIER(keys);
2433 PyObject *func;
2434 if (_PyObject_LookupAttrId(arg, &PyId_keys, &func) < 0) {
2435 return -1;
2436 }
2437 if (func != NULL) {
2438 Py_DECREF(func);
2439 return PyDict_Merge(self, arg, 1);
2440 }
2441 return PyDict_MergeFromSeq2(self, arg, 1);
2442}
2443
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002444static int
Victor Stinner742da042016-09-07 17:40:12 -07002445dict_update_common(PyObject *self, PyObject *args, PyObject *kwds,
2446 const char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002447{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002448 PyObject *arg = NULL;
2449 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002450
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002451 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002452 result = -1;
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002453 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002454 else if (arg != NULL) {
Brandt Buchereb8ac572020-02-24 19:47:34 -08002455 result = dict_update_arg(self, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002456 }
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002458 if (result == 0 && kwds != NULL) {
2459 if (PyArg_ValidateKeywordArguments(kwds))
2460 result = PyDict_Merge(self, kwds, 1);
2461 else
2462 result = -1;
2463 }
2464 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002465}
2466
Victor Stinner91f0d4a2017-01-19 12:45:06 +01002467/* Note: dict.update() uses the METH_VARARGS|METH_KEYWORDS calling convention.
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +03002468 Using METH_FASTCALL|METH_KEYWORDS would make dict.update(**dict2) calls
2469 slower, see the issue #29312. */
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002470static PyObject *
2471dict_update(PyObject *self, PyObject *args, PyObject *kwds)
2472{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002473 if (dict_update_common(self, args, kwds, "update") != -1)
2474 Py_RETURN_NONE;
2475 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002476}
2477
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002478/* Update unconditionally replaces existing items.
2479 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00002480 otherwise it leaves existing items unchanged.
2481
2482 PyDict_{Update,Merge} update/merge from a mapping object.
2483
Tim Petersf582b822001-12-11 18:51:08 +00002484 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00002485 producing iterable objects of length 2.
2486*/
2487
Tim Petersf582b822001-12-11 18:51:08 +00002488int
Tim Peters1fc240e2001-10-26 05:06:50 +00002489PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
2490{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002491 PyObject *it; /* iter(seq2) */
2492 Py_ssize_t i; /* index into seq2 of current element */
2493 PyObject *item; /* seq2[i] */
2494 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00002495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002496 assert(d != NULL);
2497 assert(PyDict_Check(d));
2498 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00002499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002500 it = PyObject_GetIter(seq2);
2501 if (it == NULL)
2502 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002504 for (i = 0; ; ++i) {
2505 PyObject *key, *value;
2506 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00002507
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002508 fast = NULL;
2509 item = PyIter_Next(it);
2510 if (item == NULL) {
2511 if (PyErr_Occurred())
2512 goto Fail;
2513 break;
2514 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002516 /* Convert item to sequence, and verify length 2. */
2517 fast = PySequence_Fast(item, "");
2518 if (fast == NULL) {
2519 if (PyErr_ExceptionMatches(PyExc_TypeError))
2520 PyErr_Format(PyExc_TypeError,
2521 "cannot convert dictionary update "
2522 "sequence element #%zd to a sequence",
2523 i);
2524 goto Fail;
2525 }
2526 n = PySequence_Fast_GET_SIZE(fast);
2527 if (n != 2) {
2528 PyErr_Format(PyExc_ValueError,
2529 "dictionary update sequence element #%zd "
2530 "has length %zd; 2 is required",
2531 i, n);
2532 goto Fail;
2533 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002535 /* Update/merge with this (key, value) pair. */
2536 key = PySequence_Fast_GET_ITEM(fast, 0);
2537 value = PySequence_Fast_GET_ITEM(fast, 1);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002538 Py_INCREF(key);
2539 Py_INCREF(value);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002540 if (override) {
2541 if (PyDict_SetItem(d, key, value) < 0) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002542 Py_DECREF(key);
2543 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002544 goto Fail;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002545 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002546 }
Serhiy Storchakab510e102020-10-26 12:47:57 +02002547 else {
2548 if (PyDict_SetDefault(d, key, value) == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002549 Py_DECREF(key);
2550 Py_DECREF(value);
2551 goto Fail;
2552 }
2553 }
2554
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002555 Py_DECREF(key);
2556 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002557 Py_DECREF(fast);
2558 Py_DECREF(item);
2559 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002561 i = 0;
Victor Stinner0fc91ee2019-04-12 21:51:34 +02002562 ASSERT_CONSISTENT(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002563 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00002564Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002565 Py_XDECREF(item);
2566 Py_XDECREF(fast);
2567 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002568Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002569 Py_DECREF(it);
2570 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00002571}
2572
doko@ubuntu.comc96df682016-10-11 08:04:02 +02002573static int
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002574dict_merge(PyObject *a, PyObject *b, int override)
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002575{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002576 PyDictObject *mp, *other;
2577 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -07002578 PyDictKeyEntry *entry, *ep0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002579
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002580 assert(0 <= override && override <= 2);
2581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002582 /* We accept for the argument either a concrete dictionary object,
2583 * or an abstract "mapping" object. For the former, we can do
2584 * things quite efficiently. For the latter, we only require that
2585 * PyMapping_Keys() and PyObject_GetItem() be supported.
2586 */
2587 if (a == NULL || !PyDict_Check(a) || b == NULL) {
2588 PyErr_BadInternalCall();
2589 return -1;
2590 }
2591 mp = (PyDictObject*)a;
INADA Naoki2aaf98c2018-09-26 12:59:00 +09002592 if (PyDict_Check(b) && (Py_TYPE(b)->tp_iter == (getiterfunc)dict_iter)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002593 other = (PyDictObject*)b;
2594 if (other == mp || other->ma_used == 0)
2595 /* a.update(a) or a.update({}); nothing to do */
2596 return 0;
Inada Naokidb6d9a52020-08-04 11:08:06 +09002597 if (mp->ma_used == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002598 /* Since the target dict is empty, PyDict_GetItem()
2599 * always returns NULL. Setting override to 1
2600 * skips the unnecessary test.
2601 */
2602 override = 1;
Inada Naokidb6d9a52020-08-04 11:08:06 +09002603 PyDictKeysObject *okeys = other->ma_keys;
2604
2605 // If other is clean, combined, and just allocated, just clone it.
2606 if (other->ma_values == NULL &&
2607 other->ma_used == okeys->dk_nentries &&
2608 (okeys->dk_size == PyDict_MINSIZE ||
2609 USABLE_FRACTION(okeys->dk_size/2) < other->ma_used)) {
2610 PyDictKeysObject *keys = clone_combined_dict_keys(other);
2611 if (keys == NULL) {
2612 return -1;
2613 }
2614
2615 dictkeys_decref(mp->ma_keys);
2616 mp->ma_keys = keys;
2617 if (mp->ma_values != NULL) {
2618 if (mp->ma_values != empty_values) {
2619 free_values(mp->ma_values);
2620 }
2621 mp->ma_values = NULL;
2622 }
2623
2624 mp->ma_used = other->ma_used;
2625 mp->ma_version_tag = DICT_NEXT_VERSION();
2626 ASSERT_CONSISTENT(mp);
2627
2628 if (_PyObject_GC_IS_TRACKED(other) && !_PyObject_GC_IS_TRACKED(mp)) {
2629 /* Maintain tracking. */
2630 _PyObject_GC_TRACK(mp);
2631 }
2632
2633 return 0;
2634 }
2635 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002636 /* Do one big resize at the start, rather than
2637 * incrementally resizing as we insert new items. Expect
2638 * that there will be no (or few) overlapping keys.
2639 */
INADA Naokib1152be2016-10-27 19:26:50 +09002640 if (USABLE_FRACTION(mp->ma_keys->dk_size) < other->ma_used) {
Inada Naokid9323a82020-08-07 14:08:55 +09002641 if (dictresize(mp, estimate_keysize(mp->ma_used + other->ma_used))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002642 return -1;
INADA Naokib1152be2016-10-27 19:26:50 +09002643 }
2644 }
Victor Stinner742da042016-09-07 17:40:12 -07002645 ep0 = DK_ENTRIES(other->ma_keys);
2646 for (i = 0, n = other->ma_keys->dk_nentries; i < n; i++) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002647 PyObject *key, *value;
2648 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002649 entry = &ep0[i];
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002650 key = entry->me_key;
2651 hash = entry->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002652 if (other->ma_values)
2653 value = other->ma_values[i];
2654 else
2655 value = entry->me_value;
2656
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002657 if (value != NULL) {
2658 int err = 0;
2659 Py_INCREF(key);
2660 Py_INCREF(value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002661 if (override == 1)
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002662 err = insertdict(mp, key, hash, value);
Serhiy Storchakab510e102020-10-26 12:47:57 +02002663 else {
2664 err = _PyDict_Contains_KnownHash(a, key, hash);
2665 if (err == 0) {
2666 err = insertdict(mp, key, hash, value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002667 }
Serhiy Storchakab510e102020-10-26 12:47:57 +02002668 else if (err > 0) {
2669 if (override != 0) {
2670 _PyErr_SetKeyError(key);
2671 Py_DECREF(value);
2672 Py_DECREF(key);
2673 return -1;
2674 }
2675 err = 0;
2676 }
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002677 }
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002678 Py_DECREF(value);
2679 Py_DECREF(key);
2680 if (err != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002681 return -1;
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002682
Victor Stinner742da042016-09-07 17:40:12 -07002683 if (n != other->ma_keys->dk_nentries) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002684 PyErr_SetString(PyExc_RuntimeError,
2685 "dict mutated during update");
2686 return -1;
2687 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002688 }
2689 }
2690 }
2691 else {
2692 /* Do it the generic, slower way */
2693 PyObject *keys = PyMapping_Keys(b);
2694 PyObject *iter;
2695 PyObject *key, *value;
2696 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002698 if (keys == NULL)
2699 /* Docstring says this is equivalent to E.keys() so
2700 * if E doesn't have a .keys() method we want
2701 * AttributeError to percolate up. Might as well
2702 * do the same for any other error.
2703 */
2704 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002705
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002706 iter = PyObject_GetIter(keys);
2707 Py_DECREF(keys);
2708 if (iter == NULL)
2709 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002711 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002712 if (override != 1) {
Serhiy Storchakab510e102020-10-26 12:47:57 +02002713 status = PyDict_Contains(a, key);
2714 if (status != 0) {
2715 if (status > 0) {
2716 if (override == 0) {
2717 Py_DECREF(key);
2718 continue;
2719 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002720 _PyErr_SetKeyError(key);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002721 }
2722 Py_DECREF(key);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002723 Py_DECREF(iter);
2724 return -1;
2725 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002726 }
2727 value = PyObject_GetItem(b, key);
2728 if (value == NULL) {
2729 Py_DECREF(iter);
2730 Py_DECREF(key);
2731 return -1;
2732 }
2733 status = PyDict_SetItem(a, key, value);
2734 Py_DECREF(key);
2735 Py_DECREF(value);
2736 if (status < 0) {
2737 Py_DECREF(iter);
2738 return -1;
2739 }
2740 }
2741 Py_DECREF(iter);
2742 if (PyErr_Occurred())
2743 /* Iterator completed, via error */
2744 return -1;
2745 }
Victor Stinner0fc91ee2019-04-12 21:51:34 +02002746 ASSERT_CONSISTENT(a);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002747 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002748}
2749
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002750int
2751PyDict_Update(PyObject *a, PyObject *b)
2752{
2753 return dict_merge(a, b, 1);
2754}
2755
2756int
2757PyDict_Merge(PyObject *a, PyObject *b, int override)
2758{
2759 /* XXX Deprecate override not in (0, 1). */
2760 return dict_merge(a, b, override != 0);
2761}
2762
2763int
2764_PyDict_MergeEx(PyObject *a, PyObject *b, int override)
2765{
2766 return dict_merge(a, b, override);
2767}
2768
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002769static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302770dict_copy(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002771{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002772 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002773}
2774
2775PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002776PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002777{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002778 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002779 PyDictObject *mp;
2780 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002781
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002782 if (o == NULL || !PyDict_Check(o)) {
2783 PyErr_BadInternalCall();
2784 return NULL;
2785 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002786
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002787 mp = (PyDictObject *)o;
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002788 if (mp->ma_used == 0) {
2789 /* The dict is empty; just return a new dict. */
2790 return PyDict_New();
2791 }
2792
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002793 if (_PyDict_HasSplitTable(mp)) {
2794 PyDictObject *split_copy;
Victor Stinner742da042016-09-07 17:40:12 -07002795 Py_ssize_t size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
2796 PyObject **newvalues;
2797 newvalues = new_values(size);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002798 if (newvalues == NULL)
2799 return PyErr_NoMemory();
2800 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2801 if (split_copy == NULL) {
2802 free_values(newvalues);
2803 return NULL;
2804 }
2805 split_copy->ma_values = newvalues;
2806 split_copy->ma_keys = mp->ma_keys;
2807 split_copy->ma_used = mp->ma_used;
INADA Naokid1c82c52018-04-03 11:43:53 +09002808 split_copy->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokia7576492018-11-14 18:39:27 +09002809 dictkeys_incref(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002810 for (i = 0, n = size; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002811 PyObject *value = mp->ma_values[i];
2812 Py_XINCREF(value);
2813 split_copy->ma_values[i] = value;
2814 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002815 if (_PyObject_GC_IS_TRACKED(mp))
2816 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002817 return (PyObject *)split_copy;
2818 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002819
Inada Naokidb6d9a52020-08-04 11:08:06 +09002820 if (Py_TYPE(mp)->tp_iter == (getiterfunc)dict_iter &&
2821 mp->ma_values == NULL &&
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002822 (mp->ma_used >= (mp->ma_keys->dk_nentries * 2) / 3))
2823 {
2824 /* Use fast-copy if:
2825
Inada Naokidb6d9a52020-08-04 11:08:06 +09002826 (1) type(mp) doesn't override tp_iter; and
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002827
2828 (2) 'mp' is not a split-dict; and
2829
2830 (3) if 'mp' is non-compact ('del' operation does not resize dicts),
2831 do fast-copy only if it has at most 1/3 non-used keys.
2832
Ville Skyttä61f82e02018-04-20 23:08:45 +03002833 The last condition (3) is important to guard against a pathological
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002834 case when a large dict is almost emptied with multiple del/pop
2835 operations and copied after that. In cases like this, we defer to
2836 PyDict_Merge, which produces a compacted copy.
2837 */
Inada Naokidb6d9a52020-08-04 11:08:06 +09002838 PyDictKeysObject *keys = clone_combined_dict_keys(mp);
2839 if (keys == NULL) {
2840 return NULL;
2841 }
2842 PyDictObject *new = (PyDictObject *)new_dict(keys, NULL);
2843 if (new == NULL) {
2844 /* In case of an error, `new_dict()` takes care of
2845 cleaning up `keys`. */
2846 return NULL;
2847 }
2848
2849 new->ma_used = mp->ma_used;
2850 ASSERT_CONSISTENT(new);
2851 if (_PyObject_GC_IS_TRACKED(mp)) {
2852 /* Maintain tracking. */
2853 _PyObject_GC_TRACK(new);
2854 }
2855
2856 return (PyObject *)new;
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002857 }
2858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002859 copy = PyDict_New();
2860 if (copy == NULL)
2861 return NULL;
Inada Naokidb6d9a52020-08-04 11:08:06 +09002862 if (dict_merge(copy, o, 1) == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002863 return copy;
2864 Py_DECREF(copy);
2865 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002866}
2867
Martin v. Löwis18e16552006-02-15 17:27:45 +00002868Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002869PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002870{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002871 if (mp == NULL || !PyDict_Check(mp)) {
2872 PyErr_BadInternalCall();
2873 return -1;
2874 }
2875 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002876}
2877
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002878PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002879PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002880{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002881 if (mp == NULL || !PyDict_Check(mp)) {
2882 PyErr_BadInternalCall();
2883 return NULL;
2884 }
2885 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002886}
2887
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002888PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002889PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002890{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002891 if (mp == NULL || !PyDict_Check(mp)) {
2892 PyErr_BadInternalCall();
2893 return NULL;
2894 }
2895 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002896}
2897
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002898PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002899PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002900{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 if (mp == NULL || !PyDict_Check(mp)) {
2902 PyErr_BadInternalCall();
2903 return NULL;
2904 }
2905 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002906}
2907
Tim Peterse63415e2001-05-08 04:38:29 +00002908/* Return 1 if dicts equal, 0 if not, -1 if error.
2909 * Gets out as soon as any difference is detected.
2910 * Uses only Py_EQ comparison.
2911 */
2912static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002913dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002914{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002915 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002916
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 if (a->ma_used != b->ma_used)
2918 /* can't be equal if # of entries differ */
2919 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002920 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Victor Stinner742da042016-09-07 17:40:12 -07002921 for (i = 0; i < a->ma_keys->dk_nentries; i++) {
2922 PyDictKeyEntry *ep = &DK_ENTRIES(a->ma_keys)[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002923 PyObject *aval;
2924 if (a->ma_values)
2925 aval = a->ma_values[i];
2926 else
2927 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002928 if (aval != NULL) {
2929 int cmp;
2930 PyObject *bval;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002931 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002932 /* temporarily bump aval's refcount to ensure it stays
2933 alive until we're done with it */
2934 Py_INCREF(aval);
2935 /* ditto for key */
2936 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002937 /* reuse the known hash value */
INADA Naoki778928b2017-08-03 23:45:15 +09002938 b->ma_keys->dk_lookup(b, key, ep->me_hash, &bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002939 if (bval == NULL) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002940 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002941 Py_DECREF(aval);
2942 if (PyErr_Occurred())
2943 return -1;
2944 return 0;
2945 }
Dong-hee Na2d5bf562019-12-31 10:04:22 +09002946 Py_INCREF(bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002947 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002948 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002949 Py_DECREF(aval);
Dong-hee Na2d5bf562019-12-31 10:04:22 +09002950 Py_DECREF(bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002951 if (cmp <= 0) /* error or not equal */
2952 return cmp;
2953 }
2954 }
2955 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002956}
Tim Peterse63415e2001-05-08 04:38:29 +00002957
2958static PyObject *
2959dict_richcompare(PyObject *v, PyObject *w, int op)
2960{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002961 int cmp;
2962 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002964 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2965 res = Py_NotImplemented;
2966 }
2967 else if (op == Py_EQ || op == Py_NE) {
2968 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2969 if (cmp < 0)
2970 return NULL;
2971 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2972 }
2973 else
2974 res = Py_NotImplemented;
2975 Py_INCREF(res);
2976 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002977}
Tim Peterse63415e2001-05-08 04:38:29 +00002978
Larry Hastings61272b72014-01-07 12:41:53 -08002979/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002980
2981@coexist
2982dict.__contains__
2983
2984 key: object
2985 /
2986
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002987True if the dictionary has the specified key, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002988[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002989
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002990static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002991dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka19d25972017-02-04 08:05:07 +02002992/*[clinic end generated code: output=a3d03db709ed6e6b input=fe1cb42ad831e820]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002993{
Larry Hastingsc2047262014-01-25 20:43:29 -08002994 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002995 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002996 Py_ssize_t ix;
INADA Naokiba609772016-12-07 20:41:42 +09002997 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002999 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003000 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003001 hash = PyObject_Hash(key);
3002 if (hash == -1)
3003 return NULL;
3004 }
INADA Naoki778928b2017-08-03 23:45:15 +09003005 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003006 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003007 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09003008 if (ix == DKIX_EMPTY || value == NULL)
Victor Stinner742da042016-09-07 17:40:12 -07003009 Py_RETURN_FALSE;
3010 Py_RETURN_TRUE;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003011}
3012
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003013/*[clinic input]
3014dict.get
3015
3016 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02003017 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003018 /
3019
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02003020Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003021[clinic start generated code]*/
3022
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003023static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02003024dict_get_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02003025/*[clinic end generated code: output=bba707729dee05bf input=279ddb5790b6b107]*/
Barry Warsawc38c5da1997-10-06 17:49:20 +00003026{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003027 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003028 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07003029 Py_ssize_t ix;
Barry Warsawc38c5da1997-10-06 17:49:20 +00003030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003031 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003032 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003033 hash = PyObject_Hash(key);
3034 if (hash == -1)
3035 return NULL;
3036 }
INADA Naoki778928b2017-08-03 23:45:15 +09003037 ix = (self->ma_keys->dk_lookup) (self, key, hash, &val);
Victor Stinner742da042016-09-07 17:40:12 -07003038 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003039 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09003040 if (ix == DKIX_EMPTY || val == NULL) {
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02003041 val = default_value;
INADA Naokiba609772016-12-07 20:41:42 +09003042 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003043 Py_INCREF(val);
3044 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00003045}
3046
Benjamin Peterson00e98862013-03-07 22:16:29 -05003047PyObject *
3048PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00003049{
Benjamin Peterson00e98862013-03-07 22:16:29 -05003050 PyDictObject *mp = (PyDictObject *)d;
INADA Naoki93f26f72016-11-02 18:45:16 +09003051 PyObject *value;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003052 Py_hash_t hash;
Guido van Rossum164452c2000-08-08 16:12:54 +00003053
Benjamin Peterson00e98862013-03-07 22:16:29 -05003054 if (!PyDict_Check(d)) {
3055 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003056 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05003057 }
INADA Naoki93f26f72016-11-02 18:45:16 +09003058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003059 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003060 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003061 hash = PyObject_Hash(key);
3062 if (hash == -1)
3063 return NULL;
3064 }
Inada Naoki2ddc7f62019-03-18 20:38:33 +09003065 if (mp->ma_keys == Py_EMPTY_KEYS) {
3066 if (insert_to_emptydict(mp, key, hash, defaultobj) < 0) {
3067 return NULL;
3068 }
3069 return defaultobj;
3070 }
INADA Naoki93f26f72016-11-02 18:45:16 +09003071
3072 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
3073 if (insertion_resize(mp) < 0)
3074 return NULL;
3075 }
3076
INADA Naoki778928b2017-08-03 23:45:15 +09003077 Py_ssize_t ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003078 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003079 return NULL;
INADA Naoki93f26f72016-11-02 18:45:16 +09003080
3081 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09003082 ((ix >= 0 && value == NULL && mp->ma_used != ix) ||
INADA Naoki93f26f72016-11-02 18:45:16 +09003083 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
3084 if (insertion_resize(mp) < 0) {
3085 return NULL;
3086 }
INADA Naoki93f26f72016-11-02 18:45:16 +09003087 ix = DKIX_EMPTY;
3088 }
3089
3090 if (ix == DKIX_EMPTY) {
3091 PyDictKeyEntry *ep, *ep0;
3092 value = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003093 if (mp->ma_keys->dk_usable <= 0) {
Victor Stinner3c336c52016-09-12 14:17:40 +02003094 if (insertion_resize(mp) < 0) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003095 return NULL;
Victor Stinner3c336c52016-09-12 14:17:40 +02003096 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003097 }
INADA Naoki778928b2017-08-03 23:45:15 +09003098 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naoki93f26f72016-11-02 18:45:16 +09003099 ep0 = DK_ENTRIES(mp->ma_keys);
3100 ep = &ep0[mp->ma_keys->dk_nentries];
INADA Naokia7576492018-11-14 18:39:27 +09003101 dictkeys_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05003102 Py_INCREF(key);
INADA Naoki93f26f72016-11-02 18:45:16 +09003103 Py_INCREF(value);
3104 MAINTAIN_TRACKING(mp, key, value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003105 ep->me_key = key;
3106 ep->me_hash = hash;
INADA Naokiba609772016-12-07 20:41:42 +09003107 if (_PyDict_HasSplitTable(mp)) {
INADA Naoki93f26f72016-11-02 18:45:16 +09003108 assert(mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
3109 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Victor Stinner742da042016-09-07 17:40:12 -07003110 }
3111 else {
INADA Naoki93f26f72016-11-02 18:45:16 +09003112 ep->me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07003113 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003114 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003115 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naoki93f26f72016-11-02 18:45:16 +09003116 mp->ma_keys->dk_usable--;
3117 mp->ma_keys->dk_nentries++;
3118 assert(mp->ma_keys->dk_usable >= 0);
3119 }
INADA Naokiba609772016-12-07 20:41:42 +09003120 else if (value == NULL) {
INADA Naoki93f26f72016-11-02 18:45:16 +09003121 value = defaultobj;
3122 assert(_PyDict_HasSplitTable(mp));
3123 assert(ix == mp->ma_used);
3124 Py_INCREF(value);
3125 MAINTAIN_TRACKING(mp, key, value);
INADA Naokiba609772016-12-07 20:41:42 +09003126 mp->ma_values[ix] = value;
INADA Naoki93f26f72016-11-02 18:45:16 +09003127 mp->ma_used++;
3128 mp->ma_version_tag = DICT_NEXT_VERSION();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003129 }
INADA Naoki93f26f72016-11-02 18:45:16 +09003130
Victor Stinner0fc91ee2019-04-12 21:51:34 +02003131 ASSERT_CONSISTENT(mp);
INADA Naoki93f26f72016-11-02 18:45:16 +09003132 return value;
Guido van Rossum164452c2000-08-08 16:12:54 +00003133}
3134
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003135/*[clinic input]
3136dict.setdefault
3137
3138 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02003139 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003140 /
3141
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02003142Insert key with a value of default if key is not in the dictionary.
3143
3144Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003145[clinic start generated code]*/
3146
Benjamin Peterson00e98862013-03-07 22:16:29 -05003147static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02003148dict_setdefault_impl(PyDictObject *self, PyObject *key,
3149 PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02003150/*[clinic end generated code: output=f8c1101ebf69e220 input=0f063756e815fd9d]*/
Benjamin Peterson00e98862013-03-07 22:16:29 -05003151{
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003152 PyObject *val;
Benjamin Peterson00e98862013-03-07 22:16:29 -05003153
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02003154 val = PyDict_SetDefault((PyObject *)self, key, default_value);
Benjamin Peterson00e98862013-03-07 22:16:29 -05003155 Py_XINCREF(val);
3156 return val;
3157}
Guido van Rossum164452c2000-08-08 16:12:54 +00003158
3159static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303160dict_clear(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00003161{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003162 PyDict_Clear((PyObject *)mp);
3163 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00003164}
3165
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003166/*[clinic input]
3167dict.pop
3168
3169 key: object
3170 default: object = NULL
3171 /
3172
Serhiy Storchaka279f4462019-09-14 12:24:05 +03003173D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003174
Serhiy Storchaka6bf323732020-07-19 09:18:55 +03003175If the key is not found, return the default if given; otherwise,
3176raise a KeyError.
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003177[clinic start generated code]*/
3178
Guido van Rossumba6ab842000-12-12 22:02:18 +00003179static PyObject *
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003180dict_pop_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
Serhiy Storchaka6bf323732020-07-19 09:18:55 +03003181/*[clinic end generated code: output=3abb47b89f24c21c input=e221baa01044c44c]*/
Guido van Rossume027d982002-04-12 15:11:59 +00003182{
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003183 return _PyDict_Pop((PyObject*)self, key, default_value);
Guido van Rossume027d982002-04-12 15:11:59 +00003184}
3185
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003186/*[clinic input]
3187dict.popitem
3188
3189Remove and return a (key, value) pair as a 2-tuple.
3190
3191Pairs are returned in LIFO (last-in, first-out) order.
3192Raises KeyError if the dict is empty.
3193[clinic start generated code]*/
3194
Guido van Rossume027d982002-04-12 15:11:59 +00003195static PyObject *
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003196dict_popitem_impl(PyDictObject *self)
3197/*[clinic end generated code: output=e65fcb04420d230d input=1c38a49f21f64941]*/
Guido van Rossumba6ab842000-12-12 22:02:18 +00003198{
Victor Stinner742da042016-09-07 17:40:12 -07003199 Py_ssize_t i, j;
3200 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003201 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00003202
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003203 /* Allocate the result tuple before checking the size. Believe it
3204 * or not, this allocation could trigger a garbage collection which
3205 * could empty the dict, so if we checked the size first and that
3206 * happened, the result would be an infinite loop (searching for an
3207 * entry that no longer exists). Note that the usual popitem()
3208 * idiom is "while d: k, v = d.popitem()". so needing to throw the
3209 * tuple away if the dict *is* empty isn't a significant
3210 * inefficiency -- possible, but unlikely in practice.
3211 */
3212 res = PyTuple_New(2);
3213 if (res == NULL)
3214 return NULL;
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003215 if (self->ma_used == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003216 Py_DECREF(res);
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003217 PyErr_SetString(PyExc_KeyError, "popitem(): dictionary is empty");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003218 return NULL;
3219 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003220 /* Convert split table to combined table */
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003221 if (self->ma_keys->dk_lookup == lookdict_split) {
3222 if (dictresize(self, DK_SIZE(self->ma_keys))) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003223 Py_DECREF(res);
3224 return NULL;
3225 }
3226 }
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003227 ENSURE_ALLOWS_DELETIONS(self);
Victor Stinner742da042016-09-07 17:40:12 -07003228
3229 /* Pop last item */
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003230 ep0 = DK_ENTRIES(self->ma_keys);
3231 i = self->ma_keys->dk_nentries - 1;
Victor Stinner742da042016-09-07 17:40:12 -07003232 while (i >= 0 && ep0[i].me_value == NULL) {
3233 i--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003234 }
Victor Stinner742da042016-09-07 17:40:12 -07003235 assert(i >= 0);
3236
3237 ep = &ep0[i];
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003238 j = lookdict_index(self->ma_keys, ep->me_hash, i);
Victor Stinner742da042016-09-07 17:40:12 -07003239 assert(j >= 0);
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003240 assert(dictkeys_get_index(self->ma_keys, j) == i);
3241 dictkeys_set_index(self->ma_keys, j, DKIX_DUMMY);
Victor Stinner742da042016-09-07 17:40:12 -07003242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003243 PyTuple_SET_ITEM(res, 0, ep->me_key);
3244 PyTuple_SET_ITEM(res, 1, ep->me_value);
Victor Stinner742da042016-09-07 17:40:12 -07003245 ep->me_key = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003246 ep->me_value = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07003247 /* We can't dk_usable++ since there is DKIX_DUMMY in indices */
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003248 self->ma_keys->dk_nentries = i;
3249 self->ma_used--;
3250 self->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner0fc91ee2019-04-12 21:51:34 +02003251 ASSERT_CONSISTENT(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003252 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00003253}
3254
Jeremy Hylton8caad492000-06-23 14:18:11 +00003255static int
3256dict_traverse(PyObject *op, visitproc visit, void *arg)
3257{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003258 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson55f44522016-09-05 12:12:59 -07003259 PyDictKeysObject *keys = mp->ma_keys;
Serhiy Storchaka46825d22016-09-26 21:29:34 +03003260 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Victor Stinner742da042016-09-07 17:40:12 -07003261 Py_ssize_t i, n = keys->dk_nentries;
3262
Benjamin Peterson55f44522016-09-05 12:12:59 -07003263 if (keys->dk_lookup == lookdict) {
3264 for (i = 0; i < n; i++) {
3265 if (entries[i].me_value != NULL) {
3266 Py_VISIT(entries[i].me_value);
3267 Py_VISIT(entries[i].me_key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003268 }
3269 }
Victor Stinner742da042016-09-07 17:40:12 -07003270 }
3271 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003272 if (mp->ma_values != NULL) {
Benjamin Peterson55f44522016-09-05 12:12:59 -07003273 for (i = 0; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003274 Py_VISIT(mp->ma_values[i]);
3275 }
3276 }
3277 else {
Benjamin Peterson55f44522016-09-05 12:12:59 -07003278 for (i = 0; i < n; i++) {
3279 Py_VISIT(entries[i].me_value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003280 }
3281 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003282 }
3283 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00003284}
3285
3286static int
3287dict_tp_clear(PyObject *op)
3288{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003289 PyDict_Clear(op);
3290 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00003291}
3292
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003293static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003294
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003295Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003296_PyDict_SizeOf(PyDictObject *mp)
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003297{
Victor Stinner742da042016-09-07 17:40:12 -07003298 Py_ssize_t size, usable, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003299
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003300 size = DK_SIZE(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07003301 usable = USABLE_FRACTION(size);
3302
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02003303 res = _PyObject_SIZE(Py_TYPE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003304 if (mp->ma_values)
Victor Stinner742da042016-09-07 17:40:12 -07003305 res += usable * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003306 /* If the dictionary is split, the keys portion is accounted-for
3307 in the type object. */
3308 if (mp->ma_keys->dk_refcnt == 1)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003309 res += (sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003310 + DK_IXSIZE(mp->ma_keys) * size
3311 + sizeof(PyDictKeyEntry) * usable);
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003312 return res;
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003313}
3314
3315Py_ssize_t
3316_PyDict_KeysSize(PyDictKeysObject *keys)
3317{
Victor Stinner98ee9d52016-09-08 09:33:56 -07003318 return (sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003319 + DK_IXSIZE(keys) * DK_SIZE(keys)
3320 + USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry));
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003321}
3322
doko@ubuntu.com17210f52016-01-14 14:04:59 +01003323static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303324dict_sizeof(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003325{
3326 return PyLong_FromSsize_t(_PyDict_SizeOf(mp));
3327}
3328
Brandt Buchereb8ac572020-02-24 19:47:34 -08003329static PyObject *
3330dict_or(PyObject *self, PyObject *other)
3331{
3332 if (!PyDict_Check(self) || !PyDict_Check(other)) {
3333 Py_RETURN_NOTIMPLEMENTED;
3334 }
3335 PyObject *new = PyDict_Copy(self);
3336 if (new == NULL) {
3337 return NULL;
3338 }
3339 if (dict_update_arg(new, other)) {
3340 Py_DECREF(new);
3341 return NULL;
3342 }
3343 return new;
3344}
3345
3346static PyObject *
3347dict_ior(PyObject *self, PyObject *other)
3348{
3349 if (dict_update_arg(self, other)) {
3350 return NULL;
3351 }
3352 Py_INCREF(self);
3353 return self;
3354}
3355
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00003356PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
3357
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003358PyDoc_STRVAR(sizeof__doc__,
3359"D.__sizeof__() -> size of D in memory, in bytes");
3360
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003361PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04003362"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
3363If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
3364If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
3365In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00003366
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003367PyDoc_STRVAR(clear__doc__,
3368"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003369
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003370PyDoc_STRVAR(copy__doc__,
3371"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00003372
Guido van Rossumb90c8482007-02-10 01:11:45 +00003373/* Forward */
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303374static PyObject *dictkeys_new(PyObject *, PyObject *);
3375static PyObject *dictitems_new(PyObject *, PyObject *);
3376static PyObject *dictvalues_new(PyObject *, PyObject *);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003377
Guido van Rossum45c85d12007-07-27 16:31:40 +00003378PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003379 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003380PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003381 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003382PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003383 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00003384
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003385static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07003386 DICT___CONTAINS___METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003387 {"__getitem__", (PyCFunction)(void(*)(void))dict_subscript, METH_O | METH_COEXIST,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003388 getitem__doc__},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003389 {"__sizeof__", (PyCFunction)(void(*)(void))dict_sizeof, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003390 sizeof__doc__},
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003391 DICT_GET_METHODDEF
3392 DICT_SETDEFAULT_METHODDEF
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003393 DICT_POP_METHODDEF
3394 DICT_POPITEM_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303395 {"keys", dictkeys_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003396 keys__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303397 {"items", dictitems_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003398 items__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303399 {"values", dictvalues_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003400 values__doc__},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003401 {"update", (PyCFunction)(void(*)(void))dict_update, METH_VARARGS | METH_KEYWORDS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003402 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08003403 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003404 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
3405 clear__doc__},
3406 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
3407 copy__doc__},
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003408 DICT___REVERSED___METHODDEF
Guido van Rossum48b069a2020-04-07 09:50:06 -07003409 {"__class_getitem__", (PyCFunction)Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003410 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003411};
3412
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00003413/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00003414int
3415PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003416{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003417 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07003418 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003419 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003420 PyObject *value;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003422 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003423 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003424 hash = PyObject_Hash(key);
3425 if (hash == -1)
3426 return -1;
3427 }
INADA Naoki778928b2017-08-03 23:45:15 +09003428 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003429 if (ix == DKIX_ERROR)
3430 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003431 return (ix != DKIX_EMPTY && value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003432}
3433
Thomas Wouterscf297e42007-02-23 15:07:44 +00003434/* Internal version of PyDict_Contains used when the hash value is already known */
3435int
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02003436_PyDict_Contains_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00003437{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003438 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003439 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -07003440 Py_ssize_t ix;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003441
INADA Naoki778928b2017-08-03 23:45:15 +09003442 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003443 if (ix == DKIX_ERROR)
3444 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003445 return (ix != DKIX_EMPTY && value != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00003446}
3447
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02003448int
3449_PyDict_ContainsId(PyObject *op, struct _Py_Identifier *key)
3450{
3451 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
3452 if (kv == NULL) {
3453 return -1;
3454 }
3455 return PyDict_Contains(op, kv);
3456}
3457
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003458/* Hack to implement "key in dict" */
3459static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003460 0, /* sq_length */
3461 0, /* sq_concat */
3462 0, /* sq_repeat */
3463 0, /* sq_item */
3464 0, /* sq_slice */
3465 0, /* sq_ass_item */
3466 0, /* sq_ass_slice */
3467 PyDict_Contains, /* sq_contains */
3468 0, /* sq_inplace_concat */
3469 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003470};
3471
Brandt Buchereb8ac572020-02-24 19:47:34 -08003472static PyNumberMethods dict_as_number = {
3473 .nb_or = dict_or,
3474 .nb_inplace_or = dict_ior,
3475};
3476
Guido van Rossum09e563a2001-05-01 12:10:21 +00003477static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003478dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3479{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003480 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003481 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003483 assert(type != NULL && type->tp_alloc != NULL);
3484 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02003485 if (self == NULL)
3486 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003487 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003488
Victor Stinnera9f61a52013-07-16 22:17:26 +02003489 /* The object has been implicitly tracked by tp_alloc */
Inada Naokidb6d9a52020-08-04 11:08:06 +09003490 if (type == &PyDict_Type) {
Victor Stinnera9f61a52013-07-16 22:17:26 +02003491 _PyObject_GC_UNTRACK(d);
Inada Naokidb6d9a52020-08-04 11:08:06 +09003492 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003493
3494 d->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003495 d->ma_version_tag = DICT_NEXT_VERSION();
Inada Naokidb6d9a52020-08-04 11:08:06 +09003496 dictkeys_incref(Py_EMPTY_KEYS);
3497 d->ma_keys = Py_EMPTY_KEYS;
3498 d->ma_values = empty_values;
Victor Stinner0fc91ee2019-04-12 21:51:34 +02003499 ASSERT_CONSISTENT(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003500 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003501}
3502
Tim Peters25786c02001-09-02 08:22:48 +00003503static int
3504dict_init(PyObject *self, PyObject *args, PyObject *kwds)
3505{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003506 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00003507}
3508
Tim Peters6d6c1a32001-08-02 04:15:00 +00003509static PyObject *
Dong-hee Nae27916b2020-04-02 09:55:43 +09003510dict_vectorcall(PyObject *type, PyObject * const*args,
3511 size_t nargsf, PyObject *kwnames)
3512{
3513 assert(PyType_Check(type));
3514 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
3515 if (!_PyArg_CheckPositional("dict", nargs, 0, 1)) {
3516 return NULL;
3517 }
3518
3519 PyObject *self = dict_new((PyTypeObject *)type, NULL, NULL);
3520 if (self == NULL) {
3521 return NULL;
3522 }
3523 if (nargs == 1) {
3524 if (dict_update_arg(self, args[0]) < 0) {
3525 Py_DECREF(self);
3526 return NULL;
3527 }
3528 args++;
3529 }
3530 if (kwnames != NULL) {
3531 for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(kwnames); i++) {
3532 if (PyDict_SetItem(self, PyTuple_GET_ITEM(kwnames, i), args[i]) < 0) {
3533 Py_DECREF(self);
3534 return NULL;
3535 }
3536 }
3537 }
3538 return self;
3539}
3540
3541static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003542dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00003543{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003544 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003545}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003546
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003547PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00003548"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00003549"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003550" (key, value) pairs\n"
3551"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00003552" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003553" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00003554" d[k] = v\n"
3555"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
3556" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00003557
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003558PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003559 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3560 "dict",
3561 sizeof(PyDictObject),
3562 0,
3563 (destructor)dict_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003564 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003565 0, /* tp_getattr */
3566 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003567 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003568 (reprfunc)dict_repr, /* tp_repr */
Brandt Buchereb8ac572020-02-24 19:47:34 -08003569 &dict_as_number, /* tp_as_number */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003570 &dict_as_sequence, /* tp_as_sequence */
3571 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003572 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003573 0, /* tp_call */
3574 0, /* tp_str */
3575 PyObject_GenericGetAttr, /* tp_getattro */
3576 0, /* tp_setattro */
3577 0, /* tp_as_buffer */
3578 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3579 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
3580 dictionary_doc, /* tp_doc */
3581 dict_traverse, /* tp_traverse */
3582 dict_tp_clear, /* tp_clear */
3583 dict_richcompare, /* tp_richcompare */
3584 0, /* tp_weaklistoffset */
3585 (getiterfunc)dict_iter, /* tp_iter */
3586 0, /* tp_iternext */
3587 mapp_methods, /* tp_methods */
3588 0, /* tp_members */
3589 0, /* tp_getset */
3590 0, /* tp_base */
3591 0, /* tp_dict */
3592 0, /* tp_descr_get */
3593 0, /* tp_descr_set */
3594 0, /* tp_dictoffset */
3595 dict_init, /* tp_init */
3596 PyType_GenericAlloc, /* tp_alloc */
3597 dict_new, /* tp_new */
3598 PyObject_GC_Del, /* tp_free */
Dong-hee Nae27916b2020-04-02 09:55:43 +09003599 .tp_vectorcall = dict_vectorcall,
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003600};
3601
Guido van Rossum3cca2451997-05-16 14:23:33 +00003602/* For backward compatibility with old dictionary interface */
3603
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003604PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003605PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003606{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003607 PyObject *kv, *rv;
3608 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003609 if (kv == NULL) {
3610 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003611 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003612 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003613 rv = PyDict_GetItem(v, kv);
3614 Py_DECREF(kv);
3615 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003616}
3617
3618int
Victor Stinner3c1e4812012-03-26 22:10:51 +02003619_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
3620{
3621 PyObject *kv;
3622 kv = _PyUnicode_FromId(key); /* borrowed */
3623 if (kv == NULL)
3624 return -1;
3625 return PyDict_SetItem(v, kv, item);
3626}
3627
3628int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003629PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003630{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003631 PyObject *kv;
3632 int err;
3633 kv = PyUnicode_FromString(key);
3634 if (kv == NULL)
3635 return -1;
3636 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
3637 err = PyDict_SetItem(v, kv, item);
3638 Py_DECREF(kv);
3639 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003640}
3641
3642int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01003643_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
3644{
3645 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
3646 if (kv == NULL)
3647 return -1;
3648 return PyDict_DelItem(v, kv);
3649}
3650
3651int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003652PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003653{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003654 PyObject *kv;
3655 int err;
3656 kv = PyUnicode_FromString(key);
3657 if (kv == NULL)
3658 return -1;
3659 err = PyDict_DelItem(v, kv);
3660 Py_DECREF(kv);
3661 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003662}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003663
Raymond Hettinger019a1482004-03-18 02:41:19 +00003664/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003665
3666typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003667 PyObject_HEAD
3668 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
3669 Py_ssize_t di_used;
3670 Py_ssize_t di_pos;
3671 PyObject* di_result; /* reusable result tuple for iteritems */
3672 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003673} dictiterobject;
3674
3675static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003676dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003677{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003678 dictiterobject *di;
3679 di = PyObject_GC_New(dictiterobject, itertype);
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003680 if (di == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003681 return NULL;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003682 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003683 Py_INCREF(dict);
3684 di->di_dict = dict;
3685 di->di_used = dict->ma_used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003686 di->len = dict->ma_used;
Dong-hee Na24dc2f82019-10-20 05:01:08 +09003687 if (itertype == &PyDictRevIterKey_Type ||
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003688 itertype == &PyDictRevIterItem_Type ||
Dong-hee Na24dc2f82019-10-20 05:01:08 +09003689 itertype == &PyDictRevIterValue_Type) {
3690 if (dict->ma_values) {
3691 di->di_pos = dict->ma_used - 1;
3692 }
3693 else {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003694 di->di_pos = dict->ma_keys->dk_nentries - 1;
Dong-hee Na24dc2f82019-10-20 05:01:08 +09003695 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003696 }
3697 else {
3698 di->di_pos = 0;
3699 }
3700 if (itertype == &PyDictIterItem_Type ||
3701 itertype == &PyDictRevIterItem_Type) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003702 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
3703 if (di->di_result == NULL) {
3704 Py_DECREF(di);
3705 return NULL;
3706 }
3707 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003708 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003709 di->di_result = NULL;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003710 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003711 _PyObject_GC_TRACK(di);
3712 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003713}
3714
3715static void
3716dictiter_dealloc(dictiterobject *di)
3717{
INADA Naokia6296d32017-08-24 14:55:17 +09003718 /* bpo-31095: UnTrack is needed before calling any callbacks */
3719 _PyObject_GC_UNTRACK(di);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003720 Py_XDECREF(di->di_dict);
3721 Py_XDECREF(di->di_result);
3722 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003723}
3724
3725static int
3726dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
3727{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003728 Py_VISIT(di->di_dict);
3729 Py_VISIT(di->di_result);
3730 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003731}
3732
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003733static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303734dictiter_len(dictiterobject *di, PyObject *Py_UNUSED(ignored))
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003735{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003736 Py_ssize_t len = 0;
3737 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
3738 len = di->len;
3739 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003740}
3741
Guido van Rossumb90c8482007-02-10 01:11:45 +00003742PyDoc_STRVAR(length_hint_doc,
3743 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003744
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003745static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303746dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003747
3748PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3749
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003750static PyMethodDef dictiter_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003751 {"__length_hint__", (PyCFunction)(void(*)(void))dictiter_len, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003752 length_hint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003753 {"__reduce__", (PyCFunction)(void(*)(void))dictiter_reduce, METH_NOARGS,
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003754 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003755 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003756};
3757
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003758static PyObject*
3759dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00003760{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003761 PyObject *key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003762 Py_ssize_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003763 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003764 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00003765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003766 if (d == NULL)
3767 return NULL;
3768 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00003769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003770 if (di->di_used != d->ma_used) {
3771 PyErr_SetString(PyExc_RuntimeError,
3772 "dictionary changed size during iteration");
3773 di->di_used = -1; /* Make this state sticky */
3774 return NULL;
3775 }
Guido van Rossum2147df72002-07-16 20:30:22 +00003776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003777 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003778 k = d->ma_keys;
INADA Naokica2d8be2016-11-04 16:59:10 +09003779 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003780 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003781 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003782 goto fail;
3783 key = DK_ENTRIES(k)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003784 assert(d->ma_values[i] != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003785 }
3786 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003787 Py_ssize_t n = k->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003788 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
3789 while (i < n && entry_ptr->me_value == NULL) {
3790 entry_ptr++;
3791 i++;
3792 }
3793 if (i >= n)
3794 goto fail;
3795 key = entry_ptr->me_key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003796 }
Thomas Perl796cc6e2019-03-28 07:03:25 +01003797 // We found an element (key), but did not expect it
3798 if (di->len == 0) {
3799 PyErr_SetString(PyExc_RuntimeError,
3800 "dictionary keys changed during iteration");
3801 goto fail;
3802 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003803 di->di_pos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003804 di->len--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003805 Py_INCREF(key);
3806 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003807
3808fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003809 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003810 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003811 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003812}
3813
Raymond Hettinger019a1482004-03-18 02:41:19 +00003814PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003815 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3816 "dict_keyiterator", /* tp_name */
3817 sizeof(dictiterobject), /* tp_basicsize */
3818 0, /* tp_itemsize */
3819 /* methods */
3820 (destructor)dictiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003821 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003822 0, /* tp_getattr */
3823 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003824 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003825 0, /* tp_repr */
3826 0, /* tp_as_number */
3827 0, /* tp_as_sequence */
3828 0, /* tp_as_mapping */
3829 0, /* tp_hash */
3830 0, /* tp_call */
3831 0, /* tp_str */
3832 PyObject_GenericGetAttr, /* tp_getattro */
3833 0, /* tp_setattro */
3834 0, /* tp_as_buffer */
3835 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3836 0, /* tp_doc */
3837 (traverseproc)dictiter_traverse, /* tp_traverse */
3838 0, /* tp_clear */
3839 0, /* tp_richcompare */
3840 0, /* tp_weaklistoffset */
3841 PyObject_SelfIter, /* tp_iter */
3842 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
3843 dictiter_methods, /* tp_methods */
3844 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003845};
3846
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003847static PyObject *
3848dictiter_iternextvalue(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003849{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003850 PyObject *value;
INADA Naokica2d8be2016-11-04 16:59:10 +09003851 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003852 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003854 if (d == NULL)
3855 return NULL;
3856 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003857
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003858 if (di->di_used != d->ma_used) {
3859 PyErr_SetString(PyExc_RuntimeError,
3860 "dictionary changed size during iteration");
3861 di->di_used = -1; /* Make this state sticky */
3862 return NULL;
3863 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003865 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003866 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003867 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003868 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003869 goto fail;
INADA Naokica2d8be2016-11-04 16:59:10 +09003870 value = d->ma_values[i];
3871 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003872 }
3873 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003874 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003875 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3876 while (i < n && entry_ptr->me_value == NULL) {
3877 entry_ptr++;
3878 i++;
3879 }
3880 if (i >= n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003881 goto fail;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003882 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003883 }
Thomas Perlb8311cf2019-04-02 11:30:10 +02003884 // We found an element, but did not expect it
3885 if (di->len == 0) {
3886 PyErr_SetString(PyExc_RuntimeError,
3887 "dictionary keys changed during iteration");
3888 goto fail;
3889 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003890 di->di_pos = i+1;
3891 di->len--;
3892 Py_INCREF(value);
3893 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003894
3895fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003896 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003897 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003898 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003899}
3900
3901PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003902 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3903 "dict_valueiterator", /* tp_name */
3904 sizeof(dictiterobject), /* tp_basicsize */
3905 0, /* tp_itemsize */
3906 /* methods */
3907 (destructor)dictiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003908 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003909 0, /* tp_getattr */
3910 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003911 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003912 0, /* tp_repr */
3913 0, /* tp_as_number */
3914 0, /* tp_as_sequence */
3915 0, /* tp_as_mapping */
3916 0, /* tp_hash */
3917 0, /* tp_call */
3918 0, /* tp_str */
3919 PyObject_GenericGetAttr, /* tp_getattro */
3920 0, /* tp_setattro */
3921 0, /* tp_as_buffer */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003922 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003923 0, /* tp_doc */
3924 (traverseproc)dictiter_traverse, /* tp_traverse */
3925 0, /* tp_clear */
3926 0, /* tp_richcompare */
3927 0, /* tp_weaklistoffset */
3928 PyObject_SelfIter, /* tp_iter */
3929 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3930 dictiter_methods, /* tp_methods */
3931 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003932};
3933
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003934static PyObject *
3935dictiter_iternextitem(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003936{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003937 PyObject *key, *value, *result;
INADA Naokica2d8be2016-11-04 16:59:10 +09003938 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003939 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003940
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003941 if (d == NULL)
3942 return NULL;
3943 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003944
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003945 if (di->di_used != d->ma_used) {
3946 PyErr_SetString(PyExc_RuntimeError,
3947 "dictionary changed size during iteration");
3948 di->di_used = -1; /* Make this state sticky */
3949 return NULL;
3950 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003952 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003953 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003954 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003955 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003956 goto fail;
3957 key = DK_ENTRIES(d->ma_keys)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003958 value = d->ma_values[i];
3959 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003960 }
3961 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003962 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003963 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3964 while (i < n && entry_ptr->me_value == NULL) {
3965 entry_ptr++;
3966 i++;
3967 }
3968 if (i >= n)
3969 goto fail;
3970 key = entry_ptr->me_key;
3971 value = entry_ptr->me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003972 }
Thomas Perlb8311cf2019-04-02 11:30:10 +02003973 // We found an element, but did not expect it
3974 if (di->len == 0) {
3975 PyErr_SetString(PyExc_RuntimeError,
3976 "dictionary keys changed during iteration");
3977 goto fail;
3978 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003979 di->di_pos = i+1;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003980 di->len--;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003981 Py_INCREF(key);
3982 Py_INCREF(value);
3983 result = di->di_result;
3984 if (Py_REFCNT(result) == 1) {
3985 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3986 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3987 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3988 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003989 Py_INCREF(result);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003990 Py_DECREF(oldkey);
3991 Py_DECREF(oldvalue);
Brandt Bucher226a0122020-12-04 19:45:57 -08003992 // bpo-42536: The GC may have untracked this result tuple. Since we're
3993 // recycling it, make sure it's tracked again:
3994 if (!_PyObject_GC_IS_TRACKED(result)) {
3995 _PyObject_GC_TRACK(result);
3996 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003997 }
3998 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003999 result = PyTuple_New(2);
4000 if (result == NULL)
4001 return NULL;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004002 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
4003 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004004 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004005 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00004006
4007fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004008 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03004009 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004010 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00004011}
4012
4013PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004014 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4015 "dict_itemiterator", /* tp_name */
4016 sizeof(dictiterobject), /* tp_basicsize */
4017 0, /* tp_itemsize */
4018 /* methods */
4019 (destructor)dictiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004020 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004021 0, /* tp_getattr */
4022 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004023 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004024 0, /* tp_repr */
4025 0, /* tp_as_number */
4026 0, /* tp_as_sequence */
4027 0, /* tp_as_mapping */
4028 0, /* tp_hash */
4029 0, /* tp_call */
4030 0, /* tp_str */
4031 PyObject_GenericGetAttr, /* tp_getattro */
4032 0, /* tp_setattro */
4033 0, /* tp_as_buffer */
4034 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4035 0, /* tp_doc */
4036 (traverseproc)dictiter_traverse, /* tp_traverse */
4037 0, /* tp_clear */
4038 0, /* tp_richcompare */
4039 0, /* tp_weaklistoffset */
4040 PyObject_SelfIter, /* tp_iter */
4041 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
4042 dictiter_methods, /* tp_methods */
4043 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00004044};
Guido van Rossumb90c8482007-02-10 01:11:45 +00004045
4046
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004047/* dictreviter */
4048
4049static PyObject *
4050dictreviter_iternext(dictiterobject *di)
4051{
4052 PyDictObject *d = di->di_dict;
4053
4054 if (d == NULL) {
4055 return NULL;
4056 }
4057 assert (PyDict_Check(d));
4058
4059 if (di->di_used != d->ma_used) {
4060 PyErr_SetString(PyExc_RuntimeError,
4061 "dictionary changed size during iteration");
4062 di->di_used = -1; /* Make this state sticky */
4063 return NULL;
4064 }
4065
4066 Py_ssize_t i = di->di_pos;
4067 PyDictKeysObject *k = d->ma_keys;
4068 PyObject *key, *value, *result;
4069
Serhiy Storchaka2e3d8732019-10-23 14:48:08 +03004070 if (i < 0) {
4071 goto fail;
4072 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004073 if (d->ma_values) {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004074 key = DK_ENTRIES(k)[i].me_key;
4075 value = d->ma_values[i];
4076 assert (value != NULL);
4077 }
4078 else {
4079 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
Serhiy Storchaka2e3d8732019-10-23 14:48:08 +03004080 while (entry_ptr->me_value == NULL) {
4081 if (--i < 0) {
4082 goto fail;
4083 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004084 entry_ptr--;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004085 }
4086 key = entry_ptr->me_key;
4087 value = entry_ptr->me_value;
4088 }
4089 di->di_pos = i-1;
4090 di->len--;
4091
Dong-hee Na1b55b652020-02-17 19:09:15 +09004092 if (Py_IS_TYPE(di, &PyDictRevIterKey_Type)) {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004093 Py_INCREF(key);
4094 return key;
4095 }
Dong-hee Na1b55b652020-02-17 19:09:15 +09004096 else if (Py_IS_TYPE(di, &PyDictRevIterValue_Type)) {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004097 Py_INCREF(value);
4098 return value;
4099 }
Dong-hee Na1b55b652020-02-17 19:09:15 +09004100 else if (Py_IS_TYPE(di, &PyDictRevIterItem_Type)) {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004101 Py_INCREF(key);
4102 Py_INCREF(value);
4103 result = di->di_result;
4104 if (Py_REFCNT(result) == 1) {
4105 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
4106 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
4107 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
4108 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
4109 Py_INCREF(result);
4110 Py_DECREF(oldkey);
4111 Py_DECREF(oldvalue);
Brandt Bucher226a0122020-12-04 19:45:57 -08004112 // bpo-42536: The GC may have untracked this result tuple. Since
4113 // we're recycling it, make sure it's tracked again:
4114 if (!_PyObject_GC_IS_TRACKED(result)) {
4115 _PyObject_GC_TRACK(result);
4116 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004117 }
4118 else {
4119 result = PyTuple_New(2);
4120 if (result == NULL) {
4121 return NULL;
4122 }
4123 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
4124 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
4125 }
4126 return result;
4127 }
4128 else {
4129 Py_UNREACHABLE();
4130 }
4131
4132fail:
4133 di->di_dict = NULL;
4134 Py_DECREF(d);
4135 return NULL;
4136}
4137
4138PyTypeObject PyDictRevIterKey_Type = {
4139 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4140 "dict_reversekeyiterator",
4141 sizeof(dictiterobject),
4142 .tp_dealloc = (destructor)dictiter_dealloc,
4143 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
4144 .tp_traverse = (traverseproc)dictiter_traverse,
4145 .tp_iter = PyObject_SelfIter,
4146 .tp_iternext = (iternextfunc)dictreviter_iternext,
4147 .tp_methods = dictiter_methods
4148};
4149
4150
4151/*[clinic input]
4152dict.__reversed__
4153
4154Return a reverse iterator over the dict keys.
4155[clinic start generated code]*/
4156
4157static PyObject *
4158dict___reversed___impl(PyDictObject *self)
4159/*[clinic end generated code: output=e674483336d1ed51 input=23210ef3477d8c4d]*/
4160{
4161 assert (PyDict_Check(self));
4162 return dictiter_new(self, &PyDictRevIterKey_Type);
4163}
4164
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004165static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304166dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004167{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004168 _Py_IDENTIFIER(iter);
Sergey Fedoseev63958442018-10-20 05:43:33 +05004169 /* copy the iterator state */
4170 dictiterobject tmp = *di;
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004171 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004172
Sergey Fedoseev63958442018-10-20 05:43:33 +05004173 PyObject *list = PySequence_List((PyObject*)&tmp);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004174 Py_XDECREF(tmp.di_dict);
Sergey Fedoseev63958442018-10-20 05:43:33 +05004175 if (list == NULL) {
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004176 return NULL;
4177 }
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004178 return Py_BuildValue("N(N)", _PyEval_GetBuiltinId(&PyId_iter), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004179}
4180
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004181PyTypeObject PyDictRevIterItem_Type = {
4182 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4183 "dict_reverseitemiterator",
4184 sizeof(dictiterobject),
4185 .tp_dealloc = (destructor)dictiter_dealloc,
4186 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
4187 .tp_traverse = (traverseproc)dictiter_traverse,
4188 .tp_iter = PyObject_SelfIter,
4189 .tp_iternext = (iternextfunc)dictreviter_iternext,
4190 .tp_methods = dictiter_methods
4191};
4192
4193PyTypeObject PyDictRevIterValue_Type = {
4194 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4195 "dict_reversevalueiterator",
4196 sizeof(dictiterobject),
4197 .tp_dealloc = (destructor)dictiter_dealloc,
4198 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
4199 .tp_traverse = (traverseproc)dictiter_traverse,
4200 .tp_iter = PyObject_SelfIter,
4201 .tp_iternext = (iternextfunc)dictreviter_iternext,
4202 .tp_methods = dictiter_methods
4203};
4204
Guido van Rossum3ac67412007-02-10 18:55:06 +00004205/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004206/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00004207/***********************************************/
4208
Guido van Rossumb90c8482007-02-10 01:11:45 +00004209/* The instance lay-out is the same for all three; but the type differs. */
4210
Guido van Rossumb90c8482007-02-10 01:11:45 +00004211static void
Eric Snow96c6af92015-05-29 22:21:39 -06004212dictview_dealloc(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004213{
INADA Naokia6296d32017-08-24 14:55:17 +09004214 /* bpo-31095: UnTrack is needed before calling any callbacks */
4215 _PyObject_GC_UNTRACK(dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004216 Py_XDECREF(dv->dv_dict);
4217 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00004218}
4219
4220static int
Eric Snow96c6af92015-05-29 22:21:39 -06004221dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
Antoine Pitrou7ddda782009-01-01 15:35:33 +00004222{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004223 Py_VISIT(dv->dv_dict);
4224 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004225}
4226
Guido van Rossum83825ac2007-02-10 04:54:19 +00004227static Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06004228dictview_len(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004229{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004230 Py_ssize_t len = 0;
4231 if (dv->dv_dict != NULL)
4232 len = dv->dv_dict->ma_used;
4233 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004234}
4235
Eric Snow96c6af92015-05-29 22:21:39 -06004236PyObject *
4237_PyDictView_New(PyObject *dict, PyTypeObject *type)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004238{
Eric Snow96c6af92015-05-29 22:21:39 -06004239 _PyDictViewObject *dv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004240 if (dict == NULL) {
4241 PyErr_BadInternalCall();
4242 return NULL;
4243 }
4244 if (!PyDict_Check(dict)) {
4245 /* XXX Get rid of this restriction later */
4246 PyErr_Format(PyExc_TypeError,
4247 "%s() requires a dict argument, not '%s'",
Victor Stinner58ac7002020-02-07 03:04:21 +01004248 type->tp_name, Py_TYPE(dict)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004249 return NULL;
4250 }
Eric Snow96c6af92015-05-29 22:21:39 -06004251 dv = PyObject_GC_New(_PyDictViewObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004252 if (dv == NULL)
4253 return NULL;
4254 Py_INCREF(dict);
4255 dv->dv_dict = (PyDictObject *)dict;
4256 _PyObject_GC_TRACK(dv);
4257 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004258}
4259
Dennis Sweeney3ee0e482020-06-12 13:19:25 -04004260static PyObject *
Pablo Galindo10c3b212020-06-15 02:05:20 +01004261dictview_mapping(PyObject *view, void *Py_UNUSED(ignored)) {
Dennis Sweeney3ee0e482020-06-12 13:19:25 -04004262 assert(view != NULL);
4263 assert(PyDictKeys_Check(view)
4264 || PyDictValues_Check(view)
4265 || PyDictItems_Check(view));
4266 PyObject *mapping = (PyObject *)((_PyDictViewObject *)view)->dv_dict;
4267 return PyDictProxy_New(mapping);
4268}
4269
4270static PyGetSetDef dictview_getset[] = {
Pablo Galindo10c3b212020-06-15 02:05:20 +01004271 {"mapping", dictview_mapping, (setter)NULL,
Dennis Sweeney3ee0e482020-06-12 13:19:25 -04004272 "dictionary that this view refers to", NULL},
4273 {0}
4274};
4275
Neal Norwitze36f2ba2007-02-26 23:12:28 +00004276/* TODO(guido): The views objects are not complete:
4277
4278 * support more set operations
4279 * support arbitrary mappings?
4280 - either these should be static or exported in dictobject.h
4281 - if public then they should probably be in builtins
4282*/
4283
Guido van Rossumaac530c2007-08-24 22:33:45 +00004284/* Return 1 if self is a subset of other, iterating over self;
4285 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00004286static int
4287all_contained_in(PyObject *self, PyObject *other)
4288{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004289 PyObject *iter = PyObject_GetIter(self);
4290 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00004291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004292 if (iter == NULL)
4293 return -1;
4294 for (;;) {
4295 PyObject *next = PyIter_Next(iter);
4296 if (next == NULL) {
4297 if (PyErr_Occurred())
4298 ok = -1;
4299 break;
4300 }
4301 ok = PySequence_Contains(other, next);
4302 Py_DECREF(next);
4303 if (ok <= 0)
4304 break;
4305 }
4306 Py_DECREF(iter);
4307 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00004308}
4309
4310static PyObject *
4311dictview_richcompare(PyObject *self, PyObject *other, int op)
4312{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004313 Py_ssize_t len_self, len_other;
4314 int ok;
4315 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004317 assert(self != NULL);
4318 assert(PyDictViewSet_Check(self));
4319 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00004320
Brian Curtindfc80e32011-08-10 20:28:54 -05004321 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
4322 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004324 len_self = PyObject_Size(self);
4325 if (len_self < 0)
4326 return NULL;
4327 len_other = PyObject_Size(other);
4328 if (len_other < 0)
4329 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004331 ok = 0;
4332 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00004333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004334 case Py_NE:
4335 case Py_EQ:
4336 if (len_self == len_other)
4337 ok = all_contained_in(self, other);
4338 if (op == Py_NE && ok >= 0)
4339 ok = !ok;
4340 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004342 case Py_LT:
4343 if (len_self < len_other)
4344 ok = all_contained_in(self, other);
4345 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004347 case Py_LE:
4348 if (len_self <= len_other)
4349 ok = all_contained_in(self, other);
4350 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004352 case Py_GT:
4353 if (len_self > len_other)
4354 ok = all_contained_in(other, self);
4355 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004357 case Py_GE:
4358 if (len_self >= len_other)
4359 ok = all_contained_in(other, self);
4360 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004362 }
4363 if (ok < 0)
4364 return NULL;
4365 result = ok ? Py_True : Py_False;
4366 Py_INCREF(result);
4367 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00004368}
4369
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004370static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004371dictview_repr(_PyDictViewObject *dv)
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004372{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004373 PyObject *seq;
bennorthd7773d92018-01-26 15:46:01 +00004374 PyObject *result = NULL;
4375 Py_ssize_t rc;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004376
bennorthd7773d92018-01-26 15:46:01 +00004377 rc = Py_ReprEnter((PyObject *)dv);
4378 if (rc != 0) {
4379 return rc > 0 ? PyUnicode_FromString("...") : NULL;
4380 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004381 seq = PySequence_List((PyObject *)dv);
bennorthd7773d92018-01-26 15:46:01 +00004382 if (seq == NULL) {
4383 goto Done;
4384 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004385 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
4386 Py_DECREF(seq);
bennorthd7773d92018-01-26 15:46:01 +00004387
4388Done:
4389 Py_ReprLeave((PyObject *)dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004390 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004391}
4392
Guido van Rossum3ac67412007-02-10 18:55:06 +00004393/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004394
4395static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004396dictkeys_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004397{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004398 if (dv->dv_dict == NULL) {
4399 Py_RETURN_NONE;
4400 }
4401 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004402}
4403
4404static int
Eric Snow96c6af92015-05-29 22:21:39 -06004405dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004406{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004407 if (dv->dv_dict == NULL)
4408 return 0;
4409 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004410}
4411
Guido van Rossum83825ac2007-02-10 04:54:19 +00004412static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004413 (lenfunc)dictview_len, /* sq_length */
4414 0, /* sq_concat */
4415 0, /* sq_repeat */
4416 0, /* sq_item */
4417 0, /* sq_slice */
4418 0, /* sq_ass_item */
4419 0, /* sq_ass_slice */
4420 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004421};
4422
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004423// Create an set object from dictviews object.
4424// Returns a new reference.
4425// This utility function is used by set operations.
Guido van Rossum523259b2007-08-24 23:41:22 +00004426static PyObject*
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004427dictviews_to_set(PyObject *self)
Guido van Rossum523259b2007-08-24 23:41:22 +00004428{
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004429 PyObject *left = self;
4430 if (PyDictKeys_Check(self)) {
4431 // PySet_New() has fast path for the dict object.
4432 PyObject *dict = (PyObject *)((_PyDictViewObject *)self)->dv_dict;
4433 if (PyDict_CheckExact(dict)) {
4434 left = dict;
4435 }
4436 }
4437 return PySet_New(left);
4438}
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004439
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004440static PyObject*
4441dictviews_sub(PyObject *self, PyObject *other)
4442{
4443 PyObject *result = dictviews_to_set(self);
4444 if (result == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004445 return NULL;
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004446 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004447
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004448 _Py_IDENTIFIER(difference_update);
4449 PyObject *tmp = _PyObject_CallMethodIdOneArg(
4450 result, &PyId_difference_update, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004451 if (tmp == NULL) {
4452 Py_DECREF(result);
4453 return NULL;
4454 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004456 Py_DECREF(tmp);
4457 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004458}
4459
Forest Gregg998cf1f2019-08-26 02:17:43 -05004460static int
4461dictitems_contains(_PyDictViewObject *dv, PyObject *obj);
4462
4463PyObject *
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004464_PyDictView_Intersect(PyObject* self, PyObject *other)
Guido van Rossum523259b2007-08-24 23:41:22 +00004465{
Forest Gregg998cf1f2019-08-26 02:17:43 -05004466 PyObject *result;
4467 PyObject *it;
4468 PyObject *key;
4469 Py_ssize_t len_self;
4470 int rv;
4471 int (*dict_contains)(_PyDictViewObject *, PyObject *);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004472
Forest Gregg998cf1f2019-08-26 02:17:43 -05004473 /* Python interpreter swaps parameters when dict view
4474 is on right side of & */
4475 if (!PyDictViewSet_Check(self)) {
4476 PyObject *tmp = other;
4477 other = self;
4478 self = tmp;
4479 }
4480
4481 len_self = dictview_len((_PyDictViewObject *)self);
4482
4483 /* if other is a set and self is smaller than other,
4484 reuse set intersection logic */
Pablo Galindod439fb32021-02-20 18:03:08 +00004485 if (PySet_CheckExact(other) && len_self <= PyObject_Size(other)) {
Forest Gregg998cf1f2019-08-26 02:17:43 -05004486 _Py_IDENTIFIER(intersection);
4487 return _PyObject_CallMethodIdObjArgs(other, &PyId_intersection, self, NULL);
4488 }
4489
4490 /* if other is another dict view, and it is bigger than self,
4491 swap them */
4492 if (PyDictViewSet_Check(other)) {
4493 Py_ssize_t len_other = dictview_len((_PyDictViewObject *)other);
4494 if (len_other > len_self) {
4495 PyObject *tmp = other;
4496 other = self;
4497 self = tmp;
4498 }
4499 }
4500
4501 /* at this point, two things should be true
4502 1. self is a dictview
4503 2. if other is a dictview then it is smaller than self */
4504 result = PySet_New(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004505 if (result == NULL)
4506 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004507
Forest Gregg998cf1f2019-08-26 02:17:43 -05004508 it = PyObject_GetIter(other);
Zackery Spytzb16e3822019-10-13 05:49:05 -06004509 if (it == NULL) {
4510 Py_DECREF(result);
4511 return NULL;
4512 }
Forest Gregg998cf1f2019-08-26 02:17:43 -05004513
Forest Gregg998cf1f2019-08-26 02:17:43 -05004514 if (PyDictKeys_Check(self)) {
4515 dict_contains = dictkeys_contains;
4516 }
4517 /* else PyDictItems_Check(self) */
4518 else {
4519 dict_contains = dictitems_contains;
4520 }
4521
4522 while ((key = PyIter_Next(it)) != NULL) {
4523 rv = dict_contains((_PyDictViewObject *)self, key);
4524 if (rv < 0) {
4525 goto error;
4526 }
4527 if (rv) {
4528 if (PySet_Add(result, key)) {
4529 goto error;
4530 }
4531 }
4532 Py_DECREF(key);
4533 }
4534 Py_DECREF(it);
4535 if (PyErr_Occurred()) {
4536 Py_DECREF(result);
4537 return NULL;
4538 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004539 return result;
Forest Gregg998cf1f2019-08-26 02:17:43 -05004540
4541error:
4542 Py_DECREF(it);
4543 Py_DECREF(result);
4544 Py_DECREF(key);
4545 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004546}
4547
4548static PyObject*
4549dictviews_or(PyObject* self, PyObject *other)
4550{
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004551 PyObject *result = dictviews_to_set(self);
4552 if (result == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004553 return NULL;
4554 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004555
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004556 if (_PySet_Update(result, other) < 0) {
4557 Py_DECREF(result);
4558 return NULL;
4559 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004560 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004561}
4562
Dennis Sweeney07d81122020-06-10 01:56:56 -04004563static PyObject *
4564dictitems_xor(PyObject *self, PyObject *other)
4565{
4566 assert(PyDictItems_Check(self));
4567 assert(PyDictItems_Check(other));
4568 PyObject *d1 = (PyObject *)((_PyDictViewObject *)self)->dv_dict;
4569 PyObject *d2 = (PyObject *)((_PyDictViewObject *)other)->dv_dict;
4570
4571 PyObject *temp_dict = PyDict_Copy(d1);
4572 if (temp_dict == NULL) {
4573 return NULL;
4574 }
4575 PyObject *result_set = PySet_New(NULL);
4576 if (result_set == NULL) {
4577 Py_CLEAR(temp_dict);
4578 return NULL;
4579 }
4580
4581 PyObject *key = NULL, *val1 = NULL, *val2 = NULL;
4582 Py_ssize_t pos = 0;
4583 Py_hash_t hash;
4584
4585 while (_PyDict_Next(d2, &pos, &key, &val2, &hash)) {
4586 Py_INCREF(key);
4587 Py_INCREF(val2);
4588 val1 = _PyDict_GetItem_KnownHash(temp_dict, key, hash);
4589
4590 int to_delete;
4591 if (val1 == NULL) {
4592 if (PyErr_Occurred()) {
4593 goto error;
4594 }
4595 to_delete = 0;
4596 }
4597 else {
4598 Py_INCREF(val1);
4599 to_delete = PyObject_RichCompareBool(val1, val2, Py_EQ);
4600 if (to_delete < 0) {
4601 goto error;
4602 }
4603 }
4604
4605 if (to_delete) {
4606 if (_PyDict_DelItem_KnownHash(temp_dict, key, hash) < 0) {
4607 goto error;
4608 }
4609 }
4610 else {
4611 PyObject *pair = PyTuple_Pack(2, key, val2);
4612 if (pair == NULL) {
4613 goto error;
4614 }
4615 if (PySet_Add(result_set, pair) < 0) {
4616 Py_DECREF(pair);
4617 goto error;
4618 }
4619 Py_DECREF(pair);
4620 }
4621 Py_DECREF(key);
4622 Py_XDECREF(val1);
4623 Py_DECREF(val2);
4624 }
4625 key = val1 = val2 = NULL;
4626
4627 _Py_IDENTIFIER(items);
4628 PyObject *remaining_pairs = _PyObject_CallMethodIdNoArgs(temp_dict,
4629 &PyId_items);
4630 if (remaining_pairs == NULL) {
4631 goto error;
4632 }
4633 if (_PySet_Update(result_set, remaining_pairs) < 0) {
4634 Py_DECREF(remaining_pairs);
4635 goto error;
4636 }
4637 Py_DECREF(temp_dict);
4638 Py_DECREF(remaining_pairs);
4639 return result_set;
4640
4641error:
4642 Py_XDECREF(temp_dict);
4643 Py_XDECREF(result_set);
4644 Py_XDECREF(key);
4645 Py_XDECREF(val1);
4646 Py_XDECREF(val2);
4647 return NULL;
4648}
4649
Guido van Rossum523259b2007-08-24 23:41:22 +00004650static PyObject*
4651dictviews_xor(PyObject* self, PyObject *other)
4652{
Dennis Sweeney07d81122020-06-10 01:56:56 -04004653 if (PyDictItems_Check(self) && PyDictItems_Check(other)) {
4654 return dictitems_xor(self, other);
4655 }
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004656 PyObject *result = dictviews_to_set(self);
4657 if (result == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004658 return NULL;
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004659 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004660
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004661 _Py_IDENTIFIER(symmetric_difference_update);
4662 PyObject *tmp = _PyObject_CallMethodIdOneArg(
4663 result, &PyId_symmetric_difference_update, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004664 if (tmp == NULL) {
4665 Py_DECREF(result);
4666 return NULL;
4667 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004669 Py_DECREF(tmp);
4670 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004671}
4672
4673static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004674 0, /*nb_add*/
4675 (binaryfunc)dictviews_sub, /*nb_subtract*/
4676 0, /*nb_multiply*/
4677 0, /*nb_remainder*/
4678 0, /*nb_divmod*/
4679 0, /*nb_power*/
4680 0, /*nb_negative*/
4681 0, /*nb_positive*/
4682 0, /*nb_absolute*/
4683 0, /*nb_bool*/
4684 0, /*nb_invert*/
4685 0, /*nb_lshift*/
4686 0, /*nb_rshift*/
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004687 (binaryfunc)_PyDictView_Intersect, /*nb_and*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004688 (binaryfunc)dictviews_xor, /*nb_xor*/
4689 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00004690};
4691
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004692static PyObject*
4693dictviews_isdisjoint(PyObject *self, PyObject *other)
4694{
4695 PyObject *it;
4696 PyObject *item = NULL;
4697
4698 if (self == other) {
Eric Snow96c6af92015-05-29 22:21:39 -06004699 if (dictview_len((_PyDictViewObject *)self) == 0)
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004700 Py_RETURN_TRUE;
4701 else
4702 Py_RETURN_FALSE;
4703 }
4704
4705 /* Iterate over the shorter object (only if other is a set,
4706 * because PySequence_Contains may be expensive otherwise): */
4707 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
Eric Snow96c6af92015-05-29 22:21:39 -06004708 Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self);
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004709 Py_ssize_t len_other = PyObject_Size(other);
4710 if (len_other == -1)
4711 return NULL;
4712
4713 if ((len_other > len_self)) {
4714 PyObject *tmp = other;
4715 other = self;
4716 self = tmp;
4717 }
4718 }
4719
4720 it = PyObject_GetIter(other);
4721 if (it == NULL)
4722 return NULL;
4723
4724 while ((item = PyIter_Next(it)) != NULL) {
4725 int contains = PySequence_Contains(self, item);
4726 Py_DECREF(item);
4727 if (contains == -1) {
4728 Py_DECREF(it);
4729 return NULL;
4730 }
4731
4732 if (contains) {
4733 Py_DECREF(it);
4734 Py_RETURN_FALSE;
4735 }
4736 }
4737 Py_DECREF(it);
4738 if (PyErr_Occurred())
4739 return NULL; /* PyIter_Next raised an exception. */
4740 Py_RETURN_TRUE;
4741}
4742
4743PyDoc_STRVAR(isdisjoint_doc,
4744"Return True if the view and the given iterable have a null intersection.");
4745
Serhiy Storchaka81524022018-11-27 13:05:02 +02004746static PyObject* dictkeys_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored));
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004747
4748PyDoc_STRVAR(reversed_keys_doc,
4749"Return a reverse iterator over the dict keys.");
4750
Guido van Rossumb90c8482007-02-10 01:11:45 +00004751static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004752 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4753 isdisjoint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004754 {"__reversed__", (PyCFunction)(void(*)(void))dictkeys_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004755 reversed_keys_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004756 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004757};
4758
4759PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004760 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4761 "dict_keys", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004762 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004763 0, /* tp_itemsize */
4764 /* methods */
4765 (destructor)dictview_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004766 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004767 0, /* tp_getattr */
4768 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004769 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004770 (reprfunc)dictview_repr, /* tp_repr */
4771 &dictviews_as_number, /* tp_as_number */
4772 &dictkeys_as_sequence, /* tp_as_sequence */
4773 0, /* tp_as_mapping */
4774 0, /* tp_hash */
4775 0, /* tp_call */
4776 0, /* tp_str */
4777 PyObject_GenericGetAttr, /* tp_getattro */
4778 0, /* tp_setattro */
4779 0, /* tp_as_buffer */
4780 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4781 0, /* tp_doc */
4782 (traverseproc)dictview_traverse, /* tp_traverse */
4783 0, /* tp_clear */
4784 dictview_richcompare, /* tp_richcompare */
4785 0, /* tp_weaklistoffset */
4786 (getiterfunc)dictkeys_iter, /* tp_iter */
4787 0, /* tp_iternext */
4788 dictkeys_methods, /* tp_methods */
Dennis Sweeney3ee0e482020-06-12 13:19:25 -04004789 .tp_getset = dictview_getset,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004790};
4791
4792static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304793dictkeys_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004794{
Eric Snow96c6af92015-05-29 22:21:39 -06004795 return _PyDictView_New(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004796}
4797
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004798static PyObject *
Serhiy Storchaka81524022018-11-27 13:05:02 +02004799dictkeys_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored))
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004800{
4801 if (dv->dv_dict == NULL) {
4802 Py_RETURN_NONE;
4803 }
4804 return dictiter_new(dv->dv_dict, &PyDictRevIterKey_Type);
4805}
4806
Guido van Rossum3ac67412007-02-10 18:55:06 +00004807/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004808
4809static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004810dictitems_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004811{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004812 if (dv->dv_dict == NULL) {
4813 Py_RETURN_NONE;
4814 }
4815 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004816}
4817
4818static int
Eric Snow96c6af92015-05-29 22:21:39 -06004819dictitems_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004820{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004821 int result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004822 PyObject *key, *value, *found;
4823 if (dv->dv_dict == NULL)
4824 return 0;
4825 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
4826 return 0;
4827 key = PyTuple_GET_ITEM(obj, 0);
4828 value = PyTuple_GET_ITEM(obj, 1);
Raymond Hettinger6692f012016-09-18 21:46:08 -07004829 found = PyDict_GetItemWithError((PyObject *)dv->dv_dict, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004830 if (found == NULL) {
4831 if (PyErr_Occurred())
4832 return -1;
4833 return 0;
4834 }
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004835 Py_INCREF(found);
Serhiy Storchaka18b711c2019-08-04 14:12:48 +03004836 result = PyObject_RichCompareBool(found, value, Py_EQ);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004837 Py_DECREF(found);
4838 return result;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004839}
4840
Guido van Rossum83825ac2007-02-10 04:54:19 +00004841static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004842 (lenfunc)dictview_len, /* sq_length */
4843 0, /* sq_concat */
4844 0, /* sq_repeat */
4845 0, /* sq_item */
4846 0, /* sq_slice */
4847 0, /* sq_ass_item */
4848 0, /* sq_ass_slice */
4849 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004850};
4851
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004852static PyObject* dictitems_reversed(_PyDictViewObject *dv);
4853
4854PyDoc_STRVAR(reversed_items_doc,
4855"Return a reverse iterator over the dict items.");
4856
Guido van Rossumb90c8482007-02-10 01:11:45 +00004857static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004858 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4859 isdisjoint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004860 {"__reversed__", (PyCFunction)(void(*)(void))dictitems_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004861 reversed_items_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004862 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004863};
4864
4865PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004866 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4867 "dict_items", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004868 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004869 0, /* tp_itemsize */
4870 /* methods */
4871 (destructor)dictview_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004872 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004873 0, /* tp_getattr */
4874 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004875 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004876 (reprfunc)dictview_repr, /* tp_repr */
4877 &dictviews_as_number, /* tp_as_number */
4878 &dictitems_as_sequence, /* tp_as_sequence */
4879 0, /* tp_as_mapping */
4880 0, /* tp_hash */
4881 0, /* tp_call */
4882 0, /* tp_str */
4883 PyObject_GenericGetAttr, /* tp_getattro */
4884 0, /* tp_setattro */
4885 0, /* tp_as_buffer */
4886 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4887 0, /* tp_doc */
4888 (traverseproc)dictview_traverse, /* tp_traverse */
4889 0, /* tp_clear */
4890 dictview_richcompare, /* tp_richcompare */
4891 0, /* tp_weaklistoffset */
4892 (getiterfunc)dictitems_iter, /* tp_iter */
4893 0, /* tp_iternext */
4894 dictitems_methods, /* tp_methods */
Dennis Sweeney3ee0e482020-06-12 13:19:25 -04004895 .tp_getset = dictview_getset,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004896};
4897
4898static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304899dictitems_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004900{
Eric Snow96c6af92015-05-29 22:21:39 -06004901 return _PyDictView_New(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004902}
4903
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004904static PyObject *
4905dictitems_reversed(_PyDictViewObject *dv)
4906{
4907 if (dv->dv_dict == NULL) {
4908 Py_RETURN_NONE;
4909 }
4910 return dictiter_new(dv->dv_dict, &PyDictRevIterItem_Type);
4911}
4912
Guido van Rossum3ac67412007-02-10 18:55:06 +00004913/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004914
4915static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004916dictvalues_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004917{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004918 if (dv->dv_dict == NULL) {
4919 Py_RETURN_NONE;
4920 }
4921 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004922}
4923
Guido van Rossum83825ac2007-02-10 04:54:19 +00004924static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004925 (lenfunc)dictview_len, /* sq_length */
4926 0, /* sq_concat */
4927 0, /* sq_repeat */
4928 0, /* sq_item */
4929 0, /* sq_slice */
4930 0, /* sq_ass_item */
4931 0, /* sq_ass_slice */
4932 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004933};
4934
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004935static PyObject* dictvalues_reversed(_PyDictViewObject *dv);
4936
4937PyDoc_STRVAR(reversed_values_doc,
4938"Return a reverse iterator over the dict values.");
4939
Guido van Rossumb90c8482007-02-10 01:11:45 +00004940static PyMethodDef dictvalues_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004941 {"__reversed__", (PyCFunction)(void(*)(void))dictvalues_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004942 reversed_values_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004943 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004944};
4945
4946PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004947 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4948 "dict_values", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004949 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004950 0, /* tp_itemsize */
4951 /* methods */
4952 (destructor)dictview_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004953 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004954 0, /* tp_getattr */
4955 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004956 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004957 (reprfunc)dictview_repr, /* tp_repr */
4958 0, /* tp_as_number */
4959 &dictvalues_as_sequence, /* tp_as_sequence */
4960 0, /* tp_as_mapping */
4961 0, /* tp_hash */
4962 0, /* tp_call */
4963 0, /* tp_str */
4964 PyObject_GenericGetAttr, /* tp_getattro */
4965 0, /* tp_setattro */
4966 0, /* tp_as_buffer */
4967 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4968 0, /* tp_doc */
4969 (traverseproc)dictview_traverse, /* tp_traverse */
4970 0, /* tp_clear */
4971 0, /* tp_richcompare */
4972 0, /* tp_weaklistoffset */
4973 (getiterfunc)dictvalues_iter, /* tp_iter */
4974 0, /* tp_iternext */
4975 dictvalues_methods, /* tp_methods */
Dennis Sweeney3ee0e482020-06-12 13:19:25 -04004976 .tp_getset = dictview_getset,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004977};
4978
4979static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304980dictvalues_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004981{
Eric Snow96c6af92015-05-29 22:21:39 -06004982 return _PyDictView_New(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004983}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004984
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004985static PyObject *
4986dictvalues_reversed(_PyDictViewObject *dv)
4987{
4988 if (dv->dv_dict == NULL) {
4989 Py_RETURN_NONE;
4990 }
4991 return dictiter_new(dv->dv_dict, &PyDictRevIterValue_Type);
4992}
4993
4994
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004995/* Returns NULL if cannot allocate a new PyDictKeysObject,
4996 but does not set an error */
4997PyDictKeysObject *
4998_PyDict_NewKeysForClass(void)
4999{
Victor Stinner742da042016-09-07 17:40:12 -07005000 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04005001 if (keys == NULL)
5002 PyErr_Clear();
5003 else
5004 keys->dk_lookup = lookdict_split;
5005 return keys;
5006}
5007
5008#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
5009
5010PyObject *
5011PyObject_GenericGetDict(PyObject *obj, void *context)
5012{
5013 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
5014 if (dictptr == NULL) {
5015 PyErr_SetString(PyExc_AttributeError,
5016 "This object has no __dict__");
5017 return NULL;
5018 }
5019 dict = *dictptr;
5020 if (dict == NULL) {
5021 PyTypeObject *tp = Py_TYPE(obj);
5022 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
INADA Naokia7576492018-11-14 18:39:27 +09005023 dictkeys_incref(CACHED_KEYS(tp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04005024 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
5025 }
5026 else {
5027 *dictptr = dict = PyDict_New();
5028 }
5029 }
5030 Py_XINCREF(dict);
5031 return dict;
5032}
5033
5034int
5035_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
Victor Stinner742da042016-09-07 17:40:12 -07005036 PyObject *key, PyObject *value)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04005037{
5038 PyObject *dict;
5039 int res;
5040 PyDictKeysObject *cached;
5041
5042 assert(dictptr != NULL);
5043 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
5044 assert(dictptr != NULL);
5045 dict = *dictptr;
5046 if (dict == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +09005047 dictkeys_incref(cached);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04005048 dict = new_dict_with_shared_keys(cached);
5049 if (dict == NULL)
5050 return -1;
5051 *dictptr = dict;
5052 }
5053 if (value == NULL) {
5054 res = PyDict_DelItem(dict, key);
INADA Naoki2294f3a2017-02-12 13:51:30 +09005055 // Since key sharing dict doesn't allow deletion, PyDict_DelItem()
5056 // always converts dict to combined form.
5057 if ((cached = CACHED_KEYS(tp)) != NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04005058 CACHED_KEYS(tp) = NULL;
INADA Naokia7576492018-11-14 18:39:27 +09005059 dictkeys_decref(cached);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04005060 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01005061 }
5062 else {
INADA Naoki2294f3a2017-02-12 13:51:30 +09005063 int was_shared = (cached == ((PyDictObject *)dict)->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04005064 res = PyDict_SetItem(dict, key, value);
INADA Naoki2294f3a2017-02-12 13:51:30 +09005065 if (was_shared &&
5066 (cached = CACHED_KEYS(tp)) != NULL &&
5067 cached != ((PyDictObject *)dict)->ma_keys) {
Victor Stinner3d3f2642016-12-15 17:21:23 +01005068 /* PyDict_SetItem() may call dictresize and convert split table
5069 * into combined table. In such case, convert it to split
5070 * table again and update type's shared key only when this is
5071 * the only dict sharing key with the type.
5072 *
5073 * This is to allow using shared key in class like this:
5074 *
5075 * class C:
5076 * def __init__(self):
5077 * # one dict resize happens
5078 * self.a, self.b, self.c = 1, 2, 3
5079 * self.d, self.e, self.f = 4, 5, 6
5080 * a = C()
5081 */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04005082 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04005083 CACHED_KEYS(tp) = make_keys_shared(dict);
Victor Stinner742da042016-09-07 17:40:12 -07005084 }
5085 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04005086 CACHED_KEYS(tp) = NULL;
5087 }
INADA Naokia7576492018-11-14 18:39:27 +09005088 dictkeys_decref(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04005089 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
5090 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04005091 }
5092 }
5093 } else {
5094 dict = *dictptr;
5095 if (dict == NULL) {
5096 dict = PyDict_New();
5097 if (dict == NULL)
5098 return -1;
5099 *dictptr = dict;
5100 }
5101 if (value == NULL) {
5102 res = PyDict_DelItem(dict, key);
5103 } else {
5104 res = PyDict_SetItem(dict, key, value);
5105 }
5106 }
5107 return res;
5108}
5109
5110void
5111_PyDictKeys_DecRef(PyDictKeysObject *keys)
5112{
INADA Naokia7576492018-11-14 18:39:27 +09005113 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04005114}