blob: faee6bc901eeba0f614dec7fdb60f174c352df7e [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
Victor Stinnerb4b53862020-05-05 19:55:29 +0200254#if PyDict_MAXFREELIST > 0
Christian Heimes2202f872008-02-06 14:31:34 +0000255static PyDictObject *free_list[PyDict_MAXFREELIST];
256static int numfree = 0;
Victor Stinner742da042016-09-07 17:40:12 -0700257static PyDictKeysObject *keys_free_list[PyDict_MAXFREELIST];
258static int numfreekeys = 0;
Victor Stinnerb4b53862020-05-05 19:55:29 +0200259#endif
Raymond Hettinger43442782004-03-17 21:55:03 +0000260
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300261#include "clinic/dictobject.c.h"
262
Victor Stinnerae00a5a2020-04-29 02:29:20 +0200263void
264_PyDict_ClearFreeList(void)
Christian Heimes77c02eb2008-02-09 02:18:51 +0000265{
Victor Stinnerb4b53862020-05-05 19:55:29 +0200266#if PyDict_MAXFREELIST > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 while (numfree) {
Victor Stinnerae00a5a2020-04-29 02:29:20 +0200268 PyDictObject *op = free_list[--numfree];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 assert(PyDict_CheckExact(op));
270 PyObject_GC_Del(op);
271 }
Victor Stinner742da042016-09-07 17:40:12 -0700272 while (numfreekeys) {
273 PyObject_FREE(keys_free_list[--numfreekeys]);
274 }
Victor Stinnerb4b53862020-05-05 19:55:29 +0200275#endif
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100276}
277
David Malcolm49526f42012-06-22 14:55:41 -0400278/* Print summary info about the state of the optimized allocator */
279void
280_PyDict_DebugMallocStats(FILE *out)
281{
Victor Stinnerb4b53862020-05-05 19:55:29 +0200282#if PyDict_MAXFREELIST > 0
David Malcolm49526f42012-06-22 14:55:41 -0400283 _PyDebugAllocatorStats(out,
284 "free PyDictObject", numfree, sizeof(PyDictObject));
Victor Stinnerb4b53862020-05-05 19:55:29 +0200285#endif
David Malcolm49526f42012-06-22 14:55:41 -0400286}
287
288
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100289void
Victor Stinnerbed48172019-08-27 00:12:32 +0200290_PyDict_Fini(void)
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100291{
Victor Stinnerae00a5a2020-04-29 02:29:20 +0200292 _PyDict_ClearFreeList();
Christian Heimes77c02eb2008-02-09 02:18:51 +0000293}
294
Victor Stinner742da042016-09-07 17:40:12 -0700295#define DK_SIZE(dk) ((dk)->dk_size)
296#if SIZEOF_VOID_P > 4
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700297#define DK_IXSIZE(dk) \
298 (DK_SIZE(dk) <= 0xff ? \
299 1 : DK_SIZE(dk) <= 0xffff ? \
300 2 : DK_SIZE(dk) <= 0xffffffff ? \
Benjamin Peterson3c569292016-09-08 13:16:41 -0700301 4 : sizeof(int64_t))
Victor Stinner742da042016-09-07 17:40:12 -0700302#else
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700303#define DK_IXSIZE(dk) \
304 (DK_SIZE(dk) <= 0xff ? \
305 1 : DK_SIZE(dk) <= 0xffff ? \
Benjamin Peterson3c569292016-09-08 13:16:41 -0700306 2 : sizeof(int32_t))
Victor Stinner742da042016-09-07 17:40:12 -0700307#endif
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700308#define DK_ENTRIES(dk) \
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700309 ((PyDictKeyEntry*)(&((int8_t*)((dk)->dk_indices))[DK_SIZE(dk) * DK_IXSIZE(dk)]))
Victor Stinner742da042016-09-07 17:40:12 -0700310
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400311#define DK_MASK(dk) (((dk)->dk_size)-1)
312#define IS_POWER_OF_2(x) (((x) & (x-1)) == 0)
313
INADA Naokia7576492018-11-14 18:39:27 +0900314static void free_keys_object(PyDictKeysObject *keys);
315
316static inline void
317dictkeys_incref(PyDictKeysObject *dk)
318{
Victor Stinner49932fe2020-02-03 17:55:05 +0100319#ifdef Py_REF_DEBUG
320 _Py_RefTotal++;
321#endif
INADA Naokia7576492018-11-14 18:39:27 +0900322 dk->dk_refcnt++;
323}
324
325static inline void
326dictkeys_decref(PyDictKeysObject *dk)
327{
328 assert(dk->dk_refcnt > 0);
Victor Stinner49932fe2020-02-03 17:55:05 +0100329#ifdef Py_REF_DEBUG
330 _Py_RefTotal--;
331#endif
INADA Naokia7576492018-11-14 18:39:27 +0900332 if (--dk->dk_refcnt == 0) {
333 free_keys_object(dk);
334 }
335}
336
Victor Stinner742da042016-09-07 17:40:12 -0700337/* lookup indices. returns DKIX_EMPTY, DKIX_DUMMY, or ix >=0 */
Benjamin Peterson73222252016-09-08 09:58:47 -0700338static inline Py_ssize_t
Andy Lester62d21c92020-03-25 23:13:01 -0500339dictkeys_get_index(const PyDictKeysObject *keys, Py_ssize_t i)
Victor Stinner742da042016-09-07 17:40:12 -0700340{
341 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700342 Py_ssize_t ix;
343
Victor Stinner742da042016-09-07 17:40:12 -0700344 if (s <= 0xff) {
Andy Lester62d21c92020-03-25 23:13:01 -0500345 const int8_t *indices = (const int8_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700346 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700347 }
348 else if (s <= 0xffff) {
Andy Lester62d21c92020-03-25 23:13:01 -0500349 const int16_t *indices = (const int16_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700350 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700351 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700352#if SIZEOF_VOID_P > 4
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300353 else if (s > 0xffffffff) {
Andy Lester62d21c92020-03-25 23:13:01 -0500354 const int64_t *indices = (const int64_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700355 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700356 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700357#endif
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300358 else {
Andy Lester62d21c92020-03-25 23:13:01 -0500359 const int32_t *indices = (const int32_t*)(keys->dk_indices);
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300360 ix = indices[i];
361 }
Victor Stinner71211e32016-09-08 10:52:46 -0700362 assert(ix >= DKIX_DUMMY);
363 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700364}
365
366/* write to indices. */
Benjamin Peterson73222252016-09-08 09:58:47 -0700367static inline void
INADA Naokia7576492018-11-14 18:39:27 +0900368dictkeys_set_index(PyDictKeysObject *keys, Py_ssize_t i, Py_ssize_t ix)
Victor Stinner742da042016-09-07 17:40:12 -0700369{
370 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700371
372 assert(ix >= DKIX_DUMMY);
373
Victor Stinner742da042016-09-07 17:40:12 -0700374 if (s <= 0xff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700375 int8_t *indices = (int8_t*)(keys->dk_indices);
Victor Stinner71211e32016-09-08 10:52:46 -0700376 assert(ix <= 0x7f);
Victor Stinner208857e2016-09-08 11:35:46 -0700377 indices[i] = (char)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700378 }
379 else if (s <= 0xffff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700380 int16_t *indices = (int16_t*)(keys->dk_indices);
Victor Stinner71211e32016-09-08 10:52:46 -0700381 assert(ix <= 0x7fff);
Victor Stinner208857e2016-09-08 11:35:46 -0700382 indices[i] = (int16_t)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700383 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700384#if SIZEOF_VOID_P > 4
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300385 else if (s > 0xffffffff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700386 int64_t *indices = (int64_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700387 indices[i] = ix;
Victor Stinner742da042016-09-07 17:40:12 -0700388 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700389#endif
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300390 else {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700391 int32_t *indices = (int32_t*)(keys->dk_indices);
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300392 assert(ix <= 0x7fffffff);
393 indices[i] = (int32_t)ix;
394 }
Victor Stinner742da042016-09-07 17:40:12 -0700395}
396
397
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200398/* USABLE_FRACTION is the maximum dictionary load.
Victor Stinner742da042016-09-07 17:40:12 -0700399 * Increasing this ratio makes dictionaries more dense resulting in more
400 * collisions. Decreasing it improves sparseness at the expense of spreading
401 * indices over more cache lines and at the cost of total memory consumed.
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200402 *
403 * USABLE_FRACTION must obey the following:
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400404 * (0 < USABLE_FRACTION(n) < n) for all n >= 2
405 *
Victor Stinner742da042016-09-07 17:40:12 -0700406 * USABLE_FRACTION should be quick to calculate.
407 * Fractions around 1/2 to 2/3 seem to work well in practice.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400408 */
Victor Stinner742da042016-09-07 17:40:12 -0700409#define USABLE_FRACTION(n) (((n) << 1)/3)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400410
Victor Stinner742da042016-09-07 17:40:12 -0700411/* ESTIMATE_SIZE is reverse function of USABLE_FRACTION.
412 * This can be used to reserve enough size to insert n entries without
413 * resizing.
414 */
INADA Naoki92c50ee2016-11-22 00:57:02 +0900415#define ESTIMATE_SIZE(n) (((n)*3+1) >> 1)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400416
Victor Stinner742da042016-09-07 17:40:12 -0700417/* Alternative fraction that is otherwise close enough to 2n/3 to make
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400418 * little difference. 8 * 2/3 == 8 * 5/8 == 5. 16 * 2/3 == 16 * 5/8 == 10.
419 * 32 * 2/3 = 21, 32 * 5/8 = 20.
420 * Its advantage is that it is faster to compute on machines with slow division.
421 * #define USABLE_FRACTION(n) (((n) >> 1) + ((n) >> 2) - ((n) >> 3))
Victor Stinner742da042016-09-07 17:40:12 -0700422 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400423
Victor Stinnera9f61a52013-07-16 22:17:26 +0200424/* GROWTH_RATE. Growth rate upon hitting maximum load.
INADA Naoki5fbc5112018-04-17 15:53:34 +0900425 * Currently set to used*3.
Victor Stinnera9f61a52013-07-16 22:17:26 +0200426 * This means that dicts double in size when growing without deletions,
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700427 * but have more head room when the number of deletions is on a par with the
INADA Naoki5fbc5112018-04-17 15:53:34 +0900428 * number of insertions. See also bpo-17563 and bpo-33205.
429 *
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700430 * GROWTH_RATE was set to used*4 up to version 3.2.
431 * GROWTH_RATE was set to used*2 in version 3.3.0
INADA Naoki5fbc5112018-04-17 15:53:34 +0900432 * GROWTH_RATE was set to used*2 + capacity/2 in 3.4.0-3.6.0.
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200433 */
INADA Naoki5fbc5112018-04-17 15:53:34 +0900434#define GROWTH_RATE(d) ((d)->ma_used*3)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400435
436#define ENSURE_ALLOWS_DELETIONS(d) \
437 if ((d)->ma_keys->dk_lookup == lookdict_unicode_nodummy) { \
438 (d)->ma_keys->dk_lookup = lookdict_unicode; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400440
441/* This immutable, empty PyDictKeysObject is used for PyDict_Clear()
442 * (which cannot fail and thus can do no allocation).
443 */
444static PyDictKeysObject empty_keys_struct = {
Serhiy Storchaka97932e42016-09-26 23:01:23 +0300445 1, /* dk_refcnt */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400446 1, /* dk_size */
447 lookdict_split, /* dk_lookup */
448 0, /* dk_usable (immutable) */
Victor Stinner742da042016-09-07 17:40:12 -0700449 0, /* dk_nentries */
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700450 {DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY,
451 DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY}, /* dk_indices */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400452};
453
454static PyObject *empty_values[1] = { NULL };
455
456#define Py_EMPTY_KEYS &empty_keys_struct
457
Victor Stinner611b0fa2016-09-14 15:02:01 +0200458/* Uncomment to check the dict content in _PyDict_CheckConsistency() */
459/* #define DEBUG_PYDICT */
460
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200461#ifdef DEBUG_PYDICT
462# define ASSERT_CONSISTENT(op) assert(_PyDict_CheckConsistency((PyObject *)(op), 1))
463#else
464# define ASSERT_CONSISTENT(op) assert(_PyDict_CheckConsistency((PyObject *)(op), 0))
465#endif
Victor Stinner611b0fa2016-09-14 15:02:01 +0200466
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200467
468int
469_PyDict_CheckConsistency(PyObject *op, int check_content)
Victor Stinner611b0fa2016-09-14 15:02:01 +0200470{
Victor Stinner68762572019-10-07 18:42:01 +0200471#define CHECK(expr) \
472 do { if (!(expr)) { _PyObject_ASSERT_FAILED_MSG(op, Py_STRINGIFY(expr)); } } while (0)
473
474 assert(op != NULL);
475 CHECK(PyDict_Check(op));
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200476 PyDictObject *mp = (PyDictObject *)op;
Victor Stinner50fe3f82018-10-26 18:47:15 +0200477
Victor Stinner611b0fa2016-09-14 15:02:01 +0200478 PyDictKeysObject *keys = mp->ma_keys;
479 int splitted = _PyDict_HasSplitTable(mp);
480 Py_ssize_t usable = USABLE_FRACTION(keys->dk_size);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200481
Victor Stinner68762572019-10-07 18:42:01 +0200482 CHECK(0 <= mp->ma_used && mp->ma_used <= usable);
483 CHECK(IS_POWER_OF_2(keys->dk_size));
484 CHECK(0 <= keys->dk_usable && keys->dk_usable <= usable);
485 CHECK(0 <= keys->dk_nentries && keys->dk_nentries <= usable);
486 CHECK(keys->dk_usable + keys->dk_nentries <= usable);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200487
488 if (!splitted) {
489 /* combined table */
Victor Stinner68762572019-10-07 18:42:01 +0200490 CHECK(keys->dk_refcnt == 1);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200491 }
492
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200493 if (check_content) {
494 PyDictKeyEntry *entries = DK_ENTRIES(keys);
495 Py_ssize_t i;
Victor Stinner611b0fa2016-09-14 15:02:01 +0200496
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200497 for (i=0; i < keys->dk_size; i++) {
498 Py_ssize_t ix = dictkeys_get_index(keys, i);
Victor Stinner68762572019-10-07 18:42:01 +0200499 CHECK(DKIX_DUMMY <= ix && ix <= usable);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200500 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200501
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200502 for (i=0; i < usable; i++) {
503 PyDictKeyEntry *entry = &entries[i];
504 PyObject *key = entry->me_key;
505
506 if (key != NULL) {
507 if (PyUnicode_CheckExact(key)) {
508 Py_hash_t hash = ((PyASCIIObject *)key)->hash;
Victor Stinner68762572019-10-07 18:42:01 +0200509 CHECK(hash != -1);
510 CHECK(entry->me_hash == hash);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200511 }
512 else {
513 /* test_dict fails if PyObject_Hash() is called again */
Victor Stinner68762572019-10-07 18:42:01 +0200514 CHECK(entry->me_hash != -1);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200515 }
516 if (!splitted) {
Victor Stinner68762572019-10-07 18:42:01 +0200517 CHECK(entry->me_value != NULL);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200518 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200519 }
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200520
521 if (splitted) {
Victor Stinner68762572019-10-07 18:42:01 +0200522 CHECK(entry->me_value == NULL);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200523 }
524 }
525
526 if (splitted) {
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200527 /* splitted table */
528 for (i=0; i < mp->ma_used; i++) {
Victor Stinner68762572019-10-07 18:42:01 +0200529 CHECK(mp->ma_values[i] != NULL);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200530 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200531 }
532 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200533 return 1;
Victor Stinner68762572019-10-07 18:42:01 +0200534
535#undef CHECK
Victor Stinner611b0fa2016-09-14 15:02:01 +0200536}
Victor Stinner611b0fa2016-09-14 15:02:01 +0200537
538
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400539static PyDictKeysObject *new_keys_object(Py_ssize_t size)
540{
541 PyDictKeysObject *dk;
Victor Stinner742da042016-09-07 17:40:12 -0700542 Py_ssize_t es, usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400543
Victor Stinner742da042016-09-07 17:40:12 -0700544 assert(size >= PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400545 assert(IS_POWER_OF_2(size));
Victor Stinner742da042016-09-07 17:40:12 -0700546
547 usable = USABLE_FRACTION(size);
548 if (size <= 0xff) {
549 es = 1;
550 }
551 else if (size <= 0xffff) {
552 es = 2;
553 }
554#if SIZEOF_VOID_P > 4
555 else if (size <= 0xffffffff) {
556 es = 4;
557 }
558#endif
559 else {
560 es = sizeof(Py_ssize_t);
561 }
562
Victor Stinnerb4b53862020-05-05 19:55:29 +0200563#if PyDict_MAXFREELIST > 0
Victor Stinner742da042016-09-07 17:40:12 -0700564 if (size == PyDict_MINSIZE && numfreekeys > 0) {
565 dk = keys_free_list[--numfreekeys];
566 }
Victor Stinnerb4b53862020-05-05 19:55:29 +0200567 else
568#endif
569 {
Victor Stinner98ee9d52016-09-08 09:33:56 -0700570 dk = PyObject_MALLOC(sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -0700571 + es * size
572 + sizeof(PyDictKeyEntry) * usable);
Victor Stinner742da042016-09-07 17:40:12 -0700573 if (dk == NULL) {
574 PyErr_NoMemory();
575 return NULL;
576 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400577 }
Victor Stinner49932fe2020-02-03 17:55:05 +0100578#ifdef Py_REF_DEBUG
579 _Py_RefTotal++;
580#endif
INADA Naokia7576492018-11-14 18:39:27 +0900581 dk->dk_refcnt = 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400582 dk->dk_size = size;
Victor Stinner742da042016-09-07 17:40:12 -0700583 dk->dk_usable = usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400584 dk->dk_lookup = lookdict_unicode_nodummy;
Victor Stinner742da042016-09-07 17:40:12 -0700585 dk->dk_nentries = 0;
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700586 memset(&dk->dk_indices[0], 0xff, es * size);
Victor Stinner742da042016-09-07 17:40:12 -0700587 memset(DK_ENTRIES(dk), 0, sizeof(PyDictKeyEntry) * usable);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400588 return dk;
589}
590
591static void
592free_keys_object(PyDictKeysObject *keys)
593{
Victor Stinner742da042016-09-07 17:40:12 -0700594 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400595 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -0700596 for (i = 0, n = keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400597 Py_XDECREF(entries[i].me_key);
598 Py_XDECREF(entries[i].me_value);
599 }
Victor Stinnerb4b53862020-05-05 19:55:29 +0200600#if PyDict_MAXFREELIST > 0
Victor Stinner742da042016-09-07 17:40:12 -0700601 if (keys->dk_size == PyDict_MINSIZE && numfreekeys < PyDict_MAXFREELIST) {
602 keys_free_list[numfreekeys++] = keys;
603 return;
604 }
Victor Stinnerb4b53862020-05-05 19:55:29 +0200605#endif
Raymond Hettingerce5179f2016-01-31 08:56:21 -0800606 PyObject_FREE(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400607}
608
609#define new_values(size) PyMem_NEW(PyObject *, size)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400610#define free_values(values) PyMem_FREE(values)
611
612/* Consumes a reference to the keys object */
613static PyObject *
614new_dict(PyDictKeysObject *keys, PyObject **values)
615{
616 PyDictObject *mp;
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200617 assert(keys != NULL);
Victor Stinnerb4b53862020-05-05 19:55:29 +0200618#if PyDict_MAXFREELIST > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 if (numfree) {
620 mp = free_list[--numfree];
621 assert (mp != NULL);
Dong-hee Na1b55b652020-02-17 19:09:15 +0900622 assert (Py_IS_TYPE(mp, &PyDict_Type));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 _Py_NewReference((PyObject *)mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 }
Victor Stinnerb4b53862020-05-05 19:55:29 +0200625 else
626#endif
627 {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400628 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
629 if (mp == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +0900630 dictkeys_decref(keys);
Zackery Spytz3d07c1e2019-03-23 20:23:29 -0600631 if (values != empty_values) {
632 free_values(values);
633 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400634 return NULL;
635 }
636 }
637 mp->ma_keys = keys;
638 mp->ma_values = values;
639 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -0700640 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200641 ASSERT_CONSISTENT(mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000643}
644
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400645/* Consumes a reference to the keys object */
646static PyObject *
647new_dict_with_shared_keys(PyDictKeysObject *keys)
648{
649 PyObject **values;
650 Py_ssize_t i, size;
651
Victor Stinner742da042016-09-07 17:40:12 -0700652 size = USABLE_FRACTION(DK_SIZE(keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400653 values = new_values(size);
654 if (values == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +0900655 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400656 return PyErr_NoMemory();
657 }
658 for (i = 0; i < size; i++) {
659 values[i] = NULL;
660 }
661 return new_dict(keys, values);
662}
663
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500664
665static PyObject *
666clone_combined_dict(PyDictObject *orig)
667{
668 assert(PyDict_CheckExact(orig));
669 assert(orig->ma_values == NULL);
670 assert(orig->ma_keys->dk_refcnt == 1);
671
672 Py_ssize_t keys_size = _PyDict_KeysSize(orig->ma_keys);
673 PyDictKeysObject *keys = PyObject_Malloc(keys_size);
674 if (keys == NULL) {
675 PyErr_NoMemory();
676 return NULL;
677 }
678
679 memcpy(keys, orig->ma_keys, keys_size);
680
681 /* After copying key/value pairs, we need to incref all
682 keys and values and they are about to be co-owned by a
683 new dict object. */
684 PyDictKeyEntry *ep0 = DK_ENTRIES(keys);
685 Py_ssize_t n = keys->dk_nentries;
686 for (Py_ssize_t i = 0; i < n; i++) {
687 PyDictKeyEntry *entry = &ep0[i];
688 PyObject *value = entry->me_value;
689 if (value != NULL) {
690 Py_INCREF(value);
691 Py_INCREF(entry->me_key);
692 }
693 }
694
695 PyDictObject *new = (PyDictObject *)new_dict(keys, NULL);
696 if (new == NULL) {
697 /* In case of an error, `new_dict()` takes care of
698 cleaning up `keys`. */
699 return NULL;
700 }
701 new->ma_used = orig->ma_used;
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200702 ASSERT_CONSISTENT(new);
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500703 if (_PyObject_GC_IS_TRACKED(orig)) {
704 /* Maintain tracking. */
705 _PyObject_GC_TRACK(new);
706 }
Yury Selivanov0b752282018-07-06 12:20:07 -0400707
708 /* Since we copied the keys table we now have an extra reference
Victor Stinner49932fe2020-02-03 17:55:05 +0100709 in the system. Manually call increment _Py_RefTotal to signal that
INADA Naokia7576492018-11-14 18:39:27 +0900710 we have it now; calling dictkeys_incref would be an error as
Yury Selivanov0b752282018-07-06 12:20:07 -0400711 keys->dk_refcnt is already set to 1 (after memcpy). */
Victor Stinner49932fe2020-02-03 17:55:05 +0100712#ifdef Py_REF_DEBUG
713 _Py_RefTotal++;
714#endif
Yury Selivanov0b752282018-07-06 12:20:07 -0400715
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500716 return (PyObject *)new;
717}
718
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400719PyObject *
720PyDict_New(void)
721{
Inada Naokif2a18672019-03-12 17:25:44 +0900722 dictkeys_incref(Py_EMPTY_KEYS);
723 return new_dict(Py_EMPTY_KEYS, empty_values);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400724}
725
Victor Stinner742da042016-09-07 17:40:12 -0700726/* Search index of hash table from offset of entry table */
727static Py_ssize_t
728lookdict_index(PyDictKeysObject *k, Py_hash_t hash, Py_ssize_t index)
729{
Victor Stinner742da042016-09-07 17:40:12 -0700730 size_t mask = DK_MASK(k);
INADA Naoki073ae482017-06-23 15:22:50 +0900731 size_t perturb = (size_t)hash;
732 size_t i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700733
INADA Naoki073ae482017-06-23 15:22:50 +0900734 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900735 Py_ssize_t ix = dictkeys_get_index(k, i);
Victor Stinner742da042016-09-07 17:40:12 -0700736 if (ix == index) {
737 return i;
738 }
739 if (ix == DKIX_EMPTY) {
740 return DKIX_EMPTY;
741 }
INADA Naoki073ae482017-06-23 15:22:50 +0900742 perturb >>= PERTURB_SHIFT;
743 i = mask & (i*5 + perturb + 1);
Victor Stinner742da042016-09-07 17:40:12 -0700744 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700745 Py_UNREACHABLE();
Victor Stinner742da042016-09-07 17:40:12 -0700746}
747
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000748/*
749The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000750This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000751Open addressing is preferred over chaining since the link overhead for
752chaining would be substantial (100% with typical malloc overhead).
753
Tim Peterseb28ef22001-06-02 05:27:19 +0000754The initial probe index is computed as hash mod the table size. Subsequent
755probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000756
757All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000758
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000759The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000760contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000761Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000762
Victor Stinner742da042016-09-07 17:40:12 -0700763lookdict() is general-purpose, and may return DKIX_ERROR if (and only if) a
Victor Stinnera4348cc2016-09-08 12:01:25 -0700764comparison raises an exception.
Guido van Rossum89d8c602007-09-18 17:26:56 +0000765lookdict_unicode() below is specialized to string keys, comparison of which can
INADA Naoki1b8df102017-02-20 22:48:10 +0900766never raise an exception; that function can never return DKIX_ERROR when key
767is string. Otherwise, it falls back to lookdict().
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400768lookdict_unicode_nodummy is further specialized for string keys that cannot be
769the <dummy> value.
INADA Naoki778928b2017-08-03 23:45:15 +0900770For both, when the key isn't found a DKIX_EMPTY is returned.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000771*/
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100772static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400773lookdict(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900774 Py_hash_t hash, PyObject **value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000775{
INADA Naoki778928b2017-08-03 23:45:15 +0900776 size_t i, mask, perturb;
Victor Stinner742da042016-09-07 17:40:12 -0700777 PyDictKeysObject *dk;
INADA Naoki778928b2017-08-03 23:45:15 +0900778 PyDictKeyEntry *ep0;
Tim Peterseb28ef22001-06-02 05:27:19 +0000779
Antoine Pitrou9a234902012-05-13 20:48:01 +0200780top:
Victor Stinner742da042016-09-07 17:40:12 -0700781 dk = mp->ma_keys;
Victor Stinner742da042016-09-07 17:40:12 -0700782 ep0 = DK_ENTRIES(dk);
INADA Naoki778928b2017-08-03 23:45:15 +0900783 mask = DK_MASK(dk);
784 perturb = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700786
INADA Naoki778928b2017-08-03 23:45:15 +0900787 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900788 Py_ssize_t ix = dictkeys_get_index(dk, i);
Victor Stinner742da042016-09-07 17:40:12 -0700789 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700790 *value_addr = NULL;
791 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400792 }
INADA Naoki778928b2017-08-03 23:45:15 +0900793 if (ix >= 0) {
794 PyDictKeyEntry *ep = &ep0[ix];
795 assert(ep->me_key != NULL);
796 if (ep->me_key == key) {
797 *value_addr = ep->me_value;
798 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700799 }
INADA Naoki778928b2017-08-03 23:45:15 +0900800 if (ep->me_hash == hash) {
801 PyObject *startkey = ep->me_key;
802 Py_INCREF(startkey);
803 int cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
804 Py_DECREF(startkey);
805 if (cmp < 0) {
806 *value_addr = NULL;
807 return DKIX_ERROR;
808 }
809 if (dk == mp->ma_keys && ep->me_key == startkey) {
810 if (cmp > 0) {
811 *value_addr = ep->me_value;
812 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700813 }
INADA Naoki778928b2017-08-03 23:45:15 +0900814 }
815 else {
816 /* The dict was mutated, restart */
817 goto top;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400818 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 }
INADA Naoki778928b2017-08-03 23:45:15 +0900821 perturb >>= PERTURB_SHIFT;
822 i = (i*5 + perturb + 1) & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700824 Py_UNREACHABLE();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000825}
826
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400827/* Specialized version for string-only keys */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100828static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400829lookdict_unicode(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900830 Py_hash_t hash, PyObject **value_addr)
Fred Drake1bff34a2000-08-31 19:31:38 +0000831{
Victor Stinner742da042016-09-07 17:40:12 -0700832 assert(mp->ma_values == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 /* Make sure this function doesn't have to handle non-unicode keys,
834 including subclasses of str; e.g., one reason to subclass
835 unicodes is to override __eq__, and for speed we don't cater to
836 that here. */
837 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400838 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900839 return lookdict(mp, key, hash, value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 }
Tim Peters15d49292001-05-27 07:39:22 +0000841
INADA Naoki778928b2017-08-03 23:45:15 +0900842 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
843 size_t mask = DK_MASK(mp->ma_keys);
844 size_t perturb = (size_t)hash;
845 size_t i = (size_t)hash & mask;
846
847 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900848 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
Victor Stinner742da042016-09-07 17:40:12 -0700849 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700850 *value_addr = NULL;
851 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400852 }
INADA Naoki778928b2017-08-03 23:45:15 +0900853 if (ix >= 0) {
854 PyDictKeyEntry *ep = &ep0[ix];
855 assert(ep->me_key != NULL);
856 assert(PyUnicode_CheckExact(ep->me_key));
857 if (ep->me_key == key ||
858 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
859 *value_addr = ep->me_value;
860 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700861 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400862 }
INADA Naoki778928b2017-08-03 23:45:15 +0900863 perturb >>= PERTURB_SHIFT;
864 i = mask & (i*5 + perturb + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700866 Py_UNREACHABLE();
Fred Drake1bff34a2000-08-31 19:31:38 +0000867}
868
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400869/* Faster version of lookdict_unicode when it is known that no <dummy> keys
870 * will be present. */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100871static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400872lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900873 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400874{
Victor Stinner742da042016-09-07 17:40:12 -0700875 assert(mp->ma_values == NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400876 /* Make sure this function doesn't have to handle non-unicode keys,
877 including subclasses of str; e.g., one reason to subclass
878 unicodes is to override __eq__, and for speed we don't cater to
879 that here. */
880 if (!PyUnicode_CheckExact(key)) {
881 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900882 return lookdict(mp, key, hash, value_addr);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400883 }
INADA Naoki778928b2017-08-03 23:45:15 +0900884
885 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
886 size_t mask = DK_MASK(mp->ma_keys);
887 size_t perturb = (size_t)hash;
888 size_t i = (size_t)hash & mask;
889
890 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900891 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
Victor Stinner742da042016-09-07 17:40:12 -0700892 assert (ix != DKIX_DUMMY);
893 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700894 *value_addr = NULL;
895 return DKIX_EMPTY;
896 }
INADA Naoki778928b2017-08-03 23:45:15 +0900897 PyDictKeyEntry *ep = &ep0[ix];
898 assert(ep->me_key != NULL);
899 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700900 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400901 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900902 *value_addr = ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700903 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400904 }
INADA Naoki778928b2017-08-03 23:45:15 +0900905 perturb >>= PERTURB_SHIFT;
906 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400907 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700908 Py_UNREACHABLE();
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400909}
910
911/* Version of lookdict for split tables.
912 * All split tables and only split tables use this lookup function.
913 * Split tables only contain unicode keys and no dummy keys,
914 * so algorithm is the same as lookdict_unicode_nodummy.
915 */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100916static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400917lookdict_split(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900918 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400919{
Victor Stinner742da042016-09-07 17:40:12 -0700920 /* mp must split table */
921 assert(mp->ma_values != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400922 if (!PyUnicode_CheckExact(key)) {
INADA Naoki778928b2017-08-03 23:45:15 +0900923 Py_ssize_t ix = lookdict(mp, key, hash, value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700924 if (ix >= 0) {
INADA Naokiba609772016-12-07 20:41:42 +0900925 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700926 }
927 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400928 }
Victor Stinner742da042016-09-07 17:40:12 -0700929
INADA Naoki778928b2017-08-03 23:45:15 +0900930 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
931 size_t mask = DK_MASK(mp->ma_keys);
932 size_t perturb = (size_t)hash;
933 size_t i = (size_t)hash & mask;
934
935 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900936 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
INADA Naoki778928b2017-08-03 23:45:15 +0900937 assert (ix != DKIX_DUMMY);
Victor Stinner742da042016-09-07 17:40:12 -0700938 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700939 *value_addr = NULL;
940 return DKIX_EMPTY;
941 }
INADA Naoki778928b2017-08-03 23:45:15 +0900942 PyDictKeyEntry *ep = &ep0[ix];
943 assert(ep->me_key != NULL);
944 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700945 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400946 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900947 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700948 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400949 }
INADA Naoki778928b2017-08-03 23:45:15 +0900950 perturb >>= PERTURB_SHIFT;
951 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400952 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700953 Py_UNREACHABLE();
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400954}
955
Benjamin Petersonfb886362010-04-24 18:21:17 +0000956int
957_PyDict_HasOnlyStringKeys(PyObject *dict)
958{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 Py_ssize_t pos = 0;
960 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000961 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400963 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 return 1;
965 while (PyDict_Next(dict, &pos, &key, &value))
966 if (!PyUnicode_Check(key))
967 return 0;
968 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000969}
970
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000971#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 do { \
973 if (!_PyObject_GC_IS_TRACKED(mp)) { \
974 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
975 _PyObject_GC_MAY_BE_TRACKED(value)) { \
976 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 } \
978 } \
979 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000980
981void
982_PyDict_MaybeUntrack(PyObject *op)
983{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 PyDictObject *mp;
985 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -0700986 Py_ssize_t i, numentries;
987 PyDictKeyEntry *ep0;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
990 return;
991
992 mp = (PyDictObject *) op;
Victor Stinner742da042016-09-07 17:40:12 -0700993 ep0 = DK_ENTRIES(mp->ma_keys);
994 numentries = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400995 if (_PyDict_HasSplitTable(mp)) {
Victor Stinner742da042016-09-07 17:40:12 -0700996 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400997 if ((value = mp->ma_values[i]) == NULL)
998 continue;
999 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
Victor Stinner742da042016-09-07 17:40:12 -07001000 assert(!_PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001001 return;
1002 }
1003 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001005 else {
Victor Stinner742da042016-09-07 17:40:12 -07001006 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001007 if ((value = ep0[i].me_value) == NULL)
1008 continue;
1009 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
1010 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
1011 return;
1012 }
1013 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001015}
1016
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001017/* Internal function to find slot for an item from its hash
Victor Stinner3c336c52016-09-12 14:17:40 +02001018 when it is known that the key is not present in the dict.
1019
1020 The dict must be combined. */
INADA Naokiba609772016-12-07 20:41:42 +09001021static Py_ssize_t
INADA Naoki778928b2017-08-03 23:45:15 +09001022find_empty_slot(PyDictKeysObject *keys, Py_hash_t hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001023{
INADA Naoki778928b2017-08-03 23:45:15 +09001024 assert(keys != NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001025
INADA Naoki778928b2017-08-03 23:45:15 +09001026 const size_t mask = DK_MASK(keys);
1027 size_t i = hash & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001028 Py_ssize_t ix = dictkeys_get_index(keys, i);
INADA Naoki778928b2017-08-03 23:45:15 +09001029 for (size_t perturb = hash; ix >= 0;) {
INADA Naoki267941c2016-10-06 15:19:07 +09001030 perturb >>= PERTURB_SHIFT;
INADA Naoki778928b2017-08-03 23:45:15 +09001031 i = (i*5 + perturb + 1) & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001032 ix = dictkeys_get_index(keys, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 }
INADA Naoki778928b2017-08-03 23:45:15 +09001034 return i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001035}
1036
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001037static int
1038insertion_resize(PyDictObject *mp)
1039{
Raymond Hettinger36f74aa2013-05-17 03:01:13 -07001040 return dictresize(mp, GROWTH_RATE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001041}
Antoine Pitroue965d972012-02-27 00:45:12 +01001042
1043/*
1044Internal routine to insert a new item into the table.
1045Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +01001046Returns -1 if an error occurred, or 0 on success.
1047*/
1048static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001049insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001050{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001051 PyObject *old_value;
INADA Naokiba609772016-12-07 20:41:42 +09001052 PyDictKeyEntry *ep;
Antoine Pitroue965d972012-02-27 00:45:12 +01001053
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001054 Py_INCREF(key);
1055 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001056 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
1057 if (insertion_resize(mp) < 0)
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001058 goto Fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001059 }
1060
INADA Naoki778928b2017-08-03 23:45:15 +09001061 Py_ssize_t ix = mp->ma_keys->dk_lookup(mp, key, hash, &old_value);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001062 if (ix == DKIX_ERROR)
1063 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001064
Antoine Pitroud6967322014-10-18 00:35:00 +02001065 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001066 MAINTAIN_TRACKING(mp, key, value);
Victor Stinner742da042016-09-07 17:40:12 -07001067
1068 /* When insertion order is different from shared key, we can't share
1069 * the key anymore. Convert this instance to combine table.
1070 */
1071 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09001072 ((ix >= 0 && old_value == NULL && mp->ma_used != ix) ||
Victor Stinner742da042016-09-07 17:40:12 -07001073 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001074 if (insertion_resize(mp) < 0)
1075 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001076 ix = DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001077 }
Victor Stinner742da042016-09-07 17:40:12 -07001078
1079 if (ix == DKIX_EMPTY) {
1080 /* Insert into new slot. */
INADA Naokiba609772016-12-07 20:41:42 +09001081 assert(old_value == NULL);
Victor Stinner742da042016-09-07 17:40:12 -07001082 if (mp->ma_keys->dk_usable <= 0) {
1083 /* Need to resize. */
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001084 if (insertion_resize(mp) < 0)
1085 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001086 }
INADA Naoki778928b2017-08-03 23:45:15 +09001087 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naokiba609772016-12-07 20:41:42 +09001088 ep = &DK_ENTRIES(mp->ma_keys)[mp->ma_keys->dk_nentries];
INADA Naokia7576492018-11-14 18:39:27 +09001089 dictkeys_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Victor Stinner742da042016-09-07 17:40:12 -07001090 ep->me_key = key;
1091 ep->me_hash = hash;
1092 if (mp->ma_values) {
1093 assert (mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
1094 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001095 }
1096 else {
Victor Stinner742da042016-09-07 17:40:12 -07001097 ep->me_value = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001098 }
1099 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001100 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07001101 mp->ma_keys->dk_usable--;
1102 mp->ma_keys->dk_nentries++;
1103 assert(mp->ma_keys->dk_usable >= 0);
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001104 ASSERT_CONSISTENT(mp);
Victor Stinner742da042016-09-07 17:40:12 -07001105 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001106 }
Victor Stinner742da042016-09-07 17:40:12 -07001107
Inada Naoki91234a12019-06-03 21:30:58 +09001108 if (old_value != value) {
1109 if (_PyDict_HasSplitTable(mp)) {
1110 mp->ma_values[ix] = value;
1111 if (old_value == NULL) {
1112 /* pending state */
1113 assert(ix == mp->ma_used);
1114 mp->ma_used++;
1115 }
INADA Naokiba609772016-12-07 20:41:42 +09001116 }
Inada Naoki91234a12019-06-03 21:30:58 +09001117 else {
1118 assert(old_value != NULL);
1119 DK_ENTRIES(mp->ma_keys)[ix].me_value = value;
1120 }
1121 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokiba609772016-12-07 20:41:42 +09001122 }
INADA Naokiba609772016-12-07 20:41:42 +09001123 Py_XDECREF(old_value); /* which **CAN** re-enter (see issue #22653) */
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001124 ASSERT_CONSISTENT(mp);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001125 Py_DECREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001126 return 0;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001127
1128Fail:
1129 Py_DECREF(value);
1130 Py_DECREF(key);
1131 return -1;
Antoine Pitroue965d972012-02-27 00:45:12 +01001132}
1133
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001134// Same to insertdict but specialized for ma_keys = Py_EMPTY_KEYS.
1135static int
1136insert_to_emptydict(PyDictObject *mp, PyObject *key, Py_hash_t hash,
1137 PyObject *value)
1138{
1139 assert(mp->ma_keys == Py_EMPTY_KEYS);
1140
1141 PyDictKeysObject *newkeys = new_keys_object(PyDict_MINSIZE);
1142 if (newkeys == NULL) {
1143 return -1;
1144 }
1145 if (!PyUnicode_CheckExact(key)) {
1146 newkeys->dk_lookup = lookdict;
1147 }
1148 dictkeys_decref(Py_EMPTY_KEYS);
1149 mp->ma_keys = newkeys;
1150 mp->ma_values = NULL;
1151
1152 Py_INCREF(key);
1153 Py_INCREF(value);
1154 MAINTAIN_TRACKING(mp, key, value);
1155
1156 size_t hashpos = (size_t)hash & (PyDict_MINSIZE-1);
Dong-hee Nac39d1dd2019-10-11 17:43:11 +09001157 PyDictKeyEntry *ep = DK_ENTRIES(mp->ma_keys);
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001158 dictkeys_set_index(mp->ma_keys, hashpos, 0);
1159 ep->me_key = key;
1160 ep->me_hash = hash;
1161 ep->me_value = value;
1162 mp->ma_used++;
1163 mp->ma_version_tag = DICT_NEXT_VERSION();
1164 mp->ma_keys->dk_usable--;
1165 mp->ma_keys->dk_nentries++;
1166 return 0;
1167}
1168
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001169/*
luzpaza5293b42017-11-05 07:37:50 -06001170Internal routine used by dictresize() to build a hashtable of entries.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001171*/
1172static void
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001173build_indices(PyDictKeysObject *keys, PyDictKeyEntry *ep, Py_ssize_t n)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001174{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001175 size_t mask = (size_t)DK_SIZE(keys) - 1;
1176 for (Py_ssize_t ix = 0; ix != n; ix++, ep++) {
1177 Py_hash_t hash = ep->me_hash;
1178 size_t i = hash & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001179 for (size_t perturb = hash; dictkeys_get_index(keys, i) != DKIX_EMPTY;) {
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001180 perturb >>= PERTURB_SHIFT;
INADA Naoki870c2862017-06-24 09:03:19 +09001181 i = mask & (i*5 + perturb + 1);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001182 }
INADA Naokia7576492018-11-14 18:39:27 +09001183 dictkeys_set_index(keys, i, ix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001185}
1186
1187/*
1188Restructure the table by allocating a new table and reinserting all
1189items again. When entries have been deleted, the new table may
1190actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001191If a table is split (its keys and hashes are shared, its values are not),
1192then the values are temporarily copied into the table, it is resized as
1193a combined table, then the me_value slots in the old table are NULLed out.
1194After resizing a table is always combined,
1195but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001196*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001197static int
Victor Stinner3d3f2642016-12-15 17:21:23 +01001198dictresize(PyDictObject *mp, Py_ssize_t minsize)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001199{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001200 Py_ssize_t newsize, numentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001201 PyDictKeysObject *oldkeys;
1202 PyObject **oldvalues;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001203 PyDictKeyEntry *oldentries, *newentries;
Tim Peters91a364d2001-05-19 07:04:38 +00001204
Victor Stinner742da042016-09-07 17:40:12 -07001205 /* Find the smallest table size > minused. */
1206 for (newsize = PyDict_MINSIZE;
Victor Stinner3d3f2642016-12-15 17:21:23 +01001207 newsize < minsize && newsize > 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 newsize <<= 1)
1209 ;
1210 if (newsize <= 0) {
1211 PyErr_NoMemory();
1212 return -1;
1213 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001214
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001215 oldkeys = mp->ma_keys;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001216
1217 /* NOTE: Current odict checks mp->ma_keys to detect resize happen.
1218 * So we can't reuse oldkeys even if oldkeys->dk_size == newsize.
1219 * TODO: Try reusing oldkeys when reimplement odict.
1220 */
1221
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001222 /* Allocate a new table. */
1223 mp->ma_keys = new_keys_object(newsize);
1224 if (mp->ma_keys == NULL) {
1225 mp->ma_keys = oldkeys;
1226 return -1;
1227 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01001228 // New table must be large enough.
1229 assert(mp->ma_keys->dk_usable >= mp->ma_used);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001230 if (oldkeys->dk_lookup == lookdict)
1231 mp->ma_keys->dk_lookup = lookdict;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001232
1233 numentries = mp->ma_used;
1234 oldentries = DK_ENTRIES(oldkeys);
1235 newentries = DK_ENTRIES(mp->ma_keys);
1236 oldvalues = mp->ma_values;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001237 if (oldvalues != NULL) {
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001238 /* Convert split table into new combined table.
1239 * We must incref keys; we can transfer values.
1240 * Note that values of split table is always dense.
1241 */
1242 for (Py_ssize_t i = 0; i < numentries; i++) {
1243 assert(oldvalues[i] != NULL);
1244 PyDictKeyEntry *ep = &oldentries[i];
1245 PyObject *key = ep->me_key;
1246 Py_INCREF(key);
1247 newentries[i].me_key = key;
1248 newentries[i].me_hash = ep->me_hash;
1249 newentries[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001251
INADA Naokia7576492018-11-14 18:39:27 +09001252 dictkeys_decref(oldkeys);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001253 mp->ma_values = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001254 if (oldvalues != empty_values) {
1255 free_values(oldvalues);
1256 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001257 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001258 else { // combined table.
1259 if (oldkeys->dk_nentries == numentries) {
1260 memcpy(newentries, oldentries, numentries * sizeof(PyDictKeyEntry));
1261 }
1262 else {
1263 PyDictKeyEntry *ep = oldentries;
1264 for (Py_ssize_t i = 0; i < numentries; i++) {
1265 while (ep->me_value == NULL)
1266 ep++;
1267 newentries[i] = *ep++;
1268 }
1269 }
1270
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001271 assert(oldkeys->dk_lookup != lookdict_split);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001272 assert(oldkeys->dk_refcnt == 1);
Victor Stinner49932fe2020-02-03 17:55:05 +01001273#ifdef Py_REF_DEBUG
1274 _Py_RefTotal--;
1275#endif
Victor Stinnerb4b53862020-05-05 19:55:29 +02001276#if PyDict_MAXFREELIST > 0
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001277 if (oldkeys->dk_size == PyDict_MINSIZE &&
Victor Stinner49932fe2020-02-03 17:55:05 +01001278 numfreekeys < PyDict_MAXFREELIST)
1279 {
INADA Naokia7576492018-11-14 18:39:27 +09001280 keys_free_list[numfreekeys++] = oldkeys;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001281 }
Victor Stinnerb4b53862020-05-05 19:55:29 +02001282 else
1283#endif
1284 {
INADA Naokia7576492018-11-14 18:39:27 +09001285 PyObject_FREE(oldkeys);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001286 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001287 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001288
1289 build_indices(mp->ma_keys, newentries, numentries);
1290 mp->ma_keys->dk_usable -= numentries;
1291 mp->ma_keys->dk_nentries = numentries;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001293}
1294
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001295/* Returns NULL if unable to split table.
1296 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001297static PyDictKeysObject *
1298make_keys_shared(PyObject *op)
1299{
1300 Py_ssize_t i;
1301 Py_ssize_t size;
1302 PyDictObject *mp = (PyDictObject *)op;
1303
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001304 if (!PyDict_CheckExact(op))
1305 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001306 if (!_PyDict_HasSplitTable(mp)) {
1307 PyDictKeyEntry *ep0;
1308 PyObject **values;
1309 assert(mp->ma_keys->dk_refcnt == 1);
1310 if (mp->ma_keys->dk_lookup == lookdict) {
1311 return NULL;
1312 }
1313 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
1314 /* Remove dummy keys */
1315 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
1316 return NULL;
1317 }
1318 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
1319 /* Copy values into a new array */
Victor Stinner742da042016-09-07 17:40:12 -07001320 ep0 = DK_ENTRIES(mp->ma_keys);
1321 size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001322 values = new_values(size);
1323 if (values == NULL) {
1324 PyErr_SetString(PyExc_MemoryError,
1325 "Not enough memory to allocate new values array");
1326 return NULL;
1327 }
1328 for (i = 0; i < size; i++) {
1329 values[i] = ep0[i].me_value;
1330 ep0[i].me_value = NULL;
1331 }
1332 mp->ma_keys->dk_lookup = lookdict_split;
1333 mp->ma_values = values;
1334 }
INADA Naokia7576492018-11-14 18:39:27 +09001335 dictkeys_incref(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001336 return mp->ma_keys;
1337}
Christian Heimes99170a52007-12-19 02:07:34 +00001338
1339PyObject *
1340_PyDict_NewPresized(Py_ssize_t minused)
1341{
INADA Naoki92c50ee2016-11-22 00:57:02 +09001342 const Py_ssize_t max_presize = 128 * 1024;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001343 Py_ssize_t newsize;
1344 PyDictKeysObject *new_keys;
INADA Naoki92c50ee2016-11-22 00:57:02 +09001345
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001346 if (minused <= USABLE_FRACTION(PyDict_MINSIZE)) {
Inada Naokif2a18672019-03-12 17:25:44 +09001347 return PyDict_New();
1348 }
INADA Naoki92c50ee2016-11-22 00:57:02 +09001349 /* There are no strict guarantee that returned dict can contain minused
1350 * items without resize. So we create medium size dict instead of very
1351 * large dict or MemoryError.
1352 */
1353 if (minused > USABLE_FRACTION(max_presize)) {
1354 newsize = max_presize;
1355 }
1356 else {
1357 Py_ssize_t minsize = ESTIMATE_SIZE(minused);
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001358 newsize = PyDict_MINSIZE*2;
INADA Naoki92c50ee2016-11-22 00:57:02 +09001359 while (newsize < minsize) {
1360 newsize <<= 1;
1361 }
1362 }
1363 assert(IS_POWER_OF_2(newsize));
1364
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001365 new_keys = new_keys_object(newsize);
1366 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001368 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001369}
1370
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001371/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1372 * that may occur (originally dicts supported only string keys, and exceptions
1373 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001374 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001375 * (suppressed) occurred while computing the key's hash, or that some error
1376 * (suppressed) occurred when comparing keys in the dict's internal probe
1377 * sequence. A nasty example of the latter is when a Python-coded comparison
1378 * function hits a stack-depth error, which can cause this to return NULL
1379 * even if the key is present.
1380 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001381PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001382PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001383{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001384 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07001385 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 PyDictObject *mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 PyThreadState *tstate;
INADA Naokiba609772016-12-07 20:41:42 +09001388 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 if (!PyDict_Check(op))
1391 return NULL;
1392 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001393 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 {
1395 hash = PyObject_Hash(key);
1396 if (hash == -1) {
1397 PyErr_Clear();
1398 return NULL;
1399 }
1400 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 /* We can arrive here with a NULL tstate during initialization: try
1403 running "python -Wi" for an example related to string interning.
1404 Let's just hope that no exception occurs then... This must be
Victor Stinner50b48572018-11-01 01:51:40 +01001405 _PyThreadState_GET() and not PyThreadState_Get() because the latter
Victor Stinner9204fb82018-10-30 15:13:17 +01001406 abort Python if tstate is NULL. */
Victor Stinner50b48572018-11-01 01:51:40 +01001407 tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 if (tstate != NULL && tstate->curexc_type != NULL) {
1409 /* preserve the existing exception */
1410 PyObject *err_type, *err_value, *err_tb;
1411 PyErr_Fetch(&err_type, &err_value, &err_tb);
INADA Naoki778928b2017-08-03 23:45:15 +09001412 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 /* ignore errors */
1414 PyErr_Restore(err_type, err_value, err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001415 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 return NULL;
1417 }
1418 else {
INADA Naoki778928b2017-08-03 23:45:15 +09001419 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001420 if (ix < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 PyErr_Clear();
1422 return NULL;
1423 }
1424 }
INADA Naokiba609772016-12-07 20:41:42 +09001425 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001426}
1427
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001428/* Same as PyDict_GetItemWithError() but with hash supplied by caller.
1429 This returns NULL *with* an exception set if an exception occurred.
1430 It returns NULL *without* an exception set if the key wasn't present.
1431*/
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001432PyObject *
1433_PyDict_GetItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1434{
Victor Stinner742da042016-09-07 17:40:12 -07001435 Py_ssize_t ix;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001436 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001437 PyObject *value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001438
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001439 if (!PyDict_Check(op)) {
1440 PyErr_BadInternalCall();
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001441 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001442 }
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001443
INADA Naoki778928b2017-08-03 23:45:15 +09001444 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001445 if (ix < 0) {
1446 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001447 }
INADA Naokiba609772016-12-07 20:41:42 +09001448 return value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001449}
1450
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001451/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1452 This returns NULL *with* an exception set if an exception occurred.
1453 It returns NULL *without* an exception set if the key wasn't present.
1454*/
1455PyObject *
1456PyDict_GetItemWithError(PyObject *op, PyObject *key)
1457{
Victor Stinner742da042016-09-07 17:40:12 -07001458 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001459 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 PyDictObject*mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001461 PyObject *value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 if (!PyDict_Check(op)) {
1464 PyErr_BadInternalCall();
1465 return NULL;
1466 }
1467 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001468 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 {
1470 hash = PyObject_Hash(key);
1471 if (hash == -1) {
1472 return NULL;
1473 }
1474 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001475
INADA Naoki778928b2017-08-03 23:45:15 +09001476 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001477 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001478 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001479 return value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001480}
1481
Brett Cannonfd074152012-04-14 14:10:13 -04001482PyObject *
1483_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1484{
1485 PyObject *kv;
1486 kv = _PyUnicode_FromId(key); /* borrowed */
1487 if (kv == NULL)
1488 return NULL;
scoder6067d4b2020-05-11 06:04:31 +02001489 Py_hash_t hash = ((PyASCIIObject *) kv)->hash;
1490 assert (hash != -1); /* interned strings have their hash value initialised */
1491 return _PyDict_GetItem_KnownHash(dp, kv, hash);
Brett Cannonfd074152012-04-14 14:10:13 -04001492}
1493
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001494PyObject *
1495_PyDict_GetItemStringWithError(PyObject *v, const char *key)
1496{
1497 PyObject *kv, *rv;
1498 kv = PyUnicode_FromString(key);
1499 if (kv == NULL) {
1500 return NULL;
1501 }
1502 rv = PyDict_GetItemWithError(v, kv);
1503 Py_DECREF(kv);
1504 return rv;
1505}
1506
Victor Stinnerb4efc962015-11-20 09:24:02 +01001507/* Fast version of global value lookup (LOAD_GLOBAL).
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001508 * Lookup in globals, then builtins.
Victor Stinnerb4efc962015-11-20 09:24:02 +01001509 *
1510 * Raise an exception and return NULL if an error occurred (ex: computing the
1511 * key hash failed, key comparison failed, ...). Return NULL if the key doesn't
1512 * exist. Return the value if the key exists.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001513 */
1514PyObject *
1515_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001516{
Victor Stinner742da042016-09-07 17:40:12 -07001517 Py_ssize_t ix;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001518 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09001519 PyObject *value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001520
1521 if (!PyUnicode_CheckExact(key) ||
1522 (hash = ((PyASCIIObject *) key)->hash) == -1)
1523 {
1524 hash = PyObject_Hash(key);
1525 if (hash == -1)
1526 return NULL;
Antoine Pitroue965d972012-02-27 00:45:12 +01001527 }
Victor Stinnerb4efc962015-11-20 09:24:02 +01001528
1529 /* namespace 1: globals */
INADA Naoki778928b2017-08-03 23:45:15 +09001530 ix = globals->ma_keys->dk_lookup(globals, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001531 if (ix == DKIX_ERROR)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001532 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001533 if (ix != DKIX_EMPTY && value != NULL)
1534 return value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001535
1536 /* namespace 2: builtins */
INADA Naoki778928b2017-08-03 23:45:15 +09001537 ix = builtins->ma_keys->dk_lookup(builtins, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001538 if (ix < 0)
Victor Stinnerb4efc962015-11-20 09:24:02 +01001539 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001540 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001541}
1542
Antoine Pitroue965d972012-02-27 00:45:12 +01001543/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1544 * dictionary if it's merely replacing the value for an existing key.
1545 * This means that it's safe to loop over a dictionary with PyDict_Next()
1546 * and occasionally replace a value -- but you can't insert new keys or
1547 * remove them.
1548 */
1549int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001550PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001551{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001552 PyDictObject *mp;
1553 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001554 if (!PyDict_Check(op)) {
1555 PyErr_BadInternalCall();
1556 return -1;
1557 }
1558 assert(key);
1559 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001560 mp = (PyDictObject *)op;
1561 if (!PyUnicode_CheckExact(key) ||
1562 (hash = ((PyASCIIObject *) key)->hash) == -1)
1563 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001564 hash = PyObject_Hash(key);
1565 if (hash == -1)
1566 return -1;
1567 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001568
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001569 if (mp->ma_keys == Py_EMPTY_KEYS) {
1570 return insert_to_emptydict(mp, key, hash, value);
1571 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001572 /* insertdict() handles any resizing that might be necessary */
1573 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001574}
1575
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001576int
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001577_PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
1578 Py_hash_t hash)
1579{
1580 PyDictObject *mp;
1581
1582 if (!PyDict_Check(op)) {
1583 PyErr_BadInternalCall();
1584 return -1;
1585 }
1586 assert(key);
1587 assert(value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001588 assert(hash != -1);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001589 mp = (PyDictObject *)op;
1590
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001591 if (mp->ma_keys == Py_EMPTY_KEYS) {
1592 return insert_to_emptydict(mp, key, hash, value);
1593 }
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001594 /* insertdict() handles any resizing that might be necessary */
1595 return insertdict(mp, key, hash, value);
1596}
1597
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001598static int
INADA Naoki778928b2017-08-03 23:45:15 +09001599delitem_common(PyDictObject *mp, Py_hash_t hash, Py_ssize_t ix,
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001600 PyObject *old_value)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001601{
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001602 PyObject *old_key;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001603 PyDictKeyEntry *ep;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001604
INADA Naoki778928b2017-08-03 23:45:15 +09001605 Py_ssize_t hashpos = lookdict_index(mp->ma_keys, hash, ix);
1606 assert(hashpos >= 0);
1607
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001608 mp->ma_used--;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001609 mp->ma_version_tag = DICT_NEXT_VERSION();
1610 ep = &DK_ENTRIES(mp->ma_keys)[ix];
INADA Naokia7576492018-11-14 18:39:27 +09001611 dictkeys_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001612 ENSURE_ALLOWS_DELETIONS(mp);
1613 old_key = ep->me_key;
1614 ep->me_key = NULL;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001615 ep->me_value = NULL;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001616 Py_DECREF(old_key);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001617 Py_DECREF(old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001618
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001619 ASSERT_CONSISTENT(mp);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001620 return 0;
1621}
1622
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001623int
Tim Peters1f5871e2000-07-04 17:44:48 +00001624PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001625{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001626 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 assert(key);
1628 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001629 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001630 hash = PyObject_Hash(key);
1631 if (hash == -1)
1632 return -1;
1633 }
Victor Stinner742da042016-09-07 17:40:12 -07001634
1635 return _PyDict_DelItem_KnownHash(op, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001636}
1637
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001638int
1639_PyDict_DelItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1640{
INADA Naoki778928b2017-08-03 23:45:15 +09001641 Py_ssize_t ix;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001642 PyDictObject *mp;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001643 PyObject *old_value;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001644
1645 if (!PyDict_Check(op)) {
1646 PyErr_BadInternalCall();
1647 return -1;
1648 }
1649 assert(key);
1650 assert(hash != -1);
1651 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001652 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001653 if (ix == DKIX_ERROR)
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001654 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09001655 if (ix == DKIX_EMPTY || old_value == NULL) {
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001656 _PyErr_SetKeyError(key);
1657 return -1;
1658 }
Victor Stinner78601a32016-09-09 19:28:36 -07001659
1660 // Split table doesn't allow deletion. Combine it.
1661 if (_PyDict_HasSplitTable(mp)) {
1662 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1663 return -1;
1664 }
INADA Naoki778928b2017-08-03 23:45:15 +09001665 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001666 assert(ix >= 0);
1667 }
1668
INADA Naoki778928b2017-08-03 23:45:15 +09001669 return delitem_common(mp, hash, ix, old_value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001670}
1671
Antoine Pitroud741ed42016-12-27 14:23:43 +01001672/* This function promises that the predicate -> deletion sequence is atomic
1673 * (i.e. protected by the GIL), assuming the predicate itself doesn't
1674 * release the GIL.
1675 */
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001676int
1677_PyDict_DelItemIf(PyObject *op, PyObject *key,
1678 int (*predicate)(PyObject *value))
1679{
Antoine Pitroud741ed42016-12-27 14:23:43 +01001680 Py_ssize_t hashpos, ix;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001681 PyDictObject *mp;
1682 Py_hash_t hash;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001683 PyObject *old_value;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001684 int res;
1685
1686 if (!PyDict_Check(op)) {
1687 PyErr_BadInternalCall();
1688 return -1;
1689 }
1690 assert(key);
1691 hash = PyObject_Hash(key);
1692 if (hash == -1)
1693 return -1;
1694 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001695 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001696 if (ix == DKIX_ERROR)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001697 return -1;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001698 if (ix == DKIX_EMPTY || old_value == NULL) {
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001699 _PyErr_SetKeyError(key);
1700 return -1;
1701 }
Antoine Pitroud741ed42016-12-27 14:23:43 +01001702
1703 // Split table doesn't allow deletion. Combine it.
1704 if (_PyDict_HasSplitTable(mp)) {
1705 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1706 return -1;
1707 }
INADA Naoki778928b2017-08-03 23:45:15 +09001708 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001709 assert(ix >= 0);
1710 }
1711
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001712 res = predicate(old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001713 if (res == -1)
1714 return -1;
INADA Naoki778928b2017-08-03 23:45:15 +09001715
1716 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1717 assert(hashpos >= 0);
1718
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001719 if (res > 0)
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001720 return delitem_common(mp, hashpos, ix, old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001721 else
1722 return 0;
1723}
1724
1725
Guido van Rossum25831651993-05-19 14:50:45 +00001726void
Tim Peters1f5871e2000-07-04 17:44:48 +00001727PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001728{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001730 PyDictKeysObject *oldkeys;
1731 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001734 if (!PyDict_Check(op))
1735 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001736 mp = ((PyDictObject *)op);
1737 oldkeys = mp->ma_keys;
1738 oldvalues = mp->ma_values;
1739 if (oldvalues == empty_values)
1740 return;
1741 /* Empty the dict... */
INADA Naokia7576492018-11-14 18:39:27 +09001742 dictkeys_incref(Py_EMPTY_KEYS);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001743 mp->ma_keys = Py_EMPTY_KEYS;
1744 mp->ma_values = empty_values;
1745 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001746 mp->ma_version_tag = DICT_NEXT_VERSION();
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001747 /* ...then clear the keys and values */
1748 if (oldvalues != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001749 n = oldkeys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001750 for (i = 0; i < n; i++)
1751 Py_CLEAR(oldvalues[i]);
1752 free_values(oldvalues);
INADA Naokia7576492018-11-14 18:39:27 +09001753 dictkeys_decref(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001754 }
1755 else {
1756 assert(oldkeys->dk_refcnt == 1);
INADA Naokia7576492018-11-14 18:39:27 +09001757 dictkeys_decref(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001758 }
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001759 ASSERT_CONSISTENT(mp);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001760}
1761
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001762/* Internal version of PyDict_Next that returns a hash value in addition
1763 * to the key and value.
1764 * Return 1 on success, return 0 when the reached the end of the dictionary
1765 * (or if op is not a dictionary)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001766 */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001767int
1768_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1769 PyObject **pvalue, Py_hash_t *phash)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001770{
INADA Naokica2d8be2016-11-04 16:59:10 +09001771 Py_ssize_t i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001772 PyDictObject *mp;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001773 PyDictKeyEntry *entry_ptr;
1774 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001775
1776 if (!PyDict_Check(op))
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001777 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001778 mp = (PyDictObject *)op;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001779 i = *ppos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001780 if (mp->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09001781 if (i < 0 || i >= mp->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001782 return 0;
INADA Naokica2d8be2016-11-04 16:59:10 +09001783 /* values of split table is always dense */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001784 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
INADA Naokica2d8be2016-11-04 16:59:10 +09001785 value = mp->ma_values[i];
1786 assert(value != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001788 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09001789 Py_ssize_t n = mp->ma_keys->dk_nentries;
1790 if (i < 0 || i >= n)
1791 return 0;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001792 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
1793 while (i < n && entry_ptr->me_value == NULL) {
1794 entry_ptr++;
1795 i++;
Victor Stinner742da042016-09-07 17:40:12 -07001796 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001797 if (i >= n)
1798 return 0;
1799 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001800 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001801 *ppos = i+1;
1802 if (pkey)
1803 *pkey = entry_ptr->me_key;
1804 if (phash)
1805 *phash = entry_ptr->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001806 if (pvalue)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001807 *pvalue = value;
1808 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001809}
1810
Tim Peters080c88b2003-02-15 03:01:11 +00001811/*
1812 * Iterate over a dict. Use like so:
1813 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001814 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001815 * PyObject *key, *value;
1816 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001817 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001818 * Refer to borrowed references in key and value.
Tim Peters080c88b2003-02-15 03:01:11 +00001819 * }
1820 *
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001821 * Return 1 on success, return 0 when the reached the end of the dictionary
1822 * (or if op is not a dictionary)
1823 *
Tim Peters080c88b2003-02-15 03:01:11 +00001824 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001825 * mutates the dict. One exception: it is safe if the loop merely changes
1826 * the values associated with the keys (but doesn't insert new keys or
1827 * delete keys), via PyDict_SetItem().
1828 */
Guido van Rossum25831651993-05-19 14:50:45 +00001829int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001830PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001831{
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001832 return _PyDict_Next(op, ppos, pkey, pvalue, NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001833}
1834
Eric Snow96c6af92015-05-29 22:21:39 -06001835/* Internal version of dict.pop(). */
1836PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001837_PyDict_Pop_KnownHash(PyObject *dict, PyObject *key, Py_hash_t hash, PyObject *deflt)
Eric Snow96c6af92015-05-29 22:21:39 -06001838{
Victor Stinner742da042016-09-07 17:40:12 -07001839 Py_ssize_t ix, hashpos;
Eric Snow96c6af92015-05-29 22:21:39 -06001840 PyObject *old_value, *old_key;
1841 PyDictKeyEntry *ep;
Yury Selivanov684ef2c2016-10-28 19:01:21 -04001842 PyDictObject *mp;
1843
1844 assert(PyDict_Check(dict));
1845 mp = (PyDictObject *)dict;
Eric Snow96c6af92015-05-29 22:21:39 -06001846
1847 if (mp->ma_used == 0) {
1848 if (deflt) {
1849 Py_INCREF(deflt);
1850 return deflt;
1851 }
1852 _PyErr_SetKeyError(key);
1853 return NULL;
1854 }
INADA Naoki778928b2017-08-03 23:45:15 +09001855 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001856 if (ix == DKIX_ERROR)
Eric Snow96c6af92015-05-29 22:21:39 -06001857 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001858 if (ix == DKIX_EMPTY || old_value == NULL) {
Eric Snow96c6af92015-05-29 22:21:39 -06001859 if (deflt) {
1860 Py_INCREF(deflt);
1861 return deflt;
1862 }
1863 _PyErr_SetKeyError(key);
1864 return NULL;
1865 }
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001866
Victor Stinner78601a32016-09-09 19:28:36 -07001867 // Split table doesn't allow deletion. Combine it.
1868 if (_PyDict_HasSplitTable(mp)) {
1869 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1870 return NULL;
1871 }
INADA Naoki778928b2017-08-03 23:45:15 +09001872 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001873 assert(ix >= 0);
1874 }
1875
INADA Naoki778928b2017-08-03 23:45:15 +09001876 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1877 assert(hashpos >= 0);
Victor Stinner78601a32016-09-09 19:28:36 -07001878 assert(old_value != NULL);
Eric Snow96c6af92015-05-29 22:21:39 -06001879 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001880 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokia7576492018-11-14 18:39:27 +09001881 dictkeys_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
Victor Stinner78601a32016-09-09 19:28:36 -07001882 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1883 ENSURE_ALLOWS_DELETIONS(mp);
1884 old_key = ep->me_key;
1885 ep->me_key = NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001886 ep->me_value = NULL;
Victor Stinner78601a32016-09-09 19:28:36 -07001887 Py_DECREF(old_key);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001888
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001889 ASSERT_CONSISTENT(mp);
Eric Snow96c6af92015-05-29 22:21:39 -06001890 return old_value;
1891}
1892
Serhiy Storchaka67796522017-01-12 18:34:33 +02001893PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001894_PyDict_Pop(PyObject *dict, PyObject *key, PyObject *deflt)
Serhiy Storchaka67796522017-01-12 18:34:33 +02001895{
1896 Py_hash_t hash;
1897
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001898 if (((PyDictObject *)dict)->ma_used == 0) {
Serhiy Storchaka67796522017-01-12 18:34:33 +02001899 if (deflt) {
1900 Py_INCREF(deflt);
1901 return deflt;
1902 }
1903 _PyErr_SetKeyError(key);
1904 return NULL;
1905 }
1906 if (!PyUnicode_CheckExact(key) ||
1907 (hash = ((PyASCIIObject *) key)->hash) == -1) {
1908 hash = PyObject_Hash(key);
1909 if (hash == -1)
1910 return NULL;
1911 }
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001912 return _PyDict_Pop_KnownHash(dict, key, hash, deflt);
Serhiy Storchaka67796522017-01-12 18:34:33 +02001913}
1914
Eric Snow96c6af92015-05-29 22:21:39 -06001915/* Internal version of dict.from_keys(). It is subclass-friendly. */
1916PyObject *
1917_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
1918{
1919 PyObject *it; /* iter(iterable) */
1920 PyObject *key;
1921 PyObject *d;
1922 int status;
1923
Victor Stinnera5ed5f02016-12-06 18:45:50 +01001924 d = _PyObject_CallNoArg(cls);
Eric Snow96c6af92015-05-29 22:21:39 -06001925 if (d == NULL)
1926 return NULL;
1927
1928 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
1929 if (PyDict_CheckExact(iterable)) {
1930 PyDictObject *mp = (PyDictObject *)d;
1931 PyObject *oldvalue;
1932 Py_ssize_t pos = 0;
1933 PyObject *key;
1934 Py_hash_t hash;
1935
Serhiy Storchakac61ac162017-03-21 08:52:38 +02001936 if (dictresize(mp, ESTIMATE_SIZE(PyDict_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001937 Py_DECREF(d);
1938 return NULL;
1939 }
1940
1941 while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
1942 if (insertdict(mp, key, hash, value)) {
1943 Py_DECREF(d);
1944 return NULL;
1945 }
1946 }
1947 return d;
1948 }
1949 if (PyAnySet_CheckExact(iterable)) {
1950 PyDictObject *mp = (PyDictObject *)d;
1951 Py_ssize_t pos = 0;
1952 PyObject *key;
1953 Py_hash_t hash;
1954
Victor Stinner742da042016-09-07 17:40:12 -07001955 if (dictresize(mp, ESTIMATE_SIZE(PySet_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001956 Py_DECREF(d);
1957 return NULL;
1958 }
1959
1960 while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
1961 if (insertdict(mp, key, hash, value)) {
1962 Py_DECREF(d);
1963 return NULL;
1964 }
1965 }
1966 return d;
1967 }
1968 }
1969
1970 it = PyObject_GetIter(iterable);
1971 if (it == NULL){
1972 Py_DECREF(d);
1973 return NULL;
1974 }
1975
1976 if (PyDict_CheckExact(d)) {
1977 while ((key = PyIter_Next(it)) != NULL) {
1978 status = PyDict_SetItem(d, key, value);
1979 Py_DECREF(key);
1980 if (status < 0)
1981 goto Fail;
1982 }
1983 } else {
1984 while ((key = PyIter_Next(it)) != NULL) {
1985 status = PyObject_SetItem(d, key, value);
1986 Py_DECREF(key);
1987 if (status < 0)
1988 goto Fail;
1989 }
1990 }
1991
1992 if (PyErr_Occurred())
1993 goto Fail;
1994 Py_DECREF(it);
1995 return d;
1996
1997Fail:
1998 Py_DECREF(it);
1999 Py_DECREF(d);
2000 return NULL;
2001}
2002
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002003/* Methods */
2004
2005static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002006dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002007{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002008 PyObject **values = mp->ma_values;
2009 PyDictKeysObject *keys = mp->ma_keys;
2010 Py_ssize_t i, n;
INADA Naokia6296d32017-08-24 14:55:17 +09002011
2012 /* bpo-31095: UnTrack is needed before calling any callbacks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002013 PyObject_GC_UnTrack(mp);
Jeroen Demeyer351c6742019-05-10 19:21:11 +02002014 Py_TRASHCAN_BEGIN(mp, dict_dealloc)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002015 if (values != NULL) {
2016 if (values != empty_values) {
Victor Stinner742da042016-09-07 17:40:12 -07002017 for (i = 0, n = mp->ma_keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002018 Py_XDECREF(values[i]);
2019 }
2020 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002021 }
INADA Naokia7576492018-11-14 18:39:27 +09002022 dictkeys_decref(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002023 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02002024 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02002025 assert(keys->dk_refcnt == 1);
INADA Naokia7576492018-11-14 18:39:27 +09002026 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002027 }
Victor Stinnerb4b53862020-05-05 19:55:29 +02002028#if PyDict_MAXFREELIST > 0
2029 if (numfree < PyDict_MAXFREELIST && Py_IS_TYPE(mp, &PyDict_Type)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002030 free_list[numfree++] = mp;
Victor Stinnerb4b53862020-05-05 19:55:29 +02002031 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002032 else
Victor Stinnerb4b53862020-05-05 19:55:29 +02002033#endif
2034 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002035 Py_TYPE(mp)->tp_free((PyObject *)mp);
Victor Stinnerb4b53862020-05-05 19:55:29 +02002036 }
Jeroen Demeyer351c6742019-05-10 19:21:11 +02002037 Py_TRASHCAN_END
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002038}
2039
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002040
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002041static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002042dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002043{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002044 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01002045 PyObject *key = NULL, *value = NULL;
2046 _PyUnicodeWriter writer;
2047 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00002048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002049 i = Py_ReprEnter((PyObject *)mp);
2050 if (i != 0) {
2051 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
2052 }
Guido van Rossum255443b1998-04-10 22:47:14 +00002053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002054 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01002055 Py_ReprLeave((PyObject *)mp);
2056 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002057 }
Tim Petersa7259592001-06-16 05:11:17 +00002058
Victor Stinnerf91929b2013-11-19 13:07:38 +01002059 _PyUnicodeWriter_Init(&writer);
2060 writer.overallocate = 1;
2061 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
2062 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00002063
Victor Stinnerf91929b2013-11-19 13:07:38 +01002064 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
2065 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00002066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 /* Do repr() on each key+value pair, and insert ": " between them.
2068 Note that repr may mutate the dict. */
2069 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01002070 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002071 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01002072 PyObject *s;
2073 int res;
2074
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002075 /* Prevent repr from deleting key or value during key format. */
2076 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002077 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02002078
Victor Stinnerf91929b2013-11-19 13:07:38 +01002079 if (!first) {
2080 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
2081 goto error;
2082 }
2083 first = 0;
2084
2085 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002086 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01002087 goto error;
2088 res = _PyUnicodeWriter_WriteStr(&writer, s);
2089 Py_DECREF(s);
2090 if (res < 0)
2091 goto error;
2092
2093 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
2094 goto error;
2095
2096 s = PyObject_Repr(value);
2097 if (s == NULL)
2098 goto error;
2099 res = _PyUnicodeWriter_WriteStr(&writer, s);
2100 Py_DECREF(s);
2101 if (res < 0)
2102 goto error;
2103
2104 Py_CLEAR(key);
2105 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002106 }
Tim Petersa7259592001-06-16 05:11:17 +00002107
Victor Stinnerf91929b2013-11-19 13:07:38 +01002108 writer.overallocate = 0;
2109 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
2110 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00002111
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002112 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01002113
2114 return _PyUnicodeWriter_Finish(&writer);
2115
2116error:
2117 Py_ReprLeave((PyObject *)mp);
2118 _PyUnicodeWriter_Dealloc(&writer);
2119 Py_XDECREF(key);
2120 Py_XDECREF(value);
2121 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002122}
2123
Martin v. Löwis18e16552006-02-15 17:27:45 +00002124static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002125dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002126{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002127 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002128}
2129
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002130static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002131dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002132{
Victor Stinner742da042016-09-07 17:40:12 -07002133 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002134 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09002135 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002137 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002138 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002139 hash = PyObject_Hash(key);
2140 if (hash == -1)
2141 return NULL;
2142 }
INADA Naoki778928b2017-08-03 23:45:15 +09002143 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002144 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002145 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002146 if (ix == DKIX_EMPTY || value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002147 if (!PyDict_CheckExact(mp)) {
2148 /* Look up __missing__ method if we're a subclass. */
2149 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05002150 _Py_IDENTIFIER(__missing__);
2151 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002152 if (missing != NULL) {
Petr Viktorinffd97532020-02-11 17:46:57 +01002153 res = PyObject_CallOneArg(missing, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002154 Py_DECREF(missing);
2155 return res;
2156 }
2157 else if (PyErr_Occurred())
2158 return NULL;
2159 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07002160 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002161 return NULL;
2162 }
INADA Naokiba609772016-12-07 20:41:42 +09002163 Py_INCREF(value);
2164 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002165}
2166
2167static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002168dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002169{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002170 if (w == NULL)
2171 return PyDict_DelItem((PyObject *)mp, v);
2172 else
2173 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002174}
2175
Guido van Rossuma9e7a811997-05-13 21:02:11 +00002176static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002177 (lenfunc)dict_length, /*mp_length*/
2178 (binaryfunc)dict_subscript, /*mp_subscript*/
2179 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002180};
2181
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002182static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002183dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002184{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002185 PyObject *v;
2186 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002187 PyDictKeyEntry *ep;
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002188 Py_ssize_t n, offset;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002189 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002190
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002191 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 n = mp->ma_used;
2193 v = PyList_New(n);
2194 if (v == NULL)
2195 return NULL;
2196 if (n != mp->ma_used) {
2197 /* Durnit. The allocations caused the dict to resize.
2198 * Just start over, this shouldn't normally happen.
2199 */
2200 Py_DECREF(v);
2201 goto again;
2202 }
Victor Stinner742da042016-09-07 17:40:12 -07002203 ep = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002204 if (mp->ma_values) {
2205 value_ptr = mp->ma_values;
2206 offset = sizeof(PyObject *);
2207 }
2208 else {
2209 value_ptr = &ep[0].me_value;
2210 offset = sizeof(PyDictKeyEntry);
2211 }
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002212 for (i = 0, j = 0; j < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002213 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002214 PyObject *key = ep[i].me_key;
2215 Py_INCREF(key);
2216 PyList_SET_ITEM(v, j, key);
2217 j++;
2218 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002219 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002220 }
2221 assert(j == n);
2222 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002223}
2224
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002225static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002226dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002227{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002228 PyObject *v;
2229 Py_ssize_t i, j;
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002230 PyDictKeyEntry *ep;
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002231 Py_ssize_t n, offset;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002232 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002233
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002234 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002235 n = mp->ma_used;
2236 v = PyList_New(n);
2237 if (v == NULL)
2238 return NULL;
2239 if (n != mp->ma_used) {
2240 /* Durnit. The allocations caused the dict to resize.
2241 * Just start over, this shouldn't normally happen.
2242 */
2243 Py_DECREF(v);
2244 goto again;
2245 }
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002246 ep = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002247 if (mp->ma_values) {
2248 value_ptr = mp->ma_values;
2249 offset = sizeof(PyObject *);
2250 }
2251 else {
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002252 value_ptr = &ep[0].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002253 offset = sizeof(PyDictKeyEntry);
2254 }
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002255 for (i = 0, j = 0; j < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002256 PyObject *value = *value_ptr;
2257 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2258 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002259 Py_INCREF(value);
2260 PyList_SET_ITEM(v, j, value);
2261 j++;
2262 }
2263 }
2264 assert(j == n);
2265 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002266}
2267
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002268static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002269dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002270{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002271 PyObject *v;
2272 Py_ssize_t i, j, n;
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002273 Py_ssize_t offset;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002274 PyObject *item, *key;
2275 PyDictKeyEntry *ep;
2276 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 /* Preallocate the list of tuples, to avoid allocations during
2279 * the loop over the items, which could trigger GC, which
2280 * could resize the dict. :-(
2281 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002282 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002283 n = mp->ma_used;
2284 v = PyList_New(n);
2285 if (v == NULL)
2286 return NULL;
2287 for (i = 0; i < n; i++) {
2288 item = PyTuple_New(2);
2289 if (item == NULL) {
2290 Py_DECREF(v);
2291 return NULL;
2292 }
2293 PyList_SET_ITEM(v, i, item);
2294 }
2295 if (n != mp->ma_used) {
2296 /* Durnit. The allocations caused the dict to resize.
2297 * Just start over, this shouldn't normally happen.
2298 */
2299 Py_DECREF(v);
2300 goto again;
2301 }
2302 /* Nothing we do below makes any function calls. */
Victor Stinner742da042016-09-07 17:40:12 -07002303 ep = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002304 if (mp->ma_values) {
2305 value_ptr = mp->ma_values;
2306 offset = sizeof(PyObject *);
2307 }
2308 else {
2309 value_ptr = &ep[0].me_value;
2310 offset = sizeof(PyDictKeyEntry);
2311 }
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002312 for (i = 0, j = 0; j < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002313 PyObject *value = *value_ptr;
2314 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2315 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002316 key = ep[i].me_key;
2317 item = PyList_GET_ITEM(v, j);
2318 Py_INCREF(key);
2319 PyTuple_SET_ITEM(item, 0, key);
2320 Py_INCREF(value);
2321 PyTuple_SET_ITEM(item, 1, value);
2322 j++;
2323 }
2324 }
2325 assert(j == n);
2326 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002327}
2328
Larry Hastings5c661892014-01-24 06:17:25 -08002329/*[clinic input]
2330@classmethod
2331dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08002332 iterable: object
2333 value: object=None
2334 /
2335
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002336Create a new dictionary with keys from iterable and values set to value.
Larry Hastings5c661892014-01-24 06:17:25 -08002337[clinic start generated code]*/
2338
Larry Hastings5c661892014-01-24 06:17:25 -08002339static PyObject *
2340dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002341/*[clinic end generated code: output=8fb98e4b10384999 input=382ba4855d0f74c3]*/
Larry Hastings5c661892014-01-24 06:17:25 -08002342{
Eric Snow96c6af92015-05-29 22:21:39 -06002343 return _PyDict_FromKeys((PyObject *)type, iterable, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002344}
2345
Brandt Buchereb8ac572020-02-24 19:47:34 -08002346/* Single-arg dict update; used by dict_update_common and operators. */
2347static int
2348dict_update_arg(PyObject *self, PyObject *arg)
2349{
2350 if (PyDict_CheckExact(arg)) {
2351 return PyDict_Merge(self, arg, 1);
2352 }
2353 _Py_IDENTIFIER(keys);
2354 PyObject *func;
2355 if (_PyObject_LookupAttrId(arg, &PyId_keys, &func) < 0) {
2356 return -1;
2357 }
2358 if (func != NULL) {
2359 Py_DECREF(func);
2360 return PyDict_Merge(self, arg, 1);
2361 }
2362 return PyDict_MergeFromSeq2(self, arg, 1);
2363}
2364
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002365static int
Victor Stinner742da042016-09-07 17:40:12 -07002366dict_update_common(PyObject *self, PyObject *args, PyObject *kwds,
2367 const char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002368{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 PyObject *arg = NULL;
2370 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002371
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002372 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002373 result = -1;
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002374 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002375 else if (arg != NULL) {
Brandt Buchereb8ac572020-02-24 19:47:34 -08002376 result = dict_update_arg(self, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002377 }
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379 if (result == 0 && kwds != NULL) {
2380 if (PyArg_ValidateKeywordArguments(kwds))
2381 result = PyDict_Merge(self, kwds, 1);
2382 else
2383 result = -1;
2384 }
2385 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002386}
2387
Victor Stinner91f0d4a2017-01-19 12:45:06 +01002388/* Note: dict.update() uses the METH_VARARGS|METH_KEYWORDS calling convention.
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +03002389 Using METH_FASTCALL|METH_KEYWORDS would make dict.update(**dict2) calls
2390 slower, see the issue #29312. */
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002391static PyObject *
2392dict_update(PyObject *self, PyObject *args, PyObject *kwds)
2393{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 if (dict_update_common(self, args, kwds, "update") != -1)
2395 Py_RETURN_NONE;
2396 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002397}
2398
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002399/* Update unconditionally replaces existing items.
2400 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00002401 otherwise it leaves existing items unchanged.
2402
2403 PyDict_{Update,Merge} update/merge from a mapping object.
2404
Tim Petersf582b822001-12-11 18:51:08 +00002405 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00002406 producing iterable objects of length 2.
2407*/
2408
Tim Petersf582b822001-12-11 18:51:08 +00002409int
Tim Peters1fc240e2001-10-26 05:06:50 +00002410PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
2411{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002412 PyObject *it; /* iter(seq2) */
2413 Py_ssize_t i; /* index into seq2 of current element */
2414 PyObject *item; /* seq2[i] */
2415 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00002416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002417 assert(d != NULL);
2418 assert(PyDict_Check(d));
2419 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00002420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002421 it = PyObject_GetIter(seq2);
2422 if (it == NULL)
2423 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002425 for (i = 0; ; ++i) {
2426 PyObject *key, *value;
2427 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00002428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 fast = NULL;
2430 item = PyIter_Next(it);
2431 if (item == NULL) {
2432 if (PyErr_Occurred())
2433 goto Fail;
2434 break;
2435 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002437 /* Convert item to sequence, and verify length 2. */
2438 fast = PySequence_Fast(item, "");
2439 if (fast == NULL) {
2440 if (PyErr_ExceptionMatches(PyExc_TypeError))
2441 PyErr_Format(PyExc_TypeError,
2442 "cannot convert dictionary update "
2443 "sequence element #%zd to a sequence",
2444 i);
2445 goto Fail;
2446 }
2447 n = PySequence_Fast_GET_SIZE(fast);
2448 if (n != 2) {
2449 PyErr_Format(PyExc_ValueError,
2450 "dictionary update sequence element #%zd "
2451 "has length %zd; 2 is required",
2452 i, n);
2453 goto Fail;
2454 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002456 /* Update/merge with this (key, value) pair. */
2457 key = PySequence_Fast_GET_ITEM(fast, 0);
2458 value = PySequence_Fast_GET_ITEM(fast, 1);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002459 Py_INCREF(key);
2460 Py_INCREF(value);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002461 if (override) {
2462 if (PyDict_SetItem(d, key, value) < 0) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002463 Py_DECREF(key);
2464 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002465 goto Fail;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002466 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002467 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002468 else if (PyDict_GetItemWithError(d, key) == NULL) {
2469 if (PyErr_Occurred() || PyDict_SetItem(d, key, value) < 0) {
2470 Py_DECREF(key);
2471 Py_DECREF(value);
2472 goto Fail;
2473 }
2474 }
2475
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002476 Py_DECREF(key);
2477 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002478 Py_DECREF(fast);
2479 Py_DECREF(item);
2480 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 i = 0;
Victor Stinner0fc91ee2019-04-12 21:51:34 +02002483 ASSERT_CONSISTENT(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002484 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00002485Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002486 Py_XDECREF(item);
2487 Py_XDECREF(fast);
2488 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002489Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002490 Py_DECREF(it);
2491 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00002492}
2493
doko@ubuntu.comc96df682016-10-11 08:04:02 +02002494static int
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002495dict_merge(PyObject *a, PyObject *b, int override)
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002496{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002497 PyDictObject *mp, *other;
2498 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -07002499 PyDictKeyEntry *entry, *ep0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002500
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002501 assert(0 <= override && override <= 2);
2502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002503 /* We accept for the argument either a concrete dictionary object,
2504 * or an abstract "mapping" object. For the former, we can do
2505 * things quite efficiently. For the latter, we only require that
2506 * PyMapping_Keys() and PyObject_GetItem() be supported.
2507 */
2508 if (a == NULL || !PyDict_Check(a) || b == NULL) {
2509 PyErr_BadInternalCall();
2510 return -1;
2511 }
2512 mp = (PyDictObject*)a;
INADA Naoki2aaf98c2018-09-26 12:59:00 +09002513 if (PyDict_Check(b) && (Py_TYPE(b)->tp_iter == (getiterfunc)dict_iter)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002514 other = (PyDictObject*)b;
2515 if (other == mp || other->ma_used == 0)
2516 /* a.update(a) or a.update({}); nothing to do */
2517 return 0;
2518 if (mp->ma_used == 0)
2519 /* Since the target dict is empty, PyDict_GetItem()
2520 * always returns NULL. Setting override to 1
2521 * skips the unnecessary test.
2522 */
2523 override = 1;
2524 /* Do one big resize at the start, rather than
2525 * incrementally resizing as we insert new items. Expect
2526 * that there will be no (or few) overlapping keys.
2527 */
INADA Naokib1152be2016-10-27 19:26:50 +09002528 if (USABLE_FRACTION(mp->ma_keys->dk_size) < other->ma_used) {
2529 if (dictresize(mp, ESTIMATE_SIZE(mp->ma_used + other->ma_used))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002530 return -1;
INADA Naokib1152be2016-10-27 19:26:50 +09002531 }
2532 }
Victor Stinner742da042016-09-07 17:40:12 -07002533 ep0 = DK_ENTRIES(other->ma_keys);
2534 for (i = 0, n = other->ma_keys->dk_nentries; i < n; i++) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002535 PyObject *key, *value;
2536 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002537 entry = &ep0[i];
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002538 key = entry->me_key;
2539 hash = entry->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002540 if (other->ma_values)
2541 value = other->ma_values[i];
2542 else
2543 value = entry->me_value;
2544
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002545 if (value != NULL) {
2546 int err = 0;
2547 Py_INCREF(key);
2548 Py_INCREF(value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002549 if (override == 1)
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002550 err = insertdict(mp, key, hash, value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002551 else if (_PyDict_GetItem_KnownHash(a, key, hash) == NULL) {
2552 if (PyErr_Occurred()) {
2553 Py_DECREF(value);
2554 Py_DECREF(key);
2555 return -1;
2556 }
2557 err = insertdict(mp, key, hash, value);
2558 }
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002559 else if (override != 0) {
2560 _PyErr_SetKeyError(key);
2561 Py_DECREF(value);
2562 Py_DECREF(key);
2563 return -1;
2564 }
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002565 Py_DECREF(value);
2566 Py_DECREF(key);
2567 if (err != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002568 return -1;
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002569
Victor Stinner742da042016-09-07 17:40:12 -07002570 if (n != other->ma_keys->dk_nentries) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002571 PyErr_SetString(PyExc_RuntimeError,
2572 "dict mutated during update");
2573 return -1;
2574 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002575 }
2576 }
2577 }
2578 else {
2579 /* Do it the generic, slower way */
2580 PyObject *keys = PyMapping_Keys(b);
2581 PyObject *iter;
2582 PyObject *key, *value;
2583 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002585 if (keys == NULL)
2586 /* Docstring says this is equivalent to E.keys() so
2587 * if E doesn't have a .keys() method we want
2588 * AttributeError to percolate up. Might as well
2589 * do the same for any other error.
2590 */
2591 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002593 iter = PyObject_GetIter(keys);
2594 Py_DECREF(keys);
2595 if (iter == NULL)
2596 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002598 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002599 if (override != 1) {
2600 if (PyDict_GetItemWithError(a, key) != NULL) {
2601 if (override != 0) {
2602 _PyErr_SetKeyError(key);
2603 Py_DECREF(key);
2604 Py_DECREF(iter);
2605 return -1;
2606 }
2607 Py_DECREF(key);
2608 continue;
2609 }
2610 else if (PyErr_Occurred()) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002611 Py_DECREF(key);
2612 Py_DECREF(iter);
2613 return -1;
2614 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002615 }
2616 value = PyObject_GetItem(b, key);
2617 if (value == NULL) {
2618 Py_DECREF(iter);
2619 Py_DECREF(key);
2620 return -1;
2621 }
2622 status = PyDict_SetItem(a, key, value);
2623 Py_DECREF(key);
2624 Py_DECREF(value);
2625 if (status < 0) {
2626 Py_DECREF(iter);
2627 return -1;
2628 }
2629 }
2630 Py_DECREF(iter);
2631 if (PyErr_Occurred())
2632 /* Iterator completed, via error */
2633 return -1;
2634 }
Victor Stinner0fc91ee2019-04-12 21:51:34 +02002635 ASSERT_CONSISTENT(a);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002636 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002637}
2638
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002639int
2640PyDict_Update(PyObject *a, PyObject *b)
2641{
2642 return dict_merge(a, b, 1);
2643}
2644
2645int
2646PyDict_Merge(PyObject *a, PyObject *b, int override)
2647{
2648 /* XXX Deprecate override not in (0, 1). */
2649 return dict_merge(a, b, override != 0);
2650}
2651
2652int
2653_PyDict_MergeEx(PyObject *a, PyObject *b, int override)
2654{
2655 return dict_merge(a, b, override);
2656}
2657
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002658static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302659dict_copy(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002660{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002661 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002662}
2663
2664PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002665PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002666{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002667 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002668 PyDictObject *mp;
2669 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002671 if (o == NULL || !PyDict_Check(o)) {
2672 PyErr_BadInternalCall();
2673 return NULL;
2674 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002675
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002676 mp = (PyDictObject *)o;
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002677 if (mp->ma_used == 0) {
2678 /* The dict is empty; just return a new dict. */
2679 return PyDict_New();
2680 }
2681
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002682 if (_PyDict_HasSplitTable(mp)) {
2683 PyDictObject *split_copy;
Victor Stinner742da042016-09-07 17:40:12 -07002684 Py_ssize_t size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
2685 PyObject **newvalues;
2686 newvalues = new_values(size);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002687 if (newvalues == NULL)
2688 return PyErr_NoMemory();
2689 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2690 if (split_copy == NULL) {
2691 free_values(newvalues);
2692 return NULL;
2693 }
2694 split_copy->ma_values = newvalues;
2695 split_copy->ma_keys = mp->ma_keys;
2696 split_copy->ma_used = mp->ma_used;
INADA Naokid1c82c52018-04-03 11:43:53 +09002697 split_copy->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokia7576492018-11-14 18:39:27 +09002698 dictkeys_incref(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002699 for (i = 0, n = size; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002700 PyObject *value = mp->ma_values[i];
2701 Py_XINCREF(value);
2702 split_copy->ma_values[i] = value;
2703 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002704 if (_PyObject_GC_IS_TRACKED(mp))
2705 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002706 return (PyObject *)split_copy;
2707 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002708
2709 if (PyDict_CheckExact(mp) && mp->ma_values == NULL &&
2710 (mp->ma_used >= (mp->ma_keys->dk_nentries * 2) / 3))
2711 {
2712 /* Use fast-copy if:
2713
2714 (1) 'mp' is an instance of a subclassed dict; and
2715
2716 (2) 'mp' is not a split-dict; and
2717
2718 (3) if 'mp' is non-compact ('del' operation does not resize dicts),
2719 do fast-copy only if it has at most 1/3 non-used keys.
2720
Ville Skyttä61f82e02018-04-20 23:08:45 +03002721 The last condition (3) is important to guard against a pathological
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002722 case when a large dict is almost emptied with multiple del/pop
2723 operations and copied after that. In cases like this, we defer to
2724 PyDict_Merge, which produces a compacted copy.
2725 */
2726 return clone_combined_dict(mp);
2727 }
2728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002729 copy = PyDict_New();
2730 if (copy == NULL)
2731 return NULL;
2732 if (PyDict_Merge(copy, o, 1) == 0)
2733 return copy;
2734 Py_DECREF(copy);
2735 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002736}
2737
Martin v. Löwis18e16552006-02-15 17:27:45 +00002738Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002739PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002740{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002741 if (mp == NULL || !PyDict_Check(mp)) {
2742 PyErr_BadInternalCall();
2743 return -1;
2744 }
2745 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002746}
2747
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002748PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002749PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002750{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002751 if (mp == NULL || !PyDict_Check(mp)) {
2752 PyErr_BadInternalCall();
2753 return NULL;
2754 }
2755 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002756}
2757
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002758PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002759PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002760{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002761 if (mp == NULL || !PyDict_Check(mp)) {
2762 PyErr_BadInternalCall();
2763 return NULL;
2764 }
2765 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002766}
2767
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002768PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002769PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002770{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002771 if (mp == NULL || !PyDict_Check(mp)) {
2772 PyErr_BadInternalCall();
2773 return NULL;
2774 }
2775 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002776}
2777
Tim Peterse63415e2001-05-08 04:38:29 +00002778/* Return 1 if dicts equal, 0 if not, -1 if error.
2779 * Gets out as soon as any difference is detected.
2780 * Uses only Py_EQ comparison.
2781 */
2782static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002783dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002784{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002785 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002787 if (a->ma_used != b->ma_used)
2788 /* can't be equal if # of entries differ */
2789 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002790 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Victor Stinner742da042016-09-07 17:40:12 -07002791 for (i = 0; i < a->ma_keys->dk_nentries; i++) {
2792 PyDictKeyEntry *ep = &DK_ENTRIES(a->ma_keys)[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002793 PyObject *aval;
2794 if (a->ma_values)
2795 aval = a->ma_values[i];
2796 else
2797 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002798 if (aval != NULL) {
2799 int cmp;
2800 PyObject *bval;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002801 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002802 /* temporarily bump aval's refcount to ensure it stays
2803 alive until we're done with it */
2804 Py_INCREF(aval);
2805 /* ditto for key */
2806 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002807 /* reuse the known hash value */
INADA Naoki778928b2017-08-03 23:45:15 +09002808 b->ma_keys->dk_lookup(b, key, ep->me_hash, &bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002809 if (bval == NULL) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002810 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002811 Py_DECREF(aval);
2812 if (PyErr_Occurred())
2813 return -1;
2814 return 0;
2815 }
Dong-hee Na2d5bf562019-12-31 10:04:22 +09002816 Py_INCREF(bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002817 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002818 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002819 Py_DECREF(aval);
Dong-hee Na2d5bf562019-12-31 10:04:22 +09002820 Py_DECREF(bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002821 if (cmp <= 0) /* error or not equal */
2822 return cmp;
2823 }
2824 }
2825 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002826}
Tim Peterse63415e2001-05-08 04:38:29 +00002827
2828static PyObject *
2829dict_richcompare(PyObject *v, PyObject *w, int op)
2830{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002831 int cmp;
2832 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002834 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2835 res = Py_NotImplemented;
2836 }
2837 else if (op == Py_EQ || op == Py_NE) {
2838 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2839 if (cmp < 0)
2840 return NULL;
2841 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2842 }
2843 else
2844 res = Py_NotImplemented;
2845 Py_INCREF(res);
2846 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002847}
Tim Peterse63415e2001-05-08 04:38:29 +00002848
Larry Hastings61272b72014-01-07 12:41:53 -08002849/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002850
2851@coexist
2852dict.__contains__
2853
2854 key: object
2855 /
2856
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002857True if the dictionary has the specified key, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002858[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002859
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002860static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002861dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka19d25972017-02-04 08:05:07 +02002862/*[clinic end generated code: output=a3d03db709ed6e6b input=fe1cb42ad831e820]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002863{
Larry Hastingsc2047262014-01-25 20:43:29 -08002864 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002865 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002866 Py_ssize_t ix;
INADA Naokiba609772016-12-07 20:41:42 +09002867 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002868
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002869 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002870 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002871 hash = PyObject_Hash(key);
2872 if (hash == -1)
2873 return NULL;
2874 }
INADA Naoki778928b2017-08-03 23:45:15 +09002875 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002876 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002877 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002878 if (ix == DKIX_EMPTY || value == NULL)
Victor Stinner742da042016-09-07 17:40:12 -07002879 Py_RETURN_FALSE;
2880 Py_RETURN_TRUE;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002881}
2882
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002883/*[clinic input]
2884dict.get
2885
2886 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002887 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002888 /
2889
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002890Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002891[clinic start generated code]*/
2892
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002893static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002894dict_get_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002895/*[clinic end generated code: output=bba707729dee05bf input=279ddb5790b6b107]*/
Barry Warsawc38c5da1997-10-06 17:49:20 +00002896{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002897 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002898 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002899 Py_ssize_t ix;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002900
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002902 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002903 hash = PyObject_Hash(key);
2904 if (hash == -1)
2905 return NULL;
2906 }
INADA Naoki778928b2017-08-03 23:45:15 +09002907 ix = (self->ma_keys->dk_lookup) (self, key, hash, &val);
Victor Stinner742da042016-09-07 17:40:12 -07002908 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002909 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002910 if (ix == DKIX_EMPTY || val == NULL) {
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002911 val = default_value;
INADA Naokiba609772016-12-07 20:41:42 +09002912 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 Py_INCREF(val);
2914 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002915}
2916
Benjamin Peterson00e98862013-03-07 22:16:29 -05002917PyObject *
2918PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002919{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002920 PyDictObject *mp = (PyDictObject *)d;
INADA Naoki93f26f72016-11-02 18:45:16 +09002921 PyObject *value;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002922 Py_hash_t hash;
Guido van Rossum164452c2000-08-08 16:12:54 +00002923
Benjamin Peterson00e98862013-03-07 22:16:29 -05002924 if (!PyDict_Check(d)) {
2925 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002926 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002927 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002929 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002930 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002931 hash = PyObject_Hash(key);
2932 if (hash == -1)
2933 return NULL;
2934 }
Inada Naoki2ddc7f62019-03-18 20:38:33 +09002935 if (mp->ma_keys == Py_EMPTY_KEYS) {
2936 if (insert_to_emptydict(mp, key, hash, defaultobj) < 0) {
2937 return NULL;
2938 }
2939 return defaultobj;
2940 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002941
2942 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
2943 if (insertion_resize(mp) < 0)
2944 return NULL;
2945 }
2946
INADA Naoki778928b2017-08-03 23:45:15 +09002947 Py_ssize_t ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002948 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002949 return NULL;
INADA Naoki93f26f72016-11-02 18:45:16 +09002950
2951 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09002952 ((ix >= 0 && value == NULL && mp->ma_used != ix) ||
INADA Naoki93f26f72016-11-02 18:45:16 +09002953 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
2954 if (insertion_resize(mp) < 0) {
2955 return NULL;
2956 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002957 ix = DKIX_EMPTY;
2958 }
2959
2960 if (ix == DKIX_EMPTY) {
2961 PyDictKeyEntry *ep, *ep0;
2962 value = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002963 if (mp->ma_keys->dk_usable <= 0) {
Victor Stinner3c336c52016-09-12 14:17:40 +02002964 if (insertion_resize(mp) < 0) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002965 return NULL;
Victor Stinner3c336c52016-09-12 14:17:40 +02002966 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002967 }
INADA Naoki778928b2017-08-03 23:45:15 +09002968 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naoki93f26f72016-11-02 18:45:16 +09002969 ep0 = DK_ENTRIES(mp->ma_keys);
2970 ep = &ep0[mp->ma_keys->dk_nentries];
INADA Naokia7576492018-11-14 18:39:27 +09002971 dictkeys_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002972 Py_INCREF(key);
INADA Naoki93f26f72016-11-02 18:45:16 +09002973 Py_INCREF(value);
2974 MAINTAIN_TRACKING(mp, key, value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002975 ep->me_key = key;
2976 ep->me_hash = hash;
INADA Naokiba609772016-12-07 20:41:42 +09002977 if (_PyDict_HasSplitTable(mp)) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002978 assert(mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
2979 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Victor Stinner742da042016-09-07 17:40:12 -07002980 }
2981 else {
INADA Naoki93f26f72016-11-02 18:45:16 +09002982 ep->me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07002983 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002984 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002985 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naoki93f26f72016-11-02 18:45:16 +09002986 mp->ma_keys->dk_usable--;
2987 mp->ma_keys->dk_nentries++;
2988 assert(mp->ma_keys->dk_usable >= 0);
2989 }
INADA Naokiba609772016-12-07 20:41:42 +09002990 else if (value == NULL) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002991 value = defaultobj;
2992 assert(_PyDict_HasSplitTable(mp));
2993 assert(ix == mp->ma_used);
2994 Py_INCREF(value);
2995 MAINTAIN_TRACKING(mp, key, value);
INADA Naokiba609772016-12-07 20:41:42 +09002996 mp->ma_values[ix] = value;
INADA Naoki93f26f72016-11-02 18:45:16 +09002997 mp->ma_used++;
2998 mp->ma_version_tag = DICT_NEXT_VERSION();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002999 }
INADA Naoki93f26f72016-11-02 18:45:16 +09003000
Victor Stinner0fc91ee2019-04-12 21:51:34 +02003001 ASSERT_CONSISTENT(mp);
INADA Naoki93f26f72016-11-02 18:45:16 +09003002 return value;
Guido van Rossum164452c2000-08-08 16:12:54 +00003003}
3004
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003005/*[clinic input]
3006dict.setdefault
3007
3008 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02003009 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003010 /
3011
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02003012Insert key with a value of default if key is not in the dictionary.
3013
3014Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003015[clinic start generated code]*/
3016
Benjamin Peterson00e98862013-03-07 22:16:29 -05003017static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02003018dict_setdefault_impl(PyDictObject *self, PyObject *key,
3019 PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02003020/*[clinic end generated code: output=f8c1101ebf69e220 input=0f063756e815fd9d]*/
Benjamin Peterson00e98862013-03-07 22:16:29 -05003021{
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003022 PyObject *val;
Benjamin Peterson00e98862013-03-07 22:16:29 -05003023
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02003024 val = PyDict_SetDefault((PyObject *)self, key, default_value);
Benjamin Peterson00e98862013-03-07 22:16:29 -05003025 Py_XINCREF(val);
3026 return val;
3027}
Guido van Rossum164452c2000-08-08 16:12:54 +00003028
3029static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303030dict_clear(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00003031{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003032 PyDict_Clear((PyObject *)mp);
3033 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00003034}
3035
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003036/*[clinic input]
3037dict.pop
3038
3039 key: object
3040 default: object = NULL
3041 /
3042
Serhiy Storchaka279f4462019-09-14 12:24:05 +03003043D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003044
3045If key is not found, default is returned if given, otherwise KeyError is raised
3046[clinic start generated code]*/
3047
Guido van Rossumba6ab842000-12-12 22:02:18 +00003048static PyObject *
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003049dict_pop_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
Serhiy Storchaka279f4462019-09-14 12:24:05 +03003050/*[clinic end generated code: output=3abb47b89f24c21c input=eeebec7812190348]*/
Guido van Rossume027d982002-04-12 15:11:59 +00003051{
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003052 return _PyDict_Pop((PyObject*)self, key, default_value);
Guido van Rossume027d982002-04-12 15:11:59 +00003053}
3054
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003055/*[clinic input]
3056dict.popitem
3057
3058Remove and return a (key, value) pair as a 2-tuple.
3059
3060Pairs are returned in LIFO (last-in, first-out) order.
3061Raises KeyError if the dict is empty.
3062[clinic start generated code]*/
3063
Guido van Rossume027d982002-04-12 15:11:59 +00003064static PyObject *
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003065dict_popitem_impl(PyDictObject *self)
3066/*[clinic end generated code: output=e65fcb04420d230d input=1c38a49f21f64941]*/
Guido van Rossumba6ab842000-12-12 22:02:18 +00003067{
Victor Stinner742da042016-09-07 17:40:12 -07003068 Py_ssize_t i, j;
3069 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003070 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00003071
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003072 /* Allocate the result tuple before checking the size. Believe it
3073 * or not, this allocation could trigger a garbage collection which
3074 * could empty the dict, so if we checked the size first and that
3075 * happened, the result would be an infinite loop (searching for an
3076 * entry that no longer exists). Note that the usual popitem()
3077 * idiom is "while d: k, v = d.popitem()". so needing to throw the
3078 * tuple away if the dict *is* empty isn't a significant
3079 * inefficiency -- possible, but unlikely in practice.
3080 */
3081 res = PyTuple_New(2);
3082 if (res == NULL)
3083 return NULL;
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003084 if (self->ma_used == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003085 Py_DECREF(res);
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003086 PyErr_SetString(PyExc_KeyError, "popitem(): dictionary is empty");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003087 return NULL;
3088 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003089 /* Convert split table to combined table */
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003090 if (self->ma_keys->dk_lookup == lookdict_split) {
3091 if (dictresize(self, DK_SIZE(self->ma_keys))) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003092 Py_DECREF(res);
3093 return NULL;
3094 }
3095 }
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003096 ENSURE_ALLOWS_DELETIONS(self);
Victor Stinner742da042016-09-07 17:40:12 -07003097
3098 /* Pop last item */
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003099 ep0 = DK_ENTRIES(self->ma_keys);
3100 i = self->ma_keys->dk_nentries - 1;
Victor Stinner742da042016-09-07 17:40:12 -07003101 while (i >= 0 && ep0[i].me_value == NULL) {
3102 i--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003103 }
Victor Stinner742da042016-09-07 17:40:12 -07003104 assert(i >= 0);
3105
3106 ep = &ep0[i];
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003107 j = lookdict_index(self->ma_keys, ep->me_hash, i);
Victor Stinner742da042016-09-07 17:40:12 -07003108 assert(j >= 0);
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003109 assert(dictkeys_get_index(self->ma_keys, j) == i);
3110 dictkeys_set_index(self->ma_keys, j, DKIX_DUMMY);
Victor Stinner742da042016-09-07 17:40:12 -07003111
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003112 PyTuple_SET_ITEM(res, 0, ep->me_key);
3113 PyTuple_SET_ITEM(res, 1, ep->me_value);
Victor Stinner742da042016-09-07 17:40:12 -07003114 ep->me_key = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003115 ep->me_value = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07003116 /* We can't dk_usable++ since there is DKIX_DUMMY in indices */
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003117 self->ma_keys->dk_nentries = i;
3118 self->ma_used--;
3119 self->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner0fc91ee2019-04-12 21:51:34 +02003120 ASSERT_CONSISTENT(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003121 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00003122}
3123
Jeremy Hylton8caad492000-06-23 14:18:11 +00003124static int
3125dict_traverse(PyObject *op, visitproc visit, void *arg)
3126{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003127 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson55f44522016-09-05 12:12:59 -07003128 PyDictKeysObject *keys = mp->ma_keys;
Serhiy Storchaka46825d22016-09-26 21:29:34 +03003129 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Victor Stinner742da042016-09-07 17:40:12 -07003130 Py_ssize_t i, n = keys->dk_nentries;
3131
Benjamin Peterson55f44522016-09-05 12:12:59 -07003132 if (keys->dk_lookup == lookdict) {
3133 for (i = 0; i < n; i++) {
3134 if (entries[i].me_value != NULL) {
3135 Py_VISIT(entries[i].me_value);
3136 Py_VISIT(entries[i].me_key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003137 }
3138 }
Victor Stinner742da042016-09-07 17:40:12 -07003139 }
3140 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003141 if (mp->ma_values != NULL) {
Benjamin Peterson55f44522016-09-05 12:12:59 -07003142 for (i = 0; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003143 Py_VISIT(mp->ma_values[i]);
3144 }
3145 }
3146 else {
Benjamin Peterson55f44522016-09-05 12:12:59 -07003147 for (i = 0; i < n; i++) {
3148 Py_VISIT(entries[i].me_value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003149 }
3150 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003151 }
3152 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00003153}
3154
3155static int
3156dict_tp_clear(PyObject *op)
3157{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003158 PyDict_Clear(op);
3159 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00003160}
3161
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003162static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003163
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003164Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003165_PyDict_SizeOf(PyDictObject *mp)
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003166{
Victor Stinner742da042016-09-07 17:40:12 -07003167 Py_ssize_t size, usable, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003168
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003169 size = DK_SIZE(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07003170 usable = USABLE_FRACTION(size);
3171
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02003172 res = _PyObject_SIZE(Py_TYPE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003173 if (mp->ma_values)
Victor Stinner742da042016-09-07 17:40:12 -07003174 res += usable * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003175 /* If the dictionary is split, the keys portion is accounted-for
3176 in the type object. */
3177 if (mp->ma_keys->dk_refcnt == 1)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003178 res += (sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003179 + DK_IXSIZE(mp->ma_keys) * size
3180 + sizeof(PyDictKeyEntry) * usable);
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003181 return res;
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003182}
3183
3184Py_ssize_t
3185_PyDict_KeysSize(PyDictKeysObject *keys)
3186{
Victor Stinner98ee9d52016-09-08 09:33:56 -07003187 return (sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003188 + DK_IXSIZE(keys) * DK_SIZE(keys)
3189 + USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry));
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003190}
3191
doko@ubuntu.com17210f52016-01-14 14:04:59 +01003192static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303193dict_sizeof(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003194{
3195 return PyLong_FromSsize_t(_PyDict_SizeOf(mp));
3196}
3197
Brandt Buchereb8ac572020-02-24 19:47:34 -08003198static PyObject *
3199dict_or(PyObject *self, PyObject *other)
3200{
3201 if (!PyDict_Check(self) || !PyDict_Check(other)) {
3202 Py_RETURN_NOTIMPLEMENTED;
3203 }
3204 PyObject *new = PyDict_Copy(self);
3205 if (new == NULL) {
3206 return NULL;
3207 }
3208 if (dict_update_arg(new, other)) {
3209 Py_DECREF(new);
3210 return NULL;
3211 }
3212 return new;
3213}
3214
3215static PyObject *
3216dict_ior(PyObject *self, PyObject *other)
3217{
3218 if (dict_update_arg(self, other)) {
3219 return NULL;
3220 }
3221 Py_INCREF(self);
3222 return self;
3223}
3224
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00003225PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
3226
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003227PyDoc_STRVAR(sizeof__doc__,
3228"D.__sizeof__() -> size of D in memory, in bytes");
3229
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003230PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04003231"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
3232If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
3233If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
3234In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00003235
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003236PyDoc_STRVAR(clear__doc__,
3237"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003238
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003239PyDoc_STRVAR(copy__doc__,
3240"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00003241
Guido van Rossumb90c8482007-02-10 01:11:45 +00003242/* Forward */
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303243static PyObject *dictkeys_new(PyObject *, PyObject *);
3244static PyObject *dictitems_new(PyObject *, PyObject *);
3245static PyObject *dictvalues_new(PyObject *, PyObject *);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003246
Guido van Rossum45c85d12007-07-27 16:31:40 +00003247PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003248 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003249PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003250 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003251PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003252 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00003253
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003254static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07003255 DICT___CONTAINS___METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003256 {"__getitem__", (PyCFunction)(void(*)(void))dict_subscript, METH_O | METH_COEXIST,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003257 getitem__doc__},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003258 {"__sizeof__", (PyCFunction)(void(*)(void))dict_sizeof, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003259 sizeof__doc__},
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003260 DICT_GET_METHODDEF
3261 DICT_SETDEFAULT_METHODDEF
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003262 DICT_POP_METHODDEF
3263 DICT_POPITEM_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303264 {"keys", dictkeys_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003265 keys__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303266 {"items", dictitems_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003267 items__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303268 {"values", dictvalues_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003269 values__doc__},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003270 {"update", (PyCFunction)(void(*)(void))dict_update, METH_VARARGS | METH_KEYWORDS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003271 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08003272 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003273 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
3274 clear__doc__},
3275 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
3276 copy__doc__},
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003277 DICT___REVERSED___METHODDEF
Guido van Rossum48b069a2020-04-07 09:50:06 -07003278 {"__class_getitem__", (PyCFunction)Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003279 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003280};
3281
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00003282/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00003283int
3284PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003285{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003286 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07003287 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003288 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003289 PyObject *value;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003291 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003292 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003293 hash = PyObject_Hash(key);
3294 if (hash == -1)
3295 return -1;
3296 }
INADA Naoki778928b2017-08-03 23:45:15 +09003297 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003298 if (ix == DKIX_ERROR)
3299 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003300 return (ix != DKIX_EMPTY && value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003301}
3302
Thomas Wouterscf297e42007-02-23 15:07:44 +00003303/* Internal version of PyDict_Contains used when the hash value is already known */
3304int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003305_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00003306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003307 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003308 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -07003309 Py_ssize_t ix;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003310
INADA Naoki778928b2017-08-03 23:45:15 +09003311 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003312 if (ix == DKIX_ERROR)
3313 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003314 return (ix != DKIX_EMPTY && value != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00003315}
3316
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003317/* Hack to implement "key in dict" */
3318static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003319 0, /* sq_length */
3320 0, /* sq_concat */
3321 0, /* sq_repeat */
3322 0, /* sq_item */
3323 0, /* sq_slice */
3324 0, /* sq_ass_item */
3325 0, /* sq_ass_slice */
3326 PyDict_Contains, /* sq_contains */
3327 0, /* sq_inplace_concat */
3328 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003329};
3330
Brandt Buchereb8ac572020-02-24 19:47:34 -08003331static PyNumberMethods dict_as_number = {
3332 .nb_or = dict_or,
3333 .nb_inplace_or = dict_ior,
3334};
3335
Guido van Rossum09e563a2001-05-01 12:10:21 +00003336static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003337dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3338{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003339 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003340 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003342 assert(type != NULL && type->tp_alloc != NULL);
3343 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02003344 if (self == NULL)
3345 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003346 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003347
Victor Stinnera9f61a52013-07-16 22:17:26 +02003348 /* The object has been implicitly tracked by tp_alloc */
3349 if (type == &PyDict_Type)
3350 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003351
3352 d->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003353 d->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07003354 d->ma_keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003355 if (d->ma_keys == NULL) {
3356 Py_DECREF(self);
3357 return NULL;
3358 }
Victor Stinner0fc91ee2019-04-12 21:51:34 +02003359 ASSERT_CONSISTENT(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003360 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003361}
3362
Tim Peters25786c02001-09-02 08:22:48 +00003363static int
3364dict_init(PyObject *self, PyObject *args, PyObject *kwds)
3365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003366 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00003367}
3368
Tim Peters6d6c1a32001-08-02 04:15:00 +00003369static PyObject *
Dong-hee Nae27916b2020-04-02 09:55:43 +09003370dict_vectorcall(PyObject *type, PyObject * const*args,
3371 size_t nargsf, PyObject *kwnames)
3372{
3373 assert(PyType_Check(type));
3374 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
3375 if (!_PyArg_CheckPositional("dict", nargs, 0, 1)) {
3376 return NULL;
3377 }
3378
3379 PyObject *self = dict_new((PyTypeObject *)type, NULL, NULL);
3380 if (self == NULL) {
3381 return NULL;
3382 }
3383 if (nargs == 1) {
3384 if (dict_update_arg(self, args[0]) < 0) {
3385 Py_DECREF(self);
3386 return NULL;
3387 }
3388 args++;
3389 }
3390 if (kwnames != NULL) {
3391 for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(kwnames); i++) {
3392 if (PyDict_SetItem(self, PyTuple_GET_ITEM(kwnames, i), args[i]) < 0) {
3393 Py_DECREF(self);
3394 return NULL;
3395 }
3396 }
3397 }
3398 return self;
3399}
3400
3401static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003402dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00003403{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003404 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003405}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003406
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003407PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00003408"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00003409"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003410" (key, value) pairs\n"
3411"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00003412" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003413" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00003414" d[k] = v\n"
3415"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
3416" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00003417
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003418PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003419 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3420 "dict",
3421 sizeof(PyDictObject),
3422 0,
3423 (destructor)dict_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003424 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003425 0, /* tp_getattr */
3426 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003427 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003428 (reprfunc)dict_repr, /* tp_repr */
Brandt Buchereb8ac572020-02-24 19:47:34 -08003429 &dict_as_number, /* tp_as_number */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003430 &dict_as_sequence, /* tp_as_sequence */
3431 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003432 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003433 0, /* tp_call */
3434 0, /* tp_str */
3435 PyObject_GenericGetAttr, /* tp_getattro */
3436 0, /* tp_setattro */
3437 0, /* tp_as_buffer */
3438 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3439 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
3440 dictionary_doc, /* tp_doc */
3441 dict_traverse, /* tp_traverse */
3442 dict_tp_clear, /* tp_clear */
3443 dict_richcompare, /* tp_richcompare */
3444 0, /* tp_weaklistoffset */
3445 (getiterfunc)dict_iter, /* tp_iter */
3446 0, /* tp_iternext */
3447 mapp_methods, /* tp_methods */
3448 0, /* tp_members */
3449 0, /* tp_getset */
3450 0, /* tp_base */
3451 0, /* tp_dict */
3452 0, /* tp_descr_get */
3453 0, /* tp_descr_set */
3454 0, /* tp_dictoffset */
3455 dict_init, /* tp_init */
3456 PyType_GenericAlloc, /* tp_alloc */
3457 dict_new, /* tp_new */
3458 PyObject_GC_Del, /* tp_free */
Dong-hee Nae27916b2020-04-02 09:55:43 +09003459 .tp_vectorcall = dict_vectorcall,
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003460};
3461
Victor Stinner3c1e4812012-03-26 22:10:51 +02003462PyObject *
3463_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
3464{
3465 PyObject *kv;
3466 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02003467 if (kv == NULL) {
3468 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003469 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02003470 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02003471 return PyDict_GetItem(dp, kv);
3472}
3473
Guido van Rossum3cca2451997-05-16 14:23:33 +00003474/* For backward compatibility with old dictionary interface */
3475
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003476PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003477PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003478{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003479 PyObject *kv, *rv;
3480 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003481 if (kv == NULL) {
3482 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003483 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003484 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003485 rv = PyDict_GetItem(v, kv);
3486 Py_DECREF(kv);
3487 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003488}
3489
3490int
Victor Stinner3c1e4812012-03-26 22:10:51 +02003491_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
3492{
3493 PyObject *kv;
3494 kv = _PyUnicode_FromId(key); /* borrowed */
3495 if (kv == NULL)
3496 return -1;
3497 return PyDict_SetItem(v, kv, item);
3498}
3499
3500int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003501PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003502{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003503 PyObject *kv;
3504 int err;
3505 kv = PyUnicode_FromString(key);
3506 if (kv == NULL)
3507 return -1;
3508 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
3509 err = PyDict_SetItem(v, kv, item);
3510 Py_DECREF(kv);
3511 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003512}
3513
3514int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01003515_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
3516{
3517 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
3518 if (kv == NULL)
3519 return -1;
3520 return PyDict_DelItem(v, kv);
3521}
3522
3523int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003524PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003525{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003526 PyObject *kv;
3527 int err;
3528 kv = PyUnicode_FromString(key);
3529 if (kv == NULL)
3530 return -1;
3531 err = PyDict_DelItem(v, kv);
3532 Py_DECREF(kv);
3533 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003534}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003535
Raymond Hettinger019a1482004-03-18 02:41:19 +00003536/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003537
3538typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003539 PyObject_HEAD
3540 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
3541 Py_ssize_t di_used;
3542 Py_ssize_t di_pos;
3543 PyObject* di_result; /* reusable result tuple for iteritems */
3544 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003545} dictiterobject;
3546
3547static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003548dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003549{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003550 dictiterobject *di;
3551 di = PyObject_GC_New(dictiterobject, itertype);
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003552 if (di == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003553 return NULL;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003554 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003555 Py_INCREF(dict);
3556 di->di_dict = dict;
3557 di->di_used = dict->ma_used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003558 di->len = dict->ma_used;
Dong-hee Na24dc2f82019-10-20 05:01:08 +09003559 if (itertype == &PyDictRevIterKey_Type ||
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003560 itertype == &PyDictRevIterItem_Type ||
Dong-hee Na24dc2f82019-10-20 05:01:08 +09003561 itertype == &PyDictRevIterValue_Type) {
3562 if (dict->ma_values) {
3563 di->di_pos = dict->ma_used - 1;
3564 }
3565 else {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003566 di->di_pos = dict->ma_keys->dk_nentries - 1;
Dong-hee Na24dc2f82019-10-20 05:01:08 +09003567 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003568 }
3569 else {
3570 di->di_pos = 0;
3571 }
3572 if (itertype == &PyDictIterItem_Type ||
3573 itertype == &PyDictRevIterItem_Type) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003574 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
3575 if (di->di_result == NULL) {
3576 Py_DECREF(di);
3577 return NULL;
3578 }
3579 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003580 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003581 di->di_result = NULL;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003582 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003583 _PyObject_GC_TRACK(di);
3584 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003585}
3586
3587static void
3588dictiter_dealloc(dictiterobject *di)
3589{
INADA Naokia6296d32017-08-24 14:55:17 +09003590 /* bpo-31095: UnTrack is needed before calling any callbacks */
3591 _PyObject_GC_UNTRACK(di);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003592 Py_XDECREF(di->di_dict);
3593 Py_XDECREF(di->di_result);
3594 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003595}
3596
3597static int
3598dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
3599{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003600 Py_VISIT(di->di_dict);
3601 Py_VISIT(di->di_result);
3602 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003603}
3604
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003605static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303606dictiter_len(dictiterobject *di, PyObject *Py_UNUSED(ignored))
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003607{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003608 Py_ssize_t len = 0;
3609 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
3610 len = di->len;
3611 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003612}
3613
Guido van Rossumb90c8482007-02-10 01:11:45 +00003614PyDoc_STRVAR(length_hint_doc,
3615 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003616
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003617static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303618dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003619
3620PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3621
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003622static PyMethodDef dictiter_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003623 {"__length_hint__", (PyCFunction)(void(*)(void))dictiter_len, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003624 length_hint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003625 {"__reduce__", (PyCFunction)(void(*)(void))dictiter_reduce, METH_NOARGS,
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003626 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003627 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003628};
3629
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003630static PyObject*
3631dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00003632{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003633 PyObject *key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003634 Py_ssize_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003635 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003636 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00003637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003638 if (d == NULL)
3639 return NULL;
3640 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00003641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003642 if (di->di_used != d->ma_used) {
3643 PyErr_SetString(PyExc_RuntimeError,
3644 "dictionary changed size during iteration");
3645 di->di_used = -1; /* Make this state sticky */
3646 return NULL;
3647 }
Guido van Rossum2147df72002-07-16 20:30:22 +00003648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003649 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003650 k = d->ma_keys;
INADA Naokica2d8be2016-11-04 16:59:10 +09003651 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003652 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003653 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003654 goto fail;
3655 key = DK_ENTRIES(k)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003656 assert(d->ma_values[i] != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003657 }
3658 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003659 Py_ssize_t n = k->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003660 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
3661 while (i < n && entry_ptr->me_value == NULL) {
3662 entry_ptr++;
3663 i++;
3664 }
3665 if (i >= n)
3666 goto fail;
3667 key = entry_ptr->me_key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003668 }
Thomas Perl796cc6e2019-03-28 07:03:25 +01003669 // We found an element (key), but did not expect it
3670 if (di->len == 0) {
3671 PyErr_SetString(PyExc_RuntimeError,
3672 "dictionary keys changed during iteration");
3673 goto fail;
3674 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003675 di->di_pos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003676 di->len--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003677 Py_INCREF(key);
3678 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003679
3680fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003681 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003682 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003683 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003684}
3685
Raymond Hettinger019a1482004-03-18 02:41:19 +00003686PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003687 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3688 "dict_keyiterator", /* tp_name */
3689 sizeof(dictiterobject), /* tp_basicsize */
3690 0, /* tp_itemsize */
3691 /* methods */
3692 (destructor)dictiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003693 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003694 0, /* tp_getattr */
3695 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003696 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003697 0, /* tp_repr */
3698 0, /* tp_as_number */
3699 0, /* tp_as_sequence */
3700 0, /* tp_as_mapping */
3701 0, /* tp_hash */
3702 0, /* tp_call */
3703 0, /* tp_str */
3704 PyObject_GenericGetAttr, /* tp_getattro */
3705 0, /* tp_setattro */
3706 0, /* tp_as_buffer */
3707 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3708 0, /* tp_doc */
3709 (traverseproc)dictiter_traverse, /* tp_traverse */
3710 0, /* tp_clear */
3711 0, /* tp_richcompare */
3712 0, /* tp_weaklistoffset */
3713 PyObject_SelfIter, /* tp_iter */
3714 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
3715 dictiter_methods, /* tp_methods */
3716 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003717};
3718
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003719static PyObject *
3720dictiter_iternextvalue(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003721{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003722 PyObject *value;
INADA Naokica2d8be2016-11-04 16:59:10 +09003723 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003724 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003725
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003726 if (d == NULL)
3727 return NULL;
3728 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003730 if (di->di_used != d->ma_used) {
3731 PyErr_SetString(PyExc_RuntimeError,
3732 "dictionary changed size during iteration");
3733 di->di_used = -1; /* Make this state sticky */
3734 return NULL;
3735 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003736
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003737 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003738 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003739 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003740 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003741 goto fail;
INADA Naokica2d8be2016-11-04 16:59:10 +09003742 value = d->ma_values[i];
3743 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003744 }
3745 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003746 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003747 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3748 while (i < n && entry_ptr->me_value == NULL) {
3749 entry_ptr++;
3750 i++;
3751 }
3752 if (i >= n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003753 goto fail;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003754 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003755 }
Thomas Perlb8311cf2019-04-02 11:30:10 +02003756 // We found an element, but did not expect it
3757 if (di->len == 0) {
3758 PyErr_SetString(PyExc_RuntimeError,
3759 "dictionary keys changed during iteration");
3760 goto fail;
3761 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003762 di->di_pos = i+1;
3763 di->len--;
3764 Py_INCREF(value);
3765 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003766
3767fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003768 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003769 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003770 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003771}
3772
3773PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003774 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3775 "dict_valueiterator", /* tp_name */
3776 sizeof(dictiterobject), /* tp_basicsize */
3777 0, /* tp_itemsize */
3778 /* methods */
3779 (destructor)dictiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003780 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003781 0, /* tp_getattr */
3782 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003783 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003784 0, /* tp_repr */
3785 0, /* tp_as_number */
3786 0, /* tp_as_sequence */
3787 0, /* tp_as_mapping */
3788 0, /* tp_hash */
3789 0, /* tp_call */
3790 0, /* tp_str */
3791 PyObject_GenericGetAttr, /* tp_getattro */
3792 0, /* tp_setattro */
3793 0, /* tp_as_buffer */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003794 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003795 0, /* tp_doc */
3796 (traverseproc)dictiter_traverse, /* tp_traverse */
3797 0, /* tp_clear */
3798 0, /* tp_richcompare */
3799 0, /* tp_weaklistoffset */
3800 PyObject_SelfIter, /* tp_iter */
3801 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3802 dictiter_methods, /* tp_methods */
3803 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003804};
3805
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003806static PyObject *
3807dictiter_iternextitem(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003808{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003809 PyObject *key, *value, *result;
INADA Naokica2d8be2016-11-04 16:59:10 +09003810 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003811 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003813 if (d == NULL)
3814 return NULL;
3815 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003816
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003817 if (di->di_used != d->ma_used) {
3818 PyErr_SetString(PyExc_RuntimeError,
3819 "dictionary changed size during iteration");
3820 di->di_used = -1; /* Make this state sticky */
3821 return NULL;
3822 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003824 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003825 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003826 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003827 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003828 goto fail;
3829 key = DK_ENTRIES(d->ma_keys)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003830 value = d->ma_values[i];
3831 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003832 }
3833 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003834 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003835 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3836 while (i < n && entry_ptr->me_value == NULL) {
3837 entry_ptr++;
3838 i++;
3839 }
3840 if (i >= n)
3841 goto fail;
3842 key = entry_ptr->me_key;
3843 value = entry_ptr->me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003844 }
Thomas Perlb8311cf2019-04-02 11:30:10 +02003845 // We found an element, but did not expect it
3846 if (di->len == 0) {
3847 PyErr_SetString(PyExc_RuntimeError,
3848 "dictionary keys changed during iteration");
3849 goto fail;
3850 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003851 di->di_pos = i+1;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003852 di->len--;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003853 Py_INCREF(key);
3854 Py_INCREF(value);
3855 result = di->di_result;
3856 if (Py_REFCNT(result) == 1) {
3857 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3858 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3859 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3860 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003861 Py_INCREF(result);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003862 Py_DECREF(oldkey);
3863 Py_DECREF(oldvalue);
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003864 }
3865 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003866 result = PyTuple_New(2);
3867 if (result == NULL)
3868 return NULL;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003869 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3870 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003871 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003872 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003873
3874fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003875 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003876 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003877 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003878}
3879
3880PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003881 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3882 "dict_itemiterator", /* tp_name */
3883 sizeof(dictiterobject), /* tp_basicsize */
3884 0, /* tp_itemsize */
3885 /* methods */
3886 (destructor)dictiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003887 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003888 0, /* tp_getattr */
3889 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003890 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003891 0, /* tp_repr */
3892 0, /* tp_as_number */
3893 0, /* tp_as_sequence */
3894 0, /* tp_as_mapping */
3895 0, /* tp_hash */
3896 0, /* tp_call */
3897 0, /* tp_str */
3898 PyObject_GenericGetAttr, /* tp_getattro */
3899 0, /* tp_setattro */
3900 0, /* tp_as_buffer */
3901 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3902 0, /* tp_doc */
3903 (traverseproc)dictiter_traverse, /* tp_traverse */
3904 0, /* tp_clear */
3905 0, /* tp_richcompare */
3906 0, /* tp_weaklistoffset */
3907 PyObject_SelfIter, /* tp_iter */
3908 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3909 dictiter_methods, /* tp_methods */
3910 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003911};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003912
3913
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003914/* dictreviter */
3915
3916static PyObject *
3917dictreviter_iternext(dictiterobject *di)
3918{
3919 PyDictObject *d = di->di_dict;
3920
3921 if (d == NULL) {
3922 return NULL;
3923 }
3924 assert (PyDict_Check(d));
3925
3926 if (di->di_used != d->ma_used) {
3927 PyErr_SetString(PyExc_RuntimeError,
3928 "dictionary changed size during iteration");
3929 di->di_used = -1; /* Make this state sticky */
3930 return NULL;
3931 }
3932
3933 Py_ssize_t i = di->di_pos;
3934 PyDictKeysObject *k = d->ma_keys;
3935 PyObject *key, *value, *result;
3936
Serhiy Storchaka2e3d8732019-10-23 14:48:08 +03003937 if (i < 0) {
3938 goto fail;
3939 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003940 if (d->ma_values) {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003941 key = DK_ENTRIES(k)[i].me_key;
3942 value = d->ma_values[i];
3943 assert (value != NULL);
3944 }
3945 else {
3946 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
Serhiy Storchaka2e3d8732019-10-23 14:48:08 +03003947 while (entry_ptr->me_value == NULL) {
3948 if (--i < 0) {
3949 goto fail;
3950 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003951 entry_ptr--;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003952 }
3953 key = entry_ptr->me_key;
3954 value = entry_ptr->me_value;
3955 }
3956 di->di_pos = i-1;
3957 di->len--;
3958
Dong-hee Na1b55b652020-02-17 19:09:15 +09003959 if (Py_IS_TYPE(di, &PyDictRevIterKey_Type)) {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003960 Py_INCREF(key);
3961 return key;
3962 }
Dong-hee Na1b55b652020-02-17 19:09:15 +09003963 else if (Py_IS_TYPE(di, &PyDictRevIterValue_Type)) {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003964 Py_INCREF(value);
3965 return value;
3966 }
Dong-hee Na1b55b652020-02-17 19:09:15 +09003967 else if (Py_IS_TYPE(di, &PyDictRevIterItem_Type)) {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003968 Py_INCREF(key);
3969 Py_INCREF(value);
3970 result = di->di_result;
3971 if (Py_REFCNT(result) == 1) {
3972 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3973 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3974 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3975 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
3976 Py_INCREF(result);
3977 Py_DECREF(oldkey);
3978 Py_DECREF(oldvalue);
3979 }
3980 else {
3981 result = PyTuple_New(2);
3982 if (result == NULL) {
3983 return NULL;
3984 }
3985 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3986 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
3987 }
3988 return result;
3989 }
3990 else {
3991 Py_UNREACHABLE();
3992 }
3993
3994fail:
3995 di->di_dict = NULL;
3996 Py_DECREF(d);
3997 return NULL;
3998}
3999
4000PyTypeObject PyDictRevIterKey_Type = {
4001 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4002 "dict_reversekeyiterator",
4003 sizeof(dictiterobject),
4004 .tp_dealloc = (destructor)dictiter_dealloc,
4005 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
4006 .tp_traverse = (traverseproc)dictiter_traverse,
4007 .tp_iter = PyObject_SelfIter,
4008 .tp_iternext = (iternextfunc)dictreviter_iternext,
4009 .tp_methods = dictiter_methods
4010};
4011
4012
4013/*[clinic input]
4014dict.__reversed__
4015
4016Return a reverse iterator over the dict keys.
4017[clinic start generated code]*/
4018
4019static PyObject *
4020dict___reversed___impl(PyDictObject *self)
4021/*[clinic end generated code: output=e674483336d1ed51 input=23210ef3477d8c4d]*/
4022{
4023 assert (PyDict_Check(self));
4024 return dictiter_new(self, &PyDictRevIterKey_Type);
4025}
4026
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004027static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304028dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004029{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004030 _Py_IDENTIFIER(iter);
Sergey Fedoseev63958442018-10-20 05:43:33 +05004031 /* copy the iterator state */
4032 dictiterobject tmp = *di;
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004033 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004034
Sergey Fedoseev63958442018-10-20 05:43:33 +05004035 PyObject *list = PySequence_List((PyObject*)&tmp);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004036 Py_XDECREF(tmp.di_dict);
Sergey Fedoseev63958442018-10-20 05:43:33 +05004037 if (list == NULL) {
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004038 return NULL;
4039 }
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004040 return Py_BuildValue("N(N)", _PyEval_GetBuiltinId(&PyId_iter), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004041}
4042
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004043PyTypeObject PyDictRevIterItem_Type = {
4044 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4045 "dict_reverseitemiterator",
4046 sizeof(dictiterobject),
4047 .tp_dealloc = (destructor)dictiter_dealloc,
4048 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
4049 .tp_traverse = (traverseproc)dictiter_traverse,
4050 .tp_iter = PyObject_SelfIter,
4051 .tp_iternext = (iternextfunc)dictreviter_iternext,
4052 .tp_methods = dictiter_methods
4053};
4054
4055PyTypeObject PyDictRevIterValue_Type = {
4056 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4057 "dict_reversevalueiterator",
4058 sizeof(dictiterobject),
4059 .tp_dealloc = (destructor)dictiter_dealloc,
4060 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
4061 .tp_traverse = (traverseproc)dictiter_traverse,
4062 .tp_iter = PyObject_SelfIter,
4063 .tp_iternext = (iternextfunc)dictreviter_iternext,
4064 .tp_methods = dictiter_methods
4065};
4066
Guido van Rossum3ac67412007-02-10 18:55:06 +00004067/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004068/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00004069/***********************************************/
4070
Guido van Rossumb90c8482007-02-10 01:11:45 +00004071/* The instance lay-out is the same for all three; but the type differs. */
4072
Guido van Rossumb90c8482007-02-10 01:11:45 +00004073static void
Eric Snow96c6af92015-05-29 22:21:39 -06004074dictview_dealloc(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004075{
INADA Naokia6296d32017-08-24 14:55:17 +09004076 /* bpo-31095: UnTrack is needed before calling any callbacks */
4077 _PyObject_GC_UNTRACK(dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004078 Py_XDECREF(dv->dv_dict);
4079 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00004080}
4081
4082static int
Eric Snow96c6af92015-05-29 22:21:39 -06004083dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
Antoine Pitrou7ddda782009-01-01 15:35:33 +00004084{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004085 Py_VISIT(dv->dv_dict);
4086 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004087}
4088
Guido van Rossum83825ac2007-02-10 04:54:19 +00004089static Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06004090dictview_len(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004091{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004092 Py_ssize_t len = 0;
4093 if (dv->dv_dict != NULL)
4094 len = dv->dv_dict->ma_used;
4095 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004096}
4097
Eric Snow96c6af92015-05-29 22:21:39 -06004098PyObject *
4099_PyDictView_New(PyObject *dict, PyTypeObject *type)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004100{
Eric Snow96c6af92015-05-29 22:21:39 -06004101 _PyDictViewObject *dv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004102 if (dict == NULL) {
4103 PyErr_BadInternalCall();
4104 return NULL;
4105 }
4106 if (!PyDict_Check(dict)) {
4107 /* XXX Get rid of this restriction later */
4108 PyErr_Format(PyExc_TypeError,
4109 "%s() requires a dict argument, not '%s'",
Victor Stinner58ac7002020-02-07 03:04:21 +01004110 type->tp_name, Py_TYPE(dict)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004111 return NULL;
4112 }
Eric Snow96c6af92015-05-29 22:21:39 -06004113 dv = PyObject_GC_New(_PyDictViewObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004114 if (dv == NULL)
4115 return NULL;
4116 Py_INCREF(dict);
4117 dv->dv_dict = (PyDictObject *)dict;
4118 _PyObject_GC_TRACK(dv);
4119 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004120}
4121
Neal Norwitze36f2ba2007-02-26 23:12:28 +00004122/* TODO(guido): The views objects are not complete:
4123
4124 * support more set operations
4125 * support arbitrary mappings?
4126 - either these should be static or exported in dictobject.h
4127 - if public then they should probably be in builtins
4128*/
4129
Guido van Rossumaac530c2007-08-24 22:33:45 +00004130/* Return 1 if self is a subset of other, iterating over self;
4131 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00004132static int
4133all_contained_in(PyObject *self, PyObject *other)
4134{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004135 PyObject *iter = PyObject_GetIter(self);
4136 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00004137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004138 if (iter == NULL)
4139 return -1;
4140 for (;;) {
4141 PyObject *next = PyIter_Next(iter);
4142 if (next == NULL) {
4143 if (PyErr_Occurred())
4144 ok = -1;
4145 break;
4146 }
4147 ok = PySequence_Contains(other, next);
4148 Py_DECREF(next);
4149 if (ok <= 0)
4150 break;
4151 }
4152 Py_DECREF(iter);
4153 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00004154}
4155
4156static PyObject *
4157dictview_richcompare(PyObject *self, PyObject *other, int op)
4158{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004159 Py_ssize_t len_self, len_other;
4160 int ok;
4161 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004162
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004163 assert(self != NULL);
4164 assert(PyDictViewSet_Check(self));
4165 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00004166
Brian Curtindfc80e32011-08-10 20:28:54 -05004167 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
4168 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004170 len_self = PyObject_Size(self);
4171 if (len_self < 0)
4172 return NULL;
4173 len_other = PyObject_Size(other);
4174 if (len_other < 0)
4175 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004177 ok = 0;
4178 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00004179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004180 case Py_NE:
4181 case Py_EQ:
4182 if (len_self == len_other)
4183 ok = all_contained_in(self, other);
4184 if (op == Py_NE && ok >= 0)
4185 ok = !ok;
4186 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004187
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004188 case Py_LT:
4189 if (len_self < len_other)
4190 ok = all_contained_in(self, other);
4191 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004193 case Py_LE:
4194 if (len_self <= len_other)
4195 ok = all_contained_in(self, other);
4196 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004197
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004198 case Py_GT:
4199 if (len_self > len_other)
4200 ok = all_contained_in(other, self);
4201 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004202
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004203 case Py_GE:
4204 if (len_self >= len_other)
4205 ok = all_contained_in(other, self);
4206 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004208 }
4209 if (ok < 0)
4210 return NULL;
4211 result = ok ? Py_True : Py_False;
4212 Py_INCREF(result);
4213 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00004214}
4215
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004216static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004217dictview_repr(_PyDictViewObject *dv)
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004219 PyObject *seq;
bennorthd7773d92018-01-26 15:46:01 +00004220 PyObject *result = NULL;
4221 Py_ssize_t rc;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004222
bennorthd7773d92018-01-26 15:46:01 +00004223 rc = Py_ReprEnter((PyObject *)dv);
4224 if (rc != 0) {
4225 return rc > 0 ? PyUnicode_FromString("...") : NULL;
4226 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004227 seq = PySequence_List((PyObject *)dv);
bennorthd7773d92018-01-26 15:46:01 +00004228 if (seq == NULL) {
4229 goto Done;
4230 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004231 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
4232 Py_DECREF(seq);
bennorthd7773d92018-01-26 15:46:01 +00004233
4234Done:
4235 Py_ReprLeave((PyObject *)dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004236 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004237}
4238
Guido van Rossum3ac67412007-02-10 18:55:06 +00004239/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004240
4241static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004242dictkeys_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004243{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004244 if (dv->dv_dict == NULL) {
4245 Py_RETURN_NONE;
4246 }
4247 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004248}
4249
4250static int
Eric Snow96c6af92015-05-29 22:21:39 -06004251dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004252{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004253 if (dv->dv_dict == NULL)
4254 return 0;
4255 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004256}
4257
Guido van Rossum83825ac2007-02-10 04:54:19 +00004258static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004259 (lenfunc)dictview_len, /* sq_length */
4260 0, /* sq_concat */
4261 0, /* sq_repeat */
4262 0, /* sq_item */
4263 0, /* sq_slice */
4264 0, /* sq_ass_item */
4265 0, /* sq_ass_slice */
4266 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004267};
4268
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004269// Create an set object from dictviews object.
4270// Returns a new reference.
4271// This utility function is used by set operations.
Guido van Rossum523259b2007-08-24 23:41:22 +00004272static PyObject*
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004273dictviews_to_set(PyObject *self)
Guido van Rossum523259b2007-08-24 23:41:22 +00004274{
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004275 PyObject *left = self;
4276 if (PyDictKeys_Check(self)) {
4277 // PySet_New() has fast path for the dict object.
4278 PyObject *dict = (PyObject *)((_PyDictViewObject *)self)->dv_dict;
4279 if (PyDict_CheckExact(dict)) {
4280 left = dict;
4281 }
4282 }
4283 return PySet_New(left);
4284}
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004285
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004286static PyObject*
4287dictviews_sub(PyObject *self, PyObject *other)
4288{
4289 PyObject *result = dictviews_to_set(self);
4290 if (result == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004291 return NULL;
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004292 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004293
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004294 _Py_IDENTIFIER(difference_update);
4295 PyObject *tmp = _PyObject_CallMethodIdOneArg(
4296 result, &PyId_difference_update, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004297 if (tmp == NULL) {
4298 Py_DECREF(result);
4299 return NULL;
4300 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004302 Py_DECREF(tmp);
4303 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004304}
4305
Forest Gregg998cf1f2019-08-26 02:17:43 -05004306static int
4307dictitems_contains(_PyDictViewObject *dv, PyObject *obj);
4308
4309PyObject *
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004310_PyDictView_Intersect(PyObject* self, PyObject *other)
Guido van Rossum523259b2007-08-24 23:41:22 +00004311{
Forest Gregg998cf1f2019-08-26 02:17:43 -05004312 PyObject *result;
4313 PyObject *it;
4314 PyObject *key;
4315 Py_ssize_t len_self;
4316 int rv;
4317 int (*dict_contains)(_PyDictViewObject *, PyObject *);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004318
Forest Gregg998cf1f2019-08-26 02:17:43 -05004319 /* Python interpreter swaps parameters when dict view
4320 is on right side of & */
4321 if (!PyDictViewSet_Check(self)) {
4322 PyObject *tmp = other;
4323 other = self;
4324 self = tmp;
4325 }
4326
4327 len_self = dictview_len((_PyDictViewObject *)self);
4328
4329 /* if other is a set and self is smaller than other,
4330 reuse set intersection logic */
Dong-hee Na1b55b652020-02-17 19:09:15 +09004331 if (Py_IS_TYPE(other, &PySet_Type) && len_self <= PyObject_Size(other)) {
Forest Gregg998cf1f2019-08-26 02:17:43 -05004332 _Py_IDENTIFIER(intersection);
4333 return _PyObject_CallMethodIdObjArgs(other, &PyId_intersection, self, NULL);
4334 }
4335
4336 /* if other is another dict view, and it is bigger than self,
4337 swap them */
4338 if (PyDictViewSet_Check(other)) {
4339 Py_ssize_t len_other = dictview_len((_PyDictViewObject *)other);
4340 if (len_other > len_self) {
4341 PyObject *tmp = other;
4342 other = self;
4343 self = tmp;
4344 }
4345 }
4346
4347 /* at this point, two things should be true
4348 1. self is a dictview
4349 2. if other is a dictview then it is smaller than self */
4350 result = PySet_New(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004351 if (result == NULL)
4352 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004353
Forest Gregg998cf1f2019-08-26 02:17:43 -05004354 it = PyObject_GetIter(other);
Zackery Spytzb16e3822019-10-13 05:49:05 -06004355 if (it == NULL) {
4356 Py_DECREF(result);
4357 return NULL;
4358 }
Forest Gregg998cf1f2019-08-26 02:17:43 -05004359
Forest Gregg998cf1f2019-08-26 02:17:43 -05004360 if (PyDictKeys_Check(self)) {
4361 dict_contains = dictkeys_contains;
4362 }
4363 /* else PyDictItems_Check(self) */
4364 else {
4365 dict_contains = dictitems_contains;
4366 }
4367
4368 while ((key = PyIter_Next(it)) != NULL) {
4369 rv = dict_contains((_PyDictViewObject *)self, key);
4370 if (rv < 0) {
4371 goto error;
4372 }
4373 if (rv) {
4374 if (PySet_Add(result, key)) {
4375 goto error;
4376 }
4377 }
4378 Py_DECREF(key);
4379 }
4380 Py_DECREF(it);
4381 if (PyErr_Occurred()) {
4382 Py_DECREF(result);
4383 return NULL;
4384 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004385 return result;
Forest Gregg998cf1f2019-08-26 02:17:43 -05004386
4387error:
4388 Py_DECREF(it);
4389 Py_DECREF(result);
4390 Py_DECREF(key);
4391 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004392}
4393
4394static PyObject*
4395dictviews_or(PyObject* self, PyObject *other)
4396{
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004397 PyObject *result = dictviews_to_set(self);
4398 if (result == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004399 return NULL;
4400 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004401
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004402 if (_PySet_Update(result, other) < 0) {
4403 Py_DECREF(result);
4404 return NULL;
4405 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004406 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004407}
4408
4409static PyObject*
4410dictviews_xor(PyObject* self, PyObject *other)
4411{
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004412 PyObject *result = dictviews_to_set(self);
4413 if (result == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004414 return NULL;
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004415 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004416
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004417 _Py_IDENTIFIER(symmetric_difference_update);
4418 PyObject *tmp = _PyObject_CallMethodIdOneArg(
4419 result, &PyId_symmetric_difference_update, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004420 if (tmp == NULL) {
4421 Py_DECREF(result);
4422 return NULL;
4423 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004425 Py_DECREF(tmp);
4426 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004427}
4428
4429static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004430 0, /*nb_add*/
4431 (binaryfunc)dictviews_sub, /*nb_subtract*/
4432 0, /*nb_multiply*/
4433 0, /*nb_remainder*/
4434 0, /*nb_divmod*/
4435 0, /*nb_power*/
4436 0, /*nb_negative*/
4437 0, /*nb_positive*/
4438 0, /*nb_absolute*/
4439 0, /*nb_bool*/
4440 0, /*nb_invert*/
4441 0, /*nb_lshift*/
4442 0, /*nb_rshift*/
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004443 (binaryfunc)_PyDictView_Intersect, /*nb_and*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004444 (binaryfunc)dictviews_xor, /*nb_xor*/
4445 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00004446};
4447
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004448static PyObject*
4449dictviews_isdisjoint(PyObject *self, PyObject *other)
4450{
4451 PyObject *it;
4452 PyObject *item = NULL;
4453
4454 if (self == other) {
Eric Snow96c6af92015-05-29 22:21:39 -06004455 if (dictview_len((_PyDictViewObject *)self) == 0)
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004456 Py_RETURN_TRUE;
4457 else
4458 Py_RETURN_FALSE;
4459 }
4460
4461 /* Iterate over the shorter object (only if other is a set,
4462 * because PySequence_Contains may be expensive otherwise): */
4463 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
Eric Snow96c6af92015-05-29 22:21:39 -06004464 Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self);
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004465 Py_ssize_t len_other = PyObject_Size(other);
4466 if (len_other == -1)
4467 return NULL;
4468
4469 if ((len_other > len_self)) {
4470 PyObject *tmp = other;
4471 other = self;
4472 self = tmp;
4473 }
4474 }
4475
4476 it = PyObject_GetIter(other);
4477 if (it == NULL)
4478 return NULL;
4479
4480 while ((item = PyIter_Next(it)) != NULL) {
4481 int contains = PySequence_Contains(self, item);
4482 Py_DECREF(item);
4483 if (contains == -1) {
4484 Py_DECREF(it);
4485 return NULL;
4486 }
4487
4488 if (contains) {
4489 Py_DECREF(it);
4490 Py_RETURN_FALSE;
4491 }
4492 }
4493 Py_DECREF(it);
4494 if (PyErr_Occurred())
4495 return NULL; /* PyIter_Next raised an exception. */
4496 Py_RETURN_TRUE;
4497}
4498
4499PyDoc_STRVAR(isdisjoint_doc,
4500"Return True if the view and the given iterable have a null intersection.");
4501
Serhiy Storchaka81524022018-11-27 13:05:02 +02004502static PyObject* dictkeys_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored));
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004503
4504PyDoc_STRVAR(reversed_keys_doc,
4505"Return a reverse iterator over the dict keys.");
4506
Guido van Rossumb90c8482007-02-10 01:11:45 +00004507static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004508 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4509 isdisjoint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004510 {"__reversed__", (PyCFunction)(void(*)(void))dictkeys_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004511 reversed_keys_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004512 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004513};
4514
4515PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004516 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4517 "dict_keys", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004518 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004519 0, /* tp_itemsize */
4520 /* methods */
4521 (destructor)dictview_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004522 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004523 0, /* tp_getattr */
4524 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004525 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004526 (reprfunc)dictview_repr, /* tp_repr */
4527 &dictviews_as_number, /* tp_as_number */
4528 &dictkeys_as_sequence, /* tp_as_sequence */
4529 0, /* tp_as_mapping */
4530 0, /* tp_hash */
4531 0, /* tp_call */
4532 0, /* tp_str */
4533 PyObject_GenericGetAttr, /* tp_getattro */
4534 0, /* tp_setattro */
4535 0, /* tp_as_buffer */
4536 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4537 0, /* tp_doc */
4538 (traverseproc)dictview_traverse, /* tp_traverse */
4539 0, /* tp_clear */
4540 dictview_richcompare, /* tp_richcompare */
4541 0, /* tp_weaklistoffset */
4542 (getiterfunc)dictkeys_iter, /* tp_iter */
4543 0, /* tp_iternext */
4544 dictkeys_methods, /* tp_methods */
4545 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004546};
4547
4548static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304549dictkeys_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004550{
Eric Snow96c6af92015-05-29 22:21:39 -06004551 return _PyDictView_New(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004552}
4553
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004554static PyObject *
Serhiy Storchaka81524022018-11-27 13:05:02 +02004555dictkeys_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored))
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004556{
4557 if (dv->dv_dict == NULL) {
4558 Py_RETURN_NONE;
4559 }
4560 return dictiter_new(dv->dv_dict, &PyDictRevIterKey_Type);
4561}
4562
Guido van Rossum3ac67412007-02-10 18:55:06 +00004563/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004564
4565static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004566dictitems_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004567{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004568 if (dv->dv_dict == NULL) {
4569 Py_RETURN_NONE;
4570 }
4571 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004572}
4573
4574static int
Eric Snow96c6af92015-05-29 22:21:39 -06004575dictitems_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004576{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004577 int result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004578 PyObject *key, *value, *found;
4579 if (dv->dv_dict == NULL)
4580 return 0;
4581 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
4582 return 0;
4583 key = PyTuple_GET_ITEM(obj, 0);
4584 value = PyTuple_GET_ITEM(obj, 1);
Raymond Hettinger6692f012016-09-18 21:46:08 -07004585 found = PyDict_GetItemWithError((PyObject *)dv->dv_dict, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004586 if (found == NULL) {
4587 if (PyErr_Occurred())
4588 return -1;
4589 return 0;
4590 }
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004591 Py_INCREF(found);
Serhiy Storchaka18b711c2019-08-04 14:12:48 +03004592 result = PyObject_RichCompareBool(found, value, Py_EQ);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004593 Py_DECREF(found);
4594 return result;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004595}
4596
Guido van Rossum83825ac2007-02-10 04:54:19 +00004597static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004598 (lenfunc)dictview_len, /* sq_length */
4599 0, /* sq_concat */
4600 0, /* sq_repeat */
4601 0, /* sq_item */
4602 0, /* sq_slice */
4603 0, /* sq_ass_item */
4604 0, /* sq_ass_slice */
4605 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004606};
4607
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004608static PyObject* dictitems_reversed(_PyDictViewObject *dv);
4609
4610PyDoc_STRVAR(reversed_items_doc,
4611"Return a reverse iterator over the dict items.");
4612
Guido van Rossumb90c8482007-02-10 01:11:45 +00004613static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004614 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4615 isdisjoint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004616 {"__reversed__", (PyCFunction)(void(*)(void))dictitems_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004617 reversed_items_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004618 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004619};
4620
4621PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004622 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4623 "dict_items", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004624 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004625 0, /* tp_itemsize */
4626 /* methods */
4627 (destructor)dictview_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004628 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004629 0, /* tp_getattr */
4630 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004631 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004632 (reprfunc)dictview_repr, /* tp_repr */
4633 &dictviews_as_number, /* tp_as_number */
4634 &dictitems_as_sequence, /* tp_as_sequence */
4635 0, /* tp_as_mapping */
4636 0, /* tp_hash */
4637 0, /* tp_call */
4638 0, /* tp_str */
4639 PyObject_GenericGetAttr, /* tp_getattro */
4640 0, /* tp_setattro */
4641 0, /* tp_as_buffer */
4642 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4643 0, /* tp_doc */
4644 (traverseproc)dictview_traverse, /* tp_traverse */
4645 0, /* tp_clear */
4646 dictview_richcompare, /* tp_richcompare */
4647 0, /* tp_weaklistoffset */
4648 (getiterfunc)dictitems_iter, /* tp_iter */
4649 0, /* tp_iternext */
4650 dictitems_methods, /* tp_methods */
4651 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004652};
4653
4654static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304655dictitems_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004656{
Eric Snow96c6af92015-05-29 22:21:39 -06004657 return _PyDictView_New(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004658}
4659
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004660static PyObject *
4661dictitems_reversed(_PyDictViewObject *dv)
4662{
4663 if (dv->dv_dict == NULL) {
4664 Py_RETURN_NONE;
4665 }
4666 return dictiter_new(dv->dv_dict, &PyDictRevIterItem_Type);
4667}
4668
Guido van Rossum3ac67412007-02-10 18:55:06 +00004669/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004670
4671static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004672dictvalues_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004673{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004674 if (dv->dv_dict == NULL) {
4675 Py_RETURN_NONE;
4676 }
4677 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004678}
4679
Guido van Rossum83825ac2007-02-10 04:54:19 +00004680static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004681 (lenfunc)dictview_len, /* sq_length */
4682 0, /* sq_concat */
4683 0, /* sq_repeat */
4684 0, /* sq_item */
4685 0, /* sq_slice */
4686 0, /* sq_ass_item */
4687 0, /* sq_ass_slice */
4688 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004689};
4690
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004691static PyObject* dictvalues_reversed(_PyDictViewObject *dv);
4692
4693PyDoc_STRVAR(reversed_values_doc,
4694"Return a reverse iterator over the dict values.");
4695
Guido van Rossumb90c8482007-02-10 01:11:45 +00004696static PyMethodDef dictvalues_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004697 {"__reversed__", (PyCFunction)(void(*)(void))dictvalues_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004698 reversed_values_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004699 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004700};
4701
4702PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004703 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4704 "dict_values", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004705 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004706 0, /* tp_itemsize */
4707 /* methods */
4708 (destructor)dictview_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004709 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004710 0, /* tp_getattr */
4711 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004712 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004713 (reprfunc)dictview_repr, /* tp_repr */
4714 0, /* tp_as_number */
4715 &dictvalues_as_sequence, /* tp_as_sequence */
4716 0, /* tp_as_mapping */
4717 0, /* tp_hash */
4718 0, /* tp_call */
4719 0, /* tp_str */
4720 PyObject_GenericGetAttr, /* tp_getattro */
4721 0, /* tp_setattro */
4722 0, /* tp_as_buffer */
4723 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4724 0, /* tp_doc */
4725 (traverseproc)dictview_traverse, /* tp_traverse */
4726 0, /* tp_clear */
4727 0, /* tp_richcompare */
4728 0, /* tp_weaklistoffset */
4729 (getiterfunc)dictvalues_iter, /* tp_iter */
4730 0, /* tp_iternext */
4731 dictvalues_methods, /* tp_methods */
4732 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004733};
4734
4735static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304736dictvalues_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004737{
Eric Snow96c6af92015-05-29 22:21:39 -06004738 return _PyDictView_New(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004739}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004740
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004741static PyObject *
4742dictvalues_reversed(_PyDictViewObject *dv)
4743{
4744 if (dv->dv_dict == NULL) {
4745 Py_RETURN_NONE;
4746 }
4747 return dictiter_new(dv->dv_dict, &PyDictRevIterValue_Type);
4748}
4749
4750
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004751/* Returns NULL if cannot allocate a new PyDictKeysObject,
4752 but does not set an error */
4753PyDictKeysObject *
4754_PyDict_NewKeysForClass(void)
4755{
Victor Stinner742da042016-09-07 17:40:12 -07004756 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004757 if (keys == NULL)
4758 PyErr_Clear();
4759 else
4760 keys->dk_lookup = lookdict_split;
4761 return keys;
4762}
4763
4764#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
4765
4766PyObject *
4767PyObject_GenericGetDict(PyObject *obj, void *context)
4768{
4769 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
4770 if (dictptr == NULL) {
4771 PyErr_SetString(PyExc_AttributeError,
4772 "This object has no __dict__");
4773 return NULL;
4774 }
4775 dict = *dictptr;
4776 if (dict == NULL) {
4777 PyTypeObject *tp = Py_TYPE(obj);
4778 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
INADA Naokia7576492018-11-14 18:39:27 +09004779 dictkeys_incref(CACHED_KEYS(tp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004780 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
4781 }
4782 else {
4783 *dictptr = dict = PyDict_New();
4784 }
4785 }
4786 Py_XINCREF(dict);
4787 return dict;
4788}
4789
4790int
4791_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
Victor Stinner742da042016-09-07 17:40:12 -07004792 PyObject *key, PyObject *value)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004793{
4794 PyObject *dict;
4795 int res;
4796 PyDictKeysObject *cached;
4797
4798 assert(dictptr != NULL);
4799 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
4800 assert(dictptr != NULL);
4801 dict = *dictptr;
4802 if (dict == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +09004803 dictkeys_incref(cached);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004804 dict = new_dict_with_shared_keys(cached);
4805 if (dict == NULL)
4806 return -1;
4807 *dictptr = dict;
4808 }
4809 if (value == NULL) {
4810 res = PyDict_DelItem(dict, key);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004811 // Since key sharing dict doesn't allow deletion, PyDict_DelItem()
4812 // always converts dict to combined form.
4813 if ((cached = CACHED_KEYS(tp)) != NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004814 CACHED_KEYS(tp) = NULL;
INADA Naokia7576492018-11-14 18:39:27 +09004815 dictkeys_decref(cached);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004816 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01004817 }
4818 else {
INADA Naoki2294f3a2017-02-12 13:51:30 +09004819 int was_shared = (cached == ((PyDictObject *)dict)->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004820 res = PyDict_SetItem(dict, key, value);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004821 if (was_shared &&
4822 (cached = CACHED_KEYS(tp)) != NULL &&
4823 cached != ((PyDictObject *)dict)->ma_keys) {
Victor Stinner3d3f2642016-12-15 17:21:23 +01004824 /* PyDict_SetItem() may call dictresize and convert split table
4825 * into combined table. In such case, convert it to split
4826 * table again and update type's shared key only when this is
4827 * the only dict sharing key with the type.
4828 *
4829 * This is to allow using shared key in class like this:
4830 *
4831 * class C:
4832 * def __init__(self):
4833 * # one dict resize happens
4834 * self.a, self.b, self.c = 1, 2, 3
4835 * self.d, self.e, self.f = 4, 5, 6
4836 * a = C()
4837 */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004838 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004839 CACHED_KEYS(tp) = make_keys_shared(dict);
Victor Stinner742da042016-09-07 17:40:12 -07004840 }
4841 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004842 CACHED_KEYS(tp) = NULL;
4843 }
INADA Naokia7576492018-11-14 18:39:27 +09004844 dictkeys_decref(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004845 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
4846 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004847 }
4848 }
4849 } else {
4850 dict = *dictptr;
4851 if (dict == NULL) {
4852 dict = PyDict_New();
4853 if (dict == NULL)
4854 return -1;
4855 *dictptr = dict;
4856 }
4857 if (value == NULL) {
4858 res = PyDict_DelItem(dict, key);
4859 } else {
4860 res = PyDict_SetItem(dict, key, value);
4861 }
4862 }
4863 return res;
4864}
4865
4866void
4867_PyDictKeys_DecRef(PyDictKeysObject *keys)
4868{
INADA Naokia7576492018-11-14 18:39:27 +09004869 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004870}