blob: df92bfd6a9781658de1a2311ec235cd1434f669c [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
42dk_entries is array of PyDictKeyEntry. It's size is USABLE_FRACTION(dk_size).
43DK_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 Stinner621cebe2018-11-12 16:53:38 +0100114#include "pycore_pystate.h"
Eric Snow96c6af92015-05-29 22:21:39 -0600115#include "dict-common.h"
Victor Stinner990397e2016-09-09 20:22:59 -0700116#include "stringlib/eq.h" /* to get unicode_eq() */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000117
Larry Hastings61272b72014-01-07 12:41:53 -0800118/*[clinic input]
Larry Hastingsc2047262014-01-25 20:43:29 -0800119class dict "PyDictObject *" "&PyDict_Type"
Larry Hastings61272b72014-01-07 12:41:53 -0800120[clinic start generated code]*/
Larry Hastings581ee362014-01-28 05:00:08 -0800121/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f157a5a0ce9589d6]*/
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800122
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400123
124/*
125To ensure the lookup algorithm terminates, there must be at least one Unused
126slot (NULL key) in the table.
127To avoid slowing down lookups on a near-full table, we resize the table when
128it's USABLE_FRACTION (currently two-thirds) full.
129*/
Guido van Rossum16e93a81997-01-28 00:00:11 +0000130
Tim Peterseb28ef22001-06-02 05:27:19 +0000131#define PERTURB_SHIFT 5
132
Guido van Rossum16e93a81997-01-28 00:00:11 +0000133/*
Tim Peterseb28ef22001-06-02 05:27:19 +0000134Major subtleties ahead: Most hash schemes depend on having a "good" hash
135function, in the sense of simulating randomness. Python doesn't: its most
R David Murray537ad7a2016-07-10 12:33:18 -0400136important hash functions (for ints) are very regular in common
Tim Peterseb28ef22001-06-02 05:27:19 +0000137cases:
Tim Peters15d49292001-05-27 07:39:22 +0000138
R David Murray537ad7a2016-07-10 12:33:18 -0400139 >>>[hash(i) for i in range(4)]
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000140 [0, 1, 2, 3]
Tim Peters15d49292001-05-27 07:39:22 +0000141
Tim Peterseb28ef22001-06-02 05:27:19 +0000142This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
143the low-order i bits as the initial table index is extremely fast, and there
R David Murray537ad7a2016-07-10 12:33:18 -0400144are no collisions at all for dicts indexed by a contiguous range of ints. So
145this gives better-than-random behavior in common cases, and that's very
146desirable.
Tim Peters15d49292001-05-27 07:39:22 +0000147
Tim Peterseb28ef22001-06-02 05:27:19 +0000148OTOH, when collisions occur, the tendency to fill contiguous slices of the
149hash table makes a good collision resolution strategy crucial. Taking only
150the last i bits of the hash code is also vulnerable: for example, consider
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151the list [i << 16 for i in range(20000)] as a set of keys. Since ints are
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000152their own hash codes, and this fits in a dict of size 2**15, the last 15 bits
153 of every hash code are all 0: they *all* map to the same table index.
Tim Peters15d49292001-05-27 07:39:22 +0000154
Tim Peterseb28ef22001-06-02 05:27:19 +0000155But catering to unusual cases should not slow the usual ones, so we just take
156the last i bits anyway. It's up to collision resolution to do the rest. If
157we *usually* find the key we're looking for on the first try (and, it turns
158out, we usually do -- the table load factor is kept under 2/3, so the odds
159are solidly in our favor), then it makes best sense to keep the initial index
160computation dirt cheap.
Tim Peters15d49292001-05-27 07:39:22 +0000161
Tim Peterseb28ef22001-06-02 05:27:19 +0000162The first half of collision resolution is to visit table indices via this
163recurrence:
Tim Peters15d49292001-05-27 07:39:22 +0000164
Tim Peterseb28ef22001-06-02 05:27:19 +0000165 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +0000166
Tim Peterseb28ef22001-06-02 05:27:19 +0000167For any initial j in range(2**i), repeating that 2**i times generates each
168int in range(2**i) exactly once (see any text on random-number generation for
169proof). By itself, this doesn't help much: like linear probing (setting
170j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
171order. This would be bad, except that's not the only thing we do, and it's
172actually *good* in the common cases where hash keys are consecutive. In an
173example that's really too small to make this entirely clear, for a table of
174size 2**3 the order of indices is:
Tim Peters15d49292001-05-27 07:39:22 +0000175
Tim Peterseb28ef22001-06-02 05:27:19 +0000176 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
177
178If two things come in at index 5, the first place we look after is index 2,
179not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
180Linear probing is deadly in this case because there the fixed probe order
181is the *same* as the order consecutive keys are likely to arrive. But it's
182extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
183and certain that consecutive hash codes do not.
184
185The other half of the strategy is to get the other bits of the hash code
186into play. This is done by initializing a (unsigned) vrbl "perturb" to the
187full hash code, and changing the recurrence to:
188
Tim Peterseb28ef22001-06-02 05:27:19 +0000189 perturb >>= PERTURB_SHIFT;
INADA Naoki267941c2016-10-06 15:19:07 +0900190 j = (5*j) + 1 + perturb;
Tim Peterseb28ef22001-06-02 05:27:19 +0000191 use j % 2**i as the next table index;
192
193Now the probe sequence depends (eventually) on every bit in the hash code,
194and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
195because it quickly magnifies small differences in the bits that didn't affect
196the initial index. Note that because perturb is unsigned, if the recurrence
197is executed often enough perturb eventually becomes and remains 0. At that
198point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
199that's certain to find an empty slot eventually (since it generates every int
200in range(2**i), and we make sure there's always at least one empty slot).
201
202Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
203small so that the high bits of the hash code continue to affect the probe
204sequence across iterations; but you want it large so that in really bad cases
205the high-order hash bits have an effect on early iterations. 5 was "the
206best" in minimizing total collisions across experiments Tim Peters ran (on
207both normal and pathological cases), but 4 and 6 weren't significantly worse.
208
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000209Historical: Reimer Behrends contributed the idea of using a polynomial-based
Tim Peterseb28ef22001-06-02 05:27:19 +0000210approach, using repeated multiplication by x in GF(2**n) where an irreducible
211polynomial for each table size was chosen such that x was a primitive root.
212Christian Tismer later extended that to use division by x instead, as an
213efficient way to get the high bits of the hash code into play. This scheme
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000214also gave excellent collision statistics, but was more expensive: two
215if-tests were required inside the loop; computing "the next" index took about
216the same number of operations but without as much potential parallelism
217(e.g., computing 5*j can go on at the same time as computing 1+perturb in the
218above, and then shifting perturb can be done while the table index is being
219masked); and the PyDictObject struct required a member to hold the table's
220polynomial. In Tim's experiments the current scheme ran faster, produced
221equally good collision statistics, needed less code & used less memory.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000222
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000223*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000224
Fred Drake1bff34a2000-08-31 19:31:38 +0000225/* forward declarations */
Victor Stinner742da042016-09-07 17:40:12 -0700226static Py_ssize_t lookdict(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900227 Py_hash_t hash, PyObject **value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700228static Py_ssize_t lookdict_unicode(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
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400231lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900232 Py_hash_t hash, PyObject **value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700233static Py_ssize_t lookdict_split(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900234 Py_hash_t hash, PyObject **value_addr);
Fred Drake1bff34a2000-08-31 19:31:38 +0000235
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400236static int dictresize(PyDictObject *mp, Py_ssize_t minused);
Tim Petersdea48ec2001-05-22 20:40:22 +0000237
INADA Naoki2aaf98c2018-09-26 12:59:00 +0900238static PyObject* dict_iter(PyDictObject *dict);
239
Benjamin Peterson3c569292016-09-08 13:16:41 -0700240/*Global counter used to set ma_version_tag field of dictionary.
Victor Stinner3b6a6b42016-09-08 12:51:24 -0700241 * It is incremented each time that a dictionary is created and each
242 * time that a dictionary is modified. */
243static uint64_t pydict_global_version = 0;
244
245#define DICT_NEXT_VERSION() (++pydict_global_version)
246
Victor Stinner742da042016-09-07 17:40:12 -0700247/* Dictionary reuse scheme to save calls to malloc and free */
Christian Heimes2202f872008-02-06 14:31:34 +0000248#ifndef PyDict_MAXFREELIST
249#define PyDict_MAXFREELIST 80
250#endif
251static PyDictObject *free_list[PyDict_MAXFREELIST];
252static int numfree = 0;
Victor Stinner742da042016-09-07 17:40:12 -0700253static PyDictKeysObject *keys_free_list[PyDict_MAXFREELIST];
254static int numfreekeys = 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000255
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300256#include "clinic/dictobject.c.h"
257
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100258int
259PyDict_ClearFreeList(void)
Christian Heimes77c02eb2008-02-09 02:18:51 +0000260{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 PyDictObject *op;
Victor Stinner742da042016-09-07 17:40:12 -0700262 int ret = numfree + numfreekeys;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 while (numfree) {
264 op = free_list[--numfree];
265 assert(PyDict_CheckExact(op));
266 PyObject_GC_Del(op);
267 }
Victor Stinner742da042016-09-07 17:40:12 -0700268 while (numfreekeys) {
269 PyObject_FREE(keys_free_list[--numfreekeys]);
270 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100271 return ret;
272}
273
David Malcolm49526f42012-06-22 14:55:41 -0400274/* Print summary info about the state of the optimized allocator */
275void
276_PyDict_DebugMallocStats(FILE *out)
277{
278 _PyDebugAllocatorStats(out,
279 "free PyDictObject", numfree, sizeof(PyDictObject));
280}
281
282
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100283void
284PyDict_Fini(void)
285{
286 PyDict_ClearFreeList();
Christian Heimes77c02eb2008-02-09 02:18:51 +0000287}
288
Victor Stinner742da042016-09-07 17:40:12 -0700289#define DK_SIZE(dk) ((dk)->dk_size)
290#if SIZEOF_VOID_P > 4
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700291#define DK_IXSIZE(dk) \
292 (DK_SIZE(dk) <= 0xff ? \
293 1 : DK_SIZE(dk) <= 0xffff ? \
294 2 : DK_SIZE(dk) <= 0xffffffff ? \
Benjamin Peterson3c569292016-09-08 13:16:41 -0700295 4 : sizeof(int64_t))
Victor Stinner742da042016-09-07 17:40:12 -0700296#else
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700297#define DK_IXSIZE(dk) \
298 (DK_SIZE(dk) <= 0xff ? \
299 1 : DK_SIZE(dk) <= 0xffff ? \
Benjamin Peterson3c569292016-09-08 13:16:41 -0700300 2 : sizeof(int32_t))
Victor Stinner742da042016-09-07 17:40:12 -0700301#endif
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700302#define DK_ENTRIES(dk) \
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700303 ((PyDictKeyEntry*)(&((int8_t*)((dk)->dk_indices))[DK_SIZE(dk) * DK_IXSIZE(dk)]))
Victor Stinner742da042016-09-07 17:40:12 -0700304
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400305#define DK_MASK(dk) (((dk)->dk_size)-1)
306#define IS_POWER_OF_2(x) (((x) & (x-1)) == 0)
307
INADA Naokia7576492018-11-14 18:39:27 +0900308static void free_keys_object(PyDictKeysObject *keys);
309
310static inline void
311dictkeys_incref(PyDictKeysObject *dk)
312{
313 _Py_INC_REFTOTAL;
314 dk->dk_refcnt++;
315}
316
317static inline void
318dictkeys_decref(PyDictKeysObject *dk)
319{
320 assert(dk->dk_refcnt > 0);
321 _Py_DEC_REFTOTAL;
322 if (--dk->dk_refcnt == 0) {
323 free_keys_object(dk);
324 }
325}
326
Victor Stinner742da042016-09-07 17:40:12 -0700327/* lookup indices. returns DKIX_EMPTY, DKIX_DUMMY, or ix >=0 */
Benjamin Peterson73222252016-09-08 09:58:47 -0700328static inline Py_ssize_t
INADA Naokia7576492018-11-14 18:39:27 +0900329dictkeys_get_index(PyDictKeysObject *keys, Py_ssize_t i)
Victor Stinner742da042016-09-07 17:40:12 -0700330{
331 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700332 Py_ssize_t ix;
333
Victor Stinner742da042016-09-07 17:40:12 -0700334 if (s <= 0xff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700335 int8_t *indices = (int8_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700336 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700337 }
338 else if (s <= 0xffff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700339 int16_t *indices = (int16_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700340 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700341 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700342#if SIZEOF_VOID_P > 4
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300343 else if (s > 0xffffffff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700344 int64_t *indices = (int64_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700345 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700346 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700347#endif
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300348 else {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700349 int32_t *indices = (int32_t*)(keys->dk_indices);
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300350 ix = indices[i];
351 }
Victor Stinner71211e32016-09-08 10:52:46 -0700352 assert(ix >= DKIX_DUMMY);
353 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700354}
355
356/* write to indices. */
Benjamin Peterson73222252016-09-08 09:58:47 -0700357static inline void
INADA Naokia7576492018-11-14 18:39:27 +0900358dictkeys_set_index(PyDictKeysObject *keys, Py_ssize_t i, Py_ssize_t ix)
Victor Stinner742da042016-09-07 17:40:12 -0700359{
360 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700361
362 assert(ix >= DKIX_DUMMY);
363
Victor Stinner742da042016-09-07 17:40:12 -0700364 if (s <= 0xff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700365 int8_t *indices = (int8_t*)(keys->dk_indices);
Victor Stinner71211e32016-09-08 10:52:46 -0700366 assert(ix <= 0x7f);
Victor Stinner208857e2016-09-08 11:35:46 -0700367 indices[i] = (char)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700368 }
369 else if (s <= 0xffff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700370 int16_t *indices = (int16_t*)(keys->dk_indices);
Victor Stinner71211e32016-09-08 10:52:46 -0700371 assert(ix <= 0x7fff);
Victor Stinner208857e2016-09-08 11:35:46 -0700372 indices[i] = (int16_t)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700373 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700374#if SIZEOF_VOID_P > 4
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300375 else if (s > 0xffffffff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700376 int64_t *indices = (int64_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700377 indices[i] = ix;
Victor Stinner742da042016-09-07 17:40:12 -0700378 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700379#endif
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300380 else {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700381 int32_t *indices = (int32_t*)(keys->dk_indices);
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300382 assert(ix <= 0x7fffffff);
383 indices[i] = (int32_t)ix;
384 }
Victor Stinner742da042016-09-07 17:40:12 -0700385}
386
387
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200388/* USABLE_FRACTION is the maximum dictionary load.
Victor Stinner742da042016-09-07 17:40:12 -0700389 * Increasing this ratio makes dictionaries more dense resulting in more
390 * collisions. Decreasing it improves sparseness at the expense of spreading
391 * indices over more cache lines and at the cost of total memory consumed.
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200392 *
393 * USABLE_FRACTION must obey the following:
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400394 * (0 < USABLE_FRACTION(n) < n) for all n >= 2
395 *
Victor Stinner742da042016-09-07 17:40:12 -0700396 * USABLE_FRACTION should be quick to calculate.
397 * Fractions around 1/2 to 2/3 seem to work well in practice.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400398 */
Victor Stinner742da042016-09-07 17:40:12 -0700399#define USABLE_FRACTION(n) (((n) << 1)/3)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400400
Victor Stinner742da042016-09-07 17:40:12 -0700401/* ESTIMATE_SIZE is reverse function of USABLE_FRACTION.
402 * This can be used to reserve enough size to insert n entries without
403 * resizing.
404 */
INADA Naoki92c50ee2016-11-22 00:57:02 +0900405#define ESTIMATE_SIZE(n) (((n)*3+1) >> 1)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400406
Victor Stinner742da042016-09-07 17:40:12 -0700407/* Alternative fraction that is otherwise close enough to 2n/3 to make
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400408 * little difference. 8 * 2/3 == 8 * 5/8 == 5. 16 * 2/3 == 16 * 5/8 == 10.
409 * 32 * 2/3 = 21, 32 * 5/8 = 20.
410 * Its advantage is that it is faster to compute on machines with slow division.
411 * #define USABLE_FRACTION(n) (((n) >> 1) + ((n) >> 2) - ((n) >> 3))
Victor Stinner742da042016-09-07 17:40:12 -0700412 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400413
Victor Stinnera9f61a52013-07-16 22:17:26 +0200414/* GROWTH_RATE. Growth rate upon hitting maximum load.
INADA Naoki5fbc5112018-04-17 15:53:34 +0900415 * Currently set to used*3.
Victor Stinnera9f61a52013-07-16 22:17:26 +0200416 * This means that dicts double in size when growing without deletions,
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700417 * but have more head room when the number of deletions is on a par with the
INADA Naoki5fbc5112018-04-17 15:53:34 +0900418 * number of insertions. See also bpo-17563 and bpo-33205.
419 *
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700420 * GROWTH_RATE was set to used*4 up to version 3.2.
421 * GROWTH_RATE was set to used*2 in version 3.3.0
INADA Naoki5fbc5112018-04-17 15:53:34 +0900422 * GROWTH_RATE was set to used*2 + capacity/2 in 3.4.0-3.6.0.
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200423 */
INADA Naoki5fbc5112018-04-17 15:53:34 +0900424#define GROWTH_RATE(d) ((d)->ma_used*3)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400425
426#define ENSURE_ALLOWS_DELETIONS(d) \
427 if ((d)->ma_keys->dk_lookup == lookdict_unicode_nodummy) { \
428 (d)->ma_keys->dk_lookup = lookdict_unicode; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400430
431/* This immutable, empty PyDictKeysObject is used for PyDict_Clear()
432 * (which cannot fail and thus can do no allocation).
433 */
434static PyDictKeysObject empty_keys_struct = {
Serhiy Storchaka97932e42016-09-26 23:01:23 +0300435 1, /* dk_refcnt */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400436 1, /* dk_size */
437 lookdict_split, /* dk_lookup */
438 0, /* dk_usable (immutable) */
Victor Stinner742da042016-09-07 17:40:12 -0700439 0, /* dk_nentries */
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700440 {DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY,
441 DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY}, /* dk_indices */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400442};
443
444static PyObject *empty_values[1] = { NULL };
445
446#define Py_EMPTY_KEYS &empty_keys_struct
447
Victor Stinner611b0fa2016-09-14 15:02:01 +0200448/* Uncomment to check the dict content in _PyDict_CheckConsistency() */
449/* #define DEBUG_PYDICT */
450
451
T. Woutersa00c3fd2017-03-31 09:14:41 -0700452#ifndef NDEBUG
Victor Stinner611b0fa2016-09-14 15:02:01 +0200453static int
454_PyDict_CheckConsistency(PyDictObject *mp)
455{
Victor Stinner50fe3f82018-10-26 18:47:15 +0200456#define ASSERT(expr) _PyObject_ASSERT((PyObject *)mp, (expr))
457
Victor Stinner611b0fa2016-09-14 15:02:01 +0200458 PyDictKeysObject *keys = mp->ma_keys;
459 int splitted = _PyDict_HasSplitTable(mp);
460 Py_ssize_t usable = USABLE_FRACTION(keys->dk_size);
461#ifdef DEBUG_PYDICT
462 PyDictKeyEntry *entries = DK_ENTRIES(keys);
463 Py_ssize_t i;
464#endif
465
Victor Stinner50fe3f82018-10-26 18:47:15 +0200466 ASSERT(0 <= mp->ma_used && mp->ma_used <= usable);
467 ASSERT(IS_POWER_OF_2(keys->dk_size));
468 ASSERT(0 <= keys->dk_usable
Victor Stinner611b0fa2016-09-14 15:02:01 +0200469 && keys->dk_usable <= usable);
Victor Stinner50fe3f82018-10-26 18:47:15 +0200470 ASSERT(0 <= keys->dk_nentries
Victor Stinner611b0fa2016-09-14 15:02:01 +0200471 && keys->dk_nentries <= usable);
Victor Stinner50fe3f82018-10-26 18:47:15 +0200472 ASSERT(keys->dk_usable + keys->dk_nentries <= usable);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200473
474 if (!splitted) {
475 /* combined table */
Victor Stinner50fe3f82018-10-26 18:47:15 +0200476 ASSERT(keys->dk_refcnt == 1);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200477 }
478
479#ifdef DEBUG_PYDICT
480 for (i=0; i < keys->dk_size; i++) {
INADA Naokia7576492018-11-14 18:39:27 +0900481 Py_ssize_t ix = dictkeys_get_index(keys, i);
Victor Stinner50fe3f82018-10-26 18:47:15 +0200482 ASSERT(DKIX_DUMMY <= ix && ix <= usable);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200483 }
484
485 for (i=0; i < usable; i++) {
486 PyDictKeyEntry *entry = &entries[i];
487 PyObject *key = entry->me_key;
488
489 if (key != NULL) {
490 if (PyUnicode_CheckExact(key)) {
491 Py_hash_t hash = ((PyASCIIObject *)key)->hash;
Victor Stinner50fe3f82018-10-26 18:47:15 +0200492 ASSERT(hash != -1);
493 ASSERT(entry->me_hash == hash);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200494 }
495 else {
496 /* test_dict fails if PyObject_Hash() is called again */
Victor Stinner50fe3f82018-10-26 18:47:15 +0200497 ASSERT(entry->me_hash != -1);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200498 }
499 if (!splitted) {
Victor Stinner50fe3f82018-10-26 18:47:15 +0200500 ASSERT(entry->me_value != NULL);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200501 }
502 }
503
504 if (splitted) {
Victor Stinner50fe3f82018-10-26 18:47:15 +0200505 ASSERT(entry->me_value == NULL);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200506 }
507 }
508
509 if (splitted) {
510 /* splitted table */
511 for (i=0; i < mp->ma_used; i++) {
Victor Stinner50fe3f82018-10-26 18:47:15 +0200512 ASSERT(mp->ma_values[i] != NULL);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200513 }
514 }
515#endif
516
517 return 1;
Victor Stinner50fe3f82018-10-26 18:47:15 +0200518
519#undef ASSERT
Victor Stinner611b0fa2016-09-14 15:02:01 +0200520}
521#endif
522
523
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400524static PyDictKeysObject *new_keys_object(Py_ssize_t size)
525{
526 PyDictKeysObject *dk;
Victor Stinner742da042016-09-07 17:40:12 -0700527 Py_ssize_t es, usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400528
Victor Stinner742da042016-09-07 17:40:12 -0700529 assert(size >= PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400530 assert(IS_POWER_OF_2(size));
Victor Stinner742da042016-09-07 17:40:12 -0700531
532 usable = USABLE_FRACTION(size);
533 if (size <= 0xff) {
534 es = 1;
535 }
536 else if (size <= 0xffff) {
537 es = 2;
538 }
539#if SIZEOF_VOID_P > 4
540 else if (size <= 0xffffffff) {
541 es = 4;
542 }
543#endif
544 else {
545 es = sizeof(Py_ssize_t);
546 }
547
548 if (size == PyDict_MINSIZE && numfreekeys > 0) {
549 dk = keys_free_list[--numfreekeys];
550 }
551 else {
Victor Stinner98ee9d52016-09-08 09:33:56 -0700552 dk = PyObject_MALLOC(sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -0700553 + es * size
554 + sizeof(PyDictKeyEntry) * usable);
Victor Stinner742da042016-09-07 17:40:12 -0700555 if (dk == NULL) {
556 PyErr_NoMemory();
557 return NULL;
558 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400559 }
INADA Naokia7576492018-11-14 18:39:27 +0900560 _Py_INC_REFTOTAL;
561 dk->dk_refcnt = 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400562 dk->dk_size = size;
Victor Stinner742da042016-09-07 17:40:12 -0700563 dk->dk_usable = usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400564 dk->dk_lookup = lookdict_unicode_nodummy;
Victor Stinner742da042016-09-07 17:40:12 -0700565 dk->dk_nentries = 0;
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700566 memset(&dk->dk_indices[0], 0xff, es * size);
Victor Stinner742da042016-09-07 17:40:12 -0700567 memset(DK_ENTRIES(dk), 0, sizeof(PyDictKeyEntry) * usable);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400568 return dk;
569}
570
571static void
572free_keys_object(PyDictKeysObject *keys)
573{
Victor Stinner742da042016-09-07 17:40:12 -0700574 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400575 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -0700576 for (i = 0, n = keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400577 Py_XDECREF(entries[i].me_key);
578 Py_XDECREF(entries[i].me_value);
579 }
Victor Stinner742da042016-09-07 17:40:12 -0700580 if (keys->dk_size == PyDict_MINSIZE && numfreekeys < PyDict_MAXFREELIST) {
581 keys_free_list[numfreekeys++] = keys;
582 return;
583 }
Raymond Hettingerce5179f2016-01-31 08:56:21 -0800584 PyObject_FREE(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400585}
586
587#define new_values(size) PyMem_NEW(PyObject *, size)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400588#define free_values(values) PyMem_FREE(values)
589
590/* Consumes a reference to the keys object */
591static PyObject *
592new_dict(PyDictKeysObject *keys, PyObject **values)
593{
594 PyDictObject *mp;
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200595 assert(keys != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 if (numfree) {
597 mp = free_list[--numfree];
598 assert (mp != NULL);
599 assert (Py_TYPE(mp) == &PyDict_Type);
600 _Py_NewReference((PyObject *)mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400602 else {
603 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
604 if (mp == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +0900605 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400606 free_values(values);
607 return NULL;
608 }
609 }
610 mp->ma_keys = keys;
611 mp->ma_values = values;
612 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -0700613 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +0200614 assert(_PyDict_CheckConsistency(mp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000616}
617
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400618/* Consumes a reference to the keys object */
619static PyObject *
620new_dict_with_shared_keys(PyDictKeysObject *keys)
621{
622 PyObject **values;
623 Py_ssize_t i, size;
624
Victor Stinner742da042016-09-07 17:40:12 -0700625 size = USABLE_FRACTION(DK_SIZE(keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400626 values = new_values(size);
627 if (values == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +0900628 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400629 return PyErr_NoMemory();
630 }
631 for (i = 0; i < size; i++) {
632 values[i] = NULL;
633 }
634 return new_dict(keys, values);
635}
636
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500637
638static PyObject *
639clone_combined_dict(PyDictObject *orig)
640{
641 assert(PyDict_CheckExact(orig));
642 assert(orig->ma_values == NULL);
643 assert(orig->ma_keys->dk_refcnt == 1);
644
645 Py_ssize_t keys_size = _PyDict_KeysSize(orig->ma_keys);
646 PyDictKeysObject *keys = PyObject_Malloc(keys_size);
647 if (keys == NULL) {
648 PyErr_NoMemory();
649 return NULL;
650 }
651
652 memcpy(keys, orig->ma_keys, keys_size);
653
654 /* After copying key/value pairs, we need to incref all
655 keys and values and they are about to be co-owned by a
656 new dict object. */
657 PyDictKeyEntry *ep0 = DK_ENTRIES(keys);
658 Py_ssize_t n = keys->dk_nentries;
659 for (Py_ssize_t i = 0; i < n; i++) {
660 PyDictKeyEntry *entry = &ep0[i];
661 PyObject *value = entry->me_value;
662 if (value != NULL) {
663 Py_INCREF(value);
664 Py_INCREF(entry->me_key);
665 }
666 }
667
668 PyDictObject *new = (PyDictObject *)new_dict(keys, NULL);
669 if (new == NULL) {
670 /* In case of an error, `new_dict()` takes care of
671 cleaning up `keys`. */
672 return NULL;
673 }
674 new->ma_used = orig->ma_used;
675 assert(_PyDict_CheckConsistency(new));
676 if (_PyObject_GC_IS_TRACKED(orig)) {
677 /* Maintain tracking. */
678 _PyObject_GC_TRACK(new);
679 }
Yury Selivanov0b752282018-07-06 12:20:07 -0400680
681 /* Since we copied the keys table we now have an extra reference
682 in the system. Manually call _Py_INC_REFTOTAL to signal that
INADA Naokia7576492018-11-14 18:39:27 +0900683 we have it now; calling dictkeys_incref would be an error as
Yury Selivanov0b752282018-07-06 12:20:07 -0400684 keys->dk_refcnt is already set to 1 (after memcpy). */
685 _Py_INC_REFTOTAL;
686
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500687 return (PyObject *)new;
688}
689
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400690PyObject *
691PyDict_New(void)
692{
Victor Stinner742da042016-09-07 17:40:12 -0700693 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200694 if (keys == NULL)
695 return NULL;
696 return new_dict(keys, NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400697}
698
Victor Stinner742da042016-09-07 17:40:12 -0700699/* Search index of hash table from offset of entry table */
700static Py_ssize_t
701lookdict_index(PyDictKeysObject *k, Py_hash_t hash, Py_ssize_t index)
702{
Victor Stinner742da042016-09-07 17:40:12 -0700703 size_t mask = DK_MASK(k);
INADA Naoki073ae482017-06-23 15:22:50 +0900704 size_t perturb = (size_t)hash;
705 size_t i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700706
INADA Naoki073ae482017-06-23 15:22:50 +0900707 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900708 Py_ssize_t ix = dictkeys_get_index(k, i);
Victor Stinner742da042016-09-07 17:40:12 -0700709 if (ix == index) {
710 return i;
711 }
712 if (ix == DKIX_EMPTY) {
713 return DKIX_EMPTY;
714 }
INADA Naoki073ae482017-06-23 15:22:50 +0900715 perturb >>= PERTURB_SHIFT;
716 i = mask & (i*5 + perturb + 1);
Victor Stinner742da042016-09-07 17:40:12 -0700717 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700718 Py_UNREACHABLE();
Victor Stinner742da042016-09-07 17:40:12 -0700719}
720
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000721/*
722The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000723This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000724Open addressing is preferred over chaining since the link overhead for
725chaining would be substantial (100% with typical malloc overhead).
726
Tim Peterseb28ef22001-06-02 05:27:19 +0000727The initial probe index is computed as hash mod the table size. Subsequent
728probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000729
730All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000731
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000732The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000733contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000734Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000735
Victor Stinner742da042016-09-07 17:40:12 -0700736lookdict() is general-purpose, and may return DKIX_ERROR if (and only if) a
Victor Stinnera4348cc2016-09-08 12:01:25 -0700737comparison raises an exception.
Guido van Rossum89d8c602007-09-18 17:26:56 +0000738lookdict_unicode() below is specialized to string keys, comparison of which can
INADA Naoki1b8df102017-02-20 22:48:10 +0900739never raise an exception; that function can never return DKIX_ERROR when key
740is string. Otherwise, it falls back to lookdict().
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400741lookdict_unicode_nodummy is further specialized for string keys that cannot be
742the <dummy> value.
INADA Naoki778928b2017-08-03 23:45:15 +0900743For both, when the key isn't found a DKIX_EMPTY is returned.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000744*/
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100745static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400746lookdict(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900747 Py_hash_t hash, PyObject **value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000748{
INADA Naoki778928b2017-08-03 23:45:15 +0900749 size_t i, mask, perturb;
Victor Stinner742da042016-09-07 17:40:12 -0700750 PyDictKeysObject *dk;
INADA Naoki778928b2017-08-03 23:45:15 +0900751 PyDictKeyEntry *ep0;
Tim Peterseb28ef22001-06-02 05:27:19 +0000752
Antoine Pitrou9a234902012-05-13 20:48:01 +0200753top:
Victor Stinner742da042016-09-07 17:40:12 -0700754 dk = mp->ma_keys;
Victor Stinner742da042016-09-07 17:40:12 -0700755 ep0 = DK_ENTRIES(dk);
INADA Naoki778928b2017-08-03 23:45:15 +0900756 mask = DK_MASK(dk);
757 perturb = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700759
INADA Naoki778928b2017-08-03 23:45:15 +0900760 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900761 Py_ssize_t ix = dictkeys_get_index(dk, i);
Victor Stinner742da042016-09-07 17:40:12 -0700762 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700763 *value_addr = NULL;
764 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400765 }
INADA Naoki778928b2017-08-03 23:45:15 +0900766 if (ix >= 0) {
767 PyDictKeyEntry *ep = &ep0[ix];
768 assert(ep->me_key != NULL);
769 if (ep->me_key == key) {
770 *value_addr = ep->me_value;
771 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700772 }
INADA Naoki778928b2017-08-03 23:45:15 +0900773 if (ep->me_hash == hash) {
774 PyObject *startkey = ep->me_key;
775 Py_INCREF(startkey);
776 int cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
777 Py_DECREF(startkey);
778 if (cmp < 0) {
779 *value_addr = NULL;
780 return DKIX_ERROR;
781 }
782 if (dk == mp->ma_keys && ep->me_key == startkey) {
783 if (cmp > 0) {
784 *value_addr = ep->me_value;
785 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700786 }
INADA Naoki778928b2017-08-03 23:45:15 +0900787 }
788 else {
789 /* The dict was mutated, restart */
790 goto top;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400791 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 }
INADA Naoki778928b2017-08-03 23:45:15 +0900794 perturb >>= PERTURB_SHIFT;
795 i = (i*5 + perturb + 1) & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700797 Py_UNREACHABLE();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000798}
799
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400800/* Specialized version for string-only keys */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100801static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400802lookdict_unicode(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900803 Py_hash_t hash, PyObject **value_addr)
Fred Drake1bff34a2000-08-31 19:31:38 +0000804{
Victor Stinner742da042016-09-07 17:40:12 -0700805 assert(mp->ma_values == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 /* Make sure this function doesn't have to handle non-unicode keys,
807 including subclasses of str; e.g., one reason to subclass
808 unicodes is to override __eq__, and for speed we don't cater to
809 that here. */
810 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400811 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900812 return lookdict(mp, key, hash, value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 }
Tim Peters15d49292001-05-27 07:39:22 +0000814
INADA Naoki778928b2017-08-03 23:45:15 +0900815 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
816 size_t mask = DK_MASK(mp->ma_keys);
817 size_t perturb = (size_t)hash;
818 size_t i = (size_t)hash & mask;
819
820 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900821 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
Victor Stinner742da042016-09-07 17:40:12 -0700822 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700823 *value_addr = NULL;
824 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400825 }
INADA Naoki778928b2017-08-03 23:45:15 +0900826 if (ix >= 0) {
827 PyDictKeyEntry *ep = &ep0[ix];
828 assert(ep->me_key != NULL);
829 assert(PyUnicode_CheckExact(ep->me_key));
830 if (ep->me_key == key ||
831 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
832 *value_addr = ep->me_value;
833 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700834 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400835 }
INADA Naoki778928b2017-08-03 23:45:15 +0900836 perturb >>= PERTURB_SHIFT;
837 i = mask & (i*5 + perturb + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700839 Py_UNREACHABLE();
Fred Drake1bff34a2000-08-31 19:31:38 +0000840}
841
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400842/* Faster version of lookdict_unicode when it is known that no <dummy> keys
843 * will be present. */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100844static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400845lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900846 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400847{
Victor Stinner742da042016-09-07 17:40:12 -0700848 assert(mp->ma_values == NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400849 /* Make sure this function doesn't have to handle non-unicode keys,
850 including subclasses of str; e.g., one reason to subclass
851 unicodes is to override __eq__, and for speed we don't cater to
852 that here. */
853 if (!PyUnicode_CheckExact(key)) {
854 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900855 return lookdict(mp, key, hash, value_addr);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400856 }
INADA Naoki778928b2017-08-03 23:45:15 +0900857
858 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
859 size_t mask = DK_MASK(mp->ma_keys);
860 size_t perturb = (size_t)hash;
861 size_t i = (size_t)hash & mask;
862
863 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900864 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
Victor Stinner742da042016-09-07 17:40:12 -0700865 assert (ix != DKIX_DUMMY);
866 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700867 *value_addr = NULL;
868 return DKIX_EMPTY;
869 }
INADA Naoki778928b2017-08-03 23:45:15 +0900870 PyDictKeyEntry *ep = &ep0[ix];
871 assert(ep->me_key != NULL);
872 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700873 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400874 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900875 *value_addr = ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700876 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400877 }
INADA Naoki778928b2017-08-03 23:45:15 +0900878 perturb >>= PERTURB_SHIFT;
879 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400880 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700881 Py_UNREACHABLE();
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400882}
883
884/* Version of lookdict for split tables.
885 * All split tables and only split tables use this lookup function.
886 * Split tables only contain unicode keys and no dummy keys,
887 * so algorithm is the same as lookdict_unicode_nodummy.
888 */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100889static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400890lookdict_split(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900891 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400892{
Victor Stinner742da042016-09-07 17:40:12 -0700893 /* mp must split table */
894 assert(mp->ma_values != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400895 if (!PyUnicode_CheckExact(key)) {
INADA Naoki778928b2017-08-03 23:45:15 +0900896 Py_ssize_t ix = lookdict(mp, key, hash, value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700897 if (ix >= 0) {
INADA Naokiba609772016-12-07 20:41:42 +0900898 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700899 }
900 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400901 }
Victor Stinner742da042016-09-07 17:40:12 -0700902
INADA Naoki778928b2017-08-03 23:45:15 +0900903 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
904 size_t mask = DK_MASK(mp->ma_keys);
905 size_t perturb = (size_t)hash;
906 size_t i = (size_t)hash & mask;
907
908 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900909 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
INADA Naoki778928b2017-08-03 23:45:15 +0900910 assert (ix != DKIX_DUMMY);
Victor Stinner742da042016-09-07 17:40:12 -0700911 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700912 *value_addr = NULL;
913 return DKIX_EMPTY;
914 }
INADA Naoki778928b2017-08-03 23:45:15 +0900915 PyDictKeyEntry *ep = &ep0[ix];
916 assert(ep->me_key != NULL);
917 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700918 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400919 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900920 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700921 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400922 }
INADA Naoki778928b2017-08-03 23:45:15 +0900923 perturb >>= PERTURB_SHIFT;
924 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400925 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700926 Py_UNREACHABLE();
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400927}
928
Benjamin Petersonfb886362010-04-24 18:21:17 +0000929int
930_PyDict_HasOnlyStringKeys(PyObject *dict)
931{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 Py_ssize_t pos = 0;
933 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000934 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400936 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 return 1;
938 while (PyDict_Next(dict, &pos, &key, &value))
939 if (!PyUnicode_Check(key))
940 return 0;
941 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000942}
943
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000944#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 do { \
946 if (!_PyObject_GC_IS_TRACKED(mp)) { \
947 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
948 _PyObject_GC_MAY_BE_TRACKED(value)) { \
949 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 } \
951 } \
952 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000953
954void
955_PyDict_MaybeUntrack(PyObject *op)
956{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 PyDictObject *mp;
958 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -0700959 Py_ssize_t i, numentries;
960 PyDictKeyEntry *ep0;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
963 return;
964
965 mp = (PyDictObject *) op;
Victor Stinner742da042016-09-07 17:40:12 -0700966 ep0 = DK_ENTRIES(mp->ma_keys);
967 numentries = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400968 if (_PyDict_HasSplitTable(mp)) {
Victor Stinner742da042016-09-07 17:40:12 -0700969 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400970 if ((value = mp->ma_values[i]) == NULL)
971 continue;
972 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
Victor Stinner742da042016-09-07 17:40:12 -0700973 assert(!_PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400974 return;
975 }
976 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400978 else {
Victor Stinner742da042016-09-07 17:40:12 -0700979 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400980 if ((value = ep0[i].me_value) == NULL)
981 continue;
982 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
983 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
984 return;
985 }
986 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000988}
989
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400990/* Internal function to find slot for an item from its hash
Victor Stinner3c336c52016-09-12 14:17:40 +0200991 when it is known that the key is not present in the dict.
992
993 The dict must be combined. */
INADA Naokiba609772016-12-07 20:41:42 +0900994static Py_ssize_t
INADA Naoki778928b2017-08-03 23:45:15 +0900995find_empty_slot(PyDictKeysObject *keys, Py_hash_t hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000996{
INADA Naoki778928b2017-08-03 23:45:15 +0900997 assert(keys != NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000998
INADA Naoki778928b2017-08-03 23:45:15 +0900999 const size_t mask = DK_MASK(keys);
1000 size_t i = hash & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001001 Py_ssize_t ix = dictkeys_get_index(keys, i);
INADA Naoki778928b2017-08-03 23:45:15 +09001002 for (size_t perturb = hash; ix >= 0;) {
INADA Naoki267941c2016-10-06 15:19:07 +09001003 perturb >>= PERTURB_SHIFT;
INADA Naoki778928b2017-08-03 23:45:15 +09001004 i = (i*5 + perturb + 1) & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001005 ix = dictkeys_get_index(keys, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 }
INADA Naoki778928b2017-08-03 23:45:15 +09001007 return i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001008}
1009
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001010static int
1011insertion_resize(PyDictObject *mp)
1012{
Raymond Hettinger36f74aa2013-05-17 03:01:13 -07001013 return dictresize(mp, GROWTH_RATE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001014}
Antoine Pitroue965d972012-02-27 00:45:12 +01001015
1016/*
1017Internal routine to insert a new item into the table.
1018Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +01001019Returns -1 if an error occurred, or 0 on success.
1020*/
1021static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001022insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001023{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001024 PyObject *old_value;
INADA Naokiba609772016-12-07 20:41:42 +09001025 PyDictKeyEntry *ep;
Antoine Pitroue965d972012-02-27 00:45:12 +01001026
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001027 Py_INCREF(key);
1028 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001029 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
1030 if (insertion_resize(mp) < 0)
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001031 goto Fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001032 }
1033
INADA Naoki778928b2017-08-03 23:45:15 +09001034 Py_ssize_t ix = mp->ma_keys->dk_lookup(mp, key, hash, &old_value);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001035 if (ix == DKIX_ERROR)
1036 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001037
Antoine Pitroud6967322014-10-18 00:35:00 +02001038 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001039 MAINTAIN_TRACKING(mp, key, value);
Victor Stinner742da042016-09-07 17:40:12 -07001040
1041 /* When insertion order is different from shared key, we can't share
1042 * the key anymore. Convert this instance to combine table.
1043 */
1044 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09001045 ((ix >= 0 && old_value == NULL && mp->ma_used != ix) ||
Victor Stinner742da042016-09-07 17:40:12 -07001046 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001047 if (insertion_resize(mp) < 0)
1048 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001049 ix = DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001050 }
Victor Stinner742da042016-09-07 17:40:12 -07001051
1052 if (ix == DKIX_EMPTY) {
1053 /* Insert into new slot. */
INADA Naokiba609772016-12-07 20:41:42 +09001054 assert(old_value == NULL);
Victor Stinner742da042016-09-07 17:40:12 -07001055 if (mp->ma_keys->dk_usable <= 0) {
1056 /* Need to resize. */
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001057 if (insertion_resize(mp) < 0)
1058 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001059 }
INADA Naoki778928b2017-08-03 23:45:15 +09001060 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naokiba609772016-12-07 20:41:42 +09001061 ep = &DK_ENTRIES(mp->ma_keys)[mp->ma_keys->dk_nentries];
INADA Naokia7576492018-11-14 18:39:27 +09001062 dictkeys_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Victor Stinner742da042016-09-07 17:40:12 -07001063 ep->me_key = key;
1064 ep->me_hash = hash;
1065 if (mp->ma_values) {
1066 assert (mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
1067 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001068 }
1069 else {
Victor Stinner742da042016-09-07 17:40:12 -07001070 ep->me_value = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001071 }
1072 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001073 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07001074 mp->ma_keys->dk_usable--;
1075 mp->ma_keys->dk_nentries++;
1076 assert(mp->ma_keys->dk_usable >= 0);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001077 assert(_PyDict_CheckConsistency(mp));
Victor Stinner742da042016-09-07 17:40:12 -07001078 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001079 }
Victor Stinner742da042016-09-07 17:40:12 -07001080
INADA Naokiba609772016-12-07 20:41:42 +09001081 if (_PyDict_HasSplitTable(mp)) {
1082 mp->ma_values[ix] = value;
1083 if (old_value == NULL) {
1084 /* pending state */
1085 assert(ix == mp->ma_used);
1086 mp->ma_used++;
1087 }
1088 }
1089 else {
1090 assert(old_value != NULL);
1091 DK_ENTRIES(mp->ma_keys)[ix].me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07001092 }
1093
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001094 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokiba609772016-12-07 20:41:42 +09001095 Py_XDECREF(old_value); /* which **CAN** re-enter (see issue #22653) */
Victor Stinner611b0fa2016-09-14 15:02:01 +02001096 assert(_PyDict_CheckConsistency(mp));
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001097 Py_DECREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001098 return 0;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001099
1100Fail:
1101 Py_DECREF(value);
1102 Py_DECREF(key);
1103 return -1;
Antoine Pitroue965d972012-02-27 00:45:12 +01001104}
1105
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001106/*
luzpaza5293b42017-11-05 07:37:50 -06001107Internal routine used by dictresize() to build a hashtable of entries.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001108*/
1109static void
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001110build_indices(PyDictKeysObject *keys, PyDictKeyEntry *ep, Py_ssize_t n)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001111{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001112 size_t mask = (size_t)DK_SIZE(keys) - 1;
1113 for (Py_ssize_t ix = 0; ix != n; ix++, ep++) {
1114 Py_hash_t hash = ep->me_hash;
1115 size_t i = hash & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001116 for (size_t perturb = hash; dictkeys_get_index(keys, i) != DKIX_EMPTY;) {
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001117 perturb >>= PERTURB_SHIFT;
INADA Naoki870c2862017-06-24 09:03:19 +09001118 i = mask & (i*5 + perturb + 1);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001119 }
INADA Naokia7576492018-11-14 18:39:27 +09001120 dictkeys_set_index(keys, i, ix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001122}
1123
1124/*
1125Restructure the table by allocating a new table and reinserting all
1126items again. When entries have been deleted, the new table may
1127actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001128If a table is split (its keys and hashes are shared, its values are not),
1129then the values are temporarily copied into the table, it is resized as
1130a combined table, then the me_value slots in the old table are NULLed out.
1131After resizing a table is always combined,
1132but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001133*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001134static int
Victor Stinner3d3f2642016-12-15 17:21:23 +01001135dictresize(PyDictObject *mp, Py_ssize_t minsize)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001136{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001137 Py_ssize_t newsize, numentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001138 PyDictKeysObject *oldkeys;
1139 PyObject **oldvalues;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001140 PyDictKeyEntry *oldentries, *newentries;
Tim Peters91a364d2001-05-19 07:04:38 +00001141
Victor Stinner742da042016-09-07 17:40:12 -07001142 /* Find the smallest table size > minused. */
1143 for (newsize = PyDict_MINSIZE;
Victor Stinner3d3f2642016-12-15 17:21:23 +01001144 newsize < minsize && newsize > 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 newsize <<= 1)
1146 ;
1147 if (newsize <= 0) {
1148 PyErr_NoMemory();
1149 return -1;
1150 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001151
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001152 oldkeys = mp->ma_keys;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001153
1154 /* NOTE: Current odict checks mp->ma_keys to detect resize happen.
1155 * So we can't reuse oldkeys even if oldkeys->dk_size == newsize.
1156 * TODO: Try reusing oldkeys when reimplement odict.
1157 */
1158
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001159 /* Allocate a new table. */
1160 mp->ma_keys = new_keys_object(newsize);
1161 if (mp->ma_keys == NULL) {
1162 mp->ma_keys = oldkeys;
1163 return -1;
1164 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01001165 // New table must be large enough.
1166 assert(mp->ma_keys->dk_usable >= mp->ma_used);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001167 if (oldkeys->dk_lookup == lookdict)
1168 mp->ma_keys->dk_lookup = lookdict;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001169
1170 numentries = mp->ma_used;
1171 oldentries = DK_ENTRIES(oldkeys);
1172 newentries = DK_ENTRIES(mp->ma_keys);
1173 oldvalues = mp->ma_values;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001174 if (oldvalues != NULL) {
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001175 /* Convert split table into new combined table.
1176 * We must incref keys; we can transfer values.
1177 * Note that values of split table is always dense.
1178 */
1179 for (Py_ssize_t i = 0; i < numentries; i++) {
1180 assert(oldvalues[i] != NULL);
1181 PyDictKeyEntry *ep = &oldentries[i];
1182 PyObject *key = ep->me_key;
1183 Py_INCREF(key);
1184 newentries[i].me_key = key;
1185 newentries[i].me_hash = ep->me_hash;
1186 newentries[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001188
INADA Naokia7576492018-11-14 18:39:27 +09001189 dictkeys_decref(oldkeys);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001190 mp->ma_values = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001191 if (oldvalues != empty_values) {
1192 free_values(oldvalues);
1193 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001194 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001195 else { // combined table.
1196 if (oldkeys->dk_nentries == numentries) {
1197 memcpy(newentries, oldentries, numentries * sizeof(PyDictKeyEntry));
1198 }
1199 else {
1200 PyDictKeyEntry *ep = oldentries;
1201 for (Py_ssize_t i = 0; i < numentries; i++) {
1202 while (ep->me_value == NULL)
1203 ep++;
1204 newentries[i] = *ep++;
1205 }
1206 }
1207
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001208 assert(oldkeys->dk_lookup != lookdict_split);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001209 assert(oldkeys->dk_refcnt == 1);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001210 if (oldkeys->dk_size == PyDict_MINSIZE &&
1211 numfreekeys < PyDict_MAXFREELIST) {
INADA Naokia7576492018-11-14 18:39:27 +09001212 _Py_DEC_REFTOTAL;
1213 keys_free_list[numfreekeys++] = oldkeys;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001214 }
1215 else {
INADA Naokia7576492018-11-14 18:39:27 +09001216 _Py_DEC_REFTOTAL;
1217 PyObject_FREE(oldkeys);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001218 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001219 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001220
1221 build_indices(mp->ma_keys, newentries, numentries);
1222 mp->ma_keys->dk_usable -= numentries;
1223 mp->ma_keys->dk_nentries = numentries;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001225}
1226
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001227/* Returns NULL if unable to split table.
1228 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001229static PyDictKeysObject *
1230make_keys_shared(PyObject *op)
1231{
1232 Py_ssize_t i;
1233 Py_ssize_t size;
1234 PyDictObject *mp = (PyDictObject *)op;
1235
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001236 if (!PyDict_CheckExact(op))
1237 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001238 if (!_PyDict_HasSplitTable(mp)) {
1239 PyDictKeyEntry *ep0;
1240 PyObject **values;
1241 assert(mp->ma_keys->dk_refcnt == 1);
1242 if (mp->ma_keys->dk_lookup == lookdict) {
1243 return NULL;
1244 }
1245 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
1246 /* Remove dummy keys */
1247 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
1248 return NULL;
1249 }
1250 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
1251 /* Copy values into a new array */
Victor Stinner742da042016-09-07 17:40:12 -07001252 ep0 = DK_ENTRIES(mp->ma_keys);
1253 size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001254 values = new_values(size);
1255 if (values == NULL) {
1256 PyErr_SetString(PyExc_MemoryError,
1257 "Not enough memory to allocate new values array");
1258 return NULL;
1259 }
1260 for (i = 0; i < size; i++) {
1261 values[i] = ep0[i].me_value;
1262 ep0[i].me_value = NULL;
1263 }
1264 mp->ma_keys->dk_lookup = lookdict_split;
1265 mp->ma_values = values;
1266 }
INADA Naokia7576492018-11-14 18:39:27 +09001267 dictkeys_incref(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001268 return mp->ma_keys;
1269}
Christian Heimes99170a52007-12-19 02:07:34 +00001270
1271PyObject *
1272_PyDict_NewPresized(Py_ssize_t minused)
1273{
INADA Naoki92c50ee2016-11-22 00:57:02 +09001274 const Py_ssize_t max_presize = 128 * 1024;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001275 Py_ssize_t newsize;
1276 PyDictKeysObject *new_keys;
INADA Naoki92c50ee2016-11-22 00:57:02 +09001277
1278 /* There are no strict guarantee that returned dict can contain minused
1279 * items without resize. So we create medium size dict instead of very
1280 * large dict or MemoryError.
1281 */
1282 if (minused > USABLE_FRACTION(max_presize)) {
1283 newsize = max_presize;
1284 }
1285 else {
1286 Py_ssize_t minsize = ESTIMATE_SIZE(minused);
1287 newsize = PyDict_MINSIZE;
1288 while (newsize < minsize) {
1289 newsize <<= 1;
1290 }
1291 }
1292 assert(IS_POWER_OF_2(newsize));
1293
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001294 new_keys = new_keys_object(newsize);
1295 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001297 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001298}
1299
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001300/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1301 * that may occur (originally dicts supported only string keys, and exceptions
1302 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001303 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001304 * (suppressed) occurred while computing the key's hash, or that some error
1305 * (suppressed) occurred when comparing keys in the dict's internal probe
1306 * sequence. A nasty example of the latter is when a Python-coded comparison
1307 * function hits a stack-depth error, which can cause this to return NULL
1308 * even if the key is present.
1309 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001310PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001311PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001312{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001313 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07001314 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 PyDictObject *mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 PyThreadState *tstate;
INADA Naokiba609772016-12-07 20:41:42 +09001317 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 if (!PyDict_Check(op))
1320 return NULL;
1321 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001322 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 {
1324 hash = PyObject_Hash(key);
1325 if (hash == -1) {
1326 PyErr_Clear();
1327 return NULL;
1328 }
1329 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 /* We can arrive here with a NULL tstate during initialization: try
1332 running "python -Wi" for an example related to string interning.
1333 Let's just hope that no exception occurs then... This must be
Victor Stinner50b48572018-11-01 01:51:40 +01001334 _PyThreadState_GET() and not PyThreadState_Get() because the latter
Victor Stinner9204fb82018-10-30 15:13:17 +01001335 abort Python if tstate is NULL. */
Victor Stinner50b48572018-11-01 01:51:40 +01001336 tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 if (tstate != NULL && tstate->curexc_type != NULL) {
1338 /* preserve the existing exception */
1339 PyObject *err_type, *err_value, *err_tb;
1340 PyErr_Fetch(&err_type, &err_value, &err_tb);
INADA Naoki778928b2017-08-03 23:45:15 +09001341 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 /* ignore errors */
1343 PyErr_Restore(err_type, err_value, err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001344 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 return NULL;
1346 }
1347 else {
INADA Naoki778928b2017-08-03 23:45:15 +09001348 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001349 if (ix < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 PyErr_Clear();
1351 return NULL;
1352 }
1353 }
INADA Naokiba609772016-12-07 20:41:42 +09001354 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001355}
1356
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001357/* Same as PyDict_GetItemWithError() but with hash supplied by caller.
1358 This returns NULL *with* an exception set if an exception occurred.
1359 It returns NULL *without* an exception set if the key wasn't present.
1360*/
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001361PyObject *
1362_PyDict_GetItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1363{
Victor Stinner742da042016-09-07 17:40:12 -07001364 Py_ssize_t ix;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001365 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001366 PyObject *value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001367
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001368 if (!PyDict_Check(op)) {
1369 PyErr_BadInternalCall();
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001370 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001371 }
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001372
INADA Naoki778928b2017-08-03 23:45:15 +09001373 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001374 if (ix < 0) {
1375 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001376 }
INADA Naokiba609772016-12-07 20:41:42 +09001377 return value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001378}
1379
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001380/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1381 This returns NULL *with* an exception set if an exception occurred.
1382 It returns NULL *without* an exception set if the key wasn't present.
1383*/
1384PyObject *
1385PyDict_GetItemWithError(PyObject *op, PyObject *key)
1386{
Victor Stinner742da042016-09-07 17:40:12 -07001387 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001388 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 PyDictObject*mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001390 PyObject *value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 if (!PyDict_Check(op)) {
1393 PyErr_BadInternalCall();
1394 return NULL;
1395 }
1396 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001397 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 {
1399 hash = PyObject_Hash(key);
1400 if (hash == -1) {
1401 return NULL;
1402 }
1403 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001404
INADA Naoki778928b2017-08-03 23:45:15 +09001405 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001406 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001408 return value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001409}
1410
Brett Cannonfd074152012-04-14 14:10:13 -04001411PyObject *
1412_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1413{
1414 PyObject *kv;
1415 kv = _PyUnicode_FromId(key); /* borrowed */
1416 if (kv == NULL)
1417 return NULL;
1418 return PyDict_GetItemWithError(dp, kv);
1419}
1420
Victor Stinnerb4efc962015-11-20 09:24:02 +01001421/* Fast version of global value lookup (LOAD_GLOBAL).
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001422 * Lookup in globals, then builtins.
Victor Stinnerb4efc962015-11-20 09:24:02 +01001423 *
1424 * Raise an exception and return NULL if an error occurred (ex: computing the
1425 * key hash failed, key comparison failed, ...). Return NULL if the key doesn't
1426 * exist. Return the value if the key exists.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001427 */
1428PyObject *
1429_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001430{
Victor Stinner742da042016-09-07 17:40:12 -07001431 Py_ssize_t ix;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001432 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09001433 PyObject *value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001434
1435 if (!PyUnicode_CheckExact(key) ||
1436 (hash = ((PyASCIIObject *) key)->hash) == -1)
1437 {
1438 hash = PyObject_Hash(key);
1439 if (hash == -1)
1440 return NULL;
Antoine Pitroue965d972012-02-27 00:45:12 +01001441 }
Victor Stinnerb4efc962015-11-20 09:24:02 +01001442
1443 /* namespace 1: globals */
INADA Naoki778928b2017-08-03 23:45:15 +09001444 ix = globals->ma_keys->dk_lookup(globals, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001445 if (ix == DKIX_ERROR)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001446 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001447 if (ix != DKIX_EMPTY && value != NULL)
1448 return value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001449
1450 /* namespace 2: builtins */
INADA Naoki778928b2017-08-03 23:45:15 +09001451 ix = builtins->ma_keys->dk_lookup(builtins, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001452 if (ix < 0)
Victor Stinnerb4efc962015-11-20 09:24:02 +01001453 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001454 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001455}
1456
Antoine Pitroue965d972012-02-27 00:45:12 +01001457/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1458 * dictionary if it's merely replacing the value for an existing key.
1459 * This means that it's safe to loop over a dictionary with PyDict_Next()
1460 * and occasionally replace a value -- but you can't insert new keys or
1461 * remove them.
1462 */
1463int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001464PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001465{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001466 PyDictObject *mp;
1467 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001468 if (!PyDict_Check(op)) {
1469 PyErr_BadInternalCall();
1470 return -1;
1471 }
1472 assert(key);
1473 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001474 mp = (PyDictObject *)op;
1475 if (!PyUnicode_CheckExact(key) ||
1476 (hash = ((PyASCIIObject *) key)->hash) == -1)
1477 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001478 hash = PyObject_Hash(key);
1479 if (hash == -1)
1480 return -1;
1481 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001482
1483 /* insertdict() handles any resizing that might be necessary */
1484 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001485}
1486
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001487int
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001488_PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
1489 Py_hash_t hash)
1490{
1491 PyDictObject *mp;
1492
1493 if (!PyDict_Check(op)) {
1494 PyErr_BadInternalCall();
1495 return -1;
1496 }
1497 assert(key);
1498 assert(value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001499 assert(hash != -1);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001500 mp = (PyDictObject *)op;
1501
1502 /* insertdict() handles any resizing that might be necessary */
1503 return insertdict(mp, key, hash, value);
1504}
1505
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001506static int
INADA Naoki778928b2017-08-03 23:45:15 +09001507delitem_common(PyDictObject *mp, Py_hash_t hash, Py_ssize_t ix,
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001508 PyObject *old_value)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001509{
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001510 PyObject *old_key;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001511 PyDictKeyEntry *ep;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001512
INADA Naoki778928b2017-08-03 23:45:15 +09001513 Py_ssize_t hashpos = lookdict_index(mp->ma_keys, hash, ix);
1514 assert(hashpos >= 0);
1515
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001516 mp->ma_used--;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001517 mp->ma_version_tag = DICT_NEXT_VERSION();
1518 ep = &DK_ENTRIES(mp->ma_keys)[ix];
INADA Naokia7576492018-11-14 18:39:27 +09001519 dictkeys_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001520 ENSURE_ALLOWS_DELETIONS(mp);
1521 old_key = ep->me_key;
1522 ep->me_key = NULL;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001523 ep->me_value = NULL;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001524 Py_DECREF(old_key);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001525 Py_DECREF(old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001526
1527 assert(_PyDict_CheckConsistency(mp));
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001528 return 0;
1529}
1530
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001531int
Tim Peters1f5871e2000-07-04 17:44:48 +00001532PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001533{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001534 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535 assert(key);
1536 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001537 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 hash = PyObject_Hash(key);
1539 if (hash == -1)
1540 return -1;
1541 }
Victor Stinner742da042016-09-07 17:40:12 -07001542
1543 return _PyDict_DelItem_KnownHash(op, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001544}
1545
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001546int
1547_PyDict_DelItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1548{
INADA Naoki778928b2017-08-03 23:45:15 +09001549 Py_ssize_t ix;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001550 PyDictObject *mp;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001551 PyObject *old_value;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001552
1553 if (!PyDict_Check(op)) {
1554 PyErr_BadInternalCall();
1555 return -1;
1556 }
1557 assert(key);
1558 assert(hash != -1);
1559 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001560 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001561 if (ix == DKIX_ERROR)
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001562 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09001563 if (ix == DKIX_EMPTY || old_value == NULL) {
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001564 _PyErr_SetKeyError(key);
1565 return -1;
1566 }
Victor Stinner78601a32016-09-09 19:28:36 -07001567
1568 // Split table doesn't allow deletion. Combine it.
1569 if (_PyDict_HasSplitTable(mp)) {
1570 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1571 return -1;
1572 }
INADA Naoki778928b2017-08-03 23:45:15 +09001573 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001574 assert(ix >= 0);
1575 }
1576
INADA Naoki778928b2017-08-03 23:45:15 +09001577 return delitem_common(mp, hash, ix, old_value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001578}
1579
Antoine Pitroud741ed42016-12-27 14:23:43 +01001580/* This function promises that the predicate -> deletion sequence is atomic
1581 * (i.e. protected by the GIL), assuming the predicate itself doesn't
1582 * release the GIL.
1583 */
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001584int
1585_PyDict_DelItemIf(PyObject *op, PyObject *key,
1586 int (*predicate)(PyObject *value))
1587{
Antoine Pitroud741ed42016-12-27 14:23:43 +01001588 Py_ssize_t hashpos, ix;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001589 PyDictObject *mp;
1590 Py_hash_t hash;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001591 PyObject *old_value;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001592 int res;
1593
1594 if (!PyDict_Check(op)) {
1595 PyErr_BadInternalCall();
1596 return -1;
1597 }
1598 assert(key);
1599 hash = PyObject_Hash(key);
1600 if (hash == -1)
1601 return -1;
1602 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001603 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001604 if (ix == DKIX_ERROR)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001605 return -1;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001606 if (ix == DKIX_EMPTY || old_value == NULL) {
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001607 _PyErr_SetKeyError(key);
1608 return -1;
1609 }
Antoine Pitroud741ed42016-12-27 14:23:43 +01001610
1611 // Split table doesn't allow deletion. Combine it.
1612 if (_PyDict_HasSplitTable(mp)) {
1613 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1614 return -1;
1615 }
INADA Naoki778928b2017-08-03 23:45:15 +09001616 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001617 assert(ix >= 0);
1618 }
1619
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001620 res = predicate(old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001621 if (res == -1)
1622 return -1;
INADA Naoki778928b2017-08-03 23:45:15 +09001623
1624 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1625 assert(hashpos >= 0);
1626
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001627 if (res > 0)
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001628 return delitem_common(mp, hashpos, ix, old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001629 else
1630 return 0;
1631}
1632
1633
Guido van Rossum25831651993-05-19 14:50:45 +00001634void
Tim Peters1f5871e2000-07-04 17:44:48 +00001635PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001636{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001637 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001638 PyDictKeysObject *oldkeys;
1639 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 if (!PyDict_Check(op))
1643 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001644 mp = ((PyDictObject *)op);
1645 oldkeys = mp->ma_keys;
1646 oldvalues = mp->ma_values;
1647 if (oldvalues == empty_values)
1648 return;
1649 /* Empty the dict... */
INADA Naokia7576492018-11-14 18:39:27 +09001650 dictkeys_incref(Py_EMPTY_KEYS);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001651 mp->ma_keys = Py_EMPTY_KEYS;
1652 mp->ma_values = empty_values;
1653 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001654 mp->ma_version_tag = DICT_NEXT_VERSION();
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001655 /* ...then clear the keys and values */
1656 if (oldvalues != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001657 n = oldkeys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001658 for (i = 0; i < n; i++)
1659 Py_CLEAR(oldvalues[i]);
1660 free_values(oldvalues);
INADA Naokia7576492018-11-14 18:39:27 +09001661 dictkeys_decref(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001662 }
1663 else {
1664 assert(oldkeys->dk_refcnt == 1);
INADA Naokia7576492018-11-14 18:39:27 +09001665 dictkeys_decref(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001666 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02001667 assert(_PyDict_CheckConsistency(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001668}
1669
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001670/* Internal version of PyDict_Next that returns a hash value in addition
1671 * to the key and value.
1672 * Return 1 on success, return 0 when the reached the end of the dictionary
1673 * (or if op is not a dictionary)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001674 */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001675int
1676_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1677 PyObject **pvalue, Py_hash_t *phash)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001678{
INADA Naokica2d8be2016-11-04 16:59:10 +09001679 Py_ssize_t i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001680 PyDictObject *mp;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001681 PyDictKeyEntry *entry_ptr;
1682 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001683
1684 if (!PyDict_Check(op))
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001685 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 mp = (PyDictObject *)op;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001687 i = *ppos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001688 if (mp->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09001689 if (i < 0 || i >= mp->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001690 return 0;
INADA Naokica2d8be2016-11-04 16:59:10 +09001691 /* values of split table is always dense */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001692 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
INADA Naokica2d8be2016-11-04 16:59:10 +09001693 value = mp->ma_values[i];
1694 assert(value != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001696 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09001697 Py_ssize_t n = mp->ma_keys->dk_nentries;
1698 if (i < 0 || i >= n)
1699 return 0;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001700 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
1701 while (i < n && entry_ptr->me_value == NULL) {
1702 entry_ptr++;
1703 i++;
Victor Stinner742da042016-09-07 17:40:12 -07001704 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001705 if (i >= n)
1706 return 0;
1707 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001709 *ppos = i+1;
1710 if (pkey)
1711 *pkey = entry_ptr->me_key;
1712 if (phash)
1713 *phash = entry_ptr->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001714 if (pvalue)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001715 *pvalue = value;
1716 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001717}
1718
Tim Peters080c88b2003-02-15 03:01:11 +00001719/*
1720 * Iterate over a dict. Use like so:
1721 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001722 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001723 * PyObject *key, *value;
1724 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001725 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001726 * Refer to borrowed references in key and value.
Tim Peters080c88b2003-02-15 03:01:11 +00001727 * }
1728 *
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001729 * Return 1 on success, return 0 when the reached the end of the dictionary
1730 * (or if op is not a dictionary)
1731 *
Tim Peters080c88b2003-02-15 03:01:11 +00001732 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001733 * mutates the dict. One exception: it is safe if the loop merely changes
1734 * the values associated with the keys (but doesn't insert new keys or
1735 * delete keys), via PyDict_SetItem().
1736 */
Guido van Rossum25831651993-05-19 14:50:45 +00001737int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001738PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001739{
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001740 return _PyDict_Next(op, ppos, pkey, pvalue, NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001741}
1742
Eric Snow96c6af92015-05-29 22:21:39 -06001743/* Internal version of dict.pop(). */
1744PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001745_PyDict_Pop_KnownHash(PyObject *dict, PyObject *key, Py_hash_t hash, PyObject *deflt)
Eric Snow96c6af92015-05-29 22:21:39 -06001746{
Victor Stinner742da042016-09-07 17:40:12 -07001747 Py_ssize_t ix, hashpos;
Eric Snow96c6af92015-05-29 22:21:39 -06001748 PyObject *old_value, *old_key;
1749 PyDictKeyEntry *ep;
Yury Selivanov684ef2c2016-10-28 19:01:21 -04001750 PyDictObject *mp;
1751
1752 assert(PyDict_Check(dict));
1753 mp = (PyDictObject *)dict;
Eric Snow96c6af92015-05-29 22:21:39 -06001754
1755 if (mp->ma_used == 0) {
1756 if (deflt) {
1757 Py_INCREF(deflt);
1758 return deflt;
1759 }
1760 _PyErr_SetKeyError(key);
1761 return NULL;
1762 }
INADA Naoki778928b2017-08-03 23:45:15 +09001763 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001764 if (ix == DKIX_ERROR)
Eric Snow96c6af92015-05-29 22:21:39 -06001765 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001766 if (ix == DKIX_EMPTY || old_value == NULL) {
Eric Snow96c6af92015-05-29 22:21:39 -06001767 if (deflt) {
1768 Py_INCREF(deflt);
1769 return deflt;
1770 }
1771 _PyErr_SetKeyError(key);
1772 return NULL;
1773 }
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001774
Victor Stinner78601a32016-09-09 19:28:36 -07001775 // Split table doesn't allow deletion. Combine it.
1776 if (_PyDict_HasSplitTable(mp)) {
1777 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1778 return NULL;
1779 }
INADA Naoki778928b2017-08-03 23:45:15 +09001780 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001781 assert(ix >= 0);
1782 }
1783
INADA Naoki778928b2017-08-03 23:45:15 +09001784 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1785 assert(hashpos >= 0);
Victor Stinner78601a32016-09-09 19:28:36 -07001786 assert(old_value != NULL);
Eric Snow96c6af92015-05-29 22:21:39 -06001787 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001788 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokia7576492018-11-14 18:39:27 +09001789 dictkeys_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
Victor Stinner78601a32016-09-09 19:28:36 -07001790 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1791 ENSURE_ALLOWS_DELETIONS(mp);
1792 old_key = ep->me_key;
1793 ep->me_key = NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001794 ep->me_value = NULL;
Victor Stinner78601a32016-09-09 19:28:36 -07001795 Py_DECREF(old_key);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001796
1797 assert(_PyDict_CheckConsistency(mp));
Eric Snow96c6af92015-05-29 22:21:39 -06001798 return old_value;
1799}
1800
Serhiy Storchaka67796522017-01-12 18:34:33 +02001801PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001802_PyDict_Pop(PyObject *dict, PyObject *key, PyObject *deflt)
Serhiy Storchaka67796522017-01-12 18:34:33 +02001803{
1804 Py_hash_t hash;
1805
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001806 if (((PyDictObject *)dict)->ma_used == 0) {
Serhiy Storchaka67796522017-01-12 18:34:33 +02001807 if (deflt) {
1808 Py_INCREF(deflt);
1809 return deflt;
1810 }
1811 _PyErr_SetKeyError(key);
1812 return NULL;
1813 }
1814 if (!PyUnicode_CheckExact(key) ||
1815 (hash = ((PyASCIIObject *) key)->hash) == -1) {
1816 hash = PyObject_Hash(key);
1817 if (hash == -1)
1818 return NULL;
1819 }
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001820 return _PyDict_Pop_KnownHash(dict, key, hash, deflt);
Serhiy Storchaka67796522017-01-12 18:34:33 +02001821}
1822
Eric Snow96c6af92015-05-29 22:21:39 -06001823/* Internal version of dict.from_keys(). It is subclass-friendly. */
1824PyObject *
1825_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
1826{
1827 PyObject *it; /* iter(iterable) */
1828 PyObject *key;
1829 PyObject *d;
1830 int status;
1831
Victor Stinnera5ed5f02016-12-06 18:45:50 +01001832 d = _PyObject_CallNoArg(cls);
Eric Snow96c6af92015-05-29 22:21:39 -06001833 if (d == NULL)
1834 return NULL;
1835
1836 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
1837 if (PyDict_CheckExact(iterable)) {
1838 PyDictObject *mp = (PyDictObject *)d;
1839 PyObject *oldvalue;
1840 Py_ssize_t pos = 0;
1841 PyObject *key;
1842 Py_hash_t hash;
1843
Serhiy Storchakac61ac162017-03-21 08:52:38 +02001844 if (dictresize(mp, ESTIMATE_SIZE(PyDict_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001845 Py_DECREF(d);
1846 return NULL;
1847 }
1848
1849 while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
1850 if (insertdict(mp, key, hash, value)) {
1851 Py_DECREF(d);
1852 return NULL;
1853 }
1854 }
1855 return d;
1856 }
1857 if (PyAnySet_CheckExact(iterable)) {
1858 PyDictObject *mp = (PyDictObject *)d;
1859 Py_ssize_t pos = 0;
1860 PyObject *key;
1861 Py_hash_t hash;
1862
Victor Stinner742da042016-09-07 17:40:12 -07001863 if (dictresize(mp, ESTIMATE_SIZE(PySet_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001864 Py_DECREF(d);
1865 return NULL;
1866 }
1867
1868 while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
1869 if (insertdict(mp, key, hash, value)) {
1870 Py_DECREF(d);
1871 return NULL;
1872 }
1873 }
1874 return d;
1875 }
1876 }
1877
1878 it = PyObject_GetIter(iterable);
1879 if (it == NULL){
1880 Py_DECREF(d);
1881 return NULL;
1882 }
1883
1884 if (PyDict_CheckExact(d)) {
1885 while ((key = PyIter_Next(it)) != NULL) {
1886 status = PyDict_SetItem(d, key, value);
1887 Py_DECREF(key);
1888 if (status < 0)
1889 goto Fail;
1890 }
1891 } else {
1892 while ((key = PyIter_Next(it)) != NULL) {
1893 status = PyObject_SetItem(d, key, value);
1894 Py_DECREF(key);
1895 if (status < 0)
1896 goto Fail;
1897 }
1898 }
1899
1900 if (PyErr_Occurred())
1901 goto Fail;
1902 Py_DECREF(it);
1903 return d;
1904
1905Fail:
1906 Py_DECREF(it);
1907 Py_DECREF(d);
1908 return NULL;
1909}
1910
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001911/* Methods */
1912
1913static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001914dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001915{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001916 PyObject **values = mp->ma_values;
1917 PyDictKeysObject *keys = mp->ma_keys;
1918 Py_ssize_t i, n;
INADA Naokia6296d32017-08-24 14:55:17 +09001919
1920 /* bpo-31095: UnTrack is needed before calling any callbacks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 PyObject_GC_UnTrack(mp);
1922 Py_TRASHCAN_SAFE_BEGIN(mp)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001923 if (values != NULL) {
1924 if (values != empty_values) {
Victor Stinner742da042016-09-07 17:40:12 -07001925 for (i = 0, n = mp->ma_keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001926 Py_XDECREF(values[i]);
1927 }
1928 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001929 }
INADA Naokia7576492018-11-14 18:39:27 +09001930 dictkeys_decref(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02001932 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001933 assert(keys->dk_refcnt == 1);
INADA Naokia7576492018-11-14 18:39:27 +09001934 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001935 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001936 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1937 free_list[numfree++] = mp;
1938 else
1939 Py_TYPE(mp)->tp_free((PyObject *)mp);
1940 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001941}
1942
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001943
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001944static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001945dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001946{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001947 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001948 PyObject *key = NULL, *value = NULL;
1949 _PyUnicodeWriter writer;
1950 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00001951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 i = Py_ReprEnter((PyObject *)mp);
1953 if (i != 0) {
1954 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
1955 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001957 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001958 Py_ReprLeave((PyObject *)mp);
1959 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 }
Tim Petersa7259592001-06-16 05:11:17 +00001961
Victor Stinnerf91929b2013-11-19 13:07:38 +01001962 _PyUnicodeWriter_Init(&writer);
1963 writer.overallocate = 1;
1964 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
1965 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00001966
Victor Stinnerf91929b2013-11-19 13:07:38 +01001967 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
1968 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001970 /* Do repr() on each key+value pair, and insert ": " between them.
1971 Note that repr may mutate the dict. */
1972 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001973 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001975 PyObject *s;
1976 int res;
1977
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001978 /* Prevent repr from deleting key or value during key format. */
1979 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02001981
Victor Stinnerf91929b2013-11-19 13:07:38 +01001982 if (!first) {
1983 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
1984 goto error;
1985 }
1986 first = 0;
1987
1988 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01001990 goto error;
1991 res = _PyUnicodeWriter_WriteStr(&writer, s);
1992 Py_DECREF(s);
1993 if (res < 0)
1994 goto error;
1995
1996 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
1997 goto error;
1998
1999 s = PyObject_Repr(value);
2000 if (s == NULL)
2001 goto error;
2002 res = _PyUnicodeWriter_WriteStr(&writer, s);
2003 Py_DECREF(s);
2004 if (res < 0)
2005 goto error;
2006
2007 Py_CLEAR(key);
2008 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002009 }
Tim Petersa7259592001-06-16 05:11:17 +00002010
Victor Stinnerf91929b2013-11-19 13:07:38 +01002011 writer.overallocate = 0;
2012 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
2013 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00002014
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01002016
2017 return _PyUnicodeWriter_Finish(&writer);
2018
2019error:
2020 Py_ReprLeave((PyObject *)mp);
2021 _PyUnicodeWriter_Dealloc(&writer);
2022 Py_XDECREF(key);
2023 Py_XDECREF(value);
2024 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002025}
2026
Martin v. Löwis18e16552006-02-15 17:27:45 +00002027static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002028dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002029{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002030 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002031}
2032
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002033static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002034dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002035{
Victor Stinner742da042016-09-07 17:40:12 -07002036 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002037 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09002038 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002040 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002041 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002042 hash = PyObject_Hash(key);
2043 if (hash == -1)
2044 return NULL;
2045 }
INADA Naoki778928b2017-08-03 23:45:15 +09002046 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002047 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002048 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002049 if (ix == DKIX_EMPTY || value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002050 if (!PyDict_CheckExact(mp)) {
2051 /* Look up __missing__ method if we're a subclass. */
2052 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05002053 _Py_IDENTIFIER(__missing__);
2054 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002055 if (missing != NULL) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01002056 res = PyObject_CallFunctionObjArgs(missing,
2057 key, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002058 Py_DECREF(missing);
2059 return res;
2060 }
2061 else if (PyErr_Occurred())
2062 return NULL;
2063 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07002064 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002065 return NULL;
2066 }
INADA Naokiba609772016-12-07 20:41:42 +09002067 Py_INCREF(value);
2068 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002069}
2070
2071static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002072dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002073{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002074 if (w == NULL)
2075 return PyDict_DelItem((PyObject *)mp, v);
2076 else
2077 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002078}
2079
Guido van Rossuma9e7a811997-05-13 21:02:11 +00002080static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002081 (lenfunc)dict_length, /*mp_length*/
2082 (binaryfunc)dict_subscript, /*mp_subscript*/
2083 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002084};
2085
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002086static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002087dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002088{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002089 PyObject *v;
2090 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002091 PyDictKeyEntry *ep;
2092 Py_ssize_t size, n, offset;
2093 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002094
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002095 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002096 n = mp->ma_used;
2097 v = PyList_New(n);
2098 if (v == NULL)
2099 return NULL;
2100 if (n != mp->ma_used) {
2101 /* Durnit. The allocations caused the dict to resize.
2102 * Just start over, this shouldn't normally happen.
2103 */
2104 Py_DECREF(v);
2105 goto again;
2106 }
Victor Stinner742da042016-09-07 17:40:12 -07002107 ep = DK_ENTRIES(mp->ma_keys);
2108 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002109 if (mp->ma_values) {
2110 value_ptr = mp->ma_values;
2111 offset = sizeof(PyObject *);
2112 }
2113 else {
2114 value_ptr = &ep[0].me_value;
2115 offset = sizeof(PyDictKeyEntry);
2116 }
2117 for (i = 0, j = 0; i < size; i++) {
2118 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002119 PyObject *key = ep[i].me_key;
2120 Py_INCREF(key);
2121 PyList_SET_ITEM(v, j, key);
2122 j++;
2123 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002124 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002125 }
2126 assert(j == n);
2127 return v;
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_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002132{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002133 PyObject *v;
2134 Py_ssize_t i, j;
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002135 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002136 Py_ssize_t size, n, offset;
2137 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002138
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002139 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002140 n = mp->ma_used;
2141 v = PyList_New(n);
2142 if (v == NULL)
2143 return NULL;
2144 if (n != mp->ma_used) {
2145 /* Durnit. The allocations caused the dict to resize.
2146 * Just start over, this shouldn't normally happen.
2147 */
2148 Py_DECREF(v);
2149 goto again;
2150 }
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002151 ep = DK_ENTRIES(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002152 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002153 if (mp->ma_values) {
2154 value_ptr = mp->ma_values;
2155 offset = sizeof(PyObject *);
2156 }
2157 else {
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002158 value_ptr = &ep[0].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002159 offset = sizeof(PyDictKeyEntry);
2160 }
2161 for (i = 0, j = 0; i < size; i++) {
2162 PyObject *value = *value_ptr;
2163 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2164 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002165 Py_INCREF(value);
2166 PyList_SET_ITEM(v, j, value);
2167 j++;
2168 }
2169 }
2170 assert(j == n);
2171 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002172}
2173
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002174static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002175dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002176{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002177 PyObject *v;
2178 Py_ssize_t i, j, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002179 Py_ssize_t size, offset;
2180 PyObject *item, *key;
2181 PyDictKeyEntry *ep;
2182 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002184 /* Preallocate the list of tuples, to avoid allocations during
2185 * the loop over the items, which could trigger GC, which
2186 * could resize the dict. :-(
2187 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002188 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002189 n = mp->ma_used;
2190 v = PyList_New(n);
2191 if (v == NULL)
2192 return NULL;
2193 for (i = 0; i < n; i++) {
2194 item = PyTuple_New(2);
2195 if (item == NULL) {
2196 Py_DECREF(v);
2197 return NULL;
2198 }
2199 PyList_SET_ITEM(v, i, item);
2200 }
2201 if (n != mp->ma_used) {
2202 /* Durnit. The allocations caused the dict to resize.
2203 * Just start over, this shouldn't normally happen.
2204 */
2205 Py_DECREF(v);
2206 goto again;
2207 }
2208 /* Nothing we do below makes any function calls. */
Victor Stinner742da042016-09-07 17:40:12 -07002209 ep = DK_ENTRIES(mp->ma_keys);
2210 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002211 if (mp->ma_values) {
2212 value_ptr = mp->ma_values;
2213 offset = sizeof(PyObject *);
2214 }
2215 else {
2216 value_ptr = &ep[0].me_value;
2217 offset = sizeof(PyDictKeyEntry);
2218 }
2219 for (i = 0, j = 0; i < size; i++) {
2220 PyObject *value = *value_ptr;
2221 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2222 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002223 key = ep[i].me_key;
2224 item = PyList_GET_ITEM(v, j);
2225 Py_INCREF(key);
2226 PyTuple_SET_ITEM(item, 0, key);
2227 Py_INCREF(value);
2228 PyTuple_SET_ITEM(item, 1, value);
2229 j++;
2230 }
2231 }
2232 assert(j == n);
2233 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002234}
2235
Larry Hastings5c661892014-01-24 06:17:25 -08002236/*[clinic input]
2237@classmethod
2238dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08002239 iterable: object
2240 value: object=None
2241 /
2242
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002243Create a new dictionary with keys from iterable and values set to value.
Larry Hastings5c661892014-01-24 06:17:25 -08002244[clinic start generated code]*/
2245
Larry Hastings5c661892014-01-24 06:17:25 -08002246static PyObject *
2247dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002248/*[clinic end generated code: output=8fb98e4b10384999 input=382ba4855d0f74c3]*/
Larry Hastings5c661892014-01-24 06:17:25 -08002249{
Eric Snow96c6af92015-05-29 22:21:39 -06002250 return _PyDict_FromKeys((PyObject *)type, iterable, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002251}
2252
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002253static int
Victor Stinner742da042016-09-07 17:40:12 -07002254dict_update_common(PyObject *self, PyObject *args, PyObject *kwds,
2255 const char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002256{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002257 PyObject *arg = NULL;
2258 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002259
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002260 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261 result = -1;
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002262 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002263 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002264 _Py_IDENTIFIER(keys);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002265 PyObject *func;
2266 if (_PyObject_LookupAttrId(arg, &PyId_keys, &func) < 0) {
2267 result = -1;
2268 }
2269 else if (func != NULL) {
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002270 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002271 result = PyDict_Merge(self, arg, 1);
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002272 }
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002273 else {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002274 result = PyDict_MergeFromSeq2(self, arg, 1);
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002275 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002276 }
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 if (result == 0 && kwds != NULL) {
2279 if (PyArg_ValidateKeywordArguments(kwds))
2280 result = PyDict_Merge(self, kwds, 1);
2281 else
2282 result = -1;
2283 }
2284 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002285}
2286
Victor Stinner91f0d4a2017-01-19 12:45:06 +01002287/* Note: dict.update() uses the METH_VARARGS|METH_KEYWORDS calling convention.
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +03002288 Using METH_FASTCALL|METH_KEYWORDS would make dict.update(**dict2) calls
2289 slower, see the issue #29312. */
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002290static PyObject *
2291dict_update(PyObject *self, PyObject *args, PyObject *kwds)
2292{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002293 if (dict_update_common(self, args, kwds, "update") != -1)
2294 Py_RETURN_NONE;
2295 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002296}
2297
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002298/* Update unconditionally replaces existing items.
2299 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00002300 otherwise it leaves existing items unchanged.
2301
2302 PyDict_{Update,Merge} update/merge from a mapping object.
2303
Tim Petersf582b822001-12-11 18:51:08 +00002304 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00002305 producing iterable objects of length 2.
2306*/
2307
Tim Petersf582b822001-12-11 18:51:08 +00002308int
Tim Peters1fc240e2001-10-26 05:06:50 +00002309PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
2310{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002311 PyObject *it; /* iter(seq2) */
2312 Py_ssize_t i; /* index into seq2 of current element */
2313 PyObject *item; /* seq2[i] */
2314 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00002315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002316 assert(d != NULL);
2317 assert(PyDict_Check(d));
2318 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00002319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002320 it = PyObject_GetIter(seq2);
2321 if (it == NULL)
2322 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002324 for (i = 0; ; ++i) {
2325 PyObject *key, *value;
2326 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00002327
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002328 fast = NULL;
2329 item = PyIter_Next(it);
2330 if (item == NULL) {
2331 if (PyErr_Occurred())
2332 goto Fail;
2333 break;
2334 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002336 /* Convert item to sequence, and verify length 2. */
2337 fast = PySequence_Fast(item, "");
2338 if (fast == NULL) {
2339 if (PyErr_ExceptionMatches(PyExc_TypeError))
2340 PyErr_Format(PyExc_TypeError,
2341 "cannot convert dictionary update "
2342 "sequence element #%zd to a sequence",
2343 i);
2344 goto Fail;
2345 }
2346 n = PySequence_Fast_GET_SIZE(fast);
2347 if (n != 2) {
2348 PyErr_Format(PyExc_ValueError,
2349 "dictionary update sequence element #%zd "
2350 "has length %zd; 2 is required",
2351 i, n);
2352 goto Fail;
2353 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002355 /* Update/merge with this (key, value) pair. */
2356 key = PySequence_Fast_GET_ITEM(fast, 0);
2357 value = PySequence_Fast_GET_ITEM(fast, 1);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002358 Py_INCREF(key);
2359 Py_INCREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360 if (override || PyDict_GetItem(d, key) == NULL) {
2361 int status = PyDict_SetItem(d, key, value);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002362 if (status < 0) {
2363 Py_DECREF(key);
2364 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002365 goto Fail;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002366 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002367 }
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002368 Py_DECREF(key);
2369 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002370 Py_DECREF(fast);
2371 Py_DECREF(item);
2372 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002374 i = 0;
Victor Stinner611b0fa2016-09-14 15:02:01 +02002375 assert(_PyDict_CheckConsistency((PyDictObject *)d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00002377Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002378 Py_XDECREF(item);
2379 Py_XDECREF(fast);
2380 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002381Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002382 Py_DECREF(it);
2383 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00002384}
2385
doko@ubuntu.comc96df682016-10-11 08:04:02 +02002386static int
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002387dict_merge(PyObject *a, PyObject *b, int override)
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002388{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002389 PyDictObject *mp, *other;
2390 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -07002391 PyDictKeyEntry *entry, *ep0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002392
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002393 assert(0 <= override && override <= 2);
2394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 /* We accept for the argument either a concrete dictionary object,
2396 * or an abstract "mapping" object. For the former, we can do
2397 * things quite efficiently. For the latter, we only require that
2398 * PyMapping_Keys() and PyObject_GetItem() be supported.
2399 */
2400 if (a == NULL || !PyDict_Check(a) || b == NULL) {
2401 PyErr_BadInternalCall();
2402 return -1;
2403 }
2404 mp = (PyDictObject*)a;
INADA Naoki2aaf98c2018-09-26 12:59:00 +09002405 if (PyDict_Check(b) && (Py_TYPE(b)->tp_iter == (getiterfunc)dict_iter)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002406 other = (PyDictObject*)b;
2407 if (other == mp || other->ma_used == 0)
2408 /* a.update(a) or a.update({}); nothing to do */
2409 return 0;
2410 if (mp->ma_used == 0)
2411 /* Since the target dict is empty, PyDict_GetItem()
2412 * always returns NULL. Setting override to 1
2413 * skips the unnecessary test.
2414 */
2415 override = 1;
2416 /* Do one big resize at the start, rather than
2417 * incrementally resizing as we insert new items. Expect
2418 * that there will be no (or few) overlapping keys.
2419 */
INADA Naokib1152be2016-10-27 19:26:50 +09002420 if (USABLE_FRACTION(mp->ma_keys->dk_size) < other->ma_used) {
2421 if (dictresize(mp, ESTIMATE_SIZE(mp->ma_used + other->ma_used))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002422 return -1;
INADA Naokib1152be2016-10-27 19:26:50 +09002423 }
2424 }
Victor Stinner742da042016-09-07 17:40:12 -07002425 ep0 = DK_ENTRIES(other->ma_keys);
2426 for (i = 0, n = other->ma_keys->dk_nentries; i < n; i++) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002427 PyObject *key, *value;
2428 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002429 entry = &ep0[i];
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002430 key = entry->me_key;
2431 hash = entry->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002432 if (other->ma_values)
2433 value = other->ma_values[i];
2434 else
2435 value = entry->me_value;
2436
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002437 if (value != NULL) {
2438 int err = 0;
2439 Py_INCREF(key);
2440 Py_INCREF(value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002441 if (override == 1)
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002442 err = insertdict(mp, key, hash, value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002443 else if (_PyDict_GetItem_KnownHash(a, key, hash) == NULL) {
2444 if (PyErr_Occurred()) {
2445 Py_DECREF(value);
2446 Py_DECREF(key);
2447 return -1;
2448 }
2449 err = insertdict(mp, key, hash, value);
2450 }
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002451 else if (override != 0) {
2452 _PyErr_SetKeyError(key);
2453 Py_DECREF(value);
2454 Py_DECREF(key);
2455 return -1;
2456 }
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002457 Py_DECREF(value);
2458 Py_DECREF(key);
2459 if (err != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002460 return -1;
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002461
Victor Stinner742da042016-09-07 17:40:12 -07002462 if (n != other->ma_keys->dk_nentries) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002463 PyErr_SetString(PyExc_RuntimeError,
2464 "dict mutated during update");
2465 return -1;
2466 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002467 }
2468 }
2469 }
2470 else {
2471 /* Do it the generic, slower way */
2472 PyObject *keys = PyMapping_Keys(b);
2473 PyObject *iter;
2474 PyObject *key, *value;
2475 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002477 if (keys == NULL)
2478 /* Docstring says this is equivalent to E.keys() so
2479 * if E doesn't have a .keys() method we want
2480 * AttributeError to percolate up. Might as well
2481 * do the same for any other error.
2482 */
2483 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002485 iter = PyObject_GetIter(keys);
2486 Py_DECREF(keys);
2487 if (iter == NULL)
2488 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002490 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002491 if (override != 1 && PyDict_GetItem(a, key) != NULL) {
2492 if (override != 0) {
2493 _PyErr_SetKeyError(key);
2494 Py_DECREF(key);
2495 Py_DECREF(iter);
2496 return -1;
2497 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002498 Py_DECREF(key);
2499 continue;
2500 }
2501 value = PyObject_GetItem(b, key);
2502 if (value == NULL) {
2503 Py_DECREF(iter);
2504 Py_DECREF(key);
2505 return -1;
2506 }
2507 status = PyDict_SetItem(a, key, value);
2508 Py_DECREF(key);
2509 Py_DECREF(value);
2510 if (status < 0) {
2511 Py_DECREF(iter);
2512 return -1;
2513 }
2514 }
2515 Py_DECREF(iter);
2516 if (PyErr_Occurred())
2517 /* Iterator completed, via error */
2518 return -1;
2519 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02002520 assert(_PyDict_CheckConsistency((PyDictObject *)a));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002521 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002522}
2523
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002524int
2525PyDict_Update(PyObject *a, PyObject *b)
2526{
2527 return dict_merge(a, b, 1);
2528}
2529
2530int
2531PyDict_Merge(PyObject *a, PyObject *b, int override)
2532{
2533 /* XXX Deprecate override not in (0, 1). */
2534 return dict_merge(a, b, override != 0);
2535}
2536
2537int
2538_PyDict_MergeEx(PyObject *a, PyObject *b, int override)
2539{
2540 return dict_merge(a, b, override);
2541}
2542
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002543static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302544dict_copy(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002545{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002546 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002547}
2548
2549PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002550PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002551{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002552 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002553 PyDictObject *mp;
2554 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002556 if (o == NULL || !PyDict_Check(o)) {
2557 PyErr_BadInternalCall();
2558 return NULL;
2559 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002560
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002561 mp = (PyDictObject *)o;
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002562 if (mp->ma_used == 0) {
2563 /* The dict is empty; just return a new dict. */
2564 return PyDict_New();
2565 }
2566
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002567 if (_PyDict_HasSplitTable(mp)) {
2568 PyDictObject *split_copy;
Victor Stinner742da042016-09-07 17:40:12 -07002569 Py_ssize_t size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
2570 PyObject **newvalues;
2571 newvalues = new_values(size);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002572 if (newvalues == NULL)
2573 return PyErr_NoMemory();
2574 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2575 if (split_copy == NULL) {
2576 free_values(newvalues);
2577 return NULL;
2578 }
2579 split_copy->ma_values = newvalues;
2580 split_copy->ma_keys = mp->ma_keys;
2581 split_copy->ma_used = mp->ma_used;
INADA Naokid1c82c52018-04-03 11:43:53 +09002582 split_copy->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokia7576492018-11-14 18:39:27 +09002583 dictkeys_incref(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002584 for (i = 0, n = size; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002585 PyObject *value = mp->ma_values[i];
2586 Py_XINCREF(value);
2587 split_copy->ma_values[i] = value;
2588 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002589 if (_PyObject_GC_IS_TRACKED(mp))
2590 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002591 return (PyObject *)split_copy;
2592 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002593
2594 if (PyDict_CheckExact(mp) && mp->ma_values == NULL &&
2595 (mp->ma_used >= (mp->ma_keys->dk_nentries * 2) / 3))
2596 {
2597 /* Use fast-copy if:
2598
2599 (1) 'mp' is an instance of a subclassed dict; and
2600
2601 (2) 'mp' is not a split-dict; and
2602
2603 (3) if 'mp' is non-compact ('del' operation does not resize dicts),
2604 do fast-copy only if it has at most 1/3 non-used keys.
2605
Ville Skyttä61f82e02018-04-20 23:08:45 +03002606 The last condition (3) is important to guard against a pathological
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002607 case when a large dict is almost emptied with multiple del/pop
2608 operations and copied after that. In cases like this, we defer to
2609 PyDict_Merge, which produces a compacted copy.
2610 */
2611 return clone_combined_dict(mp);
2612 }
2613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002614 copy = PyDict_New();
2615 if (copy == NULL)
2616 return NULL;
2617 if (PyDict_Merge(copy, o, 1) == 0)
2618 return copy;
2619 Py_DECREF(copy);
2620 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002621}
2622
Martin v. Löwis18e16552006-02-15 17:27:45 +00002623Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002624PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002625{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002626 if (mp == NULL || !PyDict_Check(mp)) {
2627 PyErr_BadInternalCall();
2628 return -1;
2629 }
2630 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002631}
2632
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002633PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002634PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002635{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002636 if (mp == NULL || !PyDict_Check(mp)) {
2637 PyErr_BadInternalCall();
2638 return NULL;
2639 }
2640 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002641}
2642
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002643PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002644PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002645{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002646 if (mp == NULL || !PyDict_Check(mp)) {
2647 PyErr_BadInternalCall();
2648 return NULL;
2649 }
2650 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002651}
2652
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002653PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002654PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002655{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002656 if (mp == NULL || !PyDict_Check(mp)) {
2657 PyErr_BadInternalCall();
2658 return NULL;
2659 }
2660 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002661}
2662
Tim Peterse63415e2001-05-08 04:38:29 +00002663/* Return 1 if dicts equal, 0 if not, -1 if error.
2664 * Gets out as soon as any difference is detected.
2665 * Uses only Py_EQ comparison.
2666 */
2667static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002668dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002669{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002670 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002672 if (a->ma_used != b->ma_used)
2673 /* can't be equal if # of entries differ */
2674 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002675 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Victor Stinner742da042016-09-07 17:40:12 -07002676 for (i = 0; i < a->ma_keys->dk_nentries; i++) {
2677 PyDictKeyEntry *ep = &DK_ENTRIES(a->ma_keys)[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002678 PyObject *aval;
2679 if (a->ma_values)
2680 aval = a->ma_values[i];
2681 else
2682 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002683 if (aval != NULL) {
2684 int cmp;
2685 PyObject *bval;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002686 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002687 /* temporarily bump aval's refcount to ensure it stays
2688 alive until we're done with it */
2689 Py_INCREF(aval);
2690 /* ditto for key */
2691 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002692 /* reuse the known hash value */
INADA Naoki778928b2017-08-03 23:45:15 +09002693 b->ma_keys->dk_lookup(b, key, ep->me_hash, &bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002694 if (bval == NULL) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002695 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002696 Py_DECREF(aval);
2697 if (PyErr_Occurred())
2698 return -1;
2699 return 0;
2700 }
2701 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002702 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002703 Py_DECREF(aval);
2704 if (cmp <= 0) /* error or not equal */
2705 return cmp;
2706 }
2707 }
2708 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002709}
Tim Peterse63415e2001-05-08 04:38:29 +00002710
2711static PyObject *
2712dict_richcompare(PyObject *v, PyObject *w, int op)
2713{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002714 int cmp;
2715 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002717 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2718 res = Py_NotImplemented;
2719 }
2720 else if (op == Py_EQ || op == Py_NE) {
2721 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2722 if (cmp < 0)
2723 return NULL;
2724 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2725 }
2726 else
2727 res = Py_NotImplemented;
2728 Py_INCREF(res);
2729 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002730}
Tim Peterse63415e2001-05-08 04:38:29 +00002731
Larry Hastings61272b72014-01-07 12:41:53 -08002732/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002733
2734@coexist
2735dict.__contains__
2736
2737 key: object
2738 /
2739
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002740True if the dictionary has the specified key, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002741[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002742
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002743static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002744dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka19d25972017-02-04 08:05:07 +02002745/*[clinic end generated code: output=a3d03db709ed6e6b input=fe1cb42ad831e820]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002746{
Larry Hastingsc2047262014-01-25 20:43:29 -08002747 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002748 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002749 Py_ssize_t ix;
INADA Naokiba609772016-12-07 20:41:42 +09002750 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002752 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002753 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002754 hash = PyObject_Hash(key);
2755 if (hash == -1)
2756 return NULL;
2757 }
INADA Naoki778928b2017-08-03 23:45:15 +09002758 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002759 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002760 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002761 if (ix == DKIX_EMPTY || value == NULL)
Victor Stinner742da042016-09-07 17:40:12 -07002762 Py_RETURN_FALSE;
2763 Py_RETURN_TRUE;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002764}
2765
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002766/*[clinic input]
2767dict.get
2768
2769 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002770 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002771 /
2772
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002773Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002774[clinic start generated code]*/
2775
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002776static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002777dict_get_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002778/*[clinic end generated code: output=bba707729dee05bf input=279ddb5790b6b107]*/
Barry Warsawc38c5da1997-10-06 17:49:20 +00002779{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002780 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002781 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002782 Py_ssize_t ix;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002784 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002785 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002786 hash = PyObject_Hash(key);
2787 if (hash == -1)
2788 return NULL;
2789 }
INADA Naoki778928b2017-08-03 23:45:15 +09002790 ix = (self->ma_keys->dk_lookup) (self, key, hash, &val);
Victor Stinner742da042016-09-07 17:40:12 -07002791 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002792 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002793 if (ix == DKIX_EMPTY || val == NULL) {
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002794 val = default_value;
INADA Naokiba609772016-12-07 20:41:42 +09002795 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002796 Py_INCREF(val);
2797 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002798}
2799
Benjamin Peterson00e98862013-03-07 22:16:29 -05002800PyObject *
2801PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002802{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002803 PyDictObject *mp = (PyDictObject *)d;
INADA Naoki93f26f72016-11-02 18:45:16 +09002804 PyObject *value;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002805 Py_hash_t hash;
Guido van Rossum164452c2000-08-08 16:12:54 +00002806
Benjamin Peterson00e98862013-03-07 22:16:29 -05002807 if (!PyDict_Check(d)) {
2808 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002809 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002810 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002812 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002813 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002814 hash = PyObject_Hash(key);
2815 if (hash == -1)
2816 return NULL;
2817 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002818
2819 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
2820 if (insertion_resize(mp) < 0)
2821 return NULL;
2822 }
2823
INADA Naoki778928b2017-08-03 23:45:15 +09002824 Py_ssize_t ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002825 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002826 return NULL;
INADA Naoki93f26f72016-11-02 18:45:16 +09002827
2828 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09002829 ((ix >= 0 && value == NULL && mp->ma_used != ix) ||
INADA Naoki93f26f72016-11-02 18:45:16 +09002830 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
2831 if (insertion_resize(mp) < 0) {
2832 return NULL;
2833 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002834 ix = DKIX_EMPTY;
2835 }
2836
2837 if (ix == DKIX_EMPTY) {
2838 PyDictKeyEntry *ep, *ep0;
2839 value = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002840 if (mp->ma_keys->dk_usable <= 0) {
Victor Stinner3c336c52016-09-12 14:17:40 +02002841 if (insertion_resize(mp) < 0) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002842 return NULL;
Victor Stinner3c336c52016-09-12 14:17:40 +02002843 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002844 }
INADA Naoki778928b2017-08-03 23:45:15 +09002845 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naoki93f26f72016-11-02 18:45:16 +09002846 ep0 = DK_ENTRIES(mp->ma_keys);
2847 ep = &ep0[mp->ma_keys->dk_nentries];
INADA Naokia7576492018-11-14 18:39:27 +09002848 dictkeys_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002849 Py_INCREF(key);
INADA Naoki93f26f72016-11-02 18:45:16 +09002850 Py_INCREF(value);
2851 MAINTAIN_TRACKING(mp, key, value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002852 ep->me_key = key;
2853 ep->me_hash = hash;
INADA Naokiba609772016-12-07 20:41:42 +09002854 if (_PyDict_HasSplitTable(mp)) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002855 assert(mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
2856 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Victor Stinner742da042016-09-07 17:40:12 -07002857 }
2858 else {
INADA Naoki93f26f72016-11-02 18:45:16 +09002859 ep->me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07002860 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002861 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002862 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naoki93f26f72016-11-02 18:45:16 +09002863 mp->ma_keys->dk_usable--;
2864 mp->ma_keys->dk_nentries++;
2865 assert(mp->ma_keys->dk_usable >= 0);
2866 }
INADA Naokiba609772016-12-07 20:41:42 +09002867 else if (value == NULL) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002868 value = defaultobj;
2869 assert(_PyDict_HasSplitTable(mp));
2870 assert(ix == mp->ma_used);
2871 Py_INCREF(value);
2872 MAINTAIN_TRACKING(mp, key, value);
INADA Naokiba609772016-12-07 20:41:42 +09002873 mp->ma_values[ix] = value;
INADA Naoki93f26f72016-11-02 18:45:16 +09002874 mp->ma_used++;
2875 mp->ma_version_tag = DICT_NEXT_VERSION();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002877
2878 assert(_PyDict_CheckConsistency(mp));
2879 return value;
Guido van Rossum164452c2000-08-08 16:12:54 +00002880}
2881
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002882/*[clinic input]
2883dict.setdefault
2884
2885 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002886 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002887 /
2888
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002889Insert key with a value of default if key is not in the dictionary.
2890
2891Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002892[clinic start generated code]*/
2893
Benjamin Peterson00e98862013-03-07 22:16:29 -05002894static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002895dict_setdefault_impl(PyDictObject *self, PyObject *key,
2896 PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002897/*[clinic end generated code: output=f8c1101ebf69e220 input=0f063756e815fd9d]*/
Benjamin Peterson00e98862013-03-07 22:16:29 -05002898{
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002899 PyObject *val;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002900
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002901 val = PyDict_SetDefault((PyObject *)self, key, default_value);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002902 Py_XINCREF(val);
2903 return val;
2904}
Guido van Rossum164452c2000-08-08 16:12:54 +00002905
2906static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302907dict_clear(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002908{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002909 PyDict_Clear((PyObject *)mp);
2910 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002911}
2912
Guido van Rossumba6ab842000-12-12 22:02:18 +00002913static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002914dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002915{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002916 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00002917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002918 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2919 return NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06002920
Yury Selivanov684ef2c2016-10-28 19:01:21 -04002921 return _PyDict_Pop((PyObject*)mp, key, deflt);
Guido van Rossume027d982002-04-12 15:11:59 +00002922}
2923
2924static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302925dict_popitem(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Guido van Rossumba6ab842000-12-12 22:02:18 +00002926{
Victor Stinner742da042016-09-07 17:40:12 -07002927 Py_ssize_t i, j;
2928 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002929 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002931 /* Allocate the result tuple before checking the size. Believe it
2932 * or not, this allocation could trigger a garbage collection which
2933 * could empty the dict, so if we checked the size first and that
2934 * happened, the result would be an infinite loop (searching for an
2935 * entry that no longer exists). Note that the usual popitem()
2936 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2937 * tuple away if the dict *is* empty isn't a significant
2938 * inefficiency -- possible, but unlikely in practice.
2939 */
2940 res = PyTuple_New(2);
2941 if (res == NULL)
2942 return NULL;
2943 if (mp->ma_used == 0) {
2944 Py_DECREF(res);
2945 PyErr_SetString(PyExc_KeyError,
2946 "popitem(): dictionary is empty");
2947 return NULL;
2948 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002949 /* Convert split table to combined table */
2950 if (mp->ma_keys->dk_lookup == lookdict_split) {
2951 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
2952 Py_DECREF(res);
2953 return NULL;
2954 }
2955 }
2956 ENSURE_ALLOWS_DELETIONS(mp);
Victor Stinner742da042016-09-07 17:40:12 -07002957
2958 /* Pop last item */
2959 ep0 = DK_ENTRIES(mp->ma_keys);
2960 i = mp->ma_keys->dk_nentries - 1;
2961 while (i >= 0 && ep0[i].me_value == NULL) {
2962 i--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002963 }
Victor Stinner742da042016-09-07 17:40:12 -07002964 assert(i >= 0);
2965
2966 ep = &ep0[i];
2967 j = lookdict_index(mp->ma_keys, ep->me_hash, i);
2968 assert(j >= 0);
INADA Naokia7576492018-11-14 18:39:27 +09002969 assert(dictkeys_get_index(mp->ma_keys, j) == i);
2970 dictkeys_set_index(mp->ma_keys, j, DKIX_DUMMY);
Victor Stinner742da042016-09-07 17:40:12 -07002971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002972 PyTuple_SET_ITEM(res, 0, ep->me_key);
2973 PyTuple_SET_ITEM(res, 1, ep->me_value);
Victor Stinner742da042016-09-07 17:40:12 -07002974 ep->me_key = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002975 ep->me_value = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002976 /* We can't dk_usable++ since there is DKIX_DUMMY in indices */
2977 mp->ma_keys->dk_nentries = i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002978 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002979 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +02002980 assert(_PyDict_CheckConsistency(mp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002981 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002982}
2983
Jeremy Hylton8caad492000-06-23 14:18:11 +00002984static int
2985dict_traverse(PyObject *op, visitproc visit, void *arg)
2986{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002987 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson55f44522016-09-05 12:12:59 -07002988 PyDictKeysObject *keys = mp->ma_keys;
Serhiy Storchaka46825d22016-09-26 21:29:34 +03002989 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Victor Stinner742da042016-09-07 17:40:12 -07002990 Py_ssize_t i, n = keys->dk_nentries;
2991
Benjamin Peterson55f44522016-09-05 12:12:59 -07002992 if (keys->dk_lookup == lookdict) {
2993 for (i = 0; i < n; i++) {
2994 if (entries[i].me_value != NULL) {
2995 Py_VISIT(entries[i].me_value);
2996 Py_VISIT(entries[i].me_key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002997 }
2998 }
Victor Stinner742da042016-09-07 17:40:12 -07002999 }
3000 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003001 if (mp->ma_values != NULL) {
Benjamin Peterson55f44522016-09-05 12:12:59 -07003002 for (i = 0; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003003 Py_VISIT(mp->ma_values[i]);
3004 }
3005 }
3006 else {
Benjamin Peterson55f44522016-09-05 12:12:59 -07003007 for (i = 0; i < n; i++) {
3008 Py_VISIT(entries[i].me_value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003009 }
3010 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003011 }
3012 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00003013}
3014
3015static int
3016dict_tp_clear(PyObject *op)
3017{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003018 PyDict_Clear(op);
3019 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00003020}
3021
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003022static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003023
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003024Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003025_PyDict_SizeOf(PyDictObject *mp)
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003026{
Victor Stinner742da042016-09-07 17:40:12 -07003027 Py_ssize_t size, usable, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003028
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003029 size = DK_SIZE(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07003030 usable = USABLE_FRACTION(size);
3031
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02003032 res = _PyObject_SIZE(Py_TYPE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003033 if (mp->ma_values)
Victor Stinner742da042016-09-07 17:40:12 -07003034 res += usable * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003035 /* If the dictionary is split, the keys portion is accounted-for
3036 in the type object. */
3037 if (mp->ma_keys->dk_refcnt == 1)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003038 res += (sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003039 + DK_IXSIZE(mp->ma_keys) * size
3040 + sizeof(PyDictKeyEntry) * usable);
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003041 return res;
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003042}
3043
3044Py_ssize_t
3045_PyDict_KeysSize(PyDictKeysObject *keys)
3046{
Victor Stinner98ee9d52016-09-08 09:33:56 -07003047 return (sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003048 + DK_IXSIZE(keys) * DK_SIZE(keys)
3049 + USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry));
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003050}
3051
doko@ubuntu.com17210f52016-01-14 14:04:59 +01003052static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303053dict_sizeof(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003054{
3055 return PyLong_FromSsize_t(_PyDict_SizeOf(mp));
3056}
3057
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00003058PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
3059
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003060PyDoc_STRVAR(sizeof__doc__,
3061"D.__sizeof__() -> size of D in memory, in bytes");
3062
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003063PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00003064"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00003065If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00003066
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003067PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00003068"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000030692-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003070
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003071PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04003072"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
3073If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
3074If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
3075In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00003076
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003077PyDoc_STRVAR(clear__doc__,
3078"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003079
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003080PyDoc_STRVAR(copy__doc__,
3081"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00003082
Guido van Rossumb90c8482007-02-10 01:11:45 +00003083/* Forward */
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303084static PyObject *dictkeys_new(PyObject *, PyObject *);
3085static PyObject *dictitems_new(PyObject *, PyObject *);
3086static PyObject *dictvalues_new(PyObject *, PyObject *);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003087
Guido van Rossum45c85d12007-07-27 16:31:40 +00003088PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003089 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003090PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003091 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003092PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003093 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00003094
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003095static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07003096 DICT___CONTAINS___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003097 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
3098 getitem__doc__},
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003099 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003100 sizeof__doc__},
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003101 DICT_GET_METHODDEF
3102 DICT_SETDEFAULT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003103 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
3104 pop__doc__},
3105 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
3106 popitem__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303107 {"keys", dictkeys_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003108 keys__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303109 {"items", dictitems_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003110 items__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303111 {"values", dictvalues_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003112 values__doc__},
3113 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
3114 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08003115 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003116 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
3117 clear__doc__},
3118 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
3119 copy__doc__},
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003120 DICT___REVERSED___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003121 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003122};
3123
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00003124/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00003125int
3126PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003127{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003128 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07003129 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003130 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003131 PyObject *value;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003132
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003133 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003134 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003135 hash = PyObject_Hash(key);
3136 if (hash == -1)
3137 return -1;
3138 }
INADA Naoki778928b2017-08-03 23:45:15 +09003139 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003140 if (ix == DKIX_ERROR)
3141 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003142 return (ix != DKIX_EMPTY && value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003143}
3144
Thomas Wouterscf297e42007-02-23 15:07:44 +00003145/* Internal version of PyDict_Contains used when the hash value is already known */
3146int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003147_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00003148{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003149 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003150 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -07003151 Py_ssize_t ix;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003152
INADA Naoki778928b2017-08-03 23:45:15 +09003153 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003154 if (ix == DKIX_ERROR)
3155 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003156 return (ix != DKIX_EMPTY && value != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00003157}
3158
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003159/* Hack to implement "key in dict" */
3160static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003161 0, /* sq_length */
3162 0, /* sq_concat */
3163 0, /* sq_repeat */
3164 0, /* sq_item */
3165 0, /* sq_slice */
3166 0, /* sq_ass_item */
3167 0, /* sq_ass_slice */
3168 PyDict_Contains, /* sq_contains */
3169 0, /* sq_inplace_concat */
3170 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003171};
3172
Guido van Rossum09e563a2001-05-01 12:10:21 +00003173static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003174dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3175{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003176 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003177 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003179 assert(type != NULL && type->tp_alloc != NULL);
3180 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02003181 if (self == NULL)
3182 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003183 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003184
Victor Stinnera9f61a52013-07-16 22:17:26 +02003185 /* The object has been implicitly tracked by tp_alloc */
3186 if (type == &PyDict_Type)
3187 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003188
3189 d->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003190 d->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07003191 d->ma_keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003192 if (d->ma_keys == NULL) {
3193 Py_DECREF(self);
3194 return NULL;
3195 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02003196 assert(_PyDict_CheckConsistency(d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003197 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003198}
3199
Tim Peters25786c02001-09-02 08:22:48 +00003200static int
3201dict_init(PyObject *self, PyObject *args, PyObject *kwds)
3202{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003203 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00003204}
3205
Tim Peters6d6c1a32001-08-02 04:15:00 +00003206static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003207dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00003208{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003209 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003210}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003211
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003212PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00003213"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00003214"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003215" (key, value) pairs\n"
3216"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00003217" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003218" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00003219" d[k] = v\n"
3220"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
3221" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00003222
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003223PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003224 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3225 "dict",
3226 sizeof(PyDictObject),
3227 0,
3228 (destructor)dict_dealloc, /* tp_dealloc */
3229 0, /* tp_print */
3230 0, /* tp_getattr */
3231 0, /* tp_setattr */
3232 0, /* tp_reserved */
3233 (reprfunc)dict_repr, /* tp_repr */
3234 0, /* tp_as_number */
3235 &dict_as_sequence, /* tp_as_sequence */
3236 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003237 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003238 0, /* tp_call */
3239 0, /* tp_str */
3240 PyObject_GenericGetAttr, /* tp_getattro */
3241 0, /* tp_setattro */
3242 0, /* tp_as_buffer */
3243 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3244 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
3245 dictionary_doc, /* tp_doc */
3246 dict_traverse, /* tp_traverse */
3247 dict_tp_clear, /* tp_clear */
3248 dict_richcompare, /* tp_richcompare */
3249 0, /* tp_weaklistoffset */
3250 (getiterfunc)dict_iter, /* tp_iter */
3251 0, /* tp_iternext */
3252 mapp_methods, /* tp_methods */
3253 0, /* tp_members */
3254 0, /* tp_getset */
3255 0, /* tp_base */
3256 0, /* tp_dict */
3257 0, /* tp_descr_get */
3258 0, /* tp_descr_set */
3259 0, /* tp_dictoffset */
3260 dict_init, /* tp_init */
3261 PyType_GenericAlloc, /* tp_alloc */
3262 dict_new, /* tp_new */
3263 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003264};
3265
Victor Stinner3c1e4812012-03-26 22:10:51 +02003266PyObject *
3267_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
3268{
3269 PyObject *kv;
3270 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02003271 if (kv == NULL) {
3272 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003273 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02003274 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02003275 return PyDict_GetItem(dp, kv);
3276}
3277
Guido van Rossum3cca2451997-05-16 14:23:33 +00003278/* For backward compatibility with old dictionary interface */
3279
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003280PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003281PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003282{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003283 PyObject *kv, *rv;
3284 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003285 if (kv == NULL) {
3286 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003287 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003288 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003289 rv = PyDict_GetItem(v, kv);
3290 Py_DECREF(kv);
3291 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003292}
3293
3294int
Victor Stinner3c1e4812012-03-26 22:10:51 +02003295_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
3296{
3297 PyObject *kv;
3298 kv = _PyUnicode_FromId(key); /* borrowed */
3299 if (kv == NULL)
3300 return -1;
3301 return PyDict_SetItem(v, kv, item);
3302}
3303
3304int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003305PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003307 PyObject *kv;
3308 int err;
3309 kv = PyUnicode_FromString(key);
3310 if (kv == NULL)
3311 return -1;
3312 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
3313 err = PyDict_SetItem(v, kv, item);
3314 Py_DECREF(kv);
3315 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003316}
3317
3318int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01003319_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
3320{
3321 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
3322 if (kv == NULL)
3323 return -1;
3324 return PyDict_DelItem(v, kv);
3325}
3326
3327int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003328PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003329{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003330 PyObject *kv;
3331 int err;
3332 kv = PyUnicode_FromString(key);
3333 if (kv == NULL)
3334 return -1;
3335 err = PyDict_DelItem(v, kv);
3336 Py_DECREF(kv);
3337 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003338}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003339
Raymond Hettinger019a1482004-03-18 02:41:19 +00003340/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003341
3342typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003343 PyObject_HEAD
3344 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
3345 Py_ssize_t di_used;
3346 Py_ssize_t di_pos;
3347 PyObject* di_result; /* reusable result tuple for iteritems */
3348 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003349} dictiterobject;
3350
3351static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003352dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003353{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003354 dictiterobject *di;
3355 di = PyObject_GC_New(dictiterobject, itertype);
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003356 if (di == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003357 return NULL;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003358 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003359 Py_INCREF(dict);
3360 di->di_dict = dict;
3361 di->di_used = dict->ma_used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003362 di->len = dict->ma_used;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003363 if ((itertype == &PyDictRevIterKey_Type ||
3364 itertype == &PyDictRevIterItem_Type ||
3365 itertype == &PyDictRevIterValue_Type) && dict->ma_used) {
3366 di->di_pos = dict->ma_keys->dk_nentries - 1;
3367 }
3368 else {
3369 di->di_pos = 0;
3370 }
3371 if (itertype == &PyDictIterItem_Type ||
3372 itertype == &PyDictRevIterItem_Type) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003373 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
3374 if (di->di_result == NULL) {
3375 Py_DECREF(di);
3376 return NULL;
3377 }
3378 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003379 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003380 di->di_result = NULL;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003381 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003382 _PyObject_GC_TRACK(di);
3383 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003384}
3385
3386static void
3387dictiter_dealloc(dictiterobject *di)
3388{
INADA Naokia6296d32017-08-24 14:55:17 +09003389 /* bpo-31095: UnTrack is needed before calling any callbacks */
3390 _PyObject_GC_UNTRACK(di);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003391 Py_XDECREF(di->di_dict);
3392 Py_XDECREF(di->di_result);
3393 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003394}
3395
3396static int
3397dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
3398{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003399 Py_VISIT(di->di_dict);
3400 Py_VISIT(di->di_result);
3401 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003402}
3403
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003404static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303405dictiter_len(dictiterobject *di, PyObject *Py_UNUSED(ignored))
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003406{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003407 Py_ssize_t len = 0;
3408 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
3409 len = di->len;
3410 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003411}
3412
Guido van Rossumb90c8482007-02-10 01:11:45 +00003413PyDoc_STRVAR(length_hint_doc,
3414 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003415
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003416static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303417dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003418
3419PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3420
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003421static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003422 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
3423 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003424 {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS,
3425 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003426 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003427};
3428
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003429static PyObject*
3430dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00003431{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003432 PyObject *key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003433 Py_ssize_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003434 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003435 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00003436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003437 if (d == NULL)
3438 return NULL;
3439 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00003440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003441 if (di->di_used != d->ma_used) {
3442 PyErr_SetString(PyExc_RuntimeError,
3443 "dictionary changed size during iteration");
3444 di->di_used = -1; /* Make this state sticky */
3445 return NULL;
3446 }
Guido van Rossum2147df72002-07-16 20:30:22 +00003447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003448 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003449 k = d->ma_keys;
INADA Naokica2d8be2016-11-04 16:59:10 +09003450 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003451 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003452 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003453 goto fail;
3454 key = DK_ENTRIES(k)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003455 assert(d->ma_values[i] != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003456 }
3457 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003458 Py_ssize_t n = k->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003459 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
3460 while (i < n && entry_ptr->me_value == NULL) {
3461 entry_ptr++;
3462 i++;
3463 }
3464 if (i >= n)
3465 goto fail;
3466 key = entry_ptr->me_key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003467 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003468 di->di_pos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003469 di->len--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003470 Py_INCREF(key);
3471 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003472
3473fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003474 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003475 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003476 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003477}
3478
Raymond Hettinger019a1482004-03-18 02:41:19 +00003479PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003480 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3481 "dict_keyiterator", /* tp_name */
3482 sizeof(dictiterobject), /* tp_basicsize */
3483 0, /* tp_itemsize */
3484 /* methods */
3485 (destructor)dictiter_dealloc, /* tp_dealloc */
3486 0, /* tp_print */
3487 0, /* tp_getattr */
3488 0, /* tp_setattr */
3489 0, /* tp_reserved */
3490 0, /* tp_repr */
3491 0, /* tp_as_number */
3492 0, /* tp_as_sequence */
3493 0, /* tp_as_mapping */
3494 0, /* tp_hash */
3495 0, /* tp_call */
3496 0, /* tp_str */
3497 PyObject_GenericGetAttr, /* tp_getattro */
3498 0, /* tp_setattro */
3499 0, /* tp_as_buffer */
3500 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3501 0, /* tp_doc */
3502 (traverseproc)dictiter_traverse, /* tp_traverse */
3503 0, /* tp_clear */
3504 0, /* tp_richcompare */
3505 0, /* tp_weaklistoffset */
3506 PyObject_SelfIter, /* tp_iter */
3507 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
3508 dictiter_methods, /* tp_methods */
3509 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003510};
3511
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003512static PyObject *
3513dictiter_iternextvalue(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003514{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003515 PyObject *value;
INADA Naokica2d8be2016-11-04 16:59:10 +09003516 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003517 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003519 if (d == NULL)
3520 return NULL;
3521 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003523 if (di->di_used != d->ma_used) {
3524 PyErr_SetString(PyExc_RuntimeError,
3525 "dictionary changed size during iteration");
3526 di->di_used = -1; /* Make this state sticky */
3527 return NULL;
3528 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003530 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003531 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003532 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003533 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003534 goto fail;
INADA Naokica2d8be2016-11-04 16:59:10 +09003535 value = d->ma_values[i];
3536 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003537 }
3538 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003539 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003540 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3541 while (i < n && entry_ptr->me_value == NULL) {
3542 entry_ptr++;
3543 i++;
3544 }
3545 if (i >= n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003546 goto fail;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003547 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003548 }
3549 di->di_pos = i+1;
3550 di->len--;
3551 Py_INCREF(value);
3552 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003553
3554fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003555 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003556 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003557 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003558}
3559
3560PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003561 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3562 "dict_valueiterator", /* tp_name */
3563 sizeof(dictiterobject), /* tp_basicsize */
3564 0, /* tp_itemsize */
3565 /* methods */
3566 (destructor)dictiter_dealloc, /* tp_dealloc */
3567 0, /* tp_print */
3568 0, /* tp_getattr */
3569 0, /* tp_setattr */
3570 0, /* tp_reserved */
3571 0, /* tp_repr */
3572 0, /* tp_as_number */
3573 0, /* tp_as_sequence */
3574 0, /* tp_as_mapping */
3575 0, /* tp_hash */
3576 0, /* tp_call */
3577 0, /* tp_str */
3578 PyObject_GenericGetAttr, /* tp_getattro */
3579 0, /* tp_setattro */
3580 0, /* tp_as_buffer */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003581 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003582 0, /* tp_doc */
3583 (traverseproc)dictiter_traverse, /* tp_traverse */
3584 0, /* tp_clear */
3585 0, /* tp_richcompare */
3586 0, /* tp_weaklistoffset */
3587 PyObject_SelfIter, /* tp_iter */
3588 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3589 dictiter_methods, /* tp_methods */
3590 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003591};
3592
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003593static PyObject *
3594dictiter_iternextitem(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003595{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003596 PyObject *key, *value, *result;
INADA Naokica2d8be2016-11-04 16:59:10 +09003597 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003598 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003600 if (d == NULL)
3601 return NULL;
3602 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003604 if (di->di_used != d->ma_used) {
3605 PyErr_SetString(PyExc_RuntimeError,
3606 "dictionary changed size during iteration");
3607 di->di_used = -1; /* Make this state sticky */
3608 return NULL;
3609 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003611 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003612 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003613 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003614 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003615 goto fail;
3616 key = DK_ENTRIES(d->ma_keys)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003617 value = d->ma_values[i];
3618 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003619 }
3620 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003621 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003622 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3623 while (i < n && entry_ptr->me_value == NULL) {
3624 entry_ptr++;
3625 i++;
3626 }
3627 if (i >= n)
3628 goto fail;
3629 key = entry_ptr->me_key;
3630 value = entry_ptr->me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003631 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003632 di->di_pos = i+1;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003633 di->len--;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003634 Py_INCREF(key);
3635 Py_INCREF(value);
3636 result = di->di_result;
3637 if (Py_REFCNT(result) == 1) {
3638 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3639 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3640 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3641 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003642 Py_INCREF(result);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003643 Py_DECREF(oldkey);
3644 Py_DECREF(oldvalue);
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003645 }
3646 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003647 result = PyTuple_New(2);
3648 if (result == NULL)
3649 return NULL;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003650 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3651 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003652 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003653 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003654
3655fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003656 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003657 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003658 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003659}
3660
3661PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003662 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3663 "dict_itemiterator", /* tp_name */
3664 sizeof(dictiterobject), /* tp_basicsize */
3665 0, /* tp_itemsize */
3666 /* methods */
3667 (destructor)dictiter_dealloc, /* tp_dealloc */
3668 0, /* tp_print */
3669 0, /* tp_getattr */
3670 0, /* tp_setattr */
3671 0, /* tp_reserved */
3672 0, /* tp_repr */
3673 0, /* tp_as_number */
3674 0, /* tp_as_sequence */
3675 0, /* tp_as_mapping */
3676 0, /* tp_hash */
3677 0, /* tp_call */
3678 0, /* tp_str */
3679 PyObject_GenericGetAttr, /* tp_getattro */
3680 0, /* tp_setattro */
3681 0, /* tp_as_buffer */
3682 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3683 0, /* tp_doc */
3684 (traverseproc)dictiter_traverse, /* tp_traverse */
3685 0, /* tp_clear */
3686 0, /* tp_richcompare */
3687 0, /* tp_weaklistoffset */
3688 PyObject_SelfIter, /* tp_iter */
3689 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3690 dictiter_methods, /* tp_methods */
3691 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003692};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003693
3694
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003695/* dictreviter */
3696
3697static PyObject *
3698dictreviter_iternext(dictiterobject *di)
3699{
3700 PyDictObject *d = di->di_dict;
3701
3702 if (d == NULL) {
3703 return NULL;
3704 }
3705 assert (PyDict_Check(d));
3706
3707 if (di->di_used != d->ma_used) {
3708 PyErr_SetString(PyExc_RuntimeError,
3709 "dictionary changed size during iteration");
3710 di->di_used = -1; /* Make this state sticky */
3711 return NULL;
3712 }
3713
3714 Py_ssize_t i = di->di_pos;
3715 PyDictKeysObject *k = d->ma_keys;
3716 PyObject *key, *value, *result;
3717
3718 if (d->ma_values) {
3719 if (i < 0) {
3720 goto fail;
3721 }
3722 key = DK_ENTRIES(k)[i].me_key;
3723 value = d->ma_values[i];
3724 assert (value != NULL);
3725 }
3726 else {
3727 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
3728 while (i >= 0 && entry_ptr->me_value == NULL) {
3729 entry_ptr--;
3730 i--;
3731 }
3732 if (i < 0) {
3733 goto fail;
3734 }
3735 key = entry_ptr->me_key;
3736 value = entry_ptr->me_value;
3737 }
3738 di->di_pos = i-1;
3739 di->len--;
3740
3741 if (Py_TYPE(di) == &PyDictRevIterKey_Type) {
3742 Py_INCREF(key);
3743 return key;
3744 }
3745 else if (Py_TYPE(di) == &PyDictRevIterValue_Type) {
3746 Py_INCREF(value);
3747 return value;
3748 }
3749 else if (Py_TYPE(di) == &PyDictRevIterItem_Type) {
3750 Py_INCREF(key);
3751 Py_INCREF(value);
3752 result = di->di_result;
3753 if (Py_REFCNT(result) == 1) {
3754 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3755 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3756 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3757 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
3758 Py_INCREF(result);
3759 Py_DECREF(oldkey);
3760 Py_DECREF(oldvalue);
3761 }
3762 else {
3763 result = PyTuple_New(2);
3764 if (result == NULL) {
3765 return NULL;
3766 }
3767 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3768 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
3769 }
3770 return result;
3771 }
3772 else {
3773 Py_UNREACHABLE();
3774 }
3775
3776fail:
3777 di->di_dict = NULL;
3778 Py_DECREF(d);
3779 return NULL;
3780}
3781
3782PyTypeObject PyDictRevIterKey_Type = {
3783 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3784 "dict_reversekeyiterator",
3785 sizeof(dictiterobject),
3786 .tp_dealloc = (destructor)dictiter_dealloc,
3787 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
3788 .tp_traverse = (traverseproc)dictiter_traverse,
3789 .tp_iter = PyObject_SelfIter,
3790 .tp_iternext = (iternextfunc)dictreviter_iternext,
3791 .tp_methods = dictiter_methods
3792};
3793
3794
3795/*[clinic input]
3796dict.__reversed__
3797
3798Return a reverse iterator over the dict keys.
3799[clinic start generated code]*/
3800
3801static PyObject *
3802dict___reversed___impl(PyDictObject *self)
3803/*[clinic end generated code: output=e674483336d1ed51 input=23210ef3477d8c4d]*/
3804{
3805 assert (PyDict_Check(self));
3806 return dictiter_new(self, &PyDictRevIterKey_Type);
3807}
3808
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003809static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303810dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003811{
Sergey Fedoseev63958442018-10-20 05:43:33 +05003812 /* copy the iterator state */
3813 dictiterobject tmp = *di;
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003814 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003815
Sergey Fedoseev63958442018-10-20 05:43:33 +05003816 PyObject *list = PySequence_List((PyObject*)&tmp);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003817 Py_XDECREF(tmp.di_dict);
Sergey Fedoseev63958442018-10-20 05:43:33 +05003818 if (list == NULL) {
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003819 return NULL;
3820 }
Antoine Pitroua7013882012-04-05 00:04:20 +02003821 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003822}
3823
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003824PyTypeObject PyDictRevIterItem_Type = {
3825 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3826 "dict_reverseitemiterator",
3827 sizeof(dictiterobject),
3828 .tp_dealloc = (destructor)dictiter_dealloc,
3829 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
3830 .tp_traverse = (traverseproc)dictiter_traverse,
3831 .tp_iter = PyObject_SelfIter,
3832 .tp_iternext = (iternextfunc)dictreviter_iternext,
3833 .tp_methods = dictiter_methods
3834};
3835
3836PyTypeObject PyDictRevIterValue_Type = {
3837 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3838 "dict_reversevalueiterator",
3839 sizeof(dictiterobject),
3840 .tp_dealloc = (destructor)dictiter_dealloc,
3841 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
3842 .tp_traverse = (traverseproc)dictiter_traverse,
3843 .tp_iter = PyObject_SelfIter,
3844 .tp_iternext = (iternextfunc)dictreviter_iternext,
3845 .tp_methods = dictiter_methods
3846};
3847
Guido van Rossum3ac67412007-02-10 18:55:06 +00003848/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003849/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003850/***********************************************/
3851
Guido van Rossumb90c8482007-02-10 01:11:45 +00003852/* The instance lay-out is the same for all three; but the type differs. */
3853
Guido van Rossumb90c8482007-02-10 01:11:45 +00003854static void
Eric Snow96c6af92015-05-29 22:21:39 -06003855dictview_dealloc(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003856{
INADA Naokia6296d32017-08-24 14:55:17 +09003857 /* bpo-31095: UnTrack is needed before calling any callbacks */
3858 _PyObject_GC_UNTRACK(dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003859 Py_XDECREF(dv->dv_dict);
3860 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003861}
3862
3863static int
Eric Snow96c6af92015-05-29 22:21:39 -06003864dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003865{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003866 Py_VISIT(dv->dv_dict);
3867 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003868}
3869
Guido van Rossum83825ac2007-02-10 04:54:19 +00003870static Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003871dictview_len(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003872{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003873 Py_ssize_t len = 0;
3874 if (dv->dv_dict != NULL)
3875 len = dv->dv_dict->ma_used;
3876 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003877}
3878
Eric Snow96c6af92015-05-29 22:21:39 -06003879PyObject *
3880_PyDictView_New(PyObject *dict, PyTypeObject *type)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003881{
Eric Snow96c6af92015-05-29 22:21:39 -06003882 _PyDictViewObject *dv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003883 if (dict == NULL) {
3884 PyErr_BadInternalCall();
3885 return NULL;
3886 }
3887 if (!PyDict_Check(dict)) {
3888 /* XXX Get rid of this restriction later */
3889 PyErr_Format(PyExc_TypeError,
3890 "%s() requires a dict argument, not '%s'",
3891 type->tp_name, dict->ob_type->tp_name);
3892 return NULL;
3893 }
Eric Snow96c6af92015-05-29 22:21:39 -06003894 dv = PyObject_GC_New(_PyDictViewObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003895 if (dv == NULL)
3896 return NULL;
3897 Py_INCREF(dict);
3898 dv->dv_dict = (PyDictObject *)dict;
3899 _PyObject_GC_TRACK(dv);
3900 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003901}
3902
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003903/* TODO(guido): The views objects are not complete:
3904
3905 * support more set operations
3906 * support arbitrary mappings?
3907 - either these should be static or exported in dictobject.h
3908 - if public then they should probably be in builtins
3909*/
3910
Guido van Rossumaac530c2007-08-24 22:33:45 +00003911/* Return 1 if self is a subset of other, iterating over self;
3912 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003913static int
3914all_contained_in(PyObject *self, PyObject *other)
3915{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003916 PyObject *iter = PyObject_GetIter(self);
3917 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003919 if (iter == NULL)
3920 return -1;
3921 for (;;) {
3922 PyObject *next = PyIter_Next(iter);
3923 if (next == NULL) {
3924 if (PyErr_Occurred())
3925 ok = -1;
3926 break;
3927 }
3928 ok = PySequence_Contains(other, next);
3929 Py_DECREF(next);
3930 if (ok <= 0)
3931 break;
3932 }
3933 Py_DECREF(iter);
3934 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003935}
3936
3937static PyObject *
3938dictview_richcompare(PyObject *self, PyObject *other, int op)
3939{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003940 Py_ssize_t len_self, len_other;
3941 int ok;
3942 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003944 assert(self != NULL);
3945 assert(PyDictViewSet_Check(self));
3946 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00003947
Brian Curtindfc80e32011-08-10 20:28:54 -05003948 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
3949 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003951 len_self = PyObject_Size(self);
3952 if (len_self < 0)
3953 return NULL;
3954 len_other = PyObject_Size(other);
3955 if (len_other < 0)
3956 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003958 ok = 0;
3959 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00003960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003961 case Py_NE:
3962 case Py_EQ:
3963 if (len_self == len_other)
3964 ok = all_contained_in(self, other);
3965 if (op == Py_NE && ok >= 0)
3966 ok = !ok;
3967 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003968
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003969 case Py_LT:
3970 if (len_self < len_other)
3971 ok = all_contained_in(self, other);
3972 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003973
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003974 case Py_LE:
3975 if (len_self <= len_other)
3976 ok = all_contained_in(self, other);
3977 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003979 case Py_GT:
3980 if (len_self > len_other)
3981 ok = all_contained_in(other, self);
3982 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003984 case Py_GE:
3985 if (len_self >= len_other)
3986 ok = all_contained_in(other, self);
3987 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003989 }
3990 if (ok < 0)
3991 return NULL;
3992 result = ok ? Py_True : Py_False;
3993 Py_INCREF(result);
3994 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003995}
3996
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003997static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003998dictview_repr(_PyDictViewObject *dv)
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003999{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004000 PyObject *seq;
bennorthd7773d92018-01-26 15:46:01 +00004001 PyObject *result = NULL;
4002 Py_ssize_t rc;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004003
bennorthd7773d92018-01-26 15:46:01 +00004004 rc = Py_ReprEnter((PyObject *)dv);
4005 if (rc != 0) {
4006 return rc > 0 ? PyUnicode_FromString("...") : NULL;
4007 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004008 seq = PySequence_List((PyObject *)dv);
bennorthd7773d92018-01-26 15:46:01 +00004009 if (seq == NULL) {
4010 goto Done;
4011 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004012 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
4013 Py_DECREF(seq);
bennorthd7773d92018-01-26 15:46:01 +00004014
4015Done:
4016 Py_ReprLeave((PyObject *)dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004017 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004018}
4019
Guido van Rossum3ac67412007-02-10 18:55:06 +00004020/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004021
4022static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004023dictkeys_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004024{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004025 if (dv->dv_dict == NULL) {
4026 Py_RETURN_NONE;
4027 }
4028 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004029}
4030
4031static int
Eric Snow96c6af92015-05-29 22:21:39 -06004032dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004033{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004034 if (dv->dv_dict == NULL)
4035 return 0;
4036 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004037}
4038
Guido van Rossum83825ac2007-02-10 04:54:19 +00004039static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004040 (lenfunc)dictview_len, /* sq_length */
4041 0, /* sq_concat */
4042 0, /* sq_repeat */
4043 0, /* sq_item */
4044 0, /* sq_slice */
4045 0, /* sq_ass_item */
4046 0, /* sq_ass_slice */
4047 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004048};
4049
Guido van Rossum523259b2007-08-24 23:41:22 +00004050static PyObject*
4051dictviews_sub(PyObject* self, PyObject *other)
4052{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004053 PyObject *result = PySet_New(self);
4054 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004055 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004057 if (result == NULL)
4058 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004059
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08004060 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004061 if (tmp == NULL) {
4062 Py_DECREF(result);
4063 return NULL;
4064 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004066 Py_DECREF(tmp);
4067 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004068}
4069
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004070PyObject*
4071_PyDictView_Intersect(PyObject* self, PyObject *other)
Guido van Rossum523259b2007-08-24 23:41:22 +00004072{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004073 PyObject *result = PySet_New(self);
4074 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004075 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004077 if (result == NULL)
4078 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004079
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08004080 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_intersection_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004081 if (tmp == NULL) {
4082 Py_DECREF(result);
4083 return NULL;
4084 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004085
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004086 Py_DECREF(tmp);
4087 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004088}
4089
4090static PyObject*
4091dictviews_or(PyObject* self, PyObject *other)
4092{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004093 PyObject *result = PySet_New(self);
4094 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004095 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02004096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004097 if (result == NULL)
4098 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004099
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08004100 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004101 if (tmp == NULL) {
4102 Py_DECREF(result);
4103 return NULL;
4104 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004106 Py_DECREF(tmp);
4107 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004108}
4109
4110static PyObject*
4111dictviews_xor(PyObject* self, PyObject *other)
4112{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004113 PyObject *result = PySet_New(self);
4114 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004115 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004117 if (result == NULL)
4118 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004119
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08004120 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_symmetric_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004121 if (tmp == NULL) {
4122 Py_DECREF(result);
4123 return NULL;
4124 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004125
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004126 Py_DECREF(tmp);
4127 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004128}
4129
4130static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004131 0, /*nb_add*/
4132 (binaryfunc)dictviews_sub, /*nb_subtract*/
4133 0, /*nb_multiply*/
4134 0, /*nb_remainder*/
4135 0, /*nb_divmod*/
4136 0, /*nb_power*/
4137 0, /*nb_negative*/
4138 0, /*nb_positive*/
4139 0, /*nb_absolute*/
4140 0, /*nb_bool*/
4141 0, /*nb_invert*/
4142 0, /*nb_lshift*/
4143 0, /*nb_rshift*/
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004144 (binaryfunc)_PyDictView_Intersect, /*nb_and*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004145 (binaryfunc)dictviews_xor, /*nb_xor*/
4146 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00004147};
4148
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004149static PyObject*
4150dictviews_isdisjoint(PyObject *self, PyObject *other)
4151{
4152 PyObject *it;
4153 PyObject *item = NULL;
4154
4155 if (self == other) {
Eric Snow96c6af92015-05-29 22:21:39 -06004156 if (dictview_len((_PyDictViewObject *)self) == 0)
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004157 Py_RETURN_TRUE;
4158 else
4159 Py_RETURN_FALSE;
4160 }
4161
4162 /* Iterate over the shorter object (only if other is a set,
4163 * because PySequence_Contains may be expensive otherwise): */
4164 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
Eric Snow96c6af92015-05-29 22:21:39 -06004165 Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self);
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004166 Py_ssize_t len_other = PyObject_Size(other);
4167 if (len_other == -1)
4168 return NULL;
4169
4170 if ((len_other > len_self)) {
4171 PyObject *tmp = other;
4172 other = self;
4173 self = tmp;
4174 }
4175 }
4176
4177 it = PyObject_GetIter(other);
4178 if (it == NULL)
4179 return NULL;
4180
4181 while ((item = PyIter_Next(it)) != NULL) {
4182 int contains = PySequence_Contains(self, item);
4183 Py_DECREF(item);
4184 if (contains == -1) {
4185 Py_DECREF(it);
4186 return NULL;
4187 }
4188
4189 if (contains) {
4190 Py_DECREF(it);
4191 Py_RETURN_FALSE;
4192 }
4193 }
4194 Py_DECREF(it);
4195 if (PyErr_Occurred())
4196 return NULL; /* PyIter_Next raised an exception. */
4197 Py_RETURN_TRUE;
4198}
4199
4200PyDoc_STRVAR(isdisjoint_doc,
4201"Return True if the view and the given iterable have a null intersection.");
4202
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004203static PyObject* dictkeys_reversed(_PyDictViewObject *dv);
4204
4205PyDoc_STRVAR(reversed_keys_doc,
4206"Return a reverse iterator over the dict keys.");
4207
Guido van Rossumb90c8482007-02-10 01:11:45 +00004208static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004209 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4210 isdisjoint_doc},
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004211 {"__reversed__", (PyCFunction)dictkeys_reversed, METH_NOARGS,
4212 reversed_keys_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004213 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004214};
4215
4216PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004217 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4218 "dict_keys", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004219 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004220 0, /* tp_itemsize */
4221 /* methods */
4222 (destructor)dictview_dealloc, /* tp_dealloc */
4223 0, /* tp_print */
4224 0, /* tp_getattr */
4225 0, /* tp_setattr */
4226 0, /* tp_reserved */
4227 (reprfunc)dictview_repr, /* tp_repr */
4228 &dictviews_as_number, /* tp_as_number */
4229 &dictkeys_as_sequence, /* tp_as_sequence */
4230 0, /* tp_as_mapping */
4231 0, /* tp_hash */
4232 0, /* tp_call */
4233 0, /* tp_str */
4234 PyObject_GenericGetAttr, /* tp_getattro */
4235 0, /* tp_setattro */
4236 0, /* tp_as_buffer */
4237 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4238 0, /* tp_doc */
4239 (traverseproc)dictview_traverse, /* tp_traverse */
4240 0, /* tp_clear */
4241 dictview_richcompare, /* tp_richcompare */
4242 0, /* tp_weaklistoffset */
4243 (getiterfunc)dictkeys_iter, /* tp_iter */
4244 0, /* tp_iternext */
4245 dictkeys_methods, /* tp_methods */
4246 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004247};
4248
4249static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304250dictkeys_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004251{
Eric Snow96c6af92015-05-29 22:21:39 -06004252 return _PyDictView_New(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004253}
4254
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004255static PyObject *
4256dictkeys_reversed(_PyDictViewObject *dv)
4257{
4258 if (dv->dv_dict == NULL) {
4259 Py_RETURN_NONE;
4260 }
4261 return dictiter_new(dv->dv_dict, &PyDictRevIterKey_Type);
4262}
4263
Guido van Rossum3ac67412007-02-10 18:55:06 +00004264/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004265
4266static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004267dictitems_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004268{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004269 if (dv->dv_dict == NULL) {
4270 Py_RETURN_NONE;
4271 }
4272 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004273}
4274
4275static int
Eric Snow96c6af92015-05-29 22:21:39 -06004276dictitems_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004277{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004278 int result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004279 PyObject *key, *value, *found;
4280 if (dv->dv_dict == NULL)
4281 return 0;
4282 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
4283 return 0;
4284 key = PyTuple_GET_ITEM(obj, 0);
4285 value = PyTuple_GET_ITEM(obj, 1);
Raymond Hettinger6692f012016-09-18 21:46:08 -07004286 found = PyDict_GetItemWithError((PyObject *)dv->dv_dict, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004287 if (found == NULL) {
4288 if (PyErr_Occurred())
4289 return -1;
4290 return 0;
4291 }
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004292 Py_INCREF(found);
4293 result = PyObject_RichCompareBool(value, found, Py_EQ);
4294 Py_DECREF(found);
4295 return result;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004296}
4297
Guido van Rossum83825ac2007-02-10 04:54:19 +00004298static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004299 (lenfunc)dictview_len, /* sq_length */
4300 0, /* sq_concat */
4301 0, /* sq_repeat */
4302 0, /* sq_item */
4303 0, /* sq_slice */
4304 0, /* sq_ass_item */
4305 0, /* sq_ass_slice */
4306 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004307};
4308
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004309static PyObject* dictitems_reversed(_PyDictViewObject *dv);
4310
4311PyDoc_STRVAR(reversed_items_doc,
4312"Return a reverse iterator over the dict items.");
4313
Guido van Rossumb90c8482007-02-10 01:11:45 +00004314static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004315 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4316 isdisjoint_doc},
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004317 {"__reversed__", (PyCFunction)dictitems_reversed, METH_NOARGS,
4318 reversed_items_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004319 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004320};
4321
4322PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004323 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4324 "dict_items", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004325 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004326 0, /* tp_itemsize */
4327 /* methods */
4328 (destructor)dictview_dealloc, /* tp_dealloc */
4329 0, /* tp_print */
4330 0, /* tp_getattr */
4331 0, /* tp_setattr */
4332 0, /* tp_reserved */
4333 (reprfunc)dictview_repr, /* tp_repr */
4334 &dictviews_as_number, /* tp_as_number */
4335 &dictitems_as_sequence, /* tp_as_sequence */
4336 0, /* tp_as_mapping */
4337 0, /* tp_hash */
4338 0, /* tp_call */
4339 0, /* tp_str */
4340 PyObject_GenericGetAttr, /* tp_getattro */
4341 0, /* tp_setattro */
4342 0, /* tp_as_buffer */
4343 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4344 0, /* tp_doc */
4345 (traverseproc)dictview_traverse, /* tp_traverse */
4346 0, /* tp_clear */
4347 dictview_richcompare, /* tp_richcompare */
4348 0, /* tp_weaklistoffset */
4349 (getiterfunc)dictitems_iter, /* tp_iter */
4350 0, /* tp_iternext */
4351 dictitems_methods, /* tp_methods */
4352 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004353};
4354
4355static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304356dictitems_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004357{
Eric Snow96c6af92015-05-29 22:21:39 -06004358 return _PyDictView_New(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004359}
4360
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004361static PyObject *
4362dictitems_reversed(_PyDictViewObject *dv)
4363{
4364 if (dv->dv_dict == NULL) {
4365 Py_RETURN_NONE;
4366 }
4367 return dictiter_new(dv->dv_dict, &PyDictRevIterItem_Type);
4368}
4369
Guido van Rossum3ac67412007-02-10 18:55:06 +00004370/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004371
4372static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004373dictvalues_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004374{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004375 if (dv->dv_dict == NULL) {
4376 Py_RETURN_NONE;
4377 }
4378 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004379}
4380
Guido van Rossum83825ac2007-02-10 04:54:19 +00004381static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004382 (lenfunc)dictview_len, /* sq_length */
4383 0, /* sq_concat */
4384 0, /* sq_repeat */
4385 0, /* sq_item */
4386 0, /* sq_slice */
4387 0, /* sq_ass_item */
4388 0, /* sq_ass_slice */
4389 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004390};
4391
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004392static PyObject* dictvalues_reversed(_PyDictViewObject *dv);
4393
4394PyDoc_STRVAR(reversed_values_doc,
4395"Return a reverse iterator over the dict values.");
4396
Guido van Rossumb90c8482007-02-10 01:11:45 +00004397static PyMethodDef dictvalues_methods[] = {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004398 {"__reversed__", (PyCFunction)dictvalues_reversed, METH_NOARGS,
4399 reversed_values_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004400 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004401};
4402
4403PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004404 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4405 "dict_values", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004406 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004407 0, /* tp_itemsize */
4408 /* methods */
4409 (destructor)dictview_dealloc, /* tp_dealloc */
4410 0, /* tp_print */
4411 0, /* tp_getattr */
4412 0, /* tp_setattr */
4413 0, /* tp_reserved */
4414 (reprfunc)dictview_repr, /* tp_repr */
4415 0, /* tp_as_number */
4416 &dictvalues_as_sequence, /* tp_as_sequence */
4417 0, /* tp_as_mapping */
4418 0, /* tp_hash */
4419 0, /* tp_call */
4420 0, /* tp_str */
4421 PyObject_GenericGetAttr, /* tp_getattro */
4422 0, /* tp_setattro */
4423 0, /* tp_as_buffer */
4424 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4425 0, /* tp_doc */
4426 (traverseproc)dictview_traverse, /* tp_traverse */
4427 0, /* tp_clear */
4428 0, /* tp_richcompare */
4429 0, /* tp_weaklistoffset */
4430 (getiterfunc)dictvalues_iter, /* tp_iter */
4431 0, /* tp_iternext */
4432 dictvalues_methods, /* tp_methods */
4433 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004434};
4435
4436static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304437dictvalues_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004438{
Eric Snow96c6af92015-05-29 22:21:39 -06004439 return _PyDictView_New(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004440}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004441
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004442static PyObject *
4443dictvalues_reversed(_PyDictViewObject *dv)
4444{
4445 if (dv->dv_dict == NULL) {
4446 Py_RETURN_NONE;
4447 }
4448 return dictiter_new(dv->dv_dict, &PyDictRevIterValue_Type);
4449}
4450
4451
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004452/* Returns NULL if cannot allocate a new PyDictKeysObject,
4453 but does not set an error */
4454PyDictKeysObject *
4455_PyDict_NewKeysForClass(void)
4456{
Victor Stinner742da042016-09-07 17:40:12 -07004457 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004458 if (keys == NULL)
4459 PyErr_Clear();
4460 else
4461 keys->dk_lookup = lookdict_split;
4462 return keys;
4463}
4464
4465#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
4466
4467PyObject *
4468PyObject_GenericGetDict(PyObject *obj, void *context)
4469{
4470 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
4471 if (dictptr == NULL) {
4472 PyErr_SetString(PyExc_AttributeError,
4473 "This object has no __dict__");
4474 return NULL;
4475 }
4476 dict = *dictptr;
4477 if (dict == NULL) {
4478 PyTypeObject *tp = Py_TYPE(obj);
4479 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
INADA Naokia7576492018-11-14 18:39:27 +09004480 dictkeys_incref(CACHED_KEYS(tp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004481 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
4482 }
4483 else {
4484 *dictptr = dict = PyDict_New();
4485 }
4486 }
4487 Py_XINCREF(dict);
4488 return dict;
4489}
4490
4491int
4492_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
Victor Stinner742da042016-09-07 17:40:12 -07004493 PyObject *key, PyObject *value)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004494{
4495 PyObject *dict;
4496 int res;
4497 PyDictKeysObject *cached;
4498
4499 assert(dictptr != NULL);
4500 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
4501 assert(dictptr != NULL);
4502 dict = *dictptr;
4503 if (dict == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +09004504 dictkeys_incref(cached);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004505 dict = new_dict_with_shared_keys(cached);
4506 if (dict == NULL)
4507 return -1;
4508 *dictptr = dict;
4509 }
4510 if (value == NULL) {
4511 res = PyDict_DelItem(dict, key);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004512 // Since key sharing dict doesn't allow deletion, PyDict_DelItem()
4513 // always converts dict to combined form.
4514 if ((cached = CACHED_KEYS(tp)) != NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004515 CACHED_KEYS(tp) = NULL;
INADA Naokia7576492018-11-14 18:39:27 +09004516 dictkeys_decref(cached);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004517 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01004518 }
4519 else {
INADA Naoki2294f3a2017-02-12 13:51:30 +09004520 int was_shared = (cached == ((PyDictObject *)dict)->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004521 res = PyDict_SetItem(dict, key, value);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004522 if (was_shared &&
4523 (cached = CACHED_KEYS(tp)) != NULL &&
4524 cached != ((PyDictObject *)dict)->ma_keys) {
Victor Stinner3d3f2642016-12-15 17:21:23 +01004525 /* PyDict_SetItem() may call dictresize and convert split table
4526 * into combined table. In such case, convert it to split
4527 * table again and update type's shared key only when this is
4528 * the only dict sharing key with the type.
4529 *
4530 * This is to allow using shared key in class like this:
4531 *
4532 * class C:
4533 * def __init__(self):
4534 * # one dict resize happens
4535 * self.a, self.b, self.c = 1, 2, 3
4536 * self.d, self.e, self.f = 4, 5, 6
4537 * a = C()
4538 */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004539 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004540 CACHED_KEYS(tp) = make_keys_shared(dict);
Victor Stinner742da042016-09-07 17:40:12 -07004541 }
4542 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004543 CACHED_KEYS(tp) = NULL;
4544 }
INADA Naokia7576492018-11-14 18:39:27 +09004545 dictkeys_decref(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004546 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
4547 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004548 }
4549 }
4550 } else {
4551 dict = *dictptr;
4552 if (dict == NULL) {
4553 dict = PyDict_New();
4554 if (dict == NULL)
4555 return -1;
4556 *dictptr = dict;
4557 }
4558 if (value == NULL) {
4559 res = PyDict_DelItem(dict, key);
4560 } else {
4561 res = PyDict_SetItem(dict, key, value);
4562 }
4563 }
4564 return res;
4565}
4566
4567void
4568_PyDictKeys_DecRef(PyDictKeysObject *keys)
4569{
INADA Naokia7576492018-11-14 18:39:27 +09004570 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004571}