blob: 2f108bd48d00a2111c38017f137be1892d3b6978 [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 Stinnerbcda8f12018-11-21 22:27:47 +0100114#include "pycore_object.h"
Victor Stinner621cebe2018-11-12 16:53:38 +0100115#include "pycore_pystate.h"
Eric Snow96c6af92015-05-29 22:21:39 -0600116#include "dict-common.h"
Victor Stinner990397e2016-09-09 20:22:59 -0700117#include "stringlib/eq.h" /* to get unicode_eq() */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000118
Larry Hastings61272b72014-01-07 12:41:53 -0800119/*[clinic input]
Larry Hastingsc2047262014-01-25 20:43:29 -0800120class dict "PyDictObject *" "&PyDict_Type"
Larry Hastings61272b72014-01-07 12:41:53 -0800121[clinic start generated code]*/
Larry Hastings581ee362014-01-28 05:00:08 -0800122/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f157a5a0ce9589d6]*/
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800123
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400124
125/*
126To ensure the lookup algorithm terminates, there must be at least one Unused
127slot (NULL key) in the table.
128To avoid slowing down lookups on a near-full table, we resize the table when
129it's USABLE_FRACTION (currently two-thirds) full.
130*/
Guido van Rossum16e93a81997-01-28 00:00:11 +0000131
Tim Peterseb28ef22001-06-02 05:27:19 +0000132#define PERTURB_SHIFT 5
133
Guido van Rossum16e93a81997-01-28 00:00:11 +0000134/*
Tim Peterseb28ef22001-06-02 05:27:19 +0000135Major subtleties ahead: Most hash schemes depend on having a "good" hash
136function, in the sense of simulating randomness. Python doesn't: its most
R David Murray537ad7a2016-07-10 12:33:18 -0400137important hash functions (for ints) are very regular in common
Tim Peterseb28ef22001-06-02 05:27:19 +0000138cases:
Tim Peters15d49292001-05-27 07:39:22 +0000139
R David Murray537ad7a2016-07-10 12:33:18 -0400140 >>>[hash(i) for i in range(4)]
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000141 [0, 1, 2, 3]
Tim Peters15d49292001-05-27 07:39:22 +0000142
Tim Peterseb28ef22001-06-02 05:27:19 +0000143This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
144the low-order i bits as the initial table index is extremely fast, and there
R David Murray537ad7a2016-07-10 12:33:18 -0400145are no collisions at all for dicts indexed by a contiguous range of ints. So
146this gives better-than-random behavior in common cases, and that's very
147desirable.
Tim Peters15d49292001-05-27 07:39:22 +0000148
Tim Peterseb28ef22001-06-02 05:27:19 +0000149OTOH, when collisions occur, the tendency to fill contiguous slices of the
150hash table makes a good collision resolution strategy crucial. Taking only
151the last i bits of the hash code is also vulnerable: for example, consider
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152the list [i << 16 for i in range(20000)] as a set of keys. Since ints are
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000153their own hash codes, and this fits in a dict of size 2**15, the last 15 bits
154 of every hash code are all 0: they *all* map to the same table index.
Tim Peters15d49292001-05-27 07:39:22 +0000155
Tim Peterseb28ef22001-06-02 05:27:19 +0000156But catering to unusual cases should not slow the usual ones, so we just take
157the last i bits anyway. It's up to collision resolution to do the rest. If
158we *usually* find the key we're looking for on the first try (and, it turns
159out, we usually do -- the table load factor is kept under 2/3, so the odds
160are solidly in our favor), then it makes best sense to keep the initial index
161computation dirt cheap.
Tim Peters15d49292001-05-27 07:39:22 +0000162
Tim Peterseb28ef22001-06-02 05:27:19 +0000163The first half of collision resolution is to visit table indices via this
164recurrence:
Tim Peters15d49292001-05-27 07:39:22 +0000165
Tim Peterseb28ef22001-06-02 05:27:19 +0000166 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +0000167
Tim Peterseb28ef22001-06-02 05:27:19 +0000168For any initial j in range(2**i), repeating that 2**i times generates each
169int in range(2**i) exactly once (see any text on random-number generation for
170proof). By itself, this doesn't help much: like linear probing (setting
171j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
172order. This would be bad, except that's not the only thing we do, and it's
173actually *good* in the common cases where hash keys are consecutive. In an
174example that's really too small to make this entirely clear, for a table of
175size 2**3 the order of indices is:
Tim Peters15d49292001-05-27 07:39:22 +0000176
Tim Peterseb28ef22001-06-02 05:27:19 +0000177 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
178
179If two things come in at index 5, the first place we look after is index 2,
180not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
181Linear probing is deadly in this case because there the fixed probe order
182is the *same* as the order consecutive keys are likely to arrive. But it's
183extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
184and certain that consecutive hash codes do not.
185
186The other half of the strategy is to get the other bits of the hash code
187into play. This is done by initializing a (unsigned) vrbl "perturb" to the
188full hash code, and changing the recurrence to:
189
Tim Peterseb28ef22001-06-02 05:27:19 +0000190 perturb >>= PERTURB_SHIFT;
INADA Naoki267941c2016-10-06 15:19:07 +0900191 j = (5*j) + 1 + perturb;
Tim Peterseb28ef22001-06-02 05:27:19 +0000192 use j % 2**i as the next table index;
193
194Now the probe sequence depends (eventually) on every bit in the hash code,
195and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
196because it quickly magnifies small differences in the bits that didn't affect
197the initial index. Note that because perturb is unsigned, if the recurrence
198is executed often enough perturb eventually becomes and remains 0. At that
199point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
200that's certain to find an empty slot eventually (since it generates every int
201in range(2**i), and we make sure there's always at least one empty slot).
202
203Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
204small so that the high bits of the hash code continue to affect the probe
205sequence across iterations; but you want it large so that in really bad cases
206the high-order hash bits have an effect on early iterations. 5 was "the
207best" in minimizing total collisions across experiments Tim Peters ran (on
208both normal and pathological cases), but 4 and 6 weren't significantly worse.
209
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000210Historical: Reimer Behrends contributed the idea of using a polynomial-based
Tim Peterseb28ef22001-06-02 05:27:19 +0000211approach, using repeated multiplication by x in GF(2**n) where an irreducible
212polynomial for each table size was chosen such that x was a primitive root.
213Christian Tismer later extended that to use division by x instead, as an
214efficient way to get the high bits of the hash code into play. This scheme
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000215also gave excellent collision statistics, but was more expensive: two
216if-tests were required inside the loop; computing "the next" index took about
217the same number of operations but without as much potential parallelism
218(e.g., computing 5*j can go on at the same time as computing 1+perturb in the
219above, and then shifting perturb can be done while the table index is being
220masked); and the PyDictObject struct required a member to hold the table's
221polynomial. In Tim's experiments the current scheme ran faster, produced
222equally good collision statistics, needed less code & used less memory.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000223
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000224*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000225
Fred Drake1bff34a2000-08-31 19:31:38 +0000226/* forward declarations */
Victor Stinner742da042016-09-07 17:40:12 -0700227static Py_ssize_t lookdict(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900228 Py_hash_t hash, PyObject **value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700229static Py_ssize_t lookdict_unicode(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900230 Py_hash_t hash, PyObject **value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700231static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400232lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900233 Py_hash_t hash, PyObject **value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700234static Py_ssize_t lookdict_split(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900235 Py_hash_t hash, PyObject **value_addr);
Fred Drake1bff34a2000-08-31 19:31:38 +0000236
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400237static int dictresize(PyDictObject *mp, Py_ssize_t minused);
Tim Petersdea48ec2001-05-22 20:40:22 +0000238
INADA Naoki2aaf98c2018-09-26 12:59:00 +0900239static PyObject* dict_iter(PyDictObject *dict);
240
Benjamin Peterson3c569292016-09-08 13:16:41 -0700241/*Global counter used to set ma_version_tag field of dictionary.
Victor Stinner3b6a6b42016-09-08 12:51:24 -0700242 * It is incremented each time that a dictionary is created and each
243 * time that a dictionary is modified. */
244static uint64_t pydict_global_version = 0;
245
246#define DICT_NEXT_VERSION() (++pydict_global_version)
247
Victor Stinner742da042016-09-07 17:40:12 -0700248/* Dictionary reuse scheme to save calls to malloc and free */
Christian Heimes2202f872008-02-06 14:31:34 +0000249#ifndef PyDict_MAXFREELIST
250#define PyDict_MAXFREELIST 80
251#endif
252static PyDictObject *free_list[PyDict_MAXFREELIST];
253static int numfree = 0;
Victor Stinner742da042016-09-07 17:40:12 -0700254static PyDictKeysObject *keys_free_list[PyDict_MAXFREELIST];
255static int numfreekeys = 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000256
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300257#include "clinic/dictobject.c.h"
258
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100259int
260PyDict_ClearFreeList(void)
Christian Heimes77c02eb2008-02-09 02:18:51 +0000261{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 PyDictObject *op;
Victor Stinner742da042016-09-07 17:40:12 -0700263 int ret = numfree + numfreekeys;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 while (numfree) {
265 op = free_list[--numfree];
266 assert(PyDict_CheckExact(op));
267 PyObject_GC_Del(op);
268 }
Victor Stinner742da042016-09-07 17:40:12 -0700269 while (numfreekeys) {
270 PyObject_FREE(keys_free_list[--numfreekeys]);
271 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100272 return ret;
273}
274
David Malcolm49526f42012-06-22 14:55:41 -0400275/* Print summary info about the state of the optimized allocator */
276void
277_PyDict_DebugMallocStats(FILE *out)
278{
279 _PyDebugAllocatorStats(out,
280 "free PyDictObject", numfree, sizeof(PyDictObject));
281}
282
283
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100284void
285PyDict_Fini(void)
286{
287 PyDict_ClearFreeList();
Christian Heimes77c02eb2008-02-09 02:18:51 +0000288}
289
Victor Stinner742da042016-09-07 17:40:12 -0700290#define DK_SIZE(dk) ((dk)->dk_size)
291#if SIZEOF_VOID_P > 4
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700292#define DK_IXSIZE(dk) \
293 (DK_SIZE(dk) <= 0xff ? \
294 1 : DK_SIZE(dk) <= 0xffff ? \
295 2 : DK_SIZE(dk) <= 0xffffffff ? \
Benjamin Peterson3c569292016-09-08 13:16:41 -0700296 4 : sizeof(int64_t))
Victor Stinner742da042016-09-07 17:40:12 -0700297#else
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700298#define DK_IXSIZE(dk) \
299 (DK_SIZE(dk) <= 0xff ? \
300 1 : DK_SIZE(dk) <= 0xffff ? \
Benjamin Peterson3c569292016-09-08 13:16:41 -0700301 2 : sizeof(int32_t))
Victor Stinner742da042016-09-07 17:40:12 -0700302#endif
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700303#define DK_ENTRIES(dk) \
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700304 ((PyDictKeyEntry*)(&((int8_t*)((dk)->dk_indices))[DK_SIZE(dk) * DK_IXSIZE(dk)]))
Victor Stinner742da042016-09-07 17:40:12 -0700305
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400306#define DK_MASK(dk) (((dk)->dk_size)-1)
307#define IS_POWER_OF_2(x) (((x) & (x-1)) == 0)
308
INADA Naokia7576492018-11-14 18:39:27 +0900309static void free_keys_object(PyDictKeysObject *keys);
310
311static inline void
312dictkeys_incref(PyDictKeysObject *dk)
313{
314 _Py_INC_REFTOTAL;
315 dk->dk_refcnt++;
316}
317
318static inline void
319dictkeys_decref(PyDictKeysObject *dk)
320{
321 assert(dk->dk_refcnt > 0);
322 _Py_DEC_REFTOTAL;
323 if (--dk->dk_refcnt == 0) {
324 free_keys_object(dk);
325 }
326}
327
Victor Stinner742da042016-09-07 17:40:12 -0700328/* lookup indices. returns DKIX_EMPTY, DKIX_DUMMY, or ix >=0 */
Benjamin Peterson73222252016-09-08 09:58:47 -0700329static inline Py_ssize_t
INADA Naokia7576492018-11-14 18:39:27 +0900330dictkeys_get_index(PyDictKeysObject *keys, Py_ssize_t i)
Victor Stinner742da042016-09-07 17:40:12 -0700331{
332 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700333 Py_ssize_t ix;
334
Victor Stinner742da042016-09-07 17:40:12 -0700335 if (s <= 0xff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700336 int8_t *indices = (int8_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700337 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700338 }
339 else if (s <= 0xffff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700340 int16_t *indices = (int16_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700341 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700342 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700343#if SIZEOF_VOID_P > 4
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300344 else if (s > 0xffffffff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700345 int64_t *indices = (int64_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700346 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700347 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700348#endif
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300349 else {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700350 int32_t *indices = (int32_t*)(keys->dk_indices);
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300351 ix = indices[i];
352 }
Victor Stinner71211e32016-09-08 10:52:46 -0700353 assert(ix >= DKIX_DUMMY);
354 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700355}
356
357/* write to indices. */
Benjamin Peterson73222252016-09-08 09:58:47 -0700358static inline void
INADA Naokia7576492018-11-14 18:39:27 +0900359dictkeys_set_index(PyDictKeysObject *keys, Py_ssize_t i, Py_ssize_t ix)
Victor Stinner742da042016-09-07 17:40:12 -0700360{
361 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700362
363 assert(ix >= DKIX_DUMMY);
364
Victor Stinner742da042016-09-07 17:40:12 -0700365 if (s <= 0xff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700366 int8_t *indices = (int8_t*)(keys->dk_indices);
Victor Stinner71211e32016-09-08 10:52:46 -0700367 assert(ix <= 0x7f);
Victor Stinner208857e2016-09-08 11:35:46 -0700368 indices[i] = (char)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700369 }
370 else if (s <= 0xffff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700371 int16_t *indices = (int16_t*)(keys->dk_indices);
Victor Stinner71211e32016-09-08 10:52:46 -0700372 assert(ix <= 0x7fff);
Victor Stinner208857e2016-09-08 11:35:46 -0700373 indices[i] = (int16_t)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700374 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700375#if SIZEOF_VOID_P > 4
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300376 else if (s > 0xffffffff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700377 int64_t *indices = (int64_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700378 indices[i] = ix;
Victor Stinner742da042016-09-07 17:40:12 -0700379 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700380#endif
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300381 else {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700382 int32_t *indices = (int32_t*)(keys->dk_indices);
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300383 assert(ix <= 0x7fffffff);
384 indices[i] = (int32_t)ix;
385 }
Victor Stinner742da042016-09-07 17:40:12 -0700386}
387
388
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200389/* USABLE_FRACTION is the maximum dictionary load.
Victor Stinner742da042016-09-07 17:40:12 -0700390 * Increasing this ratio makes dictionaries more dense resulting in more
391 * collisions. Decreasing it improves sparseness at the expense of spreading
392 * indices over more cache lines and at the cost of total memory consumed.
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200393 *
394 * USABLE_FRACTION must obey the following:
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400395 * (0 < USABLE_FRACTION(n) < n) for all n >= 2
396 *
Victor Stinner742da042016-09-07 17:40:12 -0700397 * USABLE_FRACTION should be quick to calculate.
398 * Fractions around 1/2 to 2/3 seem to work well in practice.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400399 */
Victor Stinner742da042016-09-07 17:40:12 -0700400#define USABLE_FRACTION(n) (((n) << 1)/3)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400401
Victor Stinner742da042016-09-07 17:40:12 -0700402/* ESTIMATE_SIZE is reverse function of USABLE_FRACTION.
403 * This can be used to reserve enough size to insert n entries without
404 * resizing.
405 */
INADA Naoki92c50ee2016-11-22 00:57:02 +0900406#define ESTIMATE_SIZE(n) (((n)*3+1) >> 1)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400407
Victor Stinner742da042016-09-07 17:40:12 -0700408/* Alternative fraction that is otherwise close enough to 2n/3 to make
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400409 * little difference. 8 * 2/3 == 8 * 5/8 == 5. 16 * 2/3 == 16 * 5/8 == 10.
410 * 32 * 2/3 = 21, 32 * 5/8 = 20.
411 * Its advantage is that it is faster to compute on machines with slow division.
412 * #define USABLE_FRACTION(n) (((n) >> 1) + ((n) >> 2) - ((n) >> 3))
Victor Stinner742da042016-09-07 17:40:12 -0700413 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400414
Victor Stinnera9f61a52013-07-16 22:17:26 +0200415/* GROWTH_RATE. Growth rate upon hitting maximum load.
INADA Naoki5fbc5112018-04-17 15:53:34 +0900416 * Currently set to used*3.
Victor Stinnera9f61a52013-07-16 22:17:26 +0200417 * This means that dicts double in size when growing without deletions,
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700418 * but have more head room when the number of deletions is on a par with the
INADA Naoki5fbc5112018-04-17 15:53:34 +0900419 * number of insertions. See also bpo-17563 and bpo-33205.
420 *
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700421 * GROWTH_RATE was set to used*4 up to version 3.2.
422 * GROWTH_RATE was set to used*2 in version 3.3.0
INADA Naoki5fbc5112018-04-17 15:53:34 +0900423 * GROWTH_RATE was set to used*2 + capacity/2 in 3.4.0-3.6.0.
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200424 */
INADA Naoki5fbc5112018-04-17 15:53:34 +0900425#define GROWTH_RATE(d) ((d)->ma_used*3)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400426
427#define ENSURE_ALLOWS_DELETIONS(d) \
428 if ((d)->ma_keys->dk_lookup == lookdict_unicode_nodummy) { \
429 (d)->ma_keys->dk_lookup = lookdict_unicode; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400431
432/* This immutable, empty PyDictKeysObject is used for PyDict_Clear()
433 * (which cannot fail and thus can do no allocation).
434 */
435static PyDictKeysObject empty_keys_struct = {
Serhiy Storchaka97932e42016-09-26 23:01:23 +0300436 1, /* dk_refcnt */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400437 1, /* dk_size */
438 lookdict_split, /* dk_lookup */
439 0, /* dk_usable (immutable) */
Victor Stinner742da042016-09-07 17:40:12 -0700440 0, /* dk_nentries */
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700441 {DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY,
442 DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY}, /* dk_indices */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400443};
444
445static PyObject *empty_values[1] = { NULL };
446
447#define Py_EMPTY_KEYS &empty_keys_struct
448
Victor Stinner611b0fa2016-09-14 15:02:01 +0200449/* Uncomment to check the dict content in _PyDict_CheckConsistency() */
450/* #define DEBUG_PYDICT */
451
452
T. Woutersa00c3fd2017-03-31 09:14:41 -0700453#ifndef NDEBUG
Victor Stinner611b0fa2016-09-14 15:02:01 +0200454static int
455_PyDict_CheckConsistency(PyDictObject *mp)
456{
Victor Stinner50fe3f82018-10-26 18:47:15 +0200457#define ASSERT(expr) _PyObject_ASSERT((PyObject *)mp, (expr))
458
Victor Stinner611b0fa2016-09-14 15:02:01 +0200459 PyDictKeysObject *keys = mp->ma_keys;
460 int splitted = _PyDict_HasSplitTable(mp);
461 Py_ssize_t usable = USABLE_FRACTION(keys->dk_size);
462#ifdef DEBUG_PYDICT
463 PyDictKeyEntry *entries = DK_ENTRIES(keys);
464 Py_ssize_t i;
465#endif
466
Victor Stinner50fe3f82018-10-26 18:47:15 +0200467 ASSERT(0 <= mp->ma_used && mp->ma_used <= usable);
468 ASSERT(IS_POWER_OF_2(keys->dk_size));
469 ASSERT(0 <= keys->dk_usable
Victor Stinner611b0fa2016-09-14 15:02:01 +0200470 && keys->dk_usable <= usable);
Victor Stinner50fe3f82018-10-26 18:47:15 +0200471 ASSERT(0 <= keys->dk_nentries
Victor Stinner611b0fa2016-09-14 15:02:01 +0200472 && keys->dk_nentries <= usable);
Victor Stinner50fe3f82018-10-26 18:47:15 +0200473 ASSERT(keys->dk_usable + keys->dk_nentries <= usable);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200474
475 if (!splitted) {
476 /* combined table */
Victor Stinner50fe3f82018-10-26 18:47:15 +0200477 ASSERT(keys->dk_refcnt == 1);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200478 }
479
480#ifdef DEBUG_PYDICT
481 for (i=0; i < keys->dk_size; i++) {
INADA Naokia7576492018-11-14 18:39:27 +0900482 Py_ssize_t ix = dictkeys_get_index(keys, i);
Victor Stinner50fe3f82018-10-26 18:47:15 +0200483 ASSERT(DKIX_DUMMY <= ix && ix <= usable);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200484 }
485
486 for (i=0; i < usable; i++) {
487 PyDictKeyEntry *entry = &entries[i];
488 PyObject *key = entry->me_key;
489
490 if (key != NULL) {
491 if (PyUnicode_CheckExact(key)) {
492 Py_hash_t hash = ((PyASCIIObject *)key)->hash;
Victor Stinner50fe3f82018-10-26 18:47:15 +0200493 ASSERT(hash != -1);
494 ASSERT(entry->me_hash == hash);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200495 }
496 else {
497 /* test_dict fails if PyObject_Hash() is called again */
Victor Stinner50fe3f82018-10-26 18:47:15 +0200498 ASSERT(entry->me_hash != -1);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200499 }
500 if (!splitted) {
Victor Stinner50fe3f82018-10-26 18:47:15 +0200501 ASSERT(entry->me_value != NULL);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200502 }
503 }
504
505 if (splitted) {
Victor Stinner50fe3f82018-10-26 18:47:15 +0200506 ASSERT(entry->me_value == NULL);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200507 }
508 }
509
510 if (splitted) {
511 /* splitted table */
512 for (i=0; i < mp->ma_used; i++) {
Victor Stinner50fe3f82018-10-26 18:47:15 +0200513 ASSERT(mp->ma_values[i] != NULL);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200514 }
515 }
516#endif
517
518 return 1;
Victor Stinner50fe3f82018-10-26 18:47:15 +0200519
520#undef ASSERT
Victor Stinner611b0fa2016-09-14 15:02:01 +0200521}
522#endif
523
524
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400525static PyDictKeysObject *new_keys_object(Py_ssize_t size)
526{
527 PyDictKeysObject *dk;
Victor Stinner742da042016-09-07 17:40:12 -0700528 Py_ssize_t es, usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400529
Victor Stinner742da042016-09-07 17:40:12 -0700530 assert(size >= PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400531 assert(IS_POWER_OF_2(size));
Victor Stinner742da042016-09-07 17:40:12 -0700532
533 usable = USABLE_FRACTION(size);
534 if (size <= 0xff) {
535 es = 1;
536 }
537 else if (size <= 0xffff) {
538 es = 2;
539 }
540#if SIZEOF_VOID_P > 4
541 else if (size <= 0xffffffff) {
542 es = 4;
543 }
544#endif
545 else {
546 es = sizeof(Py_ssize_t);
547 }
548
549 if (size == PyDict_MINSIZE && numfreekeys > 0) {
550 dk = keys_free_list[--numfreekeys];
551 }
552 else {
Victor Stinner98ee9d52016-09-08 09:33:56 -0700553 dk = PyObject_MALLOC(sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -0700554 + es * size
555 + sizeof(PyDictKeyEntry) * usable);
Victor Stinner742da042016-09-07 17:40:12 -0700556 if (dk == NULL) {
557 PyErr_NoMemory();
558 return NULL;
559 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400560 }
INADA Naokia7576492018-11-14 18:39:27 +0900561 _Py_INC_REFTOTAL;
562 dk->dk_refcnt = 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400563 dk->dk_size = size;
Victor Stinner742da042016-09-07 17:40:12 -0700564 dk->dk_usable = usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400565 dk->dk_lookup = lookdict_unicode_nodummy;
Victor Stinner742da042016-09-07 17:40:12 -0700566 dk->dk_nentries = 0;
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700567 memset(&dk->dk_indices[0], 0xff, es * size);
Victor Stinner742da042016-09-07 17:40:12 -0700568 memset(DK_ENTRIES(dk), 0, sizeof(PyDictKeyEntry) * usable);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400569 return dk;
570}
571
572static void
573free_keys_object(PyDictKeysObject *keys)
574{
Victor Stinner742da042016-09-07 17:40:12 -0700575 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400576 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -0700577 for (i = 0, n = keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400578 Py_XDECREF(entries[i].me_key);
579 Py_XDECREF(entries[i].me_value);
580 }
Victor Stinner742da042016-09-07 17:40:12 -0700581 if (keys->dk_size == PyDict_MINSIZE && numfreekeys < PyDict_MAXFREELIST) {
582 keys_free_list[numfreekeys++] = keys;
583 return;
584 }
Raymond Hettingerce5179f2016-01-31 08:56:21 -0800585 PyObject_FREE(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400586}
587
588#define new_values(size) PyMem_NEW(PyObject *, size)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400589#define free_values(values) PyMem_FREE(values)
590
591/* Consumes a reference to the keys object */
592static PyObject *
593new_dict(PyDictKeysObject *keys, PyObject **values)
594{
595 PyDictObject *mp;
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200596 assert(keys != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 if (numfree) {
598 mp = free_list[--numfree];
599 assert (mp != NULL);
600 assert (Py_TYPE(mp) == &PyDict_Type);
601 _Py_NewReference((PyObject *)mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400603 else {
604 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
605 if (mp == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +0900606 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400607 free_values(values);
608 return NULL;
609 }
610 }
611 mp->ma_keys = keys;
612 mp->ma_values = values;
613 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -0700614 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +0200615 assert(_PyDict_CheckConsistency(mp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000617}
618
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400619/* Consumes a reference to the keys object */
620static PyObject *
621new_dict_with_shared_keys(PyDictKeysObject *keys)
622{
623 PyObject **values;
624 Py_ssize_t i, size;
625
Victor Stinner742da042016-09-07 17:40:12 -0700626 size = USABLE_FRACTION(DK_SIZE(keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400627 values = new_values(size);
628 if (values == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +0900629 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400630 return PyErr_NoMemory();
631 }
632 for (i = 0; i < size; i++) {
633 values[i] = NULL;
634 }
635 return new_dict(keys, values);
636}
637
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500638
639static PyObject *
640clone_combined_dict(PyDictObject *orig)
641{
642 assert(PyDict_CheckExact(orig));
643 assert(orig->ma_values == NULL);
644 assert(orig->ma_keys->dk_refcnt == 1);
645
646 Py_ssize_t keys_size = _PyDict_KeysSize(orig->ma_keys);
647 PyDictKeysObject *keys = PyObject_Malloc(keys_size);
648 if (keys == NULL) {
649 PyErr_NoMemory();
650 return NULL;
651 }
652
653 memcpy(keys, orig->ma_keys, keys_size);
654
655 /* After copying key/value pairs, we need to incref all
656 keys and values and they are about to be co-owned by a
657 new dict object. */
658 PyDictKeyEntry *ep0 = DK_ENTRIES(keys);
659 Py_ssize_t n = keys->dk_nentries;
660 for (Py_ssize_t i = 0; i < n; i++) {
661 PyDictKeyEntry *entry = &ep0[i];
662 PyObject *value = entry->me_value;
663 if (value != NULL) {
664 Py_INCREF(value);
665 Py_INCREF(entry->me_key);
666 }
667 }
668
669 PyDictObject *new = (PyDictObject *)new_dict(keys, NULL);
670 if (new == NULL) {
671 /* In case of an error, `new_dict()` takes care of
672 cleaning up `keys`. */
673 return NULL;
674 }
675 new->ma_used = orig->ma_used;
676 assert(_PyDict_CheckConsistency(new));
677 if (_PyObject_GC_IS_TRACKED(orig)) {
678 /* Maintain tracking. */
679 _PyObject_GC_TRACK(new);
680 }
Yury Selivanov0b752282018-07-06 12:20:07 -0400681
682 /* Since we copied the keys table we now have an extra reference
683 in the system. Manually call _Py_INC_REFTOTAL to signal that
INADA Naokia7576492018-11-14 18:39:27 +0900684 we have it now; calling dictkeys_incref would be an error as
Yury Selivanov0b752282018-07-06 12:20:07 -0400685 keys->dk_refcnt is already set to 1 (after memcpy). */
686 _Py_INC_REFTOTAL;
687
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500688 return (PyObject *)new;
689}
690
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400691PyObject *
692PyDict_New(void)
693{
Victor Stinner742da042016-09-07 17:40:12 -0700694 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200695 if (keys == NULL)
696 return NULL;
697 return new_dict(keys, NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400698}
699
Victor Stinner742da042016-09-07 17:40:12 -0700700/* Search index of hash table from offset of entry table */
701static Py_ssize_t
702lookdict_index(PyDictKeysObject *k, Py_hash_t hash, Py_ssize_t index)
703{
Victor Stinner742da042016-09-07 17:40:12 -0700704 size_t mask = DK_MASK(k);
INADA Naoki073ae482017-06-23 15:22:50 +0900705 size_t perturb = (size_t)hash;
706 size_t i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700707
INADA Naoki073ae482017-06-23 15:22:50 +0900708 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900709 Py_ssize_t ix = dictkeys_get_index(k, i);
Victor Stinner742da042016-09-07 17:40:12 -0700710 if (ix == index) {
711 return i;
712 }
713 if (ix == DKIX_EMPTY) {
714 return DKIX_EMPTY;
715 }
INADA Naoki073ae482017-06-23 15:22:50 +0900716 perturb >>= PERTURB_SHIFT;
717 i = mask & (i*5 + perturb + 1);
Victor Stinner742da042016-09-07 17:40:12 -0700718 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700719 Py_UNREACHABLE();
Victor Stinner742da042016-09-07 17:40:12 -0700720}
721
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000722/*
723The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000724This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000725Open addressing is preferred over chaining since the link overhead for
726chaining would be substantial (100% with typical malloc overhead).
727
Tim Peterseb28ef22001-06-02 05:27:19 +0000728The initial probe index is computed as hash mod the table size. Subsequent
729probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000730
731All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000732
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000733The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000734contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000735Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000736
Victor Stinner742da042016-09-07 17:40:12 -0700737lookdict() is general-purpose, and may return DKIX_ERROR if (and only if) a
Victor Stinnera4348cc2016-09-08 12:01:25 -0700738comparison raises an exception.
Guido van Rossum89d8c602007-09-18 17:26:56 +0000739lookdict_unicode() below is specialized to string keys, comparison of which can
INADA Naoki1b8df102017-02-20 22:48:10 +0900740never raise an exception; that function can never return DKIX_ERROR when key
741is string. Otherwise, it falls back to lookdict().
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400742lookdict_unicode_nodummy is further specialized for string keys that cannot be
743the <dummy> value.
INADA Naoki778928b2017-08-03 23:45:15 +0900744For both, when the key isn't found a DKIX_EMPTY is returned.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000745*/
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100746static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400747lookdict(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900748 Py_hash_t hash, PyObject **value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000749{
INADA Naoki778928b2017-08-03 23:45:15 +0900750 size_t i, mask, perturb;
Victor Stinner742da042016-09-07 17:40:12 -0700751 PyDictKeysObject *dk;
INADA Naoki778928b2017-08-03 23:45:15 +0900752 PyDictKeyEntry *ep0;
Tim Peterseb28ef22001-06-02 05:27:19 +0000753
Antoine Pitrou9a234902012-05-13 20:48:01 +0200754top:
Victor Stinner742da042016-09-07 17:40:12 -0700755 dk = mp->ma_keys;
Victor Stinner742da042016-09-07 17:40:12 -0700756 ep0 = DK_ENTRIES(dk);
INADA Naoki778928b2017-08-03 23:45:15 +0900757 mask = DK_MASK(dk);
758 perturb = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700760
INADA Naoki778928b2017-08-03 23:45:15 +0900761 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900762 Py_ssize_t ix = dictkeys_get_index(dk, i);
Victor Stinner742da042016-09-07 17:40:12 -0700763 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700764 *value_addr = NULL;
765 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400766 }
INADA Naoki778928b2017-08-03 23:45:15 +0900767 if (ix >= 0) {
768 PyDictKeyEntry *ep = &ep0[ix];
769 assert(ep->me_key != NULL);
770 if (ep->me_key == key) {
771 *value_addr = ep->me_value;
772 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700773 }
INADA Naoki778928b2017-08-03 23:45:15 +0900774 if (ep->me_hash == hash) {
775 PyObject *startkey = ep->me_key;
776 Py_INCREF(startkey);
777 int cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
778 Py_DECREF(startkey);
779 if (cmp < 0) {
780 *value_addr = NULL;
781 return DKIX_ERROR;
782 }
783 if (dk == mp->ma_keys && ep->me_key == startkey) {
784 if (cmp > 0) {
785 *value_addr = ep->me_value;
786 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700787 }
INADA Naoki778928b2017-08-03 23:45:15 +0900788 }
789 else {
790 /* The dict was mutated, restart */
791 goto top;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400792 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 }
INADA Naoki778928b2017-08-03 23:45:15 +0900795 perturb >>= PERTURB_SHIFT;
796 i = (i*5 + perturb + 1) & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700798 Py_UNREACHABLE();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000799}
800
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400801/* Specialized version for string-only keys */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100802static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400803lookdict_unicode(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900804 Py_hash_t hash, PyObject **value_addr)
Fred Drake1bff34a2000-08-31 19:31:38 +0000805{
Victor Stinner742da042016-09-07 17:40:12 -0700806 assert(mp->ma_values == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 /* Make sure this function doesn't have to handle non-unicode keys,
808 including subclasses of str; e.g., one reason to subclass
809 unicodes is to override __eq__, and for speed we don't cater to
810 that here. */
811 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400812 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900813 return lookdict(mp, key, hash, value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 }
Tim Peters15d49292001-05-27 07:39:22 +0000815
INADA Naoki778928b2017-08-03 23:45:15 +0900816 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
817 size_t mask = DK_MASK(mp->ma_keys);
818 size_t perturb = (size_t)hash;
819 size_t i = (size_t)hash & mask;
820
821 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900822 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
Victor Stinner742da042016-09-07 17:40:12 -0700823 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700824 *value_addr = NULL;
825 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400826 }
INADA Naoki778928b2017-08-03 23:45:15 +0900827 if (ix >= 0) {
828 PyDictKeyEntry *ep = &ep0[ix];
829 assert(ep->me_key != NULL);
830 assert(PyUnicode_CheckExact(ep->me_key));
831 if (ep->me_key == key ||
832 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
833 *value_addr = ep->me_value;
834 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700835 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400836 }
INADA Naoki778928b2017-08-03 23:45:15 +0900837 perturb >>= PERTURB_SHIFT;
838 i = mask & (i*5 + perturb + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700840 Py_UNREACHABLE();
Fred Drake1bff34a2000-08-31 19:31:38 +0000841}
842
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400843/* Faster version of lookdict_unicode when it is known that no <dummy> keys
844 * will be present. */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100845static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400846lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900847 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400848{
Victor Stinner742da042016-09-07 17:40:12 -0700849 assert(mp->ma_values == NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400850 /* Make sure this function doesn't have to handle non-unicode keys,
851 including subclasses of str; e.g., one reason to subclass
852 unicodes is to override __eq__, and for speed we don't cater to
853 that here. */
854 if (!PyUnicode_CheckExact(key)) {
855 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900856 return lookdict(mp, key, hash, value_addr);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400857 }
INADA Naoki778928b2017-08-03 23:45:15 +0900858
859 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
860 size_t mask = DK_MASK(mp->ma_keys);
861 size_t perturb = (size_t)hash;
862 size_t i = (size_t)hash & mask;
863
864 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900865 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
Victor Stinner742da042016-09-07 17:40:12 -0700866 assert (ix != DKIX_DUMMY);
867 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700868 *value_addr = NULL;
869 return DKIX_EMPTY;
870 }
INADA Naoki778928b2017-08-03 23:45:15 +0900871 PyDictKeyEntry *ep = &ep0[ix];
872 assert(ep->me_key != NULL);
873 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700874 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400875 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900876 *value_addr = ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700877 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400878 }
INADA Naoki778928b2017-08-03 23:45:15 +0900879 perturb >>= PERTURB_SHIFT;
880 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400881 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700882 Py_UNREACHABLE();
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400883}
884
885/* Version of lookdict for split tables.
886 * All split tables and only split tables use this lookup function.
887 * Split tables only contain unicode keys and no dummy keys,
888 * so algorithm is the same as lookdict_unicode_nodummy.
889 */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100890static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400891lookdict_split(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900892 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400893{
Victor Stinner742da042016-09-07 17:40:12 -0700894 /* mp must split table */
895 assert(mp->ma_values != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400896 if (!PyUnicode_CheckExact(key)) {
INADA Naoki778928b2017-08-03 23:45:15 +0900897 Py_ssize_t ix = lookdict(mp, key, hash, value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700898 if (ix >= 0) {
INADA Naokiba609772016-12-07 20:41:42 +0900899 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700900 }
901 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400902 }
Victor Stinner742da042016-09-07 17:40:12 -0700903
INADA Naoki778928b2017-08-03 23:45:15 +0900904 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
905 size_t mask = DK_MASK(mp->ma_keys);
906 size_t perturb = (size_t)hash;
907 size_t i = (size_t)hash & mask;
908
909 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900910 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
INADA Naoki778928b2017-08-03 23:45:15 +0900911 assert (ix != DKIX_DUMMY);
Victor Stinner742da042016-09-07 17:40:12 -0700912 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700913 *value_addr = NULL;
914 return DKIX_EMPTY;
915 }
INADA Naoki778928b2017-08-03 23:45:15 +0900916 PyDictKeyEntry *ep = &ep0[ix];
917 assert(ep->me_key != NULL);
918 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700919 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400920 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900921 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700922 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400923 }
INADA Naoki778928b2017-08-03 23:45:15 +0900924 perturb >>= PERTURB_SHIFT;
925 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400926 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700927 Py_UNREACHABLE();
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400928}
929
Benjamin Petersonfb886362010-04-24 18:21:17 +0000930int
931_PyDict_HasOnlyStringKeys(PyObject *dict)
932{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 Py_ssize_t pos = 0;
934 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000935 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400937 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 return 1;
939 while (PyDict_Next(dict, &pos, &key, &value))
940 if (!PyUnicode_Check(key))
941 return 0;
942 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000943}
944
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000945#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 do { \
947 if (!_PyObject_GC_IS_TRACKED(mp)) { \
948 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
949 _PyObject_GC_MAY_BE_TRACKED(value)) { \
950 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 } \
952 } \
953 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000954
955void
956_PyDict_MaybeUntrack(PyObject *op)
957{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 PyDictObject *mp;
959 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -0700960 Py_ssize_t i, numentries;
961 PyDictKeyEntry *ep0;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
964 return;
965
966 mp = (PyDictObject *) op;
Victor Stinner742da042016-09-07 17:40:12 -0700967 ep0 = DK_ENTRIES(mp->ma_keys);
968 numentries = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400969 if (_PyDict_HasSplitTable(mp)) {
Victor Stinner742da042016-09-07 17:40:12 -0700970 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400971 if ((value = mp->ma_values[i]) == NULL)
972 continue;
973 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
Victor Stinner742da042016-09-07 17:40:12 -0700974 assert(!_PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400975 return;
976 }
977 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400979 else {
Victor Stinner742da042016-09-07 17:40:12 -0700980 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400981 if ((value = ep0[i].me_value) == NULL)
982 continue;
983 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
984 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
985 return;
986 }
987 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000989}
990
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400991/* Internal function to find slot for an item from its hash
Victor Stinner3c336c52016-09-12 14:17:40 +0200992 when it is known that the key is not present in the dict.
993
994 The dict must be combined. */
INADA Naokiba609772016-12-07 20:41:42 +0900995static Py_ssize_t
INADA Naoki778928b2017-08-03 23:45:15 +0900996find_empty_slot(PyDictKeysObject *keys, Py_hash_t hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000997{
INADA Naoki778928b2017-08-03 23:45:15 +0900998 assert(keys != NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000999
INADA Naoki778928b2017-08-03 23:45:15 +09001000 const size_t mask = DK_MASK(keys);
1001 size_t i = hash & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001002 Py_ssize_t ix = dictkeys_get_index(keys, i);
INADA Naoki778928b2017-08-03 23:45:15 +09001003 for (size_t perturb = hash; ix >= 0;) {
INADA Naoki267941c2016-10-06 15:19:07 +09001004 perturb >>= PERTURB_SHIFT;
INADA Naoki778928b2017-08-03 23:45:15 +09001005 i = (i*5 + perturb + 1) & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001006 ix = dictkeys_get_index(keys, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 }
INADA Naoki778928b2017-08-03 23:45:15 +09001008 return i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001009}
1010
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001011static int
1012insertion_resize(PyDictObject *mp)
1013{
Raymond Hettinger36f74aa2013-05-17 03:01:13 -07001014 return dictresize(mp, GROWTH_RATE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001015}
Antoine Pitroue965d972012-02-27 00:45:12 +01001016
1017/*
1018Internal routine to insert a new item into the table.
1019Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +01001020Returns -1 if an error occurred, or 0 on success.
1021*/
1022static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001023insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001024{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001025 PyObject *old_value;
INADA Naokiba609772016-12-07 20:41:42 +09001026 PyDictKeyEntry *ep;
Antoine Pitroue965d972012-02-27 00:45:12 +01001027
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001028 Py_INCREF(key);
1029 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001030 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
1031 if (insertion_resize(mp) < 0)
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001032 goto Fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001033 }
1034
INADA Naoki778928b2017-08-03 23:45:15 +09001035 Py_ssize_t ix = mp->ma_keys->dk_lookup(mp, key, hash, &old_value);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001036 if (ix == DKIX_ERROR)
1037 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001038
Antoine Pitroud6967322014-10-18 00:35:00 +02001039 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001040 MAINTAIN_TRACKING(mp, key, value);
Victor Stinner742da042016-09-07 17:40:12 -07001041
1042 /* When insertion order is different from shared key, we can't share
1043 * the key anymore. Convert this instance to combine table.
1044 */
1045 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09001046 ((ix >= 0 && old_value == NULL && mp->ma_used != ix) ||
Victor Stinner742da042016-09-07 17:40:12 -07001047 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001048 if (insertion_resize(mp) < 0)
1049 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001050 ix = DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001051 }
Victor Stinner742da042016-09-07 17:40:12 -07001052
1053 if (ix == DKIX_EMPTY) {
1054 /* Insert into new slot. */
INADA Naokiba609772016-12-07 20:41:42 +09001055 assert(old_value == NULL);
Victor Stinner742da042016-09-07 17:40:12 -07001056 if (mp->ma_keys->dk_usable <= 0) {
1057 /* Need to resize. */
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001058 if (insertion_resize(mp) < 0)
1059 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001060 }
INADA Naoki778928b2017-08-03 23:45:15 +09001061 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naokiba609772016-12-07 20:41:42 +09001062 ep = &DK_ENTRIES(mp->ma_keys)[mp->ma_keys->dk_nentries];
INADA Naokia7576492018-11-14 18:39:27 +09001063 dictkeys_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Victor Stinner742da042016-09-07 17:40:12 -07001064 ep->me_key = key;
1065 ep->me_hash = hash;
1066 if (mp->ma_values) {
1067 assert (mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
1068 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001069 }
1070 else {
Victor Stinner742da042016-09-07 17:40:12 -07001071 ep->me_value = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001072 }
1073 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001074 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07001075 mp->ma_keys->dk_usable--;
1076 mp->ma_keys->dk_nentries++;
1077 assert(mp->ma_keys->dk_usable >= 0);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001078 assert(_PyDict_CheckConsistency(mp));
Victor Stinner742da042016-09-07 17:40:12 -07001079 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001080 }
Victor Stinner742da042016-09-07 17:40:12 -07001081
INADA Naokiba609772016-12-07 20:41:42 +09001082 if (_PyDict_HasSplitTable(mp)) {
1083 mp->ma_values[ix] = value;
1084 if (old_value == NULL) {
1085 /* pending state */
1086 assert(ix == mp->ma_used);
1087 mp->ma_used++;
1088 }
1089 }
1090 else {
1091 assert(old_value != NULL);
1092 DK_ENTRIES(mp->ma_keys)[ix].me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07001093 }
1094
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001095 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokiba609772016-12-07 20:41:42 +09001096 Py_XDECREF(old_value); /* which **CAN** re-enter (see issue #22653) */
Victor Stinner611b0fa2016-09-14 15:02:01 +02001097 assert(_PyDict_CheckConsistency(mp));
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001098 Py_DECREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001099 return 0;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001100
1101Fail:
1102 Py_DECREF(value);
1103 Py_DECREF(key);
1104 return -1;
Antoine Pitroue965d972012-02-27 00:45:12 +01001105}
1106
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001107/*
luzpaza5293b42017-11-05 07:37:50 -06001108Internal routine used by dictresize() to build a hashtable of entries.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001109*/
1110static void
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001111build_indices(PyDictKeysObject *keys, PyDictKeyEntry *ep, Py_ssize_t n)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001112{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001113 size_t mask = (size_t)DK_SIZE(keys) - 1;
1114 for (Py_ssize_t ix = 0; ix != n; ix++, ep++) {
1115 Py_hash_t hash = ep->me_hash;
1116 size_t i = hash & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001117 for (size_t perturb = hash; dictkeys_get_index(keys, i) != DKIX_EMPTY;) {
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001118 perturb >>= PERTURB_SHIFT;
INADA Naoki870c2862017-06-24 09:03:19 +09001119 i = mask & (i*5 + perturb + 1);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001120 }
INADA Naokia7576492018-11-14 18:39:27 +09001121 dictkeys_set_index(keys, i, ix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001123}
1124
1125/*
1126Restructure the table by allocating a new table and reinserting all
1127items again. When entries have been deleted, the new table may
1128actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001129If a table is split (its keys and hashes are shared, its values are not),
1130then the values are temporarily copied into the table, it is resized as
1131a combined table, then the me_value slots in the old table are NULLed out.
1132After resizing a table is always combined,
1133but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001134*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001135static int
Victor Stinner3d3f2642016-12-15 17:21:23 +01001136dictresize(PyDictObject *mp, Py_ssize_t minsize)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001137{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001138 Py_ssize_t newsize, numentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001139 PyDictKeysObject *oldkeys;
1140 PyObject **oldvalues;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001141 PyDictKeyEntry *oldentries, *newentries;
Tim Peters91a364d2001-05-19 07:04:38 +00001142
Victor Stinner742da042016-09-07 17:40:12 -07001143 /* Find the smallest table size > minused. */
1144 for (newsize = PyDict_MINSIZE;
Victor Stinner3d3f2642016-12-15 17:21:23 +01001145 newsize < minsize && newsize > 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 newsize <<= 1)
1147 ;
1148 if (newsize <= 0) {
1149 PyErr_NoMemory();
1150 return -1;
1151 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001152
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001153 oldkeys = mp->ma_keys;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001154
1155 /* NOTE: Current odict checks mp->ma_keys to detect resize happen.
1156 * So we can't reuse oldkeys even if oldkeys->dk_size == newsize.
1157 * TODO: Try reusing oldkeys when reimplement odict.
1158 */
1159
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001160 /* Allocate a new table. */
1161 mp->ma_keys = new_keys_object(newsize);
1162 if (mp->ma_keys == NULL) {
1163 mp->ma_keys = oldkeys;
1164 return -1;
1165 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01001166 // New table must be large enough.
1167 assert(mp->ma_keys->dk_usable >= mp->ma_used);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001168 if (oldkeys->dk_lookup == lookdict)
1169 mp->ma_keys->dk_lookup = lookdict;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001170
1171 numentries = mp->ma_used;
1172 oldentries = DK_ENTRIES(oldkeys);
1173 newentries = DK_ENTRIES(mp->ma_keys);
1174 oldvalues = mp->ma_values;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001175 if (oldvalues != NULL) {
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001176 /* Convert split table into new combined table.
1177 * We must incref keys; we can transfer values.
1178 * Note that values of split table is always dense.
1179 */
1180 for (Py_ssize_t i = 0; i < numentries; i++) {
1181 assert(oldvalues[i] != NULL);
1182 PyDictKeyEntry *ep = &oldentries[i];
1183 PyObject *key = ep->me_key;
1184 Py_INCREF(key);
1185 newentries[i].me_key = key;
1186 newentries[i].me_hash = ep->me_hash;
1187 newentries[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001189
INADA Naokia7576492018-11-14 18:39:27 +09001190 dictkeys_decref(oldkeys);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001191 mp->ma_values = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001192 if (oldvalues != empty_values) {
1193 free_values(oldvalues);
1194 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001195 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001196 else { // combined table.
1197 if (oldkeys->dk_nentries == numentries) {
1198 memcpy(newentries, oldentries, numentries * sizeof(PyDictKeyEntry));
1199 }
1200 else {
1201 PyDictKeyEntry *ep = oldentries;
1202 for (Py_ssize_t i = 0; i < numentries; i++) {
1203 while (ep->me_value == NULL)
1204 ep++;
1205 newentries[i] = *ep++;
1206 }
1207 }
1208
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001209 assert(oldkeys->dk_lookup != lookdict_split);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001210 assert(oldkeys->dk_refcnt == 1);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001211 if (oldkeys->dk_size == PyDict_MINSIZE &&
1212 numfreekeys < PyDict_MAXFREELIST) {
INADA Naokia7576492018-11-14 18:39:27 +09001213 _Py_DEC_REFTOTAL;
1214 keys_free_list[numfreekeys++] = oldkeys;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001215 }
1216 else {
INADA Naokia7576492018-11-14 18:39:27 +09001217 _Py_DEC_REFTOTAL;
1218 PyObject_FREE(oldkeys);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001219 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001220 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001221
1222 build_indices(mp->ma_keys, newentries, numentries);
1223 mp->ma_keys->dk_usable -= numentries;
1224 mp->ma_keys->dk_nentries = numentries;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001226}
1227
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001228/* Returns NULL if unable to split table.
1229 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001230static PyDictKeysObject *
1231make_keys_shared(PyObject *op)
1232{
1233 Py_ssize_t i;
1234 Py_ssize_t size;
1235 PyDictObject *mp = (PyDictObject *)op;
1236
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001237 if (!PyDict_CheckExact(op))
1238 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001239 if (!_PyDict_HasSplitTable(mp)) {
1240 PyDictKeyEntry *ep0;
1241 PyObject **values;
1242 assert(mp->ma_keys->dk_refcnt == 1);
1243 if (mp->ma_keys->dk_lookup == lookdict) {
1244 return NULL;
1245 }
1246 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
1247 /* Remove dummy keys */
1248 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
1249 return NULL;
1250 }
1251 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
1252 /* Copy values into a new array */
Victor Stinner742da042016-09-07 17:40:12 -07001253 ep0 = DK_ENTRIES(mp->ma_keys);
1254 size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001255 values = new_values(size);
1256 if (values == NULL) {
1257 PyErr_SetString(PyExc_MemoryError,
1258 "Not enough memory to allocate new values array");
1259 return NULL;
1260 }
1261 for (i = 0; i < size; i++) {
1262 values[i] = ep0[i].me_value;
1263 ep0[i].me_value = NULL;
1264 }
1265 mp->ma_keys->dk_lookup = lookdict_split;
1266 mp->ma_values = values;
1267 }
INADA Naokia7576492018-11-14 18:39:27 +09001268 dictkeys_incref(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001269 return mp->ma_keys;
1270}
Christian Heimes99170a52007-12-19 02:07:34 +00001271
1272PyObject *
1273_PyDict_NewPresized(Py_ssize_t minused)
1274{
INADA Naoki92c50ee2016-11-22 00:57:02 +09001275 const Py_ssize_t max_presize = 128 * 1024;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001276 Py_ssize_t newsize;
1277 PyDictKeysObject *new_keys;
INADA Naoki92c50ee2016-11-22 00:57:02 +09001278
1279 /* There are no strict guarantee that returned dict can contain minused
1280 * items without resize. So we create medium size dict instead of very
1281 * large dict or MemoryError.
1282 */
1283 if (minused > USABLE_FRACTION(max_presize)) {
1284 newsize = max_presize;
1285 }
1286 else {
1287 Py_ssize_t minsize = ESTIMATE_SIZE(minused);
1288 newsize = PyDict_MINSIZE;
1289 while (newsize < minsize) {
1290 newsize <<= 1;
1291 }
1292 }
1293 assert(IS_POWER_OF_2(newsize));
1294
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001295 new_keys = new_keys_object(newsize);
1296 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001298 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001299}
1300
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001301/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1302 * that may occur (originally dicts supported only string keys, and exceptions
1303 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001304 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001305 * (suppressed) occurred while computing the key's hash, or that some error
1306 * (suppressed) occurred when comparing keys in the dict's internal probe
1307 * sequence. A nasty example of the latter is when a Python-coded comparison
1308 * function hits a stack-depth error, which can cause this to return NULL
1309 * even if the key is present.
1310 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001311PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001312PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001313{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001314 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07001315 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 PyDictObject *mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 PyThreadState *tstate;
INADA Naokiba609772016-12-07 20:41:42 +09001318 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 if (!PyDict_Check(op))
1321 return NULL;
1322 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001323 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 {
1325 hash = PyObject_Hash(key);
1326 if (hash == -1) {
1327 PyErr_Clear();
1328 return NULL;
1329 }
1330 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 /* We can arrive here with a NULL tstate during initialization: try
1333 running "python -Wi" for an example related to string interning.
1334 Let's just hope that no exception occurs then... This must be
Victor Stinner50b48572018-11-01 01:51:40 +01001335 _PyThreadState_GET() and not PyThreadState_Get() because the latter
Victor Stinner9204fb82018-10-30 15:13:17 +01001336 abort Python if tstate is NULL. */
Victor Stinner50b48572018-11-01 01:51:40 +01001337 tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 if (tstate != NULL && tstate->curexc_type != NULL) {
1339 /* preserve the existing exception */
1340 PyObject *err_type, *err_value, *err_tb;
1341 PyErr_Fetch(&err_type, &err_value, &err_tb);
INADA Naoki778928b2017-08-03 23:45:15 +09001342 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 /* ignore errors */
1344 PyErr_Restore(err_type, err_value, err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001345 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 return NULL;
1347 }
1348 else {
INADA Naoki778928b2017-08-03 23:45:15 +09001349 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001350 if (ix < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 PyErr_Clear();
1352 return NULL;
1353 }
1354 }
INADA Naokiba609772016-12-07 20:41:42 +09001355 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001356}
1357
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001358/* Same as PyDict_GetItemWithError() but with hash supplied by caller.
1359 This returns NULL *with* an exception set if an exception occurred.
1360 It returns NULL *without* an exception set if the key wasn't present.
1361*/
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001362PyObject *
1363_PyDict_GetItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1364{
Victor Stinner742da042016-09-07 17:40:12 -07001365 Py_ssize_t ix;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001366 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001367 PyObject *value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001368
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001369 if (!PyDict_Check(op)) {
1370 PyErr_BadInternalCall();
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001371 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001372 }
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001373
INADA Naoki778928b2017-08-03 23:45:15 +09001374 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001375 if (ix < 0) {
1376 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001377 }
INADA Naokiba609772016-12-07 20:41:42 +09001378 return value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001379}
1380
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001381/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1382 This returns NULL *with* an exception set if an exception occurred.
1383 It returns NULL *without* an exception set if the key wasn't present.
1384*/
1385PyObject *
1386PyDict_GetItemWithError(PyObject *op, PyObject *key)
1387{
Victor Stinner742da042016-09-07 17:40:12 -07001388 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001389 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 PyDictObject*mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001391 PyObject *value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 if (!PyDict_Check(op)) {
1394 PyErr_BadInternalCall();
1395 return NULL;
1396 }
1397 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001398 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 {
1400 hash = PyObject_Hash(key);
1401 if (hash == -1) {
1402 return NULL;
1403 }
1404 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001405
INADA Naoki778928b2017-08-03 23:45:15 +09001406 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001407 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001409 return value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001410}
1411
Brett Cannonfd074152012-04-14 14:10:13 -04001412PyObject *
1413_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1414{
1415 PyObject *kv;
1416 kv = _PyUnicode_FromId(key); /* borrowed */
1417 if (kv == NULL)
1418 return NULL;
1419 return PyDict_GetItemWithError(dp, kv);
1420}
1421
Victor Stinnerb4efc962015-11-20 09:24:02 +01001422/* Fast version of global value lookup (LOAD_GLOBAL).
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001423 * Lookup in globals, then builtins.
Victor Stinnerb4efc962015-11-20 09:24:02 +01001424 *
1425 * Raise an exception and return NULL if an error occurred (ex: computing the
1426 * key hash failed, key comparison failed, ...). Return NULL if the key doesn't
1427 * exist. Return the value if the key exists.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001428 */
1429PyObject *
1430_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001431{
Victor Stinner742da042016-09-07 17:40:12 -07001432 Py_ssize_t ix;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001433 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09001434 PyObject *value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001435
1436 if (!PyUnicode_CheckExact(key) ||
1437 (hash = ((PyASCIIObject *) key)->hash) == -1)
1438 {
1439 hash = PyObject_Hash(key);
1440 if (hash == -1)
1441 return NULL;
Antoine Pitroue965d972012-02-27 00:45:12 +01001442 }
Victor Stinnerb4efc962015-11-20 09:24:02 +01001443
1444 /* namespace 1: globals */
INADA Naoki778928b2017-08-03 23:45:15 +09001445 ix = globals->ma_keys->dk_lookup(globals, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001446 if (ix == DKIX_ERROR)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001447 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001448 if (ix != DKIX_EMPTY && value != NULL)
1449 return value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001450
1451 /* namespace 2: builtins */
INADA Naoki778928b2017-08-03 23:45:15 +09001452 ix = builtins->ma_keys->dk_lookup(builtins, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001453 if (ix < 0)
Victor Stinnerb4efc962015-11-20 09:24:02 +01001454 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001455 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001456}
1457
Antoine Pitroue965d972012-02-27 00:45:12 +01001458/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1459 * dictionary if it's merely replacing the value for an existing key.
1460 * This means that it's safe to loop over a dictionary with PyDict_Next()
1461 * and occasionally replace a value -- but you can't insert new keys or
1462 * remove them.
1463 */
1464int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001465PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001466{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001467 PyDictObject *mp;
1468 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001469 if (!PyDict_Check(op)) {
1470 PyErr_BadInternalCall();
1471 return -1;
1472 }
1473 assert(key);
1474 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001475 mp = (PyDictObject *)op;
1476 if (!PyUnicode_CheckExact(key) ||
1477 (hash = ((PyASCIIObject *) key)->hash) == -1)
1478 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001479 hash = PyObject_Hash(key);
1480 if (hash == -1)
1481 return -1;
1482 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001483
1484 /* insertdict() handles any resizing that might be necessary */
1485 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001486}
1487
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001488int
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001489_PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
1490 Py_hash_t hash)
1491{
1492 PyDictObject *mp;
1493
1494 if (!PyDict_Check(op)) {
1495 PyErr_BadInternalCall();
1496 return -1;
1497 }
1498 assert(key);
1499 assert(value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001500 assert(hash != -1);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001501 mp = (PyDictObject *)op;
1502
1503 /* insertdict() handles any resizing that might be necessary */
1504 return insertdict(mp, key, hash, value);
1505}
1506
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001507static int
INADA Naoki778928b2017-08-03 23:45:15 +09001508delitem_common(PyDictObject *mp, Py_hash_t hash, Py_ssize_t ix,
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001509 PyObject *old_value)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001510{
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001511 PyObject *old_key;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001512 PyDictKeyEntry *ep;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001513
INADA Naoki778928b2017-08-03 23:45:15 +09001514 Py_ssize_t hashpos = lookdict_index(mp->ma_keys, hash, ix);
1515 assert(hashpos >= 0);
1516
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001517 mp->ma_used--;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001518 mp->ma_version_tag = DICT_NEXT_VERSION();
1519 ep = &DK_ENTRIES(mp->ma_keys)[ix];
INADA Naokia7576492018-11-14 18:39:27 +09001520 dictkeys_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001521 ENSURE_ALLOWS_DELETIONS(mp);
1522 old_key = ep->me_key;
1523 ep->me_key = NULL;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001524 ep->me_value = NULL;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001525 Py_DECREF(old_key);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001526 Py_DECREF(old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001527
1528 assert(_PyDict_CheckConsistency(mp));
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001529 return 0;
1530}
1531
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001532int
Tim Peters1f5871e2000-07-04 17:44:48 +00001533PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001534{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001535 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 assert(key);
1537 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001538 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539 hash = PyObject_Hash(key);
1540 if (hash == -1)
1541 return -1;
1542 }
Victor Stinner742da042016-09-07 17:40:12 -07001543
1544 return _PyDict_DelItem_KnownHash(op, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001545}
1546
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001547int
1548_PyDict_DelItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1549{
INADA Naoki778928b2017-08-03 23:45:15 +09001550 Py_ssize_t ix;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001551 PyDictObject *mp;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001552 PyObject *old_value;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001553
1554 if (!PyDict_Check(op)) {
1555 PyErr_BadInternalCall();
1556 return -1;
1557 }
1558 assert(key);
1559 assert(hash != -1);
1560 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001561 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001562 if (ix == DKIX_ERROR)
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001563 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09001564 if (ix == DKIX_EMPTY || old_value == NULL) {
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001565 _PyErr_SetKeyError(key);
1566 return -1;
1567 }
Victor Stinner78601a32016-09-09 19:28:36 -07001568
1569 // Split table doesn't allow deletion. Combine it.
1570 if (_PyDict_HasSplitTable(mp)) {
1571 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1572 return -1;
1573 }
INADA Naoki778928b2017-08-03 23:45:15 +09001574 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001575 assert(ix >= 0);
1576 }
1577
INADA Naoki778928b2017-08-03 23:45:15 +09001578 return delitem_common(mp, hash, ix, old_value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001579}
1580
Antoine Pitroud741ed42016-12-27 14:23:43 +01001581/* This function promises that the predicate -> deletion sequence is atomic
1582 * (i.e. protected by the GIL), assuming the predicate itself doesn't
1583 * release the GIL.
1584 */
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001585int
1586_PyDict_DelItemIf(PyObject *op, PyObject *key,
1587 int (*predicate)(PyObject *value))
1588{
Antoine Pitroud741ed42016-12-27 14:23:43 +01001589 Py_ssize_t hashpos, ix;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001590 PyDictObject *mp;
1591 Py_hash_t hash;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001592 PyObject *old_value;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001593 int res;
1594
1595 if (!PyDict_Check(op)) {
1596 PyErr_BadInternalCall();
1597 return -1;
1598 }
1599 assert(key);
1600 hash = PyObject_Hash(key);
1601 if (hash == -1)
1602 return -1;
1603 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001604 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001605 if (ix == DKIX_ERROR)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001606 return -1;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001607 if (ix == DKIX_EMPTY || old_value == NULL) {
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001608 _PyErr_SetKeyError(key);
1609 return -1;
1610 }
Antoine Pitroud741ed42016-12-27 14:23:43 +01001611
1612 // Split table doesn't allow deletion. Combine it.
1613 if (_PyDict_HasSplitTable(mp)) {
1614 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1615 return -1;
1616 }
INADA Naoki778928b2017-08-03 23:45:15 +09001617 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001618 assert(ix >= 0);
1619 }
1620
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001621 res = predicate(old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001622 if (res == -1)
1623 return -1;
INADA Naoki778928b2017-08-03 23:45:15 +09001624
1625 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1626 assert(hashpos >= 0);
1627
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001628 if (res > 0)
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001629 return delitem_common(mp, hashpos, ix, old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001630 else
1631 return 0;
1632}
1633
1634
Guido van Rossum25831651993-05-19 14:50:45 +00001635void
Tim Peters1f5871e2000-07-04 17:44:48 +00001636PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001637{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001639 PyDictKeysObject *oldkeys;
1640 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001643 if (!PyDict_Check(op))
1644 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001645 mp = ((PyDictObject *)op);
1646 oldkeys = mp->ma_keys;
1647 oldvalues = mp->ma_values;
1648 if (oldvalues == empty_values)
1649 return;
1650 /* Empty the dict... */
INADA Naokia7576492018-11-14 18:39:27 +09001651 dictkeys_incref(Py_EMPTY_KEYS);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001652 mp->ma_keys = Py_EMPTY_KEYS;
1653 mp->ma_values = empty_values;
1654 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001655 mp->ma_version_tag = DICT_NEXT_VERSION();
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001656 /* ...then clear the keys and values */
1657 if (oldvalues != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001658 n = oldkeys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001659 for (i = 0; i < n; i++)
1660 Py_CLEAR(oldvalues[i]);
1661 free_values(oldvalues);
INADA Naokia7576492018-11-14 18:39:27 +09001662 dictkeys_decref(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001663 }
1664 else {
1665 assert(oldkeys->dk_refcnt == 1);
INADA Naokia7576492018-11-14 18:39:27 +09001666 dictkeys_decref(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001667 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02001668 assert(_PyDict_CheckConsistency(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001669}
1670
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001671/* Internal version of PyDict_Next that returns a hash value in addition
1672 * to the key and value.
1673 * Return 1 on success, return 0 when the reached the end of the dictionary
1674 * (or if op is not a dictionary)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001675 */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001676int
1677_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1678 PyObject **pvalue, Py_hash_t *phash)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001679{
INADA Naokica2d8be2016-11-04 16:59:10 +09001680 Py_ssize_t i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001681 PyDictObject *mp;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001682 PyDictKeyEntry *entry_ptr;
1683 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001684
1685 if (!PyDict_Check(op))
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001686 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001687 mp = (PyDictObject *)op;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001688 i = *ppos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001689 if (mp->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09001690 if (i < 0 || i >= mp->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001691 return 0;
INADA Naokica2d8be2016-11-04 16:59:10 +09001692 /* values of split table is always dense */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001693 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
INADA Naokica2d8be2016-11-04 16:59:10 +09001694 value = mp->ma_values[i];
1695 assert(value != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001696 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001697 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09001698 Py_ssize_t n = mp->ma_keys->dk_nentries;
1699 if (i < 0 || i >= n)
1700 return 0;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001701 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
1702 while (i < n && entry_ptr->me_value == NULL) {
1703 entry_ptr++;
1704 i++;
Victor Stinner742da042016-09-07 17:40:12 -07001705 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001706 if (i >= n)
1707 return 0;
1708 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001710 *ppos = i+1;
1711 if (pkey)
1712 *pkey = entry_ptr->me_key;
1713 if (phash)
1714 *phash = entry_ptr->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001715 if (pvalue)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001716 *pvalue = value;
1717 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001718}
1719
Tim Peters080c88b2003-02-15 03:01:11 +00001720/*
1721 * Iterate over a dict. Use like so:
1722 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001723 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001724 * PyObject *key, *value;
1725 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001726 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001727 * Refer to borrowed references in key and value.
Tim Peters080c88b2003-02-15 03:01:11 +00001728 * }
1729 *
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001730 * Return 1 on success, return 0 when the reached the end of the dictionary
1731 * (or if op is not a dictionary)
1732 *
Tim Peters080c88b2003-02-15 03:01:11 +00001733 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001734 * mutates the dict. One exception: it is safe if the loop merely changes
1735 * the values associated with the keys (but doesn't insert new keys or
1736 * delete keys), via PyDict_SetItem().
1737 */
Guido van Rossum25831651993-05-19 14:50:45 +00001738int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001739PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001740{
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001741 return _PyDict_Next(op, ppos, pkey, pvalue, NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001742}
1743
Eric Snow96c6af92015-05-29 22:21:39 -06001744/* Internal version of dict.pop(). */
1745PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001746_PyDict_Pop_KnownHash(PyObject *dict, PyObject *key, Py_hash_t hash, PyObject *deflt)
Eric Snow96c6af92015-05-29 22:21:39 -06001747{
Victor Stinner742da042016-09-07 17:40:12 -07001748 Py_ssize_t ix, hashpos;
Eric Snow96c6af92015-05-29 22:21:39 -06001749 PyObject *old_value, *old_key;
1750 PyDictKeyEntry *ep;
Yury Selivanov684ef2c2016-10-28 19:01:21 -04001751 PyDictObject *mp;
1752
1753 assert(PyDict_Check(dict));
1754 mp = (PyDictObject *)dict;
Eric Snow96c6af92015-05-29 22:21:39 -06001755
1756 if (mp->ma_used == 0) {
1757 if (deflt) {
1758 Py_INCREF(deflt);
1759 return deflt;
1760 }
1761 _PyErr_SetKeyError(key);
1762 return NULL;
1763 }
INADA Naoki778928b2017-08-03 23:45:15 +09001764 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001765 if (ix == DKIX_ERROR)
Eric Snow96c6af92015-05-29 22:21:39 -06001766 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001767 if (ix == DKIX_EMPTY || old_value == NULL) {
Eric Snow96c6af92015-05-29 22:21:39 -06001768 if (deflt) {
1769 Py_INCREF(deflt);
1770 return deflt;
1771 }
1772 _PyErr_SetKeyError(key);
1773 return NULL;
1774 }
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001775
Victor Stinner78601a32016-09-09 19:28:36 -07001776 // Split table doesn't allow deletion. Combine it.
1777 if (_PyDict_HasSplitTable(mp)) {
1778 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1779 return NULL;
1780 }
INADA Naoki778928b2017-08-03 23:45:15 +09001781 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001782 assert(ix >= 0);
1783 }
1784
INADA Naoki778928b2017-08-03 23:45:15 +09001785 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1786 assert(hashpos >= 0);
Victor Stinner78601a32016-09-09 19:28:36 -07001787 assert(old_value != NULL);
Eric Snow96c6af92015-05-29 22:21:39 -06001788 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001789 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokia7576492018-11-14 18:39:27 +09001790 dictkeys_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
Victor Stinner78601a32016-09-09 19:28:36 -07001791 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1792 ENSURE_ALLOWS_DELETIONS(mp);
1793 old_key = ep->me_key;
1794 ep->me_key = NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001795 ep->me_value = NULL;
Victor Stinner78601a32016-09-09 19:28:36 -07001796 Py_DECREF(old_key);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001797
1798 assert(_PyDict_CheckConsistency(mp));
Eric Snow96c6af92015-05-29 22:21:39 -06001799 return old_value;
1800}
1801
Serhiy Storchaka67796522017-01-12 18:34:33 +02001802PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001803_PyDict_Pop(PyObject *dict, PyObject *key, PyObject *deflt)
Serhiy Storchaka67796522017-01-12 18:34:33 +02001804{
1805 Py_hash_t hash;
1806
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001807 if (((PyDictObject *)dict)->ma_used == 0) {
Serhiy Storchaka67796522017-01-12 18:34:33 +02001808 if (deflt) {
1809 Py_INCREF(deflt);
1810 return deflt;
1811 }
1812 _PyErr_SetKeyError(key);
1813 return NULL;
1814 }
1815 if (!PyUnicode_CheckExact(key) ||
1816 (hash = ((PyASCIIObject *) key)->hash) == -1) {
1817 hash = PyObject_Hash(key);
1818 if (hash == -1)
1819 return NULL;
1820 }
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001821 return _PyDict_Pop_KnownHash(dict, key, hash, deflt);
Serhiy Storchaka67796522017-01-12 18:34:33 +02001822}
1823
Eric Snow96c6af92015-05-29 22:21:39 -06001824/* Internal version of dict.from_keys(). It is subclass-friendly. */
1825PyObject *
1826_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
1827{
1828 PyObject *it; /* iter(iterable) */
1829 PyObject *key;
1830 PyObject *d;
1831 int status;
1832
Victor Stinnera5ed5f02016-12-06 18:45:50 +01001833 d = _PyObject_CallNoArg(cls);
Eric Snow96c6af92015-05-29 22:21:39 -06001834 if (d == NULL)
1835 return NULL;
1836
1837 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
1838 if (PyDict_CheckExact(iterable)) {
1839 PyDictObject *mp = (PyDictObject *)d;
1840 PyObject *oldvalue;
1841 Py_ssize_t pos = 0;
1842 PyObject *key;
1843 Py_hash_t hash;
1844
Serhiy Storchakac61ac162017-03-21 08:52:38 +02001845 if (dictresize(mp, ESTIMATE_SIZE(PyDict_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001846 Py_DECREF(d);
1847 return NULL;
1848 }
1849
1850 while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
1851 if (insertdict(mp, key, hash, value)) {
1852 Py_DECREF(d);
1853 return NULL;
1854 }
1855 }
1856 return d;
1857 }
1858 if (PyAnySet_CheckExact(iterable)) {
1859 PyDictObject *mp = (PyDictObject *)d;
1860 Py_ssize_t pos = 0;
1861 PyObject *key;
1862 Py_hash_t hash;
1863
Victor Stinner742da042016-09-07 17:40:12 -07001864 if (dictresize(mp, ESTIMATE_SIZE(PySet_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001865 Py_DECREF(d);
1866 return NULL;
1867 }
1868
1869 while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
1870 if (insertdict(mp, key, hash, value)) {
1871 Py_DECREF(d);
1872 return NULL;
1873 }
1874 }
1875 return d;
1876 }
1877 }
1878
1879 it = PyObject_GetIter(iterable);
1880 if (it == NULL){
1881 Py_DECREF(d);
1882 return NULL;
1883 }
1884
1885 if (PyDict_CheckExact(d)) {
1886 while ((key = PyIter_Next(it)) != NULL) {
1887 status = PyDict_SetItem(d, key, value);
1888 Py_DECREF(key);
1889 if (status < 0)
1890 goto Fail;
1891 }
1892 } else {
1893 while ((key = PyIter_Next(it)) != NULL) {
1894 status = PyObject_SetItem(d, key, value);
1895 Py_DECREF(key);
1896 if (status < 0)
1897 goto Fail;
1898 }
1899 }
1900
1901 if (PyErr_Occurred())
1902 goto Fail;
1903 Py_DECREF(it);
1904 return d;
1905
1906Fail:
1907 Py_DECREF(it);
1908 Py_DECREF(d);
1909 return NULL;
1910}
1911
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001912/* Methods */
1913
1914static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001915dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001916{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001917 PyObject **values = mp->ma_values;
1918 PyDictKeysObject *keys = mp->ma_keys;
1919 Py_ssize_t i, n;
INADA Naokia6296d32017-08-24 14:55:17 +09001920
1921 /* bpo-31095: UnTrack is needed before calling any callbacks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 PyObject_GC_UnTrack(mp);
1923 Py_TRASHCAN_SAFE_BEGIN(mp)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001924 if (values != NULL) {
1925 if (values != empty_values) {
Victor Stinner742da042016-09-07 17:40:12 -07001926 for (i = 0, n = mp->ma_keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001927 Py_XDECREF(values[i]);
1928 }
1929 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 }
INADA Naokia7576492018-11-14 18:39:27 +09001931 dictkeys_decref(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02001933 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001934 assert(keys->dk_refcnt == 1);
INADA Naokia7576492018-11-14 18:39:27 +09001935 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001936 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1938 free_list[numfree++] = mp;
1939 else
1940 Py_TYPE(mp)->tp_free((PyObject *)mp);
1941 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001942}
1943
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001944
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001945static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001946dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001947{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001948 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001949 PyObject *key = NULL, *value = NULL;
1950 _PyUnicodeWriter writer;
1951 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00001952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001953 i = Py_ReprEnter((PyObject *)mp);
1954 if (i != 0) {
1955 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
1956 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001958 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001959 Py_ReprLeave((PyObject *)mp);
1960 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 }
Tim Petersa7259592001-06-16 05:11:17 +00001962
Victor Stinnerf91929b2013-11-19 13:07:38 +01001963 _PyUnicodeWriter_Init(&writer);
1964 writer.overallocate = 1;
1965 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
1966 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00001967
Victor Stinnerf91929b2013-11-19 13:07:38 +01001968 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
1969 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001970
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001971 /* Do repr() on each key+value pair, and insert ": " between them.
1972 Note that repr may mutate the dict. */
1973 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001974 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001976 PyObject *s;
1977 int res;
1978
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001979 /* Prevent repr from deleting key or value during key format. */
1980 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001981 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02001982
Victor Stinnerf91929b2013-11-19 13:07:38 +01001983 if (!first) {
1984 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
1985 goto error;
1986 }
1987 first = 0;
1988
1989 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001990 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01001991 goto error;
1992 res = _PyUnicodeWriter_WriteStr(&writer, s);
1993 Py_DECREF(s);
1994 if (res < 0)
1995 goto error;
1996
1997 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
1998 goto error;
1999
2000 s = PyObject_Repr(value);
2001 if (s == NULL)
2002 goto error;
2003 res = _PyUnicodeWriter_WriteStr(&writer, s);
2004 Py_DECREF(s);
2005 if (res < 0)
2006 goto error;
2007
2008 Py_CLEAR(key);
2009 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002010 }
Tim Petersa7259592001-06-16 05:11:17 +00002011
Victor Stinnerf91929b2013-11-19 13:07:38 +01002012 writer.overallocate = 0;
2013 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
2014 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00002015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002016 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01002017
2018 return _PyUnicodeWriter_Finish(&writer);
2019
2020error:
2021 Py_ReprLeave((PyObject *)mp);
2022 _PyUnicodeWriter_Dealloc(&writer);
2023 Py_XDECREF(key);
2024 Py_XDECREF(value);
2025 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002026}
2027
Martin v. Löwis18e16552006-02-15 17:27:45 +00002028static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002029dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002030{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002031 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002032}
2033
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002034static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002035dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002036{
Victor Stinner742da042016-09-07 17:40:12 -07002037 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002038 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09002039 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002041 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002042 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002043 hash = PyObject_Hash(key);
2044 if (hash == -1)
2045 return NULL;
2046 }
INADA Naoki778928b2017-08-03 23:45:15 +09002047 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002048 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002049 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002050 if (ix == DKIX_EMPTY || value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002051 if (!PyDict_CheckExact(mp)) {
2052 /* Look up __missing__ method if we're a subclass. */
2053 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05002054 _Py_IDENTIFIER(__missing__);
2055 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002056 if (missing != NULL) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01002057 res = PyObject_CallFunctionObjArgs(missing,
2058 key, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059 Py_DECREF(missing);
2060 return res;
2061 }
2062 else if (PyErr_Occurred())
2063 return NULL;
2064 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07002065 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002066 return NULL;
2067 }
INADA Naokiba609772016-12-07 20:41:42 +09002068 Py_INCREF(value);
2069 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002070}
2071
2072static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002073dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002074{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002075 if (w == NULL)
2076 return PyDict_DelItem((PyObject *)mp, v);
2077 else
2078 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002079}
2080
Guido van Rossuma9e7a811997-05-13 21:02:11 +00002081static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002082 (lenfunc)dict_length, /*mp_length*/
2083 (binaryfunc)dict_subscript, /*mp_subscript*/
2084 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002085};
2086
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002087static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002088dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002089{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002090 PyObject *v;
2091 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002092 PyDictKeyEntry *ep;
2093 Py_ssize_t size, n, offset;
2094 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002095
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002096 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002097 n = mp->ma_used;
2098 v = PyList_New(n);
2099 if (v == NULL)
2100 return NULL;
2101 if (n != mp->ma_used) {
2102 /* Durnit. The allocations caused the dict to resize.
2103 * Just start over, this shouldn't normally happen.
2104 */
2105 Py_DECREF(v);
2106 goto again;
2107 }
Victor Stinner742da042016-09-07 17:40:12 -07002108 ep = DK_ENTRIES(mp->ma_keys);
2109 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002110 if (mp->ma_values) {
2111 value_ptr = mp->ma_values;
2112 offset = sizeof(PyObject *);
2113 }
2114 else {
2115 value_ptr = &ep[0].me_value;
2116 offset = sizeof(PyDictKeyEntry);
2117 }
2118 for (i = 0, j = 0; i < size; i++) {
2119 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002120 PyObject *key = ep[i].me_key;
2121 Py_INCREF(key);
2122 PyList_SET_ITEM(v, j, key);
2123 j++;
2124 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002125 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002126 }
2127 assert(j == n);
2128 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002129}
2130
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002131static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002132dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002133{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002134 PyObject *v;
2135 Py_ssize_t i, j;
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002136 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002137 Py_ssize_t size, n, offset;
2138 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002139
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002140 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002141 n = mp->ma_used;
2142 v = PyList_New(n);
2143 if (v == NULL)
2144 return NULL;
2145 if (n != mp->ma_used) {
2146 /* Durnit. The allocations caused the dict to resize.
2147 * Just start over, this shouldn't normally happen.
2148 */
2149 Py_DECREF(v);
2150 goto again;
2151 }
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002152 ep = DK_ENTRIES(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002153 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002154 if (mp->ma_values) {
2155 value_ptr = mp->ma_values;
2156 offset = sizeof(PyObject *);
2157 }
2158 else {
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002159 value_ptr = &ep[0].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002160 offset = sizeof(PyDictKeyEntry);
2161 }
2162 for (i = 0, j = 0; i < size; i++) {
2163 PyObject *value = *value_ptr;
2164 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2165 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002166 Py_INCREF(value);
2167 PyList_SET_ITEM(v, j, value);
2168 j++;
2169 }
2170 }
2171 assert(j == n);
2172 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002173}
2174
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002175static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002176dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002177{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002178 PyObject *v;
2179 Py_ssize_t i, j, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002180 Py_ssize_t size, offset;
2181 PyObject *item, *key;
2182 PyDictKeyEntry *ep;
2183 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002184
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002185 /* Preallocate the list of tuples, to avoid allocations during
2186 * the loop over the items, which could trigger GC, which
2187 * could resize the dict. :-(
2188 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002189 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002190 n = mp->ma_used;
2191 v = PyList_New(n);
2192 if (v == NULL)
2193 return NULL;
2194 for (i = 0; i < n; i++) {
2195 item = PyTuple_New(2);
2196 if (item == NULL) {
2197 Py_DECREF(v);
2198 return NULL;
2199 }
2200 PyList_SET_ITEM(v, i, item);
2201 }
2202 if (n != mp->ma_used) {
2203 /* Durnit. The allocations caused the dict to resize.
2204 * Just start over, this shouldn't normally happen.
2205 */
2206 Py_DECREF(v);
2207 goto again;
2208 }
2209 /* Nothing we do below makes any function calls. */
Victor Stinner742da042016-09-07 17:40:12 -07002210 ep = DK_ENTRIES(mp->ma_keys);
2211 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002212 if (mp->ma_values) {
2213 value_ptr = mp->ma_values;
2214 offset = sizeof(PyObject *);
2215 }
2216 else {
2217 value_ptr = &ep[0].me_value;
2218 offset = sizeof(PyDictKeyEntry);
2219 }
2220 for (i = 0, j = 0; i < size; i++) {
2221 PyObject *value = *value_ptr;
2222 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2223 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002224 key = ep[i].me_key;
2225 item = PyList_GET_ITEM(v, j);
2226 Py_INCREF(key);
2227 PyTuple_SET_ITEM(item, 0, key);
2228 Py_INCREF(value);
2229 PyTuple_SET_ITEM(item, 1, value);
2230 j++;
2231 }
2232 }
2233 assert(j == n);
2234 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002235}
2236
Larry Hastings5c661892014-01-24 06:17:25 -08002237/*[clinic input]
2238@classmethod
2239dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08002240 iterable: object
2241 value: object=None
2242 /
2243
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002244Create a new dictionary with keys from iterable and values set to value.
Larry Hastings5c661892014-01-24 06:17:25 -08002245[clinic start generated code]*/
2246
Larry Hastings5c661892014-01-24 06:17:25 -08002247static PyObject *
2248dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002249/*[clinic end generated code: output=8fb98e4b10384999 input=382ba4855d0f74c3]*/
Larry Hastings5c661892014-01-24 06:17:25 -08002250{
Eric Snow96c6af92015-05-29 22:21:39 -06002251 return _PyDict_FromKeys((PyObject *)type, iterable, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002252}
2253
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002254static int
Victor Stinner742da042016-09-07 17:40:12 -07002255dict_update_common(PyObject *self, PyObject *args, PyObject *kwds,
2256 const char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002257{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002258 PyObject *arg = NULL;
2259 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002260
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002261 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002262 result = -1;
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002263 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002264 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002265 _Py_IDENTIFIER(keys);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002266 PyObject *func;
2267 if (_PyObject_LookupAttrId(arg, &PyId_keys, &func) < 0) {
2268 result = -1;
2269 }
2270 else if (func != NULL) {
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002271 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002272 result = PyDict_Merge(self, arg, 1);
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002273 }
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002274 else {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002275 result = PyDict_MergeFromSeq2(self, arg, 1);
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002276 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002277 }
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002279 if (result == 0 && kwds != NULL) {
2280 if (PyArg_ValidateKeywordArguments(kwds))
2281 result = PyDict_Merge(self, kwds, 1);
2282 else
2283 result = -1;
2284 }
2285 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002286}
2287
Victor Stinner91f0d4a2017-01-19 12:45:06 +01002288/* Note: dict.update() uses the METH_VARARGS|METH_KEYWORDS calling convention.
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +03002289 Using METH_FASTCALL|METH_KEYWORDS would make dict.update(**dict2) calls
2290 slower, see the issue #29312. */
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002291static PyObject *
2292dict_update(PyObject *self, PyObject *args, PyObject *kwds)
2293{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 if (dict_update_common(self, args, kwds, "update") != -1)
2295 Py_RETURN_NONE;
2296 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002297}
2298
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002299/* Update unconditionally replaces existing items.
2300 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00002301 otherwise it leaves existing items unchanged.
2302
2303 PyDict_{Update,Merge} update/merge from a mapping object.
2304
Tim Petersf582b822001-12-11 18:51:08 +00002305 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00002306 producing iterable objects of length 2.
2307*/
2308
Tim Petersf582b822001-12-11 18:51:08 +00002309int
Tim Peters1fc240e2001-10-26 05:06:50 +00002310PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
2311{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002312 PyObject *it; /* iter(seq2) */
2313 Py_ssize_t i; /* index into seq2 of current element */
2314 PyObject *item; /* seq2[i] */
2315 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00002316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002317 assert(d != NULL);
2318 assert(PyDict_Check(d));
2319 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00002320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002321 it = PyObject_GetIter(seq2);
2322 if (it == NULL)
2323 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002325 for (i = 0; ; ++i) {
2326 PyObject *key, *value;
2327 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00002328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002329 fast = NULL;
2330 item = PyIter_Next(it);
2331 if (item == NULL) {
2332 if (PyErr_Occurred())
2333 goto Fail;
2334 break;
2335 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002337 /* Convert item to sequence, and verify length 2. */
2338 fast = PySequence_Fast(item, "");
2339 if (fast == NULL) {
2340 if (PyErr_ExceptionMatches(PyExc_TypeError))
2341 PyErr_Format(PyExc_TypeError,
2342 "cannot convert dictionary update "
2343 "sequence element #%zd to a sequence",
2344 i);
2345 goto Fail;
2346 }
2347 n = PySequence_Fast_GET_SIZE(fast);
2348 if (n != 2) {
2349 PyErr_Format(PyExc_ValueError,
2350 "dictionary update sequence element #%zd "
2351 "has length %zd; 2 is required",
2352 i, n);
2353 goto Fail;
2354 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002356 /* Update/merge with this (key, value) pair. */
2357 key = PySequence_Fast_GET_ITEM(fast, 0);
2358 value = PySequence_Fast_GET_ITEM(fast, 1);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002359 Py_INCREF(key);
2360 Py_INCREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 if (override || PyDict_GetItem(d, key) == NULL) {
2362 int status = PyDict_SetItem(d, key, value);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002363 if (status < 0) {
2364 Py_DECREF(key);
2365 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 goto Fail;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002367 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002368 }
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002369 Py_DECREF(key);
2370 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002371 Py_DECREF(fast);
2372 Py_DECREF(item);
2373 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002375 i = 0;
Victor Stinner611b0fa2016-09-14 15:02:01 +02002376 assert(_PyDict_CheckConsistency((PyDictObject *)d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002377 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00002378Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379 Py_XDECREF(item);
2380 Py_XDECREF(fast);
2381 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002382Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 Py_DECREF(it);
2384 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00002385}
2386
doko@ubuntu.comc96df682016-10-11 08:04:02 +02002387static int
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002388dict_merge(PyObject *a, PyObject *b, int override)
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002389{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002390 PyDictObject *mp, *other;
2391 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -07002392 PyDictKeyEntry *entry, *ep0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002393
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002394 assert(0 <= override && override <= 2);
2395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002396 /* We accept for the argument either a concrete dictionary object,
2397 * or an abstract "mapping" object. For the former, we can do
2398 * things quite efficiently. For the latter, we only require that
2399 * PyMapping_Keys() and PyObject_GetItem() be supported.
2400 */
2401 if (a == NULL || !PyDict_Check(a) || b == NULL) {
2402 PyErr_BadInternalCall();
2403 return -1;
2404 }
2405 mp = (PyDictObject*)a;
INADA Naoki2aaf98c2018-09-26 12:59:00 +09002406 if (PyDict_Check(b) && (Py_TYPE(b)->tp_iter == (getiterfunc)dict_iter)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002407 other = (PyDictObject*)b;
2408 if (other == mp || other->ma_used == 0)
2409 /* a.update(a) or a.update({}); nothing to do */
2410 return 0;
2411 if (mp->ma_used == 0)
2412 /* Since the target dict is empty, PyDict_GetItem()
2413 * always returns NULL. Setting override to 1
2414 * skips the unnecessary test.
2415 */
2416 override = 1;
2417 /* Do one big resize at the start, rather than
2418 * incrementally resizing as we insert new items. Expect
2419 * that there will be no (or few) overlapping keys.
2420 */
INADA Naokib1152be2016-10-27 19:26:50 +09002421 if (USABLE_FRACTION(mp->ma_keys->dk_size) < other->ma_used) {
2422 if (dictresize(mp, ESTIMATE_SIZE(mp->ma_used + other->ma_used))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002423 return -1;
INADA Naokib1152be2016-10-27 19:26:50 +09002424 }
2425 }
Victor Stinner742da042016-09-07 17:40:12 -07002426 ep0 = DK_ENTRIES(other->ma_keys);
2427 for (i = 0, n = other->ma_keys->dk_nentries; i < n; i++) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002428 PyObject *key, *value;
2429 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002430 entry = &ep0[i];
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002431 key = entry->me_key;
2432 hash = entry->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002433 if (other->ma_values)
2434 value = other->ma_values[i];
2435 else
2436 value = entry->me_value;
2437
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002438 if (value != NULL) {
2439 int err = 0;
2440 Py_INCREF(key);
2441 Py_INCREF(value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002442 if (override == 1)
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002443 err = insertdict(mp, key, hash, value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002444 else if (_PyDict_GetItem_KnownHash(a, key, hash) == NULL) {
2445 if (PyErr_Occurred()) {
2446 Py_DECREF(value);
2447 Py_DECREF(key);
2448 return -1;
2449 }
2450 err = insertdict(mp, key, hash, value);
2451 }
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002452 else if (override != 0) {
2453 _PyErr_SetKeyError(key);
2454 Py_DECREF(value);
2455 Py_DECREF(key);
2456 return -1;
2457 }
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002458 Py_DECREF(value);
2459 Py_DECREF(key);
2460 if (err != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002461 return -1;
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002462
Victor Stinner742da042016-09-07 17:40:12 -07002463 if (n != other->ma_keys->dk_nentries) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002464 PyErr_SetString(PyExc_RuntimeError,
2465 "dict mutated during update");
2466 return -1;
2467 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002468 }
2469 }
2470 }
2471 else {
2472 /* Do it the generic, slower way */
2473 PyObject *keys = PyMapping_Keys(b);
2474 PyObject *iter;
2475 PyObject *key, *value;
2476 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002478 if (keys == NULL)
2479 /* Docstring says this is equivalent to E.keys() so
2480 * if E doesn't have a .keys() method we want
2481 * AttributeError to percolate up. Might as well
2482 * do the same for any other error.
2483 */
2484 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002486 iter = PyObject_GetIter(keys);
2487 Py_DECREF(keys);
2488 if (iter == NULL)
2489 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002491 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002492 if (override != 1 && PyDict_GetItem(a, key) != NULL) {
2493 if (override != 0) {
2494 _PyErr_SetKeyError(key);
2495 Py_DECREF(key);
2496 Py_DECREF(iter);
2497 return -1;
2498 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002499 Py_DECREF(key);
2500 continue;
2501 }
2502 value = PyObject_GetItem(b, key);
2503 if (value == NULL) {
2504 Py_DECREF(iter);
2505 Py_DECREF(key);
2506 return -1;
2507 }
2508 status = PyDict_SetItem(a, key, value);
2509 Py_DECREF(key);
2510 Py_DECREF(value);
2511 if (status < 0) {
2512 Py_DECREF(iter);
2513 return -1;
2514 }
2515 }
2516 Py_DECREF(iter);
2517 if (PyErr_Occurred())
2518 /* Iterator completed, via error */
2519 return -1;
2520 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02002521 assert(_PyDict_CheckConsistency((PyDictObject *)a));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002522 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002523}
2524
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002525int
2526PyDict_Update(PyObject *a, PyObject *b)
2527{
2528 return dict_merge(a, b, 1);
2529}
2530
2531int
2532PyDict_Merge(PyObject *a, PyObject *b, int override)
2533{
2534 /* XXX Deprecate override not in (0, 1). */
2535 return dict_merge(a, b, override != 0);
2536}
2537
2538int
2539_PyDict_MergeEx(PyObject *a, PyObject *b, int override)
2540{
2541 return dict_merge(a, b, override);
2542}
2543
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002544static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302545dict_copy(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002546{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002547 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002548}
2549
2550PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002551PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002552{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002553 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002554 PyDictObject *mp;
2555 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002557 if (o == NULL || !PyDict_Check(o)) {
2558 PyErr_BadInternalCall();
2559 return NULL;
2560 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002561
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002562 mp = (PyDictObject *)o;
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002563 if (mp->ma_used == 0) {
2564 /* The dict is empty; just return a new dict. */
2565 return PyDict_New();
2566 }
2567
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002568 if (_PyDict_HasSplitTable(mp)) {
2569 PyDictObject *split_copy;
Victor Stinner742da042016-09-07 17:40:12 -07002570 Py_ssize_t size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
2571 PyObject **newvalues;
2572 newvalues = new_values(size);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002573 if (newvalues == NULL)
2574 return PyErr_NoMemory();
2575 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2576 if (split_copy == NULL) {
2577 free_values(newvalues);
2578 return NULL;
2579 }
2580 split_copy->ma_values = newvalues;
2581 split_copy->ma_keys = mp->ma_keys;
2582 split_copy->ma_used = mp->ma_used;
INADA Naokid1c82c52018-04-03 11:43:53 +09002583 split_copy->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokia7576492018-11-14 18:39:27 +09002584 dictkeys_incref(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002585 for (i = 0, n = size; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002586 PyObject *value = mp->ma_values[i];
2587 Py_XINCREF(value);
2588 split_copy->ma_values[i] = value;
2589 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002590 if (_PyObject_GC_IS_TRACKED(mp))
2591 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002592 return (PyObject *)split_copy;
2593 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002594
2595 if (PyDict_CheckExact(mp) && mp->ma_values == NULL &&
2596 (mp->ma_used >= (mp->ma_keys->dk_nentries * 2) / 3))
2597 {
2598 /* Use fast-copy if:
2599
2600 (1) 'mp' is an instance of a subclassed dict; and
2601
2602 (2) 'mp' is not a split-dict; and
2603
2604 (3) if 'mp' is non-compact ('del' operation does not resize dicts),
2605 do fast-copy only if it has at most 1/3 non-used keys.
2606
Ville Skyttä61f82e02018-04-20 23:08:45 +03002607 The last condition (3) is important to guard against a pathological
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002608 case when a large dict is almost emptied with multiple del/pop
2609 operations and copied after that. In cases like this, we defer to
2610 PyDict_Merge, which produces a compacted copy.
2611 */
2612 return clone_combined_dict(mp);
2613 }
2614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002615 copy = PyDict_New();
2616 if (copy == NULL)
2617 return NULL;
2618 if (PyDict_Merge(copy, o, 1) == 0)
2619 return copy;
2620 Py_DECREF(copy);
2621 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002622}
2623
Martin v. Löwis18e16552006-02-15 17:27:45 +00002624Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002625PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002626{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002627 if (mp == NULL || !PyDict_Check(mp)) {
2628 PyErr_BadInternalCall();
2629 return -1;
2630 }
2631 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002632}
2633
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002634PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002635PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002636{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002637 if (mp == NULL || !PyDict_Check(mp)) {
2638 PyErr_BadInternalCall();
2639 return NULL;
2640 }
2641 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002642}
2643
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002644PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002645PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002646{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002647 if (mp == NULL || !PyDict_Check(mp)) {
2648 PyErr_BadInternalCall();
2649 return NULL;
2650 }
2651 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002652}
2653
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002654PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002655PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002656{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002657 if (mp == NULL || !PyDict_Check(mp)) {
2658 PyErr_BadInternalCall();
2659 return NULL;
2660 }
2661 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002662}
2663
Tim Peterse63415e2001-05-08 04:38:29 +00002664/* Return 1 if dicts equal, 0 if not, -1 if error.
2665 * Gets out as soon as any difference is detected.
2666 * Uses only Py_EQ comparison.
2667 */
2668static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002669dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002670{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002671 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002673 if (a->ma_used != b->ma_used)
2674 /* can't be equal if # of entries differ */
2675 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002676 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Victor Stinner742da042016-09-07 17:40:12 -07002677 for (i = 0; i < a->ma_keys->dk_nentries; i++) {
2678 PyDictKeyEntry *ep = &DK_ENTRIES(a->ma_keys)[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002679 PyObject *aval;
2680 if (a->ma_values)
2681 aval = a->ma_values[i];
2682 else
2683 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002684 if (aval != NULL) {
2685 int cmp;
2686 PyObject *bval;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002687 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002688 /* temporarily bump aval's refcount to ensure it stays
2689 alive until we're done with it */
2690 Py_INCREF(aval);
2691 /* ditto for key */
2692 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002693 /* reuse the known hash value */
INADA Naoki778928b2017-08-03 23:45:15 +09002694 b->ma_keys->dk_lookup(b, key, ep->me_hash, &bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002695 if (bval == NULL) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002696 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002697 Py_DECREF(aval);
2698 if (PyErr_Occurred())
2699 return -1;
2700 return 0;
2701 }
2702 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002703 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002704 Py_DECREF(aval);
2705 if (cmp <= 0) /* error or not equal */
2706 return cmp;
2707 }
2708 }
2709 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002710}
Tim Peterse63415e2001-05-08 04:38:29 +00002711
2712static PyObject *
2713dict_richcompare(PyObject *v, PyObject *w, int op)
2714{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002715 int cmp;
2716 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002718 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2719 res = Py_NotImplemented;
2720 }
2721 else if (op == Py_EQ || op == Py_NE) {
2722 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2723 if (cmp < 0)
2724 return NULL;
2725 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2726 }
2727 else
2728 res = Py_NotImplemented;
2729 Py_INCREF(res);
2730 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002731}
Tim Peterse63415e2001-05-08 04:38:29 +00002732
Larry Hastings61272b72014-01-07 12:41:53 -08002733/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002734
2735@coexist
2736dict.__contains__
2737
2738 key: object
2739 /
2740
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002741True if the dictionary has the specified key, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002742[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002743
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002744static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002745dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka19d25972017-02-04 08:05:07 +02002746/*[clinic end generated code: output=a3d03db709ed6e6b input=fe1cb42ad831e820]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002747{
Larry Hastingsc2047262014-01-25 20:43:29 -08002748 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002749 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002750 Py_ssize_t ix;
INADA Naokiba609772016-12-07 20:41:42 +09002751 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002753 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002754 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002755 hash = PyObject_Hash(key);
2756 if (hash == -1)
2757 return NULL;
2758 }
INADA Naoki778928b2017-08-03 23:45:15 +09002759 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002760 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002761 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002762 if (ix == DKIX_EMPTY || value == NULL)
Victor Stinner742da042016-09-07 17:40:12 -07002763 Py_RETURN_FALSE;
2764 Py_RETURN_TRUE;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002765}
2766
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002767/*[clinic input]
2768dict.get
2769
2770 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002771 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002772 /
2773
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002774Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002775[clinic start generated code]*/
2776
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002777static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002778dict_get_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002779/*[clinic end generated code: output=bba707729dee05bf input=279ddb5790b6b107]*/
Barry Warsawc38c5da1997-10-06 17:49:20 +00002780{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002781 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002782 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002783 Py_ssize_t ix;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002784
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002785 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002786 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002787 hash = PyObject_Hash(key);
2788 if (hash == -1)
2789 return NULL;
2790 }
INADA Naoki778928b2017-08-03 23:45:15 +09002791 ix = (self->ma_keys->dk_lookup) (self, key, hash, &val);
Victor Stinner742da042016-09-07 17:40:12 -07002792 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002793 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002794 if (ix == DKIX_EMPTY || val == NULL) {
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002795 val = default_value;
INADA Naokiba609772016-12-07 20:41:42 +09002796 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002797 Py_INCREF(val);
2798 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002799}
2800
Benjamin Peterson00e98862013-03-07 22:16:29 -05002801PyObject *
2802PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002803{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002804 PyDictObject *mp = (PyDictObject *)d;
INADA Naoki93f26f72016-11-02 18:45:16 +09002805 PyObject *value;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002806 Py_hash_t hash;
Guido van Rossum164452c2000-08-08 16:12:54 +00002807
Benjamin Peterson00e98862013-03-07 22:16:29 -05002808 if (!PyDict_Check(d)) {
2809 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002810 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002811 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002813 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002814 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002815 hash = PyObject_Hash(key);
2816 if (hash == -1)
2817 return NULL;
2818 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002819
2820 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
2821 if (insertion_resize(mp) < 0)
2822 return NULL;
2823 }
2824
INADA Naoki778928b2017-08-03 23:45:15 +09002825 Py_ssize_t ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002826 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002827 return NULL;
INADA Naoki93f26f72016-11-02 18:45:16 +09002828
2829 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09002830 ((ix >= 0 && value == NULL && mp->ma_used != ix) ||
INADA Naoki93f26f72016-11-02 18:45:16 +09002831 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
2832 if (insertion_resize(mp) < 0) {
2833 return NULL;
2834 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002835 ix = DKIX_EMPTY;
2836 }
2837
2838 if (ix == DKIX_EMPTY) {
2839 PyDictKeyEntry *ep, *ep0;
2840 value = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002841 if (mp->ma_keys->dk_usable <= 0) {
Victor Stinner3c336c52016-09-12 14:17:40 +02002842 if (insertion_resize(mp) < 0) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002843 return NULL;
Victor Stinner3c336c52016-09-12 14:17:40 +02002844 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002845 }
INADA Naoki778928b2017-08-03 23:45:15 +09002846 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naoki93f26f72016-11-02 18:45:16 +09002847 ep0 = DK_ENTRIES(mp->ma_keys);
2848 ep = &ep0[mp->ma_keys->dk_nentries];
INADA Naokia7576492018-11-14 18:39:27 +09002849 dictkeys_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002850 Py_INCREF(key);
INADA Naoki93f26f72016-11-02 18:45:16 +09002851 Py_INCREF(value);
2852 MAINTAIN_TRACKING(mp, key, value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002853 ep->me_key = key;
2854 ep->me_hash = hash;
INADA Naokiba609772016-12-07 20:41:42 +09002855 if (_PyDict_HasSplitTable(mp)) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002856 assert(mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
2857 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Victor Stinner742da042016-09-07 17:40:12 -07002858 }
2859 else {
INADA Naoki93f26f72016-11-02 18:45:16 +09002860 ep->me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07002861 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002862 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002863 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naoki93f26f72016-11-02 18:45:16 +09002864 mp->ma_keys->dk_usable--;
2865 mp->ma_keys->dk_nentries++;
2866 assert(mp->ma_keys->dk_usable >= 0);
2867 }
INADA Naokiba609772016-12-07 20:41:42 +09002868 else if (value == NULL) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002869 value = defaultobj;
2870 assert(_PyDict_HasSplitTable(mp));
2871 assert(ix == mp->ma_used);
2872 Py_INCREF(value);
2873 MAINTAIN_TRACKING(mp, key, value);
INADA Naokiba609772016-12-07 20:41:42 +09002874 mp->ma_values[ix] = value;
INADA Naoki93f26f72016-11-02 18:45:16 +09002875 mp->ma_used++;
2876 mp->ma_version_tag = DICT_NEXT_VERSION();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002877 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002878
2879 assert(_PyDict_CheckConsistency(mp));
2880 return value;
Guido van Rossum164452c2000-08-08 16:12:54 +00002881}
2882
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002883/*[clinic input]
2884dict.setdefault
2885
2886 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002887 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002888 /
2889
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002890Insert key with a value of default if key is not in the dictionary.
2891
2892Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002893[clinic start generated code]*/
2894
Benjamin Peterson00e98862013-03-07 22:16:29 -05002895static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002896dict_setdefault_impl(PyDictObject *self, PyObject *key,
2897 PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002898/*[clinic end generated code: output=f8c1101ebf69e220 input=0f063756e815fd9d]*/
Benjamin Peterson00e98862013-03-07 22:16:29 -05002899{
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002900 PyObject *val;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002901
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002902 val = PyDict_SetDefault((PyObject *)self, key, default_value);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002903 Py_XINCREF(val);
2904 return val;
2905}
Guido van Rossum164452c2000-08-08 16:12:54 +00002906
2907static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302908dict_clear(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002909{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002910 PyDict_Clear((PyObject *)mp);
2911 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002912}
2913
Guido van Rossumba6ab842000-12-12 22:02:18 +00002914static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002915dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002916{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00002918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002919 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2920 return NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06002921
Yury Selivanov684ef2c2016-10-28 19:01:21 -04002922 return _PyDict_Pop((PyObject*)mp, key, deflt);
Guido van Rossume027d982002-04-12 15:11:59 +00002923}
2924
2925static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302926dict_popitem(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Guido van Rossumba6ab842000-12-12 22:02:18 +00002927{
Victor Stinner742da042016-09-07 17:40:12 -07002928 Py_ssize_t i, j;
2929 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002930 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002932 /* Allocate the result tuple before checking the size. Believe it
2933 * or not, this allocation could trigger a garbage collection which
2934 * could empty the dict, so if we checked the size first and that
2935 * happened, the result would be an infinite loop (searching for an
2936 * entry that no longer exists). Note that the usual popitem()
2937 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2938 * tuple away if the dict *is* empty isn't a significant
2939 * inefficiency -- possible, but unlikely in practice.
2940 */
2941 res = PyTuple_New(2);
2942 if (res == NULL)
2943 return NULL;
2944 if (mp->ma_used == 0) {
2945 Py_DECREF(res);
2946 PyErr_SetString(PyExc_KeyError,
2947 "popitem(): dictionary is empty");
2948 return NULL;
2949 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002950 /* Convert split table to combined table */
2951 if (mp->ma_keys->dk_lookup == lookdict_split) {
2952 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
2953 Py_DECREF(res);
2954 return NULL;
2955 }
2956 }
2957 ENSURE_ALLOWS_DELETIONS(mp);
Victor Stinner742da042016-09-07 17:40:12 -07002958
2959 /* Pop last item */
2960 ep0 = DK_ENTRIES(mp->ma_keys);
2961 i = mp->ma_keys->dk_nentries - 1;
2962 while (i >= 0 && ep0[i].me_value == NULL) {
2963 i--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002964 }
Victor Stinner742da042016-09-07 17:40:12 -07002965 assert(i >= 0);
2966
2967 ep = &ep0[i];
2968 j = lookdict_index(mp->ma_keys, ep->me_hash, i);
2969 assert(j >= 0);
INADA Naokia7576492018-11-14 18:39:27 +09002970 assert(dictkeys_get_index(mp->ma_keys, j) == i);
2971 dictkeys_set_index(mp->ma_keys, j, DKIX_DUMMY);
Victor Stinner742da042016-09-07 17:40:12 -07002972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002973 PyTuple_SET_ITEM(res, 0, ep->me_key);
2974 PyTuple_SET_ITEM(res, 1, ep->me_value);
Victor Stinner742da042016-09-07 17:40:12 -07002975 ep->me_key = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002976 ep->me_value = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002977 /* We can't dk_usable++ since there is DKIX_DUMMY in indices */
2978 mp->ma_keys->dk_nentries = i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002979 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002980 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +02002981 assert(_PyDict_CheckConsistency(mp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002982 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002983}
2984
Jeremy Hylton8caad492000-06-23 14:18:11 +00002985static int
2986dict_traverse(PyObject *op, visitproc visit, void *arg)
2987{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002988 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson55f44522016-09-05 12:12:59 -07002989 PyDictKeysObject *keys = mp->ma_keys;
Serhiy Storchaka46825d22016-09-26 21:29:34 +03002990 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Victor Stinner742da042016-09-07 17:40:12 -07002991 Py_ssize_t i, n = keys->dk_nentries;
2992
Benjamin Peterson55f44522016-09-05 12:12:59 -07002993 if (keys->dk_lookup == lookdict) {
2994 for (i = 0; i < n; i++) {
2995 if (entries[i].me_value != NULL) {
2996 Py_VISIT(entries[i].me_value);
2997 Py_VISIT(entries[i].me_key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002998 }
2999 }
Victor Stinner742da042016-09-07 17:40:12 -07003000 }
3001 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003002 if (mp->ma_values != NULL) {
Benjamin Peterson55f44522016-09-05 12:12:59 -07003003 for (i = 0; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003004 Py_VISIT(mp->ma_values[i]);
3005 }
3006 }
3007 else {
Benjamin Peterson55f44522016-09-05 12:12:59 -07003008 for (i = 0; i < n; i++) {
3009 Py_VISIT(entries[i].me_value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003010 }
3011 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003012 }
3013 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00003014}
3015
3016static int
3017dict_tp_clear(PyObject *op)
3018{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003019 PyDict_Clear(op);
3020 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00003021}
3022
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003023static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003024
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003025Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003026_PyDict_SizeOf(PyDictObject *mp)
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003027{
Victor Stinner742da042016-09-07 17:40:12 -07003028 Py_ssize_t size, usable, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003029
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003030 size = DK_SIZE(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07003031 usable = USABLE_FRACTION(size);
3032
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02003033 res = _PyObject_SIZE(Py_TYPE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003034 if (mp->ma_values)
Victor Stinner742da042016-09-07 17:40:12 -07003035 res += usable * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003036 /* If the dictionary is split, the keys portion is accounted-for
3037 in the type object. */
3038 if (mp->ma_keys->dk_refcnt == 1)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003039 res += (sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003040 + DK_IXSIZE(mp->ma_keys) * size
3041 + sizeof(PyDictKeyEntry) * usable);
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003042 return res;
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003043}
3044
3045Py_ssize_t
3046_PyDict_KeysSize(PyDictKeysObject *keys)
3047{
Victor Stinner98ee9d52016-09-08 09:33:56 -07003048 return (sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003049 + DK_IXSIZE(keys) * DK_SIZE(keys)
3050 + USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry));
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003051}
3052
doko@ubuntu.com17210f52016-01-14 14:04:59 +01003053static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303054dict_sizeof(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003055{
3056 return PyLong_FromSsize_t(_PyDict_SizeOf(mp));
3057}
3058
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00003059PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
3060
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003061PyDoc_STRVAR(sizeof__doc__,
3062"D.__sizeof__() -> size of D in memory, in bytes");
3063
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003064PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00003065"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00003066If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00003067
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003068PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00003069"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000030702-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003071
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003072PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04003073"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
3074If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
3075If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
3076In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00003077
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003078PyDoc_STRVAR(clear__doc__,
3079"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003080
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003081PyDoc_STRVAR(copy__doc__,
3082"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00003083
Guido van Rossumb90c8482007-02-10 01:11:45 +00003084/* Forward */
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303085static PyObject *dictkeys_new(PyObject *, PyObject *);
3086static PyObject *dictitems_new(PyObject *, PyObject *);
3087static PyObject *dictvalues_new(PyObject *, PyObject *);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003088
Guido van Rossum45c85d12007-07-27 16:31:40 +00003089PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003090 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003091PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003092 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003093PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003094 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00003095
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003096static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07003097 DICT___CONTAINS___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003098 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
3099 getitem__doc__},
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003100 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003101 sizeof__doc__},
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003102 DICT_GET_METHODDEF
3103 DICT_SETDEFAULT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003104 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
3105 pop__doc__},
3106 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
3107 popitem__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303108 {"keys", dictkeys_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003109 keys__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303110 {"items", dictitems_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003111 items__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303112 {"values", dictvalues_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003113 values__doc__},
3114 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
3115 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08003116 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003117 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
3118 clear__doc__},
3119 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
3120 copy__doc__},
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003121 DICT___REVERSED___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003122 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003123};
3124
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00003125/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00003126int
3127PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003128{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003129 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07003130 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003131 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003132 PyObject *value;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003133
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003134 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003135 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003136 hash = PyObject_Hash(key);
3137 if (hash == -1)
3138 return -1;
3139 }
INADA Naoki778928b2017-08-03 23:45:15 +09003140 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003141 if (ix == DKIX_ERROR)
3142 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003143 return (ix != DKIX_EMPTY && value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003144}
3145
Thomas Wouterscf297e42007-02-23 15:07:44 +00003146/* Internal version of PyDict_Contains used when the hash value is already known */
3147int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003148_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00003149{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003150 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003151 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -07003152 Py_ssize_t ix;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003153
INADA Naoki778928b2017-08-03 23:45:15 +09003154 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003155 if (ix == DKIX_ERROR)
3156 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003157 return (ix != DKIX_EMPTY && value != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00003158}
3159
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003160/* Hack to implement "key in dict" */
3161static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003162 0, /* sq_length */
3163 0, /* sq_concat */
3164 0, /* sq_repeat */
3165 0, /* sq_item */
3166 0, /* sq_slice */
3167 0, /* sq_ass_item */
3168 0, /* sq_ass_slice */
3169 PyDict_Contains, /* sq_contains */
3170 0, /* sq_inplace_concat */
3171 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003172};
3173
Guido van Rossum09e563a2001-05-01 12:10:21 +00003174static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003175dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3176{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003177 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003178 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003180 assert(type != NULL && type->tp_alloc != NULL);
3181 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02003182 if (self == NULL)
3183 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003184 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003185
Victor Stinnera9f61a52013-07-16 22:17:26 +02003186 /* The object has been implicitly tracked by tp_alloc */
3187 if (type == &PyDict_Type)
3188 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003189
3190 d->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003191 d->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07003192 d->ma_keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003193 if (d->ma_keys == NULL) {
3194 Py_DECREF(self);
3195 return NULL;
3196 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02003197 assert(_PyDict_CheckConsistency(d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003198 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003199}
3200
Tim Peters25786c02001-09-02 08:22:48 +00003201static int
3202dict_init(PyObject *self, PyObject *args, PyObject *kwds)
3203{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003204 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00003205}
3206
Tim Peters6d6c1a32001-08-02 04:15:00 +00003207static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003208dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00003209{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003210 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003211}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003212
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003213PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00003214"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00003215"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003216" (key, value) pairs\n"
3217"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00003218" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003219" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00003220" d[k] = v\n"
3221"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
3222" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00003223
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003224PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003225 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3226 "dict",
3227 sizeof(PyDictObject),
3228 0,
3229 (destructor)dict_dealloc, /* tp_dealloc */
3230 0, /* tp_print */
3231 0, /* tp_getattr */
3232 0, /* tp_setattr */
3233 0, /* tp_reserved */
3234 (reprfunc)dict_repr, /* tp_repr */
3235 0, /* tp_as_number */
3236 &dict_as_sequence, /* tp_as_sequence */
3237 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003238 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003239 0, /* tp_call */
3240 0, /* tp_str */
3241 PyObject_GenericGetAttr, /* tp_getattro */
3242 0, /* tp_setattro */
3243 0, /* tp_as_buffer */
3244 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3245 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
3246 dictionary_doc, /* tp_doc */
3247 dict_traverse, /* tp_traverse */
3248 dict_tp_clear, /* tp_clear */
3249 dict_richcompare, /* tp_richcompare */
3250 0, /* tp_weaklistoffset */
3251 (getiterfunc)dict_iter, /* tp_iter */
3252 0, /* tp_iternext */
3253 mapp_methods, /* tp_methods */
3254 0, /* tp_members */
3255 0, /* tp_getset */
3256 0, /* tp_base */
3257 0, /* tp_dict */
3258 0, /* tp_descr_get */
3259 0, /* tp_descr_set */
3260 0, /* tp_dictoffset */
3261 dict_init, /* tp_init */
3262 PyType_GenericAlloc, /* tp_alloc */
3263 dict_new, /* tp_new */
3264 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003265};
3266
Victor Stinner3c1e4812012-03-26 22:10:51 +02003267PyObject *
3268_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
3269{
3270 PyObject *kv;
3271 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02003272 if (kv == NULL) {
3273 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003274 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02003275 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02003276 return PyDict_GetItem(dp, kv);
3277}
3278
Guido van Rossum3cca2451997-05-16 14:23:33 +00003279/* For backward compatibility with old dictionary interface */
3280
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003281PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003282PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003283{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003284 PyObject *kv, *rv;
3285 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003286 if (kv == NULL) {
3287 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003288 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003289 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003290 rv = PyDict_GetItem(v, kv);
3291 Py_DECREF(kv);
3292 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003293}
3294
3295int
Victor Stinner3c1e4812012-03-26 22:10:51 +02003296_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
3297{
3298 PyObject *kv;
3299 kv = _PyUnicode_FromId(key); /* borrowed */
3300 if (kv == NULL)
3301 return -1;
3302 return PyDict_SetItem(v, kv, item);
3303}
3304
3305int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003306PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003307{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003308 PyObject *kv;
3309 int err;
3310 kv = PyUnicode_FromString(key);
3311 if (kv == NULL)
3312 return -1;
3313 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
3314 err = PyDict_SetItem(v, kv, item);
3315 Py_DECREF(kv);
3316 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003317}
3318
3319int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01003320_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
3321{
3322 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
3323 if (kv == NULL)
3324 return -1;
3325 return PyDict_DelItem(v, kv);
3326}
3327
3328int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003329PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003330{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003331 PyObject *kv;
3332 int err;
3333 kv = PyUnicode_FromString(key);
3334 if (kv == NULL)
3335 return -1;
3336 err = PyDict_DelItem(v, kv);
3337 Py_DECREF(kv);
3338 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003339}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003340
Raymond Hettinger019a1482004-03-18 02:41:19 +00003341/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003342
3343typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003344 PyObject_HEAD
3345 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
3346 Py_ssize_t di_used;
3347 Py_ssize_t di_pos;
3348 PyObject* di_result; /* reusable result tuple for iteritems */
3349 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003350} dictiterobject;
3351
3352static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003353dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003354{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003355 dictiterobject *di;
3356 di = PyObject_GC_New(dictiterobject, itertype);
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003357 if (di == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003358 return NULL;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003359 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003360 Py_INCREF(dict);
3361 di->di_dict = dict;
3362 di->di_used = dict->ma_used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003363 di->len = dict->ma_used;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003364 if ((itertype == &PyDictRevIterKey_Type ||
3365 itertype == &PyDictRevIterItem_Type ||
3366 itertype == &PyDictRevIterValue_Type) && dict->ma_used) {
3367 di->di_pos = dict->ma_keys->dk_nentries - 1;
3368 }
3369 else {
3370 di->di_pos = 0;
3371 }
3372 if (itertype == &PyDictIterItem_Type ||
3373 itertype == &PyDictRevIterItem_Type) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003374 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
3375 if (di->di_result == NULL) {
3376 Py_DECREF(di);
3377 return NULL;
3378 }
3379 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003380 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003381 di->di_result = NULL;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003382 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003383 _PyObject_GC_TRACK(di);
3384 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003385}
3386
3387static void
3388dictiter_dealloc(dictiterobject *di)
3389{
INADA Naokia6296d32017-08-24 14:55:17 +09003390 /* bpo-31095: UnTrack is needed before calling any callbacks */
3391 _PyObject_GC_UNTRACK(di);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003392 Py_XDECREF(di->di_dict);
3393 Py_XDECREF(di->di_result);
3394 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003395}
3396
3397static int
3398dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
3399{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003400 Py_VISIT(di->di_dict);
3401 Py_VISIT(di->di_result);
3402 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003403}
3404
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003405static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303406dictiter_len(dictiterobject *di, PyObject *Py_UNUSED(ignored))
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003407{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003408 Py_ssize_t len = 0;
3409 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
3410 len = di->len;
3411 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003412}
3413
Guido van Rossumb90c8482007-02-10 01:11:45 +00003414PyDoc_STRVAR(length_hint_doc,
3415 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003416
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003417static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303418dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003419
3420PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3421
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003422static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003423 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
3424 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003425 {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS,
3426 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003427 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003428};
3429
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003430static PyObject*
3431dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00003432{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003433 PyObject *key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003434 Py_ssize_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003435 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003436 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00003437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003438 if (d == NULL)
3439 return NULL;
3440 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00003441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003442 if (di->di_used != d->ma_used) {
3443 PyErr_SetString(PyExc_RuntimeError,
3444 "dictionary changed size during iteration");
3445 di->di_used = -1; /* Make this state sticky */
3446 return NULL;
3447 }
Guido van Rossum2147df72002-07-16 20:30:22 +00003448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003449 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003450 k = d->ma_keys;
INADA Naokica2d8be2016-11-04 16:59:10 +09003451 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003452 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003453 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003454 goto fail;
3455 key = DK_ENTRIES(k)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003456 assert(d->ma_values[i] != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003457 }
3458 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003459 Py_ssize_t n = k->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003460 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
3461 while (i < n && entry_ptr->me_value == NULL) {
3462 entry_ptr++;
3463 i++;
3464 }
3465 if (i >= n)
3466 goto fail;
3467 key = entry_ptr->me_key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003468 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003469 di->di_pos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003470 di->len--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003471 Py_INCREF(key);
3472 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003473
3474fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003475 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003476 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003477 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003478}
3479
Raymond Hettinger019a1482004-03-18 02:41:19 +00003480PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003481 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3482 "dict_keyiterator", /* tp_name */
3483 sizeof(dictiterobject), /* tp_basicsize */
3484 0, /* tp_itemsize */
3485 /* methods */
3486 (destructor)dictiter_dealloc, /* tp_dealloc */
3487 0, /* tp_print */
3488 0, /* tp_getattr */
3489 0, /* tp_setattr */
3490 0, /* tp_reserved */
3491 0, /* tp_repr */
3492 0, /* tp_as_number */
3493 0, /* tp_as_sequence */
3494 0, /* tp_as_mapping */
3495 0, /* tp_hash */
3496 0, /* tp_call */
3497 0, /* tp_str */
3498 PyObject_GenericGetAttr, /* tp_getattro */
3499 0, /* tp_setattro */
3500 0, /* tp_as_buffer */
3501 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3502 0, /* tp_doc */
3503 (traverseproc)dictiter_traverse, /* tp_traverse */
3504 0, /* tp_clear */
3505 0, /* tp_richcompare */
3506 0, /* tp_weaklistoffset */
3507 PyObject_SelfIter, /* tp_iter */
3508 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
3509 dictiter_methods, /* tp_methods */
3510 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003511};
3512
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003513static PyObject *
3514dictiter_iternextvalue(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003515{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003516 PyObject *value;
INADA Naokica2d8be2016-11-04 16:59:10 +09003517 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003518 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003520 if (d == NULL)
3521 return NULL;
3522 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003524 if (di->di_used != d->ma_used) {
3525 PyErr_SetString(PyExc_RuntimeError,
3526 "dictionary changed size during iteration");
3527 di->di_used = -1; /* Make this state sticky */
3528 return NULL;
3529 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003531 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003532 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003533 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003534 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003535 goto fail;
INADA Naokica2d8be2016-11-04 16:59:10 +09003536 value = d->ma_values[i];
3537 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003538 }
3539 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003540 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003541 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3542 while (i < n && entry_ptr->me_value == NULL) {
3543 entry_ptr++;
3544 i++;
3545 }
3546 if (i >= n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003547 goto fail;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003548 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003549 }
3550 di->di_pos = i+1;
3551 di->len--;
3552 Py_INCREF(value);
3553 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003554
3555fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003556 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003557 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003558 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003559}
3560
3561PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003562 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3563 "dict_valueiterator", /* tp_name */
3564 sizeof(dictiterobject), /* tp_basicsize */
3565 0, /* tp_itemsize */
3566 /* methods */
3567 (destructor)dictiter_dealloc, /* tp_dealloc */
3568 0, /* tp_print */
3569 0, /* tp_getattr */
3570 0, /* tp_setattr */
3571 0, /* tp_reserved */
3572 0, /* tp_repr */
3573 0, /* tp_as_number */
3574 0, /* tp_as_sequence */
3575 0, /* tp_as_mapping */
3576 0, /* tp_hash */
3577 0, /* tp_call */
3578 0, /* tp_str */
3579 PyObject_GenericGetAttr, /* tp_getattro */
3580 0, /* tp_setattro */
3581 0, /* tp_as_buffer */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003582 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003583 0, /* tp_doc */
3584 (traverseproc)dictiter_traverse, /* tp_traverse */
3585 0, /* tp_clear */
3586 0, /* tp_richcompare */
3587 0, /* tp_weaklistoffset */
3588 PyObject_SelfIter, /* tp_iter */
3589 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3590 dictiter_methods, /* tp_methods */
3591 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003592};
3593
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003594static PyObject *
3595dictiter_iternextitem(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003596{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003597 PyObject *key, *value, *result;
INADA Naokica2d8be2016-11-04 16:59:10 +09003598 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003599 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003601 if (d == NULL)
3602 return NULL;
3603 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003605 if (di->di_used != d->ma_used) {
3606 PyErr_SetString(PyExc_RuntimeError,
3607 "dictionary changed size during iteration");
3608 di->di_used = -1; /* Make this state sticky */
3609 return NULL;
3610 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003612 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003613 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003614 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003615 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003616 goto fail;
3617 key = DK_ENTRIES(d->ma_keys)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003618 value = d->ma_values[i];
3619 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003620 }
3621 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003622 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003623 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3624 while (i < n && entry_ptr->me_value == NULL) {
3625 entry_ptr++;
3626 i++;
3627 }
3628 if (i >= n)
3629 goto fail;
3630 key = entry_ptr->me_key;
3631 value = entry_ptr->me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003632 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003633 di->di_pos = i+1;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003634 di->len--;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003635 Py_INCREF(key);
3636 Py_INCREF(value);
3637 result = di->di_result;
3638 if (Py_REFCNT(result) == 1) {
3639 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3640 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3641 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3642 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003643 Py_INCREF(result);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003644 Py_DECREF(oldkey);
3645 Py_DECREF(oldvalue);
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003646 }
3647 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003648 result = PyTuple_New(2);
3649 if (result == NULL)
3650 return NULL;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003651 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3652 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003653 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003654 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003655
3656fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003657 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003658 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003659 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003660}
3661
3662PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003663 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3664 "dict_itemiterator", /* tp_name */
3665 sizeof(dictiterobject), /* tp_basicsize */
3666 0, /* tp_itemsize */
3667 /* methods */
3668 (destructor)dictiter_dealloc, /* tp_dealloc */
3669 0, /* tp_print */
3670 0, /* tp_getattr */
3671 0, /* tp_setattr */
3672 0, /* tp_reserved */
3673 0, /* tp_repr */
3674 0, /* tp_as_number */
3675 0, /* tp_as_sequence */
3676 0, /* tp_as_mapping */
3677 0, /* tp_hash */
3678 0, /* tp_call */
3679 0, /* tp_str */
3680 PyObject_GenericGetAttr, /* tp_getattro */
3681 0, /* tp_setattro */
3682 0, /* tp_as_buffer */
3683 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3684 0, /* tp_doc */
3685 (traverseproc)dictiter_traverse, /* tp_traverse */
3686 0, /* tp_clear */
3687 0, /* tp_richcompare */
3688 0, /* tp_weaklistoffset */
3689 PyObject_SelfIter, /* tp_iter */
3690 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3691 dictiter_methods, /* tp_methods */
3692 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003693};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003694
3695
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003696/* dictreviter */
3697
3698static PyObject *
3699dictreviter_iternext(dictiterobject *di)
3700{
3701 PyDictObject *d = di->di_dict;
3702
3703 if (d == NULL) {
3704 return NULL;
3705 }
3706 assert (PyDict_Check(d));
3707
3708 if (di->di_used != d->ma_used) {
3709 PyErr_SetString(PyExc_RuntimeError,
3710 "dictionary changed size during iteration");
3711 di->di_used = -1; /* Make this state sticky */
3712 return NULL;
3713 }
3714
3715 Py_ssize_t i = di->di_pos;
3716 PyDictKeysObject *k = d->ma_keys;
3717 PyObject *key, *value, *result;
3718
3719 if (d->ma_values) {
3720 if (i < 0) {
3721 goto fail;
3722 }
3723 key = DK_ENTRIES(k)[i].me_key;
3724 value = d->ma_values[i];
3725 assert (value != NULL);
3726 }
3727 else {
3728 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
3729 while (i >= 0 && entry_ptr->me_value == NULL) {
3730 entry_ptr--;
3731 i--;
3732 }
3733 if (i < 0) {
3734 goto fail;
3735 }
3736 key = entry_ptr->me_key;
3737 value = entry_ptr->me_value;
3738 }
3739 di->di_pos = i-1;
3740 di->len--;
3741
3742 if (Py_TYPE(di) == &PyDictRevIterKey_Type) {
3743 Py_INCREF(key);
3744 return key;
3745 }
3746 else if (Py_TYPE(di) == &PyDictRevIterValue_Type) {
3747 Py_INCREF(value);
3748 return value;
3749 }
3750 else if (Py_TYPE(di) == &PyDictRevIterItem_Type) {
3751 Py_INCREF(key);
3752 Py_INCREF(value);
3753 result = di->di_result;
3754 if (Py_REFCNT(result) == 1) {
3755 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3756 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3757 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3758 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
3759 Py_INCREF(result);
3760 Py_DECREF(oldkey);
3761 Py_DECREF(oldvalue);
3762 }
3763 else {
3764 result = PyTuple_New(2);
3765 if (result == NULL) {
3766 return NULL;
3767 }
3768 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3769 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
3770 }
3771 return result;
3772 }
3773 else {
3774 Py_UNREACHABLE();
3775 }
3776
3777fail:
3778 di->di_dict = NULL;
3779 Py_DECREF(d);
3780 return NULL;
3781}
3782
3783PyTypeObject PyDictRevIterKey_Type = {
3784 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3785 "dict_reversekeyiterator",
3786 sizeof(dictiterobject),
3787 .tp_dealloc = (destructor)dictiter_dealloc,
3788 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
3789 .tp_traverse = (traverseproc)dictiter_traverse,
3790 .tp_iter = PyObject_SelfIter,
3791 .tp_iternext = (iternextfunc)dictreviter_iternext,
3792 .tp_methods = dictiter_methods
3793};
3794
3795
3796/*[clinic input]
3797dict.__reversed__
3798
3799Return a reverse iterator over the dict keys.
3800[clinic start generated code]*/
3801
3802static PyObject *
3803dict___reversed___impl(PyDictObject *self)
3804/*[clinic end generated code: output=e674483336d1ed51 input=23210ef3477d8c4d]*/
3805{
3806 assert (PyDict_Check(self));
3807 return dictiter_new(self, &PyDictRevIterKey_Type);
3808}
3809
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003810static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303811dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003812{
Sergey Fedoseev63958442018-10-20 05:43:33 +05003813 /* copy the iterator state */
3814 dictiterobject tmp = *di;
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003815 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003816
Sergey Fedoseev63958442018-10-20 05:43:33 +05003817 PyObject *list = PySequence_List((PyObject*)&tmp);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003818 Py_XDECREF(tmp.di_dict);
Sergey Fedoseev63958442018-10-20 05:43:33 +05003819 if (list == NULL) {
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003820 return NULL;
3821 }
Antoine Pitroua7013882012-04-05 00:04:20 +02003822 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003823}
3824
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003825PyTypeObject PyDictRevIterItem_Type = {
3826 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3827 "dict_reverseitemiterator",
3828 sizeof(dictiterobject),
3829 .tp_dealloc = (destructor)dictiter_dealloc,
3830 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
3831 .tp_traverse = (traverseproc)dictiter_traverse,
3832 .tp_iter = PyObject_SelfIter,
3833 .tp_iternext = (iternextfunc)dictreviter_iternext,
3834 .tp_methods = dictiter_methods
3835};
3836
3837PyTypeObject PyDictRevIterValue_Type = {
3838 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3839 "dict_reversevalueiterator",
3840 sizeof(dictiterobject),
3841 .tp_dealloc = (destructor)dictiter_dealloc,
3842 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
3843 .tp_traverse = (traverseproc)dictiter_traverse,
3844 .tp_iter = PyObject_SelfIter,
3845 .tp_iternext = (iternextfunc)dictreviter_iternext,
3846 .tp_methods = dictiter_methods
3847};
3848
Guido van Rossum3ac67412007-02-10 18:55:06 +00003849/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003850/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003851/***********************************************/
3852
Guido van Rossumb90c8482007-02-10 01:11:45 +00003853/* The instance lay-out is the same for all three; but the type differs. */
3854
Guido van Rossumb90c8482007-02-10 01:11:45 +00003855static void
Eric Snow96c6af92015-05-29 22:21:39 -06003856dictview_dealloc(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003857{
INADA Naokia6296d32017-08-24 14:55:17 +09003858 /* bpo-31095: UnTrack is needed before calling any callbacks */
3859 _PyObject_GC_UNTRACK(dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003860 Py_XDECREF(dv->dv_dict);
3861 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003862}
3863
3864static int
Eric Snow96c6af92015-05-29 22:21:39 -06003865dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003866{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003867 Py_VISIT(dv->dv_dict);
3868 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003869}
3870
Guido van Rossum83825ac2007-02-10 04:54:19 +00003871static Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003872dictview_len(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003873{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003874 Py_ssize_t len = 0;
3875 if (dv->dv_dict != NULL)
3876 len = dv->dv_dict->ma_used;
3877 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003878}
3879
Eric Snow96c6af92015-05-29 22:21:39 -06003880PyObject *
3881_PyDictView_New(PyObject *dict, PyTypeObject *type)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003882{
Eric Snow96c6af92015-05-29 22:21:39 -06003883 _PyDictViewObject *dv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003884 if (dict == NULL) {
3885 PyErr_BadInternalCall();
3886 return NULL;
3887 }
3888 if (!PyDict_Check(dict)) {
3889 /* XXX Get rid of this restriction later */
3890 PyErr_Format(PyExc_TypeError,
3891 "%s() requires a dict argument, not '%s'",
3892 type->tp_name, dict->ob_type->tp_name);
3893 return NULL;
3894 }
Eric Snow96c6af92015-05-29 22:21:39 -06003895 dv = PyObject_GC_New(_PyDictViewObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003896 if (dv == NULL)
3897 return NULL;
3898 Py_INCREF(dict);
3899 dv->dv_dict = (PyDictObject *)dict;
3900 _PyObject_GC_TRACK(dv);
3901 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003902}
3903
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003904/* TODO(guido): The views objects are not complete:
3905
3906 * support more set operations
3907 * support arbitrary mappings?
3908 - either these should be static or exported in dictobject.h
3909 - if public then they should probably be in builtins
3910*/
3911
Guido van Rossumaac530c2007-08-24 22:33:45 +00003912/* Return 1 if self is a subset of other, iterating over self;
3913 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003914static int
3915all_contained_in(PyObject *self, PyObject *other)
3916{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003917 PyObject *iter = PyObject_GetIter(self);
3918 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003920 if (iter == NULL)
3921 return -1;
3922 for (;;) {
3923 PyObject *next = PyIter_Next(iter);
3924 if (next == NULL) {
3925 if (PyErr_Occurred())
3926 ok = -1;
3927 break;
3928 }
3929 ok = PySequence_Contains(other, next);
3930 Py_DECREF(next);
3931 if (ok <= 0)
3932 break;
3933 }
3934 Py_DECREF(iter);
3935 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003936}
3937
3938static PyObject *
3939dictview_richcompare(PyObject *self, PyObject *other, int op)
3940{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003941 Py_ssize_t len_self, len_other;
3942 int ok;
3943 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003944
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003945 assert(self != NULL);
3946 assert(PyDictViewSet_Check(self));
3947 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00003948
Brian Curtindfc80e32011-08-10 20:28:54 -05003949 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
3950 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003952 len_self = PyObject_Size(self);
3953 if (len_self < 0)
3954 return NULL;
3955 len_other = PyObject_Size(other);
3956 if (len_other < 0)
3957 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003958
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003959 ok = 0;
3960 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00003961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003962 case Py_NE:
3963 case Py_EQ:
3964 if (len_self == len_other)
3965 ok = all_contained_in(self, other);
3966 if (op == Py_NE && ok >= 0)
3967 ok = !ok;
3968 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003970 case Py_LT:
3971 if (len_self < len_other)
3972 ok = all_contained_in(self, other);
3973 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003975 case Py_LE:
3976 if (len_self <= len_other)
3977 ok = all_contained_in(self, other);
3978 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003980 case Py_GT:
3981 if (len_self > len_other)
3982 ok = all_contained_in(other, self);
3983 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003985 case Py_GE:
3986 if (len_self >= len_other)
3987 ok = all_contained_in(other, self);
3988 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003990 }
3991 if (ok < 0)
3992 return NULL;
3993 result = ok ? Py_True : Py_False;
3994 Py_INCREF(result);
3995 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003996}
3997
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003998static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003999dictview_repr(_PyDictViewObject *dv)
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004000{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004001 PyObject *seq;
bennorthd7773d92018-01-26 15:46:01 +00004002 PyObject *result = NULL;
4003 Py_ssize_t rc;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004004
bennorthd7773d92018-01-26 15:46:01 +00004005 rc = Py_ReprEnter((PyObject *)dv);
4006 if (rc != 0) {
4007 return rc > 0 ? PyUnicode_FromString("...") : NULL;
4008 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004009 seq = PySequence_List((PyObject *)dv);
bennorthd7773d92018-01-26 15:46:01 +00004010 if (seq == NULL) {
4011 goto Done;
4012 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004013 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
4014 Py_DECREF(seq);
bennorthd7773d92018-01-26 15:46:01 +00004015
4016Done:
4017 Py_ReprLeave((PyObject *)dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004018 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004019}
4020
Guido van Rossum3ac67412007-02-10 18:55:06 +00004021/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004022
4023static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004024dictkeys_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004025{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004026 if (dv->dv_dict == NULL) {
4027 Py_RETURN_NONE;
4028 }
4029 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004030}
4031
4032static int
Eric Snow96c6af92015-05-29 22:21:39 -06004033dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004034{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004035 if (dv->dv_dict == NULL)
4036 return 0;
4037 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004038}
4039
Guido van Rossum83825ac2007-02-10 04:54:19 +00004040static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004041 (lenfunc)dictview_len, /* sq_length */
4042 0, /* sq_concat */
4043 0, /* sq_repeat */
4044 0, /* sq_item */
4045 0, /* sq_slice */
4046 0, /* sq_ass_item */
4047 0, /* sq_ass_slice */
4048 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004049};
4050
Guido van Rossum523259b2007-08-24 23:41:22 +00004051static PyObject*
4052dictviews_sub(PyObject* self, PyObject *other)
4053{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004054 PyObject *result = PySet_New(self);
4055 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004056 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004058 if (result == NULL)
4059 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004060
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08004061 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004062 if (tmp == NULL) {
4063 Py_DECREF(result);
4064 return NULL;
4065 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004067 Py_DECREF(tmp);
4068 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004069}
4070
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004071PyObject*
4072_PyDictView_Intersect(PyObject* self, PyObject *other)
Guido van Rossum523259b2007-08-24 23:41:22 +00004073{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004074 PyObject *result = PySet_New(self);
4075 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004076 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004078 if (result == NULL)
4079 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004080
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08004081 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_intersection_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004082 if (tmp == NULL) {
4083 Py_DECREF(result);
4084 return NULL;
4085 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004087 Py_DECREF(tmp);
4088 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004089}
4090
4091static PyObject*
4092dictviews_or(PyObject* self, PyObject *other)
4093{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004094 PyObject *result = PySet_New(self);
4095 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004096 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02004097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004098 if (result == NULL)
4099 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004100
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08004101 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004102 if (tmp == NULL) {
4103 Py_DECREF(result);
4104 return NULL;
4105 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004107 Py_DECREF(tmp);
4108 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004109}
4110
4111static PyObject*
4112dictviews_xor(PyObject* self, PyObject *other)
4113{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004114 PyObject *result = PySet_New(self);
4115 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004116 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004118 if (result == NULL)
4119 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004120
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08004121 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_symmetric_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004122 if (tmp == NULL) {
4123 Py_DECREF(result);
4124 return NULL;
4125 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004127 Py_DECREF(tmp);
4128 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004129}
4130
4131static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004132 0, /*nb_add*/
4133 (binaryfunc)dictviews_sub, /*nb_subtract*/
4134 0, /*nb_multiply*/
4135 0, /*nb_remainder*/
4136 0, /*nb_divmod*/
4137 0, /*nb_power*/
4138 0, /*nb_negative*/
4139 0, /*nb_positive*/
4140 0, /*nb_absolute*/
4141 0, /*nb_bool*/
4142 0, /*nb_invert*/
4143 0, /*nb_lshift*/
4144 0, /*nb_rshift*/
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004145 (binaryfunc)_PyDictView_Intersect, /*nb_and*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004146 (binaryfunc)dictviews_xor, /*nb_xor*/
4147 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00004148};
4149
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004150static PyObject*
4151dictviews_isdisjoint(PyObject *self, PyObject *other)
4152{
4153 PyObject *it;
4154 PyObject *item = NULL;
4155
4156 if (self == other) {
Eric Snow96c6af92015-05-29 22:21:39 -06004157 if (dictview_len((_PyDictViewObject *)self) == 0)
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004158 Py_RETURN_TRUE;
4159 else
4160 Py_RETURN_FALSE;
4161 }
4162
4163 /* Iterate over the shorter object (only if other is a set,
4164 * because PySequence_Contains may be expensive otherwise): */
4165 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
Eric Snow96c6af92015-05-29 22:21:39 -06004166 Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self);
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004167 Py_ssize_t len_other = PyObject_Size(other);
4168 if (len_other == -1)
4169 return NULL;
4170
4171 if ((len_other > len_self)) {
4172 PyObject *tmp = other;
4173 other = self;
4174 self = tmp;
4175 }
4176 }
4177
4178 it = PyObject_GetIter(other);
4179 if (it == NULL)
4180 return NULL;
4181
4182 while ((item = PyIter_Next(it)) != NULL) {
4183 int contains = PySequence_Contains(self, item);
4184 Py_DECREF(item);
4185 if (contains == -1) {
4186 Py_DECREF(it);
4187 return NULL;
4188 }
4189
4190 if (contains) {
4191 Py_DECREF(it);
4192 Py_RETURN_FALSE;
4193 }
4194 }
4195 Py_DECREF(it);
4196 if (PyErr_Occurred())
4197 return NULL; /* PyIter_Next raised an exception. */
4198 Py_RETURN_TRUE;
4199}
4200
4201PyDoc_STRVAR(isdisjoint_doc,
4202"Return True if the view and the given iterable have a null intersection.");
4203
Serhiy Storchaka81524022018-11-27 13:05:02 +02004204static PyObject* dictkeys_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored));
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004205
4206PyDoc_STRVAR(reversed_keys_doc,
4207"Return a reverse iterator over the dict keys.");
4208
Guido van Rossumb90c8482007-02-10 01:11:45 +00004209static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004210 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4211 isdisjoint_doc},
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004212 {"__reversed__", (PyCFunction)dictkeys_reversed, METH_NOARGS,
4213 reversed_keys_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004214 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004215};
4216
4217PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004218 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4219 "dict_keys", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004220 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004221 0, /* tp_itemsize */
4222 /* methods */
4223 (destructor)dictview_dealloc, /* tp_dealloc */
4224 0, /* tp_print */
4225 0, /* tp_getattr */
4226 0, /* tp_setattr */
4227 0, /* tp_reserved */
4228 (reprfunc)dictview_repr, /* tp_repr */
4229 &dictviews_as_number, /* tp_as_number */
4230 &dictkeys_as_sequence, /* tp_as_sequence */
4231 0, /* tp_as_mapping */
4232 0, /* tp_hash */
4233 0, /* tp_call */
4234 0, /* tp_str */
4235 PyObject_GenericGetAttr, /* tp_getattro */
4236 0, /* tp_setattro */
4237 0, /* tp_as_buffer */
4238 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4239 0, /* tp_doc */
4240 (traverseproc)dictview_traverse, /* tp_traverse */
4241 0, /* tp_clear */
4242 dictview_richcompare, /* tp_richcompare */
4243 0, /* tp_weaklistoffset */
4244 (getiterfunc)dictkeys_iter, /* tp_iter */
4245 0, /* tp_iternext */
4246 dictkeys_methods, /* tp_methods */
4247 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004248};
4249
4250static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304251dictkeys_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004252{
Eric Snow96c6af92015-05-29 22:21:39 -06004253 return _PyDictView_New(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004254}
4255
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004256static PyObject *
Serhiy Storchaka81524022018-11-27 13:05:02 +02004257dictkeys_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored))
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004258{
4259 if (dv->dv_dict == NULL) {
4260 Py_RETURN_NONE;
4261 }
4262 return dictiter_new(dv->dv_dict, &PyDictRevIterKey_Type);
4263}
4264
Guido van Rossum3ac67412007-02-10 18:55:06 +00004265/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004266
4267static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004268dictitems_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004270 if (dv->dv_dict == NULL) {
4271 Py_RETURN_NONE;
4272 }
4273 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004274}
4275
4276static int
Eric Snow96c6af92015-05-29 22:21:39 -06004277dictitems_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004278{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004279 int result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004280 PyObject *key, *value, *found;
4281 if (dv->dv_dict == NULL)
4282 return 0;
4283 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
4284 return 0;
4285 key = PyTuple_GET_ITEM(obj, 0);
4286 value = PyTuple_GET_ITEM(obj, 1);
Raymond Hettinger6692f012016-09-18 21:46:08 -07004287 found = PyDict_GetItemWithError((PyObject *)dv->dv_dict, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004288 if (found == NULL) {
4289 if (PyErr_Occurred())
4290 return -1;
4291 return 0;
4292 }
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004293 Py_INCREF(found);
4294 result = PyObject_RichCompareBool(value, found, Py_EQ);
4295 Py_DECREF(found);
4296 return result;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004297}
4298
Guido van Rossum83825ac2007-02-10 04:54:19 +00004299static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004300 (lenfunc)dictview_len, /* sq_length */
4301 0, /* sq_concat */
4302 0, /* sq_repeat */
4303 0, /* sq_item */
4304 0, /* sq_slice */
4305 0, /* sq_ass_item */
4306 0, /* sq_ass_slice */
4307 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004308};
4309
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004310static PyObject* dictitems_reversed(_PyDictViewObject *dv);
4311
4312PyDoc_STRVAR(reversed_items_doc,
4313"Return a reverse iterator over the dict items.");
4314
Guido van Rossumb90c8482007-02-10 01:11:45 +00004315static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004316 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4317 isdisjoint_doc},
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004318 {"__reversed__", (PyCFunction)dictitems_reversed, METH_NOARGS,
4319 reversed_items_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004320 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004321};
4322
4323PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004324 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4325 "dict_items", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004326 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004327 0, /* tp_itemsize */
4328 /* methods */
4329 (destructor)dictview_dealloc, /* tp_dealloc */
4330 0, /* tp_print */
4331 0, /* tp_getattr */
4332 0, /* tp_setattr */
4333 0, /* tp_reserved */
4334 (reprfunc)dictview_repr, /* tp_repr */
4335 &dictviews_as_number, /* tp_as_number */
4336 &dictitems_as_sequence, /* tp_as_sequence */
4337 0, /* tp_as_mapping */
4338 0, /* tp_hash */
4339 0, /* tp_call */
4340 0, /* tp_str */
4341 PyObject_GenericGetAttr, /* tp_getattro */
4342 0, /* tp_setattro */
4343 0, /* tp_as_buffer */
4344 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4345 0, /* tp_doc */
4346 (traverseproc)dictview_traverse, /* tp_traverse */
4347 0, /* tp_clear */
4348 dictview_richcompare, /* tp_richcompare */
4349 0, /* tp_weaklistoffset */
4350 (getiterfunc)dictitems_iter, /* tp_iter */
4351 0, /* tp_iternext */
4352 dictitems_methods, /* tp_methods */
4353 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004354};
4355
4356static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304357dictitems_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004358{
Eric Snow96c6af92015-05-29 22:21:39 -06004359 return _PyDictView_New(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004360}
4361
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004362static PyObject *
4363dictitems_reversed(_PyDictViewObject *dv)
4364{
4365 if (dv->dv_dict == NULL) {
4366 Py_RETURN_NONE;
4367 }
4368 return dictiter_new(dv->dv_dict, &PyDictRevIterItem_Type);
4369}
4370
Guido van Rossum3ac67412007-02-10 18:55:06 +00004371/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004372
4373static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004374dictvalues_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004375{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004376 if (dv->dv_dict == NULL) {
4377 Py_RETURN_NONE;
4378 }
4379 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004380}
4381
Guido van Rossum83825ac2007-02-10 04:54:19 +00004382static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004383 (lenfunc)dictview_len, /* sq_length */
4384 0, /* sq_concat */
4385 0, /* sq_repeat */
4386 0, /* sq_item */
4387 0, /* sq_slice */
4388 0, /* sq_ass_item */
4389 0, /* sq_ass_slice */
4390 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004391};
4392
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004393static PyObject* dictvalues_reversed(_PyDictViewObject *dv);
4394
4395PyDoc_STRVAR(reversed_values_doc,
4396"Return a reverse iterator over the dict values.");
4397
Guido van Rossumb90c8482007-02-10 01:11:45 +00004398static PyMethodDef dictvalues_methods[] = {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004399 {"__reversed__", (PyCFunction)dictvalues_reversed, METH_NOARGS,
4400 reversed_values_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004401 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004402};
4403
4404PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004405 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4406 "dict_values", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004407 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004408 0, /* tp_itemsize */
4409 /* methods */
4410 (destructor)dictview_dealloc, /* tp_dealloc */
4411 0, /* tp_print */
4412 0, /* tp_getattr */
4413 0, /* tp_setattr */
4414 0, /* tp_reserved */
4415 (reprfunc)dictview_repr, /* tp_repr */
4416 0, /* tp_as_number */
4417 &dictvalues_as_sequence, /* tp_as_sequence */
4418 0, /* tp_as_mapping */
4419 0, /* tp_hash */
4420 0, /* tp_call */
4421 0, /* tp_str */
4422 PyObject_GenericGetAttr, /* tp_getattro */
4423 0, /* tp_setattro */
4424 0, /* tp_as_buffer */
4425 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4426 0, /* tp_doc */
4427 (traverseproc)dictview_traverse, /* tp_traverse */
4428 0, /* tp_clear */
4429 0, /* tp_richcompare */
4430 0, /* tp_weaklistoffset */
4431 (getiterfunc)dictvalues_iter, /* tp_iter */
4432 0, /* tp_iternext */
4433 dictvalues_methods, /* tp_methods */
4434 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004435};
4436
4437static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304438dictvalues_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004439{
Eric Snow96c6af92015-05-29 22:21:39 -06004440 return _PyDictView_New(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004441}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004442
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004443static PyObject *
4444dictvalues_reversed(_PyDictViewObject *dv)
4445{
4446 if (dv->dv_dict == NULL) {
4447 Py_RETURN_NONE;
4448 }
4449 return dictiter_new(dv->dv_dict, &PyDictRevIterValue_Type);
4450}
4451
4452
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004453/* Returns NULL if cannot allocate a new PyDictKeysObject,
4454 but does not set an error */
4455PyDictKeysObject *
4456_PyDict_NewKeysForClass(void)
4457{
Victor Stinner742da042016-09-07 17:40:12 -07004458 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004459 if (keys == NULL)
4460 PyErr_Clear();
4461 else
4462 keys->dk_lookup = lookdict_split;
4463 return keys;
4464}
4465
4466#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
4467
4468PyObject *
4469PyObject_GenericGetDict(PyObject *obj, void *context)
4470{
4471 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
4472 if (dictptr == NULL) {
4473 PyErr_SetString(PyExc_AttributeError,
4474 "This object has no __dict__");
4475 return NULL;
4476 }
4477 dict = *dictptr;
4478 if (dict == NULL) {
4479 PyTypeObject *tp = Py_TYPE(obj);
4480 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
INADA Naokia7576492018-11-14 18:39:27 +09004481 dictkeys_incref(CACHED_KEYS(tp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004482 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
4483 }
4484 else {
4485 *dictptr = dict = PyDict_New();
4486 }
4487 }
4488 Py_XINCREF(dict);
4489 return dict;
4490}
4491
4492int
4493_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
Victor Stinner742da042016-09-07 17:40:12 -07004494 PyObject *key, PyObject *value)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004495{
4496 PyObject *dict;
4497 int res;
4498 PyDictKeysObject *cached;
4499
4500 assert(dictptr != NULL);
4501 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
4502 assert(dictptr != NULL);
4503 dict = *dictptr;
4504 if (dict == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +09004505 dictkeys_incref(cached);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004506 dict = new_dict_with_shared_keys(cached);
4507 if (dict == NULL)
4508 return -1;
4509 *dictptr = dict;
4510 }
4511 if (value == NULL) {
4512 res = PyDict_DelItem(dict, key);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004513 // Since key sharing dict doesn't allow deletion, PyDict_DelItem()
4514 // always converts dict to combined form.
4515 if ((cached = CACHED_KEYS(tp)) != NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004516 CACHED_KEYS(tp) = NULL;
INADA Naokia7576492018-11-14 18:39:27 +09004517 dictkeys_decref(cached);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004518 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01004519 }
4520 else {
INADA Naoki2294f3a2017-02-12 13:51:30 +09004521 int was_shared = (cached == ((PyDictObject *)dict)->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004522 res = PyDict_SetItem(dict, key, value);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004523 if (was_shared &&
4524 (cached = CACHED_KEYS(tp)) != NULL &&
4525 cached != ((PyDictObject *)dict)->ma_keys) {
Victor Stinner3d3f2642016-12-15 17:21:23 +01004526 /* PyDict_SetItem() may call dictresize and convert split table
4527 * into combined table. In such case, convert it to split
4528 * table again and update type's shared key only when this is
4529 * the only dict sharing key with the type.
4530 *
4531 * This is to allow using shared key in class like this:
4532 *
4533 * class C:
4534 * def __init__(self):
4535 * # one dict resize happens
4536 * self.a, self.b, self.c = 1, 2, 3
4537 * self.d, self.e, self.f = 4, 5, 6
4538 * a = C()
4539 */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004540 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004541 CACHED_KEYS(tp) = make_keys_shared(dict);
Victor Stinner742da042016-09-07 17:40:12 -07004542 }
4543 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004544 CACHED_KEYS(tp) = NULL;
4545 }
INADA Naokia7576492018-11-14 18:39:27 +09004546 dictkeys_decref(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004547 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
4548 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004549 }
4550 }
4551 } else {
4552 dict = *dictptr;
4553 if (dict == NULL) {
4554 dict = PyDict_New();
4555 if (dict == NULL)
4556 return -1;
4557 *dictptr = dict;
4558 }
4559 if (value == NULL) {
4560 res = PyDict_DelItem(dict, key);
4561 } else {
4562 res = PyDict_SetItem(dict, key, value);
4563 }
4564 }
4565 return res;
4566}
4567
4568void
4569_PyDictKeys_DecRef(PyDictKeysObject *keys)
4570{
INADA Naokia7576492018-11-14 18:39:27 +09004571 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004572}