blob: 60660adf897c35982b6fe40f3d6fcbf89d3f7718 [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"
Victor Stinnerbcda8f12018-11-21 22:27:47 +0100114#include "pycore_object.h"
Victor Stinner621cebe2018-11-12 16:53:38 +0100115#include "pycore_pystate.h"
Eric Snow96c6af92015-05-29 22:21:39 -0600116#include "dict-common.h"
Victor Stinner990397e2016-09-09 20:22:59 -0700117#include "stringlib/eq.h" /* to get unicode_eq() */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000118
Larry Hastings61272b72014-01-07 12:41:53 -0800119/*[clinic input]
Larry Hastingsc2047262014-01-25 20:43:29 -0800120class dict "PyDictObject *" "&PyDict_Type"
Larry Hastings61272b72014-01-07 12:41:53 -0800121[clinic start generated code]*/
Larry Hastings581ee362014-01-28 05:00:08 -0800122/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f157a5a0ce9589d6]*/
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800123
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400124
125/*
126To ensure the lookup algorithm terminates, there must be at least one Unused
127slot (NULL key) in the table.
128To avoid slowing down lookups on a near-full table, we resize the table when
129it's USABLE_FRACTION (currently two-thirds) full.
130*/
Guido van Rossum16e93a81997-01-28 00:00:11 +0000131
Tim Peterseb28ef22001-06-02 05:27:19 +0000132#define PERTURB_SHIFT 5
133
Guido van Rossum16e93a81997-01-28 00:00:11 +0000134/*
Tim Peterseb28ef22001-06-02 05:27:19 +0000135Major subtleties ahead: Most hash schemes depend on having a "good" hash
136function, in the sense of simulating randomness. Python doesn't: its most
R David Murray537ad7a2016-07-10 12:33:18 -0400137important hash functions (for ints) are very regular in common
Tim Peterseb28ef22001-06-02 05:27:19 +0000138cases:
Tim Peters15d49292001-05-27 07:39:22 +0000139
R David Murray537ad7a2016-07-10 12:33:18 -0400140 >>>[hash(i) for i in range(4)]
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000141 [0, 1, 2, 3]
Tim Peters15d49292001-05-27 07:39:22 +0000142
Tim Peterseb28ef22001-06-02 05:27:19 +0000143This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
144the low-order i bits as the initial table index is extremely fast, and there
R David Murray537ad7a2016-07-10 12:33:18 -0400145are no collisions at all for dicts indexed by a contiguous range of ints. So
146this gives better-than-random behavior in common cases, and that's very
147desirable.
Tim Peters15d49292001-05-27 07:39:22 +0000148
Tim Peterseb28ef22001-06-02 05:27:19 +0000149OTOH, when collisions occur, the tendency to fill contiguous slices of the
150hash table makes a good collision resolution strategy crucial. Taking only
151the last i bits of the hash code is also vulnerable: for example, consider
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152the list [i << 16 for i in range(20000)] as a set of keys. Since ints are
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000153their own hash codes, and this fits in a dict of size 2**15, the last 15 bits
154 of every hash code are all 0: they *all* map to the same table index.
Tim Peters15d49292001-05-27 07:39:22 +0000155
Tim Peterseb28ef22001-06-02 05:27:19 +0000156But catering to unusual cases should not slow the usual ones, so we just take
157the last i bits anyway. It's up to collision resolution to do the rest. If
158we *usually* find the key we're looking for on the first try (and, it turns
159out, we usually do -- the table load factor is kept under 2/3, so the odds
160are solidly in our favor), then it makes best sense to keep the initial index
161computation dirt cheap.
Tim Peters15d49292001-05-27 07:39:22 +0000162
Tim Peterseb28ef22001-06-02 05:27:19 +0000163The first half of collision resolution is to visit table indices via this
164recurrence:
Tim Peters15d49292001-05-27 07:39:22 +0000165
Tim Peterseb28ef22001-06-02 05:27:19 +0000166 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +0000167
Tim Peterseb28ef22001-06-02 05:27:19 +0000168For any initial j in range(2**i), repeating that 2**i times generates each
169int in range(2**i) exactly once (see any text on random-number generation for
170proof). By itself, this doesn't help much: like linear probing (setting
171j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
172order. This would be bad, except that's not the only thing we do, and it's
173actually *good* in the common cases where hash keys are consecutive. In an
174example that's really too small to make this entirely clear, for a table of
175size 2**3 the order of indices is:
Tim Peters15d49292001-05-27 07:39:22 +0000176
Tim Peterseb28ef22001-06-02 05:27:19 +0000177 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
178
179If two things come in at index 5, the first place we look after is index 2,
180not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
181Linear probing is deadly in this case because there the fixed probe order
182is the *same* as the order consecutive keys are likely to arrive. But it's
183extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
184and certain that consecutive hash codes do not.
185
186The other half of the strategy is to get the other bits of the hash code
187into play. This is done by initializing a (unsigned) vrbl "perturb" to the
188full hash code, and changing the recurrence to:
189
Tim Peterseb28ef22001-06-02 05:27:19 +0000190 perturb >>= PERTURB_SHIFT;
INADA Naoki267941c2016-10-06 15:19:07 +0900191 j = (5*j) + 1 + perturb;
Tim Peterseb28ef22001-06-02 05:27:19 +0000192 use j % 2**i as the next table index;
193
194Now the probe sequence depends (eventually) on every bit in the hash code,
195and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
196because it quickly magnifies small differences in the bits that didn't affect
197the initial index. Note that because perturb is unsigned, if the recurrence
198is executed often enough perturb eventually becomes and remains 0. At that
199point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
200that's certain to find an empty slot eventually (since it generates every int
201in range(2**i), and we make sure there's always at least one empty slot).
202
203Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
204small so that the high bits of the hash code continue to affect the probe
205sequence across iterations; but you want it large so that in really bad cases
206the high-order hash bits have an effect on early iterations. 5 was "the
207best" in minimizing total collisions across experiments Tim Peters ran (on
208both normal and pathological cases), but 4 and 6 weren't significantly worse.
209
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000210Historical: Reimer Behrends contributed the idea of using a polynomial-based
Tim Peterseb28ef22001-06-02 05:27:19 +0000211approach, using repeated multiplication by x in GF(2**n) where an irreducible
212polynomial for each table size was chosen such that x was a primitive root.
213Christian Tismer later extended that to use division by x instead, as an
214efficient way to get the high bits of the hash code into play. This scheme
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000215also gave excellent collision statistics, but was more expensive: two
216if-tests were required inside the loop; computing "the next" index took about
217the same number of operations but without as much potential parallelism
218(e.g., computing 5*j can go on at the same time as computing 1+perturb in the
219above, and then shifting perturb can be done while the table index is being
220masked); and the PyDictObject struct required a member to hold the table's
221polynomial. In Tim's experiments the current scheme ran faster, produced
222equally good collision statistics, needed less code & used less memory.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000223
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000224*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000225
Fred Drake1bff34a2000-08-31 19:31:38 +0000226/* forward declarations */
Victor Stinner742da042016-09-07 17:40:12 -0700227static Py_ssize_t lookdict(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900228 Py_hash_t hash, PyObject **value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700229static Py_ssize_t lookdict_unicode(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900230 Py_hash_t hash, PyObject **value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700231static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400232lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900233 Py_hash_t hash, PyObject **value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700234static Py_ssize_t lookdict_split(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900235 Py_hash_t hash, PyObject **value_addr);
Fred Drake1bff34a2000-08-31 19:31:38 +0000236
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400237static int dictresize(PyDictObject *mp, Py_ssize_t minused);
Tim Petersdea48ec2001-05-22 20:40:22 +0000238
INADA Naoki2aaf98c2018-09-26 12:59:00 +0900239static PyObject* dict_iter(PyDictObject *dict);
240
Benjamin Peterson3c569292016-09-08 13:16:41 -0700241/*Global counter used to set ma_version_tag field of dictionary.
Victor Stinner3b6a6b42016-09-08 12:51:24 -0700242 * It is incremented each time that a dictionary is created and each
243 * time that a dictionary is modified. */
244static uint64_t pydict_global_version = 0;
245
246#define DICT_NEXT_VERSION() (++pydict_global_version)
247
Victor Stinner742da042016-09-07 17:40:12 -0700248/* Dictionary reuse scheme to save calls to malloc and free */
Christian Heimes2202f872008-02-06 14:31:34 +0000249#ifndef PyDict_MAXFREELIST
250#define PyDict_MAXFREELIST 80
251#endif
252static PyDictObject *free_list[PyDict_MAXFREELIST];
253static int numfree = 0;
Victor Stinner742da042016-09-07 17:40:12 -0700254static PyDictKeysObject *keys_free_list[PyDict_MAXFREELIST];
255static int numfreekeys = 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000256
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300257#include "clinic/dictobject.c.h"
258
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100259int
260PyDict_ClearFreeList(void)
Christian Heimes77c02eb2008-02-09 02:18:51 +0000261{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 PyDictObject *op;
Victor Stinner742da042016-09-07 17:40:12 -0700263 int ret = numfree + numfreekeys;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 while (numfree) {
265 op = free_list[--numfree];
266 assert(PyDict_CheckExact(op));
267 PyObject_GC_Del(op);
268 }
Victor Stinner742da042016-09-07 17:40:12 -0700269 while (numfreekeys) {
270 PyObject_FREE(keys_free_list[--numfreekeys]);
271 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100272 return ret;
273}
274
David Malcolm49526f42012-06-22 14:55:41 -0400275/* Print summary info about the state of the optimized allocator */
276void
277_PyDict_DebugMallocStats(FILE *out)
278{
279 _PyDebugAllocatorStats(out,
280 "free PyDictObject", numfree, sizeof(PyDictObject));
281}
282
283
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100284void
Victor Stinnerbed48172019-08-27 00:12:32 +0200285_PyDict_Fini(void)
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100286{
287 PyDict_ClearFreeList();
Christian Heimes77c02eb2008-02-09 02:18:51 +0000288}
289
Victor Stinner742da042016-09-07 17:40:12 -0700290#define DK_SIZE(dk) ((dk)->dk_size)
291#if SIZEOF_VOID_P > 4
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700292#define DK_IXSIZE(dk) \
293 (DK_SIZE(dk) <= 0xff ? \
294 1 : DK_SIZE(dk) <= 0xffff ? \
295 2 : DK_SIZE(dk) <= 0xffffffff ? \
Benjamin Peterson3c569292016-09-08 13:16:41 -0700296 4 : sizeof(int64_t))
Victor Stinner742da042016-09-07 17:40:12 -0700297#else
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700298#define DK_IXSIZE(dk) \
299 (DK_SIZE(dk) <= 0xff ? \
300 1 : DK_SIZE(dk) <= 0xffff ? \
Benjamin Peterson3c569292016-09-08 13:16:41 -0700301 2 : sizeof(int32_t))
Victor Stinner742da042016-09-07 17:40:12 -0700302#endif
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700303#define DK_ENTRIES(dk) \
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700304 ((PyDictKeyEntry*)(&((int8_t*)((dk)->dk_indices))[DK_SIZE(dk) * DK_IXSIZE(dk)]))
Victor Stinner742da042016-09-07 17:40:12 -0700305
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400306#define DK_MASK(dk) (((dk)->dk_size)-1)
307#define IS_POWER_OF_2(x) (((x) & (x-1)) == 0)
308
INADA Naokia7576492018-11-14 18:39:27 +0900309static void free_keys_object(PyDictKeysObject *keys);
310
311static inline void
312dictkeys_incref(PyDictKeysObject *dk)
313{
Victor Stinner49932fe2020-02-03 17:55:05 +0100314#ifdef Py_REF_DEBUG
315 _Py_RefTotal++;
316#endif
INADA Naokia7576492018-11-14 18:39:27 +0900317 dk->dk_refcnt++;
318}
319
320static inline void
321dictkeys_decref(PyDictKeysObject *dk)
322{
323 assert(dk->dk_refcnt > 0);
Victor Stinner49932fe2020-02-03 17:55:05 +0100324#ifdef Py_REF_DEBUG
325 _Py_RefTotal--;
326#endif
INADA Naokia7576492018-11-14 18:39:27 +0900327 if (--dk->dk_refcnt == 0) {
328 free_keys_object(dk);
329 }
330}
331
Victor Stinner742da042016-09-07 17:40:12 -0700332/* lookup indices. returns DKIX_EMPTY, DKIX_DUMMY, or ix >=0 */
Benjamin Peterson73222252016-09-08 09:58:47 -0700333static inline Py_ssize_t
Andy Lester62d21c92020-03-25 23:13:01 -0500334dictkeys_get_index(const PyDictKeysObject *keys, Py_ssize_t i)
Victor Stinner742da042016-09-07 17:40:12 -0700335{
336 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700337 Py_ssize_t ix;
338
Victor Stinner742da042016-09-07 17:40:12 -0700339 if (s <= 0xff) {
Andy Lester62d21c92020-03-25 23:13:01 -0500340 const int8_t *indices = (const int8_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700341 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700342 }
343 else if (s <= 0xffff) {
Andy Lester62d21c92020-03-25 23:13:01 -0500344 const int16_t *indices = (const int16_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700345 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700346 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700347#if SIZEOF_VOID_P > 4
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300348 else if (s > 0xffffffff) {
Andy Lester62d21c92020-03-25 23:13:01 -0500349 const int64_t *indices = (const int64_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700350 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700351 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700352#endif
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300353 else {
Andy Lester62d21c92020-03-25 23:13:01 -0500354 const int32_t *indices = (const int32_t*)(keys->dk_indices);
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300355 ix = indices[i];
356 }
Victor Stinner71211e32016-09-08 10:52:46 -0700357 assert(ix >= DKIX_DUMMY);
358 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700359}
360
361/* write to indices. */
Benjamin Peterson73222252016-09-08 09:58:47 -0700362static inline void
INADA Naokia7576492018-11-14 18:39:27 +0900363dictkeys_set_index(PyDictKeysObject *keys, Py_ssize_t i, Py_ssize_t ix)
Victor Stinner742da042016-09-07 17:40:12 -0700364{
365 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700366
367 assert(ix >= DKIX_DUMMY);
368
Victor Stinner742da042016-09-07 17:40:12 -0700369 if (s <= 0xff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700370 int8_t *indices = (int8_t*)(keys->dk_indices);
Victor Stinner71211e32016-09-08 10:52:46 -0700371 assert(ix <= 0x7f);
Victor Stinner208857e2016-09-08 11:35:46 -0700372 indices[i] = (char)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700373 }
374 else if (s <= 0xffff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700375 int16_t *indices = (int16_t*)(keys->dk_indices);
Victor Stinner71211e32016-09-08 10:52:46 -0700376 assert(ix <= 0x7fff);
Victor Stinner208857e2016-09-08 11:35:46 -0700377 indices[i] = (int16_t)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700378 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700379#if SIZEOF_VOID_P > 4
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300380 else if (s > 0xffffffff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700381 int64_t *indices = (int64_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700382 indices[i] = ix;
Victor Stinner742da042016-09-07 17:40:12 -0700383 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700384#endif
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300385 else {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700386 int32_t *indices = (int32_t*)(keys->dk_indices);
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300387 assert(ix <= 0x7fffffff);
388 indices[i] = (int32_t)ix;
389 }
Victor Stinner742da042016-09-07 17:40:12 -0700390}
391
392
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200393/* USABLE_FRACTION is the maximum dictionary load.
Victor Stinner742da042016-09-07 17:40:12 -0700394 * Increasing this ratio makes dictionaries more dense resulting in more
395 * collisions. Decreasing it improves sparseness at the expense of spreading
396 * indices over more cache lines and at the cost of total memory consumed.
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200397 *
398 * USABLE_FRACTION must obey the following:
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400399 * (0 < USABLE_FRACTION(n) < n) for all n >= 2
400 *
Victor Stinner742da042016-09-07 17:40:12 -0700401 * USABLE_FRACTION should be quick to calculate.
402 * Fractions around 1/2 to 2/3 seem to work well in practice.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400403 */
Victor Stinner742da042016-09-07 17:40:12 -0700404#define USABLE_FRACTION(n) (((n) << 1)/3)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400405
Victor Stinner742da042016-09-07 17:40:12 -0700406/* ESTIMATE_SIZE is reverse function of USABLE_FRACTION.
407 * This can be used to reserve enough size to insert n entries without
408 * resizing.
409 */
INADA Naoki92c50ee2016-11-22 00:57:02 +0900410#define ESTIMATE_SIZE(n) (((n)*3+1) >> 1)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400411
Victor Stinner742da042016-09-07 17:40:12 -0700412/* Alternative fraction that is otherwise close enough to 2n/3 to make
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400413 * little difference. 8 * 2/3 == 8 * 5/8 == 5. 16 * 2/3 == 16 * 5/8 == 10.
414 * 32 * 2/3 = 21, 32 * 5/8 = 20.
415 * Its advantage is that it is faster to compute on machines with slow division.
416 * #define USABLE_FRACTION(n) (((n) >> 1) + ((n) >> 2) - ((n) >> 3))
Victor Stinner742da042016-09-07 17:40:12 -0700417 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400418
Victor Stinnera9f61a52013-07-16 22:17:26 +0200419/* GROWTH_RATE. Growth rate upon hitting maximum load.
INADA Naoki5fbc5112018-04-17 15:53:34 +0900420 * Currently set to used*3.
Victor Stinnera9f61a52013-07-16 22:17:26 +0200421 * This means that dicts double in size when growing without deletions,
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700422 * but have more head room when the number of deletions is on a par with the
INADA Naoki5fbc5112018-04-17 15:53:34 +0900423 * number of insertions. See also bpo-17563 and bpo-33205.
424 *
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700425 * GROWTH_RATE was set to used*4 up to version 3.2.
426 * GROWTH_RATE was set to used*2 in version 3.3.0
INADA Naoki5fbc5112018-04-17 15:53:34 +0900427 * GROWTH_RATE was set to used*2 + capacity/2 in 3.4.0-3.6.0.
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200428 */
INADA Naoki5fbc5112018-04-17 15:53:34 +0900429#define GROWTH_RATE(d) ((d)->ma_used*3)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400430
431#define ENSURE_ALLOWS_DELETIONS(d) \
432 if ((d)->ma_keys->dk_lookup == lookdict_unicode_nodummy) { \
433 (d)->ma_keys->dk_lookup = lookdict_unicode; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400435
436/* This immutable, empty PyDictKeysObject is used for PyDict_Clear()
437 * (which cannot fail and thus can do no allocation).
438 */
439static PyDictKeysObject empty_keys_struct = {
Serhiy Storchaka97932e42016-09-26 23:01:23 +0300440 1, /* dk_refcnt */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400441 1, /* dk_size */
442 lookdict_split, /* dk_lookup */
443 0, /* dk_usable (immutable) */
Victor Stinner742da042016-09-07 17:40:12 -0700444 0, /* dk_nentries */
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700445 {DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY,
446 DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY}, /* dk_indices */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400447};
448
449static PyObject *empty_values[1] = { NULL };
450
451#define Py_EMPTY_KEYS &empty_keys_struct
452
Victor Stinner611b0fa2016-09-14 15:02:01 +0200453/* Uncomment to check the dict content in _PyDict_CheckConsistency() */
454/* #define DEBUG_PYDICT */
455
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200456#ifdef DEBUG_PYDICT
457# define ASSERT_CONSISTENT(op) assert(_PyDict_CheckConsistency((PyObject *)(op), 1))
458#else
459# define ASSERT_CONSISTENT(op) assert(_PyDict_CheckConsistency((PyObject *)(op), 0))
460#endif
Victor Stinner611b0fa2016-09-14 15:02:01 +0200461
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200462
463int
464_PyDict_CheckConsistency(PyObject *op, int check_content)
Victor Stinner611b0fa2016-09-14 15:02:01 +0200465{
Victor Stinner68762572019-10-07 18:42:01 +0200466#define CHECK(expr) \
467 do { if (!(expr)) { _PyObject_ASSERT_FAILED_MSG(op, Py_STRINGIFY(expr)); } } while (0)
468
469 assert(op != NULL);
470 CHECK(PyDict_Check(op));
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200471 PyDictObject *mp = (PyDictObject *)op;
Victor Stinner50fe3f82018-10-26 18:47:15 +0200472
Victor Stinner611b0fa2016-09-14 15:02:01 +0200473 PyDictKeysObject *keys = mp->ma_keys;
474 int splitted = _PyDict_HasSplitTable(mp);
475 Py_ssize_t usable = USABLE_FRACTION(keys->dk_size);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200476
Victor Stinner68762572019-10-07 18:42:01 +0200477 CHECK(0 <= mp->ma_used && mp->ma_used <= usable);
478 CHECK(IS_POWER_OF_2(keys->dk_size));
479 CHECK(0 <= keys->dk_usable && keys->dk_usable <= usable);
480 CHECK(0 <= keys->dk_nentries && keys->dk_nentries <= usable);
481 CHECK(keys->dk_usable + keys->dk_nentries <= usable);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200482
483 if (!splitted) {
484 /* combined table */
Victor Stinner68762572019-10-07 18:42:01 +0200485 CHECK(keys->dk_refcnt == 1);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200486 }
487
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200488 if (check_content) {
489 PyDictKeyEntry *entries = DK_ENTRIES(keys);
490 Py_ssize_t i;
Victor Stinner611b0fa2016-09-14 15:02:01 +0200491
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200492 for (i=0; i < keys->dk_size; i++) {
493 Py_ssize_t ix = dictkeys_get_index(keys, i);
Victor Stinner68762572019-10-07 18:42:01 +0200494 CHECK(DKIX_DUMMY <= ix && ix <= usable);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200495 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200496
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200497 for (i=0; i < usable; i++) {
498 PyDictKeyEntry *entry = &entries[i];
499 PyObject *key = entry->me_key;
500
501 if (key != NULL) {
502 if (PyUnicode_CheckExact(key)) {
503 Py_hash_t hash = ((PyASCIIObject *)key)->hash;
Victor Stinner68762572019-10-07 18:42:01 +0200504 CHECK(hash != -1);
505 CHECK(entry->me_hash == hash);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200506 }
507 else {
508 /* test_dict fails if PyObject_Hash() is called again */
Victor Stinner68762572019-10-07 18:42:01 +0200509 CHECK(entry->me_hash != -1);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200510 }
511 if (!splitted) {
Victor Stinner68762572019-10-07 18:42:01 +0200512 CHECK(entry->me_value != NULL);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200513 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200514 }
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200515
516 if (splitted) {
Victor Stinner68762572019-10-07 18:42:01 +0200517 CHECK(entry->me_value == NULL);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200518 }
519 }
520
521 if (splitted) {
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200522 /* splitted table */
523 for (i=0; i < mp->ma_used; i++) {
Victor Stinner68762572019-10-07 18:42:01 +0200524 CHECK(mp->ma_values[i] != NULL);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200525 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200526 }
527 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200528 return 1;
Victor Stinner68762572019-10-07 18:42:01 +0200529
530#undef CHECK
Victor Stinner611b0fa2016-09-14 15:02:01 +0200531}
Victor Stinner611b0fa2016-09-14 15:02:01 +0200532
533
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400534static PyDictKeysObject *new_keys_object(Py_ssize_t size)
535{
536 PyDictKeysObject *dk;
Victor Stinner742da042016-09-07 17:40:12 -0700537 Py_ssize_t es, usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400538
Victor Stinner742da042016-09-07 17:40:12 -0700539 assert(size >= PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400540 assert(IS_POWER_OF_2(size));
Victor Stinner742da042016-09-07 17:40:12 -0700541
542 usable = USABLE_FRACTION(size);
543 if (size <= 0xff) {
544 es = 1;
545 }
546 else if (size <= 0xffff) {
547 es = 2;
548 }
549#if SIZEOF_VOID_P > 4
550 else if (size <= 0xffffffff) {
551 es = 4;
552 }
553#endif
554 else {
555 es = sizeof(Py_ssize_t);
556 }
557
558 if (size == PyDict_MINSIZE && numfreekeys > 0) {
559 dk = keys_free_list[--numfreekeys];
560 }
561 else {
Victor Stinner98ee9d52016-09-08 09:33:56 -0700562 dk = PyObject_MALLOC(sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -0700563 + es * size
564 + sizeof(PyDictKeyEntry) * usable);
Victor Stinner742da042016-09-07 17:40:12 -0700565 if (dk == NULL) {
566 PyErr_NoMemory();
567 return NULL;
568 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400569 }
Victor Stinner49932fe2020-02-03 17:55:05 +0100570#ifdef Py_REF_DEBUG
571 _Py_RefTotal++;
572#endif
INADA Naokia7576492018-11-14 18:39:27 +0900573 dk->dk_refcnt = 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400574 dk->dk_size = size;
Victor Stinner742da042016-09-07 17:40:12 -0700575 dk->dk_usable = usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400576 dk->dk_lookup = lookdict_unicode_nodummy;
Victor Stinner742da042016-09-07 17:40:12 -0700577 dk->dk_nentries = 0;
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700578 memset(&dk->dk_indices[0], 0xff, es * size);
Victor Stinner742da042016-09-07 17:40:12 -0700579 memset(DK_ENTRIES(dk), 0, sizeof(PyDictKeyEntry) * usable);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400580 return dk;
581}
582
583static void
584free_keys_object(PyDictKeysObject *keys)
585{
Victor Stinner742da042016-09-07 17:40:12 -0700586 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400587 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -0700588 for (i = 0, n = keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400589 Py_XDECREF(entries[i].me_key);
590 Py_XDECREF(entries[i].me_value);
591 }
Victor Stinner742da042016-09-07 17:40:12 -0700592 if (keys->dk_size == PyDict_MINSIZE && numfreekeys < PyDict_MAXFREELIST) {
593 keys_free_list[numfreekeys++] = keys;
594 return;
595 }
Raymond Hettingerce5179f2016-01-31 08:56:21 -0800596 PyObject_FREE(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400597}
598
599#define new_values(size) PyMem_NEW(PyObject *, size)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400600#define free_values(values) PyMem_FREE(values)
601
602/* Consumes a reference to the keys object */
603static PyObject *
604new_dict(PyDictKeysObject *keys, PyObject **values)
605{
606 PyDictObject *mp;
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200607 assert(keys != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 if (numfree) {
609 mp = free_list[--numfree];
610 assert (mp != NULL);
Dong-hee Na1b55b652020-02-17 19:09:15 +0900611 assert (Py_IS_TYPE(mp, &PyDict_Type));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 _Py_NewReference((PyObject *)mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400614 else {
615 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
616 if (mp == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +0900617 dictkeys_decref(keys);
Zackery Spytz3d07c1e2019-03-23 20:23:29 -0600618 if (values != empty_values) {
619 free_values(values);
620 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400621 return NULL;
622 }
623 }
624 mp->ma_keys = keys;
625 mp->ma_values = values;
626 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -0700627 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200628 ASSERT_CONSISTENT(mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000630}
631
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400632/* Consumes a reference to the keys object */
633static PyObject *
634new_dict_with_shared_keys(PyDictKeysObject *keys)
635{
636 PyObject **values;
637 Py_ssize_t i, size;
638
Victor Stinner742da042016-09-07 17:40:12 -0700639 size = USABLE_FRACTION(DK_SIZE(keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400640 values = new_values(size);
641 if (values == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +0900642 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400643 return PyErr_NoMemory();
644 }
645 for (i = 0; i < size; i++) {
646 values[i] = NULL;
647 }
648 return new_dict(keys, values);
649}
650
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500651
652static PyObject *
653clone_combined_dict(PyDictObject *orig)
654{
655 assert(PyDict_CheckExact(orig));
656 assert(orig->ma_values == NULL);
657 assert(orig->ma_keys->dk_refcnt == 1);
658
659 Py_ssize_t keys_size = _PyDict_KeysSize(orig->ma_keys);
660 PyDictKeysObject *keys = PyObject_Malloc(keys_size);
661 if (keys == NULL) {
662 PyErr_NoMemory();
663 return NULL;
664 }
665
666 memcpy(keys, orig->ma_keys, keys_size);
667
668 /* After copying key/value pairs, we need to incref all
669 keys and values and they are about to be co-owned by a
670 new dict object. */
671 PyDictKeyEntry *ep0 = DK_ENTRIES(keys);
672 Py_ssize_t n = keys->dk_nentries;
673 for (Py_ssize_t i = 0; i < n; i++) {
674 PyDictKeyEntry *entry = &ep0[i];
675 PyObject *value = entry->me_value;
676 if (value != NULL) {
677 Py_INCREF(value);
678 Py_INCREF(entry->me_key);
679 }
680 }
681
682 PyDictObject *new = (PyDictObject *)new_dict(keys, NULL);
683 if (new == NULL) {
684 /* In case of an error, `new_dict()` takes care of
685 cleaning up `keys`. */
686 return NULL;
687 }
688 new->ma_used = orig->ma_used;
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200689 ASSERT_CONSISTENT(new);
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500690 if (_PyObject_GC_IS_TRACKED(orig)) {
691 /* Maintain tracking. */
692 _PyObject_GC_TRACK(new);
693 }
Yury Selivanov0b752282018-07-06 12:20:07 -0400694
695 /* Since we copied the keys table we now have an extra reference
Victor Stinner49932fe2020-02-03 17:55:05 +0100696 in the system. Manually call increment _Py_RefTotal to signal that
INADA Naokia7576492018-11-14 18:39:27 +0900697 we have it now; calling dictkeys_incref would be an error as
Yury Selivanov0b752282018-07-06 12:20:07 -0400698 keys->dk_refcnt is already set to 1 (after memcpy). */
Victor Stinner49932fe2020-02-03 17:55:05 +0100699#ifdef Py_REF_DEBUG
700 _Py_RefTotal++;
701#endif
Yury Selivanov0b752282018-07-06 12:20:07 -0400702
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500703 return (PyObject *)new;
704}
705
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400706PyObject *
707PyDict_New(void)
708{
Inada Naokif2a18672019-03-12 17:25:44 +0900709 dictkeys_incref(Py_EMPTY_KEYS);
710 return new_dict(Py_EMPTY_KEYS, empty_values);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400711}
712
Victor Stinner742da042016-09-07 17:40:12 -0700713/* Search index of hash table from offset of entry table */
714static Py_ssize_t
715lookdict_index(PyDictKeysObject *k, Py_hash_t hash, Py_ssize_t index)
716{
Victor Stinner742da042016-09-07 17:40:12 -0700717 size_t mask = DK_MASK(k);
INADA Naoki073ae482017-06-23 15:22:50 +0900718 size_t perturb = (size_t)hash;
719 size_t i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700720
INADA Naoki073ae482017-06-23 15:22:50 +0900721 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900722 Py_ssize_t ix = dictkeys_get_index(k, i);
Victor Stinner742da042016-09-07 17:40:12 -0700723 if (ix == index) {
724 return i;
725 }
726 if (ix == DKIX_EMPTY) {
727 return DKIX_EMPTY;
728 }
INADA Naoki073ae482017-06-23 15:22:50 +0900729 perturb >>= PERTURB_SHIFT;
730 i = mask & (i*5 + perturb + 1);
Victor Stinner742da042016-09-07 17:40:12 -0700731 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700732 Py_UNREACHABLE();
Victor Stinner742da042016-09-07 17:40:12 -0700733}
734
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000735/*
736The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000737This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000738Open addressing is preferred over chaining since the link overhead for
739chaining would be substantial (100% with typical malloc overhead).
740
Tim Peterseb28ef22001-06-02 05:27:19 +0000741The initial probe index is computed as hash mod the table size. Subsequent
742probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000743
744All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000745
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000746The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000747contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000748Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000749
Victor Stinner742da042016-09-07 17:40:12 -0700750lookdict() is general-purpose, and may return DKIX_ERROR if (and only if) a
Victor Stinnera4348cc2016-09-08 12:01:25 -0700751comparison raises an exception.
Guido van Rossum89d8c602007-09-18 17:26:56 +0000752lookdict_unicode() below is specialized to string keys, comparison of which can
INADA Naoki1b8df102017-02-20 22:48:10 +0900753never raise an exception; that function can never return DKIX_ERROR when key
754is string. Otherwise, it falls back to lookdict().
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400755lookdict_unicode_nodummy is further specialized for string keys that cannot be
756the <dummy> value.
INADA Naoki778928b2017-08-03 23:45:15 +0900757For both, when the key isn't found a DKIX_EMPTY is returned.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000758*/
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100759static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400760lookdict(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900761 Py_hash_t hash, PyObject **value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000762{
INADA Naoki778928b2017-08-03 23:45:15 +0900763 size_t i, mask, perturb;
Victor Stinner742da042016-09-07 17:40:12 -0700764 PyDictKeysObject *dk;
INADA Naoki778928b2017-08-03 23:45:15 +0900765 PyDictKeyEntry *ep0;
Tim Peterseb28ef22001-06-02 05:27:19 +0000766
Antoine Pitrou9a234902012-05-13 20:48:01 +0200767top:
Victor Stinner742da042016-09-07 17:40:12 -0700768 dk = mp->ma_keys;
Victor Stinner742da042016-09-07 17:40:12 -0700769 ep0 = DK_ENTRIES(dk);
INADA Naoki778928b2017-08-03 23:45:15 +0900770 mask = DK_MASK(dk);
771 perturb = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700773
INADA Naoki778928b2017-08-03 23:45:15 +0900774 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900775 Py_ssize_t ix = dictkeys_get_index(dk, i);
Victor Stinner742da042016-09-07 17:40:12 -0700776 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700777 *value_addr = NULL;
778 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400779 }
INADA Naoki778928b2017-08-03 23:45:15 +0900780 if (ix >= 0) {
781 PyDictKeyEntry *ep = &ep0[ix];
782 assert(ep->me_key != NULL);
783 if (ep->me_key == key) {
784 *value_addr = ep->me_value;
785 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700786 }
INADA Naoki778928b2017-08-03 23:45:15 +0900787 if (ep->me_hash == hash) {
788 PyObject *startkey = ep->me_key;
789 Py_INCREF(startkey);
790 int cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
791 Py_DECREF(startkey);
792 if (cmp < 0) {
793 *value_addr = NULL;
794 return DKIX_ERROR;
795 }
796 if (dk == mp->ma_keys && ep->me_key == startkey) {
797 if (cmp > 0) {
798 *value_addr = ep->me_value;
799 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700800 }
INADA Naoki778928b2017-08-03 23:45:15 +0900801 }
802 else {
803 /* The dict was mutated, restart */
804 goto top;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400805 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 }
INADA Naoki778928b2017-08-03 23:45:15 +0900808 perturb >>= PERTURB_SHIFT;
809 i = (i*5 + perturb + 1) & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700811 Py_UNREACHABLE();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000812}
813
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400814/* Specialized version for string-only keys */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100815static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400816lookdict_unicode(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900817 Py_hash_t hash, PyObject **value_addr)
Fred Drake1bff34a2000-08-31 19:31:38 +0000818{
Victor Stinner742da042016-09-07 17:40:12 -0700819 assert(mp->ma_values == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 /* Make sure this function doesn't have to handle non-unicode keys,
821 including subclasses of str; e.g., one reason to subclass
822 unicodes is to override __eq__, and for speed we don't cater to
823 that here. */
824 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400825 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900826 return lookdict(mp, key, hash, value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 }
Tim Peters15d49292001-05-27 07:39:22 +0000828
INADA Naoki778928b2017-08-03 23:45:15 +0900829 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
830 size_t mask = DK_MASK(mp->ma_keys);
831 size_t perturb = (size_t)hash;
832 size_t i = (size_t)hash & mask;
833
834 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900835 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
Victor Stinner742da042016-09-07 17:40:12 -0700836 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700837 *value_addr = NULL;
838 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400839 }
INADA Naoki778928b2017-08-03 23:45:15 +0900840 if (ix >= 0) {
841 PyDictKeyEntry *ep = &ep0[ix];
842 assert(ep->me_key != NULL);
843 assert(PyUnicode_CheckExact(ep->me_key));
844 if (ep->me_key == key ||
845 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
846 *value_addr = ep->me_value;
847 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700848 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400849 }
INADA Naoki778928b2017-08-03 23:45:15 +0900850 perturb >>= PERTURB_SHIFT;
851 i = mask & (i*5 + perturb + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700853 Py_UNREACHABLE();
Fred Drake1bff34a2000-08-31 19:31:38 +0000854}
855
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400856/* Faster version of lookdict_unicode when it is known that no <dummy> keys
857 * will be present. */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100858static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400859lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900860 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400861{
Victor Stinner742da042016-09-07 17:40:12 -0700862 assert(mp->ma_values == NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400863 /* Make sure this function doesn't have to handle non-unicode keys,
864 including subclasses of str; e.g., one reason to subclass
865 unicodes is to override __eq__, and for speed we don't cater to
866 that here. */
867 if (!PyUnicode_CheckExact(key)) {
868 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900869 return lookdict(mp, key, hash, value_addr);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400870 }
INADA Naoki778928b2017-08-03 23:45:15 +0900871
872 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
873 size_t mask = DK_MASK(mp->ma_keys);
874 size_t perturb = (size_t)hash;
875 size_t i = (size_t)hash & mask;
876
877 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900878 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
Victor Stinner742da042016-09-07 17:40:12 -0700879 assert (ix != DKIX_DUMMY);
880 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700881 *value_addr = NULL;
882 return DKIX_EMPTY;
883 }
INADA Naoki778928b2017-08-03 23:45:15 +0900884 PyDictKeyEntry *ep = &ep0[ix];
885 assert(ep->me_key != NULL);
886 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700887 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400888 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900889 *value_addr = ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700890 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400891 }
INADA Naoki778928b2017-08-03 23:45:15 +0900892 perturb >>= PERTURB_SHIFT;
893 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400894 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700895 Py_UNREACHABLE();
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400896}
897
898/* Version of lookdict for split tables.
899 * All split tables and only split tables use this lookup function.
900 * Split tables only contain unicode keys and no dummy keys,
901 * so algorithm is the same as lookdict_unicode_nodummy.
902 */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100903static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400904lookdict_split(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900905 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400906{
Victor Stinner742da042016-09-07 17:40:12 -0700907 /* mp must split table */
908 assert(mp->ma_values != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400909 if (!PyUnicode_CheckExact(key)) {
INADA Naoki778928b2017-08-03 23:45:15 +0900910 Py_ssize_t ix = lookdict(mp, key, hash, value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700911 if (ix >= 0) {
INADA Naokiba609772016-12-07 20:41:42 +0900912 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700913 }
914 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400915 }
Victor Stinner742da042016-09-07 17:40:12 -0700916
INADA Naoki778928b2017-08-03 23:45:15 +0900917 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
918 size_t mask = DK_MASK(mp->ma_keys);
919 size_t perturb = (size_t)hash;
920 size_t i = (size_t)hash & mask;
921
922 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900923 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
INADA Naoki778928b2017-08-03 23:45:15 +0900924 assert (ix != DKIX_DUMMY);
Victor Stinner742da042016-09-07 17:40:12 -0700925 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700926 *value_addr = NULL;
927 return DKIX_EMPTY;
928 }
INADA Naoki778928b2017-08-03 23:45:15 +0900929 PyDictKeyEntry *ep = &ep0[ix];
930 assert(ep->me_key != NULL);
931 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700932 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400933 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900934 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700935 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400936 }
INADA Naoki778928b2017-08-03 23:45:15 +0900937 perturb >>= PERTURB_SHIFT;
938 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400939 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700940 Py_UNREACHABLE();
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400941}
942
Benjamin Petersonfb886362010-04-24 18:21:17 +0000943int
944_PyDict_HasOnlyStringKeys(PyObject *dict)
945{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 Py_ssize_t pos = 0;
947 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000948 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400950 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 return 1;
952 while (PyDict_Next(dict, &pos, &key, &value))
953 if (!PyUnicode_Check(key))
954 return 0;
955 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000956}
957
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000958#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 do { \
960 if (!_PyObject_GC_IS_TRACKED(mp)) { \
961 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
962 _PyObject_GC_MAY_BE_TRACKED(value)) { \
963 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 } \
965 } \
966 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000967
968void
969_PyDict_MaybeUntrack(PyObject *op)
970{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 PyDictObject *mp;
972 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -0700973 Py_ssize_t i, numentries;
974 PyDictKeyEntry *ep0;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
977 return;
978
979 mp = (PyDictObject *) op;
Victor Stinner742da042016-09-07 17:40:12 -0700980 ep0 = DK_ENTRIES(mp->ma_keys);
981 numentries = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400982 if (_PyDict_HasSplitTable(mp)) {
Victor Stinner742da042016-09-07 17:40:12 -0700983 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400984 if ((value = mp->ma_values[i]) == NULL)
985 continue;
986 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
Victor Stinner742da042016-09-07 17:40:12 -0700987 assert(!_PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400988 return;
989 }
990 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400992 else {
Victor Stinner742da042016-09-07 17:40:12 -0700993 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400994 if ((value = ep0[i].me_value) == NULL)
995 continue;
996 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
997 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
998 return;
999 }
1000 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001002}
1003
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001004/* Internal function to find slot for an item from its hash
Victor Stinner3c336c52016-09-12 14:17:40 +02001005 when it is known that the key is not present in the dict.
1006
1007 The dict must be combined. */
INADA Naokiba609772016-12-07 20:41:42 +09001008static Py_ssize_t
INADA Naoki778928b2017-08-03 23:45:15 +09001009find_empty_slot(PyDictKeysObject *keys, Py_hash_t hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001010{
INADA Naoki778928b2017-08-03 23:45:15 +09001011 assert(keys != NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001012
INADA Naoki778928b2017-08-03 23:45:15 +09001013 const size_t mask = DK_MASK(keys);
1014 size_t i = hash & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001015 Py_ssize_t ix = dictkeys_get_index(keys, i);
INADA Naoki778928b2017-08-03 23:45:15 +09001016 for (size_t perturb = hash; ix >= 0;) {
INADA Naoki267941c2016-10-06 15:19:07 +09001017 perturb >>= PERTURB_SHIFT;
INADA Naoki778928b2017-08-03 23:45:15 +09001018 i = (i*5 + perturb + 1) & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001019 ix = dictkeys_get_index(keys, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 }
INADA Naoki778928b2017-08-03 23:45:15 +09001021 return i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001022}
1023
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001024static int
1025insertion_resize(PyDictObject *mp)
1026{
Raymond Hettinger36f74aa2013-05-17 03:01:13 -07001027 return dictresize(mp, GROWTH_RATE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001028}
Antoine Pitroue965d972012-02-27 00:45:12 +01001029
1030/*
1031Internal routine to insert a new item into the table.
1032Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +01001033Returns -1 if an error occurred, or 0 on success.
1034*/
1035static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001036insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001037{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001038 PyObject *old_value;
INADA Naokiba609772016-12-07 20:41:42 +09001039 PyDictKeyEntry *ep;
Antoine Pitroue965d972012-02-27 00:45:12 +01001040
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001041 Py_INCREF(key);
1042 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001043 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
1044 if (insertion_resize(mp) < 0)
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001045 goto Fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001046 }
1047
INADA Naoki778928b2017-08-03 23:45:15 +09001048 Py_ssize_t ix = mp->ma_keys->dk_lookup(mp, key, hash, &old_value);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001049 if (ix == DKIX_ERROR)
1050 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001051
Antoine Pitroud6967322014-10-18 00:35:00 +02001052 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001053 MAINTAIN_TRACKING(mp, key, value);
Victor Stinner742da042016-09-07 17:40:12 -07001054
1055 /* When insertion order is different from shared key, we can't share
1056 * the key anymore. Convert this instance to combine table.
1057 */
1058 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09001059 ((ix >= 0 && old_value == NULL && mp->ma_used != ix) ||
Victor Stinner742da042016-09-07 17:40:12 -07001060 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001061 if (insertion_resize(mp) < 0)
1062 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001063 ix = DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001064 }
Victor Stinner742da042016-09-07 17:40:12 -07001065
1066 if (ix == DKIX_EMPTY) {
1067 /* Insert into new slot. */
INADA Naokiba609772016-12-07 20:41:42 +09001068 assert(old_value == NULL);
Victor Stinner742da042016-09-07 17:40:12 -07001069 if (mp->ma_keys->dk_usable <= 0) {
1070 /* Need to resize. */
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001071 if (insertion_resize(mp) < 0)
1072 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001073 }
INADA Naoki778928b2017-08-03 23:45:15 +09001074 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naokiba609772016-12-07 20:41:42 +09001075 ep = &DK_ENTRIES(mp->ma_keys)[mp->ma_keys->dk_nentries];
INADA Naokia7576492018-11-14 18:39:27 +09001076 dictkeys_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Victor Stinner742da042016-09-07 17:40:12 -07001077 ep->me_key = key;
1078 ep->me_hash = hash;
1079 if (mp->ma_values) {
1080 assert (mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
1081 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001082 }
1083 else {
Victor Stinner742da042016-09-07 17:40:12 -07001084 ep->me_value = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001085 }
1086 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001087 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07001088 mp->ma_keys->dk_usable--;
1089 mp->ma_keys->dk_nentries++;
1090 assert(mp->ma_keys->dk_usable >= 0);
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001091 ASSERT_CONSISTENT(mp);
Victor Stinner742da042016-09-07 17:40:12 -07001092 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001093 }
Victor Stinner742da042016-09-07 17:40:12 -07001094
Inada Naoki91234a12019-06-03 21:30:58 +09001095 if (old_value != value) {
1096 if (_PyDict_HasSplitTable(mp)) {
1097 mp->ma_values[ix] = value;
1098 if (old_value == NULL) {
1099 /* pending state */
1100 assert(ix == mp->ma_used);
1101 mp->ma_used++;
1102 }
INADA Naokiba609772016-12-07 20:41:42 +09001103 }
Inada Naoki91234a12019-06-03 21:30:58 +09001104 else {
1105 assert(old_value != NULL);
1106 DK_ENTRIES(mp->ma_keys)[ix].me_value = value;
1107 }
1108 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokiba609772016-12-07 20:41:42 +09001109 }
INADA Naokiba609772016-12-07 20:41:42 +09001110 Py_XDECREF(old_value); /* which **CAN** re-enter (see issue #22653) */
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001111 ASSERT_CONSISTENT(mp);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001112 Py_DECREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001113 return 0;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001114
1115Fail:
1116 Py_DECREF(value);
1117 Py_DECREF(key);
1118 return -1;
Antoine Pitroue965d972012-02-27 00:45:12 +01001119}
1120
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001121// Same to insertdict but specialized for ma_keys = Py_EMPTY_KEYS.
1122static int
1123insert_to_emptydict(PyDictObject *mp, PyObject *key, Py_hash_t hash,
1124 PyObject *value)
1125{
1126 assert(mp->ma_keys == Py_EMPTY_KEYS);
1127
1128 PyDictKeysObject *newkeys = new_keys_object(PyDict_MINSIZE);
1129 if (newkeys == NULL) {
1130 return -1;
1131 }
1132 if (!PyUnicode_CheckExact(key)) {
1133 newkeys->dk_lookup = lookdict;
1134 }
1135 dictkeys_decref(Py_EMPTY_KEYS);
1136 mp->ma_keys = newkeys;
1137 mp->ma_values = NULL;
1138
1139 Py_INCREF(key);
1140 Py_INCREF(value);
1141 MAINTAIN_TRACKING(mp, key, value);
1142
1143 size_t hashpos = (size_t)hash & (PyDict_MINSIZE-1);
Dong-hee Nac39d1dd2019-10-11 17:43:11 +09001144 PyDictKeyEntry *ep = DK_ENTRIES(mp->ma_keys);
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001145 dictkeys_set_index(mp->ma_keys, hashpos, 0);
1146 ep->me_key = key;
1147 ep->me_hash = hash;
1148 ep->me_value = value;
1149 mp->ma_used++;
1150 mp->ma_version_tag = DICT_NEXT_VERSION();
1151 mp->ma_keys->dk_usable--;
1152 mp->ma_keys->dk_nentries++;
1153 return 0;
1154}
1155
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001156/*
luzpaza5293b42017-11-05 07:37:50 -06001157Internal routine used by dictresize() to build a hashtable of entries.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001158*/
1159static void
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001160build_indices(PyDictKeysObject *keys, PyDictKeyEntry *ep, Py_ssize_t n)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001161{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001162 size_t mask = (size_t)DK_SIZE(keys) - 1;
1163 for (Py_ssize_t ix = 0; ix != n; ix++, ep++) {
1164 Py_hash_t hash = ep->me_hash;
1165 size_t i = hash & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001166 for (size_t perturb = hash; dictkeys_get_index(keys, i) != DKIX_EMPTY;) {
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001167 perturb >>= PERTURB_SHIFT;
INADA Naoki870c2862017-06-24 09:03:19 +09001168 i = mask & (i*5 + perturb + 1);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001169 }
INADA Naokia7576492018-11-14 18:39:27 +09001170 dictkeys_set_index(keys, i, ix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001172}
1173
1174/*
1175Restructure the table by allocating a new table and reinserting all
1176items again. When entries have been deleted, the new table may
1177actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001178If a table is split (its keys and hashes are shared, its values are not),
1179then the values are temporarily copied into the table, it is resized as
1180a combined table, then the me_value slots in the old table are NULLed out.
1181After resizing a table is always combined,
1182but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001183*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001184static int
Victor Stinner3d3f2642016-12-15 17:21:23 +01001185dictresize(PyDictObject *mp, Py_ssize_t minsize)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001186{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001187 Py_ssize_t newsize, numentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001188 PyDictKeysObject *oldkeys;
1189 PyObject **oldvalues;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001190 PyDictKeyEntry *oldentries, *newentries;
Tim Peters91a364d2001-05-19 07:04:38 +00001191
Victor Stinner742da042016-09-07 17:40:12 -07001192 /* Find the smallest table size > minused. */
1193 for (newsize = PyDict_MINSIZE;
Victor Stinner3d3f2642016-12-15 17:21:23 +01001194 newsize < minsize && newsize > 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 newsize <<= 1)
1196 ;
1197 if (newsize <= 0) {
1198 PyErr_NoMemory();
1199 return -1;
1200 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001201
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001202 oldkeys = mp->ma_keys;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001203
1204 /* NOTE: Current odict checks mp->ma_keys to detect resize happen.
1205 * So we can't reuse oldkeys even if oldkeys->dk_size == newsize.
1206 * TODO: Try reusing oldkeys when reimplement odict.
1207 */
1208
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001209 /* Allocate a new table. */
1210 mp->ma_keys = new_keys_object(newsize);
1211 if (mp->ma_keys == NULL) {
1212 mp->ma_keys = oldkeys;
1213 return -1;
1214 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01001215 // New table must be large enough.
1216 assert(mp->ma_keys->dk_usable >= mp->ma_used);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001217 if (oldkeys->dk_lookup == lookdict)
1218 mp->ma_keys->dk_lookup = lookdict;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001219
1220 numentries = mp->ma_used;
1221 oldentries = DK_ENTRIES(oldkeys);
1222 newentries = DK_ENTRIES(mp->ma_keys);
1223 oldvalues = mp->ma_values;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001224 if (oldvalues != NULL) {
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001225 /* Convert split table into new combined table.
1226 * We must incref keys; we can transfer values.
1227 * Note that values of split table is always dense.
1228 */
1229 for (Py_ssize_t i = 0; i < numentries; i++) {
1230 assert(oldvalues[i] != NULL);
1231 PyDictKeyEntry *ep = &oldentries[i];
1232 PyObject *key = ep->me_key;
1233 Py_INCREF(key);
1234 newentries[i].me_key = key;
1235 newentries[i].me_hash = ep->me_hash;
1236 newentries[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001238
INADA Naokia7576492018-11-14 18:39:27 +09001239 dictkeys_decref(oldkeys);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001240 mp->ma_values = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001241 if (oldvalues != empty_values) {
1242 free_values(oldvalues);
1243 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001244 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001245 else { // combined table.
1246 if (oldkeys->dk_nentries == numentries) {
1247 memcpy(newentries, oldentries, numentries * sizeof(PyDictKeyEntry));
1248 }
1249 else {
1250 PyDictKeyEntry *ep = oldentries;
1251 for (Py_ssize_t i = 0; i < numentries; i++) {
1252 while (ep->me_value == NULL)
1253 ep++;
1254 newentries[i] = *ep++;
1255 }
1256 }
1257
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001258 assert(oldkeys->dk_lookup != lookdict_split);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001259 assert(oldkeys->dk_refcnt == 1);
Victor Stinner49932fe2020-02-03 17:55:05 +01001260#ifdef Py_REF_DEBUG
1261 _Py_RefTotal--;
1262#endif
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001263 if (oldkeys->dk_size == PyDict_MINSIZE &&
Victor Stinner49932fe2020-02-03 17:55:05 +01001264 numfreekeys < PyDict_MAXFREELIST)
1265 {
INADA Naokia7576492018-11-14 18:39:27 +09001266 keys_free_list[numfreekeys++] = oldkeys;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001267 }
1268 else {
INADA Naokia7576492018-11-14 18:39:27 +09001269 PyObject_FREE(oldkeys);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001270 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001271 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001272
1273 build_indices(mp->ma_keys, newentries, numentries);
1274 mp->ma_keys->dk_usable -= numentries;
1275 mp->ma_keys->dk_nentries = numentries;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001277}
1278
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001279/* Returns NULL if unable to split table.
1280 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001281static PyDictKeysObject *
1282make_keys_shared(PyObject *op)
1283{
1284 Py_ssize_t i;
1285 Py_ssize_t size;
1286 PyDictObject *mp = (PyDictObject *)op;
1287
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001288 if (!PyDict_CheckExact(op))
1289 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001290 if (!_PyDict_HasSplitTable(mp)) {
1291 PyDictKeyEntry *ep0;
1292 PyObject **values;
1293 assert(mp->ma_keys->dk_refcnt == 1);
1294 if (mp->ma_keys->dk_lookup == lookdict) {
1295 return NULL;
1296 }
1297 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
1298 /* Remove dummy keys */
1299 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
1300 return NULL;
1301 }
1302 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
1303 /* Copy values into a new array */
Victor Stinner742da042016-09-07 17:40:12 -07001304 ep0 = DK_ENTRIES(mp->ma_keys);
1305 size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001306 values = new_values(size);
1307 if (values == NULL) {
1308 PyErr_SetString(PyExc_MemoryError,
1309 "Not enough memory to allocate new values array");
1310 return NULL;
1311 }
1312 for (i = 0; i < size; i++) {
1313 values[i] = ep0[i].me_value;
1314 ep0[i].me_value = NULL;
1315 }
1316 mp->ma_keys->dk_lookup = lookdict_split;
1317 mp->ma_values = values;
1318 }
INADA Naokia7576492018-11-14 18:39:27 +09001319 dictkeys_incref(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001320 return mp->ma_keys;
1321}
Christian Heimes99170a52007-12-19 02:07:34 +00001322
1323PyObject *
1324_PyDict_NewPresized(Py_ssize_t minused)
1325{
INADA Naoki92c50ee2016-11-22 00:57:02 +09001326 const Py_ssize_t max_presize = 128 * 1024;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001327 Py_ssize_t newsize;
1328 PyDictKeysObject *new_keys;
INADA Naoki92c50ee2016-11-22 00:57:02 +09001329
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001330 if (minused <= USABLE_FRACTION(PyDict_MINSIZE)) {
Inada Naokif2a18672019-03-12 17:25:44 +09001331 return PyDict_New();
1332 }
INADA Naoki92c50ee2016-11-22 00:57:02 +09001333 /* There are no strict guarantee that returned dict can contain minused
1334 * items without resize. So we create medium size dict instead of very
1335 * large dict or MemoryError.
1336 */
1337 if (minused > USABLE_FRACTION(max_presize)) {
1338 newsize = max_presize;
1339 }
1340 else {
1341 Py_ssize_t minsize = ESTIMATE_SIZE(minused);
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001342 newsize = PyDict_MINSIZE*2;
INADA Naoki92c50ee2016-11-22 00:57:02 +09001343 while (newsize < minsize) {
1344 newsize <<= 1;
1345 }
1346 }
1347 assert(IS_POWER_OF_2(newsize));
1348
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001349 new_keys = new_keys_object(newsize);
1350 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001352 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001353}
1354
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001355/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1356 * that may occur (originally dicts supported only string keys, and exceptions
1357 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001358 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001359 * (suppressed) occurred while computing the key's hash, or that some error
1360 * (suppressed) occurred when comparing keys in the dict's internal probe
1361 * sequence. A nasty example of the latter is when a Python-coded comparison
1362 * function hits a stack-depth error, which can cause this to return NULL
1363 * even if the key is present.
1364 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001365PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001366PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001367{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001368 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07001369 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 PyDictObject *mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 PyThreadState *tstate;
INADA Naokiba609772016-12-07 20:41:42 +09001372 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 if (!PyDict_Check(op))
1375 return NULL;
1376 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001377 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 {
1379 hash = PyObject_Hash(key);
1380 if (hash == -1) {
1381 PyErr_Clear();
1382 return NULL;
1383 }
1384 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 /* We can arrive here with a NULL tstate during initialization: try
1387 running "python -Wi" for an example related to string interning.
1388 Let's just hope that no exception occurs then... This must be
Victor Stinner50b48572018-11-01 01:51:40 +01001389 _PyThreadState_GET() and not PyThreadState_Get() because the latter
Victor Stinner9204fb82018-10-30 15:13:17 +01001390 abort Python if tstate is NULL. */
Victor Stinner50b48572018-11-01 01:51:40 +01001391 tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 if (tstate != NULL && tstate->curexc_type != NULL) {
1393 /* preserve the existing exception */
1394 PyObject *err_type, *err_value, *err_tb;
1395 PyErr_Fetch(&err_type, &err_value, &err_tb);
INADA Naoki778928b2017-08-03 23:45:15 +09001396 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 /* ignore errors */
1398 PyErr_Restore(err_type, err_value, err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001399 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 return NULL;
1401 }
1402 else {
INADA Naoki778928b2017-08-03 23:45:15 +09001403 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001404 if (ix < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 PyErr_Clear();
1406 return NULL;
1407 }
1408 }
INADA Naokiba609772016-12-07 20:41:42 +09001409 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001410}
1411
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001412/* Same as PyDict_GetItemWithError() but with hash supplied by caller.
1413 This returns NULL *with* an exception set if an exception occurred.
1414 It returns NULL *without* an exception set if the key wasn't present.
1415*/
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001416PyObject *
1417_PyDict_GetItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1418{
Victor Stinner742da042016-09-07 17:40:12 -07001419 Py_ssize_t ix;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001420 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001421 PyObject *value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001422
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001423 if (!PyDict_Check(op)) {
1424 PyErr_BadInternalCall();
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001425 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001426 }
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001427
INADA Naoki778928b2017-08-03 23:45:15 +09001428 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001429 if (ix < 0) {
1430 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001431 }
INADA Naokiba609772016-12-07 20:41:42 +09001432 return value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001433}
1434
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001435/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1436 This returns NULL *with* an exception set if an exception occurred.
1437 It returns NULL *without* an exception set if the key wasn't present.
1438*/
1439PyObject *
1440PyDict_GetItemWithError(PyObject *op, PyObject *key)
1441{
Victor Stinner742da042016-09-07 17:40:12 -07001442 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001443 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 PyDictObject*mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001445 PyObject *value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 if (!PyDict_Check(op)) {
1448 PyErr_BadInternalCall();
1449 return NULL;
1450 }
1451 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001452 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001453 {
1454 hash = PyObject_Hash(key);
1455 if (hash == -1) {
1456 return NULL;
1457 }
1458 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001459
INADA Naoki778928b2017-08-03 23:45:15 +09001460 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001461 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001463 return value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001464}
1465
Brett Cannonfd074152012-04-14 14:10:13 -04001466PyObject *
1467_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1468{
1469 PyObject *kv;
1470 kv = _PyUnicode_FromId(key); /* borrowed */
1471 if (kv == NULL)
1472 return NULL;
1473 return PyDict_GetItemWithError(dp, kv);
1474}
1475
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001476PyObject *
1477_PyDict_GetItemStringWithError(PyObject *v, const char *key)
1478{
1479 PyObject *kv, *rv;
1480 kv = PyUnicode_FromString(key);
1481 if (kv == NULL) {
1482 return NULL;
1483 }
1484 rv = PyDict_GetItemWithError(v, kv);
1485 Py_DECREF(kv);
1486 return rv;
1487}
1488
Victor Stinnerb4efc962015-11-20 09:24:02 +01001489/* Fast version of global value lookup (LOAD_GLOBAL).
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001490 * Lookup in globals, then builtins.
Victor Stinnerb4efc962015-11-20 09:24:02 +01001491 *
1492 * Raise an exception and return NULL if an error occurred (ex: computing the
1493 * key hash failed, key comparison failed, ...). Return NULL if the key doesn't
1494 * exist. Return the value if the key exists.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001495 */
1496PyObject *
1497_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001498{
Victor Stinner742da042016-09-07 17:40:12 -07001499 Py_ssize_t ix;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001500 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09001501 PyObject *value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001502
1503 if (!PyUnicode_CheckExact(key) ||
1504 (hash = ((PyASCIIObject *) key)->hash) == -1)
1505 {
1506 hash = PyObject_Hash(key);
1507 if (hash == -1)
1508 return NULL;
Antoine Pitroue965d972012-02-27 00:45:12 +01001509 }
Victor Stinnerb4efc962015-11-20 09:24:02 +01001510
1511 /* namespace 1: globals */
INADA Naoki778928b2017-08-03 23:45:15 +09001512 ix = globals->ma_keys->dk_lookup(globals, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001513 if (ix == DKIX_ERROR)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001514 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001515 if (ix != DKIX_EMPTY && value != NULL)
1516 return value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001517
1518 /* namespace 2: builtins */
INADA Naoki778928b2017-08-03 23:45:15 +09001519 ix = builtins->ma_keys->dk_lookup(builtins, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001520 if (ix < 0)
Victor Stinnerb4efc962015-11-20 09:24:02 +01001521 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001522 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001523}
1524
Antoine Pitroue965d972012-02-27 00:45:12 +01001525/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1526 * dictionary if it's merely replacing the value for an existing key.
1527 * This means that it's safe to loop over a dictionary with PyDict_Next()
1528 * and occasionally replace a value -- but you can't insert new keys or
1529 * remove them.
1530 */
1531int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001532PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001533{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001534 PyDictObject *mp;
1535 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001536 if (!PyDict_Check(op)) {
1537 PyErr_BadInternalCall();
1538 return -1;
1539 }
1540 assert(key);
1541 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001542 mp = (PyDictObject *)op;
1543 if (!PyUnicode_CheckExact(key) ||
1544 (hash = ((PyASCIIObject *) key)->hash) == -1)
1545 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001546 hash = PyObject_Hash(key);
1547 if (hash == -1)
1548 return -1;
1549 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001550
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001551 if (mp->ma_keys == Py_EMPTY_KEYS) {
1552 return insert_to_emptydict(mp, key, hash, value);
1553 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001554 /* insertdict() handles any resizing that might be necessary */
1555 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001556}
1557
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001558int
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001559_PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
1560 Py_hash_t hash)
1561{
1562 PyDictObject *mp;
1563
1564 if (!PyDict_Check(op)) {
1565 PyErr_BadInternalCall();
1566 return -1;
1567 }
1568 assert(key);
1569 assert(value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001570 assert(hash != -1);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001571 mp = (PyDictObject *)op;
1572
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001573 if (mp->ma_keys == Py_EMPTY_KEYS) {
1574 return insert_to_emptydict(mp, key, hash, value);
1575 }
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001576 /* insertdict() handles any resizing that might be necessary */
1577 return insertdict(mp, key, hash, value);
1578}
1579
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001580static int
INADA Naoki778928b2017-08-03 23:45:15 +09001581delitem_common(PyDictObject *mp, Py_hash_t hash, Py_ssize_t ix,
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001582 PyObject *old_value)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001583{
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001584 PyObject *old_key;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001585 PyDictKeyEntry *ep;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001586
INADA Naoki778928b2017-08-03 23:45:15 +09001587 Py_ssize_t hashpos = lookdict_index(mp->ma_keys, hash, ix);
1588 assert(hashpos >= 0);
1589
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001590 mp->ma_used--;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001591 mp->ma_version_tag = DICT_NEXT_VERSION();
1592 ep = &DK_ENTRIES(mp->ma_keys)[ix];
INADA Naokia7576492018-11-14 18:39:27 +09001593 dictkeys_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001594 ENSURE_ALLOWS_DELETIONS(mp);
1595 old_key = ep->me_key;
1596 ep->me_key = NULL;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001597 ep->me_value = NULL;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001598 Py_DECREF(old_key);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001599 Py_DECREF(old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001600
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001601 ASSERT_CONSISTENT(mp);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001602 return 0;
1603}
1604
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001605int
Tim Peters1f5871e2000-07-04 17:44:48 +00001606PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001607{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001608 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609 assert(key);
1610 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001611 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 hash = PyObject_Hash(key);
1613 if (hash == -1)
1614 return -1;
1615 }
Victor Stinner742da042016-09-07 17:40:12 -07001616
1617 return _PyDict_DelItem_KnownHash(op, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001618}
1619
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001620int
1621_PyDict_DelItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1622{
INADA Naoki778928b2017-08-03 23:45:15 +09001623 Py_ssize_t ix;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001624 PyDictObject *mp;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001625 PyObject *old_value;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001626
1627 if (!PyDict_Check(op)) {
1628 PyErr_BadInternalCall();
1629 return -1;
1630 }
1631 assert(key);
1632 assert(hash != -1);
1633 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001634 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001635 if (ix == DKIX_ERROR)
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001636 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09001637 if (ix == DKIX_EMPTY || old_value == NULL) {
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001638 _PyErr_SetKeyError(key);
1639 return -1;
1640 }
Victor Stinner78601a32016-09-09 19:28:36 -07001641
1642 // Split table doesn't allow deletion. Combine it.
1643 if (_PyDict_HasSplitTable(mp)) {
1644 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1645 return -1;
1646 }
INADA Naoki778928b2017-08-03 23:45:15 +09001647 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001648 assert(ix >= 0);
1649 }
1650
INADA Naoki778928b2017-08-03 23:45:15 +09001651 return delitem_common(mp, hash, ix, old_value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001652}
1653
Antoine Pitroud741ed42016-12-27 14:23:43 +01001654/* This function promises that the predicate -> deletion sequence is atomic
1655 * (i.e. protected by the GIL), assuming the predicate itself doesn't
1656 * release the GIL.
1657 */
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001658int
1659_PyDict_DelItemIf(PyObject *op, PyObject *key,
1660 int (*predicate)(PyObject *value))
1661{
Antoine Pitroud741ed42016-12-27 14:23:43 +01001662 Py_ssize_t hashpos, ix;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001663 PyDictObject *mp;
1664 Py_hash_t hash;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001665 PyObject *old_value;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001666 int res;
1667
1668 if (!PyDict_Check(op)) {
1669 PyErr_BadInternalCall();
1670 return -1;
1671 }
1672 assert(key);
1673 hash = PyObject_Hash(key);
1674 if (hash == -1)
1675 return -1;
1676 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001677 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001678 if (ix == DKIX_ERROR)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001679 return -1;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001680 if (ix == DKIX_EMPTY || old_value == NULL) {
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001681 _PyErr_SetKeyError(key);
1682 return -1;
1683 }
Antoine Pitroud741ed42016-12-27 14:23:43 +01001684
1685 // Split table doesn't allow deletion. Combine it.
1686 if (_PyDict_HasSplitTable(mp)) {
1687 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1688 return -1;
1689 }
INADA Naoki778928b2017-08-03 23:45:15 +09001690 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001691 assert(ix >= 0);
1692 }
1693
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001694 res = predicate(old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001695 if (res == -1)
1696 return -1;
INADA Naoki778928b2017-08-03 23:45:15 +09001697
1698 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1699 assert(hashpos >= 0);
1700
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001701 if (res > 0)
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001702 return delitem_common(mp, hashpos, ix, old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001703 else
1704 return 0;
1705}
1706
1707
Guido van Rossum25831651993-05-19 14:50:45 +00001708void
Tim Peters1f5871e2000-07-04 17:44:48 +00001709PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001710{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001712 PyDictKeysObject *oldkeys;
1713 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001716 if (!PyDict_Check(op))
1717 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001718 mp = ((PyDictObject *)op);
1719 oldkeys = mp->ma_keys;
1720 oldvalues = mp->ma_values;
1721 if (oldvalues == empty_values)
1722 return;
1723 /* Empty the dict... */
INADA Naokia7576492018-11-14 18:39:27 +09001724 dictkeys_incref(Py_EMPTY_KEYS);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001725 mp->ma_keys = Py_EMPTY_KEYS;
1726 mp->ma_values = empty_values;
1727 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001728 mp->ma_version_tag = DICT_NEXT_VERSION();
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001729 /* ...then clear the keys and values */
1730 if (oldvalues != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001731 n = oldkeys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001732 for (i = 0; i < n; i++)
1733 Py_CLEAR(oldvalues[i]);
1734 free_values(oldvalues);
INADA Naokia7576492018-11-14 18:39:27 +09001735 dictkeys_decref(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001736 }
1737 else {
1738 assert(oldkeys->dk_refcnt == 1);
INADA Naokia7576492018-11-14 18:39:27 +09001739 dictkeys_decref(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001740 }
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001741 ASSERT_CONSISTENT(mp);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001742}
1743
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001744/* Internal version of PyDict_Next that returns a hash value in addition
1745 * to the key and value.
1746 * Return 1 on success, return 0 when the reached the end of the dictionary
1747 * (or if op is not a dictionary)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001748 */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001749int
1750_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1751 PyObject **pvalue, Py_hash_t *phash)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001752{
INADA Naokica2d8be2016-11-04 16:59:10 +09001753 Py_ssize_t i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001754 PyDictObject *mp;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001755 PyDictKeyEntry *entry_ptr;
1756 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001757
1758 if (!PyDict_Check(op))
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001759 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001760 mp = (PyDictObject *)op;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001761 i = *ppos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001762 if (mp->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09001763 if (i < 0 || i >= mp->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001764 return 0;
INADA Naokica2d8be2016-11-04 16:59:10 +09001765 /* values of split table is always dense */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001766 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
INADA Naokica2d8be2016-11-04 16:59:10 +09001767 value = mp->ma_values[i];
1768 assert(value != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001770 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09001771 Py_ssize_t n = mp->ma_keys->dk_nentries;
1772 if (i < 0 || i >= n)
1773 return 0;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001774 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
1775 while (i < n && entry_ptr->me_value == NULL) {
1776 entry_ptr++;
1777 i++;
Victor Stinner742da042016-09-07 17:40:12 -07001778 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001779 if (i >= n)
1780 return 0;
1781 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001782 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001783 *ppos = i+1;
1784 if (pkey)
1785 *pkey = entry_ptr->me_key;
1786 if (phash)
1787 *phash = entry_ptr->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001788 if (pvalue)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001789 *pvalue = value;
1790 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001791}
1792
Tim Peters080c88b2003-02-15 03:01:11 +00001793/*
1794 * Iterate over a dict. Use like so:
1795 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001796 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001797 * PyObject *key, *value;
1798 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001799 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001800 * Refer to borrowed references in key and value.
Tim Peters080c88b2003-02-15 03:01:11 +00001801 * }
1802 *
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001803 * Return 1 on success, return 0 when the reached the end of the dictionary
1804 * (or if op is not a dictionary)
1805 *
Tim Peters080c88b2003-02-15 03:01:11 +00001806 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001807 * mutates the dict. One exception: it is safe if the loop merely changes
1808 * the values associated with the keys (but doesn't insert new keys or
1809 * delete keys), via PyDict_SetItem().
1810 */
Guido van Rossum25831651993-05-19 14:50:45 +00001811int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001812PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001813{
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001814 return _PyDict_Next(op, ppos, pkey, pvalue, NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001815}
1816
Eric Snow96c6af92015-05-29 22:21:39 -06001817/* Internal version of dict.pop(). */
1818PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001819_PyDict_Pop_KnownHash(PyObject *dict, PyObject *key, Py_hash_t hash, PyObject *deflt)
Eric Snow96c6af92015-05-29 22:21:39 -06001820{
Victor Stinner742da042016-09-07 17:40:12 -07001821 Py_ssize_t ix, hashpos;
Eric Snow96c6af92015-05-29 22:21:39 -06001822 PyObject *old_value, *old_key;
1823 PyDictKeyEntry *ep;
Yury Selivanov684ef2c2016-10-28 19:01:21 -04001824 PyDictObject *mp;
1825
1826 assert(PyDict_Check(dict));
1827 mp = (PyDictObject *)dict;
Eric Snow96c6af92015-05-29 22:21:39 -06001828
1829 if (mp->ma_used == 0) {
1830 if (deflt) {
1831 Py_INCREF(deflt);
1832 return deflt;
1833 }
1834 _PyErr_SetKeyError(key);
1835 return NULL;
1836 }
INADA Naoki778928b2017-08-03 23:45:15 +09001837 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001838 if (ix == DKIX_ERROR)
Eric Snow96c6af92015-05-29 22:21:39 -06001839 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001840 if (ix == DKIX_EMPTY || old_value == NULL) {
Eric Snow96c6af92015-05-29 22:21:39 -06001841 if (deflt) {
1842 Py_INCREF(deflt);
1843 return deflt;
1844 }
1845 _PyErr_SetKeyError(key);
1846 return NULL;
1847 }
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001848
Victor Stinner78601a32016-09-09 19:28:36 -07001849 // Split table doesn't allow deletion. Combine it.
1850 if (_PyDict_HasSplitTable(mp)) {
1851 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1852 return NULL;
1853 }
INADA Naoki778928b2017-08-03 23:45:15 +09001854 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001855 assert(ix >= 0);
1856 }
1857
INADA Naoki778928b2017-08-03 23:45:15 +09001858 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1859 assert(hashpos >= 0);
Victor Stinner78601a32016-09-09 19:28:36 -07001860 assert(old_value != NULL);
Eric Snow96c6af92015-05-29 22:21:39 -06001861 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001862 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokia7576492018-11-14 18:39:27 +09001863 dictkeys_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
Victor Stinner78601a32016-09-09 19:28:36 -07001864 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1865 ENSURE_ALLOWS_DELETIONS(mp);
1866 old_key = ep->me_key;
1867 ep->me_key = NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001868 ep->me_value = NULL;
Victor Stinner78601a32016-09-09 19:28:36 -07001869 Py_DECREF(old_key);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001870
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001871 ASSERT_CONSISTENT(mp);
Eric Snow96c6af92015-05-29 22:21:39 -06001872 return old_value;
1873}
1874
Serhiy Storchaka67796522017-01-12 18:34:33 +02001875PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001876_PyDict_Pop(PyObject *dict, PyObject *key, PyObject *deflt)
Serhiy Storchaka67796522017-01-12 18:34:33 +02001877{
1878 Py_hash_t hash;
1879
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001880 if (((PyDictObject *)dict)->ma_used == 0) {
Serhiy Storchaka67796522017-01-12 18:34:33 +02001881 if (deflt) {
1882 Py_INCREF(deflt);
1883 return deflt;
1884 }
1885 _PyErr_SetKeyError(key);
1886 return NULL;
1887 }
1888 if (!PyUnicode_CheckExact(key) ||
1889 (hash = ((PyASCIIObject *) key)->hash) == -1) {
1890 hash = PyObject_Hash(key);
1891 if (hash == -1)
1892 return NULL;
1893 }
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001894 return _PyDict_Pop_KnownHash(dict, key, hash, deflt);
Serhiy Storchaka67796522017-01-12 18:34:33 +02001895}
1896
Eric Snow96c6af92015-05-29 22:21:39 -06001897/* Internal version of dict.from_keys(). It is subclass-friendly. */
1898PyObject *
1899_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
1900{
1901 PyObject *it; /* iter(iterable) */
1902 PyObject *key;
1903 PyObject *d;
1904 int status;
1905
Victor Stinnera5ed5f02016-12-06 18:45:50 +01001906 d = _PyObject_CallNoArg(cls);
Eric Snow96c6af92015-05-29 22:21:39 -06001907 if (d == NULL)
1908 return NULL;
1909
1910 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
1911 if (PyDict_CheckExact(iterable)) {
1912 PyDictObject *mp = (PyDictObject *)d;
1913 PyObject *oldvalue;
1914 Py_ssize_t pos = 0;
1915 PyObject *key;
1916 Py_hash_t hash;
1917
Serhiy Storchakac61ac162017-03-21 08:52:38 +02001918 if (dictresize(mp, ESTIMATE_SIZE(PyDict_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001919 Py_DECREF(d);
1920 return NULL;
1921 }
1922
1923 while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
1924 if (insertdict(mp, key, hash, value)) {
1925 Py_DECREF(d);
1926 return NULL;
1927 }
1928 }
1929 return d;
1930 }
1931 if (PyAnySet_CheckExact(iterable)) {
1932 PyDictObject *mp = (PyDictObject *)d;
1933 Py_ssize_t pos = 0;
1934 PyObject *key;
1935 Py_hash_t hash;
1936
Victor Stinner742da042016-09-07 17:40:12 -07001937 if (dictresize(mp, ESTIMATE_SIZE(PySet_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001938 Py_DECREF(d);
1939 return NULL;
1940 }
1941
1942 while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
1943 if (insertdict(mp, key, hash, value)) {
1944 Py_DECREF(d);
1945 return NULL;
1946 }
1947 }
1948 return d;
1949 }
1950 }
1951
1952 it = PyObject_GetIter(iterable);
1953 if (it == NULL){
1954 Py_DECREF(d);
1955 return NULL;
1956 }
1957
1958 if (PyDict_CheckExact(d)) {
1959 while ((key = PyIter_Next(it)) != NULL) {
1960 status = PyDict_SetItem(d, key, value);
1961 Py_DECREF(key);
1962 if (status < 0)
1963 goto Fail;
1964 }
1965 } else {
1966 while ((key = PyIter_Next(it)) != NULL) {
1967 status = PyObject_SetItem(d, key, value);
1968 Py_DECREF(key);
1969 if (status < 0)
1970 goto Fail;
1971 }
1972 }
1973
1974 if (PyErr_Occurred())
1975 goto Fail;
1976 Py_DECREF(it);
1977 return d;
1978
1979Fail:
1980 Py_DECREF(it);
1981 Py_DECREF(d);
1982 return NULL;
1983}
1984
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001985/* Methods */
1986
1987static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001988dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001989{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001990 PyObject **values = mp->ma_values;
1991 PyDictKeysObject *keys = mp->ma_keys;
1992 Py_ssize_t i, n;
INADA Naokia6296d32017-08-24 14:55:17 +09001993
1994 /* bpo-31095: UnTrack is needed before calling any callbacks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001995 PyObject_GC_UnTrack(mp);
Jeroen Demeyer351c6742019-05-10 19:21:11 +02001996 Py_TRASHCAN_BEGIN(mp, dict_dealloc)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001997 if (values != NULL) {
1998 if (values != empty_values) {
Victor Stinner742da042016-09-07 17:40:12 -07001999 for (i = 0, n = mp->ma_keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002000 Py_XDECREF(values[i]);
2001 }
2002 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003 }
INADA Naokia7576492018-11-14 18:39:27 +09002004 dictkeys_decref(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002005 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02002006 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02002007 assert(keys->dk_refcnt == 1);
INADA Naokia7576492018-11-14 18:39:27 +09002008 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002009 }
Dong-hee Na1b55b652020-02-17 19:09:15 +09002010 if (numfree < PyDict_MAXFREELIST && Py_IS_TYPE(mp, &PyDict_Type))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002011 free_list[numfree++] = mp;
2012 else
2013 Py_TYPE(mp)->tp_free((PyObject *)mp);
Jeroen Demeyer351c6742019-05-10 19:21:11 +02002014 Py_TRASHCAN_END
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002015}
2016
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002017
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002018static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002019dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002020{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002021 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01002022 PyObject *key = NULL, *value = NULL;
2023 _PyUnicodeWriter writer;
2024 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00002025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002026 i = Py_ReprEnter((PyObject *)mp);
2027 if (i != 0) {
2028 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
2029 }
Guido van Rossum255443b1998-04-10 22:47:14 +00002030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002031 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01002032 Py_ReprLeave((PyObject *)mp);
2033 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002034 }
Tim Petersa7259592001-06-16 05:11:17 +00002035
Victor Stinnerf91929b2013-11-19 13:07:38 +01002036 _PyUnicodeWriter_Init(&writer);
2037 writer.overallocate = 1;
2038 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
2039 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00002040
Victor Stinnerf91929b2013-11-19 13:07:38 +01002041 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
2042 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00002043
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002044 /* Do repr() on each key+value pair, and insert ": " between them.
2045 Note that repr may mutate the dict. */
2046 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01002047 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002048 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01002049 PyObject *s;
2050 int res;
2051
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002052 /* Prevent repr from deleting key or value during key format. */
2053 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002054 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02002055
Victor Stinnerf91929b2013-11-19 13:07:38 +01002056 if (!first) {
2057 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
2058 goto error;
2059 }
2060 first = 0;
2061
2062 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002063 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01002064 goto error;
2065 res = _PyUnicodeWriter_WriteStr(&writer, s);
2066 Py_DECREF(s);
2067 if (res < 0)
2068 goto error;
2069
2070 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
2071 goto error;
2072
2073 s = PyObject_Repr(value);
2074 if (s == NULL)
2075 goto error;
2076 res = _PyUnicodeWriter_WriteStr(&writer, s);
2077 Py_DECREF(s);
2078 if (res < 0)
2079 goto error;
2080
2081 Py_CLEAR(key);
2082 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002083 }
Tim Petersa7259592001-06-16 05:11:17 +00002084
Victor Stinnerf91929b2013-11-19 13:07:38 +01002085 writer.overallocate = 0;
2086 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
2087 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00002088
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002089 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01002090
2091 return _PyUnicodeWriter_Finish(&writer);
2092
2093error:
2094 Py_ReprLeave((PyObject *)mp);
2095 _PyUnicodeWriter_Dealloc(&writer);
2096 Py_XDECREF(key);
2097 Py_XDECREF(value);
2098 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002099}
2100
Martin v. Löwis18e16552006-02-15 17:27:45 +00002101static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002102dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002103{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002104 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002105}
2106
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002107static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002108dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002109{
Victor Stinner742da042016-09-07 17:40:12 -07002110 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002111 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09002112 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002114 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002115 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002116 hash = PyObject_Hash(key);
2117 if (hash == -1)
2118 return NULL;
2119 }
INADA Naoki778928b2017-08-03 23:45:15 +09002120 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002121 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002123 if (ix == DKIX_EMPTY || value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002124 if (!PyDict_CheckExact(mp)) {
2125 /* Look up __missing__ method if we're a subclass. */
2126 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05002127 _Py_IDENTIFIER(__missing__);
2128 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002129 if (missing != NULL) {
Petr Viktorinffd97532020-02-11 17:46:57 +01002130 res = PyObject_CallOneArg(missing, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002131 Py_DECREF(missing);
2132 return res;
2133 }
2134 else if (PyErr_Occurred())
2135 return NULL;
2136 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07002137 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138 return NULL;
2139 }
INADA Naokiba609772016-12-07 20:41:42 +09002140 Py_INCREF(value);
2141 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002142}
2143
2144static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002145dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002146{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002147 if (w == NULL)
2148 return PyDict_DelItem((PyObject *)mp, v);
2149 else
2150 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002151}
2152
Guido van Rossuma9e7a811997-05-13 21:02:11 +00002153static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002154 (lenfunc)dict_length, /*mp_length*/
2155 (binaryfunc)dict_subscript, /*mp_subscript*/
2156 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002157};
2158
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002159static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002160dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002161{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002162 PyObject *v;
2163 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002164 PyDictKeyEntry *ep;
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002165 Py_ssize_t n, offset;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002166 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002167
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002168 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002169 n = mp->ma_used;
2170 v = PyList_New(n);
2171 if (v == NULL)
2172 return NULL;
2173 if (n != mp->ma_used) {
2174 /* Durnit. The allocations caused the dict to resize.
2175 * Just start over, this shouldn't normally happen.
2176 */
2177 Py_DECREF(v);
2178 goto again;
2179 }
Victor Stinner742da042016-09-07 17:40:12 -07002180 ep = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002181 if (mp->ma_values) {
2182 value_ptr = mp->ma_values;
2183 offset = sizeof(PyObject *);
2184 }
2185 else {
2186 value_ptr = &ep[0].me_value;
2187 offset = sizeof(PyDictKeyEntry);
2188 }
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002189 for (i = 0, j = 0; j < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002190 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002191 PyObject *key = ep[i].me_key;
2192 Py_INCREF(key);
2193 PyList_SET_ITEM(v, j, key);
2194 j++;
2195 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002196 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 }
2198 assert(j == n);
2199 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002200}
2201
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002202static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002203dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002204{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002205 PyObject *v;
2206 Py_ssize_t i, j;
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002207 PyDictKeyEntry *ep;
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002208 Py_ssize_t n, offset;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002209 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002210
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002211 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002212 n = mp->ma_used;
2213 v = PyList_New(n);
2214 if (v == NULL)
2215 return NULL;
2216 if (n != mp->ma_used) {
2217 /* Durnit. The allocations caused the dict to resize.
2218 * Just start over, this shouldn't normally happen.
2219 */
2220 Py_DECREF(v);
2221 goto again;
2222 }
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002223 ep = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002224 if (mp->ma_values) {
2225 value_ptr = mp->ma_values;
2226 offset = sizeof(PyObject *);
2227 }
2228 else {
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002229 value_ptr = &ep[0].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002230 offset = sizeof(PyDictKeyEntry);
2231 }
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002232 for (i = 0, j = 0; j < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002233 PyObject *value = *value_ptr;
2234 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2235 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002236 Py_INCREF(value);
2237 PyList_SET_ITEM(v, j, value);
2238 j++;
2239 }
2240 }
2241 assert(j == n);
2242 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002243}
2244
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002245static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002246dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002247{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002248 PyObject *v;
2249 Py_ssize_t i, j, n;
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002250 Py_ssize_t offset;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002251 PyObject *item, *key;
2252 PyDictKeyEntry *ep;
2253 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002255 /* Preallocate the list of tuples, to avoid allocations during
2256 * the loop over the items, which could trigger GC, which
2257 * could resize the dict. :-(
2258 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002259 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002260 n = mp->ma_used;
2261 v = PyList_New(n);
2262 if (v == NULL)
2263 return NULL;
2264 for (i = 0; i < n; i++) {
2265 item = PyTuple_New(2);
2266 if (item == NULL) {
2267 Py_DECREF(v);
2268 return NULL;
2269 }
2270 PyList_SET_ITEM(v, i, item);
2271 }
2272 if (n != mp->ma_used) {
2273 /* Durnit. The allocations caused the dict to resize.
2274 * Just start over, this shouldn't normally happen.
2275 */
2276 Py_DECREF(v);
2277 goto again;
2278 }
2279 /* Nothing we do below makes any function calls. */
Victor Stinner742da042016-09-07 17:40:12 -07002280 ep = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002281 if (mp->ma_values) {
2282 value_ptr = mp->ma_values;
2283 offset = sizeof(PyObject *);
2284 }
2285 else {
2286 value_ptr = &ep[0].me_value;
2287 offset = sizeof(PyDictKeyEntry);
2288 }
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002289 for (i = 0, j = 0; j < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002290 PyObject *value = *value_ptr;
2291 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2292 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002293 key = ep[i].me_key;
2294 item = PyList_GET_ITEM(v, j);
2295 Py_INCREF(key);
2296 PyTuple_SET_ITEM(item, 0, key);
2297 Py_INCREF(value);
2298 PyTuple_SET_ITEM(item, 1, value);
2299 j++;
2300 }
2301 }
2302 assert(j == n);
2303 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002304}
2305
Larry Hastings5c661892014-01-24 06:17:25 -08002306/*[clinic input]
2307@classmethod
2308dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08002309 iterable: object
2310 value: object=None
2311 /
2312
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002313Create a new dictionary with keys from iterable and values set to value.
Larry Hastings5c661892014-01-24 06:17:25 -08002314[clinic start generated code]*/
2315
Larry Hastings5c661892014-01-24 06:17:25 -08002316static PyObject *
2317dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002318/*[clinic end generated code: output=8fb98e4b10384999 input=382ba4855d0f74c3]*/
Larry Hastings5c661892014-01-24 06:17:25 -08002319{
Eric Snow96c6af92015-05-29 22:21:39 -06002320 return _PyDict_FromKeys((PyObject *)type, iterable, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002321}
2322
Brandt Buchereb8ac572020-02-24 19:47:34 -08002323/* Single-arg dict update; used by dict_update_common and operators. */
2324static int
2325dict_update_arg(PyObject *self, PyObject *arg)
2326{
2327 if (PyDict_CheckExact(arg)) {
2328 return PyDict_Merge(self, arg, 1);
2329 }
2330 _Py_IDENTIFIER(keys);
2331 PyObject *func;
2332 if (_PyObject_LookupAttrId(arg, &PyId_keys, &func) < 0) {
2333 return -1;
2334 }
2335 if (func != NULL) {
2336 Py_DECREF(func);
2337 return PyDict_Merge(self, arg, 1);
2338 }
2339 return PyDict_MergeFromSeq2(self, arg, 1);
2340}
2341
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002342static int
Victor Stinner742da042016-09-07 17:40:12 -07002343dict_update_common(PyObject *self, PyObject *args, PyObject *kwds,
2344 const char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002345{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346 PyObject *arg = NULL;
2347 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002348
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002349 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002350 result = -1;
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002351 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002352 else if (arg != NULL) {
Brandt Buchereb8ac572020-02-24 19:47:34 -08002353 result = dict_update_arg(self, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 }
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002356 if (result == 0 && kwds != NULL) {
2357 if (PyArg_ValidateKeywordArguments(kwds))
2358 result = PyDict_Merge(self, kwds, 1);
2359 else
2360 result = -1;
2361 }
2362 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002363}
2364
Victor Stinner91f0d4a2017-01-19 12:45:06 +01002365/* Note: dict.update() uses the METH_VARARGS|METH_KEYWORDS calling convention.
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +03002366 Using METH_FASTCALL|METH_KEYWORDS would make dict.update(**dict2) calls
2367 slower, see the issue #29312. */
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002368static PyObject *
2369dict_update(PyObject *self, PyObject *args, PyObject *kwds)
2370{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002371 if (dict_update_common(self, args, kwds, "update") != -1)
2372 Py_RETURN_NONE;
2373 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002374}
2375
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002376/* Update unconditionally replaces existing items.
2377 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00002378 otherwise it leaves existing items unchanged.
2379
2380 PyDict_{Update,Merge} update/merge from a mapping object.
2381
Tim Petersf582b822001-12-11 18:51:08 +00002382 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00002383 producing iterable objects of length 2.
2384*/
2385
Tim Petersf582b822001-12-11 18:51:08 +00002386int
Tim Peters1fc240e2001-10-26 05:06:50 +00002387PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
2388{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 PyObject *it; /* iter(seq2) */
2390 Py_ssize_t i; /* index into seq2 of current element */
2391 PyObject *item; /* seq2[i] */
2392 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00002393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 assert(d != NULL);
2395 assert(PyDict_Check(d));
2396 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00002397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398 it = PyObject_GetIter(seq2);
2399 if (it == NULL)
2400 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002402 for (i = 0; ; ++i) {
2403 PyObject *key, *value;
2404 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00002405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002406 fast = NULL;
2407 item = PyIter_Next(it);
2408 if (item == NULL) {
2409 if (PyErr_Occurred())
2410 goto Fail;
2411 break;
2412 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002414 /* Convert item to sequence, and verify length 2. */
2415 fast = PySequence_Fast(item, "");
2416 if (fast == NULL) {
2417 if (PyErr_ExceptionMatches(PyExc_TypeError))
2418 PyErr_Format(PyExc_TypeError,
2419 "cannot convert dictionary update "
2420 "sequence element #%zd to a sequence",
2421 i);
2422 goto Fail;
2423 }
2424 n = PySequence_Fast_GET_SIZE(fast);
2425 if (n != 2) {
2426 PyErr_Format(PyExc_ValueError,
2427 "dictionary update sequence element #%zd "
2428 "has length %zd; 2 is required",
2429 i, n);
2430 goto Fail;
2431 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002433 /* Update/merge with this (key, value) pair. */
2434 key = PySequence_Fast_GET_ITEM(fast, 0);
2435 value = PySequence_Fast_GET_ITEM(fast, 1);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002436 Py_INCREF(key);
2437 Py_INCREF(value);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002438 if (override) {
2439 if (PyDict_SetItem(d, key, value) < 0) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002440 Py_DECREF(key);
2441 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002442 goto Fail;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002443 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002444 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002445 else if (PyDict_GetItemWithError(d, key) == NULL) {
2446 if (PyErr_Occurred() || PyDict_SetItem(d, key, value) < 0) {
2447 Py_DECREF(key);
2448 Py_DECREF(value);
2449 goto Fail;
2450 }
2451 }
2452
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002453 Py_DECREF(key);
2454 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002455 Py_DECREF(fast);
2456 Py_DECREF(item);
2457 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002458
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002459 i = 0;
Victor Stinner0fc91ee2019-04-12 21:51:34 +02002460 ASSERT_CONSISTENT(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002461 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00002462Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002463 Py_XDECREF(item);
2464 Py_XDECREF(fast);
2465 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002466Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002467 Py_DECREF(it);
2468 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00002469}
2470
doko@ubuntu.comc96df682016-10-11 08:04:02 +02002471static int
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002472dict_merge(PyObject *a, PyObject *b, int override)
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002473{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002474 PyDictObject *mp, *other;
2475 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -07002476 PyDictKeyEntry *entry, *ep0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002477
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002478 assert(0 <= override && override <= 2);
2479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002480 /* We accept for the argument either a concrete dictionary object,
2481 * or an abstract "mapping" object. For the former, we can do
2482 * things quite efficiently. For the latter, we only require that
2483 * PyMapping_Keys() and PyObject_GetItem() be supported.
2484 */
2485 if (a == NULL || !PyDict_Check(a) || b == NULL) {
2486 PyErr_BadInternalCall();
2487 return -1;
2488 }
2489 mp = (PyDictObject*)a;
INADA Naoki2aaf98c2018-09-26 12:59:00 +09002490 if (PyDict_Check(b) && (Py_TYPE(b)->tp_iter == (getiterfunc)dict_iter)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002491 other = (PyDictObject*)b;
2492 if (other == mp || other->ma_used == 0)
2493 /* a.update(a) or a.update({}); nothing to do */
2494 return 0;
2495 if (mp->ma_used == 0)
2496 /* Since the target dict is empty, PyDict_GetItem()
2497 * always returns NULL. Setting override to 1
2498 * skips the unnecessary test.
2499 */
2500 override = 1;
2501 /* Do one big resize at the start, rather than
2502 * incrementally resizing as we insert new items. Expect
2503 * that there will be no (or few) overlapping keys.
2504 */
INADA Naokib1152be2016-10-27 19:26:50 +09002505 if (USABLE_FRACTION(mp->ma_keys->dk_size) < other->ma_used) {
2506 if (dictresize(mp, ESTIMATE_SIZE(mp->ma_used + other->ma_used))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002507 return -1;
INADA Naokib1152be2016-10-27 19:26:50 +09002508 }
2509 }
Victor Stinner742da042016-09-07 17:40:12 -07002510 ep0 = DK_ENTRIES(other->ma_keys);
2511 for (i = 0, n = other->ma_keys->dk_nentries; i < n; i++) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002512 PyObject *key, *value;
2513 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002514 entry = &ep0[i];
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002515 key = entry->me_key;
2516 hash = entry->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002517 if (other->ma_values)
2518 value = other->ma_values[i];
2519 else
2520 value = entry->me_value;
2521
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002522 if (value != NULL) {
2523 int err = 0;
2524 Py_INCREF(key);
2525 Py_INCREF(value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002526 if (override == 1)
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002527 err = insertdict(mp, key, hash, value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002528 else if (_PyDict_GetItem_KnownHash(a, key, hash) == NULL) {
2529 if (PyErr_Occurred()) {
2530 Py_DECREF(value);
2531 Py_DECREF(key);
2532 return -1;
2533 }
2534 err = insertdict(mp, key, hash, value);
2535 }
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002536 else if (override != 0) {
2537 _PyErr_SetKeyError(key);
2538 Py_DECREF(value);
2539 Py_DECREF(key);
2540 return -1;
2541 }
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002542 Py_DECREF(value);
2543 Py_DECREF(key);
2544 if (err != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002545 return -1;
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002546
Victor Stinner742da042016-09-07 17:40:12 -07002547 if (n != other->ma_keys->dk_nentries) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002548 PyErr_SetString(PyExc_RuntimeError,
2549 "dict mutated during update");
2550 return -1;
2551 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002552 }
2553 }
2554 }
2555 else {
2556 /* Do it the generic, slower way */
2557 PyObject *keys = PyMapping_Keys(b);
2558 PyObject *iter;
2559 PyObject *key, *value;
2560 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002562 if (keys == NULL)
2563 /* Docstring says this is equivalent to E.keys() so
2564 * if E doesn't have a .keys() method we want
2565 * AttributeError to percolate up. Might as well
2566 * do the same for any other error.
2567 */
2568 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002570 iter = PyObject_GetIter(keys);
2571 Py_DECREF(keys);
2572 if (iter == NULL)
2573 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002575 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002576 if (override != 1) {
2577 if (PyDict_GetItemWithError(a, key) != NULL) {
2578 if (override != 0) {
2579 _PyErr_SetKeyError(key);
2580 Py_DECREF(key);
2581 Py_DECREF(iter);
2582 return -1;
2583 }
2584 Py_DECREF(key);
2585 continue;
2586 }
2587 else if (PyErr_Occurred()) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002588 Py_DECREF(key);
2589 Py_DECREF(iter);
2590 return -1;
2591 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002592 }
2593 value = PyObject_GetItem(b, key);
2594 if (value == NULL) {
2595 Py_DECREF(iter);
2596 Py_DECREF(key);
2597 return -1;
2598 }
2599 status = PyDict_SetItem(a, key, value);
2600 Py_DECREF(key);
2601 Py_DECREF(value);
2602 if (status < 0) {
2603 Py_DECREF(iter);
2604 return -1;
2605 }
2606 }
2607 Py_DECREF(iter);
2608 if (PyErr_Occurred())
2609 /* Iterator completed, via error */
2610 return -1;
2611 }
Victor Stinner0fc91ee2019-04-12 21:51:34 +02002612 ASSERT_CONSISTENT(a);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002613 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002614}
2615
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002616int
2617PyDict_Update(PyObject *a, PyObject *b)
2618{
2619 return dict_merge(a, b, 1);
2620}
2621
2622int
2623PyDict_Merge(PyObject *a, PyObject *b, int override)
2624{
2625 /* XXX Deprecate override not in (0, 1). */
2626 return dict_merge(a, b, override != 0);
2627}
2628
2629int
2630_PyDict_MergeEx(PyObject *a, PyObject *b, int override)
2631{
2632 return dict_merge(a, b, override);
2633}
2634
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002635static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302636dict_copy(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002637{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002638 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002639}
2640
2641PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002642PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002643{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002644 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002645 PyDictObject *mp;
2646 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002648 if (o == NULL || !PyDict_Check(o)) {
2649 PyErr_BadInternalCall();
2650 return NULL;
2651 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002652
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002653 mp = (PyDictObject *)o;
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002654 if (mp->ma_used == 0) {
2655 /* The dict is empty; just return a new dict. */
2656 return PyDict_New();
2657 }
2658
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002659 if (_PyDict_HasSplitTable(mp)) {
2660 PyDictObject *split_copy;
Victor Stinner742da042016-09-07 17:40:12 -07002661 Py_ssize_t size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
2662 PyObject **newvalues;
2663 newvalues = new_values(size);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002664 if (newvalues == NULL)
2665 return PyErr_NoMemory();
2666 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2667 if (split_copy == NULL) {
2668 free_values(newvalues);
2669 return NULL;
2670 }
2671 split_copy->ma_values = newvalues;
2672 split_copy->ma_keys = mp->ma_keys;
2673 split_copy->ma_used = mp->ma_used;
INADA Naokid1c82c52018-04-03 11:43:53 +09002674 split_copy->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokia7576492018-11-14 18:39:27 +09002675 dictkeys_incref(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002676 for (i = 0, n = size; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002677 PyObject *value = mp->ma_values[i];
2678 Py_XINCREF(value);
2679 split_copy->ma_values[i] = value;
2680 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002681 if (_PyObject_GC_IS_TRACKED(mp))
2682 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002683 return (PyObject *)split_copy;
2684 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002685
2686 if (PyDict_CheckExact(mp) && mp->ma_values == NULL &&
2687 (mp->ma_used >= (mp->ma_keys->dk_nentries * 2) / 3))
2688 {
2689 /* Use fast-copy if:
2690
2691 (1) 'mp' is an instance of a subclassed dict; and
2692
2693 (2) 'mp' is not a split-dict; and
2694
2695 (3) if 'mp' is non-compact ('del' operation does not resize dicts),
2696 do fast-copy only if it has at most 1/3 non-used keys.
2697
Ville Skyttä61f82e02018-04-20 23:08:45 +03002698 The last condition (3) is important to guard against a pathological
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002699 case when a large dict is almost emptied with multiple del/pop
2700 operations and copied after that. In cases like this, we defer to
2701 PyDict_Merge, which produces a compacted copy.
2702 */
2703 return clone_combined_dict(mp);
2704 }
2705
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002706 copy = PyDict_New();
2707 if (copy == NULL)
2708 return NULL;
2709 if (PyDict_Merge(copy, o, 1) == 0)
2710 return copy;
2711 Py_DECREF(copy);
2712 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002713}
2714
Martin v. Löwis18e16552006-02-15 17:27:45 +00002715Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002716PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002717{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002718 if (mp == NULL || !PyDict_Check(mp)) {
2719 PyErr_BadInternalCall();
2720 return -1;
2721 }
2722 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002723}
2724
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002725PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002726PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002727{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002728 if (mp == NULL || !PyDict_Check(mp)) {
2729 PyErr_BadInternalCall();
2730 return NULL;
2731 }
2732 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002733}
2734
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002735PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002736PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002737{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002738 if (mp == NULL || !PyDict_Check(mp)) {
2739 PyErr_BadInternalCall();
2740 return NULL;
2741 }
2742 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002743}
2744
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002745PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002746PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002747{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002748 if (mp == NULL || !PyDict_Check(mp)) {
2749 PyErr_BadInternalCall();
2750 return NULL;
2751 }
2752 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002753}
2754
Tim Peterse63415e2001-05-08 04:38:29 +00002755/* Return 1 if dicts equal, 0 if not, -1 if error.
2756 * Gets out as soon as any difference is detected.
2757 * Uses only Py_EQ comparison.
2758 */
2759static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002760dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002761{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002762 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002764 if (a->ma_used != b->ma_used)
2765 /* can't be equal if # of entries differ */
2766 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002767 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Victor Stinner742da042016-09-07 17:40:12 -07002768 for (i = 0; i < a->ma_keys->dk_nentries; i++) {
2769 PyDictKeyEntry *ep = &DK_ENTRIES(a->ma_keys)[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002770 PyObject *aval;
2771 if (a->ma_values)
2772 aval = a->ma_values[i];
2773 else
2774 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002775 if (aval != NULL) {
2776 int cmp;
2777 PyObject *bval;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002778 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002779 /* temporarily bump aval's refcount to ensure it stays
2780 alive until we're done with it */
2781 Py_INCREF(aval);
2782 /* ditto for key */
2783 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002784 /* reuse the known hash value */
INADA Naoki778928b2017-08-03 23:45:15 +09002785 b->ma_keys->dk_lookup(b, key, ep->me_hash, &bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002786 if (bval == NULL) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002787 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002788 Py_DECREF(aval);
2789 if (PyErr_Occurred())
2790 return -1;
2791 return 0;
2792 }
Dong-hee Na2d5bf562019-12-31 10:04:22 +09002793 Py_INCREF(bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002794 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002795 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002796 Py_DECREF(aval);
Dong-hee Na2d5bf562019-12-31 10:04:22 +09002797 Py_DECREF(bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002798 if (cmp <= 0) /* error or not equal */
2799 return cmp;
2800 }
2801 }
2802 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002803}
Tim Peterse63415e2001-05-08 04:38:29 +00002804
2805static PyObject *
2806dict_richcompare(PyObject *v, PyObject *w, int op)
2807{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002808 int cmp;
2809 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002810
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002811 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2812 res = Py_NotImplemented;
2813 }
2814 else if (op == Py_EQ || op == Py_NE) {
2815 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2816 if (cmp < 0)
2817 return NULL;
2818 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2819 }
2820 else
2821 res = Py_NotImplemented;
2822 Py_INCREF(res);
2823 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002824}
Tim Peterse63415e2001-05-08 04:38:29 +00002825
Larry Hastings61272b72014-01-07 12:41:53 -08002826/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002827
2828@coexist
2829dict.__contains__
2830
2831 key: object
2832 /
2833
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002834True if the dictionary has the specified key, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002835[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002836
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002837static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002838dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka19d25972017-02-04 08:05:07 +02002839/*[clinic end generated code: output=a3d03db709ed6e6b input=fe1cb42ad831e820]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002840{
Larry Hastingsc2047262014-01-25 20:43:29 -08002841 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002842 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002843 Py_ssize_t ix;
INADA Naokiba609772016-12-07 20:41:42 +09002844 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002846 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002847 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002848 hash = PyObject_Hash(key);
2849 if (hash == -1)
2850 return NULL;
2851 }
INADA Naoki778928b2017-08-03 23:45:15 +09002852 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002853 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002854 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002855 if (ix == DKIX_EMPTY || value == NULL)
Victor Stinner742da042016-09-07 17:40:12 -07002856 Py_RETURN_FALSE;
2857 Py_RETURN_TRUE;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002858}
2859
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002860/*[clinic input]
2861dict.get
2862
2863 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002864 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002865 /
2866
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002867Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002868[clinic start generated code]*/
2869
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002870static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002871dict_get_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002872/*[clinic end generated code: output=bba707729dee05bf input=279ddb5790b6b107]*/
Barry Warsawc38c5da1997-10-06 17:49:20 +00002873{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002875 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002876 Py_ssize_t ix;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002878 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002879 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002880 hash = PyObject_Hash(key);
2881 if (hash == -1)
2882 return NULL;
2883 }
INADA Naoki778928b2017-08-03 23:45:15 +09002884 ix = (self->ma_keys->dk_lookup) (self, key, hash, &val);
Victor Stinner742da042016-09-07 17:40:12 -07002885 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002886 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002887 if (ix == DKIX_EMPTY || val == NULL) {
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002888 val = default_value;
INADA Naokiba609772016-12-07 20:41:42 +09002889 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002890 Py_INCREF(val);
2891 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002892}
2893
Benjamin Peterson00e98862013-03-07 22:16:29 -05002894PyObject *
2895PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002896{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002897 PyDictObject *mp = (PyDictObject *)d;
INADA Naoki93f26f72016-11-02 18:45:16 +09002898 PyObject *value;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002899 Py_hash_t hash;
Guido van Rossum164452c2000-08-08 16:12:54 +00002900
Benjamin Peterson00e98862013-03-07 22:16:29 -05002901 if (!PyDict_Check(d)) {
2902 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002903 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002904 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002905
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002906 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002907 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002908 hash = PyObject_Hash(key);
2909 if (hash == -1)
2910 return NULL;
2911 }
Inada Naoki2ddc7f62019-03-18 20:38:33 +09002912 if (mp->ma_keys == Py_EMPTY_KEYS) {
2913 if (insert_to_emptydict(mp, key, hash, defaultobj) < 0) {
2914 return NULL;
2915 }
2916 return defaultobj;
2917 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002918
2919 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
2920 if (insertion_resize(mp) < 0)
2921 return NULL;
2922 }
2923
INADA Naoki778928b2017-08-03 23:45:15 +09002924 Py_ssize_t ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002925 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002926 return NULL;
INADA Naoki93f26f72016-11-02 18:45:16 +09002927
2928 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09002929 ((ix >= 0 && value == NULL && mp->ma_used != ix) ||
INADA Naoki93f26f72016-11-02 18:45:16 +09002930 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
2931 if (insertion_resize(mp) < 0) {
2932 return NULL;
2933 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002934 ix = DKIX_EMPTY;
2935 }
2936
2937 if (ix == DKIX_EMPTY) {
2938 PyDictKeyEntry *ep, *ep0;
2939 value = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002940 if (mp->ma_keys->dk_usable <= 0) {
Victor Stinner3c336c52016-09-12 14:17:40 +02002941 if (insertion_resize(mp) < 0) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002942 return NULL;
Victor Stinner3c336c52016-09-12 14:17:40 +02002943 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002944 }
INADA Naoki778928b2017-08-03 23:45:15 +09002945 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naoki93f26f72016-11-02 18:45:16 +09002946 ep0 = DK_ENTRIES(mp->ma_keys);
2947 ep = &ep0[mp->ma_keys->dk_nentries];
INADA Naokia7576492018-11-14 18:39:27 +09002948 dictkeys_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002949 Py_INCREF(key);
INADA Naoki93f26f72016-11-02 18:45:16 +09002950 Py_INCREF(value);
2951 MAINTAIN_TRACKING(mp, key, value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002952 ep->me_key = key;
2953 ep->me_hash = hash;
INADA Naokiba609772016-12-07 20:41:42 +09002954 if (_PyDict_HasSplitTable(mp)) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002955 assert(mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
2956 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Victor Stinner742da042016-09-07 17:40:12 -07002957 }
2958 else {
INADA Naoki93f26f72016-11-02 18:45:16 +09002959 ep->me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07002960 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002961 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002962 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naoki93f26f72016-11-02 18:45:16 +09002963 mp->ma_keys->dk_usable--;
2964 mp->ma_keys->dk_nentries++;
2965 assert(mp->ma_keys->dk_usable >= 0);
2966 }
INADA Naokiba609772016-12-07 20:41:42 +09002967 else if (value == NULL) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002968 value = defaultobj;
2969 assert(_PyDict_HasSplitTable(mp));
2970 assert(ix == mp->ma_used);
2971 Py_INCREF(value);
2972 MAINTAIN_TRACKING(mp, key, value);
INADA Naokiba609772016-12-07 20:41:42 +09002973 mp->ma_values[ix] = value;
INADA Naoki93f26f72016-11-02 18:45:16 +09002974 mp->ma_used++;
2975 mp->ma_version_tag = DICT_NEXT_VERSION();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002976 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002977
Victor Stinner0fc91ee2019-04-12 21:51:34 +02002978 ASSERT_CONSISTENT(mp);
INADA Naoki93f26f72016-11-02 18:45:16 +09002979 return value;
Guido van Rossum164452c2000-08-08 16:12:54 +00002980}
2981
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002982/*[clinic input]
2983dict.setdefault
2984
2985 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002986 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002987 /
2988
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002989Insert key with a value of default if key is not in the dictionary.
2990
2991Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002992[clinic start generated code]*/
2993
Benjamin Peterson00e98862013-03-07 22:16:29 -05002994static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002995dict_setdefault_impl(PyDictObject *self, PyObject *key,
2996 PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002997/*[clinic end generated code: output=f8c1101ebf69e220 input=0f063756e815fd9d]*/
Benjamin Peterson00e98862013-03-07 22:16:29 -05002998{
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002999 PyObject *val;
Benjamin Peterson00e98862013-03-07 22:16:29 -05003000
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02003001 val = PyDict_SetDefault((PyObject *)self, key, default_value);
Benjamin Peterson00e98862013-03-07 22:16:29 -05003002 Py_XINCREF(val);
3003 return val;
3004}
Guido van Rossum164452c2000-08-08 16:12:54 +00003005
3006static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303007dict_clear(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00003008{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003009 PyDict_Clear((PyObject *)mp);
3010 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00003011}
3012
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003013/*[clinic input]
3014dict.pop
3015
3016 key: object
3017 default: object = NULL
3018 /
3019
Serhiy Storchaka279f4462019-09-14 12:24:05 +03003020D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003021
3022If key is not found, default is returned if given, otherwise KeyError is raised
3023[clinic start generated code]*/
3024
Guido van Rossumba6ab842000-12-12 22:02:18 +00003025static PyObject *
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003026dict_pop_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
Serhiy Storchaka279f4462019-09-14 12:24:05 +03003027/*[clinic end generated code: output=3abb47b89f24c21c input=eeebec7812190348]*/
Guido van Rossume027d982002-04-12 15:11:59 +00003028{
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003029 return _PyDict_Pop((PyObject*)self, key, default_value);
Guido van Rossume027d982002-04-12 15:11:59 +00003030}
3031
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003032/*[clinic input]
3033dict.popitem
3034
3035Remove and return a (key, value) pair as a 2-tuple.
3036
3037Pairs are returned in LIFO (last-in, first-out) order.
3038Raises KeyError if the dict is empty.
3039[clinic start generated code]*/
3040
Guido van Rossume027d982002-04-12 15:11:59 +00003041static PyObject *
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003042dict_popitem_impl(PyDictObject *self)
3043/*[clinic end generated code: output=e65fcb04420d230d input=1c38a49f21f64941]*/
Guido van Rossumba6ab842000-12-12 22:02:18 +00003044{
Victor Stinner742da042016-09-07 17:40:12 -07003045 Py_ssize_t i, j;
3046 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003047 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00003048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003049 /* Allocate the result tuple before checking the size. Believe it
3050 * or not, this allocation could trigger a garbage collection which
3051 * could empty the dict, so if we checked the size first and that
3052 * happened, the result would be an infinite loop (searching for an
3053 * entry that no longer exists). Note that the usual popitem()
3054 * idiom is "while d: k, v = d.popitem()". so needing to throw the
3055 * tuple away if the dict *is* empty isn't a significant
3056 * inefficiency -- possible, but unlikely in practice.
3057 */
3058 res = PyTuple_New(2);
3059 if (res == NULL)
3060 return NULL;
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003061 if (self->ma_used == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003062 Py_DECREF(res);
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003063 PyErr_SetString(PyExc_KeyError, "popitem(): dictionary is empty");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003064 return NULL;
3065 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003066 /* Convert split table to combined table */
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003067 if (self->ma_keys->dk_lookup == lookdict_split) {
3068 if (dictresize(self, DK_SIZE(self->ma_keys))) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003069 Py_DECREF(res);
3070 return NULL;
3071 }
3072 }
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003073 ENSURE_ALLOWS_DELETIONS(self);
Victor Stinner742da042016-09-07 17:40:12 -07003074
3075 /* Pop last item */
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003076 ep0 = DK_ENTRIES(self->ma_keys);
3077 i = self->ma_keys->dk_nentries - 1;
Victor Stinner742da042016-09-07 17:40:12 -07003078 while (i >= 0 && ep0[i].me_value == NULL) {
3079 i--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003080 }
Victor Stinner742da042016-09-07 17:40:12 -07003081 assert(i >= 0);
3082
3083 ep = &ep0[i];
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003084 j = lookdict_index(self->ma_keys, ep->me_hash, i);
Victor Stinner742da042016-09-07 17:40:12 -07003085 assert(j >= 0);
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003086 assert(dictkeys_get_index(self->ma_keys, j) == i);
3087 dictkeys_set_index(self->ma_keys, j, DKIX_DUMMY);
Victor Stinner742da042016-09-07 17:40:12 -07003088
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003089 PyTuple_SET_ITEM(res, 0, ep->me_key);
3090 PyTuple_SET_ITEM(res, 1, ep->me_value);
Victor Stinner742da042016-09-07 17:40:12 -07003091 ep->me_key = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003092 ep->me_value = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07003093 /* We can't dk_usable++ since there is DKIX_DUMMY in indices */
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003094 self->ma_keys->dk_nentries = i;
3095 self->ma_used--;
3096 self->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner0fc91ee2019-04-12 21:51:34 +02003097 ASSERT_CONSISTENT(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003098 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00003099}
3100
Jeremy Hylton8caad492000-06-23 14:18:11 +00003101static int
3102dict_traverse(PyObject *op, visitproc visit, void *arg)
3103{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003104 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson55f44522016-09-05 12:12:59 -07003105 PyDictKeysObject *keys = mp->ma_keys;
Serhiy Storchaka46825d22016-09-26 21:29:34 +03003106 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Victor Stinner742da042016-09-07 17:40:12 -07003107 Py_ssize_t i, n = keys->dk_nentries;
3108
Benjamin Peterson55f44522016-09-05 12:12:59 -07003109 if (keys->dk_lookup == lookdict) {
3110 for (i = 0; i < n; i++) {
3111 if (entries[i].me_value != NULL) {
3112 Py_VISIT(entries[i].me_value);
3113 Py_VISIT(entries[i].me_key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003114 }
3115 }
Victor Stinner742da042016-09-07 17:40:12 -07003116 }
3117 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003118 if (mp->ma_values != NULL) {
Benjamin Peterson55f44522016-09-05 12:12:59 -07003119 for (i = 0; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003120 Py_VISIT(mp->ma_values[i]);
3121 }
3122 }
3123 else {
Benjamin Peterson55f44522016-09-05 12:12:59 -07003124 for (i = 0; i < n; i++) {
3125 Py_VISIT(entries[i].me_value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003126 }
3127 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003128 }
3129 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00003130}
3131
3132static int
3133dict_tp_clear(PyObject *op)
3134{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003135 PyDict_Clear(op);
3136 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00003137}
3138
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003139static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003140
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003141Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003142_PyDict_SizeOf(PyDictObject *mp)
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003143{
Victor Stinner742da042016-09-07 17:40:12 -07003144 Py_ssize_t size, usable, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003145
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003146 size = DK_SIZE(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07003147 usable = USABLE_FRACTION(size);
3148
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02003149 res = _PyObject_SIZE(Py_TYPE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003150 if (mp->ma_values)
Victor Stinner742da042016-09-07 17:40:12 -07003151 res += usable * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003152 /* If the dictionary is split, the keys portion is accounted-for
3153 in the type object. */
3154 if (mp->ma_keys->dk_refcnt == 1)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003155 res += (sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003156 + DK_IXSIZE(mp->ma_keys) * size
3157 + sizeof(PyDictKeyEntry) * usable);
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003158 return res;
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003159}
3160
3161Py_ssize_t
3162_PyDict_KeysSize(PyDictKeysObject *keys)
3163{
Victor Stinner98ee9d52016-09-08 09:33:56 -07003164 return (sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003165 + DK_IXSIZE(keys) * DK_SIZE(keys)
3166 + USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry));
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003167}
3168
doko@ubuntu.com17210f52016-01-14 14:04:59 +01003169static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303170dict_sizeof(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003171{
3172 return PyLong_FromSsize_t(_PyDict_SizeOf(mp));
3173}
3174
Brandt Buchereb8ac572020-02-24 19:47:34 -08003175static PyObject *
3176dict_or(PyObject *self, PyObject *other)
3177{
3178 if (!PyDict_Check(self) || !PyDict_Check(other)) {
3179 Py_RETURN_NOTIMPLEMENTED;
3180 }
3181 PyObject *new = PyDict_Copy(self);
3182 if (new == NULL) {
3183 return NULL;
3184 }
3185 if (dict_update_arg(new, other)) {
3186 Py_DECREF(new);
3187 return NULL;
3188 }
3189 return new;
3190}
3191
3192static PyObject *
3193dict_ior(PyObject *self, PyObject *other)
3194{
3195 if (dict_update_arg(self, other)) {
3196 return NULL;
3197 }
3198 Py_INCREF(self);
3199 return self;
3200}
3201
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00003202PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
3203
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003204PyDoc_STRVAR(sizeof__doc__,
3205"D.__sizeof__() -> size of D in memory, in bytes");
3206
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003207PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04003208"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
3209If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
3210If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
3211In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00003212
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003213PyDoc_STRVAR(clear__doc__,
3214"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003215
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003216PyDoc_STRVAR(copy__doc__,
3217"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00003218
Guido van Rossumb90c8482007-02-10 01:11:45 +00003219/* Forward */
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303220static PyObject *dictkeys_new(PyObject *, PyObject *);
3221static PyObject *dictitems_new(PyObject *, PyObject *);
3222static PyObject *dictvalues_new(PyObject *, PyObject *);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003223
Guido van Rossum45c85d12007-07-27 16:31:40 +00003224PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003225 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003226PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003227 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003228PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003229 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00003230
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003231static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07003232 DICT___CONTAINS___METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003233 {"__getitem__", (PyCFunction)(void(*)(void))dict_subscript, METH_O | METH_COEXIST,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003234 getitem__doc__},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003235 {"__sizeof__", (PyCFunction)(void(*)(void))dict_sizeof, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003236 sizeof__doc__},
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003237 DICT_GET_METHODDEF
3238 DICT_SETDEFAULT_METHODDEF
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003239 DICT_POP_METHODDEF
3240 DICT_POPITEM_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303241 {"keys", dictkeys_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003242 keys__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303243 {"items", dictitems_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003244 items__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303245 {"values", dictvalues_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003246 values__doc__},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003247 {"update", (PyCFunction)(void(*)(void))dict_update, METH_VARARGS | METH_KEYWORDS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003248 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08003249 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003250 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
3251 clear__doc__},
3252 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
3253 copy__doc__},
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003254 DICT___REVERSED___METHODDEF
Guido van Rossum48b069a2020-04-07 09:50:06 -07003255 {"__class_getitem__", (PyCFunction)Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003256 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003257};
3258
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00003259/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00003260int
3261PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003262{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003263 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07003264 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003265 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003266 PyObject *value;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003268 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003269 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003270 hash = PyObject_Hash(key);
3271 if (hash == -1)
3272 return -1;
3273 }
INADA Naoki778928b2017-08-03 23:45:15 +09003274 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003275 if (ix == DKIX_ERROR)
3276 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003277 return (ix != DKIX_EMPTY && value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003278}
3279
Thomas Wouterscf297e42007-02-23 15:07:44 +00003280/* Internal version of PyDict_Contains used when the hash value is already known */
3281int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003282_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00003283{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003284 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003285 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -07003286 Py_ssize_t ix;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003287
INADA Naoki778928b2017-08-03 23:45:15 +09003288 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003289 if (ix == DKIX_ERROR)
3290 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003291 return (ix != DKIX_EMPTY && value != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00003292}
3293
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003294/* Hack to implement "key in dict" */
3295static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003296 0, /* sq_length */
3297 0, /* sq_concat */
3298 0, /* sq_repeat */
3299 0, /* sq_item */
3300 0, /* sq_slice */
3301 0, /* sq_ass_item */
3302 0, /* sq_ass_slice */
3303 PyDict_Contains, /* sq_contains */
3304 0, /* sq_inplace_concat */
3305 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003306};
3307
Brandt Buchereb8ac572020-02-24 19:47:34 -08003308static PyNumberMethods dict_as_number = {
3309 .nb_or = dict_or,
3310 .nb_inplace_or = dict_ior,
3311};
3312
Guido van Rossum09e563a2001-05-01 12:10:21 +00003313static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003314dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3315{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003316 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003317 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003319 assert(type != NULL && type->tp_alloc != NULL);
3320 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02003321 if (self == NULL)
3322 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003323 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003324
Victor Stinnera9f61a52013-07-16 22:17:26 +02003325 /* The object has been implicitly tracked by tp_alloc */
3326 if (type == &PyDict_Type)
3327 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003328
3329 d->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003330 d->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07003331 d->ma_keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003332 if (d->ma_keys == NULL) {
3333 Py_DECREF(self);
3334 return NULL;
3335 }
Victor Stinner0fc91ee2019-04-12 21:51:34 +02003336 ASSERT_CONSISTENT(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003337 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003338}
3339
Tim Peters25786c02001-09-02 08:22:48 +00003340static int
3341dict_init(PyObject *self, PyObject *args, PyObject *kwds)
3342{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003343 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00003344}
3345
Tim Peters6d6c1a32001-08-02 04:15:00 +00003346static PyObject *
Dong-hee Nae27916b2020-04-02 09:55:43 +09003347dict_vectorcall(PyObject *type, PyObject * const*args,
3348 size_t nargsf, PyObject *kwnames)
3349{
3350 assert(PyType_Check(type));
3351 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
3352 if (!_PyArg_CheckPositional("dict", nargs, 0, 1)) {
3353 return NULL;
3354 }
3355
3356 PyObject *self = dict_new((PyTypeObject *)type, NULL, NULL);
3357 if (self == NULL) {
3358 return NULL;
3359 }
3360 if (nargs == 1) {
3361 if (dict_update_arg(self, args[0]) < 0) {
3362 Py_DECREF(self);
3363 return NULL;
3364 }
3365 args++;
3366 }
3367 if (kwnames != NULL) {
3368 for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(kwnames); i++) {
3369 if (PyDict_SetItem(self, PyTuple_GET_ITEM(kwnames, i), args[i]) < 0) {
3370 Py_DECREF(self);
3371 return NULL;
3372 }
3373 }
3374 }
3375 return self;
3376}
3377
3378static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003379dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00003380{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003381 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003382}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003383
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003384PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00003385"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00003386"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003387" (key, value) pairs\n"
3388"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00003389" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003390" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00003391" d[k] = v\n"
3392"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
3393" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00003394
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003395PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003396 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3397 "dict",
3398 sizeof(PyDictObject),
3399 0,
3400 (destructor)dict_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003401 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003402 0, /* tp_getattr */
3403 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003404 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003405 (reprfunc)dict_repr, /* tp_repr */
Brandt Buchereb8ac572020-02-24 19:47:34 -08003406 &dict_as_number, /* tp_as_number */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003407 &dict_as_sequence, /* tp_as_sequence */
3408 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003409 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003410 0, /* tp_call */
3411 0, /* tp_str */
3412 PyObject_GenericGetAttr, /* tp_getattro */
3413 0, /* tp_setattro */
3414 0, /* tp_as_buffer */
3415 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3416 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
3417 dictionary_doc, /* tp_doc */
3418 dict_traverse, /* tp_traverse */
3419 dict_tp_clear, /* tp_clear */
3420 dict_richcompare, /* tp_richcompare */
3421 0, /* tp_weaklistoffset */
3422 (getiterfunc)dict_iter, /* tp_iter */
3423 0, /* tp_iternext */
3424 mapp_methods, /* tp_methods */
3425 0, /* tp_members */
3426 0, /* tp_getset */
3427 0, /* tp_base */
3428 0, /* tp_dict */
3429 0, /* tp_descr_get */
3430 0, /* tp_descr_set */
3431 0, /* tp_dictoffset */
3432 dict_init, /* tp_init */
3433 PyType_GenericAlloc, /* tp_alloc */
3434 dict_new, /* tp_new */
3435 PyObject_GC_Del, /* tp_free */
Dong-hee Nae27916b2020-04-02 09:55:43 +09003436 .tp_vectorcall = dict_vectorcall,
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003437};
3438
Victor Stinner3c1e4812012-03-26 22:10:51 +02003439PyObject *
3440_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
3441{
3442 PyObject *kv;
3443 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02003444 if (kv == NULL) {
3445 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003446 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02003447 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02003448 return PyDict_GetItem(dp, kv);
3449}
3450
Guido van Rossum3cca2451997-05-16 14:23:33 +00003451/* For backward compatibility with old dictionary interface */
3452
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003453PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003454PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003455{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003456 PyObject *kv, *rv;
3457 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003458 if (kv == NULL) {
3459 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003460 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003461 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003462 rv = PyDict_GetItem(v, kv);
3463 Py_DECREF(kv);
3464 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003465}
3466
3467int
Victor Stinner3c1e4812012-03-26 22:10:51 +02003468_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
3469{
3470 PyObject *kv;
3471 kv = _PyUnicode_FromId(key); /* borrowed */
3472 if (kv == NULL)
3473 return -1;
3474 return PyDict_SetItem(v, kv, item);
3475}
3476
3477int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003478PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003479{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003480 PyObject *kv;
3481 int err;
3482 kv = PyUnicode_FromString(key);
3483 if (kv == NULL)
3484 return -1;
3485 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
3486 err = PyDict_SetItem(v, kv, item);
3487 Py_DECREF(kv);
3488 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003489}
3490
3491int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01003492_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
3493{
3494 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
3495 if (kv == NULL)
3496 return -1;
3497 return PyDict_DelItem(v, kv);
3498}
3499
3500int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003501PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003502{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003503 PyObject *kv;
3504 int err;
3505 kv = PyUnicode_FromString(key);
3506 if (kv == NULL)
3507 return -1;
3508 err = PyDict_DelItem(v, kv);
3509 Py_DECREF(kv);
3510 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003511}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003512
Raymond Hettinger019a1482004-03-18 02:41:19 +00003513/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003514
3515typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003516 PyObject_HEAD
3517 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
3518 Py_ssize_t di_used;
3519 Py_ssize_t di_pos;
3520 PyObject* di_result; /* reusable result tuple for iteritems */
3521 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003522} dictiterobject;
3523
3524static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003525dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003526{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003527 dictiterobject *di;
3528 di = PyObject_GC_New(dictiterobject, itertype);
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003529 if (di == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003530 return NULL;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003531 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003532 Py_INCREF(dict);
3533 di->di_dict = dict;
3534 di->di_used = dict->ma_used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003535 di->len = dict->ma_used;
Dong-hee Na24dc2f82019-10-20 05:01:08 +09003536 if (itertype == &PyDictRevIterKey_Type ||
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003537 itertype == &PyDictRevIterItem_Type ||
Dong-hee Na24dc2f82019-10-20 05:01:08 +09003538 itertype == &PyDictRevIterValue_Type) {
3539 if (dict->ma_values) {
3540 di->di_pos = dict->ma_used - 1;
3541 }
3542 else {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003543 di->di_pos = dict->ma_keys->dk_nentries - 1;
Dong-hee Na24dc2f82019-10-20 05:01:08 +09003544 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003545 }
3546 else {
3547 di->di_pos = 0;
3548 }
3549 if (itertype == &PyDictIterItem_Type ||
3550 itertype == &PyDictRevIterItem_Type) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003551 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
3552 if (di->di_result == NULL) {
3553 Py_DECREF(di);
3554 return NULL;
3555 }
3556 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003557 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003558 di->di_result = NULL;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003559 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003560 _PyObject_GC_TRACK(di);
3561 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003562}
3563
3564static void
3565dictiter_dealloc(dictiterobject *di)
3566{
INADA Naokia6296d32017-08-24 14:55:17 +09003567 /* bpo-31095: UnTrack is needed before calling any callbacks */
3568 _PyObject_GC_UNTRACK(di);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003569 Py_XDECREF(di->di_dict);
3570 Py_XDECREF(di->di_result);
3571 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003572}
3573
3574static int
3575dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
3576{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003577 Py_VISIT(di->di_dict);
3578 Py_VISIT(di->di_result);
3579 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003580}
3581
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003582static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303583dictiter_len(dictiterobject *di, PyObject *Py_UNUSED(ignored))
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003584{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003585 Py_ssize_t len = 0;
3586 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
3587 len = di->len;
3588 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003589}
3590
Guido van Rossumb90c8482007-02-10 01:11:45 +00003591PyDoc_STRVAR(length_hint_doc,
3592 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003593
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003594static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303595dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003596
3597PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3598
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003599static PyMethodDef dictiter_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003600 {"__length_hint__", (PyCFunction)(void(*)(void))dictiter_len, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003601 length_hint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003602 {"__reduce__", (PyCFunction)(void(*)(void))dictiter_reduce, METH_NOARGS,
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003603 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003604 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003605};
3606
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003607static PyObject*
3608dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00003609{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003610 PyObject *key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003611 Py_ssize_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003612 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003613 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00003614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003615 if (d == NULL)
3616 return NULL;
3617 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00003618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003619 if (di->di_used != d->ma_used) {
3620 PyErr_SetString(PyExc_RuntimeError,
3621 "dictionary changed size during iteration");
3622 di->di_used = -1; /* Make this state sticky */
3623 return NULL;
3624 }
Guido van Rossum2147df72002-07-16 20:30:22 +00003625
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003626 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003627 k = d->ma_keys;
INADA Naokica2d8be2016-11-04 16:59:10 +09003628 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003629 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003630 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003631 goto fail;
3632 key = DK_ENTRIES(k)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003633 assert(d->ma_values[i] != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003634 }
3635 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003636 Py_ssize_t n = k->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003637 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
3638 while (i < n && entry_ptr->me_value == NULL) {
3639 entry_ptr++;
3640 i++;
3641 }
3642 if (i >= n)
3643 goto fail;
3644 key = entry_ptr->me_key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003645 }
Thomas Perl796cc6e2019-03-28 07:03:25 +01003646 // We found an element (key), but did not expect it
3647 if (di->len == 0) {
3648 PyErr_SetString(PyExc_RuntimeError,
3649 "dictionary keys changed during iteration");
3650 goto fail;
3651 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003652 di->di_pos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003653 di->len--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003654 Py_INCREF(key);
3655 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003656
3657fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003658 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003659 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003660 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003661}
3662
Raymond Hettinger019a1482004-03-18 02:41:19 +00003663PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003664 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3665 "dict_keyiterator", /* tp_name */
3666 sizeof(dictiterobject), /* tp_basicsize */
3667 0, /* tp_itemsize */
3668 /* methods */
3669 (destructor)dictiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003670 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003671 0, /* tp_getattr */
3672 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003673 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003674 0, /* tp_repr */
3675 0, /* tp_as_number */
3676 0, /* tp_as_sequence */
3677 0, /* tp_as_mapping */
3678 0, /* tp_hash */
3679 0, /* tp_call */
3680 0, /* tp_str */
3681 PyObject_GenericGetAttr, /* tp_getattro */
3682 0, /* tp_setattro */
3683 0, /* tp_as_buffer */
3684 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3685 0, /* tp_doc */
3686 (traverseproc)dictiter_traverse, /* tp_traverse */
3687 0, /* tp_clear */
3688 0, /* tp_richcompare */
3689 0, /* tp_weaklistoffset */
3690 PyObject_SelfIter, /* tp_iter */
3691 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
3692 dictiter_methods, /* tp_methods */
3693 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003694};
3695
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003696static PyObject *
3697dictiter_iternextvalue(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003698{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003699 PyObject *value;
INADA Naokica2d8be2016-11-04 16:59:10 +09003700 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003701 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003703 if (d == NULL)
3704 return NULL;
3705 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003706
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003707 if (di->di_used != d->ma_used) {
3708 PyErr_SetString(PyExc_RuntimeError,
3709 "dictionary changed size during iteration");
3710 di->di_used = -1; /* Make this state sticky */
3711 return NULL;
3712 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003714 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003715 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003716 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003717 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003718 goto fail;
INADA Naokica2d8be2016-11-04 16:59:10 +09003719 value = d->ma_values[i];
3720 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003721 }
3722 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003723 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003724 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3725 while (i < n && entry_ptr->me_value == NULL) {
3726 entry_ptr++;
3727 i++;
3728 }
3729 if (i >= n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003730 goto fail;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003731 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003732 }
Thomas Perlb8311cf2019-04-02 11:30:10 +02003733 // We found an element, but did not expect it
3734 if (di->len == 0) {
3735 PyErr_SetString(PyExc_RuntimeError,
3736 "dictionary keys changed during iteration");
3737 goto fail;
3738 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003739 di->di_pos = i+1;
3740 di->len--;
3741 Py_INCREF(value);
3742 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003743
3744fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003745 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003746 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003747 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003748}
3749
3750PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003751 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3752 "dict_valueiterator", /* tp_name */
3753 sizeof(dictiterobject), /* tp_basicsize */
3754 0, /* tp_itemsize */
3755 /* methods */
3756 (destructor)dictiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003757 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003758 0, /* tp_getattr */
3759 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003760 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003761 0, /* tp_repr */
3762 0, /* tp_as_number */
3763 0, /* tp_as_sequence */
3764 0, /* tp_as_mapping */
3765 0, /* tp_hash */
3766 0, /* tp_call */
3767 0, /* tp_str */
3768 PyObject_GenericGetAttr, /* tp_getattro */
3769 0, /* tp_setattro */
3770 0, /* tp_as_buffer */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003771 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003772 0, /* tp_doc */
3773 (traverseproc)dictiter_traverse, /* tp_traverse */
3774 0, /* tp_clear */
3775 0, /* tp_richcompare */
3776 0, /* tp_weaklistoffset */
3777 PyObject_SelfIter, /* tp_iter */
3778 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3779 dictiter_methods, /* tp_methods */
3780 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003781};
3782
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003783static PyObject *
3784dictiter_iternextitem(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003785{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003786 PyObject *key, *value, *result;
INADA Naokica2d8be2016-11-04 16:59:10 +09003787 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003788 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003790 if (d == NULL)
3791 return NULL;
3792 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003794 if (di->di_used != d->ma_used) {
3795 PyErr_SetString(PyExc_RuntimeError,
3796 "dictionary changed size during iteration");
3797 di->di_used = -1; /* Make this state sticky */
3798 return NULL;
3799 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003801 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003802 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003803 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003804 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003805 goto fail;
3806 key = DK_ENTRIES(d->ma_keys)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003807 value = d->ma_values[i];
3808 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003809 }
3810 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003811 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003812 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3813 while (i < n && entry_ptr->me_value == NULL) {
3814 entry_ptr++;
3815 i++;
3816 }
3817 if (i >= n)
3818 goto fail;
3819 key = entry_ptr->me_key;
3820 value = entry_ptr->me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003821 }
Thomas Perlb8311cf2019-04-02 11:30:10 +02003822 // We found an element, but did not expect it
3823 if (di->len == 0) {
3824 PyErr_SetString(PyExc_RuntimeError,
3825 "dictionary keys changed during iteration");
3826 goto fail;
3827 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003828 di->di_pos = i+1;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003829 di->len--;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003830 Py_INCREF(key);
3831 Py_INCREF(value);
3832 result = di->di_result;
3833 if (Py_REFCNT(result) == 1) {
3834 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3835 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3836 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3837 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003838 Py_INCREF(result);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003839 Py_DECREF(oldkey);
3840 Py_DECREF(oldvalue);
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003841 }
3842 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003843 result = PyTuple_New(2);
3844 if (result == NULL)
3845 return NULL;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003846 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3847 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003848 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003849 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003850
3851fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003852 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003853 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003854 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003855}
3856
3857PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003858 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3859 "dict_itemiterator", /* tp_name */
3860 sizeof(dictiterobject), /* tp_basicsize */
3861 0, /* tp_itemsize */
3862 /* methods */
3863 (destructor)dictiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003864 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003865 0, /* tp_getattr */
3866 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003867 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003868 0, /* tp_repr */
3869 0, /* tp_as_number */
3870 0, /* tp_as_sequence */
3871 0, /* tp_as_mapping */
3872 0, /* tp_hash */
3873 0, /* tp_call */
3874 0, /* tp_str */
3875 PyObject_GenericGetAttr, /* tp_getattro */
3876 0, /* tp_setattro */
3877 0, /* tp_as_buffer */
3878 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3879 0, /* tp_doc */
3880 (traverseproc)dictiter_traverse, /* tp_traverse */
3881 0, /* tp_clear */
3882 0, /* tp_richcompare */
3883 0, /* tp_weaklistoffset */
3884 PyObject_SelfIter, /* tp_iter */
3885 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3886 dictiter_methods, /* tp_methods */
3887 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003888};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003889
3890
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003891/* dictreviter */
3892
3893static PyObject *
3894dictreviter_iternext(dictiterobject *di)
3895{
3896 PyDictObject *d = di->di_dict;
3897
3898 if (d == NULL) {
3899 return NULL;
3900 }
3901 assert (PyDict_Check(d));
3902
3903 if (di->di_used != d->ma_used) {
3904 PyErr_SetString(PyExc_RuntimeError,
3905 "dictionary changed size during iteration");
3906 di->di_used = -1; /* Make this state sticky */
3907 return NULL;
3908 }
3909
3910 Py_ssize_t i = di->di_pos;
3911 PyDictKeysObject *k = d->ma_keys;
3912 PyObject *key, *value, *result;
3913
Serhiy Storchaka2e3d8732019-10-23 14:48:08 +03003914 if (i < 0) {
3915 goto fail;
3916 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003917 if (d->ma_values) {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003918 key = DK_ENTRIES(k)[i].me_key;
3919 value = d->ma_values[i];
3920 assert (value != NULL);
3921 }
3922 else {
3923 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
Serhiy Storchaka2e3d8732019-10-23 14:48:08 +03003924 while (entry_ptr->me_value == NULL) {
3925 if (--i < 0) {
3926 goto fail;
3927 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003928 entry_ptr--;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003929 }
3930 key = entry_ptr->me_key;
3931 value = entry_ptr->me_value;
3932 }
3933 di->di_pos = i-1;
3934 di->len--;
3935
Dong-hee Na1b55b652020-02-17 19:09:15 +09003936 if (Py_IS_TYPE(di, &PyDictRevIterKey_Type)) {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003937 Py_INCREF(key);
3938 return key;
3939 }
Dong-hee Na1b55b652020-02-17 19:09:15 +09003940 else if (Py_IS_TYPE(di, &PyDictRevIterValue_Type)) {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003941 Py_INCREF(value);
3942 return value;
3943 }
Dong-hee Na1b55b652020-02-17 19:09:15 +09003944 else if (Py_IS_TYPE(di, &PyDictRevIterItem_Type)) {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003945 Py_INCREF(key);
3946 Py_INCREF(value);
3947 result = di->di_result;
3948 if (Py_REFCNT(result) == 1) {
3949 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3950 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3951 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3952 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
3953 Py_INCREF(result);
3954 Py_DECREF(oldkey);
3955 Py_DECREF(oldvalue);
3956 }
3957 else {
3958 result = PyTuple_New(2);
3959 if (result == NULL) {
3960 return NULL;
3961 }
3962 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3963 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
3964 }
3965 return result;
3966 }
3967 else {
3968 Py_UNREACHABLE();
3969 }
3970
3971fail:
3972 di->di_dict = NULL;
3973 Py_DECREF(d);
3974 return NULL;
3975}
3976
3977PyTypeObject PyDictRevIterKey_Type = {
3978 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3979 "dict_reversekeyiterator",
3980 sizeof(dictiterobject),
3981 .tp_dealloc = (destructor)dictiter_dealloc,
3982 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
3983 .tp_traverse = (traverseproc)dictiter_traverse,
3984 .tp_iter = PyObject_SelfIter,
3985 .tp_iternext = (iternextfunc)dictreviter_iternext,
3986 .tp_methods = dictiter_methods
3987};
3988
3989
3990/*[clinic input]
3991dict.__reversed__
3992
3993Return a reverse iterator over the dict keys.
3994[clinic start generated code]*/
3995
3996static PyObject *
3997dict___reversed___impl(PyDictObject *self)
3998/*[clinic end generated code: output=e674483336d1ed51 input=23210ef3477d8c4d]*/
3999{
4000 assert (PyDict_Check(self));
4001 return dictiter_new(self, &PyDictRevIterKey_Type);
4002}
4003
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004004static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304005dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004006{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004007 _Py_IDENTIFIER(iter);
Sergey Fedoseev63958442018-10-20 05:43:33 +05004008 /* copy the iterator state */
4009 dictiterobject tmp = *di;
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004010 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004011
Sergey Fedoseev63958442018-10-20 05:43:33 +05004012 PyObject *list = PySequence_List((PyObject*)&tmp);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004013 Py_XDECREF(tmp.di_dict);
Sergey Fedoseev63958442018-10-20 05:43:33 +05004014 if (list == NULL) {
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004015 return NULL;
4016 }
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004017 return Py_BuildValue("N(N)", _PyEval_GetBuiltinId(&PyId_iter), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004018}
4019
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004020PyTypeObject PyDictRevIterItem_Type = {
4021 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4022 "dict_reverseitemiterator",
4023 sizeof(dictiterobject),
4024 .tp_dealloc = (destructor)dictiter_dealloc,
4025 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
4026 .tp_traverse = (traverseproc)dictiter_traverse,
4027 .tp_iter = PyObject_SelfIter,
4028 .tp_iternext = (iternextfunc)dictreviter_iternext,
4029 .tp_methods = dictiter_methods
4030};
4031
4032PyTypeObject PyDictRevIterValue_Type = {
4033 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4034 "dict_reversevalueiterator",
4035 sizeof(dictiterobject),
4036 .tp_dealloc = (destructor)dictiter_dealloc,
4037 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
4038 .tp_traverse = (traverseproc)dictiter_traverse,
4039 .tp_iter = PyObject_SelfIter,
4040 .tp_iternext = (iternextfunc)dictreviter_iternext,
4041 .tp_methods = dictiter_methods
4042};
4043
Guido van Rossum3ac67412007-02-10 18:55:06 +00004044/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004045/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00004046/***********************************************/
4047
Guido van Rossumb90c8482007-02-10 01:11:45 +00004048/* The instance lay-out is the same for all three; but the type differs. */
4049
Guido van Rossumb90c8482007-02-10 01:11:45 +00004050static void
Eric Snow96c6af92015-05-29 22:21:39 -06004051dictview_dealloc(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004052{
INADA Naokia6296d32017-08-24 14:55:17 +09004053 /* bpo-31095: UnTrack is needed before calling any callbacks */
4054 _PyObject_GC_UNTRACK(dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004055 Py_XDECREF(dv->dv_dict);
4056 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00004057}
4058
4059static int
Eric Snow96c6af92015-05-29 22:21:39 -06004060dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
Antoine Pitrou7ddda782009-01-01 15:35:33 +00004061{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004062 Py_VISIT(dv->dv_dict);
4063 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004064}
4065
Guido van Rossum83825ac2007-02-10 04:54:19 +00004066static Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06004067dictview_len(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004068{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004069 Py_ssize_t len = 0;
4070 if (dv->dv_dict != NULL)
4071 len = dv->dv_dict->ma_used;
4072 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004073}
4074
Eric Snow96c6af92015-05-29 22:21:39 -06004075PyObject *
4076_PyDictView_New(PyObject *dict, PyTypeObject *type)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004077{
Eric Snow96c6af92015-05-29 22:21:39 -06004078 _PyDictViewObject *dv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004079 if (dict == NULL) {
4080 PyErr_BadInternalCall();
4081 return NULL;
4082 }
4083 if (!PyDict_Check(dict)) {
4084 /* XXX Get rid of this restriction later */
4085 PyErr_Format(PyExc_TypeError,
4086 "%s() requires a dict argument, not '%s'",
Victor Stinner58ac7002020-02-07 03:04:21 +01004087 type->tp_name, Py_TYPE(dict)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004088 return NULL;
4089 }
Eric Snow96c6af92015-05-29 22:21:39 -06004090 dv = PyObject_GC_New(_PyDictViewObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004091 if (dv == NULL)
4092 return NULL;
4093 Py_INCREF(dict);
4094 dv->dv_dict = (PyDictObject *)dict;
4095 _PyObject_GC_TRACK(dv);
4096 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004097}
4098
Neal Norwitze36f2ba2007-02-26 23:12:28 +00004099/* TODO(guido): The views objects are not complete:
4100
4101 * support more set operations
4102 * support arbitrary mappings?
4103 - either these should be static or exported in dictobject.h
4104 - if public then they should probably be in builtins
4105*/
4106
Guido van Rossumaac530c2007-08-24 22:33:45 +00004107/* Return 1 if self is a subset of other, iterating over self;
4108 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00004109static int
4110all_contained_in(PyObject *self, PyObject *other)
4111{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004112 PyObject *iter = PyObject_GetIter(self);
4113 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00004114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004115 if (iter == NULL)
4116 return -1;
4117 for (;;) {
4118 PyObject *next = PyIter_Next(iter);
4119 if (next == NULL) {
4120 if (PyErr_Occurred())
4121 ok = -1;
4122 break;
4123 }
4124 ok = PySequence_Contains(other, next);
4125 Py_DECREF(next);
4126 if (ok <= 0)
4127 break;
4128 }
4129 Py_DECREF(iter);
4130 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00004131}
4132
4133static PyObject *
4134dictview_richcompare(PyObject *self, PyObject *other, int op)
4135{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004136 Py_ssize_t len_self, len_other;
4137 int ok;
4138 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004140 assert(self != NULL);
4141 assert(PyDictViewSet_Check(self));
4142 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00004143
Brian Curtindfc80e32011-08-10 20:28:54 -05004144 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
4145 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004147 len_self = PyObject_Size(self);
4148 if (len_self < 0)
4149 return NULL;
4150 len_other = PyObject_Size(other);
4151 if (len_other < 0)
4152 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004153
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004154 ok = 0;
4155 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00004156
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004157 case Py_NE:
4158 case Py_EQ:
4159 if (len_self == len_other)
4160 ok = all_contained_in(self, other);
4161 if (op == Py_NE && ok >= 0)
4162 ok = !ok;
4163 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004165 case Py_LT:
4166 if (len_self < len_other)
4167 ok = all_contained_in(self, other);
4168 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004170 case Py_LE:
4171 if (len_self <= len_other)
4172 ok = all_contained_in(self, other);
4173 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004175 case Py_GT:
4176 if (len_self > len_other)
4177 ok = all_contained_in(other, self);
4178 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004180 case Py_GE:
4181 if (len_self >= len_other)
4182 ok = all_contained_in(other, self);
4183 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004184
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004185 }
4186 if (ok < 0)
4187 return NULL;
4188 result = ok ? Py_True : Py_False;
4189 Py_INCREF(result);
4190 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00004191}
4192
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004193static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004194dictview_repr(_PyDictViewObject *dv)
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004195{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004196 PyObject *seq;
bennorthd7773d92018-01-26 15:46:01 +00004197 PyObject *result = NULL;
4198 Py_ssize_t rc;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004199
bennorthd7773d92018-01-26 15:46:01 +00004200 rc = Py_ReprEnter((PyObject *)dv);
4201 if (rc != 0) {
4202 return rc > 0 ? PyUnicode_FromString("...") : NULL;
4203 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004204 seq = PySequence_List((PyObject *)dv);
bennorthd7773d92018-01-26 15:46:01 +00004205 if (seq == NULL) {
4206 goto Done;
4207 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004208 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
4209 Py_DECREF(seq);
bennorthd7773d92018-01-26 15:46:01 +00004210
4211Done:
4212 Py_ReprLeave((PyObject *)dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004213 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004214}
4215
Guido van Rossum3ac67412007-02-10 18:55:06 +00004216/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004217
4218static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004219dictkeys_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004220{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004221 if (dv->dv_dict == NULL) {
4222 Py_RETURN_NONE;
4223 }
4224 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004225}
4226
4227static int
Eric Snow96c6af92015-05-29 22:21:39 -06004228dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004229{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004230 if (dv->dv_dict == NULL)
4231 return 0;
4232 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004233}
4234
Guido van Rossum83825ac2007-02-10 04:54:19 +00004235static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004236 (lenfunc)dictview_len, /* sq_length */
4237 0, /* sq_concat */
4238 0, /* sq_repeat */
4239 0, /* sq_item */
4240 0, /* sq_slice */
4241 0, /* sq_ass_item */
4242 0, /* sq_ass_slice */
4243 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004244};
4245
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004246// Create an set object from dictviews object.
4247// Returns a new reference.
4248// This utility function is used by set operations.
Guido van Rossum523259b2007-08-24 23:41:22 +00004249static PyObject*
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004250dictviews_to_set(PyObject *self)
Guido van Rossum523259b2007-08-24 23:41:22 +00004251{
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004252 PyObject *left = self;
4253 if (PyDictKeys_Check(self)) {
4254 // PySet_New() has fast path for the dict object.
4255 PyObject *dict = (PyObject *)((_PyDictViewObject *)self)->dv_dict;
4256 if (PyDict_CheckExact(dict)) {
4257 left = dict;
4258 }
4259 }
4260 return PySet_New(left);
4261}
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004262
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004263static PyObject*
4264dictviews_sub(PyObject *self, PyObject *other)
4265{
4266 PyObject *result = dictviews_to_set(self);
4267 if (result == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004268 return NULL;
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004269 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004270
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004271 _Py_IDENTIFIER(difference_update);
4272 PyObject *tmp = _PyObject_CallMethodIdOneArg(
4273 result, &PyId_difference_update, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004274 if (tmp == NULL) {
4275 Py_DECREF(result);
4276 return NULL;
4277 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004279 Py_DECREF(tmp);
4280 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004281}
4282
Forest Gregg998cf1f2019-08-26 02:17:43 -05004283static int
4284dictitems_contains(_PyDictViewObject *dv, PyObject *obj);
4285
4286PyObject *
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004287_PyDictView_Intersect(PyObject* self, PyObject *other)
Guido van Rossum523259b2007-08-24 23:41:22 +00004288{
Forest Gregg998cf1f2019-08-26 02:17:43 -05004289 PyObject *result;
4290 PyObject *it;
4291 PyObject *key;
4292 Py_ssize_t len_self;
4293 int rv;
4294 int (*dict_contains)(_PyDictViewObject *, PyObject *);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004295
Forest Gregg998cf1f2019-08-26 02:17:43 -05004296 /* Python interpreter swaps parameters when dict view
4297 is on right side of & */
4298 if (!PyDictViewSet_Check(self)) {
4299 PyObject *tmp = other;
4300 other = self;
4301 self = tmp;
4302 }
4303
4304 len_self = dictview_len((_PyDictViewObject *)self);
4305
4306 /* if other is a set and self is smaller than other,
4307 reuse set intersection logic */
Dong-hee Na1b55b652020-02-17 19:09:15 +09004308 if (Py_IS_TYPE(other, &PySet_Type) && len_self <= PyObject_Size(other)) {
Forest Gregg998cf1f2019-08-26 02:17:43 -05004309 _Py_IDENTIFIER(intersection);
4310 return _PyObject_CallMethodIdObjArgs(other, &PyId_intersection, self, NULL);
4311 }
4312
4313 /* if other is another dict view, and it is bigger than self,
4314 swap them */
4315 if (PyDictViewSet_Check(other)) {
4316 Py_ssize_t len_other = dictview_len((_PyDictViewObject *)other);
4317 if (len_other > len_self) {
4318 PyObject *tmp = other;
4319 other = self;
4320 self = tmp;
4321 }
4322 }
4323
4324 /* at this point, two things should be true
4325 1. self is a dictview
4326 2. if other is a dictview then it is smaller than self */
4327 result = PySet_New(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004328 if (result == NULL)
4329 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004330
Forest Gregg998cf1f2019-08-26 02:17:43 -05004331 it = PyObject_GetIter(other);
Zackery Spytzb16e3822019-10-13 05:49:05 -06004332 if (it == NULL) {
4333 Py_DECREF(result);
4334 return NULL;
4335 }
Forest Gregg998cf1f2019-08-26 02:17:43 -05004336
Forest Gregg998cf1f2019-08-26 02:17:43 -05004337 if (PyDictKeys_Check(self)) {
4338 dict_contains = dictkeys_contains;
4339 }
4340 /* else PyDictItems_Check(self) */
4341 else {
4342 dict_contains = dictitems_contains;
4343 }
4344
4345 while ((key = PyIter_Next(it)) != NULL) {
4346 rv = dict_contains((_PyDictViewObject *)self, key);
4347 if (rv < 0) {
4348 goto error;
4349 }
4350 if (rv) {
4351 if (PySet_Add(result, key)) {
4352 goto error;
4353 }
4354 }
4355 Py_DECREF(key);
4356 }
4357 Py_DECREF(it);
4358 if (PyErr_Occurred()) {
4359 Py_DECREF(result);
4360 return NULL;
4361 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004362 return result;
Forest Gregg998cf1f2019-08-26 02:17:43 -05004363
4364error:
4365 Py_DECREF(it);
4366 Py_DECREF(result);
4367 Py_DECREF(key);
4368 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004369}
4370
4371static PyObject*
4372dictviews_or(PyObject* self, PyObject *other)
4373{
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004374 PyObject *result = dictviews_to_set(self);
4375 if (result == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004376 return NULL;
4377 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004378
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004379 if (_PySet_Update(result, other) < 0) {
4380 Py_DECREF(result);
4381 return NULL;
4382 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004383 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004384}
4385
4386static PyObject*
4387dictviews_xor(PyObject* self, PyObject *other)
4388{
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004389 PyObject *result = dictviews_to_set(self);
4390 if (result == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004391 return NULL;
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004392 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004393
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004394 _Py_IDENTIFIER(symmetric_difference_update);
4395 PyObject *tmp = _PyObject_CallMethodIdOneArg(
4396 result, &PyId_symmetric_difference_update, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004397 if (tmp == NULL) {
4398 Py_DECREF(result);
4399 return NULL;
4400 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004402 Py_DECREF(tmp);
4403 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004404}
4405
4406static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004407 0, /*nb_add*/
4408 (binaryfunc)dictviews_sub, /*nb_subtract*/
4409 0, /*nb_multiply*/
4410 0, /*nb_remainder*/
4411 0, /*nb_divmod*/
4412 0, /*nb_power*/
4413 0, /*nb_negative*/
4414 0, /*nb_positive*/
4415 0, /*nb_absolute*/
4416 0, /*nb_bool*/
4417 0, /*nb_invert*/
4418 0, /*nb_lshift*/
4419 0, /*nb_rshift*/
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004420 (binaryfunc)_PyDictView_Intersect, /*nb_and*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004421 (binaryfunc)dictviews_xor, /*nb_xor*/
4422 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00004423};
4424
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004425static PyObject*
4426dictviews_isdisjoint(PyObject *self, PyObject *other)
4427{
4428 PyObject *it;
4429 PyObject *item = NULL;
4430
4431 if (self == other) {
Eric Snow96c6af92015-05-29 22:21:39 -06004432 if (dictview_len((_PyDictViewObject *)self) == 0)
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004433 Py_RETURN_TRUE;
4434 else
4435 Py_RETURN_FALSE;
4436 }
4437
4438 /* Iterate over the shorter object (only if other is a set,
4439 * because PySequence_Contains may be expensive otherwise): */
4440 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
Eric Snow96c6af92015-05-29 22:21:39 -06004441 Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self);
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004442 Py_ssize_t len_other = PyObject_Size(other);
4443 if (len_other == -1)
4444 return NULL;
4445
4446 if ((len_other > len_self)) {
4447 PyObject *tmp = other;
4448 other = self;
4449 self = tmp;
4450 }
4451 }
4452
4453 it = PyObject_GetIter(other);
4454 if (it == NULL)
4455 return NULL;
4456
4457 while ((item = PyIter_Next(it)) != NULL) {
4458 int contains = PySequence_Contains(self, item);
4459 Py_DECREF(item);
4460 if (contains == -1) {
4461 Py_DECREF(it);
4462 return NULL;
4463 }
4464
4465 if (contains) {
4466 Py_DECREF(it);
4467 Py_RETURN_FALSE;
4468 }
4469 }
4470 Py_DECREF(it);
4471 if (PyErr_Occurred())
4472 return NULL; /* PyIter_Next raised an exception. */
4473 Py_RETURN_TRUE;
4474}
4475
4476PyDoc_STRVAR(isdisjoint_doc,
4477"Return True if the view and the given iterable have a null intersection.");
4478
Serhiy Storchaka81524022018-11-27 13:05:02 +02004479static PyObject* dictkeys_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored));
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004480
4481PyDoc_STRVAR(reversed_keys_doc,
4482"Return a reverse iterator over the dict keys.");
4483
Guido van Rossumb90c8482007-02-10 01:11:45 +00004484static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004485 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4486 isdisjoint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004487 {"__reversed__", (PyCFunction)(void(*)(void))dictkeys_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004488 reversed_keys_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004489 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004490};
4491
4492PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004493 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4494 "dict_keys", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004495 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004496 0, /* tp_itemsize */
4497 /* methods */
4498 (destructor)dictview_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004499 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004500 0, /* tp_getattr */
4501 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004502 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004503 (reprfunc)dictview_repr, /* tp_repr */
4504 &dictviews_as_number, /* tp_as_number */
4505 &dictkeys_as_sequence, /* tp_as_sequence */
4506 0, /* tp_as_mapping */
4507 0, /* tp_hash */
4508 0, /* tp_call */
4509 0, /* tp_str */
4510 PyObject_GenericGetAttr, /* tp_getattro */
4511 0, /* tp_setattro */
4512 0, /* tp_as_buffer */
4513 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4514 0, /* tp_doc */
4515 (traverseproc)dictview_traverse, /* tp_traverse */
4516 0, /* tp_clear */
4517 dictview_richcompare, /* tp_richcompare */
4518 0, /* tp_weaklistoffset */
4519 (getiterfunc)dictkeys_iter, /* tp_iter */
4520 0, /* tp_iternext */
4521 dictkeys_methods, /* tp_methods */
4522 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004523};
4524
4525static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304526dictkeys_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004527{
Eric Snow96c6af92015-05-29 22:21:39 -06004528 return _PyDictView_New(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004529}
4530
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004531static PyObject *
Serhiy Storchaka81524022018-11-27 13:05:02 +02004532dictkeys_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored))
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004533{
4534 if (dv->dv_dict == NULL) {
4535 Py_RETURN_NONE;
4536 }
4537 return dictiter_new(dv->dv_dict, &PyDictRevIterKey_Type);
4538}
4539
Guido van Rossum3ac67412007-02-10 18:55:06 +00004540/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004541
4542static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004543dictitems_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004544{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004545 if (dv->dv_dict == NULL) {
4546 Py_RETURN_NONE;
4547 }
4548 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004549}
4550
4551static int
Eric Snow96c6af92015-05-29 22:21:39 -06004552dictitems_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004553{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004554 int result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004555 PyObject *key, *value, *found;
4556 if (dv->dv_dict == NULL)
4557 return 0;
4558 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
4559 return 0;
4560 key = PyTuple_GET_ITEM(obj, 0);
4561 value = PyTuple_GET_ITEM(obj, 1);
Raymond Hettinger6692f012016-09-18 21:46:08 -07004562 found = PyDict_GetItemWithError((PyObject *)dv->dv_dict, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004563 if (found == NULL) {
4564 if (PyErr_Occurred())
4565 return -1;
4566 return 0;
4567 }
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004568 Py_INCREF(found);
Serhiy Storchaka18b711c2019-08-04 14:12:48 +03004569 result = PyObject_RichCompareBool(found, value, Py_EQ);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004570 Py_DECREF(found);
4571 return result;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004572}
4573
Guido van Rossum83825ac2007-02-10 04:54:19 +00004574static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004575 (lenfunc)dictview_len, /* sq_length */
4576 0, /* sq_concat */
4577 0, /* sq_repeat */
4578 0, /* sq_item */
4579 0, /* sq_slice */
4580 0, /* sq_ass_item */
4581 0, /* sq_ass_slice */
4582 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004583};
4584
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004585static PyObject* dictitems_reversed(_PyDictViewObject *dv);
4586
4587PyDoc_STRVAR(reversed_items_doc,
4588"Return a reverse iterator over the dict items.");
4589
Guido van Rossumb90c8482007-02-10 01:11:45 +00004590static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004591 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4592 isdisjoint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004593 {"__reversed__", (PyCFunction)(void(*)(void))dictitems_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004594 reversed_items_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004595 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004596};
4597
4598PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004599 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4600 "dict_items", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004601 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004602 0, /* tp_itemsize */
4603 /* methods */
4604 (destructor)dictview_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004605 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004606 0, /* tp_getattr */
4607 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004608 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004609 (reprfunc)dictview_repr, /* tp_repr */
4610 &dictviews_as_number, /* tp_as_number */
4611 &dictitems_as_sequence, /* tp_as_sequence */
4612 0, /* tp_as_mapping */
4613 0, /* tp_hash */
4614 0, /* tp_call */
4615 0, /* tp_str */
4616 PyObject_GenericGetAttr, /* tp_getattro */
4617 0, /* tp_setattro */
4618 0, /* tp_as_buffer */
4619 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4620 0, /* tp_doc */
4621 (traverseproc)dictview_traverse, /* tp_traverse */
4622 0, /* tp_clear */
4623 dictview_richcompare, /* tp_richcompare */
4624 0, /* tp_weaklistoffset */
4625 (getiterfunc)dictitems_iter, /* tp_iter */
4626 0, /* tp_iternext */
4627 dictitems_methods, /* tp_methods */
4628 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004629};
4630
4631static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304632dictitems_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004633{
Eric Snow96c6af92015-05-29 22:21:39 -06004634 return _PyDictView_New(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004635}
4636
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004637static PyObject *
4638dictitems_reversed(_PyDictViewObject *dv)
4639{
4640 if (dv->dv_dict == NULL) {
4641 Py_RETURN_NONE;
4642 }
4643 return dictiter_new(dv->dv_dict, &PyDictRevIterItem_Type);
4644}
4645
Guido van Rossum3ac67412007-02-10 18:55:06 +00004646/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004647
4648static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004649dictvalues_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004650{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004651 if (dv->dv_dict == NULL) {
4652 Py_RETURN_NONE;
4653 }
4654 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004655}
4656
Guido van Rossum83825ac2007-02-10 04:54:19 +00004657static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004658 (lenfunc)dictview_len, /* sq_length */
4659 0, /* sq_concat */
4660 0, /* sq_repeat */
4661 0, /* sq_item */
4662 0, /* sq_slice */
4663 0, /* sq_ass_item */
4664 0, /* sq_ass_slice */
4665 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004666};
4667
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004668static PyObject* dictvalues_reversed(_PyDictViewObject *dv);
4669
4670PyDoc_STRVAR(reversed_values_doc,
4671"Return a reverse iterator over the dict values.");
4672
Guido van Rossumb90c8482007-02-10 01:11:45 +00004673static PyMethodDef dictvalues_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004674 {"__reversed__", (PyCFunction)(void(*)(void))dictvalues_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004675 reversed_values_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004676 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004677};
4678
4679PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004680 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4681 "dict_values", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004682 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004683 0, /* tp_itemsize */
4684 /* methods */
4685 (destructor)dictview_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004686 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004687 0, /* tp_getattr */
4688 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004689 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004690 (reprfunc)dictview_repr, /* tp_repr */
4691 0, /* tp_as_number */
4692 &dictvalues_as_sequence, /* tp_as_sequence */
4693 0, /* tp_as_mapping */
4694 0, /* tp_hash */
4695 0, /* tp_call */
4696 0, /* tp_str */
4697 PyObject_GenericGetAttr, /* tp_getattro */
4698 0, /* tp_setattro */
4699 0, /* tp_as_buffer */
4700 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4701 0, /* tp_doc */
4702 (traverseproc)dictview_traverse, /* tp_traverse */
4703 0, /* tp_clear */
4704 0, /* tp_richcompare */
4705 0, /* tp_weaklistoffset */
4706 (getiterfunc)dictvalues_iter, /* tp_iter */
4707 0, /* tp_iternext */
4708 dictvalues_methods, /* tp_methods */
4709 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004710};
4711
4712static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304713dictvalues_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004714{
Eric Snow96c6af92015-05-29 22:21:39 -06004715 return _PyDictView_New(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004716}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004717
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004718static PyObject *
4719dictvalues_reversed(_PyDictViewObject *dv)
4720{
4721 if (dv->dv_dict == NULL) {
4722 Py_RETURN_NONE;
4723 }
4724 return dictiter_new(dv->dv_dict, &PyDictRevIterValue_Type);
4725}
4726
4727
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004728/* Returns NULL if cannot allocate a new PyDictKeysObject,
4729 but does not set an error */
4730PyDictKeysObject *
4731_PyDict_NewKeysForClass(void)
4732{
Victor Stinner742da042016-09-07 17:40:12 -07004733 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004734 if (keys == NULL)
4735 PyErr_Clear();
4736 else
4737 keys->dk_lookup = lookdict_split;
4738 return keys;
4739}
4740
4741#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
4742
4743PyObject *
4744PyObject_GenericGetDict(PyObject *obj, void *context)
4745{
4746 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
4747 if (dictptr == NULL) {
4748 PyErr_SetString(PyExc_AttributeError,
4749 "This object has no __dict__");
4750 return NULL;
4751 }
4752 dict = *dictptr;
4753 if (dict == NULL) {
4754 PyTypeObject *tp = Py_TYPE(obj);
4755 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
INADA Naokia7576492018-11-14 18:39:27 +09004756 dictkeys_incref(CACHED_KEYS(tp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004757 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
4758 }
4759 else {
4760 *dictptr = dict = PyDict_New();
4761 }
4762 }
4763 Py_XINCREF(dict);
4764 return dict;
4765}
4766
4767int
4768_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
Victor Stinner742da042016-09-07 17:40:12 -07004769 PyObject *key, PyObject *value)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004770{
4771 PyObject *dict;
4772 int res;
4773 PyDictKeysObject *cached;
4774
4775 assert(dictptr != NULL);
4776 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
4777 assert(dictptr != NULL);
4778 dict = *dictptr;
4779 if (dict == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +09004780 dictkeys_incref(cached);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004781 dict = new_dict_with_shared_keys(cached);
4782 if (dict == NULL)
4783 return -1;
4784 *dictptr = dict;
4785 }
4786 if (value == NULL) {
4787 res = PyDict_DelItem(dict, key);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004788 // Since key sharing dict doesn't allow deletion, PyDict_DelItem()
4789 // always converts dict to combined form.
4790 if ((cached = CACHED_KEYS(tp)) != NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004791 CACHED_KEYS(tp) = NULL;
INADA Naokia7576492018-11-14 18:39:27 +09004792 dictkeys_decref(cached);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004793 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01004794 }
4795 else {
INADA Naoki2294f3a2017-02-12 13:51:30 +09004796 int was_shared = (cached == ((PyDictObject *)dict)->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004797 res = PyDict_SetItem(dict, key, value);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004798 if (was_shared &&
4799 (cached = CACHED_KEYS(tp)) != NULL &&
4800 cached != ((PyDictObject *)dict)->ma_keys) {
Victor Stinner3d3f2642016-12-15 17:21:23 +01004801 /* PyDict_SetItem() may call dictresize and convert split table
4802 * into combined table. In such case, convert it to split
4803 * table again and update type's shared key only when this is
4804 * the only dict sharing key with the type.
4805 *
4806 * This is to allow using shared key in class like this:
4807 *
4808 * class C:
4809 * def __init__(self):
4810 * # one dict resize happens
4811 * self.a, self.b, self.c = 1, 2, 3
4812 * self.d, self.e, self.f = 4, 5, 6
4813 * a = C()
4814 */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004815 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004816 CACHED_KEYS(tp) = make_keys_shared(dict);
Victor Stinner742da042016-09-07 17:40:12 -07004817 }
4818 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004819 CACHED_KEYS(tp) = NULL;
4820 }
INADA Naokia7576492018-11-14 18:39:27 +09004821 dictkeys_decref(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004822 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
4823 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004824 }
4825 }
4826 } else {
4827 dict = *dictptr;
4828 if (dict == NULL) {
4829 dict = PyDict_New();
4830 if (dict == NULL)
4831 return -1;
4832 *dictptr = dict;
4833 }
4834 if (value == NULL) {
4835 res = PyDict_DelItem(dict, key);
4836 } else {
4837 res = PyDict_SetItem(dict, key, value);
4838 }
4839 }
4840 return res;
4841}
4842
4843void
4844_PyDictKeys_DecRef(PyDictKeysObject *keys)
4845{
INADA Naokia7576492018-11-14 18:39:27 +09004846 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004847}