blob: 9c35f3c3f14d01f09f0256356712d20335a88a0e [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
Victor Stinnerae00a5a2020-04-29 02:29:20 +0200260void
261_PyDict_ClearFreeList(void)
Christian Heimes77c02eb2008-02-09 02:18:51 +0000262{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 while (numfree) {
Victor Stinnerae00a5a2020-04-29 02:29:20 +0200264 PyDictObject *op = free_list[--numfree];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 assert(PyDict_CheckExact(op));
266 PyObject_GC_Del(op);
267 }
Victor Stinner742da042016-09-07 17:40:12 -0700268 while (numfreekeys) {
269 PyObject_FREE(keys_free_list[--numfreekeys]);
270 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100271}
272
David Malcolm49526f42012-06-22 14:55:41 -0400273/* Print summary info about the state of the optimized allocator */
274void
275_PyDict_DebugMallocStats(FILE *out)
276{
277 _PyDebugAllocatorStats(out,
278 "free PyDictObject", numfree, sizeof(PyDictObject));
279}
280
281
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100282void
Victor Stinnerbed48172019-08-27 00:12:32 +0200283_PyDict_Fini(void)
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100284{
Victor Stinnerae00a5a2020-04-29 02:29:20 +0200285 _PyDict_ClearFreeList();
Christian Heimes77c02eb2008-02-09 02:18:51 +0000286}
287
Victor Stinner742da042016-09-07 17:40:12 -0700288#define DK_SIZE(dk) ((dk)->dk_size)
289#if SIZEOF_VOID_P > 4
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700290#define DK_IXSIZE(dk) \
291 (DK_SIZE(dk) <= 0xff ? \
292 1 : DK_SIZE(dk) <= 0xffff ? \
293 2 : DK_SIZE(dk) <= 0xffffffff ? \
Benjamin Peterson3c569292016-09-08 13:16:41 -0700294 4 : sizeof(int64_t))
Victor Stinner742da042016-09-07 17:40:12 -0700295#else
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700296#define DK_IXSIZE(dk) \
297 (DK_SIZE(dk) <= 0xff ? \
298 1 : DK_SIZE(dk) <= 0xffff ? \
Benjamin Peterson3c569292016-09-08 13:16:41 -0700299 2 : sizeof(int32_t))
Victor Stinner742da042016-09-07 17:40:12 -0700300#endif
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700301#define DK_ENTRIES(dk) \
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700302 ((PyDictKeyEntry*)(&((int8_t*)((dk)->dk_indices))[DK_SIZE(dk) * DK_IXSIZE(dk)]))
Victor Stinner742da042016-09-07 17:40:12 -0700303
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400304#define DK_MASK(dk) (((dk)->dk_size)-1)
305#define IS_POWER_OF_2(x) (((x) & (x-1)) == 0)
306
INADA Naokia7576492018-11-14 18:39:27 +0900307static void free_keys_object(PyDictKeysObject *keys);
308
309static inline void
310dictkeys_incref(PyDictKeysObject *dk)
311{
Victor Stinner49932fe2020-02-03 17:55:05 +0100312#ifdef Py_REF_DEBUG
313 _Py_RefTotal++;
314#endif
INADA Naokia7576492018-11-14 18:39:27 +0900315 dk->dk_refcnt++;
316}
317
318static inline void
319dictkeys_decref(PyDictKeysObject *dk)
320{
321 assert(dk->dk_refcnt > 0);
Victor Stinner49932fe2020-02-03 17:55:05 +0100322#ifdef Py_REF_DEBUG
323 _Py_RefTotal--;
324#endif
INADA Naokia7576492018-11-14 18:39:27 +0900325 if (--dk->dk_refcnt == 0) {
326 free_keys_object(dk);
327 }
328}
329
Victor Stinner742da042016-09-07 17:40:12 -0700330/* lookup indices. returns DKIX_EMPTY, DKIX_DUMMY, or ix >=0 */
Benjamin Peterson73222252016-09-08 09:58:47 -0700331static inline Py_ssize_t
Andy Lester62d21c92020-03-25 23:13:01 -0500332dictkeys_get_index(const PyDictKeysObject *keys, Py_ssize_t i)
Victor Stinner742da042016-09-07 17:40:12 -0700333{
334 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700335 Py_ssize_t ix;
336
Victor Stinner742da042016-09-07 17:40:12 -0700337 if (s <= 0xff) {
Andy Lester62d21c92020-03-25 23:13:01 -0500338 const int8_t *indices = (const int8_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700339 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700340 }
341 else if (s <= 0xffff) {
Andy Lester62d21c92020-03-25 23:13:01 -0500342 const int16_t *indices = (const int16_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700343 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700344 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700345#if SIZEOF_VOID_P > 4
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300346 else if (s > 0xffffffff) {
Andy Lester62d21c92020-03-25 23:13:01 -0500347 const int64_t *indices = (const int64_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700348 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700349 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700350#endif
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300351 else {
Andy Lester62d21c92020-03-25 23:13:01 -0500352 const int32_t *indices = (const int32_t*)(keys->dk_indices);
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300353 ix = indices[i];
354 }
Victor Stinner71211e32016-09-08 10:52:46 -0700355 assert(ix >= DKIX_DUMMY);
356 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700357}
358
359/* write to indices. */
Benjamin Peterson73222252016-09-08 09:58:47 -0700360static inline void
INADA Naokia7576492018-11-14 18:39:27 +0900361dictkeys_set_index(PyDictKeysObject *keys, Py_ssize_t i, Py_ssize_t ix)
Victor Stinner742da042016-09-07 17:40:12 -0700362{
363 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700364
365 assert(ix >= DKIX_DUMMY);
366
Victor Stinner742da042016-09-07 17:40:12 -0700367 if (s <= 0xff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700368 int8_t *indices = (int8_t*)(keys->dk_indices);
Victor Stinner71211e32016-09-08 10:52:46 -0700369 assert(ix <= 0x7f);
Victor Stinner208857e2016-09-08 11:35:46 -0700370 indices[i] = (char)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700371 }
372 else if (s <= 0xffff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700373 int16_t *indices = (int16_t*)(keys->dk_indices);
Victor Stinner71211e32016-09-08 10:52:46 -0700374 assert(ix <= 0x7fff);
Victor Stinner208857e2016-09-08 11:35:46 -0700375 indices[i] = (int16_t)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700376 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700377#if SIZEOF_VOID_P > 4
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300378 else if (s > 0xffffffff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700379 int64_t *indices = (int64_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700380 indices[i] = ix;
Victor Stinner742da042016-09-07 17:40:12 -0700381 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700382#endif
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300383 else {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700384 int32_t *indices = (int32_t*)(keys->dk_indices);
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300385 assert(ix <= 0x7fffffff);
386 indices[i] = (int32_t)ix;
387 }
Victor Stinner742da042016-09-07 17:40:12 -0700388}
389
390
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200391/* USABLE_FRACTION is the maximum dictionary load.
Victor Stinner742da042016-09-07 17:40:12 -0700392 * Increasing this ratio makes dictionaries more dense resulting in more
393 * collisions. Decreasing it improves sparseness at the expense of spreading
394 * indices over more cache lines and at the cost of total memory consumed.
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200395 *
396 * USABLE_FRACTION must obey the following:
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400397 * (0 < USABLE_FRACTION(n) < n) for all n >= 2
398 *
Victor Stinner742da042016-09-07 17:40:12 -0700399 * USABLE_FRACTION should be quick to calculate.
400 * Fractions around 1/2 to 2/3 seem to work well in practice.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400401 */
Victor Stinner742da042016-09-07 17:40:12 -0700402#define USABLE_FRACTION(n) (((n) << 1)/3)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400403
Victor Stinner742da042016-09-07 17:40:12 -0700404/* ESTIMATE_SIZE is reverse function of USABLE_FRACTION.
405 * This can be used to reserve enough size to insert n entries without
406 * resizing.
407 */
INADA Naoki92c50ee2016-11-22 00:57:02 +0900408#define ESTIMATE_SIZE(n) (((n)*3+1) >> 1)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400409
Victor Stinner742da042016-09-07 17:40:12 -0700410/* Alternative fraction that is otherwise close enough to 2n/3 to make
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400411 * little difference. 8 * 2/3 == 8 * 5/8 == 5. 16 * 2/3 == 16 * 5/8 == 10.
412 * 32 * 2/3 = 21, 32 * 5/8 = 20.
413 * Its advantage is that it is faster to compute on machines with slow division.
414 * #define USABLE_FRACTION(n) (((n) >> 1) + ((n) >> 2) - ((n) >> 3))
Victor Stinner742da042016-09-07 17:40:12 -0700415 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400416
Victor Stinnera9f61a52013-07-16 22:17:26 +0200417/* GROWTH_RATE. Growth rate upon hitting maximum load.
INADA Naoki5fbc5112018-04-17 15:53:34 +0900418 * Currently set to used*3.
Victor Stinnera9f61a52013-07-16 22:17:26 +0200419 * This means that dicts double in size when growing without deletions,
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700420 * but have more head room when the number of deletions is on a par with the
INADA Naoki5fbc5112018-04-17 15:53:34 +0900421 * number of insertions. See also bpo-17563 and bpo-33205.
422 *
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700423 * GROWTH_RATE was set to used*4 up to version 3.2.
424 * GROWTH_RATE was set to used*2 in version 3.3.0
INADA Naoki5fbc5112018-04-17 15:53:34 +0900425 * GROWTH_RATE was set to used*2 + capacity/2 in 3.4.0-3.6.0.
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200426 */
INADA Naoki5fbc5112018-04-17 15:53:34 +0900427#define GROWTH_RATE(d) ((d)->ma_used*3)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400428
429#define ENSURE_ALLOWS_DELETIONS(d) \
430 if ((d)->ma_keys->dk_lookup == lookdict_unicode_nodummy) { \
431 (d)->ma_keys->dk_lookup = lookdict_unicode; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400433
434/* This immutable, empty PyDictKeysObject is used for PyDict_Clear()
435 * (which cannot fail and thus can do no allocation).
436 */
437static PyDictKeysObject empty_keys_struct = {
Serhiy Storchaka97932e42016-09-26 23:01:23 +0300438 1, /* dk_refcnt */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400439 1, /* dk_size */
440 lookdict_split, /* dk_lookup */
441 0, /* dk_usable (immutable) */
Victor Stinner742da042016-09-07 17:40:12 -0700442 0, /* dk_nentries */
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700443 {DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY,
444 DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY}, /* dk_indices */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400445};
446
447static PyObject *empty_values[1] = { NULL };
448
449#define Py_EMPTY_KEYS &empty_keys_struct
450
Victor Stinner611b0fa2016-09-14 15:02:01 +0200451/* Uncomment to check the dict content in _PyDict_CheckConsistency() */
452/* #define DEBUG_PYDICT */
453
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200454#ifdef DEBUG_PYDICT
455# define ASSERT_CONSISTENT(op) assert(_PyDict_CheckConsistency((PyObject *)(op), 1))
456#else
457# define ASSERT_CONSISTENT(op) assert(_PyDict_CheckConsistency((PyObject *)(op), 0))
458#endif
Victor Stinner611b0fa2016-09-14 15:02:01 +0200459
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200460
461int
462_PyDict_CheckConsistency(PyObject *op, int check_content)
Victor Stinner611b0fa2016-09-14 15:02:01 +0200463{
Victor Stinner68762572019-10-07 18:42:01 +0200464#define CHECK(expr) \
465 do { if (!(expr)) { _PyObject_ASSERT_FAILED_MSG(op, Py_STRINGIFY(expr)); } } while (0)
466
467 assert(op != NULL);
468 CHECK(PyDict_Check(op));
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200469 PyDictObject *mp = (PyDictObject *)op;
Victor Stinner50fe3f82018-10-26 18:47:15 +0200470
Victor Stinner611b0fa2016-09-14 15:02:01 +0200471 PyDictKeysObject *keys = mp->ma_keys;
472 int splitted = _PyDict_HasSplitTable(mp);
473 Py_ssize_t usable = USABLE_FRACTION(keys->dk_size);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200474
Victor Stinner68762572019-10-07 18:42:01 +0200475 CHECK(0 <= mp->ma_used && mp->ma_used <= usable);
476 CHECK(IS_POWER_OF_2(keys->dk_size));
477 CHECK(0 <= keys->dk_usable && keys->dk_usable <= usable);
478 CHECK(0 <= keys->dk_nentries && keys->dk_nentries <= usable);
479 CHECK(keys->dk_usable + keys->dk_nentries <= usable);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200480
481 if (!splitted) {
482 /* combined table */
Victor Stinner68762572019-10-07 18:42:01 +0200483 CHECK(keys->dk_refcnt == 1);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200484 }
485
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200486 if (check_content) {
487 PyDictKeyEntry *entries = DK_ENTRIES(keys);
488 Py_ssize_t i;
Victor Stinner611b0fa2016-09-14 15:02:01 +0200489
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200490 for (i=0; i < keys->dk_size; i++) {
491 Py_ssize_t ix = dictkeys_get_index(keys, i);
Victor Stinner68762572019-10-07 18:42:01 +0200492 CHECK(DKIX_DUMMY <= ix && ix <= usable);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200493 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200494
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200495 for (i=0; i < usable; i++) {
496 PyDictKeyEntry *entry = &entries[i];
497 PyObject *key = entry->me_key;
498
499 if (key != NULL) {
500 if (PyUnicode_CheckExact(key)) {
501 Py_hash_t hash = ((PyASCIIObject *)key)->hash;
Victor Stinner68762572019-10-07 18:42:01 +0200502 CHECK(hash != -1);
503 CHECK(entry->me_hash == hash);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200504 }
505 else {
506 /* test_dict fails if PyObject_Hash() is called again */
Victor Stinner68762572019-10-07 18:42:01 +0200507 CHECK(entry->me_hash != -1);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200508 }
509 if (!splitted) {
Victor Stinner68762572019-10-07 18:42:01 +0200510 CHECK(entry->me_value != NULL);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200511 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200512 }
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200513
514 if (splitted) {
Victor Stinner68762572019-10-07 18:42:01 +0200515 CHECK(entry->me_value == NULL);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200516 }
517 }
518
519 if (splitted) {
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200520 /* splitted table */
521 for (i=0; i < mp->ma_used; i++) {
Victor Stinner68762572019-10-07 18:42:01 +0200522 CHECK(mp->ma_values[i] != NULL);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200523 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200524 }
525 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200526 return 1;
Victor Stinner68762572019-10-07 18:42:01 +0200527
528#undef CHECK
Victor Stinner611b0fa2016-09-14 15:02:01 +0200529}
Victor Stinner611b0fa2016-09-14 15:02:01 +0200530
531
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400532static PyDictKeysObject *new_keys_object(Py_ssize_t size)
533{
534 PyDictKeysObject *dk;
Victor Stinner742da042016-09-07 17:40:12 -0700535 Py_ssize_t es, usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400536
Victor Stinner742da042016-09-07 17:40:12 -0700537 assert(size >= PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400538 assert(IS_POWER_OF_2(size));
Victor Stinner742da042016-09-07 17:40:12 -0700539
540 usable = USABLE_FRACTION(size);
541 if (size <= 0xff) {
542 es = 1;
543 }
544 else if (size <= 0xffff) {
545 es = 2;
546 }
547#if SIZEOF_VOID_P > 4
548 else if (size <= 0xffffffff) {
549 es = 4;
550 }
551#endif
552 else {
553 es = sizeof(Py_ssize_t);
554 }
555
556 if (size == PyDict_MINSIZE && numfreekeys > 0) {
557 dk = keys_free_list[--numfreekeys];
558 }
559 else {
Victor Stinner98ee9d52016-09-08 09:33:56 -0700560 dk = PyObject_MALLOC(sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -0700561 + es * size
562 + sizeof(PyDictKeyEntry) * usable);
Victor Stinner742da042016-09-07 17:40:12 -0700563 if (dk == NULL) {
564 PyErr_NoMemory();
565 return NULL;
566 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400567 }
Victor Stinner49932fe2020-02-03 17:55:05 +0100568#ifdef Py_REF_DEBUG
569 _Py_RefTotal++;
570#endif
INADA Naokia7576492018-11-14 18:39:27 +0900571 dk->dk_refcnt = 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400572 dk->dk_size = size;
Victor Stinner742da042016-09-07 17:40:12 -0700573 dk->dk_usable = usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400574 dk->dk_lookup = lookdict_unicode_nodummy;
Victor Stinner742da042016-09-07 17:40:12 -0700575 dk->dk_nentries = 0;
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700576 memset(&dk->dk_indices[0], 0xff, es * size);
Victor Stinner742da042016-09-07 17:40:12 -0700577 memset(DK_ENTRIES(dk), 0, sizeof(PyDictKeyEntry) * usable);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400578 return dk;
579}
580
581static void
582free_keys_object(PyDictKeysObject *keys)
583{
Victor Stinner742da042016-09-07 17:40:12 -0700584 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400585 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -0700586 for (i = 0, n = keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400587 Py_XDECREF(entries[i].me_key);
588 Py_XDECREF(entries[i].me_value);
589 }
Victor Stinner742da042016-09-07 17:40:12 -0700590 if (keys->dk_size == PyDict_MINSIZE && numfreekeys < PyDict_MAXFREELIST) {
591 keys_free_list[numfreekeys++] = keys;
592 return;
593 }
Raymond Hettingerce5179f2016-01-31 08:56:21 -0800594 PyObject_FREE(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400595}
596
597#define new_values(size) PyMem_NEW(PyObject *, size)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400598#define free_values(values) PyMem_FREE(values)
599
600/* Consumes a reference to the keys object */
601static PyObject *
602new_dict(PyDictKeysObject *keys, PyObject **values)
603{
604 PyDictObject *mp;
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200605 assert(keys != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 if (numfree) {
607 mp = free_list[--numfree];
608 assert (mp != NULL);
Dong-hee Na1b55b652020-02-17 19:09:15 +0900609 assert (Py_IS_TYPE(mp, &PyDict_Type));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 _Py_NewReference((PyObject *)mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400612 else {
613 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
614 if (mp == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +0900615 dictkeys_decref(keys);
Zackery Spytz3d07c1e2019-03-23 20:23:29 -0600616 if (values != empty_values) {
617 free_values(values);
618 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400619 return NULL;
620 }
621 }
622 mp->ma_keys = keys;
623 mp->ma_values = values;
624 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -0700625 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200626 ASSERT_CONSISTENT(mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000628}
629
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400630/* Consumes a reference to the keys object */
631static PyObject *
632new_dict_with_shared_keys(PyDictKeysObject *keys)
633{
634 PyObject **values;
635 Py_ssize_t i, size;
636
Victor Stinner742da042016-09-07 17:40:12 -0700637 size = USABLE_FRACTION(DK_SIZE(keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400638 values = new_values(size);
639 if (values == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +0900640 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400641 return PyErr_NoMemory();
642 }
643 for (i = 0; i < size; i++) {
644 values[i] = NULL;
645 }
646 return new_dict(keys, values);
647}
648
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500649
650static PyObject *
651clone_combined_dict(PyDictObject *orig)
652{
653 assert(PyDict_CheckExact(orig));
654 assert(orig->ma_values == NULL);
655 assert(orig->ma_keys->dk_refcnt == 1);
656
657 Py_ssize_t keys_size = _PyDict_KeysSize(orig->ma_keys);
658 PyDictKeysObject *keys = PyObject_Malloc(keys_size);
659 if (keys == NULL) {
660 PyErr_NoMemory();
661 return NULL;
662 }
663
664 memcpy(keys, orig->ma_keys, keys_size);
665
666 /* After copying key/value pairs, we need to incref all
667 keys and values and they are about to be co-owned by a
668 new dict object. */
669 PyDictKeyEntry *ep0 = DK_ENTRIES(keys);
670 Py_ssize_t n = keys->dk_nentries;
671 for (Py_ssize_t i = 0; i < n; i++) {
672 PyDictKeyEntry *entry = &ep0[i];
673 PyObject *value = entry->me_value;
674 if (value != NULL) {
675 Py_INCREF(value);
676 Py_INCREF(entry->me_key);
677 }
678 }
679
680 PyDictObject *new = (PyDictObject *)new_dict(keys, NULL);
681 if (new == NULL) {
682 /* In case of an error, `new_dict()` takes care of
683 cleaning up `keys`. */
684 return NULL;
685 }
686 new->ma_used = orig->ma_used;
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200687 ASSERT_CONSISTENT(new);
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500688 if (_PyObject_GC_IS_TRACKED(orig)) {
689 /* Maintain tracking. */
690 _PyObject_GC_TRACK(new);
691 }
Yury Selivanov0b752282018-07-06 12:20:07 -0400692
693 /* Since we copied the keys table we now have an extra reference
Victor Stinner49932fe2020-02-03 17:55:05 +0100694 in the system. Manually call increment _Py_RefTotal to signal that
INADA Naokia7576492018-11-14 18:39:27 +0900695 we have it now; calling dictkeys_incref would be an error as
Yury Selivanov0b752282018-07-06 12:20:07 -0400696 keys->dk_refcnt is already set to 1 (after memcpy). */
Victor Stinner49932fe2020-02-03 17:55:05 +0100697#ifdef Py_REF_DEBUG
698 _Py_RefTotal++;
699#endif
Yury Selivanov0b752282018-07-06 12:20:07 -0400700
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500701 return (PyObject *)new;
702}
703
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400704PyObject *
705PyDict_New(void)
706{
Inada Naokif2a18672019-03-12 17:25:44 +0900707 dictkeys_incref(Py_EMPTY_KEYS);
708 return new_dict(Py_EMPTY_KEYS, empty_values);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400709}
710
Victor Stinner742da042016-09-07 17:40:12 -0700711/* Search index of hash table from offset of entry table */
712static Py_ssize_t
713lookdict_index(PyDictKeysObject *k, Py_hash_t hash, Py_ssize_t index)
714{
Victor Stinner742da042016-09-07 17:40:12 -0700715 size_t mask = DK_MASK(k);
INADA Naoki073ae482017-06-23 15:22:50 +0900716 size_t perturb = (size_t)hash;
717 size_t i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700718
INADA Naoki073ae482017-06-23 15:22:50 +0900719 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900720 Py_ssize_t ix = dictkeys_get_index(k, i);
Victor Stinner742da042016-09-07 17:40:12 -0700721 if (ix == index) {
722 return i;
723 }
724 if (ix == DKIX_EMPTY) {
725 return DKIX_EMPTY;
726 }
INADA Naoki073ae482017-06-23 15:22:50 +0900727 perturb >>= PERTURB_SHIFT;
728 i = mask & (i*5 + perturb + 1);
Victor Stinner742da042016-09-07 17:40:12 -0700729 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700730 Py_UNREACHABLE();
Victor Stinner742da042016-09-07 17:40:12 -0700731}
732
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000733/*
734The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000735This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000736Open addressing is preferred over chaining since the link overhead for
737chaining would be substantial (100% with typical malloc overhead).
738
Tim Peterseb28ef22001-06-02 05:27:19 +0000739The initial probe index is computed as hash mod the table size. Subsequent
740probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000741
742All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000743
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000744The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000745contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000746Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000747
Victor Stinner742da042016-09-07 17:40:12 -0700748lookdict() is general-purpose, and may return DKIX_ERROR if (and only if) a
Victor Stinnera4348cc2016-09-08 12:01:25 -0700749comparison raises an exception.
Guido van Rossum89d8c602007-09-18 17:26:56 +0000750lookdict_unicode() below is specialized to string keys, comparison of which can
INADA Naoki1b8df102017-02-20 22:48:10 +0900751never raise an exception; that function can never return DKIX_ERROR when key
752is string. Otherwise, it falls back to lookdict().
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400753lookdict_unicode_nodummy is further specialized for string keys that cannot be
754the <dummy> value.
INADA Naoki778928b2017-08-03 23:45:15 +0900755For both, when the key isn't found a DKIX_EMPTY is returned.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000756*/
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100757static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400758lookdict(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900759 Py_hash_t hash, PyObject **value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000760{
INADA Naoki778928b2017-08-03 23:45:15 +0900761 size_t i, mask, perturb;
Victor Stinner742da042016-09-07 17:40:12 -0700762 PyDictKeysObject *dk;
INADA Naoki778928b2017-08-03 23:45:15 +0900763 PyDictKeyEntry *ep0;
Tim Peterseb28ef22001-06-02 05:27:19 +0000764
Antoine Pitrou9a234902012-05-13 20:48:01 +0200765top:
Victor Stinner742da042016-09-07 17:40:12 -0700766 dk = mp->ma_keys;
Victor Stinner742da042016-09-07 17:40:12 -0700767 ep0 = DK_ENTRIES(dk);
INADA Naoki778928b2017-08-03 23:45:15 +0900768 mask = DK_MASK(dk);
769 perturb = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700771
INADA Naoki778928b2017-08-03 23:45:15 +0900772 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900773 Py_ssize_t ix = dictkeys_get_index(dk, i);
Victor Stinner742da042016-09-07 17:40:12 -0700774 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700775 *value_addr = NULL;
776 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400777 }
INADA Naoki778928b2017-08-03 23:45:15 +0900778 if (ix >= 0) {
779 PyDictKeyEntry *ep = &ep0[ix];
780 assert(ep->me_key != NULL);
781 if (ep->me_key == key) {
782 *value_addr = ep->me_value;
783 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700784 }
INADA Naoki778928b2017-08-03 23:45:15 +0900785 if (ep->me_hash == hash) {
786 PyObject *startkey = ep->me_key;
787 Py_INCREF(startkey);
788 int cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
789 Py_DECREF(startkey);
790 if (cmp < 0) {
791 *value_addr = NULL;
792 return DKIX_ERROR;
793 }
794 if (dk == mp->ma_keys && ep->me_key == startkey) {
795 if (cmp > 0) {
796 *value_addr = ep->me_value;
797 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700798 }
INADA Naoki778928b2017-08-03 23:45:15 +0900799 }
800 else {
801 /* The dict was mutated, restart */
802 goto top;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400803 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 }
INADA Naoki778928b2017-08-03 23:45:15 +0900806 perturb >>= PERTURB_SHIFT;
807 i = (i*5 + perturb + 1) & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700809 Py_UNREACHABLE();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000810}
811
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400812/* Specialized version for string-only keys */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100813static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400814lookdict_unicode(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900815 Py_hash_t hash, PyObject **value_addr)
Fred Drake1bff34a2000-08-31 19:31:38 +0000816{
Victor Stinner742da042016-09-07 17:40:12 -0700817 assert(mp->ma_values == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 /* Make sure this function doesn't have to handle non-unicode keys,
819 including subclasses of str; e.g., one reason to subclass
820 unicodes is to override __eq__, and for speed we don't cater to
821 that here. */
822 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400823 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900824 return lookdict(mp, key, hash, value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 }
Tim Peters15d49292001-05-27 07:39:22 +0000826
INADA Naoki778928b2017-08-03 23:45:15 +0900827 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
828 size_t mask = DK_MASK(mp->ma_keys);
829 size_t perturb = (size_t)hash;
830 size_t i = (size_t)hash & mask;
831
832 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900833 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
Victor Stinner742da042016-09-07 17:40:12 -0700834 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700835 *value_addr = NULL;
836 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400837 }
INADA Naoki778928b2017-08-03 23:45:15 +0900838 if (ix >= 0) {
839 PyDictKeyEntry *ep = &ep0[ix];
840 assert(ep->me_key != NULL);
841 assert(PyUnicode_CheckExact(ep->me_key));
842 if (ep->me_key == key ||
843 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
844 *value_addr = ep->me_value;
845 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700846 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400847 }
INADA Naoki778928b2017-08-03 23:45:15 +0900848 perturb >>= PERTURB_SHIFT;
849 i = mask & (i*5 + perturb + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700851 Py_UNREACHABLE();
Fred Drake1bff34a2000-08-31 19:31:38 +0000852}
853
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400854/* Faster version of lookdict_unicode when it is known that no <dummy> keys
855 * will be present. */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100856static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400857lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900858 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400859{
Victor Stinner742da042016-09-07 17:40:12 -0700860 assert(mp->ma_values == NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400861 /* Make sure this function doesn't have to handle non-unicode keys,
862 including subclasses of str; e.g., one reason to subclass
863 unicodes is to override __eq__, and for speed we don't cater to
864 that here. */
865 if (!PyUnicode_CheckExact(key)) {
866 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900867 return lookdict(mp, key, hash, value_addr);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400868 }
INADA Naoki778928b2017-08-03 23:45:15 +0900869
870 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
871 size_t mask = DK_MASK(mp->ma_keys);
872 size_t perturb = (size_t)hash;
873 size_t i = (size_t)hash & mask;
874
875 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900876 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
Victor Stinner742da042016-09-07 17:40:12 -0700877 assert (ix != DKIX_DUMMY);
878 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700879 *value_addr = NULL;
880 return DKIX_EMPTY;
881 }
INADA Naoki778928b2017-08-03 23:45:15 +0900882 PyDictKeyEntry *ep = &ep0[ix];
883 assert(ep->me_key != NULL);
884 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700885 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400886 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900887 *value_addr = ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700888 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400889 }
INADA Naoki778928b2017-08-03 23:45:15 +0900890 perturb >>= PERTURB_SHIFT;
891 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400892 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700893 Py_UNREACHABLE();
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400894}
895
896/* Version of lookdict for split tables.
897 * All split tables and only split tables use this lookup function.
898 * Split tables only contain unicode keys and no dummy keys,
899 * so algorithm is the same as lookdict_unicode_nodummy.
900 */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100901static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400902lookdict_split(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900903 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400904{
Victor Stinner742da042016-09-07 17:40:12 -0700905 /* mp must split table */
906 assert(mp->ma_values != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400907 if (!PyUnicode_CheckExact(key)) {
INADA Naoki778928b2017-08-03 23:45:15 +0900908 Py_ssize_t ix = lookdict(mp, key, hash, value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700909 if (ix >= 0) {
INADA Naokiba609772016-12-07 20:41:42 +0900910 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700911 }
912 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400913 }
Victor Stinner742da042016-09-07 17:40:12 -0700914
INADA Naoki778928b2017-08-03 23:45:15 +0900915 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
916 size_t mask = DK_MASK(mp->ma_keys);
917 size_t perturb = (size_t)hash;
918 size_t i = (size_t)hash & mask;
919
920 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900921 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
INADA Naoki778928b2017-08-03 23:45:15 +0900922 assert (ix != DKIX_DUMMY);
Victor Stinner742da042016-09-07 17:40:12 -0700923 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700924 *value_addr = NULL;
925 return DKIX_EMPTY;
926 }
INADA Naoki778928b2017-08-03 23:45:15 +0900927 PyDictKeyEntry *ep = &ep0[ix];
928 assert(ep->me_key != NULL);
929 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700930 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400931 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900932 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700933 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400934 }
INADA Naoki778928b2017-08-03 23:45:15 +0900935 perturb >>= PERTURB_SHIFT;
936 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400937 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700938 Py_UNREACHABLE();
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400939}
940
Benjamin Petersonfb886362010-04-24 18:21:17 +0000941int
942_PyDict_HasOnlyStringKeys(PyObject *dict)
943{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 Py_ssize_t pos = 0;
945 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000946 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400948 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 return 1;
950 while (PyDict_Next(dict, &pos, &key, &value))
951 if (!PyUnicode_Check(key))
952 return 0;
953 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000954}
955
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000956#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 do { \
958 if (!_PyObject_GC_IS_TRACKED(mp)) { \
959 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
960 _PyObject_GC_MAY_BE_TRACKED(value)) { \
961 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 } \
963 } \
964 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000965
966void
967_PyDict_MaybeUntrack(PyObject *op)
968{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 PyDictObject *mp;
970 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -0700971 Py_ssize_t i, numentries;
972 PyDictKeyEntry *ep0;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000973
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
975 return;
976
977 mp = (PyDictObject *) op;
Victor Stinner742da042016-09-07 17:40:12 -0700978 ep0 = DK_ENTRIES(mp->ma_keys);
979 numentries = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400980 if (_PyDict_HasSplitTable(mp)) {
Victor Stinner742da042016-09-07 17:40:12 -0700981 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400982 if ((value = mp->ma_values[i]) == NULL)
983 continue;
984 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
Victor Stinner742da042016-09-07 17:40:12 -0700985 assert(!_PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400986 return;
987 }
988 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400990 else {
Victor Stinner742da042016-09-07 17:40:12 -0700991 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400992 if ((value = ep0[i].me_value) == NULL)
993 continue;
994 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
995 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
996 return;
997 }
998 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001000}
1001
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001002/* Internal function to find slot for an item from its hash
Victor Stinner3c336c52016-09-12 14:17:40 +02001003 when it is known that the key is not present in the dict.
1004
1005 The dict must be combined. */
INADA Naokiba609772016-12-07 20:41:42 +09001006static Py_ssize_t
INADA Naoki778928b2017-08-03 23:45:15 +09001007find_empty_slot(PyDictKeysObject *keys, Py_hash_t hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001008{
INADA Naoki778928b2017-08-03 23:45:15 +09001009 assert(keys != NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001010
INADA Naoki778928b2017-08-03 23:45:15 +09001011 const size_t mask = DK_MASK(keys);
1012 size_t i = hash & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001013 Py_ssize_t ix = dictkeys_get_index(keys, i);
INADA Naoki778928b2017-08-03 23:45:15 +09001014 for (size_t perturb = hash; ix >= 0;) {
INADA Naoki267941c2016-10-06 15:19:07 +09001015 perturb >>= PERTURB_SHIFT;
INADA Naoki778928b2017-08-03 23:45:15 +09001016 i = (i*5 + perturb + 1) & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001017 ix = dictkeys_get_index(keys, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 }
INADA Naoki778928b2017-08-03 23:45:15 +09001019 return i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001020}
1021
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001022static int
1023insertion_resize(PyDictObject *mp)
1024{
Raymond Hettinger36f74aa2013-05-17 03:01:13 -07001025 return dictresize(mp, GROWTH_RATE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001026}
Antoine Pitroue965d972012-02-27 00:45:12 +01001027
1028/*
1029Internal routine to insert a new item into the table.
1030Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +01001031Returns -1 if an error occurred, or 0 on success.
1032*/
1033static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001034insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001035{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001036 PyObject *old_value;
INADA Naokiba609772016-12-07 20:41:42 +09001037 PyDictKeyEntry *ep;
Antoine Pitroue965d972012-02-27 00:45:12 +01001038
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001039 Py_INCREF(key);
1040 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001041 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
1042 if (insertion_resize(mp) < 0)
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001043 goto Fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001044 }
1045
INADA Naoki778928b2017-08-03 23:45:15 +09001046 Py_ssize_t ix = mp->ma_keys->dk_lookup(mp, key, hash, &old_value);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001047 if (ix == DKIX_ERROR)
1048 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001049
Antoine Pitroud6967322014-10-18 00:35:00 +02001050 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001051 MAINTAIN_TRACKING(mp, key, value);
Victor Stinner742da042016-09-07 17:40:12 -07001052
1053 /* When insertion order is different from shared key, we can't share
1054 * the key anymore. Convert this instance to combine table.
1055 */
1056 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09001057 ((ix >= 0 && old_value == NULL && mp->ma_used != ix) ||
Victor Stinner742da042016-09-07 17:40:12 -07001058 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001059 if (insertion_resize(mp) < 0)
1060 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001061 ix = DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001062 }
Victor Stinner742da042016-09-07 17:40:12 -07001063
1064 if (ix == DKIX_EMPTY) {
1065 /* Insert into new slot. */
INADA Naokiba609772016-12-07 20:41:42 +09001066 assert(old_value == NULL);
Victor Stinner742da042016-09-07 17:40:12 -07001067 if (mp->ma_keys->dk_usable <= 0) {
1068 /* Need to resize. */
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001069 if (insertion_resize(mp) < 0)
1070 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001071 }
INADA Naoki778928b2017-08-03 23:45:15 +09001072 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naokiba609772016-12-07 20:41:42 +09001073 ep = &DK_ENTRIES(mp->ma_keys)[mp->ma_keys->dk_nentries];
INADA Naokia7576492018-11-14 18:39:27 +09001074 dictkeys_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Victor Stinner742da042016-09-07 17:40:12 -07001075 ep->me_key = key;
1076 ep->me_hash = hash;
1077 if (mp->ma_values) {
1078 assert (mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
1079 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001080 }
1081 else {
Victor Stinner742da042016-09-07 17:40:12 -07001082 ep->me_value = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001083 }
1084 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001085 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07001086 mp->ma_keys->dk_usable--;
1087 mp->ma_keys->dk_nentries++;
1088 assert(mp->ma_keys->dk_usable >= 0);
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001089 ASSERT_CONSISTENT(mp);
Victor Stinner742da042016-09-07 17:40:12 -07001090 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001091 }
Victor Stinner742da042016-09-07 17:40:12 -07001092
Inada Naoki91234a12019-06-03 21:30:58 +09001093 if (old_value != value) {
1094 if (_PyDict_HasSplitTable(mp)) {
1095 mp->ma_values[ix] = value;
1096 if (old_value == NULL) {
1097 /* pending state */
1098 assert(ix == mp->ma_used);
1099 mp->ma_used++;
1100 }
INADA Naokiba609772016-12-07 20:41:42 +09001101 }
Inada Naoki91234a12019-06-03 21:30:58 +09001102 else {
1103 assert(old_value != NULL);
1104 DK_ENTRIES(mp->ma_keys)[ix].me_value = value;
1105 }
1106 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokiba609772016-12-07 20:41:42 +09001107 }
INADA Naokiba609772016-12-07 20:41:42 +09001108 Py_XDECREF(old_value); /* which **CAN** re-enter (see issue #22653) */
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001109 ASSERT_CONSISTENT(mp);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001110 Py_DECREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001111 return 0;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001112
1113Fail:
1114 Py_DECREF(value);
1115 Py_DECREF(key);
1116 return -1;
Antoine Pitroue965d972012-02-27 00:45:12 +01001117}
1118
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001119// Same to insertdict but specialized for ma_keys = Py_EMPTY_KEYS.
1120static int
1121insert_to_emptydict(PyDictObject *mp, PyObject *key, Py_hash_t hash,
1122 PyObject *value)
1123{
1124 assert(mp->ma_keys == Py_EMPTY_KEYS);
1125
1126 PyDictKeysObject *newkeys = new_keys_object(PyDict_MINSIZE);
1127 if (newkeys == NULL) {
1128 return -1;
1129 }
1130 if (!PyUnicode_CheckExact(key)) {
1131 newkeys->dk_lookup = lookdict;
1132 }
1133 dictkeys_decref(Py_EMPTY_KEYS);
1134 mp->ma_keys = newkeys;
1135 mp->ma_values = NULL;
1136
1137 Py_INCREF(key);
1138 Py_INCREF(value);
1139 MAINTAIN_TRACKING(mp, key, value);
1140
1141 size_t hashpos = (size_t)hash & (PyDict_MINSIZE-1);
Dong-hee Nac39d1dd2019-10-11 17:43:11 +09001142 PyDictKeyEntry *ep = DK_ENTRIES(mp->ma_keys);
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001143 dictkeys_set_index(mp->ma_keys, hashpos, 0);
1144 ep->me_key = key;
1145 ep->me_hash = hash;
1146 ep->me_value = value;
1147 mp->ma_used++;
1148 mp->ma_version_tag = DICT_NEXT_VERSION();
1149 mp->ma_keys->dk_usable--;
1150 mp->ma_keys->dk_nentries++;
1151 return 0;
1152}
1153
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001154/*
luzpaza5293b42017-11-05 07:37:50 -06001155Internal routine used by dictresize() to build a hashtable of entries.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001156*/
1157static void
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001158build_indices(PyDictKeysObject *keys, PyDictKeyEntry *ep, Py_ssize_t n)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001159{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001160 size_t mask = (size_t)DK_SIZE(keys) - 1;
1161 for (Py_ssize_t ix = 0; ix != n; ix++, ep++) {
1162 Py_hash_t hash = ep->me_hash;
1163 size_t i = hash & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001164 for (size_t perturb = hash; dictkeys_get_index(keys, i) != DKIX_EMPTY;) {
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001165 perturb >>= PERTURB_SHIFT;
INADA Naoki870c2862017-06-24 09:03:19 +09001166 i = mask & (i*5 + perturb + 1);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001167 }
INADA Naokia7576492018-11-14 18:39:27 +09001168 dictkeys_set_index(keys, i, ix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001170}
1171
1172/*
1173Restructure the table by allocating a new table and reinserting all
1174items again. When entries have been deleted, the new table may
1175actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001176If a table is split (its keys and hashes are shared, its values are not),
1177then the values are temporarily copied into the table, it is resized as
1178a combined table, then the me_value slots in the old table are NULLed out.
1179After resizing a table is always combined,
1180but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001181*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001182static int
Victor Stinner3d3f2642016-12-15 17:21:23 +01001183dictresize(PyDictObject *mp, Py_ssize_t minsize)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001184{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001185 Py_ssize_t newsize, numentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001186 PyDictKeysObject *oldkeys;
1187 PyObject **oldvalues;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001188 PyDictKeyEntry *oldentries, *newentries;
Tim Peters91a364d2001-05-19 07:04:38 +00001189
Victor Stinner742da042016-09-07 17:40:12 -07001190 /* Find the smallest table size > minused. */
1191 for (newsize = PyDict_MINSIZE;
Victor Stinner3d3f2642016-12-15 17:21:23 +01001192 newsize < minsize && newsize > 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 newsize <<= 1)
1194 ;
1195 if (newsize <= 0) {
1196 PyErr_NoMemory();
1197 return -1;
1198 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001199
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001200 oldkeys = mp->ma_keys;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001201
1202 /* NOTE: Current odict checks mp->ma_keys to detect resize happen.
1203 * So we can't reuse oldkeys even if oldkeys->dk_size == newsize.
1204 * TODO: Try reusing oldkeys when reimplement odict.
1205 */
1206
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001207 /* Allocate a new table. */
1208 mp->ma_keys = new_keys_object(newsize);
1209 if (mp->ma_keys == NULL) {
1210 mp->ma_keys = oldkeys;
1211 return -1;
1212 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01001213 // New table must be large enough.
1214 assert(mp->ma_keys->dk_usable >= mp->ma_used);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001215 if (oldkeys->dk_lookup == lookdict)
1216 mp->ma_keys->dk_lookup = lookdict;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001217
1218 numentries = mp->ma_used;
1219 oldentries = DK_ENTRIES(oldkeys);
1220 newentries = DK_ENTRIES(mp->ma_keys);
1221 oldvalues = mp->ma_values;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001222 if (oldvalues != NULL) {
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001223 /* Convert split table into new combined table.
1224 * We must incref keys; we can transfer values.
1225 * Note that values of split table is always dense.
1226 */
1227 for (Py_ssize_t i = 0; i < numentries; i++) {
1228 assert(oldvalues[i] != NULL);
1229 PyDictKeyEntry *ep = &oldentries[i];
1230 PyObject *key = ep->me_key;
1231 Py_INCREF(key);
1232 newentries[i].me_key = key;
1233 newentries[i].me_hash = ep->me_hash;
1234 newentries[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001236
INADA Naokia7576492018-11-14 18:39:27 +09001237 dictkeys_decref(oldkeys);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001238 mp->ma_values = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001239 if (oldvalues != empty_values) {
1240 free_values(oldvalues);
1241 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001242 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001243 else { // combined table.
1244 if (oldkeys->dk_nentries == numentries) {
1245 memcpy(newentries, oldentries, numentries * sizeof(PyDictKeyEntry));
1246 }
1247 else {
1248 PyDictKeyEntry *ep = oldentries;
1249 for (Py_ssize_t i = 0; i < numentries; i++) {
1250 while (ep->me_value == NULL)
1251 ep++;
1252 newentries[i] = *ep++;
1253 }
1254 }
1255
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001256 assert(oldkeys->dk_lookup != lookdict_split);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001257 assert(oldkeys->dk_refcnt == 1);
Victor Stinner49932fe2020-02-03 17:55:05 +01001258#ifdef Py_REF_DEBUG
1259 _Py_RefTotal--;
1260#endif
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001261 if (oldkeys->dk_size == PyDict_MINSIZE &&
Victor Stinner49932fe2020-02-03 17:55:05 +01001262 numfreekeys < PyDict_MAXFREELIST)
1263 {
INADA Naokia7576492018-11-14 18:39:27 +09001264 keys_free_list[numfreekeys++] = oldkeys;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001265 }
1266 else {
INADA Naokia7576492018-11-14 18:39:27 +09001267 PyObject_FREE(oldkeys);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001268 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001269 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001270
1271 build_indices(mp->ma_keys, newentries, numentries);
1272 mp->ma_keys->dk_usable -= numentries;
1273 mp->ma_keys->dk_nentries = numentries;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001275}
1276
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001277/* Returns NULL if unable to split table.
1278 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001279static PyDictKeysObject *
1280make_keys_shared(PyObject *op)
1281{
1282 Py_ssize_t i;
1283 Py_ssize_t size;
1284 PyDictObject *mp = (PyDictObject *)op;
1285
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001286 if (!PyDict_CheckExact(op))
1287 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001288 if (!_PyDict_HasSplitTable(mp)) {
1289 PyDictKeyEntry *ep0;
1290 PyObject **values;
1291 assert(mp->ma_keys->dk_refcnt == 1);
1292 if (mp->ma_keys->dk_lookup == lookdict) {
1293 return NULL;
1294 }
1295 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
1296 /* Remove dummy keys */
1297 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
1298 return NULL;
1299 }
1300 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
1301 /* Copy values into a new array */
Victor Stinner742da042016-09-07 17:40:12 -07001302 ep0 = DK_ENTRIES(mp->ma_keys);
1303 size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001304 values = new_values(size);
1305 if (values == NULL) {
1306 PyErr_SetString(PyExc_MemoryError,
1307 "Not enough memory to allocate new values array");
1308 return NULL;
1309 }
1310 for (i = 0; i < size; i++) {
1311 values[i] = ep0[i].me_value;
1312 ep0[i].me_value = NULL;
1313 }
1314 mp->ma_keys->dk_lookup = lookdict_split;
1315 mp->ma_values = values;
1316 }
INADA Naokia7576492018-11-14 18:39:27 +09001317 dictkeys_incref(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001318 return mp->ma_keys;
1319}
Christian Heimes99170a52007-12-19 02:07:34 +00001320
1321PyObject *
1322_PyDict_NewPresized(Py_ssize_t minused)
1323{
INADA Naoki92c50ee2016-11-22 00:57:02 +09001324 const Py_ssize_t max_presize = 128 * 1024;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001325 Py_ssize_t newsize;
1326 PyDictKeysObject *new_keys;
INADA Naoki92c50ee2016-11-22 00:57:02 +09001327
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001328 if (minused <= USABLE_FRACTION(PyDict_MINSIZE)) {
Inada Naokif2a18672019-03-12 17:25:44 +09001329 return PyDict_New();
1330 }
INADA Naoki92c50ee2016-11-22 00:57:02 +09001331 /* There are no strict guarantee that returned dict can contain minused
1332 * items without resize. So we create medium size dict instead of very
1333 * large dict or MemoryError.
1334 */
1335 if (minused > USABLE_FRACTION(max_presize)) {
1336 newsize = max_presize;
1337 }
1338 else {
1339 Py_ssize_t minsize = ESTIMATE_SIZE(minused);
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001340 newsize = PyDict_MINSIZE*2;
INADA Naoki92c50ee2016-11-22 00:57:02 +09001341 while (newsize < minsize) {
1342 newsize <<= 1;
1343 }
1344 }
1345 assert(IS_POWER_OF_2(newsize));
1346
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001347 new_keys = new_keys_object(newsize);
1348 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001350 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001351}
1352
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001353/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1354 * that may occur (originally dicts supported only string keys, and exceptions
1355 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001356 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001357 * (suppressed) occurred while computing the key's hash, or that some error
1358 * (suppressed) occurred when comparing keys in the dict's internal probe
1359 * sequence. A nasty example of the latter is when a Python-coded comparison
1360 * function hits a stack-depth error, which can cause this to return NULL
1361 * even if the key is present.
1362 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001363PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001364PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001365{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001366 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07001367 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 PyDictObject *mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 PyThreadState *tstate;
INADA Naokiba609772016-12-07 20:41:42 +09001370 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 if (!PyDict_Check(op))
1373 return NULL;
1374 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001375 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 {
1377 hash = PyObject_Hash(key);
1378 if (hash == -1) {
1379 PyErr_Clear();
1380 return NULL;
1381 }
1382 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 /* We can arrive here with a NULL tstate during initialization: try
1385 running "python -Wi" for an example related to string interning.
1386 Let's just hope that no exception occurs then... This must be
Victor Stinner50b48572018-11-01 01:51:40 +01001387 _PyThreadState_GET() and not PyThreadState_Get() because the latter
Victor Stinner9204fb82018-10-30 15:13:17 +01001388 abort Python if tstate is NULL. */
Victor Stinner50b48572018-11-01 01:51:40 +01001389 tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 if (tstate != NULL && tstate->curexc_type != NULL) {
1391 /* preserve the existing exception */
1392 PyObject *err_type, *err_value, *err_tb;
1393 PyErr_Fetch(&err_type, &err_value, &err_tb);
INADA Naoki778928b2017-08-03 23:45:15 +09001394 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 /* ignore errors */
1396 PyErr_Restore(err_type, err_value, err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001397 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 return NULL;
1399 }
1400 else {
INADA Naoki778928b2017-08-03 23:45:15 +09001401 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001402 if (ix < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 PyErr_Clear();
1404 return NULL;
1405 }
1406 }
INADA Naokiba609772016-12-07 20:41:42 +09001407 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001408}
1409
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001410/* Same as PyDict_GetItemWithError() but with hash supplied by caller.
1411 This returns NULL *with* an exception set if an exception occurred.
1412 It returns NULL *without* an exception set if the key wasn't present.
1413*/
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001414PyObject *
1415_PyDict_GetItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1416{
Victor Stinner742da042016-09-07 17:40:12 -07001417 Py_ssize_t ix;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001418 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001419 PyObject *value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001420
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001421 if (!PyDict_Check(op)) {
1422 PyErr_BadInternalCall();
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001423 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001424 }
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001425
INADA Naoki778928b2017-08-03 23:45:15 +09001426 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001427 if (ix < 0) {
1428 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001429 }
INADA Naokiba609772016-12-07 20:41:42 +09001430 return value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001431}
1432
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001433/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1434 This returns NULL *with* an exception set if an exception occurred.
1435 It returns NULL *without* an exception set if the key wasn't present.
1436*/
1437PyObject *
1438PyDict_GetItemWithError(PyObject *op, PyObject *key)
1439{
Victor Stinner742da042016-09-07 17:40:12 -07001440 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001441 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 PyDictObject*mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001443 PyObject *value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001445 if (!PyDict_Check(op)) {
1446 PyErr_BadInternalCall();
1447 return NULL;
1448 }
1449 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001450 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001451 {
1452 hash = PyObject_Hash(key);
1453 if (hash == -1) {
1454 return NULL;
1455 }
1456 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001457
INADA Naoki778928b2017-08-03 23:45:15 +09001458 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001459 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001461 return value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001462}
1463
Brett Cannonfd074152012-04-14 14:10:13 -04001464PyObject *
1465_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1466{
1467 PyObject *kv;
1468 kv = _PyUnicode_FromId(key); /* borrowed */
1469 if (kv == NULL)
1470 return NULL;
1471 return PyDict_GetItemWithError(dp, kv);
1472}
1473
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001474PyObject *
1475_PyDict_GetItemStringWithError(PyObject *v, const char *key)
1476{
1477 PyObject *kv, *rv;
1478 kv = PyUnicode_FromString(key);
1479 if (kv == NULL) {
1480 return NULL;
1481 }
1482 rv = PyDict_GetItemWithError(v, kv);
1483 Py_DECREF(kv);
1484 return rv;
1485}
1486
Victor Stinnerb4efc962015-11-20 09:24:02 +01001487/* Fast version of global value lookup (LOAD_GLOBAL).
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001488 * Lookup in globals, then builtins.
Victor Stinnerb4efc962015-11-20 09:24:02 +01001489 *
1490 * Raise an exception and return NULL if an error occurred (ex: computing the
1491 * key hash failed, key comparison failed, ...). Return NULL if the key doesn't
1492 * exist. Return the value if the key exists.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001493 */
1494PyObject *
1495_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001496{
Victor Stinner742da042016-09-07 17:40:12 -07001497 Py_ssize_t ix;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001498 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09001499 PyObject *value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001500
1501 if (!PyUnicode_CheckExact(key) ||
1502 (hash = ((PyASCIIObject *) key)->hash) == -1)
1503 {
1504 hash = PyObject_Hash(key);
1505 if (hash == -1)
1506 return NULL;
Antoine Pitroue965d972012-02-27 00:45:12 +01001507 }
Victor Stinnerb4efc962015-11-20 09:24:02 +01001508
1509 /* namespace 1: globals */
INADA Naoki778928b2017-08-03 23:45:15 +09001510 ix = globals->ma_keys->dk_lookup(globals, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001511 if (ix == DKIX_ERROR)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001512 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001513 if (ix != DKIX_EMPTY && value != NULL)
1514 return value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001515
1516 /* namespace 2: builtins */
INADA Naoki778928b2017-08-03 23:45:15 +09001517 ix = builtins->ma_keys->dk_lookup(builtins, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001518 if (ix < 0)
Victor Stinnerb4efc962015-11-20 09:24:02 +01001519 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001520 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001521}
1522
Antoine Pitroue965d972012-02-27 00:45:12 +01001523/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1524 * dictionary if it's merely replacing the value for an existing key.
1525 * This means that it's safe to loop over a dictionary with PyDict_Next()
1526 * and occasionally replace a value -- but you can't insert new keys or
1527 * remove them.
1528 */
1529int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001530PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001531{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001532 PyDictObject *mp;
1533 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001534 if (!PyDict_Check(op)) {
1535 PyErr_BadInternalCall();
1536 return -1;
1537 }
1538 assert(key);
1539 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001540 mp = (PyDictObject *)op;
1541 if (!PyUnicode_CheckExact(key) ||
1542 (hash = ((PyASCIIObject *) key)->hash) == -1)
1543 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001544 hash = PyObject_Hash(key);
1545 if (hash == -1)
1546 return -1;
1547 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001548
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001549 if (mp->ma_keys == Py_EMPTY_KEYS) {
1550 return insert_to_emptydict(mp, key, hash, value);
1551 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001552 /* insertdict() handles any resizing that might be necessary */
1553 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001554}
1555
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001556int
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001557_PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
1558 Py_hash_t hash)
1559{
1560 PyDictObject *mp;
1561
1562 if (!PyDict_Check(op)) {
1563 PyErr_BadInternalCall();
1564 return -1;
1565 }
1566 assert(key);
1567 assert(value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001568 assert(hash != -1);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001569 mp = (PyDictObject *)op;
1570
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001571 if (mp->ma_keys == Py_EMPTY_KEYS) {
1572 return insert_to_emptydict(mp, key, hash, value);
1573 }
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001574 /* insertdict() handles any resizing that might be necessary */
1575 return insertdict(mp, key, hash, value);
1576}
1577
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001578static int
INADA Naoki778928b2017-08-03 23:45:15 +09001579delitem_common(PyDictObject *mp, Py_hash_t hash, Py_ssize_t ix,
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001580 PyObject *old_value)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001581{
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001582 PyObject *old_key;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001583 PyDictKeyEntry *ep;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001584
INADA Naoki778928b2017-08-03 23:45:15 +09001585 Py_ssize_t hashpos = lookdict_index(mp->ma_keys, hash, ix);
1586 assert(hashpos >= 0);
1587
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001588 mp->ma_used--;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001589 mp->ma_version_tag = DICT_NEXT_VERSION();
1590 ep = &DK_ENTRIES(mp->ma_keys)[ix];
INADA Naokia7576492018-11-14 18:39:27 +09001591 dictkeys_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001592 ENSURE_ALLOWS_DELETIONS(mp);
1593 old_key = ep->me_key;
1594 ep->me_key = NULL;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001595 ep->me_value = NULL;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001596 Py_DECREF(old_key);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001597 Py_DECREF(old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001598
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001599 ASSERT_CONSISTENT(mp);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001600 return 0;
1601}
1602
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001603int
Tim Peters1f5871e2000-07-04 17:44:48 +00001604PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001605{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001606 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 assert(key);
1608 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001609 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 hash = PyObject_Hash(key);
1611 if (hash == -1)
1612 return -1;
1613 }
Victor Stinner742da042016-09-07 17:40:12 -07001614
1615 return _PyDict_DelItem_KnownHash(op, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001616}
1617
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001618int
1619_PyDict_DelItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1620{
INADA Naoki778928b2017-08-03 23:45:15 +09001621 Py_ssize_t ix;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001622 PyDictObject *mp;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001623 PyObject *old_value;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001624
1625 if (!PyDict_Check(op)) {
1626 PyErr_BadInternalCall();
1627 return -1;
1628 }
1629 assert(key);
1630 assert(hash != -1);
1631 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001632 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001633 if (ix == DKIX_ERROR)
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001634 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09001635 if (ix == DKIX_EMPTY || old_value == NULL) {
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001636 _PyErr_SetKeyError(key);
1637 return -1;
1638 }
Victor Stinner78601a32016-09-09 19:28:36 -07001639
1640 // Split table doesn't allow deletion. Combine it.
1641 if (_PyDict_HasSplitTable(mp)) {
1642 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1643 return -1;
1644 }
INADA Naoki778928b2017-08-03 23:45:15 +09001645 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001646 assert(ix >= 0);
1647 }
1648
INADA Naoki778928b2017-08-03 23:45:15 +09001649 return delitem_common(mp, hash, ix, old_value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001650}
1651
Antoine Pitroud741ed42016-12-27 14:23:43 +01001652/* This function promises that the predicate -> deletion sequence is atomic
1653 * (i.e. protected by the GIL), assuming the predicate itself doesn't
1654 * release the GIL.
1655 */
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001656int
1657_PyDict_DelItemIf(PyObject *op, PyObject *key,
1658 int (*predicate)(PyObject *value))
1659{
Antoine Pitroud741ed42016-12-27 14:23:43 +01001660 Py_ssize_t hashpos, ix;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001661 PyDictObject *mp;
1662 Py_hash_t hash;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001663 PyObject *old_value;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001664 int res;
1665
1666 if (!PyDict_Check(op)) {
1667 PyErr_BadInternalCall();
1668 return -1;
1669 }
1670 assert(key);
1671 hash = PyObject_Hash(key);
1672 if (hash == -1)
1673 return -1;
1674 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001675 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001676 if (ix == DKIX_ERROR)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001677 return -1;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001678 if (ix == DKIX_EMPTY || old_value == NULL) {
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001679 _PyErr_SetKeyError(key);
1680 return -1;
1681 }
Antoine Pitroud741ed42016-12-27 14:23:43 +01001682
1683 // Split table doesn't allow deletion. Combine it.
1684 if (_PyDict_HasSplitTable(mp)) {
1685 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1686 return -1;
1687 }
INADA Naoki778928b2017-08-03 23:45:15 +09001688 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001689 assert(ix >= 0);
1690 }
1691
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001692 res = predicate(old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001693 if (res == -1)
1694 return -1;
INADA Naoki778928b2017-08-03 23:45:15 +09001695
1696 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1697 assert(hashpos >= 0);
1698
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001699 if (res > 0)
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001700 return delitem_common(mp, hashpos, ix, old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001701 else
1702 return 0;
1703}
1704
1705
Guido van Rossum25831651993-05-19 14:50:45 +00001706void
Tim Peters1f5871e2000-07-04 17:44:48 +00001707PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001708{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001710 PyDictKeysObject *oldkeys;
1711 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 if (!PyDict_Check(op))
1715 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001716 mp = ((PyDictObject *)op);
1717 oldkeys = mp->ma_keys;
1718 oldvalues = mp->ma_values;
1719 if (oldvalues == empty_values)
1720 return;
1721 /* Empty the dict... */
INADA Naokia7576492018-11-14 18:39:27 +09001722 dictkeys_incref(Py_EMPTY_KEYS);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001723 mp->ma_keys = Py_EMPTY_KEYS;
1724 mp->ma_values = empty_values;
1725 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001726 mp->ma_version_tag = DICT_NEXT_VERSION();
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001727 /* ...then clear the keys and values */
1728 if (oldvalues != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001729 n = oldkeys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001730 for (i = 0; i < n; i++)
1731 Py_CLEAR(oldvalues[i]);
1732 free_values(oldvalues);
INADA Naokia7576492018-11-14 18:39:27 +09001733 dictkeys_decref(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001734 }
1735 else {
1736 assert(oldkeys->dk_refcnt == 1);
INADA Naokia7576492018-11-14 18:39:27 +09001737 dictkeys_decref(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001738 }
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001739 ASSERT_CONSISTENT(mp);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001740}
1741
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001742/* Internal version of PyDict_Next that returns a hash value in addition
1743 * to the key and value.
1744 * Return 1 on success, return 0 when the reached the end of the dictionary
1745 * (or if op is not a dictionary)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001746 */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001747int
1748_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1749 PyObject **pvalue, Py_hash_t *phash)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001750{
INADA Naokica2d8be2016-11-04 16:59:10 +09001751 Py_ssize_t i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001752 PyDictObject *mp;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001753 PyDictKeyEntry *entry_ptr;
1754 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001755
1756 if (!PyDict_Check(op))
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001757 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001758 mp = (PyDictObject *)op;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001759 i = *ppos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001760 if (mp->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09001761 if (i < 0 || i >= mp->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001762 return 0;
INADA Naokica2d8be2016-11-04 16:59:10 +09001763 /* values of split table is always dense */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001764 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
INADA Naokica2d8be2016-11-04 16:59:10 +09001765 value = mp->ma_values[i];
1766 assert(value != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001767 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001768 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09001769 Py_ssize_t n = mp->ma_keys->dk_nentries;
1770 if (i < 0 || i >= n)
1771 return 0;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001772 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
1773 while (i < n && entry_ptr->me_value == NULL) {
1774 entry_ptr++;
1775 i++;
Victor Stinner742da042016-09-07 17:40:12 -07001776 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001777 if (i >= n)
1778 return 0;
1779 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001780 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001781 *ppos = i+1;
1782 if (pkey)
1783 *pkey = entry_ptr->me_key;
1784 if (phash)
1785 *phash = entry_ptr->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001786 if (pvalue)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001787 *pvalue = value;
1788 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001789}
1790
Tim Peters080c88b2003-02-15 03:01:11 +00001791/*
1792 * Iterate over a dict. Use like so:
1793 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001794 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001795 * PyObject *key, *value;
1796 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001797 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001798 * Refer to borrowed references in key and value.
Tim Peters080c88b2003-02-15 03:01:11 +00001799 * }
1800 *
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001801 * Return 1 on success, return 0 when the reached the end of the dictionary
1802 * (or if op is not a dictionary)
1803 *
Tim Peters080c88b2003-02-15 03:01:11 +00001804 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001805 * mutates the dict. One exception: it is safe if the loop merely changes
1806 * the values associated with the keys (but doesn't insert new keys or
1807 * delete keys), via PyDict_SetItem().
1808 */
Guido van Rossum25831651993-05-19 14:50:45 +00001809int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001810PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001811{
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001812 return _PyDict_Next(op, ppos, pkey, pvalue, NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001813}
1814
Eric Snow96c6af92015-05-29 22:21:39 -06001815/* Internal version of dict.pop(). */
1816PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001817_PyDict_Pop_KnownHash(PyObject *dict, PyObject *key, Py_hash_t hash, PyObject *deflt)
Eric Snow96c6af92015-05-29 22:21:39 -06001818{
Victor Stinner742da042016-09-07 17:40:12 -07001819 Py_ssize_t ix, hashpos;
Eric Snow96c6af92015-05-29 22:21:39 -06001820 PyObject *old_value, *old_key;
1821 PyDictKeyEntry *ep;
Yury Selivanov684ef2c2016-10-28 19:01:21 -04001822 PyDictObject *mp;
1823
1824 assert(PyDict_Check(dict));
1825 mp = (PyDictObject *)dict;
Eric Snow96c6af92015-05-29 22:21:39 -06001826
1827 if (mp->ma_used == 0) {
1828 if (deflt) {
1829 Py_INCREF(deflt);
1830 return deflt;
1831 }
1832 _PyErr_SetKeyError(key);
1833 return NULL;
1834 }
INADA Naoki778928b2017-08-03 23:45:15 +09001835 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001836 if (ix == DKIX_ERROR)
Eric Snow96c6af92015-05-29 22:21:39 -06001837 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001838 if (ix == DKIX_EMPTY || old_value == NULL) {
Eric Snow96c6af92015-05-29 22:21:39 -06001839 if (deflt) {
1840 Py_INCREF(deflt);
1841 return deflt;
1842 }
1843 _PyErr_SetKeyError(key);
1844 return NULL;
1845 }
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001846
Victor Stinner78601a32016-09-09 19:28:36 -07001847 // Split table doesn't allow deletion. Combine it.
1848 if (_PyDict_HasSplitTable(mp)) {
1849 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1850 return NULL;
1851 }
INADA Naoki778928b2017-08-03 23:45:15 +09001852 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001853 assert(ix >= 0);
1854 }
1855
INADA Naoki778928b2017-08-03 23:45:15 +09001856 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1857 assert(hashpos >= 0);
Victor Stinner78601a32016-09-09 19:28:36 -07001858 assert(old_value != NULL);
Eric Snow96c6af92015-05-29 22:21:39 -06001859 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001860 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokia7576492018-11-14 18:39:27 +09001861 dictkeys_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
Victor Stinner78601a32016-09-09 19:28:36 -07001862 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1863 ENSURE_ALLOWS_DELETIONS(mp);
1864 old_key = ep->me_key;
1865 ep->me_key = NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001866 ep->me_value = NULL;
Victor Stinner78601a32016-09-09 19:28:36 -07001867 Py_DECREF(old_key);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001868
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001869 ASSERT_CONSISTENT(mp);
Eric Snow96c6af92015-05-29 22:21:39 -06001870 return old_value;
1871}
1872
Serhiy Storchaka67796522017-01-12 18:34:33 +02001873PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001874_PyDict_Pop(PyObject *dict, PyObject *key, PyObject *deflt)
Serhiy Storchaka67796522017-01-12 18:34:33 +02001875{
1876 Py_hash_t hash;
1877
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001878 if (((PyDictObject *)dict)->ma_used == 0) {
Serhiy Storchaka67796522017-01-12 18:34:33 +02001879 if (deflt) {
1880 Py_INCREF(deflt);
1881 return deflt;
1882 }
1883 _PyErr_SetKeyError(key);
1884 return NULL;
1885 }
1886 if (!PyUnicode_CheckExact(key) ||
1887 (hash = ((PyASCIIObject *) key)->hash) == -1) {
1888 hash = PyObject_Hash(key);
1889 if (hash == -1)
1890 return NULL;
1891 }
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001892 return _PyDict_Pop_KnownHash(dict, key, hash, deflt);
Serhiy Storchaka67796522017-01-12 18:34:33 +02001893}
1894
Eric Snow96c6af92015-05-29 22:21:39 -06001895/* Internal version of dict.from_keys(). It is subclass-friendly. */
1896PyObject *
1897_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
1898{
1899 PyObject *it; /* iter(iterable) */
1900 PyObject *key;
1901 PyObject *d;
1902 int status;
1903
Victor Stinnera5ed5f02016-12-06 18:45:50 +01001904 d = _PyObject_CallNoArg(cls);
Eric Snow96c6af92015-05-29 22:21:39 -06001905 if (d == NULL)
1906 return NULL;
1907
1908 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
1909 if (PyDict_CheckExact(iterable)) {
1910 PyDictObject *mp = (PyDictObject *)d;
1911 PyObject *oldvalue;
1912 Py_ssize_t pos = 0;
1913 PyObject *key;
1914 Py_hash_t hash;
1915
Serhiy Storchakac61ac162017-03-21 08:52:38 +02001916 if (dictresize(mp, ESTIMATE_SIZE(PyDict_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001917 Py_DECREF(d);
1918 return NULL;
1919 }
1920
1921 while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
1922 if (insertdict(mp, key, hash, value)) {
1923 Py_DECREF(d);
1924 return NULL;
1925 }
1926 }
1927 return d;
1928 }
1929 if (PyAnySet_CheckExact(iterable)) {
1930 PyDictObject *mp = (PyDictObject *)d;
1931 Py_ssize_t pos = 0;
1932 PyObject *key;
1933 Py_hash_t hash;
1934
Victor Stinner742da042016-09-07 17:40:12 -07001935 if (dictresize(mp, ESTIMATE_SIZE(PySet_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001936 Py_DECREF(d);
1937 return NULL;
1938 }
1939
1940 while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
1941 if (insertdict(mp, key, hash, value)) {
1942 Py_DECREF(d);
1943 return NULL;
1944 }
1945 }
1946 return d;
1947 }
1948 }
1949
1950 it = PyObject_GetIter(iterable);
1951 if (it == NULL){
1952 Py_DECREF(d);
1953 return NULL;
1954 }
1955
1956 if (PyDict_CheckExact(d)) {
1957 while ((key = PyIter_Next(it)) != NULL) {
1958 status = PyDict_SetItem(d, key, value);
1959 Py_DECREF(key);
1960 if (status < 0)
1961 goto Fail;
1962 }
1963 } else {
1964 while ((key = PyIter_Next(it)) != NULL) {
1965 status = PyObject_SetItem(d, key, value);
1966 Py_DECREF(key);
1967 if (status < 0)
1968 goto Fail;
1969 }
1970 }
1971
1972 if (PyErr_Occurred())
1973 goto Fail;
1974 Py_DECREF(it);
1975 return d;
1976
1977Fail:
1978 Py_DECREF(it);
1979 Py_DECREF(d);
1980 return NULL;
1981}
1982
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001983/* Methods */
1984
1985static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001986dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001987{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001988 PyObject **values = mp->ma_values;
1989 PyDictKeysObject *keys = mp->ma_keys;
1990 Py_ssize_t i, n;
INADA Naokia6296d32017-08-24 14:55:17 +09001991
1992 /* bpo-31095: UnTrack is needed before calling any callbacks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001993 PyObject_GC_UnTrack(mp);
Jeroen Demeyer351c6742019-05-10 19:21:11 +02001994 Py_TRASHCAN_BEGIN(mp, dict_dealloc)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001995 if (values != NULL) {
1996 if (values != empty_values) {
Victor Stinner742da042016-09-07 17:40:12 -07001997 for (i = 0, n = mp->ma_keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001998 Py_XDECREF(values[i]);
1999 }
2000 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002001 }
INADA Naokia7576492018-11-14 18:39:27 +09002002 dictkeys_decref(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02002004 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02002005 assert(keys->dk_refcnt == 1);
INADA Naokia7576492018-11-14 18:39:27 +09002006 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002007 }
Dong-hee Na1b55b652020-02-17 19:09:15 +09002008 if (numfree < PyDict_MAXFREELIST && Py_IS_TYPE(mp, &PyDict_Type))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002009 free_list[numfree++] = mp;
2010 else
2011 Py_TYPE(mp)->tp_free((PyObject *)mp);
Jeroen Demeyer351c6742019-05-10 19:21:11 +02002012 Py_TRASHCAN_END
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002013}
2014
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002015
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002016static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002017dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002018{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002019 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01002020 PyObject *key = NULL, *value = NULL;
2021 _PyUnicodeWriter writer;
2022 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00002023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002024 i = Py_ReprEnter((PyObject *)mp);
2025 if (i != 0) {
2026 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
2027 }
Guido van Rossum255443b1998-04-10 22:47:14 +00002028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002029 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01002030 Py_ReprLeave((PyObject *)mp);
2031 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002032 }
Tim Petersa7259592001-06-16 05:11:17 +00002033
Victor Stinnerf91929b2013-11-19 13:07:38 +01002034 _PyUnicodeWriter_Init(&writer);
2035 writer.overallocate = 1;
2036 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
2037 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00002038
Victor Stinnerf91929b2013-11-19 13:07:38 +01002039 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
2040 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00002041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002042 /* Do repr() on each key+value pair, and insert ": " between them.
2043 Note that repr may mutate the dict. */
2044 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01002045 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002046 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01002047 PyObject *s;
2048 int res;
2049
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002050 /* Prevent repr from deleting key or value during key format. */
2051 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002052 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02002053
Victor Stinnerf91929b2013-11-19 13:07:38 +01002054 if (!first) {
2055 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
2056 goto error;
2057 }
2058 first = 0;
2059
2060 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01002062 goto error;
2063 res = _PyUnicodeWriter_WriteStr(&writer, s);
2064 Py_DECREF(s);
2065 if (res < 0)
2066 goto error;
2067
2068 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
2069 goto error;
2070
2071 s = PyObject_Repr(value);
2072 if (s == NULL)
2073 goto error;
2074 res = _PyUnicodeWriter_WriteStr(&writer, s);
2075 Py_DECREF(s);
2076 if (res < 0)
2077 goto error;
2078
2079 Py_CLEAR(key);
2080 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002081 }
Tim Petersa7259592001-06-16 05:11:17 +00002082
Victor Stinnerf91929b2013-11-19 13:07:38 +01002083 writer.overallocate = 0;
2084 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
2085 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00002086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002087 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01002088
2089 return _PyUnicodeWriter_Finish(&writer);
2090
2091error:
2092 Py_ReprLeave((PyObject *)mp);
2093 _PyUnicodeWriter_Dealloc(&writer);
2094 Py_XDECREF(key);
2095 Py_XDECREF(value);
2096 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002097}
2098
Martin v. Löwis18e16552006-02-15 17:27:45 +00002099static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002100dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002101{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002102 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002103}
2104
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002105static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002106dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002107{
Victor Stinner742da042016-09-07 17:40:12 -07002108 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002109 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09002110 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002111
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002112 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002113 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002114 hash = PyObject_Hash(key);
2115 if (hash == -1)
2116 return NULL;
2117 }
INADA Naoki778928b2017-08-03 23:45:15 +09002118 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002119 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002120 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002121 if (ix == DKIX_EMPTY || value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122 if (!PyDict_CheckExact(mp)) {
2123 /* Look up __missing__ method if we're a subclass. */
2124 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05002125 _Py_IDENTIFIER(__missing__);
2126 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002127 if (missing != NULL) {
Petr Viktorinffd97532020-02-11 17:46:57 +01002128 res = PyObject_CallOneArg(missing, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002129 Py_DECREF(missing);
2130 return res;
2131 }
2132 else if (PyErr_Occurred())
2133 return NULL;
2134 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07002135 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002136 return NULL;
2137 }
INADA Naokiba609772016-12-07 20:41:42 +09002138 Py_INCREF(value);
2139 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002140}
2141
2142static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002143dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002144{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002145 if (w == NULL)
2146 return PyDict_DelItem((PyObject *)mp, v);
2147 else
2148 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002149}
2150
Guido van Rossuma9e7a811997-05-13 21:02:11 +00002151static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002152 (lenfunc)dict_length, /*mp_length*/
2153 (binaryfunc)dict_subscript, /*mp_subscript*/
2154 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002155};
2156
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002157static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002158dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002159{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002160 PyObject *v;
2161 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002162 PyDictKeyEntry *ep;
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002163 Py_ssize_t n, offset;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002164 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002165
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002166 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002167 n = mp->ma_used;
2168 v = PyList_New(n);
2169 if (v == NULL)
2170 return NULL;
2171 if (n != mp->ma_used) {
2172 /* Durnit. The allocations caused the dict to resize.
2173 * Just start over, this shouldn't normally happen.
2174 */
2175 Py_DECREF(v);
2176 goto again;
2177 }
Victor Stinner742da042016-09-07 17:40:12 -07002178 ep = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002179 if (mp->ma_values) {
2180 value_ptr = mp->ma_values;
2181 offset = sizeof(PyObject *);
2182 }
2183 else {
2184 value_ptr = &ep[0].me_value;
2185 offset = sizeof(PyDictKeyEntry);
2186 }
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002187 for (i = 0, j = 0; j < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002188 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002189 PyObject *key = ep[i].me_key;
2190 Py_INCREF(key);
2191 PyList_SET_ITEM(v, j, key);
2192 j++;
2193 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002194 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002195 }
2196 assert(j == n);
2197 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002198}
2199
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002200static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002201dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002202{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002203 PyObject *v;
2204 Py_ssize_t i, j;
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002205 PyDictKeyEntry *ep;
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002206 Py_ssize_t n, offset;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002207 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002208
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002209 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002210 n = mp->ma_used;
2211 v = PyList_New(n);
2212 if (v == NULL)
2213 return NULL;
2214 if (n != mp->ma_used) {
2215 /* Durnit. The allocations caused the dict to resize.
2216 * Just start over, this shouldn't normally happen.
2217 */
2218 Py_DECREF(v);
2219 goto again;
2220 }
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002221 ep = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002222 if (mp->ma_values) {
2223 value_ptr = mp->ma_values;
2224 offset = sizeof(PyObject *);
2225 }
2226 else {
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002227 value_ptr = &ep[0].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002228 offset = sizeof(PyDictKeyEntry);
2229 }
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002230 for (i = 0, j = 0; j < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002231 PyObject *value = *value_ptr;
2232 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2233 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002234 Py_INCREF(value);
2235 PyList_SET_ITEM(v, j, value);
2236 j++;
2237 }
2238 }
2239 assert(j == n);
2240 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002241}
2242
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002243static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002244dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002245{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002246 PyObject *v;
2247 Py_ssize_t i, j, n;
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002248 Py_ssize_t offset;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002249 PyObject *item, *key;
2250 PyDictKeyEntry *ep;
2251 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002253 /* Preallocate the list of tuples, to avoid allocations during
2254 * the loop over the items, which could trigger GC, which
2255 * could resize the dict. :-(
2256 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002257 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002258 n = mp->ma_used;
2259 v = PyList_New(n);
2260 if (v == NULL)
2261 return NULL;
2262 for (i = 0; i < n; i++) {
2263 item = PyTuple_New(2);
2264 if (item == NULL) {
2265 Py_DECREF(v);
2266 return NULL;
2267 }
2268 PyList_SET_ITEM(v, i, item);
2269 }
2270 if (n != mp->ma_used) {
2271 /* Durnit. The allocations caused the dict to resize.
2272 * Just start over, this shouldn't normally happen.
2273 */
2274 Py_DECREF(v);
2275 goto again;
2276 }
2277 /* Nothing we do below makes any function calls. */
Victor Stinner742da042016-09-07 17:40:12 -07002278 ep = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002279 if (mp->ma_values) {
2280 value_ptr = mp->ma_values;
2281 offset = sizeof(PyObject *);
2282 }
2283 else {
2284 value_ptr = &ep[0].me_value;
2285 offset = sizeof(PyDictKeyEntry);
2286 }
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002287 for (i = 0, j = 0; j < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002288 PyObject *value = *value_ptr;
2289 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2290 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002291 key = ep[i].me_key;
2292 item = PyList_GET_ITEM(v, j);
2293 Py_INCREF(key);
2294 PyTuple_SET_ITEM(item, 0, key);
2295 Py_INCREF(value);
2296 PyTuple_SET_ITEM(item, 1, value);
2297 j++;
2298 }
2299 }
2300 assert(j == n);
2301 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002302}
2303
Larry Hastings5c661892014-01-24 06:17:25 -08002304/*[clinic input]
2305@classmethod
2306dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08002307 iterable: object
2308 value: object=None
2309 /
2310
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002311Create a new dictionary with keys from iterable and values set to value.
Larry Hastings5c661892014-01-24 06:17:25 -08002312[clinic start generated code]*/
2313
Larry Hastings5c661892014-01-24 06:17:25 -08002314static PyObject *
2315dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002316/*[clinic end generated code: output=8fb98e4b10384999 input=382ba4855d0f74c3]*/
Larry Hastings5c661892014-01-24 06:17:25 -08002317{
Eric Snow96c6af92015-05-29 22:21:39 -06002318 return _PyDict_FromKeys((PyObject *)type, iterable, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002319}
2320
Brandt Buchereb8ac572020-02-24 19:47:34 -08002321/* Single-arg dict update; used by dict_update_common and operators. */
2322static int
2323dict_update_arg(PyObject *self, PyObject *arg)
2324{
2325 if (PyDict_CheckExact(arg)) {
2326 return PyDict_Merge(self, arg, 1);
2327 }
2328 _Py_IDENTIFIER(keys);
2329 PyObject *func;
2330 if (_PyObject_LookupAttrId(arg, &PyId_keys, &func) < 0) {
2331 return -1;
2332 }
2333 if (func != NULL) {
2334 Py_DECREF(func);
2335 return PyDict_Merge(self, arg, 1);
2336 }
2337 return PyDict_MergeFromSeq2(self, arg, 1);
2338}
2339
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002340static int
Victor Stinner742da042016-09-07 17:40:12 -07002341dict_update_common(PyObject *self, PyObject *args, PyObject *kwds,
2342 const char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002343{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002344 PyObject *arg = NULL;
2345 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002346
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002347 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002348 result = -1;
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002349 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002350 else if (arg != NULL) {
Brandt Buchereb8ac572020-02-24 19:47:34 -08002351 result = dict_update_arg(self, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002352 }
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 if (result == 0 && kwds != NULL) {
2355 if (PyArg_ValidateKeywordArguments(kwds))
2356 result = PyDict_Merge(self, kwds, 1);
2357 else
2358 result = -1;
2359 }
2360 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002361}
2362
Victor Stinner91f0d4a2017-01-19 12:45:06 +01002363/* Note: dict.update() uses the METH_VARARGS|METH_KEYWORDS calling convention.
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +03002364 Using METH_FASTCALL|METH_KEYWORDS would make dict.update(**dict2) calls
2365 slower, see the issue #29312. */
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002366static PyObject *
2367dict_update(PyObject *self, PyObject *args, PyObject *kwds)
2368{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 if (dict_update_common(self, args, kwds, "update") != -1)
2370 Py_RETURN_NONE;
2371 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002372}
2373
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002374/* Update unconditionally replaces existing items.
2375 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00002376 otherwise it leaves existing items unchanged.
2377
2378 PyDict_{Update,Merge} update/merge from a mapping object.
2379
Tim Petersf582b822001-12-11 18:51:08 +00002380 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00002381 producing iterable objects of length 2.
2382*/
2383
Tim Petersf582b822001-12-11 18:51:08 +00002384int
Tim Peters1fc240e2001-10-26 05:06:50 +00002385PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
2386{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002387 PyObject *it; /* iter(seq2) */
2388 Py_ssize_t i; /* index into seq2 of current element */
2389 PyObject *item; /* seq2[i] */
2390 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00002391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392 assert(d != NULL);
2393 assert(PyDict_Check(d));
2394 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00002395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002396 it = PyObject_GetIter(seq2);
2397 if (it == NULL)
2398 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002400 for (i = 0; ; ++i) {
2401 PyObject *key, *value;
2402 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00002403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002404 fast = NULL;
2405 item = PyIter_Next(it);
2406 if (item == NULL) {
2407 if (PyErr_Occurred())
2408 goto Fail;
2409 break;
2410 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002412 /* Convert item to sequence, and verify length 2. */
2413 fast = PySequence_Fast(item, "");
2414 if (fast == NULL) {
2415 if (PyErr_ExceptionMatches(PyExc_TypeError))
2416 PyErr_Format(PyExc_TypeError,
2417 "cannot convert dictionary update "
2418 "sequence element #%zd to a sequence",
2419 i);
2420 goto Fail;
2421 }
2422 n = PySequence_Fast_GET_SIZE(fast);
2423 if (n != 2) {
2424 PyErr_Format(PyExc_ValueError,
2425 "dictionary update sequence element #%zd "
2426 "has length %zd; 2 is required",
2427 i, n);
2428 goto Fail;
2429 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002431 /* Update/merge with this (key, value) pair. */
2432 key = PySequence_Fast_GET_ITEM(fast, 0);
2433 value = PySequence_Fast_GET_ITEM(fast, 1);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002434 Py_INCREF(key);
2435 Py_INCREF(value);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002436 if (override) {
2437 if (PyDict_SetItem(d, key, value) < 0) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002438 Py_DECREF(key);
2439 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002440 goto Fail;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002441 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002442 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002443 else if (PyDict_GetItemWithError(d, key) == NULL) {
2444 if (PyErr_Occurred() || PyDict_SetItem(d, key, value) < 0) {
2445 Py_DECREF(key);
2446 Py_DECREF(value);
2447 goto Fail;
2448 }
2449 }
2450
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002451 Py_DECREF(key);
2452 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002453 Py_DECREF(fast);
2454 Py_DECREF(item);
2455 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002457 i = 0;
Victor Stinner0fc91ee2019-04-12 21:51:34 +02002458 ASSERT_CONSISTENT(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002459 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00002460Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002461 Py_XDECREF(item);
2462 Py_XDECREF(fast);
2463 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002464Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002465 Py_DECREF(it);
2466 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00002467}
2468
doko@ubuntu.comc96df682016-10-11 08:04:02 +02002469static int
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002470dict_merge(PyObject *a, PyObject *b, int override)
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002471{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002472 PyDictObject *mp, *other;
2473 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -07002474 PyDictKeyEntry *entry, *ep0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002475
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002476 assert(0 <= override && override <= 2);
2477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002478 /* We accept for the argument either a concrete dictionary object,
2479 * or an abstract "mapping" object. For the former, we can do
2480 * things quite efficiently. For the latter, we only require that
2481 * PyMapping_Keys() and PyObject_GetItem() be supported.
2482 */
2483 if (a == NULL || !PyDict_Check(a) || b == NULL) {
2484 PyErr_BadInternalCall();
2485 return -1;
2486 }
2487 mp = (PyDictObject*)a;
INADA Naoki2aaf98c2018-09-26 12:59:00 +09002488 if (PyDict_Check(b) && (Py_TYPE(b)->tp_iter == (getiterfunc)dict_iter)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002489 other = (PyDictObject*)b;
2490 if (other == mp || other->ma_used == 0)
2491 /* a.update(a) or a.update({}); nothing to do */
2492 return 0;
2493 if (mp->ma_used == 0)
2494 /* Since the target dict is empty, PyDict_GetItem()
2495 * always returns NULL. Setting override to 1
2496 * skips the unnecessary test.
2497 */
2498 override = 1;
2499 /* Do one big resize at the start, rather than
2500 * incrementally resizing as we insert new items. Expect
2501 * that there will be no (or few) overlapping keys.
2502 */
INADA Naokib1152be2016-10-27 19:26:50 +09002503 if (USABLE_FRACTION(mp->ma_keys->dk_size) < other->ma_used) {
2504 if (dictresize(mp, ESTIMATE_SIZE(mp->ma_used + other->ma_used))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002505 return -1;
INADA Naokib1152be2016-10-27 19:26:50 +09002506 }
2507 }
Victor Stinner742da042016-09-07 17:40:12 -07002508 ep0 = DK_ENTRIES(other->ma_keys);
2509 for (i = 0, n = other->ma_keys->dk_nentries; i < n; i++) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002510 PyObject *key, *value;
2511 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002512 entry = &ep0[i];
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002513 key = entry->me_key;
2514 hash = entry->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002515 if (other->ma_values)
2516 value = other->ma_values[i];
2517 else
2518 value = entry->me_value;
2519
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002520 if (value != NULL) {
2521 int err = 0;
2522 Py_INCREF(key);
2523 Py_INCREF(value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002524 if (override == 1)
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002525 err = insertdict(mp, key, hash, value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002526 else if (_PyDict_GetItem_KnownHash(a, key, hash) == NULL) {
2527 if (PyErr_Occurred()) {
2528 Py_DECREF(value);
2529 Py_DECREF(key);
2530 return -1;
2531 }
2532 err = insertdict(mp, key, hash, value);
2533 }
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002534 else if (override != 0) {
2535 _PyErr_SetKeyError(key);
2536 Py_DECREF(value);
2537 Py_DECREF(key);
2538 return -1;
2539 }
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002540 Py_DECREF(value);
2541 Py_DECREF(key);
2542 if (err != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002543 return -1;
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002544
Victor Stinner742da042016-09-07 17:40:12 -07002545 if (n != other->ma_keys->dk_nentries) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002546 PyErr_SetString(PyExc_RuntimeError,
2547 "dict mutated during update");
2548 return -1;
2549 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002550 }
2551 }
2552 }
2553 else {
2554 /* Do it the generic, slower way */
2555 PyObject *keys = PyMapping_Keys(b);
2556 PyObject *iter;
2557 PyObject *key, *value;
2558 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002560 if (keys == NULL)
2561 /* Docstring says this is equivalent to E.keys() so
2562 * if E doesn't have a .keys() method we want
2563 * AttributeError to percolate up. Might as well
2564 * do the same for any other error.
2565 */
2566 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002568 iter = PyObject_GetIter(keys);
2569 Py_DECREF(keys);
2570 if (iter == NULL)
2571 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002573 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002574 if (override != 1) {
2575 if (PyDict_GetItemWithError(a, key) != NULL) {
2576 if (override != 0) {
2577 _PyErr_SetKeyError(key);
2578 Py_DECREF(key);
2579 Py_DECREF(iter);
2580 return -1;
2581 }
2582 Py_DECREF(key);
2583 continue;
2584 }
2585 else if (PyErr_Occurred()) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002586 Py_DECREF(key);
2587 Py_DECREF(iter);
2588 return -1;
2589 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002590 }
2591 value = PyObject_GetItem(b, key);
2592 if (value == NULL) {
2593 Py_DECREF(iter);
2594 Py_DECREF(key);
2595 return -1;
2596 }
2597 status = PyDict_SetItem(a, key, value);
2598 Py_DECREF(key);
2599 Py_DECREF(value);
2600 if (status < 0) {
2601 Py_DECREF(iter);
2602 return -1;
2603 }
2604 }
2605 Py_DECREF(iter);
2606 if (PyErr_Occurred())
2607 /* Iterator completed, via error */
2608 return -1;
2609 }
Victor Stinner0fc91ee2019-04-12 21:51:34 +02002610 ASSERT_CONSISTENT(a);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002611 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002612}
2613
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002614int
2615PyDict_Update(PyObject *a, PyObject *b)
2616{
2617 return dict_merge(a, b, 1);
2618}
2619
2620int
2621PyDict_Merge(PyObject *a, PyObject *b, int override)
2622{
2623 /* XXX Deprecate override not in (0, 1). */
2624 return dict_merge(a, b, override != 0);
2625}
2626
2627int
2628_PyDict_MergeEx(PyObject *a, PyObject *b, int override)
2629{
2630 return dict_merge(a, b, override);
2631}
2632
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002633static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302634dict_copy(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002635{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002636 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002637}
2638
2639PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002640PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002641{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002642 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002643 PyDictObject *mp;
2644 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002646 if (o == NULL || !PyDict_Check(o)) {
2647 PyErr_BadInternalCall();
2648 return NULL;
2649 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002650
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002651 mp = (PyDictObject *)o;
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002652 if (mp->ma_used == 0) {
2653 /* The dict is empty; just return a new dict. */
2654 return PyDict_New();
2655 }
2656
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002657 if (_PyDict_HasSplitTable(mp)) {
2658 PyDictObject *split_copy;
Victor Stinner742da042016-09-07 17:40:12 -07002659 Py_ssize_t size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
2660 PyObject **newvalues;
2661 newvalues = new_values(size);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002662 if (newvalues == NULL)
2663 return PyErr_NoMemory();
2664 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2665 if (split_copy == NULL) {
2666 free_values(newvalues);
2667 return NULL;
2668 }
2669 split_copy->ma_values = newvalues;
2670 split_copy->ma_keys = mp->ma_keys;
2671 split_copy->ma_used = mp->ma_used;
INADA Naokid1c82c52018-04-03 11:43:53 +09002672 split_copy->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokia7576492018-11-14 18:39:27 +09002673 dictkeys_incref(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002674 for (i = 0, n = size; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002675 PyObject *value = mp->ma_values[i];
2676 Py_XINCREF(value);
2677 split_copy->ma_values[i] = value;
2678 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002679 if (_PyObject_GC_IS_TRACKED(mp))
2680 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002681 return (PyObject *)split_copy;
2682 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002683
2684 if (PyDict_CheckExact(mp) && mp->ma_values == NULL &&
2685 (mp->ma_used >= (mp->ma_keys->dk_nentries * 2) / 3))
2686 {
2687 /* Use fast-copy if:
2688
2689 (1) 'mp' is an instance of a subclassed dict; and
2690
2691 (2) 'mp' is not a split-dict; and
2692
2693 (3) if 'mp' is non-compact ('del' operation does not resize dicts),
2694 do fast-copy only if it has at most 1/3 non-used keys.
2695
Ville Skyttä61f82e02018-04-20 23:08:45 +03002696 The last condition (3) is important to guard against a pathological
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002697 case when a large dict is almost emptied with multiple del/pop
2698 operations and copied after that. In cases like this, we defer to
2699 PyDict_Merge, which produces a compacted copy.
2700 */
2701 return clone_combined_dict(mp);
2702 }
2703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002704 copy = PyDict_New();
2705 if (copy == NULL)
2706 return NULL;
2707 if (PyDict_Merge(copy, o, 1) == 0)
2708 return copy;
2709 Py_DECREF(copy);
2710 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002711}
2712
Martin v. Löwis18e16552006-02-15 17:27:45 +00002713Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002714PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002715{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002716 if (mp == NULL || !PyDict_Check(mp)) {
2717 PyErr_BadInternalCall();
2718 return -1;
2719 }
2720 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002721}
2722
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002723PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002724PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002725{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002726 if (mp == NULL || !PyDict_Check(mp)) {
2727 PyErr_BadInternalCall();
2728 return NULL;
2729 }
2730 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002731}
2732
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002733PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002734PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002735{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002736 if (mp == NULL || !PyDict_Check(mp)) {
2737 PyErr_BadInternalCall();
2738 return NULL;
2739 }
2740 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002741}
2742
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002743PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002744PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002745{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002746 if (mp == NULL || !PyDict_Check(mp)) {
2747 PyErr_BadInternalCall();
2748 return NULL;
2749 }
2750 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002751}
2752
Tim Peterse63415e2001-05-08 04:38:29 +00002753/* Return 1 if dicts equal, 0 if not, -1 if error.
2754 * Gets out as soon as any difference is detected.
2755 * Uses only Py_EQ comparison.
2756 */
2757static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002758dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002759{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002760 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002762 if (a->ma_used != b->ma_used)
2763 /* can't be equal if # of entries differ */
2764 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002765 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Victor Stinner742da042016-09-07 17:40:12 -07002766 for (i = 0; i < a->ma_keys->dk_nentries; i++) {
2767 PyDictKeyEntry *ep = &DK_ENTRIES(a->ma_keys)[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002768 PyObject *aval;
2769 if (a->ma_values)
2770 aval = a->ma_values[i];
2771 else
2772 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002773 if (aval != NULL) {
2774 int cmp;
2775 PyObject *bval;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002776 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002777 /* temporarily bump aval's refcount to ensure it stays
2778 alive until we're done with it */
2779 Py_INCREF(aval);
2780 /* ditto for key */
2781 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002782 /* reuse the known hash value */
INADA Naoki778928b2017-08-03 23:45:15 +09002783 b->ma_keys->dk_lookup(b, key, ep->me_hash, &bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002784 if (bval == NULL) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002785 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002786 Py_DECREF(aval);
2787 if (PyErr_Occurred())
2788 return -1;
2789 return 0;
2790 }
Dong-hee Na2d5bf562019-12-31 10:04:22 +09002791 Py_INCREF(bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002792 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002793 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002794 Py_DECREF(aval);
Dong-hee Na2d5bf562019-12-31 10:04:22 +09002795 Py_DECREF(bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002796 if (cmp <= 0) /* error or not equal */
2797 return cmp;
2798 }
2799 }
2800 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002801}
Tim Peterse63415e2001-05-08 04:38:29 +00002802
2803static PyObject *
2804dict_richcompare(PyObject *v, PyObject *w, int op)
2805{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002806 int cmp;
2807 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002808
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002809 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2810 res = Py_NotImplemented;
2811 }
2812 else if (op == Py_EQ || op == Py_NE) {
2813 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2814 if (cmp < 0)
2815 return NULL;
2816 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2817 }
2818 else
2819 res = Py_NotImplemented;
2820 Py_INCREF(res);
2821 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002822}
Tim Peterse63415e2001-05-08 04:38:29 +00002823
Larry Hastings61272b72014-01-07 12:41:53 -08002824/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002825
2826@coexist
2827dict.__contains__
2828
2829 key: object
2830 /
2831
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002832True if the dictionary has the specified key, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002833[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002834
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002835static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002836dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka19d25972017-02-04 08:05:07 +02002837/*[clinic end generated code: output=a3d03db709ed6e6b input=fe1cb42ad831e820]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002838{
Larry Hastingsc2047262014-01-25 20:43:29 -08002839 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002840 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002841 Py_ssize_t ix;
INADA Naokiba609772016-12-07 20:41:42 +09002842 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002844 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002845 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002846 hash = PyObject_Hash(key);
2847 if (hash == -1)
2848 return NULL;
2849 }
INADA Naoki778928b2017-08-03 23:45:15 +09002850 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002851 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002852 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002853 if (ix == DKIX_EMPTY || value == NULL)
Victor Stinner742da042016-09-07 17:40:12 -07002854 Py_RETURN_FALSE;
2855 Py_RETURN_TRUE;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002856}
2857
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002858/*[clinic input]
2859dict.get
2860
2861 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002862 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002863 /
2864
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002865Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002866[clinic start generated code]*/
2867
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002868static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002869dict_get_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002870/*[clinic end generated code: output=bba707729dee05bf input=279ddb5790b6b107]*/
Barry Warsawc38c5da1997-10-06 17:49:20 +00002871{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002872 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002873 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002874 Py_ssize_t ix;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002875
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002877 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002878 hash = PyObject_Hash(key);
2879 if (hash == -1)
2880 return NULL;
2881 }
INADA Naoki778928b2017-08-03 23:45:15 +09002882 ix = (self->ma_keys->dk_lookup) (self, key, hash, &val);
Victor Stinner742da042016-09-07 17:40:12 -07002883 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002884 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002885 if (ix == DKIX_EMPTY || val == NULL) {
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002886 val = default_value;
INADA Naokiba609772016-12-07 20:41:42 +09002887 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002888 Py_INCREF(val);
2889 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002890}
2891
Benjamin Peterson00e98862013-03-07 22:16:29 -05002892PyObject *
2893PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002894{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002895 PyDictObject *mp = (PyDictObject *)d;
INADA Naoki93f26f72016-11-02 18:45:16 +09002896 PyObject *value;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002897 Py_hash_t hash;
Guido van Rossum164452c2000-08-08 16:12:54 +00002898
Benjamin Peterson00e98862013-03-07 22:16:29 -05002899 if (!PyDict_Check(d)) {
2900 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002902 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002904 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002905 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002906 hash = PyObject_Hash(key);
2907 if (hash == -1)
2908 return NULL;
2909 }
Inada Naoki2ddc7f62019-03-18 20:38:33 +09002910 if (mp->ma_keys == Py_EMPTY_KEYS) {
2911 if (insert_to_emptydict(mp, key, hash, defaultobj) < 0) {
2912 return NULL;
2913 }
2914 return defaultobj;
2915 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002916
2917 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
2918 if (insertion_resize(mp) < 0)
2919 return NULL;
2920 }
2921
INADA Naoki778928b2017-08-03 23:45:15 +09002922 Py_ssize_t ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002923 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002924 return NULL;
INADA Naoki93f26f72016-11-02 18:45:16 +09002925
2926 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09002927 ((ix >= 0 && value == NULL && mp->ma_used != ix) ||
INADA Naoki93f26f72016-11-02 18:45:16 +09002928 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
2929 if (insertion_resize(mp) < 0) {
2930 return NULL;
2931 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002932 ix = DKIX_EMPTY;
2933 }
2934
2935 if (ix == DKIX_EMPTY) {
2936 PyDictKeyEntry *ep, *ep0;
2937 value = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002938 if (mp->ma_keys->dk_usable <= 0) {
Victor Stinner3c336c52016-09-12 14:17:40 +02002939 if (insertion_resize(mp) < 0) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002940 return NULL;
Victor Stinner3c336c52016-09-12 14:17:40 +02002941 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002942 }
INADA Naoki778928b2017-08-03 23:45:15 +09002943 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naoki93f26f72016-11-02 18:45:16 +09002944 ep0 = DK_ENTRIES(mp->ma_keys);
2945 ep = &ep0[mp->ma_keys->dk_nentries];
INADA Naokia7576492018-11-14 18:39:27 +09002946 dictkeys_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002947 Py_INCREF(key);
INADA Naoki93f26f72016-11-02 18:45:16 +09002948 Py_INCREF(value);
2949 MAINTAIN_TRACKING(mp, key, value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002950 ep->me_key = key;
2951 ep->me_hash = hash;
INADA Naokiba609772016-12-07 20:41:42 +09002952 if (_PyDict_HasSplitTable(mp)) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002953 assert(mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
2954 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Victor Stinner742da042016-09-07 17:40:12 -07002955 }
2956 else {
INADA Naoki93f26f72016-11-02 18:45:16 +09002957 ep->me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07002958 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002959 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002960 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naoki93f26f72016-11-02 18:45:16 +09002961 mp->ma_keys->dk_usable--;
2962 mp->ma_keys->dk_nentries++;
2963 assert(mp->ma_keys->dk_usable >= 0);
2964 }
INADA Naokiba609772016-12-07 20:41:42 +09002965 else if (value == NULL) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002966 value = defaultobj;
2967 assert(_PyDict_HasSplitTable(mp));
2968 assert(ix == mp->ma_used);
2969 Py_INCREF(value);
2970 MAINTAIN_TRACKING(mp, key, value);
INADA Naokiba609772016-12-07 20:41:42 +09002971 mp->ma_values[ix] = value;
INADA Naoki93f26f72016-11-02 18:45:16 +09002972 mp->ma_used++;
2973 mp->ma_version_tag = DICT_NEXT_VERSION();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002974 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002975
Victor Stinner0fc91ee2019-04-12 21:51:34 +02002976 ASSERT_CONSISTENT(mp);
INADA Naoki93f26f72016-11-02 18:45:16 +09002977 return value;
Guido van Rossum164452c2000-08-08 16:12:54 +00002978}
2979
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002980/*[clinic input]
2981dict.setdefault
2982
2983 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002984 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002985 /
2986
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002987Insert key with a value of default if key is not in the dictionary.
2988
2989Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002990[clinic start generated code]*/
2991
Benjamin Peterson00e98862013-03-07 22:16:29 -05002992static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002993dict_setdefault_impl(PyDictObject *self, PyObject *key,
2994 PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002995/*[clinic end generated code: output=f8c1101ebf69e220 input=0f063756e815fd9d]*/
Benjamin Peterson00e98862013-03-07 22:16:29 -05002996{
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002997 PyObject *val;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002998
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002999 val = PyDict_SetDefault((PyObject *)self, key, default_value);
Benjamin Peterson00e98862013-03-07 22:16:29 -05003000 Py_XINCREF(val);
3001 return val;
3002}
Guido van Rossum164452c2000-08-08 16:12:54 +00003003
3004static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303005dict_clear(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00003006{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003007 PyDict_Clear((PyObject *)mp);
3008 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00003009}
3010
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003011/*[clinic input]
3012dict.pop
3013
3014 key: object
3015 default: object = NULL
3016 /
3017
Serhiy Storchaka279f4462019-09-14 12:24:05 +03003018D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003019
3020If key is not found, default is returned if given, otherwise KeyError is raised
3021[clinic start generated code]*/
3022
Guido van Rossumba6ab842000-12-12 22:02:18 +00003023static PyObject *
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003024dict_pop_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
Serhiy Storchaka279f4462019-09-14 12:24:05 +03003025/*[clinic end generated code: output=3abb47b89f24c21c input=eeebec7812190348]*/
Guido van Rossume027d982002-04-12 15:11:59 +00003026{
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003027 return _PyDict_Pop((PyObject*)self, key, default_value);
Guido van Rossume027d982002-04-12 15:11:59 +00003028}
3029
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003030/*[clinic input]
3031dict.popitem
3032
3033Remove and return a (key, value) pair as a 2-tuple.
3034
3035Pairs are returned in LIFO (last-in, first-out) order.
3036Raises KeyError if the dict is empty.
3037[clinic start generated code]*/
3038
Guido van Rossume027d982002-04-12 15:11:59 +00003039static PyObject *
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003040dict_popitem_impl(PyDictObject *self)
3041/*[clinic end generated code: output=e65fcb04420d230d input=1c38a49f21f64941]*/
Guido van Rossumba6ab842000-12-12 22:02:18 +00003042{
Victor Stinner742da042016-09-07 17:40:12 -07003043 Py_ssize_t i, j;
3044 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003045 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00003046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003047 /* Allocate the result tuple before checking the size. Believe it
3048 * or not, this allocation could trigger a garbage collection which
3049 * could empty the dict, so if we checked the size first and that
3050 * happened, the result would be an infinite loop (searching for an
3051 * entry that no longer exists). Note that the usual popitem()
3052 * idiom is "while d: k, v = d.popitem()". so needing to throw the
3053 * tuple away if the dict *is* empty isn't a significant
3054 * inefficiency -- possible, but unlikely in practice.
3055 */
3056 res = PyTuple_New(2);
3057 if (res == NULL)
3058 return NULL;
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003059 if (self->ma_used == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003060 Py_DECREF(res);
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003061 PyErr_SetString(PyExc_KeyError, "popitem(): dictionary is empty");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003062 return NULL;
3063 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003064 /* Convert split table to combined table */
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003065 if (self->ma_keys->dk_lookup == lookdict_split) {
3066 if (dictresize(self, DK_SIZE(self->ma_keys))) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003067 Py_DECREF(res);
3068 return NULL;
3069 }
3070 }
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003071 ENSURE_ALLOWS_DELETIONS(self);
Victor Stinner742da042016-09-07 17:40:12 -07003072
3073 /* Pop last item */
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003074 ep0 = DK_ENTRIES(self->ma_keys);
3075 i = self->ma_keys->dk_nentries - 1;
Victor Stinner742da042016-09-07 17:40:12 -07003076 while (i >= 0 && ep0[i].me_value == NULL) {
3077 i--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003078 }
Victor Stinner742da042016-09-07 17:40:12 -07003079 assert(i >= 0);
3080
3081 ep = &ep0[i];
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003082 j = lookdict_index(self->ma_keys, ep->me_hash, i);
Victor Stinner742da042016-09-07 17:40:12 -07003083 assert(j >= 0);
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003084 assert(dictkeys_get_index(self->ma_keys, j) == i);
3085 dictkeys_set_index(self->ma_keys, j, DKIX_DUMMY);
Victor Stinner742da042016-09-07 17:40:12 -07003086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003087 PyTuple_SET_ITEM(res, 0, ep->me_key);
3088 PyTuple_SET_ITEM(res, 1, ep->me_value);
Victor Stinner742da042016-09-07 17:40:12 -07003089 ep->me_key = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003090 ep->me_value = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07003091 /* We can't dk_usable++ since there is DKIX_DUMMY in indices */
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003092 self->ma_keys->dk_nentries = i;
3093 self->ma_used--;
3094 self->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner0fc91ee2019-04-12 21:51:34 +02003095 ASSERT_CONSISTENT(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003096 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00003097}
3098
Jeremy Hylton8caad492000-06-23 14:18:11 +00003099static int
3100dict_traverse(PyObject *op, visitproc visit, void *arg)
3101{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003102 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson55f44522016-09-05 12:12:59 -07003103 PyDictKeysObject *keys = mp->ma_keys;
Serhiy Storchaka46825d22016-09-26 21:29:34 +03003104 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Victor Stinner742da042016-09-07 17:40:12 -07003105 Py_ssize_t i, n = keys->dk_nentries;
3106
Benjamin Peterson55f44522016-09-05 12:12:59 -07003107 if (keys->dk_lookup == lookdict) {
3108 for (i = 0; i < n; i++) {
3109 if (entries[i].me_value != NULL) {
3110 Py_VISIT(entries[i].me_value);
3111 Py_VISIT(entries[i].me_key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003112 }
3113 }
Victor Stinner742da042016-09-07 17:40:12 -07003114 }
3115 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003116 if (mp->ma_values != NULL) {
Benjamin Peterson55f44522016-09-05 12:12:59 -07003117 for (i = 0; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003118 Py_VISIT(mp->ma_values[i]);
3119 }
3120 }
3121 else {
Benjamin Peterson55f44522016-09-05 12:12:59 -07003122 for (i = 0; i < n; i++) {
3123 Py_VISIT(entries[i].me_value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003124 }
3125 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003126 }
3127 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00003128}
3129
3130static int
3131dict_tp_clear(PyObject *op)
3132{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003133 PyDict_Clear(op);
3134 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00003135}
3136
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003137static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003138
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003139Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003140_PyDict_SizeOf(PyDictObject *mp)
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003141{
Victor Stinner742da042016-09-07 17:40:12 -07003142 Py_ssize_t size, usable, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003143
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003144 size = DK_SIZE(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07003145 usable = USABLE_FRACTION(size);
3146
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02003147 res = _PyObject_SIZE(Py_TYPE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003148 if (mp->ma_values)
Victor Stinner742da042016-09-07 17:40:12 -07003149 res += usable * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003150 /* If the dictionary is split, the keys portion is accounted-for
3151 in the type object. */
3152 if (mp->ma_keys->dk_refcnt == 1)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003153 res += (sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003154 + DK_IXSIZE(mp->ma_keys) * size
3155 + sizeof(PyDictKeyEntry) * usable);
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003156 return res;
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003157}
3158
3159Py_ssize_t
3160_PyDict_KeysSize(PyDictKeysObject *keys)
3161{
Victor Stinner98ee9d52016-09-08 09:33:56 -07003162 return (sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003163 + DK_IXSIZE(keys) * DK_SIZE(keys)
3164 + USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry));
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003165}
3166
doko@ubuntu.com17210f52016-01-14 14:04:59 +01003167static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303168dict_sizeof(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003169{
3170 return PyLong_FromSsize_t(_PyDict_SizeOf(mp));
3171}
3172
Brandt Buchereb8ac572020-02-24 19:47:34 -08003173static PyObject *
3174dict_or(PyObject *self, PyObject *other)
3175{
3176 if (!PyDict_Check(self) || !PyDict_Check(other)) {
3177 Py_RETURN_NOTIMPLEMENTED;
3178 }
3179 PyObject *new = PyDict_Copy(self);
3180 if (new == NULL) {
3181 return NULL;
3182 }
3183 if (dict_update_arg(new, other)) {
3184 Py_DECREF(new);
3185 return NULL;
3186 }
3187 return new;
3188}
3189
3190static PyObject *
3191dict_ior(PyObject *self, PyObject *other)
3192{
3193 if (dict_update_arg(self, other)) {
3194 return NULL;
3195 }
3196 Py_INCREF(self);
3197 return self;
3198}
3199
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00003200PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
3201
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003202PyDoc_STRVAR(sizeof__doc__,
3203"D.__sizeof__() -> size of D in memory, in bytes");
3204
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003205PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04003206"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
3207If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
3208If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
3209In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00003210
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003211PyDoc_STRVAR(clear__doc__,
3212"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003213
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003214PyDoc_STRVAR(copy__doc__,
3215"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00003216
Guido van Rossumb90c8482007-02-10 01:11:45 +00003217/* Forward */
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303218static PyObject *dictkeys_new(PyObject *, PyObject *);
3219static PyObject *dictitems_new(PyObject *, PyObject *);
3220static PyObject *dictvalues_new(PyObject *, PyObject *);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003221
Guido van Rossum45c85d12007-07-27 16:31:40 +00003222PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003223 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003224PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003225 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003226PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003227 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00003228
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003229static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07003230 DICT___CONTAINS___METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003231 {"__getitem__", (PyCFunction)(void(*)(void))dict_subscript, METH_O | METH_COEXIST,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003232 getitem__doc__},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003233 {"__sizeof__", (PyCFunction)(void(*)(void))dict_sizeof, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003234 sizeof__doc__},
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003235 DICT_GET_METHODDEF
3236 DICT_SETDEFAULT_METHODDEF
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003237 DICT_POP_METHODDEF
3238 DICT_POPITEM_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303239 {"keys", dictkeys_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003240 keys__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303241 {"items", dictitems_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003242 items__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303243 {"values", dictvalues_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003244 values__doc__},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003245 {"update", (PyCFunction)(void(*)(void))dict_update, METH_VARARGS | METH_KEYWORDS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003246 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08003247 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003248 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
3249 clear__doc__},
3250 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
3251 copy__doc__},
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003252 DICT___REVERSED___METHODDEF
Guido van Rossum48b069a2020-04-07 09:50:06 -07003253 {"__class_getitem__", (PyCFunction)Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003254 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003255};
3256
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00003257/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00003258int
3259PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003260{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003261 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07003262 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003263 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003264 PyObject *value;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003266 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003267 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003268 hash = PyObject_Hash(key);
3269 if (hash == -1)
3270 return -1;
3271 }
INADA Naoki778928b2017-08-03 23:45:15 +09003272 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003273 if (ix == DKIX_ERROR)
3274 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003275 return (ix != DKIX_EMPTY && value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003276}
3277
Thomas Wouterscf297e42007-02-23 15:07:44 +00003278/* Internal version of PyDict_Contains used when the hash value is already known */
3279int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003280_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00003281{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003282 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003283 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -07003284 Py_ssize_t ix;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003285
INADA Naoki778928b2017-08-03 23:45:15 +09003286 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003287 if (ix == DKIX_ERROR)
3288 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003289 return (ix != DKIX_EMPTY && value != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00003290}
3291
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003292/* Hack to implement "key in dict" */
3293static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003294 0, /* sq_length */
3295 0, /* sq_concat */
3296 0, /* sq_repeat */
3297 0, /* sq_item */
3298 0, /* sq_slice */
3299 0, /* sq_ass_item */
3300 0, /* sq_ass_slice */
3301 PyDict_Contains, /* sq_contains */
3302 0, /* sq_inplace_concat */
3303 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003304};
3305
Brandt Buchereb8ac572020-02-24 19:47:34 -08003306static PyNumberMethods dict_as_number = {
3307 .nb_or = dict_or,
3308 .nb_inplace_or = dict_ior,
3309};
3310
Guido van Rossum09e563a2001-05-01 12:10:21 +00003311static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003312dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3313{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003314 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003315 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003317 assert(type != NULL && type->tp_alloc != NULL);
3318 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02003319 if (self == NULL)
3320 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003321 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003322
Victor Stinnera9f61a52013-07-16 22:17:26 +02003323 /* The object has been implicitly tracked by tp_alloc */
3324 if (type == &PyDict_Type)
3325 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003326
3327 d->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003328 d->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07003329 d->ma_keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003330 if (d->ma_keys == NULL) {
3331 Py_DECREF(self);
3332 return NULL;
3333 }
Victor Stinner0fc91ee2019-04-12 21:51:34 +02003334 ASSERT_CONSISTENT(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003335 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003336}
3337
Tim Peters25786c02001-09-02 08:22:48 +00003338static int
3339dict_init(PyObject *self, PyObject *args, PyObject *kwds)
3340{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003341 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00003342}
3343
Tim Peters6d6c1a32001-08-02 04:15:00 +00003344static PyObject *
Dong-hee Nae27916b2020-04-02 09:55:43 +09003345dict_vectorcall(PyObject *type, PyObject * const*args,
3346 size_t nargsf, PyObject *kwnames)
3347{
3348 assert(PyType_Check(type));
3349 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
3350 if (!_PyArg_CheckPositional("dict", nargs, 0, 1)) {
3351 return NULL;
3352 }
3353
3354 PyObject *self = dict_new((PyTypeObject *)type, NULL, NULL);
3355 if (self == NULL) {
3356 return NULL;
3357 }
3358 if (nargs == 1) {
3359 if (dict_update_arg(self, args[0]) < 0) {
3360 Py_DECREF(self);
3361 return NULL;
3362 }
3363 args++;
3364 }
3365 if (kwnames != NULL) {
3366 for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(kwnames); i++) {
3367 if (PyDict_SetItem(self, PyTuple_GET_ITEM(kwnames, i), args[i]) < 0) {
3368 Py_DECREF(self);
3369 return NULL;
3370 }
3371 }
3372 }
3373 return self;
3374}
3375
3376static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003377dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00003378{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003379 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003380}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003381
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003382PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00003383"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00003384"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003385" (key, value) pairs\n"
3386"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00003387" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003388" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00003389" d[k] = v\n"
3390"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
3391" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00003392
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003393PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003394 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3395 "dict",
3396 sizeof(PyDictObject),
3397 0,
3398 (destructor)dict_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003399 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003400 0, /* tp_getattr */
3401 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003402 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003403 (reprfunc)dict_repr, /* tp_repr */
Brandt Buchereb8ac572020-02-24 19:47:34 -08003404 &dict_as_number, /* tp_as_number */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003405 &dict_as_sequence, /* tp_as_sequence */
3406 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003407 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003408 0, /* tp_call */
3409 0, /* tp_str */
3410 PyObject_GenericGetAttr, /* tp_getattro */
3411 0, /* tp_setattro */
3412 0, /* tp_as_buffer */
3413 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3414 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
3415 dictionary_doc, /* tp_doc */
3416 dict_traverse, /* tp_traverse */
3417 dict_tp_clear, /* tp_clear */
3418 dict_richcompare, /* tp_richcompare */
3419 0, /* tp_weaklistoffset */
3420 (getiterfunc)dict_iter, /* tp_iter */
3421 0, /* tp_iternext */
3422 mapp_methods, /* tp_methods */
3423 0, /* tp_members */
3424 0, /* tp_getset */
3425 0, /* tp_base */
3426 0, /* tp_dict */
3427 0, /* tp_descr_get */
3428 0, /* tp_descr_set */
3429 0, /* tp_dictoffset */
3430 dict_init, /* tp_init */
3431 PyType_GenericAlloc, /* tp_alloc */
3432 dict_new, /* tp_new */
3433 PyObject_GC_Del, /* tp_free */
Dong-hee Nae27916b2020-04-02 09:55:43 +09003434 .tp_vectorcall = dict_vectorcall,
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003435};
3436
Victor Stinner3c1e4812012-03-26 22:10:51 +02003437PyObject *
3438_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
3439{
3440 PyObject *kv;
3441 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02003442 if (kv == NULL) {
3443 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003444 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02003445 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02003446 return PyDict_GetItem(dp, kv);
3447}
3448
Guido van Rossum3cca2451997-05-16 14:23:33 +00003449/* For backward compatibility with old dictionary interface */
3450
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003451PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003452PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003453{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003454 PyObject *kv, *rv;
3455 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003456 if (kv == NULL) {
3457 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003458 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003459 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003460 rv = PyDict_GetItem(v, kv);
3461 Py_DECREF(kv);
3462 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003463}
3464
3465int
Victor Stinner3c1e4812012-03-26 22:10:51 +02003466_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
3467{
3468 PyObject *kv;
3469 kv = _PyUnicode_FromId(key); /* borrowed */
3470 if (kv == NULL)
3471 return -1;
3472 return PyDict_SetItem(v, kv, item);
3473}
3474
3475int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003476PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003477{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003478 PyObject *kv;
3479 int err;
3480 kv = PyUnicode_FromString(key);
3481 if (kv == NULL)
3482 return -1;
3483 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
3484 err = PyDict_SetItem(v, kv, item);
3485 Py_DECREF(kv);
3486 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003487}
3488
3489int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01003490_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
3491{
3492 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
3493 if (kv == NULL)
3494 return -1;
3495 return PyDict_DelItem(v, kv);
3496}
3497
3498int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003499PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003500{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003501 PyObject *kv;
3502 int err;
3503 kv = PyUnicode_FromString(key);
3504 if (kv == NULL)
3505 return -1;
3506 err = PyDict_DelItem(v, kv);
3507 Py_DECREF(kv);
3508 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003509}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003510
Raymond Hettinger019a1482004-03-18 02:41:19 +00003511/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003512
3513typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003514 PyObject_HEAD
3515 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
3516 Py_ssize_t di_used;
3517 Py_ssize_t di_pos;
3518 PyObject* di_result; /* reusable result tuple for iteritems */
3519 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003520} dictiterobject;
3521
3522static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003523dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003524{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003525 dictiterobject *di;
3526 di = PyObject_GC_New(dictiterobject, itertype);
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003527 if (di == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003528 return NULL;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003529 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003530 Py_INCREF(dict);
3531 di->di_dict = dict;
3532 di->di_used = dict->ma_used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003533 di->len = dict->ma_used;
Dong-hee Na24dc2f82019-10-20 05:01:08 +09003534 if (itertype == &PyDictRevIterKey_Type ||
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003535 itertype == &PyDictRevIterItem_Type ||
Dong-hee Na24dc2f82019-10-20 05:01:08 +09003536 itertype == &PyDictRevIterValue_Type) {
3537 if (dict->ma_values) {
3538 di->di_pos = dict->ma_used - 1;
3539 }
3540 else {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003541 di->di_pos = dict->ma_keys->dk_nentries - 1;
Dong-hee Na24dc2f82019-10-20 05:01:08 +09003542 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003543 }
3544 else {
3545 di->di_pos = 0;
3546 }
3547 if (itertype == &PyDictIterItem_Type ||
3548 itertype == &PyDictRevIterItem_Type) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003549 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
3550 if (di->di_result == NULL) {
3551 Py_DECREF(di);
3552 return NULL;
3553 }
3554 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003555 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003556 di->di_result = NULL;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003557 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003558 _PyObject_GC_TRACK(di);
3559 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003560}
3561
3562static void
3563dictiter_dealloc(dictiterobject *di)
3564{
INADA Naokia6296d32017-08-24 14:55:17 +09003565 /* bpo-31095: UnTrack is needed before calling any callbacks */
3566 _PyObject_GC_UNTRACK(di);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003567 Py_XDECREF(di->di_dict);
3568 Py_XDECREF(di->di_result);
3569 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003570}
3571
3572static int
3573dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
3574{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003575 Py_VISIT(di->di_dict);
3576 Py_VISIT(di->di_result);
3577 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003578}
3579
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003580static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303581dictiter_len(dictiterobject *di, PyObject *Py_UNUSED(ignored))
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003582{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003583 Py_ssize_t len = 0;
3584 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
3585 len = di->len;
3586 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003587}
3588
Guido van Rossumb90c8482007-02-10 01:11:45 +00003589PyDoc_STRVAR(length_hint_doc,
3590 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003591
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003592static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303593dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003594
3595PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3596
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003597static PyMethodDef dictiter_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003598 {"__length_hint__", (PyCFunction)(void(*)(void))dictiter_len, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003599 length_hint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003600 {"__reduce__", (PyCFunction)(void(*)(void))dictiter_reduce, METH_NOARGS,
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003601 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003602 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003603};
3604
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003605static PyObject*
3606dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00003607{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003608 PyObject *key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003609 Py_ssize_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003610 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003611 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00003612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003613 if (d == NULL)
3614 return NULL;
3615 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00003616
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003617 if (di->di_used != d->ma_used) {
3618 PyErr_SetString(PyExc_RuntimeError,
3619 "dictionary changed size during iteration");
3620 di->di_used = -1; /* Make this state sticky */
3621 return NULL;
3622 }
Guido van Rossum2147df72002-07-16 20:30:22 +00003623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003624 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003625 k = d->ma_keys;
INADA Naokica2d8be2016-11-04 16:59:10 +09003626 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003627 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003628 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003629 goto fail;
3630 key = DK_ENTRIES(k)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003631 assert(d->ma_values[i] != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003632 }
3633 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003634 Py_ssize_t n = k->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003635 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
3636 while (i < n && entry_ptr->me_value == NULL) {
3637 entry_ptr++;
3638 i++;
3639 }
3640 if (i >= n)
3641 goto fail;
3642 key = entry_ptr->me_key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003643 }
Thomas Perl796cc6e2019-03-28 07:03:25 +01003644 // We found an element (key), but did not expect it
3645 if (di->len == 0) {
3646 PyErr_SetString(PyExc_RuntimeError,
3647 "dictionary keys changed during iteration");
3648 goto fail;
3649 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003650 di->di_pos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003651 di->len--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003652 Py_INCREF(key);
3653 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003654
3655fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003656 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003657 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003658 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003659}
3660
Raymond Hettinger019a1482004-03-18 02:41:19 +00003661PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003662 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3663 "dict_keyiterator", /* tp_name */
3664 sizeof(dictiterobject), /* tp_basicsize */
3665 0, /* tp_itemsize */
3666 /* methods */
3667 (destructor)dictiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003668 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003669 0, /* tp_getattr */
3670 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003671 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003672 0, /* tp_repr */
3673 0, /* tp_as_number */
3674 0, /* tp_as_sequence */
3675 0, /* tp_as_mapping */
3676 0, /* tp_hash */
3677 0, /* tp_call */
3678 0, /* tp_str */
3679 PyObject_GenericGetAttr, /* tp_getattro */
3680 0, /* tp_setattro */
3681 0, /* tp_as_buffer */
3682 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3683 0, /* tp_doc */
3684 (traverseproc)dictiter_traverse, /* tp_traverse */
3685 0, /* tp_clear */
3686 0, /* tp_richcompare */
3687 0, /* tp_weaklistoffset */
3688 PyObject_SelfIter, /* tp_iter */
3689 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
3690 dictiter_methods, /* tp_methods */
3691 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003692};
3693
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003694static PyObject *
3695dictiter_iternextvalue(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003696{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003697 PyObject *value;
INADA Naokica2d8be2016-11-04 16:59:10 +09003698 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003699 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003701 if (d == NULL)
3702 return NULL;
3703 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003705 if (di->di_used != d->ma_used) {
3706 PyErr_SetString(PyExc_RuntimeError,
3707 "dictionary changed size during iteration");
3708 di->di_used = -1; /* Make this state sticky */
3709 return NULL;
3710 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003712 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003713 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003714 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003715 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003716 goto fail;
INADA Naokica2d8be2016-11-04 16:59:10 +09003717 value = d->ma_values[i];
3718 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003719 }
3720 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003721 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003722 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3723 while (i < n && entry_ptr->me_value == NULL) {
3724 entry_ptr++;
3725 i++;
3726 }
3727 if (i >= n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003728 goto fail;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003729 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003730 }
Thomas Perlb8311cf2019-04-02 11:30:10 +02003731 // We found an element, but did not expect it
3732 if (di->len == 0) {
3733 PyErr_SetString(PyExc_RuntimeError,
3734 "dictionary keys changed during iteration");
3735 goto fail;
3736 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003737 di->di_pos = i+1;
3738 di->len--;
3739 Py_INCREF(value);
3740 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003741
3742fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003743 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003744 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003745 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003746}
3747
3748PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003749 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3750 "dict_valueiterator", /* tp_name */
3751 sizeof(dictiterobject), /* tp_basicsize */
3752 0, /* tp_itemsize */
3753 /* methods */
3754 (destructor)dictiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003755 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003756 0, /* tp_getattr */
3757 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003758 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003759 0, /* tp_repr */
3760 0, /* tp_as_number */
3761 0, /* tp_as_sequence */
3762 0, /* tp_as_mapping */
3763 0, /* tp_hash */
3764 0, /* tp_call */
3765 0, /* tp_str */
3766 PyObject_GenericGetAttr, /* tp_getattro */
3767 0, /* tp_setattro */
3768 0, /* tp_as_buffer */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003769 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003770 0, /* tp_doc */
3771 (traverseproc)dictiter_traverse, /* tp_traverse */
3772 0, /* tp_clear */
3773 0, /* tp_richcompare */
3774 0, /* tp_weaklistoffset */
3775 PyObject_SelfIter, /* tp_iter */
3776 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3777 dictiter_methods, /* tp_methods */
3778 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003779};
3780
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003781static PyObject *
3782dictiter_iternextitem(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003783{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003784 PyObject *key, *value, *result;
INADA Naokica2d8be2016-11-04 16:59:10 +09003785 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003786 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003788 if (d == NULL)
3789 return NULL;
3790 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003792 if (di->di_used != d->ma_used) {
3793 PyErr_SetString(PyExc_RuntimeError,
3794 "dictionary changed size during iteration");
3795 di->di_used = -1; /* Make this state sticky */
3796 return NULL;
3797 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003799 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003800 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003801 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003802 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003803 goto fail;
3804 key = DK_ENTRIES(d->ma_keys)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003805 value = d->ma_values[i];
3806 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003807 }
3808 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003809 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003810 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3811 while (i < n && entry_ptr->me_value == NULL) {
3812 entry_ptr++;
3813 i++;
3814 }
3815 if (i >= n)
3816 goto fail;
3817 key = entry_ptr->me_key;
3818 value = entry_ptr->me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003819 }
Thomas Perlb8311cf2019-04-02 11:30:10 +02003820 // We found an element, but did not expect it
3821 if (di->len == 0) {
3822 PyErr_SetString(PyExc_RuntimeError,
3823 "dictionary keys changed during iteration");
3824 goto fail;
3825 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003826 di->di_pos = i+1;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003827 di->len--;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003828 Py_INCREF(key);
3829 Py_INCREF(value);
3830 result = di->di_result;
3831 if (Py_REFCNT(result) == 1) {
3832 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3833 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3834 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3835 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003836 Py_INCREF(result);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003837 Py_DECREF(oldkey);
3838 Py_DECREF(oldvalue);
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003839 }
3840 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003841 result = PyTuple_New(2);
3842 if (result == NULL)
3843 return NULL;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003844 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3845 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003846 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003847 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003848
3849fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003850 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003851 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003852 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003853}
3854
3855PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003856 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3857 "dict_itemiterator", /* tp_name */
3858 sizeof(dictiterobject), /* tp_basicsize */
3859 0, /* tp_itemsize */
3860 /* methods */
3861 (destructor)dictiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003862 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003863 0, /* tp_getattr */
3864 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003865 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003866 0, /* tp_repr */
3867 0, /* tp_as_number */
3868 0, /* tp_as_sequence */
3869 0, /* tp_as_mapping */
3870 0, /* tp_hash */
3871 0, /* tp_call */
3872 0, /* tp_str */
3873 PyObject_GenericGetAttr, /* tp_getattro */
3874 0, /* tp_setattro */
3875 0, /* tp_as_buffer */
3876 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3877 0, /* tp_doc */
3878 (traverseproc)dictiter_traverse, /* tp_traverse */
3879 0, /* tp_clear */
3880 0, /* tp_richcompare */
3881 0, /* tp_weaklistoffset */
3882 PyObject_SelfIter, /* tp_iter */
3883 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3884 dictiter_methods, /* tp_methods */
3885 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003886};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003887
3888
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003889/* dictreviter */
3890
3891static PyObject *
3892dictreviter_iternext(dictiterobject *di)
3893{
3894 PyDictObject *d = di->di_dict;
3895
3896 if (d == NULL) {
3897 return NULL;
3898 }
3899 assert (PyDict_Check(d));
3900
3901 if (di->di_used != d->ma_used) {
3902 PyErr_SetString(PyExc_RuntimeError,
3903 "dictionary changed size during iteration");
3904 di->di_used = -1; /* Make this state sticky */
3905 return NULL;
3906 }
3907
3908 Py_ssize_t i = di->di_pos;
3909 PyDictKeysObject *k = d->ma_keys;
3910 PyObject *key, *value, *result;
3911
Serhiy Storchaka2e3d8732019-10-23 14:48:08 +03003912 if (i < 0) {
3913 goto fail;
3914 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003915 if (d->ma_values) {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003916 key = DK_ENTRIES(k)[i].me_key;
3917 value = d->ma_values[i];
3918 assert (value != NULL);
3919 }
3920 else {
3921 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
Serhiy Storchaka2e3d8732019-10-23 14:48:08 +03003922 while (entry_ptr->me_value == NULL) {
3923 if (--i < 0) {
3924 goto fail;
3925 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003926 entry_ptr--;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003927 }
3928 key = entry_ptr->me_key;
3929 value = entry_ptr->me_value;
3930 }
3931 di->di_pos = i-1;
3932 di->len--;
3933
Dong-hee Na1b55b652020-02-17 19:09:15 +09003934 if (Py_IS_TYPE(di, &PyDictRevIterKey_Type)) {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003935 Py_INCREF(key);
3936 return key;
3937 }
Dong-hee Na1b55b652020-02-17 19:09:15 +09003938 else if (Py_IS_TYPE(di, &PyDictRevIterValue_Type)) {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003939 Py_INCREF(value);
3940 return value;
3941 }
Dong-hee Na1b55b652020-02-17 19:09:15 +09003942 else if (Py_IS_TYPE(di, &PyDictRevIterItem_Type)) {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003943 Py_INCREF(key);
3944 Py_INCREF(value);
3945 result = di->di_result;
3946 if (Py_REFCNT(result) == 1) {
3947 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3948 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3949 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3950 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
3951 Py_INCREF(result);
3952 Py_DECREF(oldkey);
3953 Py_DECREF(oldvalue);
3954 }
3955 else {
3956 result = PyTuple_New(2);
3957 if (result == NULL) {
3958 return NULL;
3959 }
3960 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3961 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
3962 }
3963 return result;
3964 }
3965 else {
3966 Py_UNREACHABLE();
3967 }
3968
3969fail:
3970 di->di_dict = NULL;
3971 Py_DECREF(d);
3972 return NULL;
3973}
3974
3975PyTypeObject PyDictRevIterKey_Type = {
3976 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3977 "dict_reversekeyiterator",
3978 sizeof(dictiterobject),
3979 .tp_dealloc = (destructor)dictiter_dealloc,
3980 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
3981 .tp_traverse = (traverseproc)dictiter_traverse,
3982 .tp_iter = PyObject_SelfIter,
3983 .tp_iternext = (iternextfunc)dictreviter_iternext,
3984 .tp_methods = dictiter_methods
3985};
3986
3987
3988/*[clinic input]
3989dict.__reversed__
3990
3991Return a reverse iterator over the dict keys.
3992[clinic start generated code]*/
3993
3994static PyObject *
3995dict___reversed___impl(PyDictObject *self)
3996/*[clinic end generated code: output=e674483336d1ed51 input=23210ef3477d8c4d]*/
3997{
3998 assert (PyDict_Check(self));
3999 return dictiter_new(self, &PyDictRevIterKey_Type);
4000}
4001
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004002static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304003dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004004{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004005 _Py_IDENTIFIER(iter);
Sergey Fedoseev63958442018-10-20 05:43:33 +05004006 /* copy the iterator state */
4007 dictiterobject tmp = *di;
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004008 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004009
Sergey Fedoseev63958442018-10-20 05:43:33 +05004010 PyObject *list = PySequence_List((PyObject*)&tmp);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004011 Py_XDECREF(tmp.di_dict);
Sergey Fedoseev63958442018-10-20 05:43:33 +05004012 if (list == NULL) {
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004013 return NULL;
4014 }
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004015 return Py_BuildValue("N(N)", _PyEval_GetBuiltinId(&PyId_iter), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004016}
4017
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004018PyTypeObject PyDictRevIterItem_Type = {
4019 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4020 "dict_reverseitemiterator",
4021 sizeof(dictiterobject),
4022 .tp_dealloc = (destructor)dictiter_dealloc,
4023 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
4024 .tp_traverse = (traverseproc)dictiter_traverse,
4025 .tp_iter = PyObject_SelfIter,
4026 .tp_iternext = (iternextfunc)dictreviter_iternext,
4027 .tp_methods = dictiter_methods
4028};
4029
4030PyTypeObject PyDictRevIterValue_Type = {
4031 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4032 "dict_reversevalueiterator",
4033 sizeof(dictiterobject),
4034 .tp_dealloc = (destructor)dictiter_dealloc,
4035 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
4036 .tp_traverse = (traverseproc)dictiter_traverse,
4037 .tp_iter = PyObject_SelfIter,
4038 .tp_iternext = (iternextfunc)dictreviter_iternext,
4039 .tp_methods = dictiter_methods
4040};
4041
Guido van Rossum3ac67412007-02-10 18:55:06 +00004042/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004043/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00004044/***********************************************/
4045
Guido van Rossumb90c8482007-02-10 01:11:45 +00004046/* The instance lay-out is the same for all three; but the type differs. */
4047
Guido van Rossumb90c8482007-02-10 01:11:45 +00004048static void
Eric Snow96c6af92015-05-29 22:21:39 -06004049dictview_dealloc(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004050{
INADA Naokia6296d32017-08-24 14:55:17 +09004051 /* bpo-31095: UnTrack is needed before calling any callbacks */
4052 _PyObject_GC_UNTRACK(dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004053 Py_XDECREF(dv->dv_dict);
4054 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00004055}
4056
4057static int
Eric Snow96c6af92015-05-29 22:21:39 -06004058dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
Antoine Pitrou7ddda782009-01-01 15:35:33 +00004059{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004060 Py_VISIT(dv->dv_dict);
4061 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004062}
4063
Guido van Rossum83825ac2007-02-10 04:54:19 +00004064static Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06004065dictview_len(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004066{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004067 Py_ssize_t len = 0;
4068 if (dv->dv_dict != NULL)
4069 len = dv->dv_dict->ma_used;
4070 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004071}
4072
Eric Snow96c6af92015-05-29 22:21:39 -06004073PyObject *
4074_PyDictView_New(PyObject *dict, PyTypeObject *type)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004075{
Eric Snow96c6af92015-05-29 22:21:39 -06004076 _PyDictViewObject *dv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004077 if (dict == NULL) {
4078 PyErr_BadInternalCall();
4079 return NULL;
4080 }
4081 if (!PyDict_Check(dict)) {
4082 /* XXX Get rid of this restriction later */
4083 PyErr_Format(PyExc_TypeError,
4084 "%s() requires a dict argument, not '%s'",
Victor Stinner58ac7002020-02-07 03:04:21 +01004085 type->tp_name, Py_TYPE(dict)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004086 return NULL;
4087 }
Eric Snow96c6af92015-05-29 22:21:39 -06004088 dv = PyObject_GC_New(_PyDictViewObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004089 if (dv == NULL)
4090 return NULL;
4091 Py_INCREF(dict);
4092 dv->dv_dict = (PyDictObject *)dict;
4093 _PyObject_GC_TRACK(dv);
4094 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004095}
4096
Neal Norwitze36f2ba2007-02-26 23:12:28 +00004097/* TODO(guido): The views objects are not complete:
4098
4099 * support more set operations
4100 * support arbitrary mappings?
4101 - either these should be static or exported in dictobject.h
4102 - if public then they should probably be in builtins
4103*/
4104
Guido van Rossumaac530c2007-08-24 22:33:45 +00004105/* Return 1 if self is a subset of other, iterating over self;
4106 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00004107static int
4108all_contained_in(PyObject *self, PyObject *other)
4109{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004110 PyObject *iter = PyObject_GetIter(self);
4111 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00004112
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004113 if (iter == NULL)
4114 return -1;
4115 for (;;) {
4116 PyObject *next = PyIter_Next(iter);
4117 if (next == NULL) {
4118 if (PyErr_Occurred())
4119 ok = -1;
4120 break;
4121 }
4122 ok = PySequence_Contains(other, next);
4123 Py_DECREF(next);
4124 if (ok <= 0)
4125 break;
4126 }
4127 Py_DECREF(iter);
4128 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00004129}
4130
4131static PyObject *
4132dictview_richcompare(PyObject *self, PyObject *other, int op)
4133{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004134 Py_ssize_t len_self, len_other;
4135 int ok;
4136 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004138 assert(self != NULL);
4139 assert(PyDictViewSet_Check(self));
4140 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00004141
Brian Curtindfc80e32011-08-10 20:28:54 -05004142 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
4143 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004145 len_self = PyObject_Size(self);
4146 if (len_self < 0)
4147 return NULL;
4148 len_other = PyObject_Size(other);
4149 if (len_other < 0)
4150 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004152 ok = 0;
4153 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00004154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004155 case Py_NE:
4156 case Py_EQ:
4157 if (len_self == len_other)
4158 ok = all_contained_in(self, other);
4159 if (op == Py_NE && ok >= 0)
4160 ok = !ok;
4161 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004162
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004163 case Py_LT:
4164 if (len_self < len_other)
4165 ok = all_contained_in(self, other);
4166 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004168 case Py_LE:
4169 if (len_self <= len_other)
4170 ok = all_contained_in(self, other);
4171 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004173 case Py_GT:
4174 if (len_self > len_other)
4175 ok = all_contained_in(other, self);
4176 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004178 case Py_GE:
4179 if (len_self >= len_other)
4180 ok = all_contained_in(other, self);
4181 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004182
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004183 }
4184 if (ok < 0)
4185 return NULL;
4186 result = ok ? Py_True : Py_False;
4187 Py_INCREF(result);
4188 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00004189}
4190
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004191static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004192dictview_repr(_PyDictViewObject *dv)
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004193{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004194 PyObject *seq;
bennorthd7773d92018-01-26 15:46:01 +00004195 PyObject *result = NULL;
4196 Py_ssize_t rc;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004197
bennorthd7773d92018-01-26 15:46:01 +00004198 rc = Py_ReprEnter((PyObject *)dv);
4199 if (rc != 0) {
4200 return rc > 0 ? PyUnicode_FromString("...") : NULL;
4201 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004202 seq = PySequence_List((PyObject *)dv);
bennorthd7773d92018-01-26 15:46:01 +00004203 if (seq == NULL) {
4204 goto Done;
4205 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004206 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
4207 Py_DECREF(seq);
bennorthd7773d92018-01-26 15:46:01 +00004208
4209Done:
4210 Py_ReprLeave((PyObject *)dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004211 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004212}
4213
Guido van Rossum3ac67412007-02-10 18:55:06 +00004214/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004215
4216static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004217dictkeys_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004219 if (dv->dv_dict == NULL) {
4220 Py_RETURN_NONE;
4221 }
4222 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004223}
4224
4225static int
Eric Snow96c6af92015-05-29 22:21:39 -06004226dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004227{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004228 if (dv->dv_dict == NULL)
4229 return 0;
4230 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004231}
4232
Guido van Rossum83825ac2007-02-10 04:54:19 +00004233static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004234 (lenfunc)dictview_len, /* sq_length */
4235 0, /* sq_concat */
4236 0, /* sq_repeat */
4237 0, /* sq_item */
4238 0, /* sq_slice */
4239 0, /* sq_ass_item */
4240 0, /* sq_ass_slice */
4241 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004242};
4243
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004244// Create an set object from dictviews object.
4245// Returns a new reference.
4246// This utility function is used by set operations.
Guido van Rossum523259b2007-08-24 23:41:22 +00004247static PyObject*
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004248dictviews_to_set(PyObject *self)
Guido van Rossum523259b2007-08-24 23:41:22 +00004249{
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004250 PyObject *left = self;
4251 if (PyDictKeys_Check(self)) {
4252 // PySet_New() has fast path for the dict object.
4253 PyObject *dict = (PyObject *)((_PyDictViewObject *)self)->dv_dict;
4254 if (PyDict_CheckExact(dict)) {
4255 left = dict;
4256 }
4257 }
4258 return PySet_New(left);
4259}
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004260
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004261static PyObject*
4262dictviews_sub(PyObject *self, PyObject *other)
4263{
4264 PyObject *result = dictviews_to_set(self);
4265 if (result == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004266 return NULL;
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004267 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004268
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004269 _Py_IDENTIFIER(difference_update);
4270 PyObject *tmp = _PyObject_CallMethodIdOneArg(
4271 result, &PyId_difference_update, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004272 if (tmp == NULL) {
4273 Py_DECREF(result);
4274 return NULL;
4275 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004277 Py_DECREF(tmp);
4278 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004279}
4280
Forest Gregg998cf1f2019-08-26 02:17:43 -05004281static int
4282dictitems_contains(_PyDictViewObject *dv, PyObject *obj);
4283
4284PyObject *
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004285_PyDictView_Intersect(PyObject* self, PyObject *other)
Guido van Rossum523259b2007-08-24 23:41:22 +00004286{
Forest Gregg998cf1f2019-08-26 02:17:43 -05004287 PyObject *result;
4288 PyObject *it;
4289 PyObject *key;
4290 Py_ssize_t len_self;
4291 int rv;
4292 int (*dict_contains)(_PyDictViewObject *, PyObject *);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004293
Forest Gregg998cf1f2019-08-26 02:17:43 -05004294 /* Python interpreter swaps parameters when dict view
4295 is on right side of & */
4296 if (!PyDictViewSet_Check(self)) {
4297 PyObject *tmp = other;
4298 other = self;
4299 self = tmp;
4300 }
4301
4302 len_self = dictview_len((_PyDictViewObject *)self);
4303
4304 /* if other is a set and self is smaller than other,
4305 reuse set intersection logic */
Dong-hee Na1b55b652020-02-17 19:09:15 +09004306 if (Py_IS_TYPE(other, &PySet_Type) && len_self <= PyObject_Size(other)) {
Forest Gregg998cf1f2019-08-26 02:17:43 -05004307 _Py_IDENTIFIER(intersection);
4308 return _PyObject_CallMethodIdObjArgs(other, &PyId_intersection, self, NULL);
4309 }
4310
4311 /* if other is another dict view, and it is bigger than self,
4312 swap them */
4313 if (PyDictViewSet_Check(other)) {
4314 Py_ssize_t len_other = dictview_len((_PyDictViewObject *)other);
4315 if (len_other > len_self) {
4316 PyObject *tmp = other;
4317 other = self;
4318 self = tmp;
4319 }
4320 }
4321
4322 /* at this point, two things should be true
4323 1. self is a dictview
4324 2. if other is a dictview then it is smaller than self */
4325 result = PySet_New(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004326 if (result == NULL)
4327 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004328
Forest Gregg998cf1f2019-08-26 02:17:43 -05004329 it = PyObject_GetIter(other);
Zackery Spytzb16e3822019-10-13 05:49:05 -06004330 if (it == NULL) {
4331 Py_DECREF(result);
4332 return NULL;
4333 }
Forest Gregg998cf1f2019-08-26 02:17:43 -05004334
Forest Gregg998cf1f2019-08-26 02:17:43 -05004335 if (PyDictKeys_Check(self)) {
4336 dict_contains = dictkeys_contains;
4337 }
4338 /* else PyDictItems_Check(self) */
4339 else {
4340 dict_contains = dictitems_contains;
4341 }
4342
4343 while ((key = PyIter_Next(it)) != NULL) {
4344 rv = dict_contains((_PyDictViewObject *)self, key);
4345 if (rv < 0) {
4346 goto error;
4347 }
4348 if (rv) {
4349 if (PySet_Add(result, key)) {
4350 goto error;
4351 }
4352 }
4353 Py_DECREF(key);
4354 }
4355 Py_DECREF(it);
4356 if (PyErr_Occurred()) {
4357 Py_DECREF(result);
4358 return NULL;
4359 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004360 return result;
Forest Gregg998cf1f2019-08-26 02:17:43 -05004361
4362error:
4363 Py_DECREF(it);
4364 Py_DECREF(result);
4365 Py_DECREF(key);
4366 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004367}
4368
4369static PyObject*
4370dictviews_or(PyObject* self, PyObject *other)
4371{
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004372 PyObject *result = dictviews_to_set(self);
4373 if (result == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004374 return NULL;
4375 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004376
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004377 if (_PySet_Update(result, other) < 0) {
4378 Py_DECREF(result);
4379 return NULL;
4380 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004381 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004382}
4383
4384static PyObject*
4385dictviews_xor(PyObject* self, PyObject *other)
4386{
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004387 PyObject *result = dictviews_to_set(self);
4388 if (result == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004389 return NULL;
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004390 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004391
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004392 _Py_IDENTIFIER(symmetric_difference_update);
4393 PyObject *tmp = _PyObject_CallMethodIdOneArg(
4394 result, &PyId_symmetric_difference_update, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004395 if (tmp == NULL) {
4396 Py_DECREF(result);
4397 return NULL;
4398 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004400 Py_DECREF(tmp);
4401 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004402}
4403
4404static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004405 0, /*nb_add*/
4406 (binaryfunc)dictviews_sub, /*nb_subtract*/
4407 0, /*nb_multiply*/
4408 0, /*nb_remainder*/
4409 0, /*nb_divmod*/
4410 0, /*nb_power*/
4411 0, /*nb_negative*/
4412 0, /*nb_positive*/
4413 0, /*nb_absolute*/
4414 0, /*nb_bool*/
4415 0, /*nb_invert*/
4416 0, /*nb_lshift*/
4417 0, /*nb_rshift*/
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004418 (binaryfunc)_PyDictView_Intersect, /*nb_and*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004419 (binaryfunc)dictviews_xor, /*nb_xor*/
4420 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00004421};
4422
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004423static PyObject*
4424dictviews_isdisjoint(PyObject *self, PyObject *other)
4425{
4426 PyObject *it;
4427 PyObject *item = NULL;
4428
4429 if (self == other) {
Eric Snow96c6af92015-05-29 22:21:39 -06004430 if (dictview_len((_PyDictViewObject *)self) == 0)
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004431 Py_RETURN_TRUE;
4432 else
4433 Py_RETURN_FALSE;
4434 }
4435
4436 /* Iterate over the shorter object (only if other is a set,
4437 * because PySequence_Contains may be expensive otherwise): */
4438 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
Eric Snow96c6af92015-05-29 22:21:39 -06004439 Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self);
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004440 Py_ssize_t len_other = PyObject_Size(other);
4441 if (len_other == -1)
4442 return NULL;
4443
4444 if ((len_other > len_self)) {
4445 PyObject *tmp = other;
4446 other = self;
4447 self = tmp;
4448 }
4449 }
4450
4451 it = PyObject_GetIter(other);
4452 if (it == NULL)
4453 return NULL;
4454
4455 while ((item = PyIter_Next(it)) != NULL) {
4456 int contains = PySequence_Contains(self, item);
4457 Py_DECREF(item);
4458 if (contains == -1) {
4459 Py_DECREF(it);
4460 return NULL;
4461 }
4462
4463 if (contains) {
4464 Py_DECREF(it);
4465 Py_RETURN_FALSE;
4466 }
4467 }
4468 Py_DECREF(it);
4469 if (PyErr_Occurred())
4470 return NULL; /* PyIter_Next raised an exception. */
4471 Py_RETURN_TRUE;
4472}
4473
4474PyDoc_STRVAR(isdisjoint_doc,
4475"Return True if the view and the given iterable have a null intersection.");
4476
Serhiy Storchaka81524022018-11-27 13:05:02 +02004477static PyObject* dictkeys_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored));
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004478
4479PyDoc_STRVAR(reversed_keys_doc,
4480"Return a reverse iterator over the dict keys.");
4481
Guido van Rossumb90c8482007-02-10 01:11:45 +00004482static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004483 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4484 isdisjoint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004485 {"__reversed__", (PyCFunction)(void(*)(void))dictkeys_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004486 reversed_keys_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004487 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004488};
4489
4490PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004491 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4492 "dict_keys", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004493 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004494 0, /* tp_itemsize */
4495 /* methods */
4496 (destructor)dictview_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004497 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004498 0, /* tp_getattr */
4499 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004500 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004501 (reprfunc)dictview_repr, /* tp_repr */
4502 &dictviews_as_number, /* tp_as_number */
4503 &dictkeys_as_sequence, /* tp_as_sequence */
4504 0, /* tp_as_mapping */
4505 0, /* tp_hash */
4506 0, /* tp_call */
4507 0, /* tp_str */
4508 PyObject_GenericGetAttr, /* tp_getattro */
4509 0, /* tp_setattro */
4510 0, /* tp_as_buffer */
4511 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4512 0, /* tp_doc */
4513 (traverseproc)dictview_traverse, /* tp_traverse */
4514 0, /* tp_clear */
4515 dictview_richcompare, /* tp_richcompare */
4516 0, /* tp_weaklistoffset */
4517 (getiterfunc)dictkeys_iter, /* tp_iter */
4518 0, /* tp_iternext */
4519 dictkeys_methods, /* tp_methods */
4520 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004521};
4522
4523static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304524dictkeys_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004525{
Eric Snow96c6af92015-05-29 22:21:39 -06004526 return _PyDictView_New(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004527}
4528
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004529static PyObject *
Serhiy Storchaka81524022018-11-27 13:05:02 +02004530dictkeys_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored))
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004531{
4532 if (dv->dv_dict == NULL) {
4533 Py_RETURN_NONE;
4534 }
4535 return dictiter_new(dv->dv_dict, &PyDictRevIterKey_Type);
4536}
4537
Guido van Rossum3ac67412007-02-10 18:55:06 +00004538/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004539
4540static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004541dictitems_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004542{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004543 if (dv->dv_dict == NULL) {
4544 Py_RETURN_NONE;
4545 }
4546 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004547}
4548
4549static int
Eric Snow96c6af92015-05-29 22:21:39 -06004550dictitems_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004551{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004552 int result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004553 PyObject *key, *value, *found;
4554 if (dv->dv_dict == NULL)
4555 return 0;
4556 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
4557 return 0;
4558 key = PyTuple_GET_ITEM(obj, 0);
4559 value = PyTuple_GET_ITEM(obj, 1);
Raymond Hettinger6692f012016-09-18 21:46:08 -07004560 found = PyDict_GetItemWithError((PyObject *)dv->dv_dict, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004561 if (found == NULL) {
4562 if (PyErr_Occurred())
4563 return -1;
4564 return 0;
4565 }
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004566 Py_INCREF(found);
Serhiy Storchaka18b711c2019-08-04 14:12:48 +03004567 result = PyObject_RichCompareBool(found, value, Py_EQ);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004568 Py_DECREF(found);
4569 return result;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004570}
4571
Guido van Rossum83825ac2007-02-10 04:54:19 +00004572static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004573 (lenfunc)dictview_len, /* sq_length */
4574 0, /* sq_concat */
4575 0, /* sq_repeat */
4576 0, /* sq_item */
4577 0, /* sq_slice */
4578 0, /* sq_ass_item */
4579 0, /* sq_ass_slice */
4580 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004581};
4582
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004583static PyObject* dictitems_reversed(_PyDictViewObject *dv);
4584
4585PyDoc_STRVAR(reversed_items_doc,
4586"Return a reverse iterator over the dict items.");
4587
Guido van Rossumb90c8482007-02-10 01:11:45 +00004588static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004589 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4590 isdisjoint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004591 {"__reversed__", (PyCFunction)(void(*)(void))dictitems_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004592 reversed_items_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004593 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004594};
4595
4596PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004597 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4598 "dict_items", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004599 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004600 0, /* tp_itemsize */
4601 /* methods */
4602 (destructor)dictview_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004603 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004604 0, /* tp_getattr */
4605 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004606 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004607 (reprfunc)dictview_repr, /* tp_repr */
4608 &dictviews_as_number, /* tp_as_number */
4609 &dictitems_as_sequence, /* tp_as_sequence */
4610 0, /* tp_as_mapping */
4611 0, /* tp_hash */
4612 0, /* tp_call */
4613 0, /* tp_str */
4614 PyObject_GenericGetAttr, /* tp_getattro */
4615 0, /* tp_setattro */
4616 0, /* tp_as_buffer */
4617 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4618 0, /* tp_doc */
4619 (traverseproc)dictview_traverse, /* tp_traverse */
4620 0, /* tp_clear */
4621 dictview_richcompare, /* tp_richcompare */
4622 0, /* tp_weaklistoffset */
4623 (getiterfunc)dictitems_iter, /* tp_iter */
4624 0, /* tp_iternext */
4625 dictitems_methods, /* tp_methods */
4626 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004627};
4628
4629static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304630dictitems_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004631{
Eric Snow96c6af92015-05-29 22:21:39 -06004632 return _PyDictView_New(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004633}
4634
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004635static PyObject *
4636dictitems_reversed(_PyDictViewObject *dv)
4637{
4638 if (dv->dv_dict == NULL) {
4639 Py_RETURN_NONE;
4640 }
4641 return dictiter_new(dv->dv_dict, &PyDictRevIterItem_Type);
4642}
4643
Guido van Rossum3ac67412007-02-10 18:55:06 +00004644/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004645
4646static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004647dictvalues_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004648{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004649 if (dv->dv_dict == NULL) {
4650 Py_RETURN_NONE;
4651 }
4652 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004653}
4654
Guido van Rossum83825ac2007-02-10 04:54:19 +00004655static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004656 (lenfunc)dictview_len, /* sq_length */
4657 0, /* sq_concat */
4658 0, /* sq_repeat */
4659 0, /* sq_item */
4660 0, /* sq_slice */
4661 0, /* sq_ass_item */
4662 0, /* sq_ass_slice */
4663 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004664};
4665
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004666static PyObject* dictvalues_reversed(_PyDictViewObject *dv);
4667
4668PyDoc_STRVAR(reversed_values_doc,
4669"Return a reverse iterator over the dict values.");
4670
Guido van Rossumb90c8482007-02-10 01:11:45 +00004671static PyMethodDef dictvalues_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004672 {"__reversed__", (PyCFunction)(void(*)(void))dictvalues_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004673 reversed_values_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004674 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004675};
4676
4677PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004678 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4679 "dict_values", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004680 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004681 0, /* tp_itemsize */
4682 /* methods */
4683 (destructor)dictview_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004684 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004685 0, /* tp_getattr */
4686 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004687 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004688 (reprfunc)dictview_repr, /* tp_repr */
4689 0, /* tp_as_number */
4690 &dictvalues_as_sequence, /* tp_as_sequence */
4691 0, /* tp_as_mapping */
4692 0, /* tp_hash */
4693 0, /* tp_call */
4694 0, /* tp_str */
4695 PyObject_GenericGetAttr, /* tp_getattro */
4696 0, /* tp_setattro */
4697 0, /* tp_as_buffer */
4698 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4699 0, /* tp_doc */
4700 (traverseproc)dictview_traverse, /* tp_traverse */
4701 0, /* tp_clear */
4702 0, /* tp_richcompare */
4703 0, /* tp_weaklistoffset */
4704 (getiterfunc)dictvalues_iter, /* tp_iter */
4705 0, /* tp_iternext */
4706 dictvalues_methods, /* tp_methods */
4707 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004708};
4709
4710static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304711dictvalues_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004712{
Eric Snow96c6af92015-05-29 22:21:39 -06004713 return _PyDictView_New(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004714}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004715
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004716static PyObject *
4717dictvalues_reversed(_PyDictViewObject *dv)
4718{
4719 if (dv->dv_dict == NULL) {
4720 Py_RETURN_NONE;
4721 }
4722 return dictiter_new(dv->dv_dict, &PyDictRevIterValue_Type);
4723}
4724
4725
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004726/* Returns NULL if cannot allocate a new PyDictKeysObject,
4727 but does not set an error */
4728PyDictKeysObject *
4729_PyDict_NewKeysForClass(void)
4730{
Victor Stinner742da042016-09-07 17:40:12 -07004731 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004732 if (keys == NULL)
4733 PyErr_Clear();
4734 else
4735 keys->dk_lookup = lookdict_split;
4736 return keys;
4737}
4738
4739#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
4740
4741PyObject *
4742PyObject_GenericGetDict(PyObject *obj, void *context)
4743{
4744 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
4745 if (dictptr == NULL) {
4746 PyErr_SetString(PyExc_AttributeError,
4747 "This object has no __dict__");
4748 return NULL;
4749 }
4750 dict = *dictptr;
4751 if (dict == NULL) {
4752 PyTypeObject *tp = Py_TYPE(obj);
4753 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
INADA Naokia7576492018-11-14 18:39:27 +09004754 dictkeys_incref(CACHED_KEYS(tp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004755 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
4756 }
4757 else {
4758 *dictptr = dict = PyDict_New();
4759 }
4760 }
4761 Py_XINCREF(dict);
4762 return dict;
4763}
4764
4765int
4766_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
Victor Stinner742da042016-09-07 17:40:12 -07004767 PyObject *key, PyObject *value)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004768{
4769 PyObject *dict;
4770 int res;
4771 PyDictKeysObject *cached;
4772
4773 assert(dictptr != NULL);
4774 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
4775 assert(dictptr != NULL);
4776 dict = *dictptr;
4777 if (dict == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +09004778 dictkeys_incref(cached);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004779 dict = new_dict_with_shared_keys(cached);
4780 if (dict == NULL)
4781 return -1;
4782 *dictptr = dict;
4783 }
4784 if (value == NULL) {
4785 res = PyDict_DelItem(dict, key);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004786 // Since key sharing dict doesn't allow deletion, PyDict_DelItem()
4787 // always converts dict to combined form.
4788 if ((cached = CACHED_KEYS(tp)) != NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004789 CACHED_KEYS(tp) = NULL;
INADA Naokia7576492018-11-14 18:39:27 +09004790 dictkeys_decref(cached);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004791 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01004792 }
4793 else {
INADA Naoki2294f3a2017-02-12 13:51:30 +09004794 int was_shared = (cached == ((PyDictObject *)dict)->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004795 res = PyDict_SetItem(dict, key, value);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004796 if (was_shared &&
4797 (cached = CACHED_KEYS(tp)) != NULL &&
4798 cached != ((PyDictObject *)dict)->ma_keys) {
Victor Stinner3d3f2642016-12-15 17:21:23 +01004799 /* PyDict_SetItem() may call dictresize and convert split table
4800 * into combined table. In such case, convert it to split
4801 * table again and update type's shared key only when this is
4802 * the only dict sharing key with the type.
4803 *
4804 * This is to allow using shared key in class like this:
4805 *
4806 * class C:
4807 * def __init__(self):
4808 * # one dict resize happens
4809 * self.a, self.b, self.c = 1, 2, 3
4810 * self.d, self.e, self.f = 4, 5, 6
4811 * a = C()
4812 */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004813 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004814 CACHED_KEYS(tp) = make_keys_shared(dict);
Victor Stinner742da042016-09-07 17:40:12 -07004815 }
4816 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004817 CACHED_KEYS(tp) = NULL;
4818 }
INADA Naokia7576492018-11-14 18:39:27 +09004819 dictkeys_decref(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004820 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
4821 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004822 }
4823 }
4824 } else {
4825 dict = *dictptr;
4826 if (dict == NULL) {
4827 dict = PyDict_New();
4828 if (dict == NULL)
4829 return -1;
4830 *dictptr = dict;
4831 }
4832 if (value == NULL) {
4833 res = PyDict_DelItem(dict, key);
4834 } else {
4835 res = PyDict_SetItem(dict, key, value);
4836 }
4837 }
4838 return res;
4839}
4840
4841void
4842_PyDictKeys_DecRef(PyDictKeysObject *keys)
4843{
INADA Naokia7576492018-11-14 18:39:27 +09004844 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004845}