blob: 83cadda84c6dc4cc02ddfa246237d5c836950775 [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
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001422PyObject *
1423_PyDict_GetItemStringWithError(PyObject *v, const char *key)
1424{
1425 PyObject *kv, *rv;
1426 kv = PyUnicode_FromString(key);
1427 if (kv == NULL) {
1428 return NULL;
1429 }
1430 rv = PyDict_GetItemWithError(v, kv);
1431 Py_DECREF(kv);
1432 return rv;
1433}
1434
Victor Stinnerb4efc962015-11-20 09:24:02 +01001435/* Fast version of global value lookup (LOAD_GLOBAL).
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001436 * Lookup in globals, then builtins.
Victor Stinnerb4efc962015-11-20 09:24:02 +01001437 *
1438 * Raise an exception and return NULL if an error occurred (ex: computing the
1439 * key hash failed, key comparison failed, ...). Return NULL if the key doesn't
1440 * exist. Return the value if the key exists.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001441 */
1442PyObject *
1443_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001444{
Victor Stinner742da042016-09-07 17:40:12 -07001445 Py_ssize_t ix;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001446 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09001447 PyObject *value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001448
1449 if (!PyUnicode_CheckExact(key) ||
1450 (hash = ((PyASCIIObject *) key)->hash) == -1)
1451 {
1452 hash = PyObject_Hash(key);
1453 if (hash == -1)
1454 return NULL;
Antoine Pitroue965d972012-02-27 00:45:12 +01001455 }
Victor Stinnerb4efc962015-11-20 09:24:02 +01001456
1457 /* namespace 1: globals */
INADA Naoki778928b2017-08-03 23:45:15 +09001458 ix = globals->ma_keys->dk_lookup(globals, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001459 if (ix == DKIX_ERROR)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001460 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001461 if (ix != DKIX_EMPTY && value != NULL)
1462 return value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001463
1464 /* namespace 2: builtins */
INADA Naoki778928b2017-08-03 23:45:15 +09001465 ix = builtins->ma_keys->dk_lookup(builtins, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001466 if (ix < 0)
Victor Stinnerb4efc962015-11-20 09:24:02 +01001467 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001468 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001469}
1470
Antoine Pitroue965d972012-02-27 00:45:12 +01001471/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1472 * dictionary if it's merely replacing the value for an existing key.
1473 * This means that it's safe to loop over a dictionary with PyDict_Next()
1474 * and occasionally replace a value -- but you can't insert new keys or
1475 * remove them.
1476 */
1477int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001478PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001479{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001480 PyDictObject *mp;
1481 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001482 if (!PyDict_Check(op)) {
1483 PyErr_BadInternalCall();
1484 return -1;
1485 }
1486 assert(key);
1487 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001488 mp = (PyDictObject *)op;
1489 if (!PyUnicode_CheckExact(key) ||
1490 (hash = ((PyASCIIObject *) key)->hash) == -1)
1491 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001492 hash = PyObject_Hash(key);
1493 if (hash == -1)
1494 return -1;
1495 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001496
1497 /* insertdict() handles any resizing that might be necessary */
1498 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001499}
1500
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001501int
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001502_PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
1503 Py_hash_t hash)
1504{
1505 PyDictObject *mp;
1506
1507 if (!PyDict_Check(op)) {
1508 PyErr_BadInternalCall();
1509 return -1;
1510 }
1511 assert(key);
1512 assert(value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001513 assert(hash != -1);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001514 mp = (PyDictObject *)op;
1515
1516 /* insertdict() handles any resizing that might be necessary */
1517 return insertdict(mp, key, hash, value);
1518}
1519
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001520static int
INADA Naoki778928b2017-08-03 23:45:15 +09001521delitem_common(PyDictObject *mp, Py_hash_t hash, Py_ssize_t ix,
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001522 PyObject *old_value)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001523{
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001524 PyObject *old_key;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001525 PyDictKeyEntry *ep;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001526
INADA Naoki778928b2017-08-03 23:45:15 +09001527 Py_ssize_t hashpos = lookdict_index(mp->ma_keys, hash, ix);
1528 assert(hashpos >= 0);
1529
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001530 mp->ma_used--;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001531 mp->ma_version_tag = DICT_NEXT_VERSION();
1532 ep = &DK_ENTRIES(mp->ma_keys)[ix];
INADA Naokia7576492018-11-14 18:39:27 +09001533 dictkeys_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001534 ENSURE_ALLOWS_DELETIONS(mp);
1535 old_key = ep->me_key;
1536 ep->me_key = NULL;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001537 ep->me_value = NULL;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001538 Py_DECREF(old_key);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001539 Py_DECREF(old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001540
1541 assert(_PyDict_CheckConsistency(mp));
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001542 return 0;
1543}
1544
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001545int
Tim Peters1f5871e2000-07-04 17:44:48 +00001546PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001547{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001548 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 assert(key);
1550 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001551 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 hash = PyObject_Hash(key);
1553 if (hash == -1)
1554 return -1;
1555 }
Victor Stinner742da042016-09-07 17:40:12 -07001556
1557 return _PyDict_DelItem_KnownHash(op, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001558}
1559
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001560int
1561_PyDict_DelItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1562{
INADA Naoki778928b2017-08-03 23:45:15 +09001563 Py_ssize_t ix;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001564 PyDictObject *mp;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001565 PyObject *old_value;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001566
1567 if (!PyDict_Check(op)) {
1568 PyErr_BadInternalCall();
1569 return -1;
1570 }
1571 assert(key);
1572 assert(hash != -1);
1573 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001574 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001575 if (ix == DKIX_ERROR)
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001576 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09001577 if (ix == DKIX_EMPTY || old_value == NULL) {
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001578 _PyErr_SetKeyError(key);
1579 return -1;
1580 }
Victor Stinner78601a32016-09-09 19:28:36 -07001581
1582 // Split table doesn't allow deletion. Combine it.
1583 if (_PyDict_HasSplitTable(mp)) {
1584 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1585 return -1;
1586 }
INADA Naoki778928b2017-08-03 23:45:15 +09001587 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001588 assert(ix >= 0);
1589 }
1590
INADA Naoki778928b2017-08-03 23:45:15 +09001591 return delitem_common(mp, hash, ix, old_value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001592}
1593
Antoine Pitroud741ed42016-12-27 14:23:43 +01001594/* This function promises that the predicate -> deletion sequence is atomic
1595 * (i.e. protected by the GIL), assuming the predicate itself doesn't
1596 * release the GIL.
1597 */
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001598int
1599_PyDict_DelItemIf(PyObject *op, PyObject *key,
1600 int (*predicate)(PyObject *value))
1601{
Antoine Pitroud741ed42016-12-27 14:23:43 +01001602 Py_ssize_t hashpos, ix;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001603 PyDictObject *mp;
1604 Py_hash_t hash;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001605 PyObject *old_value;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001606 int res;
1607
1608 if (!PyDict_Check(op)) {
1609 PyErr_BadInternalCall();
1610 return -1;
1611 }
1612 assert(key);
1613 hash = PyObject_Hash(key);
1614 if (hash == -1)
1615 return -1;
1616 mp = (PyDictObject *)op;
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 if (ix == DKIX_ERROR)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001619 return -1;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001620 if (ix == DKIX_EMPTY || old_value == NULL) {
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001621 _PyErr_SetKeyError(key);
1622 return -1;
1623 }
Antoine Pitroud741ed42016-12-27 14:23:43 +01001624
1625 // Split table doesn't allow deletion. Combine it.
1626 if (_PyDict_HasSplitTable(mp)) {
1627 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1628 return -1;
1629 }
INADA Naoki778928b2017-08-03 23:45:15 +09001630 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001631 assert(ix >= 0);
1632 }
1633
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001634 res = predicate(old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001635 if (res == -1)
1636 return -1;
INADA Naoki778928b2017-08-03 23:45:15 +09001637
1638 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1639 assert(hashpos >= 0);
1640
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001641 if (res > 0)
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001642 return delitem_common(mp, hashpos, ix, old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001643 else
1644 return 0;
1645}
1646
1647
Guido van Rossum25831651993-05-19 14:50:45 +00001648void
Tim Peters1f5871e2000-07-04 17:44:48 +00001649PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001650{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001651 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001652 PyDictKeysObject *oldkeys;
1653 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 if (!PyDict_Check(op))
1657 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001658 mp = ((PyDictObject *)op);
1659 oldkeys = mp->ma_keys;
1660 oldvalues = mp->ma_values;
1661 if (oldvalues == empty_values)
1662 return;
1663 /* Empty the dict... */
INADA Naokia7576492018-11-14 18:39:27 +09001664 dictkeys_incref(Py_EMPTY_KEYS);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001665 mp->ma_keys = Py_EMPTY_KEYS;
1666 mp->ma_values = empty_values;
1667 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001668 mp->ma_version_tag = DICT_NEXT_VERSION();
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001669 /* ...then clear the keys and values */
1670 if (oldvalues != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001671 n = oldkeys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001672 for (i = 0; i < n; i++)
1673 Py_CLEAR(oldvalues[i]);
1674 free_values(oldvalues);
INADA Naokia7576492018-11-14 18:39:27 +09001675 dictkeys_decref(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001676 }
1677 else {
1678 assert(oldkeys->dk_refcnt == 1);
INADA Naokia7576492018-11-14 18:39:27 +09001679 dictkeys_decref(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001680 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02001681 assert(_PyDict_CheckConsistency(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001682}
1683
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001684/* Internal version of PyDict_Next that returns a hash value in addition
1685 * to the key and value.
1686 * Return 1 on success, return 0 when the reached the end of the dictionary
1687 * (or if op is not a dictionary)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001688 */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001689int
1690_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1691 PyObject **pvalue, Py_hash_t *phash)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001692{
INADA Naokica2d8be2016-11-04 16:59:10 +09001693 Py_ssize_t i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001694 PyDictObject *mp;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001695 PyDictKeyEntry *entry_ptr;
1696 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001697
1698 if (!PyDict_Check(op))
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001699 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 mp = (PyDictObject *)op;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001701 i = *ppos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001702 if (mp->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09001703 if (i < 0 || i >= mp->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001704 return 0;
INADA Naokica2d8be2016-11-04 16:59:10 +09001705 /* values of split table is always dense */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001706 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
INADA Naokica2d8be2016-11-04 16:59:10 +09001707 value = mp->ma_values[i];
1708 assert(value != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001710 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09001711 Py_ssize_t n = mp->ma_keys->dk_nentries;
1712 if (i < 0 || i >= n)
1713 return 0;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001714 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
1715 while (i < n && entry_ptr->me_value == NULL) {
1716 entry_ptr++;
1717 i++;
Victor Stinner742da042016-09-07 17:40:12 -07001718 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001719 if (i >= n)
1720 return 0;
1721 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001723 *ppos = i+1;
1724 if (pkey)
1725 *pkey = entry_ptr->me_key;
1726 if (phash)
1727 *phash = entry_ptr->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001728 if (pvalue)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001729 *pvalue = value;
1730 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001731}
1732
Tim Peters080c88b2003-02-15 03:01:11 +00001733/*
1734 * Iterate over a dict. Use like so:
1735 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001736 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001737 * PyObject *key, *value;
1738 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001739 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001740 * Refer to borrowed references in key and value.
Tim Peters080c88b2003-02-15 03:01:11 +00001741 * }
1742 *
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001743 * Return 1 on success, return 0 when the reached the end of the dictionary
1744 * (or if op is not a dictionary)
1745 *
Tim Peters080c88b2003-02-15 03:01:11 +00001746 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001747 * mutates the dict. One exception: it is safe if the loop merely changes
1748 * the values associated with the keys (but doesn't insert new keys or
1749 * delete keys), via PyDict_SetItem().
1750 */
Guido van Rossum25831651993-05-19 14:50:45 +00001751int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001752PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001753{
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001754 return _PyDict_Next(op, ppos, pkey, pvalue, NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001755}
1756
Eric Snow96c6af92015-05-29 22:21:39 -06001757/* Internal version of dict.pop(). */
1758PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001759_PyDict_Pop_KnownHash(PyObject *dict, PyObject *key, Py_hash_t hash, PyObject *deflt)
Eric Snow96c6af92015-05-29 22:21:39 -06001760{
Victor Stinner742da042016-09-07 17:40:12 -07001761 Py_ssize_t ix, hashpos;
Eric Snow96c6af92015-05-29 22:21:39 -06001762 PyObject *old_value, *old_key;
1763 PyDictKeyEntry *ep;
Yury Selivanov684ef2c2016-10-28 19:01:21 -04001764 PyDictObject *mp;
1765
1766 assert(PyDict_Check(dict));
1767 mp = (PyDictObject *)dict;
Eric Snow96c6af92015-05-29 22:21:39 -06001768
1769 if (mp->ma_used == 0) {
1770 if (deflt) {
1771 Py_INCREF(deflt);
1772 return deflt;
1773 }
1774 _PyErr_SetKeyError(key);
1775 return NULL;
1776 }
INADA Naoki778928b2017-08-03 23:45:15 +09001777 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001778 if (ix == DKIX_ERROR)
Eric Snow96c6af92015-05-29 22:21:39 -06001779 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001780 if (ix == DKIX_EMPTY || old_value == NULL) {
Eric Snow96c6af92015-05-29 22:21:39 -06001781 if (deflt) {
1782 Py_INCREF(deflt);
1783 return deflt;
1784 }
1785 _PyErr_SetKeyError(key);
1786 return NULL;
1787 }
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001788
Victor Stinner78601a32016-09-09 19:28:36 -07001789 // Split table doesn't allow deletion. Combine it.
1790 if (_PyDict_HasSplitTable(mp)) {
1791 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1792 return NULL;
1793 }
INADA Naoki778928b2017-08-03 23:45:15 +09001794 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001795 assert(ix >= 0);
1796 }
1797
INADA Naoki778928b2017-08-03 23:45:15 +09001798 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1799 assert(hashpos >= 0);
Victor Stinner78601a32016-09-09 19:28:36 -07001800 assert(old_value != NULL);
Eric Snow96c6af92015-05-29 22:21:39 -06001801 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001802 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokia7576492018-11-14 18:39:27 +09001803 dictkeys_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
Victor Stinner78601a32016-09-09 19:28:36 -07001804 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1805 ENSURE_ALLOWS_DELETIONS(mp);
1806 old_key = ep->me_key;
1807 ep->me_key = NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001808 ep->me_value = NULL;
Victor Stinner78601a32016-09-09 19:28:36 -07001809 Py_DECREF(old_key);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001810
1811 assert(_PyDict_CheckConsistency(mp));
Eric Snow96c6af92015-05-29 22:21:39 -06001812 return old_value;
1813}
1814
Serhiy Storchaka67796522017-01-12 18:34:33 +02001815PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001816_PyDict_Pop(PyObject *dict, PyObject *key, PyObject *deflt)
Serhiy Storchaka67796522017-01-12 18:34:33 +02001817{
1818 Py_hash_t hash;
1819
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001820 if (((PyDictObject *)dict)->ma_used == 0) {
Serhiy Storchaka67796522017-01-12 18:34:33 +02001821 if (deflt) {
1822 Py_INCREF(deflt);
1823 return deflt;
1824 }
1825 _PyErr_SetKeyError(key);
1826 return NULL;
1827 }
1828 if (!PyUnicode_CheckExact(key) ||
1829 (hash = ((PyASCIIObject *) key)->hash) == -1) {
1830 hash = PyObject_Hash(key);
1831 if (hash == -1)
1832 return NULL;
1833 }
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001834 return _PyDict_Pop_KnownHash(dict, key, hash, deflt);
Serhiy Storchaka67796522017-01-12 18:34:33 +02001835}
1836
Eric Snow96c6af92015-05-29 22:21:39 -06001837/* Internal version of dict.from_keys(). It is subclass-friendly. */
1838PyObject *
1839_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
1840{
1841 PyObject *it; /* iter(iterable) */
1842 PyObject *key;
1843 PyObject *d;
1844 int status;
1845
Victor Stinnera5ed5f02016-12-06 18:45:50 +01001846 d = _PyObject_CallNoArg(cls);
Eric Snow96c6af92015-05-29 22:21:39 -06001847 if (d == NULL)
1848 return NULL;
1849
1850 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
1851 if (PyDict_CheckExact(iterable)) {
1852 PyDictObject *mp = (PyDictObject *)d;
1853 PyObject *oldvalue;
1854 Py_ssize_t pos = 0;
1855 PyObject *key;
1856 Py_hash_t hash;
1857
Serhiy Storchakac61ac162017-03-21 08:52:38 +02001858 if (dictresize(mp, ESTIMATE_SIZE(PyDict_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001859 Py_DECREF(d);
1860 return NULL;
1861 }
1862
1863 while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
1864 if (insertdict(mp, key, hash, value)) {
1865 Py_DECREF(d);
1866 return NULL;
1867 }
1868 }
1869 return d;
1870 }
1871 if (PyAnySet_CheckExact(iterable)) {
1872 PyDictObject *mp = (PyDictObject *)d;
1873 Py_ssize_t pos = 0;
1874 PyObject *key;
1875 Py_hash_t hash;
1876
Victor Stinner742da042016-09-07 17:40:12 -07001877 if (dictresize(mp, ESTIMATE_SIZE(PySet_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001878 Py_DECREF(d);
1879 return NULL;
1880 }
1881
1882 while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
1883 if (insertdict(mp, key, hash, value)) {
1884 Py_DECREF(d);
1885 return NULL;
1886 }
1887 }
1888 return d;
1889 }
1890 }
1891
1892 it = PyObject_GetIter(iterable);
1893 if (it == NULL){
1894 Py_DECREF(d);
1895 return NULL;
1896 }
1897
1898 if (PyDict_CheckExact(d)) {
1899 while ((key = PyIter_Next(it)) != NULL) {
1900 status = PyDict_SetItem(d, key, value);
1901 Py_DECREF(key);
1902 if (status < 0)
1903 goto Fail;
1904 }
1905 } else {
1906 while ((key = PyIter_Next(it)) != NULL) {
1907 status = PyObject_SetItem(d, key, value);
1908 Py_DECREF(key);
1909 if (status < 0)
1910 goto Fail;
1911 }
1912 }
1913
1914 if (PyErr_Occurred())
1915 goto Fail;
1916 Py_DECREF(it);
1917 return d;
1918
1919Fail:
1920 Py_DECREF(it);
1921 Py_DECREF(d);
1922 return NULL;
1923}
1924
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001925/* Methods */
1926
1927static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001928dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001929{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001930 PyObject **values = mp->ma_values;
1931 PyDictKeysObject *keys = mp->ma_keys;
1932 Py_ssize_t i, n;
INADA Naokia6296d32017-08-24 14:55:17 +09001933
1934 /* bpo-31095: UnTrack is needed before calling any callbacks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001935 PyObject_GC_UnTrack(mp);
1936 Py_TRASHCAN_SAFE_BEGIN(mp)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001937 if (values != NULL) {
1938 if (values != empty_values) {
Victor Stinner742da042016-09-07 17:40:12 -07001939 for (i = 0, n = mp->ma_keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001940 Py_XDECREF(values[i]);
1941 }
1942 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001943 }
INADA Naokia7576492018-11-14 18:39:27 +09001944 dictkeys_decref(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001945 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02001946 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001947 assert(keys->dk_refcnt == 1);
INADA Naokia7576492018-11-14 18:39:27 +09001948 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001949 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001950 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1951 free_list[numfree++] = mp;
1952 else
1953 Py_TYPE(mp)->tp_free((PyObject *)mp);
1954 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001955}
1956
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001957
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001958static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001959dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001960{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001962 PyObject *key = NULL, *value = NULL;
1963 _PyUnicodeWriter writer;
1964 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00001965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001966 i = Py_ReprEnter((PyObject *)mp);
1967 if (i != 0) {
1968 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
1969 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001970
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001971 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001972 Py_ReprLeave((PyObject *)mp);
1973 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974 }
Tim Petersa7259592001-06-16 05:11:17 +00001975
Victor Stinnerf91929b2013-11-19 13:07:38 +01001976 _PyUnicodeWriter_Init(&writer);
1977 writer.overallocate = 1;
1978 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
1979 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00001980
Victor Stinnerf91929b2013-11-19 13:07:38 +01001981 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
1982 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984 /* Do repr() on each key+value pair, and insert ": " between them.
1985 Note that repr may mutate the dict. */
1986 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001987 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001988 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001989 PyObject *s;
1990 int res;
1991
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001992 /* Prevent repr from deleting key or value during key format. */
1993 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001994 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02001995
Victor Stinnerf91929b2013-11-19 13:07:38 +01001996 if (!first) {
1997 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
1998 goto error;
1999 }
2000 first = 0;
2001
2002 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01002004 goto error;
2005 res = _PyUnicodeWriter_WriteStr(&writer, s);
2006 Py_DECREF(s);
2007 if (res < 0)
2008 goto error;
2009
2010 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
2011 goto error;
2012
2013 s = PyObject_Repr(value);
2014 if (s == NULL)
2015 goto error;
2016 res = _PyUnicodeWriter_WriteStr(&writer, s);
2017 Py_DECREF(s);
2018 if (res < 0)
2019 goto error;
2020
2021 Py_CLEAR(key);
2022 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002023 }
Tim Petersa7259592001-06-16 05:11:17 +00002024
Victor Stinnerf91929b2013-11-19 13:07:38 +01002025 writer.overallocate = 0;
2026 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
2027 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00002028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002029 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01002030
2031 return _PyUnicodeWriter_Finish(&writer);
2032
2033error:
2034 Py_ReprLeave((PyObject *)mp);
2035 _PyUnicodeWriter_Dealloc(&writer);
2036 Py_XDECREF(key);
2037 Py_XDECREF(value);
2038 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002039}
2040
Martin v. Löwis18e16552006-02-15 17:27:45 +00002041static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002042dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002043{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002044 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002045}
2046
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002047static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002048dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002049{
Victor Stinner742da042016-09-07 17:40:12 -07002050 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002051 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09002052 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002054 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002055 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002056 hash = PyObject_Hash(key);
2057 if (hash == -1)
2058 return NULL;
2059 }
INADA Naoki778928b2017-08-03 23:45:15 +09002060 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002061 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002062 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002063 if (ix == DKIX_EMPTY || value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002064 if (!PyDict_CheckExact(mp)) {
2065 /* Look up __missing__ method if we're a subclass. */
2066 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05002067 _Py_IDENTIFIER(__missing__);
2068 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 if (missing != NULL) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01002070 res = PyObject_CallFunctionObjArgs(missing,
2071 key, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002072 Py_DECREF(missing);
2073 return res;
2074 }
2075 else if (PyErr_Occurred())
2076 return NULL;
2077 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07002078 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002079 return NULL;
2080 }
INADA Naokiba609772016-12-07 20:41:42 +09002081 Py_INCREF(value);
2082 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002083}
2084
2085static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002086dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002087{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002088 if (w == NULL)
2089 return PyDict_DelItem((PyObject *)mp, v);
2090 else
2091 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002092}
2093
Guido van Rossuma9e7a811997-05-13 21:02:11 +00002094static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002095 (lenfunc)dict_length, /*mp_length*/
2096 (binaryfunc)dict_subscript, /*mp_subscript*/
2097 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002098};
2099
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002100static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002101dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002102{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002103 PyObject *v;
2104 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002105 PyDictKeyEntry *ep;
2106 Py_ssize_t size, n, offset;
2107 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002108
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002109 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002110 n = mp->ma_used;
2111 v = PyList_New(n);
2112 if (v == NULL)
2113 return NULL;
2114 if (n != mp->ma_used) {
2115 /* Durnit. The allocations caused the dict to resize.
2116 * Just start over, this shouldn't normally happen.
2117 */
2118 Py_DECREF(v);
2119 goto again;
2120 }
Victor Stinner742da042016-09-07 17:40:12 -07002121 ep = DK_ENTRIES(mp->ma_keys);
2122 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002123 if (mp->ma_values) {
2124 value_ptr = mp->ma_values;
2125 offset = sizeof(PyObject *);
2126 }
2127 else {
2128 value_ptr = &ep[0].me_value;
2129 offset = sizeof(PyDictKeyEntry);
2130 }
2131 for (i = 0, j = 0; i < size; i++) {
2132 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002133 PyObject *key = ep[i].me_key;
2134 Py_INCREF(key);
2135 PyList_SET_ITEM(v, j, key);
2136 j++;
2137 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002138 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002139 }
2140 assert(j == n);
2141 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002142}
2143
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002144static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002145dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002146{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002147 PyObject *v;
2148 Py_ssize_t i, j;
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002149 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002150 Py_ssize_t size, n, offset;
2151 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002152
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002153 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002154 n = mp->ma_used;
2155 v = PyList_New(n);
2156 if (v == NULL)
2157 return NULL;
2158 if (n != mp->ma_used) {
2159 /* Durnit. The allocations caused the dict to resize.
2160 * Just start over, this shouldn't normally happen.
2161 */
2162 Py_DECREF(v);
2163 goto again;
2164 }
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002165 ep = DK_ENTRIES(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002166 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002167 if (mp->ma_values) {
2168 value_ptr = mp->ma_values;
2169 offset = sizeof(PyObject *);
2170 }
2171 else {
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002172 value_ptr = &ep[0].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002173 offset = sizeof(PyDictKeyEntry);
2174 }
2175 for (i = 0, j = 0; i < size; i++) {
2176 PyObject *value = *value_ptr;
2177 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2178 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002179 Py_INCREF(value);
2180 PyList_SET_ITEM(v, j, value);
2181 j++;
2182 }
2183 }
2184 assert(j == n);
2185 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002186}
2187
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002188static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002189dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002190{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002191 PyObject *v;
2192 Py_ssize_t i, j, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002193 Py_ssize_t size, offset;
2194 PyObject *item, *key;
2195 PyDictKeyEntry *ep;
2196 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002197
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002198 /* Preallocate the list of tuples, to avoid allocations during
2199 * the loop over the items, which could trigger GC, which
2200 * could resize the dict. :-(
2201 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002202 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002203 n = mp->ma_used;
2204 v = PyList_New(n);
2205 if (v == NULL)
2206 return NULL;
2207 for (i = 0; i < n; i++) {
2208 item = PyTuple_New(2);
2209 if (item == NULL) {
2210 Py_DECREF(v);
2211 return NULL;
2212 }
2213 PyList_SET_ITEM(v, i, item);
2214 }
2215 if (n != mp->ma_used) {
2216 /* Durnit. The allocations caused the dict to resize.
2217 * Just start over, this shouldn't normally happen.
2218 */
2219 Py_DECREF(v);
2220 goto again;
2221 }
2222 /* Nothing we do below makes any function calls. */
Victor Stinner742da042016-09-07 17:40:12 -07002223 ep = DK_ENTRIES(mp->ma_keys);
2224 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002225 if (mp->ma_values) {
2226 value_ptr = mp->ma_values;
2227 offset = sizeof(PyObject *);
2228 }
2229 else {
2230 value_ptr = &ep[0].me_value;
2231 offset = sizeof(PyDictKeyEntry);
2232 }
2233 for (i = 0, j = 0; i < size; i++) {
2234 PyObject *value = *value_ptr;
2235 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2236 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002237 key = ep[i].me_key;
2238 item = PyList_GET_ITEM(v, j);
2239 Py_INCREF(key);
2240 PyTuple_SET_ITEM(item, 0, key);
2241 Py_INCREF(value);
2242 PyTuple_SET_ITEM(item, 1, value);
2243 j++;
2244 }
2245 }
2246 assert(j == n);
2247 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002248}
2249
Larry Hastings5c661892014-01-24 06:17:25 -08002250/*[clinic input]
2251@classmethod
2252dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08002253 iterable: object
2254 value: object=None
2255 /
2256
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002257Create a new dictionary with keys from iterable and values set to value.
Larry Hastings5c661892014-01-24 06:17:25 -08002258[clinic start generated code]*/
2259
Larry Hastings5c661892014-01-24 06:17:25 -08002260static PyObject *
2261dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002262/*[clinic end generated code: output=8fb98e4b10384999 input=382ba4855d0f74c3]*/
Larry Hastings5c661892014-01-24 06:17:25 -08002263{
Eric Snow96c6af92015-05-29 22:21:39 -06002264 return _PyDict_FromKeys((PyObject *)type, iterable, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002265}
2266
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002267static int
Victor Stinner742da042016-09-07 17:40:12 -07002268dict_update_common(PyObject *self, PyObject *args, PyObject *kwds,
2269 const char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002270{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002271 PyObject *arg = NULL;
2272 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002273
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002274 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002275 result = -1;
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002276 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002277 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002278 _Py_IDENTIFIER(keys);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002279 PyObject *func;
2280 if (_PyObject_LookupAttrId(arg, &PyId_keys, &func) < 0) {
2281 result = -1;
2282 }
2283 else if (func != NULL) {
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002284 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002285 result = PyDict_Merge(self, arg, 1);
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002286 }
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002287 else {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002288 result = PyDict_MergeFromSeq2(self, arg, 1);
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002289 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002290 }
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002292 if (result == 0 && kwds != NULL) {
2293 if (PyArg_ValidateKeywordArguments(kwds))
2294 result = PyDict_Merge(self, kwds, 1);
2295 else
2296 result = -1;
2297 }
2298 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002299}
2300
Victor Stinner91f0d4a2017-01-19 12:45:06 +01002301/* Note: dict.update() uses the METH_VARARGS|METH_KEYWORDS calling convention.
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +03002302 Using METH_FASTCALL|METH_KEYWORDS would make dict.update(**dict2) calls
2303 slower, see the issue #29312. */
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002304static PyObject *
2305dict_update(PyObject *self, PyObject *args, PyObject *kwds)
2306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 if (dict_update_common(self, args, kwds, "update") != -1)
2308 Py_RETURN_NONE;
2309 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002310}
2311
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002312/* Update unconditionally replaces existing items.
2313 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00002314 otherwise it leaves existing items unchanged.
2315
2316 PyDict_{Update,Merge} update/merge from a mapping object.
2317
Tim Petersf582b822001-12-11 18:51:08 +00002318 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00002319 producing iterable objects of length 2.
2320*/
2321
Tim Petersf582b822001-12-11 18:51:08 +00002322int
Tim Peters1fc240e2001-10-26 05:06:50 +00002323PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
2324{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002325 PyObject *it; /* iter(seq2) */
2326 Py_ssize_t i; /* index into seq2 of current element */
2327 PyObject *item; /* seq2[i] */
2328 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00002329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002330 assert(d != NULL);
2331 assert(PyDict_Check(d));
2332 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00002333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002334 it = PyObject_GetIter(seq2);
2335 if (it == NULL)
2336 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002338 for (i = 0; ; ++i) {
2339 PyObject *key, *value;
2340 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00002341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002342 fast = NULL;
2343 item = PyIter_Next(it);
2344 if (item == NULL) {
2345 if (PyErr_Occurred())
2346 goto Fail;
2347 break;
2348 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002350 /* Convert item to sequence, and verify length 2. */
2351 fast = PySequence_Fast(item, "");
2352 if (fast == NULL) {
2353 if (PyErr_ExceptionMatches(PyExc_TypeError))
2354 PyErr_Format(PyExc_TypeError,
2355 "cannot convert dictionary update "
2356 "sequence element #%zd to a sequence",
2357 i);
2358 goto Fail;
2359 }
2360 n = PySequence_Fast_GET_SIZE(fast);
2361 if (n != 2) {
2362 PyErr_Format(PyExc_ValueError,
2363 "dictionary update sequence element #%zd "
2364 "has length %zd; 2 is required",
2365 i, n);
2366 goto Fail;
2367 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 /* Update/merge with this (key, value) pair. */
2370 key = PySequence_Fast_GET_ITEM(fast, 0);
2371 value = PySequence_Fast_GET_ITEM(fast, 1);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002372 Py_INCREF(key);
2373 Py_INCREF(value);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002374 if (override) {
2375 if (PyDict_SetItem(d, key, value) < 0) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002376 Py_DECREF(key);
2377 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002378 goto Fail;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002379 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002380 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002381 else if (PyDict_GetItemWithError(d, key) == NULL) {
2382 if (PyErr_Occurred() || PyDict_SetItem(d, key, value) < 0) {
2383 Py_DECREF(key);
2384 Py_DECREF(value);
2385 goto Fail;
2386 }
2387 }
2388
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002389 Py_DECREF(key);
2390 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 Py_DECREF(fast);
2392 Py_DECREF(item);
2393 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 i = 0;
Victor Stinner611b0fa2016-09-14 15:02:01 +02002396 assert(_PyDict_CheckConsistency((PyDictObject *)d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002397 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00002398Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002399 Py_XDECREF(item);
2400 Py_XDECREF(fast);
2401 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002402Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002403 Py_DECREF(it);
2404 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00002405}
2406
doko@ubuntu.comc96df682016-10-11 08:04:02 +02002407static int
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002408dict_merge(PyObject *a, PyObject *b, int override)
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002409{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002410 PyDictObject *mp, *other;
2411 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -07002412 PyDictKeyEntry *entry, *ep0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002413
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002414 assert(0 <= override && override <= 2);
2415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 /* We accept for the argument either a concrete dictionary object,
2417 * or an abstract "mapping" object. For the former, we can do
2418 * things quite efficiently. For the latter, we only require that
2419 * PyMapping_Keys() and PyObject_GetItem() be supported.
2420 */
2421 if (a == NULL || !PyDict_Check(a) || b == NULL) {
2422 PyErr_BadInternalCall();
2423 return -1;
2424 }
2425 mp = (PyDictObject*)a;
INADA Naoki2aaf98c2018-09-26 12:59:00 +09002426 if (PyDict_Check(b) && (Py_TYPE(b)->tp_iter == (getiterfunc)dict_iter)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002427 other = (PyDictObject*)b;
2428 if (other == mp || other->ma_used == 0)
2429 /* a.update(a) or a.update({}); nothing to do */
2430 return 0;
2431 if (mp->ma_used == 0)
2432 /* Since the target dict is empty, PyDict_GetItem()
2433 * always returns NULL. Setting override to 1
2434 * skips the unnecessary test.
2435 */
2436 override = 1;
2437 /* Do one big resize at the start, rather than
2438 * incrementally resizing as we insert new items. Expect
2439 * that there will be no (or few) overlapping keys.
2440 */
INADA Naokib1152be2016-10-27 19:26:50 +09002441 if (USABLE_FRACTION(mp->ma_keys->dk_size) < other->ma_used) {
2442 if (dictresize(mp, ESTIMATE_SIZE(mp->ma_used + other->ma_used))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002443 return -1;
INADA Naokib1152be2016-10-27 19:26:50 +09002444 }
2445 }
Victor Stinner742da042016-09-07 17:40:12 -07002446 ep0 = DK_ENTRIES(other->ma_keys);
2447 for (i = 0, n = other->ma_keys->dk_nentries; i < n; i++) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002448 PyObject *key, *value;
2449 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002450 entry = &ep0[i];
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002451 key = entry->me_key;
2452 hash = entry->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002453 if (other->ma_values)
2454 value = other->ma_values[i];
2455 else
2456 value = entry->me_value;
2457
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002458 if (value != NULL) {
2459 int err = 0;
2460 Py_INCREF(key);
2461 Py_INCREF(value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002462 if (override == 1)
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002463 err = insertdict(mp, key, hash, value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002464 else if (_PyDict_GetItem_KnownHash(a, key, hash) == NULL) {
2465 if (PyErr_Occurred()) {
2466 Py_DECREF(value);
2467 Py_DECREF(key);
2468 return -1;
2469 }
2470 err = insertdict(mp, key, hash, value);
2471 }
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002472 else if (override != 0) {
2473 _PyErr_SetKeyError(key);
2474 Py_DECREF(value);
2475 Py_DECREF(key);
2476 return -1;
2477 }
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002478 Py_DECREF(value);
2479 Py_DECREF(key);
2480 if (err != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002481 return -1;
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002482
Victor Stinner742da042016-09-07 17:40:12 -07002483 if (n != other->ma_keys->dk_nentries) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002484 PyErr_SetString(PyExc_RuntimeError,
2485 "dict mutated during update");
2486 return -1;
2487 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002488 }
2489 }
2490 }
2491 else {
2492 /* Do it the generic, slower way */
2493 PyObject *keys = PyMapping_Keys(b);
2494 PyObject *iter;
2495 PyObject *key, *value;
2496 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002498 if (keys == NULL)
2499 /* Docstring says this is equivalent to E.keys() so
2500 * if E doesn't have a .keys() method we want
2501 * AttributeError to percolate up. Might as well
2502 * do the same for any other error.
2503 */
2504 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002506 iter = PyObject_GetIter(keys);
2507 Py_DECREF(keys);
2508 if (iter == NULL)
2509 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002511 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002512 if (override != 1) {
2513 if (PyDict_GetItemWithError(a, key) != NULL) {
2514 if (override != 0) {
2515 _PyErr_SetKeyError(key);
2516 Py_DECREF(key);
2517 Py_DECREF(iter);
2518 return -1;
2519 }
2520 Py_DECREF(key);
2521 continue;
2522 }
2523 else if (PyErr_Occurred()) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002524 Py_DECREF(key);
2525 Py_DECREF(iter);
2526 return -1;
2527 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002528 }
2529 value = PyObject_GetItem(b, key);
2530 if (value == NULL) {
2531 Py_DECREF(iter);
2532 Py_DECREF(key);
2533 return -1;
2534 }
2535 status = PyDict_SetItem(a, key, value);
2536 Py_DECREF(key);
2537 Py_DECREF(value);
2538 if (status < 0) {
2539 Py_DECREF(iter);
2540 return -1;
2541 }
2542 }
2543 Py_DECREF(iter);
2544 if (PyErr_Occurred())
2545 /* Iterator completed, via error */
2546 return -1;
2547 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02002548 assert(_PyDict_CheckConsistency((PyDictObject *)a));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002549 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002550}
2551
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002552int
2553PyDict_Update(PyObject *a, PyObject *b)
2554{
2555 return dict_merge(a, b, 1);
2556}
2557
2558int
2559PyDict_Merge(PyObject *a, PyObject *b, int override)
2560{
2561 /* XXX Deprecate override not in (0, 1). */
2562 return dict_merge(a, b, override != 0);
2563}
2564
2565int
2566_PyDict_MergeEx(PyObject *a, PyObject *b, int override)
2567{
2568 return dict_merge(a, b, override);
2569}
2570
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002571static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302572dict_copy(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002573{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002574 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002575}
2576
2577PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002578PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002579{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002580 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002581 PyDictObject *mp;
2582 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002584 if (o == NULL || !PyDict_Check(o)) {
2585 PyErr_BadInternalCall();
2586 return NULL;
2587 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002588
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002589 mp = (PyDictObject *)o;
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002590 if (mp->ma_used == 0) {
2591 /* The dict is empty; just return a new dict. */
2592 return PyDict_New();
2593 }
2594
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002595 if (_PyDict_HasSplitTable(mp)) {
2596 PyDictObject *split_copy;
Victor Stinner742da042016-09-07 17:40:12 -07002597 Py_ssize_t size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
2598 PyObject **newvalues;
2599 newvalues = new_values(size);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002600 if (newvalues == NULL)
2601 return PyErr_NoMemory();
2602 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2603 if (split_copy == NULL) {
2604 free_values(newvalues);
2605 return NULL;
2606 }
2607 split_copy->ma_values = newvalues;
2608 split_copy->ma_keys = mp->ma_keys;
2609 split_copy->ma_used = mp->ma_used;
INADA Naokid1c82c52018-04-03 11:43:53 +09002610 split_copy->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokia7576492018-11-14 18:39:27 +09002611 dictkeys_incref(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002612 for (i = 0, n = size; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002613 PyObject *value = mp->ma_values[i];
2614 Py_XINCREF(value);
2615 split_copy->ma_values[i] = value;
2616 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002617 if (_PyObject_GC_IS_TRACKED(mp))
2618 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002619 return (PyObject *)split_copy;
2620 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002621
2622 if (PyDict_CheckExact(mp) && mp->ma_values == NULL &&
2623 (mp->ma_used >= (mp->ma_keys->dk_nentries * 2) / 3))
2624 {
2625 /* Use fast-copy if:
2626
2627 (1) 'mp' is an instance of a subclassed dict; and
2628
2629 (2) 'mp' is not a split-dict; and
2630
2631 (3) if 'mp' is non-compact ('del' operation does not resize dicts),
2632 do fast-copy only if it has at most 1/3 non-used keys.
2633
Ville Skyttä61f82e02018-04-20 23:08:45 +03002634 The last condition (3) is important to guard against a pathological
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002635 case when a large dict is almost emptied with multiple del/pop
2636 operations and copied after that. In cases like this, we defer to
2637 PyDict_Merge, which produces a compacted copy.
2638 */
2639 return clone_combined_dict(mp);
2640 }
2641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002642 copy = PyDict_New();
2643 if (copy == NULL)
2644 return NULL;
2645 if (PyDict_Merge(copy, o, 1) == 0)
2646 return copy;
2647 Py_DECREF(copy);
2648 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002649}
2650
Martin v. Löwis18e16552006-02-15 17:27:45 +00002651Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002652PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002653{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002654 if (mp == NULL || !PyDict_Check(mp)) {
2655 PyErr_BadInternalCall();
2656 return -1;
2657 }
2658 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002659}
2660
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002661PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002662PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002663{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002664 if (mp == NULL || !PyDict_Check(mp)) {
2665 PyErr_BadInternalCall();
2666 return NULL;
2667 }
2668 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002669}
2670
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002671PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002672PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002673{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002674 if (mp == NULL || !PyDict_Check(mp)) {
2675 PyErr_BadInternalCall();
2676 return NULL;
2677 }
2678 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002679}
2680
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002681PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002682PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002683{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002684 if (mp == NULL || !PyDict_Check(mp)) {
2685 PyErr_BadInternalCall();
2686 return NULL;
2687 }
2688 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002689}
2690
Tim Peterse63415e2001-05-08 04:38:29 +00002691/* Return 1 if dicts equal, 0 if not, -1 if error.
2692 * Gets out as soon as any difference is detected.
2693 * Uses only Py_EQ comparison.
2694 */
2695static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002696dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002697{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002698 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002700 if (a->ma_used != b->ma_used)
2701 /* can't be equal if # of entries differ */
2702 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002703 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Victor Stinner742da042016-09-07 17:40:12 -07002704 for (i = 0; i < a->ma_keys->dk_nentries; i++) {
2705 PyDictKeyEntry *ep = &DK_ENTRIES(a->ma_keys)[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002706 PyObject *aval;
2707 if (a->ma_values)
2708 aval = a->ma_values[i];
2709 else
2710 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002711 if (aval != NULL) {
2712 int cmp;
2713 PyObject *bval;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002714 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002715 /* temporarily bump aval's refcount to ensure it stays
2716 alive until we're done with it */
2717 Py_INCREF(aval);
2718 /* ditto for key */
2719 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002720 /* reuse the known hash value */
INADA Naoki778928b2017-08-03 23:45:15 +09002721 b->ma_keys->dk_lookup(b, key, ep->me_hash, &bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002722 if (bval == NULL) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002723 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002724 Py_DECREF(aval);
2725 if (PyErr_Occurred())
2726 return -1;
2727 return 0;
2728 }
2729 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002730 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002731 Py_DECREF(aval);
2732 if (cmp <= 0) /* error or not equal */
2733 return cmp;
2734 }
2735 }
2736 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002737}
Tim Peterse63415e2001-05-08 04:38:29 +00002738
2739static PyObject *
2740dict_richcompare(PyObject *v, PyObject *w, int op)
2741{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002742 int cmp;
2743 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002744
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002745 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2746 res = Py_NotImplemented;
2747 }
2748 else if (op == Py_EQ || op == Py_NE) {
2749 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2750 if (cmp < 0)
2751 return NULL;
2752 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2753 }
2754 else
2755 res = Py_NotImplemented;
2756 Py_INCREF(res);
2757 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002758}
Tim Peterse63415e2001-05-08 04:38:29 +00002759
Larry Hastings61272b72014-01-07 12:41:53 -08002760/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002761
2762@coexist
2763dict.__contains__
2764
2765 key: object
2766 /
2767
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002768True if the dictionary has the specified key, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002769[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002770
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002771static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002772dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka19d25972017-02-04 08:05:07 +02002773/*[clinic end generated code: output=a3d03db709ed6e6b input=fe1cb42ad831e820]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002774{
Larry Hastingsc2047262014-01-25 20:43:29 -08002775 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002776 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002777 Py_ssize_t ix;
INADA Naokiba609772016-12-07 20:41:42 +09002778 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002779
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002780 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002781 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002782 hash = PyObject_Hash(key);
2783 if (hash == -1)
2784 return NULL;
2785 }
INADA Naoki778928b2017-08-03 23:45:15 +09002786 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002787 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002788 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002789 if (ix == DKIX_EMPTY || value == NULL)
Victor Stinner742da042016-09-07 17:40:12 -07002790 Py_RETURN_FALSE;
2791 Py_RETURN_TRUE;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002792}
2793
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002794/*[clinic input]
2795dict.get
2796
2797 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002798 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002799 /
2800
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002801Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002802[clinic start generated code]*/
2803
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002804static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002805dict_get_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002806/*[clinic end generated code: output=bba707729dee05bf input=279ddb5790b6b107]*/
Barry Warsawc38c5da1997-10-06 17:49:20 +00002807{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002808 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002809 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002810 Py_ssize_t ix;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002812 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002813 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002814 hash = PyObject_Hash(key);
2815 if (hash == -1)
2816 return NULL;
2817 }
INADA Naoki778928b2017-08-03 23:45:15 +09002818 ix = (self->ma_keys->dk_lookup) (self, key, hash, &val);
Victor Stinner742da042016-09-07 17:40:12 -07002819 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002820 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002821 if (ix == DKIX_EMPTY || val == NULL) {
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002822 val = default_value;
INADA Naokiba609772016-12-07 20:41:42 +09002823 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002824 Py_INCREF(val);
2825 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002826}
2827
Benjamin Peterson00e98862013-03-07 22:16:29 -05002828PyObject *
2829PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002830{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002831 PyDictObject *mp = (PyDictObject *)d;
INADA Naoki93f26f72016-11-02 18:45:16 +09002832 PyObject *value;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002833 Py_hash_t hash;
Guido van Rossum164452c2000-08-08 16:12:54 +00002834
Benjamin Peterson00e98862013-03-07 22:16:29 -05002835 if (!PyDict_Check(d)) {
2836 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002837 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002838 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002840 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002841 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002842 hash = PyObject_Hash(key);
2843 if (hash == -1)
2844 return NULL;
2845 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002846
2847 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
2848 if (insertion_resize(mp) < 0)
2849 return NULL;
2850 }
2851
INADA Naoki778928b2017-08-03 23:45:15 +09002852 Py_ssize_t ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002853 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002854 return NULL;
INADA Naoki93f26f72016-11-02 18:45:16 +09002855
2856 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09002857 ((ix >= 0 && value == NULL && mp->ma_used != ix) ||
INADA Naoki93f26f72016-11-02 18:45:16 +09002858 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
2859 if (insertion_resize(mp) < 0) {
2860 return NULL;
2861 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002862 ix = DKIX_EMPTY;
2863 }
2864
2865 if (ix == DKIX_EMPTY) {
2866 PyDictKeyEntry *ep, *ep0;
2867 value = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002868 if (mp->ma_keys->dk_usable <= 0) {
Victor Stinner3c336c52016-09-12 14:17:40 +02002869 if (insertion_resize(mp) < 0) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002870 return NULL;
Victor Stinner3c336c52016-09-12 14:17:40 +02002871 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002872 }
INADA Naoki778928b2017-08-03 23:45:15 +09002873 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naoki93f26f72016-11-02 18:45:16 +09002874 ep0 = DK_ENTRIES(mp->ma_keys);
2875 ep = &ep0[mp->ma_keys->dk_nentries];
INADA Naokia7576492018-11-14 18:39:27 +09002876 dictkeys_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002877 Py_INCREF(key);
INADA Naoki93f26f72016-11-02 18:45:16 +09002878 Py_INCREF(value);
2879 MAINTAIN_TRACKING(mp, key, value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002880 ep->me_key = key;
2881 ep->me_hash = hash;
INADA Naokiba609772016-12-07 20:41:42 +09002882 if (_PyDict_HasSplitTable(mp)) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002883 assert(mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
2884 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Victor Stinner742da042016-09-07 17:40:12 -07002885 }
2886 else {
INADA Naoki93f26f72016-11-02 18:45:16 +09002887 ep->me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07002888 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002889 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002890 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naoki93f26f72016-11-02 18:45:16 +09002891 mp->ma_keys->dk_usable--;
2892 mp->ma_keys->dk_nentries++;
2893 assert(mp->ma_keys->dk_usable >= 0);
2894 }
INADA Naokiba609772016-12-07 20:41:42 +09002895 else if (value == NULL) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002896 value = defaultobj;
2897 assert(_PyDict_HasSplitTable(mp));
2898 assert(ix == mp->ma_used);
2899 Py_INCREF(value);
2900 MAINTAIN_TRACKING(mp, key, value);
INADA Naokiba609772016-12-07 20:41:42 +09002901 mp->ma_values[ix] = value;
INADA Naoki93f26f72016-11-02 18:45:16 +09002902 mp->ma_used++;
2903 mp->ma_version_tag = DICT_NEXT_VERSION();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002904 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002905
2906 assert(_PyDict_CheckConsistency(mp));
2907 return value;
Guido van Rossum164452c2000-08-08 16:12:54 +00002908}
2909
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002910/*[clinic input]
2911dict.setdefault
2912
2913 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002914 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002915 /
2916
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002917Insert key with a value of default if key is not in the dictionary.
2918
2919Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002920[clinic start generated code]*/
2921
Benjamin Peterson00e98862013-03-07 22:16:29 -05002922static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002923dict_setdefault_impl(PyDictObject *self, PyObject *key,
2924 PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002925/*[clinic end generated code: output=f8c1101ebf69e220 input=0f063756e815fd9d]*/
Benjamin Peterson00e98862013-03-07 22:16:29 -05002926{
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002927 PyObject *val;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002928
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002929 val = PyDict_SetDefault((PyObject *)self, key, default_value);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002930 Py_XINCREF(val);
2931 return val;
2932}
Guido van Rossum164452c2000-08-08 16:12:54 +00002933
2934static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302935dict_clear(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002936{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002937 PyDict_Clear((PyObject *)mp);
2938 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002939}
2940
Guido van Rossumba6ab842000-12-12 22:02:18 +00002941static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002942dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002943{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002944 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00002945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002946 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2947 return NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06002948
Yury Selivanov684ef2c2016-10-28 19:01:21 -04002949 return _PyDict_Pop((PyObject*)mp, key, deflt);
Guido van Rossume027d982002-04-12 15:11:59 +00002950}
2951
2952static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302953dict_popitem(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Guido van Rossumba6ab842000-12-12 22:02:18 +00002954{
Victor Stinner742da042016-09-07 17:40:12 -07002955 Py_ssize_t i, j;
2956 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002957 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002958
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002959 /* Allocate the result tuple before checking the size. Believe it
2960 * or not, this allocation could trigger a garbage collection which
2961 * could empty the dict, so if we checked the size first and that
2962 * happened, the result would be an infinite loop (searching for an
2963 * entry that no longer exists). Note that the usual popitem()
2964 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2965 * tuple away if the dict *is* empty isn't a significant
2966 * inefficiency -- possible, but unlikely in practice.
2967 */
2968 res = PyTuple_New(2);
2969 if (res == NULL)
2970 return NULL;
2971 if (mp->ma_used == 0) {
2972 Py_DECREF(res);
2973 PyErr_SetString(PyExc_KeyError,
2974 "popitem(): dictionary is empty");
2975 return NULL;
2976 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002977 /* Convert split table to combined table */
2978 if (mp->ma_keys->dk_lookup == lookdict_split) {
2979 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
2980 Py_DECREF(res);
2981 return NULL;
2982 }
2983 }
2984 ENSURE_ALLOWS_DELETIONS(mp);
Victor Stinner742da042016-09-07 17:40:12 -07002985
2986 /* Pop last item */
2987 ep0 = DK_ENTRIES(mp->ma_keys);
2988 i = mp->ma_keys->dk_nentries - 1;
2989 while (i >= 0 && ep0[i].me_value == NULL) {
2990 i--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002991 }
Victor Stinner742da042016-09-07 17:40:12 -07002992 assert(i >= 0);
2993
2994 ep = &ep0[i];
2995 j = lookdict_index(mp->ma_keys, ep->me_hash, i);
2996 assert(j >= 0);
INADA Naokia7576492018-11-14 18:39:27 +09002997 assert(dictkeys_get_index(mp->ma_keys, j) == i);
2998 dictkeys_set_index(mp->ma_keys, j, DKIX_DUMMY);
Victor Stinner742da042016-09-07 17:40:12 -07002999
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003000 PyTuple_SET_ITEM(res, 0, ep->me_key);
3001 PyTuple_SET_ITEM(res, 1, ep->me_value);
Victor Stinner742da042016-09-07 17:40:12 -07003002 ep->me_key = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003003 ep->me_value = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07003004 /* We can't dk_usable++ since there is DKIX_DUMMY in indices */
3005 mp->ma_keys->dk_nentries = i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003006 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003007 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +02003008 assert(_PyDict_CheckConsistency(mp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003009 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00003010}
3011
Jeremy Hylton8caad492000-06-23 14:18:11 +00003012static int
3013dict_traverse(PyObject *op, visitproc visit, void *arg)
3014{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003015 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson55f44522016-09-05 12:12:59 -07003016 PyDictKeysObject *keys = mp->ma_keys;
Serhiy Storchaka46825d22016-09-26 21:29:34 +03003017 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Victor Stinner742da042016-09-07 17:40:12 -07003018 Py_ssize_t i, n = keys->dk_nentries;
3019
Benjamin Peterson55f44522016-09-05 12:12:59 -07003020 if (keys->dk_lookup == lookdict) {
3021 for (i = 0; i < n; i++) {
3022 if (entries[i].me_value != NULL) {
3023 Py_VISIT(entries[i].me_value);
3024 Py_VISIT(entries[i].me_key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003025 }
3026 }
Victor Stinner742da042016-09-07 17:40:12 -07003027 }
3028 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003029 if (mp->ma_values != NULL) {
Benjamin Peterson55f44522016-09-05 12:12:59 -07003030 for (i = 0; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003031 Py_VISIT(mp->ma_values[i]);
3032 }
3033 }
3034 else {
Benjamin Peterson55f44522016-09-05 12:12:59 -07003035 for (i = 0; i < n; i++) {
3036 Py_VISIT(entries[i].me_value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003037 }
3038 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003039 }
3040 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00003041}
3042
3043static int
3044dict_tp_clear(PyObject *op)
3045{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003046 PyDict_Clear(op);
3047 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00003048}
3049
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003050static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003051
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003052Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003053_PyDict_SizeOf(PyDictObject *mp)
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003054{
Victor Stinner742da042016-09-07 17:40:12 -07003055 Py_ssize_t size, usable, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003056
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003057 size = DK_SIZE(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07003058 usable = USABLE_FRACTION(size);
3059
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02003060 res = _PyObject_SIZE(Py_TYPE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003061 if (mp->ma_values)
Victor Stinner742da042016-09-07 17:40:12 -07003062 res += usable * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003063 /* If the dictionary is split, the keys portion is accounted-for
3064 in the type object. */
3065 if (mp->ma_keys->dk_refcnt == 1)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003066 res += (sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003067 + DK_IXSIZE(mp->ma_keys) * size
3068 + sizeof(PyDictKeyEntry) * usable);
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003069 return res;
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003070}
3071
3072Py_ssize_t
3073_PyDict_KeysSize(PyDictKeysObject *keys)
3074{
Victor Stinner98ee9d52016-09-08 09:33:56 -07003075 return (sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003076 + DK_IXSIZE(keys) * DK_SIZE(keys)
3077 + USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry));
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003078}
3079
doko@ubuntu.com17210f52016-01-14 14:04:59 +01003080static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303081dict_sizeof(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003082{
3083 return PyLong_FromSsize_t(_PyDict_SizeOf(mp));
3084}
3085
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00003086PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
3087
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003088PyDoc_STRVAR(sizeof__doc__,
3089"D.__sizeof__() -> size of D in memory, in bytes");
3090
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003091PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00003092"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00003093If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00003094
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003095PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00003096"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000030972-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003098
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003099PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04003100"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
3101If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
3102If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
3103In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00003104
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003105PyDoc_STRVAR(clear__doc__,
3106"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003107
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003108PyDoc_STRVAR(copy__doc__,
3109"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00003110
Guido van Rossumb90c8482007-02-10 01:11:45 +00003111/* Forward */
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303112static PyObject *dictkeys_new(PyObject *, PyObject *);
3113static PyObject *dictitems_new(PyObject *, PyObject *);
3114static PyObject *dictvalues_new(PyObject *, PyObject *);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003115
Guido van Rossum45c85d12007-07-27 16:31:40 +00003116PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003117 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003118PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003119 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003120PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003121 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00003122
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003123static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07003124 DICT___CONTAINS___METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003125 {"__getitem__", (PyCFunction)(void(*)(void))dict_subscript, METH_O | METH_COEXIST,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003126 getitem__doc__},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003127 {"__sizeof__", (PyCFunction)(void(*)(void))dict_sizeof, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003128 sizeof__doc__},
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003129 DICT_GET_METHODDEF
3130 DICT_SETDEFAULT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003131 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
3132 pop__doc__},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003133 {"popitem", (PyCFunction)(void(*)(void))dict_popitem, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003134 popitem__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303135 {"keys", dictkeys_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003136 keys__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303137 {"items", dictitems_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003138 items__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303139 {"values", dictvalues_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003140 values__doc__},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003141 {"update", (PyCFunction)(void(*)(void))dict_update, METH_VARARGS | METH_KEYWORDS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003142 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08003143 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003144 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
3145 clear__doc__},
3146 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
3147 copy__doc__},
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003148 DICT___REVERSED___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003149 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003150};
3151
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00003152/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00003153int
3154PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003155{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003156 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07003157 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003158 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003159 PyObject *value;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003161 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003162 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003163 hash = PyObject_Hash(key);
3164 if (hash == -1)
3165 return -1;
3166 }
INADA Naoki778928b2017-08-03 23:45:15 +09003167 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003168 if (ix == DKIX_ERROR)
3169 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003170 return (ix != DKIX_EMPTY && value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003171}
3172
Thomas Wouterscf297e42007-02-23 15:07:44 +00003173/* Internal version of PyDict_Contains used when the hash value is already known */
3174int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003175_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00003176{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003177 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003178 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -07003179 Py_ssize_t ix;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003180
INADA Naoki778928b2017-08-03 23:45:15 +09003181 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003182 if (ix == DKIX_ERROR)
3183 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003184 return (ix != DKIX_EMPTY && value != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00003185}
3186
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003187/* Hack to implement "key in dict" */
3188static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003189 0, /* sq_length */
3190 0, /* sq_concat */
3191 0, /* sq_repeat */
3192 0, /* sq_item */
3193 0, /* sq_slice */
3194 0, /* sq_ass_item */
3195 0, /* sq_ass_slice */
3196 PyDict_Contains, /* sq_contains */
3197 0, /* sq_inplace_concat */
3198 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003199};
3200
Guido van Rossum09e563a2001-05-01 12:10:21 +00003201static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003202dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3203{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003204 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003205 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003207 assert(type != NULL && type->tp_alloc != NULL);
3208 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02003209 if (self == NULL)
3210 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003211 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003212
Victor Stinnera9f61a52013-07-16 22:17:26 +02003213 /* The object has been implicitly tracked by tp_alloc */
3214 if (type == &PyDict_Type)
3215 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003216
3217 d->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003218 d->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07003219 d->ma_keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003220 if (d->ma_keys == NULL) {
3221 Py_DECREF(self);
3222 return NULL;
3223 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02003224 assert(_PyDict_CheckConsistency(d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003225 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003226}
3227
Tim Peters25786c02001-09-02 08:22:48 +00003228static int
3229dict_init(PyObject *self, PyObject *args, PyObject *kwds)
3230{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003231 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00003232}
3233
Tim Peters6d6c1a32001-08-02 04:15:00 +00003234static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003235dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00003236{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003237 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003238}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003239
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003240PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00003241"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00003242"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003243" (key, value) pairs\n"
3244"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00003245" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003246" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00003247" d[k] = v\n"
3248"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
3249" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00003250
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003251PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003252 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3253 "dict",
3254 sizeof(PyDictObject),
3255 0,
3256 (destructor)dict_dealloc, /* tp_dealloc */
3257 0, /* tp_print */
3258 0, /* tp_getattr */
3259 0, /* tp_setattr */
3260 0, /* tp_reserved */
3261 (reprfunc)dict_repr, /* tp_repr */
3262 0, /* tp_as_number */
3263 &dict_as_sequence, /* tp_as_sequence */
3264 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003265 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003266 0, /* tp_call */
3267 0, /* tp_str */
3268 PyObject_GenericGetAttr, /* tp_getattro */
3269 0, /* tp_setattro */
3270 0, /* tp_as_buffer */
3271 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3272 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
3273 dictionary_doc, /* tp_doc */
3274 dict_traverse, /* tp_traverse */
3275 dict_tp_clear, /* tp_clear */
3276 dict_richcompare, /* tp_richcompare */
3277 0, /* tp_weaklistoffset */
3278 (getiterfunc)dict_iter, /* tp_iter */
3279 0, /* tp_iternext */
3280 mapp_methods, /* tp_methods */
3281 0, /* tp_members */
3282 0, /* tp_getset */
3283 0, /* tp_base */
3284 0, /* tp_dict */
3285 0, /* tp_descr_get */
3286 0, /* tp_descr_set */
3287 0, /* tp_dictoffset */
3288 dict_init, /* tp_init */
3289 PyType_GenericAlloc, /* tp_alloc */
3290 dict_new, /* tp_new */
3291 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003292};
3293
Victor Stinner3c1e4812012-03-26 22:10:51 +02003294PyObject *
3295_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
3296{
3297 PyObject *kv;
3298 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02003299 if (kv == NULL) {
3300 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003301 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02003302 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02003303 return PyDict_GetItem(dp, kv);
3304}
3305
Guido van Rossum3cca2451997-05-16 14:23:33 +00003306/* For backward compatibility with old dictionary interface */
3307
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003308PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003309PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003310{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003311 PyObject *kv, *rv;
3312 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003313 if (kv == NULL) {
3314 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003315 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003316 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003317 rv = PyDict_GetItem(v, kv);
3318 Py_DECREF(kv);
3319 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003320}
3321
3322int
Victor Stinner3c1e4812012-03-26 22:10:51 +02003323_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
3324{
3325 PyObject *kv;
3326 kv = _PyUnicode_FromId(key); /* borrowed */
3327 if (kv == NULL)
3328 return -1;
3329 return PyDict_SetItem(v, kv, item);
3330}
3331
3332int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003333PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003334{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003335 PyObject *kv;
3336 int err;
3337 kv = PyUnicode_FromString(key);
3338 if (kv == NULL)
3339 return -1;
3340 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
3341 err = PyDict_SetItem(v, kv, item);
3342 Py_DECREF(kv);
3343 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003344}
3345
3346int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01003347_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
3348{
3349 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
3350 if (kv == NULL)
3351 return -1;
3352 return PyDict_DelItem(v, kv);
3353}
3354
3355int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003356PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003357{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003358 PyObject *kv;
3359 int err;
3360 kv = PyUnicode_FromString(key);
3361 if (kv == NULL)
3362 return -1;
3363 err = PyDict_DelItem(v, kv);
3364 Py_DECREF(kv);
3365 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003366}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003367
Raymond Hettinger019a1482004-03-18 02:41:19 +00003368/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003369
3370typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003371 PyObject_HEAD
3372 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
3373 Py_ssize_t di_used;
3374 Py_ssize_t di_pos;
3375 PyObject* di_result; /* reusable result tuple for iteritems */
3376 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003377} dictiterobject;
3378
3379static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003380dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003381{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003382 dictiterobject *di;
3383 di = PyObject_GC_New(dictiterobject, itertype);
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003384 if (di == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003385 return NULL;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003386 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003387 Py_INCREF(dict);
3388 di->di_dict = dict;
3389 di->di_used = dict->ma_used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003390 di->len = dict->ma_used;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003391 if ((itertype == &PyDictRevIterKey_Type ||
3392 itertype == &PyDictRevIterItem_Type ||
3393 itertype == &PyDictRevIterValue_Type) && dict->ma_used) {
3394 di->di_pos = dict->ma_keys->dk_nentries - 1;
3395 }
3396 else {
3397 di->di_pos = 0;
3398 }
3399 if (itertype == &PyDictIterItem_Type ||
3400 itertype == &PyDictRevIterItem_Type) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003401 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
3402 if (di->di_result == NULL) {
3403 Py_DECREF(di);
3404 return NULL;
3405 }
3406 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003407 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003408 di->di_result = NULL;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003409 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003410 _PyObject_GC_TRACK(di);
3411 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003412}
3413
3414static void
3415dictiter_dealloc(dictiterobject *di)
3416{
INADA Naokia6296d32017-08-24 14:55:17 +09003417 /* bpo-31095: UnTrack is needed before calling any callbacks */
3418 _PyObject_GC_UNTRACK(di);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003419 Py_XDECREF(di->di_dict);
3420 Py_XDECREF(di->di_result);
3421 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003422}
3423
3424static int
3425dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
3426{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003427 Py_VISIT(di->di_dict);
3428 Py_VISIT(di->di_result);
3429 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003430}
3431
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003432static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303433dictiter_len(dictiterobject *di, PyObject *Py_UNUSED(ignored))
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003434{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003435 Py_ssize_t len = 0;
3436 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
3437 len = di->len;
3438 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003439}
3440
Guido van Rossumb90c8482007-02-10 01:11:45 +00003441PyDoc_STRVAR(length_hint_doc,
3442 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003443
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003444static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303445dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003446
3447PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3448
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003449static PyMethodDef dictiter_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003450 {"__length_hint__", (PyCFunction)(void(*)(void))dictiter_len, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003451 length_hint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003452 {"__reduce__", (PyCFunction)(void(*)(void))dictiter_reduce, METH_NOARGS,
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003453 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003454 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003455};
3456
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003457static PyObject*
3458dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00003459{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003460 PyObject *key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003461 Py_ssize_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003462 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003463 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00003464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003465 if (d == NULL)
3466 return NULL;
3467 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00003468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003469 if (di->di_used != d->ma_used) {
3470 PyErr_SetString(PyExc_RuntimeError,
3471 "dictionary changed size during iteration");
3472 di->di_used = -1; /* Make this state sticky */
3473 return NULL;
3474 }
Guido van Rossum2147df72002-07-16 20:30:22 +00003475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003476 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003477 k = d->ma_keys;
INADA Naokica2d8be2016-11-04 16:59:10 +09003478 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003479 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003480 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003481 goto fail;
3482 key = DK_ENTRIES(k)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003483 assert(d->ma_values[i] != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003484 }
3485 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003486 Py_ssize_t n = k->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003487 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
3488 while (i < n && entry_ptr->me_value == NULL) {
3489 entry_ptr++;
3490 i++;
3491 }
3492 if (i >= n)
3493 goto fail;
3494 key = entry_ptr->me_key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003495 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003496 di->di_pos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003497 di->len--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003498 Py_INCREF(key);
3499 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003500
3501fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003502 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003503 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003504 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003505}
3506
Raymond Hettinger019a1482004-03-18 02:41:19 +00003507PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003508 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3509 "dict_keyiterator", /* tp_name */
3510 sizeof(dictiterobject), /* tp_basicsize */
3511 0, /* tp_itemsize */
3512 /* methods */
3513 (destructor)dictiter_dealloc, /* tp_dealloc */
3514 0, /* tp_print */
3515 0, /* tp_getattr */
3516 0, /* tp_setattr */
3517 0, /* tp_reserved */
3518 0, /* tp_repr */
3519 0, /* tp_as_number */
3520 0, /* tp_as_sequence */
3521 0, /* tp_as_mapping */
3522 0, /* tp_hash */
3523 0, /* tp_call */
3524 0, /* tp_str */
3525 PyObject_GenericGetAttr, /* tp_getattro */
3526 0, /* tp_setattro */
3527 0, /* tp_as_buffer */
3528 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3529 0, /* tp_doc */
3530 (traverseproc)dictiter_traverse, /* tp_traverse */
3531 0, /* tp_clear */
3532 0, /* tp_richcompare */
3533 0, /* tp_weaklistoffset */
3534 PyObject_SelfIter, /* tp_iter */
3535 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
3536 dictiter_methods, /* tp_methods */
3537 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003538};
3539
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003540static PyObject *
3541dictiter_iternextvalue(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003542{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003543 PyObject *value;
INADA Naokica2d8be2016-11-04 16:59:10 +09003544 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003545 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003547 if (d == NULL)
3548 return NULL;
3549 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003551 if (di->di_used != d->ma_used) {
3552 PyErr_SetString(PyExc_RuntimeError,
3553 "dictionary changed size during iteration");
3554 di->di_used = -1; /* Make this state sticky */
3555 return NULL;
3556 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003558 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003559 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003560 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003561 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003562 goto fail;
INADA Naokica2d8be2016-11-04 16:59:10 +09003563 value = d->ma_values[i];
3564 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003565 }
3566 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003567 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003568 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3569 while (i < n && entry_ptr->me_value == NULL) {
3570 entry_ptr++;
3571 i++;
3572 }
3573 if (i >= n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003574 goto fail;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003575 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003576 }
3577 di->di_pos = i+1;
3578 di->len--;
3579 Py_INCREF(value);
3580 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003581
3582fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003583 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003584 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003585 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003586}
3587
3588PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003589 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3590 "dict_valueiterator", /* tp_name */
3591 sizeof(dictiterobject), /* tp_basicsize */
3592 0, /* tp_itemsize */
3593 /* methods */
3594 (destructor)dictiter_dealloc, /* tp_dealloc */
3595 0, /* tp_print */
3596 0, /* tp_getattr */
3597 0, /* tp_setattr */
3598 0, /* tp_reserved */
3599 0, /* tp_repr */
3600 0, /* tp_as_number */
3601 0, /* tp_as_sequence */
3602 0, /* tp_as_mapping */
3603 0, /* tp_hash */
3604 0, /* tp_call */
3605 0, /* tp_str */
3606 PyObject_GenericGetAttr, /* tp_getattro */
3607 0, /* tp_setattro */
3608 0, /* tp_as_buffer */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003609 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003610 0, /* tp_doc */
3611 (traverseproc)dictiter_traverse, /* tp_traverse */
3612 0, /* tp_clear */
3613 0, /* tp_richcompare */
3614 0, /* tp_weaklistoffset */
3615 PyObject_SelfIter, /* tp_iter */
3616 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3617 dictiter_methods, /* tp_methods */
3618 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003619};
3620
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003621static PyObject *
3622dictiter_iternextitem(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003623{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003624 PyObject *key, *value, *result;
INADA Naokica2d8be2016-11-04 16:59:10 +09003625 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003626 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003628 if (d == NULL)
3629 return NULL;
3630 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003632 if (di->di_used != d->ma_used) {
3633 PyErr_SetString(PyExc_RuntimeError,
3634 "dictionary changed size during iteration");
3635 di->di_used = -1; /* Make this state sticky */
3636 return NULL;
3637 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003639 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003640 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003641 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003642 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003643 goto fail;
3644 key = DK_ENTRIES(d->ma_keys)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003645 value = d->ma_values[i];
3646 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003647 }
3648 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003649 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003650 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3651 while (i < n && entry_ptr->me_value == NULL) {
3652 entry_ptr++;
3653 i++;
3654 }
3655 if (i >= n)
3656 goto fail;
3657 key = entry_ptr->me_key;
3658 value = entry_ptr->me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003659 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003660 di->di_pos = i+1;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003661 di->len--;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003662 Py_INCREF(key);
3663 Py_INCREF(value);
3664 result = di->di_result;
3665 if (Py_REFCNT(result) == 1) {
3666 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3667 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3668 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3669 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003670 Py_INCREF(result);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003671 Py_DECREF(oldkey);
3672 Py_DECREF(oldvalue);
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003673 }
3674 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003675 result = PyTuple_New(2);
3676 if (result == NULL)
3677 return NULL;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003678 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3679 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003680 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003681 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003682
3683fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003684 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003685 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003686 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003687}
3688
3689PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003690 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3691 "dict_itemiterator", /* tp_name */
3692 sizeof(dictiterobject), /* tp_basicsize */
3693 0, /* tp_itemsize */
3694 /* methods */
3695 (destructor)dictiter_dealloc, /* tp_dealloc */
3696 0, /* tp_print */
3697 0, /* tp_getattr */
3698 0, /* tp_setattr */
3699 0, /* tp_reserved */
3700 0, /* tp_repr */
3701 0, /* tp_as_number */
3702 0, /* tp_as_sequence */
3703 0, /* tp_as_mapping */
3704 0, /* tp_hash */
3705 0, /* tp_call */
3706 0, /* tp_str */
3707 PyObject_GenericGetAttr, /* tp_getattro */
3708 0, /* tp_setattro */
3709 0, /* tp_as_buffer */
3710 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3711 0, /* tp_doc */
3712 (traverseproc)dictiter_traverse, /* tp_traverse */
3713 0, /* tp_clear */
3714 0, /* tp_richcompare */
3715 0, /* tp_weaklistoffset */
3716 PyObject_SelfIter, /* tp_iter */
3717 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3718 dictiter_methods, /* tp_methods */
3719 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003720};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003721
3722
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003723/* dictreviter */
3724
3725static PyObject *
3726dictreviter_iternext(dictiterobject *di)
3727{
3728 PyDictObject *d = di->di_dict;
3729
3730 if (d == NULL) {
3731 return NULL;
3732 }
3733 assert (PyDict_Check(d));
3734
3735 if (di->di_used != d->ma_used) {
3736 PyErr_SetString(PyExc_RuntimeError,
3737 "dictionary changed size during iteration");
3738 di->di_used = -1; /* Make this state sticky */
3739 return NULL;
3740 }
3741
3742 Py_ssize_t i = di->di_pos;
3743 PyDictKeysObject *k = d->ma_keys;
3744 PyObject *key, *value, *result;
3745
3746 if (d->ma_values) {
3747 if (i < 0) {
3748 goto fail;
3749 }
3750 key = DK_ENTRIES(k)[i].me_key;
3751 value = d->ma_values[i];
3752 assert (value != NULL);
3753 }
3754 else {
3755 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
3756 while (i >= 0 && entry_ptr->me_value == NULL) {
3757 entry_ptr--;
3758 i--;
3759 }
3760 if (i < 0) {
3761 goto fail;
3762 }
3763 key = entry_ptr->me_key;
3764 value = entry_ptr->me_value;
3765 }
3766 di->di_pos = i-1;
3767 di->len--;
3768
3769 if (Py_TYPE(di) == &PyDictRevIterKey_Type) {
3770 Py_INCREF(key);
3771 return key;
3772 }
3773 else if (Py_TYPE(di) == &PyDictRevIterValue_Type) {
3774 Py_INCREF(value);
3775 return value;
3776 }
3777 else if (Py_TYPE(di) == &PyDictRevIterItem_Type) {
3778 Py_INCREF(key);
3779 Py_INCREF(value);
3780 result = di->di_result;
3781 if (Py_REFCNT(result) == 1) {
3782 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3783 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3784 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3785 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
3786 Py_INCREF(result);
3787 Py_DECREF(oldkey);
3788 Py_DECREF(oldvalue);
3789 }
3790 else {
3791 result = PyTuple_New(2);
3792 if (result == NULL) {
3793 return NULL;
3794 }
3795 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3796 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
3797 }
3798 return result;
3799 }
3800 else {
3801 Py_UNREACHABLE();
3802 }
3803
3804fail:
3805 di->di_dict = NULL;
3806 Py_DECREF(d);
3807 return NULL;
3808}
3809
3810PyTypeObject PyDictRevIterKey_Type = {
3811 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3812 "dict_reversekeyiterator",
3813 sizeof(dictiterobject),
3814 .tp_dealloc = (destructor)dictiter_dealloc,
3815 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
3816 .tp_traverse = (traverseproc)dictiter_traverse,
3817 .tp_iter = PyObject_SelfIter,
3818 .tp_iternext = (iternextfunc)dictreviter_iternext,
3819 .tp_methods = dictiter_methods
3820};
3821
3822
3823/*[clinic input]
3824dict.__reversed__
3825
3826Return a reverse iterator over the dict keys.
3827[clinic start generated code]*/
3828
3829static PyObject *
3830dict___reversed___impl(PyDictObject *self)
3831/*[clinic end generated code: output=e674483336d1ed51 input=23210ef3477d8c4d]*/
3832{
3833 assert (PyDict_Check(self));
3834 return dictiter_new(self, &PyDictRevIterKey_Type);
3835}
3836
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003837static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303838dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003839{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003840 _Py_IDENTIFIER(iter);
Sergey Fedoseev63958442018-10-20 05:43:33 +05003841 /* copy the iterator state */
3842 dictiterobject tmp = *di;
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003843 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003844
Sergey Fedoseev63958442018-10-20 05:43:33 +05003845 PyObject *list = PySequence_List((PyObject*)&tmp);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003846 Py_XDECREF(tmp.di_dict);
Sergey Fedoseev63958442018-10-20 05:43:33 +05003847 if (list == NULL) {
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003848 return NULL;
3849 }
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003850 return Py_BuildValue("N(N)", _PyEval_GetBuiltinId(&PyId_iter), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003851}
3852
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003853PyTypeObject PyDictRevIterItem_Type = {
3854 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3855 "dict_reverseitemiterator",
3856 sizeof(dictiterobject),
3857 .tp_dealloc = (destructor)dictiter_dealloc,
3858 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
3859 .tp_traverse = (traverseproc)dictiter_traverse,
3860 .tp_iter = PyObject_SelfIter,
3861 .tp_iternext = (iternextfunc)dictreviter_iternext,
3862 .tp_methods = dictiter_methods
3863};
3864
3865PyTypeObject PyDictRevIterValue_Type = {
3866 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3867 "dict_reversevalueiterator",
3868 sizeof(dictiterobject),
3869 .tp_dealloc = (destructor)dictiter_dealloc,
3870 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
3871 .tp_traverse = (traverseproc)dictiter_traverse,
3872 .tp_iter = PyObject_SelfIter,
3873 .tp_iternext = (iternextfunc)dictreviter_iternext,
3874 .tp_methods = dictiter_methods
3875};
3876
Guido van Rossum3ac67412007-02-10 18:55:06 +00003877/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003878/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003879/***********************************************/
3880
Guido van Rossumb90c8482007-02-10 01:11:45 +00003881/* The instance lay-out is the same for all three; but the type differs. */
3882
Guido van Rossumb90c8482007-02-10 01:11:45 +00003883static void
Eric Snow96c6af92015-05-29 22:21:39 -06003884dictview_dealloc(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003885{
INADA Naokia6296d32017-08-24 14:55:17 +09003886 /* bpo-31095: UnTrack is needed before calling any callbacks */
3887 _PyObject_GC_UNTRACK(dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003888 Py_XDECREF(dv->dv_dict);
3889 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003890}
3891
3892static int
Eric Snow96c6af92015-05-29 22:21:39 -06003893dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003894{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003895 Py_VISIT(dv->dv_dict);
3896 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003897}
3898
Guido van Rossum83825ac2007-02-10 04:54:19 +00003899static Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003900dictview_len(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003901{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003902 Py_ssize_t len = 0;
3903 if (dv->dv_dict != NULL)
3904 len = dv->dv_dict->ma_used;
3905 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003906}
3907
Eric Snow96c6af92015-05-29 22:21:39 -06003908PyObject *
3909_PyDictView_New(PyObject *dict, PyTypeObject *type)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003910{
Eric Snow96c6af92015-05-29 22:21:39 -06003911 _PyDictViewObject *dv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003912 if (dict == NULL) {
3913 PyErr_BadInternalCall();
3914 return NULL;
3915 }
3916 if (!PyDict_Check(dict)) {
3917 /* XXX Get rid of this restriction later */
3918 PyErr_Format(PyExc_TypeError,
3919 "%s() requires a dict argument, not '%s'",
3920 type->tp_name, dict->ob_type->tp_name);
3921 return NULL;
3922 }
Eric Snow96c6af92015-05-29 22:21:39 -06003923 dv = PyObject_GC_New(_PyDictViewObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003924 if (dv == NULL)
3925 return NULL;
3926 Py_INCREF(dict);
3927 dv->dv_dict = (PyDictObject *)dict;
3928 _PyObject_GC_TRACK(dv);
3929 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003930}
3931
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003932/* TODO(guido): The views objects are not complete:
3933
3934 * support more set operations
3935 * support arbitrary mappings?
3936 - either these should be static or exported in dictobject.h
3937 - if public then they should probably be in builtins
3938*/
3939
Guido van Rossumaac530c2007-08-24 22:33:45 +00003940/* Return 1 if self is a subset of other, iterating over self;
3941 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003942static int
3943all_contained_in(PyObject *self, PyObject *other)
3944{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003945 PyObject *iter = PyObject_GetIter(self);
3946 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003947
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003948 if (iter == NULL)
3949 return -1;
3950 for (;;) {
3951 PyObject *next = PyIter_Next(iter);
3952 if (next == NULL) {
3953 if (PyErr_Occurred())
3954 ok = -1;
3955 break;
3956 }
3957 ok = PySequence_Contains(other, next);
3958 Py_DECREF(next);
3959 if (ok <= 0)
3960 break;
3961 }
3962 Py_DECREF(iter);
3963 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003964}
3965
3966static PyObject *
3967dictview_richcompare(PyObject *self, PyObject *other, int op)
3968{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003969 Py_ssize_t len_self, len_other;
3970 int ok;
3971 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003973 assert(self != NULL);
3974 assert(PyDictViewSet_Check(self));
3975 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00003976
Brian Curtindfc80e32011-08-10 20:28:54 -05003977 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
3978 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003980 len_self = PyObject_Size(self);
3981 if (len_self < 0)
3982 return NULL;
3983 len_other = PyObject_Size(other);
3984 if (len_other < 0)
3985 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003987 ok = 0;
3988 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00003989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003990 case Py_NE:
3991 case Py_EQ:
3992 if (len_self == len_other)
3993 ok = all_contained_in(self, other);
3994 if (op == Py_NE && ok >= 0)
3995 ok = !ok;
3996 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003998 case Py_LT:
3999 if (len_self < len_other)
4000 ok = all_contained_in(self, other);
4001 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004003 case Py_LE:
4004 if (len_self <= len_other)
4005 ok = all_contained_in(self, other);
4006 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004008 case Py_GT:
4009 if (len_self > len_other)
4010 ok = all_contained_in(other, self);
4011 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004012
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004013 case Py_GE:
4014 if (len_self >= len_other)
4015 ok = all_contained_in(other, self);
4016 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004018 }
4019 if (ok < 0)
4020 return NULL;
4021 result = ok ? Py_True : Py_False;
4022 Py_INCREF(result);
4023 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00004024}
4025
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004026static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004027dictview_repr(_PyDictViewObject *dv)
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004028{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004029 PyObject *seq;
bennorthd7773d92018-01-26 15:46:01 +00004030 PyObject *result = NULL;
4031 Py_ssize_t rc;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004032
bennorthd7773d92018-01-26 15:46:01 +00004033 rc = Py_ReprEnter((PyObject *)dv);
4034 if (rc != 0) {
4035 return rc > 0 ? PyUnicode_FromString("...") : NULL;
4036 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004037 seq = PySequence_List((PyObject *)dv);
bennorthd7773d92018-01-26 15:46:01 +00004038 if (seq == NULL) {
4039 goto Done;
4040 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004041 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
4042 Py_DECREF(seq);
bennorthd7773d92018-01-26 15:46:01 +00004043
4044Done:
4045 Py_ReprLeave((PyObject *)dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004046 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004047}
4048
Guido van Rossum3ac67412007-02-10 18:55:06 +00004049/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004050
4051static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004052dictkeys_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004053{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004054 if (dv->dv_dict == NULL) {
4055 Py_RETURN_NONE;
4056 }
4057 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004058}
4059
4060static int
Eric Snow96c6af92015-05-29 22:21:39 -06004061dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004062{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004063 if (dv->dv_dict == NULL)
4064 return 0;
4065 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004066}
4067
Guido van Rossum83825ac2007-02-10 04:54:19 +00004068static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004069 (lenfunc)dictview_len, /* sq_length */
4070 0, /* sq_concat */
4071 0, /* sq_repeat */
4072 0, /* sq_item */
4073 0, /* sq_slice */
4074 0, /* sq_ass_item */
4075 0, /* sq_ass_slice */
4076 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004077};
4078
Guido van Rossum523259b2007-08-24 23:41:22 +00004079static PyObject*
4080dictviews_sub(PyObject* self, PyObject *other)
4081{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004082 PyObject *result = PySet_New(self);
4083 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004084 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004085
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004086 if (result == NULL)
4087 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004088
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08004089 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004090 if (tmp == NULL) {
4091 Py_DECREF(result);
4092 return NULL;
4093 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004094
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004095 Py_DECREF(tmp);
4096 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004097}
4098
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004099PyObject*
4100_PyDictView_Intersect(PyObject* self, PyObject *other)
Guido van Rossum523259b2007-08-24 23:41:22 +00004101{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004102 PyObject *result = PySet_New(self);
4103 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004104 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004106 if (result == NULL)
4107 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004108
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08004109 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_intersection_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004110 if (tmp == NULL) {
4111 Py_DECREF(result);
4112 return NULL;
4113 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004115 Py_DECREF(tmp);
4116 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004117}
4118
4119static PyObject*
4120dictviews_or(PyObject* self, PyObject *other)
4121{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004122 PyObject *result = PySet_New(self);
4123 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004124 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02004125
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004126 if (result == NULL)
4127 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004128
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08004129 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004130 if (tmp == NULL) {
4131 Py_DECREF(result);
4132 return NULL;
4133 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004135 Py_DECREF(tmp);
4136 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004137}
4138
4139static PyObject*
4140dictviews_xor(PyObject* self, PyObject *other)
4141{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004142 PyObject *result = PySet_New(self);
4143 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004144 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004146 if (result == NULL)
4147 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004148
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08004149 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_symmetric_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004150 if (tmp == NULL) {
4151 Py_DECREF(result);
4152 return NULL;
4153 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004155 Py_DECREF(tmp);
4156 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004157}
4158
4159static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004160 0, /*nb_add*/
4161 (binaryfunc)dictviews_sub, /*nb_subtract*/
4162 0, /*nb_multiply*/
4163 0, /*nb_remainder*/
4164 0, /*nb_divmod*/
4165 0, /*nb_power*/
4166 0, /*nb_negative*/
4167 0, /*nb_positive*/
4168 0, /*nb_absolute*/
4169 0, /*nb_bool*/
4170 0, /*nb_invert*/
4171 0, /*nb_lshift*/
4172 0, /*nb_rshift*/
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004173 (binaryfunc)_PyDictView_Intersect, /*nb_and*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004174 (binaryfunc)dictviews_xor, /*nb_xor*/
4175 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00004176};
4177
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004178static PyObject*
4179dictviews_isdisjoint(PyObject *self, PyObject *other)
4180{
4181 PyObject *it;
4182 PyObject *item = NULL;
4183
4184 if (self == other) {
Eric Snow96c6af92015-05-29 22:21:39 -06004185 if (dictview_len((_PyDictViewObject *)self) == 0)
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004186 Py_RETURN_TRUE;
4187 else
4188 Py_RETURN_FALSE;
4189 }
4190
4191 /* Iterate over the shorter object (only if other is a set,
4192 * because PySequence_Contains may be expensive otherwise): */
4193 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
Eric Snow96c6af92015-05-29 22:21:39 -06004194 Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self);
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004195 Py_ssize_t len_other = PyObject_Size(other);
4196 if (len_other == -1)
4197 return NULL;
4198
4199 if ((len_other > len_self)) {
4200 PyObject *tmp = other;
4201 other = self;
4202 self = tmp;
4203 }
4204 }
4205
4206 it = PyObject_GetIter(other);
4207 if (it == NULL)
4208 return NULL;
4209
4210 while ((item = PyIter_Next(it)) != NULL) {
4211 int contains = PySequence_Contains(self, item);
4212 Py_DECREF(item);
4213 if (contains == -1) {
4214 Py_DECREF(it);
4215 return NULL;
4216 }
4217
4218 if (contains) {
4219 Py_DECREF(it);
4220 Py_RETURN_FALSE;
4221 }
4222 }
4223 Py_DECREF(it);
4224 if (PyErr_Occurred())
4225 return NULL; /* PyIter_Next raised an exception. */
4226 Py_RETURN_TRUE;
4227}
4228
4229PyDoc_STRVAR(isdisjoint_doc,
4230"Return True if the view and the given iterable have a null intersection.");
4231
Serhiy Storchaka81524022018-11-27 13:05:02 +02004232static PyObject* dictkeys_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored));
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004233
4234PyDoc_STRVAR(reversed_keys_doc,
4235"Return a reverse iterator over the dict keys.");
4236
Guido van Rossumb90c8482007-02-10 01:11:45 +00004237static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004238 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4239 isdisjoint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004240 {"__reversed__", (PyCFunction)(void(*)(void))dictkeys_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004241 reversed_keys_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004242 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004243};
4244
4245PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004246 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4247 "dict_keys", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004248 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004249 0, /* tp_itemsize */
4250 /* methods */
4251 (destructor)dictview_dealloc, /* tp_dealloc */
4252 0, /* tp_print */
4253 0, /* tp_getattr */
4254 0, /* tp_setattr */
4255 0, /* tp_reserved */
4256 (reprfunc)dictview_repr, /* tp_repr */
4257 &dictviews_as_number, /* tp_as_number */
4258 &dictkeys_as_sequence, /* tp_as_sequence */
4259 0, /* tp_as_mapping */
4260 0, /* tp_hash */
4261 0, /* tp_call */
4262 0, /* tp_str */
4263 PyObject_GenericGetAttr, /* tp_getattro */
4264 0, /* tp_setattro */
4265 0, /* tp_as_buffer */
4266 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4267 0, /* tp_doc */
4268 (traverseproc)dictview_traverse, /* tp_traverse */
4269 0, /* tp_clear */
4270 dictview_richcompare, /* tp_richcompare */
4271 0, /* tp_weaklistoffset */
4272 (getiterfunc)dictkeys_iter, /* tp_iter */
4273 0, /* tp_iternext */
4274 dictkeys_methods, /* tp_methods */
4275 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004276};
4277
4278static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304279dictkeys_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004280{
Eric Snow96c6af92015-05-29 22:21:39 -06004281 return _PyDictView_New(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004282}
4283
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004284static PyObject *
Serhiy Storchaka81524022018-11-27 13:05:02 +02004285dictkeys_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored))
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004286{
4287 if (dv->dv_dict == NULL) {
4288 Py_RETURN_NONE;
4289 }
4290 return dictiter_new(dv->dv_dict, &PyDictRevIterKey_Type);
4291}
4292
Guido van Rossum3ac67412007-02-10 18:55:06 +00004293/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004294
4295static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004296dictitems_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004297{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004298 if (dv->dv_dict == NULL) {
4299 Py_RETURN_NONE;
4300 }
4301 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004302}
4303
4304static int
Eric Snow96c6af92015-05-29 22:21:39 -06004305dictitems_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004306{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004307 int result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004308 PyObject *key, *value, *found;
4309 if (dv->dv_dict == NULL)
4310 return 0;
4311 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
4312 return 0;
4313 key = PyTuple_GET_ITEM(obj, 0);
4314 value = PyTuple_GET_ITEM(obj, 1);
Raymond Hettinger6692f012016-09-18 21:46:08 -07004315 found = PyDict_GetItemWithError((PyObject *)dv->dv_dict, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004316 if (found == NULL) {
4317 if (PyErr_Occurred())
4318 return -1;
4319 return 0;
4320 }
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004321 Py_INCREF(found);
4322 result = PyObject_RichCompareBool(value, found, Py_EQ);
4323 Py_DECREF(found);
4324 return result;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004325}
4326
Guido van Rossum83825ac2007-02-10 04:54:19 +00004327static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004328 (lenfunc)dictview_len, /* sq_length */
4329 0, /* sq_concat */
4330 0, /* sq_repeat */
4331 0, /* sq_item */
4332 0, /* sq_slice */
4333 0, /* sq_ass_item */
4334 0, /* sq_ass_slice */
4335 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004336};
4337
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004338static PyObject* dictitems_reversed(_PyDictViewObject *dv);
4339
4340PyDoc_STRVAR(reversed_items_doc,
4341"Return a reverse iterator over the dict items.");
4342
Guido van Rossumb90c8482007-02-10 01:11:45 +00004343static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004344 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4345 isdisjoint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004346 {"__reversed__", (PyCFunction)(void(*)(void))dictitems_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004347 reversed_items_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004348 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004349};
4350
4351PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004352 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4353 "dict_items", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004354 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004355 0, /* tp_itemsize */
4356 /* methods */
4357 (destructor)dictview_dealloc, /* tp_dealloc */
4358 0, /* tp_print */
4359 0, /* tp_getattr */
4360 0, /* tp_setattr */
4361 0, /* tp_reserved */
4362 (reprfunc)dictview_repr, /* tp_repr */
4363 &dictviews_as_number, /* tp_as_number */
4364 &dictitems_as_sequence, /* tp_as_sequence */
4365 0, /* tp_as_mapping */
4366 0, /* tp_hash */
4367 0, /* tp_call */
4368 0, /* tp_str */
4369 PyObject_GenericGetAttr, /* tp_getattro */
4370 0, /* tp_setattro */
4371 0, /* tp_as_buffer */
4372 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4373 0, /* tp_doc */
4374 (traverseproc)dictview_traverse, /* tp_traverse */
4375 0, /* tp_clear */
4376 dictview_richcompare, /* tp_richcompare */
4377 0, /* tp_weaklistoffset */
4378 (getiterfunc)dictitems_iter, /* tp_iter */
4379 0, /* tp_iternext */
4380 dictitems_methods, /* tp_methods */
4381 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004382};
4383
4384static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304385dictitems_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004386{
Eric Snow96c6af92015-05-29 22:21:39 -06004387 return _PyDictView_New(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004388}
4389
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004390static PyObject *
4391dictitems_reversed(_PyDictViewObject *dv)
4392{
4393 if (dv->dv_dict == NULL) {
4394 Py_RETURN_NONE;
4395 }
4396 return dictiter_new(dv->dv_dict, &PyDictRevIterItem_Type);
4397}
4398
Guido van Rossum3ac67412007-02-10 18:55:06 +00004399/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004400
4401static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004402dictvalues_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004403{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004404 if (dv->dv_dict == NULL) {
4405 Py_RETURN_NONE;
4406 }
4407 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004408}
4409
Guido van Rossum83825ac2007-02-10 04:54:19 +00004410static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004411 (lenfunc)dictview_len, /* sq_length */
4412 0, /* sq_concat */
4413 0, /* sq_repeat */
4414 0, /* sq_item */
4415 0, /* sq_slice */
4416 0, /* sq_ass_item */
4417 0, /* sq_ass_slice */
4418 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004419};
4420
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004421static PyObject* dictvalues_reversed(_PyDictViewObject *dv);
4422
4423PyDoc_STRVAR(reversed_values_doc,
4424"Return a reverse iterator over the dict values.");
4425
Guido van Rossumb90c8482007-02-10 01:11:45 +00004426static PyMethodDef dictvalues_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004427 {"__reversed__", (PyCFunction)(void(*)(void))dictvalues_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004428 reversed_values_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004429 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004430};
4431
4432PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004433 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4434 "dict_values", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004435 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004436 0, /* tp_itemsize */
4437 /* methods */
4438 (destructor)dictview_dealloc, /* tp_dealloc */
4439 0, /* tp_print */
4440 0, /* tp_getattr */
4441 0, /* tp_setattr */
4442 0, /* tp_reserved */
4443 (reprfunc)dictview_repr, /* tp_repr */
4444 0, /* tp_as_number */
4445 &dictvalues_as_sequence, /* tp_as_sequence */
4446 0, /* tp_as_mapping */
4447 0, /* tp_hash */
4448 0, /* tp_call */
4449 0, /* tp_str */
4450 PyObject_GenericGetAttr, /* tp_getattro */
4451 0, /* tp_setattro */
4452 0, /* tp_as_buffer */
4453 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4454 0, /* tp_doc */
4455 (traverseproc)dictview_traverse, /* tp_traverse */
4456 0, /* tp_clear */
4457 0, /* tp_richcompare */
4458 0, /* tp_weaklistoffset */
4459 (getiterfunc)dictvalues_iter, /* tp_iter */
4460 0, /* tp_iternext */
4461 dictvalues_methods, /* tp_methods */
4462 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004463};
4464
4465static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304466dictvalues_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004467{
Eric Snow96c6af92015-05-29 22:21:39 -06004468 return _PyDictView_New(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004469}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004470
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004471static PyObject *
4472dictvalues_reversed(_PyDictViewObject *dv)
4473{
4474 if (dv->dv_dict == NULL) {
4475 Py_RETURN_NONE;
4476 }
4477 return dictiter_new(dv->dv_dict, &PyDictRevIterValue_Type);
4478}
4479
4480
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004481/* Returns NULL if cannot allocate a new PyDictKeysObject,
4482 but does not set an error */
4483PyDictKeysObject *
4484_PyDict_NewKeysForClass(void)
4485{
Victor Stinner742da042016-09-07 17:40:12 -07004486 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004487 if (keys == NULL)
4488 PyErr_Clear();
4489 else
4490 keys->dk_lookup = lookdict_split;
4491 return keys;
4492}
4493
4494#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
4495
4496PyObject *
4497PyObject_GenericGetDict(PyObject *obj, void *context)
4498{
4499 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
4500 if (dictptr == NULL) {
4501 PyErr_SetString(PyExc_AttributeError,
4502 "This object has no __dict__");
4503 return NULL;
4504 }
4505 dict = *dictptr;
4506 if (dict == NULL) {
4507 PyTypeObject *tp = Py_TYPE(obj);
4508 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
INADA Naokia7576492018-11-14 18:39:27 +09004509 dictkeys_incref(CACHED_KEYS(tp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004510 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
4511 }
4512 else {
4513 *dictptr = dict = PyDict_New();
4514 }
4515 }
4516 Py_XINCREF(dict);
4517 return dict;
4518}
4519
4520int
4521_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
Victor Stinner742da042016-09-07 17:40:12 -07004522 PyObject *key, PyObject *value)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004523{
4524 PyObject *dict;
4525 int res;
4526 PyDictKeysObject *cached;
4527
4528 assert(dictptr != NULL);
4529 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
4530 assert(dictptr != NULL);
4531 dict = *dictptr;
4532 if (dict == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +09004533 dictkeys_incref(cached);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004534 dict = new_dict_with_shared_keys(cached);
4535 if (dict == NULL)
4536 return -1;
4537 *dictptr = dict;
4538 }
4539 if (value == NULL) {
4540 res = PyDict_DelItem(dict, key);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004541 // Since key sharing dict doesn't allow deletion, PyDict_DelItem()
4542 // always converts dict to combined form.
4543 if ((cached = CACHED_KEYS(tp)) != NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004544 CACHED_KEYS(tp) = NULL;
INADA Naokia7576492018-11-14 18:39:27 +09004545 dictkeys_decref(cached);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004546 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01004547 }
4548 else {
INADA Naoki2294f3a2017-02-12 13:51:30 +09004549 int was_shared = (cached == ((PyDictObject *)dict)->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004550 res = PyDict_SetItem(dict, key, value);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004551 if (was_shared &&
4552 (cached = CACHED_KEYS(tp)) != NULL &&
4553 cached != ((PyDictObject *)dict)->ma_keys) {
Victor Stinner3d3f2642016-12-15 17:21:23 +01004554 /* PyDict_SetItem() may call dictresize and convert split table
4555 * into combined table. In such case, convert it to split
4556 * table again and update type's shared key only when this is
4557 * the only dict sharing key with the type.
4558 *
4559 * This is to allow using shared key in class like this:
4560 *
4561 * class C:
4562 * def __init__(self):
4563 * # one dict resize happens
4564 * self.a, self.b, self.c = 1, 2, 3
4565 * self.d, self.e, self.f = 4, 5, 6
4566 * a = C()
4567 */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004568 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004569 CACHED_KEYS(tp) = make_keys_shared(dict);
Victor Stinner742da042016-09-07 17:40:12 -07004570 }
4571 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004572 CACHED_KEYS(tp) = NULL;
4573 }
INADA Naokia7576492018-11-14 18:39:27 +09004574 dictkeys_decref(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004575 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
4576 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004577 }
4578 }
4579 } else {
4580 dict = *dictptr;
4581 if (dict == NULL) {
4582 dict = PyDict_New();
4583 if (dict == NULL)
4584 return -1;
4585 *dictptr = dict;
4586 }
4587 if (value == NULL) {
4588 res = PyDict_DelItem(dict, key);
4589 } else {
4590 res = PyDict_SetItem(dict, key, value);
4591 }
4592 }
4593 return res;
4594}
4595
4596void
4597_PyDictKeys_DecRef(PyDictKeysObject *keys)
4598{
INADA Naokia7576492018-11-14 18:39:27 +09004599 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004600}