blob: 8f9d4e7b7314042e20ae14d10f9d2588372c71f3 [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 Stinnere5014be2020-04-14 17:52:15 +0200114#include "pycore_gc.h" // _PyObject_GC_IS_TRACKED()
Victor Stinnerbcda8f12018-11-21 22:27:47 +0100115#include "pycore_object.h"
Victor Stinnere5014be2020-04-14 17:52:15 +0200116#include "pycore_pystate.h" // _PyThreadState_GET()
Eric Snow96c6af92015-05-29 22:21:39 -0600117#include "dict-common.h"
Victor Stinnere5014be2020-04-14 17:52:15 +0200118#include "stringlib/eq.h" // unicode_eq()
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000119
Larry Hastings61272b72014-01-07 12:41:53 -0800120/*[clinic input]
Larry Hastingsc2047262014-01-25 20:43:29 -0800121class dict "PyDictObject *" "&PyDict_Type"
Larry Hastings61272b72014-01-07 12:41:53 -0800122[clinic start generated code]*/
Larry Hastings581ee362014-01-28 05:00:08 -0800123/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f157a5a0ce9589d6]*/
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800124
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400125
126/*
127To ensure the lookup algorithm terminates, there must be at least one Unused
128slot (NULL key) in the table.
129To avoid slowing down lookups on a near-full table, we resize the table when
130it's USABLE_FRACTION (currently two-thirds) full.
131*/
Guido van Rossum16e93a81997-01-28 00:00:11 +0000132
Tim Peterseb28ef22001-06-02 05:27:19 +0000133#define PERTURB_SHIFT 5
134
Guido van Rossum16e93a81997-01-28 00:00:11 +0000135/*
Tim Peterseb28ef22001-06-02 05:27:19 +0000136Major subtleties ahead: Most hash schemes depend on having a "good" hash
137function, in the sense of simulating randomness. Python doesn't: its most
R David Murray537ad7a2016-07-10 12:33:18 -0400138important hash functions (for ints) are very regular in common
Tim Peterseb28ef22001-06-02 05:27:19 +0000139cases:
Tim Peters15d49292001-05-27 07:39:22 +0000140
R David Murray537ad7a2016-07-10 12:33:18 -0400141 >>>[hash(i) for i in range(4)]
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000142 [0, 1, 2, 3]
Tim Peters15d49292001-05-27 07:39:22 +0000143
Tim Peterseb28ef22001-06-02 05:27:19 +0000144This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
145the low-order i bits as the initial table index is extremely fast, and there
R David Murray537ad7a2016-07-10 12:33:18 -0400146are no collisions at all for dicts indexed by a contiguous range of ints. So
147this gives better-than-random behavior in common cases, and that's very
148desirable.
Tim Peters15d49292001-05-27 07:39:22 +0000149
Tim Peterseb28ef22001-06-02 05:27:19 +0000150OTOH, when collisions occur, the tendency to fill contiguous slices of the
151hash table makes a good collision resolution strategy crucial. Taking only
152the last i bits of the hash code is also vulnerable: for example, consider
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153the list [i << 16 for i in range(20000)] as a set of keys. Since ints are
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000154their own hash codes, and this fits in a dict of size 2**15, the last 15 bits
155 of every hash code are all 0: they *all* map to the same table index.
Tim Peters15d49292001-05-27 07:39:22 +0000156
Tim Peterseb28ef22001-06-02 05:27:19 +0000157But catering to unusual cases should not slow the usual ones, so we just take
158the last i bits anyway. It's up to collision resolution to do the rest. If
159we *usually* find the key we're looking for on the first try (and, it turns
160out, we usually do -- the table load factor is kept under 2/3, so the odds
161are solidly in our favor), then it makes best sense to keep the initial index
162computation dirt cheap.
Tim Peters15d49292001-05-27 07:39:22 +0000163
Tim Peterseb28ef22001-06-02 05:27:19 +0000164The first half of collision resolution is to visit table indices via this
165recurrence:
Tim Peters15d49292001-05-27 07:39:22 +0000166
Tim Peterseb28ef22001-06-02 05:27:19 +0000167 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +0000168
Tim Peterseb28ef22001-06-02 05:27:19 +0000169For any initial j in range(2**i), repeating that 2**i times generates each
170int in range(2**i) exactly once (see any text on random-number generation for
171proof). By itself, this doesn't help much: like linear probing (setting
172j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
173order. This would be bad, except that's not the only thing we do, and it's
174actually *good* in the common cases where hash keys are consecutive. In an
175example that's really too small to make this entirely clear, for a table of
176size 2**3 the order of indices is:
Tim Peters15d49292001-05-27 07:39:22 +0000177
Tim Peterseb28ef22001-06-02 05:27:19 +0000178 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
179
180If two things come in at index 5, the first place we look after is index 2,
181not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
182Linear probing is deadly in this case because there the fixed probe order
183is the *same* as the order consecutive keys are likely to arrive. But it's
184extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
185and certain that consecutive hash codes do not.
186
187The other half of the strategy is to get the other bits of the hash code
188into play. This is done by initializing a (unsigned) vrbl "perturb" to the
189full hash code, and changing the recurrence to:
190
Tim Peterseb28ef22001-06-02 05:27:19 +0000191 perturb >>= PERTURB_SHIFT;
INADA Naoki267941c2016-10-06 15:19:07 +0900192 j = (5*j) + 1 + perturb;
Tim Peterseb28ef22001-06-02 05:27:19 +0000193 use j % 2**i as the next table index;
194
195Now the probe sequence depends (eventually) on every bit in the hash code,
196and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
197because it quickly magnifies small differences in the bits that didn't affect
198the initial index. Note that because perturb is unsigned, if the recurrence
199is executed often enough perturb eventually becomes and remains 0. At that
200point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
201that's certain to find an empty slot eventually (since it generates every int
202in range(2**i), and we make sure there's always at least one empty slot).
203
204Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
205small so that the high bits of the hash code continue to affect the probe
206sequence across iterations; but you want it large so that in really bad cases
207the high-order hash bits have an effect on early iterations. 5 was "the
208best" in minimizing total collisions across experiments Tim Peters ran (on
209both normal and pathological cases), but 4 and 6 weren't significantly worse.
210
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000211Historical: Reimer Behrends contributed the idea of using a polynomial-based
Tim Peterseb28ef22001-06-02 05:27:19 +0000212approach, using repeated multiplication by x in GF(2**n) where an irreducible
213polynomial for each table size was chosen such that x was a primitive root.
214Christian Tismer later extended that to use division by x instead, as an
215efficient way to get the high bits of the hash code into play. This scheme
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000216also gave excellent collision statistics, but was more expensive: two
217if-tests were required inside the loop; computing "the next" index took about
218the same number of operations but without as much potential parallelism
219(e.g., computing 5*j can go on at the same time as computing 1+perturb in the
220above, and then shifting perturb can be done while the table index is being
221masked); and the PyDictObject struct required a member to hold the table's
222polynomial. In Tim's experiments the current scheme ran faster, produced
223equally good collision statistics, needed less code & used less memory.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000224
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000225*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000226
Fred Drake1bff34a2000-08-31 19:31:38 +0000227/* forward declarations */
Victor Stinner742da042016-09-07 17:40:12 -0700228static Py_ssize_t lookdict(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900229 Py_hash_t hash, PyObject **value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700230static Py_ssize_t lookdict_unicode(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900231 Py_hash_t hash, PyObject **value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700232static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400233lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900234 Py_hash_t hash, PyObject **value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700235static Py_ssize_t lookdict_split(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900236 Py_hash_t hash, PyObject **value_addr);
Fred Drake1bff34a2000-08-31 19:31:38 +0000237
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400238static int dictresize(PyDictObject *mp, Py_ssize_t minused);
Tim Petersdea48ec2001-05-22 20:40:22 +0000239
INADA Naoki2aaf98c2018-09-26 12:59:00 +0900240static PyObject* dict_iter(PyDictObject *dict);
241
Benjamin Peterson3c569292016-09-08 13:16:41 -0700242/*Global counter used to set ma_version_tag field of dictionary.
Victor Stinner3b6a6b42016-09-08 12:51:24 -0700243 * It is incremented each time that a dictionary is created and each
244 * time that a dictionary is modified. */
245static uint64_t pydict_global_version = 0;
246
247#define DICT_NEXT_VERSION() (++pydict_global_version)
248
Victor Stinner742da042016-09-07 17:40:12 -0700249/* Dictionary reuse scheme to save calls to malloc and free */
Christian Heimes2202f872008-02-06 14:31:34 +0000250#ifndef PyDict_MAXFREELIST
251#define PyDict_MAXFREELIST 80
252#endif
253static PyDictObject *free_list[PyDict_MAXFREELIST];
254static int numfree = 0;
Victor Stinner742da042016-09-07 17:40:12 -0700255static PyDictKeysObject *keys_free_list[PyDict_MAXFREELIST];
256static int numfreekeys = 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000257
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300258#include "clinic/dictobject.c.h"
259
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100260int
261PyDict_ClearFreeList(void)
Christian Heimes77c02eb2008-02-09 02:18:51 +0000262{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 PyDictObject *op;
Victor Stinner742da042016-09-07 17:40:12 -0700264 int ret = numfree + numfreekeys;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 while (numfree) {
266 op = free_list[--numfree];
267 assert(PyDict_CheckExact(op));
268 PyObject_GC_Del(op);
269 }
Victor Stinner742da042016-09-07 17:40:12 -0700270 while (numfreekeys) {
271 PyObject_FREE(keys_free_list[--numfreekeys]);
272 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100273 return ret;
274}
275
David Malcolm49526f42012-06-22 14:55:41 -0400276/* Print summary info about the state of the optimized allocator */
277void
278_PyDict_DebugMallocStats(FILE *out)
279{
280 _PyDebugAllocatorStats(out,
281 "free PyDictObject", numfree, sizeof(PyDictObject));
282}
283
284
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100285void
Victor Stinnerbed48172019-08-27 00:12:32 +0200286_PyDict_Fini(void)
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100287{
288 PyDict_ClearFreeList();
Christian Heimes77c02eb2008-02-09 02:18:51 +0000289}
290
Victor Stinner742da042016-09-07 17:40:12 -0700291#define DK_SIZE(dk) ((dk)->dk_size)
292#if SIZEOF_VOID_P > 4
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700293#define DK_IXSIZE(dk) \
294 (DK_SIZE(dk) <= 0xff ? \
295 1 : DK_SIZE(dk) <= 0xffff ? \
296 2 : DK_SIZE(dk) <= 0xffffffff ? \
Benjamin Peterson3c569292016-09-08 13:16:41 -0700297 4 : sizeof(int64_t))
Victor Stinner742da042016-09-07 17:40:12 -0700298#else
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700299#define DK_IXSIZE(dk) \
300 (DK_SIZE(dk) <= 0xff ? \
301 1 : DK_SIZE(dk) <= 0xffff ? \
Benjamin Peterson3c569292016-09-08 13:16:41 -0700302 2 : sizeof(int32_t))
Victor Stinner742da042016-09-07 17:40:12 -0700303#endif
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700304#define DK_ENTRIES(dk) \
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700305 ((PyDictKeyEntry*)(&((int8_t*)((dk)->dk_indices))[DK_SIZE(dk) * DK_IXSIZE(dk)]))
Victor Stinner742da042016-09-07 17:40:12 -0700306
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400307#define DK_MASK(dk) (((dk)->dk_size)-1)
308#define IS_POWER_OF_2(x) (((x) & (x-1)) == 0)
309
INADA Naokia7576492018-11-14 18:39:27 +0900310static void free_keys_object(PyDictKeysObject *keys);
311
312static inline void
313dictkeys_incref(PyDictKeysObject *dk)
314{
Victor Stinner49932fe2020-02-03 17:55:05 +0100315#ifdef Py_REF_DEBUG
316 _Py_RefTotal++;
317#endif
INADA Naokia7576492018-11-14 18:39:27 +0900318 dk->dk_refcnt++;
319}
320
321static inline void
322dictkeys_decref(PyDictKeysObject *dk)
323{
324 assert(dk->dk_refcnt > 0);
Victor Stinner49932fe2020-02-03 17:55:05 +0100325#ifdef Py_REF_DEBUG
326 _Py_RefTotal--;
327#endif
INADA Naokia7576492018-11-14 18:39:27 +0900328 if (--dk->dk_refcnt == 0) {
329 free_keys_object(dk);
330 }
331}
332
Victor Stinner742da042016-09-07 17:40:12 -0700333/* lookup indices. returns DKIX_EMPTY, DKIX_DUMMY, or ix >=0 */
Benjamin Peterson73222252016-09-08 09:58:47 -0700334static inline Py_ssize_t
Andy Lester62d21c92020-03-25 23:13:01 -0500335dictkeys_get_index(const PyDictKeysObject *keys, Py_ssize_t i)
Victor Stinner742da042016-09-07 17:40:12 -0700336{
337 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700338 Py_ssize_t ix;
339
Victor Stinner742da042016-09-07 17:40:12 -0700340 if (s <= 0xff) {
Andy Lester62d21c92020-03-25 23:13:01 -0500341 const int8_t *indices = (const int8_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700342 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700343 }
344 else if (s <= 0xffff) {
Andy Lester62d21c92020-03-25 23:13:01 -0500345 const int16_t *indices = (const int16_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700346 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700347 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700348#if SIZEOF_VOID_P > 4
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300349 else if (s > 0xffffffff) {
Andy Lester62d21c92020-03-25 23:13:01 -0500350 const int64_t *indices = (const int64_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700351 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700352 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700353#endif
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300354 else {
Andy Lester62d21c92020-03-25 23:13:01 -0500355 const int32_t *indices = (const int32_t*)(keys->dk_indices);
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300356 ix = indices[i];
357 }
Victor Stinner71211e32016-09-08 10:52:46 -0700358 assert(ix >= DKIX_DUMMY);
359 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700360}
361
362/* write to indices. */
Benjamin Peterson73222252016-09-08 09:58:47 -0700363static inline void
INADA Naokia7576492018-11-14 18:39:27 +0900364dictkeys_set_index(PyDictKeysObject *keys, Py_ssize_t i, Py_ssize_t ix)
Victor Stinner742da042016-09-07 17:40:12 -0700365{
366 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700367
368 assert(ix >= DKIX_DUMMY);
369
Victor Stinner742da042016-09-07 17:40:12 -0700370 if (s <= 0xff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700371 int8_t *indices = (int8_t*)(keys->dk_indices);
Victor Stinner71211e32016-09-08 10:52:46 -0700372 assert(ix <= 0x7f);
Victor Stinner208857e2016-09-08 11:35:46 -0700373 indices[i] = (char)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700374 }
375 else if (s <= 0xffff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700376 int16_t *indices = (int16_t*)(keys->dk_indices);
Victor Stinner71211e32016-09-08 10:52:46 -0700377 assert(ix <= 0x7fff);
Victor Stinner208857e2016-09-08 11:35:46 -0700378 indices[i] = (int16_t)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700379 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700380#if SIZEOF_VOID_P > 4
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300381 else if (s > 0xffffffff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700382 int64_t *indices = (int64_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700383 indices[i] = ix;
Victor Stinner742da042016-09-07 17:40:12 -0700384 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700385#endif
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300386 else {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700387 int32_t *indices = (int32_t*)(keys->dk_indices);
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300388 assert(ix <= 0x7fffffff);
389 indices[i] = (int32_t)ix;
390 }
Victor Stinner742da042016-09-07 17:40:12 -0700391}
392
393
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200394/* USABLE_FRACTION is the maximum dictionary load.
Victor Stinner742da042016-09-07 17:40:12 -0700395 * Increasing this ratio makes dictionaries more dense resulting in more
396 * collisions. Decreasing it improves sparseness at the expense of spreading
397 * indices over more cache lines and at the cost of total memory consumed.
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200398 *
399 * USABLE_FRACTION must obey the following:
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400400 * (0 < USABLE_FRACTION(n) < n) for all n >= 2
401 *
Victor Stinner742da042016-09-07 17:40:12 -0700402 * USABLE_FRACTION should be quick to calculate.
403 * Fractions around 1/2 to 2/3 seem to work well in practice.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400404 */
Victor Stinner742da042016-09-07 17:40:12 -0700405#define USABLE_FRACTION(n) (((n) << 1)/3)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400406
Victor Stinner742da042016-09-07 17:40:12 -0700407/* ESTIMATE_SIZE is reverse function of USABLE_FRACTION.
408 * This can be used to reserve enough size to insert n entries without
409 * resizing.
410 */
INADA Naoki92c50ee2016-11-22 00:57:02 +0900411#define ESTIMATE_SIZE(n) (((n)*3+1) >> 1)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400412
Victor Stinner742da042016-09-07 17:40:12 -0700413/* Alternative fraction that is otherwise close enough to 2n/3 to make
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400414 * little difference. 8 * 2/3 == 8 * 5/8 == 5. 16 * 2/3 == 16 * 5/8 == 10.
415 * 32 * 2/3 = 21, 32 * 5/8 = 20.
416 * Its advantage is that it is faster to compute on machines with slow division.
417 * #define USABLE_FRACTION(n) (((n) >> 1) + ((n) >> 2) - ((n) >> 3))
Victor Stinner742da042016-09-07 17:40:12 -0700418 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400419
Victor Stinnera9f61a52013-07-16 22:17:26 +0200420/* GROWTH_RATE. Growth rate upon hitting maximum load.
INADA Naoki5fbc5112018-04-17 15:53:34 +0900421 * Currently set to used*3.
Victor Stinnera9f61a52013-07-16 22:17:26 +0200422 * This means that dicts double in size when growing without deletions,
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700423 * but have more head room when the number of deletions is on a par with the
INADA Naoki5fbc5112018-04-17 15:53:34 +0900424 * number of insertions. See also bpo-17563 and bpo-33205.
425 *
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700426 * GROWTH_RATE was set to used*4 up to version 3.2.
427 * GROWTH_RATE was set to used*2 in version 3.3.0
INADA Naoki5fbc5112018-04-17 15:53:34 +0900428 * GROWTH_RATE was set to used*2 + capacity/2 in 3.4.0-3.6.0.
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200429 */
INADA Naoki5fbc5112018-04-17 15:53:34 +0900430#define GROWTH_RATE(d) ((d)->ma_used*3)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400431
432#define ENSURE_ALLOWS_DELETIONS(d) \
433 if ((d)->ma_keys->dk_lookup == lookdict_unicode_nodummy) { \
434 (d)->ma_keys->dk_lookup = lookdict_unicode; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400436
437/* This immutable, empty PyDictKeysObject is used for PyDict_Clear()
438 * (which cannot fail and thus can do no allocation).
439 */
440static PyDictKeysObject empty_keys_struct = {
Serhiy Storchaka97932e42016-09-26 23:01:23 +0300441 1, /* dk_refcnt */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400442 1, /* dk_size */
443 lookdict_split, /* dk_lookup */
444 0, /* dk_usable (immutable) */
Victor Stinner742da042016-09-07 17:40:12 -0700445 0, /* dk_nentries */
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700446 {DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY,
447 DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY}, /* dk_indices */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400448};
449
450static PyObject *empty_values[1] = { NULL };
451
452#define Py_EMPTY_KEYS &empty_keys_struct
453
Victor Stinner611b0fa2016-09-14 15:02:01 +0200454/* Uncomment to check the dict content in _PyDict_CheckConsistency() */
455/* #define DEBUG_PYDICT */
456
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200457#ifdef DEBUG_PYDICT
458# define ASSERT_CONSISTENT(op) assert(_PyDict_CheckConsistency((PyObject *)(op), 1))
459#else
460# define ASSERT_CONSISTENT(op) assert(_PyDict_CheckConsistency((PyObject *)(op), 0))
461#endif
Victor Stinner611b0fa2016-09-14 15:02:01 +0200462
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200463
464int
465_PyDict_CheckConsistency(PyObject *op, int check_content)
Victor Stinner611b0fa2016-09-14 15:02:01 +0200466{
Victor Stinner68762572019-10-07 18:42:01 +0200467#define CHECK(expr) \
468 do { if (!(expr)) { _PyObject_ASSERT_FAILED_MSG(op, Py_STRINGIFY(expr)); } } while (0)
469
470 assert(op != NULL);
471 CHECK(PyDict_Check(op));
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200472 PyDictObject *mp = (PyDictObject *)op;
Victor Stinner50fe3f82018-10-26 18:47:15 +0200473
Victor Stinner611b0fa2016-09-14 15:02:01 +0200474 PyDictKeysObject *keys = mp->ma_keys;
475 int splitted = _PyDict_HasSplitTable(mp);
476 Py_ssize_t usable = USABLE_FRACTION(keys->dk_size);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200477
Victor Stinner68762572019-10-07 18:42:01 +0200478 CHECK(0 <= mp->ma_used && mp->ma_used <= usable);
479 CHECK(IS_POWER_OF_2(keys->dk_size));
480 CHECK(0 <= keys->dk_usable && keys->dk_usable <= usable);
481 CHECK(0 <= keys->dk_nentries && keys->dk_nentries <= usable);
482 CHECK(keys->dk_usable + keys->dk_nentries <= usable);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200483
484 if (!splitted) {
485 /* combined table */
Victor Stinner68762572019-10-07 18:42:01 +0200486 CHECK(keys->dk_refcnt == 1);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200487 }
488
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200489 if (check_content) {
490 PyDictKeyEntry *entries = DK_ENTRIES(keys);
491 Py_ssize_t i;
Victor Stinner611b0fa2016-09-14 15:02:01 +0200492
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200493 for (i=0; i < keys->dk_size; i++) {
494 Py_ssize_t ix = dictkeys_get_index(keys, i);
Victor Stinner68762572019-10-07 18:42:01 +0200495 CHECK(DKIX_DUMMY <= ix && ix <= usable);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200496 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200497
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200498 for (i=0; i < usable; i++) {
499 PyDictKeyEntry *entry = &entries[i];
500 PyObject *key = entry->me_key;
501
502 if (key != NULL) {
503 if (PyUnicode_CheckExact(key)) {
504 Py_hash_t hash = ((PyASCIIObject *)key)->hash;
Victor Stinner68762572019-10-07 18:42:01 +0200505 CHECK(hash != -1);
506 CHECK(entry->me_hash == hash);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200507 }
508 else {
509 /* test_dict fails if PyObject_Hash() is called again */
Victor Stinner68762572019-10-07 18:42:01 +0200510 CHECK(entry->me_hash != -1);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200511 }
512 if (!splitted) {
Victor Stinner68762572019-10-07 18:42:01 +0200513 CHECK(entry->me_value != NULL);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200514 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200515 }
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200516
517 if (splitted) {
Victor Stinner68762572019-10-07 18:42:01 +0200518 CHECK(entry->me_value == NULL);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200519 }
520 }
521
522 if (splitted) {
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200523 /* splitted table */
524 for (i=0; i < mp->ma_used; i++) {
Victor Stinner68762572019-10-07 18:42:01 +0200525 CHECK(mp->ma_values[i] != NULL);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200526 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200527 }
528 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200529 return 1;
Victor Stinner68762572019-10-07 18:42:01 +0200530
531#undef CHECK
Victor Stinner611b0fa2016-09-14 15:02:01 +0200532}
Victor Stinner611b0fa2016-09-14 15:02:01 +0200533
534
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400535static PyDictKeysObject *new_keys_object(Py_ssize_t size)
536{
537 PyDictKeysObject *dk;
Victor Stinner742da042016-09-07 17:40:12 -0700538 Py_ssize_t es, usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400539
Victor Stinner742da042016-09-07 17:40:12 -0700540 assert(size >= PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400541 assert(IS_POWER_OF_2(size));
Victor Stinner742da042016-09-07 17:40:12 -0700542
543 usable = USABLE_FRACTION(size);
544 if (size <= 0xff) {
545 es = 1;
546 }
547 else if (size <= 0xffff) {
548 es = 2;
549 }
550#if SIZEOF_VOID_P > 4
551 else if (size <= 0xffffffff) {
552 es = 4;
553 }
554#endif
555 else {
556 es = sizeof(Py_ssize_t);
557 }
558
559 if (size == PyDict_MINSIZE && numfreekeys > 0) {
560 dk = keys_free_list[--numfreekeys];
561 }
562 else {
Victor Stinner98ee9d52016-09-08 09:33:56 -0700563 dk = PyObject_MALLOC(sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -0700564 + es * size
565 + sizeof(PyDictKeyEntry) * usable);
Victor Stinner742da042016-09-07 17:40:12 -0700566 if (dk == NULL) {
567 PyErr_NoMemory();
568 return NULL;
569 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400570 }
Victor Stinner49932fe2020-02-03 17:55:05 +0100571#ifdef Py_REF_DEBUG
572 _Py_RefTotal++;
573#endif
INADA Naokia7576492018-11-14 18:39:27 +0900574 dk->dk_refcnt = 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400575 dk->dk_size = size;
Victor Stinner742da042016-09-07 17:40:12 -0700576 dk->dk_usable = usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400577 dk->dk_lookup = lookdict_unicode_nodummy;
Victor Stinner742da042016-09-07 17:40:12 -0700578 dk->dk_nentries = 0;
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700579 memset(&dk->dk_indices[0], 0xff, es * size);
Victor Stinner742da042016-09-07 17:40:12 -0700580 memset(DK_ENTRIES(dk), 0, sizeof(PyDictKeyEntry) * usable);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400581 return dk;
582}
583
584static void
585free_keys_object(PyDictKeysObject *keys)
586{
Victor Stinner742da042016-09-07 17:40:12 -0700587 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400588 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -0700589 for (i = 0, n = keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400590 Py_XDECREF(entries[i].me_key);
591 Py_XDECREF(entries[i].me_value);
592 }
Victor Stinner742da042016-09-07 17:40:12 -0700593 if (keys->dk_size == PyDict_MINSIZE && numfreekeys < PyDict_MAXFREELIST) {
594 keys_free_list[numfreekeys++] = keys;
595 return;
596 }
Raymond Hettingerce5179f2016-01-31 08:56:21 -0800597 PyObject_FREE(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400598}
599
600#define new_values(size) PyMem_NEW(PyObject *, size)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400601#define free_values(values) PyMem_FREE(values)
602
603/* Consumes a reference to the keys object */
604static PyObject *
605new_dict(PyDictKeysObject *keys, PyObject **values)
606{
607 PyDictObject *mp;
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200608 assert(keys != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 if (numfree) {
610 mp = free_list[--numfree];
611 assert (mp != NULL);
Dong-hee Na1b55b652020-02-17 19:09:15 +0900612 assert (Py_IS_TYPE(mp, &PyDict_Type));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 _Py_NewReference((PyObject *)mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400615 else {
616 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
617 if (mp == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +0900618 dictkeys_decref(keys);
Zackery Spytz3d07c1e2019-03-23 20:23:29 -0600619 if (values != empty_values) {
620 free_values(values);
621 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400622 return NULL;
623 }
624 }
625 mp->ma_keys = keys;
626 mp->ma_values = values;
627 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -0700628 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200629 ASSERT_CONSISTENT(mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000631}
632
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400633/* Consumes a reference to the keys object */
634static PyObject *
635new_dict_with_shared_keys(PyDictKeysObject *keys)
636{
637 PyObject **values;
638 Py_ssize_t i, size;
639
Victor Stinner742da042016-09-07 17:40:12 -0700640 size = USABLE_FRACTION(DK_SIZE(keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400641 values = new_values(size);
642 if (values == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +0900643 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400644 return PyErr_NoMemory();
645 }
646 for (i = 0; i < size; i++) {
647 values[i] = NULL;
648 }
649 return new_dict(keys, values);
650}
651
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500652
653static PyObject *
654clone_combined_dict(PyDictObject *orig)
655{
656 assert(PyDict_CheckExact(orig));
657 assert(orig->ma_values == NULL);
658 assert(orig->ma_keys->dk_refcnt == 1);
659
660 Py_ssize_t keys_size = _PyDict_KeysSize(orig->ma_keys);
661 PyDictKeysObject *keys = PyObject_Malloc(keys_size);
662 if (keys == NULL) {
663 PyErr_NoMemory();
664 return NULL;
665 }
666
667 memcpy(keys, orig->ma_keys, keys_size);
668
669 /* After copying key/value pairs, we need to incref all
670 keys and values and they are about to be co-owned by a
671 new dict object. */
672 PyDictKeyEntry *ep0 = DK_ENTRIES(keys);
673 Py_ssize_t n = keys->dk_nentries;
674 for (Py_ssize_t i = 0; i < n; i++) {
675 PyDictKeyEntry *entry = &ep0[i];
676 PyObject *value = entry->me_value;
677 if (value != NULL) {
678 Py_INCREF(value);
679 Py_INCREF(entry->me_key);
680 }
681 }
682
683 PyDictObject *new = (PyDictObject *)new_dict(keys, NULL);
684 if (new == NULL) {
685 /* In case of an error, `new_dict()` takes care of
686 cleaning up `keys`. */
687 return NULL;
688 }
689 new->ma_used = orig->ma_used;
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200690 ASSERT_CONSISTENT(new);
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500691 if (_PyObject_GC_IS_TRACKED(orig)) {
692 /* Maintain tracking. */
693 _PyObject_GC_TRACK(new);
694 }
Yury Selivanov0b752282018-07-06 12:20:07 -0400695
696 /* Since we copied the keys table we now have an extra reference
Victor Stinner49932fe2020-02-03 17:55:05 +0100697 in the system. Manually call increment _Py_RefTotal to signal that
INADA Naokia7576492018-11-14 18:39:27 +0900698 we have it now; calling dictkeys_incref would be an error as
Yury Selivanov0b752282018-07-06 12:20:07 -0400699 keys->dk_refcnt is already set to 1 (after memcpy). */
Victor Stinner49932fe2020-02-03 17:55:05 +0100700#ifdef Py_REF_DEBUG
701 _Py_RefTotal++;
702#endif
Yury Selivanov0b752282018-07-06 12:20:07 -0400703
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500704 return (PyObject *)new;
705}
706
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400707PyObject *
708PyDict_New(void)
709{
Inada Naokif2a18672019-03-12 17:25:44 +0900710 dictkeys_incref(Py_EMPTY_KEYS);
711 return new_dict(Py_EMPTY_KEYS, empty_values);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400712}
713
Victor Stinner742da042016-09-07 17:40:12 -0700714/* Search index of hash table from offset of entry table */
715static Py_ssize_t
716lookdict_index(PyDictKeysObject *k, Py_hash_t hash, Py_ssize_t index)
717{
Victor Stinner742da042016-09-07 17:40:12 -0700718 size_t mask = DK_MASK(k);
INADA Naoki073ae482017-06-23 15:22:50 +0900719 size_t perturb = (size_t)hash;
720 size_t i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700721
INADA Naoki073ae482017-06-23 15:22:50 +0900722 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900723 Py_ssize_t ix = dictkeys_get_index(k, i);
Victor Stinner742da042016-09-07 17:40:12 -0700724 if (ix == index) {
725 return i;
726 }
727 if (ix == DKIX_EMPTY) {
728 return DKIX_EMPTY;
729 }
INADA Naoki073ae482017-06-23 15:22:50 +0900730 perturb >>= PERTURB_SHIFT;
731 i = mask & (i*5 + perturb + 1);
Victor Stinner742da042016-09-07 17:40:12 -0700732 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700733 Py_UNREACHABLE();
Victor Stinner742da042016-09-07 17:40:12 -0700734}
735
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000736/*
737The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000738This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000739Open addressing is preferred over chaining since the link overhead for
740chaining would be substantial (100% with typical malloc overhead).
741
Tim Peterseb28ef22001-06-02 05:27:19 +0000742The initial probe index is computed as hash mod the table size. Subsequent
743probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000744
745All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000746
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000747The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000748contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000749Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000750
Victor Stinner742da042016-09-07 17:40:12 -0700751lookdict() is general-purpose, and may return DKIX_ERROR if (and only if) a
Victor Stinnera4348cc2016-09-08 12:01:25 -0700752comparison raises an exception.
Guido van Rossum89d8c602007-09-18 17:26:56 +0000753lookdict_unicode() below is specialized to string keys, comparison of which can
INADA Naoki1b8df102017-02-20 22:48:10 +0900754never raise an exception; that function can never return DKIX_ERROR when key
755is string. Otherwise, it falls back to lookdict().
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400756lookdict_unicode_nodummy is further specialized for string keys that cannot be
757the <dummy> value.
INADA Naoki778928b2017-08-03 23:45:15 +0900758For both, when the key isn't found a DKIX_EMPTY is returned.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000759*/
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100760static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400761lookdict(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900762 Py_hash_t hash, PyObject **value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000763{
INADA Naoki778928b2017-08-03 23:45:15 +0900764 size_t i, mask, perturb;
Victor Stinner742da042016-09-07 17:40:12 -0700765 PyDictKeysObject *dk;
INADA Naoki778928b2017-08-03 23:45:15 +0900766 PyDictKeyEntry *ep0;
Tim Peterseb28ef22001-06-02 05:27:19 +0000767
Antoine Pitrou9a234902012-05-13 20:48:01 +0200768top:
Victor Stinner742da042016-09-07 17:40:12 -0700769 dk = mp->ma_keys;
Victor Stinner742da042016-09-07 17:40:12 -0700770 ep0 = DK_ENTRIES(dk);
INADA Naoki778928b2017-08-03 23:45:15 +0900771 mask = DK_MASK(dk);
772 perturb = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700774
INADA Naoki778928b2017-08-03 23:45:15 +0900775 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900776 Py_ssize_t ix = dictkeys_get_index(dk, i);
Victor Stinner742da042016-09-07 17:40:12 -0700777 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700778 *value_addr = NULL;
779 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400780 }
INADA Naoki778928b2017-08-03 23:45:15 +0900781 if (ix >= 0) {
782 PyDictKeyEntry *ep = &ep0[ix];
783 assert(ep->me_key != NULL);
784 if (ep->me_key == key) {
785 *value_addr = ep->me_value;
786 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700787 }
INADA Naoki778928b2017-08-03 23:45:15 +0900788 if (ep->me_hash == hash) {
789 PyObject *startkey = ep->me_key;
790 Py_INCREF(startkey);
791 int cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
792 Py_DECREF(startkey);
793 if (cmp < 0) {
794 *value_addr = NULL;
795 return DKIX_ERROR;
796 }
797 if (dk == mp->ma_keys && ep->me_key == startkey) {
798 if (cmp > 0) {
799 *value_addr = ep->me_value;
800 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700801 }
INADA Naoki778928b2017-08-03 23:45:15 +0900802 }
803 else {
804 /* The dict was mutated, restart */
805 goto top;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400806 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 }
INADA Naoki778928b2017-08-03 23:45:15 +0900809 perturb >>= PERTURB_SHIFT;
810 i = (i*5 + perturb + 1) & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700812 Py_UNREACHABLE();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000813}
814
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400815/* Specialized version for string-only keys */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100816static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400817lookdict_unicode(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900818 Py_hash_t hash, PyObject **value_addr)
Fred Drake1bff34a2000-08-31 19:31:38 +0000819{
Victor Stinner742da042016-09-07 17:40:12 -0700820 assert(mp->ma_values == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 /* Make sure this function doesn't have to handle non-unicode keys,
822 including subclasses of str; e.g., one reason to subclass
823 unicodes is to override __eq__, and for speed we don't cater to
824 that here. */
825 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400826 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900827 return lookdict(mp, key, hash, value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 }
Tim Peters15d49292001-05-27 07:39:22 +0000829
INADA Naoki778928b2017-08-03 23:45:15 +0900830 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
831 size_t mask = DK_MASK(mp->ma_keys);
832 size_t perturb = (size_t)hash;
833 size_t i = (size_t)hash & mask;
834
835 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900836 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
Victor Stinner742da042016-09-07 17:40:12 -0700837 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700838 *value_addr = NULL;
839 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400840 }
INADA Naoki778928b2017-08-03 23:45:15 +0900841 if (ix >= 0) {
842 PyDictKeyEntry *ep = &ep0[ix];
843 assert(ep->me_key != NULL);
844 assert(PyUnicode_CheckExact(ep->me_key));
845 if (ep->me_key == key ||
846 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
847 *value_addr = ep->me_value;
848 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700849 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400850 }
INADA Naoki778928b2017-08-03 23:45:15 +0900851 perturb >>= PERTURB_SHIFT;
852 i = mask & (i*5 + perturb + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700854 Py_UNREACHABLE();
Fred Drake1bff34a2000-08-31 19:31:38 +0000855}
856
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400857/* Faster version of lookdict_unicode when it is known that no <dummy> keys
858 * will be present. */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100859static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400860lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900861 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400862{
Victor Stinner742da042016-09-07 17:40:12 -0700863 assert(mp->ma_values == NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400864 /* Make sure this function doesn't have to handle non-unicode keys,
865 including subclasses of str; e.g., one reason to subclass
866 unicodes is to override __eq__, and for speed we don't cater to
867 that here. */
868 if (!PyUnicode_CheckExact(key)) {
869 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900870 return lookdict(mp, key, hash, value_addr);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400871 }
INADA Naoki778928b2017-08-03 23:45:15 +0900872
873 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
874 size_t mask = DK_MASK(mp->ma_keys);
875 size_t perturb = (size_t)hash;
876 size_t i = (size_t)hash & mask;
877
878 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900879 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
Victor Stinner742da042016-09-07 17:40:12 -0700880 assert (ix != DKIX_DUMMY);
881 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700882 *value_addr = NULL;
883 return DKIX_EMPTY;
884 }
INADA Naoki778928b2017-08-03 23:45:15 +0900885 PyDictKeyEntry *ep = &ep0[ix];
886 assert(ep->me_key != NULL);
887 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700888 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400889 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900890 *value_addr = ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700891 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400892 }
INADA Naoki778928b2017-08-03 23:45:15 +0900893 perturb >>= PERTURB_SHIFT;
894 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400895 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700896 Py_UNREACHABLE();
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400897}
898
899/* Version of lookdict for split tables.
900 * All split tables and only split tables use this lookup function.
901 * Split tables only contain unicode keys and no dummy keys,
902 * so algorithm is the same as lookdict_unicode_nodummy.
903 */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100904static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400905lookdict_split(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900906 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400907{
Victor Stinner742da042016-09-07 17:40:12 -0700908 /* mp must split table */
909 assert(mp->ma_values != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400910 if (!PyUnicode_CheckExact(key)) {
INADA Naoki778928b2017-08-03 23:45:15 +0900911 Py_ssize_t ix = lookdict(mp, key, hash, value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700912 if (ix >= 0) {
INADA Naokiba609772016-12-07 20:41:42 +0900913 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700914 }
915 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400916 }
Victor Stinner742da042016-09-07 17:40:12 -0700917
INADA Naoki778928b2017-08-03 23:45:15 +0900918 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
919 size_t mask = DK_MASK(mp->ma_keys);
920 size_t perturb = (size_t)hash;
921 size_t i = (size_t)hash & mask;
922
923 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900924 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
INADA Naoki778928b2017-08-03 23:45:15 +0900925 assert (ix != DKIX_DUMMY);
Victor Stinner742da042016-09-07 17:40:12 -0700926 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700927 *value_addr = NULL;
928 return DKIX_EMPTY;
929 }
INADA Naoki778928b2017-08-03 23:45:15 +0900930 PyDictKeyEntry *ep = &ep0[ix];
931 assert(ep->me_key != NULL);
932 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700933 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400934 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900935 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700936 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400937 }
INADA Naoki778928b2017-08-03 23:45:15 +0900938 perturb >>= PERTURB_SHIFT;
939 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400940 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700941 Py_UNREACHABLE();
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400942}
943
Benjamin Petersonfb886362010-04-24 18:21:17 +0000944int
945_PyDict_HasOnlyStringKeys(PyObject *dict)
946{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 Py_ssize_t pos = 0;
948 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000949 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400951 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 return 1;
953 while (PyDict_Next(dict, &pos, &key, &value))
954 if (!PyUnicode_Check(key))
955 return 0;
956 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000957}
958
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000959#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 do { \
961 if (!_PyObject_GC_IS_TRACKED(mp)) { \
962 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
963 _PyObject_GC_MAY_BE_TRACKED(value)) { \
964 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 } \
966 } \
967 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000968
969void
970_PyDict_MaybeUntrack(PyObject *op)
971{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 PyDictObject *mp;
973 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -0700974 Py_ssize_t i, numentries;
975 PyDictKeyEntry *ep0;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
978 return;
979
980 mp = (PyDictObject *) op;
Victor Stinner742da042016-09-07 17:40:12 -0700981 ep0 = DK_ENTRIES(mp->ma_keys);
982 numentries = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400983 if (_PyDict_HasSplitTable(mp)) {
Victor Stinner742da042016-09-07 17:40:12 -0700984 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400985 if ((value = mp->ma_values[i]) == NULL)
986 continue;
987 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
Victor Stinner742da042016-09-07 17:40:12 -0700988 assert(!_PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400989 return;
990 }
991 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400993 else {
Victor Stinner742da042016-09-07 17:40:12 -0700994 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400995 if ((value = ep0[i].me_value) == NULL)
996 continue;
997 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
998 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
999 return;
1000 }
1001 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001003}
1004
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001005/* Internal function to find slot for an item from its hash
Victor Stinner3c336c52016-09-12 14:17:40 +02001006 when it is known that the key is not present in the dict.
1007
1008 The dict must be combined. */
INADA Naokiba609772016-12-07 20:41:42 +09001009static Py_ssize_t
INADA Naoki778928b2017-08-03 23:45:15 +09001010find_empty_slot(PyDictKeysObject *keys, Py_hash_t hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001011{
INADA Naoki778928b2017-08-03 23:45:15 +09001012 assert(keys != NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001013
INADA Naoki778928b2017-08-03 23:45:15 +09001014 const size_t mask = DK_MASK(keys);
1015 size_t i = hash & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001016 Py_ssize_t ix = dictkeys_get_index(keys, i);
INADA Naoki778928b2017-08-03 23:45:15 +09001017 for (size_t perturb = hash; ix >= 0;) {
INADA Naoki267941c2016-10-06 15:19:07 +09001018 perturb >>= PERTURB_SHIFT;
INADA Naoki778928b2017-08-03 23:45:15 +09001019 i = (i*5 + perturb + 1) & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001020 ix = dictkeys_get_index(keys, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 }
INADA Naoki778928b2017-08-03 23:45:15 +09001022 return i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001023}
1024
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001025static int
1026insertion_resize(PyDictObject *mp)
1027{
Raymond Hettinger36f74aa2013-05-17 03:01:13 -07001028 return dictresize(mp, GROWTH_RATE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001029}
Antoine Pitroue965d972012-02-27 00:45:12 +01001030
1031/*
1032Internal routine to insert a new item into the table.
1033Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +01001034Returns -1 if an error occurred, or 0 on success.
1035*/
1036static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001037insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001038{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001039 PyObject *old_value;
INADA Naokiba609772016-12-07 20:41:42 +09001040 PyDictKeyEntry *ep;
Antoine Pitroue965d972012-02-27 00:45:12 +01001041
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001042 Py_INCREF(key);
1043 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001044 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
1045 if (insertion_resize(mp) < 0)
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001046 goto Fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001047 }
1048
INADA Naoki778928b2017-08-03 23:45:15 +09001049 Py_ssize_t ix = mp->ma_keys->dk_lookup(mp, key, hash, &old_value);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001050 if (ix == DKIX_ERROR)
1051 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001052
Antoine Pitroud6967322014-10-18 00:35:00 +02001053 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001054 MAINTAIN_TRACKING(mp, key, value);
Victor Stinner742da042016-09-07 17:40:12 -07001055
1056 /* When insertion order is different from shared key, we can't share
1057 * the key anymore. Convert this instance to combine table.
1058 */
1059 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09001060 ((ix >= 0 && old_value == NULL && mp->ma_used != ix) ||
Victor Stinner742da042016-09-07 17:40:12 -07001061 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001062 if (insertion_resize(mp) < 0)
1063 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001064 ix = DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001065 }
Victor Stinner742da042016-09-07 17:40:12 -07001066
1067 if (ix == DKIX_EMPTY) {
1068 /* Insert into new slot. */
INADA Naokiba609772016-12-07 20:41:42 +09001069 assert(old_value == NULL);
Victor Stinner742da042016-09-07 17:40:12 -07001070 if (mp->ma_keys->dk_usable <= 0) {
1071 /* Need to resize. */
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001072 if (insertion_resize(mp) < 0)
1073 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001074 }
INADA Naoki778928b2017-08-03 23:45:15 +09001075 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naokiba609772016-12-07 20:41:42 +09001076 ep = &DK_ENTRIES(mp->ma_keys)[mp->ma_keys->dk_nentries];
INADA Naokia7576492018-11-14 18:39:27 +09001077 dictkeys_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Victor Stinner742da042016-09-07 17:40:12 -07001078 ep->me_key = key;
1079 ep->me_hash = hash;
1080 if (mp->ma_values) {
1081 assert (mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
1082 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001083 }
1084 else {
Victor Stinner742da042016-09-07 17:40:12 -07001085 ep->me_value = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001086 }
1087 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001088 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07001089 mp->ma_keys->dk_usable--;
1090 mp->ma_keys->dk_nentries++;
1091 assert(mp->ma_keys->dk_usable >= 0);
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001092 ASSERT_CONSISTENT(mp);
Victor Stinner742da042016-09-07 17:40:12 -07001093 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001094 }
Victor Stinner742da042016-09-07 17:40:12 -07001095
Inada Naoki91234a12019-06-03 21:30:58 +09001096 if (old_value != value) {
1097 if (_PyDict_HasSplitTable(mp)) {
1098 mp->ma_values[ix] = value;
1099 if (old_value == NULL) {
1100 /* pending state */
1101 assert(ix == mp->ma_used);
1102 mp->ma_used++;
1103 }
INADA Naokiba609772016-12-07 20:41:42 +09001104 }
Inada Naoki91234a12019-06-03 21:30:58 +09001105 else {
1106 assert(old_value != NULL);
1107 DK_ENTRIES(mp->ma_keys)[ix].me_value = value;
1108 }
1109 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokiba609772016-12-07 20:41:42 +09001110 }
INADA Naokiba609772016-12-07 20:41:42 +09001111 Py_XDECREF(old_value); /* which **CAN** re-enter (see issue #22653) */
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001112 ASSERT_CONSISTENT(mp);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001113 Py_DECREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001114 return 0;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001115
1116Fail:
1117 Py_DECREF(value);
1118 Py_DECREF(key);
1119 return -1;
Antoine Pitroue965d972012-02-27 00:45:12 +01001120}
1121
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001122// Same to insertdict but specialized for ma_keys = Py_EMPTY_KEYS.
1123static int
1124insert_to_emptydict(PyDictObject *mp, PyObject *key, Py_hash_t hash,
1125 PyObject *value)
1126{
1127 assert(mp->ma_keys == Py_EMPTY_KEYS);
1128
1129 PyDictKeysObject *newkeys = new_keys_object(PyDict_MINSIZE);
1130 if (newkeys == NULL) {
1131 return -1;
1132 }
1133 if (!PyUnicode_CheckExact(key)) {
1134 newkeys->dk_lookup = lookdict;
1135 }
1136 dictkeys_decref(Py_EMPTY_KEYS);
1137 mp->ma_keys = newkeys;
1138 mp->ma_values = NULL;
1139
1140 Py_INCREF(key);
1141 Py_INCREF(value);
1142 MAINTAIN_TRACKING(mp, key, value);
1143
1144 size_t hashpos = (size_t)hash & (PyDict_MINSIZE-1);
Dong-hee Nac39d1dd2019-10-11 17:43:11 +09001145 PyDictKeyEntry *ep = DK_ENTRIES(mp->ma_keys);
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001146 dictkeys_set_index(mp->ma_keys, hashpos, 0);
1147 ep->me_key = key;
1148 ep->me_hash = hash;
1149 ep->me_value = value;
1150 mp->ma_used++;
1151 mp->ma_version_tag = DICT_NEXT_VERSION();
1152 mp->ma_keys->dk_usable--;
1153 mp->ma_keys->dk_nentries++;
1154 return 0;
1155}
1156
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001157/*
luzpaza5293b42017-11-05 07:37:50 -06001158Internal routine used by dictresize() to build a hashtable of entries.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001159*/
1160static void
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001161build_indices(PyDictKeysObject *keys, PyDictKeyEntry *ep, Py_ssize_t n)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001162{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001163 size_t mask = (size_t)DK_SIZE(keys) - 1;
1164 for (Py_ssize_t ix = 0; ix != n; ix++, ep++) {
1165 Py_hash_t hash = ep->me_hash;
1166 size_t i = hash & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001167 for (size_t perturb = hash; dictkeys_get_index(keys, i) != DKIX_EMPTY;) {
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001168 perturb >>= PERTURB_SHIFT;
INADA Naoki870c2862017-06-24 09:03:19 +09001169 i = mask & (i*5 + perturb + 1);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001170 }
INADA Naokia7576492018-11-14 18:39:27 +09001171 dictkeys_set_index(keys, i, ix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001173}
1174
1175/*
1176Restructure the table by allocating a new table and reinserting all
1177items again. When entries have been deleted, the new table may
1178actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001179If a table is split (its keys and hashes are shared, its values are not),
1180then the values are temporarily copied into the table, it is resized as
1181a combined table, then the me_value slots in the old table are NULLed out.
1182After resizing a table is always combined,
1183but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001184*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001185static int
Victor Stinner3d3f2642016-12-15 17:21:23 +01001186dictresize(PyDictObject *mp, Py_ssize_t minsize)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001187{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001188 Py_ssize_t newsize, numentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001189 PyDictKeysObject *oldkeys;
1190 PyObject **oldvalues;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001191 PyDictKeyEntry *oldentries, *newentries;
Tim Peters91a364d2001-05-19 07:04:38 +00001192
Victor Stinner742da042016-09-07 17:40:12 -07001193 /* Find the smallest table size > minused. */
1194 for (newsize = PyDict_MINSIZE;
Victor Stinner3d3f2642016-12-15 17:21:23 +01001195 newsize < minsize && newsize > 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 newsize <<= 1)
1197 ;
1198 if (newsize <= 0) {
1199 PyErr_NoMemory();
1200 return -1;
1201 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001202
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001203 oldkeys = mp->ma_keys;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001204
1205 /* NOTE: Current odict checks mp->ma_keys to detect resize happen.
1206 * So we can't reuse oldkeys even if oldkeys->dk_size == newsize.
1207 * TODO: Try reusing oldkeys when reimplement odict.
1208 */
1209
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001210 /* Allocate a new table. */
1211 mp->ma_keys = new_keys_object(newsize);
1212 if (mp->ma_keys == NULL) {
1213 mp->ma_keys = oldkeys;
1214 return -1;
1215 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01001216 // New table must be large enough.
1217 assert(mp->ma_keys->dk_usable >= mp->ma_used);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001218 if (oldkeys->dk_lookup == lookdict)
1219 mp->ma_keys->dk_lookup = lookdict;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001220
1221 numentries = mp->ma_used;
1222 oldentries = DK_ENTRIES(oldkeys);
1223 newentries = DK_ENTRIES(mp->ma_keys);
1224 oldvalues = mp->ma_values;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001225 if (oldvalues != NULL) {
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001226 /* Convert split table into new combined table.
1227 * We must incref keys; we can transfer values.
1228 * Note that values of split table is always dense.
1229 */
1230 for (Py_ssize_t i = 0; i < numentries; i++) {
1231 assert(oldvalues[i] != NULL);
1232 PyDictKeyEntry *ep = &oldentries[i];
1233 PyObject *key = ep->me_key;
1234 Py_INCREF(key);
1235 newentries[i].me_key = key;
1236 newentries[i].me_hash = ep->me_hash;
1237 newentries[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001239
INADA Naokia7576492018-11-14 18:39:27 +09001240 dictkeys_decref(oldkeys);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001241 mp->ma_values = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001242 if (oldvalues != empty_values) {
1243 free_values(oldvalues);
1244 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001245 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001246 else { // combined table.
1247 if (oldkeys->dk_nentries == numentries) {
1248 memcpy(newentries, oldentries, numentries * sizeof(PyDictKeyEntry));
1249 }
1250 else {
1251 PyDictKeyEntry *ep = oldentries;
1252 for (Py_ssize_t i = 0; i < numentries; i++) {
1253 while (ep->me_value == NULL)
1254 ep++;
1255 newentries[i] = *ep++;
1256 }
1257 }
1258
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001259 assert(oldkeys->dk_lookup != lookdict_split);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001260 assert(oldkeys->dk_refcnt == 1);
Victor Stinner49932fe2020-02-03 17:55:05 +01001261#ifdef Py_REF_DEBUG
1262 _Py_RefTotal--;
1263#endif
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001264 if (oldkeys->dk_size == PyDict_MINSIZE &&
Victor Stinner49932fe2020-02-03 17:55:05 +01001265 numfreekeys < PyDict_MAXFREELIST)
1266 {
INADA Naokia7576492018-11-14 18:39:27 +09001267 keys_free_list[numfreekeys++] = oldkeys;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001268 }
1269 else {
INADA Naokia7576492018-11-14 18:39:27 +09001270 PyObject_FREE(oldkeys);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001271 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001272 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001273
1274 build_indices(mp->ma_keys, newentries, numentries);
1275 mp->ma_keys->dk_usable -= numentries;
1276 mp->ma_keys->dk_nentries = numentries;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001278}
1279
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001280/* Returns NULL if unable to split table.
1281 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001282static PyDictKeysObject *
1283make_keys_shared(PyObject *op)
1284{
1285 Py_ssize_t i;
1286 Py_ssize_t size;
1287 PyDictObject *mp = (PyDictObject *)op;
1288
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001289 if (!PyDict_CheckExact(op))
1290 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001291 if (!_PyDict_HasSplitTable(mp)) {
1292 PyDictKeyEntry *ep0;
1293 PyObject **values;
1294 assert(mp->ma_keys->dk_refcnt == 1);
1295 if (mp->ma_keys->dk_lookup == lookdict) {
1296 return NULL;
1297 }
1298 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
1299 /* Remove dummy keys */
1300 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
1301 return NULL;
1302 }
1303 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
1304 /* Copy values into a new array */
Victor Stinner742da042016-09-07 17:40:12 -07001305 ep0 = DK_ENTRIES(mp->ma_keys);
1306 size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001307 values = new_values(size);
1308 if (values == NULL) {
1309 PyErr_SetString(PyExc_MemoryError,
1310 "Not enough memory to allocate new values array");
1311 return NULL;
1312 }
1313 for (i = 0; i < size; i++) {
1314 values[i] = ep0[i].me_value;
1315 ep0[i].me_value = NULL;
1316 }
1317 mp->ma_keys->dk_lookup = lookdict_split;
1318 mp->ma_values = values;
1319 }
INADA Naokia7576492018-11-14 18:39:27 +09001320 dictkeys_incref(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001321 return mp->ma_keys;
1322}
Christian Heimes99170a52007-12-19 02:07:34 +00001323
1324PyObject *
1325_PyDict_NewPresized(Py_ssize_t minused)
1326{
INADA Naoki92c50ee2016-11-22 00:57:02 +09001327 const Py_ssize_t max_presize = 128 * 1024;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001328 Py_ssize_t newsize;
1329 PyDictKeysObject *new_keys;
INADA Naoki92c50ee2016-11-22 00:57:02 +09001330
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001331 if (minused <= USABLE_FRACTION(PyDict_MINSIZE)) {
Inada Naokif2a18672019-03-12 17:25:44 +09001332 return PyDict_New();
1333 }
INADA Naoki92c50ee2016-11-22 00:57:02 +09001334 /* There are no strict guarantee that returned dict can contain minused
1335 * items without resize. So we create medium size dict instead of very
1336 * large dict or MemoryError.
1337 */
1338 if (minused > USABLE_FRACTION(max_presize)) {
1339 newsize = max_presize;
1340 }
1341 else {
1342 Py_ssize_t minsize = ESTIMATE_SIZE(minused);
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001343 newsize = PyDict_MINSIZE*2;
INADA Naoki92c50ee2016-11-22 00:57:02 +09001344 while (newsize < minsize) {
1345 newsize <<= 1;
1346 }
1347 }
1348 assert(IS_POWER_OF_2(newsize));
1349
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001350 new_keys = new_keys_object(newsize);
1351 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001353 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001354}
1355
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001356/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1357 * that may occur (originally dicts supported only string keys, and exceptions
1358 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001359 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001360 * (suppressed) occurred while computing the key's hash, or that some error
1361 * (suppressed) occurred when comparing keys in the dict's internal probe
1362 * sequence. A nasty example of the latter is when a Python-coded comparison
1363 * function hits a stack-depth error, which can cause this to return NULL
1364 * even if the key is present.
1365 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001366PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001367PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001368{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001369 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07001370 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 PyDictObject *mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 PyThreadState *tstate;
INADA Naokiba609772016-12-07 20:41:42 +09001373 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 if (!PyDict_Check(op))
1376 return NULL;
1377 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001378 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 {
1380 hash = PyObject_Hash(key);
1381 if (hash == -1) {
1382 PyErr_Clear();
1383 return NULL;
1384 }
1385 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 /* We can arrive here with a NULL tstate during initialization: try
1388 running "python -Wi" for an example related to string interning.
1389 Let's just hope that no exception occurs then... This must be
Victor Stinner50b48572018-11-01 01:51:40 +01001390 _PyThreadState_GET() and not PyThreadState_Get() because the latter
Victor Stinner9204fb82018-10-30 15:13:17 +01001391 abort Python if tstate is NULL. */
Victor Stinner50b48572018-11-01 01:51:40 +01001392 tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 if (tstate != NULL && tstate->curexc_type != NULL) {
1394 /* preserve the existing exception */
1395 PyObject *err_type, *err_value, *err_tb;
1396 PyErr_Fetch(&err_type, &err_value, &err_tb);
INADA Naoki778928b2017-08-03 23:45:15 +09001397 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 /* ignore errors */
1399 PyErr_Restore(err_type, err_value, err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001400 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 return NULL;
1402 }
1403 else {
INADA Naoki778928b2017-08-03 23:45:15 +09001404 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001405 if (ix < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 PyErr_Clear();
1407 return NULL;
1408 }
1409 }
INADA Naokiba609772016-12-07 20:41:42 +09001410 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001411}
1412
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001413/* Same as PyDict_GetItemWithError() but with hash supplied by caller.
1414 This returns NULL *with* an exception set if an exception occurred.
1415 It returns NULL *without* an exception set if the key wasn't present.
1416*/
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001417PyObject *
1418_PyDict_GetItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1419{
Victor Stinner742da042016-09-07 17:40:12 -07001420 Py_ssize_t ix;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001421 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001422 PyObject *value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001423
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001424 if (!PyDict_Check(op)) {
1425 PyErr_BadInternalCall();
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001426 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001427 }
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001428
INADA Naoki778928b2017-08-03 23:45:15 +09001429 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001430 if (ix < 0) {
1431 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001432 }
INADA Naokiba609772016-12-07 20:41:42 +09001433 return value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001434}
1435
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001436/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1437 This returns NULL *with* an exception set if an exception occurred.
1438 It returns NULL *without* an exception set if the key wasn't present.
1439*/
1440PyObject *
1441PyDict_GetItemWithError(PyObject *op, PyObject *key)
1442{
Victor Stinner742da042016-09-07 17:40:12 -07001443 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001444 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001445 PyDictObject*mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001446 PyObject *value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 if (!PyDict_Check(op)) {
1449 PyErr_BadInternalCall();
1450 return NULL;
1451 }
1452 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001453 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 {
1455 hash = PyObject_Hash(key);
1456 if (hash == -1) {
1457 return NULL;
1458 }
1459 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001460
INADA Naoki778928b2017-08-03 23:45:15 +09001461 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001462 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001464 return value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001465}
1466
Brett Cannonfd074152012-04-14 14:10:13 -04001467PyObject *
1468_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1469{
1470 PyObject *kv;
1471 kv = _PyUnicode_FromId(key); /* borrowed */
1472 if (kv == NULL)
1473 return NULL;
1474 return PyDict_GetItemWithError(dp, kv);
1475}
1476
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001477PyObject *
1478_PyDict_GetItemStringWithError(PyObject *v, const char *key)
1479{
1480 PyObject *kv, *rv;
1481 kv = PyUnicode_FromString(key);
1482 if (kv == NULL) {
1483 return NULL;
1484 }
1485 rv = PyDict_GetItemWithError(v, kv);
1486 Py_DECREF(kv);
1487 return rv;
1488}
1489
Victor Stinnerb4efc962015-11-20 09:24:02 +01001490/* Fast version of global value lookup (LOAD_GLOBAL).
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001491 * Lookup in globals, then builtins.
Victor Stinnerb4efc962015-11-20 09:24:02 +01001492 *
1493 * Raise an exception and return NULL if an error occurred (ex: computing the
1494 * key hash failed, key comparison failed, ...). Return NULL if the key doesn't
1495 * exist. Return the value if the key exists.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001496 */
1497PyObject *
1498_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001499{
Victor Stinner742da042016-09-07 17:40:12 -07001500 Py_ssize_t ix;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001501 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09001502 PyObject *value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001503
1504 if (!PyUnicode_CheckExact(key) ||
1505 (hash = ((PyASCIIObject *) key)->hash) == -1)
1506 {
1507 hash = PyObject_Hash(key);
1508 if (hash == -1)
1509 return NULL;
Antoine Pitroue965d972012-02-27 00:45:12 +01001510 }
Victor Stinnerb4efc962015-11-20 09:24:02 +01001511
1512 /* namespace 1: globals */
INADA Naoki778928b2017-08-03 23:45:15 +09001513 ix = globals->ma_keys->dk_lookup(globals, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001514 if (ix == DKIX_ERROR)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001515 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001516 if (ix != DKIX_EMPTY && value != NULL)
1517 return value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001518
1519 /* namespace 2: builtins */
INADA Naoki778928b2017-08-03 23:45:15 +09001520 ix = builtins->ma_keys->dk_lookup(builtins, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001521 if (ix < 0)
Victor Stinnerb4efc962015-11-20 09:24:02 +01001522 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001523 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001524}
1525
Antoine Pitroue965d972012-02-27 00:45:12 +01001526/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1527 * dictionary if it's merely replacing the value for an existing key.
1528 * This means that it's safe to loop over a dictionary with PyDict_Next()
1529 * and occasionally replace a value -- but you can't insert new keys or
1530 * remove them.
1531 */
1532int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001533PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001534{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001535 PyDictObject *mp;
1536 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001537 if (!PyDict_Check(op)) {
1538 PyErr_BadInternalCall();
1539 return -1;
1540 }
1541 assert(key);
1542 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001543 mp = (PyDictObject *)op;
1544 if (!PyUnicode_CheckExact(key) ||
1545 (hash = ((PyASCIIObject *) key)->hash) == -1)
1546 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001547 hash = PyObject_Hash(key);
1548 if (hash == -1)
1549 return -1;
1550 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001551
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001552 if (mp->ma_keys == Py_EMPTY_KEYS) {
1553 return insert_to_emptydict(mp, key, hash, value);
1554 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001555 /* insertdict() handles any resizing that might be necessary */
1556 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001557}
1558
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001559int
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001560_PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
1561 Py_hash_t hash)
1562{
1563 PyDictObject *mp;
1564
1565 if (!PyDict_Check(op)) {
1566 PyErr_BadInternalCall();
1567 return -1;
1568 }
1569 assert(key);
1570 assert(value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001571 assert(hash != -1);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001572 mp = (PyDictObject *)op;
1573
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001574 if (mp->ma_keys == Py_EMPTY_KEYS) {
1575 return insert_to_emptydict(mp, key, hash, value);
1576 }
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001577 /* insertdict() handles any resizing that might be necessary */
1578 return insertdict(mp, key, hash, value);
1579}
1580
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001581static int
INADA Naoki778928b2017-08-03 23:45:15 +09001582delitem_common(PyDictObject *mp, Py_hash_t hash, Py_ssize_t ix,
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001583 PyObject *old_value)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001584{
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001585 PyObject *old_key;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001586 PyDictKeyEntry *ep;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001587
INADA Naoki778928b2017-08-03 23:45:15 +09001588 Py_ssize_t hashpos = lookdict_index(mp->ma_keys, hash, ix);
1589 assert(hashpos >= 0);
1590
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001591 mp->ma_used--;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001592 mp->ma_version_tag = DICT_NEXT_VERSION();
1593 ep = &DK_ENTRIES(mp->ma_keys)[ix];
INADA Naokia7576492018-11-14 18:39:27 +09001594 dictkeys_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001595 ENSURE_ALLOWS_DELETIONS(mp);
1596 old_key = ep->me_key;
1597 ep->me_key = NULL;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001598 ep->me_value = NULL;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001599 Py_DECREF(old_key);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001600 Py_DECREF(old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001601
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001602 ASSERT_CONSISTENT(mp);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001603 return 0;
1604}
1605
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001606int
Tim Peters1f5871e2000-07-04 17:44:48 +00001607PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001608{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001609 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 assert(key);
1611 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001612 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 hash = PyObject_Hash(key);
1614 if (hash == -1)
1615 return -1;
1616 }
Victor Stinner742da042016-09-07 17:40:12 -07001617
1618 return _PyDict_DelItem_KnownHash(op, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001619}
1620
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001621int
1622_PyDict_DelItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1623{
INADA Naoki778928b2017-08-03 23:45:15 +09001624 Py_ssize_t ix;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001625 PyDictObject *mp;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001626 PyObject *old_value;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001627
1628 if (!PyDict_Check(op)) {
1629 PyErr_BadInternalCall();
1630 return -1;
1631 }
1632 assert(key);
1633 assert(hash != -1);
1634 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001635 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001636 if (ix == DKIX_ERROR)
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001637 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09001638 if (ix == DKIX_EMPTY || old_value == NULL) {
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001639 _PyErr_SetKeyError(key);
1640 return -1;
1641 }
Victor Stinner78601a32016-09-09 19:28:36 -07001642
1643 // Split table doesn't allow deletion. Combine it.
1644 if (_PyDict_HasSplitTable(mp)) {
1645 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1646 return -1;
1647 }
INADA Naoki778928b2017-08-03 23:45:15 +09001648 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001649 assert(ix >= 0);
1650 }
1651
INADA Naoki778928b2017-08-03 23:45:15 +09001652 return delitem_common(mp, hash, ix, old_value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001653}
1654
Antoine Pitroud741ed42016-12-27 14:23:43 +01001655/* This function promises that the predicate -> deletion sequence is atomic
1656 * (i.e. protected by the GIL), assuming the predicate itself doesn't
1657 * release the GIL.
1658 */
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001659int
1660_PyDict_DelItemIf(PyObject *op, PyObject *key,
1661 int (*predicate)(PyObject *value))
1662{
Antoine Pitroud741ed42016-12-27 14:23:43 +01001663 Py_ssize_t hashpos, ix;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001664 PyDictObject *mp;
1665 Py_hash_t hash;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001666 PyObject *old_value;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001667 int res;
1668
1669 if (!PyDict_Check(op)) {
1670 PyErr_BadInternalCall();
1671 return -1;
1672 }
1673 assert(key);
1674 hash = PyObject_Hash(key);
1675 if (hash == -1)
1676 return -1;
1677 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001678 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001679 if (ix == DKIX_ERROR)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001680 return -1;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001681 if (ix == DKIX_EMPTY || old_value == NULL) {
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001682 _PyErr_SetKeyError(key);
1683 return -1;
1684 }
Antoine Pitroud741ed42016-12-27 14:23:43 +01001685
1686 // Split table doesn't allow deletion. Combine it.
1687 if (_PyDict_HasSplitTable(mp)) {
1688 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1689 return -1;
1690 }
INADA Naoki778928b2017-08-03 23:45:15 +09001691 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001692 assert(ix >= 0);
1693 }
1694
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001695 res = predicate(old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001696 if (res == -1)
1697 return -1;
INADA Naoki778928b2017-08-03 23:45:15 +09001698
1699 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1700 assert(hashpos >= 0);
1701
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001702 if (res > 0)
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001703 return delitem_common(mp, hashpos, ix, old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001704 else
1705 return 0;
1706}
1707
1708
Guido van Rossum25831651993-05-19 14:50:45 +00001709void
Tim Peters1f5871e2000-07-04 17:44:48 +00001710PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001711{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001713 PyDictKeysObject *oldkeys;
1714 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717 if (!PyDict_Check(op))
1718 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001719 mp = ((PyDictObject *)op);
1720 oldkeys = mp->ma_keys;
1721 oldvalues = mp->ma_values;
1722 if (oldvalues == empty_values)
1723 return;
1724 /* Empty the dict... */
INADA Naokia7576492018-11-14 18:39:27 +09001725 dictkeys_incref(Py_EMPTY_KEYS);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001726 mp->ma_keys = Py_EMPTY_KEYS;
1727 mp->ma_values = empty_values;
1728 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001729 mp->ma_version_tag = DICT_NEXT_VERSION();
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001730 /* ...then clear the keys and values */
1731 if (oldvalues != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001732 n = oldkeys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001733 for (i = 0; i < n; i++)
1734 Py_CLEAR(oldvalues[i]);
1735 free_values(oldvalues);
INADA Naokia7576492018-11-14 18:39:27 +09001736 dictkeys_decref(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001737 }
1738 else {
1739 assert(oldkeys->dk_refcnt == 1);
INADA Naokia7576492018-11-14 18:39:27 +09001740 dictkeys_decref(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001741 }
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001742 ASSERT_CONSISTENT(mp);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001743}
1744
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001745/* Internal version of PyDict_Next that returns a hash value in addition
1746 * to the key and value.
1747 * Return 1 on success, return 0 when the reached the end of the dictionary
1748 * (or if op is not a dictionary)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001749 */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001750int
1751_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1752 PyObject **pvalue, Py_hash_t *phash)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001753{
INADA Naokica2d8be2016-11-04 16:59:10 +09001754 Py_ssize_t i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001755 PyDictObject *mp;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001756 PyDictKeyEntry *entry_ptr;
1757 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001758
1759 if (!PyDict_Check(op))
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001760 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001761 mp = (PyDictObject *)op;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001762 i = *ppos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001763 if (mp->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09001764 if (i < 0 || i >= mp->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001765 return 0;
INADA Naokica2d8be2016-11-04 16:59:10 +09001766 /* values of split table is always dense */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001767 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
INADA Naokica2d8be2016-11-04 16:59:10 +09001768 value = mp->ma_values[i];
1769 assert(value != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001771 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09001772 Py_ssize_t n = mp->ma_keys->dk_nentries;
1773 if (i < 0 || i >= n)
1774 return 0;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001775 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
1776 while (i < n && entry_ptr->me_value == NULL) {
1777 entry_ptr++;
1778 i++;
Victor Stinner742da042016-09-07 17:40:12 -07001779 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001780 if (i >= n)
1781 return 0;
1782 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001784 *ppos = i+1;
1785 if (pkey)
1786 *pkey = entry_ptr->me_key;
1787 if (phash)
1788 *phash = entry_ptr->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001789 if (pvalue)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001790 *pvalue = value;
1791 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001792}
1793
Tim Peters080c88b2003-02-15 03:01:11 +00001794/*
1795 * Iterate over a dict. Use like so:
1796 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001797 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001798 * PyObject *key, *value;
1799 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001800 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001801 * Refer to borrowed references in key and value.
Tim Peters080c88b2003-02-15 03:01:11 +00001802 * }
1803 *
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001804 * Return 1 on success, return 0 when the reached the end of the dictionary
1805 * (or if op is not a dictionary)
1806 *
Tim Peters080c88b2003-02-15 03:01:11 +00001807 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001808 * mutates the dict. One exception: it is safe if the loop merely changes
1809 * the values associated with the keys (but doesn't insert new keys or
1810 * delete keys), via PyDict_SetItem().
1811 */
Guido van Rossum25831651993-05-19 14:50:45 +00001812int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001813PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001814{
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001815 return _PyDict_Next(op, ppos, pkey, pvalue, NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001816}
1817
Eric Snow96c6af92015-05-29 22:21:39 -06001818/* Internal version of dict.pop(). */
1819PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001820_PyDict_Pop_KnownHash(PyObject *dict, PyObject *key, Py_hash_t hash, PyObject *deflt)
Eric Snow96c6af92015-05-29 22:21:39 -06001821{
Victor Stinner742da042016-09-07 17:40:12 -07001822 Py_ssize_t ix, hashpos;
Eric Snow96c6af92015-05-29 22:21:39 -06001823 PyObject *old_value, *old_key;
1824 PyDictKeyEntry *ep;
Yury Selivanov684ef2c2016-10-28 19:01:21 -04001825 PyDictObject *mp;
1826
1827 assert(PyDict_Check(dict));
1828 mp = (PyDictObject *)dict;
Eric Snow96c6af92015-05-29 22:21:39 -06001829
1830 if (mp->ma_used == 0) {
1831 if (deflt) {
1832 Py_INCREF(deflt);
1833 return deflt;
1834 }
1835 _PyErr_SetKeyError(key);
1836 return NULL;
1837 }
INADA Naoki778928b2017-08-03 23:45:15 +09001838 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001839 if (ix == DKIX_ERROR)
Eric Snow96c6af92015-05-29 22:21:39 -06001840 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001841 if (ix == DKIX_EMPTY || old_value == NULL) {
Eric Snow96c6af92015-05-29 22:21:39 -06001842 if (deflt) {
1843 Py_INCREF(deflt);
1844 return deflt;
1845 }
1846 _PyErr_SetKeyError(key);
1847 return NULL;
1848 }
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001849
Victor Stinner78601a32016-09-09 19:28:36 -07001850 // Split table doesn't allow deletion. Combine it.
1851 if (_PyDict_HasSplitTable(mp)) {
1852 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1853 return NULL;
1854 }
INADA Naoki778928b2017-08-03 23:45:15 +09001855 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001856 assert(ix >= 0);
1857 }
1858
INADA Naoki778928b2017-08-03 23:45:15 +09001859 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1860 assert(hashpos >= 0);
Victor Stinner78601a32016-09-09 19:28:36 -07001861 assert(old_value != NULL);
Eric Snow96c6af92015-05-29 22:21:39 -06001862 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001863 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokia7576492018-11-14 18:39:27 +09001864 dictkeys_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
Victor Stinner78601a32016-09-09 19:28:36 -07001865 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1866 ENSURE_ALLOWS_DELETIONS(mp);
1867 old_key = ep->me_key;
1868 ep->me_key = NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001869 ep->me_value = NULL;
Victor Stinner78601a32016-09-09 19:28:36 -07001870 Py_DECREF(old_key);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001871
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001872 ASSERT_CONSISTENT(mp);
Eric Snow96c6af92015-05-29 22:21:39 -06001873 return old_value;
1874}
1875
Serhiy Storchaka67796522017-01-12 18:34:33 +02001876PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001877_PyDict_Pop(PyObject *dict, PyObject *key, PyObject *deflt)
Serhiy Storchaka67796522017-01-12 18:34:33 +02001878{
1879 Py_hash_t hash;
1880
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001881 if (((PyDictObject *)dict)->ma_used == 0) {
Serhiy Storchaka67796522017-01-12 18:34:33 +02001882 if (deflt) {
1883 Py_INCREF(deflt);
1884 return deflt;
1885 }
1886 _PyErr_SetKeyError(key);
1887 return NULL;
1888 }
1889 if (!PyUnicode_CheckExact(key) ||
1890 (hash = ((PyASCIIObject *) key)->hash) == -1) {
1891 hash = PyObject_Hash(key);
1892 if (hash == -1)
1893 return NULL;
1894 }
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001895 return _PyDict_Pop_KnownHash(dict, key, hash, deflt);
Serhiy Storchaka67796522017-01-12 18:34:33 +02001896}
1897
Eric Snow96c6af92015-05-29 22:21:39 -06001898/* Internal version of dict.from_keys(). It is subclass-friendly. */
1899PyObject *
1900_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
1901{
1902 PyObject *it; /* iter(iterable) */
1903 PyObject *key;
1904 PyObject *d;
1905 int status;
1906
Victor Stinnera5ed5f02016-12-06 18:45:50 +01001907 d = _PyObject_CallNoArg(cls);
Eric Snow96c6af92015-05-29 22:21:39 -06001908 if (d == NULL)
1909 return NULL;
1910
1911 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
1912 if (PyDict_CheckExact(iterable)) {
1913 PyDictObject *mp = (PyDictObject *)d;
1914 PyObject *oldvalue;
1915 Py_ssize_t pos = 0;
1916 PyObject *key;
1917 Py_hash_t hash;
1918
Serhiy Storchakac61ac162017-03-21 08:52:38 +02001919 if (dictresize(mp, ESTIMATE_SIZE(PyDict_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001920 Py_DECREF(d);
1921 return NULL;
1922 }
1923
1924 while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
1925 if (insertdict(mp, key, hash, value)) {
1926 Py_DECREF(d);
1927 return NULL;
1928 }
1929 }
1930 return d;
1931 }
1932 if (PyAnySet_CheckExact(iterable)) {
1933 PyDictObject *mp = (PyDictObject *)d;
1934 Py_ssize_t pos = 0;
1935 PyObject *key;
1936 Py_hash_t hash;
1937
Victor Stinner742da042016-09-07 17:40:12 -07001938 if (dictresize(mp, ESTIMATE_SIZE(PySet_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001939 Py_DECREF(d);
1940 return NULL;
1941 }
1942
1943 while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
1944 if (insertdict(mp, key, hash, value)) {
1945 Py_DECREF(d);
1946 return NULL;
1947 }
1948 }
1949 return d;
1950 }
1951 }
1952
1953 it = PyObject_GetIter(iterable);
1954 if (it == NULL){
1955 Py_DECREF(d);
1956 return NULL;
1957 }
1958
1959 if (PyDict_CheckExact(d)) {
1960 while ((key = PyIter_Next(it)) != NULL) {
1961 status = PyDict_SetItem(d, key, value);
1962 Py_DECREF(key);
1963 if (status < 0)
1964 goto Fail;
1965 }
1966 } else {
1967 while ((key = PyIter_Next(it)) != NULL) {
1968 status = PyObject_SetItem(d, key, value);
1969 Py_DECREF(key);
1970 if (status < 0)
1971 goto Fail;
1972 }
1973 }
1974
1975 if (PyErr_Occurred())
1976 goto Fail;
1977 Py_DECREF(it);
1978 return d;
1979
1980Fail:
1981 Py_DECREF(it);
1982 Py_DECREF(d);
1983 return NULL;
1984}
1985
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001986/* Methods */
1987
1988static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001989dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001990{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001991 PyObject **values = mp->ma_values;
1992 PyDictKeysObject *keys = mp->ma_keys;
1993 Py_ssize_t i, n;
INADA Naokia6296d32017-08-24 14:55:17 +09001994
1995 /* bpo-31095: UnTrack is needed before calling any callbacks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001996 PyObject_GC_UnTrack(mp);
Jeroen Demeyer351c6742019-05-10 19:21:11 +02001997 Py_TRASHCAN_BEGIN(mp, dict_dealloc)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001998 if (values != NULL) {
1999 if (values != empty_values) {
Victor Stinner742da042016-09-07 17:40:12 -07002000 for (i = 0, n = mp->ma_keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002001 Py_XDECREF(values[i]);
2002 }
2003 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 }
INADA Naokia7576492018-11-14 18:39:27 +09002005 dictkeys_decref(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002006 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02002007 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02002008 assert(keys->dk_refcnt == 1);
INADA Naokia7576492018-11-14 18:39:27 +09002009 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002010 }
Dong-hee Na1b55b652020-02-17 19:09:15 +09002011 if (numfree < PyDict_MAXFREELIST && Py_IS_TYPE(mp, &PyDict_Type))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002012 free_list[numfree++] = mp;
2013 else
2014 Py_TYPE(mp)->tp_free((PyObject *)mp);
Jeroen Demeyer351c6742019-05-10 19:21:11 +02002015 Py_TRASHCAN_END
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002016}
2017
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002018
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002019static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002020dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002021{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002022 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01002023 PyObject *key = NULL, *value = NULL;
2024 _PyUnicodeWriter writer;
2025 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00002026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027 i = Py_ReprEnter((PyObject *)mp);
2028 if (i != 0) {
2029 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
2030 }
Guido van Rossum255443b1998-04-10 22:47:14 +00002031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002032 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01002033 Py_ReprLeave((PyObject *)mp);
2034 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002035 }
Tim Petersa7259592001-06-16 05:11:17 +00002036
Victor Stinnerf91929b2013-11-19 13:07:38 +01002037 _PyUnicodeWriter_Init(&writer);
2038 writer.overallocate = 1;
2039 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
2040 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00002041
Victor Stinnerf91929b2013-11-19 13:07:38 +01002042 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
2043 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00002044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002045 /* Do repr() on each key+value pair, and insert ": " between them.
2046 Note that repr may mutate the dict. */
2047 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01002048 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002049 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01002050 PyObject *s;
2051 int res;
2052
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002053 /* Prevent repr from deleting key or value during key format. */
2054 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002055 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02002056
Victor Stinnerf91929b2013-11-19 13:07:38 +01002057 if (!first) {
2058 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
2059 goto error;
2060 }
2061 first = 0;
2062
2063 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002064 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01002065 goto error;
2066 res = _PyUnicodeWriter_WriteStr(&writer, s);
2067 Py_DECREF(s);
2068 if (res < 0)
2069 goto error;
2070
2071 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
2072 goto error;
2073
2074 s = PyObject_Repr(value);
2075 if (s == NULL)
2076 goto error;
2077 res = _PyUnicodeWriter_WriteStr(&writer, s);
2078 Py_DECREF(s);
2079 if (res < 0)
2080 goto error;
2081
2082 Py_CLEAR(key);
2083 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002084 }
Tim Petersa7259592001-06-16 05:11:17 +00002085
Victor Stinnerf91929b2013-11-19 13:07:38 +01002086 writer.overallocate = 0;
2087 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
2088 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00002089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002090 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01002091
2092 return _PyUnicodeWriter_Finish(&writer);
2093
2094error:
2095 Py_ReprLeave((PyObject *)mp);
2096 _PyUnicodeWriter_Dealloc(&writer);
2097 Py_XDECREF(key);
2098 Py_XDECREF(value);
2099 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002100}
2101
Martin v. Löwis18e16552006-02-15 17:27:45 +00002102static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002103dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002104{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002105 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002106}
2107
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002108static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002109dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002110{
Victor Stinner742da042016-09-07 17:40:12 -07002111 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002112 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09002113 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002115 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002116 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002117 hash = PyObject_Hash(key);
2118 if (hash == -1)
2119 return NULL;
2120 }
INADA Naoki778928b2017-08-03 23:45:15 +09002121 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002122 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002123 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002124 if (ix == DKIX_EMPTY || value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002125 if (!PyDict_CheckExact(mp)) {
2126 /* Look up __missing__ method if we're a subclass. */
2127 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05002128 _Py_IDENTIFIER(__missing__);
2129 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002130 if (missing != NULL) {
Petr Viktorinffd97532020-02-11 17:46:57 +01002131 res = PyObject_CallOneArg(missing, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002132 Py_DECREF(missing);
2133 return res;
2134 }
2135 else if (PyErr_Occurred())
2136 return NULL;
2137 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07002138 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002139 return NULL;
2140 }
INADA Naokiba609772016-12-07 20:41:42 +09002141 Py_INCREF(value);
2142 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002143}
2144
2145static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002146dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002147{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002148 if (w == NULL)
2149 return PyDict_DelItem((PyObject *)mp, v);
2150 else
2151 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002152}
2153
Guido van Rossuma9e7a811997-05-13 21:02:11 +00002154static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002155 (lenfunc)dict_length, /*mp_length*/
2156 (binaryfunc)dict_subscript, /*mp_subscript*/
2157 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002158};
2159
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002160static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002161dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002162{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002163 PyObject *v;
2164 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002165 PyDictKeyEntry *ep;
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002166 Py_ssize_t n, offset;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002167 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002168
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002169 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002170 n = mp->ma_used;
2171 v = PyList_New(n);
2172 if (v == NULL)
2173 return NULL;
2174 if (n != mp->ma_used) {
2175 /* Durnit. The allocations caused the dict to resize.
2176 * Just start over, this shouldn't normally happen.
2177 */
2178 Py_DECREF(v);
2179 goto again;
2180 }
Victor Stinner742da042016-09-07 17:40:12 -07002181 ep = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002182 if (mp->ma_values) {
2183 value_ptr = mp->ma_values;
2184 offset = sizeof(PyObject *);
2185 }
2186 else {
2187 value_ptr = &ep[0].me_value;
2188 offset = sizeof(PyDictKeyEntry);
2189 }
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002190 for (i = 0, j = 0; j < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002191 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 PyObject *key = ep[i].me_key;
2193 Py_INCREF(key);
2194 PyList_SET_ITEM(v, j, key);
2195 j++;
2196 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002197 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002198 }
2199 assert(j == n);
2200 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002201}
2202
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002203static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002204dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002205{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002206 PyObject *v;
2207 Py_ssize_t i, j;
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002208 PyDictKeyEntry *ep;
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002209 Py_ssize_t n, offset;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002210 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002211
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002212 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002213 n = mp->ma_used;
2214 v = PyList_New(n);
2215 if (v == NULL)
2216 return NULL;
2217 if (n != mp->ma_used) {
2218 /* Durnit. The allocations caused the dict to resize.
2219 * Just start over, this shouldn't normally happen.
2220 */
2221 Py_DECREF(v);
2222 goto again;
2223 }
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002224 ep = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002225 if (mp->ma_values) {
2226 value_ptr = mp->ma_values;
2227 offset = sizeof(PyObject *);
2228 }
2229 else {
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002230 value_ptr = &ep[0].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002231 offset = sizeof(PyDictKeyEntry);
2232 }
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002233 for (i = 0, j = 0; j < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002234 PyObject *value = *value_ptr;
2235 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2236 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002237 Py_INCREF(value);
2238 PyList_SET_ITEM(v, j, value);
2239 j++;
2240 }
2241 }
2242 assert(j == n);
2243 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002244}
2245
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002246static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002247dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002248{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002249 PyObject *v;
2250 Py_ssize_t i, j, n;
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002251 Py_ssize_t offset;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002252 PyObject *item, *key;
2253 PyDictKeyEntry *ep;
2254 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002256 /* Preallocate the list of tuples, to avoid allocations during
2257 * the loop over the items, which could trigger GC, which
2258 * could resize the dict. :-(
2259 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002260 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261 n = mp->ma_used;
2262 v = PyList_New(n);
2263 if (v == NULL)
2264 return NULL;
2265 for (i = 0; i < n; i++) {
2266 item = PyTuple_New(2);
2267 if (item == NULL) {
2268 Py_DECREF(v);
2269 return NULL;
2270 }
2271 PyList_SET_ITEM(v, i, item);
2272 }
2273 if (n != mp->ma_used) {
2274 /* Durnit. The allocations caused the dict to resize.
2275 * Just start over, this shouldn't normally happen.
2276 */
2277 Py_DECREF(v);
2278 goto again;
2279 }
2280 /* Nothing we do below makes any function calls. */
Victor Stinner742da042016-09-07 17:40:12 -07002281 ep = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002282 if (mp->ma_values) {
2283 value_ptr = mp->ma_values;
2284 offset = sizeof(PyObject *);
2285 }
2286 else {
2287 value_ptr = &ep[0].me_value;
2288 offset = sizeof(PyDictKeyEntry);
2289 }
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002290 for (i = 0, j = 0; j < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002291 PyObject *value = *value_ptr;
2292 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2293 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 key = ep[i].me_key;
2295 item = PyList_GET_ITEM(v, j);
2296 Py_INCREF(key);
2297 PyTuple_SET_ITEM(item, 0, key);
2298 Py_INCREF(value);
2299 PyTuple_SET_ITEM(item, 1, value);
2300 j++;
2301 }
2302 }
2303 assert(j == n);
2304 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002305}
2306
Larry Hastings5c661892014-01-24 06:17:25 -08002307/*[clinic input]
2308@classmethod
2309dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08002310 iterable: object
2311 value: object=None
2312 /
2313
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002314Create a new dictionary with keys from iterable and values set to value.
Larry Hastings5c661892014-01-24 06:17:25 -08002315[clinic start generated code]*/
2316
Larry Hastings5c661892014-01-24 06:17:25 -08002317static PyObject *
2318dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002319/*[clinic end generated code: output=8fb98e4b10384999 input=382ba4855d0f74c3]*/
Larry Hastings5c661892014-01-24 06:17:25 -08002320{
Eric Snow96c6af92015-05-29 22:21:39 -06002321 return _PyDict_FromKeys((PyObject *)type, iterable, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002322}
2323
Brandt Buchereb8ac572020-02-24 19:47:34 -08002324/* Single-arg dict update; used by dict_update_common and operators. */
2325static int
2326dict_update_arg(PyObject *self, PyObject *arg)
2327{
2328 if (PyDict_CheckExact(arg)) {
2329 return PyDict_Merge(self, arg, 1);
2330 }
2331 _Py_IDENTIFIER(keys);
2332 PyObject *func;
2333 if (_PyObject_LookupAttrId(arg, &PyId_keys, &func) < 0) {
2334 return -1;
2335 }
2336 if (func != NULL) {
2337 Py_DECREF(func);
2338 return PyDict_Merge(self, arg, 1);
2339 }
2340 return PyDict_MergeFromSeq2(self, arg, 1);
2341}
2342
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002343static int
Victor Stinner742da042016-09-07 17:40:12 -07002344dict_update_common(PyObject *self, PyObject *args, PyObject *kwds,
2345 const char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002346{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002347 PyObject *arg = NULL;
2348 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002349
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002350 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002351 result = -1;
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002352 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002353 else if (arg != NULL) {
Brandt Buchereb8ac572020-02-24 19:47:34 -08002354 result = dict_update_arg(self, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002355 }
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 if (result == 0 && kwds != NULL) {
2358 if (PyArg_ValidateKeywordArguments(kwds))
2359 result = PyDict_Merge(self, kwds, 1);
2360 else
2361 result = -1;
2362 }
2363 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002364}
2365
Victor Stinner91f0d4a2017-01-19 12:45:06 +01002366/* Note: dict.update() uses the METH_VARARGS|METH_KEYWORDS calling convention.
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +03002367 Using METH_FASTCALL|METH_KEYWORDS would make dict.update(**dict2) calls
2368 slower, see the issue #29312. */
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002369static PyObject *
2370dict_update(PyObject *self, PyObject *args, PyObject *kwds)
2371{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002372 if (dict_update_common(self, args, kwds, "update") != -1)
2373 Py_RETURN_NONE;
2374 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002375}
2376
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002377/* Update unconditionally replaces existing items.
2378 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00002379 otherwise it leaves existing items unchanged.
2380
2381 PyDict_{Update,Merge} update/merge from a mapping object.
2382
Tim Petersf582b822001-12-11 18:51:08 +00002383 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00002384 producing iterable objects of length 2.
2385*/
2386
Tim Petersf582b822001-12-11 18:51:08 +00002387int
Tim Peters1fc240e2001-10-26 05:06:50 +00002388PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
2389{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002390 PyObject *it; /* iter(seq2) */
2391 Py_ssize_t i; /* index into seq2 of current element */
2392 PyObject *item; /* seq2[i] */
2393 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00002394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 assert(d != NULL);
2396 assert(PyDict_Check(d));
2397 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00002398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002399 it = PyObject_GetIter(seq2);
2400 if (it == NULL)
2401 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002403 for (i = 0; ; ++i) {
2404 PyObject *key, *value;
2405 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00002406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002407 fast = NULL;
2408 item = PyIter_Next(it);
2409 if (item == NULL) {
2410 if (PyErr_Occurred())
2411 goto Fail;
2412 break;
2413 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002415 /* Convert item to sequence, and verify length 2. */
2416 fast = PySequence_Fast(item, "");
2417 if (fast == NULL) {
2418 if (PyErr_ExceptionMatches(PyExc_TypeError))
2419 PyErr_Format(PyExc_TypeError,
2420 "cannot convert dictionary update "
2421 "sequence element #%zd to a sequence",
2422 i);
2423 goto Fail;
2424 }
2425 n = PySequence_Fast_GET_SIZE(fast);
2426 if (n != 2) {
2427 PyErr_Format(PyExc_ValueError,
2428 "dictionary update sequence element #%zd "
2429 "has length %zd; 2 is required",
2430 i, n);
2431 goto Fail;
2432 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002434 /* Update/merge with this (key, value) pair. */
2435 key = PySequence_Fast_GET_ITEM(fast, 0);
2436 value = PySequence_Fast_GET_ITEM(fast, 1);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002437 Py_INCREF(key);
2438 Py_INCREF(value);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002439 if (override) {
2440 if (PyDict_SetItem(d, key, value) < 0) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002441 Py_DECREF(key);
2442 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002443 goto Fail;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002444 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002445 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002446 else if (PyDict_GetItemWithError(d, key) == NULL) {
2447 if (PyErr_Occurred() || PyDict_SetItem(d, key, value) < 0) {
2448 Py_DECREF(key);
2449 Py_DECREF(value);
2450 goto Fail;
2451 }
2452 }
2453
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002454 Py_DECREF(key);
2455 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002456 Py_DECREF(fast);
2457 Py_DECREF(item);
2458 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002460 i = 0;
Victor Stinner0fc91ee2019-04-12 21:51:34 +02002461 ASSERT_CONSISTENT(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002462 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00002463Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002464 Py_XDECREF(item);
2465 Py_XDECREF(fast);
2466 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002467Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002468 Py_DECREF(it);
2469 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00002470}
2471
doko@ubuntu.comc96df682016-10-11 08:04:02 +02002472static int
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002473dict_merge(PyObject *a, PyObject *b, int override)
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002474{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002475 PyDictObject *mp, *other;
2476 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -07002477 PyDictKeyEntry *entry, *ep0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002478
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002479 assert(0 <= override && override <= 2);
2480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002481 /* We accept for the argument either a concrete dictionary object,
2482 * or an abstract "mapping" object. For the former, we can do
2483 * things quite efficiently. For the latter, we only require that
2484 * PyMapping_Keys() and PyObject_GetItem() be supported.
2485 */
2486 if (a == NULL || !PyDict_Check(a) || b == NULL) {
2487 PyErr_BadInternalCall();
2488 return -1;
2489 }
2490 mp = (PyDictObject*)a;
INADA Naoki2aaf98c2018-09-26 12:59:00 +09002491 if (PyDict_Check(b) && (Py_TYPE(b)->tp_iter == (getiterfunc)dict_iter)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002492 other = (PyDictObject*)b;
2493 if (other == mp || other->ma_used == 0)
2494 /* a.update(a) or a.update({}); nothing to do */
2495 return 0;
2496 if (mp->ma_used == 0)
2497 /* Since the target dict is empty, PyDict_GetItem()
2498 * always returns NULL. Setting override to 1
2499 * skips the unnecessary test.
2500 */
2501 override = 1;
2502 /* Do one big resize at the start, rather than
2503 * incrementally resizing as we insert new items. Expect
2504 * that there will be no (or few) overlapping keys.
2505 */
INADA Naokib1152be2016-10-27 19:26:50 +09002506 if (USABLE_FRACTION(mp->ma_keys->dk_size) < other->ma_used) {
2507 if (dictresize(mp, ESTIMATE_SIZE(mp->ma_used + other->ma_used))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002508 return -1;
INADA Naokib1152be2016-10-27 19:26:50 +09002509 }
2510 }
Victor Stinner742da042016-09-07 17:40:12 -07002511 ep0 = DK_ENTRIES(other->ma_keys);
2512 for (i = 0, n = other->ma_keys->dk_nentries; i < n; i++) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002513 PyObject *key, *value;
2514 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002515 entry = &ep0[i];
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002516 key = entry->me_key;
2517 hash = entry->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002518 if (other->ma_values)
2519 value = other->ma_values[i];
2520 else
2521 value = entry->me_value;
2522
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002523 if (value != NULL) {
2524 int err = 0;
2525 Py_INCREF(key);
2526 Py_INCREF(value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002527 if (override == 1)
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002528 err = insertdict(mp, key, hash, value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002529 else if (_PyDict_GetItem_KnownHash(a, key, hash) == NULL) {
2530 if (PyErr_Occurred()) {
2531 Py_DECREF(value);
2532 Py_DECREF(key);
2533 return -1;
2534 }
2535 err = insertdict(mp, key, hash, value);
2536 }
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002537 else if (override != 0) {
2538 _PyErr_SetKeyError(key);
2539 Py_DECREF(value);
2540 Py_DECREF(key);
2541 return -1;
2542 }
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002543 Py_DECREF(value);
2544 Py_DECREF(key);
2545 if (err != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002546 return -1;
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002547
Victor Stinner742da042016-09-07 17:40:12 -07002548 if (n != other->ma_keys->dk_nentries) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002549 PyErr_SetString(PyExc_RuntimeError,
2550 "dict mutated during update");
2551 return -1;
2552 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002553 }
2554 }
2555 }
2556 else {
2557 /* Do it the generic, slower way */
2558 PyObject *keys = PyMapping_Keys(b);
2559 PyObject *iter;
2560 PyObject *key, *value;
2561 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002563 if (keys == NULL)
2564 /* Docstring says this is equivalent to E.keys() so
2565 * if E doesn't have a .keys() method we want
2566 * AttributeError to percolate up. Might as well
2567 * do the same for any other error.
2568 */
2569 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002571 iter = PyObject_GetIter(keys);
2572 Py_DECREF(keys);
2573 if (iter == NULL)
2574 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002576 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002577 if (override != 1) {
2578 if (PyDict_GetItemWithError(a, key) != NULL) {
2579 if (override != 0) {
2580 _PyErr_SetKeyError(key);
2581 Py_DECREF(key);
2582 Py_DECREF(iter);
2583 return -1;
2584 }
2585 Py_DECREF(key);
2586 continue;
2587 }
2588 else if (PyErr_Occurred()) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002589 Py_DECREF(key);
2590 Py_DECREF(iter);
2591 return -1;
2592 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002593 }
2594 value = PyObject_GetItem(b, key);
2595 if (value == NULL) {
2596 Py_DECREF(iter);
2597 Py_DECREF(key);
2598 return -1;
2599 }
2600 status = PyDict_SetItem(a, key, value);
2601 Py_DECREF(key);
2602 Py_DECREF(value);
2603 if (status < 0) {
2604 Py_DECREF(iter);
2605 return -1;
2606 }
2607 }
2608 Py_DECREF(iter);
2609 if (PyErr_Occurred())
2610 /* Iterator completed, via error */
2611 return -1;
2612 }
Victor Stinner0fc91ee2019-04-12 21:51:34 +02002613 ASSERT_CONSISTENT(a);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002614 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002615}
2616
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002617int
2618PyDict_Update(PyObject *a, PyObject *b)
2619{
2620 return dict_merge(a, b, 1);
2621}
2622
2623int
2624PyDict_Merge(PyObject *a, PyObject *b, int override)
2625{
2626 /* XXX Deprecate override not in (0, 1). */
2627 return dict_merge(a, b, override != 0);
2628}
2629
2630int
2631_PyDict_MergeEx(PyObject *a, PyObject *b, int override)
2632{
2633 return dict_merge(a, b, override);
2634}
2635
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002636static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302637dict_copy(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002638{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002639 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002640}
2641
2642PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002643PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002644{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002645 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002646 PyDictObject *mp;
2647 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002649 if (o == NULL || !PyDict_Check(o)) {
2650 PyErr_BadInternalCall();
2651 return NULL;
2652 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002653
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002654 mp = (PyDictObject *)o;
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002655 if (mp->ma_used == 0) {
2656 /* The dict is empty; just return a new dict. */
2657 return PyDict_New();
2658 }
2659
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002660 if (_PyDict_HasSplitTable(mp)) {
2661 PyDictObject *split_copy;
Victor Stinner742da042016-09-07 17:40:12 -07002662 Py_ssize_t size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
2663 PyObject **newvalues;
2664 newvalues = new_values(size);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002665 if (newvalues == NULL)
2666 return PyErr_NoMemory();
2667 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2668 if (split_copy == NULL) {
2669 free_values(newvalues);
2670 return NULL;
2671 }
2672 split_copy->ma_values = newvalues;
2673 split_copy->ma_keys = mp->ma_keys;
2674 split_copy->ma_used = mp->ma_used;
INADA Naokid1c82c52018-04-03 11:43:53 +09002675 split_copy->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokia7576492018-11-14 18:39:27 +09002676 dictkeys_incref(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002677 for (i = 0, n = size; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002678 PyObject *value = mp->ma_values[i];
2679 Py_XINCREF(value);
2680 split_copy->ma_values[i] = value;
2681 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002682 if (_PyObject_GC_IS_TRACKED(mp))
2683 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002684 return (PyObject *)split_copy;
2685 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002686
2687 if (PyDict_CheckExact(mp) && mp->ma_values == NULL &&
2688 (mp->ma_used >= (mp->ma_keys->dk_nentries * 2) / 3))
2689 {
2690 /* Use fast-copy if:
2691
2692 (1) 'mp' is an instance of a subclassed dict; and
2693
2694 (2) 'mp' is not a split-dict; and
2695
2696 (3) if 'mp' is non-compact ('del' operation does not resize dicts),
2697 do fast-copy only if it has at most 1/3 non-used keys.
2698
Ville Skyttä61f82e02018-04-20 23:08:45 +03002699 The last condition (3) is important to guard against a pathological
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002700 case when a large dict is almost emptied with multiple del/pop
2701 operations and copied after that. In cases like this, we defer to
2702 PyDict_Merge, which produces a compacted copy.
2703 */
2704 return clone_combined_dict(mp);
2705 }
2706
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002707 copy = PyDict_New();
2708 if (copy == NULL)
2709 return NULL;
2710 if (PyDict_Merge(copy, o, 1) == 0)
2711 return copy;
2712 Py_DECREF(copy);
2713 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002714}
2715
Martin v. Löwis18e16552006-02-15 17:27:45 +00002716Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002717PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002718{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002719 if (mp == NULL || !PyDict_Check(mp)) {
2720 PyErr_BadInternalCall();
2721 return -1;
2722 }
2723 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002724}
2725
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002726PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002727PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002728{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002729 if (mp == NULL || !PyDict_Check(mp)) {
2730 PyErr_BadInternalCall();
2731 return NULL;
2732 }
2733 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002734}
2735
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002736PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002737PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002738{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002739 if (mp == NULL || !PyDict_Check(mp)) {
2740 PyErr_BadInternalCall();
2741 return NULL;
2742 }
2743 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002744}
2745
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002746PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002747PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002748{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002749 if (mp == NULL || !PyDict_Check(mp)) {
2750 PyErr_BadInternalCall();
2751 return NULL;
2752 }
2753 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002754}
2755
Tim Peterse63415e2001-05-08 04:38:29 +00002756/* Return 1 if dicts equal, 0 if not, -1 if error.
2757 * Gets out as soon as any difference is detected.
2758 * Uses only Py_EQ comparison.
2759 */
2760static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002761dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002762{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002763 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002765 if (a->ma_used != b->ma_used)
2766 /* can't be equal if # of entries differ */
2767 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002768 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Victor Stinner742da042016-09-07 17:40:12 -07002769 for (i = 0; i < a->ma_keys->dk_nentries; i++) {
2770 PyDictKeyEntry *ep = &DK_ENTRIES(a->ma_keys)[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002771 PyObject *aval;
2772 if (a->ma_values)
2773 aval = a->ma_values[i];
2774 else
2775 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002776 if (aval != NULL) {
2777 int cmp;
2778 PyObject *bval;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002779 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002780 /* temporarily bump aval's refcount to ensure it stays
2781 alive until we're done with it */
2782 Py_INCREF(aval);
2783 /* ditto for key */
2784 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002785 /* reuse the known hash value */
INADA Naoki778928b2017-08-03 23:45:15 +09002786 b->ma_keys->dk_lookup(b, key, ep->me_hash, &bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002787 if (bval == NULL) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002788 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002789 Py_DECREF(aval);
2790 if (PyErr_Occurred())
2791 return -1;
2792 return 0;
2793 }
Dong-hee Na2d5bf562019-12-31 10:04:22 +09002794 Py_INCREF(bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002795 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002796 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002797 Py_DECREF(aval);
Dong-hee Na2d5bf562019-12-31 10:04:22 +09002798 Py_DECREF(bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002799 if (cmp <= 0) /* error or not equal */
2800 return cmp;
2801 }
2802 }
2803 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002804}
Tim Peterse63415e2001-05-08 04:38:29 +00002805
2806static PyObject *
2807dict_richcompare(PyObject *v, PyObject *w, int op)
2808{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002809 int cmp;
2810 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002812 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2813 res = Py_NotImplemented;
2814 }
2815 else if (op == Py_EQ || op == Py_NE) {
2816 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2817 if (cmp < 0)
2818 return NULL;
2819 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2820 }
2821 else
2822 res = Py_NotImplemented;
2823 Py_INCREF(res);
2824 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002825}
Tim Peterse63415e2001-05-08 04:38:29 +00002826
Larry Hastings61272b72014-01-07 12:41:53 -08002827/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002828
2829@coexist
2830dict.__contains__
2831
2832 key: object
2833 /
2834
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002835True if the dictionary has the specified key, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002836[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002837
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002838static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002839dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka19d25972017-02-04 08:05:07 +02002840/*[clinic end generated code: output=a3d03db709ed6e6b input=fe1cb42ad831e820]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002841{
Larry Hastingsc2047262014-01-25 20:43:29 -08002842 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002843 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002844 Py_ssize_t ix;
INADA Naokiba609772016-12-07 20:41:42 +09002845 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002847 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002848 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002849 hash = PyObject_Hash(key);
2850 if (hash == -1)
2851 return NULL;
2852 }
INADA Naoki778928b2017-08-03 23:45:15 +09002853 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002854 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002855 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002856 if (ix == DKIX_EMPTY || value == NULL)
Victor Stinner742da042016-09-07 17:40:12 -07002857 Py_RETURN_FALSE;
2858 Py_RETURN_TRUE;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002859}
2860
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002861/*[clinic input]
2862dict.get
2863
2864 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002865 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002866 /
2867
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002868Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002869[clinic start generated code]*/
2870
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002871static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002872dict_get_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002873/*[clinic end generated code: output=bba707729dee05bf input=279ddb5790b6b107]*/
Barry Warsawc38c5da1997-10-06 17:49:20 +00002874{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002875 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002876 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002877 Py_ssize_t ix;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002880 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002881 hash = PyObject_Hash(key);
2882 if (hash == -1)
2883 return NULL;
2884 }
INADA Naoki778928b2017-08-03 23:45:15 +09002885 ix = (self->ma_keys->dk_lookup) (self, key, hash, &val);
Victor Stinner742da042016-09-07 17:40:12 -07002886 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002887 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002888 if (ix == DKIX_EMPTY || val == NULL) {
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002889 val = default_value;
INADA Naokiba609772016-12-07 20:41:42 +09002890 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002891 Py_INCREF(val);
2892 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002893}
2894
Benjamin Peterson00e98862013-03-07 22:16:29 -05002895PyObject *
2896PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002897{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002898 PyDictObject *mp = (PyDictObject *)d;
INADA Naoki93f26f72016-11-02 18:45:16 +09002899 PyObject *value;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002900 Py_hash_t hash;
Guido van Rossum164452c2000-08-08 16:12:54 +00002901
Benjamin Peterson00e98862013-03-07 22:16:29 -05002902 if (!PyDict_Check(d)) {
2903 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002904 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002905 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002906
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002907 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002908 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002909 hash = PyObject_Hash(key);
2910 if (hash == -1)
2911 return NULL;
2912 }
Inada Naoki2ddc7f62019-03-18 20:38:33 +09002913 if (mp->ma_keys == Py_EMPTY_KEYS) {
2914 if (insert_to_emptydict(mp, key, hash, defaultobj) < 0) {
2915 return NULL;
2916 }
2917 return defaultobj;
2918 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002919
2920 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
2921 if (insertion_resize(mp) < 0)
2922 return NULL;
2923 }
2924
INADA Naoki778928b2017-08-03 23:45:15 +09002925 Py_ssize_t ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002926 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002927 return NULL;
INADA Naoki93f26f72016-11-02 18:45:16 +09002928
2929 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09002930 ((ix >= 0 && value == NULL && mp->ma_used != ix) ||
INADA Naoki93f26f72016-11-02 18:45:16 +09002931 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
2932 if (insertion_resize(mp) < 0) {
2933 return NULL;
2934 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002935 ix = DKIX_EMPTY;
2936 }
2937
2938 if (ix == DKIX_EMPTY) {
2939 PyDictKeyEntry *ep, *ep0;
2940 value = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002941 if (mp->ma_keys->dk_usable <= 0) {
Victor Stinner3c336c52016-09-12 14:17:40 +02002942 if (insertion_resize(mp) < 0) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002943 return NULL;
Victor Stinner3c336c52016-09-12 14:17:40 +02002944 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002945 }
INADA Naoki778928b2017-08-03 23:45:15 +09002946 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naoki93f26f72016-11-02 18:45:16 +09002947 ep0 = DK_ENTRIES(mp->ma_keys);
2948 ep = &ep0[mp->ma_keys->dk_nentries];
INADA Naokia7576492018-11-14 18:39:27 +09002949 dictkeys_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002950 Py_INCREF(key);
INADA Naoki93f26f72016-11-02 18:45:16 +09002951 Py_INCREF(value);
2952 MAINTAIN_TRACKING(mp, key, value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002953 ep->me_key = key;
2954 ep->me_hash = hash;
INADA Naokiba609772016-12-07 20:41:42 +09002955 if (_PyDict_HasSplitTable(mp)) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002956 assert(mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
2957 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Victor Stinner742da042016-09-07 17:40:12 -07002958 }
2959 else {
INADA Naoki93f26f72016-11-02 18:45:16 +09002960 ep->me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07002961 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002962 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002963 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naoki93f26f72016-11-02 18:45:16 +09002964 mp->ma_keys->dk_usable--;
2965 mp->ma_keys->dk_nentries++;
2966 assert(mp->ma_keys->dk_usable >= 0);
2967 }
INADA Naokiba609772016-12-07 20:41:42 +09002968 else if (value == NULL) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002969 value = defaultobj;
2970 assert(_PyDict_HasSplitTable(mp));
2971 assert(ix == mp->ma_used);
2972 Py_INCREF(value);
2973 MAINTAIN_TRACKING(mp, key, value);
INADA Naokiba609772016-12-07 20:41:42 +09002974 mp->ma_values[ix] = value;
INADA Naoki93f26f72016-11-02 18:45:16 +09002975 mp->ma_used++;
2976 mp->ma_version_tag = DICT_NEXT_VERSION();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002977 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002978
Victor Stinner0fc91ee2019-04-12 21:51:34 +02002979 ASSERT_CONSISTENT(mp);
INADA Naoki93f26f72016-11-02 18:45:16 +09002980 return value;
Guido van Rossum164452c2000-08-08 16:12:54 +00002981}
2982
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002983/*[clinic input]
2984dict.setdefault
2985
2986 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002987 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002988 /
2989
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002990Insert key with a value of default if key is not in the dictionary.
2991
2992Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002993[clinic start generated code]*/
2994
Benjamin Peterson00e98862013-03-07 22:16:29 -05002995static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002996dict_setdefault_impl(PyDictObject *self, PyObject *key,
2997 PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002998/*[clinic end generated code: output=f8c1101ebf69e220 input=0f063756e815fd9d]*/
Benjamin Peterson00e98862013-03-07 22:16:29 -05002999{
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003000 PyObject *val;
Benjamin Peterson00e98862013-03-07 22:16:29 -05003001
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02003002 val = PyDict_SetDefault((PyObject *)self, key, default_value);
Benjamin Peterson00e98862013-03-07 22:16:29 -05003003 Py_XINCREF(val);
3004 return val;
3005}
Guido van Rossum164452c2000-08-08 16:12:54 +00003006
3007static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303008dict_clear(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00003009{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003010 PyDict_Clear((PyObject *)mp);
3011 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00003012}
3013
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003014/*[clinic input]
3015dict.pop
3016
3017 key: object
3018 default: object = NULL
3019 /
3020
Serhiy Storchaka279f4462019-09-14 12:24:05 +03003021D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003022
3023If key is not found, default is returned if given, otherwise KeyError is raised
3024[clinic start generated code]*/
3025
Guido van Rossumba6ab842000-12-12 22:02:18 +00003026static PyObject *
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003027dict_pop_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
Serhiy Storchaka279f4462019-09-14 12:24:05 +03003028/*[clinic end generated code: output=3abb47b89f24c21c input=eeebec7812190348]*/
Guido van Rossume027d982002-04-12 15:11:59 +00003029{
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003030 return _PyDict_Pop((PyObject*)self, key, default_value);
Guido van Rossume027d982002-04-12 15:11:59 +00003031}
3032
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003033/*[clinic input]
3034dict.popitem
3035
3036Remove and return a (key, value) pair as a 2-tuple.
3037
3038Pairs are returned in LIFO (last-in, first-out) order.
3039Raises KeyError if the dict is empty.
3040[clinic start generated code]*/
3041
Guido van Rossume027d982002-04-12 15:11:59 +00003042static PyObject *
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003043dict_popitem_impl(PyDictObject *self)
3044/*[clinic end generated code: output=e65fcb04420d230d input=1c38a49f21f64941]*/
Guido van Rossumba6ab842000-12-12 22:02:18 +00003045{
Victor Stinner742da042016-09-07 17:40:12 -07003046 Py_ssize_t i, j;
3047 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003048 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00003049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003050 /* Allocate the result tuple before checking the size. Believe it
3051 * or not, this allocation could trigger a garbage collection which
3052 * could empty the dict, so if we checked the size first and that
3053 * happened, the result would be an infinite loop (searching for an
3054 * entry that no longer exists). Note that the usual popitem()
3055 * idiom is "while d: k, v = d.popitem()". so needing to throw the
3056 * tuple away if the dict *is* empty isn't a significant
3057 * inefficiency -- possible, but unlikely in practice.
3058 */
3059 res = PyTuple_New(2);
3060 if (res == NULL)
3061 return NULL;
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003062 if (self->ma_used == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003063 Py_DECREF(res);
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003064 PyErr_SetString(PyExc_KeyError, "popitem(): dictionary is empty");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003065 return NULL;
3066 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003067 /* Convert split table to combined table */
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003068 if (self->ma_keys->dk_lookup == lookdict_split) {
3069 if (dictresize(self, DK_SIZE(self->ma_keys))) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003070 Py_DECREF(res);
3071 return NULL;
3072 }
3073 }
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003074 ENSURE_ALLOWS_DELETIONS(self);
Victor Stinner742da042016-09-07 17:40:12 -07003075
3076 /* Pop last item */
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003077 ep0 = DK_ENTRIES(self->ma_keys);
3078 i = self->ma_keys->dk_nentries - 1;
Victor Stinner742da042016-09-07 17:40:12 -07003079 while (i >= 0 && ep0[i].me_value == NULL) {
3080 i--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003081 }
Victor Stinner742da042016-09-07 17:40:12 -07003082 assert(i >= 0);
3083
3084 ep = &ep0[i];
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003085 j = lookdict_index(self->ma_keys, ep->me_hash, i);
Victor Stinner742da042016-09-07 17:40:12 -07003086 assert(j >= 0);
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003087 assert(dictkeys_get_index(self->ma_keys, j) == i);
3088 dictkeys_set_index(self->ma_keys, j, DKIX_DUMMY);
Victor Stinner742da042016-09-07 17:40:12 -07003089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003090 PyTuple_SET_ITEM(res, 0, ep->me_key);
3091 PyTuple_SET_ITEM(res, 1, ep->me_value);
Victor Stinner742da042016-09-07 17:40:12 -07003092 ep->me_key = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003093 ep->me_value = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07003094 /* We can't dk_usable++ since there is DKIX_DUMMY in indices */
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003095 self->ma_keys->dk_nentries = i;
3096 self->ma_used--;
3097 self->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner0fc91ee2019-04-12 21:51:34 +02003098 ASSERT_CONSISTENT(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003099 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00003100}
3101
Jeremy Hylton8caad492000-06-23 14:18:11 +00003102static int
3103dict_traverse(PyObject *op, visitproc visit, void *arg)
3104{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003105 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson55f44522016-09-05 12:12:59 -07003106 PyDictKeysObject *keys = mp->ma_keys;
Serhiy Storchaka46825d22016-09-26 21:29:34 +03003107 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Victor Stinner742da042016-09-07 17:40:12 -07003108 Py_ssize_t i, n = keys->dk_nentries;
3109
Benjamin Peterson55f44522016-09-05 12:12:59 -07003110 if (keys->dk_lookup == lookdict) {
3111 for (i = 0; i < n; i++) {
3112 if (entries[i].me_value != NULL) {
3113 Py_VISIT(entries[i].me_value);
3114 Py_VISIT(entries[i].me_key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003115 }
3116 }
Victor Stinner742da042016-09-07 17:40:12 -07003117 }
3118 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003119 if (mp->ma_values != NULL) {
Benjamin Peterson55f44522016-09-05 12:12:59 -07003120 for (i = 0; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003121 Py_VISIT(mp->ma_values[i]);
3122 }
3123 }
3124 else {
Benjamin Peterson55f44522016-09-05 12:12:59 -07003125 for (i = 0; i < n; i++) {
3126 Py_VISIT(entries[i].me_value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003127 }
3128 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003129 }
3130 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00003131}
3132
3133static int
3134dict_tp_clear(PyObject *op)
3135{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003136 PyDict_Clear(op);
3137 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00003138}
3139
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003140static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003141
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003142Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003143_PyDict_SizeOf(PyDictObject *mp)
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003144{
Victor Stinner742da042016-09-07 17:40:12 -07003145 Py_ssize_t size, usable, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003146
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003147 size = DK_SIZE(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07003148 usable = USABLE_FRACTION(size);
3149
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02003150 res = _PyObject_SIZE(Py_TYPE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003151 if (mp->ma_values)
Victor Stinner742da042016-09-07 17:40:12 -07003152 res += usable * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003153 /* If the dictionary is split, the keys portion is accounted-for
3154 in the type object. */
3155 if (mp->ma_keys->dk_refcnt == 1)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003156 res += (sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003157 + DK_IXSIZE(mp->ma_keys) * size
3158 + sizeof(PyDictKeyEntry) * usable);
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003159 return res;
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003160}
3161
3162Py_ssize_t
3163_PyDict_KeysSize(PyDictKeysObject *keys)
3164{
Victor Stinner98ee9d52016-09-08 09:33:56 -07003165 return (sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003166 + DK_IXSIZE(keys) * DK_SIZE(keys)
3167 + USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry));
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003168}
3169
doko@ubuntu.com17210f52016-01-14 14:04:59 +01003170static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303171dict_sizeof(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003172{
3173 return PyLong_FromSsize_t(_PyDict_SizeOf(mp));
3174}
3175
Brandt Buchereb8ac572020-02-24 19:47:34 -08003176static PyObject *
3177dict_or(PyObject *self, PyObject *other)
3178{
3179 if (!PyDict_Check(self) || !PyDict_Check(other)) {
3180 Py_RETURN_NOTIMPLEMENTED;
3181 }
3182 PyObject *new = PyDict_Copy(self);
3183 if (new == NULL) {
3184 return NULL;
3185 }
3186 if (dict_update_arg(new, other)) {
3187 Py_DECREF(new);
3188 return NULL;
3189 }
3190 return new;
3191}
3192
3193static PyObject *
3194dict_ior(PyObject *self, PyObject *other)
3195{
3196 if (dict_update_arg(self, other)) {
3197 return NULL;
3198 }
3199 Py_INCREF(self);
3200 return self;
3201}
3202
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00003203PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
3204
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003205PyDoc_STRVAR(sizeof__doc__,
3206"D.__sizeof__() -> size of D in memory, in bytes");
3207
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003208PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04003209"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
3210If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
3211If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
3212In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00003213
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003214PyDoc_STRVAR(clear__doc__,
3215"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003216
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003217PyDoc_STRVAR(copy__doc__,
3218"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00003219
Guido van Rossumb90c8482007-02-10 01:11:45 +00003220/* Forward */
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303221static PyObject *dictkeys_new(PyObject *, PyObject *);
3222static PyObject *dictitems_new(PyObject *, PyObject *);
3223static PyObject *dictvalues_new(PyObject *, PyObject *);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003224
Guido van Rossum45c85d12007-07-27 16:31:40 +00003225PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003226 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003227PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003228 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003229PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003230 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00003231
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003232static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07003233 DICT___CONTAINS___METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003234 {"__getitem__", (PyCFunction)(void(*)(void))dict_subscript, METH_O | METH_COEXIST,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003235 getitem__doc__},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003236 {"__sizeof__", (PyCFunction)(void(*)(void))dict_sizeof, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003237 sizeof__doc__},
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003238 DICT_GET_METHODDEF
3239 DICT_SETDEFAULT_METHODDEF
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003240 DICT_POP_METHODDEF
3241 DICT_POPITEM_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303242 {"keys", dictkeys_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003243 keys__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303244 {"items", dictitems_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003245 items__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303246 {"values", dictvalues_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003247 values__doc__},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003248 {"update", (PyCFunction)(void(*)(void))dict_update, METH_VARARGS | METH_KEYWORDS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003249 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08003250 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003251 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
3252 clear__doc__},
3253 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
3254 copy__doc__},
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003255 DICT___REVERSED___METHODDEF
Guido van Rossum48b069a2020-04-07 09:50:06 -07003256 {"__class_getitem__", (PyCFunction)Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003257 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003258};
3259
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00003260/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00003261int
3262PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003263{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003264 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07003265 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003266 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003267 PyObject *value;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003269 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003270 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003271 hash = PyObject_Hash(key);
3272 if (hash == -1)
3273 return -1;
3274 }
INADA Naoki778928b2017-08-03 23:45:15 +09003275 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003276 if (ix == DKIX_ERROR)
3277 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003278 return (ix != DKIX_EMPTY && value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003279}
3280
Thomas Wouterscf297e42007-02-23 15:07:44 +00003281/* Internal version of PyDict_Contains used when the hash value is already known */
3282int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003283_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00003284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003285 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003286 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -07003287 Py_ssize_t ix;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003288
INADA Naoki778928b2017-08-03 23:45:15 +09003289 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003290 if (ix == DKIX_ERROR)
3291 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003292 return (ix != DKIX_EMPTY && value != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00003293}
3294
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003295/* Hack to implement "key in dict" */
3296static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003297 0, /* sq_length */
3298 0, /* sq_concat */
3299 0, /* sq_repeat */
3300 0, /* sq_item */
3301 0, /* sq_slice */
3302 0, /* sq_ass_item */
3303 0, /* sq_ass_slice */
3304 PyDict_Contains, /* sq_contains */
3305 0, /* sq_inplace_concat */
3306 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003307};
3308
Brandt Buchereb8ac572020-02-24 19:47:34 -08003309static PyNumberMethods dict_as_number = {
3310 .nb_or = dict_or,
3311 .nb_inplace_or = dict_ior,
3312};
3313
Guido van Rossum09e563a2001-05-01 12:10:21 +00003314static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003315dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3316{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003317 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003318 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003320 assert(type != NULL && type->tp_alloc != NULL);
3321 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02003322 if (self == NULL)
3323 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003324 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003325
Victor Stinnera9f61a52013-07-16 22:17:26 +02003326 /* The object has been implicitly tracked by tp_alloc */
3327 if (type == &PyDict_Type)
3328 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003329
3330 d->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003331 d->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07003332 d->ma_keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003333 if (d->ma_keys == NULL) {
3334 Py_DECREF(self);
3335 return NULL;
3336 }
Victor Stinner0fc91ee2019-04-12 21:51:34 +02003337 ASSERT_CONSISTENT(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003338 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003339}
3340
Tim Peters25786c02001-09-02 08:22:48 +00003341static int
3342dict_init(PyObject *self, PyObject *args, PyObject *kwds)
3343{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003344 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00003345}
3346
Tim Peters6d6c1a32001-08-02 04:15:00 +00003347static PyObject *
Dong-hee Nae27916b2020-04-02 09:55:43 +09003348dict_vectorcall(PyObject *type, PyObject * const*args,
3349 size_t nargsf, PyObject *kwnames)
3350{
3351 assert(PyType_Check(type));
3352 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
3353 if (!_PyArg_CheckPositional("dict", nargs, 0, 1)) {
3354 return NULL;
3355 }
3356
3357 PyObject *self = dict_new((PyTypeObject *)type, NULL, NULL);
3358 if (self == NULL) {
3359 return NULL;
3360 }
3361 if (nargs == 1) {
3362 if (dict_update_arg(self, args[0]) < 0) {
3363 Py_DECREF(self);
3364 return NULL;
3365 }
3366 args++;
3367 }
3368 if (kwnames != NULL) {
3369 for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(kwnames); i++) {
3370 if (PyDict_SetItem(self, PyTuple_GET_ITEM(kwnames, i), args[i]) < 0) {
3371 Py_DECREF(self);
3372 return NULL;
3373 }
3374 }
3375 }
3376 return self;
3377}
3378
3379static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003380dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00003381{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003382 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003383}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003384
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003385PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00003386"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00003387"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003388" (key, value) pairs\n"
3389"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00003390" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003391" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00003392" d[k] = v\n"
3393"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
3394" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00003395
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003396PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003397 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3398 "dict",
3399 sizeof(PyDictObject),
3400 0,
3401 (destructor)dict_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003402 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003403 0, /* tp_getattr */
3404 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003405 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003406 (reprfunc)dict_repr, /* tp_repr */
Brandt Buchereb8ac572020-02-24 19:47:34 -08003407 &dict_as_number, /* tp_as_number */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003408 &dict_as_sequence, /* tp_as_sequence */
3409 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003410 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003411 0, /* tp_call */
3412 0, /* tp_str */
3413 PyObject_GenericGetAttr, /* tp_getattro */
3414 0, /* tp_setattro */
3415 0, /* tp_as_buffer */
3416 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3417 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
3418 dictionary_doc, /* tp_doc */
3419 dict_traverse, /* tp_traverse */
3420 dict_tp_clear, /* tp_clear */
3421 dict_richcompare, /* tp_richcompare */
3422 0, /* tp_weaklistoffset */
3423 (getiterfunc)dict_iter, /* tp_iter */
3424 0, /* tp_iternext */
3425 mapp_methods, /* tp_methods */
3426 0, /* tp_members */
3427 0, /* tp_getset */
3428 0, /* tp_base */
3429 0, /* tp_dict */
3430 0, /* tp_descr_get */
3431 0, /* tp_descr_set */
3432 0, /* tp_dictoffset */
3433 dict_init, /* tp_init */
3434 PyType_GenericAlloc, /* tp_alloc */
3435 dict_new, /* tp_new */
3436 PyObject_GC_Del, /* tp_free */
Dong-hee Nae27916b2020-04-02 09:55:43 +09003437 .tp_vectorcall = dict_vectorcall,
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003438};
3439
Victor Stinner3c1e4812012-03-26 22:10:51 +02003440PyObject *
3441_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
3442{
3443 PyObject *kv;
3444 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02003445 if (kv == NULL) {
3446 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003447 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02003448 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02003449 return PyDict_GetItem(dp, kv);
3450}
3451
Guido van Rossum3cca2451997-05-16 14:23:33 +00003452/* For backward compatibility with old dictionary interface */
3453
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003454PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003455PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003456{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003457 PyObject *kv, *rv;
3458 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003459 if (kv == NULL) {
3460 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003461 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003462 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003463 rv = PyDict_GetItem(v, kv);
3464 Py_DECREF(kv);
3465 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003466}
3467
3468int
Victor Stinner3c1e4812012-03-26 22:10:51 +02003469_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
3470{
3471 PyObject *kv;
3472 kv = _PyUnicode_FromId(key); /* borrowed */
3473 if (kv == NULL)
3474 return -1;
3475 return PyDict_SetItem(v, kv, item);
3476}
3477
3478int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003479PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003480{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003481 PyObject *kv;
3482 int err;
3483 kv = PyUnicode_FromString(key);
3484 if (kv == NULL)
3485 return -1;
3486 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
3487 err = PyDict_SetItem(v, kv, item);
3488 Py_DECREF(kv);
3489 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003490}
3491
3492int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01003493_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
3494{
3495 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
3496 if (kv == NULL)
3497 return -1;
3498 return PyDict_DelItem(v, kv);
3499}
3500
3501int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003502PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003503{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003504 PyObject *kv;
3505 int err;
3506 kv = PyUnicode_FromString(key);
3507 if (kv == NULL)
3508 return -1;
3509 err = PyDict_DelItem(v, kv);
3510 Py_DECREF(kv);
3511 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003512}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003513
Raymond Hettinger019a1482004-03-18 02:41:19 +00003514/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003515
3516typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003517 PyObject_HEAD
3518 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
3519 Py_ssize_t di_used;
3520 Py_ssize_t di_pos;
3521 PyObject* di_result; /* reusable result tuple for iteritems */
3522 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003523} dictiterobject;
3524
3525static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003526dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003527{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003528 dictiterobject *di;
3529 di = PyObject_GC_New(dictiterobject, itertype);
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003530 if (di == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003531 return NULL;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003532 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003533 Py_INCREF(dict);
3534 di->di_dict = dict;
3535 di->di_used = dict->ma_used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003536 di->len = dict->ma_used;
Dong-hee Na24dc2f82019-10-20 05:01:08 +09003537 if (itertype == &PyDictRevIterKey_Type ||
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003538 itertype == &PyDictRevIterItem_Type ||
Dong-hee Na24dc2f82019-10-20 05:01:08 +09003539 itertype == &PyDictRevIterValue_Type) {
3540 if (dict->ma_values) {
3541 di->di_pos = dict->ma_used - 1;
3542 }
3543 else {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003544 di->di_pos = dict->ma_keys->dk_nentries - 1;
Dong-hee Na24dc2f82019-10-20 05:01:08 +09003545 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003546 }
3547 else {
3548 di->di_pos = 0;
3549 }
3550 if (itertype == &PyDictIterItem_Type ||
3551 itertype == &PyDictRevIterItem_Type) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003552 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
3553 if (di->di_result == NULL) {
3554 Py_DECREF(di);
3555 return NULL;
3556 }
3557 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003558 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003559 di->di_result = NULL;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003560 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003561 _PyObject_GC_TRACK(di);
3562 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003563}
3564
3565static void
3566dictiter_dealloc(dictiterobject *di)
3567{
INADA Naokia6296d32017-08-24 14:55:17 +09003568 /* bpo-31095: UnTrack is needed before calling any callbacks */
3569 _PyObject_GC_UNTRACK(di);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003570 Py_XDECREF(di->di_dict);
3571 Py_XDECREF(di->di_result);
3572 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003573}
3574
3575static int
3576dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
3577{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003578 Py_VISIT(di->di_dict);
3579 Py_VISIT(di->di_result);
3580 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003581}
3582
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003583static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303584dictiter_len(dictiterobject *di, PyObject *Py_UNUSED(ignored))
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003585{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003586 Py_ssize_t len = 0;
3587 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
3588 len = di->len;
3589 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003590}
3591
Guido van Rossumb90c8482007-02-10 01:11:45 +00003592PyDoc_STRVAR(length_hint_doc,
3593 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003594
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003595static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303596dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003597
3598PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3599
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003600static PyMethodDef dictiter_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003601 {"__length_hint__", (PyCFunction)(void(*)(void))dictiter_len, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003602 length_hint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003603 {"__reduce__", (PyCFunction)(void(*)(void))dictiter_reduce, METH_NOARGS,
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003604 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003605 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003606};
3607
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003608static PyObject*
3609dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00003610{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003611 PyObject *key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003612 Py_ssize_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003613 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003614 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00003615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003616 if (d == NULL)
3617 return NULL;
3618 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00003619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003620 if (di->di_used != d->ma_used) {
3621 PyErr_SetString(PyExc_RuntimeError,
3622 "dictionary changed size during iteration");
3623 di->di_used = -1; /* Make this state sticky */
3624 return NULL;
3625 }
Guido van Rossum2147df72002-07-16 20:30:22 +00003626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003627 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003628 k = d->ma_keys;
INADA Naokica2d8be2016-11-04 16:59:10 +09003629 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003630 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003631 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003632 goto fail;
3633 key = DK_ENTRIES(k)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003634 assert(d->ma_values[i] != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003635 }
3636 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003637 Py_ssize_t n = k->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003638 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
3639 while (i < n && entry_ptr->me_value == NULL) {
3640 entry_ptr++;
3641 i++;
3642 }
3643 if (i >= n)
3644 goto fail;
3645 key = entry_ptr->me_key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003646 }
Thomas Perl796cc6e2019-03-28 07:03:25 +01003647 // We found an element (key), but did not expect it
3648 if (di->len == 0) {
3649 PyErr_SetString(PyExc_RuntimeError,
3650 "dictionary keys changed during iteration");
3651 goto fail;
3652 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003653 di->di_pos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003654 di->len--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003655 Py_INCREF(key);
3656 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003657
3658fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003659 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003660 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003661 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003662}
3663
Raymond Hettinger019a1482004-03-18 02:41:19 +00003664PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003665 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3666 "dict_keyiterator", /* tp_name */
3667 sizeof(dictiterobject), /* tp_basicsize */
3668 0, /* tp_itemsize */
3669 /* methods */
3670 (destructor)dictiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003671 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003672 0, /* tp_getattr */
3673 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003674 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003675 0, /* tp_repr */
3676 0, /* tp_as_number */
3677 0, /* tp_as_sequence */
3678 0, /* tp_as_mapping */
3679 0, /* tp_hash */
3680 0, /* tp_call */
3681 0, /* tp_str */
3682 PyObject_GenericGetAttr, /* tp_getattro */
3683 0, /* tp_setattro */
3684 0, /* tp_as_buffer */
3685 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3686 0, /* tp_doc */
3687 (traverseproc)dictiter_traverse, /* tp_traverse */
3688 0, /* tp_clear */
3689 0, /* tp_richcompare */
3690 0, /* tp_weaklistoffset */
3691 PyObject_SelfIter, /* tp_iter */
3692 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
3693 dictiter_methods, /* tp_methods */
3694 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003695};
3696
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003697static PyObject *
3698dictiter_iternextvalue(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003699{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003700 PyObject *value;
INADA Naokica2d8be2016-11-04 16:59:10 +09003701 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003702 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003704 if (d == NULL)
3705 return NULL;
3706 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003708 if (di->di_used != d->ma_used) {
3709 PyErr_SetString(PyExc_RuntimeError,
3710 "dictionary changed size during iteration");
3711 di->di_used = -1; /* Make this state sticky */
3712 return NULL;
3713 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003715 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003716 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003717 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003718 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003719 goto fail;
INADA Naokica2d8be2016-11-04 16:59:10 +09003720 value = d->ma_values[i];
3721 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003722 }
3723 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003724 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003725 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3726 while (i < n && entry_ptr->me_value == NULL) {
3727 entry_ptr++;
3728 i++;
3729 }
3730 if (i >= n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003731 goto fail;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003732 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003733 }
Thomas Perlb8311cf2019-04-02 11:30:10 +02003734 // We found an element, but did not expect it
3735 if (di->len == 0) {
3736 PyErr_SetString(PyExc_RuntimeError,
3737 "dictionary keys changed during iteration");
3738 goto fail;
3739 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003740 di->di_pos = i+1;
3741 di->len--;
3742 Py_INCREF(value);
3743 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003744
3745fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003746 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003747 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003748 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003749}
3750
3751PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003752 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3753 "dict_valueiterator", /* tp_name */
3754 sizeof(dictiterobject), /* tp_basicsize */
3755 0, /* tp_itemsize */
3756 /* methods */
3757 (destructor)dictiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003758 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003759 0, /* tp_getattr */
3760 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003761 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003762 0, /* tp_repr */
3763 0, /* tp_as_number */
3764 0, /* tp_as_sequence */
3765 0, /* tp_as_mapping */
3766 0, /* tp_hash */
3767 0, /* tp_call */
3768 0, /* tp_str */
3769 PyObject_GenericGetAttr, /* tp_getattro */
3770 0, /* tp_setattro */
3771 0, /* tp_as_buffer */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003772 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003773 0, /* tp_doc */
3774 (traverseproc)dictiter_traverse, /* tp_traverse */
3775 0, /* tp_clear */
3776 0, /* tp_richcompare */
3777 0, /* tp_weaklistoffset */
3778 PyObject_SelfIter, /* tp_iter */
3779 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3780 dictiter_methods, /* tp_methods */
3781 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003782};
3783
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003784static PyObject *
3785dictiter_iternextitem(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003786{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003787 PyObject *key, *value, *result;
INADA Naokica2d8be2016-11-04 16:59:10 +09003788 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003789 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003791 if (d == NULL)
3792 return NULL;
3793 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003794
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003795 if (di->di_used != d->ma_used) {
3796 PyErr_SetString(PyExc_RuntimeError,
3797 "dictionary changed size during iteration");
3798 di->di_used = -1; /* Make this state sticky */
3799 return NULL;
3800 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003802 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003803 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003804 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003805 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003806 goto fail;
3807 key = DK_ENTRIES(d->ma_keys)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003808 value = d->ma_values[i];
3809 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003810 }
3811 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003812 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003813 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3814 while (i < n && entry_ptr->me_value == NULL) {
3815 entry_ptr++;
3816 i++;
3817 }
3818 if (i >= n)
3819 goto fail;
3820 key = entry_ptr->me_key;
3821 value = entry_ptr->me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003822 }
Thomas Perlb8311cf2019-04-02 11:30:10 +02003823 // We found an element, but did not expect it
3824 if (di->len == 0) {
3825 PyErr_SetString(PyExc_RuntimeError,
3826 "dictionary keys changed during iteration");
3827 goto fail;
3828 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003829 di->di_pos = i+1;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003830 di->len--;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003831 Py_INCREF(key);
3832 Py_INCREF(value);
3833 result = di->di_result;
3834 if (Py_REFCNT(result) == 1) {
3835 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3836 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3837 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3838 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003839 Py_INCREF(result);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003840 Py_DECREF(oldkey);
3841 Py_DECREF(oldvalue);
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003842 }
3843 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003844 result = PyTuple_New(2);
3845 if (result == NULL)
3846 return NULL;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003847 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3848 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003849 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003850 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003851
3852fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003853 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003854 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003855 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003856}
3857
3858PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003859 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3860 "dict_itemiterator", /* tp_name */
3861 sizeof(dictiterobject), /* tp_basicsize */
3862 0, /* tp_itemsize */
3863 /* methods */
3864 (destructor)dictiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003865 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003866 0, /* tp_getattr */
3867 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003868 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003869 0, /* tp_repr */
3870 0, /* tp_as_number */
3871 0, /* tp_as_sequence */
3872 0, /* tp_as_mapping */
3873 0, /* tp_hash */
3874 0, /* tp_call */
3875 0, /* tp_str */
3876 PyObject_GenericGetAttr, /* tp_getattro */
3877 0, /* tp_setattro */
3878 0, /* tp_as_buffer */
3879 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3880 0, /* tp_doc */
3881 (traverseproc)dictiter_traverse, /* tp_traverse */
3882 0, /* tp_clear */
3883 0, /* tp_richcompare */
3884 0, /* tp_weaklistoffset */
3885 PyObject_SelfIter, /* tp_iter */
3886 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3887 dictiter_methods, /* tp_methods */
3888 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003889};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003890
3891
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003892/* dictreviter */
3893
3894static PyObject *
3895dictreviter_iternext(dictiterobject *di)
3896{
3897 PyDictObject *d = di->di_dict;
3898
3899 if (d == NULL) {
3900 return NULL;
3901 }
3902 assert (PyDict_Check(d));
3903
3904 if (di->di_used != d->ma_used) {
3905 PyErr_SetString(PyExc_RuntimeError,
3906 "dictionary changed size during iteration");
3907 di->di_used = -1; /* Make this state sticky */
3908 return NULL;
3909 }
3910
3911 Py_ssize_t i = di->di_pos;
3912 PyDictKeysObject *k = d->ma_keys;
3913 PyObject *key, *value, *result;
3914
Serhiy Storchaka2e3d8732019-10-23 14:48:08 +03003915 if (i < 0) {
3916 goto fail;
3917 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003918 if (d->ma_values) {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003919 key = DK_ENTRIES(k)[i].me_key;
3920 value = d->ma_values[i];
3921 assert (value != NULL);
3922 }
3923 else {
3924 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
Serhiy Storchaka2e3d8732019-10-23 14:48:08 +03003925 while (entry_ptr->me_value == NULL) {
3926 if (--i < 0) {
3927 goto fail;
3928 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003929 entry_ptr--;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003930 }
3931 key = entry_ptr->me_key;
3932 value = entry_ptr->me_value;
3933 }
3934 di->di_pos = i-1;
3935 di->len--;
3936
Dong-hee Na1b55b652020-02-17 19:09:15 +09003937 if (Py_IS_TYPE(di, &PyDictRevIterKey_Type)) {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003938 Py_INCREF(key);
3939 return key;
3940 }
Dong-hee Na1b55b652020-02-17 19:09:15 +09003941 else if (Py_IS_TYPE(di, &PyDictRevIterValue_Type)) {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003942 Py_INCREF(value);
3943 return value;
3944 }
Dong-hee Na1b55b652020-02-17 19:09:15 +09003945 else if (Py_IS_TYPE(di, &PyDictRevIterItem_Type)) {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003946 Py_INCREF(key);
3947 Py_INCREF(value);
3948 result = di->di_result;
3949 if (Py_REFCNT(result) == 1) {
3950 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3951 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3952 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3953 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
3954 Py_INCREF(result);
3955 Py_DECREF(oldkey);
3956 Py_DECREF(oldvalue);
3957 }
3958 else {
3959 result = PyTuple_New(2);
3960 if (result == NULL) {
3961 return NULL;
3962 }
3963 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3964 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
3965 }
3966 return result;
3967 }
3968 else {
3969 Py_UNREACHABLE();
3970 }
3971
3972fail:
3973 di->di_dict = NULL;
3974 Py_DECREF(d);
3975 return NULL;
3976}
3977
3978PyTypeObject PyDictRevIterKey_Type = {
3979 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3980 "dict_reversekeyiterator",
3981 sizeof(dictiterobject),
3982 .tp_dealloc = (destructor)dictiter_dealloc,
3983 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
3984 .tp_traverse = (traverseproc)dictiter_traverse,
3985 .tp_iter = PyObject_SelfIter,
3986 .tp_iternext = (iternextfunc)dictreviter_iternext,
3987 .tp_methods = dictiter_methods
3988};
3989
3990
3991/*[clinic input]
3992dict.__reversed__
3993
3994Return a reverse iterator over the dict keys.
3995[clinic start generated code]*/
3996
3997static PyObject *
3998dict___reversed___impl(PyDictObject *self)
3999/*[clinic end generated code: output=e674483336d1ed51 input=23210ef3477d8c4d]*/
4000{
4001 assert (PyDict_Check(self));
4002 return dictiter_new(self, &PyDictRevIterKey_Type);
4003}
4004
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004005static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304006dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004007{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004008 _Py_IDENTIFIER(iter);
Sergey Fedoseev63958442018-10-20 05:43:33 +05004009 /* copy the iterator state */
4010 dictiterobject tmp = *di;
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004011 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004012
Sergey Fedoseev63958442018-10-20 05:43:33 +05004013 PyObject *list = PySequence_List((PyObject*)&tmp);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004014 Py_XDECREF(tmp.di_dict);
Sergey Fedoseev63958442018-10-20 05:43:33 +05004015 if (list == NULL) {
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004016 return NULL;
4017 }
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004018 return Py_BuildValue("N(N)", _PyEval_GetBuiltinId(&PyId_iter), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004019}
4020
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004021PyTypeObject PyDictRevIterItem_Type = {
4022 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4023 "dict_reverseitemiterator",
4024 sizeof(dictiterobject),
4025 .tp_dealloc = (destructor)dictiter_dealloc,
4026 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
4027 .tp_traverse = (traverseproc)dictiter_traverse,
4028 .tp_iter = PyObject_SelfIter,
4029 .tp_iternext = (iternextfunc)dictreviter_iternext,
4030 .tp_methods = dictiter_methods
4031};
4032
4033PyTypeObject PyDictRevIterValue_Type = {
4034 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4035 "dict_reversevalueiterator",
4036 sizeof(dictiterobject),
4037 .tp_dealloc = (destructor)dictiter_dealloc,
4038 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
4039 .tp_traverse = (traverseproc)dictiter_traverse,
4040 .tp_iter = PyObject_SelfIter,
4041 .tp_iternext = (iternextfunc)dictreviter_iternext,
4042 .tp_methods = dictiter_methods
4043};
4044
Guido van Rossum3ac67412007-02-10 18:55:06 +00004045/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004046/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00004047/***********************************************/
4048
Guido van Rossumb90c8482007-02-10 01:11:45 +00004049/* The instance lay-out is the same for all three; but the type differs. */
4050
Guido van Rossumb90c8482007-02-10 01:11:45 +00004051static void
Eric Snow96c6af92015-05-29 22:21:39 -06004052dictview_dealloc(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004053{
INADA Naokia6296d32017-08-24 14:55:17 +09004054 /* bpo-31095: UnTrack is needed before calling any callbacks */
4055 _PyObject_GC_UNTRACK(dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004056 Py_XDECREF(dv->dv_dict);
4057 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00004058}
4059
4060static int
Eric Snow96c6af92015-05-29 22:21:39 -06004061dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
Antoine Pitrou7ddda782009-01-01 15:35:33 +00004062{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004063 Py_VISIT(dv->dv_dict);
4064 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004065}
4066
Guido van Rossum83825ac2007-02-10 04:54:19 +00004067static Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06004068dictview_len(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004069{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004070 Py_ssize_t len = 0;
4071 if (dv->dv_dict != NULL)
4072 len = dv->dv_dict->ma_used;
4073 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004074}
4075
Eric Snow96c6af92015-05-29 22:21:39 -06004076PyObject *
4077_PyDictView_New(PyObject *dict, PyTypeObject *type)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004078{
Eric Snow96c6af92015-05-29 22:21:39 -06004079 _PyDictViewObject *dv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004080 if (dict == NULL) {
4081 PyErr_BadInternalCall();
4082 return NULL;
4083 }
4084 if (!PyDict_Check(dict)) {
4085 /* XXX Get rid of this restriction later */
4086 PyErr_Format(PyExc_TypeError,
4087 "%s() requires a dict argument, not '%s'",
Victor Stinner58ac7002020-02-07 03:04:21 +01004088 type->tp_name, Py_TYPE(dict)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004089 return NULL;
4090 }
Eric Snow96c6af92015-05-29 22:21:39 -06004091 dv = PyObject_GC_New(_PyDictViewObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004092 if (dv == NULL)
4093 return NULL;
4094 Py_INCREF(dict);
4095 dv->dv_dict = (PyDictObject *)dict;
4096 _PyObject_GC_TRACK(dv);
4097 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004098}
4099
Neal Norwitze36f2ba2007-02-26 23:12:28 +00004100/* TODO(guido): The views objects are not complete:
4101
4102 * support more set operations
4103 * support arbitrary mappings?
4104 - either these should be static or exported in dictobject.h
4105 - if public then they should probably be in builtins
4106*/
4107
Guido van Rossumaac530c2007-08-24 22:33:45 +00004108/* Return 1 if self is a subset of other, iterating over self;
4109 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00004110static int
4111all_contained_in(PyObject *self, PyObject *other)
4112{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004113 PyObject *iter = PyObject_GetIter(self);
4114 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00004115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004116 if (iter == NULL)
4117 return -1;
4118 for (;;) {
4119 PyObject *next = PyIter_Next(iter);
4120 if (next == NULL) {
4121 if (PyErr_Occurred())
4122 ok = -1;
4123 break;
4124 }
4125 ok = PySequence_Contains(other, next);
4126 Py_DECREF(next);
4127 if (ok <= 0)
4128 break;
4129 }
4130 Py_DECREF(iter);
4131 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00004132}
4133
4134static PyObject *
4135dictview_richcompare(PyObject *self, PyObject *other, int op)
4136{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004137 Py_ssize_t len_self, len_other;
4138 int ok;
4139 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004141 assert(self != NULL);
4142 assert(PyDictViewSet_Check(self));
4143 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00004144
Brian Curtindfc80e32011-08-10 20:28:54 -05004145 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
4146 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004148 len_self = PyObject_Size(self);
4149 if (len_self < 0)
4150 return NULL;
4151 len_other = PyObject_Size(other);
4152 if (len_other < 0)
4153 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004155 ok = 0;
4156 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00004157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004158 case Py_NE:
4159 case Py_EQ:
4160 if (len_self == len_other)
4161 ok = all_contained_in(self, other);
4162 if (op == Py_NE && ok >= 0)
4163 ok = !ok;
4164 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004166 case Py_LT:
4167 if (len_self < len_other)
4168 ok = all_contained_in(self, other);
4169 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004171 case Py_LE:
4172 if (len_self <= len_other)
4173 ok = all_contained_in(self, other);
4174 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004175
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004176 case Py_GT:
4177 if (len_self > len_other)
4178 ok = all_contained_in(other, self);
4179 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004181 case Py_GE:
4182 if (len_self >= len_other)
4183 ok = all_contained_in(other, self);
4184 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004186 }
4187 if (ok < 0)
4188 return NULL;
4189 result = ok ? Py_True : Py_False;
4190 Py_INCREF(result);
4191 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00004192}
4193
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004194static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004195dictview_repr(_PyDictViewObject *dv)
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004196{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004197 PyObject *seq;
bennorthd7773d92018-01-26 15:46:01 +00004198 PyObject *result = NULL;
4199 Py_ssize_t rc;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004200
bennorthd7773d92018-01-26 15:46:01 +00004201 rc = Py_ReprEnter((PyObject *)dv);
4202 if (rc != 0) {
4203 return rc > 0 ? PyUnicode_FromString("...") : NULL;
4204 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004205 seq = PySequence_List((PyObject *)dv);
bennorthd7773d92018-01-26 15:46:01 +00004206 if (seq == NULL) {
4207 goto Done;
4208 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004209 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
4210 Py_DECREF(seq);
bennorthd7773d92018-01-26 15:46:01 +00004211
4212Done:
4213 Py_ReprLeave((PyObject *)dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004214 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004215}
4216
Guido van Rossum3ac67412007-02-10 18:55:06 +00004217/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004218
4219static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004220dictkeys_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004221{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004222 if (dv->dv_dict == NULL) {
4223 Py_RETURN_NONE;
4224 }
4225 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004226}
4227
4228static int
Eric Snow96c6af92015-05-29 22:21:39 -06004229dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004230{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004231 if (dv->dv_dict == NULL)
4232 return 0;
4233 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004234}
4235
Guido van Rossum83825ac2007-02-10 04:54:19 +00004236static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004237 (lenfunc)dictview_len, /* sq_length */
4238 0, /* sq_concat */
4239 0, /* sq_repeat */
4240 0, /* sq_item */
4241 0, /* sq_slice */
4242 0, /* sq_ass_item */
4243 0, /* sq_ass_slice */
4244 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004245};
4246
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004247// Create an set object from dictviews object.
4248// Returns a new reference.
4249// This utility function is used by set operations.
Guido van Rossum523259b2007-08-24 23:41:22 +00004250static PyObject*
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004251dictviews_to_set(PyObject *self)
Guido van Rossum523259b2007-08-24 23:41:22 +00004252{
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004253 PyObject *left = self;
4254 if (PyDictKeys_Check(self)) {
4255 // PySet_New() has fast path for the dict object.
4256 PyObject *dict = (PyObject *)((_PyDictViewObject *)self)->dv_dict;
4257 if (PyDict_CheckExact(dict)) {
4258 left = dict;
4259 }
4260 }
4261 return PySet_New(left);
4262}
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004263
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004264static PyObject*
4265dictviews_sub(PyObject *self, PyObject *other)
4266{
4267 PyObject *result = dictviews_to_set(self);
4268 if (result == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004269 return NULL;
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004270 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004271
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004272 _Py_IDENTIFIER(difference_update);
4273 PyObject *tmp = _PyObject_CallMethodIdOneArg(
4274 result, &PyId_difference_update, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004275 if (tmp == NULL) {
4276 Py_DECREF(result);
4277 return NULL;
4278 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004280 Py_DECREF(tmp);
4281 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004282}
4283
Forest Gregg998cf1f2019-08-26 02:17:43 -05004284static int
4285dictitems_contains(_PyDictViewObject *dv, PyObject *obj);
4286
4287PyObject *
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004288_PyDictView_Intersect(PyObject* self, PyObject *other)
Guido van Rossum523259b2007-08-24 23:41:22 +00004289{
Forest Gregg998cf1f2019-08-26 02:17:43 -05004290 PyObject *result;
4291 PyObject *it;
4292 PyObject *key;
4293 Py_ssize_t len_self;
4294 int rv;
4295 int (*dict_contains)(_PyDictViewObject *, PyObject *);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004296
Forest Gregg998cf1f2019-08-26 02:17:43 -05004297 /* Python interpreter swaps parameters when dict view
4298 is on right side of & */
4299 if (!PyDictViewSet_Check(self)) {
4300 PyObject *tmp = other;
4301 other = self;
4302 self = tmp;
4303 }
4304
4305 len_self = dictview_len((_PyDictViewObject *)self);
4306
4307 /* if other is a set and self is smaller than other,
4308 reuse set intersection logic */
Dong-hee Na1b55b652020-02-17 19:09:15 +09004309 if (Py_IS_TYPE(other, &PySet_Type) && len_self <= PyObject_Size(other)) {
Forest Gregg998cf1f2019-08-26 02:17:43 -05004310 _Py_IDENTIFIER(intersection);
4311 return _PyObject_CallMethodIdObjArgs(other, &PyId_intersection, self, NULL);
4312 }
4313
4314 /* if other is another dict view, and it is bigger than self,
4315 swap them */
4316 if (PyDictViewSet_Check(other)) {
4317 Py_ssize_t len_other = dictview_len((_PyDictViewObject *)other);
4318 if (len_other > len_self) {
4319 PyObject *tmp = other;
4320 other = self;
4321 self = tmp;
4322 }
4323 }
4324
4325 /* at this point, two things should be true
4326 1. self is a dictview
4327 2. if other is a dictview then it is smaller than self */
4328 result = PySet_New(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004329 if (result == NULL)
4330 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004331
Forest Gregg998cf1f2019-08-26 02:17:43 -05004332 it = PyObject_GetIter(other);
Zackery Spytzb16e3822019-10-13 05:49:05 -06004333 if (it == NULL) {
4334 Py_DECREF(result);
4335 return NULL;
4336 }
Forest Gregg998cf1f2019-08-26 02:17:43 -05004337
Forest Gregg998cf1f2019-08-26 02:17:43 -05004338 if (PyDictKeys_Check(self)) {
4339 dict_contains = dictkeys_contains;
4340 }
4341 /* else PyDictItems_Check(self) */
4342 else {
4343 dict_contains = dictitems_contains;
4344 }
4345
4346 while ((key = PyIter_Next(it)) != NULL) {
4347 rv = dict_contains((_PyDictViewObject *)self, key);
4348 if (rv < 0) {
4349 goto error;
4350 }
4351 if (rv) {
4352 if (PySet_Add(result, key)) {
4353 goto error;
4354 }
4355 }
4356 Py_DECREF(key);
4357 }
4358 Py_DECREF(it);
4359 if (PyErr_Occurred()) {
4360 Py_DECREF(result);
4361 return NULL;
4362 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004363 return result;
Forest Gregg998cf1f2019-08-26 02:17:43 -05004364
4365error:
4366 Py_DECREF(it);
4367 Py_DECREF(result);
4368 Py_DECREF(key);
4369 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004370}
4371
4372static PyObject*
4373dictviews_or(PyObject* self, PyObject *other)
4374{
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004375 PyObject *result = dictviews_to_set(self);
4376 if (result == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004377 return NULL;
4378 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004379
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004380 if (_PySet_Update(result, other) < 0) {
4381 Py_DECREF(result);
4382 return NULL;
4383 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004384 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004385}
4386
4387static PyObject*
4388dictviews_xor(PyObject* self, PyObject *other)
4389{
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004390 PyObject *result = dictviews_to_set(self);
4391 if (result == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004392 return NULL;
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004393 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004394
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004395 _Py_IDENTIFIER(symmetric_difference_update);
4396 PyObject *tmp = _PyObject_CallMethodIdOneArg(
4397 result, &PyId_symmetric_difference_update, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004398 if (tmp == NULL) {
4399 Py_DECREF(result);
4400 return NULL;
4401 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004403 Py_DECREF(tmp);
4404 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004405}
4406
4407static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004408 0, /*nb_add*/
4409 (binaryfunc)dictviews_sub, /*nb_subtract*/
4410 0, /*nb_multiply*/
4411 0, /*nb_remainder*/
4412 0, /*nb_divmod*/
4413 0, /*nb_power*/
4414 0, /*nb_negative*/
4415 0, /*nb_positive*/
4416 0, /*nb_absolute*/
4417 0, /*nb_bool*/
4418 0, /*nb_invert*/
4419 0, /*nb_lshift*/
4420 0, /*nb_rshift*/
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004421 (binaryfunc)_PyDictView_Intersect, /*nb_and*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004422 (binaryfunc)dictviews_xor, /*nb_xor*/
4423 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00004424};
4425
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004426static PyObject*
4427dictviews_isdisjoint(PyObject *self, PyObject *other)
4428{
4429 PyObject *it;
4430 PyObject *item = NULL;
4431
4432 if (self == other) {
Eric Snow96c6af92015-05-29 22:21:39 -06004433 if (dictview_len((_PyDictViewObject *)self) == 0)
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004434 Py_RETURN_TRUE;
4435 else
4436 Py_RETURN_FALSE;
4437 }
4438
4439 /* Iterate over the shorter object (only if other is a set,
4440 * because PySequence_Contains may be expensive otherwise): */
4441 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
Eric Snow96c6af92015-05-29 22:21:39 -06004442 Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self);
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004443 Py_ssize_t len_other = PyObject_Size(other);
4444 if (len_other == -1)
4445 return NULL;
4446
4447 if ((len_other > len_self)) {
4448 PyObject *tmp = other;
4449 other = self;
4450 self = tmp;
4451 }
4452 }
4453
4454 it = PyObject_GetIter(other);
4455 if (it == NULL)
4456 return NULL;
4457
4458 while ((item = PyIter_Next(it)) != NULL) {
4459 int contains = PySequence_Contains(self, item);
4460 Py_DECREF(item);
4461 if (contains == -1) {
4462 Py_DECREF(it);
4463 return NULL;
4464 }
4465
4466 if (contains) {
4467 Py_DECREF(it);
4468 Py_RETURN_FALSE;
4469 }
4470 }
4471 Py_DECREF(it);
4472 if (PyErr_Occurred())
4473 return NULL; /* PyIter_Next raised an exception. */
4474 Py_RETURN_TRUE;
4475}
4476
4477PyDoc_STRVAR(isdisjoint_doc,
4478"Return True if the view and the given iterable have a null intersection.");
4479
Serhiy Storchaka81524022018-11-27 13:05:02 +02004480static PyObject* dictkeys_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored));
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004481
4482PyDoc_STRVAR(reversed_keys_doc,
4483"Return a reverse iterator over the dict keys.");
4484
Guido van Rossumb90c8482007-02-10 01:11:45 +00004485static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004486 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4487 isdisjoint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004488 {"__reversed__", (PyCFunction)(void(*)(void))dictkeys_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004489 reversed_keys_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004490 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004491};
4492
4493PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004494 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4495 "dict_keys", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004496 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004497 0, /* tp_itemsize */
4498 /* methods */
4499 (destructor)dictview_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004500 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004501 0, /* tp_getattr */
4502 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004503 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004504 (reprfunc)dictview_repr, /* tp_repr */
4505 &dictviews_as_number, /* tp_as_number */
4506 &dictkeys_as_sequence, /* tp_as_sequence */
4507 0, /* tp_as_mapping */
4508 0, /* tp_hash */
4509 0, /* tp_call */
4510 0, /* tp_str */
4511 PyObject_GenericGetAttr, /* tp_getattro */
4512 0, /* tp_setattro */
4513 0, /* tp_as_buffer */
4514 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4515 0, /* tp_doc */
4516 (traverseproc)dictview_traverse, /* tp_traverse */
4517 0, /* tp_clear */
4518 dictview_richcompare, /* tp_richcompare */
4519 0, /* tp_weaklistoffset */
4520 (getiterfunc)dictkeys_iter, /* tp_iter */
4521 0, /* tp_iternext */
4522 dictkeys_methods, /* tp_methods */
4523 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004524};
4525
4526static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304527dictkeys_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004528{
Eric Snow96c6af92015-05-29 22:21:39 -06004529 return _PyDictView_New(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004530}
4531
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004532static PyObject *
Serhiy Storchaka81524022018-11-27 13:05:02 +02004533dictkeys_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored))
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004534{
4535 if (dv->dv_dict == NULL) {
4536 Py_RETURN_NONE;
4537 }
4538 return dictiter_new(dv->dv_dict, &PyDictRevIterKey_Type);
4539}
4540
Guido van Rossum3ac67412007-02-10 18:55:06 +00004541/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004542
4543static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004544dictitems_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004545{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004546 if (dv->dv_dict == NULL) {
4547 Py_RETURN_NONE;
4548 }
4549 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004550}
4551
4552static int
Eric Snow96c6af92015-05-29 22:21:39 -06004553dictitems_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004554{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004555 int result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004556 PyObject *key, *value, *found;
4557 if (dv->dv_dict == NULL)
4558 return 0;
4559 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
4560 return 0;
4561 key = PyTuple_GET_ITEM(obj, 0);
4562 value = PyTuple_GET_ITEM(obj, 1);
Raymond Hettinger6692f012016-09-18 21:46:08 -07004563 found = PyDict_GetItemWithError((PyObject *)dv->dv_dict, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004564 if (found == NULL) {
4565 if (PyErr_Occurred())
4566 return -1;
4567 return 0;
4568 }
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004569 Py_INCREF(found);
Serhiy Storchaka18b711c2019-08-04 14:12:48 +03004570 result = PyObject_RichCompareBool(found, value, Py_EQ);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004571 Py_DECREF(found);
4572 return result;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004573}
4574
Guido van Rossum83825ac2007-02-10 04:54:19 +00004575static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004576 (lenfunc)dictview_len, /* sq_length */
4577 0, /* sq_concat */
4578 0, /* sq_repeat */
4579 0, /* sq_item */
4580 0, /* sq_slice */
4581 0, /* sq_ass_item */
4582 0, /* sq_ass_slice */
4583 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004584};
4585
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004586static PyObject* dictitems_reversed(_PyDictViewObject *dv);
4587
4588PyDoc_STRVAR(reversed_items_doc,
4589"Return a reverse iterator over the dict items.");
4590
Guido van Rossumb90c8482007-02-10 01:11:45 +00004591static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004592 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4593 isdisjoint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004594 {"__reversed__", (PyCFunction)(void(*)(void))dictitems_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004595 reversed_items_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004596 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004597};
4598
4599PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004600 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4601 "dict_items", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004602 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004603 0, /* tp_itemsize */
4604 /* methods */
4605 (destructor)dictview_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004606 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004607 0, /* tp_getattr */
4608 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004609 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004610 (reprfunc)dictview_repr, /* tp_repr */
4611 &dictviews_as_number, /* tp_as_number */
4612 &dictitems_as_sequence, /* tp_as_sequence */
4613 0, /* tp_as_mapping */
4614 0, /* tp_hash */
4615 0, /* tp_call */
4616 0, /* tp_str */
4617 PyObject_GenericGetAttr, /* tp_getattro */
4618 0, /* tp_setattro */
4619 0, /* tp_as_buffer */
4620 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4621 0, /* tp_doc */
4622 (traverseproc)dictview_traverse, /* tp_traverse */
4623 0, /* tp_clear */
4624 dictview_richcompare, /* tp_richcompare */
4625 0, /* tp_weaklistoffset */
4626 (getiterfunc)dictitems_iter, /* tp_iter */
4627 0, /* tp_iternext */
4628 dictitems_methods, /* tp_methods */
4629 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004630};
4631
4632static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304633dictitems_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004634{
Eric Snow96c6af92015-05-29 22:21:39 -06004635 return _PyDictView_New(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004636}
4637
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004638static PyObject *
4639dictitems_reversed(_PyDictViewObject *dv)
4640{
4641 if (dv->dv_dict == NULL) {
4642 Py_RETURN_NONE;
4643 }
4644 return dictiter_new(dv->dv_dict, &PyDictRevIterItem_Type);
4645}
4646
Guido van Rossum3ac67412007-02-10 18:55:06 +00004647/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004648
4649static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004650dictvalues_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004651{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004652 if (dv->dv_dict == NULL) {
4653 Py_RETURN_NONE;
4654 }
4655 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004656}
4657
Guido van Rossum83825ac2007-02-10 04:54:19 +00004658static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004659 (lenfunc)dictview_len, /* sq_length */
4660 0, /* sq_concat */
4661 0, /* sq_repeat */
4662 0, /* sq_item */
4663 0, /* sq_slice */
4664 0, /* sq_ass_item */
4665 0, /* sq_ass_slice */
4666 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004667};
4668
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004669static PyObject* dictvalues_reversed(_PyDictViewObject *dv);
4670
4671PyDoc_STRVAR(reversed_values_doc,
4672"Return a reverse iterator over the dict values.");
4673
Guido van Rossumb90c8482007-02-10 01:11:45 +00004674static PyMethodDef dictvalues_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004675 {"__reversed__", (PyCFunction)(void(*)(void))dictvalues_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004676 reversed_values_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004677 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004678};
4679
4680PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004681 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4682 "dict_values", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004683 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004684 0, /* tp_itemsize */
4685 /* methods */
4686 (destructor)dictview_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004687 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004688 0, /* tp_getattr */
4689 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004690 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004691 (reprfunc)dictview_repr, /* tp_repr */
4692 0, /* tp_as_number */
4693 &dictvalues_as_sequence, /* tp_as_sequence */
4694 0, /* tp_as_mapping */
4695 0, /* tp_hash */
4696 0, /* tp_call */
4697 0, /* tp_str */
4698 PyObject_GenericGetAttr, /* tp_getattro */
4699 0, /* tp_setattro */
4700 0, /* tp_as_buffer */
4701 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4702 0, /* tp_doc */
4703 (traverseproc)dictview_traverse, /* tp_traverse */
4704 0, /* tp_clear */
4705 0, /* tp_richcompare */
4706 0, /* tp_weaklistoffset */
4707 (getiterfunc)dictvalues_iter, /* tp_iter */
4708 0, /* tp_iternext */
4709 dictvalues_methods, /* tp_methods */
4710 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004711};
4712
4713static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304714dictvalues_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004715{
Eric Snow96c6af92015-05-29 22:21:39 -06004716 return _PyDictView_New(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004717}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004718
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004719static PyObject *
4720dictvalues_reversed(_PyDictViewObject *dv)
4721{
4722 if (dv->dv_dict == NULL) {
4723 Py_RETURN_NONE;
4724 }
4725 return dictiter_new(dv->dv_dict, &PyDictRevIterValue_Type);
4726}
4727
4728
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004729/* Returns NULL if cannot allocate a new PyDictKeysObject,
4730 but does not set an error */
4731PyDictKeysObject *
4732_PyDict_NewKeysForClass(void)
4733{
Victor Stinner742da042016-09-07 17:40:12 -07004734 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004735 if (keys == NULL)
4736 PyErr_Clear();
4737 else
4738 keys->dk_lookup = lookdict_split;
4739 return keys;
4740}
4741
4742#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
4743
4744PyObject *
4745PyObject_GenericGetDict(PyObject *obj, void *context)
4746{
4747 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
4748 if (dictptr == NULL) {
4749 PyErr_SetString(PyExc_AttributeError,
4750 "This object has no __dict__");
4751 return NULL;
4752 }
4753 dict = *dictptr;
4754 if (dict == NULL) {
4755 PyTypeObject *tp = Py_TYPE(obj);
4756 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
INADA Naokia7576492018-11-14 18:39:27 +09004757 dictkeys_incref(CACHED_KEYS(tp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004758 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
4759 }
4760 else {
4761 *dictptr = dict = PyDict_New();
4762 }
4763 }
4764 Py_XINCREF(dict);
4765 return dict;
4766}
4767
4768int
4769_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
Victor Stinner742da042016-09-07 17:40:12 -07004770 PyObject *key, PyObject *value)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004771{
4772 PyObject *dict;
4773 int res;
4774 PyDictKeysObject *cached;
4775
4776 assert(dictptr != NULL);
4777 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
4778 assert(dictptr != NULL);
4779 dict = *dictptr;
4780 if (dict == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +09004781 dictkeys_incref(cached);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004782 dict = new_dict_with_shared_keys(cached);
4783 if (dict == NULL)
4784 return -1;
4785 *dictptr = dict;
4786 }
4787 if (value == NULL) {
4788 res = PyDict_DelItem(dict, key);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004789 // Since key sharing dict doesn't allow deletion, PyDict_DelItem()
4790 // always converts dict to combined form.
4791 if ((cached = CACHED_KEYS(tp)) != NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004792 CACHED_KEYS(tp) = NULL;
INADA Naokia7576492018-11-14 18:39:27 +09004793 dictkeys_decref(cached);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004794 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01004795 }
4796 else {
INADA Naoki2294f3a2017-02-12 13:51:30 +09004797 int was_shared = (cached == ((PyDictObject *)dict)->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004798 res = PyDict_SetItem(dict, key, value);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004799 if (was_shared &&
4800 (cached = CACHED_KEYS(tp)) != NULL &&
4801 cached != ((PyDictObject *)dict)->ma_keys) {
Victor Stinner3d3f2642016-12-15 17:21:23 +01004802 /* PyDict_SetItem() may call dictresize and convert split table
4803 * into combined table. In such case, convert it to split
4804 * table again and update type's shared key only when this is
4805 * the only dict sharing key with the type.
4806 *
4807 * This is to allow using shared key in class like this:
4808 *
4809 * class C:
4810 * def __init__(self):
4811 * # one dict resize happens
4812 * self.a, self.b, self.c = 1, 2, 3
4813 * self.d, self.e, self.f = 4, 5, 6
4814 * a = C()
4815 */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004816 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004817 CACHED_KEYS(tp) = make_keys_shared(dict);
Victor Stinner742da042016-09-07 17:40:12 -07004818 }
4819 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004820 CACHED_KEYS(tp) = NULL;
4821 }
INADA Naokia7576492018-11-14 18:39:27 +09004822 dictkeys_decref(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004823 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
4824 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004825 }
4826 }
4827 } else {
4828 dict = *dictptr;
4829 if (dict == NULL) {
4830 dict = PyDict_New();
4831 if (dict == NULL)
4832 return -1;
4833 *dictptr = dict;
4834 }
4835 if (value == NULL) {
4836 res = PyDict_DelItem(dict, key);
4837 } else {
4838 res = PyDict_SetItem(dict, key, value);
4839 }
4840 }
4841 return res;
4842}
4843
4844void
4845_PyDictKeys_DecRef(PyDictKeysObject *keys)
4846{
INADA Naokia7576492018-11-14 18:39:27 +09004847 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004848}