blob: fa35d16478f63532a83d6d1dd0c7706ddd0b45d6 [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
Victor Stinnerb4b53862020-05-05 19:55:29 +0200253
254/* bpo-40521: dict free lists are shared by all interpreters. */
255#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
256# undef PyDict_MAXFREELIST
257# define PyDict_MAXFREELIST 0
258#endif
259
260#if PyDict_MAXFREELIST > 0
Christian Heimes2202f872008-02-06 14:31:34 +0000261static PyDictObject *free_list[PyDict_MAXFREELIST];
262static int numfree = 0;
Victor Stinner742da042016-09-07 17:40:12 -0700263static PyDictKeysObject *keys_free_list[PyDict_MAXFREELIST];
264static int numfreekeys = 0;
Victor Stinnerb4b53862020-05-05 19:55:29 +0200265#endif
Raymond Hettinger43442782004-03-17 21:55:03 +0000266
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300267#include "clinic/dictobject.c.h"
268
Victor Stinnerae00a5a2020-04-29 02:29:20 +0200269void
270_PyDict_ClearFreeList(void)
Christian Heimes77c02eb2008-02-09 02:18:51 +0000271{
Victor Stinnerb4b53862020-05-05 19:55:29 +0200272#if PyDict_MAXFREELIST > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 while (numfree) {
Victor Stinnerae00a5a2020-04-29 02:29:20 +0200274 PyDictObject *op = free_list[--numfree];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 assert(PyDict_CheckExact(op));
276 PyObject_GC_Del(op);
277 }
Victor Stinner742da042016-09-07 17:40:12 -0700278 while (numfreekeys) {
279 PyObject_FREE(keys_free_list[--numfreekeys]);
280 }
Victor Stinnerb4b53862020-05-05 19:55:29 +0200281#endif
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100282}
283
David Malcolm49526f42012-06-22 14:55:41 -0400284/* Print summary info about the state of the optimized allocator */
285void
286_PyDict_DebugMallocStats(FILE *out)
287{
Victor Stinnerb4b53862020-05-05 19:55:29 +0200288#if PyDict_MAXFREELIST > 0
David Malcolm49526f42012-06-22 14:55:41 -0400289 _PyDebugAllocatorStats(out,
290 "free PyDictObject", numfree, sizeof(PyDictObject));
Victor Stinnerb4b53862020-05-05 19:55:29 +0200291#endif
David Malcolm49526f42012-06-22 14:55:41 -0400292}
293
294
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100295void
Victor Stinnerbed48172019-08-27 00:12:32 +0200296_PyDict_Fini(void)
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100297{
Victor Stinnerae00a5a2020-04-29 02:29:20 +0200298 _PyDict_ClearFreeList();
Christian Heimes77c02eb2008-02-09 02:18:51 +0000299}
300
Victor Stinner742da042016-09-07 17:40:12 -0700301#define DK_SIZE(dk) ((dk)->dk_size)
302#if SIZEOF_VOID_P > 4
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700303#define DK_IXSIZE(dk) \
304 (DK_SIZE(dk) <= 0xff ? \
305 1 : DK_SIZE(dk) <= 0xffff ? \
306 2 : DK_SIZE(dk) <= 0xffffffff ? \
Benjamin Peterson3c569292016-09-08 13:16:41 -0700307 4 : sizeof(int64_t))
Victor Stinner742da042016-09-07 17:40:12 -0700308#else
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700309#define DK_IXSIZE(dk) \
310 (DK_SIZE(dk) <= 0xff ? \
311 1 : DK_SIZE(dk) <= 0xffff ? \
Benjamin Peterson3c569292016-09-08 13:16:41 -0700312 2 : sizeof(int32_t))
Victor Stinner742da042016-09-07 17:40:12 -0700313#endif
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700314#define DK_ENTRIES(dk) \
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700315 ((PyDictKeyEntry*)(&((int8_t*)((dk)->dk_indices))[DK_SIZE(dk) * DK_IXSIZE(dk)]))
Victor Stinner742da042016-09-07 17:40:12 -0700316
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400317#define DK_MASK(dk) (((dk)->dk_size)-1)
318#define IS_POWER_OF_2(x) (((x) & (x-1)) == 0)
319
INADA Naokia7576492018-11-14 18:39:27 +0900320static void free_keys_object(PyDictKeysObject *keys);
321
322static inline void
323dictkeys_incref(PyDictKeysObject *dk)
324{
Victor Stinner49932fe2020-02-03 17:55:05 +0100325#ifdef Py_REF_DEBUG
326 _Py_RefTotal++;
327#endif
INADA Naokia7576492018-11-14 18:39:27 +0900328 dk->dk_refcnt++;
329}
330
331static inline void
332dictkeys_decref(PyDictKeysObject *dk)
333{
334 assert(dk->dk_refcnt > 0);
Victor Stinner49932fe2020-02-03 17:55:05 +0100335#ifdef Py_REF_DEBUG
336 _Py_RefTotal--;
337#endif
INADA Naokia7576492018-11-14 18:39:27 +0900338 if (--dk->dk_refcnt == 0) {
339 free_keys_object(dk);
340 }
341}
342
Victor Stinner742da042016-09-07 17:40:12 -0700343/* lookup indices. returns DKIX_EMPTY, DKIX_DUMMY, or ix >=0 */
Benjamin Peterson73222252016-09-08 09:58:47 -0700344static inline Py_ssize_t
Andy Lester62d21c92020-03-25 23:13:01 -0500345dictkeys_get_index(const PyDictKeysObject *keys, Py_ssize_t i)
Victor Stinner742da042016-09-07 17:40:12 -0700346{
347 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700348 Py_ssize_t ix;
349
Victor Stinner742da042016-09-07 17:40:12 -0700350 if (s <= 0xff) {
Andy Lester62d21c92020-03-25 23:13:01 -0500351 const int8_t *indices = (const int8_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700352 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700353 }
354 else if (s <= 0xffff) {
Andy Lester62d21c92020-03-25 23:13:01 -0500355 const int16_t *indices = (const int16_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700356 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700357 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700358#if SIZEOF_VOID_P > 4
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300359 else if (s > 0xffffffff) {
Andy Lester62d21c92020-03-25 23:13:01 -0500360 const int64_t *indices = (const int64_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700361 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700362 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700363#endif
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300364 else {
Andy Lester62d21c92020-03-25 23:13:01 -0500365 const int32_t *indices = (const int32_t*)(keys->dk_indices);
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300366 ix = indices[i];
367 }
Victor Stinner71211e32016-09-08 10:52:46 -0700368 assert(ix >= DKIX_DUMMY);
369 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700370}
371
372/* write to indices. */
Benjamin Peterson73222252016-09-08 09:58:47 -0700373static inline void
INADA Naokia7576492018-11-14 18:39:27 +0900374dictkeys_set_index(PyDictKeysObject *keys, Py_ssize_t i, Py_ssize_t ix)
Victor Stinner742da042016-09-07 17:40:12 -0700375{
376 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700377
378 assert(ix >= DKIX_DUMMY);
379
Victor Stinner742da042016-09-07 17:40:12 -0700380 if (s <= 0xff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700381 int8_t *indices = (int8_t*)(keys->dk_indices);
Victor Stinner71211e32016-09-08 10:52:46 -0700382 assert(ix <= 0x7f);
Victor Stinner208857e2016-09-08 11:35:46 -0700383 indices[i] = (char)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700384 }
385 else if (s <= 0xffff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700386 int16_t *indices = (int16_t*)(keys->dk_indices);
Victor Stinner71211e32016-09-08 10:52:46 -0700387 assert(ix <= 0x7fff);
Victor Stinner208857e2016-09-08 11:35:46 -0700388 indices[i] = (int16_t)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700389 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700390#if SIZEOF_VOID_P > 4
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300391 else if (s > 0xffffffff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700392 int64_t *indices = (int64_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700393 indices[i] = ix;
Victor Stinner742da042016-09-07 17:40:12 -0700394 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700395#endif
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300396 else {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700397 int32_t *indices = (int32_t*)(keys->dk_indices);
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300398 assert(ix <= 0x7fffffff);
399 indices[i] = (int32_t)ix;
400 }
Victor Stinner742da042016-09-07 17:40:12 -0700401}
402
403
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200404/* USABLE_FRACTION is the maximum dictionary load.
Victor Stinner742da042016-09-07 17:40:12 -0700405 * Increasing this ratio makes dictionaries more dense resulting in more
406 * collisions. Decreasing it improves sparseness at the expense of spreading
407 * indices over more cache lines and at the cost of total memory consumed.
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200408 *
409 * USABLE_FRACTION must obey the following:
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400410 * (0 < USABLE_FRACTION(n) < n) for all n >= 2
411 *
Victor Stinner742da042016-09-07 17:40:12 -0700412 * USABLE_FRACTION should be quick to calculate.
413 * Fractions around 1/2 to 2/3 seem to work well in practice.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400414 */
Victor Stinner742da042016-09-07 17:40:12 -0700415#define USABLE_FRACTION(n) (((n) << 1)/3)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400416
Victor Stinner742da042016-09-07 17:40:12 -0700417/* ESTIMATE_SIZE is reverse function of USABLE_FRACTION.
418 * This can be used to reserve enough size to insert n entries without
419 * resizing.
420 */
INADA Naoki92c50ee2016-11-22 00:57:02 +0900421#define ESTIMATE_SIZE(n) (((n)*3+1) >> 1)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400422
Victor Stinner742da042016-09-07 17:40:12 -0700423/* Alternative fraction that is otherwise close enough to 2n/3 to make
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400424 * little difference. 8 * 2/3 == 8 * 5/8 == 5. 16 * 2/3 == 16 * 5/8 == 10.
425 * 32 * 2/3 = 21, 32 * 5/8 = 20.
426 * Its advantage is that it is faster to compute on machines with slow division.
427 * #define USABLE_FRACTION(n) (((n) >> 1) + ((n) >> 2) - ((n) >> 3))
Victor Stinner742da042016-09-07 17:40:12 -0700428 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400429
Victor Stinnera9f61a52013-07-16 22:17:26 +0200430/* GROWTH_RATE. Growth rate upon hitting maximum load.
INADA Naoki5fbc5112018-04-17 15:53:34 +0900431 * Currently set to used*3.
Victor Stinnera9f61a52013-07-16 22:17:26 +0200432 * This means that dicts double in size when growing without deletions,
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700433 * but have more head room when the number of deletions is on a par with the
INADA Naoki5fbc5112018-04-17 15:53:34 +0900434 * number of insertions. See also bpo-17563 and bpo-33205.
435 *
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700436 * GROWTH_RATE was set to used*4 up to version 3.2.
437 * GROWTH_RATE was set to used*2 in version 3.3.0
INADA Naoki5fbc5112018-04-17 15:53:34 +0900438 * GROWTH_RATE was set to used*2 + capacity/2 in 3.4.0-3.6.0.
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200439 */
INADA Naoki5fbc5112018-04-17 15:53:34 +0900440#define GROWTH_RATE(d) ((d)->ma_used*3)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400441
442#define ENSURE_ALLOWS_DELETIONS(d) \
443 if ((d)->ma_keys->dk_lookup == lookdict_unicode_nodummy) { \
444 (d)->ma_keys->dk_lookup = lookdict_unicode; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400446
447/* This immutable, empty PyDictKeysObject is used for PyDict_Clear()
448 * (which cannot fail and thus can do no allocation).
449 */
450static PyDictKeysObject empty_keys_struct = {
Serhiy Storchaka97932e42016-09-26 23:01:23 +0300451 1, /* dk_refcnt */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400452 1, /* dk_size */
453 lookdict_split, /* dk_lookup */
454 0, /* dk_usable (immutable) */
Victor Stinner742da042016-09-07 17:40:12 -0700455 0, /* dk_nentries */
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700456 {DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY,
457 DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY}, /* dk_indices */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400458};
459
460static PyObject *empty_values[1] = { NULL };
461
462#define Py_EMPTY_KEYS &empty_keys_struct
463
Victor Stinner611b0fa2016-09-14 15:02:01 +0200464/* Uncomment to check the dict content in _PyDict_CheckConsistency() */
465/* #define DEBUG_PYDICT */
466
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200467#ifdef DEBUG_PYDICT
468# define ASSERT_CONSISTENT(op) assert(_PyDict_CheckConsistency((PyObject *)(op), 1))
469#else
470# define ASSERT_CONSISTENT(op) assert(_PyDict_CheckConsistency((PyObject *)(op), 0))
471#endif
Victor Stinner611b0fa2016-09-14 15:02:01 +0200472
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200473
474int
475_PyDict_CheckConsistency(PyObject *op, int check_content)
Victor Stinner611b0fa2016-09-14 15:02:01 +0200476{
Victor Stinner68762572019-10-07 18:42:01 +0200477#define CHECK(expr) \
478 do { if (!(expr)) { _PyObject_ASSERT_FAILED_MSG(op, Py_STRINGIFY(expr)); } } while (0)
479
480 assert(op != NULL);
481 CHECK(PyDict_Check(op));
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200482 PyDictObject *mp = (PyDictObject *)op;
Victor Stinner50fe3f82018-10-26 18:47:15 +0200483
Victor Stinner611b0fa2016-09-14 15:02:01 +0200484 PyDictKeysObject *keys = mp->ma_keys;
485 int splitted = _PyDict_HasSplitTable(mp);
486 Py_ssize_t usable = USABLE_FRACTION(keys->dk_size);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200487
Victor Stinner68762572019-10-07 18:42:01 +0200488 CHECK(0 <= mp->ma_used && mp->ma_used <= usable);
489 CHECK(IS_POWER_OF_2(keys->dk_size));
490 CHECK(0 <= keys->dk_usable && keys->dk_usable <= usable);
491 CHECK(0 <= keys->dk_nentries && keys->dk_nentries <= usable);
492 CHECK(keys->dk_usable + keys->dk_nentries <= usable);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200493
494 if (!splitted) {
495 /* combined table */
Victor Stinner68762572019-10-07 18:42:01 +0200496 CHECK(keys->dk_refcnt == 1);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200497 }
498
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200499 if (check_content) {
500 PyDictKeyEntry *entries = DK_ENTRIES(keys);
501 Py_ssize_t i;
Victor Stinner611b0fa2016-09-14 15:02:01 +0200502
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200503 for (i=0; i < keys->dk_size; i++) {
504 Py_ssize_t ix = dictkeys_get_index(keys, i);
Victor Stinner68762572019-10-07 18:42:01 +0200505 CHECK(DKIX_DUMMY <= ix && ix <= usable);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200506 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200507
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200508 for (i=0; i < usable; i++) {
509 PyDictKeyEntry *entry = &entries[i];
510 PyObject *key = entry->me_key;
511
512 if (key != NULL) {
513 if (PyUnicode_CheckExact(key)) {
514 Py_hash_t hash = ((PyASCIIObject *)key)->hash;
Victor Stinner68762572019-10-07 18:42:01 +0200515 CHECK(hash != -1);
516 CHECK(entry->me_hash == hash);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200517 }
518 else {
519 /* test_dict fails if PyObject_Hash() is called again */
Victor Stinner68762572019-10-07 18:42:01 +0200520 CHECK(entry->me_hash != -1);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200521 }
522 if (!splitted) {
Victor Stinner68762572019-10-07 18:42:01 +0200523 CHECK(entry->me_value != NULL);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200524 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200525 }
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200526
527 if (splitted) {
Victor Stinner68762572019-10-07 18:42:01 +0200528 CHECK(entry->me_value == NULL);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200529 }
530 }
531
532 if (splitted) {
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200533 /* splitted table */
534 for (i=0; i < mp->ma_used; i++) {
Victor Stinner68762572019-10-07 18:42:01 +0200535 CHECK(mp->ma_values[i] != NULL);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200536 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200537 }
538 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200539 return 1;
Victor Stinner68762572019-10-07 18:42:01 +0200540
541#undef CHECK
Victor Stinner611b0fa2016-09-14 15:02:01 +0200542}
Victor Stinner611b0fa2016-09-14 15:02:01 +0200543
544
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400545static PyDictKeysObject *new_keys_object(Py_ssize_t size)
546{
547 PyDictKeysObject *dk;
Victor Stinner742da042016-09-07 17:40:12 -0700548 Py_ssize_t es, usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400549
Victor Stinner742da042016-09-07 17:40:12 -0700550 assert(size >= PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400551 assert(IS_POWER_OF_2(size));
Victor Stinner742da042016-09-07 17:40:12 -0700552
553 usable = USABLE_FRACTION(size);
554 if (size <= 0xff) {
555 es = 1;
556 }
557 else if (size <= 0xffff) {
558 es = 2;
559 }
560#if SIZEOF_VOID_P > 4
561 else if (size <= 0xffffffff) {
562 es = 4;
563 }
564#endif
565 else {
566 es = sizeof(Py_ssize_t);
567 }
568
Victor Stinnerb4b53862020-05-05 19:55:29 +0200569#if PyDict_MAXFREELIST > 0
Victor Stinner742da042016-09-07 17:40:12 -0700570 if (size == PyDict_MINSIZE && numfreekeys > 0) {
571 dk = keys_free_list[--numfreekeys];
572 }
Victor Stinnerb4b53862020-05-05 19:55:29 +0200573 else
574#endif
575 {
Victor Stinner98ee9d52016-09-08 09:33:56 -0700576 dk = PyObject_MALLOC(sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -0700577 + es * size
578 + sizeof(PyDictKeyEntry) * usable);
Victor Stinner742da042016-09-07 17:40:12 -0700579 if (dk == NULL) {
580 PyErr_NoMemory();
581 return NULL;
582 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400583 }
Victor Stinner49932fe2020-02-03 17:55:05 +0100584#ifdef Py_REF_DEBUG
585 _Py_RefTotal++;
586#endif
INADA Naokia7576492018-11-14 18:39:27 +0900587 dk->dk_refcnt = 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400588 dk->dk_size = size;
Victor Stinner742da042016-09-07 17:40:12 -0700589 dk->dk_usable = usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400590 dk->dk_lookup = lookdict_unicode_nodummy;
Victor Stinner742da042016-09-07 17:40:12 -0700591 dk->dk_nentries = 0;
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700592 memset(&dk->dk_indices[0], 0xff, es * size);
Victor Stinner742da042016-09-07 17:40:12 -0700593 memset(DK_ENTRIES(dk), 0, sizeof(PyDictKeyEntry) * usable);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400594 return dk;
595}
596
597static void
598free_keys_object(PyDictKeysObject *keys)
599{
Victor Stinner742da042016-09-07 17:40:12 -0700600 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400601 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -0700602 for (i = 0, n = keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400603 Py_XDECREF(entries[i].me_key);
604 Py_XDECREF(entries[i].me_value);
605 }
Victor Stinnerb4b53862020-05-05 19:55:29 +0200606#if PyDict_MAXFREELIST > 0
Victor Stinner742da042016-09-07 17:40:12 -0700607 if (keys->dk_size == PyDict_MINSIZE && numfreekeys < PyDict_MAXFREELIST) {
608 keys_free_list[numfreekeys++] = keys;
609 return;
610 }
Victor Stinnerb4b53862020-05-05 19:55:29 +0200611#endif
Raymond Hettingerce5179f2016-01-31 08:56:21 -0800612 PyObject_FREE(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400613}
614
615#define new_values(size) PyMem_NEW(PyObject *, size)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400616#define free_values(values) PyMem_FREE(values)
617
618/* Consumes a reference to the keys object */
619static PyObject *
620new_dict(PyDictKeysObject *keys, PyObject **values)
621{
622 PyDictObject *mp;
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200623 assert(keys != NULL);
Victor Stinnerb4b53862020-05-05 19:55:29 +0200624#if PyDict_MAXFREELIST > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 if (numfree) {
626 mp = free_list[--numfree];
627 assert (mp != NULL);
Dong-hee Na1b55b652020-02-17 19:09:15 +0900628 assert (Py_IS_TYPE(mp, &PyDict_Type));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 _Py_NewReference((PyObject *)mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 }
Victor Stinnerb4b53862020-05-05 19:55:29 +0200631 else
632#endif
633 {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400634 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
635 if (mp == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +0900636 dictkeys_decref(keys);
Zackery Spytz3d07c1e2019-03-23 20:23:29 -0600637 if (values != empty_values) {
638 free_values(values);
639 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400640 return NULL;
641 }
642 }
643 mp->ma_keys = keys;
644 mp->ma_values = values;
645 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -0700646 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200647 ASSERT_CONSISTENT(mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000649}
650
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400651/* Consumes a reference to the keys object */
652static PyObject *
653new_dict_with_shared_keys(PyDictKeysObject *keys)
654{
655 PyObject **values;
656 Py_ssize_t i, size;
657
Victor Stinner742da042016-09-07 17:40:12 -0700658 size = USABLE_FRACTION(DK_SIZE(keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400659 values = new_values(size);
660 if (values == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +0900661 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400662 return PyErr_NoMemory();
663 }
664 for (i = 0; i < size; i++) {
665 values[i] = NULL;
666 }
667 return new_dict(keys, values);
668}
669
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500670
671static PyObject *
672clone_combined_dict(PyDictObject *orig)
673{
674 assert(PyDict_CheckExact(orig));
675 assert(orig->ma_values == NULL);
676 assert(orig->ma_keys->dk_refcnt == 1);
677
678 Py_ssize_t keys_size = _PyDict_KeysSize(orig->ma_keys);
679 PyDictKeysObject *keys = PyObject_Malloc(keys_size);
680 if (keys == NULL) {
681 PyErr_NoMemory();
682 return NULL;
683 }
684
685 memcpy(keys, orig->ma_keys, keys_size);
686
687 /* After copying key/value pairs, we need to incref all
688 keys and values and they are about to be co-owned by a
689 new dict object. */
690 PyDictKeyEntry *ep0 = DK_ENTRIES(keys);
691 Py_ssize_t n = keys->dk_nentries;
692 for (Py_ssize_t i = 0; i < n; i++) {
693 PyDictKeyEntry *entry = &ep0[i];
694 PyObject *value = entry->me_value;
695 if (value != NULL) {
696 Py_INCREF(value);
697 Py_INCREF(entry->me_key);
698 }
699 }
700
701 PyDictObject *new = (PyDictObject *)new_dict(keys, NULL);
702 if (new == NULL) {
703 /* In case of an error, `new_dict()` takes care of
704 cleaning up `keys`. */
705 return NULL;
706 }
707 new->ma_used = orig->ma_used;
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200708 ASSERT_CONSISTENT(new);
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500709 if (_PyObject_GC_IS_TRACKED(orig)) {
710 /* Maintain tracking. */
711 _PyObject_GC_TRACK(new);
712 }
Yury Selivanov0b752282018-07-06 12:20:07 -0400713
714 /* Since we copied the keys table we now have an extra reference
Victor Stinner49932fe2020-02-03 17:55:05 +0100715 in the system. Manually call increment _Py_RefTotal to signal that
INADA Naokia7576492018-11-14 18:39:27 +0900716 we have it now; calling dictkeys_incref would be an error as
Yury Selivanov0b752282018-07-06 12:20:07 -0400717 keys->dk_refcnt is already set to 1 (after memcpy). */
Victor Stinner49932fe2020-02-03 17:55:05 +0100718#ifdef Py_REF_DEBUG
719 _Py_RefTotal++;
720#endif
Yury Selivanov0b752282018-07-06 12:20:07 -0400721
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500722 return (PyObject *)new;
723}
724
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400725PyObject *
726PyDict_New(void)
727{
Inada Naokif2a18672019-03-12 17:25:44 +0900728 dictkeys_incref(Py_EMPTY_KEYS);
729 return new_dict(Py_EMPTY_KEYS, empty_values);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400730}
731
Victor Stinner742da042016-09-07 17:40:12 -0700732/* Search index of hash table from offset of entry table */
733static Py_ssize_t
734lookdict_index(PyDictKeysObject *k, Py_hash_t hash, Py_ssize_t index)
735{
Victor Stinner742da042016-09-07 17:40:12 -0700736 size_t mask = DK_MASK(k);
INADA Naoki073ae482017-06-23 15:22:50 +0900737 size_t perturb = (size_t)hash;
738 size_t i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700739
INADA Naoki073ae482017-06-23 15:22:50 +0900740 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900741 Py_ssize_t ix = dictkeys_get_index(k, i);
Victor Stinner742da042016-09-07 17:40:12 -0700742 if (ix == index) {
743 return i;
744 }
745 if (ix == DKIX_EMPTY) {
746 return DKIX_EMPTY;
747 }
INADA Naoki073ae482017-06-23 15:22:50 +0900748 perturb >>= PERTURB_SHIFT;
749 i = mask & (i*5 + perturb + 1);
Victor Stinner742da042016-09-07 17:40:12 -0700750 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700751 Py_UNREACHABLE();
Victor Stinner742da042016-09-07 17:40:12 -0700752}
753
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000754/*
755The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000756This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000757Open addressing is preferred over chaining since the link overhead for
758chaining would be substantial (100% with typical malloc overhead).
759
Tim Peterseb28ef22001-06-02 05:27:19 +0000760The initial probe index is computed as hash mod the table size. Subsequent
761probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000762
763All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000764
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000765The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000766contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000767Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000768
Victor Stinner742da042016-09-07 17:40:12 -0700769lookdict() is general-purpose, and may return DKIX_ERROR if (and only if) a
Victor Stinnera4348cc2016-09-08 12:01:25 -0700770comparison raises an exception.
Guido van Rossum89d8c602007-09-18 17:26:56 +0000771lookdict_unicode() below is specialized to string keys, comparison of which can
INADA Naoki1b8df102017-02-20 22:48:10 +0900772never raise an exception; that function can never return DKIX_ERROR when key
773is string. Otherwise, it falls back to lookdict().
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400774lookdict_unicode_nodummy is further specialized for string keys that cannot be
775the <dummy> value.
INADA Naoki778928b2017-08-03 23:45:15 +0900776For both, when the key isn't found a DKIX_EMPTY is returned.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000777*/
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100778static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400779lookdict(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900780 Py_hash_t hash, PyObject **value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000781{
INADA Naoki778928b2017-08-03 23:45:15 +0900782 size_t i, mask, perturb;
Victor Stinner742da042016-09-07 17:40:12 -0700783 PyDictKeysObject *dk;
INADA Naoki778928b2017-08-03 23:45:15 +0900784 PyDictKeyEntry *ep0;
Tim Peterseb28ef22001-06-02 05:27:19 +0000785
Antoine Pitrou9a234902012-05-13 20:48:01 +0200786top:
Victor Stinner742da042016-09-07 17:40:12 -0700787 dk = mp->ma_keys;
Victor Stinner742da042016-09-07 17:40:12 -0700788 ep0 = DK_ENTRIES(dk);
INADA Naoki778928b2017-08-03 23:45:15 +0900789 mask = DK_MASK(dk);
790 perturb = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700792
INADA Naoki778928b2017-08-03 23:45:15 +0900793 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900794 Py_ssize_t ix = dictkeys_get_index(dk, i);
Victor Stinner742da042016-09-07 17:40:12 -0700795 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700796 *value_addr = NULL;
797 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400798 }
INADA Naoki778928b2017-08-03 23:45:15 +0900799 if (ix >= 0) {
800 PyDictKeyEntry *ep = &ep0[ix];
801 assert(ep->me_key != NULL);
802 if (ep->me_key == key) {
803 *value_addr = ep->me_value;
804 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700805 }
INADA Naoki778928b2017-08-03 23:45:15 +0900806 if (ep->me_hash == hash) {
807 PyObject *startkey = ep->me_key;
808 Py_INCREF(startkey);
809 int cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
810 Py_DECREF(startkey);
811 if (cmp < 0) {
812 *value_addr = NULL;
813 return DKIX_ERROR;
814 }
815 if (dk == mp->ma_keys && ep->me_key == startkey) {
816 if (cmp > 0) {
817 *value_addr = ep->me_value;
818 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700819 }
INADA Naoki778928b2017-08-03 23:45:15 +0900820 }
821 else {
822 /* The dict was mutated, restart */
823 goto top;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400824 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 }
INADA Naoki778928b2017-08-03 23:45:15 +0900827 perturb >>= PERTURB_SHIFT;
828 i = (i*5 + perturb + 1) & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700830 Py_UNREACHABLE();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000831}
832
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400833/* Specialized version for string-only keys */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100834static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400835lookdict_unicode(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900836 Py_hash_t hash, PyObject **value_addr)
Fred Drake1bff34a2000-08-31 19:31:38 +0000837{
Victor Stinner742da042016-09-07 17:40:12 -0700838 assert(mp->ma_values == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 /* Make sure this function doesn't have to handle non-unicode keys,
840 including subclasses of str; e.g., one reason to subclass
841 unicodes is to override __eq__, and for speed we don't cater to
842 that here. */
843 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400844 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900845 return lookdict(mp, key, hash, value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 }
Tim Peters15d49292001-05-27 07:39:22 +0000847
INADA Naoki778928b2017-08-03 23:45:15 +0900848 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
849 size_t mask = DK_MASK(mp->ma_keys);
850 size_t perturb = (size_t)hash;
851 size_t i = (size_t)hash & mask;
852
853 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900854 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
Victor Stinner742da042016-09-07 17:40:12 -0700855 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700856 *value_addr = NULL;
857 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400858 }
INADA Naoki778928b2017-08-03 23:45:15 +0900859 if (ix >= 0) {
860 PyDictKeyEntry *ep = &ep0[ix];
861 assert(ep->me_key != NULL);
862 assert(PyUnicode_CheckExact(ep->me_key));
863 if (ep->me_key == key ||
864 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
865 *value_addr = ep->me_value;
866 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700867 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400868 }
INADA Naoki778928b2017-08-03 23:45:15 +0900869 perturb >>= PERTURB_SHIFT;
870 i = mask & (i*5 + perturb + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700872 Py_UNREACHABLE();
Fred Drake1bff34a2000-08-31 19:31:38 +0000873}
874
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400875/* Faster version of lookdict_unicode when it is known that no <dummy> keys
876 * will be present. */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100877static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400878lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900879 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400880{
Victor Stinner742da042016-09-07 17:40:12 -0700881 assert(mp->ma_values == NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400882 /* Make sure this function doesn't have to handle non-unicode keys,
883 including subclasses of str; e.g., one reason to subclass
884 unicodes is to override __eq__, and for speed we don't cater to
885 that here. */
886 if (!PyUnicode_CheckExact(key)) {
887 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900888 return lookdict(mp, key, hash, value_addr);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400889 }
INADA Naoki778928b2017-08-03 23:45:15 +0900890
891 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
892 size_t mask = DK_MASK(mp->ma_keys);
893 size_t perturb = (size_t)hash;
894 size_t i = (size_t)hash & mask;
895
896 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900897 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
Victor Stinner742da042016-09-07 17:40:12 -0700898 assert (ix != DKIX_DUMMY);
899 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700900 *value_addr = NULL;
901 return DKIX_EMPTY;
902 }
INADA Naoki778928b2017-08-03 23:45:15 +0900903 PyDictKeyEntry *ep = &ep0[ix];
904 assert(ep->me_key != NULL);
905 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700906 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400907 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900908 *value_addr = ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700909 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400910 }
INADA Naoki778928b2017-08-03 23:45:15 +0900911 perturb >>= PERTURB_SHIFT;
912 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400913 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700914 Py_UNREACHABLE();
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400915}
916
917/* Version of lookdict for split tables.
918 * All split tables and only split tables use this lookup function.
919 * Split tables only contain unicode keys and no dummy keys,
920 * so algorithm is the same as lookdict_unicode_nodummy.
921 */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100922static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400923lookdict_split(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900924 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400925{
Victor Stinner742da042016-09-07 17:40:12 -0700926 /* mp must split table */
927 assert(mp->ma_values != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400928 if (!PyUnicode_CheckExact(key)) {
INADA Naoki778928b2017-08-03 23:45:15 +0900929 Py_ssize_t ix = lookdict(mp, key, hash, value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700930 if (ix >= 0) {
INADA Naokiba609772016-12-07 20:41:42 +0900931 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700932 }
933 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400934 }
Victor Stinner742da042016-09-07 17:40:12 -0700935
INADA Naoki778928b2017-08-03 23:45:15 +0900936 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
937 size_t mask = DK_MASK(mp->ma_keys);
938 size_t perturb = (size_t)hash;
939 size_t i = (size_t)hash & mask;
940
941 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900942 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
INADA Naoki778928b2017-08-03 23:45:15 +0900943 assert (ix != DKIX_DUMMY);
Victor Stinner742da042016-09-07 17:40:12 -0700944 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700945 *value_addr = NULL;
946 return DKIX_EMPTY;
947 }
INADA Naoki778928b2017-08-03 23:45:15 +0900948 PyDictKeyEntry *ep = &ep0[ix];
949 assert(ep->me_key != NULL);
950 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700951 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400952 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900953 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700954 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400955 }
INADA Naoki778928b2017-08-03 23:45:15 +0900956 perturb >>= PERTURB_SHIFT;
957 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400958 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700959 Py_UNREACHABLE();
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400960}
961
Benjamin Petersonfb886362010-04-24 18:21:17 +0000962int
963_PyDict_HasOnlyStringKeys(PyObject *dict)
964{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 Py_ssize_t pos = 0;
966 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000967 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400969 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 return 1;
971 while (PyDict_Next(dict, &pos, &key, &value))
972 if (!PyUnicode_Check(key))
973 return 0;
974 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000975}
976
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000977#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 do { \
979 if (!_PyObject_GC_IS_TRACKED(mp)) { \
980 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
981 _PyObject_GC_MAY_BE_TRACKED(value)) { \
982 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 } \
984 } \
985 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000986
987void
988_PyDict_MaybeUntrack(PyObject *op)
989{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 PyDictObject *mp;
991 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -0700992 Py_ssize_t i, numentries;
993 PyDictKeyEntry *ep0;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
996 return;
997
998 mp = (PyDictObject *) op;
Victor Stinner742da042016-09-07 17:40:12 -0700999 ep0 = DK_ENTRIES(mp->ma_keys);
1000 numentries = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001001 if (_PyDict_HasSplitTable(mp)) {
Victor Stinner742da042016-09-07 17:40:12 -07001002 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001003 if ((value = mp->ma_values[i]) == NULL)
1004 continue;
1005 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
Victor Stinner742da042016-09-07 17:40:12 -07001006 assert(!_PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001007 return;
1008 }
1009 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001011 else {
Victor Stinner742da042016-09-07 17:40:12 -07001012 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001013 if ((value = ep0[i].me_value) == NULL)
1014 continue;
1015 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
1016 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
1017 return;
1018 }
1019 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001021}
1022
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001023/* Internal function to find slot for an item from its hash
Victor Stinner3c336c52016-09-12 14:17:40 +02001024 when it is known that the key is not present in the dict.
1025
1026 The dict must be combined. */
INADA Naokiba609772016-12-07 20:41:42 +09001027static Py_ssize_t
INADA Naoki778928b2017-08-03 23:45:15 +09001028find_empty_slot(PyDictKeysObject *keys, Py_hash_t hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001029{
INADA Naoki778928b2017-08-03 23:45:15 +09001030 assert(keys != NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001031
INADA Naoki778928b2017-08-03 23:45:15 +09001032 const size_t mask = DK_MASK(keys);
1033 size_t i = hash & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001034 Py_ssize_t ix = dictkeys_get_index(keys, i);
INADA Naoki778928b2017-08-03 23:45:15 +09001035 for (size_t perturb = hash; ix >= 0;) {
INADA Naoki267941c2016-10-06 15:19:07 +09001036 perturb >>= PERTURB_SHIFT;
INADA Naoki778928b2017-08-03 23:45:15 +09001037 i = (i*5 + perturb + 1) & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001038 ix = dictkeys_get_index(keys, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 }
INADA Naoki778928b2017-08-03 23:45:15 +09001040 return i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001041}
1042
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001043static int
1044insertion_resize(PyDictObject *mp)
1045{
Raymond Hettinger36f74aa2013-05-17 03:01:13 -07001046 return dictresize(mp, GROWTH_RATE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001047}
Antoine Pitroue965d972012-02-27 00:45:12 +01001048
1049/*
1050Internal routine to insert a new item into the table.
1051Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +01001052Returns -1 if an error occurred, or 0 on success.
1053*/
1054static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001055insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001056{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001057 PyObject *old_value;
INADA Naokiba609772016-12-07 20:41:42 +09001058 PyDictKeyEntry *ep;
Antoine Pitroue965d972012-02-27 00:45:12 +01001059
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001060 Py_INCREF(key);
1061 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001062 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
1063 if (insertion_resize(mp) < 0)
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001064 goto Fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001065 }
1066
INADA Naoki778928b2017-08-03 23:45:15 +09001067 Py_ssize_t ix = mp->ma_keys->dk_lookup(mp, key, hash, &old_value);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001068 if (ix == DKIX_ERROR)
1069 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001070
Antoine Pitroud6967322014-10-18 00:35:00 +02001071 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001072 MAINTAIN_TRACKING(mp, key, value);
Victor Stinner742da042016-09-07 17:40:12 -07001073
1074 /* When insertion order is different from shared key, we can't share
1075 * the key anymore. Convert this instance to combine table.
1076 */
1077 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09001078 ((ix >= 0 && old_value == NULL && mp->ma_used != ix) ||
Victor Stinner742da042016-09-07 17:40:12 -07001079 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001080 if (insertion_resize(mp) < 0)
1081 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001082 ix = DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001083 }
Victor Stinner742da042016-09-07 17:40:12 -07001084
1085 if (ix == DKIX_EMPTY) {
1086 /* Insert into new slot. */
INADA Naokiba609772016-12-07 20:41:42 +09001087 assert(old_value == NULL);
Victor Stinner742da042016-09-07 17:40:12 -07001088 if (mp->ma_keys->dk_usable <= 0) {
1089 /* Need to resize. */
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001090 if (insertion_resize(mp) < 0)
1091 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001092 }
INADA Naoki778928b2017-08-03 23:45:15 +09001093 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naokiba609772016-12-07 20:41:42 +09001094 ep = &DK_ENTRIES(mp->ma_keys)[mp->ma_keys->dk_nentries];
INADA Naokia7576492018-11-14 18:39:27 +09001095 dictkeys_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Victor Stinner742da042016-09-07 17:40:12 -07001096 ep->me_key = key;
1097 ep->me_hash = hash;
1098 if (mp->ma_values) {
1099 assert (mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
1100 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001101 }
1102 else {
Victor Stinner742da042016-09-07 17:40:12 -07001103 ep->me_value = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001104 }
1105 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001106 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07001107 mp->ma_keys->dk_usable--;
1108 mp->ma_keys->dk_nentries++;
1109 assert(mp->ma_keys->dk_usable >= 0);
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001110 ASSERT_CONSISTENT(mp);
Victor Stinner742da042016-09-07 17:40:12 -07001111 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001112 }
Victor Stinner742da042016-09-07 17:40:12 -07001113
Inada Naoki91234a12019-06-03 21:30:58 +09001114 if (old_value != value) {
1115 if (_PyDict_HasSplitTable(mp)) {
1116 mp->ma_values[ix] = value;
1117 if (old_value == NULL) {
1118 /* pending state */
1119 assert(ix == mp->ma_used);
1120 mp->ma_used++;
1121 }
INADA Naokiba609772016-12-07 20:41:42 +09001122 }
Inada Naoki91234a12019-06-03 21:30:58 +09001123 else {
1124 assert(old_value != NULL);
1125 DK_ENTRIES(mp->ma_keys)[ix].me_value = value;
1126 }
1127 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokiba609772016-12-07 20:41:42 +09001128 }
INADA Naokiba609772016-12-07 20:41:42 +09001129 Py_XDECREF(old_value); /* which **CAN** re-enter (see issue #22653) */
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001130 ASSERT_CONSISTENT(mp);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001131 Py_DECREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001132 return 0;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001133
1134Fail:
1135 Py_DECREF(value);
1136 Py_DECREF(key);
1137 return -1;
Antoine Pitroue965d972012-02-27 00:45:12 +01001138}
1139
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001140// Same to insertdict but specialized for ma_keys = Py_EMPTY_KEYS.
1141static int
1142insert_to_emptydict(PyDictObject *mp, PyObject *key, Py_hash_t hash,
1143 PyObject *value)
1144{
1145 assert(mp->ma_keys == Py_EMPTY_KEYS);
1146
1147 PyDictKeysObject *newkeys = new_keys_object(PyDict_MINSIZE);
1148 if (newkeys == NULL) {
1149 return -1;
1150 }
1151 if (!PyUnicode_CheckExact(key)) {
1152 newkeys->dk_lookup = lookdict;
1153 }
1154 dictkeys_decref(Py_EMPTY_KEYS);
1155 mp->ma_keys = newkeys;
1156 mp->ma_values = NULL;
1157
1158 Py_INCREF(key);
1159 Py_INCREF(value);
1160 MAINTAIN_TRACKING(mp, key, value);
1161
1162 size_t hashpos = (size_t)hash & (PyDict_MINSIZE-1);
Dong-hee Nac39d1dd2019-10-11 17:43:11 +09001163 PyDictKeyEntry *ep = DK_ENTRIES(mp->ma_keys);
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001164 dictkeys_set_index(mp->ma_keys, hashpos, 0);
1165 ep->me_key = key;
1166 ep->me_hash = hash;
1167 ep->me_value = value;
1168 mp->ma_used++;
1169 mp->ma_version_tag = DICT_NEXT_VERSION();
1170 mp->ma_keys->dk_usable--;
1171 mp->ma_keys->dk_nentries++;
1172 return 0;
1173}
1174
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001175/*
luzpaza5293b42017-11-05 07:37:50 -06001176Internal routine used by dictresize() to build a hashtable of entries.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001177*/
1178static void
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001179build_indices(PyDictKeysObject *keys, PyDictKeyEntry *ep, Py_ssize_t n)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001180{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001181 size_t mask = (size_t)DK_SIZE(keys) - 1;
1182 for (Py_ssize_t ix = 0; ix != n; ix++, ep++) {
1183 Py_hash_t hash = ep->me_hash;
1184 size_t i = hash & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001185 for (size_t perturb = hash; dictkeys_get_index(keys, i) != DKIX_EMPTY;) {
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001186 perturb >>= PERTURB_SHIFT;
INADA Naoki870c2862017-06-24 09:03:19 +09001187 i = mask & (i*5 + perturb + 1);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001188 }
INADA Naokia7576492018-11-14 18:39:27 +09001189 dictkeys_set_index(keys, i, ix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001191}
1192
1193/*
1194Restructure the table by allocating a new table and reinserting all
1195items again. When entries have been deleted, the new table may
1196actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001197If a table is split (its keys and hashes are shared, its values are not),
1198then the values are temporarily copied into the table, it is resized as
1199a combined table, then the me_value slots in the old table are NULLed out.
1200After resizing a table is always combined,
1201but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001202*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001203static int
Victor Stinner3d3f2642016-12-15 17:21:23 +01001204dictresize(PyDictObject *mp, Py_ssize_t minsize)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001205{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001206 Py_ssize_t newsize, numentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001207 PyDictKeysObject *oldkeys;
1208 PyObject **oldvalues;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001209 PyDictKeyEntry *oldentries, *newentries;
Tim Peters91a364d2001-05-19 07:04:38 +00001210
Victor Stinner742da042016-09-07 17:40:12 -07001211 /* Find the smallest table size > minused. */
1212 for (newsize = PyDict_MINSIZE;
Victor Stinner3d3f2642016-12-15 17:21:23 +01001213 newsize < minsize && newsize > 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 newsize <<= 1)
1215 ;
1216 if (newsize <= 0) {
1217 PyErr_NoMemory();
1218 return -1;
1219 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001220
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001221 oldkeys = mp->ma_keys;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001222
1223 /* NOTE: Current odict checks mp->ma_keys to detect resize happen.
1224 * So we can't reuse oldkeys even if oldkeys->dk_size == newsize.
1225 * TODO: Try reusing oldkeys when reimplement odict.
1226 */
1227
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001228 /* Allocate a new table. */
1229 mp->ma_keys = new_keys_object(newsize);
1230 if (mp->ma_keys == NULL) {
1231 mp->ma_keys = oldkeys;
1232 return -1;
1233 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01001234 // New table must be large enough.
1235 assert(mp->ma_keys->dk_usable >= mp->ma_used);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001236 if (oldkeys->dk_lookup == lookdict)
1237 mp->ma_keys->dk_lookup = lookdict;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001238
1239 numentries = mp->ma_used;
1240 oldentries = DK_ENTRIES(oldkeys);
1241 newentries = DK_ENTRIES(mp->ma_keys);
1242 oldvalues = mp->ma_values;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001243 if (oldvalues != NULL) {
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001244 /* Convert split table into new combined table.
1245 * We must incref keys; we can transfer values.
1246 * Note that values of split table is always dense.
1247 */
1248 for (Py_ssize_t i = 0; i < numentries; i++) {
1249 assert(oldvalues[i] != NULL);
1250 PyDictKeyEntry *ep = &oldentries[i];
1251 PyObject *key = ep->me_key;
1252 Py_INCREF(key);
1253 newentries[i].me_key = key;
1254 newentries[i].me_hash = ep->me_hash;
1255 newentries[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001257
INADA Naokia7576492018-11-14 18:39:27 +09001258 dictkeys_decref(oldkeys);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001259 mp->ma_values = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001260 if (oldvalues != empty_values) {
1261 free_values(oldvalues);
1262 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001263 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001264 else { // combined table.
1265 if (oldkeys->dk_nentries == numentries) {
1266 memcpy(newentries, oldentries, numentries * sizeof(PyDictKeyEntry));
1267 }
1268 else {
1269 PyDictKeyEntry *ep = oldentries;
1270 for (Py_ssize_t i = 0; i < numentries; i++) {
1271 while (ep->me_value == NULL)
1272 ep++;
1273 newentries[i] = *ep++;
1274 }
1275 }
1276
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001277 assert(oldkeys->dk_lookup != lookdict_split);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001278 assert(oldkeys->dk_refcnt == 1);
Victor Stinner49932fe2020-02-03 17:55:05 +01001279#ifdef Py_REF_DEBUG
1280 _Py_RefTotal--;
1281#endif
Victor Stinnerb4b53862020-05-05 19:55:29 +02001282#if PyDict_MAXFREELIST > 0
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001283 if (oldkeys->dk_size == PyDict_MINSIZE &&
Victor Stinner49932fe2020-02-03 17:55:05 +01001284 numfreekeys < PyDict_MAXFREELIST)
1285 {
INADA Naokia7576492018-11-14 18:39:27 +09001286 keys_free_list[numfreekeys++] = oldkeys;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001287 }
Victor Stinnerb4b53862020-05-05 19:55:29 +02001288 else
1289#endif
1290 {
INADA Naokia7576492018-11-14 18:39:27 +09001291 PyObject_FREE(oldkeys);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001292 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001293 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001294
1295 build_indices(mp->ma_keys, newentries, numentries);
1296 mp->ma_keys->dk_usable -= numentries;
1297 mp->ma_keys->dk_nentries = numentries;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001299}
1300
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001301/* Returns NULL if unable to split table.
1302 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001303static PyDictKeysObject *
1304make_keys_shared(PyObject *op)
1305{
1306 Py_ssize_t i;
1307 Py_ssize_t size;
1308 PyDictObject *mp = (PyDictObject *)op;
1309
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001310 if (!PyDict_CheckExact(op))
1311 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001312 if (!_PyDict_HasSplitTable(mp)) {
1313 PyDictKeyEntry *ep0;
1314 PyObject **values;
1315 assert(mp->ma_keys->dk_refcnt == 1);
1316 if (mp->ma_keys->dk_lookup == lookdict) {
1317 return NULL;
1318 }
1319 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
1320 /* Remove dummy keys */
1321 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
1322 return NULL;
1323 }
1324 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
1325 /* Copy values into a new array */
Victor Stinner742da042016-09-07 17:40:12 -07001326 ep0 = DK_ENTRIES(mp->ma_keys);
1327 size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001328 values = new_values(size);
1329 if (values == NULL) {
1330 PyErr_SetString(PyExc_MemoryError,
1331 "Not enough memory to allocate new values array");
1332 return NULL;
1333 }
1334 for (i = 0; i < size; i++) {
1335 values[i] = ep0[i].me_value;
1336 ep0[i].me_value = NULL;
1337 }
1338 mp->ma_keys->dk_lookup = lookdict_split;
1339 mp->ma_values = values;
1340 }
INADA Naokia7576492018-11-14 18:39:27 +09001341 dictkeys_incref(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001342 return mp->ma_keys;
1343}
Christian Heimes99170a52007-12-19 02:07:34 +00001344
1345PyObject *
1346_PyDict_NewPresized(Py_ssize_t minused)
1347{
INADA Naoki92c50ee2016-11-22 00:57:02 +09001348 const Py_ssize_t max_presize = 128 * 1024;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001349 Py_ssize_t newsize;
1350 PyDictKeysObject *new_keys;
INADA Naoki92c50ee2016-11-22 00:57:02 +09001351
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001352 if (minused <= USABLE_FRACTION(PyDict_MINSIZE)) {
Inada Naokif2a18672019-03-12 17:25:44 +09001353 return PyDict_New();
1354 }
INADA Naoki92c50ee2016-11-22 00:57:02 +09001355 /* There are no strict guarantee that returned dict can contain minused
1356 * items without resize. So we create medium size dict instead of very
1357 * large dict or MemoryError.
1358 */
1359 if (minused > USABLE_FRACTION(max_presize)) {
1360 newsize = max_presize;
1361 }
1362 else {
1363 Py_ssize_t minsize = ESTIMATE_SIZE(minused);
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001364 newsize = PyDict_MINSIZE*2;
INADA Naoki92c50ee2016-11-22 00:57:02 +09001365 while (newsize < minsize) {
1366 newsize <<= 1;
1367 }
1368 }
1369 assert(IS_POWER_OF_2(newsize));
1370
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001371 new_keys = new_keys_object(newsize);
1372 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001374 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001375}
1376
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001377/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1378 * that may occur (originally dicts supported only string keys, and exceptions
1379 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001380 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001381 * (suppressed) occurred while computing the key's hash, or that some error
1382 * (suppressed) occurred when comparing keys in the dict's internal probe
1383 * sequence. A nasty example of the latter is when a Python-coded comparison
1384 * function hits a stack-depth error, which can cause this to return NULL
1385 * even if the key is present.
1386 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001387PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001388PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001389{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001390 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07001391 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 PyDictObject *mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 PyThreadState *tstate;
INADA Naokiba609772016-12-07 20:41:42 +09001394 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 if (!PyDict_Check(op))
1397 return NULL;
1398 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001399 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 {
1401 hash = PyObject_Hash(key);
1402 if (hash == -1) {
1403 PyErr_Clear();
1404 return NULL;
1405 }
1406 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 /* We can arrive here with a NULL tstate during initialization: try
1409 running "python -Wi" for an example related to string interning.
1410 Let's just hope that no exception occurs then... This must be
Victor Stinner50b48572018-11-01 01:51:40 +01001411 _PyThreadState_GET() and not PyThreadState_Get() because the latter
Victor Stinner9204fb82018-10-30 15:13:17 +01001412 abort Python if tstate is NULL. */
Victor Stinner50b48572018-11-01 01:51:40 +01001413 tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001414 if (tstate != NULL && tstate->curexc_type != NULL) {
1415 /* preserve the existing exception */
1416 PyObject *err_type, *err_value, *err_tb;
1417 PyErr_Fetch(&err_type, &err_value, &err_tb);
INADA Naoki778928b2017-08-03 23:45:15 +09001418 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 /* ignore errors */
1420 PyErr_Restore(err_type, err_value, err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001421 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 return NULL;
1423 }
1424 else {
INADA Naoki778928b2017-08-03 23:45:15 +09001425 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001426 if (ix < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 PyErr_Clear();
1428 return NULL;
1429 }
1430 }
INADA Naokiba609772016-12-07 20:41:42 +09001431 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001432}
1433
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001434/* Same as PyDict_GetItemWithError() but with hash supplied by caller.
1435 This returns NULL *with* an exception set if an exception occurred.
1436 It returns NULL *without* an exception set if the key wasn't present.
1437*/
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001438PyObject *
1439_PyDict_GetItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1440{
Victor Stinner742da042016-09-07 17:40:12 -07001441 Py_ssize_t ix;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001442 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001443 PyObject *value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001444
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001445 if (!PyDict_Check(op)) {
1446 PyErr_BadInternalCall();
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001447 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001448 }
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001449
INADA Naoki778928b2017-08-03 23:45:15 +09001450 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001451 if (ix < 0) {
1452 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001453 }
INADA Naokiba609772016-12-07 20:41:42 +09001454 return value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001455}
1456
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001457/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1458 This returns NULL *with* an exception set if an exception occurred.
1459 It returns NULL *without* an exception set if the key wasn't present.
1460*/
1461PyObject *
1462PyDict_GetItemWithError(PyObject *op, PyObject *key)
1463{
Victor Stinner742da042016-09-07 17:40:12 -07001464 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001465 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001466 PyDictObject*mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001467 PyObject *value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 if (!PyDict_Check(op)) {
1470 PyErr_BadInternalCall();
1471 return NULL;
1472 }
1473 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001474 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475 {
1476 hash = PyObject_Hash(key);
1477 if (hash == -1) {
1478 return NULL;
1479 }
1480 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001481
INADA Naoki778928b2017-08-03 23:45:15 +09001482 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001483 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001485 return value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001486}
1487
Brett Cannonfd074152012-04-14 14:10:13 -04001488PyObject *
1489_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1490{
1491 PyObject *kv;
1492 kv = _PyUnicode_FromId(key); /* borrowed */
1493 if (kv == NULL)
1494 return NULL;
1495 return PyDict_GetItemWithError(dp, kv);
1496}
1497
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001498PyObject *
1499_PyDict_GetItemStringWithError(PyObject *v, const char *key)
1500{
1501 PyObject *kv, *rv;
1502 kv = PyUnicode_FromString(key);
1503 if (kv == NULL) {
1504 return NULL;
1505 }
1506 rv = PyDict_GetItemWithError(v, kv);
1507 Py_DECREF(kv);
1508 return rv;
1509}
1510
Victor Stinnerb4efc962015-11-20 09:24:02 +01001511/* Fast version of global value lookup (LOAD_GLOBAL).
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001512 * Lookup in globals, then builtins.
Victor Stinnerb4efc962015-11-20 09:24:02 +01001513 *
1514 * Raise an exception and return NULL if an error occurred (ex: computing the
1515 * key hash failed, key comparison failed, ...). Return NULL if the key doesn't
1516 * exist. Return the value if the key exists.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001517 */
1518PyObject *
1519_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001520{
Victor Stinner742da042016-09-07 17:40:12 -07001521 Py_ssize_t ix;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001522 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09001523 PyObject *value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001524
1525 if (!PyUnicode_CheckExact(key) ||
1526 (hash = ((PyASCIIObject *) key)->hash) == -1)
1527 {
1528 hash = PyObject_Hash(key);
1529 if (hash == -1)
1530 return NULL;
Antoine Pitroue965d972012-02-27 00:45:12 +01001531 }
Victor Stinnerb4efc962015-11-20 09:24:02 +01001532
1533 /* namespace 1: globals */
INADA Naoki778928b2017-08-03 23:45:15 +09001534 ix = globals->ma_keys->dk_lookup(globals, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001535 if (ix == DKIX_ERROR)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001536 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001537 if (ix != DKIX_EMPTY && value != NULL)
1538 return value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001539
1540 /* namespace 2: builtins */
INADA Naoki778928b2017-08-03 23:45:15 +09001541 ix = builtins->ma_keys->dk_lookup(builtins, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001542 if (ix < 0)
Victor Stinnerb4efc962015-11-20 09:24:02 +01001543 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001544 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001545}
1546
Antoine Pitroue965d972012-02-27 00:45:12 +01001547/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1548 * dictionary if it's merely replacing the value for an existing key.
1549 * This means that it's safe to loop over a dictionary with PyDict_Next()
1550 * and occasionally replace a value -- but you can't insert new keys or
1551 * remove them.
1552 */
1553int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001554PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001555{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001556 PyDictObject *mp;
1557 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001558 if (!PyDict_Check(op)) {
1559 PyErr_BadInternalCall();
1560 return -1;
1561 }
1562 assert(key);
1563 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001564 mp = (PyDictObject *)op;
1565 if (!PyUnicode_CheckExact(key) ||
1566 (hash = ((PyASCIIObject *) key)->hash) == -1)
1567 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001568 hash = PyObject_Hash(key);
1569 if (hash == -1)
1570 return -1;
1571 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001572
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001573 if (mp->ma_keys == Py_EMPTY_KEYS) {
1574 return insert_to_emptydict(mp, key, hash, value);
1575 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001576 /* insertdict() handles any resizing that might be necessary */
1577 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001578}
1579
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001580int
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001581_PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
1582 Py_hash_t hash)
1583{
1584 PyDictObject *mp;
1585
1586 if (!PyDict_Check(op)) {
1587 PyErr_BadInternalCall();
1588 return -1;
1589 }
1590 assert(key);
1591 assert(value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001592 assert(hash != -1);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001593 mp = (PyDictObject *)op;
1594
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001595 if (mp->ma_keys == Py_EMPTY_KEYS) {
1596 return insert_to_emptydict(mp, key, hash, value);
1597 }
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001598 /* insertdict() handles any resizing that might be necessary */
1599 return insertdict(mp, key, hash, value);
1600}
1601
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001602static int
INADA Naoki778928b2017-08-03 23:45:15 +09001603delitem_common(PyDictObject *mp, Py_hash_t hash, Py_ssize_t ix,
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001604 PyObject *old_value)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001605{
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001606 PyObject *old_key;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001607 PyDictKeyEntry *ep;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001608
INADA Naoki778928b2017-08-03 23:45:15 +09001609 Py_ssize_t hashpos = lookdict_index(mp->ma_keys, hash, ix);
1610 assert(hashpos >= 0);
1611
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001612 mp->ma_used--;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001613 mp->ma_version_tag = DICT_NEXT_VERSION();
1614 ep = &DK_ENTRIES(mp->ma_keys)[ix];
INADA Naokia7576492018-11-14 18:39:27 +09001615 dictkeys_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001616 ENSURE_ALLOWS_DELETIONS(mp);
1617 old_key = ep->me_key;
1618 ep->me_key = NULL;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001619 ep->me_value = NULL;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001620 Py_DECREF(old_key);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001621 Py_DECREF(old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001622
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001623 ASSERT_CONSISTENT(mp);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001624 return 0;
1625}
1626
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001627int
Tim Peters1f5871e2000-07-04 17:44:48 +00001628PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001629{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001630 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 assert(key);
1632 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001633 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 hash = PyObject_Hash(key);
1635 if (hash == -1)
1636 return -1;
1637 }
Victor Stinner742da042016-09-07 17:40:12 -07001638
1639 return _PyDict_DelItem_KnownHash(op, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001640}
1641
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001642int
1643_PyDict_DelItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1644{
INADA Naoki778928b2017-08-03 23:45:15 +09001645 Py_ssize_t ix;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001646 PyDictObject *mp;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001647 PyObject *old_value;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001648
1649 if (!PyDict_Check(op)) {
1650 PyErr_BadInternalCall();
1651 return -1;
1652 }
1653 assert(key);
1654 assert(hash != -1);
1655 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001656 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001657 if (ix == DKIX_ERROR)
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001658 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09001659 if (ix == DKIX_EMPTY || old_value == NULL) {
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001660 _PyErr_SetKeyError(key);
1661 return -1;
1662 }
Victor Stinner78601a32016-09-09 19:28:36 -07001663
1664 // Split table doesn't allow deletion. Combine it.
1665 if (_PyDict_HasSplitTable(mp)) {
1666 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1667 return -1;
1668 }
INADA Naoki778928b2017-08-03 23:45:15 +09001669 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001670 assert(ix >= 0);
1671 }
1672
INADA Naoki778928b2017-08-03 23:45:15 +09001673 return delitem_common(mp, hash, ix, old_value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001674}
1675
Antoine Pitroud741ed42016-12-27 14:23:43 +01001676/* This function promises that the predicate -> deletion sequence is atomic
1677 * (i.e. protected by the GIL), assuming the predicate itself doesn't
1678 * release the GIL.
1679 */
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001680int
1681_PyDict_DelItemIf(PyObject *op, PyObject *key,
1682 int (*predicate)(PyObject *value))
1683{
Antoine Pitroud741ed42016-12-27 14:23:43 +01001684 Py_ssize_t hashpos, ix;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001685 PyDictObject *mp;
1686 Py_hash_t hash;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001687 PyObject *old_value;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001688 int res;
1689
1690 if (!PyDict_Check(op)) {
1691 PyErr_BadInternalCall();
1692 return -1;
1693 }
1694 assert(key);
1695 hash = PyObject_Hash(key);
1696 if (hash == -1)
1697 return -1;
1698 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001699 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001700 if (ix == DKIX_ERROR)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001701 return -1;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001702 if (ix == DKIX_EMPTY || old_value == NULL) {
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001703 _PyErr_SetKeyError(key);
1704 return -1;
1705 }
Antoine Pitroud741ed42016-12-27 14:23:43 +01001706
1707 // Split table doesn't allow deletion. Combine it.
1708 if (_PyDict_HasSplitTable(mp)) {
1709 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1710 return -1;
1711 }
INADA Naoki778928b2017-08-03 23:45:15 +09001712 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001713 assert(ix >= 0);
1714 }
1715
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001716 res = predicate(old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001717 if (res == -1)
1718 return -1;
INADA Naoki778928b2017-08-03 23:45:15 +09001719
1720 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1721 assert(hashpos >= 0);
1722
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001723 if (res > 0)
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001724 return delitem_common(mp, hashpos, ix, old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001725 else
1726 return 0;
1727}
1728
1729
Guido van Rossum25831651993-05-19 14:50:45 +00001730void
Tim Peters1f5871e2000-07-04 17:44:48 +00001731PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001732{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001734 PyDictKeysObject *oldkeys;
1735 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001736 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001737
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001738 if (!PyDict_Check(op))
1739 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001740 mp = ((PyDictObject *)op);
1741 oldkeys = mp->ma_keys;
1742 oldvalues = mp->ma_values;
1743 if (oldvalues == empty_values)
1744 return;
1745 /* Empty the dict... */
INADA Naokia7576492018-11-14 18:39:27 +09001746 dictkeys_incref(Py_EMPTY_KEYS);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001747 mp->ma_keys = Py_EMPTY_KEYS;
1748 mp->ma_values = empty_values;
1749 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001750 mp->ma_version_tag = DICT_NEXT_VERSION();
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001751 /* ...then clear the keys and values */
1752 if (oldvalues != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001753 n = oldkeys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001754 for (i = 0; i < n; i++)
1755 Py_CLEAR(oldvalues[i]);
1756 free_values(oldvalues);
INADA Naokia7576492018-11-14 18:39:27 +09001757 dictkeys_decref(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001758 }
1759 else {
1760 assert(oldkeys->dk_refcnt == 1);
INADA Naokia7576492018-11-14 18:39:27 +09001761 dictkeys_decref(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001762 }
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001763 ASSERT_CONSISTENT(mp);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001764}
1765
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001766/* Internal version of PyDict_Next that returns a hash value in addition
1767 * to the key and value.
1768 * Return 1 on success, return 0 when the reached the end of the dictionary
1769 * (or if op is not a dictionary)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001770 */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001771int
1772_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1773 PyObject **pvalue, Py_hash_t *phash)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001774{
INADA Naokica2d8be2016-11-04 16:59:10 +09001775 Py_ssize_t i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001776 PyDictObject *mp;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001777 PyDictKeyEntry *entry_ptr;
1778 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001779
1780 if (!PyDict_Check(op))
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001781 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001782 mp = (PyDictObject *)op;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001783 i = *ppos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001784 if (mp->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09001785 if (i < 0 || i >= mp->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001786 return 0;
INADA Naokica2d8be2016-11-04 16:59:10 +09001787 /* values of split table is always dense */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001788 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
INADA Naokica2d8be2016-11-04 16:59:10 +09001789 value = mp->ma_values[i];
1790 assert(value != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001792 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09001793 Py_ssize_t n = mp->ma_keys->dk_nentries;
1794 if (i < 0 || i >= n)
1795 return 0;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001796 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
1797 while (i < n && entry_ptr->me_value == NULL) {
1798 entry_ptr++;
1799 i++;
Victor Stinner742da042016-09-07 17:40:12 -07001800 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001801 if (i >= n)
1802 return 0;
1803 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001804 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001805 *ppos = i+1;
1806 if (pkey)
1807 *pkey = entry_ptr->me_key;
1808 if (phash)
1809 *phash = entry_ptr->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001810 if (pvalue)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001811 *pvalue = value;
1812 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001813}
1814
Tim Peters080c88b2003-02-15 03:01:11 +00001815/*
1816 * Iterate over a dict. Use like so:
1817 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001818 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001819 * PyObject *key, *value;
1820 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001821 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001822 * Refer to borrowed references in key and value.
Tim Peters080c88b2003-02-15 03:01:11 +00001823 * }
1824 *
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001825 * Return 1 on success, return 0 when the reached the end of the dictionary
1826 * (or if op is not a dictionary)
1827 *
Tim Peters080c88b2003-02-15 03:01:11 +00001828 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001829 * mutates the dict. One exception: it is safe if the loop merely changes
1830 * the values associated with the keys (but doesn't insert new keys or
1831 * delete keys), via PyDict_SetItem().
1832 */
Guido van Rossum25831651993-05-19 14:50:45 +00001833int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001834PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001835{
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001836 return _PyDict_Next(op, ppos, pkey, pvalue, NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001837}
1838
Eric Snow96c6af92015-05-29 22:21:39 -06001839/* Internal version of dict.pop(). */
1840PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001841_PyDict_Pop_KnownHash(PyObject *dict, PyObject *key, Py_hash_t hash, PyObject *deflt)
Eric Snow96c6af92015-05-29 22:21:39 -06001842{
Victor Stinner742da042016-09-07 17:40:12 -07001843 Py_ssize_t ix, hashpos;
Eric Snow96c6af92015-05-29 22:21:39 -06001844 PyObject *old_value, *old_key;
1845 PyDictKeyEntry *ep;
Yury Selivanov684ef2c2016-10-28 19:01:21 -04001846 PyDictObject *mp;
1847
1848 assert(PyDict_Check(dict));
1849 mp = (PyDictObject *)dict;
Eric Snow96c6af92015-05-29 22:21:39 -06001850
1851 if (mp->ma_used == 0) {
1852 if (deflt) {
1853 Py_INCREF(deflt);
1854 return deflt;
1855 }
1856 _PyErr_SetKeyError(key);
1857 return NULL;
1858 }
INADA Naoki778928b2017-08-03 23:45:15 +09001859 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001860 if (ix == DKIX_ERROR)
Eric Snow96c6af92015-05-29 22:21:39 -06001861 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001862 if (ix == DKIX_EMPTY || old_value == NULL) {
Eric Snow96c6af92015-05-29 22:21:39 -06001863 if (deflt) {
1864 Py_INCREF(deflt);
1865 return deflt;
1866 }
1867 _PyErr_SetKeyError(key);
1868 return NULL;
1869 }
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001870
Victor Stinner78601a32016-09-09 19:28:36 -07001871 // Split table doesn't allow deletion. Combine it.
1872 if (_PyDict_HasSplitTable(mp)) {
1873 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1874 return NULL;
1875 }
INADA Naoki778928b2017-08-03 23:45:15 +09001876 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001877 assert(ix >= 0);
1878 }
1879
INADA Naoki778928b2017-08-03 23:45:15 +09001880 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1881 assert(hashpos >= 0);
Victor Stinner78601a32016-09-09 19:28:36 -07001882 assert(old_value != NULL);
Eric Snow96c6af92015-05-29 22:21:39 -06001883 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001884 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokia7576492018-11-14 18:39:27 +09001885 dictkeys_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
Victor Stinner78601a32016-09-09 19:28:36 -07001886 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1887 ENSURE_ALLOWS_DELETIONS(mp);
1888 old_key = ep->me_key;
1889 ep->me_key = NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001890 ep->me_value = NULL;
Victor Stinner78601a32016-09-09 19:28:36 -07001891 Py_DECREF(old_key);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001892
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001893 ASSERT_CONSISTENT(mp);
Eric Snow96c6af92015-05-29 22:21:39 -06001894 return old_value;
1895}
1896
Serhiy Storchaka67796522017-01-12 18:34:33 +02001897PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001898_PyDict_Pop(PyObject *dict, PyObject *key, PyObject *deflt)
Serhiy Storchaka67796522017-01-12 18:34:33 +02001899{
1900 Py_hash_t hash;
1901
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001902 if (((PyDictObject *)dict)->ma_used == 0) {
Serhiy Storchaka67796522017-01-12 18:34:33 +02001903 if (deflt) {
1904 Py_INCREF(deflt);
1905 return deflt;
1906 }
1907 _PyErr_SetKeyError(key);
1908 return NULL;
1909 }
1910 if (!PyUnicode_CheckExact(key) ||
1911 (hash = ((PyASCIIObject *) key)->hash) == -1) {
1912 hash = PyObject_Hash(key);
1913 if (hash == -1)
1914 return NULL;
1915 }
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001916 return _PyDict_Pop_KnownHash(dict, key, hash, deflt);
Serhiy Storchaka67796522017-01-12 18:34:33 +02001917}
1918
Eric Snow96c6af92015-05-29 22:21:39 -06001919/* Internal version of dict.from_keys(). It is subclass-friendly. */
1920PyObject *
1921_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
1922{
1923 PyObject *it; /* iter(iterable) */
1924 PyObject *key;
1925 PyObject *d;
1926 int status;
1927
Victor Stinnera5ed5f02016-12-06 18:45:50 +01001928 d = _PyObject_CallNoArg(cls);
Eric Snow96c6af92015-05-29 22:21:39 -06001929 if (d == NULL)
1930 return NULL;
1931
1932 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
1933 if (PyDict_CheckExact(iterable)) {
1934 PyDictObject *mp = (PyDictObject *)d;
1935 PyObject *oldvalue;
1936 Py_ssize_t pos = 0;
1937 PyObject *key;
1938 Py_hash_t hash;
1939
Serhiy Storchakac61ac162017-03-21 08:52:38 +02001940 if (dictresize(mp, ESTIMATE_SIZE(PyDict_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001941 Py_DECREF(d);
1942 return NULL;
1943 }
1944
1945 while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
1946 if (insertdict(mp, key, hash, value)) {
1947 Py_DECREF(d);
1948 return NULL;
1949 }
1950 }
1951 return d;
1952 }
1953 if (PyAnySet_CheckExact(iterable)) {
1954 PyDictObject *mp = (PyDictObject *)d;
1955 Py_ssize_t pos = 0;
1956 PyObject *key;
1957 Py_hash_t hash;
1958
Victor Stinner742da042016-09-07 17:40:12 -07001959 if (dictresize(mp, ESTIMATE_SIZE(PySet_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001960 Py_DECREF(d);
1961 return NULL;
1962 }
1963
1964 while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
1965 if (insertdict(mp, key, hash, value)) {
1966 Py_DECREF(d);
1967 return NULL;
1968 }
1969 }
1970 return d;
1971 }
1972 }
1973
1974 it = PyObject_GetIter(iterable);
1975 if (it == NULL){
1976 Py_DECREF(d);
1977 return NULL;
1978 }
1979
1980 if (PyDict_CheckExact(d)) {
1981 while ((key = PyIter_Next(it)) != NULL) {
1982 status = PyDict_SetItem(d, key, value);
1983 Py_DECREF(key);
1984 if (status < 0)
1985 goto Fail;
1986 }
1987 } else {
1988 while ((key = PyIter_Next(it)) != NULL) {
1989 status = PyObject_SetItem(d, key, value);
1990 Py_DECREF(key);
1991 if (status < 0)
1992 goto Fail;
1993 }
1994 }
1995
1996 if (PyErr_Occurred())
1997 goto Fail;
1998 Py_DECREF(it);
1999 return d;
2000
2001Fail:
2002 Py_DECREF(it);
2003 Py_DECREF(d);
2004 return NULL;
2005}
2006
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002007/* Methods */
2008
2009static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002010dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002011{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002012 PyObject **values = mp->ma_values;
2013 PyDictKeysObject *keys = mp->ma_keys;
2014 Py_ssize_t i, n;
INADA Naokia6296d32017-08-24 14:55:17 +09002015
2016 /* bpo-31095: UnTrack is needed before calling any callbacks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002017 PyObject_GC_UnTrack(mp);
Jeroen Demeyer351c6742019-05-10 19:21:11 +02002018 Py_TRASHCAN_BEGIN(mp, dict_dealloc)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002019 if (values != NULL) {
2020 if (values != empty_values) {
Victor Stinner742da042016-09-07 17:40:12 -07002021 for (i = 0, n = mp->ma_keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002022 Py_XDECREF(values[i]);
2023 }
2024 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 }
INADA Naokia7576492018-11-14 18:39:27 +09002026 dictkeys_decref(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02002028 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02002029 assert(keys->dk_refcnt == 1);
INADA Naokia7576492018-11-14 18:39:27 +09002030 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002031 }
Victor Stinnerb4b53862020-05-05 19:55:29 +02002032#if PyDict_MAXFREELIST > 0
2033 if (numfree < PyDict_MAXFREELIST && Py_IS_TYPE(mp, &PyDict_Type)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002034 free_list[numfree++] = mp;
Victor Stinnerb4b53862020-05-05 19:55:29 +02002035 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002036 else
Victor Stinnerb4b53862020-05-05 19:55:29 +02002037#endif
2038 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002039 Py_TYPE(mp)->tp_free((PyObject *)mp);
Victor Stinnerb4b53862020-05-05 19:55:29 +02002040 }
Jeroen Demeyer351c6742019-05-10 19:21:11 +02002041 Py_TRASHCAN_END
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002042}
2043
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002044
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002045static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002046dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002047{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002048 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01002049 PyObject *key = NULL, *value = NULL;
2050 _PyUnicodeWriter writer;
2051 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00002052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053 i = Py_ReprEnter((PyObject *)mp);
2054 if (i != 0) {
2055 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
2056 }
Guido van Rossum255443b1998-04-10 22:47:14 +00002057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002058 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01002059 Py_ReprLeave((PyObject *)mp);
2060 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061 }
Tim Petersa7259592001-06-16 05:11:17 +00002062
Victor Stinnerf91929b2013-11-19 13:07:38 +01002063 _PyUnicodeWriter_Init(&writer);
2064 writer.overallocate = 1;
2065 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
2066 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00002067
Victor Stinnerf91929b2013-11-19 13:07:38 +01002068 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
2069 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00002070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002071 /* Do repr() on each key+value pair, and insert ": " between them.
2072 Note that repr may mutate the dict. */
2073 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01002074 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002075 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01002076 PyObject *s;
2077 int res;
2078
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002079 /* Prevent repr from deleting key or value during key format. */
2080 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002081 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02002082
Victor Stinnerf91929b2013-11-19 13:07:38 +01002083 if (!first) {
2084 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
2085 goto error;
2086 }
2087 first = 0;
2088
2089 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002090 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01002091 goto error;
2092 res = _PyUnicodeWriter_WriteStr(&writer, s);
2093 Py_DECREF(s);
2094 if (res < 0)
2095 goto error;
2096
2097 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
2098 goto error;
2099
2100 s = PyObject_Repr(value);
2101 if (s == NULL)
2102 goto error;
2103 res = _PyUnicodeWriter_WriteStr(&writer, s);
2104 Py_DECREF(s);
2105 if (res < 0)
2106 goto error;
2107
2108 Py_CLEAR(key);
2109 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002110 }
Tim Petersa7259592001-06-16 05:11:17 +00002111
Victor Stinnerf91929b2013-11-19 13:07:38 +01002112 writer.overallocate = 0;
2113 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
2114 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00002115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002116 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01002117
2118 return _PyUnicodeWriter_Finish(&writer);
2119
2120error:
2121 Py_ReprLeave((PyObject *)mp);
2122 _PyUnicodeWriter_Dealloc(&writer);
2123 Py_XDECREF(key);
2124 Py_XDECREF(value);
2125 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002126}
2127
Martin v. Löwis18e16552006-02-15 17:27:45 +00002128static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002129dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002130{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002131 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002132}
2133
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002134static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002135dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002136{
Victor Stinner742da042016-09-07 17:40:12 -07002137 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002138 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09002139 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002141 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002142 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002143 hash = PyObject_Hash(key);
2144 if (hash == -1)
2145 return NULL;
2146 }
INADA Naoki778928b2017-08-03 23:45:15 +09002147 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002148 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002149 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002150 if (ix == DKIX_EMPTY || value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002151 if (!PyDict_CheckExact(mp)) {
2152 /* Look up __missing__ method if we're a subclass. */
2153 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05002154 _Py_IDENTIFIER(__missing__);
2155 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002156 if (missing != NULL) {
Petr Viktorinffd97532020-02-11 17:46:57 +01002157 res = PyObject_CallOneArg(missing, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002158 Py_DECREF(missing);
2159 return res;
2160 }
2161 else if (PyErr_Occurred())
2162 return NULL;
2163 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07002164 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002165 return NULL;
2166 }
INADA Naokiba609772016-12-07 20:41:42 +09002167 Py_INCREF(value);
2168 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002169}
2170
2171static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002172dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002173{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002174 if (w == NULL)
2175 return PyDict_DelItem((PyObject *)mp, v);
2176 else
2177 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002178}
2179
Guido van Rossuma9e7a811997-05-13 21:02:11 +00002180static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002181 (lenfunc)dict_length, /*mp_length*/
2182 (binaryfunc)dict_subscript, /*mp_subscript*/
2183 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002184};
2185
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002186static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002187dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002188{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002189 PyObject *v;
2190 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002191 PyDictKeyEntry *ep;
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002192 Py_ssize_t n, offset;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002193 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002194
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002195 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002196 n = mp->ma_used;
2197 v = PyList_New(n);
2198 if (v == NULL)
2199 return NULL;
2200 if (n != mp->ma_used) {
2201 /* Durnit. The allocations caused the dict to resize.
2202 * Just start over, this shouldn't normally happen.
2203 */
2204 Py_DECREF(v);
2205 goto again;
2206 }
Victor Stinner742da042016-09-07 17:40:12 -07002207 ep = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002208 if (mp->ma_values) {
2209 value_ptr = mp->ma_values;
2210 offset = sizeof(PyObject *);
2211 }
2212 else {
2213 value_ptr = &ep[0].me_value;
2214 offset = sizeof(PyDictKeyEntry);
2215 }
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002216 for (i = 0, j = 0; j < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002217 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002218 PyObject *key = ep[i].me_key;
2219 Py_INCREF(key);
2220 PyList_SET_ITEM(v, j, key);
2221 j++;
2222 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002223 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002224 }
2225 assert(j == n);
2226 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002227}
2228
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002229static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002230dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002231{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002232 PyObject *v;
2233 Py_ssize_t i, j;
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002234 PyDictKeyEntry *ep;
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002235 Py_ssize_t n, offset;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002236 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002237
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002238 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002239 n = mp->ma_used;
2240 v = PyList_New(n);
2241 if (v == NULL)
2242 return NULL;
2243 if (n != mp->ma_used) {
2244 /* Durnit. The allocations caused the dict to resize.
2245 * Just start over, this shouldn't normally happen.
2246 */
2247 Py_DECREF(v);
2248 goto again;
2249 }
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002250 ep = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002251 if (mp->ma_values) {
2252 value_ptr = mp->ma_values;
2253 offset = sizeof(PyObject *);
2254 }
2255 else {
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002256 value_ptr = &ep[0].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002257 offset = sizeof(PyDictKeyEntry);
2258 }
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002259 for (i = 0, j = 0; j < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002260 PyObject *value = *value_ptr;
2261 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2262 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002263 Py_INCREF(value);
2264 PyList_SET_ITEM(v, j, value);
2265 j++;
2266 }
2267 }
2268 assert(j == n);
2269 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002270}
2271
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002272static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002273dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002274{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002275 PyObject *v;
2276 Py_ssize_t i, j, n;
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002277 Py_ssize_t offset;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002278 PyObject *item, *key;
2279 PyDictKeyEntry *ep;
2280 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002282 /* Preallocate the list of tuples, to avoid allocations during
2283 * the loop over the items, which could trigger GC, which
2284 * could resize the dict. :-(
2285 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002286 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002287 n = mp->ma_used;
2288 v = PyList_New(n);
2289 if (v == NULL)
2290 return NULL;
2291 for (i = 0; i < n; i++) {
2292 item = PyTuple_New(2);
2293 if (item == NULL) {
2294 Py_DECREF(v);
2295 return NULL;
2296 }
2297 PyList_SET_ITEM(v, i, item);
2298 }
2299 if (n != mp->ma_used) {
2300 /* Durnit. The allocations caused the dict to resize.
2301 * Just start over, this shouldn't normally happen.
2302 */
2303 Py_DECREF(v);
2304 goto again;
2305 }
2306 /* Nothing we do below makes any function calls. */
Victor Stinner742da042016-09-07 17:40:12 -07002307 ep = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002308 if (mp->ma_values) {
2309 value_ptr = mp->ma_values;
2310 offset = sizeof(PyObject *);
2311 }
2312 else {
2313 value_ptr = &ep[0].me_value;
2314 offset = sizeof(PyDictKeyEntry);
2315 }
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002316 for (i = 0, j = 0; j < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002317 PyObject *value = *value_ptr;
2318 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2319 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002320 key = ep[i].me_key;
2321 item = PyList_GET_ITEM(v, j);
2322 Py_INCREF(key);
2323 PyTuple_SET_ITEM(item, 0, key);
2324 Py_INCREF(value);
2325 PyTuple_SET_ITEM(item, 1, value);
2326 j++;
2327 }
2328 }
2329 assert(j == n);
2330 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002331}
2332
Larry Hastings5c661892014-01-24 06:17:25 -08002333/*[clinic input]
2334@classmethod
2335dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08002336 iterable: object
2337 value: object=None
2338 /
2339
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002340Create a new dictionary with keys from iterable and values set to value.
Larry Hastings5c661892014-01-24 06:17:25 -08002341[clinic start generated code]*/
2342
Larry Hastings5c661892014-01-24 06:17:25 -08002343static PyObject *
2344dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002345/*[clinic end generated code: output=8fb98e4b10384999 input=382ba4855d0f74c3]*/
Larry Hastings5c661892014-01-24 06:17:25 -08002346{
Eric Snow96c6af92015-05-29 22:21:39 -06002347 return _PyDict_FromKeys((PyObject *)type, iterable, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002348}
2349
Brandt Buchereb8ac572020-02-24 19:47:34 -08002350/* Single-arg dict update; used by dict_update_common and operators. */
2351static int
2352dict_update_arg(PyObject *self, PyObject *arg)
2353{
2354 if (PyDict_CheckExact(arg)) {
2355 return PyDict_Merge(self, arg, 1);
2356 }
2357 _Py_IDENTIFIER(keys);
2358 PyObject *func;
2359 if (_PyObject_LookupAttrId(arg, &PyId_keys, &func) < 0) {
2360 return -1;
2361 }
2362 if (func != NULL) {
2363 Py_DECREF(func);
2364 return PyDict_Merge(self, arg, 1);
2365 }
2366 return PyDict_MergeFromSeq2(self, arg, 1);
2367}
2368
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002369static int
Victor Stinner742da042016-09-07 17:40:12 -07002370dict_update_common(PyObject *self, PyObject *args, PyObject *kwds,
2371 const char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002372{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002373 PyObject *arg = NULL;
2374 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002375
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002376 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002377 result = -1;
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002378 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379 else if (arg != NULL) {
Brandt Buchereb8ac572020-02-24 19:47:34 -08002380 result = dict_update_arg(self, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002381 }
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 if (result == 0 && kwds != NULL) {
2384 if (PyArg_ValidateKeywordArguments(kwds))
2385 result = PyDict_Merge(self, kwds, 1);
2386 else
2387 result = -1;
2388 }
2389 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002390}
2391
Victor Stinner91f0d4a2017-01-19 12:45:06 +01002392/* Note: dict.update() uses the METH_VARARGS|METH_KEYWORDS calling convention.
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +03002393 Using METH_FASTCALL|METH_KEYWORDS would make dict.update(**dict2) calls
2394 slower, see the issue #29312. */
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002395static PyObject *
2396dict_update(PyObject *self, PyObject *args, PyObject *kwds)
2397{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398 if (dict_update_common(self, args, kwds, "update") != -1)
2399 Py_RETURN_NONE;
2400 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002401}
2402
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002403/* Update unconditionally replaces existing items.
2404 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00002405 otherwise it leaves existing items unchanged.
2406
2407 PyDict_{Update,Merge} update/merge from a mapping object.
2408
Tim Petersf582b822001-12-11 18:51:08 +00002409 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00002410 producing iterable objects of length 2.
2411*/
2412
Tim Petersf582b822001-12-11 18:51:08 +00002413int
Tim Peters1fc240e2001-10-26 05:06:50 +00002414PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
2415{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 PyObject *it; /* iter(seq2) */
2417 Py_ssize_t i; /* index into seq2 of current element */
2418 PyObject *item; /* seq2[i] */
2419 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00002420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002421 assert(d != NULL);
2422 assert(PyDict_Check(d));
2423 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00002424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002425 it = PyObject_GetIter(seq2);
2426 if (it == NULL)
2427 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 for (i = 0; ; ++i) {
2430 PyObject *key, *value;
2431 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00002432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002433 fast = NULL;
2434 item = PyIter_Next(it);
2435 if (item == NULL) {
2436 if (PyErr_Occurred())
2437 goto Fail;
2438 break;
2439 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002441 /* Convert item to sequence, and verify length 2. */
2442 fast = PySequence_Fast(item, "");
2443 if (fast == NULL) {
2444 if (PyErr_ExceptionMatches(PyExc_TypeError))
2445 PyErr_Format(PyExc_TypeError,
2446 "cannot convert dictionary update "
2447 "sequence element #%zd to a sequence",
2448 i);
2449 goto Fail;
2450 }
2451 n = PySequence_Fast_GET_SIZE(fast);
2452 if (n != 2) {
2453 PyErr_Format(PyExc_ValueError,
2454 "dictionary update sequence element #%zd "
2455 "has length %zd; 2 is required",
2456 i, n);
2457 goto Fail;
2458 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002460 /* Update/merge with this (key, value) pair. */
2461 key = PySequence_Fast_GET_ITEM(fast, 0);
2462 value = PySequence_Fast_GET_ITEM(fast, 1);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002463 Py_INCREF(key);
2464 Py_INCREF(value);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002465 if (override) {
2466 if (PyDict_SetItem(d, key, value) < 0) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002467 Py_DECREF(key);
2468 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002469 goto Fail;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002470 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002471 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002472 else if (PyDict_GetItemWithError(d, key) == NULL) {
2473 if (PyErr_Occurred() || PyDict_SetItem(d, key, value) < 0) {
2474 Py_DECREF(key);
2475 Py_DECREF(value);
2476 goto Fail;
2477 }
2478 }
2479
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002480 Py_DECREF(key);
2481 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 Py_DECREF(fast);
2483 Py_DECREF(item);
2484 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002486 i = 0;
Victor Stinner0fc91ee2019-04-12 21:51:34 +02002487 ASSERT_CONSISTENT(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002488 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00002489Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002490 Py_XDECREF(item);
2491 Py_XDECREF(fast);
2492 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002493Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002494 Py_DECREF(it);
2495 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00002496}
2497
doko@ubuntu.comc96df682016-10-11 08:04:02 +02002498static int
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002499dict_merge(PyObject *a, PyObject *b, int override)
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002500{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002501 PyDictObject *mp, *other;
2502 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -07002503 PyDictKeyEntry *entry, *ep0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002504
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002505 assert(0 <= override && override <= 2);
2506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002507 /* We accept for the argument either a concrete dictionary object,
2508 * or an abstract "mapping" object. For the former, we can do
2509 * things quite efficiently. For the latter, we only require that
2510 * PyMapping_Keys() and PyObject_GetItem() be supported.
2511 */
2512 if (a == NULL || !PyDict_Check(a) || b == NULL) {
2513 PyErr_BadInternalCall();
2514 return -1;
2515 }
2516 mp = (PyDictObject*)a;
INADA Naoki2aaf98c2018-09-26 12:59:00 +09002517 if (PyDict_Check(b) && (Py_TYPE(b)->tp_iter == (getiterfunc)dict_iter)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002518 other = (PyDictObject*)b;
2519 if (other == mp || other->ma_used == 0)
2520 /* a.update(a) or a.update({}); nothing to do */
2521 return 0;
2522 if (mp->ma_used == 0)
2523 /* Since the target dict is empty, PyDict_GetItem()
2524 * always returns NULL. Setting override to 1
2525 * skips the unnecessary test.
2526 */
2527 override = 1;
2528 /* Do one big resize at the start, rather than
2529 * incrementally resizing as we insert new items. Expect
2530 * that there will be no (or few) overlapping keys.
2531 */
INADA Naokib1152be2016-10-27 19:26:50 +09002532 if (USABLE_FRACTION(mp->ma_keys->dk_size) < other->ma_used) {
2533 if (dictresize(mp, ESTIMATE_SIZE(mp->ma_used + other->ma_used))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002534 return -1;
INADA Naokib1152be2016-10-27 19:26:50 +09002535 }
2536 }
Victor Stinner742da042016-09-07 17:40:12 -07002537 ep0 = DK_ENTRIES(other->ma_keys);
2538 for (i = 0, n = other->ma_keys->dk_nentries; i < n; i++) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002539 PyObject *key, *value;
2540 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002541 entry = &ep0[i];
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002542 key = entry->me_key;
2543 hash = entry->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002544 if (other->ma_values)
2545 value = other->ma_values[i];
2546 else
2547 value = entry->me_value;
2548
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002549 if (value != NULL) {
2550 int err = 0;
2551 Py_INCREF(key);
2552 Py_INCREF(value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002553 if (override == 1)
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002554 err = insertdict(mp, key, hash, value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002555 else if (_PyDict_GetItem_KnownHash(a, key, hash) == NULL) {
2556 if (PyErr_Occurred()) {
2557 Py_DECREF(value);
2558 Py_DECREF(key);
2559 return -1;
2560 }
2561 err = insertdict(mp, key, hash, value);
2562 }
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002563 else if (override != 0) {
2564 _PyErr_SetKeyError(key);
2565 Py_DECREF(value);
2566 Py_DECREF(key);
2567 return -1;
2568 }
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002569 Py_DECREF(value);
2570 Py_DECREF(key);
2571 if (err != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002572 return -1;
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002573
Victor Stinner742da042016-09-07 17:40:12 -07002574 if (n != other->ma_keys->dk_nentries) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002575 PyErr_SetString(PyExc_RuntimeError,
2576 "dict mutated during update");
2577 return -1;
2578 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002579 }
2580 }
2581 }
2582 else {
2583 /* Do it the generic, slower way */
2584 PyObject *keys = PyMapping_Keys(b);
2585 PyObject *iter;
2586 PyObject *key, *value;
2587 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002589 if (keys == NULL)
2590 /* Docstring says this is equivalent to E.keys() so
2591 * if E doesn't have a .keys() method we want
2592 * AttributeError to percolate up. Might as well
2593 * do the same for any other error.
2594 */
2595 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002597 iter = PyObject_GetIter(keys);
2598 Py_DECREF(keys);
2599 if (iter == NULL)
2600 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002601
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002602 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002603 if (override != 1) {
2604 if (PyDict_GetItemWithError(a, key) != NULL) {
2605 if (override != 0) {
2606 _PyErr_SetKeyError(key);
2607 Py_DECREF(key);
2608 Py_DECREF(iter);
2609 return -1;
2610 }
2611 Py_DECREF(key);
2612 continue;
2613 }
2614 else if (PyErr_Occurred()) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002615 Py_DECREF(key);
2616 Py_DECREF(iter);
2617 return -1;
2618 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002619 }
2620 value = PyObject_GetItem(b, key);
2621 if (value == NULL) {
2622 Py_DECREF(iter);
2623 Py_DECREF(key);
2624 return -1;
2625 }
2626 status = PyDict_SetItem(a, key, value);
2627 Py_DECREF(key);
2628 Py_DECREF(value);
2629 if (status < 0) {
2630 Py_DECREF(iter);
2631 return -1;
2632 }
2633 }
2634 Py_DECREF(iter);
2635 if (PyErr_Occurred())
2636 /* Iterator completed, via error */
2637 return -1;
2638 }
Victor Stinner0fc91ee2019-04-12 21:51:34 +02002639 ASSERT_CONSISTENT(a);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002640 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002641}
2642
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002643int
2644PyDict_Update(PyObject *a, PyObject *b)
2645{
2646 return dict_merge(a, b, 1);
2647}
2648
2649int
2650PyDict_Merge(PyObject *a, PyObject *b, int override)
2651{
2652 /* XXX Deprecate override not in (0, 1). */
2653 return dict_merge(a, b, override != 0);
2654}
2655
2656int
2657_PyDict_MergeEx(PyObject *a, PyObject *b, int override)
2658{
2659 return dict_merge(a, b, override);
2660}
2661
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002662static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302663dict_copy(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002664{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002665 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002666}
2667
2668PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002669PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002670{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002671 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002672 PyDictObject *mp;
2673 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002675 if (o == NULL || !PyDict_Check(o)) {
2676 PyErr_BadInternalCall();
2677 return NULL;
2678 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002679
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002680 mp = (PyDictObject *)o;
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002681 if (mp->ma_used == 0) {
2682 /* The dict is empty; just return a new dict. */
2683 return PyDict_New();
2684 }
2685
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002686 if (_PyDict_HasSplitTable(mp)) {
2687 PyDictObject *split_copy;
Victor Stinner742da042016-09-07 17:40:12 -07002688 Py_ssize_t size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
2689 PyObject **newvalues;
2690 newvalues = new_values(size);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002691 if (newvalues == NULL)
2692 return PyErr_NoMemory();
2693 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2694 if (split_copy == NULL) {
2695 free_values(newvalues);
2696 return NULL;
2697 }
2698 split_copy->ma_values = newvalues;
2699 split_copy->ma_keys = mp->ma_keys;
2700 split_copy->ma_used = mp->ma_used;
INADA Naokid1c82c52018-04-03 11:43:53 +09002701 split_copy->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokia7576492018-11-14 18:39:27 +09002702 dictkeys_incref(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002703 for (i = 0, n = size; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002704 PyObject *value = mp->ma_values[i];
2705 Py_XINCREF(value);
2706 split_copy->ma_values[i] = value;
2707 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002708 if (_PyObject_GC_IS_TRACKED(mp))
2709 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002710 return (PyObject *)split_copy;
2711 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002712
2713 if (PyDict_CheckExact(mp) && mp->ma_values == NULL &&
2714 (mp->ma_used >= (mp->ma_keys->dk_nentries * 2) / 3))
2715 {
2716 /* Use fast-copy if:
2717
2718 (1) 'mp' is an instance of a subclassed dict; and
2719
2720 (2) 'mp' is not a split-dict; and
2721
2722 (3) if 'mp' is non-compact ('del' operation does not resize dicts),
2723 do fast-copy only if it has at most 1/3 non-used keys.
2724
Ville Skyttä61f82e02018-04-20 23:08:45 +03002725 The last condition (3) is important to guard against a pathological
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002726 case when a large dict is almost emptied with multiple del/pop
2727 operations and copied after that. In cases like this, we defer to
2728 PyDict_Merge, which produces a compacted copy.
2729 */
2730 return clone_combined_dict(mp);
2731 }
2732
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002733 copy = PyDict_New();
2734 if (copy == NULL)
2735 return NULL;
2736 if (PyDict_Merge(copy, o, 1) == 0)
2737 return copy;
2738 Py_DECREF(copy);
2739 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002740}
2741
Martin v. Löwis18e16552006-02-15 17:27:45 +00002742Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002743PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002744{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002745 if (mp == NULL || !PyDict_Check(mp)) {
2746 PyErr_BadInternalCall();
2747 return -1;
2748 }
2749 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002750}
2751
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002752PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002753PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002754{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002755 if (mp == NULL || !PyDict_Check(mp)) {
2756 PyErr_BadInternalCall();
2757 return NULL;
2758 }
2759 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002760}
2761
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002762PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002763PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002764{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002765 if (mp == NULL || !PyDict_Check(mp)) {
2766 PyErr_BadInternalCall();
2767 return NULL;
2768 }
2769 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002770}
2771
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002772PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002773PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002774{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002775 if (mp == NULL || !PyDict_Check(mp)) {
2776 PyErr_BadInternalCall();
2777 return NULL;
2778 }
2779 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002780}
2781
Tim Peterse63415e2001-05-08 04:38:29 +00002782/* Return 1 if dicts equal, 0 if not, -1 if error.
2783 * Gets out as soon as any difference is detected.
2784 * Uses only Py_EQ comparison.
2785 */
2786static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002787dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002788{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002789 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002791 if (a->ma_used != b->ma_used)
2792 /* can't be equal if # of entries differ */
2793 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002794 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Victor Stinner742da042016-09-07 17:40:12 -07002795 for (i = 0; i < a->ma_keys->dk_nentries; i++) {
2796 PyDictKeyEntry *ep = &DK_ENTRIES(a->ma_keys)[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002797 PyObject *aval;
2798 if (a->ma_values)
2799 aval = a->ma_values[i];
2800 else
2801 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002802 if (aval != NULL) {
2803 int cmp;
2804 PyObject *bval;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002805 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002806 /* temporarily bump aval's refcount to ensure it stays
2807 alive until we're done with it */
2808 Py_INCREF(aval);
2809 /* ditto for key */
2810 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002811 /* reuse the known hash value */
INADA Naoki778928b2017-08-03 23:45:15 +09002812 b->ma_keys->dk_lookup(b, key, ep->me_hash, &bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002813 if (bval == NULL) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002814 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002815 Py_DECREF(aval);
2816 if (PyErr_Occurred())
2817 return -1;
2818 return 0;
2819 }
Dong-hee Na2d5bf562019-12-31 10:04:22 +09002820 Py_INCREF(bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002821 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002822 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002823 Py_DECREF(aval);
Dong-hee Na2d5bf562019-12-31 10:04:22 +09002824 Py_DECREF(bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002825 if (cmp <= 0) /* error or not equal */
2826 return cmp;
2827 }
2828 }
2829 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002830}
Tim Peterse63415e2001-05-08 04:38:29 +00002831
2832static PyObject *
2833dict_richcompare(PyObject *v, PyObject *w, int op)
2834{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002835 int cmp;
2836 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002838 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2839 res = Py_NotImplemented;
2840 }
2841 else if (op == Py_EQ || op == Py_NE) {
2842 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2843 if (cmp < 0)
2844 return NULL;
2845 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2846 }
2847 else
2848 res = Py_NotImplemented;
2849 Py_INCREF(res);
2850 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002851}
Tim Peterse63415e2001-05-08 04:38:29 +00002852
Larry Hastings61272b72014-01-07 12:41:53 -08002853/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002854
2855@coexist
2856dict.__contains__
2857
2858 key: object
2859 /
2860
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002861True if the dictionary has the specified key, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002862[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002863
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002864static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002865dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka19d25972017-02-04 08:05:07 +02002866/*[clinic end generated code: output=a3d03db709ed6e6b input=fe1cb42ad831e820]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002867{
Larry Hastingsc2047262014-01-25 20:43:29 -08002868 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002869 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002870 Py_ssize_t ix;
INADA Naokiba609772016-12-07 20:41:42 +09002871 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002872
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002873 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002874 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002875 hash = PyObject_Hash(key);
2876 if (hash == -1)
2877 return NULL;
2878 }
INADA Naoki778928b2017-08-03 23:45:15 +09002879 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002880 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002881 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002882 if (ix == DKIX_EMPTY || value == NULL)
Victor Stinner742da042016-09-07 17:40:12 -07002883 Py_RETURN_FALSE;
2884 Py_RETURN_TRUE;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002885}
2886
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002887/*[clinic input]
2888dict.get
2889
2890 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002891 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002892 /
2893
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002894Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002895[clinic start generated code]*/
2896
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002897static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002898dict_get_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002899/*[clinic end generated code: output=bba707729dee05bf input=279ddb5790b6b107]*/
Barry Warsawc38c5da1997-10-06 17:49:20 +00002900{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002902 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002903 Py_ssize_t ix;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002905 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002906 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002907 hash = PyObject_Hash(key);
2908 if (hash == -1)
2909 return NULL;
2910 }
INADA Naoki778928b2017-08-03 23:45:15 +09002911 ix = (self->ma_keys->dk_lookup) (self, key, hash, &val);
Victor Stinner742da042016-09-07 17:40:12 -07002912 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002914 if (ix == DKIX_EMPTY || val == NULL) {
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002915 val = default_value;
INADA Naokiba609772016-12-07 20:41:42 +09002916 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 Py_INCREF(val);
2918 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002919}
2920
Benjamin Peterson00e98862013-03-07 22:16:29 -05002921PyObject *
2922PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002923{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002924 PyDictObject *mp = (PyDictObject *)d;
INADA Naoki93f26f72016-11-02 18:45:16 +09002925 PyObject *value;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002926 Py_hash_t hash;
Guido van Rossum164452c2000-08-08 16:12:54 +00002927
Benjamin Peterson00e98862013-03-07 22:16:29 -05002928 if (!PyDict_Check(d)) {
2929 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002930 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002931 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002933 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002934 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002935 hash = PyObject_Hash(key);
2936 if (hash == -1)
2937 return NULL;
2938 }
Inada Naoki2ddc7f62019-03-18 20:38:33 +09002939 if (mp->ma_keys == Py_EMPTY_KEYS) {
2940 if (insert_to_emptydict(mp, key, hash, defaultobj) < 0) {
2941 return NULL;
2942 }
2943 return defaultobj;
2944 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002945
2946 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
2947 if (insertion_resize(mp) < 0)
2948 return NULL;
2949 }
2950
INADA Naoki778928b2017-08-03 23:45:15 +09002951 Py_ssize_t ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002952 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002953 return NULL;
INADA Naoki93f26f72016-11-02 18:45:16 +09002954
2955 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09002956 ((ix >= 0 && value == NULL && mp->ma_used != ix) ||
INADA Naoki93f26f72016-11-02 18:45:16 +09002957 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
2958 if (insertion_resize(mp) < 0) {
2959 return NULL;
2960 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002961 ix = DKIX_EMPTY;
2962 }
2963
2964 if (ix == DKIX_EMPTY) {
2965 PyDictKeyEntry *ep, *ep0;
2966 value = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002967 if (mp->ma_keys->dk_usable <= 0) {
Victor Stinner3c336c52016-09-12 14:17:40 +02002968 if (insertion_resize(mp) < 0) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002969 return NULL;
Victor Stinner3c336c52016-09-12 14:17:40 +02002970 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002971 }
INADA Naoki778928b2017-08-03 23:45:15 +09002972 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naoki93f26f72016-11-02 18:45:16 +09002973 ep0 = DK_ENTRIES(mp->ma_keys);
2974 ep = &ep0[mp->ma_keys->dk_nentries];
INADA Naokia7576492018-11-14 18:39:27 +09002975 dictkeys_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002976 Py_INCREF(key);
INADA Naoki93f26f72016-11-02 18:45:16 +09002977 Py_INCREF(value);
2978 MAINTAIN_TRACKING(mp, key, value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002979 ep->me_key = key;
2980 ep->me_hash = hash;
INADA Naokiba609772016-12-07 20:41:42 +09002981 if (_PyDict_HasSplitTable(mp)) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002982 assert(mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
2983 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Victor Stinner742da042016-09-07 17:40:12 -07002984 }
2985 else {
INADA Naoki93f26f72016-11-02 18:45:16 +09002986 ep->me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07002987 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002988 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002989 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naoki93f26f72016-11-02 18:45:16 +09002990 mp->ma_keys->dk_usable--;
2991 mp->ma_keys->dk_nentries++;
2992 assert(mp->ma_keys->dk_usable >= 0);
2993 }
INADA Naokiba609772016-12-07 20:41:42 +09002994 else if (value == NULL) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002995 value = defaultobj;
2996 assert(_PyDict_HasSplitTable(mp));
2997 assert(ix == mp->ma_used);
2998 Py_INCREF(value);
2999 MAINTAIN_TRACKING(mp, key, value);
INADA Naokiba609772016-12-07 20:41:42 +09003000 mp->ma_values[ix] = value;
INADA Naoki93f26f72016-11-02 18:45:16 +09003001 mp->ma_used++;
3002 mp->ma_version_tag = DICT_NEXT_VERSION();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003003 }
INADA Naoki93f26f72016-11-02 18:45:16 +09003004
Victor Stinner0fc91ee2019-04-12 21:51:34 +02003005 ASSERT_CONSISTENT(mp);
INADA Naoki93f26f72016-11-02 18:45:16 +09003006 return value;
Guido van Rossum164452c2000-08-08 16:12:54 +00003007}
3008
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003009/*[clinic input]
3010dict.setdefault
3011
3012 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02003013 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003014 /
3015
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02003016Insert key with a value of default if key is not in the dictionary.
3017
3018Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003019[clinic start generated code]*/
3020
Benjamin Peterson00e98862013-03-07 22:16:29 -05003021static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02003022dict_setdefault_impl(PyDictObject *self, PyObject *key,
3023 PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02003024/*[clinic end generated code: output=f8c1101ebf69e220 input=0f063756e815fd9d]*/
Benjamin Peterson00e98862013-03-07 22:16:29 -05003025{
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003026 PyObject *val;
Benjamin Peterson00e98862013-03-07 22:16:29 -05003027
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02003028 val = PyDict_SetDefault((PyObject *)self, key, default_value);
Benjamin Peterson00e98862013-03-07 22:16:29 -05003029 Py_XINCREF(val);
3030 return val;
3031}
Guido van Rossum164452c2000-08-08 16:12:54 +00003032
3033static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303034dict_clear(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00003035{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003036 PyDict_Clear((PyObject *)mp);
3037 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00003038}
3039
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003040/*[clinic input]
3041dict.pop
3042
3043 key: object
3044 default: object = NULL
3045 /
3046
Serhiy Storchaka279f4462019-09-14 12:24:05 +03003047D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003048
3049If key is not found, default is returned if given, otherwise KeyError is raised
3050[clinic start generated code]*/
3051
Guido van Rossumba6ab842000-12-12 22:02:18 +00003052static PyObject *
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003053dict_pop_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
Serhiy Storchaka279f4462019-09-14 12:24:05 +03003054/*[clinic end generated code: output=3abb47b89f24c21c input=eeebec7812190348]*/
Guido van Rossume027d982002-04-12 15:11:59 +00003055{
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003056 return _PyDict_Pop((PyObject*)self, key, default_value);
Guido van Rossume027d982002-04-12 15:11:59 +00003057}
3058
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003059/*[clinic input]
3060dict.popitem
3061
3062Remove and return a (key, value) pair as a 2-tuple.
3063
3064Pairs are returned in LIFO (last-in, first-out) order.
3065Raises KeyError if the dict is empty.
3066[clinic start generated code]*/
3067
Guido van Rossume027d982002-04-12 15:11:59 +00003068static PyObject *
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003069dict_popitem_impl(PyDictObject *self)
3070/*[clinic end generated code: output=e65fcb04420d230d input=1c38a49f21f64941]*/
Guido van Rossumba6ab842000-12-12 22:02:18 +00003071{
Victor Stinner742da042016-09-07 17:40:12 -07003072 Py_ssize_t i, j;
3073 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003074 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00003075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003076 /* Allocate the result tuple before checking the size. Believe it
3077 * or not, this allocation could trigger a garbage collection which
3078 * could empty the dict, so if we checked the size first and that
3079 * happened, the result would be an infinite loop (searching for an
3080 * entry that no longer exists). Note that the usual popitem()
3081 * idiom is "while d: k, v = d.popitem()". so needing to throw the
3082 * tuple away if the dict *is* empty isn't a significant
3083 * inefficiency -- possible, but unlikely in practice.
3084 */
3085 res = PyTuple_New(2);
3086 if (res == NULL)
3087 return NULL;
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003088 if (self->ma_used == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003089 Py_DECREF(res);
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003090 PyErr_SetString(PyExc_KeyError, "popitem(): dictionary is empty");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003091 return NULL;
3092 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003093 /* Convert split table to combined table */
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003094 if (self->ma_keys->dk_lookup == lookdict_split) {
3095 if (dictresize(self, DK_SIZE(self->ma_keys))) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003096 Py_DECREF(res);
3097 return NULL;
3098 }
3099 }
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003100 ENSURE_ALLOWS_DELETIONS(self);
Victor Stinner742da042016-09-07 17:40:12 -07003101
3102 /* Pop last item */
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003103 ep0 = DK_ENTRIES(self->ma_keys);
3104 i = self->ma_keys->dk_nentries - 1;
Victor Stinner742da042016-09-07 17:40:12 -07003105 while (i >= 0 && ep0[i].me_value == NULL) {
3106 i--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003107 }
Victor Stinner742da042016-09-07 17:40:12 -07003108 assert(i >= 0);
3109
3110 ep = &ep0[i];
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003111 j = lookdict_index(self->ma_keys, ep->me_hash, i);
Victor Stinner742da042016-09-07 17:40:12 -07003112 assert(j >= 0);
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003113 assert(dictkeys_get_index(self->ma_keys, j) == i);
3114 dictkeys_set_index(self->ma_keys, j, DKIX_DUMMY);
Victor Stinner742da042016-09-07 17:40:12 -07003115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003116 PyTuple_SET_ITEM(res, 0, ep->me_key);
3117 PyTuple_SET_ITEM(res, 1, ep->me_value);
Victor Stinner742da042016-09-07 17:40:12 -07003118 ep->me_key = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003119 ep->me_value = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07003120 /* We can't dk_usable++ since there is DKIX_DUMMY in indices */
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003121 self->ma_keys->dk_nentries = i;
3122 self->ma_used--;
3123 self->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner0fc91ee2019-04-12 21:51:34 +02003124 ASSERT_CONSISTENT(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003125 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00003126}
3127
Jeremy Hylton8caad492000-06-23 14:18:11 +00003128static int
3129dict_traverse(PyObject *op, visitproc visit, void *arg)
3130{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003131 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson55f44522016-09-05 12:12:59 -07003132 PyDictKeysObject *keys = mp->ma_keys;
Serhiy Storchaka46825d22016-09-26 21:29:34 +03003133 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Victor Stinner742da042016-09-07 17:40:12 -07003134 Py_ssize_t i, n = keys->dk_nentries;
3135
Benjamin Peterson55f44522016-09-05 12:12:59 -07003136 if (keys->dk_lookup == lookdict) {
3137 for (i = 0; i < n; i++) {
3138 if (entries[i].me_value != NULL) {
3139 Py_VISIT(entries[i].me_value);
3140 Py_VISIT(entries[i].me_key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003141 }
3142 }
Victor Stinner742da042016-09-07 17:40:12 -07003143 }
3144 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003145 if (mp->ma_values != NULL) {
Benjamin Peterson55f44522016-09-05 12:12:59 -07003146 for (i = 0; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003147 Py_VISIT(mp->ma_values[i]);
3148 }
3149 }
3150 else {
Benjamin Peterson55f44522016-09-05 12:12:59 -07003151 for (i = 0; i < n; i++) {
3152 Py_VISIT(entries[i].me_value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003153 }
3154 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003155 }
3156 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00003157}
3158
3159static int
3160dict_tp_clear(PyObject *op)
3161{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003162 PyDict_Clear(op);
3163 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00003164}
3165
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003166static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003167
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003168Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003169_PyDict_SizeOf(PyDictObject *mp)
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003170{
Victor Stinner742da042016-09-07 17:40:12 -07003171 Py_ssize_t size, usable, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003172
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003173 size = DK_SIZE(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07003174 usable = USABLE_FRACTION(size);
3175
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02003176 res = _PyObject_SIZE(Py_TYPE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003177 if (mp->ma_values)
Victor Stinner742da042016-09-07 17:40:12 -07003178 res += usable * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003179 /* If the dictionary is split, the keys portion is accounted-for
3180 in the type object. */
3181 if (mp->ma_keys->dk_refcnt == 1)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003182 res += (sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003183 + DK_IXSIZE(mp->ma_keys) * size
3184 + sizeof(PyDictKeyEntry) * usable);
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003185 return res;
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003186}
3187
3188Py_ssize_t
3189_PyDict_KeysSize(PyDictKeysObject *keys)
3190{
Victor Stinner98ee9d52016-09-08 09:33:56 -07003191 return (sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003192 + DK_IXSIZE(keys) * DK_SIZE(keys)
3193 + USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry));
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003194}
3195
doko@ubuntu.com17210f52016-01-14 14:04:59 +01003196static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303197dict_sizeof(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003198{
3199 return PyLong_FromSsize_t(_PyDict_SizeOf(mp));
3200}
3201
Brandt Buchereb8ac572020-02-24 19:47:34 -08003202static PyObject *
3203dict_or(PyObject *self, PyObject *other)
3204{
3205 if (!PyDict_Check(self) || !PyDict_Check(other)) {
3206 Py_RETURN_NOTIMPLEMENTED;
3207 }
3208 PyObject *new = PyDict_Copy(self);
3209 if (new == NULL) {
3210 return NULL;
3211 }
3212 if (dict_update_arg(new, other)) {
3213 Py_DECREF(new);
3214 return NULL;
3215 }
3216 return new;
3217}
3218
3219static PyObject *
3220dict_ior(PyObject *self, PyObject *other)
3221{
3222 if (dict_update_arg(self, other)) {
3223 return NULL;
3224 }
3225 Py_INCREF(self);
3226 return self;
3227}
3228
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00003229PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
3230
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003231PyDoc_STRVAR(sizeof__doc__,
3232"D.__sizeof__() -> size of D in memory, in bytes");
3233
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003234PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04003235"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
3236If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
3237If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
3238In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00003239
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003240PyDoc_STRVAR(clear__doc__,
3241"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003242
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003243PyDoc_STRVAR(copy__doc__,
3244"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00003245
Guido van Rossumb90c8482007-02-10 01:11:45 +00003246/* Forward */
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303247static PyObject *dictkeys_new(PyObject *, PyObject *);
3248static PyObject *dictitems_new(PyObject *, PyObject *);
3249static PyObject *dictvalues_new(PyObject *, PyObject *);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003250
Guido van Rossum45c85d12007-07-27 16:31:40 +00003251PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003252 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003253PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003254 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003255PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003256 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00003257
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003258static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07003259 DICT___CONTAINS___METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003260 {"__getitem__", (PyCFunction)(void(*)(void))dict_subscript, METH_O | METH_COEXIST,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003261 getitem__doc__},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003262 {"__sizeof__", (PyCFunction)(void(*)(void))dict_sizeof, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003263 sizeof__doc__},
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003264 DICT_GET_METHODDEF
3265 DICT_SETDEFAULT_METHODDEF
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003266 DICT_POP_METHODDEF
3267 DICT_POPITEM_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303268 {"keys", dictkeys_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003269 keys__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303270 {"items", dictitems_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003271 items__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303272 {"values", dictvalues_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003273 values__doc__},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003274 {"update", (PyCFunction)(void(*)(void))dict_update, METH_VARARGS | METH_KEYWORDS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003275 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08003276 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003277 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
3278 clear__doc__},
3279 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
3280 copy__doc__},
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003281 DICT___REVERSED___METHODDEF
Guido van Rossum48b069a2020-04-07 09:50:06 -07003282 {"__class_getitem__", (PyCFunction)Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003283 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003284};
3285
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00003286/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00003287int
3288PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003289{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003290 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07003291 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003292 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003293 PyObject *value;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003295 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003296 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003297 hash = PyObject_Hash(key);
3298 if (hash == -1)
3299 return -1;
3300 }
INADA Naoki778928b2017-08-03 23:45:15 +09003301 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003302 if (ix == DKIX_ERROR)
3303 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003304 return (ix != DKIX_EMPTY && value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003305}
3306
Thomas Wouterscf297e42007-02-23 15:07:44 +00003307/* Internal version of PyDict_Contains used when the hash value is already known */
3308int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003309_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00003310{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003311 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003312 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -07003313 Py_ssize_t ix;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003314
INADA Naoki778928b2017-08-03 23:45:15 +09003315 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003316 if (ix == DKIX_ERROR)
3317 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003318 return (ix != DKIX_EMPTY && value != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00003319}
3320
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003321/* Hack to implement "key in dict" */
3322static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003323 0, /* sq_length */
3324 0, /* sq_concat */
3325 0, /* sq_repeat */
3326 0, /* sq_item */
3327 0, /* sq_slice */
3328 0, /* sq_ass_item */
3329 0, /* sq_ass_slice */
3330 PyDict_Contains, /* sq_contains */
3331 0, /* sq_inplace_concat */
3332 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003333};
3334
Brandt Buchereb8ac572020-02-24 19:47:34 -08003335static PyNumberMethods dict_as_number = {
3336 .nb_or = dict_or,
3337 .nb_inplace_or = dict_ior,
3338};
3339
Guido van Rossum09e563a2001-05-01 12:10:21 +00003340static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003341dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3342{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003343 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003344 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003346 assert(type != NULL && type->tp_alloc != NULL);
3347 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02003348 if (self == NULL)
3349 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003350 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003351
Victor Stinnera9f61a52013-07-16 22:17:26 +02003352 /* The object has been implicitly tracked by tp_alloc */
3353 if (type == &PyDict_Type)
3354 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003355
3356 d->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003357 d->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07003358 d->ma_keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003359 if (d->ma_keys == NULL) {
3360 Py_DECREF(self);
3361 return NULL;
3362 }
Victor Stinner0fc91ee2019-04-12 21:51:34 +02003363 ASSERT_CONSISTENT(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003364 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003365}
3366
Tim Peters25786c02001-09-02 08:22:48 +00003367static int
3368dict_init(PyObject *self, PyObject *args, PyObject *kwds)
3369{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003370 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00003371}
3372
Tim Peters6d6c1a32001-08-02 04:15:00 +00003373static PyObject *
Dong-hee Nae27916b2020-04-02 09:55:43 +09003374dict_vectorcall(PyObject *type, PyObject * const*args,
3375 size_t nargsf, PyObject *kwnames)
3376{
3377 assert(PyType_Check(type));
3378 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
3379 if (!_PyArg_CheckPositional("dict", nargs, 0, 1)) {
3380 return NULL;
3381 }
3382
3383 PyObject *self = dict_new((PyTypeObject *)type, NULL, NULL);
3384 if (self == NULL) {
3385 return NULL;
3386 }
3387 if (nargs == 1) {
3388 if (dict_update_arg(self, args[0]) < 0) {
3389 Py_DECREF(self);
3390 return NULL;
3391 }
3392 args++;
3393 }
3394 if (kwnames != NULL) {
3395 for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(kwnames); i++) {
3396 if (PyDict_SetItem(self, PyTuple_GET_ITEM(kwnames, i), args[i]) < 0) {
3397 Py_DECREF(self);
3398 return NULL;
3399 }
3400 }
3401 }
3402 return self;
3403}
3404
3405static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003406dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00003407{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003408 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003409}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003410
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003411PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00003412"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00003413"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003414" (key, value) pairs\n"
3415"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00003416" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003417" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00003418" d[k] = v\n"
3419"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
3420" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00003421
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003422PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003423 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3424 "dict",
3425 sizeof(PyDictObject),
3426 0,
3427 (destructor)dict_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003428 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003429 0, /* tp_getattr */
3430 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003431 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003432 (reprfunc)dict_repr, /* tp_repr */
Brandt Buchereb8ac572020-02-24 19:47:34 -08003433 &dict_as_number, /* tp_as_number */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003434 &dict_as_sequence, /* tp_as_sequence */
3435 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003436 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003437 0, /* tp_call */
3438 0, /* tp_str */
3439 PyObject_GenericGetAttr, /* tp_getattro */
3440 0, /* tp_setattro */
3441 0, /* tp_as_buffer */
3442 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3443 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
3444 dictionary_doc, /* tp_doc */
3445 dict_traverse, /* tp_traverse */
3446 dict_tp_clear, /* tp_clear */
3447 dict_richcompare, /* tp_richcompare */
3448 0, /* tp_weaklistoffset */
3449 (getiterfunc)dict_iter, /* tp_iter */
3450 0, /* tp_iternext */
3451 mapp_methods, /* tp_methods */
3452 0, /* tp_members */
3453 0, /* tp_getset */
3454 0, /* tp_base */
3455 0, /* tp_dict */
3456 0, /* tp_descr_get */
3457 0, /* tp_descr_set */
3458 0, /* tp_dictoffset */
3459 dict_init, /* tp_init */
3460 PyType_GenericAlloc, /* tp_alloc */
3461 dict_new, /* tp_new */
3462 PyObject_GC_Del, /* tp_free */
Dong-hee Nae27916b2020-04-02 09:55:43 +09003463 .tp_vectorcall = dict_vectorcall,
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003464};
3465
Victor Stinner3c1e4812012-03-26 22:10:51 +02003466PyObject *
3467_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
3468{
3469 PyObject *kv;
3470 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02003471 if (kv == NULL) {
3472 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003473 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02003474 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02003475 return PyDict_GetItem(dp, kv);
3476}
3477
Guido van Rossum3cca2451997-05-16 14:23:33 +00003478/* For backward compatibility with old dictionary interface */
3479
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003480PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003481PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003482{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003483 PyObject *kv, *rv;
3484 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003485 if (kv == NULL) {
3486 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003487 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003488 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003489 rv = PyDict_GetItem(v, kv);
3490 Py_DECREF(kv);
3491 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003492}
3493
3494int
Victor Stinner3c1e4812012-03-26 22:10:51 +02003495_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
3496{
3497 PyObject *kv;
3498 kv = _PyUnicode_FromId(key); /* borrowed */
3499 if (kv == NULL)
3500 return -1;
3501 return PyDict_SetItem(v, kv, item);
3502}
3503
3504int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003505PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003506{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003507 PyObject *kv;
3508 int err;
3509 kv = PyUnicode_FromString(key);
3510 if (kv == NULL)
3511 return -1;
3512 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
3513 err = PyDict_SetItem(v, kv, item);
3514 Py_DECREF(kv);
3515 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003516}
3517
3518int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01003519_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
3520{
3521 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
3522 if (kv == NULL)
3523 return -1;
3524 return PyDict_DelItem(v, kv);
3525}
3526
3527int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003528PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003529{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003530 PyObject *kv;
3531 int err;
3532 kv = PyUnicode_FromString(key);
3533 if (kv == NULL)
3534 return -1;
3535 err = PyDict_DelItem(v, kv);
3536 Py_DECREF(kv);
3537 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003538}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003539
Raymond Hettinger019a1482004-03-18 02:41:19 +00003540/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003541
3542typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003543 PyObject_HEAD
3544 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
3545 Py_ssize_t di_used;
3546 Py_ssize_t di_pos;
3547 PyObject* di_result; /* reusable result tuple for iteritems */
3548 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003549} dictiterobject;
3550
3551static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003552dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003553{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003554 dictiterobject *di;
3555 di = PyObject_GC_New(dictiterobject, itertype);
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003556 if (di == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003557 return NULL;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003558 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003559 Py_INCREF(dict);
3560 di->di_dict = dict;
3561 di->di_used = dict->ma_used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003562 di->len = dict->ma_used;
Dong-hee Na24dc2f82019-10-20 05:01:08 +09003563 if (itertype == &PyDictRevIterKey_Type ||
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003564 itertype == &PyDictRevIterItem_Type ||
Dong-hee Na24dc2f82019-10-20 05:01:08 +09003565 itertype == &PyDictRevIterValue_Type) {
3566 if (dict->ma_values) {
3567 di->di_pos = dict->ma_used - 1;
3568 }
3569 else {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003570 di->di_pos = dict->ma_keys->dk_nentries - 1;
Dong-hee Na24dc2f82019-10-20 05:01:08 +09003571 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003572 }
3573 else {
3574 di->di_pos = 0;
3575 }
3576 if (itertype == &PyDictIterItem_Type ||
3577 itertype == &PyDictRevIterItem_Type) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003578 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
3579 if (di->di_result == NULL) {
3580 Py_DECREF(di);
3581 return NULL;
3582 }
3583 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003584 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003585 di->di_result = NULL;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003586 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003587 _PyObject_GC_TRACK(di);
3588 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003589}
3590
3591static void
3592dictiter_dealloc(dictiterobject *di)
3593{
INADA Naokia6296d32017-08-24 14:55:17 +09003594 /* bpo-31095: UnTrack is needed before calling any callbacks */
3595 _PyObject_GC_UNTRACK(di);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003596 Py_XDECREF(di->di_dict);
3597 Py_XDECREF(di->di_result);
3598 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003599}
3600
3601static int
3602dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
3603{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003604 Py_VISIT(di->di_dict);
3605 Py_VISIT(di->di_result);
3606 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003607}
3608
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003609static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303610dictiter_len(dictiterobject *di, PyObject *Py_UNUSED(ignored))
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003611{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003612 Py_ssize_t len = 0;
3613 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
3614 len = di->len;
3615 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003616}
3617
Guido van Rossumb90c8482007-02-10 01:11:45 +00003618PyDoc_STRVAR(length_hint_doc,
3619 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003620
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003621static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303622dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003623
3624PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3625
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003626static PyMethodDef dictiter_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003627 {"__length_hint__", (PyCFunction)(void(*)(void))dictiter_len, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003628 length_hint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003629 {"__reduce__", (PyCFunction)(void(*)(void))dictiter_reduce, METH_NOARGS,
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003630 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003631 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003632};
3633
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003634static PyObject*
3635dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00003636{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003637 PyObject *key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003638 Py_ssize_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003639 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003640 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00003641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003642 if (d == NULL)
3643 return NULL;
3644 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00003645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003646 if (di->di_used != d->ma_used) {
3647 PyErr_SetString(PyExc_RuntimeError,
3648 "dictionary changed size during iteration");
3649 di->di_used = -1; /* Make this state sticky */
3650 return NULL;
3651 }
Guido van Rossum2147df72002-07-16 20:30:22 +00003652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003653 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003654 k = d->ma_keys;
INADA Naokica2d8be2016-11-04 16:59:10 +09003655 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003656 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003657 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003658 goto fail;
3659 key = DK_ENTRIES(k)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003660 assert(d->ma_values[i] != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003661 }
3662 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003663 Py_ssize_t n = k->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003664 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
3665 while (i < n && entry_ptr->me_value == NULL) {
3666 entry_ptr++;
3667 i++;
3668 }
3669 if (i >= n)
3670 goto fail;
3671 key = entry_ptr->me_key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003672 }
Thomas Perl796cc6e2019-03-28 07:03:25 +01003673 // We found an element (key), but did not expect it
3674 if (di->len == 0) {
3675 PyErr_SetString(PyExc_RuntimeError,
3676 "dictionary keys changed during iteration");
3677 goto fail;
3678 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003679 di->di_pos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003680 di->len--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003681 Py_INCREF(key);
3682 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003683
3684fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003685 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003686 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003687 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003688}
3689
Raymond Hettinger019a1482004-03-18 02:41:19 +00003690PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003691 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3692 "dict_keyiterator", /* tp_name */
3693 sizeof(dictiterobject), /* tp_basicsize */
3694 0, /* tp_itemsize */
3695 /* methods */
3696 (destructor)dictiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003697 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003698 0, /* tp_getattr */
3699 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003700 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003701 0, /* tp_repr */
3702 0, /* tp_as_number */
3703 0, /* tp_as_sequence */
3704 0, /* tp_as_mapping */
3705 0, /* tp_hash */
3706 0, /* tp_call */
3707 0, /* tp_str */
3708 PyObject_GenericGetAttr, /* tp_getattro */
3709 0, /* tp_setattro */
3710 0, /* tp_as_buffer */
3711 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3712 0, /* tp_doc */
3713 (traverseproc)dictiter_traverse, /* tp_traverse */
3714 0, /* tp_clear */
3715 0, /* tp_richcompare */
3716 0, /* tp_weaklistoffset */
3717 PyObject_SelfIter, /* tp_iter */
3718 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
3719 dictiter_methods, /* tp_methods */
3720 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003721};
3722
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003723static PyObject *
3724dictiter_iternextvalue(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003725{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003726 PyObject *value;
INADA Naokica2d8be2016-11-04 16:59:10 +09003727 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003728 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003730 if (d == NULL)
3731 return NULL;
3732 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003734 if (di->di_used != d->ma_used) {
3735 PyErr_SetString(PyExc_RuntimeError,
3736 "dictionary changed size during iteration");
3737 di->di_used = -1; /* Make this state sticky */
3738 return NULL;
3739 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003741 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003742 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003743 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003744 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003745 goto fail;
INADA Naokica2d8be2016-11-04 16:59:10 +09003746 value = d->ma_values[i];
3747 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003748 }
3749 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003750 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003751 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3752 while (i < n && entry_ptr->me_value == NULL) {
3753 entry_ptr++;
3754 i++;
3755 }
3756 if (i >= n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003757 goto fail;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003758 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003759 }
Thomas Perlb8311cf2019-04-02 11:30:10 +02003760 // We found an element, but did not expect it
3761 if (di->len == 0) {
3762 PyErr_SetString(PyExc_RuntimeError,
3763 "dictionary keys changed during iteration");
3764 goto fail;
3765 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003766 di->di_pos = i+1;
3767 di->len--;
3768 Py_INCREF(value);
3769 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003770
3771fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003772 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003773 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003774 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003775}
3776
3777PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003778 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3779 "dict_valueiterator", /* tp_name */
3780 sizeof(dictiterobject), /* tp_basicsize */
3781 0, /* tp_itemsize */
3782 /* methods */
3783 (destructor)dictiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003784 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003785 0, /* tp_getattr */
3786 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003787 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003788 0, /* tp_repr */
3789 0, /* tp_as_number */
3790 0, /* tp_as_sequence */
3791 0, /* tp_as_mapping */
3792 0, /* tp_hash */
3793 0, /* tp_call */
3794 0, /* tp_str */
3795 PyObject_GenericGetAttr, /* tp_getattro */
3796 0, /* tp_setattro */
3797 0, /* tp_as_buffer */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003798 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003799 0, /* tp_doc */
3800 (traverseproc)dictiter_traverse, /* tp_traverse */
3801 0, /* tp_clear */
3802 0, /* tp_richcompare */
3803 0, /* tp_weaklistoffset */
3804 PyObject_SelfIter, /* tp_iter */
3805 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3806 dictiter_methods, /* tp_methods */
3807 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003808};
3809
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003810static PyObject *
3811dictiter_iternextitem(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003812{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003813 PyObject *key, *value, *result;
INADA Naokica2d8be2016-11-04 16:59:10 +09003814 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003815 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003816
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003817 if (d == NULL)
3818 return NULL;
3819 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003821 if (di->di_used != d->ma_used) {
3822 PyErr_SetString(PyExc_RuntimeError,
3823 "dictionary changed size during iteration");
3824 di->di_used = -1; /* Make this state sticky */
3825 return NULL;
3826 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003827
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003828 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003829 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003830 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003831 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003832 goto fail;
3833 key = DK_ENTRIES(d->ma_keys)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003834 value = d->ma_values[i];
3835 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003836 }
3837 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003838 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003839 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3840 while (i < n && entry_ptr->me_value == NULL) {
3841 entry_ptr++;
3842 i++;
3843 }
3844 if (i >= n)
3845 goto fail;
3846 key = entry_ptr->me_key;
3847 value = entry_ptr->me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003848 }
Thomas Perlb8311cf2019-04-02 11:30:10 +02003849 // We found an element, but did not expect it
3850 if (di->len == 0) {
3851 PyErr_SetString(PyExc_RuntimeError,
3852 "dictionary keys changed during iteration");
3853 goto fail;
3854 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003855 di->di_pos = i+1;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003856 di->len--;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003857 Py_INCREF(key);
3858 Py_INCREF(value);
3859 result = di->di_result;
3860 if (Py_REFCNT(result) == 1) {
3861 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3862 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3863 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3864 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003865 Py_INCREF(result);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003866 Py_DECREF(oldkey);
3867 Py_DECREF(oldvalue);
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003868 }
3869 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003870 result = PyTuple_New(2);
3871 if (result == NULL)
3872 return NULL;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003873 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3874 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003875 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003876 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003877
3878fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003879 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003880 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003881 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003882}
3883
3884PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003885 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3886 "dict_itemiterator", /* tp_name */
3887 sizeof(dictiterobject), /* tp_basicsize */
3888 0, /* tp_itemsize */
3889 /* methods */
3890 (destructor)dictiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003891 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003892 0, /* tp_getattr */
3893 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003894 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003895 0, /* tp_repr */
3896 0, /* tp_as_number */
3897 0, /* tp_as_sequence */
3898 0, /* tp_as_mapping */
3899 0, /* tp_hash */
3900 0, /* tp_call */
3901 0, /* tp_str */
3902 PyObject_GenericGetAttr, /* tp_getattro */
3903 0, /* tp_setattro */
3904 0, /* tp_as_buffer */
3905 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3906 0, /* tp_doc */
3907 (traverseproc)dictiter_traverse, /* tp_traverse */
3908 0, /* tp_clear */
3909 0, /* tp_richcompare */
3910 0, /* tp_weaklistoffset */
3911 PyObject_SelfIter, /* tp_iter */
3912 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3913 dictiter_methods, /* tp_methods */
3914 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003915};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003916
3917
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003918/* dictreviter */
3919
3920static PyObject *
3921dictreviter_iternext(dictiterobject *di)
3922{
3923 PyDictObject *d = di->di_dict;
3924
3925 if (d == NULL) {
3926 return NULL;
3927 }
3928 assert (PyDict_Check(d));
3929
3930 if (di->di_used != d->ma_used) {
3931 PyErr_SetString(PyExc_RuntimeError,
3932 "dictionary changed size during iteration");
3933 di->di_used = -1; /* Make this state sticky */
3934 return NULL;
3935 }
3936
3937 Py_ssize_t i = di->di_pos;
3938 PyDictKeysObject *k = d->ma_keys;
3939 PyObject *key, *value, *result;
3940
Serhiy Storchaka2e3d8732019-10-23 14:48:08 +03003941 if (i < 0) {
3942 goto fail;
3943 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003944 if (d->ma_values) {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003945 key = DK_ENTRIES(k)[i].me_key;
3946 value = d->ma_values[i];
3947 assert (value != NULL);
3948 }
3949 else {
3950 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
Serhiy Storchaka2e3d8732019-10-23 14:48:08 +03003951 while (entry_ptr->me_value == NULL) {
3952 if (--i < 0) {
3953 goto fail;
3954 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003955 entry_ptr--;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003956 }
3957 key = entry_ptr->me_key;
3958 value = entry_ptr->me_value;
3959 }
3960 di->di_pos = i-1;
3961 di->len--;
3962
Dong-hee Na1b55b652020-02-17 19:09:15 +09003963 if (Py_IS_TYPE(di, &PyDictRevIterKey_Type)) {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003964 Py_INCREF(key);
3965 return key;
3966 }
Dong-hee Na1b55b652020-02-17 19:09:15 +09003967 else if (Py_IS_TYPE(di, &PyDictRevIterValue_Type)) {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003968 Py_INCREF(value);
3969 return value;
3970 }
Dong-hee Na1b55b652020-02-17 19:09:15 +09003971 else if (Py_IS_TYPE(di, &PyDictRevIterItem_Type)) {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003972 Py_INCREF(key);
3973 Py_INCREF(value);
3974 result = di->di_result;
3975 if (Py_REFCNT(result) == 1) {
3976 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3977 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3978 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3979 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
3980 Py_INCREF(result);
3981 Py_DECREF(oldkey);
3982 Py_DECREF(oldvalue);
3983 }
3984 else {
3985 result = PyTuple_New(2);
3986 if (result == NULL) {
3987 return NULL;
3988 }
3989 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3990 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
3991 }
3992 return result;
3993 }
3994 else {
3995 Py_UNREACHABLE();
3996 }
3997
3998fail:
3999 di->di_dict = NULL;
4000 Py_DECREF(d);
4001 return NULL;
4002}
4003
4004PyTypeObject PyDictRevIterKey_Type = {
4005 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4006 "dict_reversekeyiterator",
4007 sizeof(dictiterobject),
4008 .tp_dealloc = (destructor)dictiter_dealloc,
4009 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
4010 .tp_traverse = (traverseproc)dictiter_traverse,
4011 .tp_iter = PyObject_SelfIter,
4012 .tp_iternext = (iternextfunc)dictreviter_iternext,
4013 .tp_methods = dictiter_methods
4014};
4015
4016
4017/*[clinic input]
4018dict.__reversed__
4019
4020Return a reverse iterator over the dict keys.
4021[clinic start generated code]*/
4022
4023static PyObject *
4024dict___reversed___impl(PyDictObject *self)
4025/*[clinic end generated code: output=e674483336d1ed51 input=23210ef3477d8c4d]*/
4026{
4027 assert (PyDict_Check(self));
4028 return dictiter_new(self, &PyDictRevIterKey_Type);
4029}
4030
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004031static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304032dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004033{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004034 _Py_IDENTIFIER(iter);
Sergey Fedoseev63958442018-10-20 05:43:33 +05004035 /* copy the iterator state */
4036 dictiterobject tmp = *di;
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004037 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004038
Sergey Fedoseev63958442018-10-20 05:43:33 +05004039 PyObject *list = PySequence_List((PyObject*)&tmp);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004040 Py_XDECREF(tmp.di_dict);
Sergey Fedoseev63958442018-10-20 05:43:33 +05004041 if (list == NULL) {
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004042 return NULL;
4043 }
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004044 return Py_BuildValue("N(N)", _PyEval_GetBuiltinId(&PyId_iter), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004045}
4046
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004047PyTypeObject PyDictRevIterItem_Type = {
4048 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4049 "dict_reverseitemiterator",
4050 sizeof(dictiterobject),
4051 .tp_dealloc = (destructor)dictiter_dealloc,
4052 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
4053 .tp_traverse = (traverseproc)dictiter_traverse,
4054 .tp_iter = PyObject_SelfIter,
4055 .tp_iternext = (iternextfunc)dictreviter_iternext,
4056 .tp_methods = dictiter_methods
4057};
4058
4059PyTypeObject PyDictRevIterValue_Type = {
4060 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4061 "dict_reversevalueiterator",
4062 sizeof(dictiterobject),
4063 .tp_dealloc = (destructor)dictiter_dealloc,
4064 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
4065 .tp_traverse = (traverseproc)dictiter_traverse,
4066 .tp_iter = PyObject_SelfIter,
4067 .tp_iternext = (iternextfunc)dictreviter_iternext,
4068 .tp_methods = dictiter_methods
4069};
4070
Guido van Rossum3ac67412007-02-10 18:55:06 +00004071/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004072/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00004073/***********************************************/
4074
Guido van Rossumb90c8482007-02-10 01:11:45 +00004075/* The instance lay-out is the same for all three; but the type differs. */
4076
Guido van Rossumb90c8482007-02-10 01:11:45 +00004077static void
Eric Snow96c6af92015-05-29 22:21:39 -06004078dictview_dealloc(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004079{
INADA Naokia6296d32017-08-24 14:55:17 +09004080 /* bpo-31095: UnTrack is needed before calling any callbacks */
4081 _PyObject_GC_UNTRACK(dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004082 Py_XDECREF(dv->dv_dict);
4083 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00004084}
4085
4086static int
Eric Snow96c6af92015-05-29 22:21:39 -06004087dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
Antoine Pitrou7ddda782009-01-01 15:35:33 +00004088{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004089 Py_VISIT(dv->dv_dict);
4090 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004091}
4092
Guido van Rossum83825ac2007-02-10 04:54:19 +00004093static Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06004094dictview_len(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004095{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004096 Py_ssize_t len = 0;
4097 if (dv->dv_dict != NULL)
4098 len = dv->dv_dict->ma_used;
4099 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004100}
4101
Eric Snow96c6af92015-05-29 22:21:39 -06004102PyObject *
4103_PyDictView_New(PyObject *dict, PyTypeObject *type)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004104{
Eric Snow96c6af92015-05-29 22:21:39 -06004105 _PyDictViewObject *dv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004106 if (dict == NULL) {
4107 PyErr_BadInternalCall();
4108 return NULL;
4109 }
4110 if (!PyDict_Check(dict)) {
4111 /* XXX Get rid of this restriction later */
4112 PyErr_Format(PyExc_TypeError,
4113 "%s() requires a dict argument, not '%s'",
Victor Stinner58ac7002020-02-07 03:04:21 +01004114 type->tp_name, Py_TYPE(dict)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004115 return NULL;
4116 }
Eric Snow96c6af92015-05-29 22:21:39 -06004117 dv = PyObject_GC_New(_PyDictViewObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004118 if (dv == NULL)
4119 return NULL;
4120 Py_INCREF(dict);
4121 dv->dv_dict = (PyDictObject *)dict;
4122 _PyObject_GC_TRACK(dv);
4123 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004124}
4125
Neal Norwitze36f2ba2007-02-26 23:12:28 +00004126/* TODO(guido): The views objects are not complete:
4127
4128 * support more set operations
4129 * support arbitrary mappings?
4130 - either these should be static or exported in dictobject.h
4131 - if public then they should probably be in builtins
4132*/
4133
Guido van Rossumaac530c2007-08-24 22:33:45 +00004134/* Return 1 if self is a subset of other, iterating over self;
4135 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00004136static int
4137all_contained_in(PyObject *self, PyObject *other)
4138{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004139 PyObject *iter = PyObject_GetIter(self);
4140 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00004141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004142 if (iter == NULL)
4143 return -1;
4144 for (;;) {
4145 PyObject *next = PyIter_Next(iter);
4146 if (next == NULL) {
4147 if (PyErr_Occurred())
4148 ok = -1;
4149 break;
4150 }
4151 ok = PySequence_Contains(other, next);
4152 Py_DECREF(next);
4153 if (ok <= 0)
4154 break;
4155 }
4156 Py_DECREF(iter);
4157 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00004158}
4159
4160static PyObject *
4161dictview_richcompare(PyObject *self, PyObject *other, int op)
4162{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004163 Py_ssize_t len_self, len_other;
4164 int ok;
4165 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004167 assert(self != NULL);
4168 assert(PyDictViewSet_Check(self));
4169 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00004170
Brian Curtindfc80e32011-08-10 20:28:54 -05004171 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
4172 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004174 len_self = PyObject_Size(self);
4175 if (len_self < 0)
4176 return NULL;
4177 len_other = PyObject_Size(other);
4178 if (len_other < 0)
4179 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004181 ok = 0;
4182 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00004183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004184 case Py_NE:
4185 case Py_EQ:
4186 if (len_self == len_other)
4187 ok = all_contained_in(self, other);
4188 if (op == Py_NE && ok >= 0)
4189 ok = !ok;
4190 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004192 case Py_LT:
4193 if (len_self < len_other)
4194 ok = all_contained_in(self, other);
4195 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004197 case Py_LE:
4198 if (len_self <= len_other)
4199 ok = all_contained_in(self, other);
4200 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004202 case Py_GT:
4203 if (len_self > len_other)
4204 ok = all_contained_in(other, self);
4205 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004207 case Py_GE:
4208 if (len_self >= len_other)
4209 ok = all_contained_in(other, self);
4210 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004212 }
4213 if (ok < 0)
4214 return NULL;
4215 result = ok ? Py_True : Py_False;
4216 Py_INCREF(result);
4217 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00004218}
4219
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004220static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004221dictview_repr(_PyDictViewObject *dv)
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004222{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004223 PyObject *seq;
bennorthd7773d92018-01-26 15:46:01 +00004224 PyObject *result = NULL;
4225 Py_ssize_t rc;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004226
bennorthd7773d92018-01-26 15:46:01 +00004227 rc = Py_ReprEnter((PyObject *)dv);
4228 if (rc != 0) {
4229 return rc > 0 ? PyUnicode_FromString("...") : NULL;
4230 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004231 seq = PySequence_List((PyObject *)dv);
bennorthd7773d92018-01-26 15:46:01 +00004232 if (seq == NULL) {
4233 goto Done;
4234 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004235 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
4236 Py_DECREF(seq);
bennorthd7773d92018-01-26 15:46:01 +00004237
4238Done:
4239 Py_ReprLeave((PyObject *)dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004240 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004241}
4242
Guido van Rossum3ac67412007-02-10 18:55:06 +00004243/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004244
4245static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004246dictkeys_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004247{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004248 if (dv->dv_dict == NULL) {
4249 Py_RETURN_NONE;
4250 }
4251 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004252}
4253
4254static int
Eric Snow96c6af92015-05-29 22:21:39 -06004255dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004256{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004257 if (dv->dv_dict == NULL)
4258 return 0;
4259 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004260}
4261
Guido van Rossum83825ac2007-02-10 04:54:19 +00004262static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004263 (lenfunc)dictview_len, /* sq_length */
4264 0, /* sq_concat */
4265 0, /* sq_repeat */
4266 0, /* sq_item */
4267 0, /* sq_slice */
4268 0, /* sq_ass_item */
4269 0, /* sq_ass_slice */
4270 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004271};
4272
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004273// Create an set object from dictviews object.
4274// Returns a new reference.
4275// This utility function is used by set operations.
Guido van Rossum523259b2007-08-24 23:41:22 +00004276static PyObject*
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004277dictviews_to_set(PyObject *self)
Guido van Rossum523259b2007-08-24 23:41:22 +00004278{
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004279 PyObject *left = self;
4280 if (PyDictKeys_Check(self)) {
4281 // PySet_New() has fast path for the dict object.
4282 PyObject *dict = (PyObject *)((_PyDictViewObject *)self)->dv_dict;
4283 if (PyDict_CheckExact(dict)) {
4284 left = dict;
4285 }
4286 }
4287 return PySet_New(left);
4288}
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004289
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004290static PyObject*
4291dictviews_sub(PyObject *self, PyObject *other)
4292{
4293 PyObject *result = dictviews_to_set(self);
4294 if (result == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004295 return NULL;
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004296 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004297
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004298 _Py_IDENTIFIER(difference_update);
4299 PyObject *tmp = _PyObject_CallMethodIdOneArg(
4300 result, &PyId_difference_update, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004301 if (tmp == NULL) {
4302 Py_DECREF(result);
4303 return NULL;
4304 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004306 Py_DECREF(tmp);
4307 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004308}
4309
Forest Gregg998cf1f2019-08-26 02:17:43 -05004310static int
4311dictitems_contains(_PyDictViewObject *dv, PyObject *obj);
4312
4313PyObject *
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004314_PyDictView_Intersect(PyObject* self, PyObject *other)
Guido van Rossum523259b2007-08-24 23:41:22 +00004315{
Forest Gregg998cf1f2019-08-26 02:17:43 -05004316 PyObject *result;
4317 PyObject *it;
4318 PyObject *key;
4319 Py_ssize_t len_self;
4320 int rv;
4321 int (*dict_contains)(_PyDictViewObject *, PyObject *);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004322
Forest Gregg998cf1f2019-08-26 02:17:43 -05004323 /* Python interpreter swaps parameters when dict view
4324 is on right side of & */
4325 if (!PyDictViewSet_Check(self)) {
4326 PyObject *tmp = other;
4327 other = self;
4328 self = tmp;
4329 }
4330
4331 len_self = dictview_len((_PyDictViewObject *)self);
4332
4333 /* if other is a set and self is smaller than other,
4334 reuse set intersection logic */
Dong-hee Na1b55b652020-02-17 19:09:15 +09004335 if (Py_IS_TYPE(other, &PySet_Type) && len_self <= PyObject_Size(other)) {
Forest Gregg998cf1f2019-08-26 02:17:43 -05004336 _Py_IDENTIFIER(intersection);
4337 return _PyObject_CallMethodIdObjArgs(other, &PyId_intersection, self, NULL);
4338 }
4339
4340 /* if other is another dict view, and it is bigger than self,
4341 swap them */
4342 if (PyDictViewSet_Check(other)) {
4343 Py_ssize_t len_other = dictview_len((_PyDictViewObject *)other);
4344 if (len_other > len_self) {
4345 PyObject *tmp = other;
4346 other = self;
4347 self = tmp;
4348 }
4349 }
4350
4351 /* at this point, two things should be true
4352 1. self is a dictview
4353 2. if other is a dictview then it is smaller than self */
4354 result = PySet_New(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004355 if (result == NULL)
4356 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004357
Forest Gregg998cf1f2019-08-26 02:17:43 -05004358 it = PyObject_GetIter(other);
Zackery Spytzb16e3822019-10-13 05:49:05 -06004359 if (it == NULL) {
4360 Py_DECREF(result);
4361 return NULL;
4362 }
Forest Gregg998cf1f2019-08-26 02:17:43 -05004363
Forest Gregg998cf1f2019-08-26 02:17:43 -05004364 if (PyDictKeys_Check(self)) {
4365 dict_contains = dictkeys_contains;
4366 }
4367 /* else PyDictItems_Check(self) */
4368 else {
4369 dict_contains = dictitems_contains;
4370 }
4371
4372 while ((key = PyIter_Next(it)) != NULL) {
4373 rv = dict_contains((_PyDictViewObject *)self, key);
4374 if (rv < 0) {
4375 goto error;
4376 }
4377 if (rv) {
4378 if (PySet_Add(result, key)) {
4379 goto error;
4380 }
4381 }
4382 Py_DECREF(key);
4383 }
4384 Py_DECREF(it);
4385 if (PyErr_Occurred()) {
4386 Py_DECREF(result);
4387 return NULL;
4388 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004389 return result;
Forest Gregg998cf1f2019-08-26 02:17:43 -05004390
4391error:
4392 Py_DECREF(it);
4393 Py_DECREF(result);
4394 Py_DECREF(key);
4395 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004396}
4397
4398static PyObject*
4399dictviews_or(PyObject* self, PyObject *other)
4400{
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004401 PyObject *result = dictviews_to_set(self);
4402 if (result == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004403 return NULL;
4404 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004405
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004406 if (_PySet_Update(result, other) < 0) {
4407 Py_DECREF(result);
4408 return NULL;
4409 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004410 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004411}
4412
4413static PyObject*
4414dictviews_xor(PyObject* self, PyObject *other)
4415{
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004416 PyObject *result = dictviews_to_set(self);
4417 if (result == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004418 return NULL;
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004419 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004420
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004421 _Py_IDENTIFIER(symmetric_difference_update);
4422 PyObject *tmp = _PyObject_CallMethodIdOneArg(
4423 result, &PyId_symmetric_difference_update, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004424 if (tmp == NULL) {
4425 Py_DECREF(result);
4426 return NULL;
4427 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004429 Py_DECREF(tmp);
4430 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004431}
4432
4433static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004434 0, /*nb_add*/
4435 (binaryfunc)dictviews_sub, /*nb_subtract*/
4436 0, /*nb_multiply*/
4437 0, /*nb_remainder*/
4438 0, /*nb_divmod*/
4439 0, /*nb_power*/
4440 0, /*nb_negative*/
4441 0, /*nb_positive*/
4442 0, /*nb_absolute*/
4443 0, /*nb_bool*/
4444 0, /*nb_invert*/
4445 0, /*nb_lshift*/
4446 0, /*nb_rshift*/
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004447 (binaryfunc)_PyDictView_Intersect, /*nb_and*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004448 (binaryfunc)dictviews_xor, /*nb_xor*/
4449 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00004450};
4451
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004452static PyObject*
4453dictviews_isdisjoint(PyObject *self, PyObject *other)
4454{
4455 PyObject *it;
4456 PyObject *item = NULL;
4457
4458 if (self == other) {
Eric Snow96c6af92015-05-29 22:21:39 -06004459 if (dictview_len((_PyDictViewObject *)self) == 0)
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004460 Py_RETURN_TRUE;
4461 else
4462 Py_RETURN_FALSE;
4463 }
4464
4465 /* Iterate over the shorter object (only if other is a set,
4466 * because PySequence_Contains may be expensive otherwise): */
4467 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
Eric Snow96c6af92015-05-29 22:21:39 -06004468 Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self);
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004469 Py_ssize_t len_other = PyObject_Size(other);
4470 if (len_other == -1)
4471 return NULL;
4472
4473 if ((len_other > len_self)) {
4474 PyObject *tmp = other;
4475 other = self;
4476 self = tmp;
4477 }
4478 }
4479
4480 it = PyObject_GetIter(other);
4481 if (it == NULL)
4482 return NULL;
4483
4484 while ((item = PyIter_Next(it)) != NULL) {
4485 int contains = PySequence_Contains(self, item);
4486 Py_DECREF(item);
4487 if (contains == -1) {
4488 Py_DECREF(it);
4489 return NULL;
4490 }
4491
4492 if (contains) {
4493 Py_DECREF(it);
4494 Py_RETURN_FALSE;
4495 }
4496 }
4497 Py_DECREF(it);
4498 if (PyErr_Occurred())
4499 return NULL; /* PyIter_Next raised an exception. */
4500 Py_RETURN_TRUE;
4501}
4502
4503PyDoc_STRVAR(isdisjoint_doc,
4504"Return True if the view and the given iterable have a null intersection.");
4505
Serhiy Storchaka81524022018-11-27 13:05:02 +02004506static PyObject* dictkeys_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored));
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004507
4508PyDoc_STRVAR(reversed_keys_doc,
4509"Return a reverse iterator over the dict keys.");
4510
Guido van Rossumb90c8482007-02-10 01:11:45 +00004511static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004512 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4513 isdisjoint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004514 {"__reversed__", (PyCFunction)(void(*)(void))dictkeys_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004515 reversed_keys_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004516 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004517};
4518
4519PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004520 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4521 "dict_keys", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004522 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004523 0, /* tp_itemsize */
4524 /* methods */
4525 (destructor)dictview_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004526 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004527 0, /* tp_getattr */
4528 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004529 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004530 (reprfunc)dictview_repr, /* tp_repr */
4531 &dictviews_as_number, /* tp_as_number */
4532 &dictkeys_as_sequence, /* tp_as_sequence */
4533 0, /* tp_as_mapping */
4534 0, /* tp_hash */
4535 0, /* tp_call */
4536 0, /* tp_str */
4537 PyObject_GenericGetAttr, /* tp_getattro */
4538 0, /* tp_setattro */
4539 0, /* tp_as_buffer */
4540 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4541 0, /* tp_doc */
4542 (traverseproc)dictview_traverse, /* tp_traverse */
4543 0, /* tp_clear */
4544 dictview_richcompare, /* tp_richcompare */
4545 0, /* tp_weaklistoffset */
4546 (getiterfunc)dictkeys_iter, /* tp_iter */
4547 0, /* tp_iternext */
4548 dictkeys_methods, /* tp_methods */
4549 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004550};
4551
4552static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304553dictkeys_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004554{
Eric Snow96c6af92015-05-29 22:21:39 -06004555 return _PyDictView_New(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004556}
4557
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004558static PyObject *
Serhiy Storchaka81524022018-11-27 13:05:02 +02004559dictkeys_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored))
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004560{
4561 if (dv->dv_dict == NULL) {
4562 Py_RETURN_NONE;
4563 }
4564 return dictiter_new(dv->dv_dict, &PyDictRevIterKey_Type);
4565}
4566
Guido van Rossum3ac67412007-02-10 18:55:06 +00004567/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004568
4569static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004570dictitems_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004571{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004572 if (dv->dv_dict == NULL) {
4573 Py_RETURN_NONE;
4574 }
4575 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004576}
4577
4578static int
Eric Snow96c6af92015-05-29 22:21:39 -06004579dictitems_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004580{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004581 int result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004582 PyObject *key, *value, *found;
4583 if (dv->dv_dict == NULL)
4584 return 0;
4585 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
4586 return 0;
4587 key = PyTuple_GET_ITEM(obj, 0);
4588 value = PyTuple_GET_ITEM(obj, 1);
Raymond Hettinger6692f012016-09-18 21:46:08 -07004589 found = PyDict_GetItemWithError((PyObject *)dv->dv_dict, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004590 if (found == NULL) {
4591 if (PyErr_Occurred())
4592 return -1;
4593 return 0;
4594 }
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004595 Py_INCREF(found);
Serhiy Storchaka18b711c2019-08-04 14:12:48 +03004596 result = PyObject_RichCompareBool(found, value, Py_EQ);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004597 Py_DECREF(found);
4598 return result;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004599}
4600
Guido van Rossum83825ac2007-02-10 04:54:19 +00004601static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004602 (lenfunc)dictview_len, /* sq_length */
4603 0, /* sq_concat */
4604 0, /* sq_repeat */
4605 0, /* sq_item */
4606 0, /* sq_slice */
4607 0, /* sq_ass_item */
4608 0, /* sq_ass_slice */
4609 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004610};
4611
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004612static PyObject* dictitems_reversed(_PyDictViewObject *dv);
4613
4614PyDoc_STRVAR(reversed_items_doc,
4615"Return a reverse iterator over the dict items.");
4616
Guido van Rossumb90c8482007-02-10 01:11:45 +00004617static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004618 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4619 isdisjoint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004620 {"__reversed__", (PyCFunction)(void(*)(void))dictitems_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004621 reversed_items_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004622 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004623};
4624
4625PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004626 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4627 "dict_items", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004628 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004629 0, /* tp_itemsize */
4630 /* methods */
4631 (destructor)dictview_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004632 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004633 0, /* tp_getattr */
4634 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004635 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004636 (reprfunc)dictview_repr, /* tp_repr */
4637 &dictviews_as_number, /* tp_as_number */
4638 &dictitems_as_sequence, /* tp_as_sequence */
4639 0, /* tp_as_mapping */
4640 0, /* tp_hash */
4641 0, /* tp_call */
4642 0, /* tp_str */
4643 PyObject_GenericGetAttr, /* tp_getattro */
4644 0, /* tp_setattro */
4645 0, /* tp_as_buffer */
4646 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4647 0, /* tp_doc */
4648 (traverseproc)dictview_traverse, /* tp_traverse */
4649 0, /* tp_clear */
4650 dictview_richcompare, /* tp_richcompare */
4651 0, /* tp_weaklistoffset */
4652 (getiterfunc)dictitems_iter, /* tp_iter */
4653 0, /* tp_iternext */
4654 dictitems_methods, /* tp_methods */
4655 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004656};
4657
4658static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304659dictitems_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004660{
Eric Snow96c6af92015-05-29 22:21:39 -06004661 return _PyDictView_New(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004662}
4663
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004664static PyObject *
4665dictitems_reversed(_PyDictViewObject *dv)
4666{
4667 if (dv->dv_dict == NULL) {
4668 Py_RETURN_NONE;
4669 }
4670 return dictiter_new(dv->dv_dict, &PyDictRevIterItem_Type);
4671}
4672
Guido van Rossum3ac67412007-02-10 18:55:06 +00004673/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004674
4675static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004676dictvalues_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004677{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004678 if (dv->dv_dict == NULL) {
4679 Py_RETURN_NONE;
4680 }
4681 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004682}
4683
Guido van Rossum83825ac2007-02-10 04:54:19 +00004684static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004685 (lenfunc)dictview_len, /* sq_length */
4686 0, /* sq_concat */
4687 0, /* sq_repeat */
4688 0, /* sq_item */
4689 0, /* sq_slice */
4690 0, /* sq_ass_item */
4691 0, /* sq_ass_slice */
4692 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004693};
4694
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004695static PyObject* dictvalues_reversed(_PyDictViewObject *dv);
4696
4697PyDoc_STRVAR(reversed_values_doc,
4698"Return a reverse iterator over the dict values.");
4699
Guido van Rossumb90c8482007-02-10 01:11:45 +00004700static PyMethodDef dictvalues_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004701 {"__reversed__", (PyCFunction)(void(*)(void))dictvalues_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004702 reversed_values_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004703 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004704};
4705
4706PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004707 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4708 "dict_values", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004709 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004710 0, /* tp_itemsize */
4711 /* methods */
4712 (destructor)dictview_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004713 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004714 0, /* tp_getattr */
4715 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004716 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004717 (reprfunc)dictview_repr, /* tp_repr */
4718 0, /* tp_as_number */
4719 &dictvalues_as_sequence, /* tp_as_sequence */
4720 0, /* tp_as_mapping */
4721 0, /* tp_hash */
4722 0, /* tp_call */
4723 0, /* tp_str */
4724 PyObject_GenericGetAttr, /* tp_getattro */
4725 0, /* tp_setattro */
4726 0, /* tp_as_buffer */
4727 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4728 0, /* tp_doc */
4729 (traverseproc)dictview_traverse, /* tp_traverse */
4730 0, /* tp_clear */
4731 0, /* tp_richcompare */
4732 0, /* tp_weaklistoffset */
4733 (getiterfunc)dictvalues_iter, /* tp_iter */
4734 0, /* tp_iternext */
4735 dictvalues_methods, /* tp_methods */
4736 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004737};
4738
4739static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304740dictvalues_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004741{
Eric Snow96c6af92015-05-29 22:21:39 -06004742 return _PyDictView_New(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004743}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004744
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004745static PyObject *
4746dictvalues_reversed(_PyDictViewObject *dv)
4747{
4748 if (dv->dv_dict == NULL) {
4749 Py_RETURN_NONE;
4750 }
4751 return dictiter_new(dv->dv_dict, &PyDictRevIterValue_Type);
4752}
4753
4754
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004755/* Returns NULL if cannot allocate a new PyDictKeysObject,
4756 but does not set an error */
4757PyDictKeysObject *
4758_PyDict_NewKeysForClass(void)
4759{
Victor Stinner742da042016-09-07 17:40:12 -07004760 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004761 if (keys == NULL)
4762 PyErr_Clear();
4763 else
4764 keys->dk_lookup = lookdict_split;
4765 return keys;
4766}
4767
4768#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
4769
4770PyObject *
4771PyObject_GenericGetDict(PyObject *obj, void *context)
4772{
4773 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
4774 if (dictptr == NULL) {
4775 PyErr_SetString(PyExc_AttributeError,
4776 "This object has no __dict__");
4777 return NULL;
4778 }
4779 dict = *dictptr;
4780 if (dict == NULL) {
4781 PyTypeObject *tp = Py_TYPE(obj);
4782 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
INADA Naokia7576492018-11-14 18:39:27 +09004783 dictkeys_incref(CACHED_KEYS(tp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004784 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
4785 }
4786 else {
4787 *dictptr = dict = PyDict_New();
4788 }
4789 }
4790 Py_XINCREF(dict);
4791 return dict;
4792}
4793
4794int
4795_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
Victor Stinner742da042016-09-07 17:40:12 -07004796 PyObject *key, PyObject *value)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004797{
4798 PyObject *dict;
4799 int res;
4800 PyDictKeysObject *cached;
4801
4802 assert(dictptr != NULL);
4803 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
4804 assert(dictptr != NULL);
4805 dict = *dictptr;
4806 if (dict == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +09004807 dictkeys_incref(cached);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004808 dict = new_dict_with_shared_keys(cached);
4809 if (dict == NULL)
4810 return -1;
4811 *dictptr = dict;
4812 }
4813 if (value == NULL) {
4814 res = PyDict_DelItem(dict, key);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004815 // Since key sharing dict doesn't allow deletion, PyDict_DelItem()
4816 // always converts dict to combined form.
4817 if ((cached = CACHED_KEYS(tp)) != NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004818 CACHED_KEYS(tp) = NULL;
INADA Naokia7576492018-11-14 18:39:27 +09004819 dictkeys_decref(cached);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004820 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01004821 }
4822 else {
INADA Naoki2294f3a2017-02-12 13:51:30 +09004823 int was_shared = (cached == ((PyDictObject *)dict)->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004824 res = PyDict_SetItem(dict, key, value);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004825 if (was_shared &&
4826 (cached = CACHED_KEYS(tp)) != NULL &&
4827 cached != ((PyDictObject *)dict)->ma_keys) {
Victor Stinner3d3f2642016-12-15 17:21:23 +01004828 /* PyDict_SetItem() may call dictresize and convert split table
4829 * into combined table. In such case, convert it to split
4830 * table again and update type's shared key only when this is
4831 * the only dict sharing key with the type.
4832 *
4833 * This is to allow using shared key in class like this:
4834 *
4835 * class C:
4836 * def __init__(self):
4837 * # one dict resize happens
4838 * self.a, self.b, self.c = 1, 2, 3
4839 * self.d, self.e, self.f = 4, 5, 6
4840 * a = C()
4841 */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004842 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004843 CACHED_KEYS(tp) = make_keys_shared(dict);
Victor Stinner742da042016-09-07 17:40:12 -07004844 }
4845 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004846 CACHED_KEYS(tp) = NULL;
4847 }
INADA Naokia7576492018-11-14 18:39:27 +09004848 dictkeys_decref(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004849 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
4850 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004851 }
4852 }
4853 } else {
4854 dict = *dictptr;
4855 if (dict == NULL) {
4856 dict = PyDict_New();
4857 if (dict == NULL)
4858 return -1;
4859 *dictptr = dict;
4860 }
4861 if (value == NULL) {
4862 res = PyDict_DelItem(dict, key);
4863 } else {
4864 res = PyDict_SetItem(dict, key, value);
4865 }
4866 }
4867 return res;
4868}
4869
4870void
4871_PyDictKeys_DecRef(PyDictKeysObject *keys)
4872{
INADA Naokia7576492018-11-14 18:39:27 +09004873 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004874}